# The Semantics of SIMPLE

The following is an example, intended to provide an introduction to denotational semantics. The details of the example are (or will be) covered in class.

## Syntactic Domains

```P in Prog
L in SL
S in Stat
E in Exp
V in Val
I in Ide
```

## Abstract Syntax

```// Programs
Prog => L
// Lists of statements
SL => S
|  S ; L
// Statements
Stat => I = E
|  if E then L fi
|  if E then L0 else L1 fi
|  while E do L od
|  write(E)
// Expressions
Exp => E1 + E2
|  E1 * E2
|  E1 - E2
|  E1 / E2
|  (E)
|  V
|  I
```

## Semantic Domains

```n in N                   Natural Numbers
env in U = Id -> N       Environments
inp in I = N*            Input (a sequence of numbers)
out in O = N*            Output (a sequence of numbers)
cont in C = U -> I -> O  Continuations
```

## Types of the Semantic Functions

```meaningProg : Prog -> I -> O
meaningSL : SL -> C-> U -> I -> -> O
meaningStat : Stat -> C -> U -> I -> O
meaningExp : Exp -> U -> N
meaningVal : Val -> N
```

## Definitions of the Semantic Functions

### `meaningProg`

```meaningProg[[SL]] = meaningSL[[SL]] cleanup basicEnv
```

### `meaningSL`

```meaningSL[[S;L]] =
\cont . \env . \inp .
meaningS[[S]] (meaningSL[[L]]] cont) env inp
meaningSL[[S]] = meaningS[[S]]
```

### `meaningStat`

```meaningStat[[read(I)]] =
\cont . \env . \inp .
cont (setenv env I (car inp)) (cdr inp)
meaningStat[[write(E)]] =
\cont . \env . \inp .
cons (meaningExp E env) (cont env inp)
meaningStat[[I = E]] =
\cont . \env . \inp .
cont (setenv env I (meaningExp E env)) inp
meaningStat [[if E then L1 else L2 fi]] =
\cont . \env . \inp .
(meaningExp E env) = 0 -> meaningSL L2 cont env inp
, meaningSL L1 cont env inp
meaningStat [[if E then L fi]] =
\cont . \env . \inp .
(meaningExp E env) = 0 -> cont env inp
; meaningSL L cont env inp
meaningStat[[while E do L od]] = F(E,L) s.t.
F(E,L) : C -> U -> I -> O
F(E,L) cont env inp =
((meaningExp E env) == 0) -> cont env inp,
meaningSL L (\e . \i . F(E,L) cont e i ) env inp
```

### `meaningExp`

```meaningExp [[E1 + E2]] =
\env .
(meaningExp E1 env) +
(meaningExp E2 env)
meaningExp [[E1 - E2]] =
\env .
(meaningExp E1 env) -
(meaningExp E2 env)
meaningExp [[E1 * E2]] =
\env .
(meaningExp E1 env) *
(meaningExp E2 env)
meaningExp [[E1 / E2]] =
\env .
(meaningExp E1 env) /
(meaningExp E2 env)
meaningExp [[I]] =
\env . (env I)
meaningExp [[V]] =
\env . meaningVal V
meaningExp [[(E)]] =
meaningExp E
```

### `meaningVal`

This function has the ``obvious'' definition, which is difficult to express. Basically, it maps every string of digits to the corresponding number.

## Helper Functions

```// Build a basic environment that assigns 0 to every variable.
basicEnv : U
basicEnv = \x . 0
// A continuation used to ``clean up'' at the end of the program.
cleanup : C
cleanup = \env . \inp . nil
// Set the value associated with an identifier in an environment.
setenv : U -> Id -> N -> U
setenv env id num =
\x . (x == id) -> num , (env id)
```

## History

Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.