123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102(* This file is part of 'travesty'.
Copyright (c) 2018, 2019 by Matt Windsor
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE. *)(** [S] contains extensions for a monad.
To create an instance of [S], use {{!Extend} Extend}. *)moduletypeS=sig(** The type of the extended monad. *)type'at(** {3 Haskell-style operators} *)valthen_m:_t->'at->'at(** [then_m x y] sequentially composes the actions [x] and [y] as with
[>>=], but, rather than using the returned value of [x], it instead
just returns [y]. *)val(>>):_t->'at->'at(** [x >> y] is [then_m x y]. *)valcompose_m:('a->'bt)->('b->'ct)->'a->'ct(** [compose_m f g] is the Kleisli composition of [f] and [g]. *)val(>=>):('a->'bt)->('b->'ct)->'a->'ct(** [x >=> y] is [k_compose x y]. *)(** {3 Guarded monadic computations} *)valmap_when_m:?otherwise:('a->'at)->bool->'a->f:('a->'at)->'at(** [map_when_m ?otherwise condition ~f a] is [f a] when [condition] is
true, and [otherwise a] (by default, [return]) otherwise. *)valwhen_m:?otherwise:(unit->unitt)->bool->f:(unit->unitt)->unitt(** [when_m ?otherwise condition ~f] is [f ()] when [condition] is true,
and [otherwise ()] (by default, [return]) otherwise. *)valmap_unless_m:?otherwise:('a->'at)->bool->'a->f:('a->'at)->'at(** [map_unless_m ?otherwise condition ~f a] is [f a] when [condition] is
false, and [otherwise a] (by default, [return]) otherwise. *)valunless_m:?otherwise:(unit->unitt)->bool->f:(unit->unitt)->unitt(** [unless_m ?otherwise condition ~f] is [f ()] when [condition] is
false, and [otherwise ()] (by default, [return]) otherwise. *)(** {3 Executing monadic effects in the middle of pipelines} *)valtee_m:'a->f:('a->unitt)->'at(** [tee_m val ~f] executes [f val] for its monadic action, then returns
[val].
Example (using an {{!Travesty_base_exts.Or_error} extended Or_error}):
{[
let fail_if_negative x =
On_error.when_m (Int.is_negative x)
~f:(fun () -> Or_error.error_string "value is negative!")
in
Or_error.(
42 |> tee_m ~f:fail_if_negative >>| (fun x -> x * x)
) (* Ok (1764) *)
]} *)valtee:'a->f:('a->unit)->'at(** [tee val ~f] behaves as {{!tee_m} tee}, but takes a non-monadic [f].
Example (using an {{!Travesty_base_exts.Or_error} extended Or_error}):
{[
let print_if_negative x =
if Int.negative x then Stdio.print_string "value is negative!"
in
Or_error.(
try_get_value ()
>>= tee ~f:print_if_negative
>>= try_use_value ()
)
]} *)end