base-4.8.1.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.

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.

Minimal complete definition

mempty, mappend

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 Source 
Monoid () Source 

Methods

mempty :: () Source

mappend :: () -> () -> () Source

mconcat :: [()] -> () Source

Monoid Any Source 
Monoid All Source 
Monoid Lifetime Source

mappend == elSupremum

Monoid Event Source 
Monoid [a] Source 

Methods

mempty :: [a] Source

mappend :: [a] -> [a] -> [a] Source

mconcat :: [[a]] -> [a] Source

Monoid a => Monoid (Maybe a) Source

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.

Methods

mempty :: Maybe a Source

mappend :: Maybe a -> Maybe a -> Maybe a Source

mconcat :: [Maybe a] -> Maybe a Source

Monoid (Last a) Source 

Methods

mempty :: Last a Source

mappend :: Last a -> Last a -> Last a Source

mconcat :: [Last a] -> Last a Source

Monoid (First a) Source 

Methods

mempty :: First a Source

mappend :: First a -> First a -> First a Source

mconcat :: [First a] -> First a Source

Num a => Monoid (Product a) Source 
Num a => Monoid (Sum a) Source 

Methods

mempty :: Sum a Source

mappend :: Sum a -> Sum a -> Sum a Source

mconcat :: [Sum a] -> Sum a Source

Monoid (Endo a) Source 

Methods

mempty :: Endo a Source

mappend :: Endo a -> Endo a -> Endo a Source

mconcat :: [Endo a] -> Endo a Source

Monoid a => Monoid (Dual a) Source 

Methods

mempty :: Dual a Source

mappend :: Dual a -> Dual a -> Dual a Source

mconcat :: [Dual a] -> Dual a Source

Monoid b => Monoid (a -> b) Source 

Methods

mempty :: a -> b Source

mappend :: (a -> b) -> (a -> b) -> a -> b Source

mconcat :: [a -> b] -> a -> b Source

(Monoid a, Monoid b) => Monoid (a, b) Source 

Methods

mempty :: (a, b) Source

mappend :: (a, b) -> (a, b) -> (a, b) Source

mconcat :: [(a, b)] -> (a, b) Source

Monoid (Proxy k s) Source 

Methods

mempty :: Proxy k s Source

mappend :: Proxy k s -> Proxy k s -> Proxy k s Source

mconcat :: [Proxy k s] -> Proxy k s Source

Monoid a => Monoid (Const a b) Source 

Methods

mempty :: Const a b Source

mappend :: Const a b -> Const a b -> Const a b Source

mconcat :: [Const a b] -> Const a b Source

(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) Source 

Methods

mempty :: (a, b, c) Source

mappend :: (a, b, c) -> (a, b, c) -> (a, b, c) Source

mconcat :: [(a, b, c)] -> (a, b, c) Source

Alternative f => Monoid (Alt * f a) Source 

Methods

mempty :: Alt * f a Source

mappend :: Alt * f a -> Alt * f a -> Alt * f a Source

mconcat :: [Alt * f a] -> Alt * f a Source

(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) Source 

Methods

mempty :: (a, b, c, d) Source

mappend :: (a, b, c, d) -> (a, b, c, d) -> (a, b, c, d) Source

mconcat :: [(a, b, c, d)] -> (a, b, c, d) Source

(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) Source 

Methods

mempty :: (a, b, c, d, e) Source

mappend :: (a, b, c, d, e) -> (a, b, c, d, e) -> (a, b, c, d, e) Source

mconcat :: [(a, b, c, d, e)] -> (a, b, c, d, e) Source

(<>) :: Monoid m => m -> m -> m infixr 6 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

Instances

Generic1 Dual Source 

Associated Types

type Rep1 (Dual :: * -> *) :: * -> * Source

Methods

from1 :: Dual a -> Rep1 Dual a Source

to1 :: Rep1 Dual a -> Dual a Source

Bounded a => Bounded (Dual a) Source 
Eq a => Eq (Dual a) Source 

Methods

(==) :: Dual a -> Dual a -> Bool

(/=) :: Dual a -> Dual a -> Bool

Ord a => Ord (Dual a) Source 

Methods

compare :: Dual a -> Dual a -> Ordering

(<) :: Dual a -> Dual a -> Bool

(<=) :: Dual a -> Dual a -> Bool

(>) :: Dual a -> Dual a -> Bool

(>=) :: Dual a -> Dual a -> Bool

max :: Dual a -> Dual a -> Dual a

min :: Dual a -> Dual a -> Dual a

Read a => Read (Dual a) Source 
Show a => Show (Dual a) Source 
Generic (Dual a) Source 

Associated Types

type Rep (Dual a) :: * -> * Source

Methods

