Chapter 7

Induction from recursion

Until now the claim of a theorem was its body. That is enough for a list of known length such as (cons x1 '()), but not for a theorem about every list. This section shows the problem on Chapter 5 of the transcript, then reads induction/claim, which turns the recursion of a total function into an induction claim, and the rules for the seed that names the function. Two proofs of the book follow, by list induction and by star induction. The section ends with the reason why the claim is enough, and with a proof left unfinished for Chapter 12.

7.1One theorem per length

Chapters 5 and 6 of the transcript work with two recursive functions, admitted with the measure (size xs) as in Section 6.3. memb? says whether the symbol ? occurs in a list: (if (atom xs) 'nil (if (equal (car xs) '?) 't (memb? (cdr xs)))) (scheme/little-prover.scm:264–265). remb removes every ?:The '((( opens the list of proofs that the entry defun.remb hands to J-Bob/define; the definition is the first element of the first proof.

scheme/little-prover.scm

278    '(((defun remb (xs)
279         (if (atom xs)
280             '()
281             (if (equal (car xs) '?) (remb (cdr xs)) (cons (car xs) (remb (cdr xs))))))

The theorem is (equal (memb? (remb xs)) 'nil). Chapter 5 proves it only for lists of length 0, 1 and 2. by-length.scm reads the four proofs of the theorem from the transcript as data and prints, for each, its claim, its seed, its length, the steps that use a theorem of the book that is not an axiom, and the steps that use the axiom equal-if:The excerpt leaves out 7 lines of by-length.out: the entry memb?/remb0 (7 steps, no lemma) and the equal-if lines of memb?/remb1 and memb?/remb2, which are empty. Each lemma is a step number and the app of that step. In memb?/remb2 the lemma is used at x1, which makes it the theorem for the tail (cons x1 '()).

by-length.scm

[…]
;; dethm.memb?/remb1
(equal (memb? (remb (cons x1 '()))) 'nil)
seed nil, 20 steps
lemmas ((11 (memb?/remb0)) (18 (memb?/remb0)))
[…]
;; dethm.memb?/remb2
(equal (memb? (remb (cons x2 (cons x1 '())))) 'nil)
seed nil, 20 steps
lemmas ((11 (memb?/remb1 x1)) (18 (memb?/remb1 x1)))
[…]
;; dethm.memb?/remb
(equal (memb? (remb xs)) 'nil)
seed (list-induction xs), 23 steps
lemmas ()
equal-if (12 19)

The proofs for one and two elements have 20 steps with the same paths and the same rules, the lemma aside; only the arguments differ (scheme/little-prover.scm:303–377). Each unfolds remb and memb?, splits on whether the first element is ?, and meets the same theorem for the list without its first element, which it settles at steps 11 and 18 with the previous theorem as a lemma. Covering every list like this would take infinitely many entries. The three proofs share one argument: the theorem holds for the empty list, and if it holds for (cdr xs), it holds for xs. The last entry, memb?/remb in Chapter 6, states that argument as one claim. Its seed is (list-induction xs) instead of nil, it uses no lemma, and at steps 12 and 19 it uses equal-if where memb?/remb1 used the lemma.

7.2The induction claim

A seed for a dethm is nil or an application of a defined function to variables of the theorem. With nil, the claim is the body of the theorem. schemes.scm prints the claims that induction/claim builds for other seeds, for a theorem (dethm foo (x y) (equal (bar x y) 't)) about a bar that is not defined (induction/claim does not check the theorem). The seed (list-induction x) gives two cases:

schemes.scm

;; (list-induction x)
(if (atom x)
  (equal (bar x y) 't)
  (if (equal (bar (cdr x) y) 't) (equal (bar x y) 't) 't))

When x is an atom, the claim is the theorem itself: the base case. Otherwise (the step case) it says that if the theorem holds with (cdr x) in place of x, it holds for x. The premise (equal (bar (cdr x) y) 't) is the induction hypothesis, written, as in Section 5.4, as an if whose E branch is 't. Only x changes: y is the same in the hypothesis and in the conclusion. star-induction recurs on both halves of a pair, (if (atom x) x (cons (star-induction (car x)) (star-induction (cdr x)))) (scheme/j-bob.scm:868–872), so (star-induction x) gives two hypotheses, for (car x) and for (cdr x) (schemes.out has the claim, and Section 7.5 shows one).

The cases come from the body of the seed’s function. The prelude defines list-induction and star-induction (scheme/j-bob.scm:856–879) only to serve as seeds. The body of list-induction, on the left of Figure 7.1, has one question, (atom x), and one recursive call, on (cdr x). The question becomes the case split, and the call becomes the hypothesis.

Left: the body of list-induction (scheme/j-bob.scm:858–862). Right: the induction claim of (dethm memb?/remb (xs) (equal (memb? (remb xs)) 'nil)) with the seed (list-induction xs), as in memb-remb.out. The formal x becomes the seed’s variable xs.
Figure 7.1: Left: the body of list-induction (scheme/j-bob.scm:858–862). Right: the induction claim of (dethm memb?/remb (xs) (equal (memb? (remb xs)) 'nil)) with the seed (list-induction xs), as in memb-remb.out. The formal x becomes the seed’s variable xs.

The general rule. Let the theorem have body CC, and let the seed be (𝚐 y1…yk)(\mathtt{g}\ y_1 \ldots y_k), where g is a defun with formals z1…zkz_1 \ldots z_k. J-Bob replaces each ziz_i by yiy_i in the body of g and walks its spine (Section 6.2): the body, and recursively the branches of every if on the spine. Each leaf ℓ\ell is reached under a path condition Γℓ\Gamma_\ell, the questions answered true (A) or false (E) on the way. If (𝚐 a⃗1)(\mathtt{g}\ \vec a_1), …, (𝚐 a⃗m)(\mathtt{g}\ \vec a_m) are the recursive calls in those questions and in the leaf, the claim for the leaf is

Γℓ→(C[a⃗1/y⃗]∧⋯∧C[a⃗m/y⃗]→C), \Gamma_\ell \rightarrow \bigl(C[\vec a_1/\vec y] \land \dots \land C[\vec a_m/\vec y] \rightarrow C\bigr),

where C[a⃗/y⃗]C[\vec a/\vec y] is CC with each yiy_i replaced by the ii-th argument of the call. The claim is the tree of questions with these formulas at the leaves. Only the seed’s variables are replaced, so a variable outside the seed, such as y above, has the same value in the hypotheses as in the conclusion: the hypothesis is not strengthened. A hypothesis is the whole body CC, premises included.

The code. induction/claim (scheme/j-bob.scm:519–524) returns the body for the seed nil. Otherwise it looks up the seed’s function, and induction/defun (scheme/j-bob.scm:514–517) substitutes the seed’s arguments for its formals and calls induction/if:

scheme/j-bob.scm

501(defun induction/if (vars claim f e)
502  (if (if? e)
503    (implication
504      (induction/prems vars claim
505        (expr-recs f (if.Q e)))
506      (if-c-when-necessary (if.Q e)
507        (induction/if vars claim f (if.A e))
508        (induction/if vars claim f (if.E e))))
509    (implication
510      (induction/prems vars claim
511        (expr-recs f e))
512      claim)))

At an if (line 502) it collects with expr-recs the recursive calls in the question and puts their hypotheses above the if (lines 503–505), so that both branches get them. At a leaf (lines 509–512) it does the same with the calls anywhere in the leaf. induction/prems (scheme/j-bob.scm:494–499) makes one hypothesis per call, the claim with the seed’s variables replaced by the call’s arguments (line 498), and implication (scheme/j-bob.scm:124–129) nests the hypotheses as ifs whose E branch is 't.

induction/if never looks at what a leaf returns, only at the calls inside it, so the values of list-induction do not matter. Two helpers shared with totality/claim (Chapter 6) also shape the claim: expr-recs (scheme/j-bob.scm:428–451) merges equal calls with list-union, and if-c-when-necessary (scheme/j-bob.scm:112–113) drops an if whose branches came out equal. In schemes.scm the function fruit has the questions and calls of list-induction but returns other values ('banana, and conses of 'pear), and twice makes the call (twice (cdr x)) twice. Both are added with list-extend, only to build claims, and both give the claim of (list-induction x):

;; (fruit x), (twice x): the claim of (list-induction x)?
(t t)

7.3Seeds

Before any claim is built, seed? (scheme/j-bob.scm:320–329) checks the seed of every proof. It accepts nil, and for a dethm it calls induction-scheme? with the theorem’s formals:

scheme/j-bob.scm

303(defun induction-scheme-for? (def vars e)
304  (if (defun? def)
305    (if (arity? (defun.formals def) (app.args e))
306      (if (formals? (app.args e))
307        (subset? (app.args e) vars)
308        'nil)
309      'nil)
310    'nil))

induction-scheme? (scheme/j-bob.scm:312–318) requires an application and passes the definition that lookup finds for its name. That must be a defun (line 304) with the right number of arguments (line 305). The arguments must be distinct variables (formals?, scheme/j-bob.scm:241–248), each a formal of the theorem (line 307). schemes.scm tries one good seed and five bad ones on foo:

seed? (list-induction x) => t
seed? (list-induction z) => nil
seed? (car/cons x y) => nil
seed? (two x x) => nil
seed? (list-induction (cdr x)) => nil
seed? (list-induction x y) => nil

They fail, in order, because z is not a formal, car/cons is an axiom, a dethm and not a defun, x is repeated, (cdr x) is not a variable, and the arity is wrong. We infer that the variables must be distinct because line 498 replaces each seed variable by one argument: with (two x x) a hypothesis would need two values for the same x.

proof? calls seed? only after def? has accepted the theorem (scheme/j-bob.scm:379–388), and foo is not accepted, since bar is not defined. So schemes.scm tries the seeds with J-Bob/prove on (dethm atom-cons (x y) (equal (atom (cons x y)) 'nil)), over the prelude, with no steps:

;; J-Bob/prove of atom-cons, seed (list-induction x), no steps
(if (atom x)
  (equal (atom (cons x y)) 'nil)
  (if (equal (atom (cons (cdr x) y)) 'nil)
    (equal (atom (cons x y)) 'nil)
    't))
;; J-Bob/prove of atom-cons, seed (list-induction z)
'nil
;; J-Bob/prove of foo, seed (list-induction x)
'nil

With the good seed, J-Bob/prove returns the induction claim, which no step has rewritten. A refused seed makes the proof ill-formed, and the answer is 'nil (scheme/j-bob.scm:784–789; Section 5.2). An ill-formed theorem gives the same 'nil, whatever the seed.

The book uses four kinds of seed. shapes.scm has a helper, cases, that walks the body as induction/if does, merges branches where if-c-when-necessary does, and counts the hypotheses of each case, left to right; Table 7.1 copies these numbers. The row of shapes.out for (dethm set?/add-atoms (a bs) (if (set? bs) (equal (set? (add-atoms a bs)) 't) 't)) (scheme/little-prover.scm:777–778) is:

shapes.scm

;; set?/add-atoms, seed (add-atoms a bs)
cases 2, hypotheses per case (0 2)
recursive calls ((add-atoms (car a) (add-atoms (cdr a) bs)) (add-atoms (cdr a) bs))

The body of add-atoms is (if (atom x) (if (member? x ys) ys (cons x ys)) (add-atoms (car x) (add-atoms (cdr x) ys))) (scheme/little-prover.scm:653–656); the seed puts a for x and bs for ys. Of its three leaves, the two under the A branch of (atom a) make no call, give the same formula, and are merged. The third makes two calls, and the second argument changes too: one hypothesis has bs, the other (add-atoms (cdr a) bs). Chapter 8 explains why the book inducts along add-atoms and align.

Table 7.1: The four kinds of seed of the book: cases of the induction claim and hypotheses per case, left to right in the claim, from shapes.out. The seeds are at scheme/little-prover.scm:385, 451, 779, 1091.
theoremseedcaseshypotheses per case
memb?/remb(list-induction xs)20, 1
ctx?/t(star-induction x)20, 2
set?/add-atoms(add-atoms a bs)20, 2
align/align(align x)30, 1, 1

7.4A proof by list induction

memb-remb.scm is one line after the load, (replay 'dethm.memb?/remb 'memb?/remb), with the helper of Chapter 3 and Chapter 5. It prints the claim of Figure 7.1 and the claim after each of the 23 steps (scheme/little-prover.scm:381–423). Steps 1–6 close the base case, whose A branch is 't in the blocks below.memb-remb.out has 207 lines. The blocks of this subsection are the claims after steps 9 and 12 and the last three lines; the rest is in the file. They are the proof of memb?/remb0, except that step 2 takes the A branch of the unfolded (remb xs) with if-nest-A and the premise (atom xs), the question of the enclosing if, where memb?/remb0 evaluated (atom '()) and used if-true (scheme/little-prover.scm:294–297). So the base case takes 6 steps where memb?/remb0 took 7. In the E branch, step 7 unfolds (remb xs) and step 8 drops its atom case with if-nest-E. Step 9 is the if-lifting of Section 4.4: if-same, used backwards (Section 4.3), splits the call of memb? on the question (equal (car xs) '?):

memb-remb.scm

;; step 9: ((E A 1) (if-same (equal (car xs) '?) (memb? (if (equal (car xs) '?) (remb (cdr xs)) (cons (car xs) (remb (cdr xs)))))))
(if (atom xs)
  't
  (if (equal (memb? (remb (cdr xs))) 'nil)
    (equal (if (equal (car xs) '?)
             (memb? (if (equal (car xs) '?)
                      (remb (cdr xs))
                      (cons (car xs) (remb (cdr xs)))))
             (memb? (if (equal (car xs) '?)
                      (remb (cdr xs))
                      (cons (car xs) (remb (cdr xs))))))
           'nil)
    't))

Steps 10 and 11 drop the two copied inner questions with if-nest-A and if-nest-E, as steps 9 and 10 of memb?/remb1 do. The A branch of the (if (equal (car xs) '?) …) inside the conclusion is then (memb? (remb (cdr xs))), the left side of the hypothesis, and step 12 rewrites it to 'nil:

;; step 12: ((E A 1 A) (equal-if (memb? (remb (cdr xs))) 'nil))
(if (atom xs)
  't
  (if (equal (memb? (remb (cdr xs))) 'nil)
    (equal (if (equal (car xs) '?)
             'nil
             (memb? (cons (car xs) (remb (cdr xs)))))
           'nil)
    't))

The axiom equal-if is (if (equal x y) (equal x y) 't) (scheme/j-bob.scm:823–824): under the premise (equal x y), x may be rewritten to y. Here the premise is the induction hypothesis, the question of the if at path (E), and the path (E A 1 A) goes through that if’s A branch, so follow-prems accepts it (Section 5.4). Steps 13–18 unfold memb? on the cons, decide its first question with atom/cons and if-false, simplify with car/cons and cdr/cons, and drop the repeated question with if-nest-E. Step 19 uses the hypothesis again, and steps 20–23 collapse the claim to 't:

;; step 23: (() (if-same (atom xs) 't))
't
;; final: 't  PROVED

7.5Star induction

star-induction recurs on the car and the cdr, so it suits trees. In Chapter 7 of the transcript, (ctx? x) says whether the tree x contains ?: (if (atom x) (equal x '?) (if (ctx? (car x)) 't (ctx? (cdr x)))) (scheme/little-prover.scm:429–430). (sub x y), from Chapter 4, replaces every ? in the tree y by x (lines 245–246). The entry dethm.ctx?/sub (lines 447–617) proves two theorems by star induction; ctx-sub.scm prints their claims and their lengths. The first, (dethm ctx?/t (x) (if (ctx? x) (equal (ctx? x) 't) 't)), is a normalisation lemma: (ctx? x), when true, is 't. As a question, (ctx? x) only says that the value is not 'nil; replacing it by 't needs the equation of ctx?/t. The proof of ctx?/sub uses it in this way twice (scheme/little-prover.scm:543, 545).

The second theorem, ctx?/sub, is (if (ctx? x) (if (ctx? y) (equal (ctx? (sub x y)) 't) 't) 't) with the seed (star-induction y). By the rule of Section 7.2, each hypothesis is the whole theorem, so the premise (ctx? x), which does not mention y, appears four times in the claim: in the base case, in both hypotheses and in the conclusion. The two proofs are long, 35 and 58 steps:The excerpt is lines 21–38 of ctx-sub.out. Line 5 gives the 35 steps of ctx?/t, and lines 7–13 its induction claim.

ctx-sub.scm

[…]
;; ctx?/sub: steps
58
;; ctx?/sub: induction claim
(if (atom y)
  (if (ctx? x) (if (ctx? y) (equal (ctx? (sub x y)) 't) 't) 't)
  (if (if (ctx? x)
        (if (ctx? (car y)) (equal (ctx? (sub x (car y))) 't) 't)
        't)
    (if (if (ctx? x)
          (if (ctx? (cdr y))
            (equal (ctx? (sub x (cdr y))) 't)
            't)
          't)
      (if (ctx? x)
        (if (ctx? y) (equal (ctx? (sub x y)) 't) 't)
        't)
      't)
    't))
[…]

The first step, (() (if-same (ctx? x) C)) with CC the whole claim (scheme/little-prover.scm:507–519), makes the claim (if (ctx? x) C C). Every inner question (ctx? x) is now in an A branch or an E branch of the same question. Steps 2–9 remove the four inner questions on each side with if-nest-A and if-nest-E, and steps 10–12 collapse the E branch to 't. ctx-sub.scm applies these 12 steps and compares the A branch of the result with the claim that (star-induction y) gives for the theorem without its premise (ctx? x):Lines 103–113 of ctx-sub.out give the claim after the 12 steps; its E branch is 't.

;; A branch = claim of (if (ctx? y) ...) by (star-induction y)
t

Twelve steps thus move the premise about x out of the way. The other 46 prove the base case with ctx?/t and use the two hypotheses with equal-if (scheme/little-prover.scm:587, 610).

7.6Why the claim is enough

J-Bob contains no argument that the induction claim implies the theorem; it only builds the claim. The argument is short, and what follows is our reading of it. Suppose the claim of CC with the seed (𝚐 y⃗)(\mathtt{g}\ \vec y) is proved, but CC is false for some values of y⃗\vec y. Suppose also that g is total: its totality claim (Chapter 6) is true, so a natural-number measure decreases on every recursive call, under the answers to the questions that lead to the call. Among the values that make CC false, take one whose measure is smallest; one exists, because measures are naturals. Its answers to the questions pick one leaf of the claim, whose hypotheses are CC at the arguments of the calls. By the totality claim, under the same answers, these arguments have a smaller measure, so CC holds for them, and the leaf then makes CC true: a contradiction. The two claims are built along the same tree: totality/if (scheme/j-bob.scm:465–478) and induction/if (scheme/j-bob.scm:501–512) have the same shape and both take the calls from expr-recs, in the questions and at the leaves.Background. Taking an induction scheme from the recursion of an admitted function, justified by that function’s termination proof, is how Boyer and Moore’s prover instantiates the induction principle of their logic. ACL2 keeps it, and picks the scheme itself from the recursive functions in the goal (Chapter 11).Robert S. Boyer and J Strother Moore. A Computational Logic. Academic Press, New York, 1979

The argument needs g to be total, and the seed check does not check it: induction-scheme-for? wants only a defun of the given definitions. loop-scheme.scm tries (defun loop (x) (loop x)) with J-Bob/define, then adds it with list-extend, as dethm.contradiction adds partial (Section 6.1), and proves (dethm boom (x) (equal 't 'nil)) with the seed (loop x) in three steps, the first of which is ((A 1) (equal-if 't 'nil)):

loop-scheme.scm

;; J-Bob/define of loop with the measure (size x): names added
()
;; its totality claim: J-Bob/prove with no steps
(if (natp (size x)) (< (size x) (size x)) 'nil)
;; induction claim of boom by (loop x)
(if (equal 't 'nil) (equal 't 'nil) 't)
;; J-Bob/prove
't
;; J-Bob/define: names added
(boom)

J-Bob/define adds nothing for loop, and no list of steps could help: (< (size x) (size x)) is false for every x. Added without a proof, loop is a valid seed. Its body is one leaf with the call (loop x), so the hypothesis is the theorem itself, and the first step uses it. In our reading, induction is sound when the seed’s function was admitted by J-Bob/define with a proved totality claim and J-Bob/define itself is sound; definitions built another way are trusted by J-Bob/prove and J-Bob/define (Chapter 6). The transcript adds a defun this way only in dethm.contradiction (scheme/little-prover.scm:217), outside the chain. Chapter 10 shows two ways in which J-Bob/define itself is not sound.

7.7An unfinished proof

natp-len.scm admits (defun len (x) (if (atom x) '0 (+ '1 (len (cdr x))))) with the measure (size x)The four steps of the totality proof of len are in natp-len.scm; they answer exercise 3 of Chapter 12. and states (dethm natp/len (x) (equal (natp (len x)) 't)). With the seed (list-induction x), four steps close the base case: unfold len, if-nest-A, evaluate (natp '0), equal-same (the first trace of natp-len.out). In the step case, unfolding (len x) leaves (natp (+ '1 (len (cdr x)))). The axiom natp/+ says that a sum of two naturals is a natural, but its premises (natp '1) and (natp (len (cdr x))) must be questions of enclosing ifs (Section 5.4). Neither is. The hypothesis says the same thing about (len (cdr x)), but as the equation (equal (natp (len (cdr x))) 't), not as the question that natp/+ asks for, and (natp '1) is not asked at all. So step 3 changes nothing and J-Bob stops (the second trace starts from the claim after the base case):The excerpt is lines 52–58 of natp-len.out. Lines 1–51 give the name len added by J-Bob/define, the induction claim of natp/len, the first trace and the start of the second; line 59 says NOT PROVED.

natp-len.scm

[…]
;; step 2: ((E A 1 1) (if-nest-E (atom x) '0 (+ '1 (len (cdr x)))))
(if (atom x)
  't
  (if (equal (natp (len (cdr x))) 't)
    (equal (natp (+ '1 (len (cdr x)))) 't)
    't))
;; step 3: ((E A 1) (natp/+ '1 (len (cdr x))))  [no change: J-Bob stops here]

Building those premises is exercise 4 of Chapter 12. Chapter 8 follows the book through its other inductions, along add-atoms and align.