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

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

Program Evaluation. From our evaluation rules we have, We have σ =(σ 0 [2/x])[3/y]. What is the value for σ(y) and σ(x)? How about σ(z), z Loc?

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.

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

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

Application: Programming Language Semantics

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

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

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

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

CS422 - Programming Language Design

3.7 Denotational Semantics

A CRASH COURSE IN SEMANTICS

Types, Expressions, and States

Introduction to Axiomatic Semantics

Programming Languages Third Edition

An Annotated Language

Induction and Semantics in Dafny

Modules, Structs, Hashes, and Operational Semantics

CS2104 Prog. Lang. Concepts

CMSC 330: Organization of Programming Languages

In Our Last Exciting Episode

CSE 505: Concepts of Programming Languages

Program Syntax; Operational Semantics

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

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

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

CS4215 Programming Language Implementation. Martin Henz

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

2 Introduction to operational semantics

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

Foundations: Syntax, Semantics, and Graphs

Formal Semantics of Programming Languages

CS558 Programming Languages

3.3 Small-Step Structural Operational Semantics (Small-Step SOS)

CSc 520 Principles of Programming Languages

Formal Semantics of Programming Languages

Chapter 3 (part 3) Describing Syntax and Semantics

Compilation 2012 Static Analysis

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Introduction to Structural Operational Semantics

Semantics. Semantics

Meeting14:Denotations

Semantics of programming languages

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

Operational Semantics 1 / 13

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

Introduction to Axiomatic Semantics (1/2)

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

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

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

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

Programming Languages

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

Com S 541. Programming Languages I

Defining Languages GMU

Simply-Typed Lambda Calculus

3.1 IMP: A Simple Imperative Language

COMP 507: Computer-Aided Program Design

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS 242. Fundamentals. Reading: See last slide

Programming Languages and Compilers (CS 421)

Intermediate Code Generation

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

Lectures 20, 21: Axiomatic Semantics

Semester Review CSC 301

PROGRAM ANALYSIS & SYNTHESIS

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Axiomatic Specification. Al-Said, Apcar, Jerejian

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CS422 - Programming Language Design

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Lecture 15 CIS 341: COMPILERS

Programming Languages, Summary CSC419; Odelia Schwartz

Operational Semantics. One-Slide Summary. Lecture Outline

Part VI. Imperative Functional Programming

The Substitution Model

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

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

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Semantics and Concurrence Module on Semantic of Programming Languages

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

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

Meeting13:Denotations

Programmiersprachen (Programming Languages)

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

COS 320. Compiling Techniques

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Denotational Semantics. Domain Theory

CSE505, Fall 2012, Midterm Examination October 30, 2012

Chapter 11 :: Functional Languages

CSCI-GA Scripting Languages

CS558 Programming Languages

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Static Program Analysis

Transcription:

Operational Semantics Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6 1 Operational vs. Axiomatic Axiomatic semantics Describes properties of program state, using first-order logic Concerned with constructing proofs for such properties Operational semantics Explicitly describes the effects of program constructs on program state Shows not only what the program does, but also how it does it 2 Defining an Operational Semantics Define an interpreter for the language Define a compiler for the language, plus an interpreter for the assembly language used Specify how the state changes as various statements are executed 3 CSC 7101: Programming Language Structures 1

Specification Language Should be high-level Should be concise Efficiency does not matter Examples Post system (next slides) Attribute grammar (seen earlier) Nice expressive languages: e.g. ML, Prolog 4 IMP IMP: simple imperative language Already used in the discussion of axiomatic semantics Only integer variables No procedures or functions No explicit var declarations 5 IMP Syntax <c> 1 ::= skip <id> := <ae> <c> 2 ; <c> 3 if <be> then <c> 2 else <c> 3 while <be> do <c> 2 <ae> 1 ::= <id> <int> <ae> 2 + <ae> 3 <be> 1 <ae> 2 - <ae> 3 <ae> 2 * <ae> 3 ::= true false <ae> 1 = <ae> 2 <ae> 1 < <ae> 2 <be> 2 <be> 2 <be> 3 <be> 2 <be> 3 6 CSC 7101: Programming Language Structures 2

