Try   HackMD

How does induction really work? (2018)

Another title of this note could have been

Theorem proving with Isabelle and Idris

(thanks to Samuel Balco for implementing the Isabelle and Idris code)

In this note we will again try to understand more deeply something we already know how to do. Namely how to reason with equations. And again, this understanding will allow us to put on a machine something that used to be the reserve of humans: automated reasoning.

But before we can go there we need to understand mathematical induction better.

Examples of Induction

Induction can come in many different forms:

Numbers

We can show, to give just one example, that the equation

(bb)1+2+n=1/2n(n+1)

holds for all positive integers as follows. If

n=1 then the LHS is 1 (you start and end at 1, so 1 is all you have) and the RHS is 1 as well. If
n=k+1
then

1+2+n=1+2+(k+1)=(1+2+k)+(k+1)=1/2k(k+1)+(k+1)=(1/2k+1)(k+1)=1/2(k+2)(k+1)=1/2n+1n=1/2n(n+1)

Exercise: Go through the equational reasoning above. Can you justify/explain each step?

Remark: Why is it correct to apply the equation we want to prove in the proof of the equation itself? Isn't circular reasoning unsound? Depends some forms of circularity are justifiable, others are not. In the example above, we prove the equation for

n=k+1 by using the equation for
n=k
. This is ok, because, roughly speaking,
k
is smaller than
k+1
. One way to understand this better is by making an analogy with programming. If I define eg the factorial function f by f(n)= if n=0 then 1 else n*f(n-1) I made a circular definition. But is this a definition? Does it actually define a function ? Or, in other words, does f terminate on all inputs? Yes, f does terminate on all inputs because each recursive call f(n-1) calls f with a smaller argument n-1.

Expressions

We defined a class of arithmetic expressions in BNF as

​​​​    num ::= 1 | num +1
​​​​    exp ::= num | exp + exp | exp * exp

This is an inductive definition.

Transitive Closure

We defined the transitive closure of a relation

R as the smallest relation
R+
such that

  • RR+
    and
  • xR+y & yR+z  xR+z

This is an inductive definition.

Equational Reasoning

We added to the exp above some equations. For example,

​​​​X + ( Y + Z ) = ( X + Y ) + Z

and then claimed that for all numbers n and m, the equation

​​​​n + m = m + n

follows already, even without having an equation for commutativity.

What do we mean when we say that an equation follows from some others?

Given a set of equations

E, can we define the set
E
of all equations that follow from
E
?

An equation is in this set

E if you can derive the equation from the equations in
E
using the usual rules of equational reasoning from high-school algebra.

This is an inductive definition.

What is Induction?

Numbers, context-free grammars, transitive closure, equational reasoning

all look different.

Can we explain how they all are instances of the same general phenomenon?

In all four examples, we define a set of elements:

N,
R+
, exp,
E
.

In all cases, the set is infinite, so writing it down with a finite number of rules is doing something clever.

Example: In case of numbers we can write the two rules as

0NnNSnN

Of course, the trick is that the rule on the right has a free variable

n that can be instantiated with any element of
N
.

What is inductive about this definition?

This is the crucial point:

When we say that the two rules above "define

N inductively" or "are an inductive definition of
N
", we are really saying that
N
consists of all elements,and only those, that can be formed according to the rules.

In other words,

N is the smallest set closed under the two rules. [1]

Exercise: What are the rules for the example of arithmetic expressions and for transitive closure? Write them in a form that resembles as much as possible the two rules for the natural numbers.

For a solution see the footnote. [2]

The case of equational reasoning is important for too many reasons to start making a list. We will discuss it in some detail in the next section.

Equational Reasoning

We define inductively the set of all equations

e1e2

that we want to study for expression. We write

n,m, to denote nums and
e
's to denote exps.

1numnnumSnnum

I write now

Sn instead of
n+1
to distinguish the
+1
from addition. And for expressions are given by

nnumnexpe1expe2expe1+e2exp

I omit the rule for multiplication because it is not needed in the following.

As an axiom on expressions we want to have for now only

e1+(e2+e3)(e1+e2)+e3

