Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active December 24, 2020 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbbx6spp/cb4166c01a81cc237b3b06665241cefc to your computer and use it in GitHub Desktop.
Save mbbx6spp/cb4166c01a81cc237b3b06665241cefc to your computer and use it in GitHub Desktop.
Fast and loose reasoning as to why Functor and Cofunctor are the same. I have to answer this when coworkers as why Comonads are also Functors and not Cofunctors.
-- Informally showing that Cofunctor is essentially the same structure as Functor thus why Comonads are a Functor which is
-- effectively a Cofunctor.
-- In PureScript Functor is defined here: https://pursuit.purescript.org/packages/purescript-prelude/4.1.1/docs/Data.Functor#t:Functor
-- class Functor (f :: Type -> Type) where
-- map :: forall a b. (a -> b) -> f a -> f b
-- Informally: Making a dual of something is basically flipping the arrows. But to make is easier to see, let's flip the
-- arguments of map in Functor definition since that shouldn't change anything, like so:
class Functor' (f :: Type -> Type) where
mapFlipped :: forall a b. f a -> (a -> b) -> f b
-- Since flipping order of arguments doesn't structurally change things then Functor' is essentially equivalent to Functor
-- in Prelude (see link above).
class Cofunctor' (f :: Type -> Type) where
comapFlipped :: forall b a. f b -> (b -> a) -> f a
-- swapping type variables of a for b and b for a gives us:
-- forall a b. f a -> (a -> b) -> f b
-- which is mapFlipped
-- Flipping order of arguments doesn't structurally change things therefore Cofunctor' is equivalent to Cofunctor
-- and since Functor' === Cofunctor' then it follows that Functor === Cofunctor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment