3.2. The IMP Language

In the previous chapter we studied operational semantics using a language of arithmetic expressions. That language, however, had none of the features that characterize an imperative program: there were no variables, no assignment and no loops. In this chapter we present IMP, a minimal but Turing-complete imperative language, which is the classic example used in textbooks on programming language semantics.

IMP consists of three syntactic categories:

  • arithmetic expressions, which denote natural numbers and can read the values of variables;

  • boolean expressions, used as conditions;

  • commands, which change the state of memory: assignment, sequencing, conditionals and loops.

We will give IMP two operational semantics: a big-step one, written \langle c, s\rangle \Downarrow s', and a small-step one, written \langle c, s\rangle \to_1 \langle c', s'\rangle, and we will prove that they are equivalent:

\langle c, s\rangle \Downarrow s' \iff \langle c, s\rangle \to^* \langle \textsf{skip}, s'\rangle

This chapter is the foundation for the next two, in which we will study Hoare logic and the generation of verification conditions for IMP programs.

3.2.1. States

Unlike the expressions of the previous chapter, the value of an IMP expression depends on the contents of memory. We model memory as a state: a total function that maps each variable name to a natural number. Variables that were never initialized simply have some arbitrary value (in the state State.empty, the value zero).

namespace Ch18 abbrev Var := String abbrev State := Var → ℕ def State.update (s : State) (x : Var) (n : ℕ) : State := fun y => if y = x then n else s y scoped notation:80 s "[" x " ↦ " n "]" => State.update s x n def State.empty : State := fun _ => 0

The operation State.update produces a new state, identical to s except at the variable x, which now holds n. In mathematical notation, we write s[x \mapsto n]:

s[x \mapsto n](y) = \begin{cases} n & \text{if } y = x \\ s(y) & \text{otherwise} \end{cases}

As with the expression language, all the notations in this chapter are declared with scoped; the following chapters make them available via open Ch18.

3.2.2. Syntax

The syntax of IMP, in BNF notation, is the following:

a ::= n | x | a + a | a - a | a * a
b ::= true | false | a = a | a ≤ a | ¬ b | b ∧ b
c ::= skip | x := a | c ; c | if b then c else c | while b do c

Each syntactic category becomes an inductive type. Subtraction is truncated, like subtraction on ℕ in Lean: 3 - 5 = 0.

inductive AExp where | num : ℕ → AExp | var : Var → AExp | add : AExp → AExp → AExp | sub : AExp → AExp → AExp | mul : AExp → AExp → AExp deriving Repr inductive BExp where | tt : BExp | ff : BExp | eq : AExp → AExp → BExp | le : AExp → AExp → BExp | not : BExp → BExp | and : BExp → BExp → BExp deriving Repr inductive Com where | skip : Com | assign : Var → AExp → Com | seq : Com → Com → Com | ite : BExp → Com → Com → Com | whl : BExp → Com → Com deriving Repr scoped infixl:70 " ;; " => Com.seq scoped notation:65 x " ::= " a => Com.assign x a

The notations c₁ ;; c₂ and x ::= a make programs a little more readable. For example, the program that swaps the values of x and y using an auxiliary variable is written as

def swap : Com := ("tmp" ::= .var "x") ;; ("x" ::= .var "y") ;; ("y" ::= .var "tmp")

3.2.3. Semantics of expressions

Expressions in IMP have no side effects and always terminate. For this reason, their meaning can be given denotationally, by recursive functions that take the current state as a parameter. A variable is evaluated by looking it up in the state:

def aeval (s : State) : AExp → ℕ | .num n => n | .var x => s x | .add a₁ a₂ => aeval s a₁ + aeval s a₂ | .sub a₁ a₂ => aeval s a₁ - aeval s a₂ | .mul a₁ a₂ => aeval s a₁ * aeval s a₂ def beval (s : State) : BExp → Bool | .tt => true | .ff => false | .eq a₁ a₂ => (aeval s a₁ == aeval s a₂) | .le a₁ a₂ => decide (aeval s a₁ ≤ aeval s a₂) | .not b => !beval s b | .and b₁ b₂ => beval s b₁ && beval s b₂ 11#eval aeval State.empty (.add (.num 3) (.mul (.num 2) (.num 4)))
11
true#eval beval (State.empty["x" ↦ 3]) (.le (.var "x") (.num 5))
true

For commands, the situation is different: a while loop may fail to terminate, and therefore we cannot directly define a total function Com → State → State in Lean. This is where operational semantics, given by inductive relations, show their strength.

3.2.4. Big-step semantics

The judgment \langle c, s\rangle \Downarrow s' means that the command c, executed from the state s, terminates in the state s'. The rules are:

