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
type annot = string list
type ('l, 'p) node =
| Int of 'l * Z.t
| String of 'l * string
| Bytes of 'l * Bytes.t
| Prim of 'l * 'p * ('l, 'p) node list * annot
| Seq of 'l * ('l, 'p) node list
type canonical_location = int
let dummy_location = -1
type 'p canonical = Canonical of (canonical_location, 'p) node [@@unboxed]
let location = function
| Int (loc, _) -> loc
| String (loc, _) -> loc
| Bytes (loc, _) -> loc
| Seq (loc, _) -> loc
| Prim (loc, _, _, _) -> loc
let annotations = function
| Int (_, _) -> []
| String (_, _) -> []
| Bytes (_, _) -> []
| Seq (_, _) -> []
| Prim (_, _, _, annots) -> annots
let root (Canonical expr) = expr
type ('l, 'p, 'la, 'pa) cont =
| Seq_cont of 'la * ('l, 'p, 'la, 'pa) list_cont
| Prim_cont of 'la * 'pa * annot * ('l, 'p, 'la, 'pa) list_cont
and ('l, 'p, 'la, 'pa) list_cont =
| List_cont of
('l, 'p) node list * ('la, 'pa) node list * ('l, 'p, 'la, 'pa) cont
| Return
let strip_locations (type a b) (root : (a, b) node) : b canonical =
let id =
let id = ref (-1) in
fun () ->
incr id ;
!id
in
let rec strip_locations l k =
let id = id () in
match l with
| Int (_, v) -> (apply [@tailcall]) k (Int (id, v))
| String (_, v) -> (apply [@tailcall]) k (String (id, v))
| Bytes (_, v) -> (apply [@tailcall]) k (Bytes (id, v))
| Seq (_, seq) ->
(strip_locations_list [@tailcall]) seq [] (Seq_cont (id, k))
| Prim (_, name, seq, annots) ->
(strip_locations_list [@tailcall])
seq
[]
(Prim_cont (id, name, annots, k))
and strip_locations_list ls acc k =
match ls with
| [] -> (apply_list [@tailcall]) k (List.rev acc)
| x :: tl -> (strip_locations [@tailcall]) x (List_cont (tl, acc, k))
and apply k node =
match k with
| List_cont (tl, acc, k) ->
(strip_locations_list [@tailcall]) tl (node :: acc) k
| Return -> node
and apply_list k node_list =
match k with
| Seq_cont (id, k) -> (apply [@tailcall]) k (Seq (id, node_list))
| Prim_cont (id, name, annots, k) ->
(apply [@tailcall]) k (Prim (id, name, node_list, annots))
in
Canonical (strip_locations root Return)
let :
type l p. (l, p) node -> p canonical * (canonical_location * l) list =
fun root ->
let id =
let id = ref (-1) in
fun () ->
incr id ;
!id
in
let loc_table = ref [] in
let rec strip_locations l k =
let id = id () in
match l with
| Int (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
(apply [@tailcall]) k (Int (id, v))
| String (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
(apply [@tailcall]) k (String (id, v))
| Bytes (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
(apply [@tailcall]) k (Bytes (id, v))
| Seq (loc, seq) ->
loc_table := (id, loc) :: !loc_table ;
(strip_locations_list [@tailcall]) seq [] (Seq_cont (id, k))
| Prim (loc, name, seq, annots) ->
loc_table := (id, loc) :: !loc_table ;
(strip_locations_list [@tailcall])
seq
[]
(Prim_cont (id, name, annots, k))
and strip_locations_list ls acc k =
match ls with
| [] -> (apply_list [@tailcall]) k (List.rev acc)
| x :: tl -> (strip_locations [@tailcall]) x (List_cont (tl, acc, k))
and apply k node =
match k with
| List_cont (tl, acc, k) ->
(strip_locations_list [@tailcall]) tl (node :: acc) k
| Return -> node
and apply_list k node_list =
match k with
| Seq_cont (id, k) -> (apply [@tailcall]) k (Seq (id, node_list))
| Prim_cont (id, name, annots, k) ->
(apply [@tailcall]) k (Prim (id, name, node_list, annots))
in
let stripped = strip_locations root Return in
(Canonical stripped, List.rev !loc_table)
let inject_locations :
type l p. (canonical_location -> l) -> p canonical -> (l, p) node =
fun lookup (Canonical root) ->
let rec inject_locations l k =
match l with
| Int (loc, v) -> (apply [@tailcall]) k (Int (lookup loc, v))
| String (loc, v) -> (apply [@tailcall]) k (String (lookup loc, v))
| Bytes (loc, v) -> (apply [@tailcall]) k (Bytes (lookup loc, v))
| Seq (loc, seq) ->
(inject_locations_list [@tailcall]) seq [] (Seq_cont (lookup loc, k))
| Prim (loc, name, seq, annots) ->
(inject_locations_list [@tailcall])
seq
[]
(Prim_cont (lookup loc, name, annots, k))
and inject_locations_list ls acc k =
match ls with
| [] -> (apply_list [@tailcall]) k (List.rev acc)
| x :: tl -> (inject_locations [@tailcall]) x (List_cont (tl, acc, k))
and apply k node =
match k with
| List_cont (tl, acc, k) ->
(inject_locations_list [@tailcall]) tl (node :: acc) k
| Return -> node
and apply_list k node_list =
match k with
| Seq_cont (id, k) -> (apply [@tailcall]) k (Seq (id, node_list))
| Prim_cont (id, name, annots, k) ->
(apply [@tailcall]) k (Prim (id, name, node_list, annots))
in
inject_locations root Return
let map : type a b. (a -> b) -> a canonical -> b canonical =
fun f (Canonical expr) ->
let rec map_node l k =
match l with
| (Int _ | String _ | Bytes _) as node -> (apply [@tailcall]) k node
| Seq (loc, seq) -> (map_list [@tailcall]) seq [] (Seq_cont (loc, k))
| Prim (loc, name, seq, annots) ->
(map_list [@tailcall]) seq [] (Prim_cont (loc, f name, annots, k))
and map_list ls acc k =
match ls with
| [] -> (apply_list [@tailcall]) k (List.rev acc)
| x :: tl -> (map_node [@tailcall]) x (List_cont (tl, acc, k))
and apply k node =
match k with
| List_cont (tl, acc, k) -> (map_list [@tailcall]) tl (node :: acc) k
| Return -> node
and apply_list k node_list =
match k with
| Seq_cont (id, k) -> (apply [@tailcall]) k (Seq (id, node_list))
| Prim_cont (id, name, annots, k) ->
(apply [@tailcall]) k (Prim (id, name, node_list, annots))
in
Canonical (map_node expr Return)
let map_node :
type la lb pa pb. (la -> lb) -> (pa -> pb) -> (la, pa) node -> (lb, pb) node
=
fun fl fp node ->
let rec map_node fl fp node k =
match node with
| Int (loc, v) -> (apply [@tailcall]) fl fp k (Int (fl loc, v))
| String (loc, v) -> (apply [@tailcall]) fl fp k (String (fl loc, v))
| Bytes (loc, v) -> (apply [@tailcall]) fl fp k (Bytes (fl loc, v))
| Seq (loc, seq) ->
(map_node_list [@tailcall]) fl fp seq [] (Seq_cont (fl loc, k))
| Prim (loc, name, seq, annots) ->
(map_node_list [@tailcall])
fl
fp
seq
[]
(Prim_cont (fl loc, fp name, annots, k))
and map_node_list fl fp ls acc k =
match ls with
| [] -> (apply_list [@tailcall]) fl fp k (List.rev acc)
| x :: tl -> (map_node [@tailcall]) fl fp x (List_cont (tl, acc, k))
and apply fl fp k node =
match k with
| List_cont (tl, acc, k) ->
(map_node_list [@tailcall]) fl fp tl (node :: acc) k
| Return -> node
and apply_list fl fp k node_list =
match k with
| Seq_cont (id, k) -> (apply [@tailcall]) fl fp k (Seq (id, node_list))
| Prim_cont (id, name, annots, k) ->
(apply [@tailcall]) fl fp k (Prim (id, name, node_list, annots))
in
(map_node [@tailcall]) fl fp node Return
(** Testing
-------
Component: Micheline
Invocation: dune build @src/lib_micheline/runtest
Subject: Test preservation of semantics wrt original implementation
*)
let%test_module "semantics_preservation" =
(module struct
module Original = struct
let strip_locations root =
let id =
let id = ref (-1) in
fun () ->
incr id ;
!id
in
let rec strip_locations l =
let id = id () in
match l with
| Int (_, v) -> Int (id, v)
| String (_, v) -> String (id, v)
| Bytes (_, v) -> Bytes (id, v)
| Seq (_, seq) -> Seq (id, List.map strip_locations seq)
| Prim (_, name, seq, annots) ->
Prim (id, name, List.map strip_locations seq, annots)
in
Canonical (strip_locations root)
let root =
let id =
let id = ref (-1) in
fun () ->
incr id ;
!id
in
let loc_table = ref [] in
let rec strip_locations l =
let id = id () in
match l with
| Int (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
Int (id, v)
| String (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
String (id, v)
| Bytes (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
Bytes (id, v)
| Seq (loc, seq) ->
loc_table := (id, loc) :: !loc_table ;
Seq (id, List.map strip_locations seq)
| Prim (loc, name, seq, annots) ->
loc_table := (id, loc) :: !loc_table ;
Prim (id, name, List.map strip_locations seq, annots)
in
let stripped = strip_locations root in
(Canonical stripped, List.rev !loc_table)
let inject_locations lookup (Canonical root) =
let rec inject_locations l =
match l with
| Int (loc, v) -> Int (lookup loc, v)
| String (loc, v) -> String (lookup loc, v)
| Bytes (loc, v) -> Bytes (lookup loc, v)
| Seq (loc, seq) -> Seq (lookup loc, List.map inject_locations seq)
| Prim (loc, name, seq, annots) ->
Prim (lookup loc, name, List.map inject_locations seq, annots)
in
inject_locations root
let map f (Canonical expr) =
let rec map_node f = function
| (Int _ | String _ | Bytes _) as node -> node
| Seq (loc, seq) -> Seq (loc, List.map (map_node f) seq)
| Prim (loc, name, seq, annots) ->
Prim (loc, f name, List.map (map_node f) seq, annots)
in
Canonical (map_node f expr)
let rec map_node fl fp = function
| Int (loc, v) -> Int (fl loc, v)
| String (loc, v) -> String (fl loc, v)
| Bytes (loc, v) -> Bytes (fl loc, v)
| Seq (loc, seq) -> Seq (fl loc, List.map (map_node fl fp) seq)
| Prim (loc, name, seq, annots) ->
Prim (fl loc, fp name, List.map (map_node fl fp) seq, annots)
end
module Sampler = struct
type 'a sampler = Random.State.t -> 'a
type width_function = depth:int -> int sampler
type node_kind =
| Int_node
| String_node
| Bytes_node
| Seq_node
| Prim_node
let all_kinds = [|Int_node; String_node; Bytes_node; Seq_node; Prim_node|]
let sample_kind : node_kind sampler =
fun rng_state ->
let i = Random.State.int rng_state (Array.length all_kinds) in
all_kinds.(i)
let sample_string _ = ""
let sample_bytes _ = Bytes.empty
let sample_z _ = Z.zero
let sample (w : width_function) rng_state =
let rec sample depth rng_state k =
match sample_kind rng_state with
| Int_node -> k (Int (0, sample_z rng_state))
| String_node -> k (String (0, sample_string rng_state))
| Bytes_node -> k (Bytes (0, sample_bytes rng_state))
| Seq_node ->
let width = w ~depth rng_state in
sample_list
depth
width
[]
(fun terms -> k (Seq (0, terms)))
rng_state
| Prim_node ->
let width = w ~depth rng_state in
sample_list
depth
width
[]
(fun terms -> k (Prim (0, (), terms, [])))
rng_state
and sample_list depth width acc k rng_state =
if width < 0 then invalid_arg "sample_list: negative width"
else if width = 0 then k (List.rev acc)
else
sample (depth + 1) rng_state (fun x ->
sample_list depth (width - 1) (x :: acc) k rng_state)
in
sample 0 rng_state (fun x -> x)
let sample_in_interval min max state =
if max - min >= 0 then min + Random.State.int state (max - min + 1)
else invalid_arg "sample_in_interval"
let reasonable_width_function ~depth rng_state =
match Sys.backend_type with
| Other _ ->
sample_in_interval 0 (19 / (Bits.numbits depth + 1)) rng_state
| Native | Bytecode ->
sample_in_interval 0 (20 / (Bits.numbits depth + 1)) rng_state
let sample = sample reasonable_width_function
end
let rng_state = Random.State.make [|0x1337; 0x533D|]
let rec sample_and_check_n_times' ~map_term n f g =
if n <= 0 then ()
else
let term = Sampler.sample rng_state in
let term = map_term term in
assert (f term = g term) ;
sample_and_check_n_times' ~map_term (n - 1) f g
let sample_and_check_n_times_canon n =
sample_and_check_n_times' ~map_term:strip_locations n
let sample_and_check_n_times n =
sample_and_check_n_times' ~map_term:(fun x -> x) n
let%expect_test "strip_locations" =
sample_and_check_n_times 1_000 Original.strip_locations strip_locations ;
[%expect {||}]
let%expect_test "extract_locations" =
sample_and_check_n_times
1_000
Original.extract_locations
extract_locations ;
[%expect {||}]
let%expect_test "inject_locations" =
sample_and_check_n_times_canon
1_000
(Original.inject_locations (fun i -> i))
(inject_locations (fun i -> i)) ;
[%expect {||}]
let%expect_test "map" =
sample_and_check_n_times_canon
1_000
(Original.map (fun _i -> ()))
(map (fun _i -> ())) ;
[%expect {||}]
let%expect_test "map_node" =
sample_and_check_n_times
1_000
(Original.map_node (fun _i -> ()) (fun _i -> ()))
(map_node (fun _i -> ()) (fun _i -> ())) ;
[%expect {||}]
end)