Created
June 9, 2013 13:14
-
-
Save joseanpg/5743471 to your computer and use it in GitHub Desktop.
Gaster's Algebraic hierarchy
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
-- 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