Closing the circle
Module 1 began with software that must not fail, and one of its examples was a compiler: CompCert, a C compiler with a machine-checked proof of correctness. Compilers deserve that attention. Much of the software you run relies on a compiler, and a translation bug can change the behaviour of correct source code. It can invalidate an end-to-end guarantee too. A proof about a C function speaks about the C code, but the computer runs whatever the compiler produced. In Exercise 1.6 the compiler appeared on the list of things that a verified program still has to trust. A verified compiler takes it off that list, or nearly so (Section 14.7 says what remains). Its theorem specifies the behaviour preserved when compilation succeeds, under the source and target semantics and the assumptions of that theorem.
The idea is almost as old as compilers. In 1967 John McCarthy and James Painter proved on paper that a compiler for arithmetic expressions is correct, the first proof of its kind. In 1972 Robin Milner and Richard Weyhrauch checked such a proof by machine, for a compiler from arithmetic expressions to code for a stack machine, in Stanford LCF, the forerunner of the LCF system of Module 1. Today CompCert compiles a large subset of C, and CakeML, verified in the prover HOL4, compiles a dialect of ML. Their proofs run to many thousands of lines, but the central argument is the one you will write in this module, for the language McCarthy and Painter started with.
You will build every piece yourself:
- a source language of arithmetic expressions, and its meaning (Section 14.2);
- a target machine that computes with a stack (Section 14.3);
- the compiler, and tests that compare source and target (Section 14.4);
- the proof that the compiler is correct (Section 14.5);
- an optimisation with its own proof, and the verified code exported to Haskell (Sections 14.6 and 14.7);
- extensions: subtraction, conditionals and jumps (Section 14.8).
What does “correct” mean for a compiler? Figure 14.1 shows the answer that this module proves. From an expression there are two ways to a result. You can evaluate the expression directly, using the meaning of the source language. Or you can compile it and run the code on the machine. The compiler is correct if the two ways always agree.
Evaluating an expression and running its compiled code must agree. The theorem compile_correct of
Section 14.5 says that they do, for every expression, every state of the variables and every stack.
The machine is a stack machine: it keeps intermediate results on a stack, and arithmetic instructions consume values from the top and push their results there. This is a realistic design. The Java Virtual Machine, WebAssembly and the interpreter of Python all run instructions for stack machines of this kind, and their compilers produce such code.
A compiler translates, and a correct translation preserves meaning. To prove that it does, you need the meaning of both languages. Here they are mathematical functions: evaluation for the source and execution for the target. Other languages may need relations to describe nontermination or nondeterminism. Here “preserves meaning” becomes an equation that you can test on examples and prove by induction.
Expressions and their meaning
Names, states and expressions
Programs are written as text, but a compiler works on their structure. The text x + (2 + y) is an
addition whose left operand is the variable x and whose right operand is another addition, of the number
2 and the variable y. The program that turns text into structure is the parser. Parsing is a subject
of its own. This module starts from syntax trees and does not verify a parser or a textual input format.
The abstract syntax of a language describes its programs as trees: each node is an operation and its children are the operands. Parentheses and spaces belong to the text, the concrete syntax, and do not appear in the tree. In Isabelle, abstract syntax is a datatype.
Expressions contain variables, so you need names for variables and a way to give them values. Names are
strings. As in Module 4, a string is written between two pairs of single quotes, as in ''x'', because
the double quote already marks the end of a term. Values are integers (int, Module 8), so that
subtraction makes sense when Section 14.8 adds it. The values of all variables at one moment form a
state: a function from names to values.
type_synonym vname = string
type_synonym state = "vname ⇒ int"type_synonym introduces a new name for an existing type. vname is another name for string, which is
itself char list, a list of characters, and state is another name for vname ⇒ int. Nothing new is
created: Isabelle’s output often shows the full types instead of the short names.
An expression is a number, a variable, or the sum of two expressions:
datatype expr = Num int | Var vname | Add expr exprThe datatype is recursive, like the trees of Module 5, and its values are exactly the trees described
above: Add makes a node with two children, and Num and Var make leaves. The text x + (2 + y)
becomes the tree Add (Var ''x'') (Add (Num 2) (Var ''y'')). It will serve as the running example, so
give it a name:
definition sample :: expr where
"sample = Add (Var ''x'') (Add (Num 2) (Var ''y''))"Evaluation
The meaning of an expression is its value, and its value depends on the state. A recursive function says what each kind of node means:
fun eval :: "expr ⇒ state ⇒ int" where
"eval (Num n) s = n"
| "eval (Var x) s = s x"
| "eval (Add a b) s = eval a s + eval b s"consts
eval :: "expr ⇒ (char list ⇒ int) ⇒ int"
Found termination order: "(λp. size (fst p)) <*mlex*> {}"Read the equations aloud: a number means itself; a variable means its value in the state s; a sum means
the sum of the meanings of its parts. On the left, Add is a piece of syntax; on the right, + is the
addition of integers. eval connects the two. Isabelle shows the type of eval with the synonyms
unfolded, state as char list ⇒ int, and reports that eval terminates because each recursive call
is on a smaller expression, the first of its two arguments (Module 8).
The semantics of a language gives each of its programs a precise mathematical meaning, the second
ingredient of a formal method in Module 1. Here the meaning of an expression e in a state s is the
integer eval e s. The definition is compositional: the meaning of a node is computed from the
meanings of its children.
To try eval, you need a state. Isabelle writes f(a := v) for the function that maps a to v and every
other argument b to f b; you met this notation in Nitpick’s counterexamples in Module 3. Start from
λ_. 0, the function that maps every name to 0 (the underscore is a variable whose name does not
matter), and update it twice:
definition s0 :: state where
"s0 = (λ_. 0)(''x'' := 5, ''y'' := 7)"
value "(s0 ''x'', s0 ''y'', s0 ''z'')""(5, 7, 0)"
:: "int × int × int"In the state s0 the variable x has the value 5, y has 7 and every other variable has 0. Now evaluate
the running example:
value "eval sample s0""14"
:: "int"The equations of eval produce the same result step by step. Unfold sample and work from the outside
in:
A stack machine
The target machine knows nothing about expressions. It has a stack of integers, reads variables from the state, as the expressions do, and understands three instructions:
PUSH nputs the number n on top of the stack;LOAD xputs the value of the variable x on top of the stack;ADDtakes the two numbers on top of the stack off and puts their sum on.
datatype instr = PUSH int | LOAD vname | ADD
type_synonym stack = "int list"A stack is a list whose head is the top: in [2, 5] the 2 lies on top of the 5. Putting a value on the
stack is #, and taking values off is pattern matching on #.
A stack machine computes with a stack, a list of values that is used only at one end, its
top. Arithmetic instructions take values from the top and push a result; instructions such as
PUSH and LOAD supply values. A program for this straight-line machine is a list of instructions,
executed from left to right.
The function exec1 executes one instruction:
fun exec1 :: "instr ⇒ state ⇒ stack ⇒ stack" where
"exec1 (PUSH n) s stk = n # stk"
| "exec1 (LOAD x) s stk = s x # stk"
| "exec1 ADD s (j # i # stk) = (i + j) # stk"
| "exec1 ADD s stk = stk"The third equation does the work of ADD: j is the top of the stack and i the number below it, and the
sum i + j replaces both. The order of i and j looks irrelevant now, but it will matter when you add
subtraction in Section 14.8. Every HOL function denotes a value for every argument (Module 8); the
fourth equation makes a deliberate choice for malformed ADD instructions instead of leaving a
missing case unspecified. With fewer than two numbers, it leaves the stack unchanged. Another machine
might report stack underflow, meaning too few operands. Compiled expressions supply both operands
before ADD; Exercise 14.7 verifies this with a machine that reports failure explicitly.
The third and fourth equations overlap: a stack with two or more numbers matches both. fun resolves the
overlap in order, the earlier equation first (Module 8). thm exec1.simps shows the equations that
fun has derived from the definition, the ones simp uses:
thm exec1.simps exec1 (PUSH ?n) ?s ?stk = ?n # ?stk
exec1 (LOAD ?x) ?s ?stk = ?s ?x # ?stk
exec1 ADD ?s (?j # ?i # ?stk) = (?i + ?j) # ?stk
exec1 ADD ?s [] = []
exec1 ADD ?s [?v] = [?v]The fourth equation has become two, for the empty stack and for a stack with one number: exactly the stacks that the third equation does not cover.
A program runs one instruction after the other, each on the stack that the previous one left:
fun exec :: "instr list ⇒ state ⇒ stack ⇒ stack" where
"exec [] s stk = stk"
| "exec (i # c) s stk = exec c s (exec1 i s stk)"The letter c stands for code, a list of instructions. Now run the machine, first one instruction, then a whole program:
value "exec1 ADD s0 [7, 5]""[12]"
:: "int list"value "exec [LOAD ''x'', PUSH 2, LOAD ''y'', ADD, ADD] s0 []""[14]"
:: "int list"Figure 14.2 shows the stack after each instruction of this program. The stack grows to three numbers
when LOAD ''y'' has run, and each ADD makes it one shorter.
The program LOAD ''x'', PUSH 2, LOAD ''y'', ADD, ADD run in the state s0 on the empty stack. Each
ADD replaces the two numbers on top by their sum.
This sequence only pushes values, so pre-existing stack contents stay below the new values:
value "exec [LOAD ''x'', PUSH 2] s0 [100]""[2, 5, 100]"
:: "int list"Arbitrary machine programs can consume pre-existing values: ADD above did so. The compiler theorem
will establish that the code of an expression leaves the original stack intact while pushing its result.
The compiler
Compiling expressions
How does the machine compute Add a b? It runs code that leaves the value of a on the stack, then code
that leaves the value of b on top of it, and then ADD. Numbers and variables need one instruction
each. That is the whole compiler:
fun compile :: "expr ⇒ instr list" where
"compile (Num n) = [PUSH n]"
| "compile (Var x) = [LOAD x]"
| "compile (Add a b) = compile a @ compile b @ [ADD]"@ appends lists (Module 1). Like eval, the compiler has one equation for each kind of node, and the
code for a node is built from the code of its children. Compile the running example:
value "compile sample""[LOAD ''x'', PUSH 2, LOAD ''y'', ADD, ADD]"
:: "instr list"This is the program of Figure 14.2. The operands come before their operator: first x, then 2 and y, then
the inner ADD, then the outer one. This order is called postfix or reverse Polish notation.
Some pocket calculators work in reverse Polish notation. To compute 2 + 3 you key in 2, Enter, 3 and
then +. The calculator keeps the numbers on a stack until the + combines them, exactly as ADD does. The
compiler translates the notation you learned at school into the keystrokes for such a calculator.
Now compare evaluation and execution on the example, with 100 already on the stack:
value "(exec (compile sample) s0 [100], eval sample s0 # [100])""([14, 100], [14, 100])"
:: "int list × int list"Both give 14 on top of the 100.
Why is the compiler called compile and not the shorter comp? Because comp is taken. It is function
composition, written ∘:
term comp"(∘)"
:: "('a ⇒ 'b) ⇒ ('c ⇒ 'a) ⇒ 'c ⇒ 'b"Isabelle would accept a function of your own called comp, and the name would then refer to yours. But
the short name would have two meanings, so Isabelle may need to print a qualified name such as
T.comp (Num 1) for a theory T to disambiguate it. Before you name a
function, ask Isabelle whether the name is free. term prints the type of a constant that exists; a name
that is fresh and undeclared is read as a free variable (Module 8); in an unconstrained term command
it has type 'a. That output is not evidence that a constant exists. find_consts (Module 9) searches
by type, and with the criterion name: also by name.
What correct means
On the example, the two ways agree. “Correct” means that they agree always:
The compiler is correct if for every expression e, every state s and every stack stk,
In words: running the code of e on any stack puts the value of e on top of that stack and changes nothing below it.
Three things about this statement are worth noticing.
- It is a specification in the sense of Module 12:
evalsays what the value must be, andcompilewithexecis the implementation. - It speaks about every stack, although a user starts with the empty one. The compiler combines code:
the code of b in
Add a bruns on a stack that already holds the value of a. Section 14.5 shows that the proof needs the general form. - It describes the whole stack. A weaker claim, “the top of the stack is the value of e”, would allow code that destroys what lies below, and such code would break the code around it.
Test before you prove
A proof takes effort; a test takes seconds. Module 9 advised testing every claim before trying to prove it, so start with Quickcheck:
lemma "exec (compile e) s stk = eval e s # stk"
quickcheck [size = 3]
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found no counterexample.Quickcheck found no counterexample among its bounded test values for expressions, states and stacks.
size = 3 limits the generators; it does not enumerate all mathematical functions from strings to
integers. With the larger default search it may run out of time. A false claim can also need values
beyond the bounds, though Quickcheck stops as soon as it finds a counterexample.
Quickcheck is at its best on claims that are wrong. Here is a correctness claim that forgets the stack:
lemma "exec (compile e) s stk = [eval e s]"
quickcheck
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found a counterexample:
e = Num (- 1)
s = λx. - 2
stk = [- 1]
Evaluated terms:
exec (compile e) s stk = [- 1, - 1]
[eval e s] = [- 1]Read the counterexample. The expression is the number −1 (Isabelle prints negative numbers with a space, Module 8), the state maps every name to −2, which does not matter here, and the stack already holds −1. The code pushes −1 on top, so the result has two numbers where the claim expects one. The claim, not the compiler, is wrong.
Now a compiler with a bug. Writing each operator between its operands, in the order of the text
x + y, is a natural first attempt:
fun compile_bad :: "expr ⇒ instr list" where
"compile_bad (Num n) = [PUSH n]"
| "compile_bad (Var x) = [LOAD x]"
| "compile_bad (Add a b) = compile_bad a @ [ADD] @ compile_bad b"
value "(compile_bad sample, exec (compile_bad sample) s0 [])""([LOAD ''x'', ADD, PUSH 2, ADD, LOAD ''y''], [7, 7])"
:: "instr list × int list"The first ADD finds a single number and changes nothing, the second adds 2 to 5, and the value of y
ends up on the stack next to the 7. On a single example you might wonder whether you set it up wrongly.
Quickcheck removes the doubt:
lemma "exec (compile_bad e) s stk = eval e s # stk"
quickcheck
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found a counterexample:
e = Add (Num 0) (Num 0)
s = λx. - 2
stk = []
Evaluated terms:
exec (compile_bad e) s stk = [0, 0]
eval e s # stk = [0]The counterexample is 0 + 0, an expression with one addition. The particular numbers do not matter: the order of the instructions is wrong. A single addition is the smallest expression shape on which this compiler differs from the correct one.
Test before you prove. value evaluates examples; Quickcheck searches bounded test cases and reports
a counterexample if it finds one. Neither establishes an unrestricted correctness theorem. A genuine
counterexample refutes the claim, while an unfinished proof attempt alone does not tell you whether
the claim or the proof strategy is at fault.
The correctness proof
On paper
Claim. For every expression e, state s and stack stk, exec (compile e) s stk = eval e s # stk.
Proof by structural induction on e (Module 5), with one case for each constructor.
- Case
Num n. The code is[PUSH n], which puts n on the stack, andeval (Num n) s = n. - Case
Var x. The code is[LOAD x], which putss xon the stack, andeval (Var x) s = s x. - Case
Add a b, where the claim holds for a and for b (the induction hypotheses). The code iscompile a @ compile b @ [ADD]. Run it in three stages (Figure 14.3):compile aruns on stk. By the hypothesis for a, it leaveseval a s # stk.compile bruns oneval a s # stk. By the hypothesis for b, applied to this stack, it leaveseval b s # eval a s # stk.ADDreplaces the two numbers on top by their sum, which gives(eval a s + eval b s) # stk, and that iseval (Add a b) s # stk. ∎
The case Add a b of the proof. The hypothesis for a is used with the stack of the claim, the
hypothesis for b with a different one.
Two steps of this argument use more than the equations of the functions.
- “Run it in three stages.” That running
c1 @ c2means running c1 and then running c2 on the stack c1 leaves is a property ofexec. It is true, but it needs its own proof: an auxiliary lemma (Module 11). - “Applied to this stack.” Stage 2 uses the hypothesis for b with the stack
eval a s # stk, not with stk. That is allowed only if the claim, and with it the induction hypothesis, speaks about every stack. The induction must be generalised over the stack (Module 11).
The next two sections let Isabelle discover both points from its proof states.
The lemma about appended code
The claim a user of the compiler cares about starts with the empty stack and expects one number at the end. Try to prove it with the usual induction:
lemma "exec (compile e) s [] = [eval e s]"
apply (induction e)
apply autoproof (prove)
goal (1 subgoal):
1. ⋀e1 e2.
⟦exec (compile e1) s [] = [eval e1 s];
exec (compile e2) s [] = [eval e2 s]⟧
⟹ exec (compile e1 @ compile e2 @ [ADD]) s [] =
[eval e1 s + eval e2 s]auto has proved the cases Num and Var and unfolded compile in the case Add; Isabelle calls the
two subexpressions e1 and e2, after the type expr. Then it stopped. Module 11’s recipe starts with the
subterm that the simplifier cannot rewrite: exec (compile e1 @ compile e2 @ [ADD]) s []. The equations
of exec are about [] and about i # c, and this argument is an append. Abandon the attempt:
oopsNext, replace the parts that are specific to this proof by variables: the question is what
exec (c1 @ c2) s stk is, for any code c1 and c2. The paper proof gives the answer, run c1 first and c2
afterwards, and an example agrees:
value "(exec ([LOAD ''x'', PUSH 2] @ [ADD]) s0 [1],
exec [ADD] s0 (exec [LOAD ''x'', PUSH 2] s0 [1]))""([7, 1], [7, 1])"
:: "int list × int list"Test the conjecture:
lemma "exec (c1 @ c2) s stk = exec c2 s (exec c1 s stk)"
quickcheck [size = 3]
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found no counterexample.Prove it by induction on c1. The recursive call of exec receives a new stack, exec1 i s stk, so
generalise the stack to give this induction the required hypothesis, following Module 11’s strategy:
lemma exec_append [simp]: "exec (c1 @ c2) s stk = exec c2 s (exec c1 s stk)"
apply (induction c1 arbitrary: stk)proof (prove)
goal (2 subgoals):
1. ⋀stk. exec ([] @ c2) s stk = exec c2 s (exec [] s stk)
2. ⋀a c1 stk.
(⋀stk. exec (c1 @ c2) s stk = exec c2 s (exec c1 s stk)) ⟹
exec ((a # c1) @ c2) s stk = exec c2 s (exec (a # c1) s stk)In subgoal 2, the hypothesis holds for every stack (⋀stk). The simplifier rewrites (a # c1) @ c2 to
a # (c1 @ c2), executes a, and then needs the hypothesis for the stack exec1 a s stk, which it may
now use:
apply auto
doneThe attribute [simp] adds the lemma to the simplifier’s rules (Module 9), so that simp and auto
split appended code from now on.
Generalise over the stack
With exec_append available, try the claim about the empty stack again:
lemma "exec (compile e) s [] = [eval e s]"
apply (induction e)
apply autoproof (prove)
goal (1 subgoal):
1. ⋀e1 e2.
⟦exec (compile e1) s [] = [eval e1 s];
exec (compile e2) s [] = [eval e2 s]⟧
⟹ exec1 ADD s (exec (compile e2) s [eval e1 s]) =
[eval e1 s + eval e2 s]Progress: the code of e1 has run, by exec_append and the hypothesis for e1, and left [eval e1 s].
Now the code of e2 runs on the stack [eval e1 s], but the hypothesis for e2 speaks only about the empty
stack. This is the second point of the paper proof. Strengthen the induction statement so that its
hypothesis covers the stack passed to the second subexpression.
oopsModule 11 showed what to do, in two steps:
- Replace the constant by a variable: claim the result for any stack stk. The expected result then
becomes
eval e s # stkinstead of[eval e s]. This is the statement of Definition 14.4. - Make the variable arbitrary in the induction, so that the hypotheses hold for every stack, including
the stack
eval e1 s # stkon which the code of e2 runs.
With both steps the proof goes through:
lemma "exec (compile e) s stk = eval e s # stk"
by (induction e arbitrary: stk) autoStep 1 alone is not enough. Without arbitrary: stk, stk is fixed for the whole induction, the
hypothesis for e2 speaks about that one stack, and the code of e2 still runs on another one. The box at
the end of Section 14.5.4 shows the error you get.
State the claim in the form that the recursion needs. compile places the code of b after the code of
a, so the code of b runs on a stack that already holds the value of a. A claim about the empty stack
cannot describe that; a claim about every stack can. The stronger claim is the easier one to prove: it
is Module 11’s inventor’s paradox once more.
The proof in Isar
The one-line proof hides the argument. The Isar proof follows the paper proof step by step, and Isabelle
checks every step. theorem means the same as lemma; it marks the main results of a theory.
theorem compile_correct: "exec (compile e) s stk = eval e s # stk"
proof (induction e arbitrary: stk)proof (state)
goal (3 subgoals):
1. ⋀x stk. exec (compile (Num x)) s stk = eval (Num x) s # stk
2. ⋀x stk. exec (compile (Var x)) s stk = eval (Var x) s # stk
3. ⋀e1 e2 stk.
⟦⋀stk. exec (compile e1) s stk = eval e1 s # stk;
⋀stk. exec (compile e2) s stk = eval e2 s # stk⟧
⟹ exec (compile (Add e1 e2)) s stk = eval (Add e1 e2) s # stkRead subgoal 3 aloud: “for all e1, e2 and stk: if for every stack the claim holds for e1, and for every
stack it holds for e2, then it holds for Add e1 e2 and stk”. The ⋀stk inside the hypotheses is the
effect of arbitrary: stk. The cases Num and Var follow from the definitions; enter the case Add
with names of your own for the subexpressions:
case (Num n)
show ?case by simp
next
case (Var x)
show ?case by simp
next
case (Add a b)proof (state)
this:
exec (compile a) s ?stk = eval a s # ?stk
exec (compile b) s ?stk = eval b s # ?stk
goal (1 subgoal):
1. ⋀e1 e2 stk.
⟦⋀stk. exec (compile e1) s stk = eval e1 s # stk;
⋀stk. exec (compile e2) s stk = eval e2 s # stk⟧
⟹ exec (compile (Add e1 e2)) s stk = eval (Add e1 e2) s # stkThe induction hypotheses are listed under this, with the schematic variable ?stk (Module 7): they
may be used with any stack. Add.IH(1) is the hypothesis for a, and Add.IH(2) the one for b. The
paper proof uses each of them once, a with the stack stk and b with the stack eval a s # stk. State
the two facts and let rule instantiate ?stk:
have run_a: "exec (compile a) s stk = eval a s # stk"
by (rule Add.IH(1))
have run_b: "exec (compile b) s (eval a s # stk)
= eval b s # eval a s # stk"proof (prove)
goal (1 subgoal):
1. exec (compile b) s (eval a s # stk) = eval b s # eval a s # stk by (rule Add.IH(2))Now the calculation of Module 10, in the three stages of Figure 14.3:
have "exec (compile (Add a b)) s stk
= exec [ADD] s (exec (compile b) s (exec (compile a) s stk))"
by simp
also have "… = exec [ADD] s (exec (compile b) s (eval a s # stk))"
by (simp only: run_a)
also have "… = exec [ADD] s (eval b s # eval a s # stk)"
by (simp only: run_b)
also have "… = eval (Add a b) s # stk"
by simp
finally show ?case .
qedThe first step is the shape of the code: simp unfolds compile and applies exec_append twice, so
that the code of a runs first, then the code of b, then ADD. The second and third steps rewrite with
run_a and run_b only (simp only, Module 9). The last step executes ADD and unfolds eval. The
theorem a user wants, for the empty stack, is now an instance:
corollary compile_correct_empty: "exec (compile e) s [] = [eval e s]"
by (simp add: compile_correct)corollary is another synonym of lemma, for a result that follows directly from a theorem.
For every expression e, state s and stack stk, exec (compile e) s stk = eval e s # stk. In
particular, exec (compile e) s [] = [eval e s]. (Theorems compile_correct and
compile_correct_empty.)
If something went wrong
Leave out arbitrary: stk, and the step run_b fails:
lemma "exec (compile e) s stk = eval e s # stk"
proof (induction e)
case (Num n)
show ?case by simp
next
case (Var x)
show ?case by simp
next
case (Add a b)
have run_b: "exec (compile b) s (eval a s # stk)
= eval b s # eval a s # stk"
by (rule Add.IH(2))
then show ?case using Add.IH(1) by simp
qedFailed to apply initial proof method⌂:
goal (1 subgoal):
1. exec (compile b) s (eval a s # stk) = eval b s # eval a s # stkWithout arbitrary, Add.IH(2) says exec (compile b) s stk = eval b s # stk for the one stack stk of
the claim. It does not fit the goal, which is about the stack eval a s # stk, so rule cannot apply
it. When rule with an induction hypothesis fails like this, look for a variable that should be
arbitrary (Module 11).
A verified optimisation
Real compilers transform code to improve it: they optimise it. Each transformation must preserve the behaviour required by its specification, which creates another proof obligation. A verified optimisation comes with a theorem stating which behaviour it preserves. Here expressions use total, pure integer arithmetic, and preservation means equality of their values in every state.
Constant folding
Part of an expression may not depend on the state at all. In (2 + 3) + (x + 0), the sum 2 + 3 is 5 in
every state, and adding 0 changes nothing. A compiler can do that work once, at compile time, instead of
every time the code runs.
Constant folding evaluates constant subexpressions at compile time. The pass below also performs an algebraic simplification, removing an addition of 0 on the right. An optimisation is correct, or semantics-preserving, if the optimised program has the same meaning as the original in every state.
The work is done one addition at a time, by a function that builds an addition and simplifies it where it can:
fun fold_add :: "expr ⇒ expr ⇒ expr" where
"fold_add (Num m) (Num n) = Num (m + n)"
| "fold_add a b = (if b = Num 0 then a else Add a b)"Two numbers are added at once. Otherwise an addition of 0 on the right is dropped, and anything else stays an addition. The two equations overlap, and the first one wins. The optimiser folds the children of a node first, and then the node itself:
fun cfold :: "expr ⇒ expr" where
"cfold (Num n) = Num n"
| "cfold (Var x) = Var x"
| "cfold (Add a b) = fold_add (cfold a) (cfold b)"Try it on (2 + 3) + (x + 0):
definition sample2 :: expr where
"sample2 = Add (Add (Num 2) (Num 3)) (Add (Var ''x'') (Num 0))"
value "cfold sample2""Add (Num 5) (Var ''x'')"
:: "expr"The result is 5 + x. The code shrinks from seven instructions to three:
value "(compile sample2, compile (cfold sample2))""([PUSH 2, PUSH 3, ADD, LOAD ''x'', PUSH 0, ADD, ADD],
[PUSH 5, LOAD ''x'', ADD])"
:: "instr list × instr list"cfold folds only where two numbers meet. In 2 + (3 + x) they do not, and nothing changes:
value "cfold (Add (Num 2) (Add (Num 3) (Var ''x'')))""Add (Num 2) (Add (Num 3) (Var ''x''))"
:: "expr"Turning it into 5 + x would need the associative law, a cleverer optimiser and a proof to match.
Folding preserves meaning
The claim is eval (cfold e) s = eval e s. Its case Add needs a fact about fold_add: whatever it
builds has the value of the sum of its arguments. Prove that first, by computation induction on
fold_add (Module 11):
lemma eval_fold_add [simp]: "eval (fold_add a b) s = eval a s + eval b s"
apply (induction a b rule: fold_add.induct)proof (prove)
goal (5 subgoals):
1. ⋀m n. eval (fold_add (Num m) (Num n)) s =
eval (Num m) s + eval (Num n) s
2. ⋀v b. eval (fold_add (Var v) b) s = eval (Var v) s + eval b s
3. ⋀v va b. eval (fold_add (Add v va) b) s = eval (Add v va) s + eval b s
4. ⋀a v. eval (fold_add a (Var v)) s = eval a s + eval (Var v) s
5. ⋀a v va. eval (fold_add a (Add v va)) s = eval a s + eval (Add v va) sThe cases follow the equations of fold_add, after fun has made them non-overlapping, as it did for
exec1. The second equation has become four: one for each shape of a and b that the first equation does
not cover. In every case the simplifier can evaluate fold_add, and auto splits the if into its
two cases:
apply auto
doneThe theorem follows by structural induction:
theorem cfold_correct: "eval (cfold e) s = eval e s"
by (induction e) autoThis time no variable is arbitrary. eval passes the state unchanged to its recursive calls, so the
hypotheses for the same s are exactly what the case Add needs. Compare exec, whose recursive call
receives a new stack: inspecting changed arguments is a useful guide to the generalisation needed by
these proofs, rather than a universal rule for choosing induction hypotheses.
Optimise, then compile
The two theorems fit together. Optimise first, then compile, and the code still computes the value of the original expression:
theorem compile_cfold_correct:
"exec (compile (cfold e)) s stk = eval e s # stk"
by (simp add: compile_correct cfold_correct)compile_correct turns the left-hand side into eval (cfold e) s # stk, and cfold_correct turns that
into the right-hand side. Verified compilers grow in this way. CompCert translates C in a long series of
passes, through several intermediate languages, and each pass has its own proof that it preserves the
meaning; the theorems compose into one about the whole chain of passes.
An optimisation is correct with respect to a semantics, here the semantics of mathematical integers.
For other kinds of numbers it may be wrong. In IEEE floating-point arithmetic under round-to-nearest,
adding +0.0 to −0.0 gives +0.0. Replacing x + 0.0 by x can therefore change the sign of zero.
A compiler required to preserve signed-zero behaviour cannot apply that rewrite unconditionally;
options that relax floating-point semantics can permit it. Applying the integer proof to floating-point
code would need a separate argument for the chosen semantics.
From proof to program
The compiler is a function in HOL, and Module 8 showed how to turn such functions into programs in other languages. Export the verified compiler and optimiser as Haskell:
export_code compile cfold in Haskell module_name StackCompilerSee theory exportsAs in Module 8, the code does not appear in the Output panel. In Isabelle/jEdit the words theory
exports are a link: click it, and the File Browser opens on the generated files, in a virtual folder
whose name begins with isabelle-export:. Each export_code of a theory writes into a folder of its
own, export1 for the first, export2 for the second, and so on; here export1 holds the Haskell
module StackCompiler.hs. Here are its central parts, as Isabelle2026 generates them. The two
datatypes:
data Expr = Num Int | Var [Char] | Add Expr Expr;
data Instr = PUSH Int | LOAD [Char] | ADD;
and the three functions:
fold_add :: Expr -> Expr -> Expr;
fold_add (Num m) (Num n) = Num (plus_int m n);
fold_add (Var v) b =
(if equal_expr b (Num Zero_int) then Var v else Add (Var v) b);
fold_add (Add v va) b =
(if equal_expr b (Num Zero_int) then Add v va else Add (Add v va) b);
fold_add a (Var v) =
(if equal_expr (Var v) (Num Zero_int) then a else Add a (Var v));
fold_add a (Add v va) =
(if equal_expr (Add v va) (Num Zero_int) then a else Add a (Add v va));
cfold :: Expr -> Expr;
cfold (Num n) = Num n;
cfold (Var x) = Var x;
cfold (Add a b) = fold_add (cfold a) (cfold b);
compile :: Expr -> [Instr];
compile (Num n) = [PUSH n];
compile (Var x) = [LOAD x];
compile (Add a b) = compile a ++ compile b ++ [ADD];
cfold and compile have the same equations as in Isabelle, with Haskell’s ++ for @. fold_add
has the five equations that fun derived from its two (Section 14.6.2), and the test b = Num 0 has
become a call of equal_expr, an equality test that the code generator wrote for the datatype. The parts
left out are the module header and the types Char and Int, with the operations on them that the
functions need, such as plus_int. Characters become a datatype with eight Boolean fields, and integers
are built from the constructors Zero_int, Pos and Neg and the binary numerals One, Bit0 and
Bit1 of Module 8. That is faithful but slow; the library theory HOL-Library.Code_Target_Int makes the
code use Haskell’s own integers instead.
The module header lists what the module exports: the types Char, Int, Expr and Instr and the
functions cfold and compile, but no constructors. Module 12 explained why export_code does this.
Here it means that other Haskell code cannot build an expression to give to compile. Write
export_code open compile cfold … to export everything, constructors included.
What does the proof guarantee for this program? The theorems are about the HOL functions. The code generator is designed so that each step of the generated program corresponds to rewriting with equations that are theorems, but it is not part of the kernel (Module 8): it belongs to the trusted base, together with the Haskell compiler that turns the generated code into machine code, and the hardware and runtime, plus the agreed representation of logical values and any hand-written target adaptations (Module 12). The theorem covers this syntax-tree compiler and the modelled stack machine; it does not verify a parser, assembler, or physical machine implementation. CompCert also uses Rocq-to-OCaml extraction and has a documented boundary of verified and unverified components.
Extending the compiler ★★★
This section is a stretch, at the level of the ★★★ exercises. It adds subtraction and a conditional expression to the language, and subtraction and jumps to the machine. Most of the development carries over; the lemma about appended code does not, and its replacement is a lesson in generalisation.
In your own theory you would extend expr, instr and the functions in place. On this page the earlier
definitions must stay as they are, so the extended versions get new names: the types and functions end
in 2, and so do the old constructors (Num2, Var2, Add2, PUSH2, LOAD2, ADD2); the new
constructors have plain names.
Subtraction and conditionals
datatype expr2 = Num2 int | Var2 vname | Add2 expr2 expr2
| Sub expr2 expr2 | Cond expr2 expr2 expr2
fun eval2 :: "expr2 ⇒ state ⇒ int" where
"eval2 (Num2 n) s = n"
| "eval2 (Var2 x) s = s x"
| "eval2 (Add2 a b) s = eval2 a s + eval2 b s"
| "eval2 (Sub a b) s = eval2 a s - eval2 b s"
| "eval2 (Cond t a b) s = (if eval2 t s ≠ 0 then eval2 a s else eval2 b s)"Sub a b is a − b. Cond t a b is a conditional expression, written t ? a : b in C: its value is the
value of a if the test t is not 0, and the value of b otherwise. As in C, 0 counts as false.
The machine gets an instruction for subtraction and two jumps. JMP n skips the next n
instructions. JMPZ n takes the number on top of the stack off and skips the next n instructions if that
number is 0; otherwise execution continues with the next instruction.
datatype instr2 = PUSH2 int | LOAD2 vname | ADD2 | SUB | JMP nat | JMPZ nat
fun exec2 :: "instr2 list ⇒ state ⇒ stack ⇒ stack" where
"exec2 [] s stk = stk"
| "exec2 (PUSH2 n # c) s stk = exec2 c s (n # stk)"
| "exec2 (LOAD2 x # c) s stk = exec2 c s (s x # stk)"
| "exec2 (ADD2 # c) s (j # i # stk) = exec2 c s ((i + j) # stk)"
| "exec2 (SUB # c) s (j # i # stk) = exec2 c s ((i - j) # stk)"
| "exec2 (JMP n # c) s stk = exec2 (drop n c) s stk"
| "exec2 (JMPZ n # c) s (v # stk) =
(if v = 0 then exec2 (drop n c) s stk else exec2 c s stk)"
| "exec2 (_ # c) s stk = exec2 c s stk"consts
exec2 :: "instr2 list ⇒ (char list ⇒ int) ⇒ int list ⇒ int list"
Found termination order: "(λp. length (fst p)) <*mlex*> {}"drop n c is c without its first n elements, so a jump forward is a recursive call on a shorter
program. The last equation catches what the others leave out, an ADD2 or SUB on a short stack and a
JMPZ on the empty one, and skips the instruction. Jumps only go forwards, so every recursive call is on
a shorter list, and Isabelle finds the termination order, the length of the program, without help. With
unrestricted backward jumps some programs could run forever, so this unfuelled recursive interpreter
would no longer pass fun’s termination requirement (more on that in Section 14.8.4).
The order of operands
SUB computes i − j, where j is the top of the stack and i the number below it. So the compiler must
push a first and b second, as it does for addition:
fun compile2 :: "expr2 ⇒ instr2 list" where
"compile2 (Num2 n) = [PUSH2 n]"
| "compile2 (Var2 x) = [LOAD2 x]"
| "compile2 (Add2 a b) = compile2 a @ compile2 b @ [ADD2]"
| "compile2 (Sub a b) = compile2 a @ compile2 b @ [SUB]"
| "compile2 (Cond t a b) =
compile2 t @ [JMPZ (length (compile2 a) + 1)] @ compile2 a @
[JMP (length (compile2 b))] @ compile2 b"For addition the order did not matter, because addition is commutative; for subtraction it does. Had the
Sub equation pushed b first, the code would compute b − a. Quickcheck catches the swap at once:
lemma "exec2 (compile2 b @ compile2 a @ [SUB]) s stk
= (eval2 a s - eval2 b s) # stk"
quickcheck
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found a counterexample:
b = Num2 0
a = Num2 (- 1)
s = λx. - 2
stk = []
Evaluated terms:
exec2 (compile2 b @ compile2 a @ [SUB]) s stk = [1]
(eval2 a s - eval2 b s) # stk = [- 1]With a = −1 and b = 0, the value of a − b is −1, but the swapped code computes 0 − (−1) = 1. A bug of this kind can survive tests on hand-picked examples: whenever a and b have the same value, the swapped code gives the right answer.
Conditionals and jumps
The code for Cond t a b evaluates the test and then jumps over the code it does not need:
code of t | JMPZ k | code of a | JMP m | code of b |
k = length (code of a) + 1 m = length (code of b)
If the test is 0, JMPZ k skips the code of a and the JMP after it, and execution continues with the
code of b. Otherwise the code of a runs, and JMP m skips the code of b. Either way, execution continues
after the code of b with one new value on the stack. Two runs, in the state s0 where x is 5 and z is 0:
value "compile2 (Cond (Var2 ''x'') (Num2 1) (Num2 2))""[LOAD2 ''x'', JMPZ 2, PUSH2 1, JMP 1, PUSH2 2]"
:: "instr2 list"value "(exec2 (compile2 (Cond (Var2 ''x'') (Num2 1) (Num2 2))) s0 [],
exec2 (compile2 (Cond (Var2 ''z'') (Num2 1) (Num2 2))) s0 [])""([1], [2])"
:: "int list × int list"Now the proof. The lemma about appended code was the key to Section 14.5. Is it still true?
lemma "exec2 (c1 @ c2) s stk = exec2 c2 s (exec2 c1 s stk)"
quickcheck
oopsTesting conjecture with Quickcheck-exhaustive...
Quickcheck found a counterexample:
c1 = [JMP 1]
c2 = [PUSH2 (- 1)]
s = λx. - 3
stk = []
Evaluated terms:
exec2 (c1 @ c2) s stk = []
exec2 c2 s (exec2 c1 s stk) = [- 1]It is not. A jump at the end of c1 skips instructions of c2 when the two run as one program, but finds
nothing to skip when c1 runs alone. Running c1 @ c2 is no longer “run c1, then c2”. The lemma held for
the old machine because each of its instructions affected only the stack, never which instruction
comes next.
The way out is another generalisation. Instead of splitting a program at an arbitrary point, describe
what the code of e does in front of any code c that follows it: running compile2 e @ c is the same as
putting the value of e on the stack and then running c.
theorem compile2_correct:
"exec2 (compile2 e @ c) s stk = exec2 c s (eval2 e s # stk)"
by (induction e arbitrary: c stk) autoThe induction hypotheses now speak about the code of a subexpression followed by anything, and in every
case that is exactly the shape the simplifier meets. In the case Cond t a b, the hypothesis for t is
used with the code that follows t, which starts with JMPZ. If the test is 0, the jump lands exactly at
the start of the code of b, followed by c, and the hypothesis for b applies; otherwise the hypothesis for
a applies, and JMP lands exactly at the start of c. The jumps never leave the code of the conditional,
except to its end, and the theorem never needs to say what a jump to anywhere else would do.
The form of Section 14.5 is the special case in which nothing follows, c = []:
corollary "exec2 (compile2 e) s stk = eval2 e s # stk"
using compile2_correct [of e "[]"] by simpWhen a lemma about splitting programs fails, generalise over the context instead: state what a piece of
code does in front of any continuation. The proof then only ever looks at the code the compiler
produces, never at arbitrary splits. Exercise 14.6 proves the theorem of Section 14.5 in this form,
without exec_append.
Further directions
- More operators. Multiplication adds a constructor, an instruction and corresponding equations. The correctness proof follows the same pattern with an additional constructor case.
let. One implementation oflet x = a in badds an instruction that stores a value into a variable. That code changes the state as well as the stack, and the theorem must say which variables it may change: storing the value of a in x itself destroys the x that the surrounding expression may need.- Loops. With backward jumps a program can run forever. One model is an inductively defined execution relation on configurations (a program counter, state and stack), as in Modules 11 and 13. Another is a total interpreter with a fuel argument limiting the number of steps. Exhausting fuel reports that the bound was reached; it does not establish divergence.
- Other machines. Exercise 14.7 makes the machine stop on a stack underflow, and Exercise 14.8 compiles to a machine with registers.
Looking back, looking ahead
This module used almost every part of the series. Here is where each piece came from.
| Module | What it contributed | Where it appeared here |
|---|---|---|
| 1 | value, lemma, Quickcheck; compilers in the trusted base |
testing, Section 14.1 |
| 2–3 | Logic; free variables of a lemma are universally quantified | “for every e, s and stk” |
| 4 | Functions | the state, a function from names to values |
| 5 | Recursion and structural induction | eval, exec, compile, every proof |
| 6–7 | Proof rules, rule, schematic variables |
rule Add.IH(2) with ?stk |
| 8 | datatype, fun, lists, export_code |
the language, the machine, the export |
| 9 | Simp rules, simp only, Quickcheck |
exec_append [simp], the tests |
| 10 | Isar: cases and calculations | the proof of compile_correct |
| 11 | arbitrary:, auxiliary lemmas, computation induction |
the stack, exec_append, fold_add.induct |
| 12 | Specification against implementation | eval against compile and exec |
| 13 | State machines | the stack machine itself |
In Module 2 you decided formulas with truth tables, by checking finitely many cases. Truth tables cannot reach a statement about every expression: there are infinitely many. Proof can. Along the way you learned to write such statements in logic, to prove them on paper with rules, and to let Isabelle check every step. The compiler is small, but its proof has the ingredients of the proofs behind CompCert and seL4: a semantics for each language, a precise statement of what correct means, an induction that follows the structure of the programs, and lemmas found by reading the goals where the proof stops.
Where to go from here:
- Concrete Semantics by Tobias Nipkow and Gerwin Klein (Springer, 2014) continues where this module ends. Its first part is an Isabelle tutorial; its second part defines a small imperative language, IMP, with assignments and loops, and treats its semantics, a compiler for it, type systems, Hoare logic and program analysis, all in Isabelle.
- The Archive of Formal Proofs holds hundreds of checked Isabelle developments, among them verified compilers, algorithms and data structures, and shows how experts structure large proofs.
- seL4 and CompCert (Module 1) show these methods at industrial scale; their papers explain what was proved, what is trusted, and what the proofs cost.
- Isabelle for model verification is an applied project with a
small model of a lid, an operation on it, an invariant, and a theorem that a translation
between two representations preserves meaning, the same kind of theorem as
compile_correct. It uses Isabelle2025-2.
Module 1 ended its first theory with three activities: compute with value, prove with lemma, refute
with Quickcheck. In this module you used all three on a real piece of software, a compiler, and the
result is a theorem about every program it will ever compile.
What you learned
- Here compiler correctness is an equation between two semantic functions:
evalfor expressions andexecfor the stack machine. The statement is the equationexec (compile e) s stk = eval e s # stk. - Abstract syntax is a datatype; a compositional semantics is a recursive function on it. A state
maps variable names to values;
f(a := v)updates a function. - A stack machine takes its operands from the top of a stack. The compiler produces code in postfix order: the code of the operands, then the operator.
- Test first.
valuecompares evaluation and execution on examples; Quickcheck searches bounded cases for counterexamples, without guaranteeing a smallest one or finding every bug. Limit the search withquickcheck [size = n]when the claim has functions among its variables. - The proof needs two ideas from Module 11: the auxiliary lemma
exec_append, found from the stuck goal, and generalisation over the stack, witharbitrary: stk, because the code of the second operand runs on a stack that holds the value of the first. - Constant folding is proved correct by structural induction, with a lemma about
fold_addproved by computation induction. Theorems about passes compose, so optimise-then-compile is correct with a one-line proof. export_codeturns the verified functions into Haskell; the code generator and the Haskell compiler remain in the trusted base.- With jumps, the lemma about appended code fails; generalising over the code that follows,
exec2 (compile2 e @ c) s stk = exec2 c s (eval2 e s # stk), repairs the proof.
Exercises
Compile the expression (x + 1) + y by hand, that is, Add (Add (Var ''x'') (Num 1)) (Var ''y''). Then
write down the stack after each instruction when the code runs in the state s0 (x = 5, y = 7) on the
empty stack. How many numbers does the stack hold at most? Compare with the program of Figure 14.2, and
check your answers with value.
Show solution
The code is the code of x + 1, then the code of y, then ADD:
value "compile (Add (Add (Var ''x'') (Num 1)) (Var ''y''))""[LOAD ''x'', PUSH 1, ADD, LOAD ''y'', ADD]"
:: "instr list"The stacks after the five instructions are [5], [1, 5], [6], [7, 6] and [13]:
value "exec (compile (Add (Add (Var ''x'') (Num 1)) (Var ''y''))) s0 []""[13]"
:: "int list"The stack holds at most two numbers. The code for x + (2 + y) in Figure 14.2 needs three, because the
second operand of its outer addition is itself an addition, whose operands go on top of the value of x.
When the nesting is on the left, each partial sum is finished before the next operand is pushed.
Decide for each claim whether it is true for every e, s and stk. Test each with Quickcheck, and prove the
true ones with simp and the theorems of Section 14.5.
exec (compile e) s stk = [eval e s]length (exec (compile e) s stk) = length stk + 1exec (compile e @ compile e) s stk = eval e s # eval e s # stk
Show solution
-
False, as Section 14.4.3 showed: when stk is not empty, its numbers stay below the value.
-
True: the code pushes exactly one number.
lemma "length (exec (compile e) s stk) = length stk + 1" by (simp add: compile_correct) -
True: by
exec_append, the second copy of the code runs on the stack that the first copy left.lemma "exec (compile e @ compile e) s stk = eval e s # eval e s # stk" by (simp add: compile_correct)
Prove that constant folding never makes the code longer:
length (compile (cfold e)) ≤ length (compile e). Start with an auxiliary lemma about fold_add,
proved by computation induction like eval_fold_add, and then prove the theorem by induction on e, in
Isar if you like.
Show solution
The code of fold_add a b is never longer than the code of a, then b, then one ADD:
lemma length_fold_add:
"length (compile (fold_add a b))
≤ length (compile a) + length (compile b) + 1"
by (induction a b rule: fold_add.induct) autoIn the case Add a b the lemma applies to cfold a and cfold b, and the induction hypotheses bound
their code:
theorem cfold_shorter:
"length (compile (cfold e)) ≤ length (compile e)"
proof (induction e)
case (Add a b)
have "length (compile (cfold (Add a b)))
≤ length (compile (cfold a)) + length (compile (cfold b)) + 1"
using length_fold_add by simp
also have "… ≤ length (compile (Add a b))"
using Add.IH by simp
finally show ?case .
qed simp_allqed simp_all proves the cases Num and Var, which the Isar text does not mention, by simp.
The result bounds instruction counts, not compilation time, running time or the memory used by integer values.
A colleague suggests a shorter fold_add that drops every number added on the right:
fun fold_add_bad :: "expr ⇒ expr ⇒ expr" where
"fold_add_bad (Num m) (Num n) = Num (m + n)"
| "fold_add_bad a (Num n) = a"
| "fold_add_bad a b = Add a b"- Use Quickcheck to show that it is wrong, and explain the counterexample.
- Write
fold_add0, a correct version offold_addthat also removes an addition of 0 on the left, and prove that it preserves the value.
Show solution
-
Quickcheck finds an expression that loses a number:
lemma "eval (fold_add_bad a b) s = eval a s + eval b s" quickcheck oopsTesting conjecture with Quickcheck-exhaustive... Quickcheck found a counterexample: a = Var [] b = Num (- 1) s = λx. - 2 Evaluated terms: eval (fold_add_bad a b) s = - 2 eval a s + eval b s = - 3Quickcheck’s a is the variable whose name is the empty string, printed
[]because a string is a list of characters. Its value in s is −2, andfold_add_badloses the −1 added to it. The second equation is right only forNum 0: any other number on the right is lost. -
Test for 0 on either side; the proof is the same as for
fold_add:fun fold_add0 :: "expr ⇒ expr ⇒ expr" where "fold_add0 (Num m) (Num n) = Num (m + n)" | "fold_add0 a b = (if a = Num 0 then b else if b = Num 0 then a else Add a b)" lemma "eval (fold_add0 a b) s = eval a s + eval b s" by (induction a b rule: fold_add0.induct) auto
Define subst :: "vname ⇒ int ⇒ expr ⇒ expr" so that subst x n e replaces every occurrence of the
variable x in e by the number n. Check it with value, then prove
eval (subst x n e) s = eval e (s(x := n))
in words: substituting n for x gives the value that e has in a state where x is n. A compiler uses this fact when it replaces a variable whose value it knows by that value.
Show solution
fun subst :: "vname ⇒ int ⇒ expr ⇒ expr" where
"subst x n (Num m) = Num m"
| "subst x n (Var y) = (if y = x then Num n else Var y)"
| "subst x n (Add a b) = Add (subst x n a) (subst x n b)"
value "subst ''x'' 3 (Add (Var ''x'') (Add (Var ''y'') (Var ''x'')))""Add (Num 3) (Add (Var ''y'') (Num 3))"
:: "expr"lemma eval_subst: "eval (subst x n e) s = eval e (s(x := n))"
by (induction e) autoIn the case Var y, the simplifier splits the if: if y = x, both sides are n; otherwise both are
s y. No variable needs to be arbitrary, because x, n and s stay the same in the recursive calls.
Section 14.8 proved the extended compiler correct in a form with a continuation c, the code that follows. Prove the theorem for the original compiler in the same form,
exec (compile e @ c) s stk = exec c s (eval e s # stk),
by induction on e, without using exec_append. Then derive exec (compile e) s stk = eval e s # stk
from it. Which variables must be arbitrary, and why?
Show solution
Both c and stk must be arbitrary: in the case Add a b, the hypothesis for a is used with the
continuation compile b @ ADD # c, and the hypothesis for b with the stack eval a s # stk.
exec_append is a simp rule, so auto would use it without being asked. Remove it with simp del:
(Module 9), so that the proof really does without it:
lemma compile_cont:
"exec (compile e @ c) s stk = exec c s (eval e s # stk)"
by (induction e arbitrary: c stk) (auto simp del: exec_append)In the case Add a b, the simplifier reassociates (compile a @ compile b @ [ADD]) @ c to
compile a @ (compile b @ ADD # c) and then applies the two hypotheses and the equation of exec for
ADD. The original theorem is the instance c = []:
lemma "exec (compile e) s stk = eval e s # stk"
using compile_cont [of e "[]"] by simpThis form needs no lemma about arbitrary splits of programs, which is why it still works with jumps.
Model a stack machine that reports stack underflow with an option type (Module
8): exec1_opt returns None when ADD finds fewer than two numbers, and exec_opt stops at the first
None. Define both, check with value that [PUSH 1, ADD] fails on the empty stack, and prove that
compiled code never fails:
exec_opt (compile e) s stk = Some (eval e s # stk).
Hint: prove the form with a continuation from Exercise 14.6 first.
Show solution
fun exec1_opt :: "instr ⇒ state ⇒ stack ⇒ stack option" where
"exec1_opt (PUSH n) s stk = Some (n # stk)"
| "exec1_opt (LOAD x) s stk = Some (s x # stk)"
| "exec1_opt ADD s (j # i # stk) = Some ((i + j) # stk)"
| "exec1_opt ADD s stk = None"
fun exec_opt :: "instr list ⇒ state ⇒ stack ⇒ stack option" where
"exec_opt [] s stk = Some stk"
| "exec_opt (i # c) s stk =
(case exec1_opt i s stk of
None ⇒ None
| Some stk' ⇒ exec_opt c s stk')"
value "(exec_opt [PUSH 1, ADD] s0 [], exec_opt (compile sample) s0 [])""(None, Some [14])"
:: "int list option × int list option"With a continuation, the proof is the same as in Exercise 14.6. In the case Add a b the simplifier
meets exec1_opt ADD s (eval b s # eval a s # stk), which is Some …, so the case disappears:
lemma exec_opt_cont:
"exec_opt (compile e @ c) s stk = exec_opt c s (eval e s # stk)"
by (induction e arbitrary: c stk) auto
theorem exec_opt_compile:
"exec_opt (compile e) s stk = Some (eval e s # stk)"
using exec_opt_cont [of e "[]"] by simpThe theorem is stronger than compile_correct: it also says that compiled code never underflows, on
any stack.
Instead of a stack, a register machine has numbered registers, modelled as a function nat ⇒ int, and
instructions that name the registers they use:
datatype rinstr = LDI nat int | LDV nat vname | ADDR nat nat
type_synonym regs = "nat ⇒ int"LDI r n loads the number n into register r, LDV r x loads the value of the variable x, and ADDR r q
adds register q to register r. Define rexec1 and rexec like exec1 and exec, and a compiler
rcompile :: "expr ⇒ nat ⇒ rinstr list" such that rcompile e r leaves the value of e in register r and
uses only registers r, r + 1, … as scratch space. Prove that it is correct: the value ends up in register
r, and the registers below r keep their contents.
Show solution
fun rexec1 :: "rinstr ⇒ state ⇒ regs ⇒ regs" where
"rexec1 (LDI r n) s rs = rs(r := n)"
| "rexec1 (LDV r x) s rs = rs(r := s x)"
| "rexec1 (ADDR r q) s rs = rs(r := rs r + rs q)"
fun rexec :: "rinstr list ⇒ state ⇒ regs ⇒ regs" where
"rexec [] s rs = rs"
| "rexec (i # c) s rs = rexec c s (rexec1 i s rs)"
lemma rexec_append [simp]:
"rexec (c1 @ c2) s rs = rexec c2 s (rexec c1 s rs)"
by (induction c1 arbitrary: rs) auto
fun rcompile :: "expr ⇒ nat ⇒ rinstr list" where
"rcompile (Num n) r = [LDI r n]"
| "rcompile (Var x) r = [LDV r x]"
| "rcompile (Add a b) r =
rcompile a r @ rcompile b (r + 1) @ [ADDR r (r + 1)]"
value "rcompile sample 0""[LDV 0 ''x'', LDI 1 2, LDV 2 ''y'', ADDR 1 2, ADDR 0 1]"
:: "rinstr list"The register number plays the part of the stack height: the code for b works one register higher, so
that it cannot overwrite the value of a in register r. The claim must say both things at once, because
the case Add needs both from its hypotheses: the value of a is still in register r after the code of b
has run only because that code leaves the registers below r + 1 alone. Both r and the register contents
change between the recursive calls, so both are arbitrary:
theorem rcompile_correct:
"rexec (rcompile e r) s rs r = eval e s ∧
(∀q < r. rexec (rcompile e r) s rs q = rs q)"
by (induction e arbitrary: r rs) autoProve only the first half, and the induction gets stuck:
lemma "rexec (rcompile e r) s rs r = eval e s"
apply (induction e arbitrary: r rs)
apply autoproof (prove)
goal (1 subgoal):
1. ⋀e1 e2 r rs.
⟦⋀r rs. rexec (rcompile e1 r) s rs r = eval e1 s;
⋀r rs. rexec (rcompile e2 r) s rs r = eval e2 s⟧
⟹ rexec (rcompile e2 (Suc r)) s (rexec (rcompile e1 r) s rs) r =
eval e1 sThe goal that remains asks whether the code of e2, compiled for register r + 1 (written Suc r), leaves
register r alone. The hypotheses of the weaker claim say nothing about other registers, so the claim must
be strengthened, as in Module 11.
oopsThe proof of McCarthy and Painter in 1967 (Section 14.1) was about a machine of this kind: an accumulator and registers, not a stack.
Check yourself
compile_correct say?exec (compile e) s stk = eval e s # stk for all e, s and stk. The empty-stack version is a special case.compile_correct need arbitrary: stk?arbitrary, the hypothesis is about the fixed stk only.compile (Add (Num 1) (Var ''x''))?quickcheck [size = 3] finds no counterexample to a claim. What do you know?exec2 (c1 @ c2) s stk = exec2 c2 s (exec2 c1 s stk) is false. Why?compile?