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:

import LeanFV.Programs.StackMachine
 
open LeanFV.Programs

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).

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).

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 Repr
 
def 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 s
 
inductive MInstr where
  | loadi : Nat → MInstr
  | load : String → MInstr
  | add : MInstr
  | mul : MInstr
  deriving Repr
 
def 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 => stk
 
def 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.

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)
Problem 5 (Compiler correctness).

Deduce HW04.mcompile_correct: mexec (mcompile e) s [] = [meval e s].

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)