I don't think there's a way to do that directly, unfortunately. With a function you can use flip
to partially apply the second argument, but that doesn't work with type constructors like Either
.
The simplest thing is probably wrapping it in a newtype
:
newtype Mirror b a = Mirrored (Either a b)
instance Functor (Mirror e) where
fmap _ (Mirrored (Right a)) = Mirrored $ Right a
fmap f (Mirrored (Left b)) = Mirrored $ Left (f b)
Wrapping with newtype
is also the standard way to create multiple instances for a single type, such as Sum
and Product
being instances of Monoid
for numeric types. Otherwise, you can only have one instance per type.
Additionally, depending on what it is you want to do, another option is to ignore Functor
and define your own type class like this:
class Bifunctor f where
bimap :: (a -> c) -> (b -> d) -> f a b -> f c d
instance Bifunctor Either where
bimap f _ (Left a) = Left $ f a
bimap _ g (Right b) = Right $ g b
instance Bifunctor (,) where
bimap f g (a, b) = (f a, g b)
Obviously, that class is twice as much fun as a regular Functor
. Of course, you can't make a Monad
instance out of that very easily.