Natural Numbers Lists Trees
Structural Induction with Haskell
Dr. Liam O’Connor
University of Edinburgh LFCS UNSW, Term 3 2020
1
Natural Numbers Lists Trees
Definition
Recap: Induction
Let P(x) be a predicate on natural numbers x ∈ N. To show ∀x ∈ N. P(x), we can use induction:
2
Natural Numbers Lists Trees
Recap: Induction
Definition
Let P(x) be a predicate on natural numbers x ∈ N. To show ∀x ∈ N. P(x), we can use induction:
Show P(0)
3
Natural Numbers Lists Trees
Recap: Induction
Definition
Let P(x) be a predicate on natural numbers x ∈ N. To show ∀x ∈ N. P(x), we can use induction:
Show P(0)
Assuming P(k) (the inductive hypothesis), show P(k + 1).
4
Natural Numbers Lists Trees
Recap: Induction
Definition
Let P(x) be a predicate on natural numbers x ∈ N. To show ∀x ∈ N. P(x), we can use induction:
Show P(0)
Assuming P(k) (the inductive hypothesis), show P(k + 1).
Example (Sum of Integers)
Write a recursive function sumTo to sum up all integers from 0 to the input n.
5
Natural Numbers Lists Trees
Recap: Induction
Definition
Let P(x) be a predicate on natural numbers x ∈ N. To show ∀x ∈ N. P(x), we can use induction:
Show P(0)
Assuming P(k) (the inductive hypothesis), show P(k + 1).
Example (Sum of Integers)
Write a recursive function sumTo to sum up all integers from 0 to the input n. Show that:
∀n ∈ N. sumTo n = n(n + 1) 2
6
Natural Numbers Lists Trees
Haskell Data Types
We can define natural numbers as a Haskell data type, reflecting this inductive structure.
data Nat = Z | S Nat
7
Natural Numbers Lists Trees
Haskell Data Types
We can define natural numbers as a Haskell data type, reflecting this inductive structure.
data Nat = Z | S Nat
Example
Define addition, prove that ∀n. n + Z = n.
8
Natural Numbers Lists Trees
Haskell Data Types
We can define natural numbers as a Haskell data type, reflecting this inductive structure.
data Nat = Z | S Nat
Example
Define addition, prove that ∀n. n + Z = n.
Inductive Structure
Observe that the non-recursive constructors correspond to base cases and the recursive constructors correspond to inductive cases
9
Natural Numbers Lists Trees
Lists
Lists are singly-linked lists in Haskell. The empty list is written as [] and a list node is written as x : xs. The value x is called the head and the rest of the list xs is called the tail. Thus:
“hi!” == [‘h’, ‘i’, ‘!’] == ‘h’:(‘i’:(‘!’:[]))
== ‘h’ : ‘i’ : ‘!’ : []
10
Natural Numbers Lists Trees
Lists
Lists are singly-linked lists in Haskell. The empty list is written as [] and a list node is written as x : xs. The value x is called the head and the rest of the list xs is called the tail. Thus:
“hi!” == [‘h’, ‘i’, ‘!’] == ‘h’:(‘i’:(‘!’:[]))
== ‘h’ : ‘i’ : ‘!’ : []
When we define recursive functions on lists, we use the last form for pattern matching.
Example
(Re)-define the functions length, take and drop.
11
Natural Numbers Lists Trees
Induction on Lists
If lists weren’t already defined in the standard library, we could define them ourselves:
data List a = Nil | Cons a (List a)
12
Natural Numbers Lists Trees
Induction on Lists
If lists weren’t already defined in the standard library, we could define them ourselves:
data List a = Nil | Cons a (List a)
Induction
If we want to prove that a proposition holds for all lists:
It suffices to:
∀xs. P(xs)
13
Natural Numbers Lists Trees
Induction on Lists
If lists weren’t already defined in the standard library, we could define them ourselves:
data List a = Nil | Cons a (List a)
Induction
If we want to prove that a proposition holds for all lists:
∀xs. P(xs) 1 Show P([]) (the base case from nil)
It suffices to:
14
Natural Numbers Lists Trees
Induction on Lists
If lists weren’t already defined in the standard library, we could define them ourselves:
data List a = Nil | Cons a (List a)
Induction
If we want to prove that a proposition holds for all lists:
∀xs. P(xs)
1 Show P([]) (the base case from nil)
2 Assuming the inductive hypothesis P(xs), show P(x:xs) (the inductive case from cons).
It suffices to:
15
Natural Numbers Lists Trees
Induction on Lists
Example (Take and Drop)
Show that take (length xs) xs = xs for all xs.
16
Natural Numbers Lists Trees
Induction on Lists
Example (Take and Drop)
Show that take (length xs) xs = xs for all xs. Show that take 5 xs ++ drop 5 xs = xs for all xs.
17
Natural Numbers Lists Trees
Induction on Lists
Example (Take and Drop)
Show that take (length xs) xs = xs for all xs.
Show that take 5 xs ++ drop 5 xs = xs for all xs. =⇒ Sometimes we must generalise the proof goal.
18
Natural Numbers Lists Trees
Induction on Lists
Example (Take and Drop)
Show that take (length xs) xs = xs for all xs.
Show that take 5 xs ++ drop 5 xs = xs for all xs. =⇒ Sometimes we must generalise the proof goal. =⇒ Sometimes we must prove auxiliary lemmas.
19
Natural Numbers Lists Trees
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
20
Natural Numbers Lists
Trees
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
Induction Principle
To prove a property P(t) for all trees t: Prove the base case P(Leaf ).
21
Natural Numbers Lists
Trees
Induction Principle
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
To prove a property P(t) for all trees t: Prove the base case P(Leaf ).
Assuming the two inductive hypotheses:
22
Natural Numbers Lists
Trees
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
Induction Principle
To prove a property P(t) for all trees t: Prove the base case P(Leaf ).
Assuming the two inductive hypotheses:
P(l) and P(r)
23
Natural Numbers Lists
Trees
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
Induction Principle
To prove a property P(t) for all trees t: Prove the base case P(Leaf ).
Assuming the two inductive hypotheses:
P(l) and P(r)
We must show P(Branch x l r).
24
Natural Numbers Lists
Trees
Binary Trees
data Tree a = Leaf
| Branch a (Tree a) (Tree a)
Induction Principle
To prove a property P(t) for all trees t: Prove the base case P(Leaf ).
Assuming the two inductive hypotheses:
P(l) and P(r)
We must show P(Branch x l r).
Example (Tree functions)
Define leaves and height, and show ∀t. height t < leaves t
25
Natural Numbers Lists Trees
Rose Trees
data Forest a = Empty | Cons (Rose a) (Forest a)
data Rose a = Node a (Forest a) Note that Forest and Rose are defined mutually.
26
Natural Numbers Lists Trees
Rose Trees
data Forest a = Empty | Cons (Rose a) (Forest a)
data Rose a = Node a (Forest a) Note that Forest and Rose are defined mutually.
Example (Rose tree functions)
Define size and height, and try to show
∀t. height t ≤ size t
27
Natural Numbers Lists Trees
Simultaneous Induction
To prove a property about two types defined mutually, we have to prove two properties simultaneously.
data Forest a = Empty | Cons (Rose a) (Forest a) data Rose a = Node a (Forest a)
Inductive Principle
To prove a property P(t) about all Rose trees t and a property Q(ts) about all Forests ts simultaneously:
28
Natural Numbers Lists Trees
Simultaneous Induction
To prove a property about two types defined mutually, we have to prove two properties simultaneously.
data Forest a = Empty | Cons (Rose a) (Forest a) data Rose a = Node a (Forest a)
Inductive Principle
To prove a property P(t) about all Rose trees t and a property Q(ts) about all Forests ts simultaneously:
Prove Q(Empty)
29
Natural Numbers Lists Trees
Simultaneous Induction
To prove a property about two types defined mutually, we have to prove two properties simultaneously.
data Forest a = Empty | Cons (Rose a) (Forest a) data Rose a = Node a (Forest a)
Inductive Principle
To prove a property P(t) about all Rose trees t and a property Q(ts) about all Forests ts simultaneously:
Prove Q(Empty)
Assuming P(t) and Q(ts) (inductive hypotheses), show Q(Cons t ts).
30
Natural Numbers Lists Trees
Simultaneous Induction
To prove a property about two types defined mutually, we have to prove two properties simultaneously.
data Forest a = Empty | Cons (Rose a) (Forest a) data Rose a = Node a (Forest a)
Inductive Principle
To prove a property P(t) about all Rose trees t and a property Q(ts) about all Forests ts simultaneously:
Prove Q(Empty)
Assuming P(t) and Q(ts) (inductive hypotheses), show Q(Cons t ts). Assuming Q(ts) (inductive hypothesis), show P(Node x ts).
31