What's so special about Monads in Kleisli category?

what'd
what w
how to pronounce what
what's
what c
what does
what h
what p

The related Question is

  1. What is so special about Monads?

  2. bind can be composed of fmap and join, so do we have to use monadic functions a -> m b?

In the first question:

What is so special about Monads?

A monad is a mathematical structure which is heavily used in (pure) functional programming, basically Haskell. However, there are many other mathematical structures available, like for example applicative functors, strong monads, or monoids. Some have more specific, some are more generic. Yet, monads are much more popular. Why is that?

The comment to reply the question:

As far as I recall, monads were popularised by Wadler, and at the time the idea of doing IO without tedious CPS and parsing without explicit state passing were huge selling points; it was a hugely exciting time. A.F.A.I.R., Haskell didn't do constructor classes, but Gofer (father of Hugs) did. Wadler proposed overloading list comprehension for monads, so the do notation came later. Once IO was monadic, monads became a big deal for beginners, cementing them as a major thing to grok. Applicatives are much nicer when you can, and Arrows more general, but they came later, and IO sells monads hard. – AndrewC May 9 '13 at 1:34

The answer by @Conal is:

I suspect that the disproportionately large attention given to this one particular type class (Monad) over the many others is mainly a historical fluke. People often associate IO with Monad, although the two are independently useful ideas (as are list reversal and bananas). Because IO is magical (having an implementation but no denotation) and Monad is often associated with IO, it's easy to fall into magical thinking about Monad.

First of all, I agree with them, and I think the usefulness of Monads mostly arises from Functors that we can embed many functions within the structure, and Monads is a little expansion for robustness of function composition by join : M(M(X)) -> M(X) to avoid the nested type.

In the 2nd Question:

do we have to use monadic functions a -> m b?

so many tutorials around the web still insist to use a monadic functions since that is the Kleisli triple and the monad-laws.

and many answers like

I like to think of such an m as meaning "plan-to-get", where "plans" involve some sort of additional interaction beyond pure computation.

or

In situations where Monad isn't necessary, it is often simpler to use Applicative, Functor, or just basic pure functions. In these cases, these things should be (and generally are) used in place of a Monad. For example:

ws <- getLine >>= return . words  -- Monad
ws <- words <$> getLine           -- Functor (much nicer)

To be clear: If it's possible without a monad, and it's simpler and more readable without a monad, then you should do it without a monad! If a monad makes the code more complex or confusing than it needs to be, don't use a monad! Haskell has monads for the sole purpose of making certain complex computations simpler, easier to read, and easier to reason about. If that's not happening, you shouldn't be using a monad.

Reading their answers, I suppose their special feeling about Monad arises from the historical incident that Haskell community has happend to chose Monads in Kleisli category to solve their problem(IO etc.)

So, again, I think the usefulness of Monads mostly arises from Functors that we can embed many functions within the structure, and Monads is a little expansion for robustness of function composition by join : M(M(X)) -> M(X) to avoid the nested type.

In fact, in JavaScript I implemented as below..

Functor

console.log("Functor");
{
  const unit = (val) => ({
    // contextValue: () => val,
    fmap: (f) => unit((() => {
      //you can do pretty much anything here
      const newVal = f(val);
    //  console.log(newVal); //IO in the functional context
      return newVal;
    })()),
  });

  const a = unit(3)
    .fmap(x => x * 2)  //6
    .fmap(x => x + 1); //7
}

What, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  Quickly send and receive WhatsApp messages right from your computer.

The point is we can implement whatever we like in the Functor structure, and in this case, I simply made it IO/console.log the value.

Another point is, to do this Monads is absolutely unnecessary.

The problem is that once you do that your functor is no longer a functor. Functors should preserve identities and composition. For Haskell Functors, those requirements amount to:

fmap id = id
fmap (g . f) = fmap g . fmap f

Those laws are a guarantee that all fmap does is using the supplied function to modify values -- it doesn't do funny stuff behind your back. In the case of your code, fmap(x => x) should do nothing; instead, it prints to the console.

Note that all of the above applies to the IO functor: if a is an IO action, executing fmap f a will have no I/O effects other than those a already had. One stab at writing something similar in spirit to your code might be...

applyAndPrint :: Show b => (a -> b) -> a -> IO b
applyAndPrint f x = let y = f x in fmap (const y) (print y)

pseudoFmap :: Show b => (a -> b) -> IO a -> IO b
pseudoFmap f a = a >>= applyAndPrint f

... but that makes use of Monad already, as we have an effect (printing a result) which depends on the result of a previous computation.

It goes without saying that if you are so inclined (and your type system allows it) you can write code that disregards all of those distinctions. There is, however, a trade-off: the decreased power of Functor with respect to Monad comes with extra guarantees on what functions using the interface can and cannot do -- that is what makes the distinctions useful in the first place.

