Show parenthesised output of expressions in ghci
Operator fixity can be a source of bugs (even for experienced users!),[#point1 ¹] [#point2 ²] precedence levels may vary between languages and then there are user-defined operators.
Attached is a patch for a ghci command allowing users to quickly parenthesise expressions as a sanity check (and as an alternative to using :info and parsing the result yourself). The command itself is activated with :paren
:
ghci> :paren 2 + 10 * 4 / 3
2 + ((10 * 4) / 3)
ghci> :paren \a b → a == b || b && a == (b == a) || b
\ a b -> (a == b) || ((b && (a == (b == a))) || b)
ghci> :paren 5 + 1 `mod` 2
5 + (1 `mod` 2)
ghci> :paren 5 * 1 `mod` 2
(5 * 1) `mod` 2
A friend of mine also pointed out that this could eventually be integrated into haskell-mode
where the user could temporarily replace an expression with a parenthesised version or have different colours indicate different levels of logical nesting.
An example that is tricky for me is calculateBmi w h = w / h^2
.
[=#point1 ¹] Developer beliefs about binary operator precedence
[=#point2 ²] Brace Yourself (tweet)