машинку продаю...
Dec. 8th, 2012 11:11 amэто не реклама, это печальный вздох жалко её, такая хорошая.
T
and U
; their composition T ∘ U
is a functor which is not always a monad. Unit is okay, but multiplication, T ∘ U ∘ T ∘ U → T ∘ U
does not always exist. It would exist if the two monads commuted, that if, if we had U ∘ T → T ∘ U
, which is also known as traversal of U
over T
; in case U
comes from a monoid, this is the famouse "map/reduce".Reader
monad and Maybe
monad, the latter also known as Option
in Scala and in various Java libraries. It is almost obvious that if we have a Maybe
wrapped into a Reader
, it cannot be naturally transformed into Reader
wrapped into a Maybe
.Maybe
in Java can be thought of as a function that may throw some kind of exception, and we don't care which, just catch it (and log it, that's what Java people do with exceptions).Reader
in Java can be represented as "dependency injection" - we have a function that is supposed to return something, but its behavior depends on some external circumstances unknown to client code. This is different from State
, an example of which is a Random Numbers Generator; with "dependency" we are tied to the same environment during the lifetime of the program.DEBUG
, which is either TRUE
or FALSE
.function: X → Y
, in our Reader
is actually a function: X → Y2
whereby we potentially produce two values, one for DEBUG=TRUE
and another for DEBUG=FALSE
.Maybe
, what a client sees as a function: X → Y
is actually function: X → 1+Y
, where failures map values into this 1
, which in Java is represented via null.(1 + Y)2 == 1 + 2×Y + Y2
, and the other is 1 + Y2
.1×Y
? Unless we have Y
as an algebra over Maybe
, that is, a natural transformation 1 → Y
.Maybe ∘ Reader
is a monad.Reader
to a dependency on a certain type E
of "environment: Reader<E>
.Reader
amounts to having a functor X ↦ XE
, where unit X → XE
amounts to constant, and multiplication (XE)E → XE
is dual to diagonal Δ E → E×E
, that is, XΔ
.Reader(Maybe(Reader(Maybe(X))))
to Reader(Maybe(X))
? Not only naturally, but with all the monadic properties of multiplication (unit and associativity, that is).(1 + (1 + X)E)E
, and we need to produce (1 + X)E
E × (1 + (1 + X)E)E → (1 + X)
Δ E → E×E
, and buildE × (1 + (1 + X)E)E → E × E × (1 + (1 + X)E)E → (1 + X)
eval: E × AE
, haveE × E × (1 + (1 + X)E)E → E × (1 + (1 + X)E)
E × (1 + (1 + X)E) → (1 + X)
E × 1 + E × (1 + X)E) → (1 + X)
1
, the second is eval
again.Maybe
-algebras, they are, are not they?)