Drake - Nice For What, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. informal a punishment or reprimand (esp in the phrase give (a person) what for) 7. what have you someone, something, or somewhere unknown or unspecified: cars, motorcycles, or what have you. 8.

Your "functor" is very manifestly not a functor, violating both the identity and composition law:

console.log("Functor");
{
  const unit = (val) => ({
    // contextValue: () => val,
    fmap: (f) => unit((() => {
      //you can do pretty much anything here
      const newVal = f(val);
      console.log(newVal); //IO in the functional context
      return newVal;
    })()),
  });

  console.log("fmap(id) ...");
  const a0 = unit(3)
    .fmap(x => x);      // prints something
  console.log("         ≡ id?");
  const a1 = (x => x)(unit(3));   // prints nothing

  console.log("fmap(f) ∘ fmap(g) ...");
  const b0 = unit(3)
    .fmap(x => 3*x)
    .fmap(x => 4+x);     // prints twice
  console.log("                   ≡ fmap(f∘g)?");
  const b1 = unit(3)
    .fmap(x => 4+(3*x));    // prints once
}

what, Usage Note: When what is the subject of a clause, it takes a singular verb if the word or phrase that completes the sentence (the complement) is singular, as in I​  noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

overly long comment:

I would suggest forgetting about Kleisli categories for now; I don't believe they have anything to do with your confusion.

Also while I still don't fully understand your question and assertions, some context that might be useful: category theory is extremely general and abstract; the concepts like Monad and Functor as they exist in haskell are (necessarily) somewhat less general and less abstract (e.g. the notion of the category of "Hask").

As a general rule the more concrete (the less abstract) a thing becomes, the more power you have: if I tell you you have a vehicle then you know you have a thing that can take you from one place to another, but you don't know how fast, you don't know whether it can go on land, etc. If I tell you you have a speed boat then there opens up a whole larger world of things you can do and reason about (you can use it to catch fish, you know that it won't get you from NYC to Denver).

When you say:

What's so special about Monads in Kleisli category?

...I believe you're making the mistake of suspecting that the conception of Monad and Functor in haskell is in some way more restrictive relative to category theory but, as I try to explain by analogy above, the opposite is true.

Your code is the same sort of flawed thinking: you model a speedboat (which is a vehicle) and claim it shows that all vehicles are fast and travel on water.

WHAT, What Goes Up Must Come Down. Lewis Rothlein and Jeff Chen raise us high and drop us low. A herd of elephants tippy-toed down to their  WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

What, What's not clear to many business leaders is what digital transformation means. Is it just a catchy way to say moving to the cloud? What are the specific steps we  Value Variety Choice - What! Stores have everything you need from craft materials to furniture, from garden tools to homeware and more - all under one roof at value prices.

What Goes UpMust Come Down, For many, their pay — for being essential, with hazard pay — is lower than what the federal government is paying in unemployment relief. This is the ORIGINAL "What What (In the Butt)" video. The South Park version aired over a year after we made this video. There's been a lot of confusion about this.

What is digital transformation?, What you need to know about coronavirus on Saturday, May 30. By Ivana Kottasová, CNN. Updated 7:43 AM ET, Sat May 30, 2020. latin america covid 19​  Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

Comments
  • I don't get your question. Yes, flattening nested structures is exactly what Monads can do that Functors cannot. That's why we use two different terms for two different concepts.
  • Branching on result.contextValue !== undefined makes no sense. You should not overload bind to do fmap as well. Also, I don't get what point you are trying to make with that example?
  • @Bergi I wrote " Is there any counter example that Functors cannnot do what Monads can do except the robustness of functional composition by flattening the nested structure?" and other got the idea.Sorry, but your comment is not helpful.
  • You must be the same person who's kept asking questions just like this on several different accounts over the last week or so. Why do you keep creating new accounts?
  • I don't use several accounts to ask.
  • Ok, ""Avoiding the nested type" is not in fact the purpose of join, it's just a neat side-effect. The way you put it makes it sound like join just strips the outer type, but the monad's value is unchanged. This is not the case. join is the heart of a monad, and it's what allows the monad to do things." I don't see any definition of Monads in categort theory explains this. All I read about join is stil MMX => MX and that is exactly what my code does.
  • "A monad is a functor with a special power: where fmap allows you to work on an inner value, bind allows you to combine outer values in a consistent way. This is much more powerful than a simple functor." There is MaybeFunctor in Haskell. There's no "join" or "bind" there, and I wonder from where the power come. What is the difference between MaybeFunctor and MaybeMonad?
  • @SmoothOperator Quoting your edit: "there is Maybe functor in Haskell, that does not has join". But Maybe has a join, like any other Monad does.