State State: a function σ from variable names to values E.g., program with 2 variables x, y σ(x) = 9 σ(y) = 5 For simplicity, we will only consider integer variables σ: Variables {0,-1,+1,-2,2, } 7 Operational Semantics for IMP Post system (proof system) If the state is σ and expression e is evaluated, what is the resulting value? <ae,σ> n for arithmetic expression <be,σ> bv for boolean expressions ae, be: parse trees; n: integer; bv: boolean If the state is σ and statement c is executed to termination, what is the resulting state? <c,σ> σ (c is a parse tree) 8 Evaluation of Arithmetic Expressions a ::= n X a 0 + a 1 a 0 a 1 a 0 * a 1 <n,σ> n <X,σ> σ(x) <a 0,σ> n 0 <a 1,σ> n 1 <a 0 +a 1,σ> n n is the sum of n 0 and n 1 similarly for a 0 a 1 and a 0 *a 1 E.g. if σ(p) = 4 and σ(q) = 6, <P+Q,σ> 10 9 CSC 7101: Programming Language Structures 3

Inference Rules Here again we represent the semantics with inference rules Zero or more premises Conclusion Optional condition (shown to the right): the rule applies only if the condition is true e.g. n is the sum of n 0 and n 1 Instances of such rules are applied for a given code fragment, in order to derive (prove) values and states 10 Level of Detail n is the sum of n 0 and n 1 This assumes that sum is a primitive notion that we will not define In some cases, we may decide to define it precisely e.g. sum is not trivial for roman numerals or maybe if we are describing a low-level language for some hardware device In this class: we will not specify how addition is done 11 Evaluation of Boolean Expressions b ::= true false a 0 = a 1 a 0 < a 1 b b 0 b 1 b 0 b 1 <true,σ> true <false,σ> false <a 0,σ> n 0 <a 1,σ> n 1 < a 0 = a 1,σ> true n 0 and n 1 are equal <a 0,σ> n 0 <a 1,σ> n 1 < a 0 = a 1,σ> false n 0 and n 1 are not equal 12 CSC 7101: Programming Language Structures 4

Evaluation of Boolean Expressions <a 0,σ> n 0 <a 1,σ> n 1 < a 0 < a 1,σ> true n 0 is less than n 1 <a 0,σ> n 0 <a 1,σ> n 1 < a 0 < a 1,σ> false n 0 is greater than or equal to n 1 <b,σ> true < b,σ> false <b,σ> false < b,σ> true 13 Evaluation of Boolean Expressions <b 0,σ> t 0 <b 1,σ> t 1 < b 0 b 1,σ> t t is true iff t 0 and t 1 are true <b 0,σ> t 0 <b 1,σ> t 1 < b 0 b 1,σ> t t is false iff t 0 and t 1 are false How about short-circuit evaluation? 14 Short-circuit Evaluations b 0 b 1 : if b 0 evaluates to false, no need to evaluate b 1 b 0 b 1 : if b 0 evaluates to true, no need to evaluate b 1 Most programming languages do this How do we represent this approach as inference rules? 15 CSC 7101: Programming Language Structures 5

Execution of Statements σ[m/x] is the same as σ except for X σ[m/x](y) = σ(y) if Y is not X σ[m/x](x) = m Also written as σ[x m] <e,σ> m <skip,σ> σ <X:=e,σ> σ[m/x] <c 0,σ> σ <c 1,σ > σ <c 0 ;c 1,σ> σ 16 Execution of Statements <b,σ> true <c 0,σ> σ <if b then c 0 else c 1, σ> σ <b,σ> false <c 1,σ> σ <if b then c 0 else c 1, σ> σ <b,σ> false <while b do c, σ> σ <b,σ> true <c,σ> σ <while b do c,σ > σ <while b do c, σ> σ 17 Equivalence Expressions x and y are equivalent if for any σ and any z, <x,σ> z iff <y,σ> z e.g. a+b is equivalent to b-5+a+5 Statements x and y are equivalent if for any σ and σ, <x,σ> σ iff<y,σ> σ e.g. statement c:=a+b; d:=c; is equivalent to statement d:=b-5+a+5; c:=d; Essential for ensuring the correctness of compiler optimizations Optimized code vs. the original code 18 CSC 7101: Programming Language Structures 6

