base-4.7.0.0: Basic libraries

Copyright(c) Andy Gill 2001, (c) Oregon Graduate Institute of Science and Technology, 2001
LicenseBSD-style (see the file libraries/base/LICENSE)
Maintainerlibraries@haskell.org
Stabilityexperimental
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Monoid

Contents

Description

A class for monoids (types with an associative binary operation that has an identity) with various general-purpose instances.

Synopsis

Monoid typeclass

class Monoid a where Source

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 Source

Identity of mappend

mappend :: a -> a -> a Source

An associative operation

mconcat :: [a] -> a Source

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.

Instances

Monoid Ordering 
Monoid () 
Monoid Any 
Monoid All 
Monoid Event 
Monoid [a] 
Monoid a => Monoid (Maybe a)

Lift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup S may be turned into a monoid simply by adjoining an element e not in S and defining e*e = e and e*s = s = s*e for all s ∈ S." Since there is no "Semigroup" typeclass providing just mappend, we use Monoid instead.

Monoid (Last a) 
Monoid (First a) 
Num a => Monoid (Product a) 
Num a => Monoid (Sum a) 
Monoid (Endo a) 
Monoid a => Monoid (Dual a) 
Monoid b => Monoid (a -> b) 
(Monoid a, Monoid b) => Monoid (a, b) 
Monoid (Proxy * s) 
Monoid a => Monoid (Const a b) 
Typeable (* -> Constraint) Monoid 
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) 
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) 
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) 

(<>) :: Monoid m => m -> m -> m Source

An infix synonym for mappend.

Since: 4.5.0.0

newtype Dual a Source

The dual of a monoid, obtained by swapping the arguments of mappend.

Constructors

Dual 

Fields

getDual :: a
 

Instances

Generic1 Dual 
Bounded a => Bounded (Dual a) 
Eq a => Eq (Dual a) 
Ord a => Ord (Dual a) 
Read a => Read (Dual a) 
Show a => Show (Dual a) 
Generic (Dual a) 
Monoid a => Monoid (Dual a) 
type Rep1 Dual = D1 D1Dual (C1 C1_0Dual (S1 S1_0_0Dual Par1)) 
type Rep (Dual a) = D1 D1Dual (C1 C1_0Dual (S1 S1_0_0Dual (Rec0 a))) 

newtype Endo a Source

The monoid of endomorphisms under composition.

Constructors

Endo 

Fields

appEndo :: a -> a
 

Instances

Generic (Endo a) 
Monoid (Endo a) 
type Rep (Endo a) = D1 D1Endo (C1 C1_0Endo (S1 S1_0_0Endo (Rec0 (a -> a)))) 

Bool wrappers

newtype All Source

Boolean monoid under conjunction.

Constructors

All 

Fields

getAll :: Bool
 

Instances

Bounded All 
Eq All 
Ord All 
Read All 
Show All 
Generic All 
Monoid All 
type Rep All = D1 D1All (C1 C1_0All (S1 S1_0_0All (Rec0 Bool))) 

newtype Any Source

Boolean monoid under disjunction.

Constructors

Any 

Fields

getAny :: Bool
 

Instances

Bounded Any 
Eq Any 
Ord Any 
Read Any 
Show Any 
Generic Any 
Monoid Any 
type Rep Any = D1 D1Any (C1 C1_0Any (S1 S1_0_0Any (Rec0 Bool))) 

Num wrappers

newtype Sum a Source

Monoid under addition.

Constructors

Sum 

Fields

getSum :: a
 

Instances

Generic1 Sum 
Bounded a => Bounded (Sum a) 
Eq a => Eq (Sum a) 
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 => Monoid (Sum a) 
type Rep1 Sum = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum Par1)) 
type Rep (Sum a) = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum (Rec0 a))) 

newtype Product a Source

Monoid under multiplication.

Constructors

Product 

Fields

getProduct :: a
 

Instances

Generic1 Product 
Bounded a => Bounded (Product a) 
Eq a => Eq (Product a) 
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 => Monoid (Product a) 
type Rep1 Product = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product Par1)) 
type Rep (Product a) = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product (Rec0 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 Source

Maybe monoid returning the leftmost non-Nothing value.

Constructors

First 

Fields

getFirst :: Maybe a
 

Instances

Generic1 First 
Eq a => Eq (First a) 
Ord a => Ord (First a) 
Read a => Read (First a) 
Show a => Show (First a) 
Generic (First a) 
Monoid (First a) 
type Rep1 First = D1 D1First (C1 C1_0First (S1 S1_0_0First (Rec1 Maybe))) 
type Rep (First a) = D1 D1First (C1 C1_0First (S1 S1_0_0First (Rec0 (Maybe a)))) 

newtype Last a Source

Maybe monoid returning the rightmost non-Nothing value.

Constructors

Last 

Fields

getLast :: Maybe a
 

Instances

Generic1 Last 
Eq a => Eq (Last a) 
Ord a => Ord (Last a) 
Read a => Read (Last a) 
Show a => Show (Last a) 
Generic (Last a) 
Monoid (Last a) 
type Rep1 Last = D1 D1Last (C1 C1_0Last (S1 S1_0_0Last (Rec1 Maybe))) 
type Rep (Last a) = D1 D1Last (C1 C1_0Last (S1 S1_0_0Last (Rec0 (Maybe a))))