package bap-std

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

State monad. See State for more info.

module type S = sig ... end

State monad interface.

include S with type 'a result = 'a
type ('a, 's) t

('a,'s) t computation that evaluates to 'a and state 's

type 'a result = 'a

'a result is a type that represents result of computation.

include Core_kernel.Std.Monad.S2 with type ('a, 's) t := ('a, 's) t
val (>>=) : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t
val (>>|) : ('a, 'e) t -> ('a -> 'b) -> ('b, 'e) t
module Let_syntax : sig ... end
module Monad_infix : sig ... end
val bind : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t
val return : 'a -> ('a, 'b) t
val map : ('a, 'e) t -> f:('a -> 'b) -> ('b, 'e) t
val join : (('a, 'e) t, 'e) t -> ('a, 'e) t
val ignore_m : ('a, 'e) t -> (unit, 'e) t
val all : ('a, 'e) t list -> ('a list, 'e) t
val all_ignore : (unit, 'e) t list -> (unit, 'e) t
val put : 's -> (unit, 's) t

put s creates a computation that has effect s and unit value. This operation effectively updates the state by overriding the existent one with the new one. An imperative counterpart of this operation is a statement that performs side effect, e.g.,

put x

is somewhat equivalent to

let state = ref init
let put x = state := x

except that state is not hidden in the language heap, but is reified into a value of type 's.

val get : unit -> ('s, 's) t

get () creates a computation that evalates to a value, that holds the state. This operation extracts the state, and gives an access for it. The get () is somewhat equivalent to the imperative operator (!), i.e.,

let state = ref init
let get () = !state
val gets : ('s -> 'r) -> ('r, 's) t

gets f is a computation whose value is f state)

update f is get () >>= fun s -> put (f s)

val update : ('s -> 's) -> (unit, 's) t

update f is get () >>= fun s -> put (f s)

val modify : ('a, 's) t -> ('s -> 's) -> ('a, 's) t

modify c f is a computation with state f, where s is c >>= get (), i.e., an effect of computation c.

Running state computations.

val run : ('a, 's) t -> 's -> ('a * 's) result

run c init runs computation c with initial state init. Result contains a pair of the computation result and total effect.

val eval : ('a, 's) t -> 's -> 'a result

eval c init contains a value computed by computation c under initial state init.

val exec : ('a, 's) t -> 's -> 's result

exec c init contains an final effect produced by a computation c under initial state init.

OCaml

Innovation. Community. Security.