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

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

Methods

mempty :: () Source

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

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

Methods

mempty :: Any Source

mappend :: Any -> Any -> Any Source

mconcat :: [Any] -> Any Source

Methods

mempty :: All Source

mappend :: All -> All -> All Source

mconcat :: [All] -> All Source

Monoid Lifetime Source

mappend == elSupremum

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

Methods

mempty :: Last a Source

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

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

Methods

mempty :: First a Source

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

mconcat :: [First a] -> First 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

Methods

mempty :: Endo a Source

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

mconcat :: [Endo a] -> Endo 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

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

Fields

Instances

Associated Types

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

Methods

from1 :: Dual a -> Rep1 Dual a Source

to1 :: Rep1 Dual a -> 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

Methods

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

show :: Dual a -> String Source

showList :: [Dual a] -> ShowS 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

Methods

mempty :: Dual a Source

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

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

type Rep (Dual a) Source

newtype Endo a Source

The monoid of endomorphisms under composition.

Constructors

Fields

Instances

Associated Types

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

Methods

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

to :: Rep (Endo a) x -> 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

Methods

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

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

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

Associated Types

type Rep All :: * -> * Source

Methods

from :: All -> Rep All x Source

to :: Rep All x -> All Source

Methods

mempty :: All Source

mappend :: All -> All -> All Source

mconcat :: [All] -> All Source

type Rep All Source

newtype Any Source

Boolean monoid under disjunction (|| ).

Constructors

Any

Fields

Instances

Methods

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

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

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

Associated Types

type Rep Any :: * -> * Source

Methods

from :: Any -> Rep Any x Source

to :: Rep Any x -> Any Source

Methods

mempty :: Any Source

mappend :: Any -> Any -> Any Source

mconcat :: [Any] -> Any Source

type Rep Any Source

Num wrappers

newtype Sum a Source

Monoid under addition.

Constructors

Sum

Fields

Instances

Associated Types

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

Methods

from1 :: Sum a -> Rep1 Sum a Source

to1 :: Rep1 Sum a -> Sum a Source

Methods

minBound :: Sum a Source

maxBound :: 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

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

Fields

Instances

Associated Types

type Rep1 (Product :: * -> *) :: * -> * 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

Associated Types

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

Methods

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

to :: Rep (Product a) x -> Product a 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

Fields

Instances

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

Methods

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

(<$) :: a -> First b -> First a 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

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

Methods

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

show :: First a -> String Source

showList :: [First a] -> ShowS 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

Methods

mempty :: First a Source

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

mconcat :: [First a] -> First a 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

Fields

Instances

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

Methods

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

(<$) :: a -> Last b -> Last a 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

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

Methods

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

show :: Last a -> String Source

showList :: [Last a] -> ShowS 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

Methods

mempty :: Last a Source

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

mconcat :: [Last a] -> Last a 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

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

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

Methods

mzero :: Alt * f a Source

mplus :: Alt * f a -> Alt * f a -> Alt * f a 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

Methods

readsPrec :: Int -> ReadS (Alt k f a) Source

readList :: ReadS [Alt k f a] Source

readPrec :: ReadPrec (Alt k f a) Source

readListPrec :: ReadPrec [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

AltStyle によって変換されたページ (->オリジナル) /