2 ms·
Also 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
by zenhack 6y ago
Also 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.