What is Disjoint Union, Sum Type?
What is Disjoint Union? (aka Tagged Union, Variant, Discriminated Union, Sum Type)
let's explain by a example.
suppose you have 2 sets:
- set 1,
{a,b}
- set 2,
{c,d,b}
They have a common element “b”.
The disjoint union of them is a new set, this:
{
(a,1),
(b,1),
(b,2),
(c,2),
(d,2),
}
Notice that each element is a pair, first part is the element from a set, second part is a index that tells us where this element came from. If a element came from multiple places, it is repeated, such as our b here, we have
(b,1)
and
(b,2)
.
So, what does it all mean?
As you can see, disjoint union, is somewhat like a union, but not really, because we have repeated things, and each new element has a added “index”. This “index”, serves the purpose of a “tag”, that's why it's also called “tagged union”
you can also see why it's called “sum type”, because the result is kinda a sum of the sets. Compare this to the “Cartesian Product” of set, nice contrast.