Chapter 6

Totality and measures

A defun gives J-Bob a new equation. Once list? is defined, the step (() (list? y)) replaces (list? y) by the body of list? with y for x, or, used backwards, the body by (list? y) (scheme/j-bob.scm:688–696; Chapter 3). Such an equation is safe only if some function satisfies it. This section shows a definition whose equation proves 'nil, then the claim J-Bob builds to keep such definitions out: a recursive defun comes with a measure, an expression over the formals whose value must be a natural number that shrinks at every recursive call, and its totality claim says so. Last come the book’s proofs of that claim and what the check can and cannot see. Chapter 7 relies on this: J-Bob/define admits a recursive defun of its proof list only with a proof that its recursion ends.

6.1A definition that proves 'nil

The entry dethm.contradiction in Chapter 4 of the transcript holds a proof that is not meant to be kept: it calls J-Bob/prove, not J-Bob/define, and no other entry uses dethm.contradiction, so it is not a link of the chain (scheme/little-prover.scm:215–231). Its definitions are:

scheme/little-prover.scm

217    (list-extend (prelude)
218      '(defun partial (x)
219         (if (partial x) 'nil 't)))

The theorem is (dethm contradiction () 'nil), and partial is added to the prelude with list-extend (scheme/j-bob.scm:227–233), the plain list operation, not with J-Bob/define. contradiction.scm replays the ten steps:

contradiction.scm

;; claim
'nil
;; step 1: (() (if-same (partial x) 'nil))
(if (partial x) 'nil 'nil)
[…]
;; step 5: ((E Q) (partial x))
(if (partial x)
  (if (if (partial x) 'nil 't) 'nil 't)
  (if (if (partial x) 'nil 't) 't 'nil))
[…]
;; step 7: ((E Q) (if-nest-E (partial x) 'nil 't))
(if (partial x) (if 'nil 'nil 't) (if 't 't 'nil))
[…]
;; final: 't  PROVED
;; (dethm.contradiction)
't

Step 1 splits on (partial x) with if-same used backwards,Runs of 10, 4 and 6 lines are left out (steps 2–4, 6 and 8–10); the full trace is in contradiction.out. The claim has no variables, yet the steps mention x: the arguments of a step are checked with the variable list 'any, which accepts every variable (scheme/j-bob.scm:272, 276, 163–164). and steps 2–3 copy the question into both branches with if-nest-A and if-nest-E, also backwards. Steps 4–5 unfold the two copies with the definition, its only uses. Steps 6–7 use if-nest-A and if-nest-E forwards: their premise (partial x) is the question of the outer if, true on its A side and false on its E side (Chapter 5), so the inner (if (partial x) 'nil 't) becomes 'nil at (A Q) and 't at (E Q). Steps 8–10 are if bookkeeping. J-Bob/prove returns 't: it certifies 'nil.

No function satisfies (equal (partial x) (if (partial x) 'nil 't)): if (partial x) is 'nil the right side is 't, and otherwise it is 'nil. As a program, partial calls itself with the same argument before doing anything else, so it never returns. A total function returns a value for every argument; partial returns none. A definition that does not terminate is not just a program that loops: its equation may have no solution, and from a false equation any claim follows.

partial was never checked for totality because it came in the definitions argument. All three entry points check that argument only with (defs? '() defs) (scheme/j-bob.scm:776, 785, 792), and defs? (scheme/j-bob.scm:364–370) only asks def? (scheme/j-bob.scm:346–362) whether each name is new and each body well-formed. A recursive body is well-formed, because a defun body is checked against extend-rec: the definitions plus a stub of the function, a defun whose body just calls it on its formals (scheme/j-bob.scm:357–360, 331–339). So the definitions an entry point is given are trusted. Only a defun in the proof list gets a totality claim, rewritten by its steps (scheme/j-bob.scm:722–726), and J-Bob/define adds it only when the result is 't (scheme/j-bob.scm:747–751). Offered partial in a proof with no steps, J-Bob/define refuses it with either seed:

;; totality/claim, seed nil
'nil
;; totality/claim, seed (size x)
(if (natp (size x)) (if (< (size x) (size x)) 't 'nil) 'nil)
;; its value at x = '(): 'nil
;; defs: prelude, define with nil, define with (size x)
(23 23 23)
;; J-Bob/define, same defs and proof: lengths before and after, last
(24 25 (dethm contradiction () 'nil))

The prelude has 23 definitions, and so do both results. The second claim is false: at x = '() the host evaluates it to 'nil, so no steps would help (Section 6.5). The last line gives J-Bob/define the definitions and the proof of dethm.contradiction: it keeps partial and adds the theorem 'nil, its 25th definition. Chapter 10 returns to this trust boundary.

6.2The totality claim

The book’s first recursive definition is list?, with the seed (size x) as its measure:

scheme/little-prover.scm

235    '(((defun list? (x)
236         (if (atom x) (equal x '()) (list? (cdr x))))
237       (size x)

size counts conses: 0 for an atom, otherwise one more than the sizes of car and cdr (scheme/j-bob-lang.scm:35–38; Chapter 2). claims.scm prints the claims that J-Bob’s own totality/claim builds, in cases labelled T1–T9 (T1 and T2 are the two prelude functions) and for the book’s functions. For list? it is:

claims.scm

;; defun.list?, seed (size x)
(if (natp (size x))
  (if (atom x) 't (< (size (cdr x)) (size x)))
  'nil)

The measure is a natural number, and when x is not an atom, which is when list? calls itself, the measure of the argument of that call is smaller than the measure of x. A natural number cannot decrease for ever, so the calls (list? x), (list? (cdr x)), … end.Background. The natural numbers ordered by < are the ordinals below ω. ACL2 measures with the ordinals below ε₀, compared by o<, which also admits, for instance, lexicographic pairs. Chapter 11 returns to this.Matt Kaufmann, Panagiotis Manolios, and J Strother Moore. Computer-Aided Reasoning: An Approach. Kluwer Academic Publishers, Boston, 2000; and 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 general, for (defun f (x₁ … xₙ) e) with measure mm, the claim is (if (natp mm) T(e)T(e) 'nil). Each recursive call (f a⃗\vec a) gives an obligation (< m[a⃗/x⃗] mm[\vec a/\vec x]\ m), the measure with the arguments of the call for the formals, and TT follows the ifs at the top of the body, its spine:

T((if Q A E))=conj(O(Q)⊕ifw(Q,T(A),T(E)))T(e)=conj(O(e))for any other eO(e)=[ (< m[a⃗/x⃗] m)∣(f a⃗) a call in e ] \begin{aligned} T(\texttt{(if }Q\ A\ E\texttt{)}) &= \mathit{conj}\bigl(O(Q) \oplus \mathit{ifw}(Q, T(A), T(E))\bigr)\\ T(e) &= \mathit{conj}(O(e)) \qquad\text{for any other } e\\ O(e) &= [\,\texttt{(< }m[\vec a/\vec x]\ m\texttt{)} \mid \texttt{(f }\vec a\texttt{)} \text{ a call in } e\,] \end{aligned}

conj\mathit{conj} of no obligation is 't, of one is that obligation, and of several is (if o₁ (if o₂ … 'nil) 'nil). ⊕\oplus adds an element at the end of a list unless it is already there. ifw(Q,A,E)\mathit{ifw}(Q,A,E) is AA when AA and EE are equal, and (if Q A E) otherwise. For list?, OO((atom x)) is empty, TT of the A branch is conj\mathit{conj} of nothing, 't, and TT of the E branch is its one obligation; the two differ, so ifw\mathit{ifw} keeps the if, which gives the claim above. For partial the call is in the question: both branches give 't, ifw\mathit{ifw} gives 't, and conj\mathit{conj}, which does not drop a 't, gives (if oo 't 'nil), the claim printed in Section 6.1. As logic: the measure is a natural, and every recursive call decreases it under the conditions Γ(f a⃗)\Gamma_{\texttt{(f }\vec a\texttt{)}} of the spine above that call (a question true on the A side, false on the E side),

natp(m)  ∧  ⋀(f a⃗)(Γ(f a⃗)⇒m[a⃗/x⃗]<m). \mathrm{natp}(m) \;\wedge\; \bigwedge_{\texttt{(f }\vec a\texttt{)}} \bigl(\Gamma_{\texttt{(f }\vec a\texttt{)}} \Rightarrow m[\vec a/\vec x] < m\bigr).

The code is a direct transcription (scheme/j-bob.scm:465–478):Line 468 is ⊕\oplus (list-extend), lines 469–470 are O(Q)O(Q), lines 471–475 are ifw\mathit{ifw} (if-c-when-necessary, scheme/j-bob.scm:112–113), and lines 476–478 handle a body that is not an if. conjunction (scheme/j-bob.scm:115–122) is conj\mathit{conj}; totality/meas and totality/< (scheme/j-bob.scm:453–463) build one obligation per call with the substitution sub-e (scheme/j-bob.scm:398–426). expr-recs (scheme/j-bob.scm:428–451) collects the calls of f anywhere, without repetitions: in arguments of other calls, inside calls of f (line 443), and inside ifs off the spine (lines 435–438).

scheme/j-bob.scm

465(defun totality/if (meas f formals e)
466  (if (if? e)
467    (conjunction
468      (list-extend
469        (totality/meas meas formals
470          (expr-recs f (if.Q e)))
471        (if-c-when-necessary (if.Q e)
472          (totality/if meas f formals
473            (if.A e))
474          (totality/if meas f formals
475            (if.E e)))))
476    (conjunction
477      (totality/meas meas formals
478        (expr-recs f e)))))

totality/claim (scheme/j-bob.scm:480–492) wraps the result in (if (natp m) … 'nil) (lines 487–492). For the seed nil it gives 't when the body has no call of the function and 'nil when it has one (lines 481–486), so a recursive function always needs a measure; a measure on a non-recursive function only asks for a natural (T3–T5 in claims.out).

6.3Shapes of the claim

The cases T4–T9 use these functions:

claims.scm

[…]
;; The functions of T4-T9.
(define f '(defun f (x) (car x)))
(define g '(defun g (x y) (if (g (cdr x) y) (g (car x) (g (cdr x) y)) 'nil)))
(define h '(defun h (x) (cons (if (atom x) '() (h (cdr x))) '())))
(define h2 '(defun h (x) (if (atom x) (cons '() '()) (cons (h (cdr x)) '()))))
(define m '(defun m (x y) (if (atom x) y (m (cdr x) (cons (car x) y)))))
(define d '(defun d (x) (if (atom x) 't (cons (d (cdr x)) (d (cdr x))))))
[…]

Several calls, and calls under an inner if. sub replaces every '? in a tree y by x, recursing into both halves (scheme/little-prover.scm:243–258). Its two calls give a conjunction of two obligations on the E side of (atom y), the shape of the claim of star-induction (T2 in claims.out). memb? (scheme/little-prover.scm:262–274) recurses only in the E of an inner if, and its claim keeps that if:

;; defun.memb?, seed (size xs)
(if (natp (size xs))
  (if (atom xs)
    't
    (if (equal (car xs) '?) 't (< (size (cdr xs)) (size xs))))
  'nil)

Equal obligations merge. remb removes every '? from a list, and calls (remb (cdr xs)) in both branches of its inner if (scheme/little-prover.scm:278–281). The two branches give the same obligation, if-c-when-necessary keeps one, and the inner test vanishes: the claim of remb is that of list?, with xs for x (claims.out). The two equal calls of d (T9) also give the claim of list?, since expr-recs returns one call.

Calls in a question, and nested calls. g (T6) has a call in the question, and in the A branch a call whose argument is a call. The nested call is the pattern of the book’s add-atoms (scheme/little-prover.scm:653–656):

;; T6 nested calls
(if (natp (size x))
  (if (< (size (cdr x)) (size x))
    (if (g (cdr x) y)
      (if (< (size (car x)) (size x))
        (< (size (cdr x)) (size x))
        'nil)
      't)
    'nil)
  'nil)

The obligation of the question comes first, with no condition, since the question is evaluated in every case. The A branch has two calls and two obligations. The one for (cdr x) appears again: list-extend compares the question’s obligation only with the whole if built from the branches. The measure ignores y, so the value of the inner call plays no part; in add-atoms the inner call is likewise the second argument.

A test that is lost. In h (T7) the call is guarded by (atom x), but that if is an argument of cons, off the spine. So the obligation has no condition, and for an atom it is false:

;; T7 if inside an application
(if (natp (size x)) (< (size (cdr x)) (size x)) 'nil)
;; value at x = 'a: 'nil
;; T7 with the if lifted to the top
(if (natp (size x))
  (if (atom x) 't (< (size (cdr x)) (size x)))
  'nil)

Yet h terminates: it calls itself only on the cdr of a cons. The check is conservative. The remedy is if-lifting (Section 4.4): h2 defines the same function with the if lifted to the top of the body, where its question becomes a condition, and it gets the claim of list?.

A measure of two arguments. m (T8) moves the elements of x onto y, with measure (+ (size x) (size y)). The substitution replaces both formals:

;; T8 measure of two arguments
(if (natp (+ (size x) (size y)))
  (if (atom x)
    't
    (< (+ (size (cdr x)) (size (cons (car x) y)))
       (+ (size x) (size y))))
  'nil)
;; value at x = '(a) y = '(): 'nil

The call puts (car x) on y in a new cons, which adds back exactly the cons that (cdr x) removes, so the sum stays the same and the obligation is false; with (size x), m gets the claim of list? (T8 in claims.out).

6.4Proving the claim

A totality claim is proved by steps like any other claim. The four steps of list? (scheme/little-prover.scm:238–241), replayed by list-totality.scm:

list-totality.scm

;; claim
(if (natp (size x))
  (if (atom x) 't (< (size (cdr x)) (size x)))
  'nil)
;; step 1: ((Q) (natp/size x))
(if 't (if (atom x) 't (< (size (cdr x)) (size x))) 'nil)
;; step 2: (() (if-true (if (atom x) 't (< (size (cdr x)) (size x))) 'nil))
(if (atom x) 't (< (size (cdr x)) (size x)))
;; step 3: ((E) (size/cdr x))
(if (atom x) 't 't)
;; step 4: (() (if-same (atom x) 't))
't
;; final: 't  PROVED

Step 1 uses the axiom natp/size, (equal (natp (size x)) 't) (scheme/j-bob.scm:825–826). Step 2 drops the if on 't; the arguments of if-true must spell out the focus, so they repeat the inner claim. Step 3 is the measure argument: size/cdr (scheme/j-bob.scm:831–834) is the equation (equal (< (size (cdr x)) (size x)) 't) under the premise that (atom x) is false, and the focus (E) lies on the E side of (atom x), which discharges it (Chapter 5). Step 4 merges the equal branches. The prelude proves the claim of list-induction with the same four axioms in another order (scheme/j-bob.scm:864–867).

The other proofs follow this plan: sub closes its obligations with size/car and size/cdr (scheme/little-prover.scm:255–258), memb? with size/cdr at (E E) (lines 272–274). book-totality.scm takes the proofs of the book’s recursive functions as data from the transcript (and the prelude’s from j-bob.scm), runs J-Bob/prove on each with the definitions the transcript gives it, and prints name, measure, number of steps and result. It then lists all 20 defun proofs of the transcript, with whether the body calls itself:

book-totality.scm

(list-induction (size x) 4 't)
(star-induction (size x) 6 't)
(list? (size x) 4 't)
(sub (size y) 6 't)
(memb? (size xs) 5 't)
(remb (size xs) 4 't)
;; defuns of the transcript
(pair nil 0 non-recursive)
[…]
(wt (size x) 6 recursive)
(align (wt x) 46 recursive)

In the full list every non-recursive defun has seed nil and 0 steps,17 lines are left out; the full list is in book-totality.out. and every recursive one a measure, size of an argument in all cases but one. The exception is align: the entry defun.align.attempt tries (size x) in 8 steps and is not proved (scheme/little-prover.scm:894–915; 08-book/align-attempt.out), and the book admits align with (wt x) (Chapter 8). Table 6.1 collects the cases of this section.

functionmeasureclaim inside (if (natp m) … 'nil)result
list?, remb, list-induction(size v)(if (atom v) 't OcdrO_{\mathit{cdr}})proved, 4 steps
sub, star-induction(size v)(if (atom v) 't (if OcarO_{\mathit{car}} OcdrO_{\mathit{cdr}} 'nil))proved, 6 steps
memb?(size v)(if (atom v) 't (if (equal (car v) '?) 't OcdrO_{\mathit{cdr}}))proved, 5 steps
partialnil'nil (the whole claim)refused
partial(size x)(if (< (size x) (size x)) 't 'nil)refused; false at '()
f = 1+f(size x)(< (size x) (size x))refused; false at '()
cdr recursionx(if (atom x) 't (< (cdr x) x))refused; false at '(a b)
Table 6.1: Totality claims and their fate. OcarO_{\mathit{car}} and OcdrO_{\mathit{cdr}} stand for (< (size (car v)) (size v)) and (< (size (cdr v)) (size v)), where v is x for list?, list-induction and star-induction, xs for remb and memb?, and y for sub. The claims are printed by claims.scm, contradiction.scm and nontermination.scm, the proofs checked by book-totality.scm; the refused ones were offered with no steps. “False at” gives a value of the variable at which the host evaluates the whole claim to 'nil.

6.5What the check refuses, and what it cannot see

Non-terminating definitions. nontermination.scm makes four attempts with two functions on (axioms), printing what J-Bob/prove returns (with no steps, the claim itself), its value at one x, and ADDED when J-Bob/define returns more definitions than it was given:

nontermination.scm

;; A f = 1+f, (size x)
(if (natp (size x)) (< (size x) (size x)) 'nil)
;; value at x = '(): 'nil
;; => REJECTED
[…]
;; G cdr recursion, measure x
(if (natp x) (if (atom x) 't (< (cdr x) x)) 'nil)
;; value at x = '(a b): 'nil
;; => REJECTED
;; G cdr recursion, (size x), 4 steps
't
;; => ADDED

(defun f (x) (+ '1 (f x))) calls itself with the same argument, so every measure gives (< m m), and the seed nil gives 'nil (the 3 lines left out); its equation would make (f x) one more than itself. The cdr recursion, last-atom in the script, is (defun f (x) (if (atom x) x (f (cdr x)))). It terminates, but the measure x is not a natural for '(a b). With (size x) and the four steps of list-induction it is admitted. We infer that no steps can turn a false claim into 't, since each step replaces an expression by one with the same value as long as the axioms and admitted definitions hold; Chapter 10 tests that assumption.

The function inside its own proof. The steps of a totality proof are checked against extend-rec defs (scheme/j-bob.scm:383–384), so they may name the function being defined, but rewrite/prove rewrites with defs (scheme/j-bob.scm:722–726), which does not contain it. In no-unfold.scm, with the prelude P, a first step turns the claim CC of len (that of list?) into (if (len x) C C), and a second unfolds (len x) at (Q).The first 4 lines of no-unfold.out, the definition of len and CC, are left out; len is (defun len (x) (if (atom x) '0 (+ '1 (len (cdr x))))).

no-unfold.scm

[…]
;; steps? against P, and against (extend-rec P len)
(nil t)
;; what extend-rec adds
(defun len (x) (len x))
;; lookup of len in P
len
;; J-Bob/prove: step 1 applies, step 2 changes nothing
(if (len x)
[…]

The steps are well-formed only with the stub. In P, lookup returns the bare name (scheme/j-bob.scm:131–136) and equality/def returns the claim unchanged (scheme/j-bob.scm:702), so step 2 is a no-op step, rewrite/continue stops (scheme/j-bob.scm:708–714), and J-Bob/prove returns the residual claim (if (len x) C C). So a function can be named in its own totality proof, but its equation cannot be used to prove its own termination.

The measure may not mention the function. proof? checks the seed with (seed? defs …) (scheme/j-bob.scm:382), and seed? checks a measure over the formals in defs (scheme/j-bob.scm:320–324), not in extend-rec defs. no-unfold.scm goes on with two bad measures, then admits len and unfolds it:Background. The ACL2 copy of J-Bob gives four of its own functions a measure: (declare (xargs :measure (size defs))) in defs?, and (size pfs) in proofs?, rewrite/prove+ and rewrite/define+1 (acl2/j-bob.lisp:367, 393, 744, 759); Chapter 11 discusses why.

;; seed? (len x) against P, and against (extend-rec P len)
(nil t)
;; J-Bob/prove with measure (len x)
'nil
;; J-Bob/prove with measure (size z)
'nil
[…]
;; unfolding (len y) after the definition
(if (atom y) '0 (+ '1 (len (cdr y))))

With a measure and no steps, J-Bob/prove would return the claim, an if, so these 'nils are its ill-formed answer (Chapter 5): (len x) would measure len by its own value, assuming what the claim is meant to show, and z is not a formal of len. After the definition, a step unfolds len. The induction claims of Chapter 7 follow the recursion of the functions admitted this way.