import Std.Data.HashMap open Std /-! # Defintional interpreters, big-step, small-step semantics. In this practical session, we will see the three main ways to define the semantics of a programming language, and show that they are equivalent. The language we consider is a simple arithemtic expression language. -/ inductive Binop where | add | sub | mul def Binop.eval (op : Binop) (x y : Int) := match op with | .add => x + y | .sub => x - y | .mul => x * y inductive Expr where | var (x : String) | const (n : Int) | neg (e : Expr) | binop (e₁ : Expr) (op : Binop) (e₂ : Expr) /-- An environment associates values with variables -/ abbrev Env := HashMap String Int section interpreter /-- The evaluator is a function that takes an environment and an expression and computes the final value of the expression. -/ def Expr.eval (env : Env) (e : Expr) : Int := match e with | .var x => env.getD x 0 -- returns either the value associated to `x`, or `0` if not present | .const n => n | .neg e => - (e.eval env) | .binop e₁ op e₂ => op.eval (e₁.eval env) (e₂.eval env) end interpreter section bigstep /-- The big-step semantics is a relation `Eval env e v` which says that the final value of the expression `e`, evaluated in the environment `env` is `v`. -/ inductive Eval (env : Env) : Expr → Int → Prop where | var_in : env[x]? = some v → Eval env (.var x) v | var_notin : env[x]? = none → Eval env (.var x) 0 | const n : Eval env (.const n) n | neg : Eval env e v → Eval env (.neg e) (-v) | binop : Eval env e₁ v₁ → Eval env e₂ v₂ → Eval env (.binop e₁ op e₂) (op.eval v₁ v₂) end bigstep section smallstep /-- The small-step semantics has a relation `Step env e₁ e₂` which says that _one step_ of the evaluation of the expression `e₁` is to reduce it to `e₂`. The `Steps` relation consists in composing an arbitrary number of steps, so that `Steps env e₁ e₂` means that `e₁` can reduce to `e₂`. For this langauge, the "final value" are the constants expressions `const n`, so that `Steps env e (.const n)` means that `n` is a final value of `e`. -/ inductive Step (env : Env) : Expr → Expr → Prop where | var_in : env[x]? = some v → Step env (.var x) (.const v) | var_notin : env[x]? = none → Step env (.var x) (.const 0) | neg_1 : Step env e e' → Step env (.neg e) (.neg e') | neg_2 : Step env (.neg (.const n)) (.const (-n)) | binop_1 : Step env e₁ e₁' → Step env (.binop e₁ op e₂) (.binop e₁' op e₂) | binop_2 : Step env e₂ e₂' → Step env (.binop (.const n₁) op e₂) (.binop (.const n₁) op e₂') | binop_3 : Step env (.binop (.const n₁) op (.const n₂)) (.const (op.eval n₁ n₂)) inductive Steps env : Expr → Expr → Prop where | refl : Steps env e e | steps e'' : Steps env e e'' → Step env e'' e''' → Steps env e e''' theorem Steps.step : Step env e e' → Steps env e e' := by intros h apply Steps.steps e .refl h end smallstep -- 1. Prove that the interpreter and the big step semantics are the same. -- Note: Loogle can be used to search for useful lemmas. For example, these -- queries should be useful to reason about the hashmap: -- https://loogle.lean-lang.org/?q=Std.HashMap.getD%2C+%22fallback%22 -- https://loogle.lean-lang.org/?q=Std.HashMap.getD%2C%20GetElem%3F.getElem%3F theorem interp_eq_eval : Eval env e (e.eval env) := by sorry -- 2. Prove that the big step semantics and the small step semantics are the same. -- First, we prove a few useful lemmas. theorem no_step_const : ¬ Step env (.const n) e := by sorry theorem rfl_of_steps_const : Steps env (.const n) e → e = (.const n) := by sorry theorem step_var_of_steps_var : Steps env (.var x) (.const n) → Step env (.var x) (.const n) := by intros h generalize heq : (Expr.const n) = e' at * suffices h : e' = (.var x) ∨ Step env (.var x) e' by grind clear heq induction h with | refl => grind | steps _ hsteps hstep ih => rcases ih with ih | ih · subst ih grind [no_step_const] · right cases ih <;> grind [no_step_const] theorem step_neg : Step env e e' → Step env (.neg e) (.neg e') := by intros h cases h <;> grind [Step] theorem steps_neg : Steps env e e' → Steps env (.neg e) (.neg e') := by sorry theorem steps_neg_inv : Steps env (.neg e) e' → (∃ e'', e' = .neg e'' ∧ Steps env e e'') ∨ (∃ n, e' = .const n) := by sorry /-- The main theorem. You do not need to do the `binop` case in the induction. -/ theorem eval_iff_steps : Eval env e v ↔ Steps env e (.const v) := by induction e generalizing v with | var x => sorry | const n => sorry | neg e ih => sorry | binop e₁ op e₂ => sorry -- keep it sorried