1 / 16

CS 611: Lecture 11

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

mea
Download Presentation

CS 611: Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 611: Lecture 11 Denotational Semantics September 22, 1999 Cornell University Computer Science Department Andrew Myers

  2. 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

  3. 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

  4. Back to IMP • Recall IMP has three kinds of expressions: a ::= n | X | a0 + a1 | a0 – a1 | a0 × a1 b ::= a0a1 | a0=a1 | b0b1 | b0b1 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

  5. 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 (SN) B b s = t B : Bexp (ST) C c s = s’C : Com (S S) CS 611—Semantics of Programming Languages—Andrew Myers

  6. 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

  7. 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

  8. Denotations Semantic (meaning) Functions:A a s = n A : Aexp SZB b s = t B : Bexp STC c s = sC : Com S S Aa : SN, Bb : ST, C c : S S Aa is the denotation of the arithmetic expression a The expression a denotes the function from states to integers Aa CS 611—Semantics of Programming Languages—Andrew Myers

  9. Arithmetic denotations • The function A : Aexp SZ 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 a0s – A a1 s A a0 × a1= l s : S . A a0s ·A a1 s CS 611—Semantics of Programming Languages—Andrew Myers

  10. 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

  11. Boolean denotations B : Bexp ST B a0 = a1= l s : S . if A a0 s = A a1strueelse false B a0a1= l s : S . if A a0 sA a1strueelse false B b0b1  = l s : S . if B b0 sandB b1sthen true else false B b0b1 = l s : S . if B b0 sorB b1s then true else false CS 611—Semantics of Programming Languages—Andrew Myers

  12. Command denotations • Commands are partial functions from states to states (C : Com SS) 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

  13. 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 bs & (s, s’) C while b do cC c}  {(s, s) | B bs } CS 611—Semantics of Programming Languages—Andrew Myers

  14. While denotation as a fixed point C while b do c ={(s, s’) | B bs & (s, s’) C while b do cC c}  {(s, s) | B bs } Define G(f) where f is the command denotation G = l f: SS . {(s, s’) | B bs & (s, s’)  fC c}  {(s, s) | B b s } = l f: SS . 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

  15. 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 bs = false (s,s’’)  C c (s’’,s’)  f (s,s’)  f B bs = true (s,s)  f • Rule induction: least fixed point exists CS 611—Semantics of Programming Languages—Andrew Myers

  16. 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

More Related