Introduction to Denotational Semantics

Similar documents
7. Introduction to Denotational Semantics. Oscar Nierstrasz

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.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 3. Describing Syntax and Semantics

CMSC 330: Organization of Programming Languages

Programming Languages Third Edition

Com S 541. Programming Languages I

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

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

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

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

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

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

Denotational Semantics. Domain Theory

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

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages

10/30/18. Dynamic Semantics DYNAMIC SEMANTICS. Operational Semantics. An Example. Operational Semantics Definition Process

Defining Languages GMU

CS 242. Fundamentals. Reading: See last slide

CMSC 330: Organization of Programming Languages. Operational Semantics

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

3.7 Denotational Semantics

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

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

Fundamental Concepts. Chapter 1

Introduction to Haskell

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Contents. Chapter 1 SPECIFYING SYNTAX 1

CS3110 Spring 2017 Lecture 10 a Module for Rational Numbers

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

Chapter 3. Describing Syntax and Semantics ISBN

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

Modules, Structs, Hashes, and Operational Semantics

Semantics of programming languages

ASTs, Objective CAML, and Ocamlyacc

Introduction to Formal Methods

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Syntax. In Text: Chapter 3

CS2104 Prog. Lang. Concepts

Application: Programming Language Semantics

3.4 Denotational Semantics

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

Types and Static Type Checking (Introducing Micro-Haskell)

Control Structures in Java if-else and switch

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

LOGIC AND DISCRETE MATHEMATICS

Programming Languages and Compilers (CS 421)

Introduction to Parsing

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

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

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

1 Introduction. 3 Syntax

CS 11 Haskell track: lecture 1

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

PROGRAM ANALYSIS & SYNTHESIS

Semantics of programming languages

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

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

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

CSC 501 Semantics of Programming Languages

Abstract Interpretation

! Use of formal notations. ! in software system descriptions. ! for a broad range of effects. ! and varying levels of use. !

Formal Systems and their Applications

COS 320. Compiling Techniques

5 The Control Structure Diagram (CSD)

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

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Special Topics: Programming Languages

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

Foundations: Syntax, Semantics, and Graphs

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

In Our Last Exciting Episode

The Worker/Wrapper Transformation

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

EECS 6083 Intro to Parsing Context Free Grammars

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

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)

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Basic Structure of Denotational Definitions

Semantics of programming languages

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

4.5 Pure Linear Functional Programming

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

1 Lexical Considerations

Control Structures in Java if-else and switch

Types and Programming Languages. Lecture 5. Extensions of simple types

Programming Languages

5. Selection: If and Switch Controls

Principles of Programming Languages COMP251: Syntax and Grammars

Program Analysis: Lecture 02 Page 1 of 32

Principles of Programming Languages

Lexical Considerations

Supplementary Notes on Abstract Syntax

Transcription:

Introduction to Denotational Semantics Overview:! Syntax and Semantics! Approaches to Specifying Semantics! Sets, Semantic Domains, Domain Algebra, and Valuation Functions! Semantics of Expressions! Semantics of Assignments! Other Issues References:! David A. Schmidt, Denotational Semantics A Methodology for Language Development, Allyn and Bacon, 1986! David Watt, Programming Language Concepts and Paradigms, Prentice Hall, 1990

Defining Programming Languages Three main characteristics of programming languages:! Syntax: What is the appearance and structure of its programs?! Semantics: What is the meaning of programs? The static semantics tells us which (syntactically valid) programs are semantically valid (i. e., which are type correct) and the dynamic semantics tells us how to interpret the meaning of valid programs.! Pragmatics: What is the usability of the language? How easy is it to implement? What kinds of applications does it suit?

Uses of Semantic Specifications! Semantic specifications are useful for language designers to communicate to the implementors as well as to programmers. A semantic specification is:! A precise standard for a computer implementation: How should the language be implemented on different machines?! User documentation: What is the meaning of a program, given a particular combination of language features?! A tool for design and analysis: How can the language definition be tuned so that it can be implemented efficiently?! An input to a compiler generator: How can a reference implementation be obtained from the specification?

Methods for Specifying Semantics! Operational Semantics:! [ program ] = abstract machine program! can be simple to implement! hard to reason about! Denotational Semantics:! [ program ] = mathematical denotation (typically, a function)! facilitates reasoning! not always easy to find suitable semantic domains! Axiomatic Semantics:! [ program ] = set of properties! good for proving theorems about programs! somewhat distant from implementation! Structured Operational Semantics:! [ program ] = transition system (defined using inference rules)! good for concurrency and non-determinism! hard to reason about equivalence

Concrete and Abstract Syntax! How to parse 4 * 2 + 1?! Abstract syntax is compact but ambiguous: Expr ::= Num Expr Op Expr Op ::= + - * /! Concrete syntax is unambiguous, but verbose: Expr ::= Expr LowOp Expr Term Term ::= Term HighOp Factor Factor Factor ::= Num ( Expr ) LowOp ::= + - HighOp ::= * /

Set, Functions, and Domains! A set is a collection: it can contain numbers, persons, other sets, or (almost) anything one wishes:! { 1, {1, 2, 4}, 4}! { red, yellow, gray }! {}! A function is like black box that accepts an object as its input and then transforms it in some way to produce another object as output. We must use an external approach to characterize functions. Sets are ideal for formalizing the method. (See Extensional and Intentional Views )! The sets that are used as value spaces in programming language semantics are called semantic domains. Semantic domains may have a different structure than a set, and in practice not all of the sets and set building operations are needed for building domains.

Basic Domains! Primitive domains:! Natural numbers N! Boolean values B! Floating point numbers F! Compound domains:! Product domains A B! Sum domains A +B! Function domains A " B! Lifted domains:! Lifted domains add a special value ( bottom ) that denotes non-termination or no value at all. Including as a value is an alternative to using a theory of partial functions. Lifted domains are written, where A = A { } A

Domain Algebra! The format for representing semantic domains is called semantic algebra and defines a grouping of a set with the fundamental operations on the set.! This format is used because it:! Clearly states the structure of a domain and how its elements are used by the functions,! Encourages the development of standard algebra modules or kits that can be used in a variety of semantics definitions,! Makes it easier to analyze a semantic definition concept by concept,! Makes it straightforward to alter a semantic definition by replacing one semantic algebra with another.

Domain Rational Domain Rational = ( Z Z) Operations makerational :: makerational = Z Z Rational λp. λq.( q= 0) ][ ( p, q) addrational :: Rational Rational Rational addrational = λ p, q). λ( p, q ).(( p * q ) + ( p * q), q * mulrational :: Rational mulrational = λ p, q). λ( p ( q 1 1 2 2 1 2 2 1 1 2 Rational Rational, q ).( p * p, q * ) ( q 1 1 2 2 2 1 1 2 )

Haskell Implementation module Rational (Rational, makerational, addrational, mulrational) where data Rational = Rat Int Int makerational :: Int -> Int -> Rational makerational p q q == 0 = error "Rational: division by zero" otherwise = Rat p q addrational :: Rational -> Rational -> Rational addrational = \(Rat p1 q1) -> \(Rat p2 q2) -> Rat ((p1 * q2) + (p2 * q1)) (q1 * q2) mulrational :: Rational -> Rational -> Rational mulrational = \(Rat p1 q1) -> \(Rat p2 q2) -> Rat (p1 * p2) (q1 * q2) instance Show Rational where show (Rat p q) = "(" ++ show p ++ ", " ++ show q ++ ")" -- tell Haskell how to print rationals

Valuation Functions! A valuation function maps a language s abstract syntax structures or syntactic categories to meanings governed by its semantic domains.! The domain of a valuation function is the set of derivation trees of a syntactic category (abstract syntax trees).! The meaning of one syntactic category is determined by determining and combining all meanings of its sub-trees.! With P identifying the syntactic category, L denoting an element of the category, a set of parameters n 1,,n m carrying a state, and the actual mathematical denotation F ( n 1,..., n ), the corresponding valuation function L P m is written P L ]( n,..., n ) = F ( n,..., ). [ P 1 m L n 1 m! Examples of syntactic categories are: Program, Statement, Expression, Number, etc.

Format of a Denotational Definition A denotational definition of a language consists of three parts:! The abstract syntax definition! compact, minimal, but ambiguous language definition! The semantic algebras! set of semantic domains that share some common properties or use! The valuation functions! functions that assign every language element a precise meaning

A Calculator Language! Abstract syntax: Prog ::= ON Stmt Stmt ::= Expr TOTAL Stmt OFF Expr ::= Expr ( + * ) Expr IF Expr, Expr, Expr LASTANSWER ( Expr ) Num The program ON 4 * ( 3 + 2 ) TOTAL OFF should print out 20 and stop.

Calculator Semantics Semantic Algebras! Truth values: Domain Booleans Operations true,false ::Booleans! Natural numbers: Domain Numbers Operations zero,one,two, ::N um bers plus,times::n um bers Numbers" Numbers equals::n um bers Numbers" Booleans

Calculator Semantics Valuation Functions Prog ::Program " [Numbers] Prog [ ON S] = Stm t[s](0) Stmt::Statem ents" Numbers" [Numbers] Stm t[e TOTAL S]( n) = let n = Expr [E ]( n)in ( n: Stm t[s]( n)) Stmt[' O FF']( n) = [] Expr::Expression " Numbers" Numbers Expr [E + E ]( n) = plus ( Expr [E ]( n), Expr [E ]( n )) 1 2 1 2 Expr [E '*'E ]( n) = timesexpr ( [E ]( n), Expr [E ]( n)) 1 2 1 2 Expr [' IF'E ',' E ',' E ]( n) = 1 2 3 equals ( Expr [E ]( n), zero) Expr [E ]( n)][ Expr [E 1 2 Expr [' LASTANSW ER']( n) = n Expr ['('E ')']( n) = Expr [E ]( n) Expr [N ]( n) = Numeral [N ] 3 ]( n)

Calculator Semantics - Data Structures! We can represent programs in our calculator language as syntax trees: data Program data Statements data Expression = On Statements = Total Expression Statements Off = Plus Expression Expression Times Expression Expression If Expression Expression Expression LastAnswer Braced Expression Numeral Int

Representing Syntax The program ON 4 * ( 3 + 2 ) TOTAL OFF can be parsed as: Program Statement Statement ON TOTAL Expression And represented as: test = On (Total (Times (N 4) (Braced (Plus (Numeral 3) (Numeral 2) ) ) ) Off) * 4 () + 3 2 OFF

Haskell Implementation prog :: Program -> [Int] prog (On s) = stmt s 0 stmt :: Statements -> Int -> [Int] stmt (Total e s) n = let n' = (expr e n) in n' : (stmt s n') stmt Off n = [] expr :: Expression -> Int -> Int expr (Plus e1 e2) n = (expr e1 n) + (expr e2 n) expr (Times e1 e2) n = (expr e1 n) * (expr e2 n) expr (If e1 e2 e3) n (expr e1 n) == 0 = (expr e2 n) otherwise = (expr e3 n) expr LastAnswer n = n expr (Braced e) n = expr e n expr (Numeral n) _ = n val = On (Total (Times (Numeral 4) (Braced (Plus (Numeral 3) (Numeral 2)))) Off)

A Language With Assignment! Abstract Syntax: Program ::= Command. Command ::= Command ; Command IF Boolean THEN Command ELSE Command Identifier := Expression Expression ::= Expression + Expression Identifier Numeral Boolean ::= Expression = Expression NOT Boolean Example: z := 1 ; IF a = 0 THEN z := 3 ELSE z := z + a.! Programs take a single number as input, which initializes the variable a. The output of a program is the final value of the variable z.

Abstract Syntax Trees! Data Structures: data Program = Dot Command data Command = Sequence Command Command Assign Identifier Expression If Boolean Command Command data Expression = Plus Expression Expression Ident Identifier Numeral Int data Boolean = Equal Expression Expression Not Boolean type Identifier = Char! Example: Dot (Sequence (Assign 'z' (Numeral 1)) (If (Equal (Ident 'a') (Numeral 0)) (Assign 'z' (Numeral 3)) (Assign 'z' (Plus (Ident 'z') (Ident 'a'))) ) )

Store Algebra Domain Store = Identifier " Numbers Operations newstore ::Store newstore= λi.zero access::identifier" Store " Numbers access= λi.λs.s(i) update::identifier " Numbers" Store " Store update= λi. λn. λsi [ a n] s

Function Update Expression! For a function f::a " B, we let [ a a b ] f be the function that 0 0 acts just like f except that it maps the specific value a 0 A to b 0 B: ([ a ([ a 0 0 a a b ] f)( a ) = b 0 0 0 b ] f)( a) = f( a),forallother a 0 A such that a a 0

Modeling Environments! A store is a mapping from identifiers to values: type Store = Identifier -> Int newstore :: Store newstore id = 0 access :: Identifier -> Store -> Int access id store = store id update :: Identifier -> Int -> Store -> Store update id val store = store' where store' id' id' == id = val otherwise = store id'

Store Trace! How does the store work?? access 'a' (update 'a' 3 (update 'z' 2 newstore)) 3 :: Int Trace: update 'z' 2 newstore --> \id' -> if id' == 'z' then 2 else newstore id' update 'a' 3 (`1Xupdate`) --> \id' -> if id' == 'a' then 3 else (`1Xupdate`) id' access 'a' (`2Xupdate`) --> (`2Xupdate`) 'a' --> if 'a' == 'a' then 3 else (`1Xupdate`) 'a' --> 3

Semantics of Assignments prog ::Program -> Int-> Int prog (D otc)n = access'z'(com m c (update'a'n new store)) com m ::Com m and ->Store->Store com m (Sequence c1 c2)s= com m c2 (com m c1 s) com m (Assign id e)s= updateid(expres)s com m (Ifb c1 c2)s= ifelse (boolean b s)(com m c1 s)(com m c2 s) expr::expression -> Store-> Int expr(pluse1 e2)s= (expre1 s)+ (expre2 s) expr(identid)s= accessids expr(numeraln)s= n boolean ::Boolean -> Store-> Bool boolean (Equale1 e2)s= (expre1 s)== (expre2 s) boolean (N otb)s= not(boolean b s) ifelse ::Bool->a->a->a ifelse True x y = x ifelse Falsexy=y

Practical and Theoretical Issues! Modeling! Errors and non-termination:! Need a special error in semantic domains! Branching:! Semantic domains in which continuations model the rest of the program make it easy to transfer control! Interactive input! Dynamic typing! What is the semantics of recursive functions?! Recursive domain specifications! Need least fixed point theory! How to model concurrency and non-determinism?! Powerdomains set of all possible results for different evaluations! true concurrency requires other models (e.g. CCS, π-calculus, Petri-Nets)