Skip to content

Instantly share code, notes, and snippets.

@emilypi
Created May 29, 2018 15:23
Show Gist options
  • Save emilypi/c9190985f9a71702db46ca53c8091840 to your computer and use it in GitHub Desktop.
Save emilypi/c9190985f9a71702db46ca53c8091840 to your computer and use it in GitHub Desktop.
Functor Results
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
module RB where
import Data.Functor.Contravariant
newtype O f g e = Comp { deComp :: f (g e) }
newtype Square f a = Square { unSquare :: f (f a) }
instance Contravariant f => Functor (Square f) where
fmap f = Square . contramap (contramap f) . unSquare
instance (Contravariant f, Contravariant g) => Functor (O f g) where
fmap f = Comp . contramap (contramap f) . deComp
class Coyo f a where
coyoneda :: (b -> a) -> f a -> f b
class Yon f a where
yoneda :: (a -> b) -> f b
instance (Functor f, Contravariant g) => Coyo (O f g) (g a) where
coyoneda f = Comp . fmap (contramap f) . deComp
instance (Contravariant f, Functor g) => Coyo (O f g) (g a) where
coyoneda f = Comp . contramap (fmap f) . deComp
instance (Functor f, Functor g) => Yon (O f g) (g a) where
yoneda f = Comp . fmap (fmap f) . deComp
instance (Contravariant f, Contravariant g) => Yon (O f g) (g a) where
yoneda f = Comp . contramap (contramap f) . deComp
newtype Product f g a = Product (f a, g a)
newtype Coproduct f g a = Coproduct (Either (f a) (g a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment