Module Picos_sync.Lazy

A lazy implementation for Picos.

ℹī¸ This intentionally mimics the interface of Stdlib.Lazy. Unlike with the standard library suspensions an attempt to force a suspension from multiple fibers, possibly running on different domains, does not raise the Undefined exception.

exception Undefined

Synonym for Stdlib.Lazy.Undefined.

type !'a t

Represents a deferred computation or suspension.

val from_fun : (unit -> 'a) -> 'a t

from_fun thunk returns a suspension.

val from_val : 'a -> 'a t

from_val value returns an already forced suspension whose result is the given value.

val is_val : 'a t -> bool

is_val susp determines whether the suspension has already been forced and didn't raise an exception.

val force : 'a t -> 'a

force susp forces the suspension, i.e. computes thunk () using the thunk passed to from_fun, stores the result of the computation to the suspension and reproduces its result. In case the suspension has already been forced the computation is skipped and stored result is reproduced.

ℹī¸ This will check whether the current fiber has been canceled before starting the computation of thunk (). This allows the suspension to be forced by another fiber. However, if the fiber is canceled and the cancelation exception is raised after the computation has been started, the suspension will then store the cancelation exception.

  • raises Undefined

    in case the suspension is currently being forced by the current fiber.

val force_val : 'a t -> 'a

force_val is a synonym for force.

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

map fn susp is equivalent to:

from_fun (fun () -> fn (force susp))
val map_val : ('a -> 'b) -> 'a t -> 'b t

map_val fn susp is equivalent to:

if is_val susp then
  from_val (fn (force susp))
else
  map fn susp