Docsity

Lecture 13: Trees in Haskell - Binary Trees and Induction Principle - Prof. James L. Caldw, Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 13
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
October 7, 2008
1 Trees
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 or k (Btree a) (B tree 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 size1
size1 :: Num a => Btree b -> a
1
pf3
pf4
pf5

Partial preview of the text

Download Lecture 13: Trees in Haskell - Binary Trees and Induction Principle - Prof. James L. Caldw and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 13

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

October 7, 2008

1 Trees

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)

  1. ∀x : a.P (Leafx) [base case]
  2. ∀xt, yt : (Btree a).P (xt) ∧ P (yt) ⇒ P (F ork xt yt) [induction case]

We also have extra case for partial elements of the type

  • P (⊥)

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