So, it follows that the property holds for all integers n greater equal to four.

So, one thing we've used here implicitly and quite liberally is that we have

applied reduction steps as equalities to parts of terms.

So, we have used a reduction in our proof of saying left-hand side equals right-hand

side. That works because pure functional

programs don't have side effects, so reducing a term is really equivalent to

rewriting that term. There's no other effect to be taken

account of. That principle is called referential

transparency, It's an important tool for equational

proofs of functional programs. So, let's look at structural induction

now. The principle of structural induction is

analogous to natural induction. The idea is that we want to prove a

property, P, for all lists, xs. And to do that, we just need to show that

P of the empty list holds, That would be the base case.

And that second, for any list xs and any element x, we show the induction step

which says, assume P of the list xs holds, then you need to show that P of x followed

by the list xs also holds. So, instead of constructing numbers

starting from a base case and adding one, we construct lists starting from the empty

list and consing elements to the top of the list.

So, back to our comcat example. We'd like to show that, for any lists xs,

ys, zs, concatenation is associative. That means, we can put the parentheses

here to the left or to the right. To do this, we use a structure induction

on the list xs. From the previous implementation of concat

that you see here, we can distill two defining clauses for plus, plus.

We can say, well, if the first element xs is Nil and is followed by a list ys, then

we get ys here. And the second clause would say, well, if

the first list is x followed by xs1, and then ys, then the answer would be x

followed by the concatenation of xs1 and ys.

So, these two clauses here, they're directly derived from the implementation.

Essentially, they codify what the implementation does.

So, lets now look at the structure induction.

The best case would be that our list xs equals Nil.

So here, we would have, for the left-hand side, of our equation Nil, followed by ys,

followed by zs. And that can simp, can be simplified to just ys, followed by zs.

Why? Because of the first clause of plus, plus which says, Nil, followed by ys is

ys. Let's look at the right-hand side of the

equation, there we would have Nil follow up by, the parenthesis to the right, ys

and zs. But, of course, again, we can evoke the

first clause of plus, plus and simplify to ys plus za.

So, we have an equality here, and here, and the cases is established.

So, let's now turn to the induction step. As the left most list, we would have x

followed by xs, and then ys, and then zs, and the left-hand side has all parenthesis

going to the left. How can we simplify that?

Well, one thing we can do is, we can pull the x out of the parenthasis with the xs.

So now, xs would go with ys, And the x would be outside of that list.

That we can do by the second clause of concat which says, concatinating a list

that starts with x is a list that starts with x and contanition of the rest of the

list. So, it was the second clause of concat

that said that. Once we have that, what's the next step?

Well, x is still within another head list, so we can involved the second clause of

concat again. And now, we would have x leading the whole

result of the concatination. So, what's the next step?

Well, the next step would be that, look here, what you see is xs followed by ys,

followed by zs. That's actually the left-hand side of our

induction hypothesis which says, assume you have proven the equation already for

the list xs. So, we can invoke the induction hypothesis

and rewrite this expression here to the expression on the right-hand side.

So, parenthesis here, now go to the right. And that's all we're going to do with the

left-hand side. Let's now turn to the right-hand side.

So here, we would have the parenthesis go to the right, ys and zs, let's

concanatinate it and then we have on the left x followed by xs.

What can we do with that expression? Well, one thing we can do is, again,

involve the second clause of plus, plus to pull out the X from the first list here,

And that gives x followed by xs, followed by ys and zs.

And that is exactly the same as what we simplified the left-hand side to.

So, the case and with it the property is established.

Let's do an exercise. Let's look at the second law for concat,

namely that Nil is the right unit for xs. Can you show by induction that, that law

holds? And how many equations do you need for the

inductive step? Two? Three? Or four?

So, let's see how we would prove this. Let's do the base case first.