package travesty

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

Or-error monad extensions.

This module contains various extensions for Core's Or_error monad, including monadic traversal over successful values and T_monad extensions.

This module re-exports all of the original monad.

include module type of Core_kernel.Or_error
val bin_read_t : 'a Bin_prot.Read.reader -> 'a t Bin_prot.Read.reader
val __bin_read_t__ : 'a Bin_prot.Read.reader -> (Base.Int.t -> 'a t) Bin_prot.Read.reader
val bin_size_t : 'a Bin_prot.Size.sizer -> 'a t Bin_prot.Size.sizer
val bin_write_t : 'a Bin_prot.Write.writer -> 'a t Bin_prot.Write.writer
val bin_shape_t : Bin_prot.Shape.t -> Bin_prot.Shape.t
include module type of struct include Base.Or_error end with type 'a t := 'a t
include sig ... end
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
val hash_fold_t : (Base.Hash.state -> 'a -> Base.Hash.state) -> Base.Hash.state -> 'a t -> Base.Hash.state
val t_of_sexp : (Base.Sexp.t -> 'a) -> Base.Sexp.t -> 'a t
val sexp_of_t : ('a -> Base.Sexp.t) -> 'a t -> Base.Sexp.t

Applicative functions don't have quite the same semantics as Applicative.of_Monad(Or_error) would give -- apply (Error e1) (Error e2) returns the combination of e1 and e2, whereas it would only return e1 if it were defined using bind.

include Base.Applicative.S with type 'a t := 'a t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t
val both : 'a t -> 'b t -> ('a * 'b) t
module Applicative_infix = Base.Or_error.Applicative_infix
include module type of Applicative_infix
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

val (<*) : 'a t -> unit t -> 'a t
val (*>) : unit t -> 'a t -> 'a t
include Base.Invariant.S1 with type 'a t := 'a t
val invariant : ('a -> unit) -> 'a t -> unit
include Base.Monad.S with type 'a t := '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 = Base.Or_error.Monad_infix
val bind : 'a t -> f:('a -> 'b t) -> 'b t

bind t ~f = t >>= f

val return : 'a -> 'a t

return v returns the (trivial) computation that returns v.

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 = Base.Or_error.Let_syntax
module Ok = Base.Or_error.Ok
val is_ok : _ t -> bool
val is_error : _ t -> bool
val ignore : _ t -> unit t
val try_with : ?backtrace:bool -> (unit -> 'a) -> 'a t

try_with f catches exceptions thrown by f and returns them in the Result.t as an Error.t. try_with_join is like try_with, except that f can throw exceptions or return an Error directly, without ending up with a nested error; it is equivalent to Result.join (try_with f).

val try_with_join : ?backtrace:bool -> (unit -> 'a t) -> 'a t
val ok : 'ok t -> 'ok option

ok t returns None if t is an Error, and otherwise returns the contents of the Ok constructor.

val ok_exn : 'a t -> 'a

ok_exn t throws an exception if t is an Error, and otherwise returns the contents of the Ok constructor.

val of_exn : ?backtrace:[ `Get | `This of string ] -> exn -> _ t

of_exn exn is Error (Error.of_exn exn).

val of_exn_result : ('a, exn) Base.Result.t -> 'a t

of_exn_result (Ok a) = Ok a, of_exn_result (Error exn) = of_exn exn

val error : ?strict:unit -> string -> 'a -> ('a -> Base.Sexp.t) -> _ t

error is a wrapper around Error.create:

error ?strict message a sexp_of_a
= Error (Error.create ?strict message a sexp_of_a)

As with Error.create, sexp_of_a a is lazily computed when the info is converted to a sexp. So, if a is mutated in the time between the call to create and the sexp conversion, those mutations will be reflected in the sexp. Use ~strict:() to force sexp_of_a a to be computed immediately.

val error_s : Base.Sexp.t -> _ t
val error_string : string -> _ t

error_string message is Error (Error.of_string message).

val errorf : ('a, unit, string, _ t) format4 -> 'a

errorf format arg1 arg2 ... is Error (sprintf format arg1 arg2 ...). Note that it calculates the string eagerly, so when performance matters you may want to use error instead.

val tag : 'a t -> tag:string -> 'a t

tag t ~tag is Result.map_error t ~f:(Error.tag ~tag). tag_arg is similar.

val tag_arg : 'a t -> string -> 'b -> ('b -> Base.Sexp.t) -> 'a t
val unimplemented : string -> _ t

For marking a given value as unimplemented. Typically combined with conditional compilation, where on some platforms the function is defined normally, and on some platforms it is defined as unimplemented. The supplied string should be the name of the function that is unimplemented.

val map : 'a t -> f:('a -> 'b) -> 'b t
val iter : 'a t -> f:('a -> unit) -> unit
val iter_error : _ t -> f:(Base.Error.t -> unit) -> unit
val combine_errors : 'a t list -> 'a list t

combine_errors ts returns Ok if every element in ts is Ok, else it returns Error with all the errors in ts. More precisely:

  • combine_errors [Ok a1; ...; Ok an] = Ok [a1; ...; an]
  • combine_errors [...; Error e1; ...; Error en; ...]
    = Error (Error.of_list [e1; ...; en]) 
val combine_errors_unit : unit t list -> unit t

combine_errors_unit ts returns Ok if every element in ts is Ok (), else it returns Error with all the errors in ts, like combine_errors.

val filter_ok_at_least_one : 'a t list -> 'a list t

filter_ok_at_least_one ts returns all values in ts that are Ok if there is at least one, otherwise it returns the same error as combine_errors ts.

val find_ok : 'a t list -> 'a t

find_ok ts returns the first value in ts that is Ok, otherwise it returns the same error as combine_errors ts.

val find_map_ok : 'a list -> f:('a -> 'b t) -> 'b t

find_map_ok l ~f returns the first value in l for which f returns Ok, otherwise it returns the same error as combine_errors (List.map l ~f).

module On_ok : Traversable.S1_container with type 'a t := 'a t

On_ok treats an Or_error value as a traversable container, containing one value when it is Ok and none otherwise.

Monad extensions for Or_error.

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) *)
OCaml

Innovation. Community. Security.