Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created June 9, 2013 13:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/5743471 to your computer and use it in GitHub Desktop.
Save joseanpg/5743471 to your computer and use it in GitHub Desktop.
Gaster's Algebraic hierarchy
-- http://benedictgaster.org/wp-content/uploads/2012/08/haskwork97.pdf
-- Polymorphic Extensible Records for Haskell (page 3)
type Monoid v r = Rec { plus :: v -> v -> v , id :: v | r }
type Group v r = Monoid v { inv :: v -> v | r }
type Ring v r = Group v { mult :: v -> v -> v , one :: v | r }
iMonoid :: Monoid Int {}
iMonoid = ( plus = (+) , id = 0 )
iGroup :: Group Int {}
iGroup = (inv = negate | iMonoid)
iRing :: Ring Int {}
iRing = ( mult = (*) , one = 1 | iGroup )
--
sum :: Monoid a r -> [a] -> a
sum monoid = foldr ( monoid.plus ) ( monoid.id )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment