Source file conv.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
open Import
type error =
| Parse_error of
{ message : string
; payload : (string * Sexp.t) list
}
| Version_error of
{ since : int * int
; until : (int * int) option
; message : string
; payload : (string * Sexp.t) list
}
let dyn_of_error =
let open Dyn in
function
| Version_error { message; payload; until; since } ->
record
[ ("message", string message)
; ("payload", list (pair string Sexp.to_dyn) payload)
; ("until", option (pair int int) until)
; ("since", (pair int int) since)
]
| Parse_error { message; payload } ->
record
[ ("message", string message)
; ("payload", list (pair string Sexp.to_dyn) payload)
]
exception Of_sexp of error
let raise_of_sexp ?(payload = []) message =
raise (Of_sexp (Parse_error { message; payload }))
let raise_version_error ?until ?(payload = []) ~since message =
raise (Of_sexp (Version_error { since; until; message; payload }))
let () =
Printexc.register_printer (function
| Of_sexp (Parse_error { message; payload }) ->
Some (message ^ " " ^ Sexp.to_string (Sexp.record payload))
| _ -> None)
module Fields = struct
type t = Unparsed of Sexp.t String.Map.t
let check_empty (Unparsed s) =
if not (String.Map.is_empty s) then
let payload =
[ ( "unparsed"
, Sexp.List
(String.Map.to_list s
|> List.map ~f:(fun (k, v) -> Sexp.List [ Sexp.Atom k; v ])) )
]
in
raise_of_sexp ~payload "unexpected fields"
let empty = Unparsed String.Map.empty
let merge (Unparsed a) (Unparsed b) =
Unparsed
(String.Map.union a b ~f:(fun _ _ _ ->
assert false))
let of_field name sexp = Unparsed (String.Map.singleton name sexp)
let of_sexp (x : Sexp.t) =
match x with
| Atom _ -> raise_of_sexp "Unexpected atom"
| List x -> (
match
String.Map.of_list_map x ~f:(function
| List [ Atom s; v ] -> (s, v)
| _ -> raise_of_sexp "unable to read field")
with
| Error (s, _, _) ->
raise_of_sexp "duplicate fields" ~payload:[ ("field", Atom s) ]
| Ok s -> Unparsed s)
let optional (Unparsed t) name =
match String.Map.find t name with
| None -> (None, Unparsed t)
| Some v -> (Some v, Unparsed (String.Map.remove t name))
let required t name =
let r, t = optional t name in
match r with
| Some s -> (s, t)
| None ->
raise_of_sexp "missing required field" ~payload:[ ("name", Atom name) ]
let to_sexp (Unparsed t) : Sexp.t =
List
(String.Map.to_list t
|> List.map ~f:(fun (k, v) -> Sexp.List [ Atom k; v ]))
end
type values = Sexp.t
type fields = Fields.t
type version =
{ since : int * int
; until : (int * int) option
}
type ('a, 'kind) t =
| Iso : ('a, 'kind) t * ('a -> 'b) * ('b -> 'a) -> ('b, 'kind) t
| Iso_result :
('a, 'kind) t * ('a -> ('b, exn) result) * ('b -> 'a)
-> ('b, 'kind) t
| Version : ('a, 'kind) t * version -> ('a, 'kind) t
| Both :
('a, fields) t
* ('b, fields) t
-> ('a * 'b, fields) t
| Sexp : (Sexp.t, values) t
| List : ('a, values) t -> ('a list, values) t
| Field : string * 'a field -> ('a, fields) t
| Enum : (string * 'a) list -> ('a, values) t
| Sum : 'a econstr list * ('a -> case) -> ('a, values) t
| Pair : ('a, values) t * ('b, values) t -> ('a * 'b, values) t
| Triple :
('a, values) t * ('b, values) t * ('c, values) t
-> ('a * 'b * 'c, values) t
| Fdecl : ('a, 'k) t Fdecl.t -> ('a, 'k) t
| Either :
('a, fields) t
* ('b, fields) t
-> (('a, 'b) Either.t, fields) t
| Record : ('a, fields) t -> ('a, values) t
and ('a, 'arg) constr =
{
name : string
; arg : ('arg, values) t
; inj : 'arg -> 'a
}
and 'a econstr = Constr : ('a, 'arg) constr -> 'a econstr
and case = Case : 'arg * ('a, 'arg) constr -> case
and 'a field =
| Required : ('a, values) t -> 'a field
| Optional : ('a, values) t -> 'a option field
and 'k ret =
| Values : values ret
| Fields : Fields.t -> fields ret
type 'a value = ('a, values) t
let case a c = Case (a, c)
let constr name arg inj = { name; arg; inj }
let econstr c = Constr c
let both x y = Both (x, y)
let list x = List x
let sum x y = Sum (x, y)
let pair x y = Pair (x, y)
let triple x y z = Triple (x, y, z)
let discard_values ((a, x) : _ * values ret) =
match (x : values ret) with
| Values -> a
let string =
Iso
( Sexp
, (function
| Atom s -> s
| List _ as list ->
raise_of_sexp
~payload:[ ("list", list) ]
"string: expected atom. received list")
, fun s -> Atom s )
let int =
Iso
( Sexp
, (function
| List _ as list ->
raise_of_sexp
~payload:[ ("list", list) ]
"int: expected atom. received list"
| Atom s -> (
match Int.of_string s with
| None -> raise_of_sexp "unable to read int"
| Some i -> i))
, fun s -> Atom (Int.to_string s) )
let unit =
Iso
( Sexp
, (function
| List [] -> ()
| _ -> raise_of_sexp "expected empty list")
, fun () -> List [] )
let option x =
let none = constr "None" unit (fun () -> None) in
let some = constr "Some" x (fun x -> Some x) in
sum
[ econstr none; econstr some ]
(function
| None -> case () none
| Some s -> case s some)
let char =
Iso
( Sexp
, (function
| Atom s ->
if String.length s = 1 then s.[0]
else raise_of_sexp "expected only a single character"
| List _ -> raise_of_sexp "expected a string of length 1")
, fun c -> Atom (String.make 1 c) )
let to_sexp : 'a. ('a, values) t -> 'a -> Sexp.t =
fun t a ->
let rec loop : type a k. (a, k) t -> a -> k =
fun t a ->
match t with
| Sexp -> a
| Version (t, _) -> loop t a
| Fdecl t -> loop (Fdecl.get t) a
| List t -> List (List.map a ~f:(loop t))
| Pair (x, y) ->
let a, b = a in
List [ loop x a; loop y b ]
| Triple (x, y, z) ->
let a, b, c = a in
List [ loop x a; loop y b; loop z c ]
| Record r ->
let fields = loop r a in
Fields.to_sexp fields
| Field (name, spec) -> (
match spec with
| Required t -> Fields.of_field name (loop t a)
| Optional t -> (
match a with
| None -> Fields.empty
| Some a -> Fields.of_field name (loop t a)))
| Iso_result (t, _, from) -> loop t (from a)
| Iso (t, _, from) -> loop t (from a)
| Both (x, y) ->
let x = loop x (fst a) in
let y = loop y (snd a) in
Fields.merge x y
| Either (x, y) -> (
match a with
| Left a -> loop x a
| Right a -> loop y a)
| Sum (_, constr) ->
let (Case (a, constr)) = constr a in
let arg = loop constr.arg a in
Sexp.List [ Atom constr.name; arg ]
| Enum choices -> (
match
List.find_map choices ~f:(fun (s, a') ->
if Poly.equal a a' then Some s else None)
with
| Some v -> Atom v
| None ->
let open Dyn in
Code_error.raise "enum does not include this value"
[ ("valid values", list (fun (x, _) -> string x) choices) ])
in
loop t a
let check_version ~version ~since ~until _ctx =
if
version < since
||
match until with
| None -> false
| Some until -> version > until
then raise_version_error ?until ~since "invalid version"
let of_sexp : 'a. ('a, values) t -> version:int * int -> Sexp.t -> 'a =
fun t ~version sexp ->
let rec loop : type a k. (a, k) t -> k -> a * k ret =
fun (type a k) (t : (a, k) t) (ctx : k) : (a * k ret) ->
match t with
| Sexp -> (ctx, Values)
| Version (t, { since; until }) ->
check_version ~version ~since ~until ctx;
loop t ctx
| Fdecl t -> loop (Fdecl.get t) ctx
| List t -> (
match ctx with
| List xs -> (List.map xs ~f:(fun x -> discard_values (loop t x)), Values)
| Atom _ -> raise_of_sexp "expected list")
| Pair (x, y) -> (
match ctx with
| List [ a; b ] ->
let a, Values = loop x a in
let b, Values = loop y b in
((a, b), Values)
| _ -> raise_of_sexp "expected field entry")
| Triple (x, y, z) -> (
match ctx with
| List [ a; b; c ] ->
let a, Values = loop x a in
let b, Values = loop y b in
let c, Values = loop z c in
((a, b, c), Values)
| _ -> raise_of_sexp "expected field entry")
| Record (r : (a, fields) t) ->
let (fields : Fields.t) = Fields.of_sexp ctx in
let a, Fields f = loop r fields in
Fields.check_empty f;
(a, Values)
| Field (name, spec) -> (
match spec with
| Required v ->
let field, rest = Fields.required ctx name in
let t, Values = loop v field in
(t, Fields rest)
| Optional v ->
let field, rest = Fields.optional ctx name in
let t =
match field with
| None -> None
| Some f ->
let a, Values = loop v f in
Some a
in
(t, Fields rest))
| Either (x, y) -> (
try
let a, x = loop x ctx in
(Left a, x)
with Of_sexp _ ->
let a, y = loop y ctx in
(Right a, y))
| Iso (t, f, _) ->
let a, k = loop t ctx in
(f a, k)
| Iso_result (t, f, _) -> (
let a, k = loop t ctx in
match f a with
| Error exn -> raise exn
| Ok a -> (a, k))
| Both (x, y) ->
let a, Fields k = loop x ctx in
let b, k = loop y k in
((a, b), k)
| Sum (constrs, _) -> (
match ctx with
| List [ Atom head; args ] -> (
match
List.find_map constrs ~f:(fun (Constr c) ->
if head = c.name then
Some
(let a, k = loop c.arg args in
(c.inj a, k))
else None)
with
| None -> raise_of_sexp "invalid constructor name"
| Some p -> p)
| _ -> raise_of_sexp "expected constructor")
| Enum choices -> (
match ctx with
| List _ -> raise_of_sexp "expected list"
| Atom a -> (
match List.assoc choices a with
| None -> raise_of_sexp "unable to read enum"
| Some s -> (s, Values)))
in
discard_values (loop t sexp)
let of_sexp conv ~version sexp =
match of_sexp conv ~version sexp with
| s -> Ok s
| exception Of_sexp e -> Error e
let record r = Record r
let either x y = Either (x, y)
let iso a t f = Iso (a, t, f)
let iso_result a t f = Iso_result (a, t, f)
let version ?until t ~since = Version (t, { until; since })
let field name spec = Field (name, spec)
let enum choices = Enum choices
let three a b c =
iso
(Both (a, Both (b, c)))
(fun (x, (y, z)) -> (x, y, z))
(fun (x, y, z) -> (x, (y, z)))
let four a b c d =
iso
(both (both a b) (both c d))
(fun ((w, x), (y, z)) -> (w, x, y, z))
(fun (w, x, y, z) -> ((w, x), (y, z)))
let five a b c d e =
iso
(both (both a b) (three c d e))
(fun ((a, b), (c, d, e)) -> (a, b, c, d, e))
(fun (a, b, c, d, e) -> ((a, b), (c, d, e)))
let six a b c d e f =
iso
(both (three a b c) (three d e f))
(fun ((a, b, c), (d, e, f)) -> (a, b, c, d, e, f))
(fun (a, b, c, d, e, f) -> ((a, b, c), (d, e, f)))
let seven a b c d e f g =
iso
(both (three a b c) (four d e f g))
(fun ((a, b, c), (d, e, f, g)) -> (a, b, c, d, e, f, g))
(fun (a, b, c, d, e, f, g) -> ((a, b, c), (d, e, f, g)))
let eight a b c d e f g h =
iso
(both (four a b c d) (four e f g h))
(fun ((a, b, c, d), (e, f, g, h)) -> (a, b, c, d, e, f, g, h))
(fun (a, b, c, d, e, f, g, h) -> ((a, b, c, d), (e, f, g, h)))
let sexp = Sexp
let required x = Required x
let optional x = Optional x
let fdecl x = Fdecl x