| 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 |
Data.Semigroup
Contents
Description
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
- 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).
Instances should satisfy the associativity law:
Since: 4.9.0.0
Minimal complete definition
Methods
(<>) :: a -> a -> a infixr 6 Source #
An associative operation.
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 = or stimesIdempotentstimes =
respectively.stimesIdempotentMonoid
Instances
| Semigroup Ordering # | Since: 4.9.0.0 |
| Semigroup () # | Since: 4.9.0.0 |
| Semigroup Any # | Since: 4.9.0.0 |
| Semigroup All # | Since: 4.9.0.0 |
| Semigroup Lifetime # | Since: 4.10.0.0 |
| Semigroup Event # | Since: 4.10.0.0 |
| Semigroup Void # | Since: 4.9.0.0 |
| Semigroup [a] # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (Maybe a) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (IO a) # | Since: 4.10.0.0 |
| Semigroup (NonEmpty a) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (Down a) # | Since: 4.11.0.0 |
| Num a => Semigroup (Product a) # | Since: 4.9.0.0 |
| Num a => Semigroup (Sum a) # | Since: 4.9.0.0 |
| Semigroup (Endo a) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (Dual a) # | Since: 4.9.0.0 |
| Semigroup (Last a) # | Since: 4.9.0.0 |
| Semigroup (First a) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (Identity a) # | |
| Semigroup a => Semigroup (Option a) # | Since: 4.9.0.0 |
| Monoid m => Semigroup (WrappedMonoid m) # | Since: 4.9.0.0 |
| Semigroup (Last a) # | Since: 4.9.0.0 |
| Semigroup (First a) # | Since: 4.9.0.0 |
| Ord a => Semigroup (Max a) # | Since: 4.9.0.0 |
| Ord a => Semigroup (Min a) # | Since: 4.9.0.0 |
| Semigroup b => Semigroup (a -> b) # | Since: 4.9.0.0 |
| Semigroup (Either a b) # | Since: 4.9.0.0 |
| (Semigroup a, Semigroup b) => Semigroup (a, b) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (ST s a) # | Since: 4.11.0.0 |
| Semigroup (Proxy k s) # | Since: 4.9.0.0 |
| (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) # | Since: 4.9.0.0 |
| Alternative f => Semigroup (Alt * f a) # | Since: 4.9.0.0 |
| Semigroup a => Semigroup (Const k a b) # | |
| (Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) # | Since: 4.9.0.0 |
| (Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e) # | 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
Instances
| Monad Min # | Since: 4.9.0.0 |
| Functor Min # | Since: 4.9.0.0 |
| MonadFix Min # | Since: 4.9.0.0 |
| Applicative Min # | Since: 4.9.0.0 |
| Foldable Min # | Since: 4.9.0.0 |
| Traversable Min # | Since: 4.9.0.0 |
| Bounded a => Bounded (Min a) # | |
| Enum a => Enum (Min a) # | Since: 4.9.0.0 |
| Eq a => Eq (Min a) # | |
| Data a => Data (Min a) # | |
| Num a => Num (Min a) # | Since: 4.9.0.0 |
| Ord a => Ord (Min a) # | |
| Read a => Read (Min a) # | |
| Show a => Show (Min a) # | |
| Generic (Min a) # | |
| Ord a => Semigroup (Min a) # | Since: 4.9.0.0 |
| (Ord a, Bounded a) => Monoid (Min a) # | Since: 4.9.0.0 |
| Generic1 * Min # | |
| type Rep (Min a) # | |
| type Rep1 * Min # | |
Instances
| Monad Max # | Since: 4.9.0.0 |
| Functor Max # | Since: 4.9.0.0 |
| MonadFix Max # | Since: 4.9.0.0 |
| Applicative Max # | Since: 4.9.0.0 |
| Foldable Max # | Since: 4.9.0.0 |
| Traversable Max # | Since: 4.9.0.0 |
| Bounded a => Bounded (Max a) # | |
| Enum a => Enum (Max a) # | Since: 4.9.0.0 |
| Eq a => Eq (Max a) # | |
| Data a => Data (Max a) # | |
| Num a => Num (Max a) # | Since: 4.9.0.0 |
| Ord a => Ord (Max a) # | |
| Read a => Read (Max a) # | |
| Show a => Show (Max a) # | |
| Generic (Max a) # | |
| Ord a => Semigroup (Max a) # | Since: 4.9.0.0 |
| (Ord a, Bounded a) => Monoid (Max a) # | Since: 4.9.0.0 |
| Generic1 * Max # | |
| type Rep (Max a) # | |
| type Rep1 * Max # | |
Use to get the behavior of
Option (First a)First from Data.Monoid.
Instances
| Monad First # | Since: 4.9.0.0 |
| Functor First # | Since: 4.9.0.0 |
| MonadFix First # | Since: 4.9.0.0 |
| Applicative First # | Since: 4.9.0.0 |
| Foldable First # | Since: 4.9.0.0 |
| Traversable First # | Since: 4.9.0.0 |
| Bounded a => Bounded (First a) # | |
| Enum a => Enum (First a) # | Since: 4.9.0.0 |
| 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) # | Since: 4.9.0.0 |
| Generic1 * First # | |
| type Rep (First a) # | |
| type Rep1 * First # | |
Use to get the behavior of
Option (Last a)Last from Data.Monoid
Instances
| Monad Last # | Since: 4.9.0.0 |
| Functor Last # | Since: 4.9.0.0 |
| MonadFix Last # | Since: 4.9.0.0 |
| Applicative Last # | Since: 4.9.0.0 |
| Foldable Last # | Since: 4.9.0.0 |
| Traversable Last # | Since: 4.9.0.0 |
| Bounded a => Bounded (Last a) # | |
| Enum a => Enum (Last a) # | Since: 4.9.0.0 |
| 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) # | Since: 4.9.0.0 |
| Generic1 * Last # | |
| type Rep (Last a) # | |
| type Rep1 * Last # | |
newtype WrappedMonoid m Source #
Provide a Semigroup for an arbitrary Monoid.
NOTE: This is not needed anymore since Semigroup became a superclass of
Monoid in base-4.11 and this newtype be deprecated at some point in the future.
Constructors
| WrapMonoid | |
Fields
| |
Instances
| Bounded m => Bounded (WrappedMonoid m) # | |
| Enum a => Enum (WrappedMonoid a) # | Since: 4.9.0.0 |
| 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) # | Since: 4.9.0.0 |
| Monoid m => Monoid (WrappedMonoid m) # | Since: 4.9.0.0 |
| Generic1 * WrappedMonoid # | |
| type Rep (WrappedMonoid m) # | |
| type Rep1 * WrappedMonoid # | |
Re-exported monoids from Data.Monoid
The dual of a Monoid, obtained by swapping the arguments of mappend.
>>>getDual (mappend (Dual "Hello") (Dual "World"))"WorldHello"
Instances
| Monad Dual # | Since: 4.8.0.0 |
| Functor Dual # | Since: 4.8.0.0 |
| MonadFix Dual # | Since: 4.8.0.0 |
| Applicative Dual # | Since: 4.8.0.0 |
| Foldable Dual # | Since: 4.8.0.0 |
| Traversable Dual # | Since: 4.8.0.0 |
| MonadZip Dual # | Since: 4.8.0.0 |
| Bounded a => Bounded (Dual a) # | |
| Eq a => Eq (Dual a) # | |
| Data a => Data (Dual a) # | Since: 4.8.0.0 |
| Ord a => Ord (Dual a) # | |
| Read a => Read (Dual a) # | |
| Show a => Show (Dual a) # | |
| Generic (Dual a) # | |
| Semigroup a => Semigroup (Dual a) # | Since: 4.9.0.0 |
| Monoid a => Monoid (Dual a) # | Since: 2.1 |
| Generic1 * Dual # | |
| type Rep (Dual a) # | |
| type Rep1 * Dual # | |
The monoid of endomorphisms under composition.
>>>let computation = Endo ("Hello, " ++) <> Endo (++ "!")>>>appEndo computation "Haskell""Hello, Haskell!"
Boolean monoid under conjunction (&&).
>>>getAll (All True <> mempty <> All False)False
>>>getAll (mconcat (map (\x -> All (even x)) [2,4,6,7,8]))False
Boolean monoid under disjunction (||).
>>>getAny (Any True <> mempty <> Any False)True
>>>getAny (mconcat (map (\x -> Any (even x)) [2,4,6,7,8]))True
Monoid under addition.
>>>getSum (Sum 1 <> Sum 2 <> mempty)3
Instances
| Monad Sum # | Since: 4.8.0.0 |
| Functor Sum # | Since: 4.8.0.0 |
| MonadFix Sum # | Since: 4.8.0.0 |
| Applicative Sum # | Since: 4.8.0.0 |
| Foldable Sum # | Since: 4.8.0.0 |
| Traversable Sum # | Since: 4.8.0.0 |
| MonadZip Sum # | Since: 4.8.0.0 |
| Bounded a => Bounded (Sum a) # | |
| Eq a => Eq (Sum a) # | |
| Data a => Data (Sum a) # | Since: 4.8.0.0 |
| 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) # | Since: 4.9.0.0 |
| Num a => Monoid (Sum a) # | Since: 2.1 |
| Generic1 * Sum # | |
| type Rep (Sum a) # | |
| type Rep1 * Sum # | |
Monoid under multiplication.
>>>getProduct (Product 3 <> Product 4 <> mempty)12
Constructors
| Product | |
Fields
| |
Instances
| Monad Product # | Since: 4.8.0.0 |
| Functor Product # | Since: 4.8.0.0 |
| MonadFix Product # | Since: 4.8.0.0 |
| Applicative Product # | Since: 4.8.0.0 |
| Foldable Product # | Since: 4.8.0.0 |
| Traversable Product # | Since: 4.8.0.0 |
| MonadZip Product # | Since: 4.8.0.0 |
| Bounded a => Bounded (Product a) # | |
| Eq a => Eq (Product a) # | |
| Data a => Data (Product a) # | Since: 4.8.0.0 |
| 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) # | Since: 4.9.0.0 |
| Num a => Monoid (Product a) # | Since: 2.1 |
| Generic1 * Product # | |
| type Rep (Product a) # | |
| type Rep1 * Product # | |
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
Instances
| Monad Option # | Since: 4.9.0.0 |
| Functor Option # | Since: 4.9.0.0 |
| MonadFix Option # | Since: 4.9.0.0 |
| Applicative Option # | Since: 4.9.0.0 |
| Foldable Option # | Since: 4.9.0.0 |
| Traversable Option # | Since: 4.9.0.0 |
| MonadPlus Option # | Since: 4.9.0.0 |
| Alternative Option # | Since: 4.9.0.0 |
| 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) # | Since: 4.9.0.0 |
| Semigroup a => Monoid (Option a) # | Since: 4.9.0.0 |
| Generic1 * Option # | |
| type Rep (Option a) # | |
| type Rep1 * Option # | |
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.
Constructors
| Arg a b |
Instances
| Bifunctor Arg # | Since: 4.9.0.0 |
| Bifoldable Arg # | Since: 4.10.0.0 |
| Bitraversable Arg # | Since: 4.10.0.0 |
| Functor (Arg a) # | Since: 4.9.0.0 |
| Foldable (Arg a) # | Since: 4.9.0.0 |
| Traversable (Arg a) # | Since: 4.9.0.0 |
| Generic1 * (Arg a) # | |
| Eq a => Eq (Arg a b) # | Since: 4.9.0.0 |
| (Data b, Data a) => Data (Arg a b) # | |
| Ord a => Ord (Arg a b) # | Since: 4.9.0.0 |
| (Read b, Read a) => Read (Arg a b) # | |
| (Show b, Show a) => Show (Arg a b) # | |
| Generic (Arg a b) # | |
| type Rep1 * (Arg a) # | |
| type Rep (Arg a b) # | |