This problem set practises Lecture 9. Part A proves two
facts about the expression language of the lecture: its big-step semantics gives each
expression one value (Problem 1), and a small optimiser keeps every value
(Problem 2). Part B adds multiplication to the language, extends the compiler
(Problem 3) and re-proves compiler correctness (Problem 4,
Problem 5). Part C verifies a loop with a Hoare triple and an invariant you
choose (Problem 6).
How to work on it. Open the lean/ folder (not the course root) in VS Code with the Lean 4
extension, then open LeanFV/Homework/HW04.lean. Each problem below names its declaration in
that file, such as HW04.bigStep_det. Replace each sorry, watching the goal in the infoview.
The file is finished when it shows no sorry warnings. It starts with these lines:
The import loads Lecture 9’s definitions from the course library, with the same names as on
the lecture page: AExp, aeval, BigStep, Env, Stack, Com, Exec, Env.set,
Hoare, and the rules hoare_assign, hoare_seq, hoare_conseq and hoare_while. The
open line lets you write AExp instead of LeanFV.Programs.AExp. There is no Mathlib, but
induction, cases, simp, omega and decide are all you need.
Each solution below is collapsed. Open it after you have a proof of your own, or after you
have been stuck for a while. Many proofs are correct, and yours need not match.
Part A: the semantics of Lecture 9
Problem 1 (Big-step evaluation is deterministic).
Prove that an expression evaluates to at most one value: BigStep e s m and
BigStep e s n give m = n (HW04.bigStep_det).
Hint
Induct on the first derivation, induction hm generalizing n, and take the second one apart
with cases hn in each case. Why must n be generalised?
Solution
theorem bigStep_det {e : AExp} {s : Env} {m n : Nat} (hm : BigStep e s m) (hn : BigStep e s n) : m = n := by induction hm generalizing n with | num => cases hn; rfl | var => cases hn; rfl | plus _ _ iha ihb => cases hn with | plus ha hb => rw [iha ha, ihb hb]
In the plus case the second derivation has its own sub-derivations ha and hb, which
end in values of their own. The hypothesis iha must accept that unknown value, so n
has to be generalised; without generalizing n, iha speaks only about the original n.
A constant folder is a small optimiser: wherever both sides of a plus are numbers, it
replaces the node by their sum. The file defines it as follows (you do not change it):
def cfold : AExp → AExp | .num n => .num n | .var x => .var x | .plus a b => match cfold a, cfold b with | .num m, .num n => .num (m + n) | a', b' => .plus a' b'
Problem 2 (Constant folding is correct).
Prove that folding never changes the value: aeval (cfold e) s = aeval e s
(HW04.cfold_correct).
Hint
Induct on e. In the plus case, rw [cfold] exposes the match, and split gives one
goal per branch. In the first branch, rename_i names the hypotheses that say what
cfold a and cfold b are; rewrite with them inside iha and ihb.
Solution
theorem cfold_correct (e : AExp) (s : Env) : aeval (cfold e) s = aeval e s := by induction e with | num n => rfl | var x => rfl | plus a b iha ihb => rw [cfold] split · rename_i m n ha hb rw [ha] at iha rw [hb] at ihb simp only [aeval] at iha ihb ⊢ omega · simp only [aeval, iha, ihb]
In the first branch, iha becomes m = aeval a s and ihb becomes n = aeval b s, and
omega finishes. The second branch is the plus node rebuilt from the folded parts.
Part B: multiplication
The file extends the language with a times node, and the machine with a mul instruction
that multiplies the top two numbers on the stack. Running code, and the lemma about running
two programs one after the other, are as in the lecture:
inductive MExp where | num : Nat → MExp | var : String → MExp | plus : MExp → MExp → MExp | times : MExp → MExp → MExp deriving Reprdef meval : MExp → Env → Nat | .num n, _ => n | .var x, s => s x | .plus a b, s => meval a s + meval b s | .times a b, s => meval a s * meval b sinductive MInstr where | loadi : Nat → MInstr | load : String → MInstr | add : MInstr | mul : MInstr deriving Reprdef mexec1 : MInstr → Env → Stack → Stack | .loadi n, _, stk => n :: stk | .load x, s, stk => s x :: stk | .add, _, m :: n :: stk => (n + m) :: stk | .mul, _, m :: n :: stk => (n * m) :: stk | _, _, stk => stkdef mexec : List MInstr → Env → Stack → Stack | [], _, stk => stk | i :: is, s, stk => mexec is s (mexec1 i s stk)theorem mexec_append (p q : List MInstr) (s : Env) (stk : Stack) : mexec (p ++ q) s stk = mexec q s (mexec p s stk) := by induction p generalizing stk with | nil => rfl | cons i is ih => exact ih (mexec1 i s stk)
Problem 3 (Extend the compiler).
Complete the times case of HW04.mcompile, the compiler from MExp to MInstr code.
Then test it with #eval on an expression such as (2 + x) * 3.
Solution
def mcompile : MExp → List MInstr | .num n => [.loadi n] | .var x => [.load x] | .plus a b => mcompile a ++ mcompile b ++ [.add] | .times a b => mcompile a ++ mcompile b ++ [.mul]#eval mcompile (.times (.plus (.num 2) (.var "x")) (.num 3))#eval mexec (mcompile (.times (.plus (.num 2) (.var "x")) (.num 3))) (fun _ => 5) []
The first #eval prints
[MInstr.loadi 2, MInstr.load "x", MInstr.add, MInstr.loadi 3, MInstr.mul], and the
second prints [21], which is .
Problem 4 (The generalised correctness theorem).
Prove HW04.mcompile_correct_gen: running the code for e followed by any code rest, on
any stack stk, is the same as running rest with the value of e pushed on stk.
mexec (mcompile e ++ rest) s stk = mexec rest s (meval e s :: stk)
Hint
Copy the structure of compile_correct_gen from the lecture, with
induction e generalizing rest stk. The new case should look exactly like the plus case.
Solution
theorem mcompile_correct_gen (e : MExp) (rest : List MInstr) (s : Env) (stk : Stack) : mexec (mcompile e ++ rest) s stk = mexec rest s (meval e s :: stk) := by induction e generalizing rest stk with | num n => rfl | var x => rfl | plus a b iha ihb => rw [mcompile, List.append_assoc, List.append_assoc, iha, ihb] rfl | times a b iha ihb => rw [mcompile, List.append_assoc, List.append_assoc, iha, ihb] rfl
Only one case is new, and it is a copy of plus. The rewrites never look at what add or
mul computes, only at the order in which the code pushes and pops. The final rfl does:
it runs one mul step on a stack whose top two entries are the two values and checks that
the result is meval a s * meval b s. (If mul multiplied the entries in the other order,
rfl would fail: b * a = a * b is a theorem, not a computation.)
Problem 5 (Compiler correctness).
Deduce HW04.mcompile_correct: mexec (mcompile e) s [] = [meval e s].
Solution
theorem mcompile_correct (e : MExp) (s : Env) : mexec (mcompile e) s [] = [meval e s] := by have h := mcompile_correct_gen e [] s [] rw [List.append_nil] at h exact h
Part C: a loop invariant
The last problem uses the commands and Hoare triples of the lecture. This program moves the
value of x into y, one unit at a time:
def countdown : Com := .whileDo (fun s => s "x" != 0) (.seq (.assign "y" (fun s => s "y" + 1)) (.assign "x" (fun s => s "x" - 1)))
Problem 6 (Verify the loop).
Prove HW04.countdown_correct: started with x = n and y = 0, the program can only stop
with y = n.
Hoare (fun s => s "x" = n ∧ s "y" = 0) countdown (fun s => s "y" = n)
Hint
First find the invariant: a fact about x and y that holds before the loop, survives one
pass of the body, and together with x = 0 gives y = n. Then build the proof from the
outside in: hoare_conseq around hoare_while, and for the body hoare_conseq around
hoare_seq of two hoare_assign. Here apply hoare_conseq _ (hoare_while body) fails with
“could not unify the conclusion of hoare_conseq …”: Hoare unfolds to a ∀ whose last
part, the postcondition at the final state, is still unknown, and apply cannot tell how
many arguments to add. Write refine hoare_conseq ?_ (hoare_while body) ?_ instead. (A bare
apply hoare_conseq works, but it leaves the intermediate pre- and postcondition as extra
goals ?P and ?Q.) simp [Env.set] evaluates the updated environments.
Solution
theorem countdown_correct (n : Nat) : Hoare (fun s => s "x" = n ∧ s "y" = 0) countdown (fun s => s "y" = n) := by have body : Hoare (fun s => s "x" + s "y" = n ∧ (s "x" != 0) = true) (.seq (.assign "y" (fun s => s "y" + 1)) (.assign "x" (fun s => s "x" - 1))) (fun s => s "x" + s "y" = n) := by refine hoare_conseq ?_ (hoare_seq (hoare_assign _ _ _) (hoare_assign _ _ _)) (fun _ h => h) intro s ⟨hinv, hb⟩ simp [Env.set] at hb ⊢ omega refine hoare_conseq ?_ (hoare_while body) ?_ · intro s ⟨hx, hy⟩ omega · intro s ⟨hinv, hb⟩ simp at hb omega
The invariant is x + y = n. It holds at the start (). The body adds one to y
and, because x ≠ 0, really takes one from x, so the sum stays n; that is the body
step, where omega needs the test x ≠ 0 to rule out the truncated 0 - 1 = 0. When the
loop stops, x = 0, so y = n. The triple is partial correctness: it says nothing about
whether the loop stops (this one does, since x goes down).