Formal Syntax and Semantics of Programming Languages

Similar documents
Formal Syntax and Semantics of Programming Languages

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Semantics with Applications 3. More on Operational Semantics

Formal Semantics of Programming Languages

Program Analysis: Lecture 02 Page 1 of 32

Application: Programming Language Semantics

Exercises on Semantics of Programming Languages

PROGRAM ANALYSIS & SYNTHESIS

The exercises and answer for programming language class

Big-step Operational Semantics (aka Natural Semantics)

Lecture 5 - Axiomatic semantics

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Induction and Semantics in Dafny

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Handout 9: Imperative Programs and State

More on Operational Semantics

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Equivalence of NTMs and TMs

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Semantics of COW. July Alex van Oostenrijk and Martijn van Beek

Programming Languages Third Edition

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Data Flow Analysis using Program Graphs

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Chapter 3. The While programming language

Self-stabilizing Spanning Tree

15 212: Principles of Programming. Some Notes on Induction

Lambda Calculus. Concepts in Programming Languages Recitation 6:

3.7 Denotational Semantics

Propositional Logic Formal Syntax and Semantics. Computability and Logic

Chapter 3. Describing Syntax and Semantics ISBN

6. Hoare Logic and Weakest Preconditions

Big-step Operational Semantics Revisited

arxiv: v1 [cs.pl] 10 May 2016

Pierce Ch. 3, 8, 11, 15. Type Systems

02157 Functional Programming Interpreters for two simple languages including exercises

Mosig M1 - PLSCD Written exam

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

Modal Logic: Implications for Design of a Language for Distributed Computation p.1/53

A Structural Operational Semantics for JavaScript

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

1. Abstract syntax: deep structure and binding. 2. Static semantics: typing rules. 3. Dynamic semantics: execution rules.

CSCC24 Functional Programming Scheme Part 2

2 Introduction to operational semantics

Introduction to Model Checking

COS 320. Compiling Techniques

Principles of Programming Languages

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

Programming Languages Fall 2014

Formal Systems and their Applications

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Calculating Correct Compilers

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

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Part II. Hoare Logic and Program Verification. Why specify programs? Specification and Verification. Code Verification. Why verify programs?

The Relatedness and Comparative Utility of Various Approaches to Operational Semantics

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Design

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

Distributed Systems Programming (F21DS1) Formal Verification

Propositional Logic. Part I

Proving Properties on Programs From the Coq Tutorial at ITP 2015

CIS 500 Software Foundations Fall October 2

The Rule of Constancy(Derived Frame Rule)

A Revisionist History of Denotational Semantics

Foundations of Computer Science Spring Mathematical Preliminaries

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.

The formal methods discussed in earlier chapters, particularly

CMSC 330: Organization of Programming Languages

Chapter 3 (part 3) Describing Syntax and Semantics

Denotational Semantics. Domain Theory

Meanings of syntax. Norman Ramsey Geoffrey Mainland. COMP 105 Programming Languages Tufts University. January 26, 2015

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Formal Specification and Verification

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic

A Structural Operational Semantics for JavaScript

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18

On the Adequacy of Program Dependence Graphs for Representing Programs

Software Engineering using Formal Methods

Chapter 3: Propositional Languages

Static analysis and all that

4/6/2011. Model Checking. Encoding test specifications. Model Checking. Encoding test specifications. Model Checking CS 4271

CSE 505: Concepts of Programming Languages

The fundamental principle of identity for strings is informally given as follows.

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

Verifying Safety Property of Lustre Programs: Temporal Induction

On the Correctness of the SIMT Execution Model of GPUs. Extended version of the author s ESOP 12 paper. Axel Habermaier and Alexander Knapp

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.1

A Simplified Abstract Syntax for the Dataflow Algebra. A. J. Cowling

A Semantics to Generate the Context-sensitive Synchronized Control-Flow Graph (extended)

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Qualifying Exam in Programming Languages and Compilers

Theory of Computer Science

Programming Languages Fall 2013

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

The SPIN Model Checker

Transcription:

Formal Syntax and Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html

axioms rules Natural Semantics for While [ass ns ] <x := a, s> s[x A a s] [skip ns ] <skip, s> s [comp ns ] <S 1, s> s, <S 2, s > s <S 1 ; S 2, s> s [if tt ns] <S 1, s> s <if b then S 1 else S 2, s> s if B b s=tt [if ff ns] <S 2, s> s <if b then S 1 else S 2, s> s if B b s=ff

