Intro to Functional Prog. by Bird, Section 2.5

senganb@ia.nsc.com senganb@ia.nsc.com
Wed, 2 Jan 2002 09:14:45 -0700 (MST)


> data Either' a b =  Left' a | Right' b
> 
> case' :: (a->g, b->g) -> Either' a b -> g
> case' (f, g) (Left' x) = f x
> case' (f, g) (Right' y) = g y
> 
> plus' :: (a->b , g->d) -> Either' a g -> Either' b d
> plus' (f,g) = case' (Left' f, Right' g)

case' takes as first argument a tuple of functions as described by the type
"(a->g,b->g)":
 * a->g is a function taking something of type a and returning something of type g.
 * b->g is a function taking something of type b and returning something of type g.
 * (x,y) is a tuple taking something of type x as first element, and something of
   type y as second element.
 * Substitute x = a->g and y = b->g and you get a tuple of functions returning the
   same type g but taking as input types a and b respectively.

case' takes as second argument something of type (Either' a b) which has 2 possible
forms: an object (Left' x) or an object (Right' y). The following line states that
the object x must be of type a, and the object y must be of type b:
> data Either' a b =  Left' a | Right' b

Take your function plus':

> plus' :: (a->b , g->d) -> Either' a g -> Either' b d
> plus' (f,g) = case' (Left' f, Right' g)

Uncurry it (convert it such that all its arguments are explicit):

    plus' (f,g)   = case' (Left' f, Right' g)
=>  plus' (f,g)   = \x -> case' (Left' f, Right' g) x
=>  plus' (f,g) x = case' (Left' f, Right' g) x

Notice that you are passing as first argument to case',
not a tuple of functions, but a tuple of objects of type
(Either i j). This is what the interpreter is telling you:

> --ERROR sec2-5.hs:8 - Type error in application
> --*** Expression     : case' (Left' f,Right' g)
> --*** Term           : (Left' f,Right' g)
> --*** Type           : (Either' (d -> e) f,Either' g (h -> i))
                          ^^^^^^^            ^^^^^^^
> --*** Does not match : (a -> b,c -> b)
                          ^^^^^^^^^^^^^

Look at the types of plus' and case', and at case' 's definition
to figure out the solution. It's not difficult.

Sengan