Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct type signature for an uncurried function

I am learning currying and uncurrying of functions in Haskell. If I have this code:

fc :: Num a =>  a ->  a ->  a -- curried function
fc x y  = x + y

fu :: Num a => (a, a) -> a --uncurried function
fu (x,y) = fc x y

uncurry' ::  Num a => (a -> a -> a) ->  Num a => (a, a) -> a
uncurry' f(x,y) = f x y

main = print(uncurry' fc 4 5)

The compiler complains with the following error:

"Illegal polymorphic or qualified type: Num a => (a, a) -> a".

What is the correct type signature for uncurry'?

like image 459
JayJay Avatar asked Dec 11 '22 02:12

JayJay


2 Answers

You only need one constraint on the type signature:

uncurry' :: Num a => (a -> a -> a) -> (a, a) -> a
uncurry' f (x, y) = f x y

You don't have to add the extra constraint in the middle of the type signature, Haskell doesn't like this. All constraints go before the =>, and only one should appear in the type signature (until you enable certain compiler extensions, but that's beyond the scope of this answer).

However, as jamshidh points out, you can make this signature more general:

uncurry' :: (a -> b -> c) -> (a, b) -> c

and it will still work just fine. Remember that you can always force a signature to go from more general to less general.

like image 64
bheklilr Avatar answered Jan 07 '23 17:01

bheklilr


Once you uncurry the function, it is type Num a=>(a, a)->a, so you should give it a tuple as input parameter

main = print(uncurry' fc (4, 5))

I would, however, keep the type of uncurry' more general

(a -> b -> c) -> (a, b) -> c

Once I made this change, it worked for me.


Oh, and @bheklilr noticed something that I didn't even see.... His fix plus mine should get you up and going....

like image 30
jamshidh Avatar answered Jan 07 '23 15:01

jamshidh