Formal Semantics of Programming Languages

Similar documents
Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Program Analysis: Lecture 02 Page 1 of 32

Formal Syntax and Semantics of Programming Languages

Formal Syntax and Semantics of Programming Languages

Exercises on Semantics of Programming Languages

Semantics with Applications 3. More on Operational Semantics

Programming Languages Third Edition

Big-step Operational Semantics (aka Natural Semantics)

Application: Programming Language Semantics

PROGRAM ANALYSIS & SYNTHESIS

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

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

CMSC 330: Organization of Programming Languages

Lecture 5 - Axiomatic semantics

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

02157 Functional Programming Interpreters for two simple languages including exercises

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

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

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Induction and Semantics in Dafny

Propositional Logic Formal Syntax and Semantics. Computability and Logic

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

Denotational Semantics

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

3.7 Denotational Semantics

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

Functional Programming. Pure Functional Programming

Chapter 3 (part 3) Describing Syntax and Semantics

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Introduction to Denotational Semantics. Brutus Is An Honorable Man. Class Likes/Dislikes Survey. Dueling Semantics

The exercises and answer for programming language class

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

Homework 3 COSE212, Fall 2018

Chapter 3. Describing Syntax and Semantics

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Introduction to Scheme

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Computation Club: Gödel s theorem

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

In Our Last Exciting Episode

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?

3. DESCRIBING SYNTAX AND SEMANTICS

Denotational Semantics

2 Introduction to operational semantics

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

Lectures 20, 21: Axiomatic Semantics

CS422 - Programming Language Design

Foundations: Syntax, Semantics, and Graphs

Principles of Programming Languages

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Static Program Analysis

Calculating Correct Compilers

A Simple Syntax-Directed Translator

7. Introduction to Denotational Semantics. Oscar Nierstrasz

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

CMSC 330: Organization of Programming Languages. Operational Semantics

Program Representations

Programming Languages

Denotational Semantics. Domain Theory

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

More on Operational Semantics

Syntax and Grammars 1 / 21

Syntax vs. semantics. Detour: Natural deduction. Syntax vs. semantics (cont d) Syntax = grammatical structure Semantics = underlying meaning

Chapter 3. Describing Syntax and Semantics ISBN

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

CS2104 Prog. Lang. Concepts

Defining Languages GMU

Static Program Analysis

Lecture 15 CIS 341: COMPILERS

Lambda Calculus. Concepts in Programming Languages Recitation 6:

A Small Interpreted Language

Denotational semantics

Lecture 5: Predicate Calculus. ffl Predicate Logic ffl The Language ffl Semantics: Structures

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

A CRASH COURSE IN SEMANTICS

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

Com S 541. Programming Languages I

The Programming Language Core

The Rule of Constancy(Derived Frame Rule)

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CSCE 314 Programming Languages

Syntax and Semantics

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Chapter 3. The While programming language

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Verifying Safety Property of Lustre Programs: Temporal Induction

CS422 - Programming Language Design

Transcription:

Formal 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

Benefits of formal definitions Intellectual Better understanding Formal proofs Mechanical checks by computer Tool generations

What is a good formal definition? Natural Concise Easy to understand Permits effective mechanical reasoning

Syntax vs. Semantics The pattern of formation of sentences or phrases in a language Examples Regular expressions Context free grammars The study or science of meaning in language Examples Interpreter Compiler Better mechanisms will be given in the course

Alternative Formal Semantics Operational Semantics The meaning of the program is described operationally Natural Operational Semantics Structural Operational Semantics Denotational Semantics The meaning of the program is an input/output relation Mathematically challenging but complicated Axiomatic Semantics The meaning of the program are observed properties

[x 3] int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y 1; } return z } [x 3, z, y ] [x 3, z 1, y ] [x 3, z 1, y 3] [x 3, z 1, y 3] [x 3, z 3, y 3] [x 3, z 3, y 2]

int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y 1; } return z } [x 3, z 3, y 2] [x 3, z 3, y 2] [x 3, z 6, y 2] [x 3, z 6, y 1]

int fact(int x) { int z, y; z = 1; y= x while (y>0) { z = z * y ; y = y 1; } return z } [x 3, z 6, y 1] [x 3, z 6, y 1] [x 3, z 6, y 1] [x 3, z 6, y 0]

int fact(int x) { int z, y; z = 1; y= x; while (y>0) { z = z * y ; y = y 1; } return z } [x 3, z 6, y 0] [x 3, z 6, y 0]

int fact(int x) { int z, y; z = 1; y= x; while (y>0) { z = z * y ; y = y 1; } return 6 } [x 3, z 6, y 0]

int fact(int x) { int z, y; z = 1; Denotational Semantics y= x ; while (y>0) { z = z * y ; y = y 1; } return z; } f= x. if x = 0 then 1 else x * f(x -1)

{x=n} int fact(int x) { int z, y; z = 1; {x=n z=1} y= x {x=n z=1 y=n} while {x=n y 0 z=n! / y!} (y>0) { {x=n y >0 z=n! / y!} z = z * y ; {x=n y>0 z=n!/(y-1)!} y = y 1; {x=n y 0 z=n!/y!} } return z} {x=n z=n!} Axiomatic Semantics

