Docsity

Haskell Programming: Functions, Polymorphism, and Currying in COSC 3015 - Prof. James L. C, Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 3
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
September 16, 2008
1 Recap
Last time we talked about functions and what they are. In Haskell, you can
write programs which fail to terminate. We wrote: :t infinity = infinity
aOne of the features of Haskell program is polymorphism - an expression can
have many types. So , infinity has all types. Often, we want to show functions
have similar property.
2 Functions
Q:How to tell if functions are equal ? A:Extensionality (pointwise equality)
Definition 1 Extensionality If f , g โˆˆAโ†’B,
f=gif and only if โˆ€x:A.f(x) = g(x)
This is introduced in discrete maths (COSC 2030). For any type we have
three things:
1. constructors
2. destructors
3. type notation
For functions, the constructor is \xโ†’bis a function with one argument
(xhere) and defined by the expression b. The destructor for a function is
function application - write name of the function next to the argument. For
example, (\xโ†’b)a. The type notation is aโ†’b. The notation in Haskell is
\xโˆ’> x :: aโˆ’> a.
2.1 Tuples
The constructor for tuples is โ€()โ€ brackets. The destructors for tuples are fst,snd
and are defined as follows:
1
pf3
pf4

Partial preview of the text

Download Haskell Programming: Functions, Polymorphism, and Currying in COSC 3015 - Prof. James L. C and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 3

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

September 16, 2008

1 Recap

Last time we talked about functions and what they are. In Haskell, you can write programs which fail to terminate. We wrote: :t infinity = infinity a One of the features of Haskell program is polymorphism - an expression can have many types. So , infinity has all types. Often, we want to show functions have similar property.

2 Functions

Q:How to tell if functions are equal? A:Extensionality (pointwise equality)

Definition 1 Extensionality If f, g โˆˆ A โ†’ B, f = g if and only if โˆ€x : A.f (x) = g(x)

This is introduced in discrete maths (COSC 2030). For any type we have three things:

  1. constructors
  2. destructors
  3. type notation

For functions, the constructor is \x โ†’ b is a function with one argument (x here) and defined by the expression b. The destructor for a function is function application - write name of the function next to the argument. For example, (\x โ†’ b)a. The type notation is a โ†’ b. The notation in Haskell is \xโˆ’ > x :: aโˆ’ > a.

2.1 Tuples

The constructor for tuples is โ€()โ€ brackets. The destructors for tuples are fst,snd and are defined as follows:

f st(a, b) = a snd(a, b) = b

The type notation is (a, b). In math, you use cartesian product of the form (a ร— b).

2.2 Bool

The constructors for Bool are T rue, F alse. There are no destructors. And the type notation is Bool.

2.3 List

The constructor for list is [] - Empty list, and โ€::โ€ - called cons. The destructors are head and tail and they have the types: head:: a list -> a tail:: a list -> a

2.4 Historical Note

About 1930, a guy named Alonzo Church invented a notation for a calculus of functions. This was called the lambda calculus. ฮ› ::= x | \x.M | M N varibales abstraction application where M, N are lambda terms constructed by the above grammar. Alan Turing - student of Church - showed that Turing Machine are equivalent to lambda calculus. Churchโ€™s thesis says

Everything that can in principle be computed can be computed by lambda- term Haskell Curry was working on logical systems called combinatory logic - turn out closely related to lambda calculus. Why was everyone working on it? Around 1920โ€™s people were wondering about what can be computed. Curry noted that a propositional logic formula is valid (true) if and only if the type associated with the formula is โ€inhabitedโ€ by the ฮป-term. This is called the Curry-Howard isomorphism. So whatโ€™s the translation? A โ‡’ B = A โ†’ B A โˆง B = (A, B) A = A

2.5 Currying and Uncurrying

Curry noticed the following

((A โˆง B) โ‡’ C) โ‡’ (A โ‡’ (B โ‡’ C)) (Currying) (A โˆง (B โ‡’ C)) โ‡’ ((A โˆง B) โ‡’ C)) (uncurrying)

We can do the translation as follows:

We can define curry in a new way as :

curryf = \x โ†’ \y โ†’ f (x, y) uncurryf = \p โ†’ f (f st p) (snd p) uncurryf = (x, y) โ†’ f x y

We can also do the following in Haskell

addc 5:: Int -> Int addc 5 5 :: Int