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. Data.Semigroup.Sum and Data.Semigroup.Product.
NOTE: Semigroup is a superclass of Monoid since base-4.11.0.0.
NOTE: This method is redundant and has the default
implementation mappend = (<>) since base-4.11.0.0.
Should it be implemented manually, since mappend is a synonym for
(<>), it is expected that the two functions are defined the same
way. In a future GHC release mappend will be removed from 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.
Monoidp => Monoid (Par1p)Defined in ghc-internal-9.1003.0 · GHC.Internal.Generics
Semigroupa => Monoid (Maybea)Defined in ghc-internal-9.1003.0 · GHC.Internal.Base
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 4.11.0: constraint on inner a value generalised from
Monoid to Semigroup.
Bitsa => Monoid (Iora)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
Bitsa => Monoid (Xora)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
FiniteBitsa => Monoid (Anda)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
This constraint is arguably too strong. However,
as some types (such as Natural) have undefined complement, this is the
only safe choice.
FiniteBitsa => Monoid (Iffa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
This constraint is arguably
too strong. However, as some types (such as Natural) have undefined
complement, this is the only safe choice.
Numa => Monoid (Producta)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
Numa => Monoid (Suma)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
Orda => Monoid (Seta)Defined in containers-0.7 · Data.Set.Internal
Orda => Monoid (Maxa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Utils
Orda => Monoid (Mina)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Utils
Data.Semigroup.Last. The former returns the last non-Nothing,
so x <> Data.Monoid.Last Nothing = x. The latter simply returns the last value,
thus x <> Data.Semigroup.Last Nothing = Data.Semigroup.Last Nothing.
Examples
Example1 expression
>>> Last (Just "hello") <> Last Nothing <> Last (Just "world")Last {getLast = Just "world"}
Example1 expression
>>> Last Nothing <> memptyLast {getLast = Nothing}
Maybe monoid returning the leftmost non-Nothing value.
First a is isomorphic to AltMaybe a, but precedes it
historically.
Beware that Data.Monoid.First is different from
Data.Semigroup.First. The former returns the first non-Nothing,
so Data.Monoid.First Nothing <> x = x. The latter simply returns the first value,
thus Data.Semigroup.First Nothing <> x = Data.Semigroup.First Nothing.
Examples
Example1 expression
>>> First (Just "hello") <> First Nothing <> First (Just "world")First {getFirst = Just "hello"}
Example1 expression
>>> First Nothing <> memptyFirst {getFirst = Nothing}
Enum (fa) => Enum (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
Eq (fa) => Eq (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
(Data (fa), Dataa, Typeablef) => Data (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
(Applicativef, Numa) => Num (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
Note that even if the underlying Num and Applicative instances are
lawful, for most Applicatives, this instance will not be lawful. If you use
this instance with the list Applicative, the following customary laws will
not hold:
Commutativity:
Example2 expressions
>>> Ap [10,20] + Ap [1,2]Ap {getAp = [11,12,21,22]}>>> Ap [1,2] + Ap [10,20]Ap {getAp = [11,21,12,22]}
Additive inverse:
Example2 expressions
>>> Ap [] + negate (Ap [])Ap {getAp = []}>>> fromInteger 0 :: Ap [] IntAp {getAp = [0]}
Distributivity:
Example2 expressions
>>> Ap [1,2] * (3 + 4)Ap {getAp = [7,14]}>>> (Ap [1,2] * 3) + (Ap [1,2] * 4)Ap {getAp = [7,11,10,14]}
Ord (fa) => Ord (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
Read (fa) => Read (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
Show (fa) => Show (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid
Generic (Apfa)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid