I have a pluggable runtime type checker that supports parametric but no ad-hoc polymorphism, because there is no compiling step and type information are erased as soon as the type checker is deactivated.
Now I recently came up with the idea to reify type classes with an explicit type, so that I gain some of their advantages without having to fully incorporate the underlying mechanism into the type checker:
data Functor f = Functor {fmap :: forall a b. (a -> b) -> f a -> f b}
mapList = Functor map
fmap (mapList) (+1) [1,2,3]
It seems as if type classes can be simulated with rank-2 types, at least at the type level, since, of course, there is still no static dispatching.
Is my assumption right and since I am a Haskell rookie, does my explicit functor type involves any advantages over just using map
directly?
Data.Set
, for which most operations demand anOrd
dictionary. Having just one dictionary possible means you can't introduce bugs by matching up dictionaries and sets in the wrong way. And storing the dictionary in the set doesn't really help much, either, because there are operations that take two sets as arguments, and you really want them to have been constructed using the same dictionary. – Seeder