\frac{}{\langle \textsf{skip}, s\rangle \Downarrow s}\,(\textsf{Skip}) \qquad \frac{}{\langle x := a, s\rangle \Downarrow s[x \mapsto [\![a]\!]_s]}\,(\textsf{Assign})

\frac{\langle c_1, s\rangle \Downarrow s' \quad \langle c_2, s'\rangle \Downarrow s''}{\langle c_1 ; c_2, s\rangle \Downarrow s''}\,(\textsf{Seq})

\frac{[\![b]\!]_s = \textsf{true} \quad \langle c_1, s\rangle \Downarrow s'}{\langle \textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, s\rangle \Downarrow s'}\,(\textsf{IfT}) \qquad \frac{[\![b]\!]_s = \textsf{false} \quad \langle c_2, s\rangle \Downarrow s'}{\langle \textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, s\rangle \Downarrow s'}\,(\textsf{IfF})

\frac{[\![b]\!]_s = \textsf{false}}{\langle \textsf{while}\ b\ \textsf{do}\ c, s\rangle \Downarrow s}\,(\textsf{WhileF})

\frac{[\![b]\!]_s = \textsf{true} \quad \langle c, s\rangle \Downarrow s' \quad \langle \textsf{while}\ b\ \textsf{do}\ c, s'\rangle \Downarrow s''}{\langle \textsf{while}\ b\ \textsf{do}\ c, s\rangle \Downarrow s''}\,(\textsf{WhileT})

The WhileT rule deserves attention: its third premise mentions the loop while b do c itself. The definition is therefore not syntax-directed, but this is not a problem for an inductive predicate, which only requires the premises to be (finite) derivations themselves. An important consequence is that, if a loop does not terminate, there is simply no derivation of \langle c, s\rangle \Downarrow s' for any s': the big-step semantics does not distinguish diverging programs from programs that are "stuck".

inductive BigStep : Com → State → State → Prop where | skip (s : State) : BigStep .skip s s | assign {x : Var} {a : AExp} (s : State) : BigStep (.assign x a) s (s[x ↦ aeval s a]) | seq {c₁ c₂ : Com} {s s' s'' : State} : BigStep c₁ s s' → BigStep c₂ s' s'' → BigStep (c₁ ;; c₂) s s'' | iteTrue {b : BExp} {c₁ c₂ : Com} {s s' : State} : beval s b = true → BigStep c₁ s s' → BigStep (.ite b c₁ c₂) s s' | iteFalse {b : BExp} {c₁ c₂ : Com} {s s' : State} : beval s b = false → BigStep c₂ s s' → BigStep (.ite b c₁ c₂) s s' | whileFalse {b : BExp} {c : Com} (s : State) : beval s b = false → BigStep (.whl b c) s s | whileTrue {b : BExp} {c : Com} {s s' s'' : State} : beval s b = true → BigStep c s s' → BigStep (.whl b c) s' s'' → BigStep (.whl b c) s s'' scoped notation:50 "⟪" c ", " s "⟫" " ⟹ " s':51 => BigStep c s s'

Some examples. The command skip preserves the state; and the sequence x := 5 ;; y := x + 1, executed from the empty state, terminates with x = 5 and y = 6. Note how the derivation, written as a term, mirrors the proof tree: a Seq node with two Assign leaves.

example (s : State) : ⟪.skip, s⟫ ⟹ s := .skip s example : ⟪("x" ::= .num 5) ;; ("y" ::= .add (.var "x") (.num 1)), State.empty⟫ ⟹ State.empty["x" ↦ 5]["y" ↦ 6] := .seq (.assign State.empty) (.assign _)

In the second example, Lean must check that the state produced by the rules, State.empty["x" ↦ 5]["y" ↦ aeval _ (x + 1)], is definitionally equal to the state in the statement. Since aeval and State.update are computable functions, this check is done by evaluation.

3.2.4.1. Determinism

As with expressions, the execution of IMP commands is deterministic: a command, started from a given state, terminates in at most one final state.

\langle c, s\rangle \Downarrow t_1 \;\wedge\; \langle c, s\rangle \Downarrow t_2 \implies t_1 = t_2

The proof strategy is the same as in the previous chapter: induction on the first derivation, generalizing the second final state, followed by case analysis on the second derivation. The cases in which the two derivations use different rules for the same command (for example, IfT and IfF) are contradictory, since they would require the guard to be both true and false; simp finds this contradiction. In the Seq and WhileT cases, we use the first induction hypothesis to identify the intermediate states and the second one to conclude.

