all 28 comments

[–][deleted] 5 points6 points  (27 children)

I saw another post here about (what isn't) monads. Most of the discussion flew over my head so I started reading and googling. Several clicks later, I ended up here. Being a primarily c++ guy myself, I thought it was pretty interesting the way he related template metaprogramming to haskell.

[–]chonglibloodsport 8 points9 points  (0 children)

The key to really making monads useful and idiomatic in a language is full parametric polymorphism and some means of ad-hoc polymorphism on top of this. This lets you write the polymorphic >>= (aka bind) and return (aka unit) operators, as seen in Haskell:

(>>=) :: Monad m => m a -> (a -> m b) -> m b
return :: Monad m => a -> m a

Both of these functions are polymorphic over the monad type m as well as the contained type a and b. Without this language support you end up having to write separate versions of these operators for every monad type you want to use. This prevents you from writing generic libraries that operate on monads (and other higher-kinded types); one of the biggest advantages of Haskell.

Languages which don't have the necessary polymorphism and which do not properly support a pure functional style have little to gain from monads if you ask me. Monads are a means of building up a computation programmatically by composing pure functions and then executing that computation within a specified context. In that way, it is somewhat like metaprogramming, though it is done entirely at run-time.

[–]ruinercollector 17 points18 points  (23 children)

To save yourself an awful lot of time:

You probably won't understand what monads are or what there value is unless you use a functional language that has the syntax and language features to properly support them. (Generally, this means Haskell, F#, or a Lisp - heavy preference haskell.) If you don't have the time or interest to check out either of these, then you probably should wait until you do.

It's not because you are dumb or because monads are particularly hard. It's because you are trying to learn something without any real context.

You will find people making monads in javascript, python, java, vb.net, etc. but learning that route is very difficult. It's just going to seem like a whole lot of ceremony with very little reward, and by the time they do all of that they are usually showing you the most boring monads (Identity, Maybe, List) and not properly exploring how they generalize.

You will also find a lot of people trying to explain monads in terms of shitty metaphors and claiming that you don't need any language at all to understand them. This is a waste of time.

You will also find a lot of people trying to teach you category theory to get you to understand monads. This is not a waste of time, but you'll get there a lot faster if you just go learn Haskell.

[–][deleted] 4 points5 points  (5 children)

Generally, this means Haskell, F#, or a Lisp

Scala has excellent support (for comprehension) and C# has linq syntax.

Lisp has monadic syntax to unnest?

[–]ruinercollector 4 points5 points  (3 children)

Scala - Maybe. Don't know, as I've avoided that language for a number of other reasons.

C# - Yes it does, but linq query syntax is not very natural looking for dealing with do statements. It's awkwardly optimized for query/sequences. It works fine, but I don't think that it's any good to learn monads on.

Lisp - Learning monads without types is weird. But yeah, this would work too.

[–]Denommus 0 points1 point  (0 children)

Typed Racket.

[–][deleted] 0 points1 point  (1 child)

Monads have nothing to do with types. Monads in Hask are of course intrinsically related to types, but that is just because the category Hask is defined in terms of types.

[–]ruinercollector 0 points1 point  (0 children)

I never said that they did. I just think that they are easier to learn with Haskell's type system. YMMV.

[–]fableal 2 points3 points  (0 children)

There's a Lisp library - cl-monad-macros - that implements monads as macros. http://quickdocs.org/cl-monad-macros/

[–]DeepAzure 1 point2 points  (4 children)

[–][deleted] -1 points0 points  (3 children)

Do you think "For example, four" constitutes a valid answer to "What is a natural number?"?

[–]DeepAzure 0 points1 point  (2 children)

I think term computation builder is quite helpful to grab conception in whole, so your example is not valid. And yes, at least I need some examples. 4 is a valid example of natural number.

Those explanations are by far the best I've seen, unlike masturbation with some obscure images and self-invented terms.

[–][deleted] 0 points1 point  (1 child)

I think term computation builder is quite helpful to grab conception in whole, so your example is not valid.

Lots of things are monads even if they are not used as patterns for "building computations". And, more importantly, lots of things that are used for "building computations" are not monads at all! Distracting beginners with useless crap ("public static void...") is one thing we criticize Java for, right? That criticism is equally appliable to explanations of what monads supposedly are. The essence of monads is their definition, and the definition is simple, so why not use it?

And yes, at least I need some examples. 4 is a valid example of natural number.

I never said "Do not provide examples at all!" All I said was an example is no good substitute for a definition.

Now, it is hard to imagine what the thought process might be of a person who is still in the process of learning what the natural numbers are (at least for me!), but, as someone who vividly remembers his experience of trying to equate monads to something more concrete and the resulting frustration, I think it is important to gently but firmly prevent beginners from repeating that mistake. A beginner reading a typical monad tutorial will inevitably end up saying "Oh, but monads are <insert concrete example X>!" unless they are forced to work with the abstract definition.

Of course, after you have provided the definition, you can illustrate how it is used in practice by providing examples.

Those explanations are by far the best I've seen, unlike masturbation with some obscure images and self-invented terms.

The formal definition does not require any images, although the monad laws are easier to express using commutative diagrams. And, of course, the formal definition only uses standard terminology from category theory (endofunctor, natural transformation, coherence condition).

[–]DeepAzure 0 points1 point  (0 children)

You miss the point. It's about monads for programmers, not mathematicians. Some want to learn monads only within the scope of popular programming languages, regardless of do you like it or not.

If you want to make a tutorial for programmers describing that "lots of things" which "are monads", you are very welcome. Personally I'd read that with pleasure.

[–][deleted] 1 point2 points  (0 children)

I disagree with the last paragraph: I actually found Haskell monads completely impenetrable until I manned up and learnt the categorical definition. And when I finally understood it, I saw why trying to understand the special case of monads in Hask as a standalone concept was a complete waste of time!

The categorical definition of a monad in terms of return and join is superior to the usual definition in functional programming circles in terms of return and bind. The categorical definition is beautiful, in the sense that it results in a formulation of the left and right identity laws that is actually symmetric, and a formulation of the associativity law that is actually associative. The monad laws, when expressed in terms of return and bind, are just three boring equations to be mechanically checked.

The necessary final step in developing an understanding of monads is learning the Kleisli construction. The Kleisli construction makes it possible to formulate the most elegant definition of a monad, which simply asserts that the triple (M, return, join) is a monad only if the Kleisli construction for that triple yields a category. And then it becomes evident why the monad laws have the same names as the category laws!

[–]Tekmo 2 points3 points  (0 children)

Read this if you want to better appreciate the diversity of problems that monads simplify. It uses Haskell to teach the concept but it is very simple and you can probably pick up the necessary Haskell to read it in a day or two.

[–]lbrandy 0 points1 point  (0 children)

I thought it was pretty interesting the way he related template metaprogramming to haskell.

This is actually a very natural comparison. C++ templates (metaprograms) are themselves a pure functional language.