48
Denotational Semantics COS 441 Princeton University Fall 2004

Denotational Semantics

  • Upload
    kaleb

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Denotational Semantics. COS 441 Princeton University Fall 2004. Denotational Semantics. Describe meaning of syntax by specifying the mathematical meaning of syntax tree as Function Function on functions Value, such as natural numbers or strings Sets of values … etc - PowerPoint PPT Presentation

Citation preview

Page 1: Denotational  Semantics

Denotational Semantics

COS 441

Princeton University

Fall 2004

Page 2: Denotational  Semantics

Denotational Semantics

Describe meaning of syntax by specifying the mathematical meaning of syntax tree as– Function– Function on functions– Value, such as natural numbers or strings– Sets of values … etc

defined by each construct

Page 3: Denotational  Semantics

Original Motivation for Topic

• Precision– Use mathematics instead of English

• Avoid details of specific machines– Aim to capture “pure meaning” apart from

implementation details

• Basis for program analysis– Justify program proof methods

• Soundness of type system, control flow analysis

– Proof of compiler correctness– Language comparisons

Page 4: Denotational  Semantics

Denotational vs. Operational

• Denotational semantics are more “abstract” than operational approaches

• Cannot reason about number of steps of a computation or algorithmic complexity

• Specify what the answer should be not how a computation takes place

Page 5: Denotational  Semantics

Principles of DS

CompositionalityThe meaning of a compound program must be

defined from the meanings of its parts (not the syntax of its parts).

ExamplesP; Q composition of two functions, state stateletrec f(x) = e1 in e2 meaning of e2 where f denotes function ...

Page 6: Denotational  Semantics

Notation and Definitions

Phrase

variable, expression, statement, declarations

[[ e ]],[[ P ]]

Syntax tree of a phrase

E[[ e ]], C [[ P ]]

Meaning functions (E,C) applied to syntax trees of phrases

Page 7: Denotational  Semantics

Example: Binary Numbers

Syntaxb ::= 0 | 1

n ::= b | nb

e ::= n | e_1 + e2

Semantics

E [[0]] 0

E [[1]] 1

E [[nb]] 2 £ E[[ n ]] + E[[ b ]]

E [[e1 + e2]] E[[ e1 ]] + E[[ e2 ]]

Page 8: Denotational  Semantics

DS of Regular Expressions

Syntax

a,b 2 re ::= a | re1 + re2 | re1 . re2 | re*

Semantics

E : Regular Exp ! *

Page 9: Denotational  Semantics

Sets of Strings

Let a,b 2 is an unspecified alphabet

* is the set of strings made up from the alphabet

2 *

a 2 * if a 2 s1 . s22 * if s12 * and s2 2 *

Page 10: Denotational  Semantics

Equality on Strings

Let s1,s2,s3 2 *

a a

. s s

s . s

(s1 . s2) . s3 s1 . (s2 . s3)

s1 . s2 s’1 . s’2 if s1 = s’1 and s2 = s’2

Page 11: Denotational  Semantics

Operations On Sets of Strings

{a} is { x | x = a }

