Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A monad is a triple:

1. A type constructor M of a single argument 2. A function of type a -> M a 3. A function of type M (M a) -> M a

subject to the monad laws. So the 'array monad' is the triple (Array, \x -> [x], Array.concat) and the 'option monad' is the triple (Option, \x -> Some(x), \x -> match x | Some(o) => o; None => None). Saying a type 'is' a monad is imprecise but just means it has an associated monad instance. I've never seen a type with multiple monad instances but I don't know if there could be one.



> I've never seen a type with multiple monad instances but I don't know if there could be one.

There can, it's just like there can two monoid structures on the same set. In fact if you have an example of a set S which has two monoid structures, you have an example of a functor with two monad structures: _ × S. join multiplies the two elements of S, so you get a different monad structure for every monoid structure on S.


Wow that's clearer. I wonder if just a list of examples (and counter-examples) would be enough to explain monads (preferrably with configurable syntax for Haskell-deprived folks).

Btw, I think you got a mistake there, in the third part of Option should be "Some(Some(o)) => Some(o), Some(None) => None, None => None", otherwise o can be not an Option at all.


Your first two cases are equivalent to the `Some(o) => o` case; the syntax I used was a bit ad-hoc but the following F# typechecks:

    let join_option oo =
        match oo with
        | Some(o) -> o
        | None -> None




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: