**Welcome to Software Development on Codidact!**

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

# Why is the `Data.Int` type not a `Semigroup` in PureScript but `String` is?

```
> "lo" <> "fa"
"lofa"
> 1 <> 2
Error found:
in module $PSCI
at :1:1 - 1:7 (line 1, column 1 - line 1, column 7)
No type class instance was found for
Data.Semigroup.Semigroup Int
```

Is this for purely technical reasons as there would have to be at least 2 `Semigroup`

instances (for addition and multiplication), but type classes cannot be implemented multiple times for the same type?

Even if it could be done using instance chaining, one would have to create an instance of the entire "stack" of algebraic abstractions (`Monoid`

, `Group`

, `Commutative`

) for each operation, right?

In contrast, `Semiring`

is conveniently defined as a set with 2 binary operations^{1}, which fits the bill perfectly:

A semiring (

`S`

,`+`

,`•`

) is a set`S`

with two binary compositions such that (`S`

,`+`

) and (`S`

,`•`

) are semigroups and distributes over`+`

.

^{source, pdf}

So, technically, `Int`

**is** a semigroup under addition (a commutative monoid even), but PureScript is not capable to "infer" that `Semigroup`

's `append`

(`<>`

) and `Semiring`

's `add`

(`+`

), in effect, do the same thing, but, for obvious reasons, cannot be used interchangeably.^{2}

Then again, I'm quite new to pure functional programming, PureScript, and abstract algebra, so my statements above may not make much sense - and thanks in advance for dispelling my confusion!

^{[1]: Although, nLab states that "mathematicians disagree on the definition of a semiring"...}

^{[2]: Are there programming languages where this level of granularity is achieved? (That is, provided if this is even a thing and if I didn't totally misinterpret these mathematical ideas.)}

## 1 answer

The following users marked this post as *Works for me*:

User | Comment | Date |
---|---|---|

toraritte |
Thread: Works for me Thank you! I totally missed |
May 27, 2024 at 21:53 |

Is this for purely technical reasons as there would have to be at least 2 Semigroup instances (for addition and multiplication), but type classes cannot be implemented multiple times for the same type?

Yes, basically this. When there is more than one plausible way to interpret a type as an instance of a class, and no reason to strongly favor one over the others, the typical thing to do is to create newtypes for each possible interpretation and define instances on the newtypes. That way the user can specify which instance to use by wrapping and unwrapping a particular newtype.

For `Int`

(or any other type with a `Semiring`

instance), you can use the `Additive`

and `Multiplicative`

types to get those two semigroups.

## 0 comment threads