theorem bigStep_det {c : Com} {s t1 t2 : State} (h1 : ⟪c, s⟫ ⟹ t1) (h2 : ⟪c, s⟫ ⟹ t2) : t1 = t2 := c:Coms:Statet1:Statet2:Stateh1:⟪c, s⟫ ⟹ t1h2:⟪c, s⟫ ⟹ t2⊢ t1 = t2 induction h1 generalizing t2 with c:Coms:Statet1:States_1:Statet2:Stateh2:⟪Com.skip, s_1⟫ ⟹ t2⊢ s_1 = t2 c:Coms:Statet1:States_1:State⊢ s_1 = s_1; All goals completed! 🐙 c:Coms:Statet1:Statex:Vara:AExps_1:Statet2:Stateh2:⟪x ::= a, s_1⟫ ⟹ t2⊢ s_1[x ↦ aeval s_1 a] = t2 c:Coms:Statet1:Statex:Vara:AExps_1:State⊢ s_1[x ↦ aeval s_1 a] = s_1[x ↦ aeval s_1 a]; All goals completed! 🐙 c:Coms:Statet1:Statec₁:Comc₂:Coms_1:States':States'':Statea:⟪c₁, s_1⟫ ⟹ s'a_1:⟪c₂, s'⟫ ⟹ s''IH1:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2IH2:∀ {t2 : State}, ⟪c₂, s'⟫ ⟹ t2 → s'' = t2t2:Stateh2:⟪c₁ ;; c₂, s_1⟫ ⟹ t2⊢ s'' = t2 cases h2 with c:Coms:Statet1:Statec₁:Comc₂:Coms_1:States':States'':Statea:⟪c₁, s_1⟫ ⟹ s'a_1:⟪c₂, s'⟫ ⟹ s''IH1:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2IH2:∀ {t2 : State}, ⟪c₂, s'⟫ ⟹ t2 → s'' = t2t2:States'_1:Stateha:⟪c₁, s_1⟫ ⟹ s'_1hb:⟪c₂, s'_1⟫ ⟹ t2⊢ s'' = t2 c:Coms:Statet1:Statec₁:Comc₂:Coms_1:States':States'':Statea:⟪c₁, s_1⟫ ⟹ s'a_1:⟪c₂, s'⟫ ⟹ s''IH1:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2t2:States'_1:StateIH2:∀ {t2 : State}, ⟪c₂, s'_1⟫ ⟹ t2 → s'' = t2ha:⟪c₁, s_1⟫ ⟹ s'_1hb:⟪c₂, s'_1⟫ ⟹ t2⊢ s'' = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbT:beval s_1 b = truea:⟪c₁, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2t2:Stateh2:⟪Com.ite b c₁ c₂, s_1⟫ ⟹ t2⊢ s' = t2 cases h2 with c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbT:beval s_1 b = truea:⟪c₁, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2t2:Statea_1:beval s_1 b = truehc:⟪c₁, s_1⟫ ⟹ t2⊢ s' = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbT:beval s_1 b = truea:⟪c₁, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₁, s_1⟫ ⟹ t2 → s' = t2t2:StatehbF:beval s_1 b = falsea_1:⟪c₂, s_1⟫ ⟹ t2⊢ s' = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbF:beval s_1 b = falsea:⟪c₂, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₂, s_1⟫ ⟹ t2 → s' = t2t2:Stateh2:⟪Com.ite b c₁ c₂, s_1⟫ ⟹ t2⊢ s' = t2 cases h2 with c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbF:beval s_1 b = falsea:⟪c₂, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₂, s_1⟫ ⟹ t2 → s' = t2t2:StatehbT:beval s_1 b = truea_1:⟪c₁, s_1⟫ ⟹ t2⊢ s' = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbF:beval s_1 b = falsea:⟪c₂, s_1⟫ ⟹ s'IH:∀ {t2 : State}, ⟪c₂, s_1⟫ ⟹ t2 → s' = t2t2:Statea_1:beval s_1 b = falsehc:⟪c₂, s_1⟫ ⟹ t2⊢ s' = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc_1:Coms_1:StatehbF:beval s_1 b = falset2:Stateh2:⟪Com.whl b c_1, s_1⟫ ⟹ t2⊢ s_1 = t2 cases h2 with c:Coms:Statet1:Stateb:BExpc_1:Coms_1:StatehbF:beval s_1 b = falsea:beval s_1 b = false⊢ s_1 = s_1 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc_1:Coms_1:StatehbF:beval s_1 b = falset2:States':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ t2⊢ s_1 = t2 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ s''IHc:∀ {t2 : State}, ⟪c_1, s_1⟫ ⟹ t2 → s' = t2IHw:∀ {t2 : State}, ⟪Com.whl b c_1, s'⟫ ⟹ t2 → s'' = t2t2:Stateh2:⟪Com.whl b c_1, s_1⟫ ⟹ t2⊢ s'' = t2 cases h2 with c:Coms:Statet1:Stateb:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ s''IHc:∀ {t2 : State}, ⟪c_1, s_1⟫ ⟹ t2 → s' = t2IHw:∀ {t2 : State}, ⟪Com.whl b c_1, s'⟫ ⟹ t2 → s'' = t2hbF:beval s_1 b = false⊢ s'' = s_1 All goals completed! 🐙 c:Coms:Statet1:Stateb:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ s''IHc:∀ {t2 : State}, ⟪c_1, s_1⟫ ⟹ t2 → s' = t2IHw:∀ {t2 : State}, ⟪Com.whl b c_1, s'⟫ ⟹ t2 → s'' = t2t2:States'_1:Statea_2:beval s_1 b = truehc:⟪c_1, s_1⟫ ⟹ s'_1hw:⟪Com.whl b c_1, s'_1⟫ ⟹ t2⊢ s'' = t2 c:Coms:Statet1:Stateb:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ s''IHc:∀ {t2 : State}, ⟪c_1, s_1⟫ ⟹ t2 → s' = t2t2:States'_1:StateIHw:∀ {t2 : State}, ⟪Com.whl b c_1, s'_1⟫ ⟹ t2 → s'' = t2a_2:beval s_1 b = truehc:⟪c_1, s_1⟫ ⟹ s'_1hw:⟪Com.whl b c_1, s'_1⟫ ⟹ t2⊢ s'' = t2 All goals completed! 🐙

