package travesty

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

Make makes an S (state monad with fixed state type) from a single state type.

Parameters

module B : Base.T

Signature

type state = B.t

The fixed state type.

include Core_kernel.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 Pervasives.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
val all_ignore : unit t list -> unit t
  • deprecated [since 2018-02] Use [all_unit]
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.

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.