CGPA
Cat Girl Program Analysis
cgpa.isarabbithole.com
๐Ÿ  Board Index ๐Ÿ‘ฅ Members ๐Ÿ” Search โ“ FAQ ๐Ÿพ Profile โ— nyaonline: 47
Welcome back, PurrfectPureLambda  ยท  You last visited: 3 hours ago ๐Ÿ“ฌ Unread posts (12)  ยท  Logout
๐Ÿฑ Haskell Fans Explain Why Monads Are Actually Simple (for the 400th time)
โญ LEGENDARY THREAD   This thread has been archived in CGPA's Hall of Infinite Arguments
Posted by: MeownadTransformer Started: 14 Mar 2021, 02:47 Replies: 523 Views: 84,291 Category: Language Wars Containment Zone
๐Ÿ“Œ Pinned by mod: TypeClassCat ๐Ÿ”ฅ Hot: Still burning after 4 years ๐Ÿ“„ Pages: 27
๐Ÿ“Œ MOD NOTE โ€” TypeClassCat (Mod): This thread has been moved to Language Wars Containment Zone for the fourth time. Please keep it civil, nya~. The burrito analogy discussion is NOT banned but you ARE on thin ice. โ€” Forum Rules
Page:
๐Ÿฑ
MeownadTransformer
"Monad Transformer Stack Enjoyer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 5,821 Joined: Jun 2018 Rep: +2,047 online

hello cgpa friends (=^.^=)

so i've been staring at haskell documentation for approximately 72 hours straight and I still cannot figure out what a monad actually is. I understand that:

class Functor m => Applicative m => Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b

i understand the code. but why do I feel nothing. like, spiritually. what IS it nya~

i've read 17 monad tutorials. they all start with "monads are like burritos" or "a monad is just a wrapper" and then immediately spiral into category theory. one of them made me cry. (=;_;=)

please, once more, for the 400th time on this forum: can someone explain monads simply? just once? for me? for a cat girl who just wants to write some Haskell in peace?

( i have read all 399 previous times this was asked. they did not help. but maybe the 400th time will be different. this is called optimism. )

Reactions: ๐Ÿ˜ญ relatable (247) โค๏ธ (89) ๐Ÿ˜‚ (134) ๐Ÿฑ nya (312) ๐ŸŒฏ burrito (67)
/\_/\ ( o.o ) โ€” MeownadTransformer > ^ < "I understood monads once. I slept. I no longer understand monads."
๐Ÿ”ฎ
CurryCat
"Haskell Curry's Spiritual Descendant (probably)"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 12,447 Joined: Feb 2016 Rep: +8,201 online

Ah yes, the eternal question. Allow me to elucidate, nya~

Consider a category C. A monad on C is an endofunctor T: C โ†’ C equipped with two natural transformations:

-- ฮท (eta): unit / return โ€” the "identity" of the monad ฮท :: Id โ†’ T -- natural transformation from Identity to T ฮผ :: T โˆ˜ T โ†’ T -- natural transformation from Tยฒ to T ("join") -- These must satisfy coherence conditions: ฮผ โˆ˜ Tฮท = Id_T -- left unit law ฮผ โˆ˜ ฮทT = Id_T -- right unit law ฮผ โˆ˜ ฮผT = ฮผ โˆ˜ Tฮผ -- associativity

So naturally, we see that a monad is precisely a monoid object in the monoidal category of endofunctors on C, where the monoidal product is functor composition and the unit is the identity functor Id_C.

Now, in Haskell's category Hask, where objects are types and morphisms are functions, an endofunctor is a type constructor m :: * -> * with an fmap. The natural transformations ฮท and ฮผ correspond precisely to return and join respectively, with (>>=) defined as the evident composition.

I hope this clarifies things! (=^โ€ขฯ‰โ€ข^=)

๐Ÿ˜ญ helps (3) ๐Ÿ’€ killed me (198) ๐Ÿค“ based (44) ๐Ÿฑ nya (21)
=^._.^= โˆซ โ€” CurryCat "The Curry-Howard correspondence tells us that programs are proofs and I am proof that cats can do type theory. QED. nya~"
๐ŸŒฏ
FluffyBinder
"Burrito Theory Evangelist"
โ˜…โ˜…โ˜…โ˜†โ˜†
Posts: 892 Joined: Nov 2019 Rep: +411 offline

