Copyright | (C) 2011-2014 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | provisional |
Portability | portable |
Safe Haskell | Trustworthy |
Language | Haskell98 |
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
- class Semigroup a where
- 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
- timesN :: Monoid a => Natural -> a -> a
- 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
Documentation
class Semigroup a where Source
Nothing
(<>) :: 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.
Semigroups
Monad Min | |
Functor Min | |
MonadFix Min | |
Applicative Min | |
Foldable Min | |
Traversable Min | |
Bounded a => Bounded (Min a) | |
Enum a => Enum (Min a) | |
Eq a => Eq (Min a) | |
Data a => Data (Min a) | |
Ord a => Ord (Min a) | |
Read a => Read (Min a) | |
Show a => Show (Min a) | |
Generic (Min a) | |
(Ord a, Bounded a) => Monoid (Min a) | |
NFData a => NFData (Min a) | |
Hashable a => Hashable (Min a) | |
Ord a => Semigroup (Min a) | |
Typeable (* -> *) Min | |
type Rep (Min a) |
Monad Max | |
Functor Max | |
MonadFix Max | |
Applicative Max | |
Foldable Max | |
Traversable Max | |
Bounded a => Bounded (Max a) | |
Enum a => Enum (Max a) | |
Eq a => Eq (Max a) | |
Data a => Data (Max a) | |
Ord a => Ord (Max a) | |
Read a => Read (Max a) | |
Show a => Show (Max a) | |
Generic (Max a) | |
(Ord a, Bounded a) => Monoid (Max a) | |
NFData a => NFData (Max a) | |
Hashable a => Hashable (Max a) | |
Ord a => Semigroup (Max a) | |
Typeable (* -> *) Max | |
type Rep (Max a) |
Monad First | |
Functor First | |
MonadFix First | |
Applicative First | |
Foldable First | |
Traversable 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) | |
NFData a => NFData (First a) | |
Hashable a => Hashable (First a) | |
Semigroup (First a) | |
Typeable (* -> *) First | |
type Rep (First a) |
Monad Last | |
Functor Last | |
MonadFix Last | |
Applicative Last | |
Foldable Last | |
Traversable 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) | |
NFData a => NFData (Last a) | |
Hashable a => Hashable (Last a) | |
Semigroup (Last a) | |
Typeable (* -> *) Last | |
type Rep (Last a) |
newtype WrappedMonoid m Source
Provide a Semigroup for an arbitrary Monoid.
WrapMonoid | |
|
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 => Monoid (WrappedMonoid m) | |
NFData m => NFData (WrappedMonoid m) | |
Hashable a => Hashable (WrappedMonoid a) | |
Monoid m => Semigroup (WrappedMonoid m) | |
Typeable (* -> *) WrappedMonoid | |
type Rep (WrappedMonoid m) |
timesN :: Monoid a => Natural -> a -> a Source
Repeat a value n
times.
timesN n a = a <> a <> ... <> a -- using <> (n-1) times
Implemented using times1p
.
Re-exported monoids from Data.Monoid
class Monoid a where
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.
Minimal complete definition: mempty
and mappend
.
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
.
mempty :: a
Identity of mappend
mappend :: a -> a -> a
An associative operation
mconcat :: [a] -> a
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 All | |
Monoid Any | |
Monoid ByteString | |
Monoid ByteString | |
Monoid IntSet | |
Monoid Text | |
Monoid Text | |
Monoid [a] | |
Monoid a => Monoid (Dual a) | |
Monoid (Endo a) | |
Num a => Monoid (Sum a) | |
Num a => Monoid (Product a) | |
Monoid (First a) | |
Monoid (Last a) | |
Monoid a => Monoid (Maybe a) | Lift a semigroup into |
Monoid (IntMap a) | |
Ord a => Monoid (Set a) | |
Monoid (Seq a) | |
(Hashable a, Eq a) => Monoid (HashSet 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 b => Monoid (a -> b) | |
(Monoid a, Monoid b) => Monoid (a, b) | |
Monoid a => Monoid (Const a b) | |
Monoid (Proxy * s) | |
Ord k => Monoid (Map k v) | |
(Eq k, Hashable k) => Monoid (HashMap k v) | |
Typeable (* -> Constraint) Monoid | |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) | |
(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) |
newtype Dual a :: * -> *
The dual of a monoid, obtained by swapping the arguments of mappend
.
Generic1 Dual | |
Bounded a => Bounded (Dual a) | |
Eq a => Eq (Dual a) | |
Ord a => Ord (Dual a) | |
Read a => Read (Dual a) | |
Show a => Show (Dual a) | |
Generic (Dual a) | |
Monoid a => Monoid (Dual a) | |
Semigroup a => Semigroup (Dual a) | |
type Rep1 Dual = D1 D1Dual (C1 C1_0Dual (S1 S1_0_0Dual Par1)) | |
type Rep (Dual a) = D1 D1Dual (C1 C1_0Dual (S1 S1_0_0Dual (Rec0 a))) |
newtype Endo a :: * -> *
The monoid of endomorphisms under composition.
newtype All :: *
Boolean monoid under conjunction.
newtype Any :: *
Boolean monoid under disjunction.
newtype Sum a :: * -> *
Monoid under addition.
Generic1 Sum | |
Bounded a => Bounded (Sum a) | |
Eq a => Eq (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 => Monoid (Sum a) | |
Num a => Semigroup (Sum a) | |
type Rep1 Sum = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum Par1)) | |
type Rep (Sum a) = D1 D1Sum (C1 C1_0Sum (S1 S1_0_0Sum (Rec0 a))) |
newtype Product a :: * -> *
Monoid under multiplication.
Product | |
|
Generic1 Product | |
Bounded a => Bounded (Product a) | |
Eq a => Eq (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 => Monoid (Product a) | |
Num a => Semigroup (Product a) | |
type Rep1 Product = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product Par1)) | |
type Rep (Product a) = D1 D1Product (C1 C1_0Product (S1 S1_0_0Product (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
Alternative Option | |
Monad Option | |
Functor Option | |
MonadFix Option | |
MonadPlus Option | |
Applicative Option | |
Foldable Option | |
Traversable 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 => Monoid (Option a) | |
NFData a => NFData (Option a) | |
Hashable a => Hashable (Option a) | |
Semigroup a => Semigroup (Option a) | |
Typeable (* -> *) Option | |
type Rep (Option a) |