from :: Dual a -> Rep (Dual a) x Source

to :: Rep (Dual a) x -> Dual a Source

Monoid a => Monoid (Dual a) Source 

Methods

mempty :: Dual a Source

mappend :: Dual a -> Dual a -> Dual a Source

mconcat :: [Dual a] -> Dual a Source

type Rep1 Dual Source 
type Rep (Dual a) Source 

newtype Endo a Source

The monoid of endomorphisms under composition.

Constructors

Endo 

Fields

Instances

Generic (Endo a) Source 

Associated Types

type Rep (Endo a) :: * -> * Source

Methods

from :: Endo a -> Rep (Endo a) x Source

to :: Rep (Endo a) x -> Endo a Source

Monoid (Endo a) Source 

Methods

mempty :: Endo a Source

mappend :: Endo a -> Endo a -> Endo a Source

mconcat :: [Endo a] -> Endo a Source

type Rep (Endo a) Source 

Bool wrappers

newtype All Source

Boolean monoid under conjunction (&&).

Constructors

All 

Fields

Instances

Bounded All Source 
Eq All Source 

Methods

(==) :: All -> All -> Bool

(/=) :: All -> All -> Bool

Ord All Source 

Methods

compare :: All -> All -> Ordering

(<) :: All -> All -> Bool

(<=) :: All -> All -> Bool

(>) :: All -> All -> Bool

(>=) :: All -> All -> Bool

max :: All -> All -> All

min :: All -> All -> All

Read All Source 
Show All Source 
Generic All Source 

Associated Types

type Rep All :: * -> * Source

Methods

from :: All -> Rep All x Source

to :: Rep All x -> All Source

Monoid All Source 
type Rep All Source 

newtype Any Source

Boolean monoid under disjunction (||).

Constructors

Any 

Fields

Instances

Bounded Any Source 
Eq Any Source 

Methods

(==) :: Any -> Any -> Bool

(/=) :: Any -> Any -> Bool

Ord Any Source 

Methods

compare :: Any -> Any -> Ordering

(<) :: Any -> Any -> Bool

(<=) :: Any -> Any -> Bool

(>) :: Any -> Any -> Bool

(>=) :: Any -> Any -> Bool

max :: Any -> Any -> Any

min :: Any -> Any -> Any

Read Any Source 
Show Any Source 
Generic Any Source 

Associated Types

type Rep Any :: * -> * Source

Methods

from :: Any -> Rep Any x Source

to :: Rep Any x -> Any Source

Monoid Any Source 
type Rep Any Source 

Num wrappers

newtype Sum a Source

Monoid under addition.

Constructors

Sum 

Fields

Instances

Generic1 Sum Source 

Associated Types

type Rep1 (Sum :: * -> *) :: * -> * Source

Methods

from1 :: Sum a -> Rep1 Sum a Source

to1 :: Rep1 Sum a -> Sum a Source

Bounded a => Bounded (Sum a) Source 
Eq a => Eq (Sum a) Source 

Methods

(==) :: Sum a -> Sum a -> Bool

(/=) :: Sum a -> Sum a -> Bool

Num a => Num (Sum a) Source 

Methods

(+) :: Sum a -> Sum a -> Sum a Source

(-) :: Sum a -> Sum a -> Sum a Source

(*) :: Sum a -> Sum a -> Sum a Source

negate :: Sum a -> Sum a Source

abs :: Sum a -> Sum a Source

signum :: Sum a -> Sum a Source

fromInteger :: Integer -> Sum a Source

Ord a => Ord (Sum a) Source 

Methods

compare :: Sum a -> Sum a -> Ordering

(<) :: Sum a -> Sum a -> Bool

(<=) :: Sum a -> Sum a -> Bool

(>) :: Sum a -> Sum a -> Bool

(>=) :: Sum a -> Sum a -> Bool

max :: Sum a -> Sum a -> Sum a

min :: Sum a -> Sum a -> Sum a

Read a => Read (Sum a) Source 
Show a => Show (Sum a) Source 

Methods

showsPrec :: Int -> Sum a -> ShowS Source

show :: Sum a -> String Source

showList :: [Sum a] -> ShowS Source

Generic (Sum a) Source 

Associated Types

type Rep (Sum a) :: * -> * Source

Methods

from :: Sum a -> Rep (Sum a) x Source

to :: Rep (Sum a) x -> Sum a Source

Num a => Monoid (Sum a) Source 

Methods

mempty :: Sum a Source

mappend :: Sum a -> Sum a -> Sum a Source

mconcat :: [Sum a] -> Sum a Source

type Rep1 Sum Source 
type Rep (Sum a) Source 

newtype Product a Source

Monoid under multiplication.

Constructors

Product 

Fields

Instances

Generic1 Product Source 

