0

I've defined my own operator:

infixr 6 >+
x >+ y = (+ x) y

It is right associative.

And now I want to use in the next expression:

(`mod` 14) (>+ 5) 10

But I get an error:

<interactive>:11:1:
    Non type-variable argument in the constraint: Integral (a -> a)
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall a. (Integral (a -> a), Num a) => a

How can I fix it?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Finkelson
  • 2,921
  • 4
  • 31
  • 49
  • 1
    None of the code you've provided does anything about right associativity. Right associativity only does something when you use a function infix. Function application _always_ has the highest precedence. – Louis Wasserman Feb 24 '16 at 23:04
  • @LouisWasserman Now I understand where the OP's confusion stems from! – jub0bs Feb 24 '16 at 23:06
  • 3
    I think the OP expects `(>+ 5)` to apply to `10` before `(\`mod\` 14)`, but that's never going to happen, basically. – Louis Wasserman Feb 24 '16 at 23:07
  • Associativity rules essentially let you omit a few parentheses in expressions like `x + y - z * w << a`, involving infix operators. Nothing more than that. – chi Feb 24 '16 at 23:40

1 Answers1

1

Thats not how associativity works.

Associativity defines the behavior of chained operation with the same operator.

For example, when an operator * is left associative, then a * b * c * d is evaluated as ((a * b) * c) * d.

If * is right associative, then it will be evaluated as a * (b * (c * d))

In conclusion, your >+ does nothing since addition is commutative. That is, a + b + c is equivalent to a >+ b >+ c since (a + b) + c = a + (b + c).

In Haskell you can also define non-associative operators. It means that the operations cannot be chained, or your compiler will raise a parse error.

baxbaxwalanuksiwe
  • 1,474
  • 10
  • 20