Talk:Monad (functional programming): Difference between revisions

Content deleted Content added
First paragraph needs a lot of help for first-timers. There's too much jargon and unspecified syntax.
 
(389 intermediate revisions by more than 100 users not shown)
Line 1:
{{Talk header}}
This is actually the same as [[Monad (category theory)]]. These should be merged. —[[User:Ashley Y|Ashley Y]] 22:19, 2004 May 31 (UTC)
{{WikiProject banner shell| class=C |
{{WikiProject Computer science|importance=mid}}
{{WikiProject Computing|importance=low}}
}}
{{FAQ}}
{{To do|collapsed=yes}}
{{Archives|auto=yes|search=yes}}
 
== Informal style ==
:They're not really the same thing: an explanation of Haskell's IO monad doesn't belong in an article on category theory, and a detailed explanation of monads in category theory isn't relavent to the average Haskell programmer. In addition, many standard Haskell monads aren't strictly speaking monads at all. I think it's probably best to keep the articles separate. [[User:Cadr|Cadr]] 22:28, 31 May 2004 (UTC)
 
Occasionally I'm seeing sentences that read more like a blog post than an encyclopedia:
::Which Haskell monads are not monads in the CT sense? —[[User:Ashley Y|Ashley Y]] 09:58, 2004 Jun 1 (UTC)
 
