Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Last active January 30, 2017 08:30
Show Gist options
  • Save chris-martin/353beaf00580f6a6d5cb788fe3e18ebc to your computer and use it in GitHub Desktop.
Save chris-martin/353beaf00580f6a6d5cb788fe3e18ebc to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
import Control.Category (Category)
import qualified Control.Category as Cat
-----------------------------------------------------------------------
-- Endofunctions
-----------------------------------------------------------------------
-- | The monoid of endofunctions under function composition.
-- This is the same as 'Endo'', specialized to the '(->)' category.
newtype Endo a = Endo { appEndo :: a -> a }
deriving (Generic)
instance Monoid (Endo a) where
mempty = Endo id
Endo f `mappend` Endo g = Endo (f . g)
instance Semigroup (Endo a) where
-----------------------------------------------------------------------
-- Endomorphisms
-----------------------------------------------------------------------
-- | The monoid of endomorphisms under morphism composition.
newtype Endo' cat a = Endo' { appEndo' :: cat a a }
deriving Generic
instance Category cat => Monoid (Endo' cat a) where
Endo' f `mappend` Endo' g = Endo' (f Cat.. g)
mempty = Endo' Cat.id
instance Category cat => Semigroup (Endo' cat a) where
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment