Why infix operators lose their fixities in let/do bindings

haskell

Solution

In Haskell, fixities are defined per name. So if you define a new name for an operator, that name will have its own fixity--different from the original.

In your list comprehension, you define a new name for `lowMult`--`op`. Since you do not specify `op`'s fixity, it gets the default level, which is the highest possible fixity with left associativity. This makes it bind even tighter than multiplication.

This behavior is really the only logical choice. What would you do in this scenario, otherwise?

[8 - 1 `op` 4 + 2 | op <- [lowMult, (*)]]

What if the operators were passed in as arguments, or results of more complicated expressions themselves? The fixity of all the operators has to be defined at compile-time, so it can't depend on runtime information like this.

Problem

I defined a low precedence (lowMult) infix operators like this : ``` a `lowMult` b = a*b infix 1 `lowMult` ``` So that GHCi evaluates `8-1 `lowMult` 4 + 2` to `42` However, in a list comprehension like : `[8 - 1 `op` 4 + 2 | op <- [lowMult]]`, the operators (`op`) seems to have the `(*)` fixity, as GHCi evaluates the previous list comprehension to `[6]`

Original source