Skip to content

Instantly share code, notes, and snippets.

@emilypi
Last active May 8, 2021 16:39
Show Gist options
  • Save emilypi/e29bd7625544cf5ca6d64883eefabbde to your computer and use it in GitHub Desktop.
Save emilypi/e29bd7625544cf5ca6d64883eefabbde to your computer and use it in GitHub Desktop.
{-# LANGUAGE NoImplicitPrelude #-}
module Homs where
(.) :: (b -> c) -> (a -> b) -> (a -> c)
g . f = \a -> g (f a)
-- the class of covariant functors
class Functor f where
fmap :: (a -> b) -> f a -> f b
-- laws: fmap f . fmap g ~ fmap (f . g)
-- the class of contravariant functors
class Contravariant f where
contramap :: (a -> b) -> f b -> f a
-- Covariant hom functors
newtype Cov n x = Cov (n -> x)
-- Contravariant hom functor
newtype Con n x = Con (x -> n)
instance Functor (Cov n) where
fmap xy (Cov nx) = Cov (xy . nx)
-- C(n,x) -> C(n,y) via post composition with f : x -> y in C
instance Contravariant (Con n) where
contramap xy (Con ny) = Con (ny . xy)
-- C(n,y) -> C(n,x) via precomposition with f : x -> y in C^op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment