



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




We will have a midterm exam on Thursday. The exam will be closed book. HW1 was a warm up and we looked at Qsort and had you analyze some code. Something like that might not be a bad question. HW3 was like beginning of the real functional programming.
\xโ > e where e is a haskell expression f x y = e
is same as \f x โ \y โ e
is same as f = \x โ \y โ e
For example, f xy = x + y f x = \y โ x + y
is same as f = \x โ \y โ x + y Given, plusc x y = x + y
:t plusc 5 Num a => a -> a
curry :: ((a, b) โ c) โ (a โ (b โ c)) curry f x y = f (x, y)
uncurry :: (a โ b โ c) โ ((a, b) โ c) uncurry f (x, y) = f x y
Extensionality - equality between functions. There is no general algorithm to decide when two functions are equal. Itโs equivalent to halting problem but you can look at individual programs and see if it halts. So same is the case with equality on functions. If f, g : a โ b then
f = g def = โx : a.f x = gx
Function composition f โฆ g = f (g x)
So what is the type of function composition (โฆ) :: (b โ c) โ (a โ b) โ a โ c
In the HW, we had to prove that curry plus = plusc
curryplus :: Int โ (Int โ Int) plusc :: Int โ (Int โ Int)
Q: How do we show that the two are equal? A: Use extensionality.
Proof. Choose arb. x โ Int and show curry plus x = plusc x
Choose an arb. y โ Int and show curry plus x y = plusc x y
On the left, curry plusc x y = plus (x, y) = x + y On the right, plusc x y = plus (x, y) = x + y
Note that we had to apply extensionality twice since we have a function of two arguments. If we apply extensionality once, we are still showing two functions are equal. Note that we cannot choose arbitrarily any y but something different from x. And in the HW I asked you to write flip. f lip :: (a โ b โ c) โ b โ a โ c f lip x y = f y x
data N at = Zero | Succ N at (+) :: N at โ N at โ N at m + zero = m m + (Succ k) = Succ (m + k)
To show that some property P holds for all finite nats (i.e. not involving โฅ):
To show for all Nats
Q: What type of induction is that? A: It is really ordinarily mathematical induction but in fact is structural induc- tion - more general in the same way.
There will be probably a question on induction principle.
In Haskell, lists are defined as data [a] = [] | (::) a [a]
Or alternatively, data List a = N il | Cons a (List a)
In the HW the lists are defined as : data Liste a = N il | Snoc (Liste a) a
It might be interesting to see what is induction principle for Lists formed from snoc? convert :: (Listea) โ [a]
Given type and desired behavior, define the function.
Implement finite functions using lists and instantiate the Eq type class for this data type so that f == g returns true iff f and g are functional, and they really are equal.
For finite functions, we can do this kind of thing but not for infinite functions. If we use equality on Lists, then the finite function equality would be slightly different. F inF un[(1, 2)(2, 1)] == F inF un[(2, 1), (1, 2)]
Lists are defined as: data List a = N il | Cons a (List a)
Structural Induction on lists (P (N il) โง โx : a, โm : List a. P (m) โ P (Cons a m)) โ โl : List a. P (l)
This can also be written as:
A binary tree is defined as: data Btree a = Leaf a | F ork (Btree a) (Btree a)
data Btree a = Leaf a | F ork (Btree a) (Btree a)
(โx : a, P (Leaf x) โง โt 1 , t2 : (Btree a), P (t1) โง P (t2) โ P (F ork t 1 t2)) โ โt : (Btree a), P (t)
data (Ord a) => Stree a = N ull|F ork (Stree a) a (Stree a)
So what does the induction principle look like? (P (N ull) โง โx : a, โt 1 , t2 : (Stree a), P (t1) โง P (t2) โ P (F ork t 1 x t2)) โ โt : Stree a, P (t).
So what about map? map f N ull = N ull map f (f ork t 1 x t2) = F ork (map f t1) (f x) (map f t2)