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
Warning! This material is for an old version of the course.
$$ \newcommand{\ox}[1]{\llbracket #1 \rrbracket} $$

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.

Problem 1: Church Numerals (15 points total)

Please put the answers to this problem in the file Church.hs in the hw3 subdirectory of your git repository.

Problem 1.1: Church Numerals (5 points)

Define the first three Church numerals in Haskell. Name them zero, one, and two.

Problem 1.2: Type of Church Numerals (5 points)

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

type Church a = ...

Give an appropriate type signature to your three Church numerals.

Problem 1.3: Converting Church Numerals to Integers (5 points)

Write a function with the following type for converting Church numerals to Integers.

churchToInteger :: Church Integer -> Integer

Problem 2: Monad Warmup (20 points total)

Submit the solutions to this problem in a file named Monads.hs in the hw3 subdirectory of your git repository.

Problem 2.1: Monad Laws (10 points)

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.

Problem 2.2: Identity Monad (10 points)

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.”

newtype Identity a = Id { unId :: a }

Please fill in the following monad instance for the Identity type.

instance Monad Identity where
    return x = ...
    m >>= f = ...

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.

Problem 3: Continuation Monad (15 points)

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.

newtype Cont r a = Cont { runCont :: (a -> r) -> r }

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.

instance Monad (Cont r) where
    return x = ...
    m >>= f = ...

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:

Cont $ \k -> ...

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.