3.1. Operational Semantics of Expressions

So far we have used Lean mainly to write functional programs and prove properties about them. From this chapter on, we change focus: we will use Lean as a metalanguage to describe and reason about other programming languages. The central question is: what does a program mean? Answering this question precisely is the goal of formal semantics.

There are several ways of assigning meaning to programs. In this chapter we study three of them, using a tiny language of arithmetic expressions as our laboratory:

  • denotational semantics, which assigns to each expression a mathematical object (in our case, a natural number) by means of a recursive function;

  • big-step (or natural) operational semantics, which relates an expression directly to its final value;

  • small-step (or structural) operational semantics, which describes execution as a sequence of elementary reduction steps.

The language is deliberately simple so that we can concentrate on the techniques: definitions by inductive predicates, proofs by induction on derivations, and proofs of equivalence between semantics. These same techniques will be applied, in the next chapter, to a full imperative language.

3.1.1. Syntax

Our language has only numeric constants, addition and multiplication. In BNF notation, we have:

e ::= n | e + e | e * e

In Lean, the abstract syntax of this language is represented by an inductive type, with one constructor for each form of expression:

namespace Ch17 inductive Expr where | num : ℕ → Expr | add : Expr → Expr → Expr | mul : Expr → Expr → Expr deriving Repr

Note that abstract syntax is not concerned with parentheses or operator precedence: the expression (3 + 4) * 2 corresponds to the tree Expr.mul (Expr.add (Expr.num 3) (Expr.num 4)) (Expr.num 2), whereas 3 + 4 * 2 corresponds to a different tree. The tree structure already resolves any ambiguity.

3.1.2. Denotational semantics

The most direct way to give meaning to an expression is to define a function that evaluates it. This function maps each expression to its denotation, a natural number, and is defined by structural recursion on the syntax:

def eval : Expr → ℕ | .num n => n | .add e1 e2 => eval e1 + eval e2 | .mul e1 e2 => eval e1 * eval e2

The essential feature of a denotational semantics is compositionality: the meaning of a compound expression is obtained from the meanings of its parts. In mathematical notation, we write

[\![ n ]\!] = n \qquad [\![ e_1 + e_2 ]\!] = [\![ e_1 ]\!] + [\![ e_2 ]\!] \qquad [\![ e_1 * e_2 ]\!] = [\![ e_1 ]\!] \times [\![ e_2 ]\!]

Since eval is a computable function, we can run it:

14#eval eval (.mul (.add (.num 3) (.num 4)) (.num 2))
14

Denotational semantics is elegant, but it completely hides how computation happens: it says nothing about the order in which subexpressions are evaluated, nor about intermediate states. For richer languages (with loops that may not terminate, for example) defining a total function that gives the meaning of every program can be difficult. Operational semantics solve this problem by describing execution through inductively defined relations.

3.1.3. Big-step semantics

In big-step semantics we write e \Downarrow n to say that the expression e evaluates to the number n, all at once, without detailing the intermediate steps. The relation is defined by the following inference rules:

\frac{}{\textsf{num}\ n \Downarrow n}\,(\textsf{BS-Num})

\frac{e_1 \Downarrow n_1 \quad e_2 \Downarrow n_2}{e_1 + e_2 \Downarrow n_1 + n_2}\,(\textsf{BS-Add}) \qquad \frac{e_1 \Downarrow n_1 \quad e_2 \Downarrow n_2}{e_1 * e_2 \Downarrow n_1 \times n_2}\,(\textsf{BS-Mul})

Each rule corresponds to a constructor of an inductive predicate. The premises become arguments of the constructor and the conclusion becomes the type of the result:

inductive BigStep : Expr → ℕ → Prop where | num (n : ℕ) : BigStep (.num n) n | add {e1 e2 : Expr} {n1 n2 : ℕ} : BigStep e1 n1 → BigStep e2 n2 → BigStep (.add e1 e2) (n1 + n2) | mul {e1 e2 : Expr} {n1 n2 : ℕ} : BigStep e1 n1 → BigStep e2 n2 → BigStep (.mul e1 e2) (n1 * n2) scoped infix:50 " ⇓ " => BigStep

The notation is declared with scoped, so that it is only available when the namespace Ch17 is open.This avoids clashes with the notations of the same name that we will define for the IMP language in the next chapter.

A proof that e \Downarrow n is a derivation tree built with the rules above. For example, to show that (1 + 2) * 3 \Downarrow 9, we must apply BS-Mul, and then BS-Add and BS-Num to the premises. There is a subtlety: the conclusion of BS-Mul has the form n_1 \times n_2, and Lean cannot automatically unify the literal 9 with 3 * 3. Therefore, before applying the rule, we rewrite the goal:

example : .mul (.add (.num 1) (.num 2)) (.num 3) ⇓ 9 := ⊢ ((Expr.num 1).add (Expr.num 2)).mul (Expr.num 3) ⇓ 9 have Heq : 9 = 3 * 3 := ⊢ ((Expr.num 1).add (Expr.num 2)).mul (Expr.num 3) ⇓ 9 All goals completed! 🐙 Heq:9 = 3 * 3⊢ ((Expr.num 1).add (Expr.num 2)).mul (Expr.num 3) ⇓ 3 * 3 Heq:9 = 3 * 3⊢ (Expr.num 1).add (Expr.num 2) ⇓ 3Heq:9 = 3 * 3⊢ Expr.num 3 ⇓ 3 Heq:9 = 3 * 3⊢ (Expr.num 1).add (Expr.num 2) ⇓ 3 have Heq1 : 3 = 1 + 2 := ⊢ ((Expr.num 1).add (Expr.num 2)).mul (Expr.num 3) ⇓ 9 All goals completed! 🐙 Heq:9 = 3 * 3Heq1:3 = 1 + 2⊢ (Expr.num 1).add (Expr.num 2) ⇓ 1 + 2 Heq:9 = 3 * 3Heq1:3 = 1 + 2⊢ Expr.num 1 ⇓ 1Heq:9 = 3 * 3Heq1:3 = 1 + 2⊢ Expr.num 2 ⇓ 2 Heq:9 = 3 * 3Heq1:3 = 1 + 2⊢ Expr.num 1 ⇓ 1 All goals completed! 🐙 Heq:9 = 3 * 3Heq1:3 = 1 + 2⊢ Expr.num 2 ⇓ 2 All goals completed! 🐙 Heq:9 = 3 * 3⊢ Expr.num 3 ⇓ 3 All goals completed! 🐙

3.1.3.1. Connection with the evaluator

We now have two definitions of meaning for expressions: the function eval and the relation BigStep. Naturally, we expect them to agree. This is expressed in two directions. The first says that the evaluator produces a result that the big-step semantics accepts:

\forall e,\ e \Downarrow [\![ e ]\!]

The proof is by structural induction on the expression e. In each case, it suffices to apply the corresponding constructor; the induction hypotheses provide the derivations for the subexpressions.

theorem eval_bigStep (e : Expr) : e ⇓ eval e := e:Expr⊢ e ⇓ eval e induction e with n:ℕ⊢ Expr.num n ⇓ eval (Expr.num n) All goals completed! 🐙 e1:Expre2:ExprIH1:e1 ⇓ eval e1IH2:e2 ⇓ eval e2⊢ e1.add e2 ⇓ eval (e1.add e2) e1:Expre2:ExprIH1:e1 ⇓ eval e1IH2:e2 ⇓ eval e2⊢ e1 ⇓ eval e1e1:Expre2:ExprIH1:e1 ⇓ eval e1IH2:e2 ⇓ eval e2⊢ e2 ⇓ eval e2 e1:Expre2:ExprIH1:e1 ⇓ eval e1IH2:e2 ⇓ eval e2⊢ e1 ⇓ eval e1e1:Expre2:ExprIH1:e1 ⇓ eval e1IH2:e2 ⇓ eval e2⊢ e2 ⇓ eval e2 All goals completed! 🐙 a:Expra_1:ExprIH1:a ⇓ eval aIH2:a_1 ⇓ eval a_1⊢ a.mul a_1 ⇓ eval (a.mul a_1) a:Expra_1:ExprIH1:a ⇓ eval aIH2:a_1 ⇓ eval a_1⊢ a ⇓ eval aa:Expra_1:ExprIH1:a ⇓ eval aIH2:a_1 ⇓ eval a_1⊢ a_1 ⇓ eval a_1 a:Expra_1:ExprIH1:a ⇓ eval aIH2:a_1 ⇓ eval a_1⊢ a ⇓ eval aa:Expra_1:ExprIH1:a ⇓ eval aIH2:a_1 ⇓ eval a_1⊢ a_1 ⇓ eval a_1 All goals completed! 🐙

The converse says that every value obtained by the big-step semantics is exactly the value computed by eval. Here the induction is not on the expression, but on the derivation of e \Downarrow n: we have one case for each rule. This is the induction principle that Lean generates automatically for the inductive predicate BigStep.

theorem bigStep_eval {e : Expr} {n : ℕ} (h : e ⇓ n) : eval e = n := e:Exprn:ℕh:e ⇓ n⊢ eval e = n induction h with e:Exprn✝:ℕn:ℕ⊢ eval (Expr.num n) = n All goals completed! 🐙 e:Exprn:ℕe1:Expre2:Exprn1:ℕn2:ℕa:e1 ⇓ n1a_1:e2 ⇓ n2IH1:eval e1 = n1IH2:eval e2 = n2⊢ eval (e1.add e2) = n1 + n2 All goals completed! 🐙 e:Exprn:ℕe1:Expre2:Exprn1:ℕn2:ℕa:e1 ⇓ n1a_1:e2 ⇓ n2IH1:eval e1 = n1IH2:eval e2 = n2⊢ eval (e1.mul e2) = n1 * n2 All goals completed! 🐙

3.1.3.2. Determinism

An important property of a semantics is determinism: each expression evaluates to at most one result. For the big-step semantics this is an immediate consequence of the two previous theorems (since n_1 = [\![ e ]\!] = n_2), but it is instructive to prove it directly, because the same technique will be used for languages where no evaluator is available.

The strategy is to do induction on the first derivation, h1, and in each case analyze the second derivation, h2, by cases. Since the shape of the expression is already fixed by the rule used in h1, only one rule can have been used in h2. The essential detail is the generalizing n2: the induction hypothesis must hold for any second result, not just for the n2 fixed in the statement.

theorem bigStep_det {e : Expr} {n1 n2 : ℕ} (h1 : e ⇓ n1) (h2 : e ⇓ n2) : n1 = n2 := e:Exprn1:ℕn2:ℕh1:e ⇓ n1h2:e ⇓ n2⊢ n1 = n2 induction h1 generalizing n2 with e:Exprn1:ℕn:ℕn2:ℕh2:Expr.num n ⇓ n2⊢ n = n2 cases h2 with e:Exprn1:ℕn:ℕ⊢ n = n All goals completed! 🐙 e:Exprn1:ℕe1:Expre2:Exprn1_1:ℕn2✝:ℕa:e1 ⇓ n1_1a_1:e2 ⇓ n2✝IH1:∀ {n2 : ℕ}, e1 ⇓ n2 → n1_1 = n2IH2:∀ {n2 : ℕ}, e2 ⇓ n2 → n2✝ = n2n2:ℕh2:e1.add e2 ⇓ n2⊢ n1_1 + n2✝ = n2 cases h2 with e:Exprn1:ℕe1:Expre2:Exprn1_1:ℕn2:ℕa:e1 ⇓ n1_1a_1:e2 ⇓ n2✝IH1:∀ {n2 : ℕ}, e1 ⇓ n2 → n1_1 = n2IH2:∀ {n2 : ℕ}, e2 ⇓ n2 → n2✝ = n2n1_2:ℕn2_1:ℕhb1:e1 ⇓ n1_2hb2:e2 ⇓ n2_1⊢ n1_1 + n2 = n1_2 + n2_1 All goals completed! 🐙 e:Exprn1:ℕe1:Expre2:Exprn1_1:ℕn2✝:ℕa:e1 ⇓ n1_1a_1:e2 ⇓ n2✝IH1:∀ {n2 : ℕ}, e1 ⇓ n2 → n1_1 = n2IH2:∀ {n2 : ℕ}, e2 ⇓ n2 → n2✝ = n2n2:ℕh2:e1.mul e2 ⇓ n2⊢ n1_1 * n2✝ = n2 cases h2 with e:Exprn1:ℕe1:Expre2:Exprn1_1:ℕn2:ℕa:e1 ⇓ n1_1a_1:e2 ⇓ n2✝IH1:∀ {n2 : ℕ}, e1 ⇓ n2 → n1_1 = n2IH2:∀ {n2 : ℕ}, e2 ⇓ n2 → n2✝ = n2n1_2:ℕn2_1:ℕhb1:e1 ⇓ n1_2hb2:e2 ⇓ n2_1⊢ n1_1 * n2 = n1_2 * n2_1 All goals completed! 🐙

3.1.4. Small-step semantics

The big-step semantics relates an expression to its final value, but it does not describe the evaluation process. The small-step semantics does exactly that: we write e \to_1 e' to say that e reduces to e' in a single step. The values (fully evaluated expressions) are the numbers \textsf{num}\ n.

The reduction rules for addition are:

\frac{e_1 \to_1 e_1'}{e_1 + e_2 \to_1 e_1' + e_2}\,(\textsf{SS-AddL}) \qquad \frac{e_2 \to_1 e_2'}{n + e_2 \to_1 n + e_2'}\,(\textsf{SS-AddR})

\frac{}{n_1 + n_2 \to_1 \textsf{num}\,(n_1 + n_2)}\,(\textsf{SS-AddNum})

and the rules for multiplication are analogous. There are two kinds of rule. The rules SS-AddNum and SS-MulNum are computation rules: they actually perform an operation. The others are congruence rules: they say at which position of an expression the next reduction must take place. Note that SS-AddR only applies when the left operand is already a number; this imposes a left-to-right evaluation strategy and, as we will see, makes the relation deterministic.