ok ok ok i got this. nya~ listen:

A monad is like a burrito. ๐ŸŒฏ

You have your ingredients (values). You wrap them in a tortilla (the monadic context). The magic is that you can put a function that takes ingredients and returns another burrito and just get back... one burrito. No nested burritos. Just one beautiful burrito.

That's bind. (>>=) is just: take my burrito, unwrap it, apply your function to the ingredients, and give me the burrito that comes out. No double-wrapping. One burrito. Always one burrito.

The Maybe monad is a burrito that might be empty. The List monad is many burritos. The IO monad is a burrito that does something dangerous when you eat it. It's all burritos.

hope this helps!! (=^โ–ฝ^=)

๐ŸŒฏ BURRITO (156) ๐Ÿ˜‹ hungry now (44) โŒ NO (89) ๐Ÿฑ nya (15)
/แ ๏ฝก๊žˆ๏ฝกแŸ\ โ€” FluffyBinder "All monads are burritos. Some burritos are also monoids. I will die on this hill."
๐Ÿ˜พ
GaloisCat
"Theorem Prover / Professional Arguer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 9,334 Joined: Jan 2017 Rep: +5,512 online
FluffyBinder wrote:
A monad is like a burrito. You wrap them in a tortilla (the monadic context)...

A MONAD IS NOT A BURRITO.

A monad is a MONOID IN THE CATEGORY OF ENDOFUNCTORS. This has been established. This is mathematical fact. A burrito is a FOOD ITEM. It is made of FLOUR and BEANS. It does not have a UNIT LAW. It does not satisfy ASSOCIATIVITY (have you tried to unsqueeze a burrito? You cannot. It is irreversible. Monads are not irreversible in this way, that is the whole POINT of purity).

The burrito analogy has been causing harm to this community for over a decade. Every time a new Haskell learner arrives and someone says "it's like a burrito" they leave confused AND hungry and STILL not understanding monads. The burrito analogy is a CRIME against pedagogy. COPE AND SEETHE, BURRITO ENJOYERS.

CurryCat's explanation above (#2) is literally correct and I will be citing Mac Lane's Categories for the Working Mathematician in my next post to prove it. Do not @ me.

...nya. (i am calm i am a cat i am fine)

๐Ÿ˜ค valid rage (201) ๐ŸŒฏ still a burrito (134) ๐Ÿ’€ unwell (88) ๐Ÿฑ nya (7)
(=โ†€แ†ฝโ†€=) โ€” GaloisCat "ร‰variste Galois died at 20 and still contributed more to mathematics than your burrito analogy. Think about that."
๐Ÿฑ
MeownadTransformer
"Monad Transformer Stack Enjoyer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 5,821 Joined: Jun 2018 Rep: +2,047 online

(=^.^=) monads are burritos, cope

(=^.^=) monads are burritos, cope

(=^.^=) monads are burritos, cope

[ this post was edited 1 time. last edit: added a third "cope" for emphasis ]

๐Ÿ’€ (404) ๐ŸŒฏ she's right (89) ๐Ÿ˜ญ (201) ๐Ÿฑ nya (511)
/\_/\ ( o.o ) โ€” MeownadTransformer > ^ < "I understood monads once. I slept. I no longer understand monads."
๐ŸŒฟ
LambdaWhisker
"Serene Lambda Enjoyer"
โ˜…โ˜…โ˜…โ˜…โ˜†
Posts: 3,102 Joined: Apr 2019 Rep: +1,899 offline

Okay, let me try a practical approach. No burritos. No category theory. Just code. nya~

You know how when you're chaining operations that might fail, you end up writing this?

-- Without monads: the pyramid of doom lookupUser :: String -> Maybe User lookupEmail :: User -> Maybe Email validateEmail :: Email -> Maybe ValidEmail getUserEmail name = case lookupUser name of Nothing -> Nothing Just user -> case lookupEmail user of Nothing -> Nothing Just email -> validateEmail email

That's horrible, right? A monad is what lets you write this instead:

-- With the Maybe monad: clean, sequential, beautiful getUserEmail name = do user <- lookupUser name email <- lookupEmail user validateEmail email

The monad is the pattern that handles the "what happens when something goes wrong" (or "what happens when there are multiple values" or "what happens when there's IO") for you, so you can write code that just describes the happy path and the plumbing is automatic.

(>>=) is literally just: "take the value out of the box, run the function, get a new box back." That's it. The specific box (Maybe, List, IO) decides what "taking the value out" means in context of potential failure/plurality/effects.

You don't need to know it's a monoid in the category of endofunctors to use it effectively. You just need to use it a lot. (=^ฯ‰^=)

๐Ÿ’š perfect (12) ๐Ÿ‘๏ธ someone will ignore this (311) ๐Ÿฑ nya (28) ๐ŸŒฏ but is it a burrito (44)
/\ /\ ( = ฯ‰ = ) โ€” LambdaWhisker ) ( "I wrote the perfect monad explanation. (_)-(_) It was immediately buried under arguing. This is fine. I am fine. nya."
๐Ÿฏ
TypeclassTigers
"Typeclass Hierarchy Appreciator"
โ˜…โ˜…โ˜…โ˜†โ˜†
Posts: 1,204 Joined: Sep 2020 Rep: +344 offline

ok but can we also talk about do-notation?? because honestly do-notation is what made it click for me. nya~

like the whole point is that do desugars to (>>=) calls:

do x <- someAction -- desugars to: someAction >>= \x -> y <- otherAction x -- desugars to: otherAction x >>= \y -> return (x, y) -- desugars to: return (x, y) -- Full desugaring: someAction >>= \x -> otherAction x >>= \y -> return (x, y)

it literally looks like imperative code but it's secretly functional! isn't that cool? (^โ—ก^)

the IO monad uses this to make side effects look sequential even though haskell is pure. THATS the magic imo.

โœจ do-notation appreciator (67) ๐Ÿฑ nya (34) ๐Ÿ“š helpful (29)
=^..^= โ€” TypeclassTigers "do notation: making haskell look like python since 2001. we don't talk about how it's secretly monads. shhhh."
๐Ÿ˜พ
GaloisCat
"Theorem Prover / Professional Arguer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 9,334 Joined: Jan 2017 Rep: +5,512 online
MeownadTransformer wrote:
(=^.^=) monads are burritos, cope [ร—3]

I have prepared my citation. Mac Lane, Categories for the Working Mathematician, Chapter VI, Section 1, and I quote (paraphrasing for the forum):

"All told, a monad in X is just a monoid in the category of endofunctors of X, with product ร— replaced by composition of endofunctors and unit set by the identity endofunctor."

Notice how Mac Lane does NOT say "a monad is a tortilla filled with beans." Saunders Mac Lane, one of the founders of category theory, did not use the burrito analogy. I rest my case. QED. CHECKMATE.

LambdaWhisker's post (#6) was actually quite good and I endorse it but I was too busy being furious to say so at the time. Consider this my retroactive endorsement. nya.

๐Ÿ’ฏ based (178) ๐ŸŒฏ still burritos (89) โค๏ธ galoiscat has a heart (44) ๐Ÿฑ nya (12)
(=โ†€แ†ฝโ†€=) โ€” GaloisCat "ร‰variste Galois died at 20 and still contributed more to mathematics than your burrito analogy. Think about that."
๐Ÿ“œ
PurelyFunctionalFelidae
"Writes 500-Word Posts At 4am"
โ˜…โ˜…โ˜…โ˜…โ˜†
Posts: 4,677 Joined: Mar 2017 Rep: +2,133 offline

I've been thinking about this thread for 45 minutes and I need to write something longer. Bear with me. nya~

The fundamental issue with monad pedagogy is that we conflate three distinct problems: (1) what monads ARE mathematically, (2) what monads ARE in Haskell's type system, and (3) what monads DO for you as a programmer. CurryCat answered (1). LambdaWhisker answered (3). Nobody has properly answered (2) yet.

In Haskell's type system, a monad is a type constructor m :: * -> * (that is: something that takes a type and produces a type) that is also a Functor and an Applicative, and additionally provides two operations: return :: a -> m a and (>>=) :: m a -> (a -> m b) -> m b (or equivalently, join :: m (m a) -> m a), subject to three laws:

-- The Three Sacred Monad Laws (tattoo these on your heart) -- Left identity: return a >>= f โ‰ก f a -- Right identity: m >>= return โ‰ก m -- Associativity: (m >>= f) >>= g โ‰ก m >>= (\x -> f x >>= g)

These laws exist so that do-notation works sensibly (associativity ensures you can refactor do-blocks without changing semantics; the identity laws ensure return is truly neutral). This is exactly the same structure as a monoid (a set with an associative binary operation and an identity), just lifted to the level of type constructors. Hence: monoid in the category of endofunctors. Both CurryCat and the burrito people are pointing at the same elephant from different sides of the room.

Anyway, the REAL answer to OP's question is: just use do-notation with Maybe and IO until it feels natural. The mathematical insight comes AFTER the intuition, not before. (=^._.^=)

I have now written 500 words. This was my destiny. Goodnight. [it is 4:47am]

๐Ÿ† best post (89) ๐Ÿ’ค long (44) ๐Ÿ“œ essay cat (67) ๐Ÿฑ nya (33) ๐ŸŒฏ but the burrito (11)
โˆง๏ผฟโˆง ( ๏ฝฅฯ‰๏ฝฅ) โ€” PurelyFunctionalFelidae / ใค โˆซ "I have opinions about type systems (_/๏ฟฃ๏ฟฃ๏ฟฃ๏ฟฃ and I WILL share all of them at 4am."
โšก
EffectfulKitten
"Algebraic Effects Missionary"
โ˜…โ˜…โ˜…โ˜…โ˜†
Posts: 2,841 Joined: Jul 2020 Rep: +1,201 online

okay but real talk:

just use algebraic effects nya~

like, monads are great and all, but have you considered that algebraic effects let you do the same thing without the composition problem? no more transformer stacks, no more lifting everything, no more crying at 3am because your ReaderT (StateT (ExceptT IO)) is doing something unexpected.

-- In a hypothetical effects system: effect State s where get :: () -> s put :: s -> () -- compose effects freely, no monad transformers needed! myProgram :: [State Int, Exception String, IO] => () myProgram = do n <- get when (n < 0) $ throw "negative!!!" put (n + 1)

this is available right now in Koka, Effekt, and to some extent with libraries in Haskell itself. the future is effects. monads are legacy technology. cope. (=^ใƒปฯ‰ใƒป^=)

[ i say this with love. i use haskell every day. it's complicated. ]

โšก based (88) ๐Ÿ”ฅ heresy (134) ๐Ÿ˜ค (67) ๐Ÿฑ nya (21) ๐ŸŒฏ effects are also burritos (12)
/แ -๊žˆ-แŸ\ โ€” EffectfulKitten "I came here to discuss algebraic effects and I am the only one discussing algebraic effects and I am at PEACE with this."
๐Ÿช
CamelCat
"OCaml Supremacist (nyam)"
โ˜…โ˜…โ˜…โ˜…โ˜†
Posts: 3,517 Joined: Dec 2018 Rep: +1,044 offline

you know, if you used OCaml with let*, you wouldn't have this problem. nya~

(* OCaml's let* syntax โ€” just as monadic, but BETTER *) let getUserEmail name = let* user = lookup_user name in let* email = lookup_email user in validate_email email

same thing. just works. nobody fights about whether it's a burrito. you know why? because OCaml programmers are too busy building robust systems to argue about food analogies.

also OCaml has a proper module system so you don't need typeclasses, your "Monad m" becomes a module parameter, no confusion, no cats crying at 4am. just clean, elegant, functional programming. (=โ†€แ†ฝโ†€=)

i'll see myself out. [CamelCat has left the thread]

๐Ÿช based camel (44) ๐Ÿ˜ค wrong (201) ๐Ÿ”ฅ thread arson (78) ๐Ÿฑ but is it nya (9)
โ‰ฝ^โ€ขโฉŠโ€ข^โ‰ผ โ€” CamelCat "OCaml: functionally correct since 1996. let* > do-notation. fight me (politely, with formal proof). nyam."
๐Ÿ˜ฐ
ScrolledTooFar
"I just got here"
โ˜†โ˜†โ˜†โ˜†โ˜†
Posts: 14 Joined: Mar 2021 Rep: +2 online

wait this thread has... 523 replies?

i came here to learn about monads

i scrolled past a category theory proof, three burrito references, a citation from an actual mathematics textbook, something about OCaml, something about algebraic effects, and a 500-word essay written at 4am

i am going to go read LYAH instead. goodbye. (=._. =)

[ p.s. are burritos actually a good analogy? asking for a friend. ]

๐Ÿ˜ญ valid (312) ๐Ÿƒ run (89) ๐Ÿฑ nya (44) ๐Ÿ“– LYAH is good (67)
=(^._.^)= โ€” ScrolledTooFar "I came. I scrolled. I left."
โ”€โ”€โ”€ [ Page 2 of 27 ] โ€” Posts #13โ€“#32 โ”€โ”€โ”€
โšก The argument continues. The burrito debate intensifies. GaloisCat has now cited 4 papers.
๐Ÿต
PawsAndPatterns
"Design Pattern Connoisseur"
โ˜…โ˜…โ˜…โ˜†โ˜†
Posts: 788 Joined: Aug 2020 Rep: +201 offline

coming back to this thread after sleeping. I see the burrito discourse consumed the first 30 posts. as expected. nya~

i want to add: the true test of whether you understand monads is whether you can implement a new one from scratch. try implementing Writer:

newtype Writer w a = Writer { runWriter :: (a, w) } instance Monoid w => Monad (Writer w) where return a = Writer (a, mempty) Writer (a, w) >>= f = let Writer (b, w') = f a in Writer (b, w <> w')

notice: you need Monoid w to implement the monad! the log values accumulate with (<>). this is why the category theory connection isn't purely academic โ€” understanding that monads and monoids are related explains WHY Writer requires that constraint.

see? it all connects. (=^โ€ฅ^=)

๐Ÿง  brainworms (89) ๐Ÿ’ก it clicked (44) ๐Ÿฑ nya (18)
/แ .๊žˆ.แŸ\ โ€” PawsAndPatterns "Writer monad: a monad that writes things. State monad: a monad that has state. IO monad: a monad that haunts my dreams."
โ”€โ”€โ”€ [ Pages 3โ€“17 ] โ€” 270 more posts โ”€โ”€โ”€
๐Ÿ’ฌ Highlights include: GaloisCat vs FluffyBinder (The Great Burrito War II), EffectfulKitten's 8-post essay on Koka language, someone comparing monads to conveyor belts, a visit from a confused JavaScript developer who thought "promise chains are the same thing" (they were banned for 3 days), and TypeclassTigers accidentally writing a working monad tutorial that everyone ignored to argue about whether Applicative should be a superclass of Monad (it is, since GHC 7.10, but we still argue).
๐Ÿฃ
TinyPawsNewcat
"Newcat (day 1)"
โ˜†โ˜†โ˜†โ˜†โ˜†
Posts: 1 Joined: TODAY Rep: 0 online

hi!! i'm new here nya~ (=^โ–ฝ^=)

i read this whole thread trying to understand monads and i think i'm starting to get it? but i have a question:

what's a functor?

everyone keeps saying "a monad is also a functor" but nobody explained what a functor is. is it also like a burrito? it's okay if it is. burritos sound nice.

thank you for your help!! nya~ (=^.^=)/

๐Ÿ’€ RIP thread (511) ๐Ÿ˜ฑ oh no (312) ๐Ÿฑ precious (201) ๐ŸŒฏ yes it's a burrito (44)
(=^-ฯ‰-^=) โ€” TinyPawsNewcat "I just got here. Everything seems fine."
๐Ÿ˜พ
GaloisCat
"Theorem Prover / Professional Arguer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 9,334 Joined: Jan 2017 Rep: +5,512 online

A FUNCTOR IS NOT A BURRITO EITHER.

A functor is a structure-preserving map between categories. Formally: given categories C and D, a functor F: C โ†’ D maps objects of C to objects of D AND morphisms of C to morphisms of D, such that:

-- Functor laws (sacred, inviolable, non-negotiable): fmap id โ‰ก id -- identity preservation fmap (f . g) โ‰ก fmap f . fmap g -- composition preservation

In Haskell, functors are type constructors that implement fmap. Maybe is a functor. [] (List) is a functor. IO is a functor. They let you apply a function "inside a box" without unpacking the box. NOT A BURRITO.

...welcome to the forum, TinyPawsNewcat. Your question is good. I am not angry at you. I am angry at the concept of analogies. nya~

๐Ÿฑ galoiscat has a soft side (289) โค๏ธ (201) ๐Ÿ˜ค still a functor-rito (77) ๐Ÿ“– actually helpful (44)
(=โ†€แ†ฝโ†€=) โ€” GaloisCat "ร‰variste Galois died at 20 and still contributed more to mathematics than your burrito analogy. Think about that."
๐ŸŒฏ
FluffyBinder
"Burrito Theory Evangelist"
โ˜…โ˜…โ˜…โ˜†โ˜†
Posts: 893 Joined: Nov 2019 Rep: +412 offline
TinyPawsNewcat wrote:
is it also like a burrito? it's okay if it is. burritos sound nice.

YES. A functor is ALSO a burrito. A functor is actually the PROTO-BURRITO. It lets you transform the inside of the burrito without opening it. nya~

Imagine your burrito has chicken inside. fmap marinate chicken_burrito gives you... a marinated chicken burrito. The tortilla didn't change. Only the inside transformed. THAT IS FMAP. THAT IS FUNCTORS.

Welcome to the forum!! (=^โ–ฝ^=) ignore GaloisCat, she explains things correctly but very angrily

๐ŸŒฏ BURRITO PEDAGOGY (178) ๐Ÿ˜ญ (89) ๐Ÿฑ nya (56) ๐Ÿ— chicken burrito (34)
/แ ๏ฝก๊žˆ๏ฝกแŸ\ โ€” FluffyBinder "All monads are burritos. Some burritos are also monoids. I will die on this hill."
๐Ÿฑ
MeownadTransformer
"Monad Transformer Stack Enjoyer"
โ˜…โ˜…โ˜…โ˜…โ˜…
Posts: 5,821 Joined: Jun 2018 Rep: +2,047 online

update: i understand monads now. i've been using them for 3 weeks and one day they just... clicked. it was a tuesday. i was eating a burrito. (=^.^=)

(=^.^=) monads are burritos, cope

anyway welcome TinyPawsNewcat!! the functor question is excellent. GaloisCat's answer (#492) is correct. FluffyBinder's answer (#493) is also kind of correct and more delicious. LambdaWhisker's original post (#6) on this thread explains basically everything you need. read that one.

i am starting a new thread: "Functors Are Actually Simple (for the first time)". see you all there. nya~ (=^.^=)/

๐ŸŽ‰ she clicked (512) ๐ŸŒฏ it WAS a burrito (201) โค๏ธ (344) ๐Ÿฑ nya (611) ๐Ÿ† legend (89)
/\_/\ ( o.o ) โ€” MeownadTransformer > ^ < "I understood monads once. I slept. I no longer understand monads." [UPDATE: I understand them again. tuesday.]
โ”€โ”€โ”€ [ Pages 26โ€“27 ] โ€” Posts #495โ€“#523 โ”€โ”€โ”€
๐Ÿ“Œ Thread continues with 29 more posts debating whether the Tuesday Epiphany was caused by the burrito or the monads. CamelCat returns briefly to say OCaml's let* also "just clicked" for them on a Wednesday. EffectfulKitten links to a paper about algebraic effects one final time. Nobody reads it. Thread is marked LEGENDARY by the mod team.
Page:

โœ๏ธ Post a Reply

Posting as: PurrfectPureLambda