Re:Where does the name "algebraic data type" come from?
by marcosdumay on Wednesday July 31, 2024 @04:10PM
(#41122950)
> C is a union of A and B, aka a "sum type", so its cardinality is the sum of cardinalities of A and B
That's exactly what's wrong. The cardinality is len(A ∪ B) = len(A) + len(B) - len(A ∩ B).
Also, the Cartesian product is a tagged operation. It literally creates a result where you access the elements by index.
Re:Where does the name "algebraic data type" come from?
by nine_k on Wednesday July 31, 2024 @06:00PM
(#41123935)
We reasonably assume that A ∩ B = ∅ in all practical cases, such as `int | None` or `string | number `, etc.
Yes, a Cartesian product is tagged (each axis is), but it does not affect the fact that len(A * B) = len(A) * len(B), hence it's logical to use multiplication to denote "record types".
Re:Where does the name "algebraic data type" come from?
by marcosdumay on Wednesday July 31, 2024 @07:03PM
(#41124459)
That's not a good assumption, and even the original example is Bool + Bool; len(Bool ∪ Bool) = 2 * len(Bool) is trivially false.
About the Cartesian product, the tags create that rule. Untagged multiplication again has fewer elements.
C = A + B means that type C is a union of A and B, aka a "sum type", so its cardinality is the sum of cardinalities of A and B. In Typescript or Python terms, C = A | B, which I read that an element of type C can be either an element of A or an element of B.
C = A * B means that C is a Cartesian product of A and B, and its cardinality is the product of those of A and B. In Python terms, C = tuple[A, B]. Hence something like Point = {x: number, y: number, z: number} is not "3 * number", but number^3, because it's a point in the R^3 space.