inductive SmallStep : Expr → Expr → Prop where | addL {e1 e1' e2 : Expr} : SmallStep e1 e1' → SmallStep (.add e1 e2) (.add e1' e2) | addR {n1 : ℕ} {e2 e2' : Expr} : SmallStep e2 e2' → SmallStep (.add (.num n1) e2) (.add (.num n1) e2') | addNum (n1 n2 : ℕ) : SmallStep (.add (.num n1) (.num n2)) (.num (n1 + n2)) | mulL {e1 e1' e2 : Expr} : SmallStep e1 e1' → SmallStep (.mul e1 e2) (.mul e1' e2) | mulR {n1 : ℕ} {e2 e2' : Expr} : SmallStep e2 e2' → SmallStep (.mul (.num n1) e2) (.mul (.num n1) e2') | mulNum (n1 n2 : ℕ) : SmallStep (.mul (.num n1) (.num n2)) (.num (n1 * n2)) scoped infix:50 " →₁ " => SmallStep

The expression (1 + 2) * 3 takes a step to 3 * 3: the congruence rule SS-MulL moves the reduction inside the left operand, where SS-AddNum performs the addition. Then 3 * 3 reduces to the value 9 by SS-MulNum.

example : .mul (.add (.num 1) (.num 2)) (.num 3) →₁ .mul (.num 3) (.num 3) := ⊢ ((Expr.num 1).add (Expr.num 2)).mul (Expr.num 3) →₁ (Expr.num 3).mul (Expr.num 3) ⊢ (Expr.num 1).add (Expr.num 2) →₁ Expr.num 3 All goals completed! 🐙 example : .mul (.num 7) (.num 2) →₁ .num 14 := .mulNum 7 2

3.1.4.1. Values do not reduce, and determinism

The first basic fact about the small-step semantics is that values are normal forms: no rule has a conclusion of the form \textsf{num}\ n \to_1 e. The proof is a simple case analysis on the reduction hypothesis, in which every case is impossible:

lemma num_irreducible {n : ℕ} {e : Expr} : ¬ (.num n →₁ e) := n:ℕe:Expr⊢ ¬Expr.num n →₁ e n:ℕe:ExprH:Expr.num n →₁ e⊢ False All goals completed! 🐙

With this lemma, we can prove that the small-step semantics is deterministic: if e \to_1 e_1 and e \to_1 e_2, then e_1 = e_2. The proof follows the same pattern as the big-step determinism proof: induction on the first derivation and case analysis on the second. The difference is that now pairs of incompatible rules show up. For example, if the first derivation used SS-AddL (reducing the left operand e_1) and the second used SS-AddR, then e_1 is a number, and the premise of SS-AddL would claim that a number reduces, which contradicts num_irreducible. All the mixed cases are eliminated in this way.

