package travesty

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

Make makes an S (state transformer with fixed state type) from a Basic.

Parameters

module B : Basic

Signature

type state = B.t

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 T_monad.Extensions with type 'a t := 'a t
val when_m : Base.bool -> f:(Base.unit -> Base.unit t) -> Base.unit t

when_m predicate ~f returns f () when predicate is true, and return () otherwise.

val unless_m : Base.bool -> f:(Base.unit -> Base.unit t) -> Base.unit t

unless_m predicate ~f returns f () when predicate is false, and return () otherwise.

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

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

Example:

let fail_if_negative x =
  T_on_error.when_m (Int.is_negative x)
    ~f:(fun () -> Or_error.error_string "value is negative!")
in
Or_error.(
  42 |> T_on_error.tee_m ~f:fail_if_negative >>| (fun x -> x * x)
) (* Ok (1764) *)
include Generic with type ('a, 's) t := 'a t and type 's state := state with module Inner = B.Inner
include Generic_builders with type 'a final := 'a with type ('a, 's) t := 'a t with type 's state := state
include Generic_types with type 'a final := 'a with type ('a, 's) t := 'a t with type 's state := state
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.

module Inner = B.Inner

Inner is the monad to which we're adding state.

State transformers have the same runner signatures as state monads, but lifted into the inner monad.

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

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

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

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.

include 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.

module Monadic : Generic_builders with type 'a state := state and type 'a final := 'a Inner.t and type ('a, 's) t := 'a t

Monadic contains a version of the builder interface that can interact with the inner monad (Inner) this state transformer is overlaying.

include Monad_transform.S_fixed with type 'a t := 'a t and module Inner := Inner
val lift : 'a Inner.t -> 'a Inner.t t

lift x lifts x from the inner monad to the outer one.

OCaml

Innovation. Community. Security.