|Version 1 (modified by simonmar@…, 10 years ago) (diff)|
The Monomorphism Restriction
The M-R is widely regarded as an ugly part of the Haskell 98 language definition. Here are the main alternatives on the table:
Remove it altogether
We could simply remove the M-R from the language, but suggest that compilers issue a warning when a loss of sharing might occur due to overloading of a variable or pattern binding.
- Simple, removes a wart from the language
- "As much polymorphism as possible" is in the spirit of Haskell - it is strange to limit polymorphism for performance reasons (indeed, this is arguably a poor compromise)
- It turns out to be quite hard to demonstrate a performance problem due to the M-R, at least with GHC, because its optimiser often recovers the sharing.
- Even if you do get loss of sharing, profiling will quickly pinpoint it
- nhc98 has never implemented the M-R, and users haven't found any significant problems as a result
- Hugs has a different (non-Haskell98) implementation of the M-R
- Might be hard to give an accurate warning; just warning about overloaded variable bindings isn't good enough, because they don't all result in loss of sharing.
- The warning might be confusing to new users (but if we could make it accurate, it wouldn't happen much)
A monomorphic binding operator
Introduce a new binding operator for monomorphic bindings, eg. :=.
- Simpler and more consistent than the M-R
- The reason for having two kinds of binding is subtle and hard to explain to newcomers.
- Still a wart, but an even more visible one.
All variable/pattern bindings are monomorphic unless a signature is given
- Simpler than the M-R
- Polymorphism in local variable bindings is rare (but less rare at the top-level), and can always be recovered with a type signature
- Against the spirit of Haskell - shouldn't compromise expressiveness for performance by default
- Already huge potential for ruining your performance without the M-R, why introduce such draconian measures just for this?
- Monomorphic bindings lead to hard to understand errors when polymorphism was expected