Ran Wei/ Formal methods/Module 5
中文
Formal Methods with Isabelle · Part I · Foundations

Module 5Induction and recursion

Define functions on numbers, lists and trees by recursion and evaluate them step by step; prove general claims by induction, on paper and in Isabelle; and see why Isabelle’s fun requires a termination proof.

⏱ 240 minYou need: Modules 1–4

In this module you will

  • Define functions on natural numbers, lists and binary trees by recursion, and evaluate them by rewriting
  • Prove a statement for every natural number by induction, naming the base case, the induction step and the induction hypothesis
  • Use strong induction, and structural induction on lists and trees
  • Explain why fun requires a termination proof, and justify termination with a measure
  • Prove properties by induction in Isabelle, and read the base case and the step in the proof state
  • Show with an invariant that a property holds after any number of steps of a loop or a machine
1

Recursive definitions

A loop describes repeated work, which may or may not terminate. A recursive definition describes a function in terms of itself. The definitions in this module call the function on smaller arguments, in a sense that guarantees termination. “The sum 0 + 1 + ⋯ + n is the sum up to n − 1, plus n” is such a definition. Recursive definitions are short and exact, and you can compute with them. They come with a matching proof method, induction, which proves a statement for every one of infinitely many inputs at once.

Recursion and induction are the heart of Isabelle/HOL. Almost every function you define in this series is recursive, and almost every proof about such a function is by induction. This module teaches both, first on paper and then in Isabelle.

Natural numbers, built from zero

Every natural number can be reached from 0 by adding 1 often enough: 3 is 0 + 1 + 1 + 1. Giuseppe Peano made this observation the basis of arithmetic in 1889, and it is the basis of everything in this module.

Definition 5.1Natural numbers

The natural numbers N={0,1,2,… }\N = \{0, 1, 2, \dots\} are built from two constructors: 00 is a natural number, and if nn is a natural number, so is its successor Suc n\mathit{Suc}\ n (read “successor of n”), which is n+1n + 1⁠. Nothing else is a natural number. Different constructions give different numbers: 0≠Suc n0 \neq \mathit{Suc}\ n⁠, and Suc m=Suc n\mathit{Suc}\ m = \mathit{Suc}\ n only if m=nm = n⁠.

So every natural number is either 0 or Suc m for exactly one m: 3 is Suc (Suc (Suc 0)). Isabelle’s type nat is built in exactly this way. It prints numbers as numerals, but 3 and Suc (Suc (Suc 0)) are the same value:

Isabelle
value "Suc (Suc (Suc 0))"
Output
"3"
  :: "nat"

That every number is either 0 or a successor is what makes recursive definitions work. To define a function on all of N\N⁠, it is enough to say what it does at 0, and what it does at n+1n + 1 in terms of what it does at nn⁠.

Four functions on numbers

Here is the sum 0+1+⋯+n0 + 1 + \dots + n as a recursive definition. Call it sum_upto\mathit{sum\_upto}⁠:

sum_upto(0)=0sum_upto(n+1)=sum_upto(n)+(n+1)\begin{aligned} \mathit{sum\_upto}(0) &= 0\\ \mathit{sum\_upto}(n+1) &= \mathit{sum\_upto}(n) + (n+1) \end{aligned}

The first equation is the base case. The second, the recursive case, defines the sum up to n+1n + 1 by the sum up to nn⁠, which is a smaller instance of the same problem. Since every number is either 0 or n+1n + 1 for some nn⁠, the two equations cover every number, each exactly once.

Definition 5.2Recursive definition

A recursive definition uses equations in which the function may also appear on the right-hand side. For the total functions defined here, the equations cover every argument, agree wherever they overlap, and recursive calls decrease in a well-founded relation. The base cases do not call the function; the recursive cases do.

Three more functions follow the same pattern: the factorial n!=1⋅2⋅…⋅nn! = 1 \cdot 2 \cdot \ldots \cdot n⁠, called fac\mathit{fac} here; the power xnx^n⁠, called pow\mathit{pow}⁠; and the Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, …, in which each number is the sum of the two before it.

Function Base cases Recursive case
sum 0+1+⋯+n0 + 1 + \dots + n sum_upto(0)=0\mathit{sum\_upto}(0) = 0 sum_upto(n+1)=sum_upto(n)+(n+1)\mathit{sum\_upto}(n+1) = \mathit{sum\_upto}(n) + (n+1)
factorial n!n! fac(0)=1\mathit{fac}(0) = 1 fac(n+1)=fac(n)⋅(n+1)\mathit{fac}(n+1) = \mathit{fac}(n) \cdot (n+1)
power xnx^n pow(x,0)=1\mathit{pow}(x, 0) = 1 pow(x,n+1)=pow(x,n)⋅x\mathit{pow}(x, n+1) = \mathit{pow}(x, n) \cdot x
Fibonacci fib(0)=0\mathit{fib}(0) = 0⁠, fib(1)=1\mathit{fib}(1) = 1 fib(n+2)=fib(n)+fib(n+1)\mathit{fib}(n+2) = \mathit{fib}(n) + \mathit{fib}(n+1)

pow\mathit{pow} has two arguments, and the recursion is on the second one, the exponent; xx stays the same in every call. fib\mathit{fib} needs two base cases, because its recursive case looks two steps back.

Check it in Isabelle

In Isabelle, fun defines a function by such equations, separated by |. The patterns use Suc n where the paper uses n+1n + 1⁠:

Isabelle
fun sum_upto :: "nat ⇒ nat" where
  "sum_upto 0 = 0"
| "sum_upto (Suc n) = sum_upto n + Suc n"
Output
consts
  sum_upto :: "nat ⇒ nat"

Found termination order: "size <*mlex*> {}"

Isabelle reports the new constant and its type, and that it has found a “termination order”; Section 5.7 explains what that means. The other three functions:

Isabelle
fun fac :: "nat ⇒ nat" where
  "fac 0 = 1"
| "fac (Suc n) = fac n * Suc n"

fun pow :: "nat ⇒ nat ⇒ nat" where
  "pow x 0 = 1"
| "pow x (Suc n) = pow x n * x"

fun fib :: "nat ⇒ nat" where
  "fib 0 = 0"
| "fib (Suc 0) = 1"
| "fib (Suc (Suc n)) = fib n + fib (Suc n)"

The pattern Suc 0 matches only the number 1, and Suc (Suc n) matches every number from 2 on, with n two less. Now compute some values:

Isabelle
value "[sum_upto 3, fac 5, pow 2 10, fib 10]"
Output
"[6, 120, 1024, 55]"
  :: "nat list"

Evaluation is rewriting

How do you compute sum_upto(3)\mathit{sum\_upto}(3) from the equations? Read each equation from left to right as a rewrite rule: wherever a term matches its left-hand side, you may replace it by its right-hand side. Since 3=2+13 = 2 + 1⁠, the recursive case applies with n=2n = 2⁠, and so on down to 0:

sum_upto(3)=sum_upto(2)+3=(sum_upto(1)+2)+3=((sum_upto(0)+1)+2)+3=((0+1)+2)+3=6\begin{aligned} \mathit{sum\_upto}(3) &= \mathit{sum\_upto}(2) + 3\\ &= (\mathit{sum\_upto}(1) + 2) + 3\\ &= ((\mathit{sum\_upto}(0) + 1) + 2) + 3\\ &= ((0 + 1) + 2) + 3\\ &= 6 \end{aligned}

Three steps use the recursive case, one the base case, and the last one does the arithmetic. Every step is justified by an equation, so the chain is also a proof that sum_upto(3)=6\mathit{sum\_upto}(3) = 6⁠.

Definition 5.3Evaluation by rewriting

To evaluate a term by rewriting, repeatedly choose a part of the term that matches the left-hand side of an equation and replace it by the right-hand side, with the variables of the equation replaced by the parts they matched. A term with no applicable rewrite is in normal form for those rules. For the closed expressions and complete definitions here, evaluation reaches a value, such as a number or a list. An expression with free variables may instead stop at a symbolic result; a normal form need not be a concrete value.

Often several parts of a term could be rewritten. This module does what most programming languages do and evaluates the arguments of a call before the call itself (call by value): it always rewrites the leftmost call, or arithmetic operation, whose arguments are already values. For the functions of this module, the order changes the terms you see along the way, but never the final value.

Good to know

For closed executable expressions, Isabelle’s value normally turns the equations into a program and runs it; it does not display the rewrite sequence above. Expressions with free variables can be evaluated symbolically, as in the list example below. The simplifier simp performs proof-producing rewriting: it uses the defining equations of every function defined with fun as rewrite rules, as you will see in Section 5.4.

2

Lists and trees

Numbers are built from 0 and Suc. Lists and trees are built in the same way, from a few constructors, and recursion on them works exactly as on numbers.

Lists

