Source file result.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
open! Import
module Either = Either0
type ('a, 'b) t = ('a, 'b) Stdlib.result =
| Ok of 'a
| Error of 'b
[@@deriving_inline sexp, sexp_grammar, compare ~localize, equal ~localize, hash]
let t_of_sexp :
'a 'b.
(Sexplib0.Sexp.t -> 'a) -> (Sexplib0.Sexp.t -> 'b) -> Sexplib0.Sexp.t -> ('a, 'b) t
=
fun (type a__017_ b__018_)
: ((Sexplib0.Sexp.t -> a__017_) -> (Sexplib0.Sexp.t -> b__018_) -> Sexplib0.Sexp.t
-> (a__017_, b__018_) t) ->
let error_source__005_ = "result.ml.t" in
fun _of_a__001_ _of_b__002_ -> function
| Sexplib0.Sexp.List
(Sexplib0.Sexp.Atom (("ok" | "Ok") as _tag__008_) :: sexp_args__009_) as
_sexp__007_ ->
(match sexp_args__009_ with
| [ arg0__010_ ] ->
let res0__011_ = _of_a__001_ arg0__010_ in
Ok res0__011_
| _ ->
Sexplib0.Sexp_conv_error.stag_incorrect_n_args
error_source__005_
_tag__008_
_sexp__007_)
| Sexplib0.Sexp.List
(Sexplib0.Sexp.Atom (("error" | "Error") as _tag__013_) :: sexp_args__014_) as
_sexp__012_ ->
(match sexp_args__014_ with
| [ arg0__015_ ] ->
let res0__016_ = _of_b__002_ arg0__015_ in
Error res0__016_
| _ ->
Sexplib0.Sexp_conv_error.stag_incorrect_n_args
error_source__005_
_tag__013_
_sexp__012_)
| Sexplib0.Sexp.Atom ("ok" | "Ok") as sexp__006_ ->
Sexplib0.Sexp_conv_error.stag_takes_args error_source__005_ sexp__006_
| Sexplib0.Sexp.Atom ("error" | "Error") as sexp__006_ ->
Sexplib0.Sexp_conv_error.stag_takes_args error_source__005_ sexp__006_
| Sexplib0.Sexp.List (Sexplib0.Sexp.List _ :: _) as sexp__004_ ->
Sexplib0.Sexp_conv_error.nested_list_invalid_sum error_source__005_ sexp__004_
| Sexplib0.Sexp.List [] as sexp__004_ ->
Sexplib0.Sexp_conv_error.empty_list_invalid_sum error_source__005_ sexp__004_
| sexp__004_ -> Sexplib0.Sexp_conv_error.unexpected_stag error_source__005_ sexp__004_
;;
let sexp_of_t :
'a 'b.
('a -> Sexplib0.Sexp.t) -> ('b -> Sexplib0.Sexp.t) -> ('a, 'b) t -> Sexplib0.Sexp.t
=
fun (type a__025_ b__026_)
: ((a__025_ -> Sexplib0.Sexp.t) -> (b__026_ -> Sexplib0.Sexp.t)
-> (a__025_, b__026_) t -> Sexplib0.Sexp.t) ->
fun _of_a__019_ _of_b__020_ -> function
| Ok arg0__021_ ->
let res0__022_ = _of_a__019_ arg0__021_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Ok"; res0__022_ ]
| Error arg0__023_ ->
let res0__024_ = _of_b__020_ arg0__023_ in
Sexplib0.Sexp.List [ Sexplib0.Sexp.Atom "Error"; res0__024_ ]
;;
let t_sexp_grammar :
'a 'b.
'a Sexplib0.Sexp_grammar.t
-> 'b Sexplib0.Sexp_grammar.t
-> ('a, 'b) t Sexplib0.Sexp_grammar.t
=
fun _'a_sexp_grammar _'b_sexp_grammar ->
{ untyped =
Variant
{ case_sensitivity = Case_sensitive_except_first_character
; clauses =
[ No_tag
{ name = "Ok"
; clause_kind =
List_clause { args = Cons (_'a_sexp_grammar.untyped, Empty) }
}
; No_tag
{ name = "Error"
; clause_kind =
List_clause { args = Cons (_'b_sexp_grammar.untyped, Empty) }
}
]
}
}
;;
let compare__local :
'a 'b. ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a, 'b) t -> ('a, 'b) t -> int
=
fun _cmp__a _cmp__b a__033_ b__034_ ->
if Stdlib.( == ) a__033_ b__034_
then 0
else (
match a__033_, b__034_ with
| Ok _a__035_, Ok _b__036_ -> _cmp__a _a__035_ _b__036_
| Ok _, _ -> -1
| _, Ok _ -> 1
| Error _a__037_, Error _b__038_ -> _cmp__b _a__037_ _b__038_)
;;
let compare :
'a 'b. ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a, 'b) t -> ('a, 'b) t -> int
=
fun _cmp__a _cmp__b a__027_ b__028_ ->
if Stdlib.( == ) a__027_ b__028_
then 0
else (
match a__027_, b__028_ with
| Ok _a__029_, Ok _b__030_ -> _cmp__a _a__029_ _b__030_
| Ok _, _ -> -1
| _, Ok _ -> 1
| Error _a__031_, Error _b__032_ -> _cmp__b _a__031_ _b__032_)
;;
let equal__local :
'a 'b. ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a, 'b) t -> ('a, 'b) t -> bool
=
fun _cmp__a _cmp__b a__045_ b__046_ ->
if Stdlib.( == ) a__045_ b__046_
then true
else (
match a__045_, b__046_ with
| Ok _a__047_, Ok _b__048_ -> _cmp__a _a__047_ _b__048_
| Ok _, _ -> false
| _, Ok _ -> false
| Error _a__049_, Error _b__050_ -> _cmp__b _a__049_ _b__050_)
;;
let equal :
'a 'b. ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a, 'b) t -> ('a, 'b) t -> bool
=
fun _cmp__a _cmp__b a__039_ b__040_ ->
if Stdlib.( == ) a__039_ b__040_
then true
else (
match a__039_, b__040_ with
| Ok _a__041_, Ok _b__042_ -> _cmp__a _a__041_ _b__042_
| Ok _, _ -> false
| _, Ok _ -> false
| Error _a__043_, Error _b__044_ -> _cmp__b _a__043_ _b__044_)
;;
let hash_fold_t
: type a b.
(Ppx_hash_lib.Std.Hash.state -> a -> Ppx_hash_lib.Std.Hash.state)
-> (Ppx_hash_lib.Std.Hash.state -> b -> Ppx_hash_lib.Std.Hash.state)
-> Ppx_hash_lib.Std.Hash.state
-> (a, b) t
-> Ppx_hash_lib.Std.Hash.state
=
fun _hash_fold_a _hash_fold_b hsv arg ->
match arg with
| Ok _a0 ->
let hsv = Ppx_hash_lib.Std.Hash.fold_int hsv 0 in
let hsv = hsv in
_hash_fold_a hsv _a0
| Error _a0 ->
let hsv = Ppx_hash_lib.Std.Hash.fold_int hsv 1 in
let hsv = hsv in
_hash_fold_b hsv _a0
;;
[@@@end]
let globalize = globalize_result
include Monad.Make2_local (struct
type nonrec ('a, 'b) t = ('a, 'b) t
let bind x ~f =
match x with
| Error _ as x -> x
| Ok x -> f x
;;
let map x ~f =
match x with
| Error _ as x -> x
| Ok x -> Ok (f x)
;;
let map = `Custom map
let return x = Ok x
end)
let invariant check_ok check_error t =
match t with
| Ok ok -> check_ok ok
| Error error -> check_error error
;;
let fail x = Error x
let failf format = Printf.ksprintf fail format
let map_error t ~f =
match t with
| Ok _ as x -> x
| Error x -> Error (f x)
;;
module Error = Monad.Make2_local (struct
type nonrec ('a, 'b) t = ('b, 'a) t
let bind x ~f =
match x with
| Ok _ as ok -> ok
| Error e -> f e
;;
let map = `Custom map_error
let return e = Error e
end)
let is_ok = function
| Ok _ -> true
| Error _ -> false
;;
let is_error = function
| Ok _ -> false
| Error _ -> true
;;
let ok = function
| Ok x -> Some x
| Error _ -> None
;;
let error = function
| Ok _ -> None
| Error x -> Some x
;;
let of_option opt ~error =
match opt with
| Some x -> Ok x
| None -> Error error
;;
let iter v ~f =
match v with
| Ok x -> f x
| Error _ -> ()
;;
let iter_error v ~f =
match v with
| Ok _ -> ()
| Error x -> f x
;;
let to_either : _ t -> _ Either.t = function
| Ok x -> First x
| Error x -> Second x
;;
let of_either : _ Either.t -> _ t = function
| First x -> Ok x
| Second x -> Error x
;;
let ok_if_true bool ~error = if bool then Ok () else Error error
let try_with f =
try Ok (f ()) with
| exn -> Error exn
;;
let ok_exn = function
| Ok x -> x
| Error exn -> raise exn
;;
let ok_or_failwith = function
| Ok x -> x
| Error str -> failwith str
;;
module Export = struct
type ('ok, 'err) _result = ('ok, 'err) t =
| Ok of 'ok
| Error of 'err
let is_error = is_error
let is_ok = is_ok
end
let combine t1 t2 ~ok ~err =
match t1, t2 with
| Ok _, Error e | Error e, Ok _ -> Error e
| Ok ok1, Ok ok2 -> Ok (ok ok1 ok2)
| Error err1, Error err2 -> Error (err err1 err2)
;;
let combine_errors l =
let ok, errs = List1.partition_map l ~f:to_either in
match errs with
| [] -> Ok ok
| _ :: _ -> Error errs
;;
let combine_errors_unit l = map (combine_errors l) ~f:(fun (_ : unit list) -> ())