haskellcommutativity

Haskell: Usage of flip results in error: Equations for ‘.>’ have different numbers of arguments


I am struggling to understand what's going on here. I want to implement a data type Direction and define a commutative operator .> for it. So far I have this:

data Direction = N | E | S | W | None

(.>) :: Direction -> Direction -> [Direction]
N .> S = [None]
W .> E = [None]
(.>) = flip (.>)

I get the error Equations for ‘.>’ have different numbers of arguments. That's what I don't understand, because both sides of the equation have the same numers of arguments when checked in ghci:

λ> :t (.>)
(.>) :: Direction -> Direction -> [Direction]
λ> :t flip (.>)
flip (.>) :: Direction -> Direction -> [Direction]

I can resolve the error by writing d1 .> d2 = d2 .> d1 instead of using flip but I can't understand why flip won't work. Any ideas?

Edit: removed second, unrelated question


Solution

  • Haskell mandates that each equation for a function has the same number of explicit arguments on the left hand side. This is not allowed:

    N .> S = ...   -- two arguments
    W .> E = ...   -- two arguments
    (.>) = ...     -- no arguments
    

    Even if the last line is morally correct, since the type of the ... part has two arguments, Haskell does not allow it, since the arguments are not explicitly present in the left hand side. So, we need to make the arguments explicit, using something like

    x .> y = ... x y  -- two arguments
    

    That is:

    x .> y = flip (.>) x y
    

    which can be simplified to

    x .> y = y .> x
    

    which is what you wrote in the question.

    If you wonder why this is disallowed, there is a question for that.