I'm playing around with custom operators, infix, infixl and infixr. Now I'm confused.
I've written a custom operator for list-multiplication, and thought, that declaring it as a simple infix-operator with no directional associativity, would automatically provide both cases, nr * list and list * number, as they can be interchanged at will.
import Prelude hiding ((*))
infix 6 *
(*) :: Int -> [a] -> [a]
n * l = if n < 0 then []
else l ++ (n - 1) * l
Now, 3 * [1, 2, 3] returns [1, 2, 3, 1, 2, 3, 1, 2, 3] as expected, but [1, 2, 3] * 3 throws an error, because I never explicitly defined list * nr.
My question: What is the unique functionality of infix and why not allways use infixl or infixr instead, as it should make no difference?
I understand "no directional associativity" / infix as a synonym to "is commutative":
a + b + c has no directional associativity and is commutative and can be written as (a + b) + c, a + (b + c), b + a + c, (b + a) + c, and so on...
For my example 2 * [1, 2] * 1 is the same as 1 * (2 * [1, 2]), and all other combinations of that, so i dont really get, why there is no implicit reshaping for commutative operator declarations, even with different typed operands.