Formal Methods 101: Induction

This is part 11 of 11 in the Formal Methods 101 series.

What induction is

Induction is a mathematical technique used to prove a property of a system. Intuitively, you can think of it as a way of proving that all dominoes will fall. If we know that the first domino falls, and we know that each domino causes the next one to fall, we are assured that all of them will fall.

An induction proof has four steps:

  1. Base case: prove that the property holds for the base case (or state).
  2. Inductive hypothesis: assume that the property holds for a later case (or state) that satisfies the assumptions of the base case. This is the symbolic base case.
  3. Inductive step: perform an inductive step, a symbolic transition to the next state.
  4. Proof step: prove that the property holds after the inductive step, that is, after the symbolic transition.

A classic example

We prove that a formula holds for all natural numbers: the sum of the first n natural numbers equals P(n).

P(n) = n(n+1)/2 = 0 + 1 + 2 + … + n

A row of boxes P(0) with sum 0, P(1) with sum 1, P(2) with sum 3, P(3) with sum 6, then dots, P(k) assumed and P(k+1) to show. The base case lights up first, each arrow adds the next number and lights the next box, and the last arrow is the inductive step from P(k) to P(k+1).
The proof as a chain of cases, like the dominoes. The base case starts the chain and each arrow adds the next number. The step shows that the arrow from P(k) to P(k+1) works for any k.

Step 1: base case

Test the base case, n = 0:

P(0) = 0(0+1)/2 = 0

The base case is proven true.

Step 2: inductive hypothesis

Assume that P(n) holds for a later case, n = k with k ∈ ℕ, which has the same properties as the base case (the symbolic base case):

P(k) = k(k+1)/2 = 0 + 1 + 2 + … + k

Step 3: inductive step

Perform an inductive step by testing n = k+1, a symbolic execution from k to k+1. The equation for P(k+1) is:

(k+1)((k+1)+1)/2 = (0 + 1 + 2 + … + k) + (k+1)

Call the left-hand side LHS and the right-hand side RHS.

Step 4: proof step

Check whether the property P(n) still holds. Simplify the left-hand side:

LHS = (k+1)((k+1)+1)/2 = (k² + 3k + 2)/2

Rewrite the right-hand side by the inductive hypothesis, then simplify in three steps:

RHS = (0 + 1 + 2 + … + k) + (k+1) = k(k+1)/2 + (k+1)
= (k(k+1) + 2(k+1))/2
= (k+1)(k+2)/2
= (k+1)((k+1)+1)/2

The right-hand side now equals the left-hand side:

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

Proof complete: we have proved that for all n, P(n) holds.

Types of induction

Weak induction (simple induction)

The classic example above is weak induction: we assume a single base case (and therefore a single symbolic base case) and take an inductive step.

(Φ(0) ∧ (∀n)(Φ(n) → Φ(n+1))) → (∀n)Φ(n)

In natural language: the property holds in the initial state, and for all future states, if the property holds for some state it holds for the next state; therefore the property holds for all states.

Strong induction (complete induction)

Complete induction makes the property easier to prove by strengthening the inductive hypothesis: we may assume the property for all earlier states, not just the previous one.

(Φ(0) ∧ (∀n)((∀m ≤ n)Φ(m) → (∀m ≤ n+1)Φ(m))) → (∀n)Φ(n)

In natural language: the property holds in the initial state, and for all future states, if the property holds for some number of states, it holds for the next state; therefore the property holds for all states.

k-induction

k-induction can be looked at from a different syntactic angle. Consider this family of formulas:

1-induction:

(Φ(0) ∧ (∀n)(Φ(n) → Φ(n+1))) → (∀n)Φ(n)

2-induction:

(Φ(0) ∧ Φ(1) ∧ (∀n)((Φ(n) ∧ Φ(n+1)) → Φ(n+2))) → (∀n)Φ(n)

k-induction:

(Φ(0) ∧ Φ(1) ∧ … ∧ Φ(k−1) ∧ (∀n)((Φ(n) ∧ Φ(n+1) ∧ … ∧ Φ(n+k−1)) → Φ(n+k))) → (∀n)Φ(n)

k-induction, simplified with a big conjunction ⋀ over i = 0 to k−1:

(⋀ Φ(i) ∧ (∀n)(⋀ Φ(n+i) → Φ(n+k))) → (∀n)Φ(n)

