std.monad

Modules

Implementation of the Monad type

Types

type Monad m = {
    applicative : std.applicative.Applicative m,
    flat_map : forall a b . (a -> m b) -> m a -> m b
}

A generalised interface for imperatively sequencing actions

Values

let flat_map ?m : forall b a m . [std.monad.Monad m] -> (a -> m b) -> m a -> m b

This can be seen as akin to sequential variable binding in an imperative language. For example in Javascript:

var x = call_fallible("hello");
do_something(x);

In gluon this would look like:

result.monad.flat_map (\x -> do_something x) (call_fallible "hello")

Note that it is sometimes more ergonomic to use the (>>=) operator:

let { (>>=) } = import! std.prelude

call_fallible "hello" >>= (\x -> do_something x)
Note
  • Known as (=<<) orflip (>>=)` in Haskell
  • Known as Option::and_then and Result::and_then in Rust

let >>= x f : forall b a m . [std.monad.Monad m] -> m a -> (a -> m b) -> m b

let =<< : forall b a m . [std.monad.Monad m] -> (a -> m b) -> m a -> m b

let join mm : forall a m . [std.monad.Monad m] -> m (m a) -> m a