<s>
In	O
functional	B-Language
programming	I-Language
,	O
a	O
monad	O
is	O
a	O
structure	O
that	O
combines	O
program	O
fragments	O
(	O
functions	O
)	O
and	O
wraps	O
their	O
return	B-Language
values	I-Language
in	O
a	O
type	O
with	O
additional	O
computation	O
.	O
</s>
<s>
In	O
addition	O
to	O
defining	O
a	O
wrapping	O
monadic	O
type	O
,	O
monads	O
define	O
two	O
operators	O
:	O
one	O
to	O
wrap	O
a	O
value	O
in	O
the	O
monad	O
type	O
,	O
and	O
another	O
to	O
compose	B-Application
together	O
functions	O
that	O
output	O
values	O
of	O
the	O
monad	O
type	O
(	O
these	O
are	O
known	O
as	O
monadic	O
functions	O
)	O
.	O
</s>
<s>
Functional	B-Language
languages	I-Language
use	O
monads	O
to	O
turn	O
complicated	O
sequences	O
of	O
functions	O
into	O
succinct	O
pipelines	B-Operating_System
that	O
abstract	O
away	O
control	O
flow	O
,	O
and	O
side-effects	O
.	O
</s>
<s>
Both	O
the	O
concept	O
of	O
a	O
monad	O
and	O
the	O
term	O
originally	O
come	O
from	O
category	O
theory	O
,	O
where	O
a	O
monad	O
is	O
defined	O
as	O
a	O
functor	B-Language
with	O
additional	O
structure	O
.	O
</s>
<s>
Since	O
monads	O
make	O
semantics	B-Application
explicit	O
for	O
a	O
kind	O
of	O
computation	O
,	O
they	O
can	O
also	O
be	O
used	O
to	O
implement	O
convenient	O
language	O
features	O
.	O
</s>
<s>
Some	O
languages	O
,	O
such	O
as	O
Haskell	B-Language
,	O
even	O
offer	O
pre-built	O
definitions	O
in	O
their	O
core	O
libraries	B-Library
for	O
the	O
general	O
monad	O
structure	O
and	O
common	O
instances	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
the	O
IO	O
monad	O
,	O
it	O
is	O
because	O
the	O
value	O
may	O
not	O
be	O
known	O
yet	O
,	O
such	O
as	O
when	O
the	O
monad	O
represents	O
user	B-General_Concept
input	I-General_Concept
that	O
will	O
only	O
be	O
provided	O
after	O
a	O
prompt	O
is	O
displayed	O
.	O
</s>
<s>
In	O
all	O
cases	O
the	O
scenarios	O
in	O
which	O
access	O
makes	O
sense	O
are	O
captured	O
by	O
the	O
bind	B-Application
operation	O
defined	O
for	O
the	O
monad	O
;	O
for	O
the	O
Maybe	O
monad	O
a	O
value	O
is	O
bound	O
only	O
if	O
it	O
exists	O
,	O
and	O
for	O
the	O
IO	O
monad	O
a	O
value	O
is	O
bound	O
only	O
after	O
the	O
previous	O
operations	O
in	O
the	O
sequence	O
have	O
been	O
performed	O
.	O
</s>
<s>
bind	B-Application
::	O
(	O
M	O
a	O
)	O
->	O
(	O
a	O
->	O
M	O
b	O
)	O
->	O
(	O
M	O
b	O
)	O
(	O
typically	O
represented	O
as	O
>>=	O
)	O
,	O
which	O
receives	O
a	O
function	O
f	O
over	O
type	O
a	O
and	O
can	O
transform	O
monadic	O
values	O
m	O
a	O
applying	O
f	O
to	O
the	O
unwrapped	O
value	O
a	O
,	O
returning	O
a	O
monadic	O
value	O
M	O
b	O
.	O
</s>
<s>
(	O
An	O
alternative	O
but	O
equivalent	O
construct	O
using	O
the	O
join	O
function	O
instead	O
of	O
the	O
bind	B-Application
operator	O
can	O
be	O
found	O
in	O
the	O
later	O
section	O
.	O
)	O
</s>
<s>
With	O
these	O
elements	O
,	O
the	O
programmer	O
composes	O
a	O
sequence	O
of	O
function	O
calls	O
(	O
a	O
"	O
pipeline	B-Operating_System
"	O
)	O
with	O
several	O
bind	B-Application
operators	O
chained	O
together	O
in	O
an	O
expression	O
.	O
</s>
<s>
Each	O
function	O
call	O
transforms	O
its	O
input	O
plain-type	O
value	O
,	O
and	O
the	O
bind	B-Application
operator	O
handles	O
the	O
returned	O
monadic	O
value	O
,	O
which	O
is	O
fed	O
into	O
the	O
next	O
step	O
in	O
the	O
sequence	O
.	O
</s>
<s>
Typically	O
,	O
the	O
bind	B-Application
operator	O
>>=	O
may	O
contain	O
code	O
unique	O
to	O
the	O
monad	O
that	O
performs	O
additional	O
computation	O
steps	O
not	O
available	O
in	O
the	O
function	O
received	O
as	O
a	O
parameter	O
.	O
</s>
<s>
Between	O
each	O
pair	O
of	O
composed	O
function	O
calls	O
,	O
the	O
bind	B-Application
operator	O
can	O
inject	O
into	O
the	O
monadic	O
value	O
m	O
a	O
some	O
additional	O
information	O
that	O
is	O
not	O
accessible	O
within	O
the	O
function	O
f	O
,	O
and	O
pass	O
it	O
along	O
down	O
the	O
pipeline	B-Operating_System
.	O
</s>
<s>
Typically	O
they	O
are	O
expressed	O
as	O
some	O
kind	O
of	O
enumerated	B-Language
type	I-Language
.	O
</s>
<s>
In	O
this	O
Rust	O
example	O
we	O
will	O
call	O
it	O
Maybexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
and	O
variants	O
of	O
this	O
type	O
can	O
either	O
be	O
a	O
value	O
of	O
generic	B-Language
type	I-Language
T	O
,	O
or	O
the	O
empty	O
variant	O
:	O
Nothing	O
.	O
</s>
<s>
In	O
the	O
following	O
hard-coded	O
example	O
,	O
a	O
Maybe	O
type	O
is	O
used	O
as	O
a	O
result	O
of	O
functions	O
that	O
may	O
fail	O
,	O
in	O
this	O
case	O
the	O
type	O
returns	O
nothing	O
if	O
there	O
is	O
a	O
divide-by-zero.One	O
such	O
way	O
to	O
test	O
whether	O
or	O
not	O
a	O
Maybe	O
contains	O
a	O
value	O
is	O
to	O
use	O
if	O
statements.Other	O
languages	O
may	O
have	O
pattern	O
matchingMonads	O
can	O
compose	B-Application
functions	O
that	O
return	O
Maybe	O
together	O
.	O
</s>
<s>
Instead	O
,	O
we	O
can	O
use	O
something	O
called	O
a	O
bind	B-Application
operator	O
.	O
</s>
<s>
This	O
operation	O
takes	O
a	O
monad	O
and	O
a	O
function	O
that	O
returns	O
a	O
monad	O
and	O
runs	O
the	O
function	O
on	O
the	O
inner	O
value	O
of	O
the	O
passed	O
monad	O
,	O
returning	O
the	O
monad	O
from	O
the	O
function.In	O
Haskell	B-Language
,	O
there	O
is	O
an	O
operator	O
bind	B-Application
,	O
or	O
(>>=	O
)	O
that	O
allows	O
for	O
this	O
monadic	O
composition	O
in	O
a	O
more	O
elegant	O
form	O
similar	O
to	O
function	B-Application
composition	I-Application
.	O
</s>
<s>
With	O
>>=	O
available	O
,	O
chainable_division	O
can	O
be	O
expressed	O
much	O
more	O
succinctly	O
with	O
the	O
help	O
of	O
anonymous	B-General_Concept
functions	I-General_Concept
(	O
i.e.	O
</s>
<s>
lambdas	B-General_Concept
)	O
.	O
</s>
<s>
Notice	O
in	O
the	O
expression	O
below	O
how	O
the	O
two	O
nested	O
lambdas	B-General_Concept
each	O
operate	O
on	O
the	O
wrapped	O
value	O
in	O
the	O
passed	O
Maybe	O
monad	O
using	O
the	O
bind	B-Application
operator	O
.	O
</s>
<s>
The	O
more	O
common	O
definition	O
for	O
a	O
monad	O
in	O
functional	B-Language
programming	I-Language
,	O
used	O
in	O
the	O
above	O
example	O
,	O
is	O
actually	O
based	O
on	O
a	O
Kleisli	O
triple	O
⟨T	O
,	O
η	O
,	O
μ⟩	O
rather	O
than	O
category	O
theory	O
's	O
standard	O
definition	O
.	O
</s>
<s>
A	O
combinator	B-Application
,	O
typically	O
called	O
bind	B-Application
(	O
as	O
in	O
binding	O
a	O
variable	O
)	O
and	O
represented	O
with	O
an	O
infix	O
operator	O
>>=	O
or	O
a	O
method	O
called	O
flatMap	O
,	O
that	O
unwraps	O
a	O
monadic	O
variable	O
,	O
then	O
inserts	O
it	O
into	O
a	O
monadic	O
function/expression	O
,	O
resulting	O
in	O
a	O
new	O
monadic	O
value	O
:	O
</s>
<s>
Algebraically	O
,	O
this	O
means	O
any	O
monad	O
both	O
gives	O
rise	O
to	O
a	O
category	O
(	O
called	O
the	O
Kleisli	O
category	O
)	O
and	O
a	O
monoid	O
in	O
the	O
category	O
of	O
functors	B-Language
(	O
from	O
values	O
to	O
computations	O
)	O
,	O
with	O
monadic	O
composition	O
as	O
a	O
binary	O
operator	O
in	O
the	O
monoid	O
and	O
as	O
identity	O
in	O
the	O
monad	O
.	O
</s>
<s>
Whatever	O
language	O
or	O
default	O
programming	O
paradigm	O
a	O
developer	O
uses	O
,	O
following	O
the	O
monad	O
pattern	O
brings	O
many	O
of	O
the	O
benefits	O
of	O
purely	B-Application
functional	I-Application
programming	I-Application
.	O
</s>
<s>
By	O
reifying	O
a	O
specific	O
kind	O
of	O
computation	O
,	O
a	O
monad	O
not	O
only	O
encapsulates	B-Application
the	O
tedious	O
details	O
of	O
that	O
computational	O
pattern	O
,	O
but	O
it	O
does	O
so	O
in	O
a	O
declarative	B-Language
way	O
,	O
improving	O
the	O
code	O
's	O
clarity	O
.	O
</s>
<s>
Typically	O
,	O
programmers	O
will	O
use	O
to	O
chain	O
monadic	O
functions	O
into	O
a	O
sequence	O
,	O
which	O
has	O
led	O
some	O
to	O
describe	O
monads	O
as	O
"	O
programmable	O
semicolons	O
"	O
,	O
a	O
reference	O
to	O
how	O
many	O
imperative	B-Application
languages	I-Application
use	O
semicolons	O
to	O
separate	O
statements	O
.	O
</s>
<s>
However	O
,	O
it	O
should	O
be	O
stressed	O
that	O
monads	O
do	O
not	O
actually	O
order	O
computations	O
;	O
even	O
in	O
languages	O
that	O
use	O
them	O
as	O
central	O
features	O
,	O
simpler	O
function	B-Application
composition	I-Application
can	O
arrange	O
steps	O
within	O
a	O
program	O
.	O
</s>
<s>
The	O
monad	O
structure	O
can	O
also	O
be	O
seen	O
as	O
a	O
uniquely	O
mathematical	O
and	O
compile	B-Application
time	I-Application
variation	O
on	O
the	O
decorator	O
pattern	O
.	O
</s>
<s>
Because	O
they	O
let	O
application	O
programmers	O
implement	O
domain	B-Architecture
logic	I-Architecture
while	O
offloading	O
boilerplate	O
code	O
onto	O
pre-developed	O
modules	O
,	O
monads	O
can	O
even	O
be	O
considered	O
a	O
tool	O
for	O
aspect-oriented	B-Architecture
programming	I-Architecture
.	O
</s>
<s>
One	O
other	O
noteworthy	O
use	O
for	O
monads	O
is	O
isolating	O
side-effects	O
,	O
like	O
input/output	B-General_Concept
or	O
mutable	O
state	B-Application
,	O
in	O
otherwise	O
purely	O
functional	O
code	O
.	O
</s>
<s>
Even	O
purely	O
functional	B-Language
languages	I-Language
can	O
still	O
implement	O
these	O
"	O
impure	O
"	O
computations	O
without	O
monads	O
,	O
via	O
an	O
intricate	O
mix	O
of	O
function	B-Application
composition	I-Application
and	O
continuation-passing	B-Application
style	I-Application
(	O
CPS	O
)	O
in	O
particular	O
.	O
</s>
<s>
When	O
translated	O
from	O
category-theory	O
to	O
programming	O
terms	O
,	O
the	O
monad	O
structure	O
is	O
a	O
generic	B-Application
concept	I-Application
and	O
can	O
be	O
defined	O
directly	O
in	O
any	O
language	O
that	O
supports	O
an	O
equivalent	O
feature	O
for	O
bounded	O
polymorphism	O
.	O
</s>
<s>
The	O
Parsec	B-Language
parser	O
library	O
uses	O
monads	O
to	O
combine	O
simpler	O
parsing	B-Language
rules	O
into	O
more	O
complex	O
ones	O
,	O
and	O
is	O
particularly	O
useful	O
for	O
smaller	O
domain-specific	B-Language
languages	I-Language
.	O
</s>
<s>
xmonad	B-Device
is	O
a	O
tiling	O
window	O
manager	O
centered	O
on	O
the	O
zipper	B-Application
data	I-Application
structure	I-Application
,	O
which	O
itself	O
can	O
be	O
treated	O
monadically	O
as	O
a	O
specific	O
case	O
of	O
delimited	O
continuations	O
.	O
</s>
<s>
LINQ	B-Language
by	O
Microsoft	O
provides	O
a	O
query	B-Language
language	I-Language
for	O
the	O
.NET	B-Application
Framework	I-Application
that	O
is	O
heavily	O
influenced	O
by	O
functional	B-Language
programming	I-Language
concepts	O
,	O
including	O
core	O
operators	O
for	O
composing	O
queries	O
monadically	O
.	O
</s>
<s>
ZipperFS	O
is	O
a	O
simple	O
,	O
experimental	O
file	B-Application
system	I-Application
that	O
also	O
uses	O
the	O
zipper	B-Application
structure	O
primarily	O
to	O
implement	O
its	O
features	O
.	O
</s>
<s>
The	O
Reactive	B-Application
extensions	I-Application
framework	O
essentially	O
provides	O
a	O
(	O
co	O
)	O
monadic	O
interface	O
to	O
data	O
streams	O
that	O
realizes	O
the	O
observer	B-Language
pattern	I-Language
.	O
</s>
<s>
The	O
term	O
"	O
monad	O
"	O
in	O
programming	O
actually	O
goes	O
all	O
the	O
way	O
back	O
to	O
the	O
APL	B-Language
and	O
J	B-Language
programming	I-Language
languages	I-Language
,	O
which	O
do	O
tend	O
toward	O
being	O
purely	O
functional	O
.	O
</s>
<s>
The	O
form	O
defined	O
above	O
using	O
,	O
however	O
,	O
was	O
originally	O
described	O
in	O
1965	O
by	O
mathematician	O
Heinrich	O
Kleisli	O
in	O
order	O
to	O
prove	O
that	O
any	O
monad	O
could	O
be	O
characterized	O
as	O
an	O
adjunction	O
between	O
two	O
(	O
covariant	O
)	O
functors	B-Language
.	O
</s>
<s>
According	O
to	O
programming	O
language	O
researcher	O
Philip	O
Wadler	O
,	O
computer	O
scientist	O
John	O
C	O
.	O
Reynolds	O
anticipated	O
several	O
facets	O
of	O
it	O
in	O
the	O
1970s	O
and	O
early	O
1980s	O
,	O
when	O
he	O
discussed	O
the	O
value	O
of	O
continuation-passing	B-Application
style	I-Application
,	O
category	O
theory	O
as	O
a	O
rich	O
source	O
for	O
formal	O
semantics	B-Application
,	O
and	O
the	O
type	O
distinction	O
between	O
values	O
and	O
computations	O
.	O
</s>
<s>
The	O
research	O
language	O
Opal	B-Language
,	O
which	O
was	O
actively	O
designed	O
up	O
until	O
1990	O
,	O
also	O
effectively	O
based	O
I/O	B-General_Concept
on	O
a	O
monadic	O
type	O
,	O
but	O
the	O
connection	O
was	O
not	O
realized	O
at	O
the	O
time	O
.	O
</s>
<s>
The	O
computer	O
scientist	O
Eugenio	O
Moggi	O
was	O
the	O
first	O
to	O
explicitly	O
link	O
the	O
monad	O
of	O
category	O
theory	O
to	O
functional	B-Language
programming	I-Language
,	O
in	O
a	O
conference	O
paper	O
in	O
1989	O
,	O
followed	O
by	O
a	O
more	O
refined	O
journal	O
submission	O
in	O
1991	O
.	O
</s>
<s>
In	O
earlier	O
work	O
,	O
several	O
computer	O
scientists	O
had	O
advanced	O
using	O
category	O
theory	O
to	O
provide	O
semantics	B-Application
for	O
the	O
lambda	B-Language
calculus	I-Language
.	O
</s>
<s>
Several	O
others	O
popularized	O
and	O
built	O
on	O
this	O
idea	O
,	O
including	O
Philip	O
Wadler	O
and	O
Simon	O
Peyton	O
Jones	O
,	O
both	O
of	O
whom	O
were	O
involved	O
in	O
the	O
specification	O
of	O
Haskell	B-Language
.	O
</s>
<s>
In	O
particular	O
,	O
Haskell	B-Language
used	O
a	O
problematic	O
"	O
lazy	O
stream	O
"	O
model	O
up	O
through	O
v1.2	O
to	O
reconcile	O
I/O	B-General_Concept
with	O
lazy	O
evaluation	O
,	O
until	O
switching	O
over	O
to	O
a	O
more	O
flexible	O
monadic	O
interface	O
.	O
</s>
<s>
The	O
Haskell	B-Language
community	O
would	O
go	O
on	O
to	O
apply	O
monads	O
to	O
many	O
problems	O
in	O
functional	B-Language
programming	I-Language
,	O
and	O
in	O
the	O
2010s	O
,	O
researchers	O
working	O
with	O
Haskell	B-Language
eventually	O
recognized	O
that	O
monads	O
are	O
applicative	O
functors	B-Language
;	O
and	O
that	O
both	O
monads	O
and	O
arrows	B-Application
are	O
monoids	O
.	O
</s>
<s>
At	O
first	O
,	O
programming	O
with	O
monads	O
was	O
largely	O
confined	O
to	O
Haskell	B-Language
and	O
its	O
derivatives	O
,	O
but	O
as	O
functional	B-Language
programming	I-Language
has	O
influenced	O
other	O
paradigms	O
,	O
many	O
languages	O
have	O
incorporated	O
a	O
monad	O
pattern	O
(	O
in	O
spirit	O
if	O
not	O
in	O
name	O
)	O
.	O
</s>
<s>
Formulations	O
now	O
exist	O
in	O
Scheme	B-Language
,	O
Perl	B-Language
,	O
Python	B-Language
,	O
Racket	B-Operating_System
,	O
Clojure	B-Language
,	O
Scala	B-Application
,	O
F#	B-Operating_System
,	O
and	O
have	O
also	O
been	O
considered	O
for	O
a	O
new	O
ML	B-Language
standard	O
.	O
</s>
<s>
Not	O
only	O
can	O
the	O
monad	O
laws	O
be	O
used	O
to	O
check	O
an	O
instance	O
's	O
validity	O
,	O
but	O
features	O
from	O
related	O
structures	O
(	O
like	O
functors	B-Language
)	O
can	O
be	O
used	O
through	O
subtyping	O
.	O
</s>
<s>
Though	O
rarer	O
in	O
computer	O
science	O
,	O
one	O
can	O
use	O
category	O
theory	O
directly	O
,	O
which	O
defines	O
a	O
monad	O
as	O
a	O
functor	B-Language
with	O
two	O
additional	O
natural	O
transformations	O
.	O
</s>
<s>
So	O
to	O
begin	O
,	O
a	O
structure	O
requires	O
a	O
higher-order	B-Language
function	I-Language
(	O
or	O
"	O
functional	O
"	O
)	O
named	O
map	O
to	O
qualify	O
as	O
a	O
functor	B-Language
:	O
</s>
<s>
This	O
is	O
not	O
always	O
a	O
major	O
issue	O
,	O
however	O
,	O
especially	O
when	O
a	O
monad	O
is	O
derived	O
from	O
a	O
pre-existing	O
functor	B-Language
,	O
whereupon	O
the	O
monad	O
inherits	O
automatically	O
.	O
</s>
<s>
(	O
For	O
historical	O
reasons	O
,	O
this	O
map	O
is	O
instead	O
called	O
fmap	O
in	O
Haskell	B-Language
.	O
)	O
</s>
<s>
A	O
monad	O
's	O
first	O
transformation	O
is	O
actually	O
the	O
same	O
from	O
the	O
Kleisli	O
triple	O
,	O
but	O
following	O
the	O
hierarchy	O
of	O
structures	O
closely	O
,	O
it	O
turns	O
out	O
characterizes	O
an	O
applicative	O
functor	B-Language
,	O
an	O
intermediate	O
structure	O
between	O
a	O
monad	O
and	O
a	O
basic	O
functor	B-Language
.	O
</s>
<s>
The	O
final	O
leap	O
from	O
applicative	O
functor	B-Language
to	O
monad	O
comes	O
with	O
the	O
second	O
transformation	O
,	O
the	O
join	O
function	O
(	O
in	O
category	O
theory	O
this	O
is	O
a	O
natural	O
transformation	O
usually	O
called	O
)	O
,	O
which	O
"	O
flattens	O
"	O
nested	O
applications	O
of	O
the	O
monad	O
:	O
</s>
<s>
The	O
List	O
monad	O
naturally	O
demonstrates	O
how	O
deriving	O
a	O
monad	O
from	O
a	O
simpler	O
functor	B-Language
can	O
come	O
in	O
handy	O
.	O
</s>
<s>
From	O
here	O
,	O
applying	O
a	O
function	O
iteratively	O
with	O
a	O
list	B-Language
comprehension	I-Language
may	O
seem	O
like	O
an	O
easy	O
choice	O
for	O
and	O
converting	O
lists	O
to	O
a	O
full	O
monad	O
.	O
</s>
<s>
Now	O
,	O
these	O
two	O
procedures	O
already	O
promote	O
List	O
to	O
an	O
applicative	O
functor	B-Language
.	O
</s>
<s>
To	O
fully	O
qualify	O
as	O
a	O
monad	O
,	O
only	O
a	O
correct	O
notion	O
of	O
to	O
flatten	O
repeated	O
structure	O
is	O
needed	O
,	O
but	O
for	O
lists	O
,	O
that	O
just	O
means	O
unwrapping	O
an	O
outer	O
list	O
to	O
append	B-Application
the	O
inner	O
ones	O
that	O
contain	O
values	O
:	O
</s>
<s>
can	O
now	O
also	O
be	O
derived	O
with	O
just	O
a	O
formula	O
,	O
then	O
used	O
to	O
feed	O
List	O
values	O
through	O
a	O
pipeline	B-Operating_System
of	O
monadic	O
functions	O
:	O
</s>
<s>
Another	O
benefit	O
is	O
that	O
checks	O
can	O
be	O
embedded	O
in	O
the	O
monad	O
;	O
specific	O
paths	O
can	O
be	O
pruned	O
transparently	O
at	O
their	O
first	O
point	O
of	O
failure	O
,	O
with	O
no	O
need	O
to	O
rewrite	O
functions	O
in	O
the	O
pipeline	B-Operating_System
.	O
</s>
<s>
A	O
second	O
situation	O
where	O
List	O
shines	O
is	O
composing	O
multivalued	B-Algorithm
functions	I-Algorithm
.	O
</s>
<s>
(	O
called	O
do-notation	O
in	O
Haskell	B-Language
,	O
perform-notation	O
in	O
OCaml	B-Language
,	O
computation	O
expressions	O
in	O
F#	B-Operating_System
,	O
and	O
for	O
comprehension	O
in	O
Scala	B-Application
)	O
.	O
</s>
<s>
This	O
is	O
only	O
syntactic	O
sugar	O
that	O
disguises	O
a	O
monadic	O
pipeline	B-Operating_System
as	O
a	O
code	O
block	O
;	O
the	O
compiler	O
will	O
then	O
quietly	O
translate	O
these	O
expressions	O
into	O
underlying	O
functional	O
code	O
.	O
</s>
<s>
Translating	O
the	O
add	O
function	O
from	O
the	O
Maybe	O
into	O
Haskell	B-Language
can	O
show	O
this	O
feature	O
in	O
action	O
.	O
</s>
<s>
A	O
non-monadic	O
version	O
of	O
add	O
in	O
Haskell	B-Language
looks	O
like	O
this	O
:	O
</s>
<s>
In	O
monadic	O
Haskell	B-Language
,	O
return	O
is	O
the	O
standard	O
name	O
for	O
,	O
plus	O
lambda	B-General_Concept
expressions	I-General_Concept
must	O
be	O
handled	O
explicitly	O
,	O
but	O
even	O
with	O
these	O
technicalities	O
,	O
the	O
Maybe	O
monad	O
makes	O
for	O
a	O
cleaner	O
definition	O
:	O
</s>
<s>
A	O
second	O
example	O
shows	O
how	O
Maybe	O
can	O
be	O
used	O
in	O
an	O
entirely	O
different	O
language	O
:	O
F#	B-Operating_System
.	O
</s>
<s>
With	O
computation	O
expressions	O
,	O
a	O
"	O
safe	O
division	O
"	O
function	O
that	O
returns	O
None	O
for	O
an	O
undefined	O
operand	O
or	O
division	B-Algorithm
by	I-Algorithm
zero	I-Algorithm
can	O
be	O
written	O
as	O
:	O
</s>
<s>
Using	O
to	O
express	O
the	O
monadic	O
pipeline	B-Operating_System
can	O
still	O
be	O
clearer	O
in	O
many	O
cases	O
,	O
and	O
some	O
functional	B-Language
programming	I-Language
advocates	O
even	O
argue	O
that	O
since	O
block-style	O
allows	O
novices	O
to	O
carry	O
over	O
habits	O
from	O
imperative	B-Application
programming	I-Application
,	O
it	O
should	O
be	O
avoided	O
by	O
default	O
and	O
only	O
used	O
when	O
obviously	O
superior	O
.	O
</s>
<s>
Besides	O
providing	O
a	O
head-start	O
to	O
development	O
and	O
guaranteeing	O
a	O
new	O
monad	O
inherits	O
features	O
from	O
a	O
supertype	O
(	O
such	O
as	O
functors	B-Language
)	O
,	O
checking	O
a	O
monad	O
's	O
design	O
against	O
the	O
interface	O
adds	O
another	O
layer	O
of	O
quality	O
control	O
.	O
</s>
<s>
In	O
turn	O
,	O
the	O
above	O
shows	O
the	O
meaning	O
of	O
the	O
"	O
do	O
"	O
block	O
in	O
Haskell	B-Language
:	O
</s>
<s>
Identity	O
does	O
actually	O
have	O
valid	O
uses	O
though	O
,	O
such	O
as	O
providing	O
a	O
base	O
case	O
for	O
recursive	O
monad	B-Application
transformers	I-Application
.	O
</s>
<s>
It	O
can	O
also	O
be	O
used	O
to	O
perform	O
basic	O
variable	O
assignment	O
within	O
an	O
imperative-style	O
block	O
.	O
</s>
<s>
Any	O
collection	B-Application
with	O
a	O
proper	O
is	O
already	O
a	O
free	O
monoid	O
,	O
but	O
it	O
turns	O
out	O
that	O
List	O
is	O
not	O
the	O
only	O
collection	B-Application
that	O
also	O
has	O
a	O
well-defined	O
and	O
qualifies	O
as	O
a	O
monad	O
.	O
</s>
<s>
This	O
idea	O
is	O
central	O
to	O
Haskell	B-Language
's	O
IO	O
monad	O
,	O
where	O
an	O
object	O
of	O
type	O
IO	O
a	O
can	O
be	O
seen	O
as	O
describing	O
an	O
action	O
to	O
be	O
performed	O
in	O
the	O
world	O
,	O
optionally	O
providing	O
information	O
about	O
the	O
world	O
of	O
type	O
a	O
.	O
</s>
<s>
When	O
a	O
programmer	O
binds	B-Application
an	O
IO	O
value	O
to	O
a	O
function	O
,	O
the	O
function	O
computes	O
the	O
next	O
action	O
to	O
be	O
performed	O
based	O
on	O
the	O
information	O
about	O
the	O
world	O
provided	O
by	O
the	O
previous	O
action	O
(	O
input	O
from	O
users	O
,	O
files	O
,	O
etc	O
.	O
)	O
.	O
</s>
<s>
Most	O
significantly	O
,	O
because	O
the	O
value	O
of	O
the	O
IO	O
monad	O
can	O
only	O
be	O
bound	O
to	O
a	O
function	O
that	O
computes	O
another	O
IO	O
monad	O
,	O
the	O
bind	B-Application
function	O
imposes	O
a	O
discipline	O
of	O
a	O
sequence	O
of	O
actions	O
where	O
the	O
result	O
of	O
an	O
action	O
can	O
only	O
be	O
provided	O
to	O
a	O
function	O
that	O
will	O
compute	O
the	O
next	O
action	O
to	O
perform	O
.	O
</s>
<s>
For	O
example	O
,	O
Haskell	B-Language
has	O
several	O
functions	O
for	O
acting	O
on	O
the	O
wider	O
file	B-Application
system	I-Application
,	O
including	O
one	O
that	O
checks	O
whether	O
a	O
file	O
exists	O
and	O
another	O
that	O
deletes	O
a	O
file	O
.	O
</s>
<s>
The	O
second	O
function	O
,	O
on	O
the	O
other	O
hand	O
,	O
is	O
only	O
concerned	O
with	O
acting	O
on	O
the	O
file	B-Application
system	I-Application
so	O
the	O
IO	O
container	B-Application
it	O
outputs	O
is	O
empty	O
.	O
</s>
<s>
IO	O
is	O
not	O
limited	O
just	O
to	O
file	O
I/O	B-General_Concept
though	O
;	O
it	O
even	O
allows	O
for	O
user	O
I/O	B-General_Concept
,	O
and	O
along	O
with	O
imperative	B-Application
syntax	O
sugar	O
,	O
can	O
mimic	O
a	O
typical	O
"	O
Hello	O
,	O
World	O
!	O
"	O
</s>
<s>
Desugared	O
,	O
this	O
translates	O
into	O
the	O
following	O
monadic	O
pipeline	B-Operating_System
(>>	O
in	O
Haskell	B-Language
is	O
just	O
a	O
variant	O
of	O
for	O
when	O
only	O
monadic	O
effects	O
matter	O
and	O
the	O
underlying	O
result	O
can	O
be	O
discarded	O
)	O
:	O
</s>
<s>
Another	O
common	O
situation	O
is	O
keeping	O
a	O
log	B-Application
file	I-Application
or	O
otherwise	O
reporting	O
a	O
program	O
's	O
progress	O
.	O
</s>
<s>
To	O
show	O
how	O
the	O
monad	O
pattern	O
is	O
not	O
restricted	O
to	O
primarily	O
functional	B-Language
languages	I-Language
,	O
this	O
example	O
implements	O
a	O
Writer	O
monad	O
in	O
JavaScript	B-Language
.	O
</s>
<s>
First	O
,	O
an	O
array	O
(	O
with	O
nested	O
tails	O
)	O
allows	O
constructing	O
the	O
Writer	O
type	O
as	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
A	O
true	O
monad	O
still	O
requires	O
,	O
but	O
for	O
Writer	O
,	O
this	O
amounts	O
simply	O
to	O
concatenating	O
a	O
function	O
's	O
output	O
to	O
the	O
monad	O
's	O
linked	B-Data_Structure
list	I-Data_Structure
:	O
</s>
<s>
The	O
final	O
result	O
is	O
a	O
clean	O
separation	O
of	O
concerns	O
between	O
stepping	O
through	O
computations	O
and	O
logging	B-Application
them	O
to	O
audit	O
later	O
:	O
</s>
<s>
As	O
in	O
a	O
state	B-Application
monad	O
,	O
computations	O
in	O
the	O
environment	O
monad	O
may	O
be	O
invoked	O
by	O
simply	O
providing	O
an	O
environment	O
value	O
and	O
applying	O
it	O
to	O
an	O
instance	O
of	O
the	O
monad	O
.	O
</s>
<s>
Formally	O
,	O
a	O
value	O
in	O
an	O
environment	O
monad	O
is	O
equivalent	O
to	O
a	O
function	O
with	O
an	O
additional	O
,	O
anonymous	O
argument	O
;	O
and	O
are	O
equivalent	O
to	O
the	O
and	O
combinators	B-Application
,	O
respectively	O
,	O
in	O
the	O
SKI	B-Application
combinator	I-Application
calculus	I-Application
.	O
</s>
<s>
A	O
state	B-Application
monad	O
allows	O
a	O
programmer	O
to	O
attach	O
state	B-Application
information	O
of	O
any	O
type	O
to	O
a	O
calculation	O
.	O
</s>
<s>
Given	O
any	O
value	O
type	O
,	O
the	O
corresponding	O
type	O
in	O
the	O
state	B-Application
monad	O
is	O
a	O
function	O
which	O
accepts	O
a	O
state	B-Application
,	O
then	O
outputs	O
a	O
new	O
state	B-Application
(	O
of	O
type	O
s	O
)	O
along	O
with	O
a	O
return	B-Language
value	I-Language
(	O
of	O
type	O
t	O
)	O
.	O
</s>
<s>
This	O
is	O
similar	O
to	O
an	O
environment	O
monad	O
,	O
except	O
that	O
it	O
also	O
returns	O
a	O
new	O
state	B-Application
,	O
and	O
thus	O
allows	O
modeling	O
a	O
mutable	O
environment	O
.	O
</s>
<s>
Note	O
that	O
this	O
monad	O
takes	O
a	O
type	O
parameter	O
,	O
the	O
type	O
of	O
the	O
state	B-Application
information	O
.	O
</s>
<s>
Useful	O
state	B-Application
operations	O
include	O
:	O
</s>
<s>
Another	O
operation	O
applies	O
a	O
state	B-Application
monad	O
to	O
a	O
given	O
initial	O
state	B-Application
:	O
</s>
<s>
do-blocks	O
in	O
a	O
state	B-Application
monad	O
are	O
sequences	O
of	O
operations	O
that	O
can	O
examine	O
and	O
update	O
the	O
state	B-Application
data	O
.	O
</s>
<s>
Informally	O
,	O
a	O
state	B-Application
monad	O
of	O
state	B-Application
type	O
maps	O
the	O
type	O
of	O
return	B-Language
values	I-Language
into	O
functions	O
of	O
type	O
,	O
where	O
is	O
the	O
underlying	O
state	B-Application
.	O
</s>
<s>
From	O
the	O
category	O
theory	O
point	O
of	O
view	O
,	O
a	O
state	B-Application
monad	O
is	O
derived	O
from	O
the	O
adjunction	O
between	O
the	O
product	O
functor	B-Language
and	O
the	O
exponential	O
functor	B-Language
,	O
which	O
exists	O
in	O
any	O
cartesian	B-Application
closed	I-Application
category	I-Application
by	O
definition	O
.	O
</s>
<s>
It	O
is	O
used	O
to	O
model	O
continuation-passing	B-Application
style	I-Application
.	O
</s>
<s>
The	O
return	O
and	O
bind	B-Application
functions	O
are	O
as	O
follows	O
:	O
</s>
<s>
But	O
suppose	O
we	O
are	O
debugging	O
our	O
program	O
,	O
and	O
we	O
would	O
like	O
to	O
add	O
logging	B-Application
messages	O
to	O
foo	O
and	O
bar	O
.	O
</s>
<s>
and	O
a	O
logging	B-Application
message	O
with	O
information	O
about	O
the	O
applied	O
function	O
and	O
all	O
the	O
previously	O
applied	O
functions	O
as	O
the	O
string	O
.	O
</s>
<s>
Unfortunately	O
,	O
this	O
means	O
we	O
can	O
no	O
longer	O
compose	B-Application
foo	O
and	O
bar	O
,	O
as	O
their	O
input	O
type	O
int	O
is	O
not	O
compatible	O
with	O
their	O
output	O
type	O
int	O
*	O
string	O
.	O
</s>
<s>
bind	B-Application
takes	O
in	O
an	O
integer	O
and	O
string	O
tuple	O
,	O
then	O
takes	O
in	O
a	O
function	O
(	O
like	O
foo	O
)	O
that	O
maps	O
from	O
an	O
integer	O
to	O
an	O
integer	O
and	O
string	O
tuple	O
.	O
</s>
<s>
In	O
this	O
way	O
,	O
we	O
only	O
need	O
to	O
write	O
boilerplate	O
code	O
to	O
extract	O
the	O
integer	O
from	O
the	O
tuple	O
once	O
,	O
in	O
bind	B-Application
.	O
</s>
<s>
To	O
make	O
the	O
benefits	O
even	O
clearer	O
,	O
let	O
us	O
define	O
an	O
infix	O
operator	O
as	O
an	O
alias	O
for	O
bind	B-Application
:	O
</s>
<s>
So	O
that	O
t	O
>>=	O
f	O
is	O
the	O
same	O
as	O
bind	B-Application
t	O
f	O
.	O
</s>
<s>
Finally	O
,	O
it	O
would	O
be	O
nice	O
to	O
not	O
have	O
to	O
write	O
(	O
x	O
,	O
""	O
)	O
every	O
time	O
we	O
wish	O
to	O
create	O
an	O
empty	O
logging	B-Application
message	O
,	O
where	O
""	O
is	O
the	O
empty	O
string	O
.	O
</s>
<s>
Now	O
we	O
have	O
a	O
nice	O
pipeline	B-Operating_System
for	O
logging	B-Application
messages	O
:	O
</s>
<s>
bind	B-Application
and	O
return	O
are	O
analogous	O
to	O
the	O
corresponding	O
functions	O
of	O
the	O
same	O
name	O
.	O
</s>
<s>
In	O
fact	O
,	O
int	O
*	O
string	O
,	O
bind	B-Application
,	O
and	O
return	O
form	O
a	O
monad	O
.	O
</s>
<s>
Just	O
as	O
a	O
free	O
monoid	O
concatenates	O
elements	O
without	O
evaluation	O
,	O
a	O
free	O
monad	O
allows	O
chaining	O
computations	O
with	O
markers	O
to	O
satisfy	O
the	O
type	O
system	O
,	O
but	O
otherwise	O
imposes	O
no	O
deeper	O
semantics	B-Application
itself	O
.	O
</s>
<s>
A	O
free	O
monad	O
can	O
be	O
used	O
to	O
track	O
syntax	O
and	O
type	O
while	O
leaving	O
semantics	B-Application
for	O
later	O
,	O
and	O
has	O
found	O
use	O
in	O
parsers	B-Language
and	O
interpreters	B-Application
as	O
a	O
result	O
.	O
</s>
<s>
Others	O
have	O
applied	O
them	O
to	O
more	O
dynamic	O
,	O
operational	O
problems	O
too	O
,	O
such	O
as	O
providing	O
iteratees	B-Application
within	O
a	O
language	O
.	O
</s>
<s>
Monadic	O
code	O
,	O
in	O
a	O
sense	O
,	O
cannot	O
be	O
fully	O
"	O
unpacked	O
"	O
;	O
once	O
a	O
value	O
is	O
wrapped	O
within	O
a	O
monad	O
,	O
it	O
remains	O
quarantined	O
there	O
along	O
with	O
any	O
side-effects	O
(	O
a	O
good	O
thing	O
in	O
purely	B-Application
functional	I-Application
programming	I-Application
)	O
.	O
</s>
<s>
Analogous	O
to	O
monads	O
,	O
comonads	O
can	O
also	O
be	O
derived	O
from	O
functors	B-Language
using	O
a	O
dual	O
of	O
:	O
</s>
<s>
While	O
operations	O
like	O
are	O
reversed	O
,	O
however	O
,	O
a	O
comonad	O
does	O
not	O
reverse	O
functions	O
it	O
acts	O
on	O
,	O
and	O
consequently	O
,	O
comonads	O
are	O
still	O
functors	B-Language
with	O
,	O
not	O
cofunctors	O
.	O
</s>
<s>
In	O
fact	O
,	O
while	O
not	O
as	O
popular	O
as	O
monads	O
,	O
researchers	O
have	O
found	O
comonads	O
particularly	O
useful	O
for	O
stream	B-Application
processing	I-Application
and	O
modeling	O
dataflow	B-Application
programming	I-Application
.	O
</s>
<s>
As	O
an	O
even	O
higher	O
abstraction	O
,	O
arrows	B-Application
can	O
subsume	O
both	O
structures	O
,	O
but	O
finding	O
more	O
granular	O
ways	O
to	O
combine	O
monadic	O
and	O
comonadic	O
code	O
is	O
an	O
active	O
area	O
of	O
research	O
.	O
</s>
