Algebraic Data Types as Sums and Products

Model selectable variants and simultaneous fields separately, then use cardinality to check a small representation.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. A product stores all components
  2. Constructors define how a value is dismantled

A product stores all components

A pair of types \(A,B\) has one state for every choice of an \(A\) value together with a \(B\) value. Finite cardinalities multiply. A tagged sum chooses one constructor and its fields, so cardinalities add. If \(B\) has exactly two Boolean values, an optional \(B\) has \(1+2=3\) values. Encoding it as a pair of two unrestricted Booleans has four states and needs a validity rule or redundant encoding.

Constructors define how a value is dismantled

A tree can have an empty alternative or a node carrying a key and two subtrees. A consumer first inspects the tag, then receives fields of the selected constructor. Pattern exhaustiveness checks all alternatives including combinations nested inside them. Natural deduction connects sum and product operations to logical disjunction and conjunction when types are read as propositions.[1][2]

References

  1. Benjamin C. Pierce et al., Software Foundations ↩
  2. Shriram Krishnamurthi, Programming Languages: Application and Interpretation ↩