Natural Semantics for While (More rules) [while ff ns] <while b do S, s> s if B b s=ff [while tt ns] <S, s> s, <while b do S, s > s <while b do S, s> s if B b s=tt

An Example Derivation Tree <(x :=x+1; y :=x+1) ; z := y), s0> s0[x 1][y 2][z 2] compns <x :=x+1; y :=x+1, s0> s0[x 1][y 2] <z :=y,s0[x 1][y 2]> s0[x 1][y 2][z 2] compns assns <x :=x+1; s0> s0[x 1] <y :=x+1, s0[x 1]> s0[x 1][y 2] assns assns

Top Down Evaluation of Derivation Trees Given a program S and an input state s Find an output state s such that <S, s> s Start with the root and repeatedly apply rules until the axioms are reached Inspect different alternatives in order In While s and the derivation tree is unique

Semantic Equivalence S 1 and S 2 are semantically equivalent if for all s and s <S 1, s> s if and only if <S 2, s> s Simple example while b do S is semantically equivalent to: if b then (S ; while b do S) else skip

Deterministic Semantics for While If <S, s> s 1 and <S, s> s 2 then s 1 =s 2 The proof uses induction on the shape of derivation trees Prove that the property holds for all simple derivation trees by showing it holds for axioms Prove that the property holds for all composite trees: For each rule assume that the property holds for its premises (induction hypothesis) and prove it holds for the conclusion of the rule

The Semantic Function S ns The meaning of a statement S is defined as a partial function from State to State S ns : Stm (State State) S ns S s = s if <S, s> s and otherwise S ns S s is undefined Examples S ns skip s =s S ns x :=1 s = s [x 1] S ns while true do skip s = undefined

Structural Operational Semantics Emphasizes the individual execution steps <S, i> If the first step of executing the statement S on an input state i leads to Two possibilities for = <S, s > = o The execution of S is not completed, S is the remaining computation which need to be performed on s The execution of S has terminated with a final state o is a stuck configuration when there are no transitions The meaning of a program P on an input state s is the set of final states that can be executed in arbitrary finite steps

axioms Structural Semantics for While [ass sos ] <x := a, s> s[x A a s] [skip sos ] <skip, s> s rules [comp 1 sos] <S 1, s> <S 1, s > <S 1 ; S 2, s> < S 1 ; S 2, s > [comp 2 sos] <S 1, s> s <S 1 ; S 2, s> < S 2, s >

Structural Semantics for While if construct [if tt sos] <if b then S 1 else S 2, s> <S 1, s> if B b s=tt [if ff os] <if b then S 1 else S 2, s> <S 2, s> if B b s=ff

Structural Semantics for While while construct [while sos ] <while b do S, s> <if b then (S; while b do S) else skip, s>

Example Let s 0 such that s 0 x = 5 and s 0 y = 7 S = (z:=x; x := y); y := z

Factorial Program Input state s such that s x = 3 y := 1; while (x=1) do (y := y * x; x := x - 1) <y :=1 ; W, s> <W, s[y 1]> <if (x =1) then skip else ((y := y * x ; x := x 1); W), s[y 1]> < ((y := y * x ; x := x 1); W), s[y 1]> <(x := x 1 ; W), s[y 3]> < W, s[y 3][x 2]> <if (x =1) then skip else ((y := y * x ; x := x 1); W), s[y 3][x 2]> < ((y := y * x ; x := x 1); W), s[y 3] [x 2] > <(x := x 1 ; W), s[y 6] [x 2] > < W, s[y 6][x 1]> <if (x =1) then skip else ((y := y * x ; x := x 1); W), s[y 6][x 1]> <skip, s[y 6][x 1]> s[y 6][x 1]

Finite Derivation Sequences finite derivation sequence starting at <S, i> 0, 1, 2, k such that 0 =<S, i> i i+1 k is either stuck configuration or a final state For each step there is a derivation tree 0 k k ink steps 0 * in finite number of steps

Infinite Derivation Sequences An infinite derivation sequence starting at <S, i> 0, 1, 2 such that 0 =<S, i> i i+1 Example S = while true do skip s 0 x = 0

Program Termination Given a statement S and input s S terminates on s if there exists a finite derivation sequence starting at <S, s> S terminates successfully on s if there exists a finite derivation sequence starting at <S, s> leading to a final state S loops on s if there exists an infinite derivation sequence starting at <S, s>

Properties of the Semantics S 1 and S 2 are semantically equivalent if: for all s and which is either final or stuck <S 1, s> * if and only if <S 2, s> * there is an infinite derivation sequence starting at <S 1, s> if and only if there is an infinite derivation sequence starting at <S 2, s> Deterministic If <S, s> * s 1 and <S, s> * s 2 then s 1 =s 2 The execution of S 1 ; S 2 on an input can be split into two parts: execute S 1 on s yielding a state s execute S 2 on s

Sequential Composition If <S 1 ; S 2, s> k s then there exists a state s and numbers k 1 and k 2 such that <S 1, s> k1 s <S 2, s > k2 s and k = k 1 + k 2 The proof uses induction on 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: Show that the property holds for sequences of length k+1 using the fact it holds on all sequences of length k (induction hypothesis)

The Semantic Function S sos The meaning of a statement S is defined as a partial function from State to State S sos : Stm (State State) S sos S s = s if <S, s> * s and otherwise S sos S s is undefined

An Equivalence Result For every statement S of the While language S nat S = S sos S

Extensions to While Abort statement (like C exit w/o return value) Non determinism Parallelism Local Variables Procedures Static Scope Dynamic scope

The While Programming Language with Abort Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S abort Abort terminates the execution No new rules are needed in natural and structural operational semantics Statements if x = 0 then abort else y := y / x skip abort while true do skip

Conclusion The natural semantics cannot distinguish between looping and abnormal termination (unless the states are modified) In the structural operational semantics looping is reflected by infinite derivations and abnormal termination is reflected by stuck configuration

The While Programming Language with Non-Determinism Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S S 1 or S 2 Either S 1 or S 2 is executed Example x := 1 or (x :=2 ; x := x+2)

The While Programming Language with Non-Determinism Natural Semantics [or 1 ns] <S 1, s> s <S 1 or S 2, s> s [or 2 ns] <S 2, s> s <S 1 or S 2, s> s

The While Programming Language with Non-Determinism Structural Semantics

The While Programming Language with Non-Determinism Examples x := 1 or (x :=2 ; x := x+2) (while true do skip) or (x :=2 ; x := x+2)

Conclusion In the natural semantics non-determinism will suppress looping if possible (mnemonic) In the structural operational semantics nondeterminism does suppress not termination configuration

The While Programming Language with Parallel Constructs Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S S 1 par S 2 All the interleaving of S 1 or S 2 are executed Example x := 1 par (x :=2 ; x := x+2)

The While Programming Language with Parallel Constructs Structural Semantics [par 1 sos] <S 1, s> <S 1, s > <S 1 par S 2, s> < S 1 par S 2, s > [par 2 sos] <S 1, s> s <S 1 par S 2, s> < S 2, s > [par 3 sos] <S 2, s> <S 2, s > <S 1 par S 2, s> < S 1 par S 2, s > [par 4 sos] <S 2, s> s <S 1 par S 2, s> < S 1, s >

The While Programming Language with Parallel Constructs Natural Semantics

Conclusion In the natural semantics immediate constituent is an atomic entity so we cannot express interleaving of computations In the structural operational semantics we concentrate on small steps so interleaving of computations can be easily expressed

The While Programming Language with local variables Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S begin D v S end D v ::= var x := a ; D v

Simple Example begin var y := 1; (var x := 2 ; begin var x := 3 ; y := x + y; end x := y + x ; ) end

Another Example while (y > 0) ( begin var x := y ; y := x + y; y : = y -1 ; end x := y + x ; ) end

Natural Semantics DV : D V 2 Var DV( ) = DV(var x := a ; D v ) = {x} DV(D v ) s[x s ] = s x if x X s' x if x X [none ns ] <, s> s [var ns ] <D v, s[x A a s]> s <var x := a; D v, s> s [block ns ] <D v, s> s, <S, s > s <begin D v S end, s> s [DV(Dv) s]

Structural Semantics? [block sos ] <begin D v S end, s> s

Conclusions Local Variables The natural semantics can remember local states Need to introduce stack or heap into state of the structural semantics

The While Programming Language with local variables and procedures Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S begin D v D p S end call p D v ::= var x := a ; D v D p ::= proc p is S ; D p

Summary SOS is powerful enough to describe imperative programs Can define the set of traces Can represent program counter implicitly Handle gotos Natural operational semantics is an abstraction Different semantics may be used to justify different behaviors Thinking in concrete semantics is essential for language designer/compiler writer/