CS 410/510 - Homework #3
Create a single Haskell file that contains solutions to the following problems.
Be sure it compiles without errors, and submit it via D2L by class time on
Thursday April 24, 2014.
Start with the file HW3Template.html
which has all the boiler plate you will need.
Be sure you put your name and e-mail in a comment
at the beginning of the file. Without this you will not get any feedback.
In this homework you will write several interpreters for a simple language
with assignments. You will do this with and without the use of monads.
The language we are interested in has the following abstract
syntax. This is a variation of the languages
we used as examples in the lecture on monadic interpreters
(the haskell code is here).
-- A language with variables and assignments
data T5 = Add5 T5 T5
| Sub5 T5 T5
| Mult5 T5 T5
| Int5 Int
| Var5 String
| Assign5 String T5
- Non Monadic Interpreter. Write an evaluator for the language T5.
Write it in functional style (that is don't use monads).
- Monadic Operations. Now write the operators on the Store monad
Given a name "x", "getStore x" returns a
Store computation, that when run, returns
the value associated in the Map with the name given as input.
getStore :: String -> (Store Map Value)
Given a name "x", and a new value "v"
"putStore x v" returns a Store computation,
that when run, returns unit, but updates
the map so "x" is now mapped to "v"
putStore :: String -> Value -> (Store Map ())
The Monad instance for the Store monad is given below.
data Store s x = St(s -> (x,s))
type Map = [(String,Value)]
instance Monad (Store s) where
return x = St(\ s -> (x,s))
(>>=) (St f) g = St h
where h s1 = g' s2 where (x,s2) = f s1
St g' = g x
- Monadic Interpreter.
Write an evaluator for the language T5 but
this time use use monads and the "do" syntax
Hint. use the operators on the Store Monad you defined above.
- Monad Transformers.
Redo the monad "Store" and its monadic operations "getStore" and "putStore"
by defining a new type "Store2", and functions "getStore2" and "putStore2".
Use the monad transformers library to do this.
- Monadic Interpreter Again. Redo question #3 by writing a monadic interpreter for T5
called: eval5T :: T5 -> Store2 Value.
Use the do syntax as you did in question #3, but this time use the monad "Store2" you
defined using monad transformers. Could eval5T have a more general type
than the one given above? What would this type be?