Isabelle’s lists, which you met in Module 1, have two constructors: [] is the empty list, and x # xs (read “x cons xs”) puts an element x in front of a list xs. The list [1, 2, 3] is short for 1 # (2 # (3 # [])), and since # groups to the right you can write it as 1 # 2 # 3 # []. All elements of a list have the same type: nat list is the type of lists of natural numbers, and 'a list (read “alpha list”) the type of lists whose elements have some type 'a.

Definition 5.4Lists

Every list is either the empty list [] or x # xs: a head x in front of a tail xs. Nothing else is a list, and lists built differently are different: x # xs ≠ [], and x # xs = y # ys only if x = y and xs = ys.

Isabelle
value "1 # 2 # [3 :: nat]"
Output
"[1, 2, 3]"
  :: "nat list"

Isabelle prints the result in the short notation. A direct structural definition on lists handles [] and x # xs. Here are four: the length of a list, appending two lists, reversing a list, and the sum of a list of numbers.

Isabelle
fun len :: "'a list ⇒ nat" where
  "len [] = 0"
| "len (x # xs) = len xs + 1"

fun app :: "'a list ⇒ 'a list ⇒ 'a list" where
  "app [] ys = ys"
| "app (x # xs) ys = x # app xs ys"

fun reverse :: "'a list ⇒ 'a list" where
  "reverse [] = []"
| "reverse (x # xs) = app (reverse xs) [x]"

fun lsum :: "nat list ⇒ nat" where
  "lsum [] = 0"
| "lsum (x # xs) = x + lsum xs"

Read the second equation of app as: to put x # xs in front of ys, put xs in front of ys and then x in front of the result. The second equation of reverse says: reverse the tail, then append the head at the end. This module refers to equations by function and number: app.2 is the second equation of app. Isabelle calls it app.simps(2).

