Intro to Functional Prog. by Bird, Section 2.5

I've been working on understanding Haskell using hugs98 and playing with some of the examples in Bird's book. In section 2.5, page 46 of the second edition, he gives an example of dependent types: But I get this error I've been unable to solve: ---------------------------------------------------------------------------------------------------------------- --Type checking --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) 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) ----------------------------------------------------------------------------- Please note that the version I've entered is correct by the errata published for this section.

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
participants (3)
-
senganb@ia.nsc.com
-
Shawn P. Garbett
-
Sven Panne