package travesty
Module State_transform.MakeSource
Make makes an S (state transformer with fixed state type) from a Basic.
Parameters
module B : State_transform_types.BasicSignature
include Base.Monad.S
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.
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 Caml.ignore. Some monads still do let ignore = ignore_m for historical reasons.
Like all, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.
These are convenient to have in scope when programming with a monad:
include Monad_exts_types.S with type 'a t := 'a t
S subsumes S_let.
Haskell-style operators
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.
compose_m f g is the Kleisli composition of f and g.
Guarded monadic computations
map_when_m ?otherwise condition ~f a is f a when condition is true, and otherwise a (by default, return) otherwise.
when_m ?otherwise condition ~f is f () when condition is true, and otherwise () (by default, return) otherwise.
map_unless_m ?otherwise condition ~f a is f a when condition is false, and otherwise a (by default, return) otherwise.
unless_m ?otherwise condition ~f is f () when condition is false, and otherwise () (by default, return) otherwise.
Executing monadic effects in the middle of pipelines
tee_m val ~f executes f val for its monadic action, then returns val.
Example (using an 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) *)tee val ~f behaves as tee_m, but takes a non-monadic f.
Example (using an 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 ())include State_transform_types.Generic
with type ('a, 's) t := 'a t
and type 's state := state
with module Inner = B.Inner
include State_transform_types.Generic_builders
with type 'a final := 'a
with type ('a, 's) t := 'a t
with type 's state := state
make creates a context-sensitive computation that can modify both the current context and the data passing through.
Specialised builders
peek creates a context-sensitive computation that can look at the current context, but not modify it.
modify creates a context-sensitive computation that can look at and modify the current context.
module Inner = B.InnerInner 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.