3 ms·
One thing to note is Haskell is exceptionally good at inlining that explicit dictionary away. Haskell isn't 0-cost-abstraction like Rust ofc, but it is definit
by whateveracct 6y ago
One thing to note is Haskell is exceptionally good at inlining that explicit dictionary away.
Haskell isn't 0-cost-abstraction like Rust ofc, but it is definitely a minimize-the-cost-of-abstraction. And the control over said minimization is getting better with each release.
- zenhack 6y agoAlso worth noting that Haskell's type system is too powerful for monomorphization to work as an implementation strategy for generics in all cases. In particular, polyorphic recursion[1] means that attempting to just monomorphize anything wouldn't necessarily terminate. Higher rank types also break this, as you can define things like: newtype GenericThing = GenericThing (forall a. [a] -> a) doManyGenericThings :: [GenericThing] -> [a] -> [a] doManyGenericThings things list = map (\GenericThing f -> f list) things Here, `doManyGenericThings` takes a list of generic functions, and applies each of them to its argument. You can't just monomorphize it, because you'd have to somehow also monomorphize every argument it is ever passed, which is a dynamic property that you can't know in advance (and again, may not be a finite set). [1]: https://en.wikipedia.org/wiki/Polymorphic_recursion https://en.wikipedia.org/wiki/Polymorphic_recursion
- heavenlyblue 6y agoYou should be able to prove that all places that call this function with a fixed list of fixed types are monomorphisable though.
- zenhack 6y agoIt is certainly possible to still monorphize some cases, but the point is it can't work as a comprehensive strategy like it does in rust; it's an optimization that applies in some cases.
- platz 6y agoIt can't inline the dict in general though, and that is more the default situation than the opposite, unless the defn is in the same module