8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
10. Applications of Logic Programming. © O. Nierstrasz PS — Applications of Logic Programming 10.2 Roadmap 1. Search problems —SEND + MORE = MONEY 2.
© M. Winter COSC 4P41 – Functional Programming Testing vs Proving Testing –uses a set of “typical” examples, –symbolic testing, –may find errors,
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 5 LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE : INTERPRETATION IN HASKELL.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
ESE Einführung in Software Engineering 7. Modeling Behaviour Prof. O. Nierstrasz.
ISBN Chapter 3 Describing Syntax and Semantics.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 355 – Programming Languages
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
10. Petri Nets Prof. O. Nierstrasz. Roadmap  Definition: —places, transitions, inputs, outputs —firing enabled transitions  Modelling: —concurrency.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap  Syntax and Semantics  Semantics of Expressions 
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
CP — Concurrent Programming 12. Petri Nets Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
Dec Formal Semantics1 Programming Language Theory Formal Semantics Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Describing Syntax and Semantics
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
Winter 2003/4Pls – syntax – Catriel Beeri1 SYNTAX Syntax: form, structure The syntax of a pl: The set of its well-formed programs The rules that define.
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
1 Programming Languages (CS 550) Lecture 9 Summary Introduction to Formal Semantics Jeremy R. Johnson TexPoint fonts used in EMF. Read the TexPoint manual.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
Languages and Compilers
ISBN Chapter 3 Describing Syntax and Semantics.
Programming Language Descriptions. What drives PL Development? Computers are “in charge” of extremely important issues Execute a program literally. Exercise.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Functional Programming
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Propositional Calculus: Boolean Functions and Expressions
Mini Language Interpreter Programming Languages (CS 550)
Programming Languages (CS 550) Mini Language Semantics
Denotational Semantics (Denotational Semantics)
Semantics In Text: Chapter 3.
Chapter 3 Describing Syntax and Semantics.
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

8. Introduction to Denotational Semantics

© O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions  Semantics of Assignment  Other Issues

© O. Nierstrasz PS — Denotational Semantics 8.3 References  D. A. Schmidt, Denotational Semantics, Wm. C. Brown Publ., 1986  D. Watt, Programming Language Concepts and Paradigms, Prentice Hall, 1990

© O. Nierstrasz PS — Denotational Semantics 8.4 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions  Semantics of Assignment  Other Issues

© O. Nierstrasz PS — Denotational Semantics 8.5 Defining Programming Languages There are three main characteristics of programming languages: 1. Syntax: What is the appearance and structure of its programs? 2. 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. 3. Pragmatics: What is the usability of the language? How easy is it to implement? What kinds of applications does it suit?

© O. Nierstrasz PS — Denotational Semantics 8.6 Uses of Semantic Specifications Semantic specifications are useful for language designers to communicate with implementors as well as with programmers. 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? Input to a compiler generator: How can a reference implementation be obtained from the specification?

© O. Nierstrasz PS — Denotational Semantics 8.7 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

© O. Nierstrasz PS — Denotational Semantics 8.8 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions  Semantics of Assignment  Other Issues

© O. Nierstrasz PS — Denotational Semantics 8.9 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 Term | Term Term::= Term HighOp Factor | Factor Factor::= Num | ( Expr ) LowOp::= + | - HighOp::= * | / Concrete syntax is needed for parsing; abstract syntax suffices for semantic specifications.

© O. Nierstrasz PS — Denotational Semantics 8.10 A Calculator Language Abstract Syntax: Prog::='ON' Stmt Stmt::=Expr 'TOTAL' Stmt |Expr 'TOTAL' 'OFF' Expr::=Expr1 '+' Expr2 |Expr1 '*' Expr2 |'IF' Expr1 ',' Expr2 ',' Expr3 |'LASTANSWER' |'(' Expr ')' |Num The program “ON 4 * ( ) TOTAL OFF” should print out 20 and stop.

© O. Nierstrasz PS — Denotational Semantics 8.11 Calculator Semantics We need three semantic functions: one for programs, one for statements (expression sequences) and one for expressions. The meaning of a program is the list of integers printed: Programs: P : Program  Int* P [[ ON S ]]=S [[ S ]] (0) A statement may use and update LASTANSWER: Statements: S :: ExprSequence  Int  Int* S [[ E TOTAL S ]] (n)=let n' = E [[ E ]] (n) in cons(n', S [[ S ]] (n')) S [[ E TOTAL OFF ]] (n)=[ E [[ E ]] (n) ]

© O. Nierstrasz PS — Denotational Semantics 8.12 Calculator Semantics... Expressions: E : Expression  Int  Int E [[ E1 + E2 ]] (n)=E [[ E1 ]] (n) + E [[ E2 ]] (n) E [[ E1 * E2 ]] (n)= E [[ E1 ]] (n)  E [[ E2 ]] (n) E [[ IF E1, E2, E3 ]] (n)=if E [[ E1 ]] (n) = 0 then E [[ E2 ]] (n) else E [[ E3 ]] (n) E [[ LASTANSWER ]] (n)=n E [[ ( E ) ]] (n)=E [[ E ]] (n) E [[ N ]] (n)=N

© O. Nierstrasz PS — Denotational Semantics 8.13 Semantic Domains In order to define semantic mappings of programs and their features to their mathematical denotations, the semantic domains must be precisely defined: data Bool = True | False (&&), (||) :: Bool -> Bool -> Bool False&&x= False True&&x= x False||x= x True||x= True not :: Bool -> Bool notTrue= False notFalse= True data Bool = True | False (&&), (||) :: Bool -> Bool -> Bool False&&x= False True&&x= x False||x= x True||x= True not :: Bool -> Bool notTrue= False notFalse= True

© O. Nierstrasz PS — Denotational Semantics 8.14 Data Structures for Abstract Syntax We can represent programs in our calculator language as syntax trees: data Program = On ExprSequence data ExprSequence = Total Expression ExprSequence |TotalOff Expression data Expression = Plus Expression Expression |Times Expression Expression |If Expression Expression Expression |LastAnswer |Braced Expression |N Int data Program = On ExprSequence data ExprSequence = Total Expression ExprSequence |TotalOff Expression data Expression = Plus Expression Expression |Times Expression Expression |If Expression Expression Expression |LastAnswer |Braced Expression |N Int

© O. Nierstrasz PS — Denotational Semantics 8.15 Representing Syntax The test program “ON 4 * ( ) TOTAL OFF” can be parsed as: test=On (TotalOff(Times(N 4) (Braced(Plus(N 3) (N 2))))) test=On (TotalOff(Times(N 4) (Braced(Plus(N 3) (N 2))))) And represented as: Prog ON Stmt TOTAL OFF 4 * ()+ 3 2

© O. Nierstrasz PS — Denotational Semantics 8.16 pp :: Program -> [Int] pp (On s)= ss s 0 ss :: ExprSequence -> Int -> [Int] ss (Total e s) n= let n' = (ee e n) in n' : (ss s n') ss (TotalOff e) n= (ee e n) : [ ] ee :: Expression -> Int -> Int ee (Plus e1 e2) n= (ee e1 n) + (ee e2 n) ee (Times e1 e2) n= (ee e1 n) * (ee e2 n) ee (If e1 e2 e3) n | (ee e1 n) == 0= (ee e2 n) | otherwise= (ee e3 n) ee (LastAnswer) n= n ee (Braced e) n= (ee e n) ee (N num) n= num pp :: Program -> [Int] pp (On s)= ss s 0 ss :: ExprSequence -> Int -> [Int] ss (Total e s) n= let n' = (ee e n) in n' : (ss s n') ss (TotalOff e) n= (ee e n) : [ ] ee :: Expression -> Int -> Int ee (Plus e1 e2) n= (ee e1 n) + (ee e2 n) ee (Times e1 e2) n= (ee e1 n) * (ee e2 n) ee (If e1 e2 e3) n | (ee e1 n) == 0= (ee e2 n) | otherwise= (ee e3 n) ee (LastAnswer) n= n ee (Braced e) n= (ee e n) ee (N num) n= num Implementing the Calculator We can implement our denotational semantics directly in a functional language like Haskell:

© O. Nierstrasz PS — Denotational Semantics 8.17 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions  Semantics of Assignment  Other Issues

© O. Nierstrasz PS — Denotational Semantics 8.18 A Language with Assignment Prog::=Cmd '.' Cmd::=Cmd1 ';' Cmd2 |'if' Bool 'then' Cmd1 'else' Cmd2 |Id ':=' Exp Exp::=Exp1 '+' Exp2 |Id |Num Bool::=Exp1 '=' Exp2 |'not' Bool Example: Input number initializes a; output is final value of z. z := 1 ; if a = 0 then z := 3 else z := z + a.

© O. Nierstrasz PS — Denotational Semantics 8.19 Representing abstract syntax trees Data Structures: data Program=Dot Command data Command=CSeq Command Command |Assign Identifier Expression |If BooleanExpr Command Command data Expression=Plus Expression Expression |Id Identifier |Num Int data BooleanExpr=Equal Expression Expression |Not BooleanExpr type Identifier=Char data Program=Dot Command data Command=CSeq Command Command |Assign Identifier Expression |If BooleanExpr Command Command data Expression=Plus Expression Expression |Id Identifier |Num Int data BooleanExpr=Equal Expression Expression |Not BooleanExpr type Identifier=Char

© O. Nierstrasz PS — Denotational Semantics 8.20 An abstract syntax tree Example: Is represented as: z := 1 ; if a = 0 then z := 3 else z := z + a. Dot(CSeq(Assign 'z' (Num 1)) (If (Equal (Id 'a') (Num 0)) (Assign 'z' (Num 3)) (Assign 'z' (Plus (Id 'z') (Id 'a'))) ) Dot(CSeq(Assign 'z' (Num 1)) (If (Equal (Id 'a') (Num 0)) (Assign 'z' (Num 3)) (Assign 'z' (Plus (Id 'z') (Id 'a'))) )

© O. Nierstrasz PS — Denotational Semantics 8.21 Modelling Environments A store is a mapping from identifiers to values: type Store = Identifier -> Int newstore :: Store newstore id=0 update :: Identifier -> Int -> Store -> Store update id val store=store' where store' id' | id' == id = val | otherwise = store id' type Store = Identifier -> Int newstore :: Store newstore id=0 update :: Identifier -> Int -> Store -> Store update id val store=store' where store' id' | id' == id = val | otherwise = store id'

© O. Nierstrasz PS — Denotational Semantics 8.22 Functional updates Example: env1 = update 'a' 1 (update 'b' 2 (newstore)) env2 = update 'b' 3 env1 env1 = update 'a' 1 (update 'b' 2 (newstore)) env2 = update 'b' 3 env1 env1 ‘b’  2 env2 ‘b’  3 env2 ‘z’  0 env1 ‘b’  2 env2 ‘b’  3 env2 ‘z’  0

© O. Nierstrasz PS — Denotational Semantics 8.23 Semantics of assignments pp :: Program -> Int -> Int pp (Dot c) n = (cc c (update 'a' n newstore)) ‘z’ cc :: Command -> Store -> Store cc (CSeq c1 c2) s= cc c2 (cc c1 s) cc (Assign id e) s= update id (ee e s) s cc (If b c1 c2) s= ifelse (bb b s) (cc c1 s) (cc c2 s) ee :: Expression -> Store -> Int ee (Plus e1 e2) s= (ee e2 s) + (ee e1 s) ee (Id id) s= s id ee (Num n) s= n bb :: BooleanExpr -> Store -> Bool bb (Equal e1 e2) s= (ee e1 s) == (ee e2 s) bb (Not b) s= not (bb b s) ifelse :: Bool -> a -> a -> a ifelse True x y= x ifelse False x y= y pp :: Program -> Int -> Int pp (Dot c) n = (cc c (update 'a' n newstore)) ‘z’ cc :: Command -> Store -> Store cc (CSeq c1 c2) s= cc c2 (cc c1 s) cc (Assign id e) s= update id (ee e s) s cc (If b c1 c2) s= ifelse (bb b s) (cc c1 s) (cc c2 s) ee :: Expression -> Store -> Int ee (Plus e1 e2) s= (ee e2 s) + (ee e1 s) ee (Id id) s= s id ee (Num n) s= n bb :: BooleanExpr -> Store -> Bool bb (Equal e1 e2) s= (ee e1 s) == (ee e2 s) bb (Not b) s= not (bb b s) ifelse :: Bool -> a -> a -> a ifelse True x y= x ifelse False x y= y

© O. Nierstrasz PS — Denotational Semantics 8.24 Running the interpreter src1 = "z := 1 ; if a = 0 then z := 3 else z := z + a." ast1 = Dot (CSeq (Assign 'z' (Num 1)) (If (Equal (Id 'a') (Num 0)) (Assign 'z' (Num 3)) (Assign 'z' (Plus (Id 'z') (Id 'a'))))) src1 = "z := 1 ; if a = 0 then z := 3 else z := z + a." ast1 = Dot (CSeq (Assign 'z' (Num 1)) (If (Equal (Id 'a') (Num 0)) (Assign 'z' (Num 3)) (Assign 'z' (Plus (Id 'z') (Id 'a'))))) pp ast1 10  11 pp ast1 10  11

© O. Nierstrasz PS — Denotational Semantics 8.25 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions  Semantics of Assignment  Other Issues

© O. Nierstrasz PS — Denotational Semantics 8.26 Practical Issues Modelling:  Errors and non-termination: —need a special “error” value 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 ...

© O. Nierstrasz PS — Denotational Semantics 8.27 Theoretical Issues What are the denotations of lambda abstractions?  need Scott’s theory of semantic domains What are the semantics of recursive functions?  need least fixed point theory How to model concurrency and non-determinism?  abandon standard semantic domains  use “interleaving semantics”  “true concurrency” requires other models...

© O. Nierstrasz PS — Denotational Semantics 8.28 What you should know!  What is the difference between syntax and semantics?  What is the difference between abstract and concrete syntax?  What is a semantic domain?  How can you specify semantics as mappings from syntax to behaviour?  How can assignments and updates be modelled with (pure) functions?

© O. Nierstrasz PS — Denotational Semantics 8.29 Can you answer these questions?  Why are semantic functions typically higher-order?  Does the calculator semantics specify strict or lazy evaluation?  Does the implementation of the calculator semantics use strict or lazy evaluation?  Why do commands and expressions have different semantic domains?

© O. Nierstrasz PS — Denotational Semantics 8.30 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.