Chapter 3

Rewriting at a path

Every J-Bob proof is a list of rewrite steps, and this section explains what one step does and how a list of them is run. The only entry point it uses is J-Bob/step (scheme/j-bob.scm:775–782), which takes a list of definitions, a claim and a list of steps, and returns the claim after the steps. A step is a two-element list (path app). The path picks one subterm of the claim, the focus. The app names a primitive, a function or a theorem and gives its arguments, and from it J-Bob builds one equation. If the focus is one side of that equation, it is replaced by the other side. Nothing else happens: J-Bob does not look for a place where an equation applies, does not choose its arguments, and does not say why a step did nothing.

3.1Paths and the focus

The claim (atom (cdr (cons (car (cons p q)) '()))) as a tree. Edges are labelled with directions. The dashed box is the focus at the path (1 1 1), the subterm (car (cons p q)).Figure 3.1: The claim (atom (cdr (cons (car (cons p q)) '()))) as a tree. Edges are labelled with directions. The dashed box is the focus at the path (1 1 1), the subterm (car (cons p q)).

The claim of chapter1.example6 in the transcript (scheme/little-prover.scm:32–37) is (atom (cdr (cons (car (cons p q)) '()))). Read as a tree (Figure 3.1), an application has its operator at a node and one child per argument, an if has three children, and a quote or a variable is a leaf. A path is a list of directions read from the root. A positive number n goes into the n-th argument of an application, counting from 1 (get-arg, scheme/j-bob.scm:191–198). The symbols Q, A and E go into the question, the answer and the else branch of an if. The empty path () is the whole claim. The program below prints the focus at six paths, using J-Bob’s own find-focus-at-path; it then replaces the focus at (1 1 1) by p with rewrite-focus-at-path:

paths.scm

;; The focus at each path of example6's claim.
(define e '(atom (cdr (cons (car (cons p q)) '()))))
(for-each
 (lambda (path) (wr path) (display " -> ")
   (wr (find-focus-at-path path e)) (newline))
 '(() (1) (1 1) (1 1 1) (1 1 2) (1 1 1 1)))
;; Replace the focus at (1 1 1) by p.
(show "rewrite at (1 1 1)" (rewrite-focus-at-path '(1 1 1) e 'p))
[…]
() -> (atom (cdr (cons (car (cons p q)) '())))
(1) -> (cdr (cons (car (cons p q)) '()))
(1 1) -> (cons (car (cons p q)) '())
(1 1 1) -> (car (cons p q))
(1 1 2) -> '()
(1 1 1 1) -> (cons p q)
;; rewrite at (1 1 1)
(atom (cdr (cons p '())))

The same program walks into an if: on the claim (if (atom x) 't (cons (car x) (cdr x))) the paths (Q), (A), (E), (E 2) and (Q 1) give (atom x), 't, (cons (car x) (cdr x)), (cdr x) and x.The 26 lines left out above run the same loop on an if, focus-is-at-path? on six pairs, path?, and J-Bob/step at five paths; all their output is in paths.out. The helper wr is the one of Chapter 2. A direction must be a natural number or one of Q, A, E (direction? and path?, scheme/j-bob.scm:250–260). These tests look only at the path, not at the claim, so (path? '(0 Q A E 7)) is t, while (path? '(X)) and (path? '(-1)) are nil. Whether a path fits the claim is decided while rewriting, one direction at a time, by focus-is-at-direction? (scheme/j-bob.scm:545–554). Q, A and E need an if. A number n needs an application with at least n arguments: (<=len n args) holds when 1 ≤ n and n is at most the number of args (scheme/j-bob.scm:211–218), so the direction 0 passes path? but never fits. A quote is not an application (app?, scheme/j-bob.scm:39–46), so no path enters '(a b), and an if is not one either, so a number cannot enter an if. focus-is-at-path? (scheme/j-bob.scm:556–562) applies the test along the whole path. Each line below shows a pair (path claim) and the answer of focus-is-at-path?:

((0) (car (cons a b))) -> nil
((3) (car (cons a b))) -> nil
((1 2 1) (car (cons a b))) -> nil
((Q) (car (cons a b))) -> nil
((1) (if a b c)) -> nil
((1 1) (car '(a b))) -> nil

Finally, J-Bob/step with the step (p (car/cons a b)) on the claim (car (cons a b)) gives a when p is (), and the claim unchanged when p is (0), (3), (Q) or (X). For the first three the step is well-formed and does nothing; (X) fails path?, which makes the whole list of steps ill-formed (Section 3.4).

3.2Three kinds of equation

The app of a step is an application (name arg …). rewrite/step (scheme/j-bob.scm:704–706) looks the name up in the definitions and hands what it finds to equality/def (scheme/j-bob.scm:684–702). lookup returns the first definition with that name, or the name itself when there is none (scheme/j-bob.scm:131–136). A primitive has no definition in the list (unless a defun shadows it, Chapter 10), so for the nine primitives lookup returns the bare symbol, which rator? recognises (scheme/j-bob.scm:87–89). equality/def then builds one equation for each kind of name (Table 3.1). We write body[a/x] for a body with each formal x replaced by the matching argument a of the app. sub-e (scheme/j-bob.scm:398–426) does the replacing, of all formals at once, and leaves quoted constants alone.

Table 3.1: The equation equality/def builds from the app of a step, by the kind of definition that lookup finds. Lines are in scheme/j-bob.scm.
kindequation built from the applines
primitive(equal app v), with v the quoted value of the app; every argument must be quoted685–687
defun(equal (f a …) body[a/x]): the call, and the body of f688–696
dethmbody[a/x]: the body of the theorem697–701
unknown namenone: the claim is returned unchanged (steps? refuses such a step, except a call of the function being defined in its own totality proof, which passes through the stub, Section 6.5)702

J-Bob never prints the equation; to see it, three-kinds.scm defines a helper equation that makes the same calls as rewrite/step and equality/def (lookup, eval-op, sub-e), written with cond because J-Bob’s if is in force (Chapter 1). Applied to the apps (car '(ham eggs)), (list-induction (cdr y)), (car/cons (cdr x) (car y)) and (frob x), it prints:

;; primitive
(equal (car '(ham eggs)) 'ham)
;; defun
(equal (list-induction (cdr y))
       (if (atom (cdr y))
         '()
         (cons (car (cdr y)) (list-induction (cdr (cdr y))))))
;; theorem
(equal (car (cons (cdr x) (car y))) (cdr x))
;; unknown name
none

Primitives. For one of the nine primitives the equation says that the app equals its value. eval-op strips the quotes from the arguments, applies the primitive as j-bob-lang.scm defines it, and quotes the result with quote-c (scheme/j-bob.scm:613–655). There cons is the host’s, and the other eight are total versions built on the host’s operations (scheme/j-bob-lang.scm:10–18, scheme/j-bob-lang.scm:35–38); +, for one, counts a non-number as 0 (scheme/j-bob-lang.scm:6, scheme/j-bob-lang.scm:16). A primitive step is accepted only when every argument is a quoted constant (step-args?, scheme/j-bob.scm:278–281), so the right-hand side is always a constant, and evaluation does not get stuck on odd arguments (Chapter 2).One exotic host constant is known to raise an error: a complex number given to < (Chapter 10). In three-kinds.scm, the claims (size '((a . b) c)), (+ 'a '3) and (equal 't 't), each with the step (() app) where the app is the claim itself, rewrite to '3, '3 and 't.

Definitions. For a defun, the equation is the definition read as an equation and instantiated, (equal (f a …) body[a/x]). Rewriting the call with this equation is unfolding the definition: the step (() (list-induction (cdr y))) turns the claim (list-induction (cdr y)) into the right-hand side printed above.

Theorems. For a dethm, axiom or proved theorem alike, the equation is body[a/x], the body of the theorem instantiated.The five axioms used in this section (scheme/j-bob.scm:799–808) are atom/cons, (equal (atom (cons x y)) 'nil); car/cons, (equal (car (cons x y)) x); cdr/cons, (equal (cdr (cons x y)) y); equal-same, (equal (equal x x) 't); and equal-swap, (equal (equal x y) (equal y x)). Chapter 4 lists all 21. The body can be an if whose question is a premise. Before the equation is used, follow-prems (scheme/j-bob.scm:604–611) tries to discharge such premises from the ifs of the claim around the focus; Chapter 5 explains how. For the unconditional theorems of this section the body is not an if, and follow-prems returns it as it is (line 611).

Exact instances. Matching is syntactic. J-Bob does not find the instance of a theorem that fits the focus: the user writes it out in the step’s arguments, and the focus must then be equal, as an S-expression, to one side of the instantiated equation. The step ((2 1) (car/cons (cdr x) (car y))) of chapter1.example10 (scheme/little-prover.scm:59–60) gives the first result below. With b where (car y) was needed, the step ((2 1) (car/cons (cdr x) b)) is well-formed but does nothing, and the claim comes back as it was given:

;; example10
(cons y (equal (cdr x) (equal (atom x) 'nil)))
;; wrong instance
(cons y
      (equal (car (cons (cdr x) (car y)))
             (equal (atom x) 'nil)))

The arguments of a defun or dethm step must be expressions, but exprs? is called with 'any in place of a list of variables (scheme/j-bob.scm:272, scheme/j-bob.scm:276), and then bound? lets every variable through (scheme/j-bob.scm:164). So a step may use variables that occur nowhere in the claim.

Figure 3.2 puts the parts of one step together. After equality/def, equality/path (scheme/j-bob.scm:676–682) checks the path, discharges premises, and passes the result to equality/equation (scheme/j-bob.scm:669–674). That function uses the result only if it is an application of equal (app-of-equal?, scheme/j-bob.scm:657–660); otherwise the focus stays as it was. Chapter 5 shows a theorem that is admitted but is useless as a step for this reason.

One rewrite step. rewrite/step looks up the name of the app, and equality/def builds the equation (top row); equality/path checks the path, discharges premises and rewrites the focus (bottom row). Every dashed exit gives back the claim as it was, which ends the list of steps (Section 3.4). Numbers are lines of scheme/j-bob.scm.
Figure 3.2: One rewrite step. rewrite/step looks up the name of the app, and equality/def builds the equation (top row); equality/path checks the path, discharges premises and rewrites the focus (bottom row). Every dashed exit gives back the claim as it was, which ends the list of steps (Section 3.4). Numbers are lines of scheme/j-bob.scm.

3.3Both directions

The last test of a step is equality:

scheme/j-bob.scm

662(defun equality (focus a b)
663  (if (equal focus a)
664    b
665    (if (equal focus b)
666      a
667      focus)))

If the focus is the left-hand side a of the equation, it becomes the right-hand side b. If it is the right-hand side, it becomes the left-hand side. So every equation can be used backwards, for all three kinds of step: this is backwards rewriting. In backwards.scm the claims x, 'ham and the body of list-induction with z for x, rewritten at () by the apps (car/cons x y), (car '(ham eggs)) and (list-induction z), give:

;; theorem
(car (cons x y))
;; primitive
(car '(ham eggs))
;; fold a defun
(list-induction z)

Read backwards, car/cons turns the variable x into (car (cons x y)), with a y the user chose; a value becomes an expression that computes it; and a body becomes a call, which folds the definition. Backwards steps usually make a claim larger, and that is how a proof brings in a term the claim does not yet contain. Chapter 4 does this with if-same to start a case split, and Chapter 8 builds a premise this way.

chapter1.example11 (scheme/little-prover.scm:62–68) uses two of its four steps backwards, steps 1 and 2. The helper replay of jbob.scm hands the entry to trace-steps (Chapter 1), which makes the checks of J-Bob/step and then prints the claim after each step of J-Bob’s rewrite/step, marking a step that changes nothing with [no change: J-Bob stops here]; its PROVED says that the final claim is 't.

;; entry chapter1.example11
;; claim
(cons y
      (equal (car (cons (cdr x) (car y)))
             (equal (atom x) 'nil)))
;; step 1: ((2 1) (car/cons (car (cons (cdr x) (car y))) '(oats)))
(cons y
      (equal (car (cons (car (cons (cdr x) (car y))) '(oats)))
             (equal (atom x) 'nil)))
;; step 2: ((2 2 2) (atom/cons (atom (cdr (cons a b))) (equal (cons a b) c)))
(cons y
      (equal (car (cons (car (cons (cdr x) (car y))) '(oats)))
             (equal (atom x)
                    (atom (cons (atom (cdr (cons a b)))
                                (equal (cons a b) c))))))
[…]

In step 1 the focus at (2 1) is (car (cons (cdr x) (car y))). The step instantiates car/cons with that whole term for x and '(oats) for y, so the focus is the right-hand side of the equation and is replaced by the left-hand side. In step 2 the focus is 'nil, the right-hand side of atom/cons, which becomes an atom of a cons of two terms the user made up. Steps 3 and 4 go forwards inside the new term: cdr/cons, then equal-swap, whose left-hand side is the focus (equal (cons a b) c).The trace prints steps 3 and 4 and a last line with the final claim; they are left out here (11 lines), and the full output is in backwards.out. The result is not 't, and is not meant to be: the entry only shows rewriting.

3.4Silent failure

J-Bob/step first checks its input: the definitions with defs?, the claim with expr?, the steps with steps?; if any check fails it returns the claim unchanged (scheme/j-bob.scm:775–782). steps? (scheme/j-bob.scm:296–301) checks every step with step? (scheme/j-bob.scm:289–294): the path must pass path?, the app must be an application, and its name must be a primitive, defun or dethm with the right number of arguments, each a well-formed expression, and quoted for a primitive (step-args?, scheme/j-bob.scm:269–282). One bad step rejects the whole list, so the good steps before it are not applied either. In silent.scm the first run is the first step of chapter1.example1 (scheme/little-prover.scm:4–8) alone; the second is that example with its second step replaced by (() (car/cons a)); the other inputs are named by their labels:

;; first step alone
(car '(ham eggs))
;; wrong arity in the second step
(car (cons 'ham '(eggs)))
;; primitive with an unquoted argument
(car (cons a b))
;; unknown name
(car (cons a b))
;; unknown function in the claim
(foo (car (cons a b)))
;; neither side
(car (cons a b))

Inputs two to five are ill-formed: car/cons needs two arguments, the primitive app (car (cons a b)) has the unquoted argument (cons a b), no-such-thm is not defined, and the claim calls an undefined foo. In the second run the good first step is lost too: the claim is not (car '(ham eggs)). The last input is well-formed: (car/cons a c) is a fine step whose equation, (equal (car (cons a c)) a), has neither side equal to the focus. In these last five runs the claim comes back exactly as it was given, so the answer does not tell an ill-formed input from a step that did not fit.

J-Bob’s input checks are also a little lenient in three places, and we infer that none of them can change what a step means.step-gaps.scm. Two gaps were shown in Section 2.7: an atom as a path acts as (), since path? and the walks stop at any atom (scheme/j-bob.scm:255–257, scheme/j-bob.scm:556–576), and extra elements after the app are ignored. The third: defs may be any atom, here 5, since defs? is t on an atom (scheme/j-bob.scm:364–366). The three runs give a, a and 'a.

We read failing quietly as a deliberate choice for a checker.This is our reading of the design, not a statement of the authors. A step that does nothing cannot make a false claim look proved: the claim is accepted only if the steps rewrite it to exactly 't (Chapter 5). The price is paid by the user, who gets no message, and whose only tool is to look at the claim after a prefix of the steps (Section 3.6).

A well-formed list of steps is run by rewrite/steps (scheme/j-bob.scm:716–720), which applies the first step and hands the old and the new claim to rewrite/continue:

scheme/j-bob.scm

708(defun rewrite/continue (defs steps old new)
709  (if (equal new old)
710    new
711    (if (atom steps)
712      new
713      (rewrite/continue defs (cdr steps) new
714        (rewrite/step defs new (car steps))))))

After each step, line 709 compares the new claim with the old one. If they are equal, J-Bob stops there and ignores the remaining steps. So a step that leaves the claim unchanged, a no-op step, ends the list. The next runs rewrite (atom (cdr (cons x '()))) with the steps ((1) (car/cons a b)), ((1) (cdr/cons x '())) and (() (atom '())): in this order, with the first two swapped, and without the first. The app (car/cons a b) fits nowhere in the claim:

;; first step does nothing
(atom (cdr (cons x '())))
;; second step does nothing
(atom '())
;; without the step that does nothing
't

In the second run the last step, (() (atom '())), would have applied to (atom '()), but it is never reached. The test is on the claim, not on whether the step matched: a step that fits but gives back the same claim is a no-op step too, as equal-swap is on (equal x x). Traced with trace-steps, the steps (() (equal-swap x x)) and (() (equal-same x)) give the lines below; the second step alone proves the claim:

;; claim
(equal x x)
;; step 1: (() (equal-swap x x))  [no change: J-Bob stops here]
;; 1 later step not applied
;; final: (equal x x)  NOT PROVED
;; equal-same alone
't

3.5Chapter 1 of the transcript

Chapter 1 of the transcript (scheme/little-prover.scm:2–73) holds twelve calls of J-Bob/step on the axioms of cons, car, cdr, atom and equal. Examples 6 and 7 rewrite the same claim to 't in two ways; chapter1.scm replays both:

;; entry chapter1.example6
;; claim
(atom (cdr (cons (car (cons p q)) '())))
;; step 1: ((1 1 1) (car/cons p q))
(atom (cdr (cons p '())))
;; step 2: ((1) (cdr/cons p '()))
(atom '())
;; step 3: (() (atom '()))
't
;; final: 't  PROVED
;; entry chapter1.example7
;; claim
(atom (cdr (cons (car (cons p q)) '())))
;; step 1: ((1) (cdr/cons (car (cons p q)) '()))
(atom '())
;; step 2: (() (atom '()))
't
;; final: 't  PROVED

Example 6 works from the inside out: it first simplifies (car (cons p q)) to p, the focus of Figure 3.1, and then removes the cdr of the cons. Example 7 starts from the outside and instantiates cdr/cons with the whole term (car (cons p q)) for x. That term is the part of the cons that the cdr throws away, so there is no need to simplify it, and the proof is one step shorter.

3.6Debugging a stuck proof

A wrong step shows up only as a final claim that is not 't. Chapter 1 found the bad step with trace-steps; here we also find why it does nothing. Here is example 6 with one mistake: the path of its second step is (1 1) instead of (1). The program prints the result of J-Bob/step on these steps, then their trace:

debug.scm

;; J-Bob/step
(atom (cdr (cons p '())))
;; claim
(atom (cdr (cons (car (cons p q)) '())))
;; step 1: ((1 1 1) (car/cons p q))
(atom (cdr (cons p '())))
;; step 2: ((1 1) (cdr/cons p '()))  [no change: J-Bob stops here]
;; 1 later step not applied
;; final: (atom (cdr (cons p '())))  NOT PROVED

J-Bob/step returns the claim after step 1; the trace adds that step 2 is the first no-op step. To see why, compare the focus at the step’s path with the step’s equation. The last lines of debug.scm compute both with J-Bob’s functions: find-focus-at-path at (1 1) and at (1) on the claim after step 1, and the body of cdr/cons with p and '() substituted by sub-e:

;; focus at (1 1)
(cons p '())
;; focus at (1)
(cdr (cons p '()))
;; cdr/cons p '()
(equal (cdr (cons p '())) '())

The focus at (1 1) is (cons p '()), which is neither side of the equation; the left-hand side is one level up, at (1).

The recipe applies to any stuck proof. First check that the result differs from the claim you gave: if it does not, and the first step should apply, the list may be ill-formed, and then no step ran at all (Section 3.4); trace-steps says so with a line ;; ill-formed instead of a trace. Otherwise run the steps one prefix at a time, or trace them, find the first no-op step, and compare its focus with its equation. Either the path is wrong, as here; or the instance is (Section 3.2); or the step fits but rewrites the focus to itself (Section 3.4); or the body of the theorem, past its premises, is not an equal (Section 3.2); or, for a theorem with premises, a premise was not discharged (Chapter 5). Chapter 12 asks for a tool that reports that step.

The theorems used so far are five of J-Bob’s 21 axioms: atom/cons, car/cons, cdr/cons, equal-same and equal-swap. Chapter 4 presents all of them, among them the if axioms, with which a proof splits a claim into cases (if-same) and simplifies an if inside a branch (if-nest-A, if-nest-E).