[Haskell-beginners] Why can't I return a partially applied function in my example?

Umair Saeed umairsd at gmail.com
Fri Oct 23 12:42:35 UTC 2015


Hello all,
I'm learning Haskell, and started to go through a set of intermediate
exercises (
https://www.fpcomplete.com/user/DanBurton/20-intermediate-exercises). I am
a bit puzzled about one of the exercises, and hope someone can help me
understand why one of my solutions doesn't work.

We have a typeclass, Misty (only the relevant banana function shown) as:


class Misty m where
    banana :: (a -> m b) -> m a -> m b


The exercise asks to implement this typeclass for the type ‘((->) t)’. I
started off by filling in the relevant types, and I get:

banana :: (a -> ((->) t b) ) -> ((->) t a) -> ((->) t b)
banana :: (a -> (t -> b)) -> (t -> a) -> (t -> b)

Based on this, I decided to implement banana as:

banana f g = (\x -> f (g x))

Here is my thought process:
- The type of f is ‘(a -> t -> b)’, and the type of g is ‘(t -> a)’
- g converts an argument of type ‘t’ into a result of type ‘a’.
- I then pass the result of ‘(g x)’ (which is of type ‘a’) as an argument
to ‘f’.
- At this point, ‘f’ would be partially applied, and I *expect* to get a
result of type ‘(t -> b)’


However, when I try to build my solution, I get the following error (code
is in a file called intermediate-help.hs):

[1 of 1] Compiling Main             ( intermediate-help.hs, interpreted )

intermediate-help.hs:7:25:
    Couldn't match expected type ‘b’ with actual type ‘t -> b’
      ‘b’ is a rigid type variable bound by
          the type signature for
            banana :: (a -> t -> b) -> (t -> a) -> t -> b
          at intermediate-help.hs:7:5
    Relevant bindings include
      x :: t (bound at intermediate-help.hs:7:20)
      g :: t -> a (bound at intermediate-help.hs:7:14)
      f :: a -> t -> b (bound at intermediate-help.hs:7:12)
      banana :: (a -> t -> b) -> (t -> a) -> t -> b
        (bound at intermediate-help.hs:7:5)
    In the expression: f (g x)
    In the expression: (\ x -> f (g x))
Failed, modules loaded: none.



So here's my confusion: The compiler is complaining that it cannot match
expected type ‘b’ with actual type ‘t -> b’. However, as I reasoned above,
when I wrote this code, I expected to get type ‘t -> b’. Clearly, my
thought process has a hole, and I need help/advice from more experienced
Haskellers to identify what I am missing.

Thank you for any help,
~Umair
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151023/ae22b250/attachment.html>


More information about the Beginners mailing list