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

• Write down whatever definitions you will use. This includes
• The inductive rules that define the infinite set.
• Any function definitions over that set
• Any other assumption you will make in your proof.
• Write down the formula you are trying to prove, both as a sentence and as a precise mathematical formula.
• State clearly what variable you are doing induction over. If there is only one variable, this is easy, but if there are more than one, look for variables which are in inductive positions in the function definitions mentioned in the formula you are proving.

It is best to make a formula of the the induction variable. For example:

P(x) ==> x+(y+z) = (x+y)+z

Note that we have chosen "x" as the induction variable by making it an argument of "P".

• Write down all the cases, be sure to include the induction hypotheses, if a case has induction hypotheses. This is easy to do usimng the formula "P". For example over the natural numbers we have 2 cases.
• P(Zero)
• P(n) => p(Succ n)
Note how the induction hypotheses become implications.
• Then write down all the steps for each case. Justify each step.

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.
• It is the empty list: []
• It was formed by consing an element x onto an existing list xs, so it looks like: (x : xs )
• Nothing else is a list.
```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
• It is the empty string: ""
• It was formed by adding a character c onto an existing string on the left y, so it looks like: (c y)
• Nothing else is a String.

Character on the Right

First note that every string has only one of two forms
• It is the empty string: ""
• It was formed by adding a character c onto an existing string on the right y, so it looks like: (y c)
• Nothing else is a String.
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 empty string "" is in the language of balanced parentheses
• If "x" is in the language of balanced parentheses, then so is "(x)"
• If "x" and "y" are in the the language of balanced parentheses, then so is "xy"
• Nothing else is in the language of balanced parenthesis.

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.

• The facts. We have lots of facts for this proof.
First the inductive defintion of the infinite set.
• The empty string "" is in the language of balanced parentheses
• If "x" is in the language of balanced parentheses, then so is "(x)"
• If "x" and "y" are in the the language of balanced parentheses, then so is "xy"
• Nothing else is in the language of balanced parenthesis.

Then the function definitions
```(1)  leftCount "" = 0
(2)  leftCount (x) = 1 + leftCount x
(3)  leftCount xy = leftCount x + leftCount y

(4)  length "" = 0
(5)  length (x) = 2 + length x
(6)  length xy = length x + length y
```

• (7) 0 + x = x
• (8) x+(y+z) = (x+y)+z
• (9) x+y = y+x

• What we are proving.
In English: The length of a string is twice its leftCount
In formal mathematical notation. length x = leftCount x + leftCount x
As a formula of the induction variable: P{x} ==> length x = leftCount x + leftCount x

• The cases of the proof
• case 1. P{""}
length "" = LeftCount "" + leftCount ""
Simplfiying both sides of the equality in each step, we get.
length "" = LeftCount "" + leftCount "" using facts (1) and (4)
0 = 0 + 0 using fact (7)
0 = 0

• case 2. P{x} => P{(x)}
(11) assume: length x = leftCount x + leftCount x
prove: length (x) = leftCount (x) + leftCount (x) using fact (2) and (5)
2 + length x = (1 + leftCount x) + (1 + leftCount x) using facts (8) and (9) many times
2 + length x = 2 + (leftCount x + leftCount y) using the assumption (11), the inductive hypothesis.
2 + (leftCount x + leftCount y) = 2 + (leftCount x + leftCount x) equal things are equal!
TRUE

• case 3. P{x} and P{y} => P{xy}
(12) assume: length x = leftCount x + leftCount x
(13) assume: length y = leftCount y + leftCount y
prove: length xy = leftCount xy + leftCount xy
length x + length y = (leftCount x + leftCount y) + (leftCount x + leftCount y) using facts (8) and (9) many times
length x + length y = (leftCount x + leftCount x) + (leftCount y + leftCount y) using facts (12) and (13)
(leftCount x + leftCount x) + (leftCount y + leftCount y)
TRUE