• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Groking Functional Programming: Monads

 
Ranch Hand
Posts: 376
2
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Michał,

Congrats on your book!

I read the TOC and didn't find Monads on it.

How do you handle/mention this pattern in your book?

Regards,

German

 
Author
Posts: 31
5
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great question!

When you pick up a book with "functional programming" in its title you are expecting the monad in the table of contents. I get that but at the same time, I feel it's not helping the cause. I mean, what's the objective?

When I started writing the book, my objective was that after finishing it, a reader is able to implement and maintain a real-world functional application in a team setting. And I chose every topic, every concept, and every example carefully, dropping anything that didn't help the main objective. And, as it turned out, monad was one of them.

It was completely fine to just say that some types have the flatMap function and no matter what type, the function behaves similarly. That's it... There are more functions that are defined on multiple types and behave similarly: map, flatten, orElse, sequence, traverse, foldLeft, foldRight, size, count, contains, forall, exists, and so on. Some of them are coming from category theory and can be named accordingly, but would that be helpful for a beginner functional programmer?

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I LOVE this answer!

- First off, so many programming books appear to have no well thought out objectives or they have impossible to achieve objectives.
- Your stated objective is so good. It's thoughtful, useful, and achievable.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michał Płachta wrote:Great question!
...
When I started writing the book, my objective was that after finishing it, a reader is able to implement and maintain a real-world functional application in a team setting. And I chose every topic, every concept, and every example carefully, dropping anything that didn't help the main objective. And, as it turned out, monad was one of them.


Now I'm forced to show my hand (or the lack thereof).

Since we're already on the topic anyway, would you mind explaining briefly what a monad is for us non-functional folks here and why you think knowing about them would not help your readers implement and maintain a real-world functional application in a team setting?

Are monads more academic than they are practical then? Or are they the functional equivalent of the Singleton, something that is more likely to be misused than applied properly in the real-world that it's regarded as a mistake and something to avoid?
 
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . explaining briefly what a monad is . . .

Or for us who have forgotten, please.

Are monads more academic than they are practical then?

Probably, yes.

Or are they the functional equivalent of the Singleton . . . ?

I think I can remember that bit: no.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Or are they the functional equivalent of the Singleton . . . ?

I think I can remember that bit: no.


I didn't mean it in the sense of "analogous" but rather similar in the way it's regarded. Let me explain further.

In object-oriented programming, there is a commonly-given piece of advice to avoid global variables if at all possible. However, because they grew up using them so much, many people actually miss them when OO canon takes global variables away and puts them in jail. I have empirical evidence to support this.

Almost every Java application I have worked with invariably had some kind of class that's named Constants or Utils that contains nothing but public static final variable declarations and/or public static methods. Just yesterday I came across one such creature, this one with an empty no arguments constructor with ZERO references to it in the project.

When some people first learn about the Singleton pattern, they use it as their Get out of Jail Free card for global variables. I was just wondering if similarly, the Monad in functional programming is regarded the same way as the Singleton in OO: a pattern that's easier to misapply and turn into an anti-pattern rather than something that's generally useful.

I believe one of the authors of the Gang of Four book said they regret having included Singleton in their book at all, given how much it is abused out in the wild. Maybe Michal, with his thoughtful approach to vetting topics for inclusion in his book, was looking to avoid a similar regret.
 
Michał Płachta
Author
Posts: 31
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Monads are very useful and they are hard to misuse in a way singleton is being misused.

However, I'd say they are more of a "low-level" mechanism for functional programs. They come from a mathematical background and have laws and all that, but they are also immensely practical.

My take is that until you are just using monads that are built-in or provided in a library, you don't need to know how they are mathematically constructed. Just like to use garbage collection you don't really need to understand GC internal implementations. It doesn't mean it's not worth it, far from it! It just means you would probably not want to teach garbage collector implementation details in the beginner Java book ;)

Many programmers are productive without knowing the intricacies of GCs—they are fine with just knowing how it works from the outside. The same can be said about functional programmers: they can be productive by just using the flatMap function on their Lists, Options, Eithers, IOs, Resources. And both groups are always welcome to dive deep and learn obscure GC details or Monad laws that make the flatMap function so useful.

