Chapter 5
Theorems, premises and the chain of definitions
Chapter 3 rewrote one expression with J-Bob/step, and Chapter 4 listed the equations a step may use.
This section turns a sequence of steps into a theorem.
It covers the format of a proof, what J-Bob/prove and J-Bob/define return, how the transcript passes its definitions from one entry to the next, how a conditional theorem finds its premises in the claim around the focus, why only an equal can rewrite, and how several proofs in one call see each other’s definitions.
Every proof written in this section has the seed nil; the two book proofs taken apart in Section 5.6 have induction seeds, and measures and induction come in Chapter 6 and Chapter 7.
5.1A proof and its claim#
The first theorem of the transcript says that first-of of a pair is its first element:
scheme/little-prover.scm
139(defun dethm.first-of-pair ()
140 (J-Bob/define (defun.second-of)
141 '(((dethm first-of-pair (a b)
142 (equal (first-of (pair a b)) a))
143 nil
144 ((1 1) (pair a b))
145 ((1) (first-of (cons a (cons b '()))))
146 ((1) (car/cons a (cons b '())))
147 (() (equal-same a))))))The entry is a function of no arguments.
It calls J-Bob/define on the definitions known so far, which the previous entry defun.second-of returns (Section 5.3), and on a list of proofs, here a single one.
A proof is a list (def seed step …).
def is the new defun or dethm, with a name not yet defined.
seed decides the claim to be proved: for a dethm with seed nil it is the body of the theorem (scheme/j-bob.scm:519–521); for a defun with seed nil it is the totality claim of Chapter 6, which is 't when the body makes no recursive call and 'nil otherwise (scheme/j-bob.scm:481–486).
So pair, first-of and second-of need no steps (scheme/little-prover.scm:121–137).
The steps are those of Chapter 3.
rewrite/prove (scheme/j-bob.scm:722–731) builds the claim and rewrites it with the steps; the helper replay prints the claim after each step:The first five lines of first-of-pair.out, which repeat the definition and the seed, are left out.
first-of-pair.scm
(replay 'dethm.first-of-pair 'first-of-pair);; claim
(equal (first-of (pair a b)) a)
;; step 1: ((1 1) (pair a b))
(equal (first-of (cons a (cons b '()))) a)
;; step 2: ((1) (first-of (cons a (cons b '()))))
(equal (car (cons a (cons b '()))) a)
;; step 3: ((1) (car/cons a (cons b '())))
(equal a a)
;; step 4: (() (equal-same a))
't
;; final: 't PROVEDStep 1 names the defun pair, so its equation is the definition instantiated at a and b, (equal (pair a b) (cons a (cons b '()))), and the focus at (1 1) is its left side.
Step 2 unfolds first-of in the same way, step 3 is car/cons with y instantiated to (cons b '()), and step 4 is equal-same.
The claim is now exactly 't: the theorem is proved.
5.2What the two entry points return#
J-Bob/prove and J-Bob/define run the same two checks before they rewrite anything (scheme/j-bob.scm:784–796): defs? on the definitions and proofs? on the proofs (scheme/j-bob.scm:389–396).
For each proof proofs? calls proof? (scheme/j-bob.scm:379–387), which checks the definition with def? (a fresh name, a body over its own formals), the seed with seed? and the steps with steps?.
A well-formed proof succeeds when its claim rewrites to exactly 't.
Here are the answers of J-Bob/prove for the theorem car-pair of Chapter 1, proved by ((1) (car/cons a b)) and (() (equal-same a)), and for some variants, one line per proof:Each line is one call (prove LABEL PROOF …) of prove-results.scm, where prove prints the label and the answer of J-Bob/prove on (prelude); the proofs are what the labels say.
prove-results.scm
complete: 't
last step left out: (equal a a)
no steps: (equal (car (cons a b)) a)
no-op step, then a good one: (equal (car (cons a b)) a)
no seed: 'nil
one argument missing: 'nil
name already defined: 'nil
c is not a formal: 'nil
no proofs at all: 'tThe first four proofs are well-formed.
Without its last step, the proof gives the residual claim (equal a a), true but not 't: J-Bob does no more rewriting once the steps run out; it only compares the claim with 't.
With no steps the residual is the claim itself, and so it is when the first step changes nothing (a no-op step), since rewrite/continue then stops and skips the later steps (scheme/j-bob.scm:708–714).
The next four are ill-formed and get 'nil (scheme/j-bob.scm:788): a proof needs a definition and a seed (list2-or-more?, scheme/j-bob.scm:372–377); (car/cons a) has one argument too few (step-args?, scheme/j-bob.scm:269–282); the name car/cons is taken (def? wants undefined?, scheme/j-bob.scm:346–353); and (dethm t1 (a) (equal c c)) mentions a variable that is not a formal (def-contents? calls expr? with the formals, scheme/j-bob.scm:341–344).
An empty list of proofs has no failure, so it gives 't (scheme/j-bob.scm:740–741).
J-Bob/define answers with a list of definitions.
As in Chapter 1, a proved definition is added at the end (rewrite/define, scheme/j-bob.scm:747–751, with list-extend) and a failure leaves the list as it was.
The program define-results.scm also tries several proofs in one call: ok, the proof of car-pair above; bad, a proof with no steps of (dethm other (a) (equal (cdr (cons a a)) a)); and ok2, a complete proof of the same statement under the name other2.
D1 is the list that J-Bob/define returns for ok alone:Left out: the first three lines of define-results.out, which repeat the lengths 23 and 24 of Chapter 1.
define-results.scm
[…]
ok, ok2: added: (car-pair other2)
ok, bad, ok2: added: (car-pair)
ok again, on D1: length: 24With several proofs, J-Bob/define keeps those before the first failure and drops the rest, ok2 included (Section 5.6).
Proving car-pair again on D1 is ill-formed, because the name is taken.
Table 5.1 collects the answers of the three entry points.
scheme/j-bob.scm:775–796). : a well-formed proof whose claim is or becomes 'nil gives the same answer as an ill-formed one (nil-ambiguity.scm).| entry point | proved | well-formed, stuck | ill-formed |
|---|---|---|---|
J-Bob/step | 't | the claim when the first no-op step is reached (later steps are skipped) | the claim |
J-Bob/prove | 't | the residual of the last failing proof, maybe 'nil | 'nil |
J-Bob/define | defs + def | defs + the proofs before the first failure | the defs |
An ambiguous 'nil. The 'nil of an ill-formed proof is also a possible residual claim.
Each row of nil-ambiguity.scm gives the answer of J-Bob/prove, the length of the list J-Bob/define returns, and the answer of proofs?, for the recursive (defun partial (x) (if (partial x) 'nil 't)), for (dethm z () 'nil), both with seed nil and no steps, and for a proof whose step is (() (frob x)):
nil-ambiguity.scm
recursive defun, seed nil: ('nil 23 t)
the theorem 'nil, no steps: ('nil 23 t)
a step that names no definition: ('nil 23 nil)The first two are well-formed: a recursive function with seed nil has the claim 'nil (scheme/j-bob.scm:481–486), and so has the theorem z, with no step to change it.
The third is ill-formed, since nothing is called frob.
J-Bob/define refuses all three, so it tells success from failure, but not one failure from another.
Only proofs?, called directly, separates the ill-formed proof from the unproved ones; it is the first thing to try when a proof gives 'nil.
5.3The chain of definitions#
Each of the 31 entries of the chain is a function of no arguments that calls the entry before it to get its definitions, as dethm.first-of-pair calls defun.second-of; the first, defun.pair, starts from (prelude) (scheme/little-prover.scm:121–125).
The three side entries, dethm.contradiction, dethm.set?/atoms.attempt and defun.align.attempt, call J-Bob/prove (scheme/little-prover.scm:216, 677, 895), and no entry calls them: the entry after each one calls the entry before it (scheme/little-prover.scm:234, 690, 918).
The definitions are never stored: each call recomputes them, and (dethm.align/align) rechecks the whole book (about 0.2 s; Chapter 8).
This program calls the eight entries of Chapter 3 of the transcript in order and prints, for each, how many definitions it returns and which names it adds:
chain.scm
defun.pair 24 (pair)
defun.first-of 25 (first-of)
defun.second-of 26 (second-of)
dethm.first-of-pair 27 (first-of-pair)
dethm.second-of-pair 28 (second-of-pair)
defun.in-pair? 29 (in-pair?)
dethm.in-first-of-pair 30 (in-first-of-pair)
dethm.in-second-of-pair 31 (in-second-of-pair)
;; the definition of pair, as data
(defun pair (x y) (cons x (cons y '())))The functions of the book exist only as data in this list: J-Bob never runs pair, it unfolds its definition in claims.
Definitions can be unfolded in any order.
first-of-pair unfolds the inner pair first.
second-of-pair starts with the outer function and then reaches pair at the longer path (1 1 1) (scheme/little-prover.scm:154–155):
second-of-pair.scm
;; step 1: ((1) (second-of (pair a b)))
(equal (car (cdr (pair a b))) b)
;; step 2: ((1 1 1) (pair a b))
(equal (car (cdr (cons a (cons b '())))) b)in-second-of-pair needs one more idea.
Unfolding in-pair? (scheme/little-prover.scm:162–163) brings an if into the claim:Left out: the first nine lines (definition, seed, claim and step 1), the 19 lines of steps 3 to 7, and the last three lines; the whole trace is in in-second-of-pair.out.
in-second-of-pair.scm
;; step 2: ((1) (in-pair? (cons a (cons '? '()))))
(equal (if (equal (first-of (cons a (cons '? '()))) '?)
't
(equal (second-of (cons a (cons '? '()))) '?))
't)
[…]
;; step 8: ((1 E) (equal-same '?))
(equal (if (equal a '?) 't 't) 't)
;; step 9: ((1) (if-same (equal a '?) 't))
(equal 't 't)Unfolding in-pair? leaves 't in the A branch.
Steps 3 and 4 reduce the question to (equal a '?), at the paths (1 Q 1), and steps 5 to 8 rewrite the E branch to 't, at paths that start with (1 E) (scheme/little-prover.scm:186–191).
The proof cannot know whether a is '?, and it does not need to: step 9 uses if-same, (equal (if x y y) y), to drop a question whose two answers are the same.
When a step of such a proof is wrong, the theorem is missing from the chain.
Here the second step of first-of-pair has the path (2) instead of (1):Left out: the five lines of the trace before its last two, and the last line; the whole output is in wrong-path.out.
wrong-path.scm
;; J-Bob/prove, path (2)
(equal (first-of (cons a (cons b '()))) a)
;; lengths: before, path (1), path (2)
(26 27 26)
[…]
;; 2 later steps not appliedThe residual is the claim after step 1, and the trace stops at step 2, the first no-op step: the focus at (2) is a, which matches neither side of the equation of first-of.
J-Bob/define returns the 26 definitions it was given, and a later proof that named first-of-pair in a step would be ill-formed: a step may name only a theorem already in the definitions (Section 5.6).
5.4Premises#
Chapter 4 showed that if-nest-A needs the focus in the A branch of an if on x; here is the rule for every conditional theorem.
cons/car+cdr says that a pair can be rebuilt from its car and its cdr, which is false for an atom:
scheme/j-bob.scm
819 (dethm cons/car+cdr (x)
820 (if (atom x)
821 't
822 (equal (cons (car x) (cdr x)) x)))Its equal is in the E branch of an if on (atom x).
That question is a premise: to use the equation, a step must know that (atom x) is false.
J-Bob discharges the premise when the focus lies in the E branch of an if of the claim that asks (atom x), as in Figure 5.1.
Figure 5.1: Top: the claim (if (atom x) 't (cons (car x) (cdr x))), focus at (E) (boxed). Bottom: cons/car+cdr instantiated by the step (cons/car+cdr x). Both E branches sit under the same question, so follow-prems reaches the equation, which rewrites the focus to x.
follow-prems walks into the instantiated theorem (its body with the step’s arguments for its formals): if it is an if whose question is a discharged premise, it enters the matching branch and repeats; otherwise it stops there:
scheme/j-bob.scm
604(defun follow-prems (path e thm)
605 (if (if? thm)
606 (if (prem-A? (if.Q thm) path e)
607 (follow-prems path e (if.A thm))
608 (if (prem-E? (if.Q thm) path e)
609 (follow-prems path e (if.E thm))
610 thm))
611 thm))prem-A? walks the claim e along the path and answers 't as soon as it leaves, by direction A, an if whose question equals the premise:
scheme/j-bob.scm
578(defun prem-A? (prem path e)
579 (if (atom path)
580 'nil
581 (if (equal (car path) 'A)
582 (if (equal (if.Q e) prem)
583 't
584 (prem-A? prem (cdr path)
585 (find-focus-at-direction (car path)
586 e)))
587 (prem-A? prem (cdr path)
588 (find-focus-at-direction (car path)
589 e)))))prem-E? is the same with E (scheme/j-bob.scm:591–602).
If follow-prems stops at an if, the result is not an equal and the step does nothing (Section 5.5).
premises.scm tries one step in thirteen claims; it prints the new claim, or unchanged:Each row is one call (at LABEL CLAIM STEP) of premises.scm, which prints the label and what J-Bob/step makes of CLAIM with the one step on (prelude). Left out: the two if-nest rows, which repeat Chapter 4.
premises.scm
E branch: (if (atom x) 't x)
no if: unchanged
A branch: unchanged
an if further up: (if (atom x) 't (if a x b))
same value, other question: unchanged
[…]
equal-if in A: (if (equal a b) b c)
equal-if in Q: unchanged
natp/+: (if (natp a) (if (natp b) 't 'x) 'y)
natp/+, other order: (if (natp b) (if (natp a) 't 'x) 'y)
natp/+, one premise: unchanged
A and E on the same question: unchangedThe premise must be the question of an if that the path goes through: true if the path goes on into A, false if into E; with no such if, or with the wrong branch, nothing happens.
Any ancestor of the focus will do, not only its parent: in (if (atom x) 't (if a (cons (car x) (cdr x)) b)) the step at (E A) works.
The match is syntactic: (atom (cdr (cons y x))) has the value of (atom x) but is another expression.
A path into Q discharges nothing: in (if (equal a b) a c), equal-if rewrites the a at (A) but not the one at (Q 1).
natp/+ has two premises, (if (natp x) (if (natp y) (equal (natp (+ x y)) 't) 't) 't) (scheme/j-bob.scm:839–844); the claims of its rows rewrite (natp (+ a b)) at (A A) or (A).
They are discharged one after the other, each anywhere on the path, so their order does not matter; if one is missing, nothing happens.
In the last row the path (A E) leaves an if on (atom x) by A and another by E.
follow-prems tries prem-A? first (scheme/j-bob.scm:606), so it enters the 't branch of cons/car+cdr and the step changes nothing.
equal-if, (if (equal x y) (equal x y) 't), rewrites x to y under a question (equal x y).
In the transcript that question is written in the claim (scheme/little-prover.scm:93–99), comes from unfolding a definition (lines 454–456, in ctx?/t), or is an induction hypothesis (Chapter 7).
5.5Only equal rewrites#
A theorem rewrites the focus only through an equal.
The function equality/equation receives the instantiated theorem after follow-prems has discharged the premises it can, and uses it only when it is an application of equal (app-of-equal?, scheme/j-bob.scm:657–660):
scheme/j-bob.scm
669(defun equality/equation (focus concl-inst)
670 (if (app-of-equal? concl-inst)
671 (equality focus
672 (elem1 (app.args concl-inst))
673 (elem2 (app.args concl-inst)))
674 focus))Yet def? accepts any expression as the body of a theorem, so a theorem can be proved and still be useless as a step.
equal-leaves.scm admits (dethm nat-size (x) (natp (size x))), proved by the single step (() (natp/size x)), and then rewrites (natp (size a)) with it and with the axiom natp/size, (equal (natp (size x)) 't):
equal-leaves.scm
admitted: (nat-size)
step with nat-size: (natp (size a))
step with natp/size: 't
theorems, leaves, (equal ...) leaves, 't leaves: (39 56 39 17)
theorems with a number of (equal ...) leaves other than 1: 0Both theorems say the same thing; only the one stated with equal rewrites.
The last two lines read the body of each theorem as a tree of ifs, whose leaves are what is left after going only into A and E branches.
Of the 39 theorems in (dethm.align/align), the 21 axioms and the 18 theorems of the book, each has exactly one equal leaf, and the other 17 leaves are 't, where a premise fails.
So a fact about a predicate P is stated as (equal (P x) 't), as in natp/size.
The dethm macro of the ACL2 files, which no file of the repository calls (Chapter 11), makes the same choice: it turns each equal leaf into a rewrite rule and gives nothing for any other leaf (if->implies, acl2/j-bob-lang.lisp:3–13; line 13 returns '()).Background. ACL2 itself does make a rewrite rule from a theorem whose conclusion p is not an equality: it treats p as (iff p t). The J-Bob macro passes its own :rule-classes (acl2/j-bob-lang.lisp:18–19).Matt Kaufmann and J Strother Moore. ACL2 Documentation. The ACL2 home page, https://www.cs.utexas.edu/~moore/acl2/. The topics DEFUN, DEFTHM, RULE-CLASSES, INDUCTION, ORDINALS and PROOF-BUILDER
5.6Lemmas, and several proofs in one call#
A theorem proved by J-Bob/define is an ordinary dethm in the definitions, so a later step names it exactly as it names an axiom.
Within one call, each proof is checked and rewritten with the definitions of the proofs before it (proofs?, scheme/j-bob.scm:393–395; rewrite/prove+, scheme/j-bob.scm:744).
ordering.scm has six proofs: ok, bad and ok2 of define-results.scm, ok without its last step, a proof that uses car-pair, and one that names itself.
Each row of its output names the proofs given to one call.
ordering.scm
[…]
(define thm '(dethm car-pair (a b) (equal (car (cons a b)) a)))
(define ok (list thm 'nil '((1) (car/cons a b))
'(() (equal-same a))))
(define unfinished (list thm 'nil '((1) (car/cons a b))))
(define uses '((dethm car-pair2 (a) (equal (car (cons a a)) a))
nil ((1) (car-pair a a)) (() (equal-same a))))
(define bad '((dethm other (a) (equal (cdr (cons a a)) a)) nil))
(define ok2 '((dethm other2 (a) (equal (cdr (cons a a)) a)) nil
((1) (cdr/cons a a)) (() (equal-same a))))
(define self '((dethm self (a) (equal (car a) (car a))) nil
(() (self a))))
[…]prove ok, uses: 't
prove uses: 'nil
prove uses, car-pair added unproved: 't
prove unfinished, uses: (equal a a)
define unfinished, uses: added: ()
prove ok, bad, ok2: (equal (cdr (cons a a)) a)
prove unfinished, bad: (equal (cdr (cons a a)) a)
define ok, bad, ok2: added: (car-pair)
prove self: 'nilAlone, uses is ill-formed: no definition is called car-pair.
After ok it passes, and it passes as well when car-pair is put into the definitions with list-extend and no proof: J-Bob/prove trusts the definitions it is given (Chapter 6 shows where that leads).
After unfinished it passes too, on a car-pair that was never proved: J-Bob/prove reports only the residual (equal a a) of unfinished, and J-Bob/define adds nothing.
A theorem cannot be used in its own proof: the steps of a dethm are checked without it (extend-rec adds only defuns, scheme/j-bob.scm:331–339), so (self a) names nothing.
The two entry points read a list of proofs in opposite directions.
rewrite/prove+ (scheme/j-bob.scm:739–745) first recurses on the rest of the list, with the current definition added by list-extend whether it is proved or not, and rewrite/prove+1 (scheme/j-bob.scm:733–737) checks a proof only if the proofs after it gave 't, so J-Bob/prove reports the last failure: bad in ok, bad, ok2, and again bad when unfinished fails before it.
rewrite/define+ starts from the head instead, and rewrite/define+1 stops at the first proof that adds nothing (scheme/j-bob.scm:753–773).
The book’s entry dethm.ctx?/sub (scheme/little-prover.scm:447–617) proves the lemma ctx?/t and then ctx?/sub, whose steps use ctx?/t twice (lines 543 and 545).
Both are induction proofs, with the seeds (star-induction x) and (star-induction y) (lines 451 and 506), so their claims are built as in Chapter 7.
ctx-sub.scm keeps each definition and seed and takes all the steps away from one proof or both:
ctx-sub.scm
base length: 43
seeds: ((star-induction x) (star-induction y))
define good, good: added: (ctx?/t ctx?/sub)
define good, broken: added: (ctx?/t)
define broken, good: added: ()
prove (broken, good) equals prove (broken): t
prove (broken, broken) equals prove (broken): nilWith the lemma broken, J-Bob/define adds nothing, though the theorem’s own steps are fine.
J-Bob/prove reports the last failure, so when ctx?/sub fails too, its residual comes out instead of the lemma’s, and the answer changes (last row).
With only the lemma broken the answer is the lemma’s residual, so ctx?/sub passed on a lemma that was never proved.
The side entry dethm.set?/atoms.attempt shows the same effect (Chapter 8).
For a chain of trusted definitions only J-Bob/define counts.
Apart from partial, which failed, every function proved in this section is non-recursive, and only the proofs of ctx-sub.scm have an induction seed.
A recursive function needs a measure as its seed (Chapter 6).