3.2.5. Small-step semantics

The small-step semantics of IMP describes execution as a sequence of configurations \langle c, s\rangle, where c is the command that still remains to be executed and s is the current state. The command skip plays the role of a value: the configuration \langle \textsf{skip}, s\rangle represents a finished execution with final state s.

\frac{}{\langle x := a, s\rangle \to_1 \langle \textsf{skip}, s[x \mapsto [\![a]\!]_s]\rangle}\,(\textsf{Assign}) \qquad \frac{}{\langle \textsf{skip} ; c_2, s\rangle \to_1 \langle c_2, s\rangle}\,(\textsf{SeqSkip})

\frac{\langle c_1, s\rangle \to_1 \langle c_1', s'\rangle}{\langle c_1 ; c_2, s\rangle \to_1 \langle c_1' ; c_2, s'\rangle}\,(\textsf{Seq})

\frac{[\![b]\!]_s = \textsf{true}}{\langle \textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, s\rangle \to_1 \langle c_1, s\rangle}\,(\textsf{IfT}) \qquad \frac{[\![b]\!]_s = \textsf{false}}{\langle \textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, s\rangle \to_1 \langle c_2, s\rangle}\,(\textsf{IfF})

\frac{}{\langle \textsf{while}\ b\ \textsf{do}\ c, s\rangle \to_1 \langle \textsf{if}\ b\ \textsf{then}\ (c ; \textsf{while}\ b\ \textsf{do}\ c)\ \textsf{else}\ \textsf{skip}, s\rangle}\,(\textsf{Unfold})

The Unfold rule is the most interesting one: instead of two rules for the loop, we have a single rule that unrolls one iteration, turning the loop into a conditional. The decision to continue or to stop is left to the rules for if. Note also that skip takes no step at all: it is a final configuration.

inductive SmallStep : Com → State → Com → State → Prop where | assign {x : Var} {a : AExp} (s : State) : SmallStep (.assign x a) s .skip (s[x ↦ aeval s a]) | seqSkip {c₂ : Com} (s : State) : SmallStep (.skip ;; c₂) s c₂ s | seq {c₁ c₁' c₂ : Com} {s s' : State} : SmallStep c₁ s c₁' s' → SmallStep (c₁ ;; c₂) s (c₁' ;; c₂) s' | iteTrue {b : BExp} {c₁ c₂ : Com} (s : State) : beval s b = true → SmallStep (.ite b c₁ c₂) s c₁ s | iteFalse {b : BExp} {c₁ c₂ : Com} (s : State) : beval s b = false → SmallStep (.ite b c₁ c₂) s c₂ s | whileUnfold {b : BExp} {c : Com} (s : State) : SmallStep (.whl b c) s (.ite b (c ;; .whl b c) .skip) s scoped notation:50 "⟪" c ", " s "⟫" " →₁ " "⟪" c' ", " s' "⟫" => SmallStep c s c' s' example (s : State) : ⟪"x" ::= .num 3, s⟫ →₁ ⟪.skip, s["x" ↦ 3]⟫ := .assign s example (s : State) (c₂ : Com) : ⟪.skip ;; c₂, s⟫ →₁ ⟪c₂, s⟫ := .seqSkip s

3.2.5.1. Reflexive-transitive closure

As in the previous chapter, a complete execution is a finite sequence of steps, described by the reflexive-transitive closure of \to_1:

\frac{}{\langle c, s\rangle \to^* \langle c, s\rangle}\,(\textsf{Refl}) \qquad \frac{\langle c, s\rangle \to_1 \langle c', s'\rangle \quad \langle c', s'\rangle \to^* \langle c'', s''\rangle}{\langle c, s\rangle \to^* \langle c'', s''\rangle}\,(\textsf{Step})

inductive MultiStep : Com → State → Com → State → Prop where | refl (c : Com) (s : State) : MultiStep c s c s | step {c c' c'' : Com} {s s' s'' : State} : SmallStep c s c' s' → MultiStep c' s' c'' s'' → MultiStep c s c'' s'' scoped notation:50 "⟪" c ", " s "⟫" " →* " "⟪" c' ", " s' "⟫" => MultiStep c s c' s'

Once again we need two structural lemmas: the transitivity of \to^* and left congruence for sequencing, which extends the Seq rule to sequences of steps. Both are proved by induction on the sequence of steps.

lemma MultiStep.trans {c c' c'' : Com} {s s' s'' : State} (h1 : ⟪c, s⟫ →* ⟪c', s'⟫) (h2 : ⟪c', s'⟫ →* ⟪c'', s''⟫) : ⟪c, s⟫ →* ⟪c'', s''⟫ := c:Comc':Comc'':Coms:States':States'':Stateh1:⟪c, s⟫ →* ⟪c', s'⟫h2:⟪c', s'⟫ →* ⟪c'', s''⟫⊢ ⟪c, s⟫ →* ⟪c'', s''⟫ induction h1 with c:Comc':Comc'':Coms:States':States'':Statec_1:Coms_1:Stateh2:⟪c_1, s_1⟫ →* ⟪c'', s''⟫⊢ ⟪c_1, s_1⟫ →* ⟪c'', s''⟫ All goals completed! 🐙 c:Comc':Comc'':Coms:States':States'':Statec_1:Comc'_1:Comc''_1:Coms_1:States'_1:States''_1:Statehs:⟪c_1, s_1⟫ →₁ ⟪c'_1, s'_1⟫a:⟪c'_1, s'_1⟫ →* ⟪c''_1, s''_1⟫IH:⟪c''_1, s''_1⟫ →* ⟪c'', s''⟫ → ⟪c'_1, s'_1⟫ →* ⟪c'', s''⟫h2:⟪c''_1, s''_1⟫ →* ⟪c'', s''⟫⊢ ⟪c_1, s_1⟫ →* ⟪c'', s''⟫ All goals completed! 🐙 lemma multiStep_seqL {c₁ c₁' c₂ : Com} {s s' : State} (h : ⟪c₁, s⟫ →* ⟪c₁', s'⟫) : ⟪c₁ ;; c₂, s⟫ →* ⟪c₁' ;; c₂, s'⟫ := c₁:Comc₁':Comc₂:Coms:States':Stateh:⟪c₁, s⟫ →* ⟪c₁', s'⟫⊢ ⟪c₁ ;; c₂, s⟫ →* ⟪c₁' ;; c₂, s'⟫ induction h with c₁:Comc₁':Comc₂:Coms:States':Statec:Coms_1:State⊢ ⟪c ;; c₂, s_1⟫ →* ⟪c ;; c₂, s_1⟫ All goals completed! 🐙 c₁:Comc₁':Comc₂:Coms:States':Statec:Comc':Comc'':Coms_1:States'_1:States'':Statehs:⟪c, s_1⟫ →₁ ⟪c', s'_1⟫a:⟪c', s'_1⟫ →* ⟪c'', s''⟫IH:⟪c' ;; c₂, s'_1⟫ →* ⟪c'' ;; c₂, s''⟫⊢ ⟪c ;; c₂, s_1⟫ →* ⟪c'' ;; c₂, s''⟫ All goals completed! 🐙

Here is the step-by-step execution of the program x := 3 ;; y := 2. There are three steps: the first assignment (inside a sequence, by the Seq rule), the elimination of the resulting skip (SeqSkip) and the second assignment.

example (s : State) : ⟪("x" ::= .num 3) ;; ("y" ::= .num 2), s⟫ →* ⟪.skip, s["x" ↦ 3]["y" ↦ 2]⟫ := .step (.seq (.assign s)) (.step (.seqSkip _) (.step (.assign _) (.refl _ _)))

3.2.6. Equivalence between the semantics

We now prove the central result of the chapter: a command terminates in t according to the big-step semantics if and only if it reduces in zero or more steps to the final configuration \langle \textsf{skip}, t\rangle.

3.2.6.1. From big-step to small-step

The proof is by induction on the big-step derivation. In each case, we explicitly build the corresponding sequence of steps:

  • Skip: zero steps.

  • Assign: one step.

  • Seq: by the induction hypotheses, \langle c_1, s\rangle \to^* \langle \textsf{skip}, s'\rangle and \langle c_2, s'\rangle \to^* \langle \textsf{skip}, s''\rangle. Then \langle c_1;c_2, s\rangle \to^* \langle \textsf{skip};c_2, s'\rangle \to_1 \langle c_2, s'\rangle \to^* \langle \textsf{skip}, s''\rangle.

  • IfT and IfF: one step choosing the branch, followed by the induction hypothesis.

  • WhileF: an Unfold step followed by an IfF step, reaching skip.

  • WhileT: Unfold, IfT, then the execution of the body inside the sequence (the induction hypothesis for the body and multiStep_seqL), SeqSkip, and finally the induction hypothesis for the loop.

theorem bigStep_to_multiStep {c : Com} {s t : State} (h : ⟪c, s⟫ ⟹ t) : ⟪c, s⟫ →* ⟪.skip, t⟫ := c:Coms:Statet:Stateh:⟪c, s⟫ ⟹ t⊢ ⟪c, s⟫ →* ⟪Com.skip, t⟫ induction h with c:Coms✝:Statet:States:State⊢ ⟪Com.skip, s⟫ →* ⟪Com.skip, s⟫ All goals completed! 🐙 c:Coms✝:Statet:Statex:Vara:AExps:State⊢ ⟪x ::= a, s⟫ →* ⟪Com.skip, s[x ↦ aeval s a]⟫ All goals completed! 🐙 c:Coms:Statet:Statec₁:Comc₂:Coms_1:States':States'':Statea:⟪c₁, s_1⟫ ⟹ s'a_1:⟪c₂, s'⟫ ⟹ s''IH1:⟪c₁, s_1⟫ →* ⟪Com.skip, s'⟫IH2:⟪c₂, s'⟫ →* ⟪Com.skip, s''⟫⊢ ⟪c₁ ;; c₂, s_1⟫ →* ⟪Com.skip, s''⟫ All goals completed! 🐙 c:Coms:Statet:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbT:beval s_1 b = truea:⟪c₁, s_1⟫ ⟹ s'IH:⟪c₁, s_1⟫ →* ⟪Com.skip, s'⟫⊢ ⟪Com.ite b c₁ c₂, s_1⟫ →* ⟪Com.skip, s'⟫ All goals completed! 🐙 c:Coms:Statet:Stateb:BExpc₁:Comc₂:Coms_1:States':StatehbF:beval s_1 b = falsea:⟪c₂, s_1⟫ ⟹ s'IH:⟪c₂, s_1⟫ →* ⟪Com.skip, s'⟫⊢ ⟪Com.ite b c₁ c₂, s_1⟫ →* ⟪Com.skip, s'⟫ All goals completed! 🐙 c:Coms✝:Statet:Stateb:BExpc_1:Coms:StatehbF:beval s b = false⊢ ⟪Com.whl b c_1, s⟫ →* ⟪Com.skip, s⟫ All goals completed! 🐙 c:Coms:Statet:Stateb:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b = truea:⟪c_1, s_1⟫ ⟹ s'a_1:⟪Com.whl b c_1, s'⟫ ⟹ s''IHc:⟪c_1, s_1⟫ →* ⟪Com.skip, s'⟫IHw:⟪Com.whl b c_1, s'⟫ →* ⟪Com.skip, s''⟫⊢ ⟪Com.whl b c_1, s_1⟫ →* ⟪Com.skip, s''⟫ All goals completed! 🐙

3.2.6.2. From small-step to big-step

For the converse direction, we use the same idea as in the previous chapter: an expansion lemma stating that a reduction step preserves the final state. If \langle c, s\rangle \to_1 \langle c', s'\rangle and \langle c', s'\rangle \Downarrow t, then \langle c, s\rangle \Downarrow t.

The proof is by induction on the step. The most interesting case is Unfold: the big-step derivation of the conditional if b then (c ;; while b do c) else skip must have been built by IfT or by IfF. In the first case, by also inverting the derivation of the sequence, we obtain exactly the premises of WhileT; in the second, the derivation of skip forces t = s and we apply WhileF.

lemma smallStep_bigStep {c c' : Com} {s s' : State} (hs : ⟪c, s⟫ →₁ ⟪c', s'⟫) : ∀ {t : State}, ⟪c', s'⟫ ⟹ t → ⟪c, s⟫ ⟹ t := c:Comc':Coms:States':Statehs:⟪c, s⟫ →₁ ⟪c', s'⟫⊢ ∀ {t : State}, ⟪c', s'⟫ ⟹ t → ⟪c, s⟫ ⟹ t induction hs with c:Comc':Coms✝:States':Statex:Vara:AExps:State⊢ ∀ {t : State}, ⟪Com.skip, s[x ↦ aeval s a]⟫ ⟹ t → ⟪x ::= a, s⟫ ⟹ t intro t c:Comc':Coms✝:States':Statex:Vara:AExps:Statet:Statehb:⟪Com.skip, s[x ↦ aeval s a]⟫ ⟹ t⊢ ⟪x ::= a, s⟫ ⟹ t c:Comc':Coms✝:States':Statex:Vara:AExps:State⊢ ⟪x ::= a, s⟫ ⟹ s[x ↦ aeval s a] All goals completed! 🐙 c:Comc':Coms✝:States':Statec₂:Coms:State⊢ ∀ {t : State}, ⟪c₂, s⟫ ⟹ t → ⟪Com.skip ;; c₂, s⟫ ⟹ t intro t c:Comc':Coms✝:States':Statec₂:Coms:Statet:Statehb:⟪c₂, s⟫ ⟹ t⊢ ⟪Com.skip ;; c₂, s⟫ ⟹ t All goals completed! 🐙 c:Comc':Coms:States':Statec₁:Comc₁':Comc₂:Coms_1:States'_1:Statea:⟪c₁, s_1⟫ →₁ ⟪c₁', s'_1⟫IH:∀ {t : State}, ⟪c₁', s'_1⟫ ⟹ t → ⟪c₁, s_1⟫ ⟹ t⊢ ∀ {t : State}, ⟪c₁' ;; c₂, s'_1⟫ ⟹ t → ⟪c₁ ;; c₂, s_1⟫ ⟹ t intro t c:Comc':Coms:States':Statec₁:Comc₁':Comc₂:Coms_1:States'_1:Statea:⟪c₁, s_1⟫ →₁ ⟪c₁', s'_1⟫IH:∀ {t : State}, ⟪c₁', s'_1⟫ ⟹ t → ⟪c₁, s_1⟫ ⟹ tt:Statehb:⟪c₁' ;; c₂, s'_1⟫ ⟹ t⊢ ⟪c₁ ;; c₂, s_1⟫ ⟹ t cases hb with c:Comc':Coms:States':Statec₁:Comc₁':Comc₂:Coms_1:States'_1:Statea:⟪c₁, s_1⟫ →₁ ⟪c₁', s'_1⟫IH:∀ {t : State}, ⟪c₁', s'_1⟫ ⟹ t → ⟪c₁, s_1⟫ ⟹ tt:States'_2:Statehc1:⟪c₁', s'_1⟫ ⟹ s'_2hc2:⟪c₂, s'_2⟫ ⟹ t⊢ ⟪c₁ ;; c₂, s_1⟫ ⟹ t All goals completed! 🐙 c:Comc':Coms✝:States':Stateb:BExpc₁:Comc₂:Coms:StatehbT:beval s b = true⊢ ∀ {t : State}, ⟪c₁, s⟫ ⟹ t → ⟪Com.ite b c₁ c₂, s⟫ ⟹ t intro t c:Comc':Coms✝:States':Stateb:BExpc₁:Comc₂:Coms:StatehbT:beval s b = truet:Statehb:⟪c₁, s⟫ ⟹ t⊢ ⟪Com.ite b c₁ c₂, s⟫ ⟹ t All goals completed! 🐙 c:Comc':Coms✝:States':Stateb:BExpc₁:Comc₂:Coms:StatehbF:beval s b = false⊢ ∀ {t : State}, ⟪c₂, s⟫ ⟹ t → ⟪Com.ite b c₁ c₂, s⟫ ⟹ t intro t c:Comc':Coms✝:States':Stateb:BExpc₁:Comc₂:Coms:StatehbF:beval s b = falset:Statehb:⟪c₂, s⟫ ⟹ t⊢ ⟪Com.ite b c₁ c₂, s⟫ ⟹ t All goals completed! 🐙 c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:State⊢ ∀ {t : State}, ⟪Com.ite b (c_1 ;; Com.whl b c_1) Com.skip, s⟫ ⟹ t → ⟪Com.whl b c_1, s⟫ ⟹ t intro t c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:Statet:Statehb:⟪Com.ite b (c_1 ;; Com.whl b c_1) Com.skip, s⟫ ⟹ t⊢ ⟪Com.whl b c_1, s⟫ ⟹ t cases hb with c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:Statet:StatehbT:beval s b = truehseq:⟪c_1 ;; Com.whl b c_1, s⟫ ⟹ t⊢ ⟪Com.whl b c_1, s⟫ ⟹ t cases hseq with c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:Statet:StatehbT:beval s b = trues'_1:Statehc:⟪c_1, s⟫ ⟹ s'_1hw:⟪Com.whl b c_1, s'_1⟫ ⟹ t⊢ ⟪Com.whl b c_1, s⟫ ⟹ t All goals completed! 🐙 c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:Statet:StatehbF:beval s b = falsehskip:⟪Com.skip, s⟫ ⟹ t⊢ ⟪Com.whl b c_1, s⟫ ⟹ t c:Comc':Coms✝:States':Stateb:BExpc_1:Coms:StatehbF:beval s b = false⊢ ⟪Com.whl b c_1, s⟫ ⟹ s All goals completed! 🐙

Iterating the expansion lemma along a sequence of steps, we obtain the version for \to^*. Here, unlike in the previous chapter, we do not need an auxiliary equality: it suffices to state the lemma for an arbitrary final configuration \langle c', s'\rangle and, in the main theorem, instantiate it with c' = \textsf{skip}, using the trivial derivation \langle \textsf{skip}, t\rangle \Downarrow t.

lemma multiStep_to_bigStep_aux {c c' : Com} {s s' : State} (h : ⟪c, s⟫ →* ⟪c', s'⟫) : ∀ {t : State}, ⟪c', s'⟫ ⟹ t → ⟪c, s⟫ ⟹ t := c:Comc':Coms:States':Stateh:⟪c, s⟫ →* ⟪c', s'⟫⊢ ∀ {t : State}, ⟪c', s'⟫ ⟹ t → ⟪c, s⟫ ⟹ t induction h with c:Comc':Coms:States':Statec_1:Coms_1:State⊢ ∀ {t : State}, ⟪c_1, s_1⟫ ⟹ t → ⟪c_1, s_1⟫ ⟹ t intro t c:Comc':Coms:States':Statec_1:Coms_1:Statet:Statehb:⟪c_1, s_1⟫ ⟹ t⊢ ⟪c_1, s_1⟫ ⟹ t; All goals completed! 🐙 c:Comc':Coms:States':Statec_1:Comc'_1:Comc'':Coms_1:States'_1:States'':Statehs:⟪c_1, s_1⟫ →₁ ⟪c'_1, s'_1⟫a:⟪c'_1, s'_1⟫ →* ⟪c'', s''⟫IH:∀ {t : State}, ⟪c'', s''⟫ ⟹ t → ⟪c'_1, s'_1⟫ ⟹ t⊢ ∀ {t : State}, ⟪c'', s''⟫ ⟹ t → ⟪c_1, s_1⟫ ⟹ t intro t c:Comc':Coms:States':Statec_1:Comc'_1:Comc'':Coms_1:States'_1:States'':Statehs:⟪c_1, s_1⟫ →₁ ⟪c'_1, s'_1⟫a:⟪c'_1, s'_1⟫ →* ⟪c'', s''⟫IH:∀ {t : State}, ⟪c'', s''⟫ ⟹ t → ⟪c'_1, s'_1⟫ ⟹ tt:Statehb:⟪c'', s''⟫ ⟹ t⊢ ⟪c_1, s_1⟫ ⟹ t All goals completed! 🐙 theorem multiStep_to_bigStep {c : Com} {s t : State} (h : ⟪c, s⟫ →* ⟪.skip, t⟫) : ⟪c, s⟫ ⟹ t := multiStep_to_bigStep_aux h (.skip t)

Finally, the equivalence theorem:

theorem bigStep_iff_multiStep (c : Com) (s t : State) : ⟪c, s⟫ ⟹ t ↔ ⟪c, s⟫ →* ⟪.skip, t⟫ := ⟨bigStep_to_multiStep, multiStep_to_bigStep⟩ end Ch18

This theorem lets us choose, in each situation, the most convenient semantics. The big-step semantics is better suited for reasoning about programs that terminate, and it will be the basis of Hoare logic in the next chapter. The small-step semantics, on the other hand, lets us talk about partial executions and nonterminating programs, and is the natural choice for studying concurrency or properties such as progress and type preservation.

3.2.7. Exercises

1. Using Com, write an IMP program that computes in "r" the sum
   0 + 1 + ... + n, where n is the initial value of the variable "n".
   Prove, using BigStep, that starting from a state with n = 2 the
   program terminates with r = 3.

2. Prove that the program  while true do skip  does not terminate,
   that is,
     ∀ s t, ¬ ⟪.whl .tt .skip, s⟫ ⟹ t.
   (Hint: generalize the command, as in multiStep_to_bigStep_aux
   from the previous chapter, and do induction on the derivation.)

3. We say that c₁ and c₂ are equivalent if
     ∀ s t, ⟪c₁, s⟫ ⟹ t ↔ ⟪c₂, s⟫ ⟹ t.
   Prove that  while b do c  is equivalent to
     if b then (c ;; while b do c) else skip.

4. Prove that the small-step semantics is deterministic:
   if ⟪c, s⟫ →₁ ⟪c₁, s₁⟫ and ⟪c, s⟫ →₁ ⟪c₂, s₂⟫ then c₁ = c₂ and s₁ = s₂.

5. Prove the progress property: for every command c and state s,
   either c = skip, or there is ⟪c', s'⟫ such that ⟪c, s⟫ →₁ ⟪c', s'⟫.

6. Extend IMP with the command  repeat c until b  and add big-step
   and small-step rules for it. Update the equivalence proof.