base-4.2.0.0: Basic librariesContentsIndex
Data.Monoid
Portabilityportable
Stabilityexperimental
Maintainerlibraries@haskell.org
Contents
Monoid typeclass
Bool wrappers
Num wrappers
Maybe wrappers
Description
A class for monoids (types with an associative binary operation that has an identity) with various general-purpose instances.
Synopsis
class Monoid a where
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> a
newtype Dual a = Dual {
getDual :: a
}
newtype Endo a = Endo {
appEndo :: a -> a
}
newtype All = All {
getAll :: Bool
}
newtype Any = Any {
getAny :: Bool
}
newtype Sum a = Sum {
getSum :: a
}
newtype Product a = Product {
getProduct :: a
}
newtype First a = First {
getFirst :: Maybe a
}
newtype Last a = Last {
getLast :: Maybe a
}
Monoid typeclass
class Monoid a where

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.

Minimal complete definition: mempty and mappend.

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 newtypes and make those instances of Monoid, e.g. Sum and Product.

Methods
mempty :: a
Identity of mappend
mappend :: a -> a -> a
An associative operation
mconcat :: [a] -> a
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.
newtype Dual a
The dual of a monoid, obtained by swapping the arguments of mappend.
Constructors
Dual
getDual :: a
newtype Endo a
The monoid of endomorphisms under composition.
Constructors
Endo
appEndo :: a -> a
Bool wrappers
newtype All
Boolean monoid under conjunction.
Constructors
All
getAll :: Bool
newtype Any
Boolean monoid under disjunction.
Constructors
Any
getAny :: Bool
Num wrappers
newtype Sum a
Monoid under addition.
Constructors
Sum
getSum :: a
newtype Product a
Monoid under multiplication.
Constructors
Product
getProduct :: a
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))
newtype First a
Maybe monoid returning the leftmost non-Nothing value.
Constructors
First
getFirst :: Maybe a
newtype Last a
Maybe monoid returning the rightmost non-Nothing value.
Constructors
Last
getLast :: Maybe a
Produced by Haddock version 2.6.0