Example Loop peeling: transform while b do c if b then (c; while b do c) else skip Take the first iteration out of the loop Common compiler optimization Can we prove that this transformation is semantics-preserving? i.e., are these statements equivalent? 19 First Direction <while,σ> σ implies <if,σ> σ There must be some derivation, leading to <b,σ> false (σ and σ are the same state), or <b,σ> true <c,σ> σ <while b to c,σ > σ Case 1: <b,σ> false, and <skip,σ> σ, so <if b then else skip,σ> σ Case 2: <b,σ> true and <c;while,σ> σ, so <if b then c;while else,σ> σ 20 Second Direction <if,σ> σ implies <while,σ> σ There must be some derivation, leading to <b,σ> false <skip,σ> σ (so σ = σ ) or <b,σ> true <c;while,σ> σ Case 1: <b,σ> false, so <while b do,σ> σ Case 2: <b,σ> true and <c;while,σ> σ, so must have had <c,σ> σ and <while,σ > σ, and therefore <while b do c,σ> σ 21 CSC 7101: Programming Language Structures 7

Another Example Partial redundancy elimination In its general form, an advanced compiler optimization if b then x:=e1 else y:=e2 fi; x:=e1 if b then x:=e1 else y:=e2; x:=e1; fi Under what conditions are these two code fragments semantically equivalent? Try this at home 22 Yet Another Example Claim: <while true do skip,σ> σ cannot be derived. Proof: suppose that a derivation <while,σ> σ exists. Consider a minimal length derivation. The last step must be <true,σ> true <skip,σ> σ <while,σ > σ <while true do skip, σ> σ But <skip,σ> σ means σ and σ are the same; premise <while,σ > σ means that the derivation is not minimal 23 Big-Step vs. Small-Step Semantics Until now: coarse semantics Abstracts away some details about the individual steps taken during execution Big-step semantics: based on the productions of the underlying grammar Alternative semantics: captures smaller steps in the execution Expressions: <e,σ> <e,σ > Statements: <c,σ> <c,σ > 24 CSC 7101: Programming Language Structures 8

Small-Step Evaluation of Expressions <e,σ> <e,σ > <e,σ> <e,σ > <X,σ> <σ(x),σ> <e, σ > <e,σ > (axiom) Example: addition is done left to right The left argument is evaluated first, the right arguments is evaluated next Big-step semantics does not capture this 25 Small-Step Addition <a 0,σ> <a 0,σ> <a 1,σ> <a 1,σ> <a 0 +a 1,σ> <a 0 +a 1,σ> <n+a 1,σ> <n+a 1,σ> <n+m,σ> <p,σ> if the sum of m and n is p If one step in the evaluation of a 0 leads to a 0, then one step in the evaluation of a 0 +a 1 leads to a 0 +a 1 (evaluate a 0 first) After a 0 is evaluated, evaluate a 1 Using rules 2 and 3, evaluate the sum 26 Example Evaluate a+b+c, with σ(a)=1, σ(b)=2, σ(c)=4 <a,σ> <1,σ> rule 1: <a+b,σ> <1+b,σ> <b,σ> <2,σ> rule 2: <1+b,σ> <1+2,σ> rule 3: <1+2,σ> <3,σ> transitivity: <a+b,σ> <3,σ> rule 1: <a+b+c,σ> <3+c,σ> <c,σ> <4,σ> rule 2: <3+c,σ> <3+4,σ> rule 3: <3+4,σ> <7,σ> transitivity: <a+b+c,σ> <7,σ> 27 CSC 7101: Programming Language Structures 9

Small-Step Execution of Statements <c 0,σ> σ <c 1,σ > σ (big step) <c 0 ;c 1,σ> σ <c 0,σ> <c 0,σ > (small step) <c 0 ;c 1,σ> <c 0 ;c 1,σ > <c 0,σ> σ (think of σ as <empty,σ >) <c 0 ;c 1,σ> <c 1,σ > 28 Example <X:=5;Y:=1,σ>? <X:=5,σ> σ[5/x] therefore <X:=5;Y:=1,σ> <Y:=1,σ[5/X]> also <Y:=1,σ[5/X]> σ[5/x][1/y] through transitivity <X:=5;Y:=1,σ> σ[5/x][1/y] 29 CSC 7101: Programming Language Structures 10