Last updated 16.03.04

Session 1, 2004

## Exercises: Semantics of IO, Part I

The following exercises are based on the operational semantics for `IO` operations defined in Simon Peyton Jones' Tackling the Awkward Squad.

### Using the Operational Semantics

Assume the following program in `do` notation:

```main =
do
c1 <- getChar
c2 <- getChar
putChar (Char.chr (Char.ord c1 + Char.ord c2))```

It can be rewritten to use the monad primitives instead of the `do` notation as follows:

```main = getChar >>= \c1 ->
getChar >>= \c2 ->
putChar (Char.chr (Char.ord c1 + Char.ord c2))```

Use the transition semantics in the same way as it is done on Page 27 of Peyton Jones' paper to execute this program (ignore the use of `IORef`s in that example for the moment). Note the interaction between the evaluation function and the transition rules.

NB: Assume that the addition of two numbers "x" and "y" gives a result of "x + y", where the "+" here is not the addition in the object language (ie, Haskell), but in the mathematical meta language in which we reason about object language programs.