Copyright  (c) Andy Gill 2001 (c) Oregon Graduate Institute of Science and Technology 2001 

License  BSDstyle (see the file libraries/base/LICENSE) 
Maintainer  [email protected] 
Stability  experimental 
Portability  portable 
Safe Haskell  Trustworthy 
Language  Haskell2010 
A class for monoids (types with an associative binary operation that has an identity) with various generalpurpose instances.
 class Monoid a where
 (<>) :: Monoid m => m > m > m
 newtype Dual a = Dual {
 getDual :: a
 newtype Endo a = Endo {
 appEndo :: a > a
 newtype All = All {}
 newtype Any = Any {}
 newtype Sum a = Sum {
 getSum :: a
 newtype Product a = Product {
 getProduct :: a
 newtype First a = First {}
 newtype Last a = Last {}
 newtype Alt f a = Alt {
 getAlt :: f a
Monoid
typeclass
The class of monoids (types with an associative binary operation that has an identity). Instances should satisfy the following laws:
mappend mempty x = x
mappend x mempty = x
mappend x (mappend y z) = mappend (mappend x y) z
mconcat =
foldr
mappend mempty
The method names refer to the monoid of lists under concatenation, but there are many other instances.
Some types can be viewed as a monoid in more than one way,
e.g. both addition and multiplication on numbers.
In such cases we often define newtype
s and make those instances
of Monoid
, e.g. Sum
and Product
.
Identity of mappend
mappend :: a > a > a Source #
An associative operation
Fold a list using the monoid.
For most types, the default definition for mconcat
will be
used, but the function is included in the class definition so
that an optimized version can be provided for specific types.
Monoid Ordering #  Since: 2.1 
Monoid () #  Since: 2.1 
Monoid Any #  Since: 2.1 
Monoid All #  Since: 2.1 
Monoid Lifetime # 
Since: 4.8.0.0 
Monoid Event #  Since: 4.3.1.0 
Monoid [a] #  Since: 2.1 
Monoid a => Monoid (Maybe a) #  Lift a semigroup into Since: 2.1 
Monoid a => Monoid (IO a) #  Since: 4.9.0.0 
Monoid (Last a) #  Since: 2.1 
Monoid (First a) #  Since: 2.1 
Num a => Monoid (Product a) #  Since: 2.1 
Num a => Monoid (Sum a) #  Since: 2.1 
Monoid (Endo a) #  Since: 2.1 
Monoid a => Monoid (Dual a) #  Since: 2.1 
Monoid a => Monoid (Identity a) #  
Semigroup a => Monoid (Option a) #  Since: 4.9.0.0 
Monoid m => Monoid (WrappedMonoid m) #  Since: 4.9.0.0 
(Ord a, Bounded a) => Monoid (Max a) #  Since: 4.9.0.0 
(Ord a, Bounded a) => Monoid (Min a) #  Since: 4.9.0.0 
Monoid b => Monoid (a > b) #  Since: 2.1 
(Monoid a, Monoid b) => Monoid (a, b) #  Since: 2.1 
Monoid (Proxy k s) #  Since: 4.7.0.0 
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) #  Since: 2.1 
Alternative f => Monoid (Alt * f a) #  Since: 4.8.0.0 
Monoid a => Monoid (Const k a b) #  
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) #  Since: 2.1 
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) #  Since: 2.1 
Monad Dual #  Since: 4.8.0.0 
Functor Dual #  Since: 4.8.0.0 
MonadFix Dual #  Since: 4.8.0.0 
Applicative Dual #  Since: 4.8.0.0 
Foldable Dual #  Since: 4.8.0.0 
Traversable Dual #  Since: 4.8.0.0 
MonadZip Dual #  Since: 4.8.0.0 
Bounded a => Bounded (Dual a) #  
Eq a => Eq (Dual a) #  
Data a => Data (Dual a) #  Since: 4.8.0.0 
Ord a => Ord (Dual a) #  
Read a => Read (Dual a) #  
Show a => Show (Dual a) #  
Generic (Dual a) #  
Semigroup a => Semigroup (Dual a) #  Since: 4.9.0.0 
Monoid a => Monoid (Dual a) #  Since: 2.1 
Generic1 * Dual #  
type Rep (Dual a) #  
type Rep1 * Dual #  
The monoid of endomorphisms under composition.
Bool
wrappers
Boolean monoid under conjunction (&&
).
Boolean monoid under disjunction (
).
Num
wrappers
Monoid under addition.
Monad Sum #  Since: 4.8.0.0 
Functor Sum #  Since: 4.8.0.0 
MonadFix Sum #  Since: 4.8.0.0 
Applicative Sum #  Since: 4.8.0.0 
Foldable Sum #  Since: 4.8.0.0 
Traversable Sum #  Since: 4.8.0.0 
MonadZip Sum #  Since: 4.8.0.0 
Bounded a => Bounded (Sum a) #  
Eq a => Eq (Sum a) #  
Data a => Data (Sum a) #  Since: 4.8.0.0 
Num a => Num (Sum a) #  
Ord a => Ord (Sum a) #  
Read a => Read (Sum a) #  
Show a => Show (Sum a) #  
Generic (Sum a) #  
Num a => Semigroup (Sum a) #  Since: 4.9.0.0 
Num a => Monoid (Sum a) #  Since: 2.1 
Generic1 * Sum #  
type Rep (Sum a) #  
type Rep1 * Sum #  
Monoid under multiplication.
Product  

Monad Product #  Since: 4.8.0.0 
Functor Product #  Since: 4.8.0.0 
MonadFix Product #  Since: 4.8.0.0 
Applicative Product #  Since: 4.8.0.0 
Foldable Product #  Since: 4.8.0.0 
Traversable Product #  Since: 4.8.0.0 
MonadZip Product #  Since: 4.8.0.0 
Bounded a => Bounded (Product a) #  
Eq a => Eq (Product a) #  
Data a => Data (Product a) #  Since: 4.8.0.0 
Num a => Num (Product a) #  
Ord a => Ord (Product a) #  
Read a => Read (Product a) #  
Show a => Show (Product a) #  
Generic (Product a) #  
Num a => Semigroup (Product a) #  Since: 4.9.0.0 
Num a => Monoid (Product a) #  Since: 2.1 
Generic1 * Product #  
type Rep (Product a) #  
type Rep1 * Product #  
Maybe
wrappers
To implement find
or findLast
on any Foldable
:
findLast :: Foldable t => (a > Bool) > t a > Maybe a findLast pred = getLast . foldMap (x > if pred x then Last (Just x) else Last Nothing)
Much of Data.Map's interface can be implemented with
Data.Map.alter. Some of the rest can be implemented with a new
alterA
function and either First
or Last
:
alterA :: (Applicative f, Ord k) => (Maybe a > f (Maybe a)) > k > Map k a > f (Map k a) instance Monoid a => Applicative ((,) a)  from Control.Applicative
insertLookupWithKey :: Ord k => (k > v > v > v) > k > v > Map k v > (Maybe v, Map k v) insertLookupWithKey combine key value = Arrow.first getFirst . alterA doChange key where doChange Nothing = (First Nothing, Just value) doChange (Just oldValue) = (First (Just oldValue), Just (combine key value oldValue))
Maybe monoid returning the leftmost nonNothing value.
is isomorphic to First
a
, but precedes it
historically.Alt
Maybe
a
Monad First #  
Functor First #  
MonadFix First #  Since: 4.8.0.0 
Applicative First #  
Foldable First #  Since: 4.8.0.0 
Traversable First #  Since: 4.8.0.0 
MonadZip First #  Since: 4.8.0.0 
Eq a => Eq (First a) #  
Data a => Data (First a) #  Since: 4.8.0.0 
Ord a => Ord (First a) #  
Read a => Read (First a) #  
Show a => Show (First a) #  
Generic (First a) #  
Semigroup (First a) #  Since: 4.9.0.0 
Monoid (First a) #  Since: 2.1 
Generic1 * First #  
type Rep (First a) #  
type Rep1 * First #  
Maybe monoid returning the rightmost nonNothing value.
is isomorphic to Last
a
, and thus to
Dual
(First
a)Dual
(Alt
Maybe
a)
Monad Last #  
Functor Last #  
MonadFix Last #  Since: 4.8.0.0 
Applicative Last #  
Foldable Last #  Since: 4.8.0.0 
Traversable Last #  Since: 4.8.0.0 
MonadZip Last #  Since: 4.8.0.0 
Eq a => Eq (Last a) #  
Data a => Data (Last a) #  Since: 4.8.0.0 
Ord a => Ord (Last a) #  
Read a => Read (Last a) #  
Show a => Show (Last a) #  
Generic (Last a) #  
Semigroup (Last a) #  Since: 4.9.0.0 
Monoid (Last a) #  Since: 2.1 
Generic1 * Last #  
type Rep (Last a) #  
type Rep1 * Last #  
Alternative
wrapper
Monoid under <>
.
Since: 4.8.0.0
Generic1 k (Alt k f) #  
Monad f => Monad (Alt * f) #  
Functor f => Functor (Alt * f) #  
MonadFix f => MonadFix (Alt * f) #  Since: 4.8.0.0 
Applicative f => Applicative (Alt * f) #  
MonadPlus f => MonadPlus (Alt * f) #  
Alternative f => Alternative (Alt * f) #  
MonadZip f => MonadZip (Alt * f) #  Since: 4.8.0.0 
Enum (f a) => Enum (Alt k f a) #  
Eq (f a) => Eq (Alt k f a) #  
(Data (f a), Data a, Typeable (* > *) f) => Data (Alt * f a) #  Since: 4.8.0.0 
Num (f a) => Num (Alt k f a) #  
Ord (f a) => Ord (Alt k f a) #  
Read (f a) => Read (Alt k f a) #  
Show (f a) => Show (Alt k f a) #  
Generic (Alt k f a) #  
Alternative f => Semigroup (Alt * f a) #  Since: 4.9.0.0 
Alternative f => Monoid (Alt * f a) #  Since: 4.8.0.0 
type Rep1 k (Alt k f) #  
type Rep (Alt k f a) #  