| Portability | portable |
|---|---|
| Stability | provisional |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Safe Haskell | Trustworthy |
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 {
- getAll :: Bool
- newtype Any = Any {
- getAny :: Bool
- newtype Sum a = Sum {
- getSum :: a
- newtype Product a = Product {
- getProduct :: a
- newtype Option a = Option {
- getOption :: Maybe a
- option :: b -> (a -> b) -> Option a -> b
- diff :: Semigroup m => m -> Endo m
- cycle1 :: Semigroup m => m -> m
Documentation
class Semigroup a where
Methods
(<>) :: a -> a -> a
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
Semigroups
newtype Min a
newtype Max a
newtype First a
newtype Last a
newtype WrappedMonoid m
Provide a Semigroup for an arbitrary Monoid.
Constructors
| WrapMonoid | |
Fields
| |
Instances
| Typeable1 WrappedMonoid | |
| 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
class Monoid a where
Instances
| Monoid Ordering | |
| Monoid () | |
| Monoid Any | |
| Monoid All | |
| Monoid IntSet | |
| Monoid ByteString | |
| Monoid ByteString | |
| Monoid Text | |
| Monoid Text | |
| Monoid [a] | |
| Monoid a => Monoid (Maybe a) | |
| Num a => Monoid (Sum a) | |
| Num a => Monoid (Product a) | |
| Monoid (Endo a) | |
| Monoid a => Monoid (Dual a) | |
| Monoid (Last a) | |
| Monoid (First a) | |
| Monoid (Seq a) | |
| Ord a => Monoid (Set a) | |
| Monoid (IntMap 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) | |
| Ord k => Monoid (Map k v) | |
| (Eq k, Hashable k) => Monoid (HashMap k v) | |
| (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
newtype All
newtype Any
newtype Sum a
newtype Product a
Constructors
| Product | |
Fields
| |
A better monoid for Maybe
newtype Option a
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 | |
| Typeable1 Option | |
| MonadFix Option | |
| MonadPlus Option | |
| Applicative Option | |
| Foldable Option | |
| Traversable 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) | |
| Semigroup a => Monoid (Option a) | |
| Semigroup a => Semigroup (Option a) |