4
$\begingroup$

I understand how monadic computation works, I am just wondering where does the name come from. I cannot relate the thing that the bind operator actually does (i.e. unbox the monadic value, and then apply a function to produce another monadic value) and the name bind (which, to me, means associate two things together).

I understand why we have the name return (I suspect it has to do with the Haskell do-notation), although IMHO it isn't a good name either. Return indicates a change in control flow but it really isn't, all it does is produce a monadic value.

$\endgroup$
  • 1
    $\begingroup$ If it helps, other languages use other names. Ive seen flatMap and SelectMany before too. $\endgroup$ – D. Ben Knoble Apr 17 at 23:46
5
$\begingroup$

In Haskell the syntactic sugar for bind e₁ (λ x . e₂) is

do x ← e₁
   e₂

That is, the result of the computation e₁ is bound to x, after which e₂ is computed.

In ML-like languages the same thing is written as

let x = e₁ in e₂ 

which again is a form of binding a variable to a value.

As for return being a control-flow mechanism – that's precisely what it is. You are probably thinking of control mechanisms for branching, looping, raising exceptions etc. Well, return is the one that ends a computation and yields a result.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Caution: Unlike imperative settings where a return causes immediate end of computation, in Haskell and in monads in-general, a return in the middle is a form of local-naming ;-) $\endgroup$ – Musa Al-hassy Apr 19 at 17:20
  • $\begingroup$ @MusaAl-hassy: The return we are discussing here is not the return from Python, C, Java, etc., so I do not what you're trying to say. You're mixing oranges and apples, I think. The return from a monad is something like "pure value" in imperative languages, not returning from a subroutine. $\endgroup$ – Andrej Bauer Apr 19 at 18:04
4
$\begingroup$

In λ-calculus, the term $\lambda x. y$ binds $x$ in $y$. Once a value is given to $x$, any well-scoped reference to $x$ in $y$ will be replaced by that same value.

Function composition $(f\circ g)$ can be seen as an operator that binds the result of $g$ to $f$'s argument. In a similar way, you can view bind : Monad m => m a -> (a -> m b) -> m b as an operator that binds the contents (the value underneath the monadic wrapper) of its first argument to the function supplied as its 2nd argument (a -> m b).

What if the 1st argument of type m a is the result of some previously applied function? In general, you can make consecutive applications of bind to mimic something like function composition, but under a monad.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.