A semiring is like a double monoid. That is, it’s a set equipped with two binary associative operations, usually written
(*) on analogy with the canonical integer monoids addition and multiplication, such that:
(+)is a commutative binary operation with an identity element, e.g., 0;
(*)is a binary operation with an identity element, e.g., 1;
- multiplication by 0 (the identity of
(+)) annihilates the set.
A semiring is a really exciting algebra, though perhaps somewhat less useful for programming than monoids, to which semirings are closely related, are.
There is no
Semiring typeclass in Haskell’s
base library. There are a few in libraries, and some related languages, most notably PureScript, do have
Since semirings are not quite half rings, the way semigroups have exactly half the properties of groups. The main difference between a semiring as we have described it here and a ring is that a ring also requires each element of the set to have an additive inverse, and a semiring does not require that. Consequently, some people use the term rig instead of semiring to indicate what we have described, taking out the ‘n’ to indicate the lack of negatives. By analogy, there can also be a rng, then, which is a semiring that has inverses but not identities.