3 ms·
Monads exist regardless of whether you call them that. They are a mathematical idea. An algebraic structure. If you made a language that used conditionals base
by Retra 11y ago
Monads exist regardless of whether you call them that. They are a mathematical idea. An algebraic structure.
If you made a language that used conditionals based on 1 and 0, it could do boolean logic, even if you didn't include boolean values explicitly.
Monads exist in just about every programming language. They weren't introduced to solve a problem. They already existed, and just happen to be useful to solve certain problems.
- panic 11y agoThey weren't introduced to solve a problem. Monads were definitely introduced to Haskell as a way to solve the problem of I/O -- see Section 7 of http://www.scs.stanford.edu/~dbg/readings/haskell-history.pdf http://www.scs.stanford.edu/~dbg/readings/haskell-history.pd... for more details. It's true that monads existed as a concept in category theory before they were used in programming languages, but they didn't really catch on until their use in Haskell. Certainly you don't see articles about applying other esoteric category theory concepts to Swift programs.
- gohrt 11y agoMonads are generally useful beyond IO, and pure programming style is available (and recommended) in almost every language. From the article you linked: "Although Wadler’s development of Moggi’s ideas was not directed towards the question of input/output, he and others at Glasgow soon realised that monads provided an ideal framework for I/O."
- panic 11y agoYes, definitely. To be clear, I'm a fan (and long-time user) of monads in Haskell, and I think they're a great solution to the problem of embedding impure effects in a pure language. I just don't understand why you'd use them in a language where you can just write the impure effects in a direct style, especially when there's no 'do' notation equivalent.
- Retra 11y agoYou don't usually need them to do IO and state, but they're useful for things like optional and error values. Otherwise, it is mostly pedagogical. Besides, writing things that exist in other languages is useful when you're writing other languages. If you were using C to write a new language with closures (for instance,) you're going to have to implement closures in C. That's a useful exercise, even if nobody is going to use such an inevitably clunky construct in C directly.
- tel 11y agoYou use them precisely when you need a computational/imperative context which differs from the natural ambient one of your language. E.g., Promises in Javascript.
- woah 11y agoI've read that synchronously executed code from a text file is actually a form of monad in itself. Is this true?
- panic 11y agoYour question is meaningless as you've written it. An object equipped with a "return" and a "bind" operation is a monad if the operations follow the three monad laws of left identity, right identity and associativity. To say whether something is a monad or not, you must first identify the object you're talking about and describe its "return" and "bind" operations.
- lmm 11y agoSequences of bytes form a monad. Sequences of machine instructions form a monad. (Indeed, for any x, sequences of x form a monad). This is occasionally a useful fact (e.g. it's what tells you that you can debug an assembly program by debugging the first half, then debugging the second half starting from the state it was in at the end of the first half). The definition of a monad is really simple - a lot of things form monads. Which is what makes them so powerful, because if you write a function that works on a generic monad, you find you can use that function for a lot of things.