Hacker News new | ask | show | jobs
by crimsonalucard 2435 days ago
You can have monads in any language. If you have a functor you have a monad. If you have a type that wraps or encodes another type in a lossless or lossy way you have a functor. Therefore a functor exists in any language with compound types therefore every language that has compound types has monads.

The difference is Haskell makes these concepts explicit.

1 comments

I would be interested in learning more about how does Haskell make monads "explicit"?

Does Haskell have the keyword "monad"?

Does Haskell have support for ensuring that monadic laws hold for any prospective monad?

Haskell provides it as an "interface" in the "standard library"

http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelud...

You have to implement the methods but things like associativity and the existence of identity are not enforced.

Additionally all IO functions in haskell are monads with a return type that is a functor called IO that can contain another type or nothing. You cannot print something to console without invoking a monad in haskell. The clever thing about the IO functor type is that it can only be instantiated by a true IO function that touches some external thing, there is no other way to instantiate this type.

So for example the Instantiated type IO(int) cannot exist unless someone called a function that receives integers from a socket. And to extract the int from the functor you have to use the monadic bind operator.

I see. Thanks. So the library-interface named "monad" includes the special machinery for programming monads. Libraries written in other languages could provide something similar, I assume.
Yeah. There's nothing special, it's purely a library which you can reimplement yourself in haskell or in other languages.

The only thing special about it is the IO thing I mentioned which is an intrinsic part of the language.

I know you mentioned javascript earlier, just know that while javascript CAN technically have monads, you kind of have to go out of your way to implement it. If you get some experience with a language that has an GADT system and really nail down the concept of a functor you'll see the full power of the monad.

I generally would avoid using a monad with languages that don't have GADT systems.