semantic function parsing s
play

Semantic Function Parsing s Denotational semantics operates - PDF document

Operational vs. denotational Operational semantics meaning of program defined by syntactic CS 611 transitions structural operational semantics: how to write a Advanced Programming Languages recursive-descent interpreter


  1. Operational vs. denotational • Operational semantics – meaning of program defined by syntactic CS 611 transitions – structural operational semantics: how to write a Advanced Programming Languages recursive-descent interpreter – meaning of language terms defined by other Andrew Myers language terms Cornell University ( λ x x) = ( λ x x) • Denotational semantics Lecture 10 – defines meaning of program in terms of underlying semantic domain (intrinsic meaning) Denotational semantics of IMP – semantic function maps expressions to meanings – how to write a compiler 15 Sep 00 CS 611 Fall '00 -- Andrew Myers, Cornell University 2 Semantic Function Parsing λ λ ’s λ λ • Denotational semantics operates on • Notation for describing a mathematical expressions to produce objects that are function of several variables: the meaning of the expression (usually λ xyz . e = λ x λ y λ z . e mathematical function) • Lambda expression extends as far to the right C � ( λ x x) � = λ x ∈ D. x as possible (like ∀ , ∃ ) λ x λ y λ z . x λ w. w = λ x ( λ y ( λ z . ( x ( λ w. w )))) λ not (( λ x ( λ y ( λ z . x ))) ( λ w. w )) { ( a , a ) | a ∈ D} x x • Application left-associates: xyz = ( xy ) z = x ( y,z ) f = λ xyz . e fabc = f ( a , b , c ) CS 611 Fall '00 -- Andrew Myers, Cornell University 3 CS 611 Fall '00 -- Andrew Myers, Cornell University 4 Typed functions Back to IMP • For mathematical functions, we will usually • Recall IMP has three kinds of expressions: write the types of the arguments a ::= n | X | a 0 + a 1 | a 0 – a 1 | a 0 × a 1 PLUS = λ x ∈ Z . λ y ∈ Z. x+y = λ x,y ∈ Z . x+y b ::= a 0 ≤ a 1 | a 0 = a 1 | b 0 ∧ b 1 | b 0 ∨ b 1 PLUS ∈ Z → ( Z → Z ) c ::= X := a 0 | skip | if b 0 then c 0 else c 1 | • Type ( T 1 → T 2 ) is domain of functions that while b 0 do c 0 maps elements from domain T 1 to domain T 2 a : Aexp , b : Bexp , c : Com • Application associates to the left � function What is the meaning of these three syntactic constructor ( → ) associates to right categories? Z → ( Z → Z ) = Z → Z → Z Does an element from a mean an integer? CS 611 Fall '00 -- Andrew Myers, Cornell University 5 CS 611 Fall '00 -- Andrew Myers, Cornell University 6 1

  2. Semantic functions for IMP Natural semantics as functions • Meaning functions A , B , C translate • Expression a denotes a unique integer given a syntactic expressions into meaning: particular store σ � a ,σ � � n mathematical functions • Expression b denotes a unique truth value given a particular store σ � b ,σ � � t A � a � σ = n A ∈ Aexp → ( Σ→ N ) • Command c maps one store into another B � b � σ = t B ∈ Bexp → ( Σ→ T ) � c ,σ � � σ � C � c � σ = σ ’ C ∈ Com → ( Σ→ Σ ) • Deterministic evaluation � exists functions A , B , C such that • A � a � is denotation of a ( A � a � ∈ Σ→ N ) A � a � σ = n ⇔ � a ,σ � � n • B � b � is denotation of b, C � c � is B � b � σ = t ⇔ � b ,σ � � t denotation of c C � c � σ = σ � ⇔ � c ,σ � � σ � CS 611 Fall '00 -- Andrew Myers, Cornell University 7 CS 611 Fall '00 -- Andrew Myers, Cornell University 8 Arithmetic denotations Semantic function as a set A � n � = λ σ∈Σ . n = {( σ, n ) | σ ∈ Σ } • The function A : Aexp →Σ→ Z is defined using induction on structure of exprs: A � a 0 + a 1 � = λ σ∈Σ . A � a 0 � σ + A � a 1 � σ = { ( σ, n 0 + n 1 ) | ( σ, n 0 ) ∈ A � a 0 � ∧ A � n � = λ σ ∈ Σ . n ( σ, n 1 ) ∈ A � a 1 � ) A � X � = λ σ ∈ Σ . σ X A � a 0 + a 1 � = λ σ ∈ Σ . A � a 0 � σ + A � a 1 � σ A � a 0 � = f 0 A � a 0 – a 1 � = λ σ ∈ Σ . A � a 0 � σ – A � a 1 � σ A � a 1 � = f 1 A � a 0 × a 1 � = λ σ ∈ Σ . A � a 0 � σ · A � a 1 � σ A � a 0 + a 1 � = λ σ ∈ Σ . f 0 σ + f 1 σ CS 611 Fall '00 -- Andrew Myers, Cornell University 9 CS 611 Fall '00 -- Andrew Myers, Cornell University 10 Boolean denotations Command denotations • Some commands do not terminate B ∈ Bexp →Σ→ T ( �� σ ’ . � c , σ � � σ ’) • Commands are partial functions from states B � a 0 = a 1 � σ = if A � a 0 � σ = A � a 1 � σ to states ( Σ � Σ) then true else false • Idea: make denotations total by adding B � a 0 ≤ a 1 � σ = if A � a 0 � σ ≤ A � a 1 � σ special state to represent non-termination: ⊥ then true else false • Domain Σ ⊥ has elements of Σ ∪ {⊥} ( lift of Σ) B � b 0 ∧ b 1 � σ = if B � b 0 � σ and B � b 1 � σ C ∈ Com →Σ ⊥ →Σ ⊥ then true else false • Advantage over large-step: can specify non- B � b 0 ∨ b 1 � σ = if B � b 0 � σ or B � b 1 � σ terminating behavior then true else false CS 611 Fall '00 -- Andrew Myers, Cornell University 11 CS 611 Fall '00 -- Andrew Myers, Cornell University 12 2

  3. Command denotations while C � skip � σ = σ • What we’d like to write: C � X := a � σ = σ [ X � A � a � σ ] C � while b do c � σ = C � if b then c 0 else c 1 � σ = if ¬ B � b � σ then σ if B � b � σ then C � c 0 � σ else C � c 1 � σ else C � while b do c � ( C � c � σ) C � c 0 ; c 1 � σ = C � c 1 � ( C � c 0 � σ ) • What’s wrong with this? Note: σ could be ⊥ ; need to wrap if σ = ⊥ then ⊥ else … around :=, if , while definitions CS 611 Fall '00 -- Andrew Myers, Cornell University 13 CS 611 Fall '00 -- Andrew Myers, Cornell University 14 while command Denotation as a fixed point C � while b do c � = C � while b do c � σ = {( σ, σ ’) | B � b � σ & ( σ, σ ’) ∈ C � while b do c � � C � c � } if B � b � σ then σ ∪ {( σ, σ ) | ¬ B � b � σ } else C � while b do c � ( C � c � σ) Define Γ ( f ) where f is a command denotation • This is an equation , not a definition (induction Γ = λ f ∈Σ ⊥ →Σ ⊥ . {( σ, σ ’) | B � b � σ & ( σ, σ ’) ∈ f � C � c � } is not well-founded) ∪ {( σ, σ ) | ¬ B � b � σ } = λ f ∈Σ ⊥ →Σ ⊥ . if B � b � σ then σ else f ( C � c � σ ) C � while b do c � = C � while b do c � = Γ ( C � while b do c � ) {( σ, σ ’) | B � b � σ & ( σ, σ ’) ∈ C � while b do c � � C � c � } ∪ {( σ, σ ) | ¬ B � b � σ } Denotation of while is fixed point of Γ CS 611 Fall '00 -- Andrew Myers, Cornell University 15 CS 611 Fall '00 -- Andrew Myers, Cornell University 16 Denotation of while C � while b do c � σ = fix ( λ f . if B � b � σ then σ else f ( C � c � σ)) • Question: how do we define least fixed point operator fix for domain Σ ⊥ →Σ ⊥ ? • Answer: next lecture CS 611 Fall '00 -- Andrew Myers, Cornell University 17 3

Recommend


More recommend