CSc 520
Principles of Programming Languages
:

Christian Collberg

Department of Computer Science

University of Arizona

1 Denotational Semantics

2 Meaning Brackets

3 Meaning Brackets -- Examples

4 Denotational Specification


Example -- A Language of Numerals


5 Denotational Specification

Syntactic Domains:

Abstract Production Rules:


\begin{EBNF}
\item[{Numeral}] \!Digit! \vert \!Numeral! \!Digit!
\item[{Digit}...
...3: \vert \:4: \vert \:5: \vert \:6: \vert \:7: \vert \:8: \vert \:9:
\end{EBNF}
Semantic Domains:

6 Denotational Specification...

Semantic Functions:

\begin{eqnarray*}
\mathit{value}& : & \mathrm{Numeral} \rightarrow \mathrm{Numb...
...thit{digit}& : & \mathrm{Digit} \rightarrow \mathrm{Number} \\
\end{eqnarray*}

Semantic Equations:

\begin{eqnarray*}
\mathit{value}\Sem{N\ D} & = & 10 * \mathit{value}\Sem{N} + \...
...= & 0 \\
& \vdots & \\
\mathit{digit}\Sem{\:9:} & = & 9 \\
\end{eqnarray*}

7 Example

8 Example...


The Semantics of Wren


9 Imperative Languages

10 Abstract Syntactic Domains

These are the abstract syntactic domains of Wren:

$P$:
$\mathrm{Program}$
$C$:
$\mathrm{Command}$
$D$:
$\mathrm{Declaration}$
$T$:
$\mathrm{Type}$
$E$:
$\mathrm{Expression}$
$O$:
$\mathrm{Operator}$
$N$:
$\mathrm{Numeral}$
$I$:
$\mathrm{Identifier}$

11 Abstract Syntax of Wrens


\begin{EBNF}
\item[Program] \:program: \!identifier! \:is: \!Declaration!* \:be...
...\vert \:+: \vert
\:­: \vert \:*: \vert \:/: \vert \:and: \vert \:or:
\end{EBNF}

12 Semantic Domains of Wren

\begin{eqnarray*}
\mathrm{Integer}& = & \{\ldots -2, -1, 0, 1, 2, \ldots \} \\ ...
...\mathrm{Identifier}\rightarrow (\mathbf{SV}+ \mathit{undefined})
\end{eqnarray*}

13 Semantic Functions of Wren

14 Commands

15 Commands...

16 Commands...

17 Expressions

\begin{eqnarray*}
\mathit{evaluate}\Sem{I}\mathrm{sto}& = & \mathbf{if}\ v=\math...
...mathrm{sto}\\
n & = & \mathit{evaluate}\Sem{E_2}\ \mathrm{sto}
\end{eqnarray*}

18 Expressions...

\begin{eqnarray*}
\mathit{evaluate}\Sem{E_1/E_2}\mathrm{sto}& = & \mathbf{if}\ n...
...mathrm{sto}\\
q & = & \mathit{evaluate}\Sem{E_2}\ \mathrm{sto}
\end{eqnarray*}


A Haskell Prototype


19 Abstract Syntax

type Num =  Rational 

data SV    = IVal Num | BVal Bool | Undefined

type Identifier = String

data Operator  = Add | Sub | Mul | Minus | Div | Not | 
                 Or | And | Lt | Gt | Eq | Ne | Le | Ge

data Expression = Id String |
                  LitInt Num |
                  TrueVal |
                  FalseVal |
                  Unary Operator Expression |
                  Binary Expression Operator Expression

20 Abstract Syntax...

data Program = Prog [Declaration] Command

data Declaration = Var [Identifier] Type 

data Type = IntType | BoolType

data Command = Skip |
               Assign String Expression |
               Read String |
               Write Expression |
               IfThen Expression Command |
               IfThenElse Expression Command Command |
               While Expression Command |
               Seq Command Command

21 Expressions

bcompute :: SV -> Operator -> SV -> SV
bcompute (IVal a) Add (IVal b) = (IVal (a + b))
bcompute (IVal a) Mul (IVal b) = (IVal (a * b))
bcompute (IVal a) Div (IVal b) = 
      if b==0 then error "Division by 0" 
      else         (IVal (toRational(a / b)))
bcompute (IVal a) Sub (IVal b) = (IVal (a - b))
bcompute (BVal a) And (BVal b) = (BVal (a && b))
bcompute (BVal a) Or  (BVal b) = (BVal (a || b))
bcompute (IVal a) Lt  (IVal b) = (BVal (a < b))
bcompute (IVal a) Gt  (IVal b) = (BVal (a > b))
bcompute (IVal a) Le  (IVal b) = (BVal (a <= b))
bcompute (IVal a) Ge  (IVal b) = (BVal (a >= b))
bcompute (IVal a) Eq  (IVal b) = (BVal (a == b))
bcompute (IVal a) Ne  (IVal b) = (BVal (not (a == b)))

22 Expressions...

ucompute :: Operator -> SV -> SV
ucompute Minus (IVal b) = (IVal (- b))
ucompute Not (BVal b) = (BVal (not b))

23 Expressions...

evaluate :: Expression -> Store -> SV
evaluate (Id id) sto = 
      if val == Undefined then val else val 
         where val = applySto sto id
evaluate (LitInt n) sto = (IVal n)
evaluate (TrueVal) sto = (BVal True)
evaluate (FalseVal) sto = (BVal False)
evaluate (Unary op r) sto = ucompute op n
   where n = evaluate r sto
evaluate (Binary l op r) sto = bcompute m op n
   where m = evaluate l sto
         n = evaluate r sto

24 Expressions -- Examples

> s1
[("b",True),("a",5 % 1)]

> evaluate (Binary (LitInt 5) Add (LitInt 6)) s1
11 % 1

> evaluate (Binary (LitInt 5) Add (Id "a")) s1
10 % 1

> evaluate (Binary (Binary (LitInt 6) Mul (LitInt 2)) Add (Id "a")) s1
17 % 1

25 Commands

execute :: Command -> Store -> Store
execute (Skip) sto = sto
execute (Assign id e) sto = updateSto sto id (evaluate e sto)
execute (Seq c1 c2) sto = execute c2 (execute c1 sto)
execute (IfThen b c) sto = 
     if (evaluate b sto) == (BVal True) then 
         execute c sto 
     else sto
execute (IfThenElse b c1 c2) sto = 
     if (evaluate b sto) == (BVal True) then 
         execute c1 sto 
      else execute c2 sto
execute (While b c) sto = loop sto
   where loop sto = if (evaluate b sto) == (BVal True) then 
                    (loop (execute c sto)) else sto

26 Commands -- Examples

> s1
[("b",True),("a",5 % 1)]

> execute (Assign "a" (LitInt 9)) s1
[("a",9 % 1),("b",True)]

> execute (IfThen (Unary Not (Id "b")) (Assign "a" (LitInt 9))) s1
[("b",True),("a",5 % 1)]

> execute (While (Binary (Id "a") Lt (LitInt 10)) (Assign "a" (Binary (Id "a") Add (LitInt 1)))) s1
[("a",10 % 1),("b",True)]

27 Store

type Store = [(Identifier,SV)]

emptySto:: Store
emptySto = []

updateSto::  Store -> Identifier -> SV -> Store
updateSto env id val = 
   (id,val) : (filter (\ (x,_) -> not(id==x)) env)

applySto::  Store -> Identifier -> SV
applySto env id = 
   snd (foldl (\ r c -> if id==(fst r) then r else c) ("",Undefined) env)

28 Store -- Examples

s1 = updateSto (updateSto emptySto "a" (IVal 5)) "b" (BVal True) 

> s1
[("b",True),("a",5 % 1)]

> applySto s1 "a"
5 % 1

> applySto s1 "b"
True

> applySto emptySto "a"
Undefined

29 Readings and References

30 Acknowledgments



Christian S. Collberg
2005-04-22