Module type State.STATE

The type of state monads.

type state

The type of states inside the monad.

type 'a t = state -> state * 'a
val get : state t

Return the state from the internals of the monad.

val put : state -> unit t

Replace the state inside the monad.

val modify : (state -> state) -> unit t

Modify the state inside the monad.

val run : 'a t -> state -> state * 'a

run a init runs a with initial state init, returning (final, v) where final is the final state and v is the output.

val eval : 'a t -> state -> 'a

eval a init is run a init = (final, v), but outputs only v.

val exec : 'a t -> state -> state

exec a init is run a init = (final, v), but outputs only final.

Traversals

val traverse_tuple2 : ('a1 -> 'b1 t) -> ('a2 -> 'b2 t) -> ('a1 * 'a2) -> ('b1 * 'b2) t
val traverse_tuple3 : ('a1 -> 'b1 t) -> ('a2 -> 'b2 t) -> ('a3 -> 'b3 t) -> ('a1 * 'a2 * 'a3) -> ('b1 * 'b2 * 'b3) t
val traverse_list : ('a -> 'b t) -> 'a List.t -> 'b List.t t
val traverse_list1 : ('a -> 'b t) -> 'a List1.t -> 'b List1.t t
val traverse_list2 : ('a -> 'b t) -> 'a List2.t -> 'b List2.t t
val traverse_list_void : ('a -> _ t) -> 'a List.t -> Stdlib.Unit.t t
val traverse_list1_void : ('a -> _ t) -> 'a List1.t -> Stdlib.Unit.t t
val traverse_list2_void : ('a -> _ t) -> 'a List2.t -> Stdlib.Unit.t t
val traverse_reverse_list : ('a -> 'b t) -> 'a List.t -> 'b List.t t
val traverse_reverse_list1 : ('a -> 'b t) -> 'a List1.t -> 'b List1.t t
val traverse_reverse_list2 : ('a -> 'b t) -> 'a List2.t -> 'b List2.t t
val traverse_reverse_list_void : ('a -> _ t) -> 'a List.t -> Stdlib.Unit.t t
val traverse_reverse_list1_void : ('a -> _ t) -> 'a List1.t -> Stdlib.Unit.t t
val traverse_reverse_list2_void : ('a -> _ t) -> 'a List2.t -> Stdlib.Unit.t t
val traverse_option : ('a -> 'b t) -> 'a Option.t -> 'b Option.t t
val traverse_option_void : ('a -> _ t) -> 'a Option.t -> Stdlib.Unit.t t
val seq_list : 'a t List.t -> 'a List.t t
val seq_list1 : 'a t List1.t -> 'a List1.t t
val seq_list_void : unit t list -> unit t

seq_list_void [x1; x2; ...; xn] performs x1, x2, ..., xn in order.

Instances

include Monad.MONAD with type 'a t := 'a t
val return : 'a -> 'a t

return a injects a into the monadic type.

val bind : ('a -> 'b t) -> 'a t -> 'b t

bind f a is the sequential composition of two actions, passing any value produced by a as argument to f.

val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

Operator alias of bind.

val compose : ('b -> 'c t) -> ('a -> 'b t) -> 'a -> 'c t

compose g f is the Kleisli composition of f and g, passing the input to f, then binding the output to g.

val (>=>) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t

Operator alias of compose.

val (let*) : 'a t -> ('a -> 'b t) -> 'b t

( let* ) ma f is bind f ma. This is a binding operator, and it is used as let* a = ma in f a.

val (and*) : 'a t -> 'b t -> ('a * 'b) t

( and* ) ma mb is let* a = ma in let* b = mb in return (a, b). This is a binding operator, and it is used as let* a = ma and* b = mb in ....

include Functor.FUNCTOR with type 'a t := 'a t

Combinators

val map : ('a -> 'b) -> 'a t -> 'b t

map f is the function that maps values of t by f. The order of arguments is for use in function pipelines as fb = fa |> map (fun a -> (* ... *)).

val ($>) : 'a t -> ('a -> 'b) -> 'b t

( $> ) is an infix synonym for map.

val (let+) : 'a t -> ('a -> 'b) -> 'b t

( let+ ) ma f is map f a. This is a binding operator, and is used as let+ a = ma in f a

val (and+) : 'a t -> 'b t -> ('a * 'b) t

( and+ ) ma mb is let+ a = ma in let+ b = mb in return (a, b). This is a binding operator, and it is used as let+ a = ma and+ b = mb in ....

include Apply.APPLY with type 'a t := 'a t
val ap : 'a t -> ('a -> 'b) t -> 'b t

ap fa fab applies argument fa to fab under the abstract datatype t.

val (<&>) : ('a -> 'b) t -> 'a t -> 'b t

( <&> ) is an infix synonym of ap.

Combinators

val ap_first : 'b t -> 'a t -> 'a t

ap_first second first combines actions first and second but keeps only first. That is, ap_first second first = first. The order of arguments is for use in function pipelines as first = first |> ap_first second.

val (<&) : 'a t -> 'b t -> 'a t

( <& ) is an infix synonym for ap_first.

val ap_second : 'b t -> 'a t -> 'b t

ap_second second first combines actions first and second but keeps only second. That is, ap_second second first = second. The order of arguments is for use in function pipelines as second = first |> ap_second second.

val (&>) : 'a t -> 'b t -> 'b t

( &> ) is an infix synonym for ap_second.

val seq2 : 'a1 t -> 'a2 t -> ('a1 * 'a2) t

seq2 fa1 fa2 sequentially executes actions fa1 and fa2, and keeps their outputs under the abstract datatype t.

val seq3 : 'a1 t -> 'a2 t -> 'a3 t -> ('a1 * 'a2 * 'a3) t

seq3 fa1 fa2 fa3 sequentially executes actions fa1, fa2 and fa3, and keeps their outputs under the abstract datatype t.

val seq4 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> ('a1 * 'a2 * 'a3 * 'a4) t

seq4 fa1 fa2 fa3 fa4 sequentially executes actions fa1, fa2, fa3 and fa4, and keeps their outputs under the abstract datatype t.

val seq5 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> ('a1 * 'a2 * 'a3 * 'a4 * 'a5) t

seq5 fa1 fa2 fa3 fa4 fa5 sequentially executes actions fa1, fa2, fa3, fa4 and fa5, and keeps their outputs under the abstract datatype t.

val lift2 : ('a1 -> 'a2 -> 'r) -> 'a1 t -> 'a2 t -> 'r t

lift2 f ma1 ma2 sequentially executes actions ma1, ma2 and passes their outputs to f.

val lift3 : ('a1 -> 'a2 -> 'a3 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'r t

lift3 f ma1 ma2 ma3 sequentially executes actions ma1, ma2, ma3 and passes their outputs to f.

val lift4 : ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'r t

lift4 f ma1 ma2 ma3 ma4 sequentially executes actions ma1, ma2, ma3, ma4 and passes their outputs to f.

val lift5 : ('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'r) -> 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> 'r t

lift5 f ma1 ma2 ma3 ma4 ma5 sequentially executes actions ma1, ma2, ma3, ma4, ma5 and passes their outputs to f.

val replicate : int -> 'a t -> 'a list t

replicate n a sequentially runs n times a.

  • raises Invalid_argument

    If n < 0.