Functional Programming Type: An Example: Maybe

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

AAAAAAS

In functional programming, a monad is a type that wraps another type and gives some form of
quality to the underlying type. In addition to wrapping a type, monads define two functions: one to
wrap a value in a monad, and another to compose together functions that output monads (these
are known as monadic functions). General-purpose languages use monads to abstract away
boilerplate code needed for common operations (such as dealing with undefined values or fallible
functions). Functional languages use monads to turn complicated sequences of functions into
[1][2]
succinct pipelines that abstract away control flow, and side-effects.
Both the concept of a monad and the term originally come from category theory, where a monad
[a]
is defined as a functor with additional structure. Research beginning in the late 1980s and early
1990s established that monads could bring seemingly disparate computer-science problems
under a unified, functional model. Category theory also provides a few formal requirements,
known as the monad laws, which should be satisfied by any monad and can be used to verify
[3][4]
monadic code.
Since monads make semantics explicit for a kind of computation, they can also be used to
implement convenient language features. Some languages, such as Haskell, even offer pre-built
[1][5]
definitions in their core libraries for the general monad structure and common instances.
Contents
● 1
● Overview
○ 1.1
○ An example: Maybe

○ 1.2
○ Definition

○ 1.3
○ Usage

● 2
● Applications

● 3
● History

● 4
● Analysis
○ 4.1
○ Verifying the monad laws

○ 4.2
○ Derivation from functors

○ 4.3
○ Another example: List

● 5
● Techniques
○ 5.1
○ Syntactic sugar do-notation

○ 5.2
○ General interface

○ 5.3
○ Operators

● 6
● Variations
○ 6.1
○ Additive monads

○ 6.2
○ Free monads

○ 6.3
○ Comonads

● 7
● More examples
○ 7.1
○ Identity monad

○ 7.2
○ Collections

○ 7.3
○ IO monad (Haskell)

○ 7.4
○ Writer monad (JavaScript)

○ 7.5
○ Environment monad

○ 7.6
○ State monads

○ 7.7
○ Continuation monad

○ 7.8
○ Program logging

● 8
● See also

● 9
● Notes

● 10
● References

● 11
● External links

Overview[edit]
"For a monad m, a value of type m a represents having access to a value of type a within the
[6]
context of the monad." —C. A. McCann
A monad can be created by defining a type constructor M and two operations: return (often
also called unit), which receives a value of type a and wraps it into a monadic value of type m a,
using the type constructor; and bind(typically represented as >>=), which receives a function f
over type a and can transform monadic values m a applying f to the unwrapped value a. (An
alternative but equivalent construct using the join function instead of the bind operator can be
found in the later section § Derivation from functors.)
With these elements, the programmer composes a sequence of function calls (a "pipeline") with
several bind operators chained together in an expression. Each function call transforms its input
plain-type value, and the bind operator handles the returned monadic value, which is fed into the
next step in the sequence.
Between each pair of composed function calls, the bind operator >>= can inject into the monadic
value m a some additional information that is not accessible within the function f, and pass it
along down the pipeline. It can also exert finer control of the flow of execution, for example by
calling the function only under some conditions, or executing the function calls in a particular
order.

An example: Maybe[edit]
One example of a monad is the Maybe type. Undefined null results are one particular pain point
that many procedural languages don't provide good tools for dealing with. See anti-pattern. This
causes bugs and makes it harder to build robust software that gracefully handles errors. The
Maybe type forces the programmer to deal with these potentially undefined results by explicitly
defining the two states of a result: Just ⌑result⌑, or Nothing. For example the programmer
might be constructing a parser, which is to return an intermediate result, or else signal a condition
which the parser has detected, and which programmer must also handle. With just a little extra
[b]: 12.3 pages
functional spice on top, this Maybe type transforms into a fully-featured monad.
148-151 

In most languages, the Maybe monad is also known as an option type, which is just a type that
marks whether or not it contains a value. Typically they are expressed as some kind of
enumerated type. In this Rust example we will call it Maybe<T> and variants of this type can
either be a value of generic type T, or the empty variant: Nothing.

You might also like