and we want to show that

n+mm+n
follows already from associativity. Of course, this is only an example. What we are really interested in is to understand what it means for one equation to follow from other equations.

As indicated above, the idea is to inductively define the set of all equations that follow. For this, we need to write out the rules of equational reasoning. They are as follows:

ee (refl) e1e2e2e1 (sym) e1e2e2e3e1e3 (trans) 
and
e1e1e2e2e1+e2e1+e2 (cong) 

To these rules of equational reasoning we want to add, as we said, in our example, the axiom
e1+(e2+e3)(e1+e2)+e3 (assoc) 

To warm up, and to understand how equational reasoning is inductive, let us prove something even simpler then

n+mm+n, namely
1+nn+1

The most important idea to understand in this note is the following.

To say that the equation

 1+nn+1  follows from
 (assoc) 
by equational reasoning is to say that
 1+nn+1 
is an element of the set inductively defined by the rules

 (refl) , (sym) , (trans) , (cong) , (assoc) .

If you have any doubts about this statement, stop and think

How do we show that

1+nn+1 is an element of that set?

How would we do this in high-school algebra style?

If

n=1, then
1+1=1+1
.

If

n=Sk, then
1+Sk=1+(k+1)=(1+k)+1=(k+1)+1=Sk+1

Exercise: Can you justify all the steps in the chain of reasoning above?

Exercise: Write the chain of reasoning out in way that one can see at each step which rule among

 (refl) , (sym) , (trans) , (cong) , (assoc)  is applied in the reasoning.

This last exercise, if done properly, means that we understand in all detail how equational reasoning works. This should mean that we can implement it on a machine.

The next section will look at two programming languages, Isabelle and Idris, in which these proofs can be implemented.

Excursion on Equational Logic

The topic of the current lecture is not logic but the more general phenomenon of induction. Nevertheless, while analysing familiar reasoning with equations from the point of view of induction, we discovered almost all the rules of equational logic. So we may as well give them here.

Equational logic is the part of logic that is only concerned with proving new equations from old. The rules of equational logic are the one we have seen above (we use now

t for "term" where we used
e
for "expression" before)

tt (refl) t1t2t2t1 (sym) t1t2t2t3t1t3 (trans) 
and, for all
n
-ary operations
f
a congruence rule
t1t1tntnf(t1,tn)f(t1,tn) (cong) 

and a rule for substitution of terms into variables that we won't explain now but encounter later again
t1t2t1[xt]t2[xt] (subst) 

Summary of what we learned so far

  • An inductive definition defines a smallest set closed under a finite set of rules
  • This accounts for such different examples as
    • the set of natural numbers
    • the set of programs of a programming language specified in BNF (ie by a context-free grammar)
    • the set of equations that can be derived from a given set of assumptions

Thus we have a set of analogies

mathematics computer science logic
numbers programs theorems

with the sets of numbers/programs/theorems being defined inductively. [3] Of course, there is much more defined inductively, for example we could also define a set of proofs inductively.

So can we compute with programs and theorems as we can compute with numbers?

Arithmetic Expressions in Isabelle and Idris

See the next lecture


  1. You could say that

    0N does not look like a rule, because it does not have premises. I'd rather say that it has an empty set of premises. ↩︎

  2. The context-free grammar

    ​​​​​​​​num ::= 1 | num +1
    ​​​​​​​​exp ::= num | exp + exp | exp * exp
    

    can be written as a set of rules as follows.

    1numnnumn+1num   nnumnexpe1expe2expe1+e2expe1expe2expe1e2exp
    The mathematical definition of transitive closure

    • RR+
      and
    • xR+y & yR+z  xR+z

    can be written as a set of rules as

    (x,y)R(x,y)R+(x,y)R(y,z)R+(y,z)R+ ↩︎

  3. As often in research, what starts out as a curious analogy can be turned into an important research programme. So let us take, for a moment, the analogy program vs theorem seriously. If programs are like theorems, then parsing is like proving. This is the basic idea of two papers by Lambek (1958, 1961) that spawned whole research areas in both linguistics and in logic. ↩︎