On the signature of >>= Monad operator

后端 未结 6 2340
粉色の甜心
粉色の甜心 2021-02-14 13:54

This is the signature of the well know >>= operator in Haskell

>>= :: Monad m => m a -> (a -> m b) -> m b

The question is why

6条回答
  •  渐次进展
    2021-02-14 14:28

    I had the same question for a while and was thinking why bother with a -> m b once mapping a -> b to m a -> m b looks more natural. This is simialr to asking "why we need a monad given the functor".

    The little answer that I give to myself is that a -> m b accounts for side-effects or other complexities that you would not capture with function a -> b.

    Even better wording form here (highly recommend):

    monadic value M a can itself be seen as a computation. Monadic functions represent computations that are, in some way, non-standard, i.e. not naturally supported by the programming language. This can mean side effects in a pure functional language or asynchronous execution in an impure functional language. An ordinary function type cannot encode such computations and they are, instead, encoded using a datatype that has the monadic structure.

    I'd put emphasis on ordinary function type cannot encode such computations, where ordinary is a -> b.

提交回复
热议问题