Source file kTSizedChain.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016

type __ = Obj.t

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

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

type ('a, 'p) sigT =
| ExistT of 'a * 'p

(** val projT1 : ('a1, 'a2) sigT -> 'a1 **)

let projT1 = function
| ExistT (a, _) -> a

(** val projT2 : ('a1, 'a2) sigT -> 'a2 **)

let projT2 = function
| ExistT (_, h) -> h



module Nat =
 struct
  (** val pred : int -> int **)

  let pred n =
    (fun fO fS n -> if n=0 then fO () else fS (n-1))
      (fun _ -> n)
      (fun u -> u)
      n
 end

type 'a xpow = __

(** val xflat : int -> 'a1 xpow -> 'a1 list **)

let rec xflat l a =
  (fun fO fS n -> if n=0 then fO () else fS (n-1))
    (fun _ -> (Obj.magic a) :: [])
    (fun l' -> let (x, y) = Obj.magic a in app (xflat l' x) (xflat l' y))
    l

module ElementTree =
 struct
  type 'a t = (int, 'a xpow) sigT

  (** val to_list : 'a1 t -> 'a1 list **)

  let to_list e =
    xflat (projT1 e) (projT2 e)

  (** val level : 'a1 t -> int **)

  let level =
    projT1

  (** val base : 'a1 -> 'a1 t **)

  let base a =
    ExistT (0, (Obj.magic a))

  (** val pair : 'a1 t -> 'a1 t -> 'a1 t **)

  let pair x y =
    ExistT ((Stdlib.Int.succ (level x)),
      (let ExistT (_, x0) = x in let ExistT (_, x1) = y in Obj.magic (x0, x1)))

  (** val unpair : 'a1 t -> ('a1 t * 'a1 t) option **)

  let unpair = function
  | ExistT (x, p) ->
    ((fun fO fS n -> if n=0 then fO () else fS (n-1))
       (fun _ -> None)
       (fun l' ->
       let (x0, y) = Obj.magic p in Some ((ExistT (l', x0)), (ExistT (l', y))))
       x)
 end

type 'x buf5 =
| B0
| B1 of 'x
| B2 of 'x * 'x
| B3 of 'x * 'x * 'x
| B4 of 'x * 'x * 'x * 'x
| B5 of 'x * 'x * 'x * 'x * 'x

(** val buf5_seq : ('a1 -> 'a2 list) -> 'a1 buf5 -> 'a2 list **)

let buf5_seq flat = function
| B0 -> []
| B1 a -> flat a
| B2 (a, b0) -> app (flat a) (flat b0)
| B3 (a, b0, c) -> app (flat a) (app (flat b0) (flat c))
| B4 (a, b0, c, d) -> app (flat a) (app (flat b0) (app (flat c) (flat d)))
| B5 (a, b0, c, d, e) ->
  app (flat a) (app (flat b0) (app (flat c) (app (flat d) (flat e))))

(** val buf5_push_naive : 'a1 -> 'a1 buf5 -> 'a1 buf5 option **)

let buf5_push_naive x = function
| B0 -> Some (B1 x)
| B1 a -> Some (B2 (x, a))
| B2 (a, b0) -> Some (B3 (x, a, b0))
| B3 (a, b0, c) -> Some (B4 (x, a, b0, c))
| B4 (a, b0, c, d) -> Some (B5 (x, a, b0, c, d))
| B5 (_, _, _, _, _) -> None

(** val buf5_inject_naive : 'a1 buf5 -> 'a1 -> 'a1 buf5 option **)

let buf5_inject_naive b x =
  match b with
  | B0 -> Some (B1 x)
  | B1 a -> Some (B2 (a, x))
  | B2 (a, b0) -> Some (B3 (a, b0, x))
  | B3 (a, b0, c) -> Some (B4 (a, b0, c, x))
  | B4 (a, b0, c, d) -> Some (B5 (a, b0, c, d, x))
  | B5 (_, _, _, _, _) -> None

(** val buf5_pop_naive : 'a1 buf5 -> ('a1 * 'a1 buf5) option **)

let buf5_pop_naive = function
| B0 -> None
| B1 a -> Some (a, B0)
| B2 (a, b0) -> Some (a, (B1 b0))
| B3 (a, b0, c) -> Some (a, (B2 (b0, c)))
| B4 (a, b0, c, d) -> Some (a, (B3 (b0, c, d)))
| B5 (a, b0, c, d, e) -> Some (a, (B4 (b0, c, d, e)))

(** val buf5_eject_naive : 'a1 buf5 -> ('a1 buf5 * 'a1) option **)

let buf5_eject_naive = function
| B0 -> None
| B1 a -> Some (B0, a)
| B2 (a, b0) -> Some ((B1 a), b0)
| B3 (a, b0, c) -> Some ((B2 (a, b0)), c)
| B4 (a, b0, c, d) -> Some ((B3 (a, b0, c)), d)
| B5 (a, b0, c, d, e) -> Some ((B4 (a, b0, c, d)), e)

type color =
| Green
| Yellow
| Red

module E = ElementTree

type 'a packet =
| Hole
| PNode of 'a E.t buf5 * 'a packet * 'a E.t buf5

type 'a chain =
| Ending of 'a E.t buf5
| ChainCons of 'a packet * 'a chain

(** val buf_seq_E : 'a1 E.t buf5 -> 'a1 list **)

let buf_seq_E b =
  buf5_seq E.to_list b

(** val packet_seq : 'a1 packet -> 'a1 list -> 'a1 list **)

let rec packet_seq p inner =
  match p with
  | Hole -> inner
  | PNode (pre, i, suf) ->
    app (buf_seq_E pre) (app (packet_seq i inner) (buf_seq_E suf))

(** val chain_seq : 'a1 chain -> 'a1 list **)

let rec chain_seq = function
| Ending b -> buf_seq_E b
| ChainCons (p, c') -> packet_seq p (chain_seq c')

(** val chain_to_list : 'a1 chain -> 'a1 list **)

let chain_to_list =
  chain_seq

module Coq_E = E

(** val green_push : 'a1 -> 'a1 buf5 -> 'a1 buf5 option **)

let green_push x = function
| B2 (a, b1) -> Some (B3 (x, a, b1))
| B3 (a, b1, c) -> Some (B4 (x, a, b1, c))
| _ -> None

(** val green_inject : 'a1 buf5 -> 'a1 -> 'a1 buf5 option **)

let green_inject b x =
  match b with
  | B2 (a, b1) -> Some (B3 (a, b1, x))
  | B3 (a, b1, c) -> Some (B4 (a, b1, c, x))
  | _ -> None

(** val yellow_push : 'a1 -> 'a1 buf5 -> 'a1 buf5 option **)

let yellow_push x = function
| B1 a -> Some (B2 (x, a))
| B2 (a, b1) -> Some (B3 (x, a, b1))
| B3 (a, b1, c) -> Some (B4 (x, a, b1, c))
| B4 (a, b1, c, d) -> Some (B5 (x, a, b1, c, d))
| _ -> None

(** val yellow_inject : 'a1 buf5 -> 'a1 -> 'a1 buf5 option **)

let yellow_inject b x =
  match b with
  | B1 a -> Some (B2 (a, x))
  | B2 (a, b1) -> Some (B3 (a, b1, x))
  | B3 (a, b1, c) -> Some (B4 (a, b1, c, x))
  | B4 (a, b1, c, d) -> Some (B5 (a, b1, c, d, x))
  | _ -> None

(** val green_pop : 'a1 buf5 -> ('a1 * 'a1 buf5) option **)

let green_pop = function
| B2 (a, b1) -> Some (a, (B1 b1))
| B3 (a, b1, c) -> Some (a, (B2 (b1, c)))
| _ -> None

(** val green_eject : 'a1 buf5 -> ('a1 buf5 * 'a1) option **)

let green_eject = function
| B2 (a, b1) -> Some ((B1 a), b1)
| B3 (a, b1, c) -> Some ((B2 (a, b1)), c)
| _ -> None

(** val yellow_pop : 'a1 buf5 -> ('a1 * 'a1 buf5) option **)

let yellow_pop = function
| B1 a -> Some (a, B0)
| B2 (a, b1) -> Some (a, (B1 b1))
| B3 (a, b1, c) -> Some (a, (B2 (b1, c)))
| B4 (a, b1, c, d) -> Some (a, (B3 (b1, c, d)))
| _ -> None

(** val yellow_eject : 'a1 buf5 -> ('a1 buf5 * 'a1) option **)

let yellow_eject = function
| B1 a -> Some (B0, a)
| B2 (a, b1) -> Some ((B1 a), b1)
| B3 (a, b1, c) -> Some ((B2 (a, b1)), c)
| B4 (a, b1, c, d) -> Some ((B3 (a, b1, c)), d)
| _ -> None

(** val prefix23 : 'a1 option -> ('a1 * 'a1) -> 'a1 buf5 **)

let prefix23 opt = function
| (b, c) -> (match opt with
             | Some a -> B3 (a, b, c)
             | None -> B2 (b, c))

(** val suffix23 : ('a1 * 'a1) -> 'a1 option -> 'a1 buf5 **)

let suffix23 p opt =
  let (a, b) = p in
  (match opt with
   | Some c -> B3 (a, b, c)
   | None -> B2 (a, b))

(** val suffix12 : 'a1 -> 'a1 option -> 'a1 buf5 **)

let suffix12 x = function
| Some y -> B2 (x, y)
| None -> B1 x

type 'x bdecomp_pre =
| BD_pre_underflow of 'x option
| BD_pre_ok of 'x buf5
| BD_pre_overflow of 'x buf5 * 'x * 'x

type 'x bdecomp_suf =
| BD_suf_underflow of 'x option
| BD_suf_ok of 'x buf5
| BD_suf_overflow of 'x buf5 * 'x * 'x

(** val prefix_decompose : 'a1 buf5 -> 'a1 bdecomp_pre **)

let prefix_decompose b = match b with
| B0 -> BD_pre_underflow None
| B1 x -> BD_pre_underflow (Some x)
| B4 (a, b1, c, d) -> BD_pre_overflow ((B2 (a, b1)), c, d)
| B5 (a, b1, c, d, e) -> BD_pre_overflow ((B3 (a, b1, c)), d, e)
| _ -> BD_pre_ok b

(** val suffix_decompose : 'a1 buf5 -> 'a1 bdecomp_suf **)

let suffix_decompose b = match b with
| B0 -> BD_suf_underflow None
| B1 x -> BD_suf_underflow (Some x)
| B4 (a, b1, c, d) -> BD_suf_overflow ((B2 (c, d)), a, b1)
| B5 (a, b1, c, d, e) -> BD_suf_overflow ((B3 (c, d, e)), a, b1)
| _ -> BD_suf_ok b

type 'x bsandwich =
| BS_alone of 'x option
| BS_sandwich of 'x * 'x buf5 * 'x

(** val buffer_unsandwich : 'a1 buf5 -> 'a1 bsandwich **)

let buffer_unsandwich = function
| B0 -> BS_alone None
| B1 a -> BS_alone (Some a)
| B2 (a, b1) -> BS_sandwich (a, B0, b1)
| B3 (a, b1, c) -> BS_sandwich (a, (B1 b1), c)
| B4 (a, b1, c, d) -> BS_sandwich (a, (B2 (b1, c)), d)
| B5 (a, b1, c, d, e) -> BS_sandwich (a, (B3 (b1, c, d)), e)

(** val prefix_rot : 'a1 -> 'a1 buf5 -> 'a1 buf5 * 'a1 **)

let prefix_rot x = function
| B0 -> (B0, x)
| B1 a -> ((B1 x), a)
| B2 (a, b1) -> ((B2 (x, a)), b1)
| B3 (a, b1, c) -> ((B3 (x, a, b1)), c)
| B4 (a, b1, c, d) -> ((B4 (x, a, b1, c)), d)
| B5 (a, b1, c, d, e) -> ((B5 (x, a, b1, c, d)), e)

(** val suffix_rot : 'a1 buf5 -> 'a1 -> 'a1 * 'a1 buf5 **)

let suffix_rot b x =
  match b with
  | B0 -> (x, B0)
  | B1 a -> (a, (B1 x))
  | B2 (a, b1) -> (a, (B2 (b1, x)))
  | B3 (a, b1, c) -> (a, (B3 (b1, c, x)))
  | B4 (a, b1, c, d) -> (a, (B4 (b1, c, d, x)))
  | B5 (a, b1, c, d, e) -> (a, (B5 (b1, c, d, e, x)))

(** val buffer_halve : 'a1 buf5 -> 'a1 option * ('a1 * 'a1) buf5 **)

let buffer_halve = function
| B0 -> (None, B0)
| B1 a -> ((Some a), B0)
| B2 (a, b1) -> (None, (B1 (a, b1)))
| B3 (a, b1, c) -> ((Some a), (B1 (b1, c)))
| B4 (a, b1, c, d) -> (None, (B2 ((a, b1), (c, d))))
| B5 (a, b1, c, d, e) -> ((Some a), (B2 ((b1, c), (d, e))))

(** val green_prefix_concat :
    'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> ('a1 Coq_E.t buf5 * 'a1 Coq_E.t
    buf5) option **)

let green_prefix_concat b1 b2 =
  match prefix_decompose b1 with
  | BD_pre_underflow opt ->
    (match green_pop b2 with
     | Some p ->
       let (ab, b2') = p in
       (match Coq_E.unpair ab with
        | Some p0 -> Some ((prefix23 opt p0), b2')
        | None -> None)
     | None -> None)
  | BD_pre_ok b1' -> Some (b1', b2)
  | BD_pre_overflow (b1', c, d) ->
    if (=) (Coq_E.level c) (Coq_E.level d)
    then (match green_push (Coq_E.pair c d) b2 with
          | Some b2' -> Some (b1', b2')
          | None -> None)
    else None

(** val green_suffix_concat :
    'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> ('a1 Coq_E.t buf5 * 'a1 Coq_E.t
    buf5) option **)

let green_suffix_concat b1 b2 =
  match suffix_decompose b2 with
  | BD_suf_underflow opt ->
    (match green_eject b1 with
     | Some p ->
       let (b1', ab) = p in
       (match Coq_E.unpair ab with
        | Some p0 -> Some (b1', (suffix23 p0 opt))
        | None -> None)
     | None -> None)
  | BD_suf_ok b2' -> Some (b1, b2')
  | BD_suf_overflow (b2', a, b) ->
    if (=) (Coq_E.level a) (Coq_E.level b)
    then (match green_inject b1 (Coq_E.pair a b) with
          | Some b1' -> Some (b1', b2')
          | None -> None)
    else None

(** val prefix_concat :
    'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> ('a1 Coq_E.t buf5 * 'a1 Coq_E.t
    buf5) option **)

let prefix_concat b1 b2 =
  match prefix_decompose b1 with
  | BD_pre_underflow opt ->
    (match yellow_pop b2 with
     | Some p ->
       let (ab, b2') = p in
       (match Coq_E.unpair ab with
        | Some p0 -> Some ((prefix23 opt p0), b2')
        | None -> None)
     | None -> None)
  | BD_pre_ok b1' -> Some (b1', b2)
  | BD_pre_overflow (b1', c, d) ->
    if (=) (Coq_E.level c) (Coq_E.level d)
    then (match yellow_push (Coq_E.pair c d) b2 with
          | Some b2' -> Some (b1', b2')
          | None -> None)
    else None

(** val suffix_concat :
    'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> ('a1 Coq_E.t buf5 * 'a1 Coq_E.t
    buf5) option **)

let suffix_concat b1 b2 =
  match suffix_decompose b2 with
  | BD_suf_underflow opt ->
    (match yellow_eject b1 with
     | Some p ->
       let (b1', ab) = p in
       (match Coq_E.unpair ab with
        | Some p0 -> Some (b1', (suffix23 p0 opt))
        | None -> None)
     | None -> None)
  | BD_suf_ok b2' -> Some (b1, b2')
  | BD_suf_overflow (b2', a, b) ->
    if (=) (Coq_E.level a) (Coq_E.level b)
    then (match yellow_inject b1 (Coq_E.pair a b) with
          | Some b1' -> Some (b1', b2')
          | None -> None)
    else None

(** val buffer_push_chain : 'a1 Coq_E.t -> 'a1 Coq_E.t buf5 -> 'a1 chain **)

let buffer_push_chain x = function
| B0 -> Ending (B1 x)
| B1 a -> Ending (B2 (x, a))
| B2 (a, b1) -> Ending (B3 (x, a, b1))
| B3 (a, b1, c) -> Ending (B4 (x, a, b1, c))
| B4 (a, b1, c, d) -> Ending (B5 (x, a, b1, c, d))
| B5 (a, b1, c, d, e) ->
  ChainCons ((PNode ((B3 (x, a, b1)), Hole, (B3 (c, d, e)))), (Ending B0))

(** val buffer_inject_chain : 'a1 Coq_E.t buf5 -> 'a1 Coq_E.t -> 'a1 chain **)

let buffer_inject_chain b x =
  match b with
  | B0 -> Ending (B1 x)
  | B1 a -> Ending (B2 (a, x))
  | B2 (a, b1) -> Ending (B3 (a, b1, x))
  | B3 (a, b1, c) -> Ending (B4 (a, b1, c, x))
  | B4 (a, b1, c, d) -> Ending (B5 (a, b1, c, d, x))
  | B5 (a, b1, c, d, e) ->
    ChainCons ((PNode ((B3 (a, b1, c)), Hole, (B3 (d, e, x)))), (Ending B0))

(** val pair_one : ('a1 Coq_E.t * 'a1 Coq_E.t) -> 'a1 Coq_E.t option **)

let pair_one = function
| (x, y) ->
  if (=) (Coq_E.level x) (Coq_E.level y) then Some (Coq_E.pair x y) else None

(** val pair_each_buf :
    ('a1 Coq_E.t * 'a1 Coq_E.t) buf5 -> 'a1 Coq_E.t buf5 option **)

let pair_each_buf = function
| B0 -> Some B0
| B1 p -> (match pair_one p with
           | Some xp -> Some (B1 xp)
           | None -> None)
| B2 (p1, p2) ->
  (match pair_one p1 with
   | Some x1 ->
     (match pair_one p2 with
      | Some x2 -> Some (B2 (x1, x2))
      | None -> None)
   | None -> None)
| B3 (p1, p2, p3) ->
  (match pair_one p1 with
   | Some x1 ->
     (match pair_one p2 with
      | Some x2 ->
        (match pair_one p3 with
         | Some x3 -> Some (B3 (x1, x2, x3))
         | None -> None)
      | None -> None)
   | None -> None)
| B4 (p1, p2, p3, p4) ->
  (match pair_one p1 with
   | Some x1 ->
     (match pair_one p2 with
      | Some x2 ->
        (match pair_one p3 with
         | Some x3 ->
           (match pair_one p4 with
            | Some x4 -> Some (B4 (x1, x2, x3, x4))
            | None -> None)
         | None -> None)
      | None -> None)
   | None -> None)
| B5 (p1, p2, p3, p4, p5) ->
  (match pair_one p1 with
   | Some x1 ->
     (match pair_one p2 with
      | Some x2 ->
        (match pair_one p3 with
         | Some x3 ->
           (match pair_one p4 with
            | Some x4 ->
              (match pair_one p5 with
               | Some x5 -> Some (B5 (x1, x2, x3, x4, x5))
               | None -> None)
            | None -> None)
         | None -> None)
      | None -> None)
   | None -> None)

(** val mk_ending_from_options :
    'a1 Coq_E.t option -> ('a1 Coq_E.t * 'a1 Coq_E.t) option -> 'a1 Coq_E.t
    option -> 'a1 chain **)

let mk_ending_from_options p1 mid s1 =
  match p1 with
  | Some a ->
    (match mid with
     | Some p ->
       let (b, c) = p in
       (match s1 with
        | Some d -> Ending (B4 (a, b, c, d))
        | None -> Ending (B3 (a, b, c)))
     | None ->
       (match s1 with
        | Some b -> Ending (B2 (a, b))
        | None -> Ending (B1 a)))
  | None ->
    (match mid with
     | Some p ->
       let (a, b) = p in
       (match s1 with
        | Some c -> Ending (B3 (a, b, c))
        | None -> Ending (B2 (a, b)))
     | None -> (match s1 with
                | Some a -> Ending (B1 a)
                | None -> Ending B0))

(** val make_small :
    'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> 'a1 Coq_E.t buf5 -> 'a1 chain
    option **)

let make_small b1 b2 b3 =
  match prefix_decompose b1 with
  | BD_pre_underflow p1opt ->
    (match suffix_decompose b3 with
     | BD_suf_underflow s1opt ->
       (match buffer_unsandwich b2 with
        | BS_alone midopt ->
          (match midopt with
           | Some elem ->
             (match Coq_E.unpair elem with
              | Some pair_elt ->
                Some (mk_ending_from_options p1opt (Some pair_elt) s1opt)
              | None -> None)
           | None -> Some (mk_ending_from_options p1opt None s1opt))
        | BS_sandwich (ab, rest, cd) ->
          (match Coq_E.unpair ab with
           | Some pair_ab ->
             (match Coq_E.unpair cd with
              | Some pair_cd ->
                Some (ChainCons ((PNode ((prefix23 p1opt pair_ab), Hole,
                  (suffix23 pair_cd s1opt))), (Ending rest)))
              | None -> None)
           | None -> None))
     | BD_suf_ok s1' ->
       (match buf5_pop_naive b2 with
        | Some p ->
          let (cd, rest) = p in
          (match Coq_E.unpair cd with
           | Some pair_cd ->
             Some (ChainCons ((PNode ((prefix23 p1opt pair_cd), Hole, s1')),
               (Ending rest)))
           | None -> None)
        | None ->
          (match p1opt with
           | Some x ->
             (match buf5_push_naive x s1' with
              | Some s1'' -> Some (Ending s1'')
              | None -> None)
           | None -> Some (Ending s1')))
     | BD_suf_overflow (s1', a, b) ->
       if (=) (Coq_E.level a) (Coq_E.level b)
       then let ab_paired = Coq_E.pair a b in
            let (cd_paired, center) = suffix_rot b2 ab_paired in
            (match Coq_E.unpair cd_paired with
             | Some pair_cd ->
               Some (ChainCons ((PNode ((prefix23 p1opt pair_cd), Hole,
                 s1')), (Ending center)))
             | None -> None)
       else None)
  | BD_pre_ok p1' ->
    (match suffix_decompose b3 with
     | BD_suf_underflow s1opt ->
       (match buf5_eject_naive b2 with
        | Some p ->
          let (rest, ab) = p in
          (match Coq_E.unpair ab with
           | Some pair_ab ->
             Some (ChainCons ((PNode (p1', Hole, (suffix23 pair_ab s1opt))),
               (Ending rest)))
           | None -> None)
        | None ->
          (match s1opt with
           | Some x ->
             (match buf5_inject_naive p1' x with
              | Some p1'' -> Some (Ending p1'')
              | None -> None)
           | None -> Some (Ending p1')))
     | BD_suf_ok s1' ->
       Some (ChainCons ((PNode (p1', Hole, s1')), (Ending b2)))
     | BD_suf_overflow (s1', a, b) ->
       if (=) (Coq_E.level a) (Coq_E.level b)
       then let ab_paired = Coq_E.pair a b in
            let c2 = buffer_inject_chain b2 ab_paired in
            Some (ChainCons ((PNode (p1', Hole, s1')), c2))
       else None)
  | BD_pre_overflow (p1', c, d) ->
    (match suffix_decompose b3 with
     | BD_suf_underflow s1opt ->
       if (=) (Coq_E.level c) (Coq_E.level d)
       then let cd_paired = Coq_E.pair c d in
            let (center, ab_paired) = prefix_rot cd_paired b2 in
            (match Coq_E.unpair ab_paired with
             | Some pair_ab ->
               Some (ChainCons ((PNode (p1', Hole,
                 (suffix23 pair_ab s1opt))), (Ending center)))
             | None -> None)
       else None
     | BD_suf_ok s1' ->
       if (=) (Coq_E.level c) (Coq_E.level d)
       then let cd_paired = Coq_E.pair c d in
            let c2 = buffer_push_chain cd_paired b2 in
            Some (ChainCons ((PNode (p1', Hole, s1')), c2))
       else None
     | BD_suf_overflow (s1', a, b) ->
       if (=) (Coq_E.level c) (Coq_E.level d)
       then if (=) (Coq_E.level a) (Coq_E.level b)
            then let cd_paired = Coq_E.pair c d in
                 let ab_paired = Coq_E.pair a b in
                 let (midopt, rest_pairs) = buffer_halve b2 in
                 (match pair_each_buf rest_pairs with
                  | Some rest ->
                    let p2 = suffix12 cd_paired midopt in
                    Some (ChainCons ((PNode (p1', (PNode (p2, Hole, (B1
                    ab_paired))), s1')), (Ending rest)))
                  | None -> None)
            else None
       else None)

type 'a kChain =
| KEnding of 'a Coq_E.t buf5
| KCons of color * 'a packet * 'a kChain

(** val chain_to_kchain_g : 'a1 chain -> 'a1 kChain **)

let rec chain_to_kchain_g = function
| Ending b -> KEnding b
| ChainCons (p, c') -> KCons (Green, p, (chain_to_kchain_g c'))

(** val kchain_to_chain : 'a1 kChain -> 'a1 chain **)

let rec kchain_to_chain = function
| KEnding b -> Ending b
| KCons (_, p, c') -> ChainCons (p, (kchain_to_chain c'))

(** val kchain_to_list : 'a1 kChain -> 'a1 list **)

let kchain_to_list c =
  chain_to_list (kchain_to_chain c)

(** val green_of_red_k : 'a1 kChain -> 'a1 kChain option **)

let green_of_red_k = function
| KEnding _ -> None
| KCons (c0, p, c1) ->
  (match c0 with
   | Red ->
     (match p with
      | Hole -> None
      | PNode (pre1, p0, suf1) ->
        (match p0 with
         | Hole ->
           (match c1 with
            | KEnding b ->
              (match make_small pre1 b suf1 with
               | Some c'' -> Some (chain_to_kchain_g c'')
               | None -> None)
            | KCons (_, p1, c2) ->
              (match p1 with
               | Hole -> None
               | PNode (pre2, child, suf2) ->
                 (match green_prefix_concat pre1 pre2 with
                  | Some p2 ->
                    let (pre1', pre2') = p2 in
                    (match green_suffix_concat suf2 suf1 with
                     | Some p3 ->
                       let (suf2', suf1') = p3 in
                       Some (KCons (Green, (PNode (pre1', (PNode (pre2',
                       child, suf2')), suf1')), c2))
                     | None -> None)
                  | None -> None)))
         | PNode (pre2, child, suf2) ->
           (match prefix_concat pre1 pre2 with
            | Some p1 ->
              let (pre1', pre2') = p1 in
              (match suffix_concat suf2 suf1 with
               | Some p2 ->
                 let (suf2', suf1') = p2 in
                 Some (KCons (Green, (PNode (pre1', Hole, suf1')), (KCons
                 (Red, (PNode (pre2', child, suf2')), c1))))
               | None -> None)
            | None -> None)))
   | _ -> None)

module Coq0_E = E

type 'a sChain =
| SEnding of int * 'a Coq0_E.t buf5
| SCons of int * color * 'a packet * 'a kChain

(** val s_empty : 'a1 sChain **)

let s_empty =
  SEnding (0, B0)

(** val s_size : 'a1 sChain -> int **)

let s_size = function
| SEnding (n, _) -> n
| SCons (n, _, _, _) -> n

(** val s_erase : 'a1 sChain -> 'a1 kChain **)

let s_erase = function
| SEnding (_, b) -> KEnding b
| SCons (_, col, p, t0) -> KCons (col, p, t0)

(** val s_of : int -> 'a1 kChain -> 'a1 sChain **)

let s_of n = function
| KEnding b -> SEnding (n, b)
| KCons (col, p, t0) -> SCons (n, col, p, t0)

(** val s_to_list : 'a1 sChain -> 'a1 list **)

let s_to_list c =
  kchain_to_list (s_erase c)

(** val yellow_wrap_s :
    'a1 sChain -> int -> 'a1 Coq0_E.t buf5 -> 'a1 packet -> 'a1 Coq0_E.t buf5
    -> 'a1 kChain -> 'a1 sChain **)

let yellow_wrap_s fb n' pre i suf t0 = match t0 with
| KEnding _ -> SCons (n', Yellow, (PNode (pre, i, suf)), t0)
| KCons (c, _, _) ->
  (match c with
   | Red ->
     (match green_of_red_k t0 with
      | Some t' -> SCons (n', Yellow, (PNode (pre, i, suf)), t')
      | None -> fb)
   | _ -> SCons (n', Yellow, (PNode (pre, i, suf)), t0))

(** val push_s : 'a1 Coq0_E.t -> 'a1 sChain -> 'a1 sChain **)

let push_s x c = match c with
| SEnding (n, b) ->
  (match b with
   | B0 -> SEnding ((Stdlib.Int.succ n), (B1 x))
   | B1 a -> SEnding ((Stdlib.Int.succ n), (B2 (x, a)))
   | B2 (a, b1) -> SEnding ((Stdlib.Int.succ n), (B3 (x, a, b1)))
   | B3 (a, b1, c1) -> SEnding ((Stdlib.Int.succ n), (B4 (x, a, b1, c1)))
   | B4 (a, b1, c1, d) ->
     SEnding ((Stdlib.Int.succ n), (B5 (x, a, b1, c1, d)))
   | B5 (a, b1, c1, d, e) ->
     SCons ((Stdlib.Int.succ n), Green, (PNode ((B3 (x, a, b1)), Hole, (B3
       (c1, d, e)))), (KEnding B0)))
| SCons (n, col, p, t0) ->
  (match col with
   | Green ->
     (match p with
      | Hole -> c
      | PNode (pre, i, suf) ->
        (match pre with
         | B2 (a, b1) ->
           yellow_wrap_s c (Stdlib.Int.succ n) (B3 (x, a, b1)) i suf t0
         | B3 (a, b1, c1) ->
           yellow_wrap_s c (Stdlib.Int.succ n) (B4 (x, a, b1, c1)) i suf t0
         | _ -> c))
   | Yellow ->
     (match p with
      | Hole -> c
      | PNode (pre, i, suf) ->
        (match pre with
         | B1 a ->
           SCons ((Stdlib.Int.succ n), Yellow, (PNode ((B2 (x, a)), i, suf)),
             t0)
         | B2 (a, b1) ->
           SCons ((Stdlib.Int.succ n), Yellow, (PNode ((B3 (x, a, b1)), i,
             suf)), t0)
         | B3 (a, b1, c1) ->
           SCons ((Stdlib.Int.succ n), Yellow, (PNode ((B4 (x, a, b1, c1)),
             i, suf)), t0)
         | B4 (a, b1, c1, d) ->
           (match green_of_red_k (KCons (Red, (PNode ((B5 (x, a, b1, c1, d)),
                    i, suf)), t0)) with
            | Some d' -> s_of (Stdlib.Int.succ n) d'
            | None -> c)
         | _ -> c))
   | Red -> c)

(** val inject_s : 'a1 sChain -> 'a1 Coq0_E.t -> 'a1 sChain **)

let inject_s c x =
  match c with
  | SEnding (n, b) ->
    (match b with
     | B0 -> SEnding ((Stdlib.Int.succ n), (B1 x))
     | B1 a -> SEnding ((Stdlib.Int.succ n), (B2 (a, x)))
     | B2 (a, b1) -> SEnding ((Stdlib.Int.succ n), (B3 (a, b1, x)))
     | B3 (a, b1, c1) -> SEnding ((Stdlib.Int.succ n), (B4 (a, b1, c1, x)))
     | B4 (a, b1, c1, d) ->
       SEnding ((Stdlib.Int.succ n), (B5 (a, b1, c1, d, x)))
     | B5 (a, b1, c1, d, e) ->
       SCons ((Stdlib.Int.succ n), Green, (PNode ((B3 (a, b1, c1)), Hole, (B3
         (d, e, x)))), (KEnding B0)))
  | SCons (n, col, p, t0) ->
    (match col with
     | Green ->
       (match p with
        | Hole -> c
        | PNode (pre, i, suf) ->
          (match suf with
           | B2 (a, b1) ->
             yellow_wrap_s c (Stdlib.Int.succ n) pre i (B3 (a, b1, x)) t0
           | B3 (a, b1, c1) ->
             yellow_wrap_s c (Stdlib.Int.succ n) pre i (B4 (a, b1, c1, x)) t0
           | _ -> c))
     | Yellow ->
       (match p with
        | Hole -> c
        | PNode (pre, i, suf) ->
          (match suf with
           | B1 a ->
             SCons ((Stdlib.Int.succ n), Yellow, (PNode (pre, i, (B2 (a,
               x)))), t0)
           | B2 (a, b1) ->
             SCons ((Stdlib.Int.succ n), Yellow, (PNode (pre, i, (B3 (a, b1,
               x)))), t0)
           | B3 (a, b1, c1) ->
             SCons ((Stdlib.Int.succ n), Yellow, (PNode (pre, i, (B4 (a, b1,
               c1, x)))), t0)
           | B4 (a, b1, c1, d) ->
             (match green_of_red_k (KCons (Red, (PNode (pre, i, (B5 (a, b1,
                      c1, d, x)))), t0)) with
              | Some d' -> s_of (Stdlib.Int.succ n) d'
              | None -> c)
           | _ -> c))
     | Red -> c)

type 'a spop_result =
| SPopFail
| SPopOk of 'a Coq0_E.t * 'a sChain

(** val pop_s : 'a1 sChain -> 'a1 spop_result **)

let pop_s = function
| SEnding (n, b) ->
  (match b with
   | B0 -> SPopFail
   | B1 a -> SPopOk (a, (SEnding ((Nat.pred n), B0)))
   | B2 (a, b1) -> SPopOk (a, (SEnding ((Nat.pred n), (B1 b1))))
   | B3 (a, b1, c1) -> SPopOk (a, (SEnding ((Nat.pred n), (B2 (b1, c1)))))
   | B4 (a, b1, c1, d) ->
     SPopOk (a, (SEnding ((Nat.pred n), (B3 (b1, c1, d)))))
   | B5 (a, b1, c1, d, e) ->
     SPopOk (a, (SEnding ((Nat.pred n), (B4 (b1, c1, d, e))))))
| SCons (n, col, p, t0) ->
  (match col with
   | Green ->
     (match p with
      | Hole -> SPopFail
      | PNode (pre, i, suf) ->
        (match pre with
         | B2 (a, b1) ->
           (match t0 with
            | KEnding _ ->
              SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B1 b1), i,
                suf)), t0)))
            | KCons (c0, _, _) ->
              (match c0 with
               | Red ->
                 (match green_of_red_k t0 with
                  | Some t' ->
                    SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B1 b1),
                      i, suf)), t')))
                  | None -> SPopFail)
               | _ ->
                 SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B1 b1), i,
                   suf)), t0)))))
         | B3 (a, b1, c1) ->
           (match t0 with
            | KEnding _ ->
              SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B2 (b1, c1)),
                i, suf)), t0)))
            | KCons (c0, _, _) ->
              (match c0 with
               | Red ->
                 (match green_of_red_k t0 with
                  | Some t' ->
                    SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B2 (b1,
                      c1)), i, suf)), t')))
                  | None -> SPopFail)
               | _ ->
                 SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B2 (b1,
                   c1)), i, suf)), t0)))))
         | _ -> SPopFail))
   | Yellow ->
     (match p with
      | Hole -> SPopFail
      | PNode (pre, i, suf) ->
        (match pre with
         | B1 a ->
           (match green_of_red_k (KCons (Red, (PNode (B0, i, suf)), t0)) with
            | Some d' -> SPopOk (a, (s_of (Nat.pred n) d'))
            | None -> SPopFail)
         | B2 (a, b1) ->
           SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B1 b1), i,
             suf)), t0)))
         | B3 (a, b1, c1) ->
           SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B2 (b1, c1)), i,
             suf)), t0)))
         | B4 (a, b1, c1, d) ->
           SPopOk (a, (SCons ((Nat.pred n), Yellow, (PNode ((B3 (b1, c1, d)),
             i, suf)), t0)))
         | _ -> SPopFail))
   | Red -> SPopFail)

(** val eject_s : 'a1 sChain -> 'a1 spop_result **)

let eject_s = function
| SEnding (n, b) ->
  (match b with
   | B0 -> SPopFail
   | B1 a -> SPopOk (a, (SEnding ((Nat.pred n), B0)))
   | B2 (a, b1) -> SPopOk (b1, (SEnding ((Nat.pred n), (B1 a))))
   | B3 (a, b1, c1) -> SPopOk (c1, (SEnding ((Nat.pred n), (B2 (a, b1)))))
   | B4 (a, b1, c1, d) ->
     SPopOk (d, (SEnding ((Nat.pred n), (B3 (a, b1, c1)))))
   | B5 (a, b1, c1, d, e) ->
     SPopOk (e, (SEnding ((Nat.pred n), (B4 (a, b1, c1, d))))))
| SCons (n, col, p, t0) ->
  (match col with
   | Green ->
     (match p with
      | Hole -> SPopFail
      | PNode (pre, i, suf) ->
        (match suf with
         | B2 (a, b1) ->
           (match t0 with
            | KEnding _ ->
              SPopOk (b1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i, (B1
                a))), t0)))
            | KCons (c0, _, _) ->
              (match c0 with
               | Red ->
                 (match green_of_red_k t0 with
                  | Some t' ->
                    SPopOk (b1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i,
                      (B1 a))), t')))
                  | None -> SPopFail)
               | _ ->
                 SPopOk (b1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i,
                   (B1 a))), t0)))))
         | B3 (a, b1, c1) ->
           (match t0 with
            | KEnding _ ->
              SPopOk (c1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i, (B2
                (a, b1)))), t0)))
            | KCons (c0, _, _) ->
              (match c0 with
               | Red ->
                 (match green_of_red_k t0 with
                  | Some t' ->
                    SPopOk (c1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i,
                      (B2 (a, b1)))), t')))
                  | None -> SPopFail)
               | _ ->
                 SPopOk (c1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i,
                   (B2 (a, b1)))), t0)))))
         | _ -> SPopFail))
   | Yellow ->
     (match p with
      | Hole -> SPopFail
      | PNode (pre, i, suf) ->
        (match suf with
         | B1 a ->
           (match green_of_red_k (KCons (Red, (PNode (pre, i, B0)), t0)) with
            | Some d' -> SPopOk (a, (s_of (Nat.pred n) d'))
            | None -> SPopFail)
         | B2 (a, b1) ->
           SPopOk (b1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i, (B1
             a))), t0)))
         | B3 (a, b1, c1) ->
           SPopOk (c1, (SCons ((Nat.pred n), Yellow, (PNode (pre, i, (B2 (a,
             b1)))), t0)))
         | B4 (a, b1, c1, d) ->
           SPopOk (d, (SCons ((Nat.pred n), Yellow, (PNode (pre, i, (B3 (a,
             b1, c1)))), t0)))
         | _ -> SPopFail))
   | Red -> SPopFail)