CS 550

Spring 2014

Programming Languages

Thursdays 18:30-21:20

University Crossings 153

Instructor: |
Geoffrey Mainland mainland+cs550@cs.drexel.edu University Crossings 106 |

Teaching Assistant: |
Mark Boady mwb33@drexel.edu |

Please submit all files for this assignment by checking them in to your cs550
git repository at `~/cs550/git/hw3`

. **Be sure to commit your work to the
repository**.

Please tell us how long each problem took you by noting the time you required to complete each problem in your submission.

Please put the answers to this problem in the file `Church.hs`

in the `hw3`

subdirectory of your git repository.

Define the first three Church numerals in Haskell. Name them `zero`

, `one`

, and
`two`

.

Define a type alias `Church a`

for Church numerals in Haskell. You type
alias definition should look like this:

Give an appropriate type signature to your three Church numerals.

Write a function with the following type for converting Church numerals to
`Integer`

s.

Submit the solutions to this problem in a file named `Monads.hs`

in the `hw3`

subdirectory of your git repository.

Restate the three monad laws in terms of do notation instead of in terms of the
`(>>=)`

combinator. Please put this answer **in a comment** in the file
`Monads.hs`

.

Consider the following data type definition. To a first approximation, `newtype`

is like `data`

. You can read about it in Chapter 6 of *Real World Haskell* in
the section “How to give a type a new identity.”

Please fill in the following monad instance for the `Identity`

type.

My solution is 3 lines. Please put this answer, which should include both the
definition of the `Identity`

data type and the corresponding `Monad`

instance,
in the file `Monads.hs`

.

Submit the solutions to this problem in a file named `Cont.hs`

in the `hw3`

subdirectory of your git repository.

Consider the following data type definition for continuations.

Think of a `Cont r a`

as a function that takes a continuation of type `a -> r`

and returns a result of type `r`

. The type `r`

is the *result type*, that is,
the type of the final value returned by the computation. You must define a
`Monad`

instance for `Cont r`

. Notice that, like in lecture where we the defined
a `Monad`

instance for `State s`

, we are “fixing” the type `r`

when we define
the `Monad`

instance for `Cont r`

. That is, the final result type `r`

is fixed
for all time, but the intermediate computations may be values of type `Cont r a`

for different instantiations of `a`

.

When you define a function of type `(a -> r) -> r`

, you will need to find an
appropriate value of type `a`

to pass to the function’s continuation, which has
type `a -> r`

. Once you find the correct value of type `a`

, it will be
straightforward to compute a value of type `r`

!

Please fill in the following type class instance in `Cont.hs`

. Include the
definition of the type `Cont r a`

in your solution.

This problem is tricky, but does not take much code.

Hint 1: For both class method definitions, you will need to produce a value of
type `Cont r a`

. Your definitions should therefore start something like this:

Here, `k`

will have type `a -> r`

for some `a`

.

Hint 2: My definition of `(>>=)`

invoked runCont twice. This makes sense, as we
are composing **two** continuations.