Ever wondered why sum types are called sum types?
Or maybe you've always wondered why the <*> operator uses exactly these symbols?
And what do these things have to do with Semirings?
Read this article and find out!
Most of us know and use Monoids and Semigroups.
They're super useful and come with properties that we can directly utilize to gain a higher level of abstractions at very little cost (In case you don't know about them, check out the Cats documentation for some insight).
Sometimes, however, certain types can have multiple Monoid or Semigroup instances.
An easy example are the various numeric types where both multiplication and addition form two completely lawful monoid instances.
In abstract algebra there is a an algebraic class for types with two Monoid instances that interact in a certain way.
These are called Semirings (sometimes also Rig) and they are defined as two Monoids with some special laws that define the interactions between them.
Because they are often used to describe numeric data types we usually classify them as Additive and Multiplicative.
Just like with numeric types the laws of Semiring state that multiplication has to distribute over addition and multiplying a value with the additive identity (i.e. zero) absorbs the value and becomes zero.
There are different ways to encode this as type classes and different libraries handle this differently, but let's look at how the algebra project handles this.
Specifically, it defines a separate AdditiveSemigroup and MultiplicativeSemigroup and goes from there.
importsimulacrum._@typeclasstraitAdditiveSemigroup[A] {
def +(x: A)(y: A): A
}
@typeclasstraitAdditiveMonoid[A] extendsAdditiveSemigroup[A] {
defzero: A
}
@typeclasstraitMultiplicativeSemigroup[A] {
def *(x: A)(y: A): A
}
@typeclasstraitMultiplicativeMonoid[A] extendsMultiplicativeSemigroup[A] {
defone: A
}
A Semiring is then just an AdditiveMonoid coupled with a MultiplicativeMonoid with the following extra laws:
Additive commutativity, i.e. x + y === y + x
Right distributivity, i.e. (x + y) * z === (x * z) + (y * z)
Left distributivity, i.e. x * (y + z) === (x * y) + (x * z)
Right absorption, i.e. x * zero === zero
Left absorption, i.e. zero * x === zero
To define it as a type class, we simply extend from both additive and multiplicative monoid:
Now we have a Semiring class, that we can use with the various numeric types like Int, Long, BigDecimal etc, but what else is a Semiring and why dedicate a whole blog post to it?
It turns out a lot of interesting things can be Semirings, including Booleans, Sets and animations.
One very interesting thing I'd like to point out is that we can form a Semiring homomorphism from types to their number of possible inhabitants.
What the hell is that?
Well, bear with me for a while and I'll try to explain step by step.
Cardinality
Okay, so let's start with what I mean by cardinality.
Every type has a specific number of values it can possibly have, e.g. a Boolean has cardinality of 2, because it has two possible values: true and false.
So Boolean has two, how many do other primitive types have?
Byte has 2^8, Short has 2^16, Int has 2^32 and Long has 2^64.
So far so good, that makes sense, what about something like String?
String is an unbounded type and therefore theoretically has infinite number of different inhabitants (practically of course, we don't have infinite memory, so the actual number may vary depending on your system).
For what other types can we determine their cardinality?
Well a couple of easy ones are Unit, which has exactly one value it can take and also Nothing, which is the "bottom" type in Scala, which means being a subtype of every possible other type and has 0 possible values. I.e you can never instantiate a value of Nothing, which gives it a cardinality of 0.
That's neat, maybe we can encode this in actual code.
We could create a type class that should be able to give us the number of inhabitants for any type we give it:
Cool, but this is all very simple, what about things like ADTs?
Can we encode them in this way as well?
Turns out, we can, we just have to figure out how to handle the basic product and sum types.
To do so, let's look at an example of both types.
First, we'll look at a simple product type: (Boolean, Byte).
How many inhabitants does this type have?
Well, we know Boolean has 2 and Byte has 256.
So we have the numbers from -127 to 128 once with true and once again with false.
That gives us 512 unique instances.
Hmmm....
512 seems to be double 256, so maybe the simple solution is to just multiply the number of inhabitants of the first type with the number of inhabitants of the second type.
If you try this with other examples, you'll see that it's exactly true, awesome!
Let's encode that fact in a type class instance:
Great, now let's look at an example of a simple sum type: Either[Boolean, Byte].
Here the answer seems even more straight forward, since a value of this type can either be one or the other, we should just be able to add the number of inhabitants of one side with the number of inhabitants of the other side.
So Either[Boolean, Byte] should have 2 + 256 = 258 number of inhabitants. Cool!
Let's also code that up and try and confirm what we learned in the REPL:
So using sum types seem to add the number of inhabitants whereas product types seem to multiply the number of inhabitants.
That makes a lot of sense given their names!
So what about that homomorphism we talked about earlier?
Well, a homomorphism is a structure-preserving mapping function between two algebraic structures of the same sort (in this case a semiring).
This means that for any two values x and y and the homomorphism f, we get
1. f(x * y) === f(x) * f(y)
2. f(x + y) === f(x) + f(y)
Now this might seem fairly abstract, but it applies exactly to what we just did.
If we "add" two types of Byte and Boolean, we get an Either[Byte, Boolean] and if we apply the homomorphism function, number to it, we get the value 258.
This is the same as first calling number on Byte and then adding that to the result of calling number on Boolean.
And of course the same applies to multiplication and product types.
However, we're still missing something from a valid semiring, we only talked about multiplication and addition, but not about their respective identities.
What we did see, though is that Unit has exactly one inhabitant and Nothing has exactly zero.
So maybe we can use these two types to get a fully formed Semiring?
Let's try it out!
If Unit is one then a product type of any type with Unit should be equivalent to just the first type.
Turns out, it is, we can easily go from something like (Int, Unit) to Int and back without losing anything and the number of inhabitants also stay exactly the same.
Okay, not bad, but how about Nothing?
Given that it is the identity for addition, any type summed with Nothing should be equivalent to that type.
Is Either[Nothing, A] equivalent to A?
It is! Since Nothing doesn't have any values an Either[Nothing, A] can only be a Right and therefore only an A, so these are in fact equivalent types.
We also have to check for the absorption law that says that any value mutliplied with the additive identity zero should be equivalent to zero.
Since Nothing is our zero a product type like (Int, Nothing) should be equivalent to Nothing.
This also holds, given the fact that we can't construct a Nothing so we can never construct a tuple that expects a value of type Nothing either.
Let's see if this translates to the number of possible inhabitants as well:
Nice!
The only thing left now is distributivity.
In type form this means that (A, Either[B, C]) should be equal to Either[(A, B), (A, C)].
If we think about it, these two types should also be exactly equivalent, woohoo!
It seems to bear some similarity to Semigroup, we have two values which we somehow combine, and it also shares Semigroups associativity requirement.
So far so good, but the name product seems a bit weird.
It makes sense given we combine the A and the B in a tuple, which is a product type, but if we're using products, maybe this isn't a generic Semigroupal but actually a multiplicative one?
Let's fix this and rename it!
Next, let us have a look at what an additive Semigroupal might look like.
Surely, the only thing we'd have to change is going from a product type to a sum type:
Pretty interesting so far, can we top this and add identities to make Monoidals?
Surely we can! For addition this should again be Nothing and Unit for multiplication:
So now we have these fancy type classes, but how are they actually useful?
Well, I'm going to make the claim that these type classes already exist in cats today, just under different names.
Let's first look at the AdditiveMonoidal.
It is defined by two methods, nothing which returns an F[Nothing] and sum which takes an F[A] and an F[B] to create an F[Either[A, B]].
What type class in Cats could be similar?
First, we'll look at the sum function and try to find a counterpart for AdditiveSemigroupal.
Since we gave the lower kinded versions of these type classes symbolic operators, why don't we do the same thing for AdditiveSemigroupal?
Since it is additive it should probably contain a + somewhere and it should also show that it's inside some context.
Optimally it'd be something like [+], but that's not a valid identifier so let's try <+> instead!
Oh! The <+> function already exists in cats as an alias for combineK which can be found on SemigroupK, but it's sort of different, it takes two F[A]s and returns an F[A], not quite what we have here.
Or is it?
These two functions are actually the same, and we can define them in terms of one another as long as we have a functor:
So our AdditiveSemigroupal is equivalent to SemigroupK, so probably AdditiveMonoidal is equivalent to MonoidK, right?
Indeed, and we can show that quite easily.
MonoidK adds an empty function with the following definition:
defempty[A]: F[A]
This function uses a universal quantifier for A, which means that it works for any A, which then means that it cannot actually include any particular A and is therefore equivalent to F[Nothing] which is what we have for AdditiveMonoidal.
Excellent, so we found counterparts for the additive type classes, and we already now that MultiplicativeSemigroupal is equivalent to cats.Semigroupal.
So the only thing left to find out is the counterpart of MultiplicativeMonoidal.
I'm going to spoil the fun and make the claim that Applicative is that counterpart.
Applicative adds pure, which takes an A and returns an F[A].
MultiplicativeMonoidal adds unit, which takes no parameters and returns an F[Unit].
So how can we go from one to another?
Well the answer is again using a functor:
defunit: F[Unit]
defpure(a: A): F[A] = unit.map(_ => a)
Applicative uses a covariant functor, but in general we could use invariant and contravariant structures as well.
Applicative also uses <*> as an alias for using product together with map, which seems like further evidence that our intuition that its a multiplicative type class is correct.
So in cats right now we have <+> and <*>, is there also a type class that combines both similar to how Semiring combines + and *?
There is, it is called Alternative, it extends Applicative and MonoidK and if we were super consistent we'd call it a Semiringal:
Excellent, now we've got both Semiring and a higher kinded version of it.
Unfortunately the lower kinded version can't be found in Cats yet, but hopefully in a future version it'll be available as well.
If it were available, we could derive a Semiring for any Alternative the same we can derive a Monoid for any MonoidK or Applicative.
We could also lift any Semiring back into Alternative, by using Const, just like we can lift Monoids into Applicative using Const.
To end this blog post, we'll have a very quick look on how to do that.
Rings and Semirings are very interesting algebraic structures and even if we didn't know about them we've probably been using them for quite some time.
This blog post aimed to show how Applicative and MonoidK relate to Monoid and how algebraic data types form a semiring and how these algebraic structures are pervasive throughout Scala and other functional programming languages.
For me personally, realizing how all of this ties together and form some really satisfying symmetry was really mind blowing and I hope this blog post can give some good insight on recognizing these interesting similarities throughout Cats and other libraries based on different mathematical abstractions.
For further material on this topic, you can check out this talk.
Addendum
This article glossed over commutativity in the type class encodings.
Commutativity is very important law for semrings and the code should show that.
However, since this post already contained a lot of different type class definitions, adding extra commutative type class definitions that do nothing but add laws felt like it would distract from what is trying to be taught.
Moreover I focused on the cardinality of only the types we need, but for completeness' sake, we could also add instances of Cardinality for things like A => B , Option[A] or Ior[A, B].
These are:
1. Cardinality.of[A => B] === Cardinality.of[B].pow(Cardinality.of[A])
2. Cardinality.of[Option[A]] === Cardinality.of[A] + 1
3. Cardinality.of[Ior[A, B]] === Cardinality.of[A] + Cardinality.of[B] + Cardinality.of[A] * Cardinality.of[B]
Luka Jacobowitz Luka is a functional programmer in love with finding great abstractions to engineering problems. He’s also a maintainer of several typelevel projects and seeks to make learning of pure functional programming as easy as possible.