Understanding Sequencing in Functional Programming

I am basically a practical guy, but I find this interesting.

I was thinking about monadic sequencing, and there are a few things I need to clarify. Thus, at the risk of seeming stupid here is this:

Monadic member binds

bind :: mb -> (b -> mc) -> mc

can perform a sequence of actions, giving you explicit access to intermediate values.

As this gives me more than a categorical member (.) :

(.) :: cat bc -> cat ab -> cat ac

With this, I can sequentially access intermediate values. In the end (f . g) x = f(g (x)) .

Why do I need bind for sequencing if I can execute a sequence with (.) ?

+6
source share
1 answer

You are on the right way. Each monad gives rise to the so-called Kleisli category . For each monad m corresponding Kleisli category has arrows a -> mb , and they can be composed using > => , which is defined as

 f >=> g = \x -> fx >>= g 

The Kleisli type encapsulates this in a Haskell type system, you can see that it has an instance

 instance Monad m => Category (Kleisli m) where id = Kleisli return (Kleisli f) . (Kleisli g) = Kleisli (g >=> f) 

Thus, sequence calculations in this category are simply sequencing operations using >=> , which can be expressed equivalently using >>= .

We define monads with return and >>= because it is more convenient, but we could also define them with return and >=> if we wanted to.

(See also my answer to Various ways to see a monad .)

+14
source

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


All Articles