formal semantics of programming language s

21
Formal Semantics of Programming Languages 虞虞虞 [email protected] Topic 4: Denotational Semantics

Upload: kitty

Post on 23-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

Formal Semantics of Programming Language s. Topic 4: Denotational Semantics. 虞慧群 [email protected]. Motivation. Operational semantics to IMP is syntax-oriented, which makes it difficult to compare two programs written in different programming languages. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Formal  Semantics  of  Programming Language s

Formal Semantics of Programming Languages

虞慧群[email protected]

Topic 4: Denotational Semantics

Page 2: Formal  Semantics  of  Programming Language s

Motivation

Operational semantics to IMP is syntax-oriented, which makes it difficult to compare two programs written in different programming languages.

A more abstract (syntax independent) level for the semantics alleviates this. Denotational semantics is such a semantics.

Page 3: Formal  Semantics  of  Programming Language s

Outline

Denotational semantics of IMP Equivalence of Semantics Complete partial orders and continuous functions

Page 4: Formal  Semantics  of  Programming Language s

Denotational semantics A: Aexp (N) B: Bexp (T) C: Com ( ) Defined by structural induction

Page 5: Formal  Semantics  of  Programming Language s

Denotational semantics of Aexp

A: Aexp (N) A [n] = {(, n) | } A [X] = {(, (X)) | } A [a0+a1] = {(, n0+n1) | (, n0)A[a0]&(,n1)A[a1]}

A [a0-a1] = {(, n0-n1) | (, n0)A[a0]&(,n1)A[a1]}

A [a0a1] = {(, n0 n1) | (, n0)A[a0]&(,n1)A[a1]}

Lemma: A [a ] is a function

Page 6: Formal  Semantics  of  Programming Language s

Equivalence of Semantics

Lemma 5.3: For all a Aexp A[a]={(, n) | < a, > n}

Proof P(a) A[a]={(, n) | < a, > n} Structural induction

Page 7: Formal  Semantics  of  Programming Language s

Denotational semantics of Bexp

B: Bexp (T) B [true] = {(, true) | } B [false] = {(, false) | } B [a0=a1] = {(, true) | & A[a0]=A[a1] }

{(, false) | & A[a0] A[a1] }

B [a0a1] = {(, true) | & A[a0] A[a1] } {(, false) | & A[a0]A[a1] }

B [b] = {(, T t) | , (, t) B[b]}

B [b0b1] = {(, t0 Tt1) | , (, t0) B[b0], (, t1) B[b1] }

B [b0b1] = {(, t0 Tt1) | , (, t0) B[b0], (, t1) B[b1] }

Lemma: B[b] is a function

Page 8: Formal  Semantics  of  Programming Language s

Equivalence of Semantics

Lemma 5.4: For all b Bexp B[b]={(, t) | < b, > t}

Proof P(b) B[b]={(, t) | < b, > n} Structural induction

Page 9: Formal  Semantics  of  Programming Language s

Denotational semantics of Com C[skip]={(, )| } C [X:=a] = {(, [n/X]) | & n=A[a]} C [c0;c1] = C [c1] C [c0] C [if b then c0 else c1] =

{(, ’) | B[b]=true & (, ’) C[c0]} {(, ’) | B[b]=false & (, ’) C[c1]}

C [while b do c] = fix() where() = {(, ’) | B[b]=true & (, ’) C[c]} {(, ) | B[b]=false}

Page 10: Formal  Semantics  of  Programming Language s

Operational Semantics of Commands

/<skip, > {<a, > n}/<X:=a, > [n/X]

{<c0, > ’’, <c1, ’’ > ’ }/<c0;c1, > ’

{<b, > true, <c0, > ’ }/<if b then c0 else c1, > ’{<b, > false, <c1, > ’ }/<if b then c0 else c1, > ’{<b, > false }/<while b do c , > {<b, > true, <c, > ’’, <while b do c, ’’ ’ >}/ <while b do c, > ’

Page 11: Formal  Semantics  of  Programming Language s

Equivalence of Semantics

Lemma 5.6: For all c Com <, c> ’ ( , ’) Cc

Proof P(c, , ’) ( , ’) Cc Rule induction

Page 12: Formal  Semantics  of  Programming Language s

Equivalence of Semantics

Theorem 5.7 For all c Com Cc = {( , ’) | <c, > ’ }

Proof , ’: (, ’) Cc <c, > ’ Structural++ induction

Page 13: Formal  Semantics  of  Programming Language s

Partial Orders A partial order (p.o.) is a set P with a binary

relation reflexive p: p P. p p transitive: p, q, r P. p q & q r p r antisymmetric: p, q P. p q & q p p=q

For a partial order (P, ) and a subset XP p is an upper bound of X

q X. q p p is a least upper bound of X (denoted by U X) if

p is an upper bound of X For all upper bounds q of X: p q

d1 U d2 U … dn = U{d1, d2, …, dn}

Page 14: Formal  Semantics  of  Programming Language s

Partial Orders For a partial order (P, ) and a subset XP

p is an lower bound of X q X. p q

p is a greatest lower bound of X (denoted by X) if p is a lower bound of X For all lower bounds q of X: q p

Examples (Pow(X), ) (Pow(X), ),

Page 15: Formal  Semantics  of  Programming Language s

Complete Partial Orders Let (D, ) be a partial order

An -chain is an increasing chain d0 d1 …dn ..

D is a complete partial order (c.p.o) if every -chain has a least upper bound

D is a complete partial order with bottom if is a c.p.o. with a minimum element .

D is a complete lattice if every subset has both greatest and lower bounds

Example (Pow(X), )

Page 16: Formal  Semantics  of  Programming Language s

Monotonic and Continuous Functions A function f: DE between cpos D and E is

monotonic if d,d’ D.d d’ f(d) f(d’)

A function: DE between cpos D and E is continuous if for all chains

d0 d1 …dn … in D: n f(dn) = f( n dn)

Page 17: Formal  Semantics  of  Programming Language s

Scott’ Thesis

Any computable function is continuous preserve information order

More information as input gives more information as output

preserve limits The total information obtainable as output from an

infinite sequence of input elements with refinement information is the sum of total of all information obtainable from each input seperately

Page 18: Formal  Semantics  of  Programming Language s

Fixed Points

Let f: D D be a continuous function on a cpo with in D.

A fixed point of f is an element d D such that f(d) =d

A pre fixed point of f is an element d D such that f(d) d

Thm: fix(f) = n fn()

Page 19: Formal  Semantics  of  Programming Language s

Knaster-Tarski Theorem

Let f: L L be a monotonic function on a complete lattice L

The least fixed point lfp(f) exists

lfp(f) = {x L: xf(x)}

Page 20: Formal  Semantics  of  Programming Language s

Summary

Denotational semantics provides a way to declare the meaning of programs in an abstract way Can handle side-effects Loops Recursion Gotos

Fixed point theory provides a declarative way to specify computations Many usages

Page 21: Formal  Semantics  of  Programming Language s

Exercise 4

(1)

(2)