Module Prelude.Result

Result (error) monad.

See Rresult for a much more sophisticated package.

Constructors

val ok : 'a -> ('a, 'b) Stdlib.result

(ok x) is (Ok x).

val error : 'a -> ('b, 'a) Stdlib.result

(error x) is (Error x).

Predicates

val good : ('a, 'b) Stdlib.result -> bool

(good x) is true if (x = Ok _) and false if (x = Error _).

val bad : ('a, 'b) Stdlib.result -> bool

(bad x) is true if (x = Error _) and false if (x = Ok _).

Selectors

val get_ok : ('a, 'b) Stdlib.result -> 'a

(get_ok r) is x iff (r = (Ok x)).

  • raises Invalid_argument

    when (r = (Error _))

val get_error : ('a, 'b) Stdlib.result -> 'b

(get_error r) is x iff (r = (Error x)).

  • raises Invalid_argument

    when (r = (Ok _))

val default : 'a -> ('a, 'b) Stdlib.result -> 'a

(default d r) is x when r = Ok x, and d otherwise.

Operators

val bind : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c, 'b) Stdlib.result

(bind r f) is (Ok (f v)) if (r = Ok v) and r if (r = Error e).

val (>>=) : ('a, 'b) Stdlib.result -> ('a -> ('c, 'b) Stdlib.result) -> ('c, 'b) Stdlib.result

(>>=) is bind.

val map : ('a -> 'b) -> ('a, 'c) Stdlib.result -> ('b, 'c) Stdlib.result

(map f r) is (bind r (fun v -> Ok (f v))).

val (>>|) : ('a, 'b) Stdlib.result -> ('a -> 'c) -> ('c, 'b) Stdlib.result

(>>|) is map.

val (>=>) : ('a -> ('b, 'c) Stdlib.result) -> ('b -> ('d, 'c) Stdlib.result) -> 'a -> ('d, 'c) Stdlib.result

(>=>) is monadic composition: ((f >=> g) x = (f x) >>= g)

val join : (('a, 'b) Stdlib.result, 'b) Stdlib.result -> ('a, 'b) Stdlib.result

(join r) is v if r = Ok v and r otherwise.

This is particularly useful when nesting trap's:

 trap Exn.to_string (withcd (fun _ -> trap Exn.to_string readfile "NONEXISTENT")) "/etc"
= Ok (Error "NONEXISTENT: No such file or directory") 

whereas:

 trap Exn.to_string (withcd (fun _ -> trap Exn.to_string readfile "NONEXISTENT")) "/etc" |> join
= Error "NONEXISTENT: No such file or directory" 
val reduce : ('a, 'b) Stdlib.result list -> 'a list

(reduce rs) is the list of all the values carried by Ok's in the list, with all the Error's tossed out.

val on_error : ('a, 'b) Stdlib.result -> ('b -> ('a, 'c) Stdlib.result) -> ('a, 'c) Stdlib.result

(on_error r f) is (f e) if r is (Error e) and is otherwise (Ok x).

val (>>/) : ('a, 'b) Stdlib.result -> ('b -> ('a, 'c) Stdlib.result) -> ('a, 'c) Stdlib.result

(>>/) is on_error

val (//=) : ('a, 'b) Stdlib.result -> ('b -> ('a, 'c) Stdlib.result) -> ('a, 'c) Stdlib.result
val or : ('a, 'b) Stdlib.result -> ('a, 'c) Stdlib.result -> ('a, 'c) Stdlib.result

a or b is Ok x if (a = Ok x) and is otherwise b.

val always : 'a -> (unit -> 'b) -> 'b

(always r f) is (f ()), regardless of what r is.

val (//*) : 'a -> (unit -> 'b) -> 'b

( //* ) is always.

Exceptions

val trap : (exn -> 'a) -> ('b -> 'c) -> 'b -> ('c, 'a) Stdlib.result

(trap e f x) is (Ok (f x)) unless an exception exn is raised, in which case it is (Error (e exn)).

val trapc : 'a -> ('b -> 'c) -> 'b -> ('c, 'a) Stdlib.result

(trapc c f x) is (Ok (f x)) unless an exception exn is raised, in which case it is (Error c).

Error Manipulation

val witherr : ('a -> 'b) -> ('c, 'a) Stdlib.result -> ('c, 'b) Stdlib.result

(witherr f r) is (Error (f e)) iff (r = Error e) and is otherwise r.

val witherrc : 'a -> ('b, 'c) Stdlib.result -> ('b, 'a) Stdlib.result

(witherrc e r) is (Error e) iff (r = Error _) and is otherwise r.

Type Conversion

val to_bool : ('a, 'b) Stdlib.result -> bool

(to_bool result) is true iff result is Ok _ and false otherwise.

val of_bool : ?err:'a -> 'a -> bool -> ('a, 'a) Stdlib.result

(of_bool ?err x bool) converts true to Ok x and false to Error err, where the default value for err is x.

val to_option : ('a, 'b) Stdlib.result -> 'a option

(to_option r) is (Some x) iff (r = Ok x) and is None otherwise.

val of_option : 'a -> 'b option -> ('b, 'a) Stdlib.result

(of_option err o) is (Ok x) iff (o = Some x) and is (Error err) otherwise.

val some_error : ('a, 'b) Stdlib.result -> 'b option

(some_error r) is in some sense the inverse of to_option: it is None if r is Ok, and (Some err) if r is (Error err).

Sequences

module Seq : sig ... end

Functions on sequences of results.

Development Utils

val to_string : error:('a -> string) -> ok:('b -> string) -> ('b, 'a) Stdlib.result -> string

(to_string ~error ~ok r) is a string representation of r, where ~error converts the Error case and ~ok the Ok case.

val print : error:('a -> string) -> ok:('b -> string) -> ('b, 'a) Stdlib.result -> unit

(print ~error ~ok) = (to_string ~error ~ok >> print_endline).

val random : error:(unit -> 'a) -> ok:(unit -> 'b) -> unit -> ('b, 'a) Stdlib.result

(random ~error ~ok ()) is either (Ok (ok ())) or (Error (error ())) with equal probability.

Infix Operators

module Ops : sig ... end

Infix operators.