S1 [ S2 is { x | x 2 S1 Ç x 2 S2 }

S1 . S2 is { x | s1 2 S1 Æ s2 2 S2 Æ x = s1 . s2 }

S0 is { }

Sn+1 is { x | x 2 S . Sn }

S* is { x | 9n. x 2 Sn }

Page 12: Denotational  Semantics

Some Theorems

E[[a + b]] {a , b}

E[[a . (b + c)]] {ab, ac}

E[[(a + b) . c]] {ac, bc}

E[[re1 . (re2 + re3)]] E[[(re1 . re2) + (re1 . re3)]]

E[[a**]] E[[a*]]

Page 13: Denotational  Semantics

DS for Regular Expressions

E[[a]] {a}

E[[re1 + re2]] { x | x 2 E[[re1]] [ E[[re2]] }

E[[re1 . re2]] { x | x 2 E[[re1]] . E[[re2]] }

E[[re*]] { x | x 2 E[[re]]* }

Page 14: Denotational  Semantics

Another DS for REs

• We can given an equivalent semantics by relating regular expressions to Non-deterministic Finite Automata (NFAs)

• Equivalent means– Any theorem about meanings in one

semantics is true iff the same theorem is true in the other model

Page 15: Denotational  Semantics

NFA

q0,q1,…,qn 2 Q States

qinit 2 Q Initial State

F ½ Q Final States

½ Q £ ( [ {}) £ Q Transition Relation

M is (qinit,F,) NFA

Page 16: Denotational  Semantics

NFA Accepting a String

2 L(qinit,F,)

if (qinit, , qf) 2 and qf 2 F

a 2 L(qinit,F,)

if (qinit, a, qf) 2 and qf 2 F

x . s 2 L(qinit,F,)

if (qinit, x, q’) 2 and s 2 L(q’,F,)

Page 17: Denotational  Semantics

Semantics of Regular Expressions

NFA[[a]] (qi, {qf}, {(qi,a,qf)})

qf

aqi

Page 18: Denotational  Semantics

Semantics of Regular Expressions

NFA[[re1 + re2]] let q’ be a unique new state

let (qi1, F1, 1) = NFA[[re1]]

let (qi2, F2, 2) = NFA[[re2]]

let ’ = 1 [ 2 [ {(q’,,qi1),(q’,,qi2)})

(q’, F1 [ F2,’)

q’

NFA[[re1]]

NFA[[re2]]

Page 19: Denotational  Semantics

Semantics of Regular Expressions

NFA[[re1 . re2]]

let (qi1, F1, 1) = NFA[[re1]]

let (qi2, F2, 2) = NFA[[re2]]

let ’ = 1 [ 2 [ {(qf,,qi2) | qf 2 F1}

(qi1, F2,’)

NFA[[re1]] NFA[[re2]]

Page 20: Denotational  Semantics

Semantics of Regular Expressions

NFA[[re*]] let q’,qf be new states

let (qi, F, ) = NFA[[re]]

let ’ = [ {(q’,,qi), (q’,,qf)} [ { (qf,qi) | qf 2 F }

(q’, {qf},’)

NFA[[re]]q’ qf

Page 21: Denotational  Semantics

Some More Theorems

L(NFA[[a + b]]) {a , b}

L(NFA[[a . (b + c)]]) {ab, ac}

L(NFA[[(a + b) . c]]) {ac, bc}

L(NFA[[re]]) E[[re]]

Page 22: Denotational  Semantics

E[[re]] L(NFA[[re]])

• We can prove the NFA semantics is equivalent to the set theoretic semantics via induction on the definition of the meaning function E

• Proof relies on compositional definition of meaning function E!

Page 23: Denotational  Semantics

Which Semantics?

• Set of string semantics clearly easier to reason about!– This is what denotation semantics was

designed for

• NFA semantics can be transformed into efficient of regular expression matcher– Our theorem is a correctness proof about the

implementation of our NFA conversion function

Page 24: Denotational  Semantics

DS for Programming Languages

• We can build a DS for a programming language

• Allows us to reason about program equivalence– Useful to prove compiler optimizations is safe

• Provides framework for reason about abstract properties of programs statically

Page 25: Denotational  Semantics

DS of Imperative Programs

Syntaxn 2 Numbers

x 2 Vars

e ::= false | true | n | x | e1 + e2 | e1 · e2

P ::= x := e | if e then P1 else P2 | P1;P2

| while e do P

Page 26: Denotational  Semantics

Semantics for Expression

Meaning function for expressions

State = Vars ! Numbers

E : Expressions ! State ! Numbers

E[[false]](s) 0

E[[true]](s) 1

E[[n]](s) n

E[[x]](s) s(x)

E[[e1 + e2]](s) E[[e1]](s) + E[[e2]](s)

E[[e1 · e2]](s) if E[[e1]] · E[[e2]] then 1 else 0

Page 27: Denotational  Semantics

Semantics for Programs

Meaning function for programsCommand = State ! State C : Programs ! Command

C[[P;Q]](s) C[[Q]](C[[P]](s)) C[[if e then P else Q]](s) C[[P]](s) if E [[e]](s) = 1

C[[Q]](s) if E [[e]](s) = 0

Page 28: Denotational  Semantics

Semantics of Assignment

modify : State £ Vars £ Numbers State

modify(s,x,a) = y. if y = x then a else s(y)

C[[x:= e]](s) modify(s,x,E[[e]](s))

Page 29: Denotational  Semantics

Semantics of Iteration

C[[while e do P]](s)

The function f such that

f(s) = s if E[[e]](s) = 0

f(s) = f(C[[P]](s)) if E[[e]] = 1

Mathematics of denotational semantics: prove that there is such a function and that it is uniquely determined. “Beyond scope of this course.”

Page 30: Denotational  Semantics

Mathematical FoundationsA full DS for imperative programs requires the definition of a special

class of sets called “domains”

From Wikipedia, the free encyclopedia.Dana S. Scott is the incumbent Hillman University Professor of

Computer Science, Philosophy, and Mathematical Logic at Carnegie Mellon University. His contributions include early work in automata theory, for which he received the ACM Turing Award in 1976, and the independence of the Boolean prime ideal theorem.

Scott is also the founder of domain theory, a branch of order theory that is used to model computation and approximation, and that provides the denotational semantics for the lambda calculus.

He received his Bachelor's degree from the University of California, Berkeley in 1954, and his Ph.D. from Princeton University in 1958

Page 31: Denotational  Semantics

Abstract Interpretation

From Wikipedia, the free encyclopedia.Abstract interpretation is a theory of sound approximation

of the semantics of computer programs, based on monotonic functions over ordered sets, especially lattices. It can be viewed as a partial execution of a computer program which gains information about its semantics (e.g. control structure, flow of information) without performing all the calculations.

…Abstract interpretation was formalized by Patrick Cousot.

Page 32: Denotational  Semantics

Abstract vs. Standard Semantics

Given a standard semantics abstract interpretation “approximates” the standard semantics in a way that guarantees the abstract interpretation is correct with respect to the original semantics

The original DS semantics again is used as part of a correctness criterion for the abstract semantics

Page 33: Denotational  Semantics

The Meaning of Meaning

• The denotational relates of syntax trees to objects in mathematical functions expressed using a metalanguage for defining functions

• Strachey and Scott used the mathematical theory of continuous function over partially ordered sets (domains) as their “target” semantics

• We can explain the meaning of programs in using the Strachey and Scott metalanguage

Page 34: Denotational  Semantics

DS Scheme

Formal semantics of Scheme is defined using the Strachey and Scott metalanguage

http://www.schemers.org/Documents/Standards/R5RS/r5rs.pdf

A non-trivial semantics language

Page 35: Denotational  Semantics

Abstract Syntax of Scheme

Page 36: Denotational  Semantics

Semantic Values

Page 37: Denotational  Semantics

Semantic Values (cont.)

Page 38: Denotational  Semantics

Semantic Functions

Page 39: Denotational  Semantics

Semantic Functions (cont.)

Page 40: Denotational  Semantics

Semantic Functions (cont.)

Page 41: Denotational  Semantics

SML as a Metalanguage

• Scheme semantics looks awfully like a complicated program written in a obscure language of functions which happen to have a precise well-understood meaning (If you’re a mathematician that is)

• We could also use SML as a metalanguage to express semantics!

Page 42: Denotational  Semantics

Semantics of Expressions in SML

type var = stringdatatype exp = | True | False| N of int

| V of var | Plus of exp * exp | Minus of exp * exp | Leq of exp * exp

Page 43: Denotational  Semantics

Semantics of Expressions in SML

type value = int type state = var -> value (* val expSem : exp -> state -> value *) fun expSem (True)(s) = 1 | expSem (False)(s) = 0 | expSem (N i)(s) = i | expSem (V v)(s:state) = s v | expSem (Plus(e1,e2))(s) =

expSem(e1)(s) + expSem(e2)(s) | expSem (Minus(e1,e2))(s) =

expSem(e1)(s) - expSem(e2)(s) | expSem (Leq(e1,e2))(s) =

if expSem(e1)(s) <= expSem(e2)(s)

then 1 else 0

Page 44: Denotational  Semantics

Summary

• Denotational techniques provide give meaning to programs by relating syntax to the semantics of some well-defined metalanguage

• DS are abstract semantics that are good to reason about correctness of implementations or static analysis

Page 45: Denotational  Semantics

Next Lecture

• Using denotationial techniques to specify the semantics of resolution independent graphical objects

• Homework for this week turn our semantics into SML code that takes a simple picture language into an image!

Page 46: Denotational  Semantics
Page 47: Denotational  Semantics
Page 48: Denotational  Semantics