Semantics with Applications 2b. Structural Operational Semantics Hanne Riis Nielson, Flemming Nielson (thanks to Henrik Pilegaard) [SwA] Hanne Riis Nielson, Flemming Nielson Semantics with Applications: An Appetizer Springer, 2007 1 / 34
Structural Operational Semantics Reading material: Section 2.2 of SwA 2 / 34
Two Approaches to Operational Semantics In an operational semantics we are concerned with how programs are executed Two approaches to operational semantics: Natural semantics (NS): (or big-step semantics ) Given a statement and a state in which it has to be executed, what is the resulting state (if it exists) Structural operational semantics (SOS): (or small-step semantics ) Given a statement and a state in which it has to be executed, what is the next step of the computation (if it exists) 3 / 34
Specifying the Structural Operational Semantics of While The emphasis is on the individual steps of the execution The transition relation has the form S, s γ where γ has one of the following forms: γ = S, s Then the execution is not yet completed; the remaining computation is expressed by S, s γ = s Then the execution has terminated with final state s SOS : z := 1; x := 5; y := 2, s x := 5; y := 2, s[z 1] SOS : z := 1, s s[z 1] The configuration S, s is called stuck if there is no γ such that S, s γ 4 / 34
Specifying the Structural Operational Semantics of While The emphasis is on the individual steps of the execution The transition relation has the form S, s γ where γ has one of the following forms: γ = S, s Then the execution is not yet completed; the remaining computation is expressed by S, s γ = s Then the execution has terminated with final state s SOS : z := 1; x := 5; y := 2, s x := 5; y := 2, s[z 1] NS : z := 1; x := 5; y := 2, s ((s[z 1])[x 5])[y 2] SOS : z := 1, s s[z 1] The configuration S, s is called stuck if there is no γ such that S, s γ 5 / 34
Specifying the Structural Operational Semantics of While The emphasis is on the individual steps of the execution The transition relation has the form S, s γ where γ has one of the following forms: γ = S, s Then the execution is not yet completed; the remaining computation is expressed by S, s γ = s Then the execution has terminated with final state s SOS : z := 1; x := 5; y := 2, s x := 5; y := 2, s[z 1] NS : z := 1; x := 5; y := 2, s ((s[z 1])[x 5])[y 2] SOS : z := 1, s s[z 1] NS : z := 1, s s[z 1] The configuration S, s is called stuck if there is no γ such that S, s γ 6 / 34
Structural Operational Semantics for While 7 / 34
The Axioms for Assignment and Skip These axioms do not differ from the ones in Natural Semantics at all: both assignment and skip are fully executed in one step The axiom [ass sos ] says: the first step of executing x := a in state s is the state s updated such that x gets the value of (evaluating) a The axiom [skip sos ] says: the first step of executing skip in state s is simply the state s 8 / 34
The Rule for Composition The first step of executing S 1 ; S 2 is the first step of executing S 1 Two possible outcomes: [comp 1 sos] The execution of S 1 is not yet completed [comp 2 sos] The execution of S 1 is completed 9 / 34
Derivation Sequences A derivation sequence of a statement S starting in state s is either a finite sequence γ0, γ 1,..., γ k γ 0 γ 1... γ k and γ k is either final or stuck an infinite sequence γ0, γ 1, γ 2,... γ 0 γ 1 γ 2... where γ 0 = S, s and γ i γ i+1 for 0 i(< k) We write γ 0 i γ i to indicate i derivation steps We write γ 0 γ i to indicate a finite number of derivation steps 10 / 34
The Rules for the if-construct The first step is to determine the outcome of the test and to select the appropriate branch Two rules [if tt sos] and [if ff sos], depending on the value of b Alternative: The first step is the first step of the branch determined by the outcome of the test S 1, s s if b then S 1 else S 2, s s if B[[b]]s = tt S 1, s S 1, s if b then S 1 else S 2, s S 1, s and two similar rules for the case B[[b]]s = ff if B[[b]]s = tt 11 / 34
The Rules for the while-construct [while sos ] The first step is to unroll the loop: only one rule Recall: while b do S and if b then (S; while b do S) else skip are semantically equivalent in Natural Semantics Alternative: The first step is to determine the outcome of the test and thereby to decide whether or not to unroll the loop while b do S, s S; while b do S, s while b do S, s s if B[[b]]s = tt if B[[b]]s = ff 12 / 34
Try It Out 1 Notation: s ij x = i and s ij y = j, e.g. s 30 = [x 3, y 0] Construct a derivation sequence for y := 1; while (x = 1) do (y := y x; x := x 1), s 30 s 13 / 34
complete it 14 / 34
Try It Out 2 Let S denote n := 2; sum := 0; i := 1; while i n do (sum := sum + i; i := i + 1) Does the following hold? S, [n 1, sum 15, i 26] [n 6, sum 3, i 7] 15 / 34
Properties of the Semantics 16 / 34
Concepts for NS and SOS The following concepts can be defined both for Natural Semantics and Structural Operational Semantics Termination Looping Semantic equivalence Determinism The formalisations differ however we will compare them in the following 17 / 34
Natural Semantics: Termination The execution of S from state s terminates if and only if there is a state s such that S, s s. Structural Operational Semantics: The execution of S from state s terminates if and only if there is a finite derivation sequence starting with S, s, i.e. and γ k is either final or stuck S, s γ 1... γ k 18 / 34
Natural Semantics: Termination The execution of S from state s terminates if and only if there is a state s such that S, s s. Structural Operational Semantics: The execution of S from state s terminates if and only if there is a finite derivation sequence starting with S, s, i.e. and γ k is either final or stuck S, s γ 1... γ k Note: It is not required that γ k has the form s (this would be called successful termination), it can also be stuck. However, for the While language there are no stuck configurations! But we will look at some extensions of While where programs can get stuck. 19 / 34
Looping Natural Semantics: We say that the execution of S from state s loops if and only if there is no state s such that S, s s. Structural Operational Semantics: We say that the execution of S from state s loops if and only if there is an infinite derivation sequence starting with S, s, i.e. S, s γ 1 γ 2... 20 / 34
Semantic Equivalence Natural Semantics: Two statements S 1 and S 2 are semantically equivalent if for all states s and s S 1, s s if and only if S 2, s s Structural Operational Semantics: Two statements S 1 and S 2 are semantically equivalent if for all states s: S 1, s γ if and only if S 2, s γ, whenever γ is either stuck or terminal there is an infinite derivation sequence starting with S 1, s if and only if there is one starting in S 2, s 21 / 34
Determinism Natural Semantics: The semantics is deterministic if for all statements S and states s, s, and s we have that S, s s and S, s s imply s = s Structural Operational Semantics: The semantics is deterministic if for all S and s, γ, and γ we have that S, s γ and S, s γ imply γ = γ 22 / 34
Induction on the Length of Derivation Sequences For Structural Operational Semantics it is often useful to conduct proofs by the length of derivation sequences Prove that the property holds for all derivation sequences of length 0 Prove that the property holds for all other derivation sequences Assume that the property holds for all derivation sequences of length at most k (this is called the induction hypothesis) Prove that it holds for derivation sequences of length k+1 23 / 34
Using the Proof Principle To illustrate the proof principle we prove the following lemma Intuitively, the lemma says that a derivation sequence for a composition S 1 ; S 2 can be split in two parts which correspond to S 1 and S 2 Lemma (2.19) If S 1 ; S 2, s k s then there exists a state s and natural numbers k 1 and k 2 such that where k = k 1 + k 2. S 1, s k 1 s and S 2, s k 2 s 24 / 34
The Semantic Function for Statements As we did in the case for Natural Semantics, the meaning of statements can be summarised as a partial function from State to State S sos : Stm (State State) Definition: { s if S, s S sos [[S]]s = s undef otherwise 25 / 34
Summary Structural Operational Semantics (SOS) Proof technique: induction on the length of derivation sequences Exercise Class Exercises 2.16, 2.17, 2.20, 2.21, 2.24, 2.25 from SwA. 26 / 34
Equivalence of NS and SOS Reading material: Section 2.3 of SwA 27 / 34
Which Approach to Choose? Sometimes it does not really matter One can formally prove that the semantic formulations are equivalent Choose the one you like best Sometimes one approach is easier to work with than another Choose the easy one Sometimes one approach may not work at all Avoid it 28 / 34
Approaches for the While Language For the pure language: NS and SOS are equivalent For extensions of the while language with: Non-determinism: possible in both NS and SOS (but they are no longer equivalent!) Parallelism: not possible in NS; no problem in SOS For reasoning about tools (compilers, program analysers, etc.): both NS and SOS are candidates 29 / 34
The Equivalence Theorem Recall: Semantic functions { s if S, s s S ns [[S]]s = undef otherwise { s if S, s S sos [[S]]s = s undef otherwise Equivalence result: Theorem (2.26) For every statement S of While, we have S ns [[S]] = S sos [[S]]. It suffices to show: S, s s if and only if S, s s 30 / 34
Structure of the Proof The main theorem: is split up in two lemmas: S, s s if and only if S, s s Lemma (2.27) For all S, s, s : S, s s implies S, s s Lemma (2.28) For all S, s, s, and natural number k: S, s k s implies S, s s 31 / 34
Lemma (2.27) Structure of the Proof For all S, s, s : S, s s implies S, s s Proof by Induction on the Shape of Derivation Trees Case by case we consider all the possible ways of constructing an inference tree for S, s s, and we show how to construct a derivation sequence S, s s In the proof we are using an auxiliary lemma (from the exercise class) Intuitively, it says that the execution of S 1 is not influenced by the statement following it: Lemma (2.21) If S 1, s k s then S 1 ; S 2, s k S 2, s. 32 / 34
Structure of the Proof Lemma (2.28) For all S, s, s, and natural number k: S, s k s implies S, s s Proof by Induction on the Length of Derivation Sequences Proof uses two auxiliary lemmas: Lemma (2.19) If S 1 ; S 2, s k s then there exists a state s and natural numbers k 1 and k 2 such that S 1, s k 1 s and S 2, s k 2 s where k = k 1 + k 2. Lemma (2.5) if b then (S; while b do S) else skip, s s implies while b do S, s s 33 / 34
Summary Recap SOS Semantics Proof technique: inductions on the length of Derivation Sequences Equivalence of NS and SOS Exercise Class Exercise 2.29 from SwA. 34 / 34