Static Vs. Dynamic Semantics (1) Static Vs
Total Page:16
File Type:pdf, Size:1020Kb
Why Does PL Semantics Matter? (1) Why Does PL Semantics Matter? (2) • Documentation • Language Design - Programmers (“What does X mean? Did - Semantic simplicity is a good guiding G54FOP: Lecture 3 the compiler get it right?”) Programming Language Semantics: principle - Implementers (“How to implement X?”) Introduction - Ensure desirable meta-theoretical • Formal Reasoning properties hold (like “well-typed programs Henrik Nilsson - Proofs about programs do not go wrong”) University of Nottingham, UK - Proofs about programming languages • Education (E.g. “Well-typed programs do not go - Learning new languages wrong”) - Comparing languages - Proofs about tools (E.g. compiler correctness) • Research G54FOP: Lecture 3 – p.1/21 G54FOP: Lecture 3 – p.2/21 G54FOP: Lecture 3 – p.3/21 Static vs. Dynamic Semantics (1) Static vs. Dynamic Semantics (2) Styles of Semantics (1) Main examples: • Static Semantics: “compile-time” meaning Distinction between static and dynamic • Operational Semantics: Meaning given by semantics not always clear cut. E.g. - Scope rules Abstract Machine, often a Transition Function - Type rules • Multi-staged languages (“more than one mapping a state to a “more evaluated” state. Example: the meaning of 1+2 is an integer run-time”) Kinds: value (its type is Integer) • Dependently typed languages (computation - small-step semantics: each step is • Dynamic Semantics: “run-time” meaning at the type level) atomic; more machine like - Exactly what value does a term evaluate to? - structural operational semantics (SOS): - What are the effects of a computation? compound, but still simple, steps Example: the meaning of 1+2 is the integer 3. - big-step or natural semantics: Single, compound step evaluates term to final value. G54FOP: Lecture 3 – p.4/21 G54FOP: Lecture 3 – p.5/21 G54FOP: Lecture 3 – p.6/21 Styles of Semantics (2) Example: Small Step Semantics (1) Example: Small-Step Semantics (2) • small-step semantics: Idea of the machine. Given state t,¯ v : Consider a simple machine for evaluating ( ¯) • ¯ t → t(1) → t(2) → . → v arithmetic expressions. Its state is denoted If t is empty, we’re done; whatever is on the value stack v¯ is the result. • (t,¯ v¯) structural operational semantics: • If head of t¯ is a value, push it onto v¯. ∇ ∇ ∇ ∇ where • ¯ t → t(1) → t(2) → . → v If head of t is an expression, prepend the • t¯ is a sequence of expressions (including individual subexpressions and the operator to • big-step or natural semantics: values) and operators the tail of t¯. ∇ • v¯ is a stack (sequence) of values • If head of t¯ is an operator, apply it to appropriate t → v number of arguments on top of the value stack and replace them with the result. where ∇ suggests a proof (tree) justifying the step. G54FOP: Lecture 3 – p.7/21 G54FOP: Lecture 3 – p.8/21 G54FOP: Lecture 3 – p.9/21 Example: Small-Step Semantics (3) Example: Small-Step Semantics (4) Styles of Semantics (3) An evaluation might proceed as follows: Note: • Denotational Semantics: More abstract (1+(2-3), ) → (2-3, 1, +, ) view: • Each step describes a small, essentially → (3, 2, -, 1, +, ) - meaning of a term is a mathematical object mechanical, syntactic transformation of the (like a number (e.g. N or Z) or function → (2, -, 1, +, 3) machine state; i.e., a very operational view of (e.g. Z → Z); → (-, 1, +, 2, 3) computation. - an interpretation function maps terms → (1, +, -1) • Our description was informal. We will discuss ((abstract) syntax) to their meaning → (+, 1, -1) at length how to formalise operational (semantics). → (, 0) semantics in a mathematically precise way, typically using inference rules. Exercise: Evaluate ((5-3)*(1+2), ) G54FOP: Lecture 3 – p.10/21 G54FOP: Lecture 3 – p.11/21 G54FOP: Lecture 3 – p.12/21 Example: Denotational Semantics (1) Example: Denotational Semantics (2) Example: Denotational Semantics (3) the denotational semantics (the interpretation Given the abstract syntax for expressions: function mapping the (abstract) syntax of an Exercise. Given: e → expressions: expression to its meaning) might be specified as: [[·]] : e → Z | Z integer literals [[·]] : e → Z [[n]] = n | e + e addition [[n]] = n [[e1 + e2]] = [[e1]]+ [[e2]] | e - e subtraction [[e1 + e2]] = [[e1]]+ [[e2]] [[e1 - e2]] = [[e1]] − [[e2]] | e * e multiplication [[e1 - e2]] = [[e1]] − [[e2]] [[e1 * e2]] = [[e1]] × [[e2]] [[e1 * e2]] = [[e1]] × [[e2]] Calculate the denotation (meaning) of: Not vacuous: e.g., note the difference between 1 + (2 - 3) +, syntax, and +, the ordinary function plus. G54FOP: Lecture 3 – p.13/21 G54FOP: Lecture 3 – p.14/21 G54FOP: Lecture 3 – p.15/21 Styles of Semantics (4) Example: Axiomatic Semantics (1) Example: Axiomatic Semantics (2) • Axiomatic Semantics: More abstract still: The meaning of a command c is given by For example, the meaning of assignment is given by: - An operational or denotational semantics specifying a precondition and a postcondition: {P [v → e]} v := e {P } implies certain properties or laws. {PRE} c {POST } - An axiomatic semantics takes such laws as This says: For any predicate P whatsoever that the starting point: the laws defines the This says: If PRE holds for a program state, then holds in a program state when e is substituted for semantics and the meaning is just what executing c in that state will terminate and POST free occurrences of v, it is the case that P holds can be proved. will hold in the resulting state. in the state resulting after the assignment. - Closely related to Hoare logic. Note: Nothing is said about how execution is carried out, but the focus is on what its effect is. G54FOP: Lecture 3 – p.16/21 G54FOP: Lecture 3 – p.17/21 G54FOP: Lecture 3 – p.18/21 Example: Axiomatic Semantics (3) Example: Axiomatic Semantics (4) Example: Axiomatic Semantics (5) We can now prove e.g. Works for any predicate. Consider proving: Exercise. Prove: {x = 7} x := x+1 {x = 8} {x ≥ 0} x := x+1 {x > 0} {i = 6 ∧ j = 7} i := i * j {i = 42 ∧ j = 7} (often easier to work backwards from postcondition): {x > 0} postcondition {x = 8} postcondition {x + 1 > 0} substituting x + 1 for x {x +1=8} substituting x + 1 for x {x + 1 ≥ 1} n> 0 ≡ n ≥ 1 for any integer n {x = 8 − 1} arithmetic {x ≥ 1 − 1} arithmetic {x = 7} simplification yields precondition {x ≥ 0} simplification yields precondition G54FOP: Lecture 3 – p.19/21 G54FOP: Lecture 3 – p.20/21 G54FOP: Lecture 3 – p.21/21.