# Data.Semigroup

### From HaskellWiki

The

must obey a single law in order to become instances:
), and multiplication () satisfy this requirement. Therefore could be defined as or for instances of class . Division () however, would not be a candidate as it is not associative: is not equal to .
have to obey an additional rule:
is equivalent to a more strict instance of .
### 5.1 Sum numbers using

### 5.2 Exponents using

### 5.3 Test for any elements which are True in a non-empty list using

### 5.4 Test if all elements are True in a non-empty list using

**represents a set with an associative binary operation. This makes a semigroup a superset of monoids. Semigoups have no other restrictions, and are a very general typeclass.**Semigroup

See also Data.Monoid: a with an identity value.

Semigroup

## Contents |

## 1 Packages

- (base) Data.Semigroup

## 2 Syntax

class Semigroup a where (<>) :: a -> a -> a sconcat :: [[Data.List.Nonempty|Nonempty]] a -> a stimes :: Integral b => b -> a -> a

### 2.1 Minimal Complete Definition

(<>)

## 3 Description

Any datatype which has an associative binary operation will be able to become a member of the typeclass. An instance of automatically satisfies the requirements of a making a strict superset of . The typeclass however does not enforce it's instances to already be instances of

a

Semigroup

Monoid a

Semigroup

Semigroup

Monoid

Monoid

Semigroup

The is a particularly forgiving typeclass in it's requirements, and datatypes may have many instances of as long as they have functions which satisfy the requirements.

Semigroup

Semigroup

### 3.1 Semigroup Laws

In addition to the class requirements above, potential instances ofSemigroup

- The binary operation must be associative<>
(a <> b) <> c == a <> (b <> c)

- As long as you do not change the order of the arguments, you can insert parenthesis anywhere, and the result will be the same.

a + (b + c) == (a + b) + c

a * (b * c) == (a * b) * c

<>

+

*

Num a

div

8 `div` (4 `div` 2) == 8 `div` 2 == 4

(8 `div` 4) `div` 2 == 2 `div` 2 == 1

In essence, the function could do anything, as long as it doesn't matter where you put parenthesis.

<>

### 3.2 Rules for Monoids

Instances ofMonoid

(<>) == mappendThis is to ensure that the instance of

Monoid

Semigroup

## 4 Methods

(<>) :: a -> a -> a

- An associative binary operation.

sconcat :: [[Data.List.Nonempty|Nonempty]] a -> a

- Take a nonempty list of type and apply theaoperation to all of them to get a single result.<>

stimes :: Integral b => b -> a -> a

- Given a number and a value of typex, applyato the value<>times.x

## 5 Examples

### 5.1 Sum numbers using <>:

<>

Sum 3 <> Sum 4 -- with the type "Sum a", "<>" becomes "+" -- returns: Sum {getSum = 7} because (3 <> 4) == (3 + 4) == 7

### 5.2 Exponents using stimes:

stimes

stimes 4 (Product 3) -- with the type "Product a" "<>" becomes "*" -- returns: Product {getProduct = 81} -- This is because (3 <> 3 <> 3 <> 3) == (3 * 3 * 3 * 3) == 81 -- i.e. 3 multiplied by itself 4 times.

### 5.3 Test for any elements which are True in a non-empty list using sconcat:

sconcat

sconcat (Any True :| [Any False, Any True, Any False]) -- sconcat will apply "<>" to all of the members in a list -- returns: Any {getAny = True} -- If any elements in the list are True than the whole expression is True -- The type "Any" converts "<>" to "||" -- (True <> False <> True <> False) == (True || False || True || False) == True

### 5.4 Test if all elements are True in a non-empty list using sconcat:

sconcat

sconcat (All True :| [All False, All True, All False]) -- returns: All {getAll = False} -- If all elements in the list are True than the whole expression is True -- The type "All" converts "<>" to "&&" -- (True <> False <> True <> False) == (True && False && True && False) == True

## 6 See Also

- Data.Monoid: a special case of with an identity elementSemigroupmempty