r/functionalprogramming • • Aug 23 '20

CompSci Which side effects cannot be represented as monads?

During an introductory Uni course about category theory the lecturer said that monads were incorporated into functional languages because they could represent side effects. However, he also remarked that not all possible side effects can be represented as monads (even though many common ones, like I/O, can).

Unfortunately, he did not elaborate this topic and I did not ask.

So let me ask it here: Which side effects cannot be represented as monads?

35 Upvotes

16 comments sorted by

9

u/im_caeus Aug 24 '20 edited Aug 24 '20

Monads do not represent side effects, but the sequential concatenation of them.

Again, monads only represent sequential concatenation of side effects. Any other capability that is not sequential concatenation of effects, is not represented by monads (ie error handling, parallelism, transactionality)... The IO Monad of Haskell is only a Monad in that it can chain IO effects, but so is the List type, a function, the maybe type, and almost every covariant higher kind.

Any other thing that you do that is not chaining of operations, is not part of the monadic properties of any type.

3

u/antonivs Aug 24 '20

error handling

What do you mean here? E.g., the Haskell Control.Monad.Except module provides exception handling via monads, and the core implemention is extremely simple, because a "sequential concatenation of [actions]" can be interrupted simply by a bind implementation that exits when an exception occurs.

Something similar goes for transactionality - a single-threaded transaction can certainly be implemented with a monad, there's a Haskell example in the monad-tx package.

Of the items you listed, only parallelism (including parallel transactions) requires support beyond the monadic model, such as language-provided or OS threads. Of course monads can still be used to package such functionality, as seen in Control.Monad.Parallel, STM, or A monad for deterministic parallelism.

7

u/im_caeus Aug 24 '20

This things are implemented in the IO type and others (like the Either) but are not part in the monadic properties of those types. That there's an try function that transforms an IO a into IO (Either Exception a) doesn't mean the try function is part of the Monad of IO (it's not)

2

u/Toricon Aug 24 '20

Yes. If it were part of the monad, it would have type a -> IO b for some a and b, rather than IO a -> IO b, like it acually does.

2

u/antonivs Aug 24 '20

Just to clarify, I referenced Control.Monad.Except, which doesn't use GHC's try and doesn't depend on the IO monad.

But I see your point, thanks. catchError (which is its equivalent of try) does have a type signature m a -> (e -> m a) -> m a. So the actual handling of the error is not part of the monad.

5

u/zesterer Aug 23 '20

Monads of different kinds don't really compose well. It's fairly difficult to do monadic mutability and monadic IO at the same time.

4

u/lightandlight Aug 24 '20

I don't believe it's true that "not all possible side effects can be represented as monads". Monadic semantics doesn't seem like it has such a limitation.

0

u/[deleted] Aug 24 '20

[deleted]

3

u/Patsonical Aug 24 '20

What are coeffects (ELI5)?

2

u/Aphrontic_Alchemist Jun 07 '25

Coffects are:

  • also known as the contexts of computations, and

  • handled by comonads.

This is in contrast with its dual, effects, which are:

  • also known as the side effects of computations, and

  • handled by monads.

Basically, coffects affect the input of functions, whereas effects affect the output of functions.

A simple example is a function possibly dropping its input, which the Drop Comonad handles. Its dual in linear logic is a function possibly dropping its output, which the Maybe monad handles.

2

u/lightandlight Aug 24 '20

Coeffects briefly crossed my mind, but I can't think of any that one might reasonably confuse with "side effects". Can you?

4

u/gasche Aug 24 '20 edited Aug 24 '20

The notion of "side effect" is not defined precisely, so some people might equate "side effect" with "stuff we can represent as a monad", or some stricter or larger definition.

One way to answer your question is to look for algebraic structures that have been considered to represent side effects, and are weaker than monads (in the sense than not all such structures are monads). Applicative functors are one candidate: they represent a more rigid notion of side-effect than monads, and some applicative functors are not monads. If you can view of of those examples as a side-effect, you have a side-effect that is not a monad.

The standard example of applicatives-that-are-not-monad is the ZipList applicative structure on lists (coming from the 2007 paper by Conor McBride and Ross Patterson that introduced applicatives), where [a] may be taken as representing a stream of a (think: one element at each global timestamp), for which parallel pairing/zipping is available but not the cartesian product of elements.

Recently I encountered another applicative functor that is not a monad in the Inferno library for constraint-based type inference. Inferno's applicative functor 'a co represents a value of type 'a under the effect of generating and solving constraints, such that 'a is built with the global knowledge of the solution to all constraints passed to the solver at the same time. This is not a monad, because implementing bind : a co -> (a -> b co) -> b co would require generating a a to pass to the function argument while the b co computation is not yet known, and in particular its constraints have not been generated, so the global solution is not yet known.

7

u/drninjabatman Aug 24 '20

Probably things like processor heating. But there is a wider point to be made here: the fundamental thing that monads (qua monads) represent in this context is /sequence/ of side effects, not side effects as such. Each different monad can represent some side effects and not others or no side effects at all. What they all have in common is that they represent operations that are applied in sequence and the order of that sequence matters.

I think it's easier to understand the other way round: you find a type that represents an action with a side effect, eg IO a represents a program that when executed will return a value of type a. If a) it makes sense that you two of those actions can be done one after the other (run one program then the other) and b) there is a trivial such action (eg noop), then you are probably dealing with a monad.

To maybe make the point clearer, even if we didn't know that IO was a monad, we could still talk about it representing side effects. We would just be unable to chain those side effects one after the other.

3

u/szpaceSZ Aug 24 '20

Something like heat given off by your CPU or sounds produced by your spinning up fan.

2

u/gasche Aug 24 '20

Those would just be Writer monads if you wanted to represent them explicitly in your semantics. (We usually prefer to model the system without taking into account, but it doesn't mean that we cannot represent them.)

2

u/szpaceSZ Aug 24 '20

But you cannot explicitly control them in your program, they are inherent random side effects that just happen.

2

u/gasche Aug 24 '20

You can decide to print things to output buffers or not, you can decide to run costly (heat-producing, noise-producing) computations or not. Some operating systems propose APIs to query the current processor temperature, and for example the application can decide to slow down its computation if it is above a certain threshold.

(One thing that is annoying with "heat" is that there is no very interesting notion of "pure computation", as mostly any computation can generate heat. But there exists useful abstractions of heat, such as "algorithmic complexity", which don't have such a strict limitation.)