Denotational Semantics - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Denotational Semantics

Description:

Define meaning functions to map syntactic constructs into their ... r ? Assign = P Bool assignments (values of names) The meaning of propositional formulae ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 22
Provided by: Schon
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: Denotational Semantics


1
Denotational Semantics
  • Syntax-directed approach, generalization of
    attribute grammars
  • Define context-free abstract syntax
  • Specify syntactic categories (terminals,
    non-terminals)
  • Specify value domains for semantic (meaning)
    functions
  • Define meaning functions to map syntactic
    constructs into their intended meaning
  • Syntax-directed approach provides compositional
    rules for meanings.

2
Example decimal numerals
  • Syntactic categories
  • D ? Digits the decimal digits 0,1
  • N ? Num decimal numerals (strings)
  • Productions
  • D 0 1 2.
  • N D N D
  • Value Domain
  • Nat 0,1, the natural numbers
  • Semantics defines mapping of strings into numbers

3
The meaning of decimal numerals
  • Semantic functions
  • D Digits ? Nat
  • M Num ? Nat
  • Semantic equations
  • D 0 0, D 1 1,
  • MD D D
    production for M
  • M N D 10 M N M D production for
    M

4
Propositional logic
  • Boolean expressions with variables and logical
    connectives.
  • Categories
  • P ? Prop propositions (variable names)
  • F ? Form Formulae
  • Productions
  • F P F F or F F and F F gt F
    (implication)
  • Value Domains
  • Bool True, False boolean values
  • r ? Assign P ? Bool assignments (values of
    names)

5
The meaning of propositional formulae
  • The value of a formula depends on the current
    assignments. Curried form is
  • M Form ? Assign ? Bool
  • Semantic equations
  • M P r r (P) -- meaning of
    variable is its value
  • M F r M F r
  • M F1 or F2 r M F1 r or M F2 r
  • M F1 and F2 r M F1 r and M F2 r
  • M F1 gt F2 r M F1 r or M F2 r
  • Must distinguish or in syntax from or in
    semantics

6
A language of expressions
  • Syntactic categories
  • Id ? Ident Identifiers
  • Num ? N numeric literals
  • E ? Exp Expressions
  • Productions
  • E num Id (E1 E2) (E1 E2)
  • E let Id E1 in E2 end
    local binding
  • Value domains
  • Semantics describes integer values of
    expressions, in terms of current bindings of
    identifiers
  • Int -2, -1, 0, 1, 2 integers
  • r ? Env Ident ? Int environments

7
The meaning of simple expressions
  • Semantics describes rule of computation. Value is
    a function of environment
  • E Exp ? Env ? Int
  • Semantic functions
  • E Id r r (Id)
  • E num r M num given
    previously
  • E E1 E2 r E E1 r E E2 r
  • E E1 - E2 r E E1 r - E E2 r

8
Bindings and environments
  • A binding is modeled by an update function, best
    described in a programming language
  • fun Update (Env, Id, Val) (x)
  • if Id x then Val else Env
    (x)
  • E let Id E1 in E2 end r E E2 Update (r,
    Id, E E1)

9
A domain with an error value
  • To model unbound names or type errors in a
    dynamically typed language, extend domain with
    undefined value
  • Int -2, -2, 0, 1, 2,
    integers
  • r ? Env Ident ? (Int ? )
    environments
  • Type checking is explicit
  • E Id r let n r (Id) in if Is_Int (n)
    then n else ? end
  • E E1 E2 r
  • let n1 E E1 r
  • n2 E E2 r
  • in
  • if Is_int (n1) andalso Is_Int (n2) then
    n1 n2 else ?
  • end

10
Denotational Semantics of Imperative languages
  • Need domains to describe the state of the
    computation, including sequences of instructions
    (the program counter) and memory (the mapping of
    names to locations).
  • C ? Com commands, describes control structures
  • C C1 C2
  • C if E then C1 else C2
  • C while E do C
  • Value domain
  • s ? States configuration of
    computation

11
The meaning of commands
  • The value of a command is a state
  • C if E then C1 else C2 s
  • if IsTrue (E E s) then C C1 s else
    C C2 s
  • C C1 C2 s let s C C1 s in C C2
    s end
  • C while E do C s
  • let
  • fun p (s) if IsTrue (E E s )
    then p (C C s ) else s
  • in
  • p (s)
  • end

12
Describing state
  • New syntactic category
  • L ? left-hand side for bindings and
    references
  • Value domains
  • a ? Loc
    locations (addresses)
  • s ? States Loc ? (Int ? ) memory
  • r ? Env Ident ? (Int Loc ? )
    environments
  • Semantic functions
  • C Com ? Env ? States ? States
  • E Exp ? Env ? States ? Int
  • L Left ? Env ? States ? Loc

13
Manipulating state
  • C C1 C2 if E then C1 else C2
  • L E
    assignment
  • local V E in C end
    local variable
  • L Id
    (lhs is simple name)
  • C L E r s let
  • a L L r s
    meaning of l.h.s is a location
  • n E E r s
    meaning of rhs is a value
  • in
  • update (s, a, n) meaning of
    assignment is new state
  • end
  • side effects of E are
    ignored

14
Local variables
  • Assume pool of available locations
  • C local V E in C end r s
  • let
  • a ? Loc such that s ( a ) unused
  • n E E r s
  • in
  • C C update (r, V, a) update (s, a,
    n)
  • end
  • Enlarge environment with new variable,
    enlarge state with its initial value, evaluate
    command in new environment / state

15
Dereferencing
  • Obtaining the value of a variable is a two-step
    affair name -gt loc, loc -gt value
  • E !L if E1 then E2 else E3
  • E !L r s let a L L r s in s (a) end

16
Programs
  • New syntactic category P
  • P program (I) C end
  • program is a command with a read /
    write parameter
  • Semantic function P Prog -gt int -gt int
  • P program (I) C end n
  • let fun r (J) undefined for all J
  • fun s (a) unused for all a
  • a ? Loc
  • sf C C update (r, I, a)
    update (s, a, n)
  • in
  • sf (a)
  • end

17
Function calls
  • Semantic domains
  • A function affects the state
  • f ? Fun States -gt int gt int States
  • The name of the function denotes a function, not
    a location
  • Denote int Loc Fun
  • r ? Env Ident -gt (Denote ?)
  • A function does not depend on the environment
    static binding of identifiers.

18
Syntax and semantics of functions
  • M function f (a) E1 in E2 end r s
  • E1 is body of function, E2
    contains calls
  • let
  • fun g s n M E1 update (r,
    a, n) s
  • in
  • M E2 update (r, f, g)
  • end
  • M call f (E) r s
  • let
  • (n, s) M E r s func
    r (f)
  • in
  • func s n
  • end

19
Transfer of control
  • In the presence of gotos and exceptions, state
    must include notion of program counter.
  • C C1 C2 if E then C1 else C2 L E
  • local I E in C end skip goto L
    L C
  • M program (I) C end
  • New semantic domains and functions
  • q ? CC States -gt int (next instruction
    to execute)
  • C Com -gt Env -gt CC -gt States -gt Int

20
Commands have continuations
  • C C1 C2 r q s
  • let fun q (s) C C2 r q s
  • in
  • C C1 r q s
  • A label captures a continuation
  • C L C r q s
  • let fun q (s) C C update (r, L, q)
    q s
  • in
  • q (s)
  • end

21
The goto resets the continuation
  • C goto L r q s (r L) s
  • The meaning of the goto is the result of applying
    the continuation of the label to the state.
  • C Here goto Here r q s q (s)
  • And q (s) q (s) for any s gt infinite loop
Write a Comment
User Comments (0)
About PowerShow.com