The library has all four functions, under the names length, @ (which you met in Module 1), rev and sum_list. The library’s theory List.thy defines @ and rev by exactly these equations, for example [] @ ys = ys and (x # xs) @ ys = x # xs @ ys; length and sum_list are defined differently but satisfy the same equations. You define your own versions here to see how they work, and because Isabelle then has to prove every fact about them from scratch, with your help.

value works with variables as well as numbers:

Isabelle
value "reverse [a, b, c]"
Output
"[c, b, a]"
  :: "'a list"

Binary trees: your first datatype

Lists are one example of a datatype, a type whose values are built from constructors. You can define your own. A binary tree is either empty, or a node that holds an element and two smaller trees, its left and right subtrees:

Isabelle
datatype 'a tree2 = Tip | Node "'a tree2" 'a "'a tree2"

Read it as: a tree with elements of type 'a is either Tip, the empty tree, or Node l x r, with a left subtree l, an element x and a right subtree r. The types of the arguments of Node follow its name; each type that is more than a single name or type variable goes in quotation marks. Isabelle comes with a library theory of trees whose type is called tree (it is not part of Main), so this one is called tree2 to keep the two apart.

Definition 5.5Datatype

A datatype is a type whose values are built from constructors. Every value is built by applying the constructors finitely often, and values built differently are different. The command datatype introduces a new type with its constructors, and Isabelle proves automatically that different constructors give different values and that each constructor is injective.

The tree on the left of Figure 5.1 has four nodes and five tips. Three functions on trees: mirror swaps left and right everywhere, nodes counts the nodes and tips counts the tips.

Isabelle
fun mirror :: "'a tree2 ⇒ 'a tree2" where
  "mirror Tip = Tip"
| "mirror (Node l x r) = Node (mirror r) x (mirror l)"

fun nodes :: "'a tree2 ⇒ nat" where
  "nodes Tip = 0"
| "nodes (Node l x r) = nodes l + nodes r + 1"

fun tips :: "'a tree2 ⇒ nat" where
  "tips Tip = 1"
| "tips (Node l x r) = tips l + tips r"

Each recursive case calls the function twice, once for each subtree. Both subtrees are smaller than the tree, so the recursive calls decrease in structural size. The equations also cover both constructors without conflict.

A tree and its mirror image21432413tmirror tleft and rightswap atevery nodet = Node (Node Tip 1 Tip) 2 (Node (Node Tip 3 Tip) 4 Tip)mirror t = Node (Node Tip 4 (Node Tip 3 Tip)) 2 (Node Tip 1 Tip)= Tip
Figure 5.1

A tree t and its mirror image. Tips are drawn as small squares. mirror swaps the left and right subtrees at every node: the subtree with 4 and 3 moves to the left, and is mirrored itself on the way.

Isabelle
value "mirror (Node (Node Tip 1 Tip) 2 (Node (Node Tip 3 Tip) 4 Tip) :: nat tree2)"
Output
"Node (Node Tip 4 (Node Tip 3 Tip)) 2 (Node Tip 1 Tip)"
  :: "nat tree2"

The annotation :: nat tree2 says that the elements are natural numbers, as :: nat did for numbers in Module 1.

Step through an evaluation

Rewriting works on lists and trees exactly as on numbers. Here is reverse [1, 2], one equation per step, with the equation named on the right:

  reverse [1, 2]
= app (reverse [2]) [1]             by reverse.2
= app (app (reverse []) [2]) [1]    by reverse.2
= app (app [] [2]) [1]              by reverse.1
= app [2] [1]                       by app.1
= 2 # app [] [1]                    by app.2
= 2 # [1]                           by app.1

and 2 # [1] is the list [2, 1]. Notice how reverse builds a pile of calls to app and only then starts to append. The stepper below performs such evaluations for every function of this module.

Try it

Choose a function and its argument, then press Next to rewrite the highlighted part of the term; the equation used lights up in the definition above it. Start with sum_upto and 3, then try reverse with [1, 2, 3] and mirror with the tree of Figure 5.1. Finally choose fib with 5, press Run to end, and read the count of calls. Then try fib with 10.

The count for fib shows a problem. fib 5 calls fib 3 and fib 4; but fib 4 calls fib 3 again, and computes from scratch everything that the first call already computed (Figure 5.2). The number of calls grows like the Fibonacci numbers themselves: computing fib n takes 2⋅fib(n+1)−12 \cdot \mathit{fib}(n+1) - 1 calls, which is 15 calls for fib 5 and more than 2.6 million for fib 30.

The calls made to compute fib 5fib 5fib 3fib 4fib 1fib 2fib 0fib 1fib 2fib 3fib 0fib 1fib 1fib 2fib 0fib 1fib 3: computed 2 timesfib 2: computed 3 timesfib 1: 5 times, fib 0: 3 times15 calls in all
Figure 5.2

The calls made to compute fib 5. Each call of fib on a number of 2 or more makes two further calls. fib 3 is computed twice and fib 2 three times, each time from scratch.

As a definition, fib is fine: it says exactly what the Fibonacci numbers are. As a program it is slow. A program that keeps the last two values computes fib n with a number of additions proportional to n, although the cost of adding the growing integers is not constant. Proving that such a faster program computes the same function as the clear definition is a typical verification task, and Modules 11 and 12 do such proofs.

3

Mathematical induction

value can compute sum_upto(n)\mathit{sum\_upto}(n) for any single nn⁠, and so can a test. But a claim such as 2⋅sum_upto(n)=n⋅(n+1)2 \cdot \mathit{sum\_upto}(n) = n \cdot (n+1) for every nn is about infinitely many numbers, and, as Module 1 showed, no amount of testing covers them all. You need an argument that works for every nn at once. Induction is such an argument.

The principle

Suppose you have shown two things: the claim holds for 0; and whenever it holds for some number nn⁠, it also holds for n+1n + 1⁠. Then it holds for 0, therefore for 1, therefore for 2, and so on. Every natural number is reached from 0 in finitely many steps of +1+1⁠, so the claim reaches every number. Figure 5.3 shows the usual picture: a row of dominoes.

Mathematical induction as falling dominoesP(0)P(1)P(2)P(3)P(4)P(n)P(n+1)P(n+2)pushP(n) ⟶ P(n+1)Base caseProve P(0):the first domino falls.Induction stepFor every n, prove that P(n)implies P(n+1): each dominoknocks over the next one.ConclusionP(n) holds for every n:every domino falls,however far along the row.
Figure 5.3

Induction as a row of dominoes. The base case pushes over the first domino. The induction step says that any domino that falls knocks over the next one. Together they make every domino fall, however far along the row it stands.

Fact 5.1Mathematical induction

Let P(n)P(n) be a statement about a natural number nn⁠. If

  1. base case: P(0)P(0) holds, and
  2. induction step: for every nn⁠, P(n)P(n) implies P(n+1)P(n+1)⁠,

then P(n)P(n) holds for every natural number nn⁠. In the induction step, the assumption P(n)P(n) is called the induction hypothesis, IH for short.

In the application notation of Modules 2 and 3: P 0∧(∀n. P n⟶P (n+1))⟶(∀n. P n)P\ 0 \land (\forall n.\ P\ n \imp P\ (n+1)) \imp (\forall n.\ P\ n)⁠. Read it as “if P of 0, and for all n, P of n implies P of n plus 1, then for all n, P of n”.

Why is this a valid way to prove something? Here is a second argument besides the dominoes. Suppose PP failed for some number. Among the numbers where it fails, take the smallest, mm⁠. It is not 0, because P(0)P(0) holds. So m=k+1m = k + 1 for some kk⁠, and P(k)P(k) holds because k<mk < m and mm was the smallest failure. But then the induction step gives P(k+1)P(k + 1)⁠, that is P(m)P(m)⁠: a contradiction. The argument uses one property of N\N⁠: every non-empty set of natural numbers has a smallest element. In the words of Module 4, the order << on N\N is well-founded: there is no infinite descending chain n0>n1>n2>⋯n_0 > n_1 > n_2 > \cdots⁠. Sections 5.6 and 5.7 build on this.

Induction need not start at 0. To prove P(n)P(n) for every n≥kn \ge k⁠, prove P(k)P(k) and, for every n≥kn \ge k⁠, that P(n)P(n) implies P(n+1)P(n + 1)⁠.

A first proof by induction

Example 5.1The sum formula

Claim. For every natural number nn⁠, 2⋅sum_upto(n)=n⋅(n+1)2 \cdot \mathit{sum\_upto}(n) = n \cdot (n + 1)⁠.

Proof by induction on nn⁠. Let P(n)P(n) be the statement 2⋅sum_upto(n)=n⋅(n+1)2 \cdot \mathit{sum\_upto}(n) = n \cdot (n+1)⁠.

Base case, P(0)P(0)⁠: by the first equation of sum_upto\mathit{sum\_upto}⁠, 2⋅sum_upto(0)=2⋅0=0=0⋅(0+1)2 \cdot \mathit{sum\_upto}(0) = 2 \cdot 0 = 0 = 0 \cdot (0 + 1)⁠.

Induction step. Let nn be any natural number, and assume the IH: 2⋅sum_upto(n)=n⋅(n+1)2 \cdot \mathit{sum\_upto}(n) = n \cdot (n+1)⁠. Show P(n+1)P(n+1)⁠, that is 2⋅sum_upto(n+1)=(n+1)⋅(n+2)2 \cdot \mathit{sum\_upto}(n+1) = (n+1) \cdot (n+2)⁠:

2⋅sum_upto(n+1)=2⋅(sum_upto(n)+(n+1))by sum_upto.2=2⋅sum_upto(n)+2⋅(n+1)arithmetic=n⋅(n+1)+2⋅(n+1)by the IH=(n+1)⋅(n+2)arithmetic\begin{aligned} 2 \cdot \mathit{sum\_upto}(n+1) &= 2 \cdot (\mathit{sum\_upto}(n) + (n+1)) && \text{by sum\_upto.2}\\ &= 2 \cdot \mathit{sum\_upto}(n) + 2 \cdot (n+1) && \text{arithmetic}\\ &= n \cdot (n+1) + 2 \cdot (n+1) && \text{by the IH}\\ &= (n+1) \cdot (n+2) && \text{arithmetic} \end{aligned}

By induction, P(n)P(n) holds for every nn⁠.

Dividing by 2 gives the familiar formula 0+1+⋯+n=n(n+1)/20 + 1 + \dots + n = n(n+1)/2⁠. The claim keeps the factor 2 on the left so that it needs no division, which makes it easier to state exactly and to prove: on natural numbers, division rounds down.

Writing an induction proof

Tip

An induction proof has five parts. Write all of them, every time.

  1. Say which variable you do induction on, and state P(n)P(n) exactly.
  2. Base case: prove P(0)P(0)⁠.
  3. Induction step: take an arbitrary nn and write down the IH, P(n)P(n)⁠.
  4. Write down the goal P(n+1)P(n + 1) and prove it. Mark the step where you use the IH. If you never use it, either the proof is wrong or it did not need induction.
  5. Conclude: by induction, P(n)P(n) holds for every nn⁠.

The second example proves an inequality. Inequalities are a little less mechanical than equations, because you have to decide what to estimate.

Example 5.2Powers of two grow faster than n

Claim. For every natural number nn⁠, n<pow(2,n)n < \mathit{pow}(2, n)⁠.

Proof by induction on nn⁠.

Base case: 0<1=pow(2,0)0 < 1 = \mathit{pow}(2, 0)⁠, by pow.1.

Induction step. Assume the IH, n<pow(2,n)n < \mathit{pow}(2, n)⁠, and show n+1<pow(2,n+1)n + 1 < \mathit{pow}(2, n+1)⁠. On natural numbers, n<pow(2,n)n < \mathit{pow}(2, n) means pow(2,n)≥n+1\mathit{pow}(2, n) \ge n + 1⁠, and in particular pow(2,n)≥1\mathit{pow}(2, n) \ge 1⁠. So

pow(2,n+1)=pow(2,n)⋅2=pow(2,n)+pow(2,n)  ≥  (n+1)+1  >  n+1,\mathit{pow}(2, n+1) = \mathit{pow}(2, n) \cdot 2 = \mathit{pow}(2, n) + \mathit{pow}(2, n) \;\ge\; (n + 1) + 1 \;>\; n + 1,

using pow.2 and then the IH for both summands. By induction, the claim holds for every nn⁠.

Watch out

Both parts are needed. Take P(n)P(n)⁠: “n=n+1n = n + 1”. The induction step holds: if n=n+1n = n + 1⁠, then adding 1 to both sides gives n+1=n+2n + 1 = n + 2⁠. But the base case 0=10 = 1 is false, and so is the claim. A step without a base case proves nothing.

The step must work for every nn⁠, the smallest included. A well-known false proof claims that in every group of n≥1n \ge 1 horses, all horses have the same colour. Base case: one horse has one colour. Step: from a group of n+1n + 1 horses, take out one horse; the remaining nn have the same colour by the IH. Put it back and take out a different horse; again the remaining nn have the same colour. The two groups overlap, so all n+1n + 1 horses have the same colour. The flaw: for n=1n = 1⁠, the two groups contain one horse each and do not overlap. The step fails from 1 to 2, so the dominoes stop after the first.

4

Induction in Isabelle

Isabelle checks induction proofs too. You state the lemma and tell Isabelle to use induction; Isabelle then shows the base case and the induction step as separate goals, which you prove. Reading these goals is the main skill of this section.

Why simp is not enough

Try the sum formula with the simplifier alone:

Isabellefails on purpose
lemma "2 * sum_upto n = n * (n + 1)"
  by simp
Output
Failed to finish proof⌂:
goal (1 subgoal):
 1. 2 * sum_upto n = n + n * n

The simplifier has rewritten n * (n + 1) into n + n * n, but it could not touch sum_upto n. Neither equation of sum_upto matches it: the first needs 0, the second Suc of something, and n is an arbitrary number that the simplifier knows nothing about. Induction splits the proof into exactly these two cases.

Base case and step in the proof state

State the lemma again, give it a name, and ask for induction on n:

Isabelle
lemma sum_formula: "2 * sum_upto n = n * (n + 1)"
  apply (induction n)
Output
proof (prove)
goal (2 subgoals):
 1. 2 * sum_upto 0 = 0 * (0 + 1)
 2. ⋀n. 2 * sum_upto n = n * (n + 1) ⟹
        2 * sum_upto (Suc n) = Suc n * (Suc n + 1)

apply applies a proof method to the goal and shows what is left to prove; the method induction n does induction on n. The one goal has become two subgoals. Subgoal 1 is the base case: the claim with 0 for n. Subgoal 2 is the induction step, and it contains two new symbols:

  • ⋀n. (type \And or !!) means “for an arbitrary n”: in this subgoal, n stands for any number, and nothing is known about it except what the subgoal says.
  • A ⟹ B (type ==>) means “A implies B”: to prove the subgoal, prove B, and you may assume A.

To the left of ⟹ is the IH; to the right, the claim for Suc n, which is n+1n + 1⁠. Side by side with the paper proof of Example 5.1:

Paper proof Isabelle’s subgoals
Base case P(0)P(0) 1. 2 * sum_upto 0 = 0 * (0 + 1)
Take an arbitrary nn 2. ⋀n.
Assume the IH P(n)P(n) 2 * sum_upto n = n * (n + 1) ⟹
Show P(n+1)P(n + 1) 2 * sum_upto (Suc n) = Suc n * (Suc n + 1)

Now prove the subgoals one at a time. simp works on the first subgoal:

Isabelle
  apply simp
Output
proof (prove)
goal (1 subgoal):
 1. ⋀n. 2 * sum_upto n = n * (n + 1) ⟹
        2 * sum_upto (Suc n) = Suc n * (Suc n + 1)

The base case is gone: simp rewrote sum_upto 0 to 0 with the first equation, and both sides to 0. What remains is the step. Apply simp again:

Isabelle
  apply simp
Output
proof (prove)
goal:
No subgoals!

In the step, simp used the second equation of sum_upto, the IH (the simplifier uses the assumptions to the left of ⟹) and arithmetic: the same three things the paper proof used. No subgoals are left, and done ends the proof:

Isabelle
  done

The lemma is now a theorem called sum_formula, which later proofs can use.

Good to know

A proof written as a sequence of apply steps ending in done is an apply script. Module 7 builds proofs step by step in this style, and Module 10 introduces Isar, a proof language that reads more like the paper proofs. For now, apply scripts are the best way to look at the proof state between steps: put the cursor after any line to see the state at that point.

The whole proof in one line

Once you know how a proof goes, you can write it in one line:

Isabelle
lemma "2 * sum_upto n = n * (n + 1)"
  by (induction n) simp_all

lemma pow_gt: "n < pow 2 n"
  by (induction n) auto

by m₁ m₂ applies the method m₁, then m₂, and fails unless no subgoals are left. simp would not do as m₂, because it works only on the first subgoal; simp_all simplifies all subgoals, and auto works on all subgoals too, combining simplification with logical reasoning. For an induction whose base case and step follow from the definitions and the IH, by (induction n) auto is the first thing to try.

If something went wrong
  • Failed to finish proof after by simp or by auto, with sum_upto n (or another recursive function applied to a variable) still in the goal. Induction may expose the defining cases. For the sum formula, try by (induction n) auto; other goals may need a lemma or a different argument.
  • Failed to finish proof after by (induction n) auto. Look at the remaining subgoal. Sections 5.5 and 5.6 show the common causes: induction on the wrong variable, a missing lemma, and an induction hypothesis that is too weak.
  • The proof state does not show. Tick Proof state in the Output panel (Module 1), and put the cursor at the end of the apply line.
5

Structural induction

Induction on natural numbers follows the way numbers are built, from 0 and Suc. Every datatype is built from its constructors in the same way, so every datatype has an induction principle of the same shape, called structural induction.

Induction on lists

Fact 5.2Structural induction on lists

Let P(xs)P(xs) be a statement about a list xsxs⁠. If

  1. P([ ])P([\,]) holds, and
  2. for every xx and xsxs⁠, P(xs)P(xs) implies P(x#xs)P(x \mathbin{\#} xs)⁠,

then P(xs)P(xs) holds for every list xsxs⁠.

This is valid for the same reason as induction on numbers: every list is built from [] by finitely many uses of #. In fact it is induction on the length of the list in disguise. The proofs below write equations in Isabelle’s notation, one step per line, as in the evaluation of reverse [1, 2].

Example 5.3Length of an append

Claim. For all lists xs and ys, len (app xs ys) = len xs + len ys.

Which variable should the induction be on? app takes its first argument apart (app.2) and leaves the second alone. So do induction on xs and keep ys fixed: P(xs)P(xs) is len (app xs ys) = len xs + len ys.

Base case, xs = []:

  len (app [] ys)
= len ys                     by app.1
= 0 + len ys                 arithmetic
= len [] + len ys            by len.1

Induction step. Assume the IH, len (app xs ys) = len xs + len ys, and show the claim for x # xs:

  len (app (x # xs) ys)
= len (x # app xs ys)        by app.2
= len (app xs ys) + 1        by len.2
= len xs + len ys + 1        by the IH
= (len xs + 1) + len ys      arithmetic
= len (x # xs) + len ys      by len.2
Check it in Isabelle

The induction method works on lists in the same way:

Isabelle
lemma len_app: "len (app xs ys) = len xs + len ys"
  apply (induction xs)
Output
proof (prove)
goal (2 subgoals):
 1. len (app [] ys) = len [] + len ys
 2. ⋀a xs.
       len (app xs ys) = len xs + len ys ⟹
       len (app (a # xs) ys) = len (a # xs) + len ys

These are the base case and the step of Example 5.3. Isabelle calls the head of the list a rather than x; the name does not matter, because ⋀a xs makes both arbitrary. auto proves both subgoals:

Isabelle
  apply auto
  done

Induction on the other variable fails:

Isabellefails on purpose
lemma "len (app xs ys) = len xs + len ys"
  by (induction ys) auto
Output
Failed to finish proof⌂:
goal (2 subgoals):
 1. len (app xs []) = len xs
 2. ⋀a ys.
       len (app xs ys) = len xs + len ys ⟹
       len (app xs (a # ys)) = Suc (len xs + len ys)

Every remaining subgoal contains app xs applied to something, and the equations of app cannot rewrite it, because xs is neither [] nor of the form x # xs. The rule of thumb: do induction on the argument that the function takes apart.

Induction on trees

Fact 5.3Structural induction on trees

Let P(t)P(t) be a statement about a tree tt⁠. If

  1. P(Tip)P(\mathit{Tip}) holds, and
  2. for all ll⁠, xx and rr⁠, P(l)P(l) and P(r)P(r) together imply P(Node l x r)P(\mathit{Node}\ l\ x\ r)⁠,

then P(t)P(t) holds for every tree tt⁠.

The step now has two induction hypotheses, one for each subtree, because a node is built from two smaller trees.

Example 5.4Mirroring twice

Claim. For every tree t, mirror (mirror t) = t.

Base case: mirror (mirror Tip) = mirror Tip = Tip, by mirror.1 twice.

Induction step. Assume both IHs, mirror (mirror l) = l and mirror (mirror r) = r:

  mirror (mirror (Node l x r))
= mirror (Node (mirror r) x (mirror l))             by mirror.2
= Node (mirror (mirror l)) x (mirror (mirror r))    by mirror.2
= Node l x r                                        by the two IHs
Check it in Isabelle
Isabelle
lemma mirror_mirror: "mirror (mirror t) = t"
  apply (induction t)
Output
proof (prove)
goal (2 subgoals):
 1. mirror (mirror Tip) = Tip
 2. ⋀t1 x2 t2.
       ⟦mirror (mirror t1) = t1; mirror (mirror t2) = t2⟧
       ⟹ mirror (mirror (Node t1 x2 t2)) = Node t1 x2 t2

The notation ⟦A; B⟧ ⟹ C is short for “A and B imply C”. Both IHs are there, for the subtrees that Isabelle calls t1 and t2.

Isabelle
  apply auto
  done

Structural induction also relates different measures of size. A tree with nn nodes has n+1n + 1 tips, whatever its shape: the tree in Figure 5.1 has 4 nodes and 5 tips. The proof is short. Base case: tips Tip = 1 = nodes Tip + 1. Step:

  tips (Node l x r)
= tips l + tips r                          by tips.2
= (nodes l + 1) + (nodes r + 1)            by the two IHs
= (nodes l + nodes r + 1) + 1              arithmetic
= nodes (Node l x r) + 1                   by nodes.2
Isabelle
lemma tips_nodes: "tips t = nodes t + 1"
  by (induction t) auto

When the induction needs a lemma

Reversing a list twice gives the list back. Try to prove it the usual way:

Isabellefails on purpose
lemma "reverse (reverse xs) = xs"
  by (induction xs) auto
Output
Failed to finish proof⌂:
goal (1 subgoal):
 1. ⋀a xs.
       reverse (reverse xs) = xs ⟹ reverse (app (reverse xs) [a]) = a # xs

The base case went through, the step did not. auto unfolded reverse (reverse (a # xs)) to reverse (app (reverse xs) [a]) and got stuck: the IH speaks about reverse (reverse xs), but the goal applies reverse to an app, and no equation says how to do that. Rewriting with the definitions cannot help; a fact about reverse and app is missing. The stuck goal tells you what to look for: how does reverse act on an app? Reversing two appended lists reverses both parts and swaps them.

Example 5.5Reverse of an append

The proof needs two simple facts about app. Both are proved by induction on xs:

  • Lemma A, app xs [] = xs. Base case: app [] [] = [] by app.1. Step: app (x # xs) [] = x # app xs [] = x # xs, by app.2 and the IH.
  • Lemma B, app (app xs ys) zs = app xs (app ys zs). Base case: both sides are app ys zs, by app.1. Step: app (app (x # xs) ys) zs = app (x # app xs ys) zs = x # app (app xs ys) zs, which is x # app xs (app ys zs) by the IH, and that is app (x # xs) (app ys zs) by app.2.

Lemma C, reverse (app xs ys) = app (reverse ys) (reverse xs), by induction on xs. Base case: reverse (app [] ys) = reverse ys, which is app (reverse ys) [] by Lemma A, and reverse [] = []. Step:

  reverse (app (x # xs) ys)
= reverse (x # app xs ys)                      by app.2
= app (reverse (app xs ys)) [x]                by reverse.2
= app (app (reverse ys) (reverse xs)) [x]      by the IH
= app (reverse ys) (app (reverse xs) [x])      by Lemma B
= app (reverse ys) (reverse (x # xs))          by reverse.2

Theorem, reverse (reverse xs) = xs, by induction on xs. Base case: reverse (reverse []) = [] by reverse.1 twice. Step, using reverse [x] = app [] [x] = [x]:

  reverse (reverse (x # xs))
= reverse (app (reverse xs) [x])               by reverse.2
= app (reverse [x]) (reverse (reverse xs))     by Lemma C
= app [x] xs                                   by reverse [x] = [x] and the IH
= x # xs                                       by app.2 and app.1
Check it in Isabelle

In Isabelle, prove the lemmas first and mark each with [simp]:

Isabelle
lemma app_nil [simp]: "app xs [] = xs"
  by (induction xs) auto

lemma app_assoc [simp]: "app (app xs ys) zs = app xs (app ys zs)"
  by (induction xs) auto

lemma reverse_app [simp]: "reverse (app xs ys) = app (reverse ys) (reverse xs)"
  by (induction xs) auto

lemma reverse_reverse: "reverse (reverse xs) = xs"
  by (induction xs) auto

The attribute [simp] makes a lemma a simplification rule: from then on, simp and auto use it to rewrite, in the same way as the defining equations. That is how the proof of reverse_app uses app_nil and app_assoc, and the proof of reverse_reverse uses reverse_app. If you try reverse_app before the two lemmas, it gets stuck with goals that need them, exactly as reverse_reverse got stuck without reverse_app. Module 9 explains which lemmas make good simplification rules.

Key idea

When an induction gets stuck, read the stuck goal. It usually contains a term that no equation can rewrite, such as reverse (app (reverse xs) [a]). Find the general fact about that term, prove it as a lemma (often by induction itself), make it available to the proof, and try again. The lemmas above are suitable [simp] rules; arbitrary lemmas can cause unhelpful rewriting or loops if registered that way. Module 9 discusses that choice, and Module 11 develops the induction strategy.

6

Strong induction

In ordinary induction, the step may use only P(n)P(n) to prove P(n+1)P(n + 1)⁠: the domino immediately before. Some proofs need more. The Fibonacci numbers look two steps back, and a divisor of a number can be any smaller number, not only the one before.

Fact 5.4Strong induction

Let P(n)P(n) be a statement about a natural number nn⁠. If, for every nn⁠, P(m)P(m) for all m<nm < n (the induction hypothesis) implies P(n)P(n)⁠, then P(n)P(n) holds for every natural number nn⁠.

In symbols: (∀n. (∀m<n. P(m))⟶P(n))⟶(∀n. P(n))(\forall n.\ (\forall m < n.\ P(m)) \imp P(n)) \imp (\forall n.\ P(n))⁠. Here ∀m<n. P(m)\forall m < n.\ P(m) reads “for all m less than n, P of m” and is short for ∀m. m<n⟶P(m)\forall m.\ m < n \imp P(m)⁠, like the bounded quantifier ∀x∈A\forall x \in A of Module 4. Strong induction is also called complete induction or course-of-values induction. There is no separate base case. For n=0n = 0 there is no m<0m < 0⁠, so the IH says nothing and P(0)P(0) must be proved without help; in practice, a proof by strong induction treats the smallest values separately.

Strong induction is no stronger than ordinary induction; it follows from it. Apply ordinary induction to the statement Q(n)Q(n)⁠: “P(m)P(m) holds for every m<nm < n”. Q(0)Q(0) holds because there is no m<0m < 0⁠. If Q(n)Q(n) holds, then P(n)P(n) holds by the assumption of strong induction, so P(m)P(m) holds for every m<n+1m < n + 1⁠: that is Q(n+1)Q(n + 1)⁠. Hence Q(n)Q(n) holds for every nn⁠, and P(n)P(n) follows from Q(n+1)Q(n + 1)⁠.

Definition 5.6Prime number

A natural number pp is prime if p≥2p \ge 2 and its only divisors are 1 and pp⁠. A prime factor of nn is a prime number that divides nn⁠.

Example 5.6Every number from 2 on has a prime factor

Claim. Every natural number n≥2n \ge 2 has a prime factor.

Proof by strong induction on nn⁠, with P(n)P(n)⁠: “if n≥2n \ge 2⁠, then nn has a prime factor”. Let n≥2n \ge 2⁠, and assume P(m)P(m) for every m<nm < n⁠.

  • If nn is prime, nn itself is a prime factor of nn⁠.
  • If nn is not prime, it has a divisor aa with 1<a<n1 < a < n⁠. By the IH, P(a)P(a) holds, and since a≥2a \ge 2⁠, aa has a prime factor pp⁠. As pp divides aa and aa divides nn⁠, pp divides nn⁠.

For n<2n < 2 there is nothing to prove. By strong induction, P(n)P(n) holds for every nn⁠.

The immediately preceding instance alone would not support this argument: knowing that n−1n - 1 has a prime factor does not supply a factor of nn⁠. The divisor aa can be any number below nn⁠, so strong induction supplies exactly the hypotheses this proof uses. Ordinary induction on a strengthened statement could supply them too, as the preceding derivation shows.

Example 5.7A bound on the Fibonacci numbers

Claim. For every natural number nn⁠, fib(n)<2n\mathit{fib}(n) < 2^n⁠.

Proof by strong induction on nn⁠. Assume fib(m)<2m\mathit{fib}(m) < 2^m for every m<nm < n⁠.

  • n=0n = 0⁠: fib(0)=0<1=20\mathit{fib}(0) = 0 < 1 = 2^0⁠.
  • n=1n = 1⁠: fib(1)=1<2=21\mathit{fib}(1) = 1 < 2 = 2^1⁠.
  • n≥2n \ge 2⁠: then n=k+2n = k + 2⁠, and both kk and k+1k + 1 are smaller than nn⁠. By the IH for both,
fib(k+2)=fib(k)+fib(k+1)<2k+2k+1<2k+1+2k+1=2k+2.\mathit{fib}(k+2) = \mathit{fib}(k) + \mathit{fib}(k+1) < 2^k + 2^{k+1} < 2^{k+1} + 2^{k+1} = 2^{k+2}.
Check it in Isabelle

Plain induction on n fails, for the reason given above:

Isabellefails on purpose
lemma "fib n < 2 ^ n"
  by (induction n) auto
Output
Failed to finish proof⌂:
goal (1 subgoal):
 1. ⋀n. fib n < 2 ^ n ⟹ fib (Suc n) < 2 * 2 ^ n

The IH speaks about fib n only, but fib (Suc n) is the sum of the two values before it, and it cannot even be unfolded: Suc n matches neither Suc 0 nor Suc (Suc m) as long as nothing is known about n. (2 ^ n is the library’s power function, defined by recursion on n like pow; it has the same values as pow 2 n.)

The repair is the trick that derived strong induction from ordinary induction: prove a stronger statement that carries the two previous values along.

Isabelle
lemma fib_bound: "fib n < 2 ^ n ∧ fib (Suc n) < 2 ^ Suc n"
  apply (induction n)
Output
proof (prove)
goal (2 subgoals):
 1. fib 0 < 2 ^ 0 ∧ fib (Suc 0) < 2 ^ Suc 0
 2. ⋀n. fib n < 2 ^ n ∧ fib (Suc n) < 2 ^ Suc n ⟹
        fib (Suc n) < 2 ^ Suc n ∧ fib (Suc (Suc n)) < 2 ^ Suc (Suc n)

Now the IH contains both fib n and fib (Suc n), which is exactly what fib (Suc (Suc n)) needs:

Isabelle
  apply auto
  done

Module 11 shows two more direct ways: induction that follows the recursion of fib (fib.induct), and strong induction itself (less_induct).

Strong induction is induction along the order << on N\N⁠, and it works because that order is well-founded. The same holds for every well-founded relation RR (Module 4): if P(x)P(x) follows whenever P(y)P(y) holds for every yy with (y,x)∈R(y, x) \in R⁠, then P(x)P(x) holds for every xx⁠. This is called well-founded induction. Structural induction is a special case, with RR relating xs to x # xs, and l and r to Node l x r.

7

Termination

Every function so far stops on every input: each recursive call works on a smaller argument, and arguments cannot get smaller forever. This section explains why Isabelle insists on that, and how it checks it.

Why fun requires termination

In a programming language, recursive code may run forever instead of returning a result. HOL functions are mathematical total functions, and their defining equations must hold. A programming recipe cannot be accepted as such equations without justification. Look at this proposed definition:

bad(n)=bad(n)+1\mathit{bad}(n) = \mathit{bad}(n) + 1

If there were a function bad\mathit{bad} on N\N with this property, subtracting bad(n)\mathit{bad}(n) from both sides would give 0=10 = 1⁠. An equation in a definition is a claim that something is true, and this one is false for every function. If Isabelle accepted it as it stands, you could prove 0=10 = 1⁠, contradicting the arithmetic already in the logic. From a contradiction every statement follows (Module 2).

So Isabelle never takes the equations of a fun on trust. It constructs a function from them and then proves the equations as theorems, and that proof needs an argument that the recursion terminates. fun searches for one automatically. For bad there is none:

Isabellefails on purpose
fun bad :: "nat ⇒ nat" where
  "bad n = bad n + 1"
Output
consts
  bad :: "nat ⇒ nat"

Unfinished subgoals:
(a, 1, <):
 1. False
Calls:
  a) n ~> n
Measures:
  1) size
Result matrix:
    1
a:  <=

Could not find lexicographic termination order.

The first lines only announce the name and type of the new constant; the definition as a whole fails, and bad is not defined afterwards. Read the error from the bottom up. Isabelle could not find a termination order. It looked at one recursive call, a), from n to n (the arrow ~> means “calls”), and at one measure, size, which for a number is the number itself. To accept the definition it must show that the measure gets strictly smaller (<) at the call; for this call that means proving False, which cannot be done. The result matrix records the best it found: the measure stays the same or gets smaller (<=), which is not enough.

Key idea

In HOL every function is total: it has a value for every argument. Complete, compatible recursive equations with well-founded recursion determine a total function. The function package checks the equations and proves termination before fun accepts them as defining theorems. Under the trusted logical infrastructure described in Module 1, this adds a definition without introducing an unjustified axiom. Termination alone would not excuse conflicting equations.

Not every non-terminating equation is contradictory: every function satisfies f(n)=f(n)f(n) = f(n)⁠. But Isabelle does not try to tell harmless cases from harmful ones; it accepts a recursive definition by fun only when it can prove termination.

Measures

Definition 5.7Measure

A measure for a recursive definition is a function mm from the arguments to N\N that gets strictly smaller at every recursive call: whenever the definition of f(a)f(a) calls f(b)f(b)⁠, m(b)<m(a)m(b) < m(a)⁠.

Fact 5.5Termination by a measure

For a recursive definition with finitely many calls in each case and terminating work outside those calls, a decreasing measure guarantees that every evaluation terminates.

The reason is the well-foundedness of N\N from Section 5.3: an evaluation that never stopped would make infinitely many nested calls with arguments a0,a1,a2,…a_0, a_1, a_2, \dots⁠, and then m(a0)>m(a1)>m(a2)>⋯m(a_0) > m(a_1) > m(a_2) > \cdots would be an infinite descending chain of natural numbers. There is none.

Functions Recursive calls Measure
sum_upto, fac f (Suc n) calls f n the number
fib fib (Suc (Suc n)) calls fib n and fib (Suc n) the number
pow pow x (Suc n) calls pow x n the second argument
len, reverse, lsum f (x # xs) calls f xs the length of the list
app app (x # xs) ys calls app xs ys the length of the first list
mirror, nodes, tips f (Node l x r) calls f l and f r the number of nodes

These are the termination orders that Isabelle reports. For sum_upto it said size <*mlex*> {}: the measure is size, which for a number is the number itself, for a list its length and for a tree its number of nodes. Read f <*mlex*> {} as “the measure f”. For the list functions Isabelle writes length instead of size, for example length <*mlex*> {} for reverse.

Sometimes none of the individual argument sizes decreases at every call. The Ackermann function is a famous example for which a lexicographic combination works:

Isabelle
fun ack :: "nat ⇒ nat ⇒ nat" where
  "ack 0 n = Suc n"
| "ack (Suc m) 0 = ack m 1"
| "ack (Suc m) (Suc n) = ack m (ack (Suc m) n)"
Output
consts
  ack :: "nat ⇒ nat ⇒ nat"

Found termination order:
  "(λp. size (fst p)) <*mlex*> (λp. size (snd p)) <*mlex*> {}"

In the call from ack (Suc m) (Suc n) to ack (Suc m) n the first argument stays the same, and in the outer call ack m (ack (Suc m) n) the second argument can grow substantially. But at every call the pair of arguments gets smaller in the lexicographic order, the order of a dictionary: compare the first arguments, and only if they are equal, compare the second ones. That is the termination order Isabelle found. The arguments form a pair p; fst p and snd p are its first and second components; and λp. size (fst p) (read “lambda p”) is the function that maps p to the size of its first component. The order uses that measure first and the size of the second argument after it. The lexicographic order on pairs of natural numbers is well-founded too, so it proves termination in the same way as a measure does.

The Ackermann function is famous because it grows so fast. Small arguments give small values:

Isabelle
value "ack 2 3"
Output
"9"
  :: "nat"

But ack 4 2 is already 265536−32^{65536} - 3⁠, a number with 19,729 decimal digits.

When fun cannot prove termination

A beginner’s version of the sum:

Isabellefails on purpose
fun sum_down :: "nat ⇒ nat" where
  "sum_down n = sum_down (n - 1) + n"
Output
consts
  sum_down :: "nat ⇒ nat"

Unfinished subgoals:
(a, 1, <):
 1. ⋀n. n - Suc 0 < n
Calls:
  a) n ~> n - 1
Measures:
  1) size
Result matrix:
    1
a:  <=

Could not find lexicographic termination order.

This time the unfinished subgoal is n - Suc 0 < n, that is n−1<nn - 1 < n⁠, and it is false for n=0n = 0⁠. Subtraction on natural numbers cannot go below 0, so 0 - 1 = 0 in Isabelle, and sum_down 0 would call sum_down 0 for ever. The definition forgot its base case. Unlike bad, it is not contradictory: for n=0n = 0 it says sum_down 0 = sum_down 0 + 0, which holds whatever the value of sum_down 0 is. But it leaves that value open, and fun rejects the definition all the same, because it cannot prove termination. Pattern matching, as in sum_upto, prevents this mistake: there the recursive equation applies only to Suc n, where n really is smaller.

Sometimes a function does terminate, but fun cannot see why. Its automatic search uses candidates such as argument sizes and lexicographic combinations; it does not discover every useful measure. A function that counts upwards, such as sum_from i n = (if n < i then 0 else i + sum_from (i + 1) n), terminates because n+1−in + 1 - i decreases at every call, yet neither argument gets smaller, so fun gives up. For such definitions Isabelle has the more general command function, with which you supply the measure yourself; Module 8 returns to termination.

Good to know

For some functions nobody knows a measure. The Collatz function counts the steps of a famous process:

collatz(n)={0if n≤1collatz(n/2)+1if n is evencollatz(3n+1)+1otherwise\mathit{collatz}(n) = \begin{cases} 0 & \text{if } n \le 1\\ \mathit{collatz}(n / 2) + 1 & \text{if } n \text{ is even}\\ \mathit{collatz}(3n + 1) + 1 & \text{otherwise} \end{cases}

Computers have checked that it terminates for every nn up to astronomically large bounds, but whether it terminates for every nn is an open problem, the Collatz conjecture, posed by Lothar Collatz in 1937. Isabelle’s fun rejects this definition, and nobody can supply a termination proof by hand either: such a proof would settle the conjecture.

8

Invariants

Induction proves facts about recursive functions. It also proves facts about processes that run step by step, such as loops, protocols and machines. The tool for that is an invariant.

Definition 5.8Invariant

An inductive invariant, called an invariant here, is a property of a process’s state that holds in every initial state and is preserved by every step: if it holds before a step, it holds after the step.

Fact 5.6Invariants hold in every reachable state

If II is an invariant of a process, then II holds in every state that the process reaches, after any number of steps.

The proof is by induction on the number of steps kk⁠. After 0 steps, the process is in its initial state, where II holds. If II holds after kk steps, the next step preserves it, so II holds after k+1k + 1 steps.

A loop

This program sums the numbers from 1 to n with a loop. Assume that n is a fixed natural number and that arithmetic uses unbounded integers; an implementation using fixed-width integers would also need an overflow argument:

i := 0
s := 0
while i < n:
    i := i + 1
    s := s + i
return s

Claim. The loop returns sum_upto(n)\mathit{sum\_upto}(n)⁠. Take as invariant II⁠: “s=sum_upto(i)s = \mathit{sum\_upto}(i) and i≤ni \le n”.

  • Initially i=0i = 0 and s=0=sum_upto(0)s = 0 = \mathit{sum\_upto}(0)⁠, and 0≤n0 \le n⁠.
  • Preserved. Assume II and that the loop runs once more, so i<ni < n⁠. Afterwards i′=i+1i' = i + 1 and s′=s+(i+1)=sum_upto(i)+(i+1)=sum_upto(i+1)=sum_upto(i′)s' = s + (i + 1) = \mathit{sum\_upto}(i) + (i + 1) = \mathit{sum\_upto}(i + 1) = \mathit{sum\_upto}(i')⁠, by sum_upto.2; and i′≤ni' \le n because i<ni < n⁠.
  • At the end the condition i<ni < n is false, so i≥ni \ge n⁠; together with i≤ni \le n this gives i=ni = n⁠, and the invariant says s=sum_upto(n)s = \mathit{sum\_upto}(n)⁠.

With Example 5.1, the loop returns n(n+1)/2n(n+1)/2⁠. It also terminates: n−in - i is a natural number that decreases by 1 at every step, a measure, which for loops is called a variant. Notice that the invariant needs the part i≤ni \le n⁠, although the claim is only about ss⁠: without it, the exit condition i≥ni \ge n would not give i=ni = n⁠. Finding an invariant that is strong enough is the creative part of such a proof. This way of reasoning about loops goes back to Robert Floyd (1967) and Tony Hoare (1969).

A machine

A model of a ticket machine accepts 5-cent and 10-cent coins and returns every other coin. We model only coin insertion, with no purchase or refund transitions. Its state is the credit, which starts at 0. Can the credit ever be exactly 12 cents? Whatever the customers do, the answer is no, and an invariant shows why. Take II⁠: “the credit is a multiple of 5”.

  • Initially the credit is 0, a multiple of 5.
  • Preserved. A 5-cent or 10-cent coin adds 5 or 10, which keeps the credit a multiple of 5. Any other coin is returned, and the credit does not change.

So every credit the machine reaches is a multiple of 5, and 12 is not. The argument covers every sequence of coins, of any length: infinitely many runs, which no amount of testing could try.

Check it in Isabelle

Describe a run by the list of coins inserted, latest first, and compute the credit by recursion over the list:

Isabelle
fun insert_coin :: "nat ⇒ nat ⇒ nat" where
  "insert_coin c m = (if c = 5 ∨ c = 10 then m + c else m)"

fun credit :: "nat list ⇒ nat" where
  "credit [] = 0"
| "credit (c # cs) = insert_coin c (credit cs)"

value "credit [10, 3, 5]"
Output
"15"
  :: "nat"

The 3-cent coin was returned. m mod 5 is the remainder of m divided by 5 (the % operator of many programming languages), so the invariant is credit cs mod 5 = 0, for every run cs:

Isabelle
lemma credit_mod: "credit cs mod 5 = 0"
  apply (induction cs)
Output
proof (prove)
goal (2 subgoals):
 1. credit [] mod 5 = 0
 2. ⋀a cs. credit cs mod 5 = 0 ⟹ credit (a # cs) mod 5 = 0

The base case is the initial state; the step says that one more coin a preserves the invariant. Induction over the list of coins is induction over the number of steps.

Isabelle
  apply auto
  done

Module 13 makes this its main method. It describes systems as state machines, defines their reachable states in Isabelle, and proves safety properties with invariants. When a property is not an invariant by itself, the remedy is the one you used for the Fibonacci bound in Section 5.6: prove a stronger statement that is. For more practice with induction proofs and loop invariants, see Proof methods, induction, and invariants in the Mathematical Foundations series.

9

What you learned

  • Natural numbers are built from 0 and Suc, lists from [] and #, and trees from Tip and Node. Direct structural recursion handles every constructor and calls the function on smaller parts. More general terminating recursion may use a different well-founded relation. datatype defines new types of this kind.
  • The definitions here can be evaluated by rewriting their equations; the chosen rewrite order changes the intermediate terms without changing their result. A symbolic normal form need not be a concrete value. Clear definitions can be slow programs: the naive fib repeats work.
  • Mathematical induction: prove P(0)P(0)⁠, and P(n)⟶P(n+1)P(n) \imp P(n+1) for an arbitrary nn⁠; then P(n)P(n) holds for every nn⁠. Both parts are needed, and the step must work for every nn⁠.
  • Structural induction does the same for lists and trees, with one IH per smaller part. Strong induction may assume P(m)P(m) for every m<nm < n⁠. All of them rest on well-founded orders.
  • In Isabelle, apply (induction n) turns a goal into the base case and the step. Read ⋀n. as “for an arbitrary n” and ⟹ as “implies”; the IH stands to the left of ⟹. by (induction xs) auto can often close the resulting goals using the definitions and IH. When it gets stuck, consider the induction variable, a stronger induction hypothesis, or an auxiliary lemma. Register a lemma [simp] only when it is suitable for automatic rewriting.
  • fun accepts a recursive definition only after proving that it terminates, with a measure or a lexicographic combination of measures that decreases at every call. Without that check, a “definition” such as f(n)=f(n)+1f(n) = f(n) + 1 could prove 0=10 = 1⁠.
  • An invariant holds initially and is preserved by every step, so by induction on the number of steps it holds in every reachable state.
10

Exercises

Exercise 5.1★Evaluate by hand

Evaluate each term by rewriting, one equation per step, and name the equation used at each step:

  1. pow 3 2
  2. app [a] [b, c]
  3. mirror (Node Tip a (Node Tip b Tip))

Then check the results with value, and with the stepper in Section 5.2.

Show solution
  pow 3 2
= pow 3 1 * 3                   by pow.2, since 2 = Suc 1
= pow 3 0 * 3 * 3               by pow.2, since 1 = Suc 0
= 1 * 3 * 3                     by pow.1
= 3 * 3                         arithmetic
= 9                             arithmetic
  app [a] [b, c]
= a # app [] [b, c]             by app.2
= a # [b, c]                    by app.1

which is the list [a, b, c].

  mirror (Node Tip a (Node Tip b Tip))
= Node (mirror (Node Tip b Tip)) a (mirror Tip)              by mirror.2
= Node (Node (mirror Tip) b (mirror Tip)) a (mirror Tip)     by mirror.2
= Node (Node Tip b (mirror Tip)) a (mirror Tip)              by mirror.1
= Node (Node Tip b Tip) a (mirror Tip)                       by mirror.1
= Node (Node Tip b Tip) a Tip                                by mirror.1

The stepper takes the same steps. With value:

Isabelle
value "pow 3 2"
value "app [a] [b, c]"
value "mirror (Node Tip a (Node Tip b Tip))"
Output
"9"
  :: "nat"

"[a, b, c]"
  :: "'a list"

"Node (Node Tip b Tip) a Tip"
  :: "'a tree2"
Exercise 5.2★What is wrong with this proof?

Claim: for every natural number nn⁠, the number n2+nn^2 + n is odd.

Proof: assume that n2+nn^2 + n is odd. Then (n+1)2+(n+1)=(n2+n)+2(n+1)(n + 1)^2 + (n + 1) = (n^2 + n) + 2(n + 1) is an odd number plus an even number, so it is odd. By induction, n2+nn^2 + n is odd for every nn⁠.

Find the mistake. Is the claim true?

Show solution

The induction step is correct, but the proof has no base case, and the base case is false: 02+0=00^2 + 0 = 0 is even. The claim is false for every nn⁠: n2+n=n(n+1)n^2 + n = n(n + 1) is the product of two consecutive numbers, one of which is even, so n2+nn^2 + n is always even. The step shows only that if n2+nn^2 + n were odd for one nn⁠, it would be odd for all larger nn too. Without a first domino, nothing falls.

Exercise 5.3★★The sum of an append

Prove on paper that lsum (app xs ys) = lsum xs + lsum ys for all lists of numbers xs and ys. On which variable do you do induction? Then write down the two subgoals you expect after apply (induction xs), compare them with Isabelle’s, and finish the proof in Isabelle.

Show solution

Induction on xs, because app takes its first argument apart. Base case: lsum (app [] ys) = lsum ys = 0 + lsum ys = lsum [] + lsum ys, by app.1 and lsum.1. Step, with the IH lsum (app xs ys) = lsum xs + lsum ys:

  lsum (app (x # xs) ys)
= lsum (x # app xs ys)           by app.2
= x + lsum (app xs ys)           by lsum.2
= x + (lsum xs + lsum ys)        by the IH
= (x + lsum xs) + lsum ys        arithmetic
= lsum (x # xs) + lsum ys        by lsum.2

In Isabelle:

Isabelle
lemma lsum_app: "lsum (app xs ys) = lsum xs + lsum ys"
  apply (induction xs)
Output
proof (prove)
goal (2 subgoals):
 1. lsum (app [] ys) = lsum [] + lsum ys
 2. ⋀a xs.
       lsum (app xs ys) = lsum xs + lsum ys ⟹
       lsum (app (a # xs) ys) = lsum (a # xs) + lsum ys

The subgoals are the base case and the step above, with a for x.

Isabelle
  apply auto
  done
Exercise 5.4★★Odd numbers

The sum of the first nn odd numbers, 1+3+⋯+(2n−1)1 + 3 + \dots + (2n - 1)⁠, is defined by odds(0)=0\mathit{odds}(0) = 0 and odds(n+1)=odds(n)+(2n+1)\mathit{odds}(n + 1) = \mathit{odds}(n) + (2n + 1)⁠. Prove by induction that odds(n)=n2\mathit{odds}(n) = n^2⁠. Then define odds in Isabelle and prove the same lemma there.

Show solution

Base case: odds(0)=0=02\mathit{odds}(0) = 0 = 0^2⁠. Step: assume odds(n)=n2\mathit{odds}(n) = n^2⁠. Then odds(n+1)=odds(n)+2n+1=n2+2n+1=(n+1)2\mathit{odds}(n + 1) = \mathit{odds}(n) + 2n + 1 = n^2 + 2n + 1 = (n + 1)^2⁠, using the definition and then the IH. By induction, odds(n)=n2\mathit{odds}(n) = n^2 for every nn⁠.

Isabelle
fun odds :: "nat ⇒ nat" where
  "odds 0 = 0"
| "odds (Suc n) = odds n + (2 * n + 1)"

lemma odds_square: "odds n = n * n"
  by (induction n) auto
Exercise 5.5★★Paying with two coins

Show by strong induction that every amount of at least 12 cents can be paid exactly with 4-cent and 5-cent coins. Hint: deal with 12, 13, 14 and 15 directly; for larger amounts, look at the amount minus 4.

Show solution

Let P(n)P(n) be: “if n≥12n \ge 12⁠, then n=4a+5bn = 4a + 5b for some natural numbers aa and bb”. Let n≥12n \ge 12 and assume P(m)P(m) for every m<nm < n⁠.

  • 12=4⋅312 = 4 \cdot 3⁠, 13=4⋅2+513 = 4 \cdot 2 + 5⁠, 14=4+5⋅214 = 4 + 5 \cdot 2 and 15=5⋅315 = 5 \cdot 3⁠.
  • If n≥16n \ge 16⁠, then 12≤n−4<n12 \le n - 4 < n⁠, so by the IH n−4=4a+5bn - 4 = 4a + 5b for some aa and bb⁠, and n=4(a+1)+5bn = 4(a + 1) + 5b⁠.

By strong induction, P(n)P(n) holds for every nn⁠. The step reaches four numbers back, not one, which is why strong induction is the convenient tool here. The bound 12 is the best possible: 11 cannot be paid, because 11=4a+5b11 = 4a + 5b has no solution in natural numbers.

Exercise 5.6★★Breaking chocolate

A chocolate bar has m×nm \times n squares, where mm and nn are positive integers. You break it into single squares. Each break takes one piece and splits it into two pieces along a line between the squares. Find an invariant that relates the number of pieces to the number of breaks made so far, and use it to show that every way of breaking the bar takes exactly m⋅n−1m \cdot n - 1 breaks.

Show solution

Invariant: pieces = breaks + 1. Initially there is 1 piece and there have been 0 breaks. Each break turns one piece into two, so it adds 1 to both numbers and preserves the equation. By the invariant, when the bar is in m⋅nm \cdot n single squares, the number of breaks is m⋅n−1m \cdot n - 1⁠, in whatever order you break. The process also terminates: m⋅n−piecesm \cdot n - \text{pieces} is a measure, a natural number that decreases by 1 at every break.

Exercise 5.7★★★In-order traversal

The function inorder lists the elements of a tree from left to right, using the library’s @:

inorder Tip = []
inorder (Node l x r) = inorder l @ [x] @ inorder r
  1. Prove on paper that inorder (mirror t) = rev (inorder t). You may use the library facts rev (xs @ ys) = rev ys @ rev xs, (xs @ ys) @ zs = xs @ (ys @ zs) and rev [x] = [x].
  2. Define inorder in Isabelle, and prove length (inorder t) = nodes t and inorder (mirror t) = rev (inorder t).
Show solution
  1. By structural induction on t. Base case: inorder (mirror Tip) = inorder Tip = [] = rev [], which is rev (inorder Tip). Step, with the IHs inorder (mirror l) = rev (inorder l) and inorder (mirror r) = rev (inorder r):

      inorder (mirror (Node l x r))
    = inorder (Node (mirror r) x (mirror l))            by mirror.2
    = inorder (mirror r) @ [x] @ inorder (mirror l)     by inorder.2
    = rev (inorder r) @ [x] @ rev (inorder l)           by the two IHs
    = rev (inorder r) @ rev [x] @ rev (inorder l)       since rev [x] = [x]
    = rev (inorder l @ [x] @ inorder r)                 by the law for rev of @, twice
    = rev (inorder (Node l x r))                        by inorder.2
    

    The step “twice” also uses associativity of @ to regroup the three parts.

  2. In Isabelle:

    Isabelle
    fun inorder :: "'a tree2 ⇒ 'a list" where
      "inorder Tip = []"
    | "inorder (Node l x r) = inorder l @ [x] @ inorder r"
    
    lemma length_inorder: "length (inorder t) = nodes t"
      by (induction t) auto
    
    lemma inorder_mirror: "inorder (mirror t) = rev (inorder t)"
      by (induction t) auto

    Both proofs go through with auto because the library has already proved the facts about @, rev and length that they need, such as rev_append and length_append, and declared them as simplification rules.

Exercise 5.8★★★Which definitions terminate?

For each definition, decide whether every evaluation terminates. If it does, give a measure; if not, give an argument on which it runs for ever. Then predict whether fun accepts the definition, and check in Isabelle.

  1. half 0 = 0, half (Suc 0) = 0 and half (Suc (Suc n)) = half n + 1
  2. nbits n = (if n = 0 then 0 else nbits (n div 2) + 1)
  3. nbits2 n = nbits2 (n div 2) + 1
  4. count_up i n = (if i < n then count_up (i + 1) n + 1 else 0)

(n div 2 is n/2n / 2 rounded down.)

Show solution
  1. Terminates, with measure nn⁠: the call goes from n+2n + 2 to nn⁠. fun accepts it.

  2. Terminates, with measure nn⁠: the recursive call happens only when n≠0n \ne 0⁠, and then ndiv2<nn \mathbin{\mathrm{div}} 2 < n⁠. fun accepts it, because it takes the condition of the if into account when it compares the arguments.

    Isabelle
    fun half :: "nat ⇒ nat" where
      "half 0 = 0"
    | "half (Suc 0) = 0"
    | "half (Suc (Suc n)) = half n + 1"
    
    fun nbits :: "nat ⇒ nat" where
      "nbits n = (if n = 0 then 0 else nbits (n div 2) + 1)"
    Output
    consts
      half :: "nat ⇒ nat"
    
    Found termination order: "size <*mlex*> {}"
    
    consts
      nbits :: "nat ⇒ nat"
    
    Found termination order: "size <*mlex*> {}"
  3. Does not terminate for n=0n = 0⁠: 0 div 2 = 0, so nbits2 0 calls nbits2 0 again. Worse, the equation for n=0n = 0 reads nbits2 0 = nbits2 0 + 1, the contradiction of bad. fun rejects it:

    Isabellefails on purpose
    fun nbits2 :: "nat ⇒ nat" where
      "nbits2 n = nbits2 (n div 2) + 1"
    Output
    consts
      nbits2 :: "nat ⇒ nat"
    
    Unfinished subgoals:
    (a, 1, <):
     1. ⋀n. n div 2 < n
    Calls:
      a) n ~> n div 2
    Measures:
      1) size
    Result matrix:
        1
    a:  <=
    
    Could not find lexicographic termination order.

    The unfinished subgoal n div 2 < n is false for n=0n = 0⁠.

  4. Terminates, with measure n−in - i⁠, which decreases by 1 at every call while i<ni < n⁠. But neither argument gets smaller, so fun cannot find a termination order: the same situation as sum_from in Section 5.7. Module 8 returns to such definitions.

    Isabellefails on purpose
    fun count_up :: "nat ⇒ nat ⇒ nat" where
      "count_up i n = (if i < n then count_up (i + 1) n + 1 else 0)"
    Output
    consts
      count_up :: "nat ⇒ nat ⇒ nat"
    
    Unfinished subgoals:
    (a, 1, <):
     1. ⋀i n. i < n ⟹ False
    (a, 2, <):
     1. ⋀i n. i < n ⟹ False
    (a, 2, <=):
     1. ⋀i n. i < n ⟹ False
    (a, 3, <):
     1. ⋀i n. i < n ⟹ False
    Calls:
      a) (i, n) ~> (i + 1, n)
    Measures:
      1) λp. size (snd p)
      2) λp. size (fst p)
      3) size
    Result matrix:
        1  2  3
    a:  <= ?  <=
    
    Could not find lexicographic termination order.
11

Check yourself

1
In the induction step of a proof of P(n)P(n) for all nn⁠, what may you assume?
2
After apply (induction xs), Isabelle shows the subgoal ⋀a xs. P xs ⟹ P (a # xs). What does it say?
3
Why does Isabelle insist that every function defined with fun terminates?
4
Which measure shows that app terminates?
5
To prove P(n)P(n) you need P(m)P(m) for some mm that can be any number smaller than nn⁠. Which proof method fits?
6
by (induction xs) auto fails, and the remaining goal contains reverse (app (reverse xs) [a]). What is usually the best next step?
Finished this module?