Search code examples
haskellfunction-compositionpointfree

Haskell function composition operator of type (c→d) → (a→b→c) → (a→b→d)


Ordinary function composition is of the type

(.) :: (b -> c) -> (a -> b) -> a -> c

I figure this should generalize to types like:

(.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d

A concrete example: calculating difference-squared. We could write diffsq a b = (a - b) ^ 2, but it feels like I should be able to compose the (-) and (^2) to write something like diffsq = (^2) . (-).

I can't, of course. One thing I can do is use a tuple instead of two arguments to (-), by transforming it with uncurry, but this isn't the same.

Is it possible to do what I want? If not, what am I misunderstanding that makes me think it should be possible?


Note: This has effectively already been asked here, but the answer (that I suspect must exist) was not given.


Solution

  • The misunderstanding is that you think of a function of type a -> b -> c as a function of two arguments with return type c, whereas it is in fact a function of one argument with return type b -> c because the function type associates to the right (i.e. it's the same as a -> (b -> c). This makes it impossible to use the standard function composition operator.

    To see why, try applying the (.) operator which is of type (y -> z) -> (x -> y) -> (x -> z) operator to two functions, g :: c -> d and f :: a -> (b -> c). This means that we must unify y with c and also with b -> c. This doesn't make much sense. How can y be both c and a function returning c? That would have to be an infinite type. So this does not work.

    Just because we can't use the standard composition operator, it doesn't stop us from defining our own.

     compose2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d
     compose2 g f x y = g (f x y)
    
     diffsq = (^2) `compose2` (-)
    

    Usually it is better to avoid using point-free style in this case and just go with

     diffsq a b = (a-b)^2