Big-step Operational Semantics (aka Natural Semantics)

Similar documents
Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

Semantics with Applications 3. More on Operational Semantics

Denotational Semantics

Exercises on Semantics of Programming Languages

Mosig M1 - PLSCD Written exam

Program Analysis: Lecture 02 Page 1 of 32

Formal Syntax and Semantics of Programming Languages

Denotational Semantics

Formal Syntax and Semantics of Programming Languages

PROGRAM ANALYSIS & SYNTHESIS

Application: Programming Language Semantics

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

02157 Functional Programming Interpreters for two simple languages including exercises

CS422 - Programming Language Design

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.

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

More on Operational Semantics

Compiler Construction

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

Compiler Construction

Foundations: Syntax, Semantics, and Graphs

Program Representations

Compiler Construction

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

Compiler Construction

Lecture 5 - Axiomatic semantics

Denotational semantics

Compiler Construction

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

Induction and Semantics in Dafny

Semantics and Verification of Software

Programming Language Concepts, cs2104 Lecture 04 ( )

2 Introduction to operational semantics

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Homework 6: Big- & small-step semantics for LC

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

Compiler Construction

Static Program Analysis

Compiler Construction

CSc 520 Principles of Programming Languages

Programming Languages Third Edition

Static Program Analysis

Proving Properties on Programs From the Coq Tutorial at ITP 2015

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

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

The Untyped Lambda Calculus

Denotational semantics (An introduction)

CS422 - Programming Language Design

Programming Languages and Compilers (CS 421)

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

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

CSE 307: Principles of Programming Languages

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

Program certification with computational

COP4020 Programming Assignment 2 - Fall 2016

3.7 Denotational Semantics

X Language Definition

Semantics. A. Demers Jan This material is primarily from Ch. 2 of the text. We present an imperative

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

CS 242. Fundamentals. Reading: See last slide

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Computation Club: Gödel s theorem

3.2 Big-Step Structural Operational Semantics (Big-Step SOS)

SOFTWARE ARCHITECTURE 5. COMPILER

The Untyped Lambda Calculus

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

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CMSC 330: Organization of Programming Languages

Lambda Calculi With Polymorphism

COS 320. Compiling Techniques

Lab 6: More Steps in Haskell

CIS 500 Software Foundations. Final Exam. May 3, Answer key

Intermediate Code Generation

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

The Substitution Model

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Calculating Correct Compilers

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Programming Languages and Compilers (CS 421)

Introduction to Denotational Semantics (1/2)

Shared Variables and Interference

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Chapter 1. Fundamentals of Higher Order Programming

Introduction to Language Based Security

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

In Our Last Exciting Episode

CMSC 330: Organization of Programming Languages

Principles of Programming Languages

Intermediate Code Generation

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Decision Making in C

Functional Programming. Pure Functional Programming

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Transcription:

x = 1 let x = 1 in... x(1).!x(1) x.set(1) Programming Language Theory Big-step Operational Semantics (aka Natural Semantics) Ralf Lämmel

A big-step operational semantics for While 2

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Syntactic categories of the While language numerals n Num variables x Var arithmetic expressions a Aexp a ::= n x a 1 + a 2 a 1 a 2 a 1 a 2 booleans expressions b Bexp b ::= true false a 1 = a 2 a 1 a 2 b b 1 b 2 statements S Stm S ::= x := a skip S 1 ; S 2 if b then S 1 else S 2 while b do S 3

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Semantic categories of the While language Natural numbers N={0, 1, 2, } Truth values T={tt, ff} States State = Var N Lookup in a state: sx Update a state: s = s[y v] s x = sx if x y v if x = y 4

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Meanings of syntactic categories Numerals N :Num N Variables s State = Var N Arithmetic expressions A :Aexp (State N) We do not define all these functions directly. Especially the last one is defined as a relation based on rules (with premises and conclusions). Boolean expressions B :Bexp (State T) Statements S :Stm (State State) 5

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Semantics of arithmetic expressions A[n]s = N [n] A[x]s = sx A[a 1 + a 2 ]s = A[a 1 ]s + A[a 2 ]s A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s 6

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Semantics of boolean expressions B[true]s = tt B[false]s = ff B[a 1 = a 2 ]s = tt if A[a 1 ]s = A[a 2 ]s ff if A[a 1 ]s A[a 2 ]s B[a 1 a 2 ]s = B[ b]s = B[b 1 b 2 ]s = tt if A[a 1 ]s A[a 2 ]s ff if A[a 1 ]s A[a 2 ]s tt if B[b]s = ff ff if B[b]s =tt tt if B[b 1 ]s =tt and B[b 2 ]s =tt ff if B[b 1 ]s = ff or B[b 2 ]s = ff 7

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Semantics of statements 8

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Derivation trees Derivation tree States 9

Prolog as a sandbox for big-step operational semantics https://slps.svn.sourceforge.net/svnroot/slps/topics/nielsonn07/prolog/while/ns/ 10

Architecture of the interpreter Makefile: see make test main.pro: main module to compose all other modules exec.pro: statement execution eval.pro: expression evaluation map.pro: abstract data type for maps (states) test.pro: framework for unit testing 11

:- ['eval.pro']. :- ['exec.pro']. :- ['map.pro']. :- ['test.pro']. % Tests main.pro :- test(evala(add(num(21),num(21)),_,42)).... :- halt. 12

