Are monads just a way of composing functions that otherwise could not have been compiled?

The snap function seems very similar, like a composition function. And it helps in compiling functions that return monads.

Is there anything more interesting about monads than this idea?

+6
source share
2 answers

Is there anything more interesting about monads than this idea?

Yes very!!

Monadic binding is a way of linking functions when something else happens over the application of the function to the input. Something else depends on the monad in question.

  • Maybe monad is a composition of functions with the possibility of failure of one of the functions in the chain, in which case the failure automatically propagates to the end of the chain. The expression return x >>= f >>= g applies f to the value of x . If the result is Nothing (i.e., Failure), then the whole expression returns Nothing , without any other work. Otherwise, g is applied to fx and its result is returned.

  • The Either e monad, where e is some type, is an integral part of the function with the possibility of failure with an error of type e . This is conceptually similar to the Maybe monad, but we get more information about how and where the failure occurred.

  • List monad is a composition of functions with the ability to return multiple values. If f and g are functions that return a list of outputs, then return x >>= f >>= g applies f to x , and then applies g to each output f , collecting all the outputs of these applications together into one big list.

Other monads are a composition of functions in various contexts. Very briefly:

  • The Writer w monad is a functional composition with a value of type w accumulated on the side. For example, often w = [String] (a list of strings), which is useful for logging.

  • The Reader r monad is a composition of functions, where each function can also depend on a value of type r . This is useful when creating evaluators for domain languages, when r can be a map from variable names to values โ€‹โ€‹in the language - this allows, for example, to simplify the implementation of lexical closures.

  • The State s monad is a bit like a reader and writer combination. This is a function in which each function is allowed to hang and change a value of type s .

+15
source

The compositional point of view is actually enlightening in itself.

Monads can be considered as part of a "funky composition" between functions of the form a -> Mb . You can compose f : a -> M b and g: b -> M c into something a -> M c through monad operations (just bind the return value of f to g ).

This turns the arrows of the form a -> M b category arrows, called the Claysley category M

If M were not a monad, but just a functor, you could compose fmap g and f into something (fmap g) . f :: a -> M (M c) (fmap g) . f :: a -> M (M c) . Monads have join :: M (M a) -> M a , which I allow you to define as a (simple and useful) exercise using only monad operations (for mathematicians, join usually part of the monad definition). Then join . (fmap g) . f join . (fmap g) . f join . (fmap g) . f gives a composition for the Claysley category.

Thus, you can see the whole funky monadic composition inside join , join represents side effects composition: for IO it exposes effects sequentially, for List it combines lists, for Maybe it โ€œstops the calculationโ€ when the result is Nothing , for Writer it writes sequentially, for State it sequentially performs operations on the state, etc. It can be thought of as an "overloaded semicolon" if you know C-like languages. It is very instructive to think of monads in this way.

Of course Dan Piponi explains this a lot better than I do, and here is his post that you can find out: http://blog.sigfpe.com/2006/06/monads-kleisli-arrows-comonads-and.html

+6
source

Source: https://habr.com/ru/post/917240/


All Articles