Zulip Chat Archive
Stream: new members
Topic: Monad and Applicative
Nam (Sep 24 2022 at 03:32):
Hi, I am reading https://leanprover.github.io/lean4/doc/monads/monads.lean.html#what-separates-monads-from-applicatives but I don't quite understand that paragraph. It says "You cannot really determine the structure of "future" operations" but we do. For example, a Maybe monad can only produce another Maybe monad in the future. Is that not right?
Mario Carneiro (Sep 24 2022 at 03:36):
There seem to be some missing words in that sentence. It's talking about applicatives there
Mario Carneiro (Sep 24 2022 at 03:37):
A monad allows you to determine the structure of future computations based on the results of past computations
Mario Carneiro (Sep 24 2022 at 03:37):
In an applicative, you determine the structure in advance of getting any of the results
Mario Carneiro (Sep 24 2022 at 03:38):
Here by "structure of computations" we mean things like which functions get called
Nam (Sep 24 2022 at 04:50):
Thank you. I think I've got it now. That sentence might have been a general statement, applicable to both Applicative and Monad.
Nam (Sep 24 2022 at 04:52):
the first sentence sets up the premise that context is what separates the two. the second sentence states that general observation. the third says that with applicative you can't get final value (i guess you can with monad, e.g. Nothing), but you can determine the call tree.
Nam (Sep 24 2022 at 04:54):
does that sound about right?
Mario Carneiro (Sep 24 2022 at 05:05):
I'm not sure what you mean by "you can't get the final value"
Mario Carneiro (Sep 24 2022 at 05:07):
you can always get the value if you know what type it is; it's just that the monad / applicative operations don't give you a way to exit the monad generically
Mario Carneiro (Sep 24 2022 at 05:08):
most monads will have a run
function which extracts the data from the monad. It's just that the type signature of this function varies a lot depending on the monad
Nam (Sep 24 2022 at 05:09):
by "final value" i mean the same as "final function result" in that paragraph
Nam (Sep 24 2022 at 05:09):
"""With applicatives, you can't get the final function result without evaluating everything,"""
Mario Carneiro (Sep 24 2022 at 05:43):
Stated that way, it's true for monads too
Mario Carneiro (Sep 24 2022 at 05:44):
or functions in general
Mario Carneiro (Sep 24 2022 at 05:44):
you don't get the result until everything is evaluated
Nam (Sep 24 2022 at 15:10):
damn. then i don't know what that paragraph means.
Kyle Miller (Sep 24 2022 at 16:03):
@Nam I think that paragraph is getting at the fact that applicatives have more restrictive control flow than monads. Roughly speaking, with applicatives you can figure out what the control flow is going to be even without evaluating anything, but with monads the control flow can depend on the specific values involved. (@Chris Lovett The sentence in the monad docs might be clearer as "With applicatives, you can determine the structure of how the operation will take place even before you evaluate everything.")
Nam (Sep 24 2022 at 17:32):
ah, yes, thank you Kyle. that's roughly what i understand too.
Nam (Sep 24 2022 at 17:52):
I've another question. the bind
operation is defined as bind : {α β : Type u} → f α → (α → f β) → f β
. it uses the same type constructor f
, which is the monad itself. that means the chain of operations must return the same type of monad. why can't it return a different type of monad?
Kyle Miller (Sep 24 2022 at 17:58):
@Nam Because then it wouldn't be a monad :wink: That's just the definition.
Nam (Sep 24 2022 at 17:59):
lol fair enough
Kyle Miller (Sep 24 2022 at 18:03):
One example of something that's monad-like but not a monad is indexed monads. One version I've seen has a bind operation of type bind : {α β : Type u} → f i j α → (α → f j k β) → f i k β
where for each i
you have that f i i
is a monad.
Last updated: Dec 20 2023 at 11:08 UTC