Chapter 12
Exercises and further reading
This last section has fifteen exercises and a list of further reading. The exercises write new proofs (1–6, 14), build tools around the checker (7, 9, 15), probe its soundness and its two hosts (8, 12, 13), and read code and output (10, 11). Each exercise whose answer can be run has a solution in examples/12-exercises/, in a file named after its number, such as ex04-natp-len.scm. The exercises name the files but do not print them; Section 12.2 shows what J-Bob made of each one.Several solutions define host functions; those of 7, 9 and 15 are tools. As in jbob.scm, they never use if, which once J-Bob’s language is imported is J-Bob’s macro, where only 'nil is false and #f counts as true (scheme/j-bob-lang.scm:7–8, 20–23); they use cond and compare J-Bob’s answers with 't. Solution 13 redefines the J-Bob functions num and natp, and summary.scm puts the originals back after it.
12.1Exercises#
Inner first. The book proves
first-of-pairby unfolding the innerpairfirst (scheme/little-prover.scm:139–147) andsecond-of-pairby unfolding the outersecond-offirst (scheme/little-prover.scm:149–158). Provesecond-of-pairinner first, with the definitions(defun.second-of), starting with((1 1) (pair a b)), and compare the number of steps. (ex01-second-of-pair.scm)Fewest steps. Prove
(dethm cadr-cons (a b c) (equal (car (cdr (cons a (cons b c)))) b))against(prelude)in as few steps as you can, and argue that no shorter proof exists. A step replaces its focus by the other side of one instance of one equation: a theorem, a definition read as(equal (f x …) body), or the evaluation of a primitive (equality/def,scheme/j-bob.scm:684–702;equality,scheme/j-bob.scm:662–667). (ex02-cadr-cons.scm)A total
len. Define(defun len (x) (if (atom x) '0 (+ '1 (len (cdr x)))))with the measure(size x), print its claim withtotality/claim(scheme/j-bob.scm:480–492), prove it (the proof oflist?,scheme/little-prover.scm:233–241, has the same shape), and addlenwithJ-Bob/define. What is the claim with the seednil? (Section 7.7 uses these steps innatp-len.scm; redo them without looking.) (ex03-len.scm)natp/len. Finish the proof that Section 7.7 leaves stuck, where step 3 of the second trace of07-induction/natp-len.outis a no-op: withlendefined, prove(dethm natp/len (x) (equal (natp (len x)) 't))with the seed(list-induction x). In the step case the focus becomes(natp (+ '1 (len (cdr x)))), whichnatp/+(scheme/j-bob.scm:839–844) fits only if the focus lies in the A branch of anifasking(natp '1)and of one asking(natp (len (cdr x))): these are its premises (follow-prems,scheme/j-bob.scm:604–611; Chapter 5). Build them backwards.if-trueread from right to left turns a claimGinto(if 't G 't). That'tcan be rewritten, again backwards, into(natp '1)by evaluation, or into(natp (len (cdr x)))byequal-if, with the induction hypothesis as premise. (ex04-natp-len.scm)Measures. Section 6.5 showed that
(defun f (x) (if (atom x) x (f (cdr x))))is total by(size x), with the four steps of the proof oflist?, and that the measurexgives a claim that is false at a cons. Show that(size (cdr x))cannot work either: print its totality claim and evaluate it at a cons (in the host,evala claim inside aletthat bindsx). Then prove totality with another measure, such as(+ '1 (size x)), which needsnatp/+as in exercise 4, thencommute-+andcommon-addends-<(scheme/j-bob.scm:837–838,scheme/j-bob.scm:851–852). (ex05-measure.scm)Associativity of append. Define
(defun app (x y) (if (atom x) y (cons (car x) (app (cdr x) y))))and prove(dethm app/app (a b c) (equal (app (app a b) c) (app a (app b c))))with the seed(list-induction a).Background. As we remember it, the associativity of a functionappis the first theorem of the introductory tours of the ACL2 documentation. We did not check the current text.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 In the step case, unfold the innerappand drop itsifwithif-nest-E, then unfold the outer one, clean up withatom/cons,if-false,car/consandcdr/cons, unfold the right side and drop itsifthe same way, and use the hypothesis withequal-if. The base case is the same withif-nest-A. (ex06-app.scm)Which step stopped?
rewrite/continue(scheme/j-bob.scm:708–714) stops silently at the first step that changes nothing, andJ-Bob/step(scheme/j-bob.scm:775–782) returns the claim unchanged if any step failsstep?(scheme/j-bob.scm:289–294). Write a host function(diagnose defs claim steps)that reports the first ill-formed step, or else the first step that changes nothing, or else the final claim, using J-Bob’sstep?andrewrite/step(scheme/j-bob.scm:704–706). (ex07-first-no-op.scm)Reserved names. Section 10.3 makes
undefined?(scheme/j-bob.scm:138–141) refuse the names of the nine primitives. Should it refuseif,quote,defunanddethmtoo? Admit(defun quote (x) x),(defun quote (x y) x),(defun if (x y z) x),(defun if (x y) x),(defun defun (x) x)and(defun dethm (x) x), and try to use each of the first four to rewrite a constant or anif. Readquote?,if?andapp?(scheme/j-bob.scm:27–28,scheme/j-bob.scm:32–33,scheme/j-bob.scm:39–46),step?andquoted-exprs?(scheme/j-bob.scm:262–267). (ex08-keywords.scm)An untrusted tactic. Write a host function
(auto defs claim)that finds a focus wherecar/cons,cdr/cons,if-true,if-false,if-same,equal-sameor the evaluation of a primitive on quoted arguments fits, applies it withrewrite/step, repeats, and returns the steps it found. Finishfirst-of-pairwith it after the book’s first two steps, which unfoldpairandfirst-of(scheme/little-prover.scm:139–147), check the whole proof withJ-Bob/prove, then spoil a found step and check again.Background. Keeping proof search out of the trusted checker is the de Bruijn criterion: proofs can be checked by a program small enough to verify by hand. ACL2 searches for proofs itself (Chapter 11).Henk Barendregt and Freek Wiedijk. The Challenge of Computer Mathematics. Philosophical Transactions of the Royal Society A, 363(1835):2351–2375, 2005 (ex09-auto.scm)Read the attempt.
dethm.set?/atoms.attempt(scheme/little-prover.scm:676–687) givesJ-Bob/proveproofs ofset?/add-atomsandset?/atoms, and returns a claim that is not't(Chapter 8). Which proof is it the residual claim of, and why? Readrewrite/prove+(scheme/j-bob.scm:739–745) andrewrite/prove+1(scheme/j-bob.scm:733–737). (On paper;ex10-attempt.scmchecks the answer.)Rules ACL2 might refuse. The ACL2
dethmmacro (acl2/j-bob-lang.lisp:15–19) turns everyequalleaf of a theorem into a:rewriterule (if->implies,acl2/j-bob-lang.lisp:3–13). Start from11-acl2/dethm-expand.out(Section 11.3), which lists the rules by the shape of their left-hand side. Which of the rules generated from the 21 axioms would ACL2 likely reject or treat specially? Look atequal-if, at the axioms whose left-hand side is anif, and atequal-swapandcommute-+. The answer is background, to be checked against the documentation of rule classes 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; nothing here runs ACL2. (On paper.)'nilis not'(). In the Scheme host the symbolniland the empty list are different data, andequalon constants is the host’sequal?(scheme/j-bob-lang.scm:13, throughbinary-op,scheme/j-bob.scm:626–635).Background. In ACL2 the symbolnilis the empty list, and the only false value.Matt Kaufmann, Panagiotis Manolios, and J Strother Moore. Computer-Aided Reasoning: An Approach. Kluwer Academic Publishers, Boston, 2000 Find a theorem without variables that J-Bob proves in the Scheme host but that is false when read in ACL2. (ex12-nil.scm)A second float proof. Section 10.4 proves
boom,(equal 't 'nil), with floats andassociate-+. Proveboomwithcommon-addends-<(scheme/j-bob.scm:851–852); look for a floatzfor which(+ '0 z)and(+ '1 z)evaluate to the same constant. Then apply Fix 2 of Section 10.4 tonumandnatp(scheme/j-bob-lang.scm:6,scheme/j-bob-lang.scm:14–15) and check that the proof fails. (ex13-float-less.scmredefinesnumandnatp.)Not an equation. Section 5.5 admitted
(dethm nat-size (x) (natp (size x)))and found that a step with it changes nothing. Explain this withapp-of-equal?andequality/equation(scheme/j-bob.scm:657–660,scheme/j-bob.scm:669–674). Then admit the theorem restated withequal, asnat-size=, and use it at the question of(if (natp (size a)) 'yes 'no)to reach'yes. (ex14-nat-size.scm)Two kinds of
'nil.J-Bob/prove(scheme/j-bob.scm:784–789) returns'nilboth for ill-formed input and for a proof that leaves the claim'nil(Chapter 5). Write(prove/why defs pfs), which runsdefs?(scheme/j-bob.scm:364–370) andproofs?(scheme/j-bob.scm:389–396) itself beforerewrite/prove+and says which case occurred. (ex15-prove-why.scm)
12.2Checking the solutions#
A proof in a solution is right when J-Bob accepts it, whatever this text says; a tool is checked by the cases its .out prints. Each solution ends by defining a short verdict, and summary.scm runs all of them again and prints the verdicts, one line per solution:
summary.scm
ex01 ('t steps 5 book 5)
ex02 ('t steps 3)
ex03 ('t steps 4 added (len))
ex04 ('t steps 16 added (len natp/len))
ex05 (x nil cdr nil 't steps 13)
ex06 ('t steps 18 added (app app/app))
ex07 ((proved 4) (no-op-step 2) (ill-formed-step 3) (stuck-after 3))
ex08 ((quote quote if if) no no no no)
ex09 (found 2 't wrong (equal (car (cons a (cons b '()))) a))
ex10 (second 't first-residual? #t)
ex12 ('t steps 2)
ex13 (before 't after (equal (< '0 '1) 'nil))
ex14 ((natp (size a)) 't)
ex15 (proved stuck stuck ill-formed ill-formed ill-formed)A 't is the answer of J-Bob/prove, steps N is the length of the solution’s proof (book 5: the book’s), and added lists the names that J-Bob/define adds to (prelude); no and #t are host answers to the questions the solution asks. Exercise 11 is on paper and has no line.
1. The inner-first proof is as long as the book’s.
2. Two steps are not enough, by an argument rather than a run. The claim has no if, so no premise can be met. A proof ends at 't, which has no subexpression, so the last step rewrites the whole claim with an equation without premises. The claim before it is then an instance of (equal X X), (natp (size X)), (car (cons 't Y)), (cdr (cons X 't)), (if X 't 't), (if 't 't Y) or (if 'nil X 't), or a primitive applied to constants with the value 't. One step cannot make the claim one of these. Below the top, a step changes one side of the equal and leaves the other, which holds a variable; the sides become the same only if the step turns one into the other, and no equation without premises has (car (cdr (cons a (cons b c)))) and b as its two sides. At the top, the equations that fit are equal-swap, which only swaps the sides, and car/cons, cdr/cons, if-same, if-true and if-false read backwards, which leave the claim itself where those shapes need 't.
3. With the seed nil, the claim is 'nil (scheme/j-bob.scm:481–486), which is false.
4. The first six steps of the solution repeat those of natp-len.scm: its four base-case steps and the first two of its step case. Step 7 wraps the focus in (if 't G 't), and step 8 turns that 't into the first premise:
ex04-natp-len.scm
;; step 7: ((E A) (if-true (equal (natp (+ '1 (len (cdr x)))) 't) 't))
[…]
;; step 8: ((E A Q) (equal-if (natp (len (cdr x))) 't))
(if (atom x)
't
(if (equal (natp (len (cdr x))) 't)
(if (natp (len (cdr x)))
(equal (natp (+ '1 (len (cdr x)))) 't)
't)
't))Steps 9 and 10 build (natp '1) the same way, natp/+ fits at step 11, and equal-same and if-same close the proof.
5. In the verdict, x and cdr name the measures x and (size (cdr x)). At '(a) both claims evaluate to the symbol nil, a host value, so it prints without a quote. The first fails at its test (natp x); the second at (< (size (cdr (cdr x))) (size (cdr x))), since both sizes are 0:
ex05-measure.scm
;; totality claim, measure x
(if (natp x) (if (atom x) 't (< (cdr x) x)) 'nil)
[…]
;; at x = '(a): (natp x) (atom x) (size (cdr x)) (size (cdr (cdr x)))
(nil nil 0 0)
;; J-Bob/prove, measure (size x)
'tIf every axiom holds for the host’s values, a claim that is false at '(a) has no proof (an inference; Section 10.4 shows axioms that floats falsify). The last two lines are the proof by (size x). The measure (+ '1 (size x)) takes 13 steps.
7. The four verdicts diagnose, in order, the book’s steps, the same steps with step 2 aimed at (2), with step 3 naming an unknown car/kons, and with the last step left out. For the second and the third, J-Bob/step returns
ex07-first-no-op.scm
;; J-Bob/step, step 2 aimed at (2)
(equal (first-of (cons a (cons b '()))) a)
;; J-Bob/step, unknown theorem
(equal (first-of (pair a b)) a)the claim after step 1 in one case and the original claim in the other, and nothing says which step was at fault.
8. All six definitions are admitted (the verdict lists the names of the first four), and no means that the claim did not change. The four attempts fail: a step naming (quote a) (a constant, not an application, so it fails step?), the evaluation of (car (quote 'a 'b)) (its argument is an application of the new quote, not a constant), the unfolding of (if 'nil 'a 'b) (an if with three arms is never an application), and if-same on the two-armed (if 'a 'b) (the sides of if-same have three arms). We found no exploit, and we infer that none goes through expressions: exprs? tests quote? and if?, which look at shape only, before it looks anything up (scheme/j-bob.scm:166–187). Refusing these names would close no hole that we found.
9. The tactic finds 2 steps and J-Bob accepts the proof; a wrong instance, car/cons applied to b, changes nothing, and J-Bob returns the claim at that point.
10. rewrite/prove+ recurses into the later proofs first, with the definitions extended by the earlier theorem, proved or not (scheme/j-bob.scm:743–744), and rewrite/prove+1 rewrites a proof only when everything after it gave 't (scheme/j-bob.scm:734). So set?/atoms passes first, using the unproved set?/add-atoms, and the residual claim of set?/add-atoms is returned.
11. The run shows only which rules have a variable (equal-if) or an if (the five if axioms) as their left-hand side. What ACL2 does with them, and with equal-swap and commute-+, is the unchecked background of the sidenote in Section 11.3.
12. (dethm nil-is-not-empty () (equal (equal 'nil '()) 'nil)) takes two evaluations. In ACL2 'nil and '() are one constant (background), so we infer that the ACL2 copy of J-Bob would evaluate the first step to (equal 't 'nil) and stop; we did not run it.
13. With z the float 1e16, both sums are the same float in CHICKEN, so (< (+ '0 z) (+ '1 z)) evaluates to 'nil:
ex13-float-less.scm
;; host: (+ 0 1e16) (+ 1 1e16)
(10000000000000000.0 10000000000000000.0)After the fix, num reads the float as 0, so the sums evaluate to '0 and '1, and step 5 finds (< '0 '1) at its path:
;; step 4: ((1 2) (+ '1 '10000000000000000.0))
(equal (< '0 '1) 'nil)
;; step 5: ((1) (< '10000000000000000.0 '10000000000000000.0)) [no change: J-Bob stops here]14. The step with nat-size changes nothing (the verdict’s first entry): equality/equation returns the focus when the body of the theorem, past its premises, is not an equal (Section 3.2 and Section 5.5). With the body (equal (natp (size x)) 't), as nat-size=, the same step gives 't (the second entry); ex14-nat-size.out also shows nat-size= and if-true turning (if (natp (size a)) 'yes 'no) into 'yes.
15. The two stuck cases are (dethm z () 'nil) without steps and a recursive defun with the seed nil; the ill-formed ones are an unknown theorem in a step, a name already defined, and definitions with a duplicate. J-Bob/prove answers 'nil to all five.
12.3Further reading#
Background — not run here. The descriptions are ours, from memory. The book: Friedman and Eastlund Daniel P. Friedman and Carl Eastlund. The Little Prover. MIT Press, Cambridge, MA, 2015; little-prover.scm transcribes its proofs (Chapter 8). The Boyer–Moore logic: Boyer and Moore Robert S. Boyer and J Strother Moore. A Computational Logic. Academic Press, New York, 1979, a logic of total recursive functions with induction, and the manual of their prover, Nqthm Robert S. Boyer and J Strother Moore. A Computational Logic Handbook. Academic Press, second edition, 1998. ACL2: Kaufmann, Manolios and Moore Matt Kaufmann, Panagiotis Manolios, and J Strother Moore. Computer-Aided Reasoning: An Approach. Kluwer Academic Publishers, Boston, 2000, a textbook; Kaufmann and Moore Matt Kaufmann and J Strother Moore. A Precise Description of the ACL2 Logic, 1997. Department of Computer Sciences, University of Texas at Austin, a precise description of the logic; and the ACL2 documentation 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, in particular the topics DEFUN, DEFTHM, RULE-CLASSES, INDUCTION, ORDINALS and PROOF-BUILDER. The proof-builder is the part of ACL2 closest to J-Bob’s paths and steps (Chapter 11). Dracula: Dracula. A Racket package for programming in ACL2. The README of the J-Bob repository says that Dracula includes J-Bob, a Racket package for ACL2, which according to the J-Bob README includes J-Bob (README.md:6–7). Trusted checkers: Barendregt and Wiedijk Henk Barendregt and Freek Wiedijk. The Challenge of Computer Mathematics. Philosophical Transactions of the Royal Society A, 363(1835):2351–2375, 2005, on the de Bruijn criterion. Dependent types, for contrast: Friedman and Christiansen Daniel P. Friedman and David Thrane Christiansen. The Little Typer. MIT Press, Cambridge, MA, 2018, where a proof is a program whose type is the theorem, and the author’s tutorial on its checker, Pie Massimo Nocentini. Dependent Types, Read Through Pie. Tutorial, October 2026b, whose form this tutorial follows.