Going back to your original singleton example. The analogy for monads would be creating your own monad class (for example, specific to your business domain), not just using provided ones. In this sense, you need to learn the math to do it correctly! The good news is that you rarely need custom monads and even if you do, you can do it in two or three different ways that don't include implementing your own monad.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michał Płachta wrote:Monads are very useful and they are hard to misuse in a way singleton is being misused.


Thank you for your explanation, but what exactly are they? Trying to read the Wikipedia article on them didn't help me either. I still have no clue what they are or how they're useful.  I have used flatMap a few times and I understand what they do but I'm not connecting it to what I'm reading about monads.

This is going to sound very juvenile but the only thing I can think of when I see the word is how it rhymes with another word that starts with g.  

Hopefully, you can help inject some seriousness in my brain when it comes to this word. It seems like an important concept to understand.
 
Michał Płachta
Author
Posts: 31
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Monad is a type that has a flatMap function plus a way to construct a value of this type:

- List has flatMap and empty List constructor
- Option has flatMap and empty constructor
- Either has flatMap and empty constructor
- ...

They are all monads (or monadic types).

One catch here is that the flatMap and the constructor need to be implemented in a specific way: they need to satisfy the three laws (left identity, right identity, and associativity)
 
Saloon Keeper
Posts: 15618
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem you (and many people new to monads) face is that a monad is really just an abstract mathematical concept. You can't really answer "what is a monad" by an analogy with something tangible.

I tried to come up with a simple satisfying answer, but Michał's answer is really the gist of it: A monad is simply any type that provides these two specific functions that are related to each other by a set of mathematical laws. That may seem unsatisfying to you, but it's these abstract properties that make monads so widely usable and pervasive in some functional languages.

The most common example of a monad in Haskell is the Maybe monad. It is essentially the same as the Optional class in Java. Here's what the Optional class would look like if we stripped it down to just the two 'functions' required of monads:

As you can see, the only purpose of the "return" function (our Optional constructor) is to wrap a value into a monadic type.

The purpose of the "bind" function (our flatMap() method) is to evaluate the function that is passed to it in an "evaluation context" that is implied by the monadic type. In the case of Optional, that evaluation context is "no value may be present", and it guarantees that the mapper function will never be called on a null argument.

The benefit of monads is that you can string together a pipeline of operations that all depend on the same evaluation context. With Optional, you can string together operations that require the value to be non-null. With Stream, you can string together operations that require the input to be provided lazily. Of course, you usually use the map() method for this, but from a perspective of monads, map() is really just a convenience method:


In Haskell, the IO monad allows you to string together operations that depend on user input, and cause application output. The "evaluation context" here is "unpredictable input and thus unpredictable output".

The List monad allows you to string together operations that take a sequence of values and return a different sequence of values. The evaluation context here is "the input will be a sequence of zero or more ordered values".

As you can see, the "evaluation context" I spoke of can be a very wide range of things, and this is what makes monads so powerful (and difficult to understand intuitively).
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"A monad is just a monoid in the category of endofunctors, what's the problem?"

I don't know where that quote came from but you can get a T-shirt with it on and it sort of sums up the whole "what is a monad?" thing.

Over the years, I have seen so many blog posts that try to explain monads by analogy and they all pretty much fail. A monad is like a burrito. A monad is like a writing desk. There was an r/ProgrammerHumor post that said a monad is like a colostomy bag but it has been deleted.

I'm a mathematician by background and studied category theory but I would be hard-pressed to explain a monad in any useful terms that most programmers could understand. I think Evan Czaplicki did the right thing when he created Elm and pretty much forbade the community from using the M-word because he wanted Elm to be welcoming to JavaScript programmers.

Most of us use monadic constructs in our daily programming lives and just don't realize it. Written code around a data structure that can hold an error or a success value? Written code around an optional value? Written a for-comprehension? (many languages have that built-in, including Scala) Congratulations, you've written monadic code.

Languages that enshrine the monad -- or rather the family of monads -- in code, either in the language itself or in a library, are just giving names to an abstraction that makes writing these constructs a bit easier and a bit more generic.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your answers. What I'm getting is that it's like trying to describe the color blue to a person who was born blind. Seems like "Grokking Monads" would sell a few copies.
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic