What is a monad?

What is a monad?

Attached: 602px-Haskell-Logo.svg.png (602x425, 4K)

Other urls found in this thread:

homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf
blog.acolyer.org/2014/11/10/monads-for-functional-programming/
en.wikipedia.org/wiki/Monad_shell
ghc.haskell.org/trac/ghc/ticket/14066?cversion=0&cnum_hist=2
twitter.com/NSFWRedditGif

A group without inverse proprieties.

a method of controlling side effects

Something with zero, append, and is associative.

the haskell guys who claim that this is complex and difficult to understand aren't able to differentiate from their maths with the code imo.

functionally, imagine you have a union type in c. one value inside the union is a state which you'd like to operate on. the other possible state is one you'd like to short circuit and return.

now imagine c code where a function returns a pointer type.

if ((a = something_something()) != NULL) {
if ((b = something_something_else(a)) != NULL) {
do_something_else(b);
} else {
return NULL;
}
} else {
return NULL;
}

pretty verbose right?

what if i used a monad instead to encapsulate the possible state and defined an infix operator to interact with it?

i could write the following (this is pseudocode):

function (>>=)(a, f) {
if (a != NULL) f(a)
else NULL;
}

return (
something_something >>= fun a ->
something_something_else(a) >>= fun b ->
do_something_else(b)
);

and it would be the functional equivalent. monads need to implement the functions "bind", "return" and "map" iirc. a good reference place is rust's standard library with the option type imho.

Let me see if I understand this:

For a function f (g a b) there are two outcomes:
if some a and some g(a) then g(b)
else none

Is that about right? Why is this a big deal?

homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf

blog.acolyer.org/2014/11/10/monads-for-functional-programming/

The big deal I believe is you're able to do this while having total type safety. Bind is the concat operation on the type level.

that's it. monads are considered a big deal by some because they can be used to describe abstract control flow. they can be (as another poster has described) used to describe side effects for instance (ie an "effect monad"), a good example of this is the io monad in haskell. using another feature called a "functor" you could define behaviour that plays nice with "fmap" so you could have code that performs the same with a list containing integers and a monad containing an optional type wrapping a monad.
it's basically a control flow thing. another good example imo is ocaml's lwt monad type for asynchronous code (ie make a get request, then consume the body and turn into a string, using an infix operator to describe each step of the way).

there's no big deal, it's just autism of haskellers. Although it's a pretty nice pattern for error handling, I wish it was more popular in c++.
Fundamentally in haskell it lets you hide io in a 'functional' code.
You can make io 'functional' by creating a dummy argument that's different each time.
state = 0
[int a, state] = readInt(state);
state = print(a, state);
[int b, state] = readInt(state);
state = print(b, 2);

This code is functional because subsequent executions depend on previous, and each state argument is different, so technically readInt is pure - for each x, readInt(x) == readInt(x)
and operations must be executed in the correct order.
It would be tiring to manually pass state each time, so io monad hides that from you.
That's pretty much it.

>This code is functional because subsequent executions depend on previous
can you please stop posting here, or at least stop pretending you know what you're talking about

he probably means functional in the sense that the code doesn't short circuit

A monad is just a monoid in the category of endofunctors.

what I wrote is correct, that's exactly how inherently imperative io is implemented in pure functional languages

>monad
array

a better word then would've been 'declarative' (vs imperative) rather 'functional'

obligatory

Attached: philosophyJeopardy1.png (1000x1500, 494K)

>Freud's score didn't decrease
dropped

>state = print(b, 2);
that should have been
state = print(b, state)

It didn't decrease at the end because he was right

may the monad be with you

Attached: starWars1.png (1000x1500, 557K)

kill yourself, please.

This is the only right answer.

second angry reaction.
Did you derive your sense of self worth from using monads or what?

then it should have went up

No, I just find it really disturbing how much bullshit about monads there is on the internet.

The example you wrote about modelling state is neither monadic nor functional. Also using monads for modelling state and for modelling IO is not the same, the io monad doesn't hide any state from you.

So please, kill yourself.

Monad is the original name for what is now known as "Powershell".

en.wikipedia.org/wiki/Monad_shell

>What is a monad?

Half of a gonad obviously.

>cmdlets
when will they learn?

My intuition is that it's a tag type that says that obtaining this value will possibly have side effects. It allows you to compose pure functions (which are super modular) with the imperative, step-by-step programming that most people are used to.

Haskell it like hidden langiage of retards, if some normalone speek it it gives em RAGE

Attached: SC_SCAM.png (224x250, 4K)

Pretty much everywhere you said "functional" should be "declarative."

>The example you wrote about modelling state is neither monadic nor functional
It's functional and does what a monad does in a more verbose way.
If readInt was made to pointlessly accept an unneeded int it's equivalent to:
ReturnType r = print(readInt(print(readInt(ReturnType(x, state)))))
The state argument is hidden by IO() because it doesn't actually do anything, but ensure that each io function gets a different argument from all previous executions.
No alternative way to execute imperative actions in a pure functional context exists.
Every program is executed in an IO monad and can't escape it in type system context (outside of explicit unsafe hacks).

The bind and return operator that make hiding easy can also be used to simplify other things like showed.

There's nothing complicated about monads and they can be implemented in eg. c++ (although with less robust type checking), but almost every explanation by a haskell programmer makes it into some magical complicated thing.

btw Haskell's (or rather, GHC's, but that's synonymous in practice) type system isn't that robust:
ghc.haskell.org/trac/ghc/ticket/14066?cversion=0&cnum_hist=2
functional code is a subtype of declarative. Logic programming (eg. prolog) is declarative too, but not functional.

>but almost every explanation by a haskell programmer makes it into some magical complicated
Haskell it like hidden langiage of retards

A monad is just a monoid in the category of endofunctors.

It's the ten underground levels of the Tartarus in Persona 3.

If it satisfies the monad laws, it's a monad.

Attached: Son-this-is-bait_(1).png (457x480, 51K)

That would be a monOID.

These are the only correct answers. There are many examples of monads that model control and side effects, but they are just examples. There are monads that don't really do either of these things, such as abstract syntax trees under substitution of variables.

All you need to know is that "monad" is a particular interface with a few invariants. Category theory won't give you a better intuition for programming with monads, it will just tell you where the name came from.

A monad is a data structure you can apply

(>>=) :: Monad m => m a -> (a -> m b) -> m b

on.

You basically have a data structure and a function that does something to said data structure and you use >>= ("bind") to connect them.

The idea is comparable to Functors. Functors are data structures to which you can apply the map function, simple as that.

You have a bunch of computational descriptions.
Monads are a way of composing those descriptions. It's a pattern, therefore it is abstract, because you don't need to write specific code for each case. If you can deduce that something is a monad, you can use the same properties everywhere, like (>>=) bind. Be it side effects in IO, nested if-else cases like the Maybe monad, sequencing operations etc. etc.
You can also think of monad as a context: this code is in the context of having an effect on the world, therefore the IO monad. Something like that. This should be enough to get you started.
The real power is that you can compose all this stuff. Therefore, you can write 100 lines of Java code in 10 lines of Haskell.