Search code examples
haskellinfix-notation

How does Haskell know which function can operate first?


I'm writing a custom language that features some functional elements. When I get stuck somewhere I usually check how Haskell does it. This time though, the problem is a bit to complicated for me to think of an example to give to Haskell.

Here's how it goes.

Say we have the following line

a . b

in Haskell. Obviously, we are composing two functions, a and b. But what if the function a took another two functions as parameters. What's stopping it from operating on . and b? You can surround it in brackets but that shouldn't make a difference since the expression still evaluates to a function, a prefix one, and prefix functions have precedence over infix functions.

If you do

(+) 2 3 * 5

for example, it will output 25 instead of 17.

Basically what I'm asking is, what mechanism does Haskell use when you want an infix function to operate before a preceding prefix function.

So. If "a" is a function that takes two functions as its parameters. How do you stop Haskell from interpreting

a . b

as "apply . and b to the function a" and Interpret it as "compose functions a and b".


Solution

  • If you don't put parens around an operator, it's always parsed as infix; i.e. as an operator, not an operand.
    E.g. if you have f g ? i j, there are no parens around ?, so the whole thing is a call to (?) (parsed as (f g) ? (i j), equivalent to (?) (f g) (i j)).