Tests :- test(evala(add(num(21),num(21)),_,42)). :- test(evala(add(num(21),id(x)),[('x',21)],42)). :- test( exec( while( not(eq(id(x),num(0))), seq( assign(y,mul(id(x),id(y))), assign(x,sub(id(x),num(1))))), [(x,5),(y,1)], [(x,0),(y,120)])). 13

Run tests $ make test swipl -f main.pro % library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,992 bytes % eval.pro compiled 0.00 sec, 7,008 bytes % exec.pro compiled 0.00 sec, 2,872 bytes % map.pro compiled 0.00 sec, 1,744 bytes % test.pro compiled 0.00 sec, 1,128 bytes OK: evala(add(num(21),num(21)),_g1199,42) OK: evala(add(num(21),id(x)),[ (x,21)],42) OK: exec(while(not(eq(id(x),num(0))),seq(assign(y,mul(id(x),id(y))),assign (x,sub(id(x),num(1))))),[ (x,5), (y,1)],[ (x,0), (y,120)]) 14

Arithmetic expression evaluation A[n]s = N [n] A[x]s = sx A[a 1 + a 2 ]s = A[a 1 ]s + A[a 2 ]s A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s % Number is evaluated to its value evala(num(v),_,v). A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s % Variable reference is evaluated to its current value evala(id(x),m,y) :- lookup(m,x,y). 15

Arithmetic expression evaluation cont d % Adddition evala(add(a1,a2),m,v) :- evala(a1,m,v1), evala(a2,m,v2), V is V1 + V2. % Subtraction... % Multiplication... A[n]s = N [n] A[x]s = sx A[a 1 + a 2 ]s = A[a 1 ]s + A[a 2 ]s A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s A[a 1 a 2 ]s = A[a 1 ]s A[a 2 ]s 16

Boolean expression evaluation evalb(true,_,tt). evalb(false,_,ff). evalb(not(b),m,v) :- evalb(b,m,v1), not(v1,v). evalb(and(b1,b2),m,v) :- evalb(b1,m,v1), evalb(b2,m,v2), and(v1,v2,v).... 17

Skip statement exec(skip,m,m). 18

Sequential composition exec(seq(s1,s2),m1,m3) :- exec(s1,m1,m2), exec(s2,m2,m3). 19

Assignment exec(assign(x,a),m1,m2) :- evala(a,m1,y), update(m1,x,y,m2). 20

Conditional % Conditional statement with true condition exec(ifthenelse(b,s1,_),m1,m2) :- evalb(b,m1,tt), exec(s1,m1,m2). % Conditional statement with false condition exec(ifthenelse(b,_,s2),m1,m2) :- evalb(b,m1,ff), exec(s2,m1,m2). 21

Loop statement % Loop statement with true condition exec(while(b,s),m1,m3) :- evalb(b,m1,tt), exec(s,m1,m2), exec(while(b,s),m2,m3). % Loop statement with false condition exec(while(b,_),m,m) :- evalb(b,m,ff). 22

Abstract data type for maps (states) % Function lookup (application) lookup(m,x,y) :- append(_,[(x,y) _],M). % Function update in one position update([],x,y,[(x,y)]). update([(x,_) M],X,Y,[(X,Y) M]). update([(x1,y1) M1],X2,Y2,[(X1,Y1) M2]) :- \+ X1 = X2, update(m1,x2,y2,m2). 23

Test framework test(g) :- ( G -> P = 'OK'; P = 'FAIL' ), format('~w: ~w~n',[p,g]). 24

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Blocks and procedures 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 ε 25

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Semantics of var declarations Extension of semantics of statements: (D V, s) D s,(s, s ) s (begin D V S end, s) s [DV(D V ) s] Semantics of variable declarations: (D V, s[x A[a]s]) D s (var x := a; D V, s) D s (ε, s) D s 26

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Scope rules Dynamic scope for variables and procedures Dynamic scope for variables but static for procedures Static scope for variables as well as procedures begin var x := 0; proc p is x := x * 2; proc q is call p; begin var x := 5; proc p is x := x + 1; call q; y := x end end 27

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Dynamic scope for variables and procedures Execution call q call p (calls inner, say local p) x := x + 1 (affects inner, say local x) y := x (obviously accesses local x) Final value of y = 6 begin var x := 0; proc p is x := x * 2; proc q is call p; begin var x := 5; proc p is x := x + 1; call q; y := x end end 28

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). NS with dynamic scope rules using an environment 29

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Dynamic scope for variables Static scope for procedures Execution call q call p (calls outer, say global p) x := x * 2 (affects inner, say local x) y := x (obviously accesses local x) Final value of y = 10 begin var x := 0; proc p is x := x * 2; proc q is call p; begin var x := 5; proc p is x := x + 1; call q; y := x end end 30

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Dynamic scope for variables Static scope for procedures Updated environment Updated environment update Updated rule for calls Recursive calls 31

This slide is derived from the book & slides by Nielson & Nielson: Semantics with applications (1991 & 1999+). Static scope for variables and procedures Execution call q call p (calls outer, say global p) x := x * 2 (affects outer, say global x) y := x (obviously accesses local x) begin var x := 0; proc p is x := x * 2; proc q is call p; begin var x := 5; proc p is x := x + 1; call q; y := x end end Final value of y = 5 Formal semantics omitted here. 32

Summary: Big-step operational semantics Models relations between syntax, states, values. Rule-based modeling (conclusion, premises). Computations are derivation trees. Reading: Semantics with applications Chapter 1, Chapter 2.1, Chapter 2.5 Lab: Operational Semantics in Prolog Outlook: Small-step semantics Semantics-based reasoning 33