200 likes | 521 Views
CS 611: Lecture 11. Denotational Semantics September 22, 1999 Cornell University Computer Science Department Andrew Myers. Limits of operational semantics. Operational semantics meaning of program defined by actions
E N D
CS 611: Lecture 11 Denotational Semantics September 22, 1999 Cornell University Computer Science Department Andrew Myers
Limits of operational semantics • Operational semantics • meaning of program defined by actions • structural operational semantics: how to write a recursive-descent interpreter • translates language terms into other language terms • (l x x) = (l x x) • Denotational semantics • meaning of program defined in terms of underlying semantic domain • translates program into a mathematical function • how to write a compiler • (l x x) = l x . x CS 611—Semantics of Programming Languages—Andrew Myers
Functions • Denotational semantics operates on expressions to produce functions that are the meaning of the expression C (l x: x) = l x . x l { (a, a) | a D} x x (l, x, x) CS 611—Semantics of Programming Languages—Andrew Myers
Back to IMP • Recall IMP has three kinds of expressions: a ::= n | X | a0 + a1 | a0 – a1 | a0 × a1 b ::= a0a1 | a0=a1 | b0b1 | b0b1 c ::= X := a0 | skip | if b0 then c0 else c1 | while b0 do c0 a : Aexp, b: Bexp, c: Com What is the meaning of these three syntactic categories? Does an element from a mean an integer? CS 611—Semantics of Programming Languages—Andrew Myers
Semantic functions for IMP • Expression a denotes an integer given a particular store s • Expression b denotes an truth value (T) given a particular store s • Command c maps one store into another A a s = n A : Aexp (SN) B b s = t B : Bexp (ST) C c s = s’C : Com (S S) CS 611—Semantics of Programming Languages—Andrew Myers
Lambda notation • Notation for describing a mathematical function of several variables:lxyz . e = lxlylz . e • Lambda expression extends as far to the right as possible (like , )lxlylz . x lw. w = lx(ly(lz . (x (lw. w))))not ((lx(ly(lz . x)))(lw. w)) • Application left-associates:xyz = (xy)z = x(y,z)f = lxyz . efabc = f(a,b,c) CS 611—Semantics of Programming Languages—Andrew Myers
Typed functions • For mathematical functions, we will usually write the types of the arguments PLUS = lx:Z . ly:Z. x+y = lx,y:Z . x+y PLUS : Z (Z Z) • Type (T1 T2) is a function that maps elements from domain T1 to domain T2 • Application associates to the left function constructor () associates to right Z (Z Z) = Z Z Z CS 611—Semantics of Programming Languages—Andrew Myers
Denotations Semantic (meaning) Functions:A a s = n A : Aexp SZB b s = t B : Bexp STC c s = sC : Com S S Aa : SN, Bb : ST, C c : S S Aa is the denotation of the arithmetic expression a The expression a denotes the function from states to integers Aa CS 611—Semantics of Programming Languages—Andrew Myers
Arithmetic denotations • The function A : Aexp SZ is defined using structural induction: A n= l s : S . n A X= l s : S . sX A a0 + a1= l s : S . A a0 s + A a1 s A a0 – a1= l s : S . A a0s – A a1 s A a0 × a1= l s : S . A a0s ·A a1 s CS 611—Semantics of Programming Languages—Andrew Myers
A a0 = f0 A a1 = f1 A a0 + a1 = l s : S . f0s+ f1s Semantic function as a set A n= l s : S . n = {(s, n) | s S} A a0 + a1= l s : S . A a0 s + A a1 s = { (s, n0 + n1) : (s, n0)A a0 (s, n1)A a1) CS 611—Semantics of Programming Languages—Andrew Myers
Boolean denotations B : Bexp ST B a0 = a1= l s : S . if A a0 s = A a1strueelse false B a0a1= l s : S . if A a0 sA a1strueelse false B b0b1 = l s : S . if B b0 sandB b1sthen true else false B b0b1 = l s : S . if B b0 sorB b1s then true else false CS 611—Semantics of Programming Languages—Andrew Myers
Command denotations • Commands are partial functions from states to states (C : Com SS) C skip s = s C X := a s = s[X C a s] C if b then c0 else c1 s =if B b s then C c0 s else C c1 C c0 ; c1 s = C c1 (C c0 s) C while b do c s =if B b s then selse C while b do c (C c s) CS 611—Semantics of Programming Languages—Andrew Myers
while command C while b do c s =if B b s thenselse C while b do c (C c s) • This is an equation, not a definition (induction is not necessarily well-founded) C while b do c ={(s, s’) | B bs & (s, s’) C while b do cC c} {(s, s) | B bs } CS 611—Semantics of Programming Languages—Andrew Myers
While denotation as a fixed point C while b do c ={(s, s’) | B bs & (s, s’) C while b do cC c} {(s, s) | B bs } Define G(f) where f is the command denotation G = l f: SS . {(s, s’) | B bs & (s, s’) fC c} {(s, s) | B b s } = l f: SS . if B b s thens else f(C c s) f = G(f ) Denotation of while is fixed point of G CS 611—Semantics of Programming Languages—Andrew Myers
Finding the fixed point • Denotation for while is defined as least fixed point of the equation f = G(f ) • Least fixed point is function that maps fewest states : while command diverges whenever it can while satisfying f = G(f ) • Can set up definition of f as inference rules: B bs = false (s,s’’) C c (s’’,s’) f (s,s’) f B bs = true (s,s) f • Rule induction: least fixed point exists CS 611—Semantics of Programming Languages—Andrew Myers
Denotation of while C while b do c s = fix ( lf. if B b s thens else f(C c s)) CS 611—Semantics of Programming Languages—Andrew Myers