semigroups-0.12.1: Anything that associates

Portabilityportable
Stabilityprovisional
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellTrustworthy

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

Synopsis

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

sconcat :: NonEmpty a -> a

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 ByteString 
Semigroup ByteString 
Semigroup Text 
Semigroup Text 
Semigroup Any 
Semigroup All 
Semigroup IntSet 
Semigroup [a] 
(Hashable a, Eq a) => Semigroup (HashSet a) 
Semigroup a => Semigroup (Maybe a) 
Num a => Semigroup (Sum a) 
Num a => Semigroup (Product a) 
Semigroup (Endo a) 
Semigroup a => Semigroup (Dual a) 
Semigroup (Last a) 
Semigroup (First a) 
Semigroup (NonEmpty a) 
Semigroup (Seq a) 
Ord a => Semigroup (Set a) 
Semigroup (IntMap v) 
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) 
(Hashable k, Eq k) => Semigroup (HashMap k a) 
Semigroup a => Semigroup (Const 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 Min a

Constructors

Min 

Fields

getMin :: a
 

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

Constructors

Max 

Fields

getMax :: a
 

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

Use Option (First a) to get the behavior of First from Data.Monoid.

Constructors

First 

Fields

getFirst :: a
 

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

Use Option (Last a) to get the behavior of Last from Data.Monoid

Constructors

Last 

Fields

getLast :: a
 

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

Provide a Semigroup for an arbitrary Monoid.

Constructors

WrapMonoid 

Fields

unwrapMonoid :: m
 

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

Methods

mempty :: a

mappend :: a -> a -> a

mconcat :: [a] -> a

Instances

Monoid Ordering 
Monoid () 
Monoid ByteString 
Monoid ByteString 
Monoid Text 
Monoid Text 
Monoid Any 
Monoid All 
Monoid IntSet 
Monoid [a] 
(Hashable a, Eq a) => Monoid (HashSet 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) 
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) 
(Eq k, Hashable k) => Monoid (HashMap k v) 
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

Constructors

Dual 

Fields

getDual :: a
 

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

Constructors

Endo 

Fields

appEndo :: a -> a
 

Instances

newtype All

Constructors

All 

Fields

getAll :: Bool
 

Instances

Bounded All 
Eq All 
Ord All 
Read All 
Show All 
Monoid All 
Semigroup All 

newtype Any

Constructors

Any 

Fields

getAny :: Bool
 

Instances

Bounded Any 
Eq Any 
Ord Any 
Read Any 
Show Any 
Monoid Any 
Semigroup Any 

newtype Sum a

Constructors

Sum 

Fields

getSum :: a
 

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

Constructors

Product 

Fields

getProduct :: a
 

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

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 

Fields

getOption :: Maybe a
 

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) 

option :: b -> (a -> b) -> Option a -> b

Fold an Option case-wise, just like maybe.

Difference lists of a semigroup

diff :: Semigroup m => m -> Endo m

This lets you use a difference list of a Semigroup as a Monoid.

cycle1 :: Semigroup m => m -> m

A generalization of cycle to an arbitrary Semigroup. May fail to terminate for some values in some semigroups.