Haskell infix function application precedence

13,161

Solution 1

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`

Solution 2

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.

Share:
13,161
demi
Author by

demi

I work for Google, love functional programming and Clojure, but code in C++, Java and JS.

Updated on June 19, 2022

Comments

  • demi
    demi almost 2 years

    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.