Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is coffeescript's `?.` operator a monad?

I just read about the Maybe Monad in Haskell and it seems to fill a similar role to Coffeescript's ?. operator. Is the ?. operator considered a monad or is it just two different ways of doing the same thing?

In Haskell, you write

a >>= b >>= c

In coffeescript, you write

a?.b?.c
like image 848
Jesse Shieh Avatar asked May 26 '14 22:05

Jesse Shieh


3 Answers

A single operator cannot be a monad in the same way an engine cannot be a motor vehicle. Short-circuiting behavior is a property of Maybe and (Maybe, return, (>>=)) happens to be a monad.

There probably exists some set of things that you can define within CoffeeScript such that together they happen to be a monad as well with ?. filling the "bind" role. Unlike Haskell those things are unlikely to all be first-class or even representable within the language.

like image 68
J. Abrahamson Avatar answered Oct 15 '22 04:10

J. Abrahamson


It works similarly to the definition of (>>=) for Maybe, but it is not a monad. It is just a short-circuiting function application, not an algebraic structure with operations return and bind (or return, fmap, and join) and associated laws.

And that's fine. It doesn't need to be a monad. Short-circuiting application is quite enough for it to be useful.

like image 20
Rein Henrichs Avatar answered Oct 15 '22 05:10

Rein Henrichs


Is the ?. operator considered a monad or is it just two different ways of doing the same thing?

Maybe's ability to fill a similar role to CoffeeScript's ?. operator does not come from Maybe being a monad. It comes from it being Maybe: the structure of the data type, and the useful operations on it that are provided.

That's basically true of all monads; all the amazing things that all the various individual monads can do is because of their individual properties, not their monad-hood. The IO monad gives you a magical model for integrating impure operations that can't be directly expressed in Haskell; not because it's a monad, but because IO itself is magical. The list monad allows you to model nondeterministic values that can take on one of multiple possible values; not because it's a monad, but because the list structure can store multiple values.

Questions of the form "is feature X from language L (that doesn't have monads) a monad?" are somewhat missing the point. The fact of something "being a monad" doesn't get you anything in isolation. It would allow you to apply certain mathematical proofs about the monadic use of feature X, which you almost certainly aren't doing. What "being a monad" is good for is a way of seeing that many different structures are all examples of "the same thing", and providing a common interface that allows you to write (and reuse existing) code that works on any monad.

A simple example is the sequence function; for any monad m it takes a list of monadic values [m a] to a monadic list of values m [a]. Used with the Maybe monad, it gives you a function from lists of maybe values [Maybe a] to Maybe [a] - if any of the internal elements are Nothing, then the result is Nothing, otherwise you get the list of all of the elements. With just a ?. operator, you have to write some (easy) code to do that, but if you want you can define a function and now you have it just as easy as with the monad, right? But sequence can also take a list of lists and give you a list of lists containing every combination of choosing one element from each of the original lists. Or it can take a list of IO operations and give you a single IO operation that executes each of the original ones and yields their results in a single list. Or etc etc.

Nobody had to think of any of these functions ahead of time. They're all pretty simple to implement directly in a non-monadic style, but you generally have to every time you need them in other languages, because most of them aren't provided by the standard libraries of most languages. In Haskell they're all already there, for free, because someone thought of sequence :: Monad m => [m a] -> m [a]. Versions of what sequence does are already implemented for you even for new monads that didn't exist when the standard library was written. And that's just a single function!

That's what "being a monad" gets you, in a programming environment where monads are widely used. Even if you find the extra structures that would allow ?. to be part of the definition of a monad in CoffeeScript (and adopt the necessary conventions to ensure that imperative nature of CoffeeScript doesn't invalidate the laws that monadic operations are supposed to adhere to), you just won't get that benefit. And you won't even see that there is a benefit that you're not getting if you approach the concept of monads from "what does monad give me in the field of replacing what ?. gives me?"

like image 20
Ben Avatar answered Oct 15 '22 04:10

Ben