andersch.dev

<2025-04-19 Sat>

Algebraic Data Types (ADT)

Algebraic data types (ADTs) are composite types in programming that combine other types using sum (OR) and product (AND) operations. ADTs are prominent in functional languages like Haskell, OCaml, and Scala.

The types are:

ADTs benefit from pattern matching (e.g., ensuring all variants are handled in a sum type).

Product Types

Product types (like structs, tuples or records) have an AND relationship. A product type's number of possible values is the product of its types.

Sum Types

Sum types have an OR relationship: Their value is either Type A OR Type B, etc. The total number of possible values is the sum of the possible values in each type - hence them being called "sum" types.