Copyright | (C) 2011 Edward Kmett |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | provisional |
Portability | portable |
Safe Haskell | Safe |
Language | Haskell98 |
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
- 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 :: (Whole n, Monoid a) => n -> 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
Methods
(<>) :: a -> a -> a infixr 6 #
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 #
An associative operation.
(a <> b) <> c = a <> (b <> c)
If a
is also a Monoid
we further require
(<>) = mappend
Reduce a non-empty list with <>
The default definition should be sufficient, but this can be overridden for efficiency.
times1p :: Whole n => n -> a -> a #
Repeat a value (n + 1) times.
times1p n a = a <> a <> ... <> a -- using <> n times
The default definition uses peasant multiplication, exploiting associativity to only
require O(log n) uses of <>
.
See also times
.
Instances
Semigroup Ordering # | |
Semigroup () # | |
Semigroup All # | |
Semigroup Any # | |
Semigroup IntSet # | |
Semigroup [a] # | |
Semigroup a => Semigroup (Maybe a) # | |
Semigroup a => Semigroup (Dual a) # | |
Semigroup (Endo a) # | |
Num a => Semigroup (Sum a) # | |
Num a => Semigroup (Product a) # | |
Semigroup (First a) # | |
Semigroup (Last a) # | |
Semigroup (IntMap v) # | |
Semigroup (Seq a) # | |
Ord a => Semigroup (Set 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 b => Semigroup (a -> b) # | |
Semigroup (Either a b) # | |
(Semigroup a, Semigroup b) => Semigroup (a, b) # | |
Ord k => Semigroup (Map k v) # | |
(Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) # | |
(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) # | |
Semigroups
newtype WrappedMonoid m #
Provide a Semigroup for an arbitrary Monoid.
Constructors
WrapMonoid | |
Fields
|
Instances
Bounded m => Bounded (WrappedMonoid m) # | |
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) # | |
Monoid m => Monoid (WrappedMonoid m) # | |
Monoid m => Semigroup (WrappedMonoid m) # | |
timesN :: (Whole n, Monoid a) => n -> a -> a #
Repeat a value n
times.
times n a = a <> a <> ... <> a -- using <> (n-1) times
Implemented using times1p
.
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
.
Methods
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.
Instances
Monoid Ordering | Since: 2.1 |
Monoid () | Since: 2.1 |
Monoid All | Since: 2.1 |
Monoid Any | Since: 2.1 |
Monoid IntSet | |
Monoid [a] | Since: 2.1 |
Monoid a => Monoid (Maybe a) | Lift a semigroup into Since: 2.1 |
Monoid a => Monoid (IO a) | Since: 4.9.0.0 |
Monoid a => Monoid (Identity a) | |
Monoid a => Monoid (Dual a) | Since: 2.1 |
Monoid (Endo a) | Since: 2.1 |
Num a => Monoid (Sum a) | Since: 2.1 |
Num a => Monoid (Product a) | Since: 2.1 |
Monoid (First a) | Since: 2.1 |
Monoid (Last a) | Since: 2.1 |
Monoid (IntMap a) | |
Monoid (Seq a) | |
Ord a => Monoid (Set 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) | Since: 2.1 |
(Monoid a, Monoid b) => Monoid (a, b) | Since: 2.1 |
Monoid (Proxy k s) | Since: 4.7.0.0 |
Ord k => Monoid (Map k v) | |
(Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) | Since: 2.1 |
Monoid a => Monoid (Const k a b) | |
Alternative f => Monoid (Alt * f a) | Since: 4.8.0.0 |
(Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) | Since: 2.1 |
(Monoid a, Monoid b, Monoid c, Monoid d, Monoid e) => Monoid (a, b, c, d, e) | Since: 2.1 |
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 |
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) | |
Monoid a => Monoid (Dual a) | Since: 2.1 |
Semigroup a => Semigroup (Dual a) # | |
Generic1 * Dual | |
type Rep (Dual a) | |
type Rep1 * Dual | |
The monoid of endomorphisms under composition.
Boolean monoid under conjunction (&&
).
Boolean monoid under disjunction (||
).
Monoid under addition.
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 |
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 => Monoid (Sum a) | Since: 2.1 |
Num a => Semigroup (Sum a) # | |
Generic1 * Sum | |
type Rep (Sum a) | |
type Rep1 * Sum | |
Monoid under multiplication.
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 |
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 => Monoid (Product a) | Since: 2.1 |
Num a => Semigroup (Product a) # | |
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
intance of Maybe
Instances
Monad Option # | |
Functor Option # | |
MonadFix Option # | |
Applicative Option # | |
Foldable Option # | |
Traversable Option # | |
Alternative Option # | |
MonadPlus 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) # | |
Semigroup a => Monoid (Option a) # | |
Semigroup a => Semigroup (Option a) # | |