
A Gentle Introduction to Monad Transformers – or, Values as Exceptions - zik
https://github.com/kqr/gists/blob/master/articles/gentle-introduction-monad-transformers.md
======
badman_ting
This is very thorough, and I'm sure it's helpful to a lot of folks. So thank
you to the author. For me, it's probably a syntactical issue, but I find
things such as the below too hard to comprehend.

 _What did we get just by doing this? Let 's see:_

    
    
        λ> :type EitherIO
        EitherIO :: IO (Either e a) -> EitherIO e a
    
        λ> :type runEitherIO
        runEitherIO :: EitherIO e a -> IO (Either e a)
    

_So already we have a way to go between our own type and the combination we
used previously! That 's gotta be useful somehow._

OK, looks like a pair of parentheses moved. I have seriously no idea why that
is interesting or useful. Conceptually what's going on is probably not
complicated, but that syntax makes it inscrutable to me. (I could be wrong and
the problem could be at a deeper level of understanding.)

~~~
lumpypua
They're different representations of the same thing.

This is useful because we can do easy error handling in the EitherIO monad but
we can't run it. We can run the IO monad but handling errors in it sucks ass.

So we handle errors in EitherIO, and make it runnable (ie. convert to the IO
monad) by calling runEitherIO.

It's like calling a function that takes a pair when some other API gives you a
two element list. The same kind of data, just munging the shape so things
work. In pseudocode:

    
    
      // EitherIO and runEitherIO in this analogy.
      fun toList((a, b)) -> [a, b]
      fun toPair([a, b]) -> (a, b)
    
      // Gotta convert to a "runnable" pair before calling the function.
      var latLngList = [blah, blah]
      var resultPair = api1ThatRequiresPair(toPair(latLngList))
    
      // Convert representations to do operations with a different API.
      api2ThatRequiresList(toList(resultPair))

------
codygman
This tutorial is awesome and really tightened up a recent application I wrote.
I've been trying to grok monad transformers for a while and I'm going back
over the transformers paper[0] since I feel this tutorial helped things
"click" for me.

0:
[http://www.cs.virginia.edu/~wh5a/personal/Transformers.pdf](http://www.cs.virginia.edu/~wh5a/personal/Transformers.pdf)

------
judk
Are Extensible Effects going to displace Monad Transformers. Oleg argues they
are clearly superior.

~~~
codygman
Any good resources on extensible effects? I'm currently reading:

[http://okmij.org/ftp/Haskell/extensible/index.html](http://okmij.org/ftp/Haskell/extensible/index.html)

I also know of:

[http://jozefg.bitbucket.org/posts/2014-07-15-reading-
extensi...](http://jozefg.bitbucket.org/posts/2014-07-15-reading-extensible-
effects.html)

However I was wondering if there was a "Gentle introduction to extensible
effects".

Edit: I found this presentation: [http://ro-
che.info/docs/2014-06-14-extensible-effects.html#1...](http://ro-
che.info/docs/2014-06-14-extensible-effects.html#1.0)

~~~
brandonbloom
It's worth looking at the Eff language, which is an OCaml variant. Much easier
to understand, especially if you're monadically challenged. This paper is a
great introduction to the idea of algebraic effects:

[http://math.andrej.com/2012/03/08/programming-with-
algebraic...](http://math.andrej.com/2012/03/08/programming-with-algebraic-
effects-and-handlers/)

Shameless plug, I gave a talk about this at the NYC "Papers We Love" meetup.
Audio & slides can be found here:
[http://www.mixcloud.com/paperswelove/bbloom_3_17_2014_progra...](http://www.mixcloud.com/paperswelove/bbloom_3_17_2014_programming_with_alegebraic_effectshandlers/)

~~~
mercurial
I read the Eff paper, and I was blown away. I find monad transformers to be
extremely clunky in comparison. The mental tax of Eff seems a lot smaller.

------
maikklein
He wrote

    
    
      either :: (LoginError -> Text) -> (Text -> Text) -> (Either LoginError Text -> Text)
    

But shouldn't it be

    
    
      either :: (LoginError -> Text) -> (Text -> Text) -> (Either LoginError Text) -> Text

~~~
hesselink
In Haskell, `a -> b -> c` and `a -> (b -> c)` are the same types.

------
renox
> A Gentle Introduction to Monad Transformers

Gentle, gentle. I didn't get it __at all __.

------
edmack
Also check out boxes in scala, these are lovely to use:
[https://www.assembla.com/wiki/show/liftweb/Box](https://www.assembla.com/wiki/show/liftweb/Box)

~~~
pramalin
Try[1] in Scala standard library is also similar in concept. Erik Meijer
covers this subject very well in his talks[2].

    
    
      [1]The Try type represents a computation that may either result in an exception, or return a successfully computed value. http://www.scala-lang.org/files/archive/nightly/docs/library/index.html#scala.util.Try
      [2]http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2014/Keynote-Duality

------
platz
Slight nit with title: Monad Transformers can be used to model exceptions, but
are more general - really they are a way to compose monads on a per-type basis
(because all monads do not compose together automatically "for free" like
functions or Functors).

Also there are non-transformer ways to handle errors in Haskell, see:
[http://blog.ezyang.com/2011/08/8-ways-to-report-errors-in-
ha...](http://blog.ezyang.com/2011/08/8-ways-to-report-errors-in-haskell-
revisited/)

~~~
clhodapp
I think that the title is meant to be interpreted as "(A Gentle Introduction
to Monad Transformers) – or, (Values as Exceptions)" rather than "(A Gentle
Introduction to ((Monad Transformers) – or, (Values as Exceptions)))". That
is, I think that the intention is to introduce the reader to monad
transformers by reifying exceptions into values (and thus arriving at monad
transformers), rather than to give the reader a complete understanding of
monad transformers, which are (in this hypothetical world) the same thing as
exception-reifying values.

------
tonetheman
holy hell why are tutorials like this always in haskell. While I am sure there
are a lot of people using Haskell, there is a whole lot more people in Java or
python.

~~~
rrradical
People that use Haskell tend to care more about this sort of thing than people
that use those languages. Or alternatively, if someone cared about this sort
of thing they would use Haskell.

~~~
lsdafjklsd
Meh, I don't know or use Haskell and I'm interested in this stuff. I'm a
javascript programmer introduced to these concepts via
[1][https://www.youtube.com/watch?v=m3svKOdZijA](https://www.youtube.com/watch?v=m3svKOdZijA)
and [https://github.com/CrossEye/ramda](https://github.com/CrossEye/ramda).
Programming in a point free style in javascript is great!

1: Hey Underscore, You're Doing it Wrong!

~~~
rrradical
Fair enough, but you're thinking about checking it out now right? :) Come to
the dark side.
[http://learnyouahaskell.com/chapters](http://learnyouahaskell.com/chapters)

(Rambda looks neat, thanks for the link.)

------
javajosh
I'd really like it if foundational language/programming advocacy articles
_led_ with a few links into github usages. I mean, I suppose it's possible
that it's so new that it hasn't actually been used, but honestly, if you're
writing an article about it you should probably be able to link into your own
examples, at least.

