r/functionalprogramming • u/quadaba • Feb 26 '25
FP Is it right: monads and algebraic effects are essentially ways to "override control flow (function calling and variable assingment)"?
At some point in the past I was reading about algebraic effects and how one could implement them using coroutines or continuation (eg in python); at another time I was reading that they were equivalent to monads. I was looking at the with block in Bend and decided to double check my understanding with you folks.
Is it true that all three (algebraic effects, monads, continuations) provide a way to add "custom logic" at every variable assignment or function call site - is that correct? Basically a way to add a custom wrapper logic around each call / override what it means to call a function? Kind of how we can override what operators or functions mean, but one abstraction level up - we are now overriding program control flow / "how function calls are applied and values are assigned"
Eg if we had a = f(b, c) wrapped in an effect handler or inside a monadic expression, that'd add extra custom logic before and after computing the value before assingment. All of the examples below could be implemented in python as we if all fn calls in a block were in the form a = yield (f, b, c) and the next caller implemented the corresponding logic (below), and some additional logic was applied when exiting the loop.
Some examples supporting this understanding:
- option: at each call site, check if arguments are Some, if so, if any of them are None, do not call the function and return None;
- exception: same thing, but we can define multiple types of "failure" return values beyond None + handlers for them that the added wrapper calls exactly once;
- async/await: at every call site, check if the returned value is not the type itself but an "awaitable" (callback) with that type signature; if yes, (start that computation if your coros are lazy), store current exec in a global store, set up a callback, and yield control to the event loop; once the callback is called, return from the effect handler / yield / bind back to the control flow;
- IO and purity: at every call site collect for each argument "lists of non-pure io ops" (eg reads and writes) required to be executed to compute all fn call arguments, merge it with with io ops generated by the function call itself, and attach to the return value eg return an object Io(result, ops) from the wrapper; the resulting program is a pure fn + a list of io ops;
- state: same thing, but instead of a list of io ops, these are a list of (get key/set key value) ops that the wrapper logic needs to "emulate" and pass back into the otherwise pure stateless function call hierarchy.
Is that right?
10
u/tisbruce Feb 26 '25 edited Feb 26 '25
No. There's no special control flow "overriding" going on there, just polymorphism and pattern matching. These are basic features of the Bend language, not special magic for monads, and you can use them in code that has nothing to do with monads. Pattern matching in Bend (and other languages that offer it) is a control structure, yes, but it doesn't "override" control flow any more than if/then; it defines a control flow structure.
If you want to call polymorphism and pattern matching "control flow overriding", fair enough. But
The fact that to create a generalised implementation of algebraic effects and monads requires code that does different things for different types and values is true, but it isn't interesting. It's true of any generalised abstraction.
8
u/faiface Feb 26 '25
I would say this is correct. Monads, algebraic effects, and continuations all do it differently, and have different capabilities, but say if we’re talking about monads, we can be talking about overriding the bind operation. Then when constructing a monadic value, binding is an operation that looks the same across monads, but each monad gives it a different control-flow.
5
u/WittyStick Feb 26 '25 edited Feb 26 '25
Monads just do sequencing.
Takes an argument which is some value in a monad, passes the value from the monad to a function which produces a new value in the same monad, and that value becomes the result of the bind expression, and with that result, we usually just pass it to some other computation in the same monad - ie, we call bind again, and again, in sequence.
foo,barandbazdo the actual computation. We can think of monads as "computation builders", which take these individual means of computation, andbindcombines them in sequence to construct a computation which represents their composition.Take for example the
Asynccase.Given some asynchronous computation which eventually produces a value
a, we can bind it to another computation which receives the eventual valueaand performs some new asynchronous computation on it, usingreturnto construct the asynchronous computation. Bind returns an asynchronous computation which represents the two occuring in sequence. It doesn't perform the asynchronous computations - it provides a value which represents the sequence of them.On a side note,
async/awaitin C# is actually a comonad, which has a different property - theTaskrepresents a computation that may already be running andcobindexpands it to do more computations, but each computation essentially blocks/pauses/suspends until we can eventually extract the value from the computation to give to the next.Essentially, we have some task, and we can use
cobindto pass it to a computation which takes the task itself as the argument and eventually this computation produces a value, and the result ofcobindrepresents this expanded computation. To receive the eventual value from the computation, we usecoreturn.In C#
coreturn:
T Task<T>.GetAwaiter().ResultieTask<T> -> Tcobind:
Task<T> Task<T>.ContinueWith(Func<Task<T>,T>)ieTask<T> -> (Task<T> -> T) -> Task<T>2
u/FabulousRecording739 Feb 26 '25
I'm mostly self-taught when it comes to functional programming, so take the following with a grain of salt;
Control flow, assignment, function calling, call site, etc. Those are imperative terms which I don't think help much in understanding monads and algebraic effects. These might be useful to understand how the machine ultimately execute our programs, but they are neither necessary nor sufficient to grasp what monads, and thus algebraic effects, are.
A functor represent a form of context within which a value lies. Monads are those functors that also are monoids, such that they can compose. When dealing with a functor (and thus a monad), we need to deal with that which is not a value. How we do so is somewhat arbitrary, it depends on the given expression we mean to build.
It happens quite often that we have "nested" monads, which means that we need to deal with multiple overlapping contexts/effects. Before effect system came around, we would use monad transformer to define those nested monads in a way that wasn't as verbose. Because any monad can be combined with any other monad, we end up with an explosion of possible combinations. Effect systems arrived and generalized that combination concept in a way that is more ergonomic.
Effect systems and monads are not equivalent, effect systems use monads, monad don't use (nor need) effect systems.
What I see in common between all your examples are the functor boundaries. If I have a Maybe, I'll have at some point to deal with the fact that the value might not be there, the effect that Maybe portray. Failures are a specific case of biased either, one side has the value that I want, the other carries a semantically meaningful reason as to why the value isn't there. The case for asynchronous computation under the lens you have does not really have a meaningful monadic equivalent. Future gets close but what a future tells you is that there might be a value now, or there will be a value in the Future. How the Future is evaluated is, again, somewhat irrelevant. Could be a single-threaded runtime, or a multithreaded one. Doesn't really matter to the semantics.
I think you get the picture. In each of your example, the call site (the "extra logic") you're thinking of is the place where we deal with the semantics of the monad at hand, the boundary between our nice world of values and that which is not a value, the effect. Wheter the machine uses, or does not use, control flow to do that is irrelevant to us.
--
Continuation is another topic altogether. The only reason I see them as bound to the concept of monad is either if you are activally using a monad that represent it, or if you see the map/bind operations as a form CPS/callback. But that's a rather barbaric way to think of what those operations are.
0
u/Tempus_Nemini Feb 26 '25
Not a programmer myself, but I would agree with this opinion. Prefer "custom logic" 😉