<blockquote>
:::The IO monad is one example (see [http://www.haskell.org/pipermail/haskell/2002-May/009622.html]). [[User:Cadr|Cadr]] 11:08, 1 Jun 2004 (UTC)
With just a little extra functional spice on top, this Maybe type transforms into a fully-featured monad.
 
Having to rewrite functions to take Maybes in this concrete example requires a lot of boilerplate (look at all those Just expressions!).
::::It's not clear, actually, since the difference is only visible with the 'seq' function, which is considered slightly dubious anyway. More importantly, these are ''Haskell's'' monad laws that IO may or may not be properly living up to: the sense in which "monad" is used in Haskell is ''precisely'' the CT sense.
</blockquote>
 
I'm not up on the latest Wikipedia guidelines, so I'm not confident about what is allowed. I'm curious about what the current thinking is from anyone who knows more about this topic. [[User:Modify|modify]] 15:16, 29 August 2022 (UTC)
::::If you think this should be a separate article, rename it "Monads in functional programming", rather than the () notation which suggests that it's a separate sense of the word.&mdash;[[User:Ashley Y|Ashley Y]] 02:49, 2004 Jun 2 (UTC)
 
:This article indeed seems to get consistently edited towards a semi-formal programming how-to style, against [[Wikipedia:NOTHOWTO]]. I have already posted a template noting this once to little effect. [[User:Terminator 2 really happened|Terminator 2 really happened]] ([[User talk:Terminator 2 really happened|talk]]) 19:26, 8 August 2024 (UTC)
:::::OK, but it's still the case that the two articles cover very different information. I'm fine with it being renamed to Monads in functional programming. [[User:Cadr|Cadr]] 14:15, 2 Jun 2004 (UTC)
 
== Pseudo-code in the article ==
Rename (from "[[Monad (functional programming)]]") done. &mdash;[[User:Ashley Y|Ashley Y]] 23:27, 2004 Jun 2 (UTC)
 
The meaning of [https://en.wikipedia.org/w/index.php?title=Monad_(functional_programming)&curid=579061&diff=1118556608&oldid=1118549561 the pseudo-code] needs to be clarified:
Er, the first sentence of the second para (and possibly more) looks like a copyvio from http://nomaware.com/monads/html/introduction.html#what --- I don't know the topic well enough to rephrase to eliminate this. Could someone who knows the topic look into this? thanks, [[User:JosephBarillari|jdb &#x274b;]] 06:00, 27 Jan 2005 (UTC)
: (f >=> g) x = (f(x) → mb) >>= g(y = b)
 
:1: My personal reading is that this is an equation. The left hand side is <code>(f >=> g) x </code>
== par function ==
:2: The right hand side is
::'''Let''' functor <code>(f(x) → mb)</code> '''be''' the functor resulting from substituting mb for y in g;
:3: The (y=b) on the right hand side is not an equation; but g(y=b) is an application of g to the type y where b
:4: The f → mb is a functor from type x to monadic value mb where b
{{anchor|klComposeOper}}The Kleisli compose operator <code>>=></code> means if some m >>= f then f is the continuation of m.<ref name= gg >[https://www.haskellforall.com/2012/12/the-continuation-monad.html? showComment=1424959716748#c3272667111873173855 Memetic Warrior (26 Feb 2015) reply to] [https://www.haskellforall.com/2012/12/the-continuation-monad.html? Gabriel Gonzalez, Haskell for All (30 Dec 2012) The Continuation Monad] </ref>
-- [[User:Ancheta Wis|Ancheta Wis]] [[User talk:Ancheta Wis| &nbsp; (talk]] [[Special:Contributions/Ancheta Wis| &#124; contribs)]] 18:59, 27 October 2022 (UTC), 01:42, 28 October 2022 (UTC)
 
{{anchor|akContin}}Alexis King<ref name=alexisKing >[https://github.com/lexi-lambda Alexis King, github]</ref> notes that if m >>= f >>= g >>= h then the continuation of m is f >=> g >=> h <ref name= lexiLambda >[https://gist.github.com/lexi-lambda/d97b8187a9b63619af29689e9fa1b880 lexi-lambda/continuations-and-reduction-semantics.md]</ref> --[[User:Ancheta Wis|Ancheta Wis]] [[User talk:Ancheta Wis| &nbsp; (talk]] [[Special:Contributions/Ancheta Wis| &#124; contribs)]] 19:18, 27 October 2022 (UTC)
Why is the function <math>(x, y) \mapsto \frac{1}{\frac{1}{x}+\frac{1}{y}}</math> called par? --[[User:MarSch|MarSch]] 11:06, 25 October 2005 (UTC)
{{talkref}}
 
: The notation doesn't make much sense and seems to have been introduced quite randomly and without references in https://en.wikipedia.org/w/index.php?title=Monad_(functional_programming)&diff=next&oldid=867467071 , along with a few other strange changes by [[user:Zar2gar1]] in the vicinity. To me, [[user:Nbrader]]'s edit is a correction. --[[User:Daniel5Ko|Daniel5Ko]] ([[User talk:Daniel5Ko|talk]]) 23:27, 28 October 2022 (UTC)
: At least that's the function for total resistance of two resistors in [[Series and parallel circuits|'''par'''allel coupling]]. --[[User:TuukkaH|TuukkaH]] 16:14, 25 October 2005 (UTC)
::So which is it,
:::(f >=> g) x = (f x) >>= g
:::or some other parenthesization needed?
:::(f >=> g) x = f x >>= g
:::ping [[user:Zar2gar1]] --[[User:Ancheta Wis|Ancheta Wis]] [[User talk:Ancheta Wis| &nbsp; (talk]] [[Special:Contributions/Ancheta Wis| &#124; contribs)]] 00:14, 29 October 2022 (UTC)
 
@[[user:Zar2gar1]], Thank you for your pseudocode in the article, which attempts to accomodate the requests of the readership to avoid Haskell. Because of the objections above, I propose the use of Bartosz Milewski's 2019 book ''Category Theory for Programmers'' pages 202 through 205, which uses Haskell notation to explain Kleisli composition <code>>=></code> . Dr. Milewski uses Kleisli composition <code>>=></code> and <code>return</code> to implement Monad. In fact Dr. Milewski implements Monad in Haskell three ways on those pages. --[[User:Ancheta Wis|Ancheta Wis]] [[User talk:Ancheta Wis| &nbsp; (talk]] [[Special:Contributions/Ancheta Wis| &#124; contribs)]] 01:08, 29 October 2022 (UTC)
==Rewrite==
I guess my scam is up. I haven't really used Haskell any significant amount, and I barely understand this monad stuff. I just hoped I might be able to push this article away from the two mistakes so many monad tutorials make&mdash;presenting them as "how you do I/O" or completely neglecting to give any intuition to the general concept of a monad. I did my rewrite with a copy of Wadler's "Comprehending Monads" in hand, but since I made so many mistakes I'll tag the article for verification. TuukaH is correct above about the naming of <code>par</code>. [[User:Gazpacho|Gazpacho]] 04:13, 29 October 2005 (UTC)
 
: @"which is it": The two alternatives have the same meaning. Even more: They already have identical parse trees. --[[User:Daniel5Ko|Daniel5Ko]] ([[User talk:Daniel5Ko|talk]]) 00:40, 30 October 2022 (UTC)
==Haskell==
 
:: Firstly, sorry for replacing the pseudo-code with Haskell code without reading the talk page first: I didn't realize there was an effort to avoid Haskell code. I will say though I found the pseudo-code confusing. My feeling (for what it's worth) is that psuedo-code that has no clear meaning to most people defeats the point of pseudo-code and you're better off going with an actual language like Haskell. [[User:Nbrader|Nbrader]] ([[User talk:Nbrader|talk]]) 01:21, 30 October 2022 (UTC)
This page really seems to be more about monads in Haskell, rather than in a generic programming language. The examples given seem to be in Haskell, which for non haskell programmers (i.e. me) are really hard to follow.
 
The equation "(f >=> g) x = (f(x) → mb) >>= g(y = b)" is very confusing, it's unclear where mb and y are bound; I have never seen anything similar. The explanation in this thread is also misguided: x is not a type and there is no substitution happening that forms functors.
Shouldn't this either be renamed to something like "Monads in Haskell" with a link to understanding Haskell, or have the equations written in a common mathematical way?
 
It makes sense to avoid Haskell-specific notation, but here the only notation used is function application written as "f x", and binary operator "f >=> g". I restored the equation, using f(x) as function application. That should be understandable without additional prerequisites. [[Special:Contributions/2001:861:3F42:1B60:AEFC:B99:1842:A29E|2001:861:3F42:1B60:AEFC:B99:1842:A29E]] ([[User talk:2001:861:3F42:1B60:AEFC:B99:1842:A29E|talk]]) <!--Template:Undated--><small class="autosigned">—&nbsp;Preceding [[Wikipedia:Signatures|undated]] comment added 07:57, 24 November 2022 (UTC)</small> <!--Autosigned by SineBot-->
:If I understand correctly, monads are containers for values: for example, a monad value like "Just 5" contains the value 5. The return function "wraps" a value inside a monad, the fmap function allows another function to "look inside" a monad value, and it looks like join turns a value like Just (Just 5) into Just 5, or [[3]] into [3]. A few examples:
 
:Hi everyone, I'm still going to be away from Wikipedia for the foreseeable future, but I did get the ping for this conversation. I'm going to try to keep my response simple. For the specific expression about monadic composition, just like you guessed @[[user:Ancheta Wis|Ancheta Wis]], the "g(y=b)" expression is a kludge to show variable binding and application in the few cases leaving them implicit is ambiguous. It's not the cleanest way for sure, but it does allow avoiding lambda calculus as a pre-req to understand the article.
return 3 = either [3] or Just 3 -- "Wrap" the value 3 with a monad
fmap (+1) [3,2,4] = [4,3,5] -- (+1) is the incrementor function, and it is allowed to "peek inside" this list to increment everything inside it.
fmap (+1) (Just 5) = Just 6 -- Peeking inside a "Just" value.
join Maybe (Maybe 7) = Maybe 7 -- "Joining" these two Maybe's into one
join <nowiki>[[2]]</nowiki> = [2] -- Joining a listifier with a listifier
 
:Big picture, anyone that wants to think through & change the code has my full support. I left [[Talk:Monad_(functional_programming)/Archive_2|a lot of comments and notes in the Talk archives]] while finishing up my changes though. My main opinions are pretty much:
:Is this clear enough? --[[User:67.172.99.160|67.172.99.160]] 02:09, 3 December 2005 (UTC)
:* Avoid making mostly independent concepts like lambda calculus or currying pre-reqs to understand the article
:* Try to be consistent and head-off misinterpretations around things like order of evaluation
:* The "right thing to do" would probably be to hash out functional pseudo-code guidelines for Wikipedia at large
:* Barring that, I do feel math-ish notation is preferable, followed by something like an established functional language (just looser & more accessible)
 
:I don't expect people to read my notes (they're long), but if you are willing to, you may still want to factor in my rationale at some points. [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 05:49, 28 November 2022 (UTC)
 
== Monad versus monoid ==
 
Talk for [https://en.wikipedia.org/w/index.php?title=Monad_(functional_programming)&curid=579061&diff=1183880921&oldid=1183845787 BRD] -- [[User:Ancheta Wis|Ancheta Wis]] [[User talk:Ancheta Wis| &nbsp; (talk]] [[Special:Contributions/Ancheta Wis| &#124; contribs)]] 02:03, 7 November 2023 (UTC)
 
== Permutation? ==
 
It was added at https://en.wikipedia.org/w/index.php?diff=438062307 that the monoid being "non-commutative and idempotent" implies permutation. I think the property as described cannot mean "free and idempotent", as [a, b, a] wouldn't be called a permutation. Is there a way to clarify the property to really give permutations? Otherwise, is it even meaningful to name the collection type arising from free and idempotent monoid? [[User:J824h|Junghyeon Park]] ([[User talk:J824h|talk]]) 04:27, 11 December 2023 (UTC)
 
:@[[User:J824h|J824h]]: Sorry it's almost a year later, but that was a really good catch. I reorganized that section into the table a few years back but didn't bother to think through the content enough.
:If you have idempotence but no commutativity to allow reduction, then I'd guess you have the most free regular language ''with no immediate repetitions'' over a given alphabet (of size ''N''). Which itself is isomorphic to the maximal, loop-free, digraph with ''N'' vertices (interpreted as a finite state machine over the alphabet)? Anyways, even if that's interesting, it doesn't strike me as particularly noteworthy or relevant to the article.
:I feel like the ability to generate permutations from a monoid is noteworthy though so I corrected the line. To distinguish from the free monoid, it explicitly says "[[partial permutation]]" now. And maybe it's a bit of hand-waving, but now it just describes the monoid as "non-commutative without repetition". Does that sound good? -- [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 17:37, 21 November 2024 (UTC)
::@[[User:Zar2gar1|Zar2gar1]] That constraint on List should suffice to give "permutations". I wouldn't even call them "partial", as in the other rows not explicitly alluding to the relation to the underlying set or basis (like saying "subsets" for sets). I guess a permutation here should have implied an arrangement, not a mapping.
::Coming back to this, I wonder whether permutations have well-defined associative join from the first place. How do I define [a] ++ [b] ++ [a]? I might be missing something. [[User:J824h|Junghyeon Park]] ([[User talk:J824h|talk]]) 14:37, 27 November 2024 (UTC)
:::Right, I think what makes it tricky to describe these container monads is also why they're interesting. A monoid is typically described in algebraic or formal language terms, but when you apply the monad structure, you get these containers that feel more combinatoric.
:::I could be wrong, but my gut feeling is the original editor intended the idea of "without replacement" by listing "permutations". Definitely double-check me, but viewed as selections, ''ordered'' selections ''with'' replacement would yield a full permutation (which is effectively the free monoid), ''unordered'' selections ''with'' replacement would yield multisets, and ''unordered'' selections ''without'' replacement would yield sets?
:::The obvious 4th piece would be ''ordered'' selections ''without'' replacement, which I guess would technically be partial permutations? The symmetry is really easy to see combinatorically, but our current table lists the property of the monoid, which isn't nearly as elegant.
:::Per your specific question about a well-defined join, if I'm thinking through this right, both full and partial permutations would have a well-defined join ''on their respective domains''. In a full permutation, there's no restriction on the ___domain so the join yields [a,b,a] without any issues. For a partial permutation though, listing 'a' twice is prohibited at the monoid level so in your example, the ++ would yield a type error at the 2nd 'a' prior to any join. As long as nothing is repeated though, there's no reason the naive join wouldn't work just like with the full permutation / free monoid. -- [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 19:11, 28 November 2024 (UTC)
::::[a, b] + [a] does need to be defined as long as both [a, b] and [a] are in the monad. To get permutations, we need a relation that identifies [a, b, a] to either [a, b] or [b, a], that is, a choice of left- or right-bias for each element. We don't have a unique but multiple valid concrete structures, which seems kind of unfortunate. Otherwise, we might simply accept [a, b, a] = error as a monadic value, which would be without a surjective homomorphism to sets and fit worse in the table. Do I make sense?
::::Regarding the whole table, I would see this table as a commutative diagram of "list → multiset → set", "list → permutation → set". The combinatoric interpretation is interesting. For the permutations, choosing universal left-bias might correspond to enforcing causality (the items selected earlier is removed from the pool and forbidden).
::::I don't think full permutation allowing duplicates is a standard terminology. Also, I stand by just permutation instead of partial permutation, as there is nothing to disambiguate in the context, and [[Partial permutation#Restricted partial permutations|being partial can mean many things]]! [[User:J824h|Junghyeon Park]] ([[User talk:J824h|talk]]) 15:18, 29 November 2024 (UTC)
 
{{outdent|4}} Ahhh, you're right, I totally forgot about the closure there. I also think you're exactly right about biasing the operator so that it's well-defined.
 
Because the join is a natural transformation, that would probably still remain naive, and the + would need to implement the bias. I would think either direction could work though; the append would just need to directly compare the items, then drop duplicates from either the left or the right. And like you said, you could interpret the left-bias causally like selection without replacement (and the right-bias is... anti-causal?)
 
''That said'', I also think you're right that we're getting into the weeds about the permutation bit. There's actually not even a citation either so while I think technical articles (especially hard topics) need some flexibility to work out the math / logic oneself, we're probably veering into [[WP:SYNTHESIS]] at this point.
 
So I just cut any mention of the permutations from the table for now. I also consolidated the table layout a bit, but noted the combinatoric properties (so the potential symmetry I mentioned is still implied). If you think it needs more work though or that it should be cut, you should probably go for it. This article definitely needs more attention to the technical details. -- [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 17:26, 3 December 2024 (UTC)
 
:Leaving only the relatively obvious varieties makes sense. About the additional columns, I personally see them redundant but having both perhaps helps readers less familiar with algebraic terms. We should defer a potential improvement on this part to a separate discussion. Thanks for the discussion! [[User:J824h|Junghyeon Park]] ([[User talk:J824h|talk]]) 16:09, 4 December 2024 (UTC)
::You're right, the new columns are redundant for anyone that knows the mathematical definitions or can understand the linked articles. But that can be a good thing for new readers.
::Thank you too for talking through it with me; it was kind of fun. Maybe I should get over my bad feelings from university and go back to grad school someday. -- [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 02:39, 5 December 2024 (UTC)
 
== Rust example ==
 
Hi all. I would like to ask regarding the Rust example. It is written in [[Monad (functional programming)#An example: Maybe|Monad (functional programming)#An example: Maybe]] that the Rust example is using <code>Maybe</code>, <code>Just</code>, and <code>Nothing</code>, while Rust actually uses <code>Option</code>, <code>Some</code>, and <code>None</code> respectively [https://doc.rust-lang.org/std/option/]. Is this intended to be written this way? [[User:Mangkoran|Mangkoran]] ([[User talk:Mangkoran|talk]]) 11:19, 19 December 2023 (UTC)
 
:It is not claiming to be in std namespace. While it is probably influenced by the crate https://crates.io/crates/rsmonad, I'm not sure if it's worth mentioning since the API seems pretty generic. Would some clarification help maybe? [[User:J824h|Junghyeon Park]] ([[User talk:J824h|talk]]) 07:36, 19 January 2024 (UTC)
 
== Multiple major issues ==
 
This article reads as an informal guide for programmers, contrary to [[WP:NOTAGUIDE]]. Almost half of all sources are Internet postings, conference talks, and Haskell wiki pages and many, not limited to these types, are strongly opinionated or tutorial; following these, the text comes across as [[WP:OR]] outside a few more encyclopedic sections like History. Normative statements about usage should be contextualized as proper to the norms of functional programming, where they can be supported as such, and otherwise avoided as editorial. Lengthy segments are motivated by only appeals to naturalness, enumerations of supposed advantages, or assertions about what 'many programmers prefer', if at all, and sourced only to guides and documentation, if at all. In my view, major cuts and rewrites are required. Looking at how the subject is handled in other languages, I can recommend [https://de.wikipedia.org/wiki/Monade_(Informatik) the German article] as being a fraction of the length and proceeding from definitions to context to a focused set of examples. [[User:Terminator 2 really happened|Terminator 2 really happened]] ([[User talk:Terminator 2 really happened|talk]]) 22:06, 8 August 2024 (UTC)
 
:@[[User:Terminator 2 really happened|Terminator 2 really happened]]: I worked on this article a while back, and while I don't really plan to much more, I mostly agree with you. I think the one thing that keeps me checking back in is that I never redid the Continuation monad section. And it bugs me for some reason ([[call/cc]] is honestly still a riddle I haven't entirely wrapped my head around).
:If you're ready to prune the article, especially to harmonize with another wiki, I say go for it. My only two thoughts :
:* I think the monad concept itself is difficult enough that it warrants some motivation and exposition. In other words, maybe [[WP:NOTAGUIDE]] should be done with a chisel instead of a sledgehammer on this one. I know my edits were still too conversational in retrospect, but for example, it might still be helpful if sections are allowed to build on each other.
:* Referring to the Haskell wiki pages is definitely less than ideal, but the talk page already saw a lot of back-and-forth on moving away from Haskell more. I think the core issue is that while we can do examples in other languages, we don't have a better idiom than Haskell-ish terms and pseudocode for explaining the actual concepts. A lot of the historical record out there on using monads is based in Haskell too. Even if it's still Haskell-centric, another harder reference (like a book) might be a good first step. At least that gets us away from citing Haskell's community wiki.
:Like I said, I can't dedicate much time to this article going forward, but if you think I can help somehow, feel free to ping me. -- [[User:Zar2gar1|Zar2gar1]] ([[User talk:Zar2gar1|talk]]) 18:05, 21 November 2024 (UTC)
 
== Confusing Introduction ==
The first paragraph refers to two ''operations'': "return : <A>(a : A) -> M(A)" and "bind : <A,B>(m_a : M(A), f : A -> M(B)) -> M(B)". The notation is not explained nor is there a link to a resource to understand them. There is a slight attempt to explain them with "which lifts a value into the monadic context" and "which chains monadic computations", but in introducing the idea of monads &mdash; which I'm still trying to understand &mdash; such things only serve to confuse. For example, what's a ''monadic context'' and why must a value be ''lifted'' into that context?
 
That same paragraph purports to provide a simpler explanation via, "In simpler terms, monads can be thought of as interfaces implemented on type constructors, that allow for functions to abstract over various type constructor variants that implement monad." However, I have no idea what a type constructor is, how functions can ''abstract over various type constructor variants'', or what it means for such type constructors to "implement monad", so how does any of that jargon help me figure out what a monad is?
[[Special:Contributions/141.162.102.57|141.162.102.57]] ([[User talk:141.162.102.57|talk]]) 16:56, 5 February 2025 (UTC)