Remember when you prove things by induction, you are proving something over elements in an infinite set. That set is always defined by some set of inductive rules.

First thing to do is identify what infinite set, and identify the inductive rules that define it.

When you write down your proof, it is best to follow some rules about its organization. Here are a few rules to follow


Here is an example of a structural induction over lists. First note that every list has only one of two forms. These rules inductively define the infinite set of lists.
Prove:  map id x = id x.

Induction variable is: x.

Formula: P(x) ==>   map id x = id x

-- List all the definitions and equations you use.
-- label each with a name

1 map f [] = []
2 map f (x:xs) = (f x) : (map f xs)

3 id x = x

-- Set up the structure of the proof
Prove 2 things

1) P([])
   map id [] = id []

2) P(zs) => P(z:zs)
   Assuming:    map id zs = id zs
   Prove:       map id (z:zs)  = id (z:zs)

-- label each step with the label of the definition
-- or equation that justifes it.

1) map id [] = id []
Proceed by transforming LHS into RHS with meaning preserving steps

 map id [] = []        -->    (By 1)
 [] = id []            -->    (By 3, backwards)
                QED

2) Assuming:    map id zs = id zs
   Prove:       map id (z:zs)  = id (z:zs)

   Proceed by transforming LHS into RHS with meaning preserving steps

map id (z:zs) = (id z): (map id zs)    --> (By 2)
(id z):(map id z) = id z:(id zs)       --> (By induction hyp)
id z:(id zs) = z : zs                  --> (By 3, used twice)
z:zs = id (z:zs)                       --> (By 3 used backwards)


Proofs about strings have a lot in common with proofs about lists (they are both finite sequences). When we prove things about strings we sometimes have a choice about how we break the string into parts. This choice is reflected in the inductive rules that define the infinite set of strings.

Character on the Left

First note that every string has only one of two forms

Character on the Right

First note that every string has only one of two forms Both of these are legitimate inductive definitions of the set of strings. Sometimes one works better than the other when proving something about functions over strings.

Example: Prove that string concatenation is associatve. This proof uses the Character on the left inductive definition of the infinite set of strings.

Prove:  xs++(ys++zs) = (xs++ys)++zs
Where ++ is string concatenation.

-- List all the definitions and equations you use.
-- label each with a name. We use the convention that
-- two character names (ending in 's') to name strings, and one
-- character names to name charaters.

1)  "" ++ ws = ws
2) (x ys) ++ ws = x (ys ++ ws)

Prove:  x++(y++z) = (x++y)++z
By induction on: x
Formula:  P(x) = x++(y++z) = (x++y)++z

-- Set up the structure of the proof
Prove 2 things

A) P("")  = "" ++(ys++zs) = ("" ++ys)++zs

B) P(ws) => P(w ws)

Assume: ws++(ys++zs) = (ws++ys)++zs
Prove:  (w ws)++(ys++zs) = ((w ws)++ys)++zs

Step A
"" ++(ys++zs) =  By rule (1)
(ys++ zs)     =  By rule (1) backwards on ys
("" ++ys)++zs

Step B
(w ws)++(ys++zs)   =   By rule (2)
w (ws++(ys++zs))   = By assumption
w ((ws++ys)++zs)   = By rule (2) backwards on (ws+zs)
((w (ws++ys))++zs) = By rule (2) backwards on ws
((w ws)++ys)++zs



The infinite set of Strings of balanced parenthesis can be defined inductively as follows.

The following strings are examples

Be sure you understand how each of these was added to the language of balanced parentheses.

Here are two functions over the language of balanced parentheses. Note how the functions are defined by cases over how the infinite set is formed.

leftCount "" = 0
leftCount (x) = 1 + leftCount x
leftCount xy = leftCount x + leftCount y

length "" = 0
length (x) = 2 + length x
length xy = length x + length y
Now we want to prove that: length x = leftCount x + leftCount x

Our proof will have three parts.