Induction for extended-state machines

Induction may be applied to extended-state machines to prove invariant properties of systems. Consider the following machine. It has two states, four transitions, one variable x of type int, and a variable for the current state (S1 or S2). It starts in state S1 with x = 0. At each transition (step), if x < 9 the system goes to state S2 and increments x by one. If x ≥ 9 the system goes to state S1 and resets x to 0.

Two states S1 and S2 with four transitions. When x is below 9 the machine moves to or stays in S2 and adds one to x; when x is at least 9 it moves to or stays in S1 and resets x to 0. It starts in S1 with x equal to 0.
The machine as a state diagram. Each transition is chosen by x alone: below 9 it counts up in S2, at 9 it resets and returns to S1.

We wish to prove by induction that the following property is invariant:

Φ = (x ≤ 9)

That is, x can never be greater than 9. This is a static invariant, a property that does not change over the life of the system. If an invariant can be proven by induction we call it an inductive invariant. Note that some invariants cannot be proven by induction.

The machine can be written as the following SMT formula, where x′ and state′ stand for the values of x and state after the next transition:

S := ((x < 9) → (x′ = x + 1 ∧ state′ = S2)) ∧ ((x ≥ 9) → (x′ = 0 ∧ state′ = S1))

We join the property to this formula with an implication, indicating that wherever the system formula is satisfied, the property is satisfied:

S → Φ := (((x < 9) → (x′ = x + 1 ∧ state′ = S2)) ∧ ((x ≥ 9) → (x′ = 0 ∧ state′ = S1))) → ((x ≤ 9) ∧ (x′ ≤ 9))

We now attempt to prove this invariant by induction.

Step 1: base case

Test the initial values, x = 0 and state = S1:

S → Φ := (((0 < 9) → (x′ = 0 + 1 ∧ state′ = S2)) ∧ ((0 ≥ 9) → (x′ = 0 ∧ state′ = S1))) → ((0 ≤ 9) ∧ (x′ ≤ 9))

Simplify. Here x′ = 1, so:

S → Φ := (((0 < 9) → (x′ = 1 ∧ state′ = S2)) ∧ ((0 ≥ 9) → (x′ = 0 ∧ state′ = S1))) → ((0 ≤ 9) ∧ (1 ≤ 9))
S → Φ := (TRUE ∧ TRUE) → (TRUE ∧ TRUE)
S → Φ := TRUE

The base case is proven true.

Step 2: inductive hypothesis

Assume the property holds for a later case, x = k, where k ≤ 9:

S → Φ := (((k < 9) → (x′ = k + 1 ∧ state′ = S2)) ∧ ((k ≥ 9) → (x′ = 0 ∧ state′ = S1))) → ((k ≤ 9) ∧ (x′ ≤ 9))
S → Φ := TRUE (assumed)

Step 3: inductive step

Perform an inductive step by testing the next value, k+1, a symbolic execution from k to k+1:

S → Φ := ((((k+1) < 9) → (x′ = (k+1) + 1 ∧ state′ = S2)) ∧ (((k+1) ≥ 9) → (x′ = 0 ∧ state′ = S1))) → (((k+1) ≤ 9) ∧ (x′ ≤ 9))

Step 4: proof step

Check whether the implication S → Φ still holds. We check the two cases, (k+1) < 9 and (k+1) ≥ 9. Only one of them can be true at a time, and one of them must always be true.

Case 1: (k+1) < 9.

x′ = k + 2
Φ := ((k+1) ≤ 9) ∧ ((k+2) ≤ 9)

Since k+1 is less than 9, the highest k could be is 7:

Φ := ((7+1) ≤ 9) ∧ ((7+2) ≤ 9)
Φ := (8 ≤ 9) ∧ (9 ≤ 9)
Φ := TRUE

Case 2: (k+1) ≥ 9.

x′ = 0

Because (k+1) ≥ 9, and the inductive hypothesis gives (k+1) ≤ 9 for integer k, we conclude that k+1 = 9, so k = 8:

Φ := ((8+1) ≤ 9) ∧ (0 ≤ 9)
Φ := (9 ≤ 9) ∧ (0 ≤ 9)
Φ := TRUE ∧ TRUE
Φ := TRUE

Proof complete: we have proved that the property Φ := (x ≤ 9) holds in all states. Since we proved it by induction, we call it an inductive invariant of the system.