
Operational Semantics 1 / 110 A programming language I Syntax I Semantics 2 / 110 Why formal semantics Formal semantics gives an unambiguous definition of what a program written in the language should do. I Understand the subtleties of the language I Offer a formal reference and a correctness definition for implementors of tools (parsers, compilers, interpreters, debuggers, etc) I Prove global properties of any program written in the language I Verify programs against formal specifications I Prove two different programs are equivalent/non-equivalent I From a computer readable version of the semantics, an interpreter can be automatically generated (full compiler generation is not yet feasible) I ... 3 / 110 Formal semantics of a programming language I Operational semantics I Denotational semantics I Axiomatic semantics 4 / 110 Operational semantics Operational semantics defines program executions: I Sequence of steps, formulated as transitions of an abstract machine Configurations of the abstract machine include: I Expression/statement being evaluated/executed I States: abstract description of registers, memory and other data structures involved in computation 5 / 110 Figure taken from Franklyn Turbak and David Gifford’s Design Concepts in Programming Languages. 6 / 110 Different approaches of operational semantics I Small-step semantics: Describe each single step of the execution I Big-step semantics: Describe the overall result of the execution We will explain both in detail by examples. 7 / 110 After this class... You should be able to: I write down the evaluation/execution steps, if given the operational semantics rules I formulate the operational semantics rule, if given the informal meaning of an expression/statement 8 / 110 Outline Syntax of a Simple Imperative Language Operational semantics Small-step operational semantics Structural operational semantics (SOS) Extensions: going wrong, local variable declaration, heap Contextual semantics (a.k.a. reduction semantics) Big-step operational semantics 9 / 110 Outline Syntax of a Simple Imperative Language Operational semantics Small-step operational semantics Structural operational semantics (SOS) Extensions: going wrong, local variable declaration, heap Contextual semantics (a.k.a. reduction semantics) Big-step operational semantics 10 / 110 Syntax (IntExp) e ::= n j x j e + e j e − e j ::: (BoolExp) b ::= true j false j e = e j e < e j e > e j :b j b ^ b j b _ b j ::: (Comm) c ::= skip j x := e j c ; c j if b then c else c j while b do c 11 / 110 Syntax (IntExp) e ::= n j x j e + e j e − e j ::: Here n ranges over the numerals 0, 1, 2,.... We distinguish between numerals, written n, 0, 1, 2, . , and the natural numbers, written n, 0, 1, 2, . The natural numbers are the normal numbers that we use in everyday life, while the numerals are just syntax for describing these numbers. We write bnc to denote the meaning of n. We assume that bnc = n, b0c = 0, b1c = 1, . The distinction is subtle, but important, because it is one manifestation of the difference between syntax and semantics. 12 / 110 Syntax Syntax Semantics b·c (IntExp) e ::= n n j x j e + e + j e − e − j ::: (BoolExp) b ::= true true j false false j e = e = j e < e < j :b : j b ^ b ^ j b _ b _ j ::: 13 / 110 Outline Syntax of a Simple Imperative Language Operational semantics Small-step operational semantics Structural operational semantics (SOS) Extensions: going wrong, local variable declaration, heap Contextual semantics (a.k.a. reduction semantics) Big-step operational semantics 14 / 110 States To evaluate variables or update variables, we need to know the current state. (State) σ 2 Var ! Values What are Values? n or n? Both are fine. Here we think Values are natural numbers, boolean values, etc. 15 / 110 Recall ( def σ(z) if z , x σfx { ng = λz: n if z = x For example, σ1fy { 7g = fx { 2; y { 7; a { 10g. Operational semantics will be defined using configurations of the forms (e; σ), (b; σ) and (c; σ). States (State) σ 2 Var ! Values For example, σ1 = f(x; 2); (y; 3); (a; 10)g, which we will write as fx { 2; y { 3; a { 10g. (For simplicity, here we assume that a state always contain all the variables that may be used in a program.) 16 / 110 Operational semantics will be defined using configurations of the forms (e; σ), (b; σ) and (c; σ). States (State) σ 2 Var ! Values For example, σ1 = f(x; 2); (y; 3); (a; 10)g, which we will write as fx { 2; y { 3; a { 10g. (For simplicity, here we assume that a state always contain all the variables that may be used in a program.) Recall ( def σ(z) if z , x σfx { ng = λz: n if z = x For example, σ1fy { 7g = fx { 2; y { 7; a { 10g. 17 / 110 States (State) σ 2 Var ! Values For example, σ1 = f(x; 2); (y; 3); (a; 10)g, which we will write as fx { 2; y { 3; a { 10g. (For simplicity, here we assume that a state always contain all the variables that may be used in a program.) Recall ( def σ(z) if z , x σfx { ng = λz: n if z = x For example, σ1fy { 7g = fx { 2; y { 7; a { 10g. Operational semantics will be defined using configurations of the forms (e; σ), (b; σ) and (c; σ). 18 / 110 Small-step structural operational semantics (SOS) Systematic definition of operational semantics: I The program syntax is inductively-defined I So we can also define the semantics of a program in terms of the semantics of its parts I “Structural”: syntax oriented and inductive Examples: I The state transition for e1 + e2 is described using the transition for e1 and the transition for e2. I The state transition for c1 ; c2 is described using the transition for c1 and the transition for c2. 19 / 110 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) bn1c b+c bn2c = bnc (n1 + n2; σ) −! (n; σ) Example: ((10 + 12) + (13 + 20); σ) Small-step SOS for expression evaluation Recall (IntExp) e ::= n j x j e + e j e − e j ::: Below we define (e; σ) −! (e0; σ0). We’ll start from addition. 20 / 110 bn1c b+c bn2c = bnc (n1 + n2; σ) −! (n; σ) Example: ((10 + 12) + (13 + 20); σ) Small-step SOS for expression evaluation Recall (IntExp) e ::= n j x j e + e j e − e j ::: Below we define (e; σ) −! (e0; σ0). We’ll start from addition. 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) 21 / 110 Example: ((10 + 12) + (13 + 20); σ) Small-step SOS for expression evaluation Recall (IntExp) e ::= n j x j e + e j e − e j ::: Below we define (e; σ) −! (e0; σ0). We’ll start from addition. 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) bn1c b+c bn2c = bnc (n1 + n2; σ) −! (n; σ) 22 / 110 Small-step SOS for expression evaluation Recall (IntExp) e ::= n j x j e + e j e − e j ::: Below we define (e; σ) −! (e0; σ0). We’ll start from addition. 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) bn1c b+c bn2c = bnc (n1 + n2; σ) −! (n; σ) Example: ((10 + 12) + (13 + 20); σ) 23 / 110 Small-step SOS for expression evaluation It is important to note that the order of evaluation is fixed by the small-step semantics. 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) It is different from the following. 0 0 (e2; σ) −! (e2; σ) (e1; σ) −! (e1; σ) 0 (e1 + e2; σ) −! (e1 + e2; σ) (e1 + n; σ) −! (e1 + n; σ) Next: subtraction. 24 / 110 Small-step SOS for expression evaluation Transitions for subtraction: 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) − 0 − − − 0 (e1 e2; σ) −! (e1 e2; σ) (n e2; σ) −! (n e2; σ) bn1c b−c bn2c = bnc (n1 − n2; σ) −! (n; σ) Next: variables. 25 / 110 Small-step SOS for expression evaluation Recall (State) σ 2 Var ! Values Transitions for evaluating variables: σ(x) = bnc (x; σ) −! (n; σ) 26 / 110 Summary: small-step SOS for expression evaluation 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 + e2; σ) −! (e1 + e2; σ) (n + e2; σ) −! (n + e2; σ) 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) − 0 − − − 0 (e1 e2; σ) −! (e1 e2; σ) (n e2; σ) −! (n e2; σ) bn1c b+c bn2c = bnc bn1c b−c bn2c = bnc σ(x) = bnc (n1 + n2; σ) −! (n; σ) (n1 − n2; σ) −! (n; σ) (x; σ) −! (n; σ) Example: Suppose σ(x) = 10 and σ(y) = 42. (x + y; σ) −! (10 + y; σ) −! (10 + 42; σ) −! (52; σ) 27 / 110 0 0 (e1; σ) −! (e1; σ) (e2; σ) −! (e2; σ) 0 0 (e1 = e2; σ) −! (e1 = e2; σ) (n = e2; σ) −! (n = e2; σ) bn1c b=c bn2c :(bn1c b=c bn2c) (n1 = n2; σ) −! (true; σ) (n1 = n2; σ) −! (false; σ) Next: negation. Small-step SOS for boolean expressions Recall (BoolExp) b ::= true j false j e = e j e < e j e > e j :b j b ^ b j b _ b j ::: We overload the symbol −!.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages110 Page
-
File Size-