Hacker News new | past | comments | ask | show | jobs | submit login

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.




I don't understand your example. Is there an explanation for people who aren't Haskell experts somewhere?


> For instance, in Haskell you can prove a functor instance has at most one implementation

How would one do that?


I believe the technique is relational parametricity, but I don't actually know how to apply it in this case.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: