More on Operational Semantics (Slides modified from those created by Xinyu Feng) 1 / 23
Outline Various formulations Extensions Going wrong Local variable declaration Heap Big-step operational semantics 2 / 23
Outline Various formulations Extensions Going wrong Local variable declaration Heap Big-step operational semantics 3 / 23
A different formulation Assignment: e intexp σ = n (x := e, σ) σ{x n} Here e intexp σ = n iff (e, σ) (n, σ) Compared to the original version: (e, σ) (e, σ) (x := e, σ) (x := e, σ) (x := n, σ) σ{x n} 4 / 23
A variation Assignment: e intexp σ = n (x := e, σ) (skip, σ{x n}) Here skip is overloaded as a flag for termination. 5 / 23
A variation Sequential composition: (c 0, σ) (c 0, σ ) (c 0 ; c 1, σ) (c 0 ; c 1, σ ) (skip ; c 1, σ) (c 1, σ) One more identity step is introduced after every command: consider x := x + 1 ; y := y + 2. Compared to the earlier rules: e intexp σ = n (x := e, σ) σ{x n} (skip, σ) σ (c 0, σ) (c 0, σ ) (c 0 ; c 1, σ) (c 0 ; c 1, σ ) (c 0, σ) σ (c 0 ; c 1, σ) (c 1, σ ) 6 / 23
A variation Other rules: b boolexp σ = tt (if b then c 0 else c 1, σ) (c 0, σ) b boolexp σ = ff (if b then c 0 else c 1, σ) (c 1, σ) b boolexp σ = tt (while b do c, σ) (c ; while b do c, σ) b boolexp σ = ff (while b do c, σ) (skip, σ) 7 / 23
Outline Various formulations Extensions Going wrong Local variable declaration Heap Big-step operational semantics 8 / 23
Going wrong Expressions: Expression evaluation: e ::=... e/e n 2 0 n 1 /n 2 = n (n 1 /n 2, σ) (n, σ) (n 1 /0, σ) abort 9 / 23
Going wrong Assignment: e intexp σ = n (x := e, σ) (skip, σ{x n}) e intexp σ = (x := e, σ) abort Here e intexp σ = n iff (e, σ) (n, σ) e intexp σ = iff (e, σ) abort 10 / 23
Going wrong Add new rules: (c 0, σ) abort (c 0 ; c 1, σ) abort b boolexp σ = (if b then c 0 else c 1, σ) abort b boolexp σ = (while b do c, σ) abort 11 / 23
Local variable declaration Statements: c ::=... newvar x := e in c An unsatisfactory attempt: n = σ x (newvar x := e in c, σ) (x := e ; c ; x := n, σ) Unsatisfactory because the value of local variable x could be exposed to external observers while c is executing. This is a problem when we have concurrency. 12 / 23
Semantics for newvar Solution (due to Eugene Fink): n = e intexp σ (c, σ{x n}) (c, σ ) n = σ x (newvar x := e in c, σ) (newvar x := n in c, σ {x σ x}) (newvar x := e in skip, σ) (skip, σ) e intexp σ = (newvar x := e in c, σ) abort n = e intexp σ (c, σ{x n}) abort (newvar x := e in c, σ) abort 13 / 23
Heap for dynamically-allocated data (States) σ ::= (s, h) (Stores) s Var Values (Heaps) h Loc fin Values (Values) v Int Bool Loc Here fin represents a partial mapping. 14 / 23
A simple language with heap manipulation Statements: c ::=... x := alloc(e) allocation y := [x] lookup [x] := e mutation free(x) deallocation 15 / 23
Operational semantics for alloc l dom(h) e intexp s = n (x := alloc(e), (s, h)) (skip, (s{x l}, h {l n})) 16 / 23
Operational semantics for free s x = l l dom(h) (free(x), (s, h)) (skip, (s, h\{l})) 17 / 23
Operational semantics for lookup and mutation s x = l h l = n (y := [x], (s, h)) (skip, (s{y n}, h)) s x = l l dom(h) e intexp s = n ([x] := e, (s, h)) (skip, (s, h{l n})) 18 / 23
Outline Various formulations Extensions Going wrong Local variable declaration Heap Big-step operational semantics 19 / 23
Big-Step Semantics (n, σ) n σ x = n (x, σ) n (e 1, σ) n 1 (e 2, σ) n 2 (e 1 + e 2, σ) n 1 + n 2 The last rule can be generalized to: (e 1, σ) n 1 (e 2, σ) n 2 (e 1 op e 2, σ) n 1 op n 2 Here italics indicates program text, and boldface indicates mathematical operators. 20 / 23
Big-Step Semantics (e, σ) n (x := e, σ) σ{x n} (skip, σ) σ (c 0, σ) σ (c 1, σ ) σ (c 0 ; c 1, σ) σ (b, σ) tt (c 0, σ) σ (if b then c 0 else c 1, σ) σ (b, σ) ff (c 1, σ) σ (if b then c 0 else c 1, σ) σ (b, σ) ff (while b do c, σ) σ (b, σ) tt (c, σ) σ (while b do c, σ ) σ (while b do c, σ) σ 21 / 23
Big-Step Semantics (e, σ) n (c, σ{x n}) σ (newvar x := e in c, σ) σ {x σ x} Also, we should add rules to handle the abort case. For instance, (e, σ) abort (x := e, σ) abort (c 0, σ) abort (c 0 ; c 1, σ) abort 22 / 23
Equivalence between big-step and small-step semantics For all c and σ, (c, σ) abort iff (c, σ) abort (c, σ) σ iff (c, σ) (skip, σ ) 23 / 23