Overview Transition Semantics Configurations and the transition relation Executions and computation Inference rules for small-step structural operational semantics for the simple imperative language Transition semantics of failure Labeled transition semantics of input and output Relationship with (direct) denotational semantics Operational (or Transition) Semantics Idea: Define the execution of a program as a sequence γ0, γ1, . . . of configurations γi ∈ Γ. Configurations are either terminal or nonterminal: Γ = ΓT ∪ Γ N ΓT ∩ ΓN = {} e.g. for the SIL ΓT = Σ ΓN = comm × Σ [x : 42] ∈ ΓT hx:= x+1, [x : 41]i ∈ ΓN Define a transition relation 7→ from ΓN to Γ: informally, γ 7→ γ 0 if γ 0 is obtained “in one step” from γ, e.g. hx:= x+1, [x : 41]i 7→ [x : 42] Executions and Computation An execution is a (finite or infinite) sequence of configurations γ0, γ1, . . . such that γi 7→ γi+1 whenever γi and γi+1 are in the sequence. The relation of computation 7→∗ is the reflexive and transitive closure of 7→ γ 7→∗ γ 0 if there is a finite execution starting with γ and ending with γ 0. For the SIL we will define 7→ which is a total function from ΓN to Γ, ⇒ for every γ ∈ Γ there is a longest execution starting with γ; if it is infinite, then γ diverges: γ ↑; otherwise there is a unique γ 0 ∈ ΓT such that γ 7→∗ γ 0. Plotkin Style Small-Step Structural Operational Semantics for the SIL We define the relation 7→ in terms of inference rules. (skip) (assgn) hskip, σi 7→ σ hv:= e, σi 7→ [σ | v : [[e]]intexp σ] hc0, σi 7→ σ 0 (seq t) hc0 ; c1, σi 7→ hc1, σ 0i hc0, σi 7→ hc00, σ 0i (seq s) hc0 ; c1, σi → 7 hc00 ; c1, σ 0i Example: (by (assgn)) hx:= x+1, [x : 4 | y : 6]i 7→ [x : 5 | y : 6] (by (seq t)) hx:= x+1 ; y:= y+x, [x : 4 | y : 6]i 7→ hy:= y+x, [x : 5 | y : 6]i (by (seq s)) hx:= x+1 ; y:= y+x ; skip, [x : 4 | y : 6]i 7→ hy:= y+x ; skip, [x : 5 | y : 6]i More SOS Rules (cond t) (cond f) (while t) (while f) hif b then c else c0, σi 7→ hc, σi hif b then c else c0, σi 7→ hc0, σi hwhile b do c, σi 7→ hc ; while b do c, σi hwhile b do c, σi 7→ σ when [[b]]boolexp σ = true when [[b]]boolexp σ = false when [[b]]boolexp σ = true when [[b]]boolexp σ = false However the naı̈ve rule for variable declaration hnewvar v:=e in c, σi 7→ hc ; v:= n, [σ | v : [[e]]intexp σ]i where n = σ v exposes the local variable name in the result, which becomes a problem when we extend the language. SOS Rule for Local Variable Declaration Idea: Use the declaration to reflect changes in the value of the variable. (decl t) (decl s) hc, [σ | v : [[e]]intexp σ]i 7→ σ 0 hnewvar v:=e in c, σi 7→ [σ 0 | v : σ v] hc, [σ | v : [[e]]intexp σ]i 7→ hc0, σ 0i hnewvar v:=e in c, σi 7→ hnewvar v:=σ 0 v in c0, [σ 0 | v : σ v]i (assgn) (seq t) (decl s) hx:= x+1, [x : 24 | y : 10]i 7→ [x : 25 | y : 10] hx:= x+1 ; y:= x+2, [x : 24 | y : 10]i 7→ hy:= x+2, [x : 25 | y : 10]i hnewvar x:=x+3 in x:= x+1 ; y:= x+2, [x : 21 | y : 10]i 7→ hnewvar x:=25 in y:= x+2, [x : 21 | y : 10]i Inference Rules for the Computation The reflexive and transitive closure of 7→ can also be defined using inference rules: γ 7→ γ 0 (incl) γ 7→∗ γ 0 (refl) γ 7→∗ γ γ 7→∗ γ 0 γ 0 7→∗ γ 00 (trans) γ 7→∗ γ 00 Meaning of Commands 7→ ∈ ΓN → Γ (total function) ⇒ ∀γ ∈ Γ there is a longest execution starting from γ, either infinite or ending with a γ 0 ∈ ΓT = Σ. ⊥, if hc, σi ↑ [[c]]comm σ = 0 σ , if hc, σi 7→∗ σ 0 Transition Semantics of Failure Define ΓT = Σ ∪ ({abort} × Σ). Then (fail) hfail, σi 7→ habort, σi Propagation of failure: hc0, σi 7→ habort, σi (seq x) hc0 ; c1, σi 7→ habort, σ 0i (decl x) hc, [σ | v : [[e]]intexp σ]i 7→ habort, σ 0i hnewvar v:=e in c, σi 7→ habort, [σ 0 | v : σ v]i The semantics of commands becomes ⊥, [[c]]comm σ = σ 0, habort σ 0i, if hc, σi ↑ if hc, σi 7→∗ σ 0 if hc, σi 7→∗ habort σ 0i Labeled Transition Semantics of Input and Output Informally: Write labels on transitions to show input or output. Rules: (output) (input) !n h!e, σi 7→ σ when n = [[e]]intexp σ ?n h?v, σi 7→ [σ | v : n] Formally, the transition “relation” becomes ternary: 7→ ⊆ ΓN × Λ × Γ, where Λ = {} ∪ {?n | n ∈ Z} ∪ {!n | n ∈ Z} λ and hc, σi 7→ γ stands for hhc, σi, λ, γi ∈ 7→. ( is silent ) Labeled Transition Semantics cont’d The other rules are generalized to propagate the labels, e.g. λ (seq t) (seq s) hc0, σi 7→ σ 0 λ hc0 ; c1, σi 7→ hc1, σ 0i λ hc0, σi 7→ hc00, σ 0i λ hc0 ; c1, σi 7→ hc00 ; c1, σ 0i λ (seq x) hc0, σi 7→ habort, σi λ hc0 ; c1, σi 7→ habort, σ 0i Properties of the Labeled Transition Semantics If γ = hc, σi ∈ ΓN , then exactly one of these holds: ∃!γ 0 ∈ Γ such that γ 7→ γ 0 (silent transition) !n ∃!γ 0 ∈ Γ, n ∈ Z such that γ 7→ γ 0 λ {λ ∈ Λ | γ 0 ∈ Γ and γ 7→ γ 0} = {?n | n ∈ Z} Hence for every γ ∈ Γ there is a longest sequence of silent transitions which is either infinite ends with a γ 0 ∈ ΓT !n ends with a γ 0 ∈ ΓN such that ∃!γ 00 ∈ Γ, n ∈ Z such that γ 0 7→ γ 00 ?n ends with a γ 0 ∈ ΓN such that ∀n ∈ Z. ∃γ 00 ∈ Γ. γ 0 7→ γ 00. Relationship with the Denotational Semantics So, for every γ ∈ Γ there is a longest sequence of silent transitions which is either infinite ends with a γ 0 ∈ ΓT !n ends with a γ 0 ∈ ΓN such that ∃!γ 00 ∈ Γ, n ∈ Z such that γ 0 7→ γ 00 ?n ends with a γ 0 ∈ ΓN such that ∀n ∈ Z. ∃γ 00 ∈ Γ. γ 0 7→ γ 00. ∼ (Σ̂ + (Z × Ω) + [Z → Ω]) and F ∈ [Γ → Ω] is the least solution of If Ω = ⊥ ⊥, ιterm σ 0, 0 F γ = ιabort σ , 00 i, ι hn, F γ out ι (λn ∈ Z. F γ ), n in then [[c]]comm σ = F hc, σi. if γ ↑ if γ → 7 ∗ σ0 if γ 7→∗ habort, σ 0i !n if ∃γ 0. γ 7→∗ γ 0 and γ 0 7→ γ 00 ?n if ∃γ 0 ∈ Γ. ∀n ∈ Z. γ 7→∗ γ 0 and γ 0 7→ γn