package travesty

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

S is the signature of state monads parametrised over their value, but with a fixed state type.

type state

The fixed state type.

include Base.Monad.S
type 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.

val (>>|) : 'a t -> ('a -> 'b) -> 'b t

t >>| f is t >>= (fun a -> return (f a)).

module Monad_infix : sig ... end
val bind : 'a t -> f:('a -> 'b t) -> 'b t

bind t ~f = t >>= f

val map : 'a t -> f:('a -> 'b) -> 'b t

map t ~f is t >>| f.

val join : 'a t t -> 'a t

join t is t >>= (fun t' -> t').

val ignore_m : 'a t -> unit t

ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Caml.ignore. Some monads still do let ignore = ignore_m for historical reasons.

val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t

Like all, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.

module Let_syntax : sig ... end

These are convenient to have in scope when programming with a monad:

include Monad_exts.S with type 'a t := 'a t

Haskell-style operators

val then_m : _ t -> 'a t -> 'a t

then_m x y sequentially composes the actions x and y as with >>=, but, rather than using the returned value of x, it instead just returns y.

val (>>) : _ t -> 'a t -> 'a t

x >> y is then_m x y.

val compose_m : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t

compose_m f g is the Kleisli composition of f and g.

val (>=>) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t

x >=> y is k_compose x y.

Guarded monadic computations

val map_when_m : ?otherwise:('a -> 'a t) -> bool -> 'a -> f:('a -> 'a t) -> 'a t

map_when_m ?otherwise condition ~f a is f a when condition is true, and otherwise a (by default, return) otherwise.

val when_m : ?otherwise:(unit -> unit t) -> bool -> f:(unit -> unit t) -> unit t

when_m ?otherwise condition ~f is f () when condition is true, and otherwise () (by default, return) otherwise.

val map_unless_m : ?otherwise:('a -> 'a t) -> bool -> 'a -> f:('a -> 'a t) -> 'a t

map_unless_m ?otherwise condition ~f a is f a when condition is false, and otherwise a (by default, return) otherwise.

val unless_m : ?otherwise:(unit -> unit t) -> bool -> f:(unit -> unit t) -> unit t

unless_m ?otherwise condition ~f is f () when condition is false, and otherwise () (by default, return) otherwise.

Executing monadic effects in the middle of pipelines

val tee_m : 'a -> f:('a -> unit t) -> 'a t

tee_m val ~f executes f val for its monadic action, then returns val.

Example (using an extended Or_error):

let fail_if_negative x =
  On_error.when_m (Int.is_negative x)
    ~f:(fun () -> Or_error.error_string "value is negative!")
in
Or_error.(
  42 |> tee_m ~f:fail_if_negative >>| (fun x -> x * x)
) (* Ok (1764) *)
val tee : 'a -> f:('a -> unit) -> 'a t

tee val ~f behaves as tee, but takes a non-monadic f.

Example (using an extended Or_error):

let print_if_negative x =
  if Int.negative x then Stdio.print_string "value is negative!"
in
Or_error.(
  try_get_value ()
  >>= tee ~f:print_if_negative
  >>= try_use_value ()
)
include Generic with type ('a, 's) t := 'a t and type 's state := state and type 'a final := 'a

State monads share the signatures of their builder functions with state transformers...

include State_transform_intf.Generic_builders with type ('a, 's) t := 'a t with type 's state := state with type 'a final := 'a
include State_transform_intf.Generic_types with type ('a, 's) t := 'a t with type 's state := state with type 'a final := 'a
val make : (state -> state * 'a) -> 'a t

make creates a context-sensitive computation that can modify both the current context and the data passing through.

Specialised builders

val peek : (state -> 'a) -> 'a t

peek creates a context-sensitive computation that can look at the current context, but not modify it.

val modify : (state -> state) -> Base.unit t

modify creates a context-sensitive computation that can look at and modify the current context.

val return : 'a -> 'a t

return lifts a value or monad into a stateful computation.

...as well as their runner functions...

include State_transform_intf.Generic_runners with type ('a, 's) t := 'a t and type 'a final := 'a and type 's state := state
include State_transform_intf.Generic_types with type ('a, 's) t := 'a t with type 'a final := 'a with type 's state := state
val run' : 'a t -> state -> state * 'a

run' unfolds a t into a function from context to final state and result.

val run : 'a t -> state -> 'a

run unfolds a t into a function from context to final result. To get the final context, use run' or call peek at the end of the computation.

...and fixed-point combinators.

include State_transform_intf.Fix with type ('a, 's) t := 'a t
val fix : f:(('a -> 'a t) -> 'a -> 'a t) -> 'a -> 'a t

fix ~f init builds a fixed point on f.

At each step, f is passed a continuation mu and a value a. It may choose to return a recursive application of mu, or some value derived from a.

To begin with, f is applied to mu and init.

OCaml

Innovation. Community. Security.