Legend:
Library
Module
Module type
Parameter
Class
Class type
State monad interface.
State monad is an interface to a computation that has a limited side effect. The side effect is defined by the following two functions:
put s -- that will store value s
get () -- that will extract the value.
There're few functions derived on get and put, like gets, update, modify, but the minimal algebra is put and set.
The value of type ('a,'s) t bears a computation that has an access to the state of type 's, and evaluates to a value of type 'a. It can be said, that 's reifies effect of computation of value 'a.
type('a, 's) t
('a,'s) t computation that evaluates to 'a and state 's
type'a result
'a result is a type that represents result of computation.
includeCore_kernel.Std.Monad.S2 withtype('a, 's) t := ('a, '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.
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.,