Added assignmemnt 6
[lambda.git] / assignment6.mdwn
1 1.  **Build a state monad.** Based on the division by zero monad,
2 build a system that will evaluate arithmetic expressions.  Instead of
3 returning a simple integer as a result, it will deliver the correct
4 answer along with a count of the number of operations performed during
5 the calculuation.  That is, the desired behavior should be like this:
6
7     # lift ( + ) (lift ( / ) (unit 20) (unit 2)) (lift ( * ) (unit 2) (unit 3)) 0;;
8     - : int * int = (16, 3)
9
10 Here, `lift` is the function that uses `bind` to prepare an ordinary
11 arithmetic operator (such as addition `( + )`, division `( / )`, or
12 multiplication `( * )`) to recieve objects from the counting monad as
13 arguments.  The response of the interpreter says two things: that
14 (20/2) + (2*3) = 16, and that the computation took three arithmetic
15 steps.  By the way, that zero at the end provides the monadic object
16 with a starting point (0 relevant computations have occurred previous
17 to the current computation).
18
19 Assume for the purposes of this excercise that no one ever tries to
20 divide by zero (so there should be no int option types anywhere in
21 your solution).
22
23 You'll need to define a computation monad type, unit, bind, and lift.
24 We encourage you to consider this hint: [[Assignment 6 Hint 1]].
25
26 2. Prove that your monad satisfies the monad laws.  First, give
27 examples illustrating specific cases in which the monad laws are
28 obeyed, then explain (briefly, not exhaustively) why the laws hold in
29 general for your unit and bind operators.
30
31 3. How would you extend your strategy if you wanted to count
32 arithmetic operations, but you also wanted to be safe from division by
33 zero?  This is a deep question: how should you combine two monads into
34 a single system?  If you don't arrive at working code, you can still
35 discuss the issues and design choices.