| Start |
|---|
| Addition and multiplication The addition and multiplication have equivalents in this type algebra. They correspond to the tagged unions and product types. We can see how the number of inhabitants of every type corresponds to the operations of the algebra. Equivalently, we can use The expected results of addition and multiplication are followed by the type algebra up to isomorphism. For example, we can see an isomorphism between 1 + 2, 2 + 1 and 3; as 1 + 2 = 3 = 2 + 1. Rules of addition and multiplicationThe common rules of commutativity, associativity and distributivity are valid because there are trivial isomorphisms between the following types: |
| Functions Functions can be seen as exponentials in our algebra. As we can see, if we take a type As an example, |
| Natural numbers in type algebra We can draw a connection between the Haskell types and the natural numbers. This connection can be made assigning to every type the number of inhabitants it has. Finite union typesFor finite types, it suffices to see that we can assign a natural type to every number, based in the number of constructors. For example: would be 3. And the Uniqueness up to isomorphismWe have seen that multiple types would correspond to a single number, but in this case, they would be isomorphic. This is to say that there would be a pair of morphisms In this case, we would say that the types are isomorphic. We will consider two types equal in our algebra as long as they are isomorphic. For example, two different representations of the number two are trivally isomorphic: Because we can see One and ZeroThe representation of the number 1 is obviously a type with only one constructor. In Haskell, this type is canonically the type And our representation of 0 will be a type without constructors. This is the Void type in Haskell, as defined in |
Recursive types ListsLists can be defined as: If we translate this into our type algebra, we get List(a) = 1 + a * List(a) But we can now substitute List(a) again in this expression multiple times, in order to get: List(a) = 1 + a + aa + aaa + aaaa + ... This makes sense if we see a list as a type that can contain only one value, as in TreesWe can do the same thing with binary trees, for example. If we define them as: We get the expression: Tree(a) = 1 + a * Tree(a) * Tree(a) And if we make the same substitutions again and again, we would obtain the following sequence: Tree(a) = 1 + a + 2 (aa) + 5 (aaa) + 14 (aaaa) + ... The coefficients we get here correspond to the Catalan numbers sequence, and the n-th catalan number is precisely the number of possible binary trees with n nodes. |
| Derivatives The derivative of a type is the type of its type of one-hole contexts. This is the type that we would get if we make a type variable disappear in every possible point and sum the results. As an example, we can take the triple type This is coherent with our usual definition of derivation, as: d/da (aaa) = 3aa More on this topic can be read on this article (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference