When I learn Scala/Haskell, I see there is a concept of Algebraic data type. I\'ve read the explanation from the wikipedia, but I still have a question:
Why does it
Consider the type Bool
. This type, of course, can take on one of two possible values: True or False.
Now consider
data EitherBool = Left Bool | Right Bool
How many values can this type take on? There are 4: Left False, Left True, Right False, Right True
. How about
data EitherBoolInt = Left Bool | Right Int8
Here there are 2 possible values in the Left branch, and 2^8 in the Right branch. For a total of 2 + 2^8 possible values for EitherBoolInt
. It should be easy to see that for any set of constructors and types, this kind of construction will give you a datatype with a space of possible values the size of the sum of the possible values of each individual constructor. For this reason, it's called a sum type.
Consider instead
data BoolAndInt = BAndI Bool Int8
or simply
type BoolAndInt = (Bool, Int)
How many values can this take on? For each possible Int8, there are two BoolAndInts, for a total of 2*2^8 = 2^9 total values. The total number of possible values is the product of the number of values of each field of the constructor, so this is called a product type.
This idea can be extended further -- for example, functions from a->b are an exponential datatype (see The Algebra of Algebraic Datatypes). You can even create a reasonable notion of the derivative of a datatype. This is not even a purely theoretical idea -- it's the basis for the functional construct of "zippers". See The Derivative of a Datatype is the Type of its One-Hole Contexts and The Wikipedia entry on zippers.
In simple words we must consider here relationship between algebra and types. Haskell's algebraic data types are named such since they correspond to an initial algebra in category theory.
Wikipedia says:
In computer programming, particularly functional programming and type theory, an algebraic data type is a kind of composite type, i.e. a type formed by combining other types.
Let's take Maybe a
data type:
data Maybe a = Nothing | Just a
Maybe a
indicates that it might contain something of type a
- Just Int
for example, but also can be empty - Nothing
. In haskell types are objects, for example Int
. Operators gets types and produces new types, for example Maybe Int
. Algebraic
refers to the property that an Algebraic Data Type is created by algebraic
operations: sums
and product
where:
For example, let's see sum
for Maybe a
. For the start let's define Add
type:
data Add a b = Left a | Right b
In haskell |
is or
, so it can be or Left a
or Right b
. Vertical bar |
shows us that Maybe
which we defined above is a sum type, it means that we can write it with Add
:
type Maybe a = Add Nothing (Just a)
Nothing
here is here is a unit
type:
In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value
data Unit = Unit
Or ()
in haskell.
Just a
is a singleton type as. Singleton types are those types which have only one value.
data Just a = Just a
After it we can rewrite it as:
type Maybe a = Add () a
So we have unit type - 1
, and singleton type which is - a
. Now we can say that Maybe a
is the same as 1 + a.
If you want to go deep - The Algebra of Data, and the Calculus of Mutation