## Concept:

# Sum monoid

## Headline

A monoid leveraging addition for the associative operation

## Illustration

Number types may be completed into monoids in different ways. The sum monoid favors addition for the associative operation of the monoid. We illustrate the sum monoid in Language:Haskell on the grounds of the type class *Monoid* with the first two members needed for a minimal complete definition:

```
-- The type class Monoid
class Monoid a
where
mempty :: a -- neutral element
mappend :: a -> a -> a -- associative operation
mconcat :: [a] -> a -- fold
mconcat = foldr mappend mempty
```

The sum monoid relies on a designated type which essentially wraps a number type:

```
-- The type of the sum monoid
newtype Sum a = Sum { getSum :: a }
```

Here is the type-class instance for the sum monoid:

```
-- The Monoid instance for numbers under addition
instance Num a => Monoid (Sum a)
where
mempty = Sum 0
x `mappend` y = Sum (getSum x + getSum y)
```

For further illustration, we can reconstruct the standard *sum* function in a monoidal way. To this end, we first review the normal definition in terms of foldr:

```
-- A foldr-based definition of sum
sum' :: Num a => [a] -> a
sum' = foldr (+) 0
```

```
-- A monoidal definition of sum
sum'' :: Num a => [a] -> a
sum'' = getSum . mconcat . map Sum
```

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**