Category:Monads: Difference between revisions

Slight reorganisation of the overview text
No edit summary
(Slight reorganisation of the overview text)
Line 1:
{{draft task}}
In functional programming, the [[wp:Monad_(functional_programming)|Monad]] pattern is a general solution to the problem of nesting (or 'composing') functionsa whenclass theof data tofunctions which theyenclose applytheir isoutput enclosedvalues in some kind of useful wrapping. ItThe involvesoutput implementingenvelope twomight, higher-orderfor functionsexample, whichcontain, betweenin themaddition to the returned value, cana log string, or a takeboolean careindicator of ensuringwhether thator not the nestedinput (data-transforming)was functionsa arelegal notvalue. chokedSometimes bythe beingoutput calledmight onsimply unexpectedbe typesenclosed ofin data.a (Wrappedlist data,representing whena range of theypossible werevalues expectingrather somethingthan rawa andsingle unwrapped)value.
 
Functions of this type can not be directly nested with each other, because their output type (wrapped) does not match their input type (raw and unwrapped).
The two higher-order functions which make up the monad pattern handle the details of: 1. wrapping data, and 2. Providing other functions with direct access to the unwrapped data contents. Delegating the mechanics to these two meta-functions allows the programmer to work with a simple and well-understood generic model, and to nest functions transparently.
 
The monad pattern consists of writing two higher-order functions which allow the programmer to easily nest the application of such functions, by abstracting out the mechanics, and making sure that a function does not choke on an unexpected input type (a wrapped type, when it was expected a raw type).
The two monad functions are sometimes named as follows:
 
More specifically, the two higher-order functions of the monad pattern handle the details of: 1. wrapping data in a particular kind of envelope, and 2. Providing other functions with direct access to the contents of an enclosing envelope.
# 'Return' or 'unit' which wraps a piece of raw data, returning the wrapped 'monadic' form.
 
# 'Bind' which applies some other function directly to the contents of a monadic wrapper, obtains a result, and returns a wrapped form of that result.
TheThese two monad functions are sometimes named as follows:
 
# '''Return''' or 'unit''Unit''', which wraps a piece of raw data, returning the wrapped 'monadic' form.
# '''Bind''', which applies some other function directly to the contents of a monadic wrapper, obtains a result, and returns a wrapped form of that result.
 
 
(The term monad derives from [[wp:Monad_(category_theory)|a concept in category theory]]. In ancient Greek the word μοναδικος means 'consisting of units').
 
Commonly used monads:
Commonly used monads include the Maybe monad, (in which the wrapper encodes whether or not the raw content is a legal value for a particular type of function), and the List monad, in which raw data is simply contained in a list. When lists are used to represent a range of possible values for a variable name, nesting functions which act on these lists allows a convenient encoding of cartesian products and set comprehensions. In this context, the two higher order monad functions ensure that each data-transforming function (in a nest or composition of such functions) gets the right kind of argument (Raw atomic values versus one or more values 'wrapped in' a list).
;the Writer monad
:Nests functions which return their output in an envelope that includes a log string.
;the Maybe monad
:Nest partial functions which return their output in a wrapper that includes a boolean flag – indicating whether or not the input value was legal.
;the List monad
:Nests functions whose outputs consist of ranges of possible values, rather than single values. This provides a convenient encoding of cartesian products and set comprehensions.
 
 
(Other frequently used monads are the Writer (or Logging) monad, the IO monad, and the State monad)
9,655

edits