The While Programming Language Abstract syntax S::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S Use parenthesizes for precedence Informal Semantics skip behaves like no-operation Import meaning of arithmetic and Boolean operations

Example While Program y := 1; while (x=1) do ( y := y * x; x := x 1; )

General Notations Syntactic categories Var the set of program variables Aexp the set of arithmetic expressions Bexp the set of Boolean expressions Stm set of program statements Semantic categories Natural values N={0, 1, 2, } Truth values T={ff, tt} States State = Var N Lookup in a state s: s x Update of a state s: s [ x 5]

Example State Manipulations [x 1, y 7, z 16] y = [x 1, y 7, z 16] t = [x 1, y 7, z 16][x 5] = [x 1, y 7, z 16][x 5] x = [x 1, y 7, z 16][x 5] y =

Prolog Implementation Environment 1: eval_env([var/val _], Var, Val). 2: eval_env([varp/_ T], Var, Val) :- Varp \= Var, eval_env(t, Var, Val). eval_env([x/5, y/7, z/9], y, V). 1:{ x=y} = false 2: {Varp=x, _ = 5, T = [y/7, z/9], Var=y, Val=V} 1: {Var1=Var=y, Val1=Val=V= 7, _ = [z/9] eval_env([y/7, z/9], Var, Val). eval_env([var1/val1 _], Var1, Val1). V=7

Prolog Implementation Environment(2) 1: upd([], Var, Val, [Var/Val]). 2: upd([var/_ T], Var, Val, [Var/Val T]). 3: upd([varp/v T], Var, Val, [Varp/V Envp]) :- Varp \= Var, upd(t, Var, Val, Envp). upd([x/5, y/7, z/9], y, 6, Env). 1: {[]=[x/5, y/7, z/9]} = false 2:{Var=x, Var=y} = false 3:{Varp=x, V=5, Var=y, Val, T=[y/7, z/9], Env=[x/5 Envp] } upd([y/7, z/9], y, 6, Envp). 1: {[]=[y/7, z/9]} = false 2:{Var1=y, Var1=y,_=7, T=[z/9], Envp=[y/6 [z/9]]}

Prolog Implementation Environment(2) 1: upd([], Var, Val, [Var/Val]). 2: upd([var/_ T], Var, Val, [Var/Val T]). 3: upd([varp/v T], Var, Val, [Varp/V Envp]) :- Varp \= Var, upd(t, Var, Val, Envp). upd([x/5, y/7, z/9], y, 6, Env). 1: {[]=[x/5, y/7, z/9]} = false 2:{Var=x, Var=y} = false 3:{Varp=x, V=5, Var=y, Val, T=[y/7, z/9], Env=[x/5 Envp] } upd([y/7, z/9], y, 6, Envp). 1: {[]=[y/7, z/9]} = false 2:{Var1=y, Var1=y,_=7, T=[z/9], Envp=[y/6 [z/9]]} Env=[x/5, y/7, z/9]

Semantics of arithmetic expressions Assume that arithmetic expressions are side-effect free A Aexp : State N Defined by induction on the syntax tree A n s = n A x s = s x A e 1 + e 2 s = A e 1 s + A e 2 s A e 1 * e 2 s = A e 1 s * A e 2 s A ( e 1 ) s = A e 1 s --- not needed A - e 1 s = -A e 1 s

Semantics of arithmetic expressions Assume that arithmetic expressions are side-effect free A Aexp : State N Defined by induction on the syntax tree A n s = n A x s = s x A e 1 + e 2 s = A e 1 s + A e 2 s A e 1 * e 2 s = A e 1 s * A e 2 s A - e 1 s = -A e 1 s Compositional Properties can be proved by structural induction We say that e 1 is semantically equivalent to e 2 (e 1 e 2 ) when A e 1 = A e 2 Theorem: for every expressions e 1, e 2 : e 1 + e 2 e 2 + e 1

Prolog Implementation (Expressions) eval_exp(num, _, Num) :- number(num). eval_exp(var, Env, Val) :- atom(var), eval_env(env, Var, Val). eval_exp(plus(e1, E2), Env, Val) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), Val is V1 + V2. eval_exp(minus(e1, E2), Env, Val) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), Val is V1 - V2. eval_exp(mul(e1, E2), Env, Val) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), Val is V1 * V2.

Prolog Implementation (Expressions) 1: eval_exp(num, _, Num) :- number(num). 2: eval_exp(var, Env, Val) :- atom(var), eval_env(env, Var, Val). 3: eval_exp(plus(e1, E2), Env, Val) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), Val is V1 + V2. eval_exp(plus(y, 3), [ y/7], Val). Val=10 3:{E1=y, E2=1, Env=[y/7], Val=Val1 } eval_exp(y, [ y/7], V1), eval_exp(3, [ y/7], V2) Val1=10 2:{Var=y, Env2=Env=[y/7], V1=Val2 } V1=7 atom(y), eval_env(env2, y, Val2). 1:{Num=3, _=[y/7], V2=3 } number(3). atom(y). eval_env(env2, y, Val2). Val2=7

Semantics of Boolean expressions Assume that Boolean expressions are side-effect free B Bexp : State T Defined by induction on the syntax tree B true s = tt B false s = ff B e 1 = e 2 s = B e 1 e 2 s = tt if A e 1 s = A e 2 s ff if A e 1 s A e 2 s tt if B e 1 s = tt and B e 2 =tt ff if B e 1 s=ff or B e 2 s=ff B e 1 e 2 s =

Prolog Implementation Boolean expression eval_boolean_exp(true,_). eval_boolean_exp(eq(e1, E2), Env) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), V1 = V2. eval_boolean_exp(le(e1, E2), Env) :- eval_exp(e1, Env, V1), eval_exp(e2, Env, V2), V1 =< V2. eval_boolean_exp(land(e1, E2), Env) :- eval_boolean_exp(e1, Env), eval_boolean_exp(e2, Env). eval_boolean_exp(lor(e1, _), Env) :- eval_boolean_exp(e1, Env). eval_boolean_exp(lor(_, E2), Env) :- eval_boolean_exp(e2, Env). eval_boolean_exp(lneg(e), Env) :- \+ eval_boolean_exp(e, Env).

Natural Operational Semantics Describe the overall effect of program constructs Ignores non terminating computations

Natural Semantics Notations <S, s> - the program statement S is executed on input state s s representing a terminal (final) state For every statement S, write meaning rules <S, i> o If the statement S is executed on an input state i, it terminates and yields an output state o The meaning of a program P on an input state s is the set of outputs states o such that <P, i> o The meaning of compound statements is defined using the meaning immediate constituent statements

Natural Semantics for While [ass ns ] <x := a, s> s[x A a s] axioms rules [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

Simple Examples Let s 0 be the state which assigns zero to all program variables Assignments [ass ns ] <x := x+1, s 0 > s 0 [x 1] Skip statement [skip ns ] <skip, s 0 > s 0 Composition [comp ns ] <skip,s 0 > s 0, <x := x+1, s 0 > s 0 [x 1] <skip; x := x +1, s 0 > s 0 [x 1]

A Derivation Tree A proof that <S, s> s The root of tree is <S, s> s Leaves are instances of axioms Internal nodes rules Immediate children match rule premises Simple Example <skip; x := x +1, s 0 > s 0 [x 1]> <skip, s 0 > s 0 compns < x := x +1, s 0 > s 0 [x 1]>

An Example Derivation Tree <(x :=x+1; y :=x+1) ; z := y), s0> s0[x 1][y 2][z 2] <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 compns <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

Example of Top Down Tree Construction Input state s such that s x = 2 Factorial program <y := 1; while (x=1) do (y := y * x; x := x - 1), s> s[y 2][x 1] > compns <W, s[y 1] > s[y 2][x 1 > <y :=1, s> s[y 1] assns <(y := y * x ; x := x -1, s[y 1]> s[y 2][x 1] > compns while tt ns <W, s[y 2][x 1] > s[y 2][x 1 > while ff ns <y := y * x ; s[y 1]> s[y 2] > <x := x - 1, s[y 2] > s[y 2][x 1] > assns assns

1: nat(skip, Env, Env). Natural Semantics Prolog 2: nat(ass(lhs, RHS), Env, Envp) :- eval_exp(rhs, Env, Val), upd(env, LHS, Val, Envp). 3: nat(seq(s1, S2), Env, Envp) :- nat(s1, Env, Envpp), nat(s2, Envpp, Envp). 4: nat(if(b, S1, _), Env, Envp) :- eval_boolean_exp(b, Env), nat(s1, Env, Envp). 5: nat(if(b, _, S2), Env, Envp) :- \+ eval_boolean_exp(b, Env), nat(s2, Env, Envp). 6: nat(while(b,s), Env, Envp) :- eval_boolean_exp(b, Env), nat(s, Env, Envpp), nat(while(b,s), Envpp, Envp). 7: nat(while(b,_), Env, Env) :- \+ eval_boolean_exp(b, Env). nat(seq(ass(y, 1), while(lneg(eq(x, 1)), seq(ass(y, mul(y, x)), ass(x, minus(x, 1))))), [x/3], E)

Program Termination Given a statement S and input s S terminates on s if there exists a state s such that <S, s> s S loops on s if there is no state s such that <S, s> s Given a statement S S always terminates if for every input state s, S terminates on s S always loops if for every input state s, S loops on s

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

Properties of Natural Semantics Equivalence of program constructs skip ; skip is semantically equivalent to skip ((S 1 ; S 2 ) ; S 3 ) is semantically equivalent to (S 1 ;( S 2 ; S 3 )) (x := 5 ; y := x * 8) is semantically equivalent to (x :=5; y := 40) Deterministic If <S, s> s 1 and <S, s> s 2 then s 1 =s 2

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

Summary Natural Semantics Simple Useful Enables simple proofs of language properties Automatic generation of interpreters But limited Concurrency