Support.Either
Ad hoc disjoint unions of two types.
include module type of Stdlib.Either
val left : 'a -> ('a, 'b) t
val right : 'b -> ('a, 'b) t
val is_left : ('a, 'b) t -> bool
val is_right : ('a, 'b) t -> bool
val find_left : ('a, 'b) t -> 'a option
val find_right : ('a, 'b) t -> 'b option
val fold : left:('a -> 'c) -> right:('b -> 'c) -> ('a, 'b) t -> 'c
val iter : left:('a -> unit) -> right:('b -> unit) -> ('a, 'b) t -> unit
val for_all : left:('a -> bool) -> right:('b -> bool) -> ('a, 'b) t -> bool
val eliminate : ('e -> 'c) -> ('a -> 'c) -> ('e, 'a) t -> 'c
Eliminator for disjoint unions.
Transforms both sides of the union.
Promotes a function that constructs a union into a function that transforms a union.
Either.bind
and Either.right
witness that Either.t
is a monad if the left-hand type is fixed.
val forget : ('e, 'a) t -> 'a option
Eliminates the union into a Stdlib.option
, forgetting the value in the left-hand side.
val of_option : 'a option -> (unit, 'a) t
Converts a Stdlib.option
into a union with a unit left-hand side.
val of_option' : (unit -> 'e) -> 'a option -> ('e, 'a) t
Converts a Stdlib.option
into a union with a left-hand side constructed from a thunk in case the Stdlib.option
contained Option.t.None
.
val to_option : ('e, 'a) t -> 'a option
Converts a union into an option, forgetting the left side, if any.
Infix form of Either.bind
.
Infix form of Either.map_right
.
val trap : (unit -> 'a) -> (exn, 'a) t
Traps exceptions thrown by evaluating a function into a union. You can delay handling the exceptions until the union is eliminated by using the following trick. Suppose e : (exn, a) t
. Then,
eliminate
(fun ex ->
try raise ex with
| Whatever -> do_something_to_handle_Whatever)
(fun x -> success x)
The benefit of using trap
is that you can be selective about which code gets its exceptions caught.