280 likes | 293 Views
Lecture 17: Denotational Semantics. David Evans http://www.cs.virginia.edu/~evans. Decide what you want to say before you worry about how you are going to say it. Strachey’s first law of programming. CS655: Programming Languages University of Virginia Computer Science. Menu.
E N D
Lecture 17: Denotational Semantics David Evans http://www.cs.virginia.edu/~evans Decide what you want to say before you worry about how you are going to say it. Strachey’s first law of programming CS655: Programming Languages University of Virginia Computer Science
Menu • Trial Verdict (and Punishment?) • Denotational Semantics Everyone should have received grade record email. Use this to decide whether or not to do Position Paper 5. University of Virginia CS 655
Trial Verdict University of Virginia CS 655
Semantics Summary So Far • Operational Semantics • Static Semantics (Type Judgments) • Axiomatic Semantics • Lambda Calculus • Today: Denotational Semantics Hint: on your final you should be able to decide which of these is best for a particular problem. University of Virginia CS 655
Apples and Oranges Adding apples and oranges is easy! Just decide what they denote. + = 17 Denotes addition Denotes 2 Denotes 3 Juxtaposition denotes multiplication University of Virginia CS 655
Denotational Semantics • Invented by Strachey and Scott (Oxford, 1970s); popularized by Stoy and Milne in mid-late 70s • Map syntactic units in program to semantic units • Combine them using rules to get the meaning of the whole program University of Virginia CS 655
M + M * M 3 M 3 M * M * M 2 M 2 M 2 Semantic Algebra Meaning Function + Syntactic Algebra Homomorphism – the tree shape is the same University of Virginia CS 655
Apples & Oranges Language (AOL) Program ::= Expression Expression ::= Expression * Expression | Expression + Expression | IntLiteral Semantic domain: Integer = { ..., -2, -1, 0, 1, 2, ... } Meaning Functions: P : Program Integer E : Expression Integer N : IntLiteral Integer University of Virginia CS 655
Defining the Meaning Functions P [[ Expression ]] = E [[ Expression ]] E [[ E1 *E2 ]] = E [[ E1 ]]E [[ E2 ]] E [[ E1 +E2 ]] = E [[ E1 ]]E [[ E2 ]] E [[ IntLiteral ]] = N [[ IntLiteral ]] N [[ IntLiteral ]] = the integer number denoted by IntLiteral University of Virginia CS 655
Example P [[ 2 * 2 * 2 + 3 * 3 ]] = E [[2 * 2 * 2 + 3 * 3 ]] = E [[2 * 2 * 2]]E [[3 * 3]] = (E [[2]] E [[2 * 2]]) (E[[3]] E [[3]]) = (E [[2]] E [[2]] E [[2]]) (E[[3]] E [[3]]) = (N [[2]] N[[2]] N[[2]]) (N[[3]] N[[3]]) = (2 2 2) (3 3) = 17 University of Virginia CS 655
A More Interesting Language Program ::= StatementList StatementList ::= empty | Statement ; StatementList Statement ::= Variable := Expression Expression ::= Expression + Expression | Variable | IntLiteral Semantic domain: = set of States where State = : Variable Integer Integer = { ..., -2, -1, 0, 1, 2, ... } University of Virginia CS 655
Meaning Function Domains P : Program ( ) L : StatementList ( ) S : Statement ( ) Programs and Statements map states to states. E : Expression ( Integer) Expressions map states to values. N : IntLiteral ( Integer) University of Virginia CS 655
Expressions N [[ IntLiteral ]]: IntLiteral ( Integer) ={ (, n) | & n = number denoted by IntLiteral } this means: . n E [[ IntLiteral ]]: Expression ( Integer) = { (, N [[ IntLiteral ]] ) | } E [[ Variable ]]: Expression ( Integer) = { (, (Variable)) | } E [[ E1 +E2 ]] : Expression ( Integer) = { (, n1 n2) | (, n1) E [[ E1 ]] &(, n2) E [[ E2 ]] } University of Virginia CS 655
Statement S [[Variable := Expression]] : Statement ( ) = { (, [n/Variable]) | & n = (E [[ Expression ]]) } University of Virginia CS 655
Statement Lists L : StatementList ( ) L [[]] = { (, ) | } L [[Statement ; StatementList ]] = L [[StatementList]] S [[Statement]] Why backwards? L [[Statement ; StatementList ]] = L [[StatementList]] (S [[Statement]] ) University of Virginia CS 655
Program P[[StatementList]] : Program ( ) = { (, s) | (, s) L [[StatementList]] } University of Virginia CS 655
Example x := x + 1; a := x; L [[x := x + 1; a := x; ]] = L [[a := x;]] S [[x := x + 1]] S [[x := x + 1]] = { (, [n/x]) | & n = (E [[ x + 1]]) } E [[ x + 1]] = { (, n1 n2) | (, n1) E [[x]] &(, n2) E [[1]] } University of Virginia CS 655
Example, cont. E [[x]] = { (, (x)) | } E [[1]] = { (, N [[1]]) | } = { (, 1) | } E [[ x + 1]] = { (, n1 n2) | (, n1) { (, (x)) | } &(, n2) { (, 1) | }} = { (, (x) 1) | } S [[x := x + 1]] = { (, [n/x]) | & n = ({E [[ x + 1]]) } = { (, [(x) 1/x]) | } University of Virginia CS 655
Example, cont. S [[x := x + 1]] = { (, [(x) 1/x]) | } L [[x := x + 1; a := x;]] = L [[a := x;]] S [[x := x + 1]] = L [[a := x;]] { (, [(x) 1/x]) | } L [[a := x;]] = L [[]] S [[a := x]] L [[]] = { (, ) | } S [[a := x]] = { (, [n/a]) | & n = (E [[x]]) } = {(, [(x)/a]) | } University of Virginia CS 655
Example, cont. L [[a := x;]] = { (, ) | } {(, [(x)/a]) | } = {(, [(x)/a]) | } L [[x := x + 1; a := x;]] = {(, [(x)/a]) | } { (, [(x) 1/x]) | } = { (, ’[’(x)/a]) | , ’ = [(x) 1/x] } = { (, [(x+1)/a][(x) 1/x]) | } University of Virginia CS 655
So what? • It took 4 slides to understand x := x + 1; a := x; • Is this better than operational semantics? • Yes, we have produced a state transition function that shows what x := x + 1; a := x; does from any initial state. • Observational equivalence to any statement list with the same state transition function. University of Virginia CS 655
If Statement ::= if Predicate then StatementList else StatementList end assume there is B[[Predicate]]: ( Boolean) S [[if b then s1 else s2 end]] = { (, ’) | B[[Predicate]] = true & (, ’) L [[s1]]} { (, ’) | B[[Predicate]] = false & (, ’) L [[s2]]} University of Virginia CS 655
While Statement ::= while Predicate do StatementList end w = while Bdo Send is equivalent to ifB then S; w else ; So, S [[w]] must equal S [[ifB then S; w else ;]] University of Virginia CS 655
Whiling Away S [[w]] = {(, ’) | B[[B]] = true & (, ’) L [[S; w]]} {(, ’) | B[[B]] = false & (, ’) L [[;]]} = {(, ’) | B[[B]] = true & (, ’) S [[w]] L [[S]]} { (, ) | B[[B]] = false} Yikes! A recursive definition – what about fixed points? University of Virginia CS 655
Generating Function G (w) = {(, ’) | B[[B]] = true & (, ’) w L [[S]]} { (, ) | B[[B]] = false} The fixed point of G (w) is S [[w]] where w = while Bdo Send. University of Virginia CS 655
Challenge • Prove or disprove: i := 1; while i < n do i := i + 1; x := x * i; end; i := 0; is observationally equivalent to: i := n; while i > 0 do x := x * i; i := i - 1; end; i := 0; when n >= 0. • Worth 1 position paper point. University of Virginia CS 655
Summary • Denotational Semantics: map program fragments to semantic domain • Popular for defining languages (in 80s, less in favor today) • Basis for most type inference algorithms • Good for reasoning about behavior of program fragments • Hard to deal with control flow – while depends on fixed point machinery, goto essentially impossible University of Virginia CS 655
Charge • Read Cardelli’s “Basic Polymorphic Typechecking” paper for Thursday • If ambitious, try reading section of FL paper that described language using denotational semantics • Keep working on your projects – but don’t get so caught up in implementation you lose big picture University of Virginia CS 655