I have heard people talking a lot about Algebraic Data Types (not to be confused with "Abstract Data Types") in functional programming. All I know is that ADT refers to some kind of composite (often recursive) data types like trees or math expressions.
In Wikipedia, it's only said that:
an algebraic data type is a kind of composite type, i.e., a type formed by combining other types. Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e. tagged or disjoint unions, or variant types).
But no formal definition is given.
So I am wondering what exactly is the definition of ADT? As per Wikipedia, product types and sum types are two examples of ADT, but are product and sum the only valid operations for defining ADT? Are there other operations which are also allowed?
The Algebraic Data Types are composite types, that is, a type formed by the combination of other types and are commonly classified in two: sums and products.
For example:
Currency = USD + EUR + GBP
Money = Amount * Currency
The way to read this is to translate sums by OR and products by AND.
A product is a type that can normally be created in any programming language, whether functional or not, such as classes in Kotlin, Java, C #, Struct in Swift or C # etc.
The parts of which they are composed are read with AND.
Money = Amount * Currency
They are known as products because the number of possible values they can have is the product of the number of possible values of the component parts.
A sum type is a type of algebraic data, also known as discriminated union or disjoint union, which traditionally had only direct support in languages such as Scala or Haskell.
The parts of which a sum type is composed are read as OR because the value of the result object can only contain one of the options.
Currency = USD + EUR + GBP
In this case, the currency value can only be USD or EUR or GBP.
They are known as sum because the number of possible values that can have is: the sum of the number of possible values of the parts that compose it.
This is a link to my blog (spanish), where I have a more complete article with kotlin examples: http://xurxodev.com/tipos-de-datos-algebraicos/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With