Why can't I return a partially applied function in my example?

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

Hi.
You are right, that 'f (g x)' has type '(t -> b)'. However, you're not
returning 'f (g x)'. You're returning '\x -> f (g x)', and that has
type 't -> (t -> b)', because 'x' is of type 't'. So you're returning
a 't -> (t -> b)' where a 't -> b' is expected. Since the 't -> ...'
of both types matches, GHC complains only about the mismatch of the
result types.
Cheers,
Andres
On Fri, Oct 23, 2015 at 2:42 PM, Umair Saeed
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

FYI, the material you are using was stolen. Here is the original. http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/ On 23/10/15 22:42, Umair Saeed wrote:
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Fri, Oct 23, 2015 at 11:02:50PM +1000, Tony Morris wrote:
FYI, the material you are using was stolen. Here is the original.
http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/
:| No attribution/link whatsoever on the article page, (on the author's page [1] you can read "Shamelessly stolen from <url>"). Can you confirm noone asked you permission to Ctrl-c/v your post? I am pretty sure FP Complete is several notches better than BuzzFeed and would act upon it. [1] https://www.fpcomplete.com/user/DanBurton
participants (4)
-
Andres Loeh
-
Francesco Ariis
-
Tony Morris
-
Umair Saeed