package octez-libs

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

Source file monad.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

(** {1 Lwt, result, and Lwt-result monad operators}

    This module provides the necessary functions and operators to use Lwt,
    Result and Lwt-Result as monads.

    {2 Basics}

    The three, tiered monads have each their own syntax module with
    {ul
      {li a [return] function,}
      {li preallocated [return_unit], [return_none], etc. values,}
      {li [let*] and [let+] bindind operators.}
    }
    In addition, the {!Lwt_syntax} module has [and*] and [and+] binding
    operators to allow concurrent evaluation of two or more promises, and the
    {!Result_syntax} and {!Lwt_result_syntax} have [fail] functions to
    error-out.

    {2 Joins}

    The {!Lwt_syntax.join} function takes a list of promises [ps] and returns a
    single promise [p] that resolves with [()] when all the promises of [ps]
    have resolved.

    The {!Lwt_syntax.all} function takes a list of promises [ps] and returns a
    single promise [p] that resolves when all the promises of [ps] have
    resolved. The value [p] resolves to is the list of values the promises of
    [ps] resolve to. The order is preserved.

    The {!Lwt_syntax.both} function takes two promises [p1] and [p2] and returns
    a single promise [p] that resolves when both promises [p1] and [p2] have
    resolved. The value [p] resolves to is the tuple of values the promises [p1]
    and [p2] resolve to.

    These Lwt-joining functions have a best-effort semantic: they only resolve
    once all the underlying promises have resolved.

    The {!Result_syntax} variants are equivalent for the result monad: the final
    result is [Ok] if all the underlying results are [Ok].

    The {!Lwt_result_syntax} variants are equivalent for the Lwt-result monad:
    the final promise resolves to [Ok] if all the underlying promise resolve to
    [Ok].

    {2 Lifting}

    Finally, the {!Lwt_result_syntax} module includes two facilities for lifting
    values from the more specilaised Lwt-only and Result-only monads.

    [let*!] binds a plain Lwt promise into an Lwt-Result promise.

{[
let open Lwt_result_syntax in
let*! x = f a b c in
…
]}

    [let*?] binds a plain result into an Lwt-Result promise.

{[
let open Lwt_result_syntax in
let*? y = f u v w in
…
]}

    In the cases where performance is not a grave concern, it is also possible to
    use [Lwt_result.ok] to lift Lwt-only expressions and [Lwt.return] to lift
    result-only expressions.
    More details on the matter within the documentation of
    {!Lwt_result_syntax.( let*! )} and {!Lwt_result_syntax.( let*? )}
    themselves. *)

module type S = sig
  (** {1 The tower of monads} *)

  (** {2 The Lwt monad: for concurrency} *)

  (** Syntax module for Lwt. This is intended to be opened locally in functions
      which use Lwt for control-flow. Within the scope of this module, the code
      can include binding operators, leading to a [let]-style syntax.

      See also {!Lwt} and {!Lwt.Syntax} *)
  module Lwt_syntax : sig
    (** [return x] is an Lwt promise that is already resolved to [x]. [return]
        is an alias for {!Lwt.return}. *)
    val return : 'a -> 'a Lwt.t

    (** [return_unit] is an Lwt promise that is already resolved to [()]. It is
        an alias for [Lwt.return_unit]. *)
    val return_unit : unit Lwt.t

    (** [return_none] is an Lwt promise that is already resolved to [None]. It
        is an alias for [Lwt.return_none]. *)
    val return_none : _ option Lwt.t

    (** [return_nil] is an Lwt promise that is already resolved to [[]]. It is
        an alias for [Lwt.return_nil]. *)
    val return_nil : _ list Lwt.t

    (** [return_true] is an Lwt promise that is already resolved to [true]. It is
        an alias for [Lwt.return_true]. *)
    val return_true : bool Lwt.t

    (** [return_false] is an Lwt promise that is already resolved to [false]. It is
        an alias for [Lwt.return_false]. *)
    val return_false : bool Lwt.t

    (** [return_some x] is an Lwt promise that is already resolved to [Some x].
        [return_some] is an alias for [Lwt.return_some]. *)
    val return_some : 'a -> 'a option Lwt.t

    (** [return_ok x] is an Lwt promise that is already resolved to [Ok x].
        [return_ok] is an alias for [Lwt.return_ok]. *)
    val return_ok : 'a -> ('a, _) result Lwt.t

    (** [return_error x] is an Lwt promise that is already resolved to [Error x].
        [return_error] is an alias for [Lwt.return_error]. *)
    val return_error : 'e -> (_, 'e) result Lwt.t

    (** The following [return_ok_*] functions are intended to be used within the
        scope of [Lwt_syntax] when returning results compatible with
        [Lwt_result_syntax]. *)

    (** [return_ok_unit] is an Lwt promise that is already resolved to [Ok ()]. *)
    val return_ok_unit : (unit, 'e) result Lwt.t

    (** [return_ok_true] is an Lwt promise that is already resolved to [Ok true]. *)
    val return_ok_true : (bool, 'e) result Lwt.t

    (** [return_ok_false] is an Lwt promise that is already resolved to [Ok false]. *)
    val return_ok_false : (bool, 'e) result Lwt.t

    (** [return_ok_none] is an Lwt promise that is already resolved to [Ok None]. *)
    val return_ok_none : ('a option, 'e) result Lwt.t

    (** [return_ok_nil] is an Lwt promise that is already resolved to [Ok []]. *)
    val return_ok_nil : ('a list, 'e) result Lwt.t

    (** [let*] is a binding operator alias for {!Lwt.bind} and {!Lwt.( >>= )}. *)
    val ( let* ) : 'a Lwt.t -> ('a -> 'b Lwt.t) -> 'b Lwt.t

    (** [and*] is a binding operator alias for {!Lwt.both} and {!Lwt.( <&> )}. *)
    val ( and* ) : 'a Lwt.t -> 'b Lwt.t -> ('a * 'b) Lwt.t

    (** [let+] is a binding operator alias for {!Lwt.map} and {!Lwt.( >|= )}. *)
    val ( let+ ) : 'a Lwt.t -> ('a -> 'b) -> 'b Lwt.t

    (** [and+] is a binding operator alias for {!Lwt.both} and {!Lwt.( <&> )}. *)
    val ( and+ ) : 'a Lwt.t -> 'b Lwt.t -> ('a * 'b) Lwt.t

    (** [join] is the joining of concurrent unit values (it is [Lwt.join]). *)
    val join : unit Lwt.t list -> unit Lwt.t

    (** [all] is the joining of concurrent non-unit values (it is [Lwt.all]). *)
    val all : 'a Lwt.t list -> 'a list Lwt.t

    (** [both] is the joining of two concurrent non-unit values (it is [Lwt.both]). *)
    val both : 'a Lwt.t -> 'b Lwt.t -> ('a * 'b) Lwt.t
  end

  (** {2 The Option monad: for optional data} *)

  (** Syntax module for Option. This is intended to be opened locally in
      functions which use [option] for control-flow. Within the scope of this
      module, the code can include binding operators, leading to a [let]-style
      syntax.

      See also {!Option} *)
  module Option_syntax : sig
    (** [return x] is [Some x]. *)
    val return : 'a -> 'a option

    (** [fail] is [None]. It is also an alias for [Option.none]. *)
    val fail : 'a option

    (** [return_unit] is [Some ()]. *)
    val return_unit : unit option

    (** [return_nil] is [Some []]. *)
    val return_nil : 'a list option

    (** [return_true] is [Some true]. *)
    val return_true : bool option

    (** [return_false] is [Some false]. *)
    val return_false : bool option

    (** Note that we do not provide [return_some] nor [return_none]. Both of
        these functions are possible but somewhat confusing and rarely useful in
        practice. If you need to carry [option]s within a Option-monad
        computation (yielding to values of the type
        ['a option option]), you need to do so by hand:
        [return (Some …)] and [return None]. *)

    (** [let*] is a binding operator alias for {!Option.bind}. *)
    val ( let* ) : 'a option -> ('a -> 'b option) -> 'b option

    (** [and*] is a binding operator alias for [both]. *)
    val ( and* ) : 'a option -> 'b option -> ('a * 'b) option

    (** [let+] is a binding operator alias for {!Option.map}. *)
    val ( let+ ) : 'a option -> ('a -> 'b) -> 'b option

    (** [and+] is a binding operator alias for [both]. *)
    val ( and+ ) : 'a option -> 'b option -> ('a * 'b) option

    (** [both] is the joining of two optional non-unit values. [both a b] is
        [Some (x, y)] iff [a] is [Some x] and [b] is [Some y]. *)
    val both : 'a option -> 'b option -> ('a * 'b) option
  end

  (** {2 The (generic) Result monad: for success/failure} *)

  (** Syntax module for Result. This is intended to be opened locally in
      functions which use [result] for control-flow. Within the scope of this
      module, the code can include binding operators, leading to a [let]-style
      syntax.

      See also {!Result} *)
  module Result_syntax : sig
    (** [return x] is [Ok x]. *)
    val return : 'a -> ('a, 'e) result

    (** [return_unit] is [Ok ()]. *)
    val return_unit : (unit, 'e) result

    (** [return_none] is [Ok None]. *)
    val return_none : ('a option, 'e) result

    (** [return_some x] is [Ok (Some x)]. *)
    val return_some : 'a -> ('a option, 'e) result

    (** [return_nil] is [Ok []]. *)
    val return_nil : ('a list, 'e) result

    (** [return_true] is [Ok true]. *)
    val return_true : (bool, 'e) result

    (** [return_false] is [Ok false]. *)
    val return_false : (bool, 'e) result

    (** Note that we do not provide [return_ok] nor [return_error]. Both of
        these functions are possible but somewhat confusing and rarely useful in
        practice. If you need to carry [result]s within a Result-monad
        computation (yielding to values of the type
        [(('a, 'e) result, 'e) result]), you need to do so by hand:
        [return (Ok …)] and [return (Error …)]. *)

    (** [fail e] is [Error e]. It is also an alias for [error]. *)
    val fail : 'e -> ('a, 'e) result

    (** [let*] is a binding operator alias for {!Result.bind} and [>>?]. *)
    val ( let* ) : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e) result

    (** [let+] is a binding operator alias for {!Result.map} and [>|?]. *)
    val ( let+ ) : ('a, 'e) result -> ('a -> 'b) -> ('b, 'e) result

    (** Note that we do not provide [and*] nor [and+]. Both of these are
        possible but their type is unsatisfying because the errors do not
        compose well. You can use [both] (below) if need be. *)

    (** [join] is the joining of success/failure unit values. *)
    val join : (unit, 'e) result list -> (unit, 'e list) result

    (** [all] is the joining of success/failure non-unit values. *)
    val all : ('a, 'e) result list -> ('a list, 'e list) result

    (** [both] is the joining of two success/failure non-unit values. *)
    val both : ('a, 'e) result -> ('b, 'e) result -> ('a * 'b, 'e list) result
  end

  (** {2 The combined Lwt+Option monad: for concurrent optional values} *)

  (** Syntax module for Lwt+Option. This is intended to be opened locally in
      functions which use Lwt and [option] for control-flow. Within the scope of
      this module, the code can include binding operators, leading to a
      [let]-style syntax.

      See also {!Lwt}, {!Option}. *)
  module Lwt_option_syntax : sig
    (** [return x] is [Lwt.return (Some x)]. *)
    val return : 'a -> 'a option Lwt.t

    (** [return_unit] is [Lwt.return (Some ())] . *)
    val return_unit : unit option Lwt.t

    (** [return_nil] is [Lwt.return (Some [])] . *)
    val return_nil : 'a list option Lwt.t

    (** [return_true] is [Lwt.return (Some true)] . *)
    val return_true : bool option Lwt.t

    (** [return_false] is [Lwt.return (Some false)] . *)
    val return_false : bool option Lwt.t

    (** Note that we do not provide [return_some] nor [return_none]. Both of
        these functions are possible but somewhat confusing and rarely useful in
        practice. If you need to carry [option]s within a LwtOption-monad
        computation (yielding values of the type
        ['a option option Lwt.t]), you need to do so by hand:
        [return (Some …)] and [return (None)]. *)

    (** [fail] is [Lwt.return None]. *)
    val fail : 'a option Lwt.t

    (** [let*] is a binding operator alias for {!Lwt.bind} mixed with {!Option.bind}. *)
    val ( let* ) : 'a option Lwt.t -> ('a -> 'b option Lwt.t) -> 'b option Lwt.t

    (** [and*] is a binding operator alias for [both]. *)
    val ( and* ) : 'a option Lwt.t -> 'b option Lwt.t -> ('a * 'b) option Lwt.t

    (** [let+] is a binding operator alias for {!Lwt.map} mixed with {!Option.map}. *)
    val ( let+ ) : 'a option Lwt.t -> ('a -> 'b) -> 'b option Lwt.t

    (** [and*] is a binding operator alias for [both]. *)
    val ( and+ ) : 'a option Lwt.t -> 'b option Lwt.t -> ('a * 'b) option Lwt.t

    (** The following values are for mixing expressions that are Lwt-only or
        Option-only within the Lwt option monad. Note that there are fundamental
        differences between [option] and [Lwt.t]: the former can be simply
        matched on (i.e., it is possible to get out of the monad at any point)
        whereas the latter can only be bound on (i.e., it is not possible to get
        out of the monad). In addition, the former is for aborting computations
        on failures (which in this case means that no value can be produced)
        whereas the latter is for waiting before continuing.

        Still, from a syntax point-of-view, both are handled the same way: with
        a specialised binding operator. *)

    (** [let*!] is for binding Lwt-only expressions into the Lwt option combined
        monad.

{[
let open Lwt_option_syntax in
let* x = … in
let*! y = … in
return (x + y)
]}

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

    (** [let*?] is for binding the value from Option-only
        expressions into the Lwt option combined monad.

{[
let open Lwt_option_syntax in
let* x = … in
let*? y = … in
…
]} *)
    val ( let*? ) : 'a option -> ('a -> 'b option Lwt.t) -> 'b option Lwt.t

    (** [both] is the joining of two concurrent optional non-unit values. *)
    val both : 'a option Lwt.t -> 'b option Lwt.t -> ('a * 'b) option Lwt.t
  end

  (** {2 The combined Lwt+Result monad: for concurrent successes/failures} *)

  (** Syntax module for Lwt+Result. This is intended to be opened locally in
      functions which use Lwt and [result] for control-flow. Within the scope of
      this module, the code can include binding operators, leading to a
      [let]-style syntax.

      See also {!Lwt}, {!Result}, and {!Lwt_result}. *)
  module Lwt_result_syntax : sig
    (** [return x] is [Lwt.return (Ok x)] or [Lwt_result.return x]. *)
    val return : 'a -> ('a, 'e) result Lwt.t

    (** [return_unit] is [Lwt.return (Ok ())] . *)
    val return_unit : (unit, 'e) result Lwt.t

    (** [return_none] is [Lwt.return (Ok None)] . *)
    val return_none : ('a option, 'e) result Lwt.t

    (** [return_some x] is [Lwt.return (Ok (Some x))] . *)
    val return_some : 'a -> ('a option, 'e) result Lwt.t

    (** [return_nil] is [Lwt.return (Ok [])] . *)
    val return_nil : ('a list, 'e) result Lwt.t

    (** [return_true] is [Lwt.return (Ok true)] . *)
    val return_true : (bool, 'e) result Lwt.t

    (** [return_false] is [Lwt.return (Ok false)] . *)
    val return_false : (bool, 'e) result Lwt.t

    (** Note that we do not provide [return_ok] nor [return_error]. Both of
        these functions are possible but somewhat confusing and rarely useful in
        practice. If you need to carry [result]s within a LwtResult-monad
        computation (yielding values of the type
        [(('a, 'e) result, 'e) result Lwt.t]), you need to do so by hand:
        [return (Ok …)] and [return (Error …)]. *)

    (** [fail e] is [Lwt.return (Error e)]. *)
    val fail : 'e -> ('a, 'e) result Lwt.t

    (** [let*] is a binding operator alias for {!Lwt_result.bind}. *)
    val ( let* ) :
      ('a, 'e) result Lwt.t ->
      ('a -> ('b, 'e) result Lwt.t) ->
      ('b, 'e) result Lwt.t

    (** [let+] is a binding operator alias for {!Lwt_result.map}. *)
    val ( let+ ) : ('a, 'e) result Lwt.t -> ('a -> 'b) -> ('b, 'e) result Lwt.t

    (** Note that we do not provide [and*] nor [and+]. Both of these are
        possible but their type is unsatisfying because the errors do not
        compose well. You can use [both] (below) if need be. *)

    (** [lwt_map_error] is an Lwt-aware variant of {!Result.map_error}. It is
        intended for mapping the errors of Lwt-result values. The main use of
        this function is for mixing results that carry different types of
        errors.

        E.g., considering [fetch : unit -> (string, unit) result Lwt.t] and
        [emit : string -> (unit, int) result Lwt.t], you can write

{[
let* data = lwt_map_error (fun () -> "fetching failed") @@ fetch () in
let* () =
  lwt_map_error (fun code -> Format.asprintf "emit failed (%d)")
  @@ emit data
in
..
]}

        *)
    val lwt_map_error :
      ('e -> 'f) -> ('a, 'e) result Lwt.t -> ('a, 'f) result Lwt.t

    (** The following values are for mixing expressions that are Lwt-only or
        Result-only within the LwtResult monad. Note that there are fundamental
        differences between [result] and [Lwt.t]: the former can be simply
        matched on (i.e., it is possible to get out of the monad at any point)
        whereas the latter can only be bound on (i.e., it is not possible to get
        out of the monad). In addition, the former is for aborting computations
        on failures whereas the latter is for waiting before continuing.

        Still, from a syntax point-of-view, both are handled the same way: with
        a specialised binding operator. *)

    (** [let*!] is for binding Lwt-only expressions into the LwtResult combined
        monad.

{[
let open Lwt_result_syntax in
let* x = … in
let*! y = … in
return (x + y)
]}

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

    (** [let*?] is for binding the value from Result-only
        expressions into the LwtResult combined monad.

{[
let open Lwt_result_syntax in
let* x = … in
let*? y = … in
…
]} *)
    val ( let*? ) :
      ('a, 'e) result -> ('a -> ('b, 'e) result Lwt.t) -> ('b, 'e) result Lwt.t

    (** Note that you can mix [let*], [let*!], and [let*?] as needed, within a
        single expression.

{[
let do_thing param =
  let open Lwt_result_syntax in
  let*? () = check_p param in (* Result-only for parameter checking *)
  let*! () = log "starting doing the thing" in (* Lwt-only for infallible logging *)
  let* r = thing param in
  let*! () = log "done doing the thing" in (* Lwt-only for infallible logging *)
  return r
]} *)

    (** [join] is the joining of concurrent success/failure unit values. *)
    val join : (unit, 'e) result Lwt.t list -> (unit, 'e list) result Lwt.t

    (** [all] is the joining of concurrent success/failure non-unit values. *)
    val all : ('a, 'e) result Lwt.t list -> ('a list, 'e list) result Lwt.t

    (** [both] is the joining of two concurrent success/failure non-unit values. *)
    val both :
      ('a, 'e) result Lwt.t ->
      ('b, 'e) result Lwt.t ->
      ('a * 'b, 'e list) result Lwt.t
  end
end
OCaml

Innovation. Community. Security.