Source file missing_pervasives.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
open Lem_basic_classes
open Lem_bool
open Lem_list
open Lem_maybe
open Lem_num
open Lem_string
open Lem_assert_extra
open Show
open Lem_sorting
let id0 x:'a= x
let compare_byte b1 b2:int= (Nat_big_num.compare (Nat_big_num.of_int (Char.code b1)) (Nat_big_num.of_int (Char.code b2)))
let instance_Basic_classes_Ord_Missing_pervasives_byte_dict:(char)ord_class= ({
compare_method = compare_byte;
isLess_method = (fun f1 -> (fun f2 -> ( Lem.orderingEqual(compare_byte f1 f2) (-1))));
isLessEqual_method = (fun f1 -> (fun f2 -> let result = (compare_byte f1 f2) in Lem.orderingEqual result (-1) || Lem.orderingEqual result 0));
isGreater_method = (fun f1 -> (fun f2 -> ( Lem.orderingEqual(compare_byte f1 f2) 1)));
isGreaterEqual_method = (fun f1 -> (fun f2 -> let result = (compare_byte f1 f2) in Lem.orderingEqual result 1 || Lem.orderingEqual result 0))})
let hex_char_of_nibble n:char=
(if Nat_big_num.equal n( (Nat_big_num.of_int 0)) then
'0'
else if Nat_big_num.equal n( (Nat_big_num.of_int 1)) then
'1'
else if Nat_big_num.equal n( (Nat_big_num.of_int 2)) then
'2'
else if Nat_big_num.equal n( (Nat_big_num.of_int 3)) then
'3'
else if Nat_big_num.equal n( (Nat_big_num.of_int 4)) then
'4'
else if Nat_big_num.equal n( (Nat_big_num.of_int 5)) then
'5'
else if Nat_big_num.equal n( (Nat_big_num.of_int 6)) then
'6'
else if Nat_big_num.equal n( (Nat_big_num.of_int 7)) then
'7'
else if Nat_big_num.equal n( (Nat_big_num.of_int 8)) then
'8'
else if Nat_big_num.equal n( (Nat_big_num.of_int 9)) then
'9'
else if Nat_big_num.equal n( (Nat_big_num.of_int 10)) then
'a'
else if Nat_big_num.equal n( (Nat_big_num.of_int 11)) then
'b'
else if Nat_big_num.equal n( (Nat_big_num.of_int 12)) then
'c'
else if Nat_big_num.equal n( (Nat_big_num.of_int 13)) then
'd'
else if Nat_big_num.equal n( (Nat_big_num.of_int 14)) then
'e'
else if Nat_big_num.equal n( (Nat_big_num.of_int 15)) then
'f'
else
(assert false))
let hex_string_of_byte b:string=
(Xstring.implode [ hex_char_of_nibble ( Nat_big_num.div(Nat_big_num.of_int (Char.code b))( (Nat_big_num.of_int 16)))
; hex_char_of_nibble ( Nat_big_num.modulus(Nat_big_num.of_int (Char.code b))( (Nat_big_num.of_int 16)))])
let instance_Show_Show_Missing_pervasives_byte_dict:(char)show_class= ({
show_method = hex_string_of_byte})
let natural_of_decimal_digit c:(Nat_big_num.num)option=
(if c = '0' then
Some( (Nat_big_num.of_int 0))
else if c = '1' then
Some( (Nat_big_num.of_int 1))
else if c = '2' then
Some( (Nat_big_num.of_int 2))
else if c = '3' then
Some( (Nat_big_num.of_int 3))
else if c = '4' then
Some( (Nat_big_num.of_int 4))
else if c = '5' then
Some( (Nat_big_num.of_int 5))
else if c = '6' then
Some( (Nat_big_num.of_int 6))
else if c = '7' then
Some( (Nat_big_num.of_int 7))
else if c = '8' then
Some( (Nat_big_num.of_int 8))
else if c = '9' then
Some( (Nat_big_num.of_int 9))
else
None)
let rec natural_of_decimal_string_helper acc chars:Nat_big_num.num=
((match chars with
[] -> acc
| c :: cs -> (match natural_of_decimal_digit c with
Some dig -> natural_of_decimal_string_helper ( Nat_big_num.add( Nat_big_num.mul( (Nat_big_num.of_int 10)) acc) dig) cs
| None -> acc
)
))
let natural_of_decimal_string s:Nat_big_num.num=
(natural_of_decimal_string_helper( (Nat_big_num.of_int 0)) (Xstring.explode s))
let rec hex_string_of_natural n:string=
(if Nat_big_num.less n( (Nat_big_num.of_int 16)) then Xstring.implode [hex_char_of_nibble n]
else (hex_string_of_natural ( Nat_big_num.div n( (Nat_big_num.of_int 16)))) ^ (Xstring.implode [hex_char_of_nibble ( Nat_big_num.modulus n( (Nat_big_num.of_int 16)))]))
let natural_of_bool b:Nat_big_num.num=
((match b with
| true -> (Nat_big_num.of_int 1)
| false -> (Nat_big_num.of_int 0)
))
let rec merge_by comp xs ys:'a list=
((match (xs, ys) with
| ([], ys) -> ys
| (xs, []) -> xs
| (x::xs, y::ys) ->
if Lem.orderingEqual (comp x y) (-1) then
x::(merge_by comp xs (y::ys))
else
y::(merge_by comp (x::xs) ys)
))
(** [mapMaybei f xs] maps a function expecting an index (the position in the list
* [xs] that it is currently viewing) and producing a [maybe] type across a list.
* Elements that produce [Nothing] under [f] are discarded in the output, whilst
* those producing [Just e] for some [e] are kept.
*)
let rec mapMaybei' f idx1 xs:'b list=
((match xs with
| [] -> []
| x::xs ->
(match f idx1 x with
| None -> mapMaybei' f ( Nat_big_num.add( (Nat_big_num.of_int 1)) idx1) xs
| Some e -> e :: mapMaybei' f ( Nat_big_num.add( (Nat_big_num.of_int 1)) idx1) xs
)
))
let mapMaybei f xs:'b list=
(mapMaybei' f( (Nat_big_num.of_int 0)) xs)
(** [partitionii is xs] returns a pair of lists: firstly those elements in [xs] that are
at indices in [is], and secondly the remaining elements.
It preserves the order of elements in xs. *)
let rec partitionii' (offset : Nat_big_num.num) sorted_is xs reverse_accum reverse_accum_compl:(Nat_big_num.num*'a)list*(Nat_big_num.num*'a)list=
(
(match sorted_is with
[] -> (List.rev reverse_accum, List.rev reverse_accum_compl)
| i :: more_is ->
let (length_to_split_off : int) = (Nat_big_num.to_int ( Nat_big_num.sub_nat i offset))
in
let (left, right) = (Lem_list.split_at length_to_split_off xs) in
let left_indices : Nat_big_num.num list = (Lem_list.genlist
(fun j -> Nat_big_num.add (Nat_big_num.of_int j) offset)
(List.length left))
in
let left_with_indices = (list_combine left_indices left) in
(match right with
[] ->
(List.rev reverse_accum,
List.rev_append reverse_accum_compl left_with_indices)
| x :: more_xs ->
partitionii' (Nat_big_num.add i( (Nat_big_num.of_int 1))) more_is more_xs ((i, x) :: reverse_accum)
(List.rev_append left_with_indices reverse_accum_compl)
)
))
let filteri is xs:'a list=
(let sorted_is = (List.sort Nat_big_num.compare is) in
let (accum, accum_compl) = (partitionii'( (Nat_big_num.of_int 0)) sorted_is xs [] [])
in
let (just_indices, just_items) = (List.split accum)
in
just_items)
let filterii is xs:(Nat_big_num.num*'a)list=
(let sorted_is = (List.sort Nat_big_num.compare is) in
let (accum, accum_compl) = (partitionii'( (Nat_big_num.of_int 0)) sorted_is xs [] [])
in
accum)
let partitioni is xs:'a list*'a list=
(let sorted_is = (List.sort Nat_big_num.compare is) in
let (accum, accum_compl) = (partitionii'( (Nat_big_num.of_int 0)) sorted_is xs [] [])
in
let (just_indices, just_items) = (List.split accum)
in
let (just_indices_compl, just_items_compl) = (List.split accum_compl)
in
(just_items, just_items_compl))
let partitionii is xs:(Nat_big_num.num*'a)list*(Nat_big_num.num*'a)list=
(let sorted_is = (List.sort Nat_big_num.compare is) in
partitionii'( (Nat_big_num.of_int 0)) sorted_is xs [] [])
(** [unzip3 ls] takes a list of triples and returns a triple of lists. *)
let rec unzip3 l:'a list*'b list*'c list= ((match l with
| [] -> ([], [], [])
| (x, y, z) :: xyzs -> let (xs, ys, zs) = (unzip3 xyzs) in ((x :: xs), (y :: ys), (z :: zs))
))
(** [zip3 ls] takes a triple of lists and returns a list of triples. *)
let rec zip3 alist blist clist:('a*'b*'c)list= ((match (alist, blist, clist) with
| ([], [], []) -> []
| (x :: morex, y :: morey, z :: morez) -> let more_xyz = (zip3 morex morey morez) in (x, y, z) :: more_xyz
))
(** [null_byte] is the null character a a byte. *)
(** [null_char] is the null character. *)
let null_char:char= ( '\000')
(** [println s] prints [s] to stdout, adding a trailing newline. *)
(** [prints s] prints [s] to stdout, without adding a trailing newline. *)
(** [errln s] prints [s] to stderr, adding a trailing newline. *)
(** [errs s] prints [s] to stderr, without adding a trailing newline. *)
(** [outln s] prints [s] to stdout, adding a trailing newline. *)
(** [outs s] prints [s] to stdout, without adding a trailing newline. *)
(** [intercalate sep xs] places [sep] between all elements of [xs].
* Made tail recursive and unrolled slightly to improve performance on large
* lists.*)
let rec intercalate' sep xs accum:'a list=
((match xs with
| [] -> List.rev accum
| [x] -> List.rev_append (List.rev (List.rev accum)) [x]
| [x; y] -> List.rev_append (List.rev (List.rev accum)) [x; sep; y]
| x::y::xs -> intercalate' sep xs (sep::(y::(sep::(x::accum))))
))
let intercalate sep xs:'a list= (intercalate' sep xs [])
(** [unlines xs] concatenates a list of strings [xs], placing each entry
* on a new line.
*)
let unlines xs:string=
(List.fold_left (^) "" (intercalate "\n" xs))
(** [bracket xs] concatenates a list of strings [xs], separating each entry with a
* space, and bracketing the resulting string.
*)
let bracket xs:string=
("(" ^ (List.fold_left (^) "" (intercalate " " xs) ^ ")"))
(** [string_of_list l] produces a string representation of list [l].
*)
let string_of_list dict_Show_Show_a l:string=
(let result = (intercalate "," (Lem_list.map
dict_Show_Show_a.show_method l)) in
let folded = (List.fold_left (^) "" result) in
"[" ^ (folded ^ "]"))
let instance_Show_Show_list_dict dict_Show_Show_a:('a list)show_class= ({
show_method =
(string_of_list dict_Show_Show_a)})
(** [split_string_on_char s c] splits a string [s] into a list of substrings
* on character [c], otherwise returning the singleton list containing [s]
* if [c] is not found in [s].
*
* NOTE: quirkily, this doesn't discard separators (e.g. because NUL characters
* are significant when indexing into string tables). FIXME: given this, is this
* function really reusable? I suspect not.
*)
let string_contains s substr:bool= (not ((Lem.option_equal Nat_big_num.equal (Ml_bindings.find_substring substr s) None)))
(** [string_of_nat m] produces a string representation of natural number [m]. *)
(** [string_suffix i s] returns all but the first [i] characters of [s].
* Fails if the index is negative, or beyond the end of the string.
*)
let length xs:Nat_big_num.num= (Nat_big_num.of_int (List.length xs))
let rec takeRevAcc m xs rev_acc:'a list=
((match xs with
| [] -> List.rev rev_acc
| x::xs ->
if Nat_big_num.equal m( (Nat_big_num.of_int 0)) then
List.rev rev_acc
else
takeRevAcc ( Nat_big_num.sub_nat m( (Nat_big_num.of_int 1))) xs (x::rev_acc)
))
(** [take cnt xs] takes the first [cnt] elements of list [xs]. Returns a truncation
* if [cnt] is greater than the length of [xs].
*)
let rec take0 m xs:'a list=
(takeRevAcc m xs [])
(** [drop cnt xs] returns all but the first [cnt] elements of list [xs]. Returns an empty list
* if [cnt] is greater than the length of [xs].
*)
let rec drop0 m xs:'a list=
((match xs with
| [] -> []
| x::xs ->
if Nat_big_num.equal m( (Nat_big_num.of_int 0)) then
x::xs
else
drop0 ( Nat_big_num.sub_nat m( (Nat_big_num.of_int 1))) xs
))
(** [string_prefix i s] returns the first [i] characters of [s].
* Fails if the index is negative, or beyond the end of the string.
*)
(** [string_index_of c s] returns [Just(i)] where [i] is the index of the first
* occurrence if [c] in [s], if it exists, otherwise returns [Nothing]. *)
let rec string_index_of' e ss idx1:(Nat_big_num.num)option=
((match ss with
| [] -> None
| s::ss ->
if s = e then
Some idx1
else
string_index_of' e ss ( Nat_big_num.add( (Nat_big_num.of_int 1)) idx1)
))
let rec find_index_helper count p xs:(Nat_big_num.num)option=
((match xs with
| [] -> None
| y::ys ->
if p y then
Some count
else
find_index_helper ( Nat_big_num.add count( (Nat_big_num.of_int 1))) p ys
))
let find_index0 p xs:(Nat_big_num.num)option= (find_index_helper( (Nat_big_num.of_int 0)) p xs)
let rec replicate_revacc revacc len e:'a list=
(
if(Nat_big_num.equal len ( (Nat_big_num.of_int 0))) then (List.rev revacc)
else
(replicate_revacc (e :: revacc)
( Nat_big_num.sub_nat len ( (Nat_big_num.of_int 1))) e))
let rec replicate0 len e:'a list=
(replicate_revacc [] len e)
let list_append l1 l2:'a list=
(List.rev_append (List.rev l1) l2)
let list_concat ll:'a list= (List.fold_left list_append [] ll)
let list_concat_map f l:'b list=
(list_concat (Lem_list.map f l))
let rec list_reverse_concat_map_helper f acc ll:'b list=
(let lcons = (fun l -> (fun i -> i :: l))
in
(match ll with
| [] -> acc
| item :: items ->
let mapped_list = (f item)
in
list_reverse_concat_map_helper f (List.fold_left lcons acc (f item)) items
))
let list_reverse_concat_map f ll:'b list= (list_reverse_concat_map_helper f [] ll)
let rec list_take_with_accum n reverse_acc l:'a list=
(
(match n with
0 -> List.rev reverse_acc
| _ -> (match l with
[] -> failwith "list_take_with_accum: not enough elements"
| x :: xs -> list_take_with_accum (Nat_num.nat_monus n 1) (x :: reverse_acc) xs
)
))
let unsafe_string_take m str:string=
(let m = (Nat_big_num.to_int m) in
Xstring.implode (Lem_list.take m (Xstring.explode str)))
(** [padding_and_maybe_newline c w s] creates enough of char [c] to pad string [s] to [w] characters,
* unless [s] is of length [w - 1] or greater, in which case it generates [w] copies preceded by a newline.
* This style of formatting is used by the GNU linker in its link map output, so we
* reproduce it using this function. Note that string [s] does not appear in the
* output. *)
let padding_and_maybe_newline c width str:string=
(let padlen = (Nat_big_num.sub_nat width (Nat_big_num.of_int (String.length str))) in
(if Nat_big_num.less_equal padlen( (Nat_big_num.of_int 1)) then "\n" else "")
^ (Xstring.implode (replicate0 (if Nat_big_num.less_equal padlen( (Nat_big_num.of_int 1)) then width else padlen) c)))
(** [space_padding_and_maybe_newline w s] creates enoughspaces to pad string [s] to [w] characters,
* unless [s] is of length [w - 1] or greater, in which case it generates [w] copies preceded by a newline.
* This style of formatting is used by the GNU linker in its link map output, so we
* reproduce it using this function. Note that string [s] does not appear in the
* output. *)
let space_padding_and_maybe_newline width str:string=
(padding_and_maybe_newline ' ' width str)
(** [padded_and_maybe_newline w s] pads string [s] to [w] characters, using char [c]
* unless [s] is of length [w - 1] or greater, in which case the padding consists of
* [w] copies of [c] preceded by a newline.
* This style of formatting is used by the GNU linker in its link map output, so we
* reproduce it using this function. *)
let padded_and_maybe_newline c width str:string=
(str ^ (padding_and_maybe_newline c width str))
(** [padding_to c w s] creates enough copies of [c] to pad string [s] to [w] characters,
* or 0 characters if [s] is of length [w] or greater. Note that string [s] does not appear in the
* output. *)
let padding_to c width str:string=
(let padlen = (Nat_big_num.sub_nat width (Nat_big_num.of_int (String.length str))) in
if Nat_big_num.less_equal padlen( (Nat_big_num.of_int 0)) then "" else (Xstring.implode (replicate0 padlen c)))
(** [left_padded_to c w s] left-pads string [s] to [w] characters using [c],
* returning it unchanged if [s] is of length [w] or greater. *)
let left_padded_to c width str:string=
((padding_to c width str) ^ str)
(** [right_padded_to c w s] right-pads string [s] to [w] characters using [c],
* returning it unchanged if [s] is of length [w] or greater. *)
let right_padded_to c width str:string=
(str ^ (padding_to c width str))
(** [space_padded_and_maybe_newline w s] pads string [s] to [w] characters, using spaces,
* unless [s] is of length [w - 1] or greater, in which case the padding consists of
* [w] spaces preceded by a newline.
* This style of formatting is used by the GNU linker in its link map output, so we
* reproduce it using this function. *)
let space_padded_and_maybe_newline width str:string=
(str ^ (padding_and_maybe_newline ' ' width str))
(** [left_space_padded_to w s] left-pads string [s] to [w] characters using spaces,
* returning it unchanged if [s] is of length [w] or greater. *)
let left_space_padded_to width str:string=
((padding_to ' ' width str) ^ str)
(** [right_space_padded_to w s] right-pads string [s] to [w] characters using spaces,
* returning it unchanged if [s] is of length [w] or greater. *)
let right_space_padded_to width str:string=
(str ^ (padding_to ' ' width str))
(** [left_zero_padded_to w s] left-pads string [s] to [w] characters using zeroes,
* returning it unchanged if [s] is of length [w] or greater. *)
let left_zero_padded_to width str:string=
((padding_to '0' width str) ^ str)
(** hex parsing *)
let natural_of_char c:Nat_big_num.num=
(let naturalOrd c'= (Nat_big_num.of_int (Char.code c')) in
let n = (naturalOrd c) in
if Nat_big_num.greater_equal n (naturalOrd '0') && Nat_big_num.less_equal n (naturalOrd '9') then Nat_big_num.sub_nat n (naturalOrd '0')
else if Nat_big_num.greater_equal n (naturalOrd 'A') && Nat_big_num.less_equal n (naturalOrd 'F') then Nat_big_num.add (Nat_big_num.sub_nat n (naturalOrd 'A'))( (Nat_big_num.of_int 10))
else if Nat_big_num.greater_equal n (naturalOrd 'a') && Nat_big_num.less_equal n (naturalOrd 'f') then Nat_big_num.add (Nat_big_num.sub_nat n (naturalOrd 'a'))( (Nat_big_num.of_int 10))
else failwith ("natural_of_char argument #'" ^ (Xstring.implode [c] ^ "' not in 0-9,A-F,a-f")))
let rec natural_of_hex' cs:Nat_big_num.num=
((match cs with
| c :: cs' -> Nat_big_num.add (natural_of_char c) (Nat_big_num.mul( (Nat_big_num.of_int 16)) (natural_of_hex' cs'))
| [] -> (Nat_big_num.of_int 0)
))
let natural_of_hex s:Nat_big_num.num=
(let cs = (Xstring.explode s) in
(match cs with
| '0'::'x'::cs' ->
(match cs' with
| c :: _ -> natural_of_hex' (List.rev cs')
| [] -> failwith ("natural_of_hex argument \"" ^ (s ^ "\" has no digits"))
)
| _ -> failwith ("natural_of_hex argument \"" ^ (s ^ "\" does not begin 0x"))
))
let assert_unwrap_maybe maybe1:'a=
((match maybe1 with
| Some v -> v
| None -> failwith "assert_unwrap_maybe: nothing"
))