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 Source # | Since: 4.9.0.0 |
Semigroup () Source # | Since: 4.9.0.0 |
Semigroup Any Source # | Since: 4.9.0.0 |
Semigroup All Source # | Since: 4.9.0.0 |
Semigroup Lifetime Source # | Since: 4.10.0.0 |
Semigroup Event Source # | Since: 4.10.0.0 |
Semigroup Void Source # | Since: 4.9.0.0 |
Semigroup [a] Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (Maybe a) Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (IO a) Source # | Since: 4.10.0.0 |
Semigroup (Last a) Source # | Since: 4.9.0.0 |
Semigroup (First a) Source # | Since: 4.9.0.0 |
Num a => Semigroup (Product a) Source # | Since: 4.9.0.0 |
Num a => Semigroup (Sum a) Source # | Since: 4.9.0.0 |
Semigroup (Endo a) Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (Dual a) Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (Identity a) Source # | Since: 4.9.0.0 |
Semigroup (NonEmpty a) Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (Option a) Source # | Since: 4.9.0.0 |
Monoid m => Semigroup (WrappedMonoid m) Source # | Since: 4.9.0.0 |
Semigroup (Last a) Source # | Since: 4.9.0.0 |
Semigroup (First a) Source # | Since: 4.9.0.0 |
Ord a => Semigroup (Max a) Source # | Since: 4.9.0.0 |
Ord a => Semigroup (Min a) Source # | Since: 4.9.0.0 |
Semigroup b => Semigroup (a -> b) Source # | Since: 4.9.0.0 |
Semigroup (Either a b) Source # | Since: 4.9.0.0 |
(Semigroup a, Semigroup b) => Semigroup (a, b) Source # | Since: 4.9.0.0 |
Semigroup (Proxy k s) Source # | Since: 4.9.0.0 |
(Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) Source # | Since: 4.9.0.0 |
Alternative f => Semigroup (Alt * f a) Source # | Since: 4.9.0.0 |
Semigroup a => Semigroup (Const k a b) Source # | Since: 4.9.0.0 |
(Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) Source # | Since: 4.9.0.0 |
(Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e) Source # | Since: 4.9.0.0 |
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 Source # | Since: 4.9.0.0 |
Functor Min Source # | Since: 4.9.0.0 |
MonadFix Min Source # | Since: 4.9.0.0 |
Applicative Min Source # | Since: 4.9.0.0 |
Foldable Min Source # | Since: 4.9.0.0 |
Traversable Min Source # | Since: 4.9.0.0 |
Bounded a => Bounded (Min a) Source # | |
Enum a => Enum (Min a) Source # | Since: 4.9.0.0 |
Eq a => Eq (Min a) Source # | |
Data a => Data (Min a) Source # | |
Num a => Num (Min a) Source # | Since: 4.9.0.0 |
Ord a => Ord (Min a) Source # | |
Read a => Read (Min a) Source # | |
Show a => Show (Min a) Source # | |
Generic (Min a) Source # | |
Ord a => Semigroup (Min a) Source # | Since: 4.9.0.0 |
(Ord a, Bounded a) => Monoid (Min a) Source # | Since: 4.9.0.0 |
Generic1 * Min Source # | |
type Rep (Min a) Source # | |
type Rep1 * Min Source # | |
Monad Max Source # | Since: 4.9.0.0 |
Functor Max Source # | Since: 4.9.0.0 |
MonadFix Max Source # | Since: 4.9.0.0 |
Applicative Max Source # | Since: 4.9.0.0 |
Foldable Max Source # | Since: 4.9.0.0 |
Traversable Max Source # | Since: 4.9.0.0 |
Bounded a => Bounded (Max a) Source # | |
Enum a => Enum (Max a) Source # | Since: 4.9.0.0 |
Eq a => Eq (Max a) Source # | |
Data a => Data (Max a) Source # | |
Num a => Num (Max a) Source # | Since: 4.9.0.0 |
Ord a => Ord (Max a) Source # | |
Read a => Read (Max a) Source # | |
Show a => Show (Max a) Source # | |
Generic (Max a) Source # | |
Ord a => Semigroup (Max a) Source # | Since: 4.9.0.0 |
(Ord a, Bounded a) => Monoid (Max a) Source # | Since: 4.9.0.0 |
Generic1 * Max Source # | |
type Rep (Max a) Source # | |
type Rep1 * Max Source # | |
Use
to get the behavior of
Option
(First
a)First
from Data.Monoid.
Monad First Source # | Since: 4.9.0.0 |
Functor First Source # | Since: 4.9.0.0 |
MonadFix First Source # | Since: 4.9.0.0 |
Applicative First Source # | Since: 4.9.0.0 |
Foldable First Source # | Since: 4.9.0.0 |
Traversable First Source # | Since: 4.9.0.0 |
Bounded a => Bounded (First a) Source # | |
Enum a => Enum (First a) Source # | Since: 4.9.0.0 |
Eq a => Eq (First a) Source # | |
Data a => Data (First a) Source # | |
Ord a => Ord (First a) Source # | |
Read a => Read (First a) Source # | |
Show a => Show (First a) Source # | |
Generic (First a) Source # | |
Semigroup (First a) Source # | Since: 4.9.0.0 |
Generic1 * First Source # | |
type Rep (First a) Source # | |
type Rep1 * First Source # | |
Use
to get the behavior of
Option
(Last
a)Last
from Data.Monoid
Monad Last Source # | Since: 4.9.0.0 |
Functor Last Source # | Since: 4.9.0.0 |
MonadFix Last Source # | Since: 4.9.0.0 |
Applicative Last Source # | Since: 4.9.0.0 |
Foldable Last Source # | Since: 4.9.0.0 |
Traversable Last Source # | Since: 4.9.0.0 |
Bounded a => Bounded (Last a) Source # | |
Enum a => Enum (Last a) Source # | Since: 4.9.0.0 |
Eq a => Eq (Last a) Source # | |
Data a => Data (Last a) Source # | |
Ord a => Ord (Last a) Source # | |
Read a => Read (Last a) Source # | |
Show a => Show (Last a) Source # | |
Generic (Last a) Source # | |
Semigroup (Last a) Source # | Since: 4.9.0.0 |
Generic1 * Last Source # | |
type Rep (Last a) Source # | |
type Rep1 * Last Source # | |
newtype WrappedMonoid m Source #
Provide a Semigroup for an arbitrary Monoid.
WrapMonoid | |
|
Bounded m => Bounded (WrappedMonoid m) Source # | |
Enum a => Enum (WrappedMonoid a) Source # | Since: 4.9.0.0 |
Eq m => Eq (WrappedMonoid m) Source # | |
Data m => Data (WrappedMonoid m) Source # | |
Ord m => Ord (WrappedMonoid m) Source # | |
Read m => Read (WrappedMonoid m) Source # | |
Show m => Show (WrappedMonoid m) Source # | |
Generic (WrappedMonoid m) Source # | |
Monoid m => Semigroup (WrappedMonoid m) Source # | Since: 4.9.0.0 |
Monoid m => Monoid (WrappedMonoid m) Source # | Since: 4.9.0.0 |
Generic1 * WrappedMonoid Source # | |
type Rep (WrappedMonoid m) Source # | |
type Rep1 * WrappedMonoid Source # | |
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
mappend :: a -> a -> a Source #
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 Source # | Since: 2.1 |
Monoid () Source # | Since: 2.1 |
Monoid Any Source # | Since: 2.1 |
Monoid All Source # | Since: 2.1 |
Monoid Lifetime Source # |
Since: 4.8.0.0 |
Monoid Event Source # | Since: 4.3.1.0 |
Monoid [a] Source # | Since: 2.1 |
Monoid a => Monoid (Maybe a) Source # | Lift a semigroup into Since: 2.1 |
Monoid a => Monoid (IO a) Source # | Since: 4.9.0.0 |
Monoid (Last a) Source # | Since: 2.1 |
Monoid (First a) Source # | Since: 2.1 |
Num a => Monoid (Product a) Source # | Since: 2.1 |
Num a => Monoid (Sum a) Source # | Since: 2.1 |
Monoid (Endo a) Source # | Since: 2.1 |
Monoid a => Monoid (Dual a) Source # | Since: 2.1 |
Monoid a => Monoid (Identity a) Source # | |
Semigroup a => Monoid (Option a) Source # | Since: 4.9.0.0 |
Monoid m => Monoid (WrappedMonoid m) Source # | Since: 4.9.0.0 |
(Ord a, Bounded a) => Monoid (Max a) Source # | Since: 4.9.0.0 |
(Ord a, Bounded a) => Monoid (Min a) Source # | Since: 4.9.0.0 |
Monoid b => Monoid (a -> b) Source # | Since: 2.1 |
(Monoid a, Monoid b) => Monoid (a, b) Source # | Since: 2.1 |
Monoid (Proxy k s) Source # | Since: 4.7.0.0 |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) Source # | Since: 2.1 |
Alternative f => Monoid (Alt * f a) Source # | Since: 4.8.0.0 |
Monoid a => Monoid (Const k a b) Source # | |
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) Source # | Since: 2.1 |
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) Source # | Since: 2.1 |
Monad Dual Source # | Since: 4.8.0.0 |
Functor Dual Source # | Since: 4.8.0.0 |
MonadFix Dual Source # | Since: 4.8.0.0 |
Applicative Dual Source # | Since: 4.8.0.0 |
Foldable Dual Source # | Since: 4.8.0.0 |
Traversable Dual Source # | Since: 4.8.0.0 |
MonadZip Dual Source # | Since: 4.8.0.0 |
Bounded a => Bounded (Dual a) Source # | |
Eq a => Eq (Dual a) Source # | |
Data a => Data (Dual a) Source # | Since: 4.8.0.0 |
Ord a => Ord (Dual a) Source # | |
Read a => Read (Dual a) Source # | |
Show a => Show (Dual a) Source # | |
Generic (Dual a) Source # | |
Semigroup a => Semigroup (Dual a) Source # | Since: 4.9.0.0 |
Monoid a => Monoid (Dual a) Source # | Since: 2.1 |
Generic1 * Dual Source # | |
type Rep (Dual a) Source # | |
type Rep1 * Dual Source # | |
The monoid of endomorphisms under composition.
Boolean monoid under conjunction (&&
).
Boolean monoid under disjunction (||
).
Monoid under addition.
Monad Sum Source # | Since: 4.8.0.0 |
Functor Sum Source # | Since: 4.8.0.0 |
MonadFix Sum Source # | Since: 4.8.0.0 |
Applicative Sum Source # | Since: 4.8.0.0 |
Foldable Sum Source # | Since: 4.8.0.0 |
Traversable Sum Source # | Since: 4.8.0.0 |
MonadZip Sum Source # | Since: 4.8.0.0 |
Bounded a => Bounded (Sum a) Source # | |
Eq a => Eq (Sum a) Source # | |
Data a => Data (Sum a) Source # | Since: 4.8.0.0 |
Num a => Num (Sum a) Source # | |
Ord a => Ord (Sum a) Source # | |
Read a => Read (Sum a) Source # | |
Show a => Show (Sum a) Source # | |
Generic (Sum a) Source # | |
Num a => Semigroup (Sum a) Source # | Since: 4.9.0.0 |
Num a => Monoid (Sum a) Source # | Since: 2.1 |
Generic1 * Sum Source # | |
type Rep (Sum a) Source # | |
type Rep1 * Sum Source # | |
Monoid under multiplication.
Product | |
|
Monad Product Source # | Since: 4.8.0.0 |
Functor Product Source # | Since: 4.8.0.0 |
MonadFix Product Source # | Since: 4.8.0.0 |
Applicative Product Source # | Since: 4.8.0.0 |
Foldable Product Source # | Since: 4.8.0.0 |
Traversable Product Source # | Since: 4.8.0.0 |
MonadZip Product Source # | Since: 4.8.0.0 |
Bounded a => Bounded (Product a) Source # | |
Eq a => Eq (Product a) Source # | |
Data a => Data (Product a) Source # | Since: 4.8.0.0 |
Num a => Num (Product a) Source # | |
Ord a => Ord (Product a) Source # | |
Read a => Read (Product a) Source # | |
Show a => Show (Product a) Source # | |
Generic (Product a) Source # | |
Num a => Semigroup (Product a) Source # | Since: 4.9.0.0 |
Num a => Monoid (Product a) Source # | Since: 2.1 |
Generic1 * Product Source # | |
type Rep (Product a) Source # | |
type Rep1 * Product Source # | |
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 Source # | Since: 4.9.0.0 |
Functor Option Source # | Since: 4.9.0.0 |
MonadFix Option Source # | Since: 4.9.0.0 |
Applicative Option Source # | Since: 4.9.0.0 |
Foldable Option Source # | Since: 4.9.0.0 |
Traversable Option Source # | Since: 4.9.0.0 |
MonadPlus Option Source # | Since: 4.9.0.0 |
Alternative Option Source # | Since: 4.9.0.0 |
Eq a => Eq (Option a) Source # | |
Data a => Data (Option a) Source # | |
Ord a => Ord (Option a) Source # | |
Read a => Read (Option a) Source # | |
Show a => Show (Option a) Source # | |
Generic (Option a) Source # | |
Semigroup a => Semigroup (Option a) Source # | Since: 4.9.0.0 |
Semigroup a => Monoid (Option a) Source # | Since: 4.9.0.0 |
Generic1 * Option Source # | |
type Rep (Option a) Source # | |
type Rep1 * Option Source # | |
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 Source # | Since: 4.9.0.0 |
Bifoldable Arg Source # | Since: 4.10.0.0 |
Bitraversable Arg Source # | Since: 4.10.0.0 |
Functor (Arg a) Source # | Since: 4.9.0.0 |
Foldable (Arg a) Source # | Since: 4.9.0.0 |
Traversable (Arg a) Source # | Since: 4.9.0.0 |
Generic1 * (Arg a) Source # | |
Eq a => Eq (Arg a b) Source # | Since: 4.9.0.0 |
(Data b, Data a) => Data (Arg a b) Source # | |
Ord a => Ord (Arg a b) Source # | Since: 4.9.0.0 |
(Read b, Read a) => Read (Arg a b) Source # | |
(Show b, Show a) => Show (Arg a b) Source # | |
Generic (Arg a b) Source # | |
type Rep1 * (Arg a) Source # | |
type Rep (Arg a b) Source # | |