Talk:Monads/Maybe monad: Difference between revisions
Content added Content deleted
(→Monad Description: Defining Just and Nothing, for example, doesn't give us a monad.) |
|||
Line 31: | Line 31: | ||
::::More concretely, defining Just and Nothing, Left and Right, or Lists, for example, gives us a type but does not give us a monad. |
::::More concretely, defining Just and Nothing, Left and Right, or Lists, for example, gives us a type but does not give us a monad. |
||
::::The monad instance for a type consists of well-formed monadic functions (bind, and return/pure) which enable monadic enchaining of computations *embedded in that type*. List is not a monad, nor are the Maybe or Either types in themselves. The List, Maybe and Either monads consists of the bind and return function instances written for each of those types. [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 23:02, 2 October 2019 (UTC) |
::::The monad instance for a type consists of well-formed monadic functions (bind, and return/pure) which enable monadic enchaining of computations *embedded in that type*. List is not a monad, nor are the Maybe or Either types in themselves. The List, Maybe and Either monads consists of the bind and return function instances written for each of those types. [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 23:02, 2 October 2019 (UTC) |
||
===Some notes=== |
|||
A monad consists of a pair of functions (specialised for a particular data-type or other computational context), which simplify the pipelining of computations which are wrapped in that context. |
|||
The two functions are sometimes called return and bind, or pure and bind. |
|||
* pure/return simply wraps a raw value in a context (for example, wraps a number in a list). |
|||
* bind takes two arguments: (1) a wrapped or context-embedded value, and (2) a function which applies to a raw value but returns a wrapped value. |
|||
When applied, bind takes care of the mechanics of: |
|||
# Extracting the raw value from the enclosing context, |
|||
# Applying the supplied function to it, and returning the wrapped output value. |
|||
To use the metaphor of postal correspondence, pure / return places the letter in an envelope. |
|||
Bind assists the writer by discarding the envelope, and offering its contents to the reader for perusal and digestion. |
|||
(The writer (or supplied write-and-wrap function) drafts a response, and places it in a new envelope, continuing the chain of correspondence) |
|||
The data-type or context (the envelope) is not the monad. The monad is the envelope-type-specific pair of functions which abstract away the boiler-plate required wrapping and unwrapping, to facilitate a chain of envelope-wrapped computations. |