package bonsai

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

A value of type 'a Computation.t represents a computation which produces a value that may change during the lifetime of a program, and the value may be influenced by the internal state of that computation.

The same 'a Computation.t can be used in multiple places in a program, and these uses will not share the same state, nor will they share the work performed by the computation.

In this normal OCaml code, if we see the same function being called multiple times:

let a = f () in
let b = f () in
a + b

You would not be surprised to know that if f has side-effects (maybe printing to the console), then those side-effects happen twice because f was called twice.

Similarly, if we wrote the code this way:

let a = f () in
let b = a in
a + b

You would (correctly) expect that the side-effect only happens once, when computing a. In these examples, the code f () is analogous to _ Computation.t. If you want to have two separate values whose computations maintain separate state, you would use two instances of "let%sub" to bind them separately:

val some_computation : int Computation.t
val add : int Value.t -> int Value.t -> int Computation.t

let open Let_syntax in
let%sub a = some_computation in
let%sub b = some_computation in
add a b

Here, a and b can take on different values depending on the states of the computations that produce them.

However, if you want to use just one value in multiple places, only use let%sub once:

let open Let_syntax in
let%sub a = some_computation in
let     b = a in
add a b

Here, a and b always take on the same value.

include Core.Applicative.S with type 'a t := 'a t
val return : 'a -> 'a t
val map : 'a t -> f:('a -> 'b) -> 'b t
val both : 'a t -> 'b t -> ('a * 'b) t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

val (<*) : 'a t -> unit t -> 'a t
val (*>) : unit t -> 'a t -> 'a t
val (>>|) : 'a t -> ('a -> 'b) -> 'b t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t
module Applicative_infix : sig ... end
val all_map : ('k, 'v t, 'cmp) Core.Map.t -> ('k, 'v, 'cmp) Core.Map.t t

Similar to all which pulls the computation outside of a list, all_map does the same, but with the data in a map. This can be a useful replacement for assoc in scenarios where the map is a constant size.

val reduce_balanced : 'a t list -> f:('a Value.t -> 'a Value.t -> 'a t) -> 'a t option

The analog of List.reduce_balanced for computations, but with f operating on values instead of the computations themselves

module Let_syntax : sig ... end
val map3 : 'a1 t -> 'a2 t -> 'a3 t -> f:('a1 -> 'a2 -> 'a3 -> 'b) -> 'b t
val map4 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> f:('a1 -> 'a2 -> 'a3 -> 'a4 -> 'b) -> 'b t
val map5 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> f:('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'b) -> 'b t
val map6 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> 'a6 t -> f:('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'b) -> 'b t
val map7 : 'a1 t -> 'a2 t -> 'a3 t -> 'a4 t -> 'a5 t -> 'a6 t -> 'a7 t -> f:('a1 -> 'a2 -> 'a3 -> 'a4 -> 'a5 -> 'a6 -> 'a7 -> 'b) -> 'b t
OCaml

Innovation. Community. Security.