Associated Types

type Rep1 (Product :: * -> *) :: * -> * Source

Bounded a => Bounded (Product a) Source 
Eq a => Eq (Product a) Source 

Methods

(==) :: Product a -> Product a -> Bool

(/=) :: Product a -> Product a -> Bool

Num a => Num (Product a) Source 
Ord a => Ord (Product a) Source 

Methods

compare :: Product a -> Product a -> Ordering

(<) :: Product a -> Product a -> Bool

(<=) :: Product a -> Product a -> Bool

(>) :: Product a -> Product a -> Bool

(>=) :: Product a -> Product a -> Bool

max :: Product a -> Product a -> Product a

min :: Product a -> Product a -> Product a

Read a => Read (Product a) Source 
Show a => Show (Product a) Source 
Generic (Product a) Source 

Associated Types

type Rep (Product a) :: * -> * Source

Methods

from :: Product a -> Rep (Product a) x Source

to :: Rep (Product a) x -> Product a Source

Num a => Monoid (Product a) Source 
type Rep1 Product Source 
type Rep (Product a) Source 

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.

First a is isomorphic to Alt Maybe a, but precedes it historically.

Constructors

First 

Fields

Instances

Monad First Source 

Methods

(>>=) :: First a -> (a -> First b) -> First b Source

(>>) :: First a -> First b -> First b Source

return :: a -> First a Source

fail :: String -> First a Source

Functor First Source 

Methods

fmap :: (a -> b) -> First a -> First b Source

(<$) :: a -> First b -> First a Source

Applicative First Source 

Methods

pure :: a -> First a Source

(<*>) :: First (a -> b) -> First a -> First b Source

(*>) :: First a -> First b -> First b Source

(<*) :: First a -> First b -> First a Source

Generic1 First Source 

Associated Types

type Rep1 (First :: * -> *) :: * -> * Source

Methods

from1 :: First a -> Rep1 First a Source

to1 :: Rep1 First a -> First a Source

Eq a => Eq (First a) Source 

Methods

(==) :: First a -> First a -> Bool

(/=) :: First a -> First a -> Bool

Ord a => Ord (First a) Source 

Methods

compare :: First a -> First a -> Ordering

(<) :: First a -> First a -> Bool

(<=) :: First a -> First a -> Bool

(>) :: First a -> First a -> Bool

(>=) :: First a -> First a -> Bool

max :: First a -> First a -> First a

min :: First a -> First a -> First a

Read a => Read (First a) Source 
Show a => Show (First a) Source 
Generic (First a) Source 

Associated Types

type Rep (First a) :: * -> * Source

Methods

from :: First a -> Rep (First a) x Source

to :: Rep (First a) x -> First a Source

Monoid (First a) Source 

Methods

mempty :: First a Source

mappend :: First a -> First a -> First a Source

mconcat :: [First a] -> First a Source

type Rep1 First Source 
type Rep (First a) Source 

newtype Last a Source

Maybe monoid returning the rightmost non-Nothing value.

Last a is isomorphic to Dual (First a), and thus to Dual (Alt Maybe a)

Constructors

Last 

Fields

Instances

Monad Last Source 

Methods

(>>=) :: Last a -> (a -> Last b) -> Last b Source

(>>) :: Last a -> Last b -> Last b Source

return :: a -> Last a Source

fail :: String -> Last a Source

Functor Last Source 

Methods

fmap :: (a -> b) -> Last a -> Last b Source

(<$) :: a -> Last b -> Last a Source

Applicative Last Source 

Methods

pure :: a -> Last a Source

(<*>) :: Last (a -> b) -> Last a -> Last b Source

(*>) :: Last a -> Last b -> Last b Source

(<*) :: Last a -> Last b -> Last a Source

Generic1 Last Source 

Associated Types

type Rep1 (Last :: * -> *) :: * -> * Source

Methods

from1 :: Last a -> Rep1 Last a Source

to1 :: Rep1 Last a -> Last a Source

Eq a => Eq (Last a) Source 

Methods

(==) :: Last a -> Last a -> Bool

(/=) :: Last a -> Last a -> Bool

Ord a => Ord (Last a) Source 

Methods

compare :: Last a -> Last a -> Ordering

(<) :: Last a -> Last a -> Bool

(<=) :: Last a -> Last a -> Bool

(>) :: Last a -> Last a -> Bool

(>=) :: Last a -> Last a -> Bool

max :: Last a -> Last a -> Last a

min :: Last a -> Last a -> Last a

Read a => Read (Last a) Source 
Show a => Show (Last a) Source 
Generic (Last a) Source 

Associated Types

type Rep (Last a) :: * -> * Source

Methods

from :: Last a -> Rep (Last a) x Source

to :: Rep (Last a) x -> Last a Source

Monoid (Last a) Source 

Methods

mempty :: Last a Source

mappend :: Last a -> Last a -> Last a Source

mconcat :: [Last a] -> Last a Source

type Rep1 Last Source 
type Rep (Last a) Source 

Alternative wrapper

newtype Alt f a Source

Monoid under <|>.

Since: 4.8.0.0

Constructors

Alt 

Fields

Instances

Monad f => Monad (Alt * f) Source 

Methods

(>>=) :: Alt * f a -> (a -> Alt * f b) -> Alt * f b Source

(>>) :: Alt * f a -> Alt * f b -> Alt * f b Source

return :: a -> Alt * f a Source

fail :: String -> Alt * f a Source

Functor f => Functor (Alt * f) Source 

Methods

fmap :: (a -> b) -> Alt * f a -> Alt * f b Source

(<$) :: a -> Alt * f b -> Alt * f a Source

Applicative f => Applicative (Alt * f) Source 

Methods

pure :: a -> Alt * f a Source

(<*>) :: Alt * f (a -> b) -> Alt * f a -> Alt * f b Source

(*>) :: Alt * f a -> Alt * f b -> Alt * f b Source

(<*) :: Alt * f a -> Alt * f b -> Alt * f a Source

Generic1 (Alt * f) Source 

Associated Types

type Rep1 (Alt * f :: * -> *) :: * -> * Source

Methods

from1 :: Alt * f a -> Rep1 (Alt * f) a Source

to1 :: Rep1 (Alt * f) a -> Alt * f a Source

MonadPlus f => MonadPlus (Alt * f) Source 

Methods

mzero :: Alt * f a Source

mplus :: Alt * f a -> Alt * f a -> Alt * f a Source

Alternative f => Alternative (Alt * f) Source 

Methods

empty :: Alt * f a Source

(<|>) :: Alt * f a -> Alt * f a -> Alt * f a Source

some :: Alt * f a -> Alt * f [a] Source

many :: Alt * f a -> Alt * f [a] Source

Enum (f a) => Enum (Alt k f a) Source 

Methods

succ :: Alt k f a -> Alt k f a Source

pred :: Alt k f a -> Alt k f a Source

toEnum :: Int -> Alt k f a Source

fromEnum :: Alt k f a -> Int Source

enumFrom :: Alt k f a -> [Alt k f a] Source

enumFromThen :: Alt k f a -> Alt k f a -> [Alt k f a] Source

enumFromTo :: Alt k f a -> Alt k f a -> [Alt k f a] Source

enumFromThenTo :: Alt k f a -> Alt k f a -> Alt k f a -> [Alt k f a] Source

Eq (f a) => Eq (Alt k f a) Source 

Methods

(==) :: Alt k f a -> Alt k f a -> Bool

(/=) :: Alt k f a -> Alt k f a -> Bool

Num (f a) => Num (Alt k f a) Source 

Methods

(+) :: Alt k f a -> Alt k f a -> Alt k f a Source

(-) :: Alt k f a -> Alt k f a -> Alt k f a Source

(*) :: Alt k f a -> Alt k f a -> Alt k f a Source

negate :: Alt k f a -> Alt k f a Source

abs :: Alt k f a -> Alt k f a Source

signum :: Alt k f a -> Alt k f a Source

fromInteger :: Integer -> Alt k f a Source

Ord (f a) => Ord (Alt k f a) Source 

Methods

compare :: Alt k f a -> Alt k f a -> Ordering

(<) :: Alt k f a -> Alt k f a -> Bool

(<=) :: Alt k f a -> Alt k f a -> Bool

(>) :: Alt k f a -> Alt k f a -> Bool

(>=) :: Alt k f a -> Alt k f a -> Bool

max :: Alt k f a -> Alt k f a -> Alt k f a

min :: Alt k f a -> Alt k f a -> Alt k f a

Read (f a) => Read (Alt k f a) Source 
Show (f a) => Show (Alt k f a) Source 

Methods

showsPrec :: Int -> Alt k f a -> ShowS Source

show :: Alt k f a -> String Source

showList :: [Alt k f a] -> ShowS Source

Generic (Alt k f a) Source 

Associated Types

type Rep (Alt k f a) :: * -> * Source

Methods

from :: Alt k f a -> Rep (Alt k f a) x Source

to :: Rep (Alt k f a) x -> Alt k f a Source

Alternative f => Monoid (Alt * f a) Source 

Methods

mempty :: Alt * f a Source

mappend :: Alt * f a -> Alt * f a -> Alt * f a Source

mconcat :: [Alt * f a] -> Alt * f a Source

type Rep1 (Alt k f) Source 
type Rep (Alt k f a) Source