semigroups-0.8: Haskell 98 semigroupsSource codeContentsIndex
Data.Semigroup
Portabilityportable
Stabilityprovisional
MaintainerEdward Kmett <ekmett@gmail.com>
Contents
Semigroups
Re-exported monoids from Data.Monoid
A better monoid for Maybe
Difference lists of a semigroup
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
Synopsis
class Semigroup a where
(<>) :: a -> a -> a
sconcat :: NonEmpty a -> a
times1p :: Whole n => n -> 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
mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> a
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 whereSource
Methods
(<>) :: a -> a -> aSource

An associative operation.

 (a <> b) <> c = a <> (b <> c)
sconcat :: NonEmpty a -> aSource

Reduce a non-empty list with <>

The default definition should be sufficient, but this can be overridden for efficiency.

times1p :: Whole n => n -> a -> aSource

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 <>.

show/hide Instances
Semigroups
newtype Min a Source
Constructors
Min
getMin :: a
show/hide Instances
Typeable1 Min
Bounded a => Bounded (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)
(Ord a, Bounded a) => Monoid (Min a)
Ord a => Semigroup (Min a)
newtype Max a Source
Constructors
Max
getMax :: a
show/hide Instances
Typeable1 Max
Bounded a => Bounded (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)
(Ord a, Bounded a) => Monoid (Max a)
Ord a => Semigroup (Max a)
newtype First a Source
Use Option (First a) -- to get the behavior of Data.Monoid.First
Constructors
First
getFirst :: a
show/hide Instances
Typeable1 First
Bounded a => Bounded (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)
Semigroup (First a)
newtype Last a Source
Use Option (Last a) -- to get the behavior of Data.Monoid.Last
Constructors
Last
getLast :: a
show/hide Instances
Typeable1 Last
Bounded a => Bounded (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)
Semigroup (Last a)
newtype WrappedMonoid m Source
Provide a Semigroup for an arbitrary Monoid.
Constructors
WrapMonoid
unwrapMonoid :: m
show/hide Instances
Re-exported monoids from Data.Monoid
class Monoid a whereSource

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 newtypes and make those instances of Monoid, e.g. Sum and Product.

Methods
mempty :: aSource
Identity of mappend
mappend :: a -> a -> aSource
An associative operation
mconcat :: [a] -> aSource
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.
show/hide Instances
Monoid Ordering
Monoid ()
Monoid All
Monoid Any
Monoid IntSet
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)
Monoid (IntMap a)
Ord a => Monoid (Set a)
Monoid (Seq 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)
(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 Source
The dual of a monoid, obtained by swapping the arguments of mappend.
Constructors
Dual
getDual :: a
show/hide Instances
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)
Monoid a => Monoid (Dual a)
Semigroup a => Semigroup (Dual a)
newtype Endo a Source
The monoid of endomorphisms under composition.
Constructors
Endo
appEndo :: a -> a
show/hide Instances
newtype All Source
Boolean monoid under conjunction.
Constructors
All
getAll :: Bool
show/hide Instances
newtype Any Source
Boolean monoid under disjunction.
Constructors
Any
getAny :: Bool
show/hide Instances
newtype Sum a Source
Monoid under addition.
Constructors
Sum
getSum :: a
show/hide Instances
Bounded a => Bounded (Sum a)
Eq a => Eq (Sum a)
Ord a => Ord (Sum a)
Read a => Read (Sum a)
Show a => Show (Sum a)
Num a => Monoid (Sum a)
Num a => Semigroup (Sum a)
newtype Product a Source
Monoid under multiplication.
Constructors
Product
getProduct :: a
show/hide Instances
Bounded a => Bounded (Product a)
Eq a => Eq (Product a)
Ord a => Ord (Product a)
Read a => Read (Product a)
Show a => Show (Product a)
Num a => Monoid (Product a)
Num a => Semigroup (Product a)
A better monoid for Maybe
newtype Option a Source
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
Constructors
Option
getOption :: Maybe a
show/hide Instances
option :: b -> (a -> b) -> Option a -> bSource
Difference lists of a semigroup
diff :: Semigroup m => m -> Endo mSource
This lets you use a difference list of a Semigroup as a Monoid.
cycle1 :: Semigroup m => m -> mSource
A generalization of Data.List.cycle to an arbitrary Semigroup. May fail to terminate for some values in some semigroups.
Produced by Haddock version 2.6.1