Search code examples
functionhaskellfunctional-programming

Haskell: applying flip twice (type of flip flip)


I am learning Haskell along some basic functions. I was doing some exercises with Flip, which takes a function of two arguments and evaluates the result flipping the order of the arguments. Consider the function flip flip, I would have thought, following the definition of flip, that it flipped the arguments twice, evaluating the original function with the parameters in the original order. When I checked this assumption with ghci checking the function type, it yielded:

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

I don't understand why this is the function type of flip flip. It takes parameter b and parameter (a -> b -> c) and yields a function a -> c. Why is this the case ? I would really appreciate an explanation since I am at lost with this. Thanks in advance


Solution

  • Flipping twice would be \f -> flip (flip f), or flip . flip. This would indeed have the type (a -> b -> c) -> (a -> b -> c).

    What you are doing here instead is applying flip on the flip function, i.e. flipping flip's argument order. So if we start with

    flip :: (a -> b -> c) -> b -> a -> c
    -- and, as the type of the argument
    flip :: (a' -> b' -> c') -> b' -> a' -> c'
    

    then if we match the types

    a = (a' -> b' -> c')
    b = b'
    c = a' -> c'
    

    we get the result

    flip flip :: b' -> (a' -> b' -> c') -> (a' -> c')