std.monad

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 a a0 a1 . [std.monad.Monad a] -> (a0 -> a a1) -> a a0 -> a a1

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 a a0 a1 . [std.monad.Monad a] -> a a0 -> (a0 -> a a1) -> a a1

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

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

let when b m : forall a . [std.monad.Monad a] -> std.types.Bool -> a () -> a ()