Copyright | (C) 2011-2015 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | libraries@haskell.org |
Stability | provisional |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell2010 |
In mathematics, a semigroup is an algebraic structure consisting of a set together with an associative binary operation. A semigroup generalizes a monoid in that there might not exist an identity element. It also (originally) generalized a group (a monoid with all inverses) to a type where every element did not have to have an inverse, thus the name semigroup.
The use of (<>)
in this module conflicts with an operator with the same
name that is being exported by Data.Monoid. However, this package
re-exports (most of) the contents of Data.Monoid, so to use semigroups
and monoids in the same package just
import Data.Semigroup
Since: 4.9.0.0
- class Semigroup a where
- stimesMonoid :: (Integral b, Monoid a) => b -> a -> a
- stimesIdempotent :: Integral b => b -> a -> a
- stimesIdempotentMonoid :: (Integral b, Monoid a) => b -> a -> a
- mtimesDefault :: (Integral b, Monoid a) => b -> a -> a
- newtype Min a = Min {
- getMin :: a
- newtype Max a = Max {
- getMax :: a
- newtype First a = First {
- getFirst :: a
- newtype Last a = Last {
- getLast :: a
- newtype WrappedMonoid m = WrapMonoid {
- unwrapMonoid :: m
- class Monoid a where
- newtype Dual a = Dual {
- getDual :: a
- newtype Endo a = Endo {
- appEndo :: a -> a
- newtype All = All {}
- newtype Any = Any {}
- newtype Sum a = Sum {
- getSum :: a
- newtype Product a = Product {
- getProduct :: a
- newtype Option a = Option {}
- option :: b -> (a -> b) -> Option a -> b
- diff :: Semigroup m => m -> Endo m
- cycle1 :: Semigroup m => m -> m
- data Arg a b = Arg a b
- type ArgMin a b = Min (Arg a b)
- type ArgMax a b = Max (Arg a b)
Documentation
class Semigroup a where Source
The class of semigroups (types with an associative binary operation).
Since: 4.9.0.0
(<>) :: a -> a -> a infixr 6 Source
An associative operation.
(a<>
b)<>
c = a<>
(b<>
c)
If a
is also a Monoid
we further require
(<>
) =mappend
(<>) :: Monoid a => a -> a -> a infixr 6 Source
An associative operation.
(a<>
b)<>
c = a<>
(b<>
c)
If a
is also a Monoid
we further require
(<>
) =mappend
sconcat :: NonEmpty a -> a Source
Reduce a non-empty list with <>
The default definition should be sufficient, but this can be overridden for efficiency.
stimes :: Integral b => b -> a -> a Source
Repeat a value n
times.
Given that this works on a Semigroup
it is allowed to fail if
you request 0 or fewer repetitions, and the default definition
will do so.
By making this a member of the class, idempotent semigroups and monoids can
upgrade this to execute in O(1) by picking
stimes = stimesIdempotent
or stimes = stimesIdempotentMonoid
respectively.
Semigroup Ordering | |
Semigroup () | |
Semigroup Any | |
Semigroup All | |
Semigroup Void | |
Semigroup [a] | |
Semigroup a => Semigroup (Maybe a) | |
Semigroup (Last a) | |
Semigroup (First a) | |
Num a => Semigroup (Product a) | |
Num a => Semigroup (Sum a) | |
Semigroup (Endo a) | |
Semigroup a => Semigroup (Dual a) | |
Semigroup (NonEmpty a) | |
Semigroup a => Semigroup (Option a) | |
Monoid m => Semigroup (WrappedMonoid m) | |
Semigroup (Last a) | |
Semigroup (First a) | |
Ord a => Semigroup (Max a) | |
Ord a => Semigroup (Min a) | |
Semigroup a => Semigroup (Identity a) | |
Semigroup b => Semigroup (a -> b) | |
Semigroup (Either a b) | |
(Semigroup a, Semigroup b) => Semigroup (a, b) | |
Semigroup (Proxy k s) | |
(Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) | |
Alternative f => Semigroup (Alt (TYPE Lifted) f a) | |
Semigroup a => Semigroup (Const k a b) | |
(Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) | |
(Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e) | |
stimesMonoid :: (Integral b, Monoid a) => b -> a -> a Source
stimesIdempotent :: Integral b => b -> a -> a Source
stimesIdempotentMonoid :: (Integral b, Monoid a) => b -> a -> a Source
mtimesDefault :: (Integral b, Monoid a) => b -> a -> a Source
Semigroups
Monad Min | |
Functor Min | |
MonadFix Min | |
Applicative Min | |
Foldable Min | |
Traversable Min | |
Generic1 Min | |
Bounded a => Bounded (Min a) | |
Enum a => Enum (Min a) | |
Eq a => Eq (Min a) | |
Data a => Data (Min a) | |
Num a => Num (Min a) | |
Ord a => Ord (Min a) | |
Read a => Read (Min a) | |
Show a => Show (Min a) | |
Generic (Min a) | |
Ord a => Semigroup (Min a) | |
(Ord a, Bounded a) => Monoid (Min a) | |
type Rep1 Min = D1 (MetaData "Min" "Data.Semigroup" "base" True) (C1 (MetaCons "Min" PrefixI True) (S1 (MetaSel (Just Symbol "getMin") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Min a) = D1 (MetaData "Min" "Data.Semigroup" "base" True) (C1 (MetaCons "Min" PrefixI True) (S1 (MetaSel (Just Symbol "getMin") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
Monad Max | |
Functor Max | |
MonadFix Max | |
Applicative Max | |
Foldable Max | |
Traversable Max | |
Generic1 Max | |
Bounded a => Bounded (Max a) | |
Enum a => Enum (Max a) | |
Eq a => Eq (Max a) | |
Data a => Data (Max a) | |
Num a => Num (Max a) | |
Ord a => Ord (Max a) | |
Read a => Read (Max a) | |
Show a => Show (Max a) | |
Generic (Max a) | |
Ord a => Semigroup (Max a) | |
(Ord a, Bounded a) => Monoid (Max a) | |
type Rep1 Max = D1 (MetaData "Max" "Data.Semigroup" "base" True) (C1 (MetaCons "Max" PrefixI True) (S1 (MetaSel (Just Symbol "getMax") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Max a) = D1 (MetaData "Max" "Data.Semigroup" "base" True) (C1 (MetaCons "Max" PrefixI True) (S1 (MetaSel (Just Symbol "getMax") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
Use
to get the behavior of
Option
(First
a)First
from Data.Monoid.
Monad First | |
Functor First | |
MonadFix First | |
Applicative First | |
Foldable First | |
Traversable First | |
Generic1 First | |
Bounded a => Bounded (First a) | |
Enum a => Enum (First a) | |
Eq a => Eq (First a) | |
Data a => Data (First a) | |
Ord a => Ord (First a) | |
Read a => Read (First a) | |
Show a => Show (First a) | |
Generic (First a) | |
Semigroup (First a) | |
type Rep1 First = D1 (MetaData "First" "Data.Semigroup" "base" True) (C1 (MetaCons "First" PrefixI True) (S1 (MetaSel (Just Symbol "getFirst") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (First a) = D1 (MetaData "First" "Data.Semigroup" "base" True) (C1 (MetaCons "First" PrefixI True) (S1 (MetaSel (Just Symbol "getFirst") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
Use
to get the behavior of
Option
(Last
a)Last
from Data.Monoid
Monad Last | |
Functor Last | |
MonadFix Last | |
Applicative Last | |
Foldable Last | |
Traversable Last | |
Generic1 Last | |
Bounded a => Bounded (Last a) | |
Enum a => Enum (Last a) | |
Eq a => Eq (Last a) | |
Data a => Data (Last a) | |
Ord a => Ord (Last a) | |
Read a => Read (Last a) | |
Show a => Show (Last a) | |
Generic (Last a) | |
Semigroup (Last a) | |
type Rep1 Last = D1 (MetaData "Last" "Data.Semigroup" "base" True) (C1 (MetaCons "Last" PrefixI True) (S1 (MetaSel (Just Symbol "getLast") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Last a) = D1 (MetaData "Last" "Data.Semigroup" "base" True) (C1 (MetaCons "Last" PrefixI True) (S1 (MetaSel (Just Symbol "getLast") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
newtype WrappedMonoid m Source
Provide a Semigroup for an arbitrary Monoid.
WrapMonoid | |
|
Generic1 WrappedMonoid | |
Bounded a => Bounded (WrappedMonoid a) | |
Enum a => Enum (WrappedMonoid a) | |
Eq m => Eq (WrappedMonoid m) | |
Data m => Data (WrappedMonoid m) | |
Ord m => Ord (WrappedMonoid m) | |
Read m => Read (WrappedMonoid m) | |
Show m => Show (WrappedMonoid m) | |
Generic (WrappedMonoid m) | |
Monoid m => Semigroup (WrappedMonoid m) | |
Monoid m => Monoid (WrappedMonoid m) | |
type Rep1 WrappedMonoid = D1 (MetaData "WrappedMonoid" "Data.Semigroup" "base" True) (C1 (MetaCons "WrapMonoid" PrefixI True) (S1 (MetaSel (Just Symbol "unwrapMonoid") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (WrappedMonoid m) = D1 (MetaData "WrappedMonoid" "Data.Semigroup" "base" True) (C1 (MetaCons "WrapMonoid" PrefixI True) (S1 (MetaSel (Just Symbol "unwrapMonoid") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 m))) |
Re-exported monoids from Data.Monoid
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 newtype
s and make those instances
of Monoid
, e.g. Sum
and Product
.
Identity of mappend
An associative operation
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.
Monoid Ordering | |
Monoid () | |
Monoid Any | |
Monoid All | |
Monoid Lifetime |
|
Monoid Event | |
Monoid [a] | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
Monoid a => Monoid (IO a) | |
Monoid (Last a) | |
Monoid (First a) | |
Num a => Monoid (Product a) | |
Num a => Monoid (Sum a) | |
Monoid (Endo a) | |
Monoid a => Monoid (Dual a) | |
Semigroup a => Monoid (Option a) | |
Monoid m => Monoid (WrappedMonoid m) | |
(Ord a, Bounded a) => Monoid (Max a) | |
(Ord a, Bounded a) => Monoid (Min a) | |
Monoid a => Monoid (Identity a) | |
Monoid b => Monoid (a -> b) | |
(Monoid a, Monoid b) => Monoid (a, b) | |
Monoid (Proxy k s) | |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) | |
Alternative f => Monoid (Alt (TYPE Lifted) f a) | |
Monoid a => Monoid (Const k a b) | |
(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) | |
Monad Dual | |
Functor Dual | |
MonadFix Dual | |
Applicative Dual | |
Foldable Dual | |
Traversable Dual | |
Generic1 Dual | |
MonadZip Dual | |
Bounded a => Bounded (Dual a) | |
Eq a => Eq (Dual a) | |
Data a => Data (Dual a) | |
Ord a => Ord (Dual a) | |
Read a => Read (Dual a) | |
Show a => Show (Dual a) | |
Generic (Dual a) | |
Semigroup a => Semigroup (Dual a) | |
Monoid a => Monoid (Dual a) | |
type Rep1 Dual = D1 (MetaData "Dual" "Data.Monoid" "base" True) (C1 (MetaCons "Dual" PrefixI True) (S1 (MetaSel (Just Symbol "getDual") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Dual a) = D1 (MetaData "Dual" "Data.Monoid" "base" True) (C1 (MetaCons "Dual" PrefixI True) (S1 (MetaSel (Just Symbol "getDual") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
The monoid of endomorphisms under composition.
Boolean monoid under conjunction (&&
).
Boolean monoid under disjunction (||
).
Monoid under addition.
Monad Sum | |
Functor Sum | |
MonadFix Sum | |
Applicative Sum | |
Foldable Sum | |
Traversable Sum | |
Generic1 Sum | |
MonadZip Sum | |
Bounded a => Bounded (Sum a) | |
Eq a => Eq (Sum a) | |
Data a => Data (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 => Semigroup (Sum a) | |
Num a => Monoid (Sum a) | |
type Rep1 Sum = D1 (MetaData "Sum" "Data.Monoid" "base" True) (C1 (MetaCons "Sum" PrefixI True) (S1 (MetaSel (Just Symbol "getSum") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Sum a) = D1 (MetaData "Sum" "Data.Monoid" "base" True) (C1 (MetaCons "Sum" PrefixI True) (S1 (MetaSel (Just Symbol "getSum") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
Monoid under multiplication.
Product | |
|
Monad Product | |
Functor Product | |
MonadFix Product | |
Applicative Product | |
Foldable Product | |
Traversable Product | |
Generic1 Product | |
MonadZip Product | |
Bounded a => Bounded (Product a) | |
Eq a => Eq (Product a) | |
Data a => Data (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 => Semigroup (Product a) | |
Num a => Monoid (Product a) | |
type Rep1 Product = D1 (MetaData "Product" "Data.Monoid" "base" True) (C1 (MetaCons "Product" PrefixI True) (S1 (MetaSel (Just Symbol "getProduct") NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1)) | |
type Rep (Product a) = D1 (MetaData "Product" "Data.Monoid" "base" True) (C1 (MetaCons "Product" PrefixI True) (S1 (MetaSel (Just Symbol "getProduct") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a))) |
A better monoid for Maybe
Option
is effectively Maybe
with a better instance of
Monoid
, built off of an underlying Semigroup
instead of an
underlying Monoid
.
Ideally, this type would not exist at all and we would just fix the
Monoid
instance of Maybe
Monad Option | |
Functor Option | |
MonadFix Option | |
Applicative Option | |
Foldable Option | |
Traversable Option | |
Generic1 Option | |
MonadPlus Option | |
Alternative Option | |
Eq a => Eq (Option a) | |
Data a => Data (Option a) | |
Ord a => Ord (Option a) | |
Read a => Read (Option a) | |
Show a => Show (Option a) | |
Generic (Option a) | |
Semigroup a => Semigroup (Option a) | |
Semigroup a => Monoid (Option a) | |
type Rep1 Option = D1 (MetaData "Option" "Data.Semigroup" "base" True) (C1 (MetaCons "Option" PrefixI True) (S1 (MetaSel (Just Symbol "getOption") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec1 Maybe))) | |
type Rep (Option a) = D1 (MetaData "Option" "Data.Semigroup" "base" True) (C1 (MetaCons "Option" PrefixI True) (S1 (MetaSel (Just Symbol "getOption") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Maybe a)))) |
Difference lists of a semigroup
ArgMin, ArgMax
Arg
isn't itself a Semigroup
in its own right, but it can be
placed inside Min
and Max
to compute an arg min or arg max.
Arg a b |
Bifunctor Arg | |
Functor (Arg a) | |
Foldable (Arg a) | |
Traversable (Arg a) | |
Generic1 (Arg a) | |
Eq a => Eq (Arg a b) | |
(Data a, Data b) => Data (Arg a b) | |
Ord a => Ord (Arg a b) | |
(Read a, Read b) => Read (Arg a b) | |
(Show a, Show b) => Show (Arg a b) | |
Generic (Arg a b) | |
type Rep1 (Arg a) = D1 (MetaData "Arg" "Data.Semigroup" "base" False) (C1 (MetaCons "Arg" PrefixI False) ((:*:) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) Par1))) | |
type Rep (Arg a b) = D1 (MetaData "Arg" "Data.Semigroup" "base" False) (C1 (MetaCons "Arg" PrefixI False) ((:*:) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a)) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 b)))) |