



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!




Chapter 5 has some really good examples. But, we will move on. And we may move to Higher-Order Perl book. We will start with Chapter 6 on trees. Today’s lecture is a sort of review, since many of the things discussed here are in 2300. A binary tree is defined as: data Btree a = Leaf a | F ork (Btree a) (Btree a)
So, here’s an example of a tree:
Fork (Leaf 1) (Fork (Leaf 2) (Leaf 3))
Main> :t Leaf Leaf :: a -> Btree a
You get case statement for free when you create a datatype. For example, consider the size function
size t = case t of (Leaf _) -> 1 (Fork xt yt) -> size xt + size yt
Main> :t size size :: Num a => Btree b -> a Main>
So, case is like a destructor for the datatype. What’s the other way of writing it?
size1 (Leaf _) = 1 size1 (Fork xt yt) = size1 xt + size1 yt
Main> :t size size1 :: Num a => Btree b -> a
Whenever you have a datatype, you also get a structural induction principle. The induction principle for Btree is
∀x : a.P (Leaf x) ∧ (∀xt, yt : (Btree a).P (xt) ∧ P (yt) ⇒ P (F ork xt yt)) ⇒ ∀t : (Btree a).P (t)
We also have extra case for partial elements of the type
Definition 1. A Btree is finite if and only if size(t) 6 = ⊥.
The book says that any finite path through a syntax tree of a recursive datatype is linear. For example
succ | Zero succ | succ | Zero
Similar is the case with lists.
So, let’s look at different trees - something used in 2300.
data T T ree a = Leaf | N ode 2 a (T T ree a) (T T ree a)
Or we can also have a tree with three sub-trees.
data T T T ree a = Leaf | N ode 3 a (T T T ree a) (T T T ree a) (T T T ree a)
Q: Can we make circular structures? A: No, we can’t. But, we can define Nat as a form of DAG (directed acyclic graph)
dataN at′^ = Zero | SN at′^ | SSN at′
where, SS k = S (S k).
So, what about the (finite) induction principle for T T T ree a.
P (xs) : size xs = (length.f latten xs) P (ys) : size ys = (length.f latten ys)
Show
size(f ork xs ys) = length.f latten(f ork xs ys)
On the left side
size(f ork xs ys) = size xs + size ys = (length.f latten xs) + (length.f latten ys) = (length (f latten xs)) + (length (f latten ys))
On the right
length.f latten (f ork xs ys)
compose = length(f latten (f ork xs ys)) flatten = length(f latten xs++f latten ys) length−append = (length (f latten xs)) + (length (f latten ys))
Note that ”.” is a function composition operator.
Lemma 1 (Length-Append). ∀xs, ys : [a]. | xs++ys |=| xs | + | ys |
We can define a function nodes as:
nodes (Leaf _) = 0 nodes (Fork xs ys) = 1 + nodes xs + nodes ys
Then, we can prove a theorem
Theorem 2. ∀xt : Btree a.size xt = 1 + nodes xt
Proof. By ind. on xt.
P(xt) def = size xt = 1+ nodes xt Again, we have two cases:
Case P(leaf x). size (Leaf x) = 1 + nodes (Leaf x) L.H.S size (Leaf x) = 1 R.H.S. 1+ nodes (Leaf x) = 1 + 0 = 1 so the base case holds.
Case P(Fork xs ys). Assume size xs = 1+ nodes xs size ys = 1 + nodes ys Show size (Fork xs ys) = 1 + nodes xs ys L.H.S = size (Fork xs ys) = 1 + nodes xs + 1 + nodes ys R.H.S 1 + nodes(Fork xs ys) = 1 + 1 + nodes xs + nodes ys