Closed kostmo closed 1 year ago
Sounds like a good idea, given this is quite a foot gun. Patch welcome!
I just came across this reddit post as more empirical evidence that the negation precedence can be "baffling" and a good demonstration of the value that this lint rule will provide.
Motivation
The operator precedence of "prefix minus" can be surprising to unsuspecting Haskell programmers.
For example, in the REPL:
and
The case that prompted the filing of this issue was:
which led to inferring incorrect semantics of Haskell's
mod
function, illustrated in this thread. As it happens, unlike some other languages,mod a b
in Haskell is non-negative for all positiveb
.There is an enlightening thread on the Haskell mailing list from July 2010 that discusses fixity of the prefix minus and references (defunct?) proposals to make it bind more tightly. This corroborates the notion that the current precedence of prefix negation can be counter-intuitive.
Proposal
In contrast with existing
hlint
rules that suggest to remove redundant brackets, I propose thathlint
suggest to add redundant brackets to disambiguate (for human readers) negation precedence.Such a rule could catch bugs; when an author writes:
hlint
would suggest instead to write:which would cause the author to realize that their expression is being parsed differently than intended and update their code to:
Implementation
The rule should fire whenever the operand of "unary minus" is an infix operation of higher precedence.
For reference, unary negation is fixity 6, the same as binary subtraction. Multiplication, division, and modulus have fixity 7, and exponentiation has fixity 8.