[Haskell] Wanted: tricks for conflicting instances
Conal Elliott
conal at conal.net
Thu Jun 14 21:48:57 EDT 2007
In playing with compositions involving functors & cofunctors, I've run into
a conflict in the instances I want to provide. I imagine this sort of
problem is well-known, and I'd like to hear what kinds of strategies people
apply.
Here's a definition of type composition:
newtype O g f a = O { unO :: g (f a) }
and a cofunctor class:
class Cofunctor cof where
cofmap :: (a -> b) -> (cof b -> cof a)
We can compose functors to get a functor, cofunctors to get a functor, and
functor & cofunctor in either order to get a cofunctor.
instance (Functor g, Functor f) => Functor (O g f) where
fmap h (O gf) = O (fmap (fmap h) gf)
instance (Cofunctor g, Functor f) => Cofunctor (O g f) where
cofmap h (O gf) = O (cofmap (fmap h) gf)
instance (Functor g, Cofunctor f) => Cofunctor (O g f) where
cofmap h (O gf) = O (fmap (cofmap h) gf)
instance (Cofunctor g, Cofunctor f) => Functor (O g f) where
fmap h (O gf) = O (cofmap (cofmap h) gf)
I've wanted all four of those instances. The problem is that instance
selection (in GHC at least) ignores the contexts. Without context, the
first and fourth instances conflict, as do the second and third. Thus I
statically choose two of the four rules and comment out the other two.
Depending on my application, sometimes I like my choices, and sometimes I
don't.
Are there work-arounds to get the flexibility I want out of GHC?
Is it plausible to improve instance selection to use contexts? I imagine
doing so would require some kind of backtracking search.
Thanks, - Conal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20070614/abd635da/attachment.htm
More information about the Haskell
mailing list