Allow specification of precedence of infix functions

Just now I typed something like

n * n `quot` 4

but got strange results. The cause turns out to be quot binding more tightly than *. I tried specifying associativity and precedence for quot, but that seems impossible at the moment. Would it be possible to fix this?

I’m thinking something like

infixl 7 quot as quot
-- or even
infixl 7 quot

Haskell does not have this problem.

1 Like

PureScript has full infix expressions. That is, backticks are an expression delimiter.

example = foo `Array.zipWith f` bar

Which makes fixity problematic. I personally consider this a huge anti-feature and would just prefer infix identifiers like Haskell, or no backticks at all.

5 Likes

Full fixity does not preclude precedence specification on single identifiers. After all, if it did it would be impossible for operators as well.

I’m fine with compound expressions implicitly having precedence 0. I would just like to be able to specify precedence for single identifiers, just like I am able to with operators. Seems like a simplification to me.

1 Like

Maybe this is a unpopular opinion but I actually prefer the way PureScript handles this.

For me n `quot` 4 is just another syntax for quot n 4 with the usual precedence.

This way I don’t have to keep all the possible precedence rules in mind when writing code to the (for me) slight disadvantage of having to write a few more (..).

3 Likes

Is it immediately obvious to you that

squaredTriangleNumber n = n * n * (n + 1) * (n * 1) `quot` 4

is wrong? (This is how I encountered this issue.)

I effectively propose to change the rule from

Is the thingy in infix position and does it look like an operator, then special precedence applies.

To

Is the thingy in infix position, then special precedence applies.

As to the need to remember these special precedences: of course they should (and would – at least in the standard library) be carefully chosen to match intuition. quot should match /, rem should match %, etc.

Come to think of it, this could actually be enforced by having infixl 7 div as / define precedence to be 7 for both div and /. I am not proposing this.

1 Like

No it’s not obvious and in Haskell I’d wonder if my expression is correct too - that’s why I’d do

squaredTriangleNumber n = (n * n * (n + 1) * (n * 1)) `quot` 4

in both languages (and probably remove the braces afterwards because the linter tells me to :wink: )

I don’t really want to argue though - I’m fine with both system and would use both (obviously) - I just like the way it is a bit better.

3 Likes