News for hackers. Stuff that matters.
Re:Monads Made Difficult by jfarmer on Sunday July 14, 2013 @07:33PM (#6042721)
As a counterpoint, I'm fluent in mathematics but know virtually no Haskell. Your write up presumes the exact opposite and is therefore totally unsuitable for someone like me. :)

I mean that quite literally: I can make neither heads nor tails of your write up. It looks like a bunch of text with funny characters to me. This write up, OTOH, is written in my language using idioms I understand and relates it to Haskell without involving too much of the machinery of the language.

Re:Monads Made Difficult by nilkn on Sunday July 14, 2013 @09:08PM (#6043061)
Personally, I found the Haskell in this article far harder than the math, which was more or less trivial to anyone who knows some category theory. For instance, the following

    newtype (FComp g f) x = FComp { unCompose :: g (f x) }

    instance (Functor b c f, Functor a b g) => Functor a c (FComp g f) where
      fmap f (FComp xs) = FComp $ fmap (fmap f) xs
just does not parse easily at all to my mind. In fact, I don't see how someone who doesn't know Haskell could possibly understand the above truly. unCompose isn't even ever used.
Re:Monads Made Difficult by comex on Sunday July 14, 2013 @10:06PM (#6043259)
And for me, as someone experienced with programming languages but only casually poking at the math, it didn't take that long to learn Haskell's syntax, but syntax that gratuitously uses η and μ (which look vaguely like mirror images of each other) is confusing, and I expect I would have a hard time remembering which is which (of course, I would probably learn to read Greek letters instinctively after spending some time reading them). And after at least 10 minutes, I cannot figure out what "Tμ" in "μ∘Tμ" means, syntactically, while no matter how confusing the Haskell code, it can at least be parsed unambiguously.

So a matter of taste!

Re:Monads Made Difficult by nilkn on Sunday July 14, 2013 @10:14PM (#6043290)
It's really not the syntax that's the issue. It's the fact that the author of the article is embedding the category-theoretic concepts into the language at the type level, so wrapper types like Id and FComp have to be introduced as "hacks" to represent functors.

My main takeaway from this article is that at the end of the day Haskell is a programming language and not the most natural setting for abstract mathematics, even if there are many connections between the two areas.

Re:Monads Made Difficult by tel on Thursday July 18, 2013 @11:00AM (#6064056)
On the other hand, embedding mathematics in the types of a language has equivalence with writing theorems (writing the values in that language with proving them) so it's not such an unnatural setting in at least one way.
Re:Monads Made Difficult by tel on Thursday July 18, 2013 @11:17AM (#6064176)
Funnily, η and μ are chosen precisely because they are mirror images of one another—their functionalities are in some sense mirror images of one another as well. η approaches a single-layer of T from the bottom and μ approaches it from the top!

(Edit: extra funnily, I actually flipped these two in my first writeup. This one is corrected, but I wanted to leave the note!)

The real trouble with parsing mathematics is that the syntax is overloaded at every opportunity. This is nice once you're in the same "mathematical mindset" as the author because it's a way for the author to focus your attention purely on the most important concepts being discussed (and everyone expects that there will be a laborious translation step once you start trying to prove things with more rigor). That said, until you are it's a bear and a half.

For instance, T is a functor which means it acts on both objects and maps in the source category. So if T : A -> B and x : Ob(A) then Tx is an object in Ob(B) and T was being used here as an object map. But, if y is also an object in Ob(A) (also written as y : Ob(A)) and there's an arrow in A called f : x -> y (which you might also write as f : A(x,y) --- serious TMTOWTDI) then Tf is an arrow in B like Tf : Tx -> Ty.

So to parse Tμ. If T is an endofunctor T : X -> X, then for any object A : Ob(X), "μ : TTA -> TA" is an arrow in X(A, TA), thus Tμ is an arrow in X(TTTA, TTA).

To parse μ.Tμ note that . is function composition, we just have Tμ as an arrow in X(TTTA, TTA). If we follow that with an application of μ then we have μ.Tμ as an arrow in X(TTTA, TA).

(Edit: also worth noting, for more feeling of parallelism between μ and η, if you, like I did, replace μ with η in the above you get Tη : X(TA, TTA) and η.Tη as (TA, TTTA).)

In more Haskelly notation, Tμ is (fmap join :: (Monad t, Functor t) => t (t (t a))) -> t (t a)) and μ.Tμ is (join . fmap join :: (Monad t, Functor t) => t (t (t a)) -> t a) where I specialized all the types to be operating on the same endofunctor/monad.

Re:Monads Made Difficult by jfarmer on Sunday July 14, 2013 @09:16PM (#6043092)
Yeah, some of the syntax was confusing for me, too, but I trusted that it was equivalent to the mathematical statements.

Here's the original paper on monads, which I highly recommend: http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/ba...

Re:Monads Made Difficult by reeses on Sunday July 14, 2013 @08:48PM (#6042993)
This is precisely what made Haskell's monads difficult for me to understand. I kept trying to fit the square block in the round hole.

I finally had to divorce the terms in my head, much like watching the film version of a book that I have read. They are..."related" but frustration will result from assuming they will be the same.

Re:Monads Made Difficult by jfarmer on Sunday July 14, 2013 @09:11PM (#6043071)
Nice! Yeah. I had two avenues of understanding available to me when it came to monads. The first was my knowledge as a mathematically-inclined software engineer. The second was my knowledge as someone fluent in high-level mathematics.

I'll say, here was one key to my understanding once I saw it. In a purely functional language you want everything to be referentially transparent. That is, an expression should be able to be substituted for its value at any given moment in time. At the very least, you'll want the ability to denote which parts of your program are referentially transparent and which aren't.

This can be expressed as utopian vision: in a purely functional language we'd like everything -- everything -- to be a value.

We now need some way of translating code with side effects into code that returns a value. The logical abstraction is to say that the "value" is not the work itself -- once the work is done it's out of the bag, after all -- but rather the "value" is the machine which can perform that work.

To a mathematician this screams "functor." If you want to move between the two spaces repeatedly this screams "adjoint functors." There's some work you want to do in Space A that's more easily expressed in Space B, so you take a thing in A, transform it to a thing in B, do the work in B, and then transform back to A.

Think in, e.g., Ruby:

    sentence.split(' ').map(&:reverse).join(' ')
split(' ') and join(' ') aren't quite inverses of each other, but they are adjoint. These pairs take us from the land of space-separated strings to the land of arrays back to the land of space separated strings.

You see that all the time in programming and mathematics. I sometimes call it the "Super Mario Brothers" maneuver when I'm trying to teach it to students. Mario wants to get to the end of the level, so he hits a block, goes up the vine to cloud land, runs through cloud land, and then comes back down.

Re:Monads Made Difficult by tel on Monday July 15, 2013 @02:20AM (#6044013)
Ka-bam! This sort of intuition is exactly what actually drove home monads to me after I spent far too long "knowing" them in a "I can code with these" kind of sense. Category theory might be "impractical" for coding, but the intuitions it helps you develop are wonderful.
Re:Monads Made Difficult by minikomi on Sunday July 14, 2013 @09:18PM (#6043102)
I would love to see you write more in this style - it's very clear and straight forward.
Re:Monads Made Difficult by omaranto on Sunday July 14, 2013 @11:42PM (#6043573)
I disagree, I found it very muddled. For example, how are split(' ') and join(' ') supposed to be adjoint functors when they're not even functors?
Re:Monads Made Difficult by chas on Monday July 15, 2013 @06:28AM (#6044703)
He's playing fast and loose with the formalisms to try to convey the intuition to people without a strong algebraic background. Ruby isn't very well behaved from a categorical perspective (heck, even Haskell doesn't get all of the way there if you want to be really formal) so it's not perfect, but I think the reasoning is morally correct and insightful.
Re:Monads Made Difficult by omaranto on Sunday July 14, 2013 @11:44PM (#6043589)

    split(' ') and join(' ') aren't quite inverses of each other, but they are adjoint.
Do you mean you think they are adjoint functors? How are they functors, between which categories?
Re:Monads Made Difficult by tel on Monday July 15, 2013 @02:26AM (#6044024)
I wouldn't say they're the tightest functors ever, but you could see them as endofunctors between the subcategories String and [String]. Some small amount of intuition would translate, although by and large this setting isn't polymorphic enough to have good behavior. After all, there are a lot of functions on strings that would not have a functorial relationship over split(' ').
Re:Monads Made Difficult by omaranto on Monday July 15, 2013 @11:23AM (#6046132)
Maybe you mean functors instead of endofunctors (an endofunctor goes from a category to itself)? But that's a minor quibble, the main problem is I don't understand which categories you mean? What are the objects and morphisms? My best guess (and I have to guess because you didn't say) is that you mean to treat String and [String] as monoids and thus as one object categories. That is String is the category with a single object, the morphisms from that object to itself are all the possible strings and composition is concatenation; and similarly for [String] with list concatenation. If that's what you meant several things are wrong:

1. split(" ") is not a functor because (x+y).split(" ") is not equal to x.split(" ") + y.split(" "), for example:

    "a BC".split(" ") == ["a", "BC"], but
    "a B".split(" ") == ["a B"] and "C".split("  ") == ["C"].
(At least join(" ") is a functor, which here just means monoid homomorphism.) I guess this problem could be fixed by replacing String with StringsThatAreEitherEmptyOrEndInSpace (you need the empty string to be the identity for composition).

2. They are not adjoint, even after fixing the monoids to make them homomorphisms! It's easy to easy that if f and g are adjoint monoid homomorphisms, with unit η and counit ε, then the functions m ⟼ ε f(m) and n ⟼ g(n) η are inverses of each other. This does not happen here since, for example, join(' ') is not injective.

Can you fix the example, maybe by regarding them as categories in some other way? My gut feeling is that this won't work, but I haven't thought about it very much.

If, as I suspect, this adjointness is wrong and not easily fixable, does it still have value as an explanation? I think that's a personality question, I find it more confusing than enlightening, but can imagine some people would find it helpful.

Re:Monads Made Difficult by tel on Monday July 15, 2013 @01:29PM (#6047082)
I'm not trying to defend the example in detail—I agree that they don't have an obvious, rigorous categorical connection. I broadly think that with so little polymorphism you're not going to get a terrific categorical reading. I agree with the op that they still "feel" adjoint and may have a better reading there with a little generalization.

But yeah, it's not the greatest example. I don't mean to defend it as something rigorous.

Re:Monads Made Difficult by jfarmer on Monday July 15, 2013 @03:16PM (#6047923)
Yes, I was specifically talking about the feeling of adjointness. Moments lik those made me think something categorical was happening somewhere.
Re:Monads Made Difficult by jfarmer on Monday July 15, 2013 @03:41AM (#6044212)
Yes, this is more correct, and I was also imagining the category being something like "space-delimited strings." I realize that'd require redefining things like string concatenation to be "space-delimited concatenation" and other such bits.

Anyhow, I was playing loose and I know it. If I were writing in a more serious context I'd cross all the t's and dot all the i's. To me the more important thing was this slow gnawing in my gut, seeing hints of natural transformations and adjoint functors here and there and thinking, "Oh come on, there has to be a way to connect these dots."

Non-functional languages like Ruby don't think in this way, so there are hole all over the language.

Re:Monads Made Difficult by jfarmer on Monday July 15, 2013 @04:35AM (#6044347)
Oh, and also, the moment this became clear to me was when I read the original monad paper. How frustrating -- all this ink spilled on explaining monads and I just needed to read the original paper to see the real motivation.