Simple algebraic data types
2 years, 3 months ago
It is the sixth article from the cycle "The Category Theory for Programmers". The previous articles were already published on Habré:
0. The category theory for programmers: preface
1. Category: composition essence
2. Types and functions
3. Categories, big and small
4. Kleysli's categories
5. Works and koproizvedeniye
In the previous article basic operations over types were considered: work and koproizvedeniye. Now we will show that the combination of these mechanisms allows to construct many of daily data structures. Such creation has essential applied relevance. For example, if we are able to check base data types for equality, and also we know how to reduce equality of work and a koproizvedeniye to equality a component, then operators of equality for composite types it is possible to display automatically. In Haskell for an extensive subset of composite types operators of equality and comparison, converting at line and back and many other operations are automatically brought.
Let's consider in more detail the place of work and a koproizvedeniye of types in programming.
Work of types
Canonical implementation of work of types in programming languages is a couple. In Haskell of steam is a primitive designer of types, and in C ++ it is rather difficult template from standard library.
Strictly speaking, work of types is not commutative: it is impossible to substitute couple of type
(Int, Bool) instead of
(Bool, Int), though they also contain the same data. However work is commutative to within the isomorphism set by function
swap, which back to:
swap :: (a, b) -> (b, a)
swap (x, y) = (y, x)
It is possible to consider such couples as different formats of storage of the same information as big endian and little endian.
Read more »
Clear code free monads
2 years, 8 months ago
From the translator:
It is the loose translation of the article "Purify code using free monads" of Gabriel of Gonzales devoted to use of free monads for representation of code as syntax tree with the subsequent managed interpretation.
On Habré there are other articles of Gabriel — "Cooperative flows from scratch in 33 lines on Haskell" and "Than free monads are good".
For reading of this article it is necessary to know that such free monad and why it is functor and monad. It is possible to learn about it in the specified two transfers or in article to which the author refers.
All notes of the translator are italicized.
According to all notes connected with transfer address in pm.
Experienced programmers on Haskell often advise beginners to do programs so pure as far as it is possible. Function is called pure if it determined (the returned value unambiguously is defined by values of all formal arguments) also has no ghost effects (that is does not change execution environment status). In classical mathematics, λ-исчислении and combinatory logic all functions pure. Purity provides set of practical advantages:
- it is possible to prove formally some properties of the written code,
- besides, it is possible to survey easily code and to tell that it does,
- at last, it is possible to banish through QuickCheck.
For demonstration I will use such unpretentious program
main = do x <- getLine
In the provided program, however, there is one shortcoming: in it the business logic and ghost effects are mixed. In specific case in it there is nothing bad, I always so write unpretentious programs which I can hold entirely in the head. However, I hope you to interest in cool pieces which turn out when ghost effects are separated from business logic.
Read more »