TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The “What Are Monads?” Fallacy

45 pointsby psibiover 10 years ago

13 comments

olavkover 10 years ago
I think it is a cultural mismatch. The Haskell community seem to be very influenced by mathematicians and their way of describing everything as abstractly a possible. You can even see it in code examples where it is pretty common to see arbitrary one-letter variable names.<p>The software development community is by and large much more pragmatic and focused on solving real-world problems. They have an easier time understanding concepts and code examples when explained in the context of real-world problems.<p>So when the Haskell people try to explain monads, they start with the abstract definition, the monadic laws and so on, and they focus on explaining the <i>concept</i> of a monad, rather than what they are practially useful for. Even the many infamous monad-metaphors (its like a spacesuit! No its like a burrito!) try to find a metaphor for the abstract concept of a monad, not for any practical use. For the typical developer this might seem like a lot of mathematical wanking for little benefit.<p>The language F# seem to have a more &#x27;pragmatic&#x27; culture. &#x27;Monads&#x27; are called &#x27;computation builders&#x27; in F#, and the equivalent to do-notation is called &#x27;computation expression&#x27;. Yeah it is just naming, but it shows a focus on the <i>use</i> of monads rather than on the underlying mathematical concept.<p>A typical F# example of the use of monads - sorry, <i>computation builders</i> is the Async monad:<p><pre><code> let AsyncHttp(url:string) = async { let req = WebRequest.Create(url) let! rsp = req.GetResponseAsync() use stream = rsp.GetResponseStream() use reader = new System.IO.StreamReader(stream) return reader.ReadToEnd() } </code></pre> This example fetches a resource over http without blocking. It is immediately obvious why this is useful. After all, similar functionality was recently added to C# through the keywords &#x27;async&#x27; and &#x27;await&#x27;. But in F# it didn&#x27;t require the addition of new keywords but could be implemented as a library, due to support for monads in the language.
评论 #8826338 未加载
评论 #8827106 未加载
sillysaurus3over 10 years ago
The fact that no one is able to show a canonical, succinct example of a monad makes me suspect it&#x27;s multiple concepts under a single banner. But I haven&#x27;t spent much time trying to understand what one is.<p>EDIT: Thanks for the responses. I&#x27;d love to study any resources that you found useful when learning Monads. Do you know of any?
评论 #8826064 未加载
评论 #8826175 未加载
评论 #8826188 未加载
评论 #8826428 未加载
评论 #8826026 未加载
评论 #8826239 未加载
评论 #8826172 未加载
评论 #8826023 未加载
评论 #8826080 未加载
评论 #8828212 未加载
评论 #8826055 未加载
评论 #8826017 未加载
评论 #8829585 未加载
flebronover 10 years ago
It&#x27;s kind of ironic that the author states &quot;In his article, Byorgey hints at what I&#x27;m going to say, but I think it deserves to be said again, with slightly different words.&quot;, which is essentially what monad tutorials say about other monad tutorials :)
评论 #8826063 未加载
fubarredover 10 years ago
There&#x27;s a bigger issue: using computational and mathematical complexity that is confusing to large swaths of mere mortal coders. This is sadly why languages, to pick on some easy target religions like PHP and so on have proliferated.<p>Sure it&#x27;s possible to reduce LoC and look clever with ever more complex representations of computational structures, but it&#x27;s another thing to write supportable code that&#x27;s straightforward to debug, improve and maintain.<p>If someone wants to use a &quot;research&quot; language in production and&#x2F;or want perpetual job security, there are plenty of languages for those requirements too.<p>(There&#x27;s never a perfect Turing power language for anything, only better fits than others. Clarity for a new-hire reader should be most valued goal after functionality.)
评论 #8825929 未加载
评论 #8826302 未加载
评论 #8826010 未加载
hmexxover 10 years ago
I&#x27;ve never really done anything seriously in Haskell, but this is how I feel monads could be explained through example. Let me know what you guys think. If it&#x27;s good I&#x27;ll add it to the stackoverflow question on Monads.<p>---<p>We know how operators (add, substract, multiply,...) work on integers. example: 5 + 6 = 11.<p>Now let&#x27;s add some extra state&#x2F;dimension to integers. Let&#x27;s assume this extra state is ROCK, PAPER or SCISSORS.<p>Here are possible values:<p>5_ROCK<p>6_PAPER<p>999_SCISSOR<p>...etc...<p>---<p>What is the result of 5_ROCK + 6_PAPER ?<p>There is no answer unless someone defines it. That&#x27;s what a monad does. In this case I am deciding that 5_ROCK + 6_PAPER = 11_PAPER<p>The way MY monad works is to use the operator on the integer portion ( 5 + 6) and then use the winner of ROCK, PAPER, SCISSORS game as the non-integer portion.<p>3_ROCK * 4_SCISSOR = 12_ROCK<p>8_SCISSOR &#x2F; 2_PAPER = 4_SCISSOR<p>...etc...
评论 #8828768 未加载
评论 #8826290 未加载
logicalleeover 10 years ago
I&#x27;m really sorry author, but this is how I read your post:<p>&gt;<i>This is like someone new to a language with regular expressions asking what a regular expression, is, or someone new to object oriented programming asking what a class or object is, or someone new to functional programming asking what functional programming is, or someone new to node.js asking what node.js is, or someone new to programming asking what a program is.<p>&gt;What&#x27;s _wrong_ with you people? These are horrible questions. I refuse to answer them, and you should know better than to ask. Yes, I could illustrate, but why should I?<p>&gt;I prefer to just explain that it&#x27;s a question that won&#x27;t help you in the slightest. I&#x27;m not going to illustrate something that will, or give you any examples.<p>&gt;Just do it. The important thing is, not in front of me.</i><p>Author, you could vastly improve your blog post by actually doing the teaching instead of sending the student off.
评论 #8826305 未加载
jamesfisherover 10 years ago
For the same reason, we should not say, &quot;use the IO monad to do output&quot;. We should just say, &quot;use IO to do output&quot;.<p>You may not even need the monad instance for IO -- for example, you might just use its functor instance.
评论 #8826645 未加载
dschiptsovover 10 years ago
Each narcissistic blogger should at least once write about monads - <a href="http://karma-engineering.com/lab/wiki/Monads2" rel="nofollow">http:&#x2F;&#x2F;karma-engineering.com&#x2F;lab&#x2F;wiki&#x2F;Monads2</a>
Scea91over 10 years ago
I don&#x27;t agree with the blog post since the same argument could be made for any design pattern. There is nothing wrong at trying to understand things at a higher level of abstraction.
评论 #8826542 未加载
评论 #8826198 未加载
bsaulover 10 years ago
i think i got what functors are in category theory ( aka morphisms between categories , IIUC ), but i can&#x27;t understand what it is in haskell. There&#x27;s a link missing in my head between category theory and programming language.<p>I also couldn&#x27;t find a post or a video that would explain what monads are in cateogy theory.
评论 #8828687 未加载
评论 #8826525 未加载
toddkaufmannover 10 years ago
Who&#x27;s on first ?<p>No results found for &quot;abbott and costello explain monads&quot;.
crispweedover 10 years ago
Is there an rss feed for this blog?
评论 #8826545 未加载
michaelsbradleyover 10 years ago
Having also struggled with Monads before having an <i>aha!</i> moment, I&#x27;ve tried on occasion to help boil it down for those on the search for understanding.<p>The hardest thing to get is that Monads aren&#x27;t quite as &quot;tangible&quot; (not sure of a better word) as concepts like variable, object, property, instance, function, method. They are essentially relational and require something of a mental leap akin to coming to grips with how JavaScript&#x27;s event loop is bound up in the control flow of your code, which otherwise proceeds from top to bottom of a .js file; or the rules governing closures and asynchronous function evaluation in this or that language.<p>The following is reprised from one of my HN comments in 2013.<p>-------<p>Monads are <i>programming patterns</i> which relate a set of values to a set of of functions – that&#x27;s pretty much it! For any monad X, we can say that a function is monadic if it returns a value in the X Monad. The set of values are precisely those such that the following three Monad Laws[1] hold:<p>[ Using JavaScript syntax, they can be approximated as... ]<p>Left Identity<p><pre><code> mBind( mReturn(a), f ) == f(a); &#x2F;&#x2F; =&gt; true </code></pre> Right Identity<p><pre><code> mBind( mReturn(a), mReturn ) == mReturn(a); &#x2F;&#x2F; =&gt; true </code></pre> Associativity<p><pre><code> mBind( mBind( mReturn(a), f ), g ) == mBind( mReturn(a), function(a) { return mBind( f(a), g ); } ); &#x2F;&#x2F; =&gt; true </code></pre> Above, `a` is (basically) any value, while `f` and `g` are any monadic functions for the same Monad. The definitions of `mBind` and `mReturn` will vary depending on the Monad, but the same laws (and sometimes additional properties) hold for each group of things taken as a whole – the monadic values, monadic functions, and the `mBind` and `mReturn` pair for those values and functions.<p>As you can see, there is nothing special that requires a statically typed language. That being said, if your language is statically typed, and even more so if its type system has advanced capabilities (e.g. Haskell&#x27;s type system), then the relationships between the monadic values and functions can be leveraged to do a number of useful things, e.g. catching a host of bugs at compile time (though that&#x27;s true regardless of Monads).<p>If your language is dynamically typed, then you won&#x27;t get those extra benefits, but you can still take advantage of the fact that Monads can abstract away a great deal of plumbing between various pieces of your program.<p>Sometimes the <i>Monad patterns</i> will appear in a language under another name, or will be used &quot;under the hood&quot; to implement a particular API. Clojure&#x2F;Script&#x27;s `let` for example is essentially the Identity Monad; and its `for` is very much akin to the List Monad (the same is true for list comprehensions in other languages). The core.async library involves an inspiring application of the State Monad pattern[2].<p>[1] <a href="http://en.wikipedia.org/wiki/Monad_(functional_programming)#Monad_laws" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Monad_(functional_programming)#...</a><p>[2] <a href="https://github.com/clojure/core.async/blob/master/src/main/clojure/clojure/core/async/impl/ioc_macros.clj#L56" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clojure&#x2F;core.async&#x2F;blob&#x2F;master&#x2F;src&#x2F;main&#x2F;c...</a><p>[&amp;] <a href="https://github.com/clojure/core.async/blob/master/src/main/clojure/cljs/core/async/impl/ioc_macros.clj#L46" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;clojure&#x2F;core.async&#x2F;blob&#x2F;master&#x2F;src&#x2F;main&#x2F;c...</a>
评论 #8826167 未加载