theorem smallStep_det {e e1 : Expr} (h1 : e →₁ e1) : ∀ {e2 : Expr}, e →₁ e2 → e1 = e2 := e:Expre1:Exprh1:e →₁ e1⊢ ∀ {e2 : Expr}, e →₁ e2 → e1 = e2 induction h1 with e:Expre1:Expre1_1:Expre1':Expre2:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2⊢ ∀ {e2_1 : Expr}, e1_1.add e2 →₁ e2_1 → e1'.add e2 = e2_1 intro e2 e:Expre1:Expre1_1:Expre1':Expre2✝:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2e2:Exprh2:e1_1.add e2✝ →₁ e2⊢ e1'.add e2✝ = e2 cases h2 with e:Expre1:Expre1_1:Expre1':Expre2:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2e1'_1:Exprhs2:e1_1 →₁ e1'_1⊢ e1'.add e2 = e1'_1.add e2 All goals completed! 🐙 e:Expre1:Expre1':Expre2:Exprn1:ℕe2':Exprhs1:Expr.num n1 →₁ e1'IH:∀ {e2 : Expr}, Expr.num n1 →₁ e2 → e1' = e2hs2:e2 →₁ e2'⊢ e1'.add e2 = (Expr.num n1).add e2' All goals completed! 🐙 e:Expre1:Expre1':Exprn1:ℕn2:ℕhs1:Expr.num n1 →₁ e1'IH:∀ {e2 : Expr}, Expr.num n1 →₁ e2 → e1' = e2⊢ e1'.add (Expr.num n2) = Expr.num (n1 + n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1⊢ ∀ {e2_1 : Expr}, (Expr.num n1).add e2 →₁ e2_1 → (Expr.num n1).add e2' = e2_1 intro e2 e:Expre1:Exprn1:ℕe2✝:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e2:Exprh2:(Expr.num n1).add e2✝ →₁ e2⊢ (Expr.num n1).add e2' = e2 cases h2 with e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e1':Exprhs2:Expr.num n1 →₁ e1'⊢ (Expr.num n1).add e2' = e1'.add e2 All goals completed! 🐙 e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e2'_1:Exprhs2:e2 →₁ e2'_1⊢ (Expr.num n1).add e2' = (Expr.num n1).add e2'_1 All goals completed! 🐙 e:Expre1:Exprn1:ℕe2':Exprn2:ℕhs1:Expr.num n2 →₁ e2'IH:∀ {e2 : Expr}, Expr.num n2 →₁ e2 → e2' = e2⊢ (Expr.num n1).add e2' = Expr.num (n1 + n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕ⊢ ∀ {e2 : Expr}, (Expr.num n1).add (Expr.num n2) →₁ e2 → Expr.num (n1 + n2) = e2 intro e2 e:Expre1:Exprn1:ℕn2:ℕe2:Exprh2:(Expr.num n1).add (Expr.num n2) →₁ e2⊢ Expr.num (n1 + n2) = e2 cases h2 with e:Expre1:Exprn1:ℕn2:ℕe1':Exprhs2:Expr.num n1 →₁ e1'⊢ Expr.num (n1 + n2) = e1'.add (Expr.num n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕe2':Exprhs2:Expr.num n2 →₁ e2'⊢ Expr.num (n1 + n2) = (Expr.num n1).add e2' All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕ⊢ Expr.num (n1 + n2) = Expr.num (n1 + n2) All goals completed! 🐙 e:Expre1:Expre1_1:Expre1':Expre2:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2⊢ ∀ {e2_1 : Expr}, e1_1.mul e2 →₁ e2_1 → e1'.mul e2 = e2_1 intro e2 e:Expre1:Expre1_1:Expre1':Expre2✝:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2e2:Exprh2:e1_1.mul e2✝ →₁ e2⊢ e1'.mul e2✝ = e2 cases h2 with e:Expre1:Expre1_1:Expre1':Expre2:Exprhs1:e1_1 →₁ e1'IH:∀ {e2 : Expr}, e1_1 →₁ e2 → e1' = e2e1'_1:Exprhs2:e1_1 →₁ e1'_1⊢ e1'.mul e2 = e1'_1.mul e2 All goals completed! 🐙 e:Expre1:Expre1':Expre2:Exprn1:ℕe2':Exprhs1:Expr.num n1 →₁ e1'IH:∀ {e2 : Expr}, Expr.num n1 →₁ e2 → e1' = e2hs2:e2 →₁ e2'⊢ e1'.mul e2 = (Expr.num n1).mul e2' All goals completed! 🐙 e:Expre1:Expre1':Exprn1:ℕn2:ℕhs1:Expr.num n1 →₁ e1'IH:∀ {e2 : Expr}, Expr.num n1 →₁ e2 → e1' = e2⊢ e1'.mul (Expr.num n2) = Expr.num (n1 * n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1⊢ ∀ {e2_1 : Expr}, (Expr.num n1).mul e2 →₁ e2_1 → (Expr.num n1).mul e2' = e2_1 intro e2 e:Expre1:Exprn1:ℕe2✝:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e2:Exprh2:(Expr.num n1).mul e2✝ →₁ e2⊢ (Expr.num n1).mul e2' = e2 cases h2 with e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e1':Exprhs2:Expr.num n1 →₁ e1'⊢ (Expr.num n1).mul e2' = e1'.mul e2 All goals completed! 🐙 e:Expre1:Exprn1:ℕe2:Expre2':Exprhs1:e2 →₁ e2'IH:∀ {e2_1 : Expr}, e2 →₁ e2_1 → e2' = e2_1e2'_1:Exprhs2:e2 →₁ e2'_1⊢ (Expr.num n1).mul e2' = (Expr.num n1).mul e2'_1 All goals completed! 🐙 e:Expre1:Exprn1:ℕe2':Exprn2:ℕhs1:Expr.num n2 →₁ e2'IH:∀ {e2 : Expr}, Expr.num n2 →₁ e2 → e2' = e2⊢ (Expr.num n1).mul e2' = Expr.num (n1 * n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕ⊢ ∀ {e2 : Expr}, (Expr.num n1).mul (Expr.num n2) →₁ e2 → Expr.num (n1 * n2) = e2 intro e2 e:Expre1:Exprn1:ℕn2:ℕe2:Exprh2:(Expr.num n1).mul (Expr.num n2) →₁ e2⊢ Expr.num (n1 * n2) = e2 cases h2 with e:Expre1:Exprn1:ℕn2:ℕe1':Exprhs2:Expr.num n1 →₁ e1'⊢ Expr.num (n1 * n2) = e1'.mul (Expr.num n2) All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕe2':Exprhs2:Expr.num n2 →₁ e2'⊢ Expr.num (n1 * n2) = (Expr.num n1).mul e2' All goals completed! 🐙 e:Expre1:Exprn1:ℕn2:ℕ⊢ Expr.num (n1 * n2) = Expr.num (n1 * n2) All goals completed! 🐙

Note that, once again, the statement quantifies over e2 after the colon. This makes the induction hypothesis IH valid for any e2, which is needed in the congruence cases, where the second result is a different one.

3.1.4.2. Reflexive-transitive closure

A single step rarely takes an expression to a value. To talk about complete evaluations we use the reflexive-transitive closure of \to_1, written \to^*: e \to^* e' means that e reduces to e' in zero or more steps.

\frac{}{e \to^* e}\,(\textsf{Refl}) \qquad \frac{e \to_1 e' \quad e' \to^* e''}{e \to^* e''}\,(\textsf{Step})

inductive MultiStep : Expr → Expr → Prop where | refl {e : Expr} : MultiStep e e | step {e e' e'' : Expr} : e →₁ e' → MultiStep e' e'' → MultiStep e e'' scoped infix:50 " →* " => MultiStep

The definition adds steps only on the left. A property we will use all the time is transitivity, proved by induction on the first sequence of steps:

lemma MultiStep.trans {e e' e'' : Expr} (h1 : e →* e') (h2 : e' →* e'') : e →* e'' := e:Expre':Expre'':Exprh1:e →* e'h2:e' →* e''⊢ e →* e'' induction h1 with e:Expre':Expre'':Expre_1:Exprh2:e_1 →* e''⊢ e_1 →* e'' All goals completed! 🐙 e:Expre':Expre'':Expre_1:Expre'_1:Expre''_1:Exprhs:e_1 →₁ e'_1a:e'_1 →* e''_1IH:e''_1 →* e'' → e'_1 →* e''h2:e''_1 →* e''⊢ e_1 →* e'' All goals completed! 🐙

3.1.5. Equivalence between big-step and small-step

We now have two operational semantics. The main result of this chapter is that they are equivalent:

e \Downarrow n \iff e \to^* \textsf{num}\ n

3.1.5.1. Congruence lemmas

The congruence rules of the small-step semantics hold for a single step. The equivalence proof will need their versions for sequences of steps: if e_1 \to^* e_1', then e_1 + e_2 \to^* e_1' + e_2, and so on. Each one is proved by induction on the sequence of steps, applying the corresponding congruence rule at each step.

lemma multiStep_addL {e1 e1' e2 : Expr} (h : e1 →* e1') : .add e1 e2 →* .add e1' e2 := e1:Expre1':Expre2:Exprh:e1 →* e1'⊢ e1.add e2 →* e1'.add e2 induction h with e1:Expre1':Expre2:Expre:Expr⊢ e.add e2 →* e.add e2 All goals completed! 🐙 e1:Expre1':Expre2:Expre:Expre':Expre'':Exprhs:e →₁ e'a:e' →* e''IH:e'.add e2 →* e''.add e2⊢ e.add e2 →* e''.add e2 All goals completed! 🐙 lemma multiStep_addR {n1 : ℕ} {e2 e2' : Expr} (h : e2 →* e2') : .add (.num n1) e2 →* .add (.num n1) e2' := n1:ℕe2:Expre2':Exprh:e2 →* e2'⊢ (Expr.num n1).add e2 →* (Expr.num n1).add e2' induction h with n1:ℕe2:Expre2':Expre:Expr⊢ (Expr.num n1).add e →* (Expr.num n1).add e All goals completed! 🐙 n1:ℕe2:Expre2':Expre:Expre':Expre'':Exprhs:e →₁ e'a:e' →* e''IH:(Expr.num n1).add e' →* (Expr.num n1).add e''⊢ (Expr.num n1).add e →* (Expr.num n1).add e'' All goals completed! 🐙 lemma multiStep_mulL {e1 e1' e2 : Expr} (h : e1 →* e1') : .mul e1 e2 →* .mul e1' e2 := e1:Expre1':Expre2:Exprh:e1 →* e1'⊢ e1.mul e2 →* e1'.mul e2 induction h with e1:Expre1':Expre2:Expre:Expr⊢ e.mul e2 →* e.mul e2 All goals completed! 🐙 e1:Expre1':Expre2:Expre:Expre':Expre'':Exprhs:e →₁ e'a:e' →* e''IH:e'.mul e2 →* e''.mul e2⊢ e.mul e2 →* e''.mul e2 All goals completed! 🐙 lemma multiStep_mulR {n1 : ℕ} {e2 e2' : Expr} (h : e2 →* e2') : .mul (.num n1) e2 →* .mul (.num n1) e2' := n1:ℕe2:Expre2':Exprh:e2 →* e2'⊢ (Expr.num n1).mul e2 →* (Expr.num n1).mul e2' induction h with n1:ℕe2:Expre2':Expre:Expr⊢ (Expr.num n1).mul e →* (Expr.num n1).mul e All goals completed! 🐙 n1:ℕe2:Expre2':Expre:Expre':Expre'':Exprhs:e →₁ e'a:e' →* e''IH:(Expr.num n1).mul e' →* (Expr.num n1).mul e''⊢ (Expr.num n1).mul e →* (Expr.num n1).mul e'' All goals completed! 🐙

3.1.5.2. From big-step to small-step

The first direction is proved by induction on the big-step derivation. The interesting case is addition: we know, from the induction hypotheses, that e_1 \to^* n_1 and e_2 \to^* n_2. We then chain together three stretches of execution:

e_1 + e_2 \;\to^*\; n_1 + e_2 \;\to^*\; n_1 + n_2 \;\to_1\; n_1 + n_2

where the first stretch uses multiStep_addL, the second uses multiStep_addR and the last is a single SS-AddNum step. The multiplication case is identical.

theorem bigStep_to_multiStep {e : Expr} {n : ℕ} (h : e ⇓ n) : e →* .num n := e:Exprn:ℕh:e ⇓ n⊢ e →* Expr.num n induction h with e:Exprn✝:ℕn:ℕ⊢ Expr.num n →* Expr.num n All goals completed! 🐙 e:Exprn:ℕe1:Expre2:Exprn1:ℕn2:ℕh1:e1 ⇓ n1h2:e2 ⇓ n2IH1:e1 →* Expr.num n1IH2:e2 →* Expr.num n2⊢ e1.add e2 →* Expr.num (n1 + n2) All goals completed! 🐙 e:Exprn:ℕe1:Expre2:Exprn1:ℕn2:ℕh1:e1 ⇓ n1h2:e2 ⇓ n2IH1:e1 →* Expr.num n1IH2:e2 →* Expr.num n2⊢ e1.mul e2 →* Expr.num (n1 * n2) All goals completed! 🐙

3.1.5.3. From small-step to big-step

The converse direction is more subtle. The key idea is an expansion lemma (sometimes called reverse preservation): if e \to_1 e' and e' \Downarrow n, then e \Downarrow n. In other words, taking a reduction step does not change the final value. The proof is by induction on the step e \to_1 e'; in each case we invert the big-step derivation of e' with cases and rebuild a derivation for e.

lemma smallStep_bigStep {e e' : Expr} (hs : e →₁ e') : ∀ {n : ℕ}, e' ⇓ n → e ⇓ n := e:Expre':Exprhs:e →₁ e'⊢ ∀ {n : ℕ}, e' ⇓ n → e ⇓ n induction hs with e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ n⊢ ∀ {n : ℕ}, e1'.add e2 ⇓ n → e1.add e2 ⇓ n intro n e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ nn:ℕhb:e1'.add e2 ⇓ n⊢ e1.add e2 ⇓ n cases hb with e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ nn1:ℕn2:ℕh1:e1' ⇓ n1h2:e2 ⇓ n2⊢ e1.add e2 ⇓ n1 + n2 All goals completed! 🐙 e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ n⊢ ∀ {n : ℕ}, (Expr.num n1).add e2' ⇓ n → (Expr.num n1).add e2 ⇓ n intro n e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ nn:ℕhb:(Expr.num n1).add e2' ⇓ n⊢ (Expr.num n1).add e2 ⇓ n cases hb with e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ nn1_1:ℕn2:ℕh1:Expr.num n1 ⇓ n1_1h2:e2' ⇓ n2⊢ (Expr.num n1).add e2 ⇓ n1_1 + n2 All goals completed! 🐙 e:Expre':Exprn1:ℕn2:ℕ⊢ ∀ {n : ℕ}, Expr.num (n1 + n2) ⇓ n → (Expr.num n1).add (Expr.num n2) ⇓ n intro n e:Expre':Exprn1:ℕn2:ℕn:ℕhb:Expr.num (n1 + n2) ⇓ n⊢ (Expr.num n1).add (Expr.num n2) ⇓ n cases hb with e:Expre':Exprn1:ℕn2:ℕ⊢ (Expr.num n1).add (Expr.num n2) ⇓ n1 + n2 All goals completed! 🐙 e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ n⊢ ∀ {n : ℕ}, e1'.mul e2 ⇓ n → e1.mul e2 ⇓ n intro n e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ nn:ℕhb:e1'.mul e2 ⇓ n⊢ e1.mul e2 ⇓ n cases hb with e:Expre':Expre1:Expre1':Expre2:Expra:e1 →₁ e1'IH:∀ {n : ℕ}, e1' ⇓ n → e1 ⇓ nn1:ℕn2:ℕh1:e1' ⇓ n1h2:e2 ⇓ n2⊢ e1.mul e2 ⇓ n1 * n2 All goals completed! 🐙 e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ n⊢ ∀ {n : ℕ}, (Expr.num n1).mul e2' ⇓ n → (Expr.num n1).mul e2 ⇓ n intro n e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ nn:ℕhb:(Expr.num n1).mul e2' ⇓ n⊢ (Expr.num n1).mul e2 ⇓ n cases hb with e:Expre':Exprn1:ℕe2:Expre2':Expra:e2 →₁ e2'IH:∀ {n : ℕ}, e2' ⇓ n → e2 ⇓ nn1_1:ℕn2:ℕh1:Expr.num n1 ⇓ n1_1h2:e2' ⇓ n2⊢ (Expr.num n1).mul e2 ⇓ n1_1 * n2 All goals completed! 🐙 e:Expre':Exprn1:ℕn2:ℕ⊢ ∀ {n : ℕ}, Expr.num (n1 * n2) ⇓ n → (Expr.num n1).mul (Expr.num n2) ⇓ n intro n e:Expre':Exprn1:ℕn2:ℕn:ℕhb:Expr.num (n1 * n2) ⇓ n⊢ (Expr.num n1).mul (Expr.num n2) ⇓ n cases hb with e:Expre':Exprn1:ℕn2:ℕ⊢ (Expr.num n1).mul (Expr.num n2) ⇓ n1 * n2 All goals completed! 🐙

Now it suffices to iterate this lemma along a sequence of steps. There is, however, a technical obstacle: the induction tactic does not cope well with hypotheses whose indices are not variables, such as e \to^* \textsf{num}\ n. The usual solution is to prove an auxiliary lemma in which the endpoint e' is a variable and the information e' = \textsf{num}\ n appears as an equality hypothesis.

lemma multiStep_to_bigStep_aux {e e' : Expr} (h : e →* e') : ∀ {n : ℕ}, e' = .num n → e ⇓ n := e:Expre':Exprh:e →* e'⊢ ∀ {n : ℕ}, e' = Expr.num n → e ⇓ n induction h with e:Expre':Expre_1:Expr⊢ ∀ {n : ℕ}, e_1 = Expr.num n → e_1 ⇓ n intro n e:Expre':Expre_1:Exprn:ℕheq:e_1 = Expr.num n⊢ e_1 ⇓ n e:Expre':Expre_1:Exprn:ℕheq:e_1 = Expr.num n⊢ Expr.num n ⇓ n All goals completed! 🐙 e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ n⊢ ∀ {n : ℕ}, e'' = Expr.num n → e_1 ⇓ n intro n e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ e_1 ⇓ n e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ e_1 →₁ ?step.e'e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ ?step.e' ⇓ ne:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ Expr e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ e_1 →₁ ?step.e'e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ ?step.e' ⇓ ne:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ Expr try e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ e'_1 ⇓ n e:Expre':Expre_1:Expre'_1:Expre'':Exprhs:e_1 →₁ e'_1a:e'_1 →* e''IH:∀ {n : ℕ}, e'' = Expr.num n → e'_1 ⇓ nn:ℕheq:e'' = Expr.num n⊢ e'' = Expr.num n ; All goals completed! 🐙 theorem multiStep_to_bigStep {e : Expr} {n : ℕ} (h : e →* .num n) : e ⇓ n := multiStep_to_bigStep_aux h rfl

Putting the two directions together, we obtain the equivalence theorem:

theorem bigStep_iff_multiStep (e : Expr) (n : ℕ) : e ⇓ n ↔ e →* .num n := ⟨bigStep_to_multiStep, multiStep_to_bigStep⟩

Combining it with eval_bigStep and bigStep_eval, we conclude that the three semantics studied (denotational, big-step and small-step) assign exactly the same meaning to every expression. For example, every expression reduces, in zero or more steps, to the value computed by the evaluator:

theorem multiStep_eval (e : Expr) : e →* .num (eval e) := bigStep_to_multiStep (eval_bigStep e) end Ch17

3.1.6. Exercises

1. Extend the language with a truncated subtraction constructor
   `sub : Expr → Expr → Expr`. Update eval, BigStep and SmallStep and
   redo the proofs of the chapter.

2. Prove the determinism of the big-step semantics using only the
   theorems eval_bigStep and bigStep_eval, without a new induction.

3. Prove that the small-step semantics "makes progress": every
   expression is either a number or takes a step, that is,
     ∀ e, (∃ n, e = .num n) ∨ (∃ e', e →₁ e').

4. Prove that, if e →* .num n1 and e →* .num n2, then n1 = n2.
   (Hint: use the equivalence theorem and big-step determinism.)

5. Define a variant of the small-step semantics that evaluates the
   right operand before the left one. Prove that it is also
   equivalent to the big-step semantics.

6. A stack machine has the instructions PUSH n, ADD and MUL.
   Define the type of instructions, a function exec that executes a
   list of instructions on a stack (List ℕ), and a compiler
   compile : Expr → List Instr. Prove that
     exec (compile e ++ is) s = exec is (eval e :: s)
   and conclude that exec (compile e) [] = [eval e].