Monad (functional Programming)

Monad (functional Programming)

In functional programming, a monad is a structure that represents computations. A type with a monad structure defines what it means to chain operations of that type together. This allows the programmer to build pipelines that process data in steps, in which each action is decorated with additional processing rules provided by the monad. A pithy description, referring to the syntax for statements in several languages, is that monads are a "programmable semicolon"—that is, a monad specifies what a statement is.

Purely functional programs can use monads to structure procedures that include sequenced operations. Many common programming concepts can be described in terms of a monad structure, including side effects such as input/output, variable assignment, exception handling, parsing, nondeterminism, concurrency, and continuations. This allows these concepts to be defined in purely functional manner, without major extensions to the language's semantics. Because a monad can insert additional operations around a program's domain logic, monads can be considered a sort of aspect-oriented programming.

Formally, a monad consists of a type constructor M and two operations, bind and return. The operations must fulfill several properties to allow the correct composition of monadic functions (i.e. functions that use values from the monad as their arguments or return value). In most contexts, a value of type M a can be thought of as an action that returns a value of type a. The return operation takes a value from a plain type a and puts it into a monadic container of type M a; the bind operation chains a monadic value of type M a with a function of type a → M b to create a monadic value of type M b, effectively creating an action that chooses the next action based on the results of previous actions.

The name is taken from the mathematical monad construct in category theory, though the two concepts are not identical.

Read more about Monad (functional Programming):  History, Background and Notational Conventions, Motivating Examples, Formal Definition, Do-notation, Generic Monadic Functions, Fmap and Join, Comonads