Reader and Maybe, comments
Dec. 10th, 2012 10:18 pm1.
sassa_nf noted that in Haskell actually Reader composes with any monad. That's a pleasant surprise; and this happens due to the fact that any monad is strong in Haskell. Yes, with the same arguments as I used before, a composition of Reader with any monad is still a monad.
2. The proof of monadicity of Reader and Maybe (or any other strong monad).
I tried; it is long and boring, and there's nothing unusual there, just apply diagonals and eval, and use Yoneda lemma. No big deal, boring.
3. What is
3.1. Formal definition.
is defined via adjointness of functors
3.2. Informal definition.
Given
4. How does it all look in Java?
Given
with two implementation classes, Some and None (having appropriate factories),
and
with a factory
and provided the implementations follows monadic laws,
We can define
and a factory
Now this
I wonder, do I need to define the guts of the methods? I could, no request.
![[livejournal.com profile]](https://www.dreamwidth.org/img/external/lj-userinfo.gif)
2. The proof of monadicity of Reader and Maybe (or any other strong monad).
I tried; it is long and boring, and there's nothing unusual there, just apply diagonals and eval, and use Yoneda lemma. No big deal, boring.
3. What is
eval
actually?3.1. Formal definition.
eval: A × BA → B
is defined via adjointness of functors
X-
and -×Y
, it comes from idA: A → B
.3.2. Informal definition.
Given
(a,f) ∈ A × BA
, eval(a,f)=f(a)
.4. How does it all look in Java?
Given
interfaceMaybe<X> { X getOrElse(X alt); <Y> Maybe<Y> map(Function<X,Y> f); <Y> Maybe<Y> flatMap(Function<X,Maybe<Y> f); }
with two implementation classes, Some and None (having appropriate factories),
and
abstract classReader<E,X> { <X,Y> Reader<E,Y> map(Function<X,Y> f); <X,Y> Reader<E,Y> flatMap(Function<X,Reader<E,Y> f); }
with a factory
<E,X> Reader<E,X> reader(X x) { return new Reader<E,X){...}; } // constant x
and provided the implementations follows monadic laws,
We can define
class<E,X> PartialReader<E,X> extends Reader<E,Maybe<X>>{ <X,Y> PartialReader<E,Y> map(Function<X,Y> f) { ... } <X,Y> PartialReader<E,Y> flatMap(Function<X,Reader<E,Maybe<Y> f) { ... } }
and a factory
<E,X> PartialReader<E,X> pr(X x) { return new PartialReader<E,X){...}; } // constant Some(x)
Now this
PartialReader
satisfies monadic laws.I wonder, do I need to define the guts of the methods? I could, no request.