Wanted: tricks for conflicting instances
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
Hello,
Is it plausible to improve instance selection to use contexts? I imagine doing so would require some kind of backtracking search.
Sometimes you can explicitly program instance selection by using type level bools. Oleg has written several examples of this; here is one which implements Prolog style proof-search: http://okmij.org/ftp/Haskell/types.html#de-typechecker If you can implement IsFunctor and IsCofunctor as type level functions (which might be very tedious or even impossible), then you could re-implement Functor and Cofunctor with the desired functionality. hth, Jeff
participants (2)
-
Conal Elliott -
jeff p