package travesty

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

Module State_transform.MakeSource

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

Parameters

module B : Basic

Signature

Sourcetype state = B.t

The fixed state type.

include Base.Monad.S
Sourcetype 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val (>>|) : 'a t -> ('a -> 'b) -> 'b t
module Monad_infix : sig ... end
val bind : 'a t -> f:('a -> 'b t) -> 'b t
val map : 'a t -> f:('a -> 'b) -> 'b t
val join : 'a t t -> 'a t
val ignore_m : 'a t -> unit t
val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t
module Let_syntax : sig ... end
include T_monad.Extensions with type 'a t := 'a t
Sourceval 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.

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

Sourceval 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
Sourceval 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

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

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

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

Sourcemodule 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
Sourceval run' : 'a t -> state -> (state * 'a) Inner.t

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

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

Sourcemodule 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
Sourceval lift : 'a Inner.t -> 'a Inner.t t

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

OCaml

Innovation. Community. Security.