Permalink
Browse files

clarified Monoid example

  • Loading branch information...
1 parent b7afdc4 commit 589d5d04d38d1b252bf3e43a7114e3c0a9ea8ab2 @ekmett committed Jun 18, 2012
Showing with 5 additions and 3 deletions.
  1. +5 −3 examples/Monoid.hs
View
@@ -1,18 +1,20 @@
-{-# LANGUAGE Rank2Types, TypeFamilies, FlexibleContexts, UndecidableInstances #-}
+{-# LANGUAGE Rank2Types, FlexibleContextsUndecidableInstances #-}
import Data.Reflection -- from reflection
import Data.Monoid -- from base
import Data.Proxy -- from tagged
--- | Values in our dynamically constructed monoid over 'a'
+-- | Values in our dynamically-constructed 'Monoid' over 'a'
newtype M a s = M { runM :: a } deriving (Eq,Ord)
--- | A dictionary describing the contents of a monoid
+-- | A dictionary describing a 'Monoid'
data Monoid_ a = Monoid_ { mappend_ :: a -> a -> a, mempty_ :: a }
instance Reifies s (Monoid_ a) => Monoid (M a s) where
mappend a b = M $ mappend_ (reflect a) (runM a) (runM b)
mempty = a where a = M $ mempty_ (reflect a)
+-- Construct a 'Monoid' instance out of a binary operation and unit that you have in scope!
+--
-- > ghci> withMonoid (+) 0 $ mempty <> M 2
-- > 2
withMonoid :: (a -> a -> a) -> a -> (forall s. Reifies s (Monoid_ a) => M a s) -> a

0 comments on commit 589d5d0

Please sign in to comment.