31

Let f x y = x * y. We can apply this function in two ways: f 5 6, or, using infix notation, 5 `f` 6. Do the operator rules apply to this last expression? What precedence will this application have? Is it just another form of function application, and so will it also have the highest precedence?

I suppose that the compiler sees this special form (due to `` and/or the name starting with a letter(?)), and actually treats this as ordinary function application, instead of considering it an operator.

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
demi
  • 5,384
  • 6
  • 37
  • 57
  • 2
    The Haskell 98 syntax allows you choose the precedence level, see http://www.haskell.org/onlinereport/decls.html#fixity for details. – Chris Kuklewicz Nov 15 '11 at 15:54

2 Answers2

30

The Haskell 98 Report has a section on Operator Applications that clears it up:

An operator is either an operator symbol, such as + or $$, or is an ordinary identifier enclosed in grave accents (backquotes), such as `op`. For example, instead of writing the prefix application op x y, one can write the infix application x `op` y. If no fixity declaration is given for `op` then it defaults to highest precedence and left associativity (see Section 4.4.2).

As indicated by the other answers, the Report also has a section on Fixity Declarations that allows you to define your own fixity, for example:

infixl 7 `op`
Nicolas Wu
  • 4,805
  • 2
  • 25
  • 32
12

If no explicit fixity declaration is given, as e.g.

infixl 7 `quot`

a backticked infix function has the default fixity of infixl 9, so will be treated like any other infix operator with the same fixity.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • Why does it default to infixl 9? Is there any reason behind this choice, or is it arbitrary? – CMCDragonkai Mar 31 '15 at 13:01
  • 1
    In a way, it's arbitrary of course. But since having it `infix` doesn't allow chaining without parentheses, it's more convenient to have a direction of associativity. Right-associativity is rarer, so `infixl` is the "least surprising" choice. The precedence level of 9 is closest to prefix application precedence, hence that changes the least in comparison to prefix application. I wasn't there when it was decided, but I think the idea is that `infixl 9` was chosen with the expectation that it is most convenient and causes least surprise. – Daniel Fischer Mar 31 '15 at 13:16
  • I see, but what about things like `!!` and `$`. Is there any reason given for why `!!` has higher precedence than `$`? Just trying to understand the reasoning for all the placements of the operators in the table: https://www.haskell.org/onlinereport/decls.html#fixity – CMCDragonkai Apr 01 '15 at 15:53
  • 5
    `$` needs a very low precedence because otherwise you'd still need parentheses when applying a function to a non-atomic argument, `foo $ a + b*c` would be pretty pointless if it meant `(foo a) + (b*c)`. The purpose of `$` demands that practically everything else has higher precedence. A lot of the [relative] precedences, `*` binds tighter than `+` etc., are common in mathematics and also other programming languages. Apart from prior conventions, considerations what would be most convenient for the [expected] common cases will have played a role deciding the precedences. – Daniel Fischer Apr 01 '15 at 16:07