Chapter 9
Inside J-Bob
The previous sections used J-Bob through its three entry points. This one reads scheme/j-bob.scm as a program: its groups of functions, its data, its checkers, its rewriter and its drivers, and the fact that J-Bob is itself a well-formed J-Bob program. Once the file is loaded, each of its functions is an ordinary Scheme procedure (defun expands to define, scheme/j-bob-lang.scm:25–28), so the examples call internal functions directly, with arguments of our choosing. Unless said otherwise, the definitions are P, the value of (prelude) (scheme/j-bob.scm:856–879): the 21 axioms, list-induction and star-induction. Most examples print a call, then => and its value, as in Section 2.7. Many of the functions called here return an expression of J-Bob’s language, so after => its constants are quoted, as in '().
9.1A map of the file#
The file has 879 lines and 128 top-level forms, all of them defuns (Section 9.9), and no comments. It is written in the language of Chapter 2. Table 9.1 sorts the functions into 15 groups in file order. The groups are ours, and groups.scm checks them against the file read as data. It prints the size of each group, as in the table, and checks that the two lists of names agree (the check is plain Scheme, hence #t):
groups.scm
;; functions per group, eight to a line
(A 14) (B 25) (C 10) (D 7) (E 4) (F 24) (G 8) (H 4)
(I 4) (J 6) (K 3) (L 6) (M 8) (N 3) (O 2)
;; listed, defined, each name once in both lists
(128 128 #t)From top to bottom the file builds a small library for its data (A–E), the checkers that decide whether an input is well-formed (F), the builders of claims (G–I), the rewriter (J–L), the drivers and entry points (M–N), and the axioms (O). Figure 9.1 shows who calls whom.
| group | functions | lines | role |
|---|---|---|---|
| A lists | list0…list3, list0?…list3?, elem1…elem3, tag tag? untag (14) | 2–23 | lists of exactly elements, tagged lists |
| B syntax | quote-c quote? quote.value, if-c if? if.Q if.A if.E, app-c app? app.name app.args, var?, defun-c defun? defun.name defun.formals defun.body and the same five for dethm, if-QAE QAE-if (25) | 25–78 | build, recognise and take apart expressions and definitions |
| C list utilities | member? get-arg set-arg <=len (and their -from helpers), subset? list-extend list-union (10) | 80–85, 191–239 | membership, 1-based arguments, sets as lists |
| D primitives | rator? rator.formals, unary-op binary-op apply-op rands eval-op (7) | 87–96, 613–655 | the nine primitives, their arity and their evaluation |
| E definitions | def.name def.formals lookup undefined? (4) | 98–110, 131–141 | the list of definitions |
| F checkers | arity? args-arity? app-arity? bound? exprs? expr?, formals? direction? path? quoted-exprs? step-args? step-app? step? steps?, induction-scheme-for? induction-scheme? seed?, extend-rec def-contents? def? defs?, list2-or-more? proof? proofs? (24) | 143–189, 241–396 | well-formedness of expressions, steps, seeds, definitions, proofs |
| G builders | if-c-when-necessary conjunction implication, sub-var sub-es sub-e, exprs-recs expr-recs (8) | 112–129, 398–451 | claim pieces, substitution, recursive calls |
| H totality | totality/< totality/meas totality/if totality/claim (4) | 453–492 | the totality claim (Chapter 6) |
| I induction | induction/prems induction/if induction/defun induction/claim (4) | 494–524 | the induction claim (Chapter 7) |
| J paths | find-focus-at-direction rewrite-focus-at-direction focus-is-at-direction?, the same three -at-path (6) | 526–576 | walk and rebuild at a path (Chapter 3) |
| K premises | prem-A? prem-E? follow-prems (3) | 578–611 | premises settled by the claim’s ifs |
| L equations | app-of-equal? equality equality/equation equality/path equality/def rewrite/step (6) | 657–706 | one rewriting step |
| M drivers | rewrite/continue rewrite/steps, rewrite/prove rewrite/prove+1 rewrite/prove+, rewrite/define rewrite/define+1 rewrite/define+ (8) | 708–773 | sequences of steps, lists of proofs |
| N entry points | J-Bob/step J-Bob/prove J-Bob/define (3) | 775–796 | check, then run |
| O data | axioms prelude (2) | 798–879 | axioms, prelude |
scheme/j-bob.scm in 15 groups, with the number of functions of each group and its lines. Groups C to G are each split over two ranges.9.2Expressions and definitions as data#
J-Bob has no parser and no syntax tree: it works on S-expressions. Group A gives it lists of a fixed length, such as list2?, which holds when its argument has exactly two elements, and elem2, which picks the second (scheme/j-bob.scm:10–13), and tagged lists, whose first element names their kind (scheme/j-bob.scm:20–23). On these, group B defines the expressions (scheme/j-bob.scm:25–57). A quote is (quote v) with exactly one element after the tag, an if has exactly three parts, an application is any other pair, and a variable is an atom other than t, nil and a natural number. Nothing here checks that an application calls a known name; the checkers do that (Section 9.4). Two small functions turn an if into the list of its parts and back (scheme/j-bob.scm:75–78):
helpers.scm
(if-QAE '(if (atom x) 'nil (f x))) => ((atom x) 'nil (f x))With them, every function that walks expressions treats an if like the argument list of an application (Section 9.8).
A list of definitions, defs in the source, is a plain list of defun and dethm forms in the order they were admitted, searched by lookup:
scheme/j-bob.scm
131(defun lookup (name defs)
132 (if (atom defs)
133 name
134 (if (equal (def.name (car defs)) name)
135 (car defs)
136 (lookup name (cdr defs)))))When nothing has the name, lookup returns the name itself. That is how J-Bob recognises a primitive: the primitives are not in defs, so their lookup gives back the symbol, which rator? compares with the nine primitive names (scheme/j-bob.scm:87–89). Most callers of lookup test its result with dethm?, defun? and rator?, and treat anything else as an unknown name (for example args-arity?, scheme/j-bob.scm:150–157); induction/claim (scheme/j-bob.scm:519–524) uses it untested and relies on seed? having checked it. undefined? just compares the result with the name (scheme/j-bob.scm:138–141), so the name of a primitive counts as undefined, and def? accepts a defun named car. Chapter 10 shows what follows. New definitions go at the end, with list-extend, which adds nothing when the element is already there (scheme/j-bob.scm:227–233):
(defun? (lookup 'list-induction P)) => t
(lookup 'car P) => car
(rator? (lookup 'car P)) => t
(undefined? 'car P) => t
(def? P '(defun car (x) x)) => t
(list-extend '(a b) 'c) => (a b c)
(list-extend '(a b) 'a) => (a b)9.3Builders#
Group G builds the pieces of claims. sub-e replaces variables by expressions (scheme/j-bob.scm:398–426). Here it swaps x and y, and leaves the quote 'x alone:
helpers.scm
(sub-e '(x y) '(y x) '(cons x (if y 'x (f y z))))
=> (cons y (if x 'x (f x z)))The substitution is simultaneous, since each variable is looked up in the original formals (sub-var, scheme/j-bob.scm:398–403). Quotes are left alone, and there is nothing to rename, because J-Bob’s expressions have no binders. expr-recs lists the calls of a function in an expression: every call at any depth, also inside the arguments of another call and in every part of an if, each once, in the order met (scheme/j-bob.scm:428–451). The totality and induction claims are built from this list (Chapter 6 and Chapter 7). Here are the calls of f in (f (f x) (if (f x) (g (f y)) (f x))):
(expr-recs 'f '(f (f x) (if (f x) (g (f y)) (f x))))
=> ((f (f x) (if (f x) (g (f y)) (f x))) (f x) (f y))The first element is the whole expression, itself a call of f. The call (f x) occurs three times and is listed once, because the lists are joined by list-union, which is list-extend one element at a time (scheme/j-bob.scm:235–239). The language has no and or implies, so three functions write them as ifs: conjunction joins a list of claims, implication puts a list of premises in front of a conclusion, and if-c-when-necessary builds (if Q A E) unless the branches are equal (scheme/j-bob.scm:112–129):
(conjunction '()) => 't
(conjunction '(p)) => p
(conjunction '(p q)) => (if p q 'nil)
(implication '() 'c) => c
(implication '(p) 'c) => (if p c 't)
(implication '(p q) 'c) => (if p (if q c 't) 't)
(if-c-when-necessary 'q 'a 'a) => a
(if-c-when-necessary 'q 'a 'e) => (if q a e)A conjunction of one claim, or an implication with no premise, adds no if, which keeps the generated claims short.
9.4The checkers#
Figure 2.1 listed what the checkers accept, and Section 2.7 ran them on examples. Here we read them as code and add what that section left out. Group F is a grammar written as code, one recogniser per kind of phrase. Those for lists of phrases recurse down the list; only eight of the 24 functions of the group recurse at all (arity?, exprs?, formals?, path?, quoted-exprs?, steps?, defs? and proofs?, in the list of Section 9.9). The central one, exprs? (scheme/j-bob.scm:166–187), checks a list of expressions es against the definitions defs and the variables in scope vars, one element at a time, with one branch per kind of expression. A variable must be bound, a quote is always fine, the parts of an if are checked as a list, and an application must call a known name with the right number of arguments: app-arity? looks the name up and compares the arguments with the formals of a defun or a primitive, and answers 'nil for a dethm, which cannot be called (scheme/j-bob.scm:143–161). The scope 'any binds every variable (scheme/j-bob.scm:163–164).
Steps. steps? checks each step with step?: the path with path?, the app with step-app? and step-args? (scheme/j-bob.scm:250–301). When a step’s app names a theorem or a defun, its arguments are checked as expressions over 'any, so a step may mention a variable that occurs nowhere in the claim; when it names a primitive, they must all be quotes, because a primitive step evaluates them (scheme/j-bob.scm:269–282):
checkers.scm
;; steps
(step? P '((1) (car/cons a zzz))) => t
(step? P '(() (car '(a b)))) => t
(step? P '(() (car x))) => nilcheckers.out goes on with the two gaps of Section 2.7: path? accepts any atom (scheme/j-bob.scm:255–257), and step? reads a step with elem1 and elem2 (scheme/j-bob.scm:289–294) without checking that nothing follows. Chapter 10 looks for gaps that matter.
Seeds. The seed of a proof is nil, which always passes (scheme/j-bob.scm:320–322), a measure for a defun, or an induction scheme for a dethm. A measure is an expression over the function’s formals, checked against defs without the function, so it cannot call the function it measures. An induction scheme calls a defun already in defs on distinct formals of the theorem (scheme/j-bob.scm:303–329). Below, z is not a formal of d, and car/cons is a theorem, not a defun:
;; seeds
(seed? P '(defun f (x) x) '(size x)) => t
(seed? P '(defun f (x) x) '(size y)) => nil
(seed? P '(defun f (x) (f x)) '(f x)) => nil
(seed? P '(dethm d (x y) 't) '(list-induction y)) => t
(seed? P '(dethm d (x y) 't) '(list-induction z)) => nil
(seed? P '(dethm d (x y) 't) '(car/cons x y)) => nilDefinitions and proofs. def? wants a new name and distinct formals, and checks a defun’s body against defs plus a stub (defun f (x …) (f x …)) built by extend-rec (scheme/j-bob.scm:331–362; Section 2.7 ran it). defs? and proofs? run def? and proof? along a list, adding each definition before checking the next (scheme/j-bob.scm:364–370, 389–396). proof? needs a definition and a seed at least, and checks the steps against defs with the stub (scheme/j-bob.scm:372–387). So the steps of a totality proof may name the function. They cannot unfold it (Section 6.5): rewrite/prove rewrites with defs alone (scheme/j-bob.scm:722–731), where the name is unknown, so such a step is a no-op step. Here f is (defun f (x) (if (atom x) 'nil (f (cdr x)))) and pf its proof with the measure (size x) and the one step ((A E) (f x)):
;; a step that names f
(proof? P pf) => t
(equal (J-Bob/prove P (list pf)) (totality/claim '(size x) f)) => tThe proof is well-formed, and J-Bob/prove returns the totality claim as it was built.
9.5Small machinery#
Every numeric direction goes through three small functions: get-arg and set-arg pick and replace the n-th argument, counted from 1 (scheme/j-bob.scm:191–209), after <=len has checked that the number of arguments (scheme/j-bob.scm:211–218, 545–554):
;; arguments
(get-arg '1 '(a b c)) => a
(set-arg '2 '(a b c) 'z) => (a z c)
(<=len '0 '(a b c)) => nil
(<=len '3 '(a b c)) => t
(<=len '4 '(a b c)) => nil
(path? '(0 1)) => t
(focus-is-at-path? '(0) '(car x)) => nilpath? accepts the direction 0, a natural number (scheme/j-bob.scm:250–253), but 0 never points anywhere, so a step with it is a no-op step (Section 3.1).
A primitive step such as (() (car '(a b))) rewrites the call to its value. rands strips the quotes, apply-op calls unary-op or binary-op, which call the host’s car, + and so on, and eval-op quotes the result (scheme/j-bob.scm:613–655):
;; primitives
(rands '('(a b) 'c)) => ((a b) c)
(eval-op '(cons '(a b) 'c)) => '((a b) . c)
(eval-op '(car 'a)) => '()
(eval-op '(+ 'a '3)) => '3
(eval-op '(size '(a (b c)))) => '4The host functions are those of j-bob-lang.scm, which make the primitives total: the car of an atom is '(), + counts a non-number as 0 (scheme/j-bob-lang.scm:6, 11, 16), and size counts the conses of its argument, 4 for (a (b c)) (scheme/j-bob-lang.scm:35–38). J-Bob trusts what they return; this is where the floating-point and complex-number problems of Chapter 10 come in.
9.6The rewrite core#
Chapter 3 described a step from the outside. Inside, rewrite/step looks up the name of the step’s app and calls equality/def (scheme/j-bob.scm:684–706), which turns the definition into an instance, a claim about the step’s arguments. For a primitive it is (equal app value), built with eval-op; for a defun, the equation between the call and the body, with the arguments put for the formals by sub-e; for a dethm, its body with the same substitution, premises included. For anything else the claim comes back unchanged. Then:
scheme/j-bob.scm
676(defun equality/path (e path thm)
677 (if (focus-is-at-path? path e)
678 (rewrite-focus-at-path path e
679 (equality/equation
680 (find-focus-at-path path e)
681 (follow-prems path e thm)))
682 e))focus.scm takes the step of Figure 5.1 apart: ((E) (cons/car+cdr x)) on the claim (if (atom x) 't (cons (car x) (cdr x))). It calls these functions one at a time, then rewrite/step on the whole step. The instance, on the first line, is the theorem itself, since its formal is x:
focus.scm
;; thm: the body with x for x
(if (atom x) 't (equal (cons (car x) (cdr x)) x))
;; focus-is-at-path?
t
;; focus
(cons (car x) (cdr x))
;; prem-A? and prem-E? of (atom x)
(nil t)
;; follow-prems
(equal (cons (car x) (cdr x)) x)
;; equality/equation
x
;; rewrite-focus-at-path
(if (atom x) 't x)
;; rewrite/step does the same
(if (atom x) 't x)The instance has the premise (atom x), and the focus is in the E branch of the claim’s (if (atom x) …), so follow-prems takes the instance’s E branch, an equation (Section 5.4); equality/equation accepts it and equality puts the other side in place of the focus (Section 3.3). rewrite-focus-at-path then rebuilds the claim (scheme/j-bob.scm:570–576).
When the claim does not settle the premise the right way, follow-prems returns something that is not an equation. On (if (atom x) (cons (car x) (cdr x)) 't) with path (A), the claim’s if says that (atom x) holds at the focus, so follow-prems takes the instance’s A branch, 't, which is not an equation. With no if around the focus it returns the whole instance:
;; follow-prems at (A): (atom x) is true there
't
;; follow-prems with no if around the focus
(if (atom x) 't (equal (cons (car x) (cdr x)) x))
;; rewrite/step at (A): no change
(if (atom x) (cons (car x) (cdr x)) 't)An unknown name, a path that leads nowhere, an unsettled premise and a focus that matches neither side all give the same result, a no-op step. rewrite/steps applies the first step itself, and rewrite/continue compares each new claim with the one before; the first no-op step ends the sequence (scheme/j-bob.scm:708–720, Section 3.4).
9.7The drivers#
rewrite/prove builds the claim of one proof (totality/claim for a defun, induction/claim for a dethm) and rewrites it with the proof’s steps (scheme/j-bob.scm:722–731). The two drivers of a list of proofs read it in opposite directions, as Section 5.6 showed by running them. rewrite/prove+ (scheme/j-bob.scm:739–745) recurses on the rest of the list first, and rewrite/prove+1 (scheme/j-bob.scm:733–737) runs the current proof only if the rest gave 't, so J-Bob/prove reports the last failure. rewrite/define+ (scheme/j-bob.scm:765–773) starts from the front, and rewrite/define+1 (scheme/j-bob.scm:753–763) carries the definitions before and after the latest proof and stops as soon as the two are equal (scheme/j-bob.scm:754), that is, as soon as a proof added nothing.

scheme/j-bob.scm, simplified, with the lines of each function. Shaded: the entry points. Dashed: the checks every entry point runs before anything else (J-Bob/step also checks its claim with expr?). rewrite/steps applies the first step itself and rewrite/continue the others. The builders are reached through helpers not drawn (totality/if, induction/if and others). lookup is also called from def?, seed? and induction/claim.9.8No mutual recursion#
A Scheme programmer would check expressions with two mutually recursive functions, one for an expression and one for a list. J-Bob has exprs?, on lists only: expr? wraps an expression in a list (scheme/j-bob.scm:188–189), and an if becomes the list of its parts. sub-es and exprs-recs are written the same way (scheme/j-bob.scm:405–449). We infer the reason from def?: a body may call only functions already in defs, or itself through the stub (scheme/j-bob.scm:346–362). Two mutually recursive functions cannot be written in either order. mutual.scm tries it with ev? and od?:
mutual.scm
[…]
(define ev '(defun ev? (x) (if (atom x) 't (od? (cdr x)))))
(define od '(defun od? (x) (if (atom x) 'nil (ev? (cdr x)))))
[…];; defs?: ev? then od?, od? then ev?
(nil nil)
;; def? of ev? alone
nil
;; J-Bob/define of both, names added
()
;; totality/claim of ev?, seed nil and seed (size x)
('t (if (natp (size x)) 't 'nil))A totality claim, too, collects only a function’s calls of itself (scheme/j-bob.scm:480–492). The claim J-Bob would build for ev? says nothing about its call of od?: with the seed nil it is 't, as for a function that does not recurse. Written with one function per family of lists, J-Bob stays inside its own language.
9.9J-Bob is a J-Bob program#
self.scm reads j-bob.scm and little-prover.scm as data and gives them to defs?, as in Section 2.1, and also lists the recursive functions:
self.scm
;; forms in j-bob.scm, and their heads
(128 (defun))
;; defs? '() on them
t
;; recursive
35
member? conjunction implication lookup arity? exprs?
get-arg-from set-arg-from <=len-from subset? list-extend
list-union formals? path? quoted-exprs? steps? defs?
proofs? sub-var sub-es exprs-recs totality/meas
totality/if induction/prems induction/if
focus-is-at-path? find-focus-at-path
rewrite-focus-at-path prem-A? prem-E? follow-prems rands
rewrite/continue rewrite/prove+ rewrite/define+1
;; forms in little-prover.scm, and their heads
(49 (defun))
;; formals of the entries, and the heads of their bodies
((()) (J-Bob/step J-Bob/define J-Bob/prove))
;; defs? '() on little-prover.scm alone
nil
;; defs? '() on j-bob.scm then little-prover.scm
tThe 128 functions are well-formed J-Bob definitions in the order of the file. A function counts as recursive when expr-recs finds a call of its own name in its body. Each of the 49 entries of the transcript is a defun of no arguments whose body calls an entry point. The claims and proofs it hands over are quoted, and a quote is always well-formed (scheme/j-bob.scm:173–174), so defs? checks the wrappers, not the proofs inside them. The entries need J-Bob’s 128 functions in front because they call J-Bob/define and the others; alone they fail.
defs? checks syntax, scope and arity, not totality: to admit J-Bob into J-Bob, each of the 35 recursive functions would need a measure and a proof. The simplest, member? (scheme/j-bob.scm:80–85), J-Bob proves total with the measure (size ys) and five steps:
own-claims.scm
[…]
(define member-pf
(list (find-def 'member?) '(size ys)
'((A E E) (size/cdr ys))
'((A E) (if-same (equal x (car ys)) 't))
'((A) (if-same (atom ys) 't))
'((Q) (natp/size ys))
'(() (if-true 't 'nil))))
(show "J-Bob/prove of member? with measure (size ys)"
(J-Bob/prove (prelude) (list member-pf)));; J-Bob/prove of member? with measure (size ys)
'tSection 11.4 proves the claims of defs? and of the three other functions to which acl2/j-bob.lisp gives a :measure (acl2/j-bob.lisp:367, 393, 744, 759), and shows that a wrong measure, such as (size known-defs) for defs?, gives a false claim.
9.10What it costs#
J-Bob is written to be read, not to be fast. The file book.r5rs of Section 1.4, which loads the three files and runs the whole book, takes about 4.4 s in csi and about 0.7 s under Racket’s plt-r5rs; the compiled modules of aux, which the examples import, take about 0.2 s.In examples/01-intro, time csi -q -s book.r5rs gave real 4.392 to 4.481 s (user 4.351 to 4.432 s) in three runs, and time plt-r5rs --no-prim book.r5rs, with Racket 8.15, real 0.707 to 0.753 s. time csi -q -e '(import (aux j-bob little-prover)) (dethm.align/align)' gave real 0.208 to 0.218 s (user 0.200 to 0.208 s). Much of the time goes into checking the same definitions again: every entry point starts with defs? '() on its whole list of definitions (scheme/j-bob.scm:776, 785, 792). The 31 J-Bob/define entries of the transcript form a chain, and cost.scm counts the definitions each one checks:
cost.scm
;; J-Bob/define entries
31
;; each takes the result of the one before, the first (prelude)
#t
;; definitions given to each, 16 to a line
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
39 40 41 42 43 45 46 47 48 49 53 54 55 56 59
;; after the last, and the length of (book-defs)
(60 60)Each entry adds its proofs to the list it was given, so each checks a longer prefix of the same 60 definitions. Those 31 calls of defs? alone take about 0.1 s of CPU time in the compiled modules, about half of the user time above.JBOB_TIME=1 csi -q -s cost.scm, in examples/09-internals, also times those 31 calls with current-process-milliseconds: 105, 104 and 109 ms in three runs. run.sh does not set JBOB_TIME. Every name is found by lookup, a linear search; there are no hash tables or indices, only lists. For 37 proofs this does not matter. Whether all these checks keep J-Bob from proving a falsehood is the question of Chapter 10.