COP 5021 Lecture -*- Outline -*- * Denotational Semantics (2.2) *** denotational example ------------------------------------------ SYNTAX OF WHILE LANGUAGE EXPRESSIONS Abstract Syntax (1.2): x,y \in Var n \in NumericLiteral a \in AExp b \in BExp op_a \in Op_a op_b \in Op_b op_r \in Op_r a ::= x | n | a1 op_a a2 b ::= true | false | not b | b1 opb b2 | a1 op_r a2 Lexical syntax: n \in NumericLiteral dgs \in Digits d \in Digit n ::= d | dgs d dgs ::= d | dgs d d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |8 | 9 ------------------------------------------ ------------------------------------------ DENOTATIONAL SEMANTICS OF EXPRESSIONS Domains: Z = { ..., -1, 0, 1, ... } T = { true, false } s \in State = Var -> Z Meaning Functions: A : Aexp -> State -> Z A[[x]]s = s(x) A[[n]]s = N(n) A[[a1 op_a a2]]s = (O_a[[op_a]]) (A[[a1]]s) (A[[a2]]s)) where N : NumericLiteral -> Z D : Digit -> Z D[[0]] = 0, D[[1]] = 1, D[[2]] = 2, D[[3]] = 3, D[[4]] = 4, D[[5]] = 5, D[[6]] = 6, D[[7]] = 7, D[[8]] = 8, D[[9]] = 9, N[[n]] = Nval[[n]](1) Nval: Digits -> Z -> Z Nval[[d]](p) = p*D[[d]] Nval[[dgs d]](p) = Nval[[dgs]](10*p) + Nval[[d]](p) O_a : Op_a -> (Z x Z -> Z) O_a[[+]] = \n1 n2 . n1 + n2 O_a[[-]] = \n1 n2 . n1 - n2 ... ------------------------------------------ In the semantic function Nval, the p argument gives the power of 10 that denotes the significance of the digit Other languages may have different definitions of N E.g., in C or C++, N[[015]] = 13, since 015 is in octal. Q: Why is the state a parameter to the meaning of arithmetic expressions? It's needed to evaluate variable references Q: How would you define B : Bexp -> (State -> T)? Recall that the synatax is: b ::= true | false | not b | b1 opb b2 | a1 op_r a2 O_r : Op_r -> (Z x Z -> T) O_b : Op_b -> (T x T -> T) B[[true]](s) = true B[[false]](s) = false B[[not b]](s) = if B[[b]](s) then false else true B[[b1 op_b b2]](s) = O_b[[op_b]](B[[b1]](s), B[[b2]](s)) B[[a1 op_r a2]](s) = O_r[[op_r]](A[[a1]](s), A[[a2]](s))