Last active
January 30, 2017 08:30
-
-
Save chris-martin/353beaf00580f6a6d5cb788fe3e18ebc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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