Semantics with Applications 2b. Structural Operational Semantics

Similar documents
Notes on Natural Logic

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

In this lecture, we will use the semantics of our simple language of arithmetic expressions,

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

HW 1 Reminder. Principles of Programming Languages. Lets try another proof. Induction. Induction on Derivations. CSE 230: Winter 2007

Semantics and Verification of Software

2 Deduction in Sentential Logic

Principles of Program Analysis: Abstract Interpretation

Lecture 14: Basic Fixpoint Theorems (cont.)

Lecture 5: Tuesday, January 27, Peterson s Algorithm satisfies the No Starvation property (Theorem 1)

Proof Techniques for Operational Semantics. Questions? Why Bother? Mathematical Induction Well-Founded Induction Structural Induction

The Real Numbers. Here we show one way to explicitly construct the real numbers R. First we need a definition.

Lecture Notes on Bidirectional Type Checking

5 Deduction in First-Order Logic

Development Separation in Lambda-Calculus

Algorithmic Game Theory and Applications. Lecture 11: Games of Perfect Information

CS 4110 Programming Languages and Logics Lecture #2: Introduction to Semantics. 1 Arithmetic Expressions

Proof Techniques for Operational Semantics

TABLEAU-BASED DECISION PROCEDURES FOR HYBRID LOGIC

Programming Languages

THE TRAVELING SALESMAN PROBLEM FOR MOVING POINTS ON A LINE

10.1 Elimination of strictly dominated strategies

Maximum Contiguous Subsequences

Lecture 2: The Simple Story of 2-SAT

Sy D. Friedman. August 28, 2001

0.1 Equivalence between Natural Deduction and Axiomatic Systems

CS792 Notes Henkin Models, Soundness and Completeness

Notes on the symmetric group

Outline Introduction Game Representations Reductions Solution Concepts. Game Theory. Enrico Franchi. May 19, 2010

Proof Techniques for Operational Semantics

A CATEGORICAL FOUNDATION FOR STRUCTURED REVERSIBLE FLOWCHART LANGUAGES: SOUNDNESS AND ADEQUACY

SAT and DPLL. Introduction. Preliminaries. Normal forms DPLL. Complexity. Espen H. Lian. DPLL Implementation. Bibliography.

Decidability and Recursive Languages

Computing Unsatisfiable k-sat Instances with Few Occurrences per Variable

CIS 500 Software Foundations Fall October. CIS 500, 6 October 1

SAT and DPLL. Espen H. Lian. May 4, Ifi, UiO. Espen H. Lian (Ifi, UiO) SAT and DPLL May 4, / 59

GUESSING MODELS IMPLY THE SINGULAR CARDINAL HYPOTHESIS arxiv: v1 [math.lo] 25 Mar 2019

Principles of Program Analysis: Algorithms

arxiv: v2 [math.lo] 13 Feb 2014

Tug of War Game. William Gasarch and Nick Sovich and Paul Zimand. October 6, Abstract

R-automata. 1 Introduction. Parosh Aziz Abdulla, Pavel Krcal, and Wang Yi

Lecture 4: Divide and Conquer

Handout 4: Deterministic Systems and the Shortest Path Problem

TR : Knowledge-Based Rational Decisions

Arborescent Architecture for Decentralized Supervisory Control of Discrete Event Systems

Microeconomics of Banking: Lecture 5

arxiv: v1 [math.lo] 24 Feb 2014

Threshold logic proof systems

4: SINGLE-PERIOD MARKET MODELS

Computing Unsatisfiable k-sat Instances with Few Occurrences per Variable

Sublinear Time Algorithms Oct 19, Lecture 1

Covering properties of derived models

Operational Semantics

Sequential Decision Making

On Existence of Equilibria. Bayesian Allocation-Mechanisms

TEST 1 SOLUTIONS MATH 1002

Copyright 1973, by the author(s). All rights reserved.

Approximate Revenue Maximization with Multiple Items

4 Martingales in Discrete-Time

The Traveling Salesman Problem. Time Complexity under Nondeterminism. A Nondeterministic Algorithm for tsp (d)

Level by Level Inequivalence, Strong Compactness, and GCH

Stochastic Games and Bayesian Games

UPWARD STABILITY TRANSFER FOR TAME ABSTRACT ELEMENTARY CLASSES

Right-cancellability of a family of operations on binary trees

Lecture 14: Examples of Martingales and Azuma s Inequality. Concentration

Lecture 7: Bayesian approach to MAB - Gittins index

Generalising the weak compactness of ω

Game Theory. Lecture Notes By Y. Narahari. Department of Computer Science and Automation Indian Institute of Science Bangalore, India August 2012

5.7 Probability Distributions and Variance

A Formally Verified Interpreter for a Shell-like Programming Language

Chapter 7: SAMPLING DISTRIBUTIONS & POINT ESTIMATION OF PARAMETERS

Supporting Information

Brief Notes on the Category Theoretic Semantics of Simply Typed Lambda Calculus

Yao s Minimax Principle

ECON385: A note on the Permanent Income Hypothesis (PIH). In this note, we will try to understand the permanent income hypothesis (PIH).

Unary PCF is Decidable

Strongly compact Magidor forcing.

Forecast Horizons for Production Planning with Stochastic Demand

Game theory for. Leonardo Badia.

3 The Model Existence Theorem

A Translation of Intersection and Union Types

Essays on Some Combinatorial Optimization Problems with Interval Data

A semantics for concurrent permission logic. Stephen Brookes CMU

Strong normalisation and the typed lambda calculus

Stochastic Games and Bayesian Games

Lecture 6. 1 Polynomial-time algorithms for the global min-cut problem

A Semantic Framework for Program Debugging

EXTENSIVE AND NORMAL FORM GAMES

The Value of Information in Central-Place Foraging. Research Report

Introduction to Greedy Algorithms: Huffman Codes

3 Arbitrage pricing theory in discrete time.

Bounds on coloring numbers

Full Abstraction for Nominal General References

Virtual Demand and Stable Mechanisms

On the Number of Permutations Avoiding a Given Pattern

Lecture Notes on Type Checking

CS 4110 Programming Languages & Logics. Lecture 2 Introduction to Semantics

Extender based forcings, fresh sets and Aronszajn trees

LECTURE 2: MULTIPERIOD MODELS AND TREES

Transcription:

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