Simple Introduction to Monads With Java Examples
Join the DZone community and get the full member experience.Join For Free
Monads are heavily used in most functional programming languages. In Haskell, for example, they are essential and appear everywhere, in all kinds of applications and libraries.
On the other hand, monads are rarely used in popular, non-pure-functional programming languages like C#, Java, Python, etc.
Why is there such a big discrepancy?
To find the answer, we first have to know:
What is a monad?
Why are they used in functional languages? What problems do they solve?
How could we use monads in languages like C#, Java, Python, etc.? Should we do that?
After reading this article, you'll hopefully be able to answer these questions without hesitation.Note: This article is targeted to software developers with a good background in non-pure-functional programming languages. Examples in this article are shown in Java. But readers are not required to be Java experts because only basic Java is used. The examples would look very similar in C#, and they could easily be rewritten in other languages that support generic types (type parameters) and higher order functions (functions that can take a function as input argument, and return functions). The full source code is available on Gitlab.
A Simple Problem
Suppose we have to write a simple function to “enthuse” a sentence. The function should do this by transforming an input string as follows:
Remove leading and trailing spaces
Convert all letters to uppercase
append an exclamation point
For example, feeding the function with
" Hello bob " should return
A Simple Solution in Java
In most programming languages, this is trivial to do. Here is a solution in Java:
To write a complete Java “application” including a rudimentary test, we can put the code below in the file
Then we can compile and run the program with the following commands:
The output looks as expected:
So far so good.
In the previous Java example, we used object methods (e.g.
sentence.trim()). However, as this article is about monads, we have to be aware that pure functional programming languages don't have methods (functions) executed on objects. A pure functional programming language (based on the lambda calculus) has only side-effect-free functions that take an input and return a result.
Let’s therefore rewrite the previous code (still in Java) by using only pure functions. This is important because we have to use functions in order to finally understand why monads have been invented.
Here is the new code:
The code starts with three pure functions (
appendExclam) that take a string as input, and return a string as result. Note that I cheated a bit, because I still use object methods in the function's bodies (e.g.
string.trim()). But that doesn't matter here, because in this exercise we don't care about the implementations of these three functions - we care about their signatures.
The interesting part is the body of function
We can see that there are only function calls (as in functional programming languages). The calls are nested, and executed like this:
trim ( sentence )is executed
The result of step 1 is fed into
The result of step 2 is fed into
The result of step 3 is returned as the result of function
The following picture illustres how the initial input is transformed by chaining three functions:
test. The result remains the same:
In functional programming languages, nested function calls (like our
appendExclam ( toUpperCase ( trim ( sentence ) ) )) are called function composition.
Important: Function composition is the bread and butter of functional programming languages. In the lambda calculus, the body of a function is a single expression. Complex expressions can be created by composing functions.As we will see later, monads provide a solution to a problem that can arise when we compose functions. Before going on, let’s therefore see variations of using function composition in different environments. Readers familiar with this important concept can jump to the chapter.
First, it is interesting to note that the idea of function composition is similar to pipes in Unix/Linux. The output of a first command is fed as input into a second command. Then, the output of the second command is fed as input into a third command, and so on. In Unix/Linux, the symbol
| is used to pipe commands. Here is an example of a pipe that counts the number of files containing
"page" in their name (example borrowed from How to Use Pipes on Linux):
Because pipes are useful in many contexts, some programming languages have a specific pipe operator. For example, F# uses
|> to chain function calls. If Java had this operator, then function
enthuse could be written as:
...which would semantically be the same, but a bit more readable than real Java that uses nested function calls:
Function Composition Operator
As function composition is essential, most functional programming languages have a dedicated function composition operator that makes it trivial to compose functions.
For example, in Haskell, a dot (
.) is used to compose functions (derived from the ring operator symbol ∘ used in maths). The dot is itself a function whose signature is defined as follows:
This tells us that the function takes two functions as input (
b -> c and
a -> b), and returns another function (
a -> c), which is the composition of the two input functions.
Hence, to state that function
h is the composition of functions
g, in Haskell, you can simply write:
Note the totally different semantics of the dot operator in Haskell and object-oriented languages like C#, Java, etc. In Java,
f.g means applying
g on object
person.name). In Haskell, it means composing functions
>> to compose functions. It is defined like this:
And it is used as follows:
>> operator must not be confused with the Monad sequencing operator in Haskell, which also uses the symbol
If Java had a syntax similar to F# for function composition, then function
enthuse could be written as:
Errors, But No Exceptions
For the sake of this tutorial, suppose that our functions can fail in the following ways:
trimfails if the input string is empty or contains only spaces (i.e. the result cannot be an empty string).
toUpperCasefails if the input string is empty or contains characters others than letters or spaces.
appendExclamfails if the input string is more than 20 characters long.
In idiomatic Java, an exception is thrown to indicate an error. But pure functional programming languages don't support exceptions because a function cannot have a side effect. A function that can fail must return error information as part of the result returned by the function. For example, the function returns a string in case of success, or error data in case of an error.
So let's do that in Java.
First, we define a simple error class with an
info field that describes the error:
As said already, the functions must be able to return a string in case of success, or else an error object. To achieve this we can define class
As we can see:
The class has two immutable fields to hold either a result or an error
There are two constructors.
The first one is used in case of success (e.g.
return new ResultOrError ( "hello");).
The second constructor is used in case of failure (e.g.
return new ResultOrError ( new Error ( "Something went wrong") );).
isErrorare utility functions
toStringis overridden for debugging purposes
Now, we can rewrite the three utility functions to include error handling:
Note: To keep this exercise code simple, we don't check and handle
null values (as we would do in production code). For example, if a function is called with
null as input we simply accept that a
NullPointerException is thrown.
What matters is that the three functions who previously returned a string, now return a
As a consequence, function
enthuse that was defined as follows:
...doesn't work anymore.
Unfortunately, function composition is now invalid, because the functions now return a
ResultOrError object, but require a
string as input. The output/input types don't match anymore. The functions can't be chained anymore.
In the previous version, when the functions returned strings, the output of a function could be fed as input to the next function:
But now this can't be done anymore:
However, we can still implement
enthuse in Java like this:
Not good! The initial simple one-liner has turned into an ugly monster.
We can improve a bit:
This kind of code works in Java and many other programming languages. But it's certainly not the code we want to write over and over again. Error handling code intermixed with normal flow makes code difficult to read, write, and maintain.
More importantly, we simply can't write code like this in pure functional programming languages. Remember: An expression returned by a function is made up of function composition.
It's easy to image other cases that lead to the same dilemma. And what should we do in situations where the same problem appears in many variations? Yes, we should try to find a general solution that can be used in a maximum of cases.
Monads to the rescue!
Monads provide a general solution for this kind of problem, and they have other benefits as well. As we will see later, monads enable function composition for so-called monadic functions that cannot be composed directly because their types are incompatible.
Some people say:
"You could have invented monads if they didn't exist."
(For example, Brian Beckman in his excellent presentation, Don't fear the Monad.)
So let's try to find a solution ourselves, ignoring the fact that a monad would solve our problem.
The 'bind' Function
In functional programming languages, everything is done with functions. So we know already that we have to create a function to solve our problem.
Let's call this function
bind, because it's role is to bind two functions that cannot be composed directly.
Next we have to decide what should be the input of
bind, and what should it return. Let's consider the case of chaining functions
The logic to implement must work as follows:
trimreturns a string, then
toUppercasecan be called, because it takes a string as input. So the final output will be the output of
trimreturns an error, then
toUppercasecannot be called, and the error must simply be forwarded. So the final output will be the output of
We can deduce that
bind needs two input arguments:
The result of
trim, which is of type
toUppercase, because if
trimreturns a string then
The output type of
bind is easy to determine. If
trim returns a string then the output of
bind is the output of
toUppercase, which is of type
trim fails then the output of
bind is the output of
trim, which is also of type
ResultOrError. As the output type is
ResultOrError in both cases, the output type of
bind must be
So now we know the signature of
In Java this is written as:
bind is easy because we know exactly what needs to be done:
enthuse can now be rewritten as follows:
But this is still imperative code (a sequence of statements). If we did a good job, then we must be able to rewrite
enthuse by just using function composition. And indeed, we can do it like this:
At first sight, the body of
bind might be a bit confusing. We will change that later. The point is that we use only function composition in the body of
Note: If you never saw this kind of code before, then please take your time to digest and fully understand what's going on here. nderstanding
bind is the key to understanding monads!
ind is the function name used in Haskell. There are alternative names, such as:
Does it work correctly? Let's test it. Here is a class containing
bind, the two variations of
enthuse, and some simplistic tests that cover the success and all error paths:
bind function defined above serves our specific problem. But to make it part of a monad we will have to make it more general. We will do that soon.
bind as shown above, is the common way to solve our problem of function composition. But it's not the only way. An alternative is called Kleisli composition (out of the scope of this article).
A Monad, Finally!
Now that we have
bind, the remaining steps to get to the monad are easy. We just need to make some improvements in order to have a more general solution that can be applied in other cases too.
Our goal in this chapter is clear: See the pattern and improve
ResultOrError, so that we can finally enthuse:
"A MONAD!"First improvement: Previously, we defined
bindas an isolated function that fulfilled our specific needs. A first improvement is to move
bindmust be part of our monad class. The reason is that the implementation of
binddepends on the monad that uses
bind. While the signature of
bindis always the same, different kinds of monads use different implementations.
ResultOrErrorso that it works with any type of result? Yes, we can. We just have to add a type parameter to
bind into the class and adding a type parameter, the new version now becomes:
Note the class name:
ResultOrErrorMona. This is not a typo. The class isn't a monad yet, so I call it a mona (just for fun).
Here is a picture to illustrate this:
bind function is not able to handle this case, because the output types of the two functions are different (
ResultOrError<String>). We have to make
bind more general, so that functions of different value types can be chained. The signature of
bind must be changed from:
The implementation of
bind must be adapted, too. Here is the new class:
Note the class name again:
Yes, now it's a monad.
Note: In the real world we don't add a "monad" suffix for types that are monads. I called the class
ResultOrErrorMonad (instead of simply
ResultOrError) to make it clear that the class is a monad.
How can we be sure the class is indeed a monad?
While the term 'monad' has a very precise definition in mathematics (as everything in maths), the term isn't yet unequivocally defined in the world of programming languages. However, Wikipedia states a common definition. A monad consists of three parts:
- A type constructor M that builds up a monadic type M T.
In other words, there is a type parameter for the value contained in the monad.
In our case it's type parameter
Rin the class declaration:Java
- A type converter, often called unit or return, that embeds an object x in the monad:
unit(x) : T → M T
In Haskell, the type converter is defined as:
return :: a -> m a
In Java-like languages, this means there must be a constructor that takes a value of type
R, and returns a monad
M<R>that contains this value.
In our specific case it's the constructor of class
- A combinator, typically called bind (as in binding a variable) and represented with an infix operator >>=, that unwraps a monadic variable, then inserts it into a monadic function/expression, resulting in a new monadic value:
(mx >>= f) : (M T, T → M U) → M U
In Haskell, bind is defined as:
(>>=) :: m a -> (a -> m b) -> m b
In our example it's the
Wikipedia then states: "To fully qualify as a monad though, these three parts must also respect a few laws: ..."
In Haskell, the three laws are defined as follows:
return a >>= k = k a
m >>= return = m
m >>= (\x -> k x >>= h) = (m >>= k) >>= h
Discussing these laws is out of the scope of this article (this is an introduction to monads). The laws ensure that monads behave well in all situations. Violating them can lead to subtle and painful bugs, as explained here, here, and here. As far as I know, there is currently no compiler able to enforce the monad laws. Hence, it's the developer's responsibility to verify that the monad laws are respected. Suffice to say that the above
ResultOrErrorMonad fulfills the monad laws.
Although we are done, there is still room for improvement.
Besides having a type parameter for the result value, we could also add a type parameter for the error value. This makes the monad more reusable, because users of the monad are now free to decide which type of error they want to use. For an example you can look at F#'s Result type.
Finally we could make the monad even more reusable by letting the user define the meaning of the two values. In our example, one value represents a result, and the other one represents an error. But we can abstract more. We can create a monad that simply holds one of two possible values - either value_1 or value_2. And the type of each value can be freely defined by a type parameter. This is indeed a standard monad supported by some functional programming languages. In Haskell it's called
Either. It's constructor is defined as follows:
ResultOrErrorMonad class as a starting point, it would be easy to create an
Either monad in Java.
Note: Some projects use an
Either monad for functions that might fail. In my opinion, using a more specific
ResultOrError type is a better, less error-prone option (for reasons not explained here).
An OO Bind
Now that we know how a monad works in functional programming languages, let's go back to the world of OOP (object-oriented programming). Could we create something like an OO-monad?
If we look at class ResultOrErrorMonad, we can see that everything in this class is already standard Java, with one exception: Function
bind is a static member of the class. This means we can't use the dot-syntax of object methods for
bind. Currently the syntax for calling
bind ( v, f ). But if
bind was a non-static member of the class, we could write
v.bind ( f ). This would make the syntax more readable in the case of nested function calls.
Luckily, it's easy to make
To make the monad a bit more versatile, let's also introduce a second type parameter for error values. Then the users are not required to use
SimpleError - they can use their own error class.
Here is the code of a
ResultOrError monad, OO-style:
Now the code for using bind in the body of function
enthuse becomes more readable. Instead of writing:
...we can avoid the nesting and write:
So, can monads be useful in real-world OOP environments?
Yes, they can.
But the word "can" needs to be stressed, because it depends (as so often) on what we want to achieve. Let's say, for instance, that we have some good reasons to 'not use exceptions' for error handling.
Remember the ugly error-handling code we had to write in chapter Errors, But No Exceptions:
Using a monad removes the boilerplate:
The key to understanding monads is to understand
bind (also called
andThen, etc.). Function
bind is used to compose two monadic functions. A monadic function is a function that takes a value of type T and returns an object that contains the value (
a -> m a). Monadic functions cannot be directly composed because the output type of the first function called is not compatible to the input type of the second function.
bind solves this problem.
bind is useful on its own. But it's just one part of a monad.
In Java-like world, a monad is a class (type)
a type parameter
Ttat defines the type of the value stored in the monad (e.g.
a constructor that takes a value of type
Tand returns a monad
M<T>containing the value
M<T> create ( T value )
return :: a -> m a
a bind function used to compose two monadic functions
M<T2> bind ( M<T1> monad, Function<T1, M<T2>> function )
(>>=) :: m a -> (a -> m b) -> m b
A monad must respect three monad laws. These laws ensure that the monad behaves well in all situations.
Monads are primarily used in functional programming languages, because these languages rely on function composition. But they can also be useful in the context of other paradigms, such as object-oriented programming languages that support generic types and higher order functions.
As hinted in the title, this article is an introduction to monads. It doesn't cover the full spectrum of monads, it doesn't show other useful examples of monads (maybe monad, IO monad, state monad, etc.) and it completely ignores “category theory,” the mathematical background for monads. For those who want to know more, an abundance of information is already available on the net.
Hopefully, this article helps you get the gist of monads, see their beauty, and understand how they can improve code and simplify life.
Published at DZone with permission of Christian Neumanns. See the original article here.
Opinions expressed by DZone contributors are their own.