We can use the extension ConstraintKinds to extend the functionality of the base type classes to allow constraints. For example, we can make an unboxed vector a functor:
class Functor f where
type FunctorConstraint f x :: Constraint
type FunctorConstraint f x = ()
fmap :: (FunctorConstraint f a, FunctorConstraint f b) => (a -> b) -> f a -> f b
instance Functor VU.Vector where
type FunctorConstraint VU.Vector x = VU.Unbox x
fmap = VU.map
(See these blog posts for more details).
I have noticed myself implementing a rather large portion of the base library type classes in this new style (basically I want to be able to work interchangeably between unboxed vectors and lists), and am wondering if a library for this already exists that I should use, or if I should flesh mine out and add it to hackage.
Edit: Also, are there plans to add this directly to base? It seems like it shouldn't break anything else just by updating the class definitions directly.
Since it sounds like no one else has a library, I've converted what I've done into a separate project called ConstraintKinds and uploaded it to github. It's extremely simple at the moment and probably doesn't cover any one else's use case yet, but feel free to make your own changes or submit requests.
I will probably wait for more feedback on if this is a good direction before uploading the project to hackage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With