| Start |
|---|
| Category theory as a system for organizing abstraction Category theory is a modern mathematical theory and a branch of abstract algebra focused on the nature of connectedness and relation. It is useful for giving solid foundations and common language to many highly reusable programming abstractions. Haskell uses Category theory as inspiration for some of the core typeclasses available in both the standard library and several popular third-party libraries. An exampleThe which lets us "map" over Given this intuition, a common question is "why not call A hint of Category TheoryThe reason is that Functor fits into a set of common structures in Category theory and therefore by calling In particular, Category Theory is highly concerned with the idea of arrows from one place to another. In Haskell, the most important set of arrows are the function arrows So a Functor is any All of these laws arise naturally from the Category Theoretic interpretation of |
Haskell types as a category Definition of the categoryThe Haskell types along with functions between types form (almost†) a category. We have an identity morphism (function) ( We usually call this category Hask. IsomorphismsIn category theory, we have an isomorphism when we have a morphism which has an inverse, in other words, there is a morphism which can be composed with it in order to create the identity. In Hask this amounts to have a pair of morphisms If we find a pair of such morphisms between two types, we call them isomorphic to one another. An example of two isomorphic types would be And we can check that FunctorsA functor, in category theory, goes from a category to another, mapping objects and morphisms. We are working only on one category, the category Hask of Haskell types, so we are going to see only functors from Hask to Hask, those functors, whose origin and destination category are the same, are called endofunctors. Our endofunctors will be the polymorphic types taking a type and returning another: To obey the categorical functor laws (preserve identities and composition) is equivalent to obey the Haskell functor laws: So, we have, for example, that MonadsA monad in category theory is a monoid on the category of endofunctors. This category has endofunctors as objects A monoid object can be defined on a monoidal category, and is a type having two morphisms: We can translate this roughly to the category of Hask endofunctors as: And, to obey the monad laws is equivalent to obey the categorical monoid object laws. †In fact, the class of all types along with the class of functions between types do not strictly form a category in Haskell, due to the existance of |
| Definition of a Category A category
which obey the following laws: In other words, composition with the identity morphism (on either the left or right) does not change the other morphism, and composition is associative. In Haskell, the In this case, The canonical example of a Category in Haskell is the function category: Another common example is the |
Product of types in Hask Categorical productsIn category theory, the product of two objects X, Y is another object Z with two projections: π₁ : Z → X and π₂ : Z → Y; such that any other two morphisms from another object decompose uniquely through those projections. In other words, if there exist f₁ : W → X and f₂ : W → Y, exists a unique morphism g : W → Z such that π₁ ○ g = f₁ and π₂ ○ g = f₂. Products in HaskThis translates into the Hask category of Haskell types as follows, The product type of two types And we can check that the decomposition is correct: Uniqueness up to isomorphismThe choice of Moreover, we could have also chosen This is because the product is not unique but unique up to isomorphism. Every two products of Uniqueness of the decompositionIt is important to remark that also the decomposition function must be unique. There are types which follow all the rules required to be product, but the decomposition is not unique. As an example, we can try to use We can check that it does work: But the problem here is that we could have written another decomposition, namely: And, as the decomposition is not unique, |
Coproduct of types in Hask IntuitionThe categorical product of two types A and B should contain the minimal information necessary to contain inside an instance of type A or type B. We can see now that the intuitive coproduct of two types should be The formal definition is derived from the categorical definition of coproduct. Categorical coproductsA categorical coproduct is the dual notion of a categorical product. It is obtained directly by reversing all the arrows in the definition of the product. The coproduct of two objects X,Y is another object Z with two inclusions: i_1: X → Z and i_2: Y → Z; such that any other two morphisms from X and Y to another object decompose uniquely through those inclusions. In other words, if there are two morphisms f₁ : X → W and f₂ : Y → W, exists a unique morphism g : Z → W such that g ○ i₁ = f₁ and g ○ i₂ = f₂ Coproducts in HaskThe translation into the Hask category is similar to the translation of the product: The coproduct type of two types |
| Haskell Applicative in terms of Category Theory A Haskell's A monoidal category is a category that has some additional structure:
Taking a pair as our product, this definition can be translated to Haskell in the following way: The |
Read Article
https://develop.horje.com/learn/1434/reference