r/functionalprogramming • • Jun 14 '25
Question When people say monads encode a context, what do they mean that is more than a data structure?
I think I've gotton a pretty good grasp of using monads by now. But I dont get what people mean when they call it a context. How are they more than data structures?
One idea that immediately comes to mind is IO and purity. But if we take for example, Maybe Int, Just 3 >>= \x -> return $ show x will reliably produce Just "3". So it feels like defining monads interms of purity is restrictive? Or is my idea of purity is incorrect?
I have been thinking of them as items in wrapped in a gift box as used in learn you as haskell and a few other resources, but I see people explicitly condemming that mental model as inadequate. So what am I missing?
I guess a concrete question will be what can I do with Maybe as monads that can not be approximated by maybe as Applicatives? I feel like I am asking what can clay do that bricks cant or something like that but it feels so incorrect on my head.
f = (*3)
Just 3 >>= \x -> return f x
Just 3 <*> Just fAnd what are side effects? I feel like I get what is meant by this in terms of the State monad and the IO monad. But I cant point to it in code. (I wrote a desugared state monad snippet hoping I could find some concrete question to ask. I didnt but maybe someone can find it useful for illustrating some idea.). But more importantly, what are side effects when it comes to the Maybe monad or the List monad?
13
u/jacobissimus Jun 14 '25
A monad is an idea that gets represented as lots of different data structures—you can think of it as an object that is going to run functions is some special way. What that special way is depends on what kind of monad it is.
Side effects are just one reason you might want to use a monad, but Maybe and List don’t have any side effects, like you pointed out. Instead, Maybe lets you take a procedure that might not have an output and turn it into a function that always returns a value.
4
u/KyleG Jun 14 '25
It's a bit of a simplification, but ultimately, a monad is two things:
const maybeX = Maybe.of(5)is a simple illustration of constructing aMaybeto contain5singleton(5) // [5]or[5]is eqivalent to #1 above (the constructor)That's it, that's the whole of monads. Everything else is derivable from #1 and #2. (Also technically there are monadic "laws" such that #1 and #2 above have to satisfy certain conditions, but honestly you only need to know that if you're thinking of making your own monads. This is similar to how a field in math has to have +, -, *, and / and they have additive and multiplicative identities, +- and */ are inverses, etc. You dont' need to know this unless you're inventing your own fields.
Now, when people say "context" they're talking about how different monads satisfy the above two criteria but also provide something special for each differnet monad variant. Either encodes one of two possibilities (this is the context). Optional encodes the context of existence or non-existence. IO encodes IO-related side effects. State encodes an implicit, mutable state accessible to anything in the monad, etc.
What I mean here is that Optional/Maybe/Option and Either and IO all have a constructor/of/just/singleton and flatmap/bind/>>=/chain, but they're still different in that they represent a different "thing" (which we often say "context")
4
u/rantingpug Jun 14 '25
I think it helps more to think of
Monadsas an interface. It simply describes a set of operations to perform, in this case,>>=andreturn. Using these 2 together allows to "chain" operations.Any data structure implementing the
Monadinterface will provide different semantics for what that "chaining" means.For instance:
you've performed 2 operations on a
Monad, in this caseMaybe. TheMaybedata structure provides the semantics of a fail-first nullability check.You can do the same thing for other
Monads, so add 1 to all the elements in aList, or the result of someEither erretc etcNotice that each different structure provides different semantics, including side-effects if you so wish - that is what
IOdoes.I think that's what most people refer to as context, some implicit behaviour that all your values of that
Monadsupport. If you then put a bunch of monads together, you get a value that supports a multitude of behaviours. Monads are famously tricky to compose together but something like monad transformers helps and you can then have something likeIO (Either String (Maybe Int)).In other words, an Int that is nullable, can error with a string and performs
IO. That's your context.So what about
Applicatives? They're the same, but the set of operations is different, so the semantics can be slightly different.An
Applicativespecifies how to map it's value fromAtoBonly, you can't produce another monadic action. With theMaybeexample from above, you can't turn aJust xinto aNothingviafmap. You can via>>=.But yeah, they kinda do offer you an added context too.
I think it's easier to think about the data structures themselves providing that, rather than abstract
MonadorApplicative. Those are just interfaces...Hope that helps
EDIT: legibility
4
u/iamemhn Jun 15 '25
Every monad can perform pure computation. The «explicit» computation, so to speak, that you end producing with
pure(the originalreturnname was confusing due to its meaning in other languages).But every monad has an «implicit» behavior.
Maybegives you early termination.Eithergives you early termination with the ability to produce an explaining value.Listgives you ordered non-determinism.Writercarries aMonoidyou can add to.These look like data structures and that's why many tutorials use the (unfortunate) wrapped box metaphor.
But
Readercarries a read only value, or does it? AndStatedoesn't carry a state (it only seems it does): it carries functions that transform the initial state. AndIdentitycarries... nothing.When we talk about context, we mean two things: the implicit thing (or combination of things) that the Monad will take care of for you (the «plumbing»), and the explicit thing you can compute at every step (the left of
>>=) that allows you to change the flow of computation in a wayApplicativecannotMonads such as
STMandIOare opaque, in the sense that you cannot see how they are implemented, as they are controlled by the runtime:STMwill take care of your concurrent transactional variables and channels, while you do whatever computation by reading and writing them.Now, plain
Applicativescompose in a straightforward manner (seeComposefunctor).Monads don't compose: they have to be rewritten as transformers. You can stack multiple transformers and their implicit behaviors will also compose (with some caveats).IOwill always be at the bottom.The above difference is rooted in the fact that the structure of an
Applicativecomputation is fixed and there's no chance to break a chain, while the structure ofMonadcomputations can change based on intermediate results.When you have
you can rewrite it to the applicative
But try to rewrite the monadic
into a pure
Applicativeform.3
u/ScientificBeastMode Jun 14 '25 edited Jun 14 '25
There are some good responses here already, but I wanted to add my own because this is how it all made sense to me when I eventually learned it.
So, just to clarify the background a bit, a monad is just some kind of data structure that wraps data of an arbitrary type, and a way of “sequencing” operations on the underlying data. In other words, the operations are chained together where the next state (and the operation that produced it) usually depends on the previous state. And the chaining operation simply provides a way to “pass in” a data transformation you want to perform.
Now, I think of the “context” as a kind of “configuration” of the monad to do different kinds of things in the background. The context is just a “strategy pattern” (to use the OOP terminology) at a very abstract level. Rather than passing in the strategy itself, each monad is usually hard-coded to perform its unique strategy.
So the “strategy” I’m referring to is really just asking “what does it mean for this monad to ‘chain’ or ‘map’?”
An
Option/Maybemonad just ignores the passed-in data transformation function when the data is missing, and chaining just means the transformed data gets wrapped again in a newOption/Maybestructure and flattened. That’s its context.The
Listmonad applies the passed-in function over an arbitrary number of contained values, and returns a new list with those values. The chaining operation just runs a passed in function for turning data into lists of data, and concatenates all those new lists into a single flat list. That’s theListcontext.The
Taskmonad usually has a second generic “error” type, but otherwise it’s the same idea. It takes a passed in function and applies it to data in the future whenever the data arrives (because it’s an async operation). Its chaining operation takes a function that produces anotherTaskinstance that will be run after the previous one is finished. That’s theTaskcontext.Now, the reason why these things are called “contexts” is because, from the point of view of the function you’re passing in to
maporchainorpure, there is some magical externally defined set of operations and assumptions that dictate the behavior of the program once your passed-in function returns a value. You can think of each context as a miniature runtime for a tiny program contained within your function. You hand over the function, and the runtime does its magic. The context is just how that specific runtime happens to work.3
u/ddmusick Jun 15 '25
I think of monads as a way of encapsulating (boxing) a value but then being able to operate on it as if it was unboxed. The monad defines what it means to "apply" functions to it, but the benefit we get is that we feel like we're operating directly on the encapsulated value. To me, it's not about side effects or early exit (those are examples of IO and Maybe monads).
3
u/[deleted] Jun 15 '25
Bind lets you handle the results of a computation at your pleasure. You can interrupt the control flow when binding a value to Y if the value is nothing. Sometimes you discard a result and don’t bind. Hence >>= and >>. They let you customize how computations are composed. Forget about the category theory for a minute. Monads let you customize how computations are sequenced. Programmable semicolons.
3
u/sullyj3 Jun 15 '25
They're not more than a data structure. It's just that if you look at it more abstractly, you can conceptualize the data structure as an implementation detail of an effect.
`Nothing` is obviously just a value, so you're not doing anything special when you return it. But in the `Maybe` monad, it *represents* the effect of abandoning the code that was executing. You're just looking at it in a different way.
3
u/Adventurous_Fill7251 Jun 15 '25
I think the problem with the 'wrapped value' analogy is it doesn't really reflect what's going on for the general case. For example, I like to think of the IO monad as a 'recipe'; a value of type 'IO String' is a set of instructions that allow us to create a string by using some IO operations. It's not a string wrapped in anything, in fact, the string isn't even there yet (think of input IO actions). Here the context is that IO environment; the string will exist, but only once we are on the appropiate context. Some monads, like Maybe, do act like wrapped values, but it just isn't the general case of what the monad is.
4
u/Wafer_Over Jun 14 '25
Monad is more than a data structure. It has to follow certain rules for it to be monad. One primary rule is the sequentiality of the operations. It needs to have flatmap which enforces that. Applicatives allow for parallel computations.
4
u/kbielefe Jun 15 '25
A monad is a very abstract thing. Trying to think about it in concrete terms usually doesn't work. It's something you can make at least one lawful
bindandreturnfunction. That's it.Side effects are basically operations that aren't visible in the arguments or return value of a function. For
Stateit's a bit confusing because it doesn't actually have side effects, but the API very much resembles side effects.2
u/Shadowys Jun 15 '25
The monad design pattern provides a consistent interface to something. That something can be anything aka the context can be anything.
0
u/kindaro Jun 15 '25 edited Jun 17 '25
All I see here in comments is some form of condescension or implicit admission of inadequacy. There is no need to be metaphorical or approximate. Saunders Mac Lane already summarized it for us.
A monad is a monoid diagram in a category of endofunctors and natural transformations.
If you have some bravery in your soul, study this definition. People like to speak of it as if it is some kind of a joke, but it is not a joke. Rather, everything people say here is a joke. A monad is not an object, an interface, a context, a design pattern.
You may have some trouble figuring out what a monoid diagram is, what a category of endofunctors and natural transformations is, and how to work with these things. Ask me if you need some details. Or else, Category Theory by Steve Awodey is a great book and reading it is sure to get you all the answers.
I know people will be very displeased by this message, but I am fed up with the nonsense that is being spread around at the expense of the clear and simple truth.
1
u/kindaro Jun 15 '25
Great question. To my best knowledge, no one knows what «side effects» are. I have been unable to either find a definition in literature or extract it from people that use this term.
My best idea for now is that «side effects» are non-local properties. For example, printing a line is a side effect because it can plausibly trigger another program to do something somewhere else. For comparison, when evaluating pure code, we expect no one to be aware of what is going on.
I am not sure if the
Readermonad should be classified as introducing side effects. After all,Reader x ais the same asx → a. Do functions introduce side effects? Hard to say.6
u/[deleted] Jun 14 '25
[removed] — view removed comment