Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The Formal Semantics of Programming Languages Types and Programming Languages Benjamin C. Pierce
Outline Course requirements What is semantics Who needs semantics Forms of semantics Tentative Plan Trace semantics Introduction to operational semantics
Course Requirements Prerequisites –Compiler Course –Basic set theory and logic A theoretical course –Forms of induction –Domain theory –No algorithms Grade –Course Notes 10% –Assignments 60% Mostly theoretical with some programming –Home exam 30%
Modern Programming Languages Imperative –PL/1 –Pascal –C Object Oriented –C++ –Java –C# Functional –Scheme –ML –Ocaml –F# –Haskel Logic –Prolog
Programming Languages Syntax –Which string is a legal program? –Usually defined using context free grammar+ contextual constraints Semantics –What does a program mean? –What is the output of the program on a given run? –When does a runtime error occur? –A formal definition
Benefits of Formal Semantics Programming language design –hard-to-define= hard-to-implement=hard-to-use –Avoid design mistakes Programming language implementation –Compiler Correctness Correctness of program optimizations Design of Static Analysis Programming language understanding Program correctness –Type checking Program equivalence Automatic generation of interpreter Techniques used in software engineering
Desired Features of PL Semantics Tractable –as simple as possible without losing the ability to express behavior accurately Abstract –uncluttered by irrelevant detail Computational –an accurate abstraction from runtime behavior Compositional –The meaning of compound language construct is defined using the meaning of subconstructs –Supports modular reasoning
Alternative Formal Semantics Operational Semantics [Plotkin, Kahn] –The meaning of the program is described “operationally” –Trace based Semantics –Structural Operational Semantics –Natural Semantics Denotational Semantics [Strachey, Scott] –The meaning of the program is an input/output relation Axiomatic Semantics [Floyd, Hoare] –The meaning of the program is observed properties –Proof rules to show that the program is correct Complement each other
Tentative Plan A simple programming language IMP –Natural Semantics of IMP –Structural operational Semantics of IMP –Denotational Semantics of IMP Axiomatic Semantics –IMP –Non-Determinism and Parallelism –Rely Guarantee Axiomatic Semantics –Separation Logic Type inference/checking
IMP: A Simple Imperative Language numbers N –Positive and negative numbers –n, m N truth values T={true, false} locations Loc –X, Y Loc arithmetic Aexp –a Aexp boolean expressions Bexp –b Bexp commands Com –c Com
(3+5) 5+ 3 Abstract Syntax for IMP Aexp –a ::= n | X | a 0 + a 1 | a 0 – a 1 | a 0 a 1 Bexp –b ::= true | false | a 0 = a 1 | a 0 a 1 | b | b 0 b 1 | b 0 b 1 Com –c ::= skip | X := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c 2+3 4-5 (2+(3 4))-5 ((2+3) 4))-5
Example Program Y := 1; while (X=1) do Y := Y * X; X := X - 1
But what about semantics
Trace Based Semantics For every program P define a set potential states (P) Let be the set of finite and infinite traces over – = (P) * (P) The meaning of P is a set of maximal traces P
Example Program 1: while 2:(X>0) do 3:X := X – 1 4: [pc 1, x 2] [pc 2, x 2] [pc 3, x 2] [pc 2, x 1] [pc 3, x 1] [pc 2, x 0] [pc 4, x 0] [pc 1, x - 7] [pc 2, x - 7] [pc 4, x - 7]..
Example Program 1: while 2:(true) do 3: skip 4: [pc 1, x 2] [pc 2, x 2] [pc 3, x 2] [pc 2, x 2] [pc 3, x 2] [pc 2, x 2] [pc 3, x 2] ..
Limitations of trace based semantics The program counter is an implementation detail Equivalent programs do not necessarily have the same set of traces Hard to define semantics by induction on the syntax Hard to prove properties of the programming language
Chapter 2 Introduction to Operational Semantics
Expression Evaluation States –Mapping locations to values – - The set of states : Loc N (X)= X=value of X in = [ X 5, Y 7] –The value of X is 5 –The value of Y is 7 –The value of Z is undefined – For a Exp, , n N, n –a is evaluated in to n
Evaluating (a 0 + a 1 ) at Evaluate a 0 to get a number n 0 at Evaluate a 1 to get a number n 1 at Add n 0 and n 1
Expression Evaluation Rules Numbers – n Locations – (X) Sums Subtractions Products Axioms
Derivations A rule instance –Instantiating meta variables with corresponding values
Derivation (Tree) Axioms in the leafs Rule instances at internal nodes
Computing a derivation We write n when there exists a derivation tree whose root is n Can be computed in a top-down manner At every node try all derivations “in parallel”
Recap Operational Semantics –The rules can be implemented easily –Define interpreter Natural semantics
Equivalence of IMP expressions a0 a1a0 a1 iff
Boolean Expression Evaluation Rules true false
Boolean Expression Evaluation Rules(cont)
Equivalence of Boolean expressions b0 b1b0 b1 iff
Extensions Shortcut evaluation of Boolean expressions “Parallel” evaluation of Boolean expressions Other data types
The execution of commands ’ –c terminates on in a final state ’ Initial state 0 – 0 (X)=0 for all X Handling assignments ’ [5/X]
Rules for commands Sequencing: Conditionals: Atomic
Rules for commands (while)
Example Program Y := 1; while (X=1) do Y := Y * X; X := X - 1
Equivalence of commands c0 c1c0 c1 iff
Proposition 2.8 while b do c if b then (c; while b do c) else skip
Small Step Operational Semantics The natural semantics defines evaluation in large steps –Abstracts “computation time” It is possible to define a small step operational semantics – 1 “one” step of executing a in a state yields a’ in a state ’
SOS for Additions
SOS Rules for commands 1 Sequencing: Atomic
SOS Rules for commands Conditionals: 1 1 true 1 1 false
SOS rules for while 1
Summary Operational semantics enables to naturally express program behavior Can handle –Non determinism –Concurrency –Procedures –Object oriented –Pointers and dynamically allocated structures But remains very closed to the implementation –Two programs which compute the same functions are not necessarily equivalent