22c:111 programming language...
TRANSCRIPT
![Page 1: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/1.jpg)
22c:111 Programming Language Concepts - Fall 2008
22c:111 Programming Language Concepts
Fall 2008
Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli.These notes were originally developed by Allen Tucker, Robert Noonan and modified by Cesare Tinelli. They arecopyrighted materials and may not be used in other course settings outside of the University of Iowa in their current formor modified form without the express written permission of one of the copyright holders. During this course, students areprohibited from selling notes to or being paid for taking notes by any person or commercial firm without the expresswritten permission of one of the copyright holders.
Semantic Interpretation
![Page 2: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/2.jpg)
22c:111 Programming Language Concepts - Fall 2008
Contents
8.1 State Transformations and PartialFunctions
8.2 Semantics of Clite8.3 Semantics with Dynamic Typing8.4 A Formal Treatment of Semantics
![Page 3: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/3.jpg)
22c:111 Programming Language Concepts - Fall 2008
Semantics of a PL
Defines the meaning of a program– Syntactically valid– Static type checking valid
![Page 4: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/4.jpg)
22c:111 Programming Language Concepts - Fall 2008
Historical Problem
Valid program had different meanings on differentmachines– More than (e.g.) size of an int or float
Problem was lack of precision in defining meaning
![Page 5: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/5.jpg)
22c:111 Programming Language Concepts - Fall 2008
Methods
Compiler C on Machine M– Ex: Fortran on IBM 709/7090– Ex: PL/1 (F) on IBM 360 series
Operational Semantics – Ch. 7Axiomatic Semantics – Ch. 18Denotational Semantics – Ch. 8.4
![Page 6: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/6.jpg)
22c:111 Programming Language Concepts - Fall 2008
Example
Environment– i, j at memory locations 154, 155{ <i, 154>, <j, 155> }
State– i has value 13, j has value -1{ ..., <154, 13>, <155, -1>, ...}
![Page 7: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/7.jpg)
22c:111 Programming Language Concepts - Fall 2008
Simple State
Ignore environmentSet of identifier – value pairsEx: { <i, 13>, <j, -1>}
Special value undefined for each type
![Page 8: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/8.jpg)
22c:111 Programming Language Concepts - Fall 2008
8.1 State Transformations
Defn: The denotational semantics of a languagedefines the meanings of abstract language elementsas a collection of state-transforming functions.
Defn: A semantic domain is a set of values whoseproperties and operations are independently well-understood and upon which the rules that definethe semantics of a language can be based.
![Page 9: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/9.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaningless Program
for (i = 1; i > -1; i++)i--;
![Page 10: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/10.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaningless Expression
Are all expressions meaningful?
![Page 11: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/11.jpg)
22c:111 Programming Language Concepts - Fall 2008
C++Lite Semantics
State – a mapping from program variables to values
A meaning function M is a mapping:M: Program → StateM: Statement x State → StateM: Expression x State → Value
![Page 12: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/12.jpg)
22c:111 Programming Language Concepts - Fall 2008
C++Lite Semantics in OCaml
type var = string
type value = I of int | B of bool
module type StateSig = sig
type state
val empty: state
val update: state → var → value → state val get_value: state → var → value
end
![Page 13: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/13.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.1
The meaning of a Program is defined to be themeaning of the body when given
an initial state consisting of the variables of thedecpart initialized to the undef value correspondingto the variable's type.
let p_meaning (decl_list, stat_list) = s_meaning (Block stat_list) State.empty
![Page 14: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/14.jpg)
22c:111 Programming Language Concepts - Fall 2008
Statements
s_meaning: statement → state → state
type statement =
Skip (* ; *)
| Block of statement list (* {…} *)
| Assign of string * expr (* x = a *)
| Cond of expr * statement * statement (* if b s1 else s2 *)
| Loop of expr * statement (* while (b) s *)
![Page 15: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/15.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.2 - Skip
The meaning of a Skip is the identity functionon the state;that is, the state is unchanged.
let rec s_meaning (s:statement) (q:state) =
match s with
Skip → q
| …
![Page 16: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/16.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.3 - Assignment
The output state is computed from the input state byreplacing the value of the target variable by thecomputed value of the source expression.
let rec s_meaning (s:statement) (q:state) =
match s with …
| Assign (x, e) -> let v = e_meaning e q in
State.update q x v
| …
![Page 17: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/17.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.4 - Conditional
The meaning of a conditional is:– If the meaning of the test is true, the meaning of the then-
branch;– Otherwise, the meaning of the else-branch
![Page 18: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/18.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.4 - ConditionalLet rec s_meaning (s:statement) (q:state) =
match s with …
| Cond (e, s1, s2) ->
(match (e_meaning e q) with
B true -> s_meaning s1 q
| … )
| …
![Page 19: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/19.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule - Looplet rec s_meaning (s:statement) (q:state) =
match s with …
| Loop (e, s’) ->
(match (e_meaning e q) with
B false -> q
| B true -> s_meaning (Block [s’; s]) q
| _ -> failwith "Impossible case” )
| …
![Page 20: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/20.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule - Block
let rec s_meaning (s:statement) (q:state) =
match s with …
| Block [] -> q
| Block s’::t ->
let q’ = s_meaning s’ q in
s_meaning (Block t) q’
![Page 21: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/21.jpg)
22c:111 Programming Language Concepts - Fall 2008
Expression Semantics
Defn: A side effect occurs during the evaluation of anexpression if, in addition to returning a value, theexpression alters the state of the program.
Ignore for now.
![Page 22: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/22.jpg)
22c:111 Programming Language Concepts - Fall 2008
Expressions
e_meaning: expr → state → value
type expr =
IntC of int
| BoolC of bool
| Var of var
| Unary of unaryOp * expr
| Binary of binaryOp * expr * expr
![Page 23: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/23.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rule 8.7
The meaning of an expression in a state is a valuedefined by:1. If a value, then the value. Ex: 32. If a variable, then the value of the variable in the state.3. If a Binary:
a) Determine meaning of term1, term2 in the state.
b) Apply the operator according to rule 8.8
...
![Page 24: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/24.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rules - Expressions
let rec e_meaning (e:expr) (q:state) =
match e with
IntC n → I n
| BoolC b → B b
| Var x → State.get_value q x
| …
![Page 25: 22c:111 Programming Language Conceptshomepage.divms.uiowa.edu/~tinelli/classes/111/Fall08/Notes/ch08.pdf · 22c:111 Programming Language Concepts - Fall 2008 Meaning Rule 8.1 The](https://reader035.vdocument.in/reader035/viewer/2022070914/5fb51cd7abed1e355f6479a5/html5/thumbnails/25.jpg)
22c:111 Programming Language Concepts - Fall 2008
Meaning Rules - Expressions
let rec e_meaning (e:expr) (q:state) =
match s with …
| Binary (op, e1, e2) →
let v1 = e_meaning e1 q in
let v2 = e_meaning e2 q in
b_meaning op v1 v2
| …