denotational semantics syntax-directed approach, generalization of attribute grammars: –define...
Post on 20-Dec-2015
232 views
TRANSCRIPT
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.
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
The meaning of decimal numerals
• Semantic functions– D : Digits → Nat– M : Num → Nat
• Semantic equations:– D [‘0’] = 0, D [‘1’] = 1, …– M[D] = D [D] production for M– M [N D] = 10 * M [N] + M [D] production for M
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 => F (implication)
• Value Domains– Bool = {True, False} boolean values Є Assign = P → Bool assignments (values of names)
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] = (P) -- meaning of variable is its value– M [ ¬ F] = ¬ M [F] – M [F1 or F2] = M [F1] or M [F2]
– M [F1 and F2] = M [F1] and M [F2]
– M [F1 => F2] = ¬ M [F1] or M [F2]
• Must distinguish “or” in syntax from or in semantics
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
Є Env : Ident → Int environments
The meaning of simple expressions
• Semantics describes rule of computation. Value is a function of environment:
• E : Exp → Env → Int
• Semantic functions:• E [Id] = (Id)• E [ num] = M [num] given previously
• E [ E1 + E2] = E [E1] + E [E2]
• E [ E1 - E2] = E [E1] - E [E2]
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] = E [E2] Update (, Id, E [E1])
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 Є Env = Ident → (Int + {} ) environments
• Type checking is explicit:– E [Id] = let n = (Id) in if Is_Int (n) then n else end;
– E [E1 + E2] =
let n1 = E [E1]
n2 = E [E2]
in
if Is_int (n1) andalso Is_Int (n2) then n1 + n2 else
end;
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
Є States configuration of computation
The meaning of commands
The value of a command is a state:
C [if E then C1 else C2] = if IsTrue (E [E] ) then C [C1] else C [C2]
C [C1 ; C2] = let ’ = C [C1] in C [C2] ’ end
C [while E do C] = let fun p (’) = if IsTrue (E [E] ) then p (C [C] ’ ) else ’ in p () end
Describing state
• New syntactic category:– L Є left-hand side : for bindings and references
• Value domains: Є Loc locations (addresses) Є States = Loc → (Int + ) memory Є Env = Ident → (Int + Loc + ) environments
• Semantic functions:– C : Com → Env → States → States
– E : Exp → Env → States → Int
– L : Left → Env → States → Loc
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] = let
= L [L] ; meaning of l.h.s is a location
n = E [E] meaning of rhs is a value
in
update (,, n); meaning of assignment is new state
end;
side effects of E are ignored
Local variables
• Assume pool of available locations
C [local V := E in C end] =
let
Є Loc such that = unused;
n := E [ E] in
C [C] update (, V, ) update (, , n)
end
Enlarge environment with new variable, enlarge state with its initial value, evaluate command in new environment / state
Dereferencing
• Obtaining the value of a variable is a two-step affair: name -> loc, loc -> value
• E ::= !L | if E1 then E2 else E3 | …
E [ !L] = let = L [L] in end
Programs
• New syntactic category: P• P ::= program (I) C end • program is a command with a read / write parameter• Semantic function: P : Prog -> int -> int P [ program (I); C end] n = let fun (J) = undefined for all J fun = unused for all Є Loc
f = C [C ] update (, I, ) update (, , n) in
f () end
Function calls
• Semantic domains: • A function affects the state:
f Є Fun = States -> int > int * States• The name of the function denotes a function, not a
location:
Denote = int + Loc + Fun
Є Env = Ident -> (Denote + )
A function does not depend on the environment: static binding of identifiers.
Syntax and semantics of functions
M [function f (a) = E1 in E2 end] = E1 is body of function, E2 contains calls let fun g ’ n = M [E1] update (, a, n) ’ in M [E2] update (, f, g) end M [call f (E)] = let (n, ’) = M [E] ; func = (f) in func ’ n end
Transfer of control
• In the presence of goto’s 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:
Є CC : States -> int (next instruction to execute)
C : Com -> Env -> CC -> States -> Int
Commands have continuations
C [C1 ; C2] =
let fun ’ (’) = C [C2] ’ in
C [C1] ’ A label captures a continuation: C [L : C] = let fun ’ (’) = C [C] update (, L, ’) ’ in ’ () end
The goto resets the continuation
C [goto L] = ( L) • The meaning of the goto is the result of applying the
continuation of the label to the state.
C [Here : goto Here] = ’ ()
• And ’ () = ’ () for any => infinite loop