Search code examples
haskelldeclarationpointfree

Declaration issue with using curry in point-free style in Haskell


I am trying to understand the concept of point-free style. I made a function try to add two values using uncurry.

add = (+) . uncurry 

and the result complains:

No instance for (Num ((a0, b0) -> c0))
  arising from a use of `+'
Possible fix:
  add an instance declaration for (Num ((a0, b0) -> c0))
In the first argument of `(.)', namely `(+)'
In the expression: (+) . uncurry
In an equation for `add': add = (+) . uncurry

Is this a declaration problem? I tried add :: (Int, Int) -> Int, it is does not work as well.


Solution

  • You should pass (+) to uncurry:

    add :: (Int, Int) -> Int
    add = uncurry (+)
    

    This is because uncurry is a function that takes a binary function and returns an unary function:

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

    Your binary function is (+) which takes two Nums and sums them up. Therefore uncurry (+) will transform (+) into:

    (Num, Num) -> Num
    

    which is then restricted by the add type signature to:

    (Int, Int) -> Int