3 ms·
I got lost at the monads bit. Where's the best place to learn about them? I did basic Abstract Algebra in university, and I understand how to use the Maybe/Opti
by lacampbell 10y ago
I got lost at the monads bit. Where's the best place to learn about them? I did basic Abstract Algebra in university, and I understand how to use the Maybe/Option/Optional monad.
- empath75 10y agohttps://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/ https://bartoszmilewski.com/2014/10/28/category-theory-for-p...
- danidiaz 10y agoThose articles (and the videos https://www.youtube.com/watch?v=I8LbkfSSR58&list=PLbgaMIhjbmEnaH_LTkxLI7FMa2HsnawM_ https://www.youtube.com/watch?v=I8LbkfSSR58&list=PLbgaMIhjbm...) are great but one doesn't need to learn category theory to understand monads in Haskell. The Haskell Wikibook is an underrated resource: https://en.wikibooks.org/wiki/Haskell/Understanding_monads https://en.wikibooks.org/wiki/Haskell/Understanding_monads
- lacampbell 10y agoHmm, this stuff looks familiar, I tried to learn it once upon a time. The operators make sense to me except for "return" which just seems like a hack, or excessive syntax sugar. If you already have type constructors, why the magic "return" function that needs to sniff the surrounding scope to see what kind of monads you are actually dealing with?
- gmartres 10y agoThe whole point of monads (and most other typeclasses) is that they're an abstraction. If you use a type constructor then you cannot generalize your function to work with any kind of monad, see for example the definition of `mapM` in https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GH... which uses `return`
- gsg 10y agoreturn isn't syntax, it's a normal function of type Monad m => a -> m a. The "sniffing" that you are talking about is that overloading is done on the return type. Why does it exist at all? Speaking loosely, monads are about composing functions that look like a -> m b. Here's regular composition alongside the 'monadic' one: (.) :: (b -> c) -> (a -> b) -> (a -> c) (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c) As you can see, the same except for the m wrapping each function's results. Just like normal function composition, there is an identity element - that's what return is. Again, a comparison: id :: a -> a return :: Monad m => a -> m a Again, the same except for the m wrapping the result. Both composition operators are associative, and both have the same relationship with their identity operation: id . f = f return <=< f = f f . id = f f <=< return = f (That's why there are monad laws, to ensure those properties hold.) So return is very straightforward: it is the identity of functions that return monads. It's unfortunate that both the name and the presentation of monads in Haskell tend to obscure this.
- lacampbell 10y agoI think what's confusing me here is haskells type classes, which I've never found intuitive (despite having "learned" them 3 or 4 times already..). The definition of return still baffles me, in the absence of some kind of module or object on its left hand side, I can't make sense of how it "knows" how to overload. I'm probably better of learning monads in the context of a language I'm more familiar with, like ocaml. I appreciate the effort though.
- gsg 10y agoVery understandable, type classes + inference + monads + do notation is quite a stack of unfamiliar machinery to understand all at once. The return type is inferred, and the type is what the type class machinery is driven by. You can get some more visibility into that by playing in ghci: return 0 :: [Int] => [0] return 0 :: Maybe Int => Just 0 Having behaviour driven by return type takes a bit of getting used to.
- 10y ago
- runeks 10y agoA monad is an environment in which we can sequence actions, and where the "sequencing functions" (>> and >>=) decide whether to continue or stop. The Maybe monad stops when it encounters a Nothing result, and continues when it encounters a Just. So: Just 1 >> Nothing >> Just 5 will return Nothing, whereas Just 1 >> Just 2 >> Just 5 will return Just 5. >>= is used to take the value inside the monad (Just) for the left-hand argument, and stuff it into the right-hand argument, like so: Just "hello" >>= Just . (++ " world") returns Just "hello world".
- lacampbell 10y agoThose two operations make sense to me. Type signatures and all. Is that all that is needed for something to be a monad?
- moomin 10y agoThe operations also need to obey the "Monad Laws" https://wiki.haskell.org/Monad_laws https://wiki.haskell.org/Monad_laws. But the intuitive version is that anything with those two operations that doesn't smell funny is a Monad. (Think of it like implementing an interface: anything that matches the type signature will work, but if you write something silly, it won't work right)
- dllthomas 10y agoThe two things you need are actually bind (>>=) and return. As mentioned, though, you should also be sure it obeys the relevant laws.
- moomin 10y agoDo this course: http://www.cis.upenn.edu/~cis194/spring13/lectures.html http://www.cis.upenn.edu/~cis194/spring13/lectures.html I know it sounds ridiculous but a) it took me about seven evenings b) it's a lot of fun and c) I guarantee you'll get some proper programming insight, whatever language you use for a day job.
- BoiledCabbage 10y agoI second this, it's a great course.