3 ms·
A major advantage of typeclass polymorphism vs dynamic polymorphism is that typeclasses enable parameteric polymorphism, and parameteric polymorphism enables re
by cynicalkane 12y ago
A major advantage of typeclass polymorphism vs dynamic polymorphism is that typeclasses enable parameteric polymorphism, and parameteric polymorphism enables reasoning that is lost if types can "pretend" to be a supertype.
For instance, in Haskell you can prove a functor instance has at most one implementation (if we disallow diverging values), uniquely given by fmap id === id. GHC can automatically derive it for you. If you have dynamic polymorphism this is lost because now you can have "nonuniform" functor instances that behave differently for different types while still satisfying the functor laws.
More generally, parameteric polymorphism eliminates a large number of "behavior surprises" that can lurk in OO code. And with existential types I never find myself missing dynamic polymorphism. Similarly, in Clojure, if I find myself using dynamic behavior, it's either to interact with Java or as a hacky version of parameteric behavior.
- skybrian 12y agoI don't understand your example. Is there an explanation for people who aren't Haskell experts somewhere?
- deleted 12y ago[deleted]
- frowaway001 12y ago> For instance, in Haskell you can prove a functor instance has at most one implementation How would one do that?
- tome 12y agoI believe the technique is relational parametricity, but I don't actually know how to apply it in this case.