User:Metaquanta/sandbox

An example: Maybe
To motivate programming with monads, here is a quick pseudocode example. Undefined values or operations are one particular problem that robust software should prepare for and handle gracefully.

The first step toward this goal might be to create an option type that will mark a value as either carrying a value of some type  (  can be any type) or carrying no value. The new type will be called  and values of that type can either contain a value of type , or be the empty value. A value  of type   which is defined but used in the context of   will be called. This is done to avoid confusion by differentiating between cases where a variable carries a defined value and those where it does not.

can be understood as a "wrapping" type, wrapping the type  into a new type with built-in exception handling, though carrying no information about the cause of an exception.

In the following code, variables prefixed with  have the type   for some type. For example, if a variable  contains a value, it is , where the variable   has the type. is an anonymous function with the parameter  whose type is inferred, and   is the function composition operator.

Another improvement would be if a function could manage simple checked exceptions with a  type, short-circuiting and returning   once a step fails, but returning the correct value without comment if a calculation succeeds.

An addition function, which does exactly this when adding two   values,   and  , can be defined like so:

Writing functions that process  values case-by-case can be tedious though, and will only become more so as more functions are defined. An operation to chain steps together is one way to alleviate this, and with an infix operator like, it can even intuitively represent feeding the (possibly undefined) result from each step into the next. Since each result is technically inserted into another function, however, the operator will take a function for a parameter. As  already specifies the type of its output value, it should not hurt to keep the operator flexible and accept functions that output different types from their input:

With  available,   can now be redefined as something much more compact:

This is more concise, but a little extra analysis reveals something even more powerful. For one, the only role that  plays in   is to tag an underlying value as also being a   value. To emphasize how  acts on the underlying value by wrapping it, it can be redefined as a function too, called   for now:

The big picture is that these two functions  and   were designed to simplify , but they clearly do not depend on the specifics of   in any way, just the   type. These functions can, in fact, apply to any values and functions of the  type, regardless of the underlying values' types. For example, here is a concise NOT operator from (Kleene's) trinary logic that uses the same functions to automate undefined values too:

It turns out the  type, together with   and , forms a monad. While other monads will embody different logical processes, and some may have extra properties, all of them will have three similar components (directly or indirectly) that follow the basic outline of this example.