package tiny_json

  1. Overview
  2. Docs

Source file base64.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
type nat =
  | O
  | S of nat

type ('a, 'b) sum =
  | Inl of 'a
  | Inr of 'b

type ('a, 'b) prod =
  | Pair of 'a * 'b

(** val fst : ('a1, 'a2) prod -> 'a1 **)

let fst = function
  | Pair (x, y) -> x

(** val snd : ('a1, 'a2) prod -> 'a2 **)

let snd = function
  | Pair (x, y) -> y

type 'a sig0 = 'a
  (* singleton inductive, whose constructor was exist *)

(** val plus : nat -> nat -> nat **)

let rec plus n m =
  match n with
    | O -> m
    | S p -> S (plus p m)

(** val mult : nat -> nat -> nat **)

let rec mult n m =
  match n with
    | O -> O
    | S p -> plus m (mult p m)

(** val minus : nat -> nat -> nat **)

let rec minus n m =
  match n with
    | O -> n
    | S k -> (match m with
                | O -> n
                | S l -> minus k l)

(** val bool_dec : bool -> bool -> bool **)

let bool_dec b1 b2 =
  if b1 then if b2 then true else false else if b2 then false else true

(** val length : 'a1 list -> nat **)

let rec length = function
  | [] -> O
  | a :: m -> S (length m)

(** val app : 'a1 list -> 'a1 list -> 'a1 list **)

let rec app l m =
  match l with
    | [] -> m
    | a :: l1 -> a :: (app l1 m)

(** val map : ('a1 -> 'a2) -> 'a1 list -> 'a2 list **)

let rec map f = function
  | [] -> []
  | a :: t -> (f a) :: (map f t)

type positive =
  | XI of positive
  | XO of positive
  | XH

(** val psucc : positive -> positive **)

let rec psucc = function
  | XI p -> XO (psucc p)
  | XO p -> XI p
  | XH -> XO XH

(** val p_of_succ_nat : nat -> positive **)

let rec p_of_succ_nat = function
  | O -> XH
  | S x -> psucc (p_of_succ_nat x)

type ascii =
  | Ascii of bool * bool * bool * bool * bool * bool * bool * bool

(** val zero : ascii **)

let zero =
  Ascii (false, false, false, false, false, false, false, false)

(** val one : ascii **)

let one =
  Ascii (true, false, false, false, false, false, false, false)

(** val app2 : (bool -> bool -> bool) -> ascii -> ascii -> ascii **)

let app2 f a b =
  let Ascii (a1, a2, a3, a4, a5, a6, a7, a8) = a in
  let Ascii (b1, b2, b3, b4, b5, b6, b7, b8) = b in
  Ascii ((f a1 b1), (f a2 b2), (f a3 b3), (f a4 b4), 
  (f a5 b5), (f a6 b6), (f a7 b7), (f a8 b8))

(** val shift : bool -> ascii -> ascii **)

let shift c = function
  | Ascii (a1, a2, a3, a4, a5, a6, a7, a8) -> Ascii (c, a1, a2, a3, a4, a5,
      a6, a7)

(** val ascii_dec : ascii -> ascii -> bool **)

let ascii_dec a b =
  let Ascii (x, x0, x1, x2, x3, x4, x5, x6) = a in
  let Ascii (b8, b9, b10, b11, b12, b13, b14, b15) = b in
  if bool_dec x b8
  then if bool_dec x0 b9
       then if bool_dec x1 b10
            then if bool_dec x2 b11
                 then if bool_dec x3 b12
                      then if bool_dec x4 b13
                           then if bool_dec x5 b14
                                then bool_dec x6 b15
                                else false
                           else false
                      else false
                 else false
            else false
       else false
  else false

(** val ascii_of_pos_aux : ascii -> ascii -> positive -> nat -> ascii **)

let rec ascii_of_pos_aux res acc z = function
  | O -> res
  | S n1 ->
      (match z with
         | XI z' ->
             ascii_of_pos_aux
               (app2 (fun b1 b2 -> if b1 then true else b2) res acc)
               (shift false acc) z' n1
         | XO z' -> ascii_of_pos_aux res (shift false acc) z' n1
         | XH -> app2 (fun b1 b2 -> if b1 then true else b2) res acc)

(** val ascii_of_pos : positive -> ascii **)

let ascii_of_pos a =
  ascii_of_pos_aux zero one a (S (S (S (S (S (S (S (S O))))))))

(** val ascii_of_nat : nat -> ascii **)

let ascii_of_nat = function
  | O -> zero
  | S a' -> ascii_of_pos (p_of_succ_nat a')

(** val nat_of_ascii : ascii -> nat **)

let nat_of_ascii = function
  | Ascii (a1, a2, a3, a4, a5, a6, a7, a8) ->
      plus
        (mult (S (S O))
          (plus
            (mult (S (S O))
              (plus
                (mult (S (S O))
                  (plus
                    (mult (S (S O))
                      (plus
                        (mult (S (S O))
                          (plus
                            (mult (S (S O))
                              (plus (mult (S (S O)) (if a8 then S O else O))
                                (if a7 then S O else O)))
                            (if a6 then S O else O)))
                        (if a5 then S O else O))) (
                    if a4 then S O else O))) (if a3 then S O else O)))
            (if a2 then S O else O))) (if a1 then S O else O)

(** val eq_nat_dec : nat -> nat -> bool **)

let rec eq_nat_dec n m =
  match n with
    | O -> (match m with
              | O -> true
              | S m0 -> false)
    | S n0 -> (match m with
                 | O -> false
                 | S m0 -> eq_nat_dec n0 m0)

(** val le_lt_dec : nat -> nat -> bool **)

let rec le_lt_dec n m =
  match n with
    | O -> true
    | S n0 -> (match m with
                 | O -> false
                 | S m0 -> le_lt_dec n0 m0)

(** val replicate : nat -> 'a1 -> 'a1 list **)

let rec replicate n x =
  match n with
    | O -> []
    | S m -> x :: (replicate m x)

(** val delete : ('a1 -> 'a1 -> bool) -> 'a1 -> 'a1 list -> 'a1 list **)

let rec delete eq_dec a = function
  | [] -> []
  | x :: xs0 ->
      if eq_dec x a then delete eq_dec a xs0 else x :: (delete eq_dec a xs0)

type 'a vec =
  | VNil
  | VCons of nat * 'a * 'a vec

(** val vec_of_list : nat -> 'a1 list -> 'a1 vec **)

let rec vec_of_list n = function
  | [] -> VNil
  | x :: xs0 -> VCons ((length xs0), x, (vec_of_list (length xs0) xs0))

(** val list_of_vec : nat -> 'a1 vec -> 'a1 list **)

let rec list_of_vec n = function
  | VNil -> []
  | VCons (n0, x, xs) -> x :: (list_of_vec n0 xs)

(** val split_dec :
    nat -> 'a1 list -> (('a1 vec, 'a1 list) prod, 'a1 list) sum **)

let rec split_dec n xs =
  match n with
    | O -> Inl (Pair (VNil, xs))
    | S m ->
        (match xs with
           | [] -> Inr xs
           | x :: xs0 ->
               (match split_dec m xs0 with
                  | Inl p ->
                      let Pair (xs1, xs2) = p in
                      Inl (Pair ((VCons (m, x, xs1)), xs2))
                  | Inr s -> Inr (x :: s)))

(** val grp_aux_terminate :
    nat -> 'a1 list -> ('a1 vec list, 'a1 list) prod **)

let rec grp_aux_terminate n = function
  | [] -> Pair ([], [])
  | x :: xs0 ->
      (match split_dec n (x :: xs0) with
         | Inl p ->
             let Pair (v, tail) = p in
             let Pair (vs, rest) = grp_aux_terminate n tail in
             Pair ((v :: (Obj.magic vs)), rest)
         | Inr r -> Pair ([], r))

(** val grp_aux : nat -> 'a1 list -> ('a1 vec list, 'a1 list) prod **)

let grp_aux x x0 =
  grp_aux_terminate x x0

(** val g1 : nat -> 'a1 list -> 'a1 vec list **)

let g1 n xs =
  fst (grp_aux n xs)

(** val g2 : nat -> 'a1 list -> 'a1 list **)

let g2 n xs =
  snd (grp_aux n xs)

(** val gcut : nat -> 'a1 list -> 'a1 vec list **)

let gcut n xs =
  g1 n xs

(** val gfill : 'a1 -> nat -> 'a1 list -> 'a1 vec list **)

let gfill default n xs =
  let ys = g2 n xs in
  app (g1 n xs)
    ((vec_of_list n (app ys (replicate (minus n (length ys)) default))) ::
    [])

(** val cm : nat -> 'a1 vec list -> 'a1 list **)

let rec cm n = function
  | [] -> []
  | v :: vs0 -> app (list_of_vec n v) (cm n vs0)

type mlchar = char

type mlint = int

type mlstring = string

(** val mlint_of_nat : nat -> mlint **)

let mlint_of_nat = let rec iter = function O -> 0 | S p -> succ (iter p) in iter

(** val nat_of_mlint : mlint -> nat **)

let nat_of_mlint = let rec iter = function 0 -> O | n -> S (iter (pred n)) in iter

(** val mlchar_of_mlint : mlint -> mlchar **)

let mlchar_of_mlint = char_of_int

(** val mlint_of_mlchar : mlchar -> mlint **)

let mlint_of_mlchar = int_of_char

(** val ascii_of_mlchar : mlchar -> ascii **)

let ascii_of_mlchar x =
  ascii_of_nat (nat_of_mlint (mlint_of_mlchar x))

(** val mlchar_of_ascii : ascii -> mlchar **)

let mlchar_of_ascii x =
  mlchar_of_mlint (mlint_of_nat (nat_of_ascii x))

(** val mlstring_of_list : ('a1 -> mlchar) -> 'a1 list -> mlstring **)

let mlstring_of_list = (fun f s -> String.concat ""
     (List.map (fun x -> String.make 1 (f x)) s))

(** val list_of_mlstring : (mlchar -> 'a1) -> mlstring -> 'a1 list **)

let list_of_mlstring = 
(fun f s ->
  let rec explode_rec n =
    if n >= String.length s then
      []
    else 
      f (String.get s n) :: explode_rec (succ n)
  in 
  explode_rec 0)


(** val mlstring_of_asciilist : ascii list -> mlstring **)

let mlstring_of_asciilist x =
  mlstring_of_list mlchar_of_ascii x

(** val asciilist_of_mlstring : mlstring -> ascii list **)

let asciilist_of_mlstring x =
  list_of_mlstring ascii_of_mlchar x

(** val ascii_of_bool6 : bool vec -> ascii **)

let ascii_of_bool6 = function
  | VNil -> assert false (* absurd case *)
  | VCons (n, h, h0) ->
      (match h0 with
         | VNil -> assert false (* absurd case *)
         | VCons (n0, h2, h3) ->
             (match h3 with
                | VNil -> assert false (* absurd case *)
                | VCons (n1, h5, h6) ->
                    (match h6 with
                       | VNil -> assert false (* absurd case *)
                       | VCons (n2, h8, h9) ->
                           (match h9 with
                              | VNil -> assert false (* absurd case *)
                              | VCons (n3, h11, h12) ->
                                  (match h12 with
                                     | VNil -> assert false (* absurd case *)
                                     | VCons (n4, h14, h15) -> Ascii (h14,
                                         h11, h8, h5, h2, h, false, false))))))

(** val bool6_of_ascii : ascii -> bool vec **)

let bool6_of_ascii = function
  | Ascii (a, b, c0, d, e, f, b0, b1) ->
      vec_of_list (S (S (S (S (S (S O)))))) (f :: (e :: (d :: (c0 :: (b :: (a
        :: []))))))

(** val ascii_of_bool8 : bool vec -> ascii **)

let ascii_of_bool8 = function
  | VNil -> assert false (* absurd case *)
  | VCons (n, h, h0) ->
      (match h0 with
         | VNil -> assert false (* absurd case *)
         | VCons (n0, h2, h3) ->
             (match h3 with
                | VNil -> assert false (* absurd case *)
                | VCons (n1, h5, h6) ->
                    (match h6 with
                       | VNil -> assert false (* absurd case *)
                       | VCons (n2, h8, h9) ->
                           (match h9 with
                              | VNil -> assert false (* absurd case *)
                              | VCons (n3, h11, h12) ->
                                  (match h12 with
                                     | VNil -> assert false (* absurd case *)
                                     | VCons (n4, h14, h15) ->
                                         (match h15 with
                                            | VNil -> assert false
                                                (* absurd case *)
                                            | VCons (
                                                n5, h17, h18) ->
                                                (match h18 with
                                                   | 
                                                 VNil -> assert false
                                                  (* absurd case *)
                                                   | 
                                                 VCons (
                                                  n6, h20, h21) -> Ascii
                                                  (h20, h17, h14, h11, h8,
                                                  h5, h2, h))))))))

(** val le_lt_dec_aux : nat -> nat -> bool **)

let le_lt_dec_aux x y =
  le_lt_dec x y

(** val tblaux : nat -> nat **)

let tblaux n =
  if le_lt_dec_aux n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
       (S (S (S (S (S (S O)))))))))))))))))))))))))
  then plus
         (nat_of_ascii (Ascii (true, false, false, false, false, false, true,
           false))) n
  else if le_lt_dec_aux n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
            (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
            (S (S (S (S (S (S (S (S (S (S (S (S
            O)))))))))))))))))))))))))))))))))))))))))))))))))))
       then plus
              (nat_of_ascii (Ascii (true, false, false, false, false, true,
                true, false)))
              (minus n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                (S (S (S (S (S (S (S (S O)))))))))))))))))))))))))))
       else if le_lt_dec_aux n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                 (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                 (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                 (S (S (S (S (S (S
                 O)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
            then plus
                   (nat_of_ascii (Ascii (false, false, false, false, true,
                     true, false, false)))
                   (minus n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     O)))))))))))))))))))))))))))))))))))))))))))))))))))))
            else if eq_nat_dec n (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                      (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                      (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                      (S (S (S (S (S (S (S (S (S (S (S
                      O))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
                 then nat_of_ascii (Ascii (true, true, false, true, false,
                        true, false, false))
                 else nat_of_ascii (Ascii (true, true, true, true, false,
                        true, false, false))

(** val tbl : bool vec -> ascii **)

let tbl bs =
  ascii_of_nat (tblaux (nat_of_ascii (ascii_of_bool6 bs)))

(** val tblaux_inv : nat -> nat **)

let tblaux_inv n =
  if le_lt_dec_aux (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
       (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
       (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
       (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
       (S (S (S (S (S (S (S (S (S
       O)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
       n
  then minus
         (plus (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
           (S (S (S (S (S O)))))))))))))))))))))))))) n)
         (nat_of_ascii (Ascii (true, false, false, false, false, true, true,
           false)))
  else if le_lt_dec_aux (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
            (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
            (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
            (S (S (S
            O)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
            n
       then minus n
              (nat_of_ascii (Ascii (true, false, false, false, false, false,
                true, false)))
       else if le_lt_dec_aux (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                 (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                 (S (S (S (S (S (S (S (S (S (S (S (S
                 O)))))))))))))))))))))))))))))))))))))))))))))))) n
            then minus
                   (plus (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                     O)))))))))))))))))))))))))))))))))))))))))))))))))))) n)
                   (nat_of_ascii (Ascii (false, false, false, false, true,
                     true, false, false)))
            else if eq_nat_dec n
                      (nat_of_ascii (Ascii (true, true, false, true, false,
                        true, false, false)))
                 then S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S
                        O)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
                 else S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S
                        (S (S (S (S (S (S (S (S
                        O))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

(** val tbl_inv : ascii -> bool vec **)

let tbl_inv c =
  bool6_of_ascii (ascii_of_nat (tblaux_inv (nat_of_ascii c)))

(** val digit : ascii -> bool vec **)

let digit = function
  | Ascii (a, b, c, d, e, f, g, h) ->
      vec_of_list (S (S (S (S (S (S (S (S O)))))))) (h :: (g :: (f :: (e ::
        (d :: (c :: (b :: (a :: []))))))))

(** val encode : mlstring -> mlstring **)

let encode s =
  mlstring_of_asciilist
    (cm (S (S (S (S O))))
      (gfill (Ascii (true, false, true, true, true, true, false, false)) (S
        (S (S (S O))))
        (map tbl
          (gfill false (S (S (S (S (S (S O))))))
            (cm (S (S (S (S (S (S (S (S O))))))))
              (map digit (asciilist_of_mlstring s)))))))

(** val decode : mlstring -> mlstring **)

let decode s =
  mlstring_of_asciilist
    (map ascii_of_bool8
      (gcut (S (S (S (S (S (S (S (S O))))))))
        (cm (S (S (S (S (S (S O))))))
          (map tbl_inv
            (delete ascii_dec (Ascii (true, false, true, true, true, true,
              false, false)) (asciilist_of_mlstring s))))))