-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_dependent_program.v
2382 lines (2086 loc) · 79 KB
/
dynamic_dependent_program.v
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
(* Xuanrui Qi, Kazunari Tanaka, Jacques Garrigue *)
From mathcomp Require Import ssreflect ssrbool ssrfun eqtype ssrnat div seq.
From mathcomp Require Import choice fintype prime tuple finfun finset bigop.
Require Import tree_traversal rank_select insert_delete Program.
Require Import set_clear Compare_dec ExtrOcamlNatInt dynamic.
Set Implicit Arguments.
Ltac subst_eq :=
repeat match goal with
| H: ?X = ?Y |- _ => (subst X || subst Y)
end.
Goal (forall x, x = 1 -> 2 = 2 -> x = 2).
intros.
subst_eq.
Abort.
Section dynamic_dependent.
Variable w : nat.
Hypothesis wordsize_gt1: w > 1.
Section insert.
(*
* Translated from https://github.com/xuanruiqi/dtp/blob/master/RedBlack.idr
* which in turn is translated from dynamic_dependent.v
*)
Program Definition balanceL {lnum lones rnum rones d cl cr} (c : color)
(l : near_tree w lnum lones d cl)
(r : tree w rnum rones d cr)
(ok_l : color_ok c (fix_color l))
(ok_r : color_ok c cr) :
{ t' : near_tree w (lnum + rnum) (lones + rones) (incr_black d c) c
| dflatteni t' = dflatteni l ++ dflatten r } :=
match c with
| Black =>
match l with
| Bad _ _ _ _ _ _ _ t1 t2 t3 => Good Black (rnode (bnode t1 t2) (bnode t3 r))
| Good _ _ _ _ _ l' => Good Black (bnode l' r)
end
| Red => match cr with
| Red => _ (* impossible *)
| Black => match l with
| Bad _ _ _ _ _ _ _ _ _ _ => _ (* impossible *)
| @Good _ _ _ _ c' _ l' =>
match c' with
| Red =>
match l' with
| Leaf _ _ _ => _ (* impossible *)
| @Node _ _ _ _ _ _ cll clr _ _ _ t1 t2 =>
match cll with
| Black => match clr with
| Black => Bad t1 t2 r
| Red => _ (* impossible *)
end
| Red => _ (* impossible *)
end
end
| Black => Good Red (rnode l' r)
end
end
end
end.
Next Obligation. by rewrite addnA. Qed.
Next Obligation. by rewrite addnA. Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceL_obligation_2 => //=.
destruct balanceL_obligation_3 => //=.
by rewrite -Heq_l //= -!catA.
Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceL_obligation_7 => //=.
by rewrite -Heq_l.
Qed.
Next Obligation. subst l; by move: ok_l. Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceL_obligation_18 => //=.
by rewrite -Heq_l -Heq_l' //= catA.
Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceL_obligation_28 => //=.
by rewrite -Heq_l.
Qed.
Program Definition balanceR {lnum lones rnum rones d cl cr} (c : color)
(l : tree w lnum lones d cl)
(r : near_tree w rnum rones d cr)
(ok_l : color_ok c cl)
(ok_r : color_ok c (fix_color r)) :
{ t' : near_tree w (lnum + rnum) (lones + rones) (incr_black d c) c |
dflatteni t' = dflatten l ++ dflatteni r } :=
match c with
| Black =>
match r with
| Bad _ _ _ _ _ _ _ t1 t2 t3 => Good Black (rnode (bnode l t1) (bnode t2 t3))
| Good _ _ _ _ _ r' => Good Black (bnode l r')
end
| Red => match cl with
| Red => _ (* impossible *)
| Black => match r with
| Bad _ _ _ _ _ _ _ _ _ _ => _ (* impossible *)
| @Good _ _ _ _ c' _ r' =>
match c' with
| Red =>
match r' with
| Leaf _ _ _ => _ (* impossible *)
| @Node _ _ _ _ _ _ cll clr _ _ _ t1 t2 =>
match cll with
| Black => match clr with
| Black => Bad l t1 t2
| Red => _ (* impossible *)
end
| Red => _ (* impossible *)
end
end
| Black => Good Red (rnode l r')
end
end
end
end.
Next Obligation. by rewrite !addnA. Qed.
Next Obligation. by rewrite !addnA. Qed.
Next Obligation.
rewrite /eq_rect.
destruct balanceR_obligation_3, balanceR_obligation_4 => //=.
by rewrite -Heq_r //= !catA.
Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceR_obligation_7 => //=.
by rewrite -Heq_r.
Qed.
Next Obligation. subst r; by move: ok_r. Qed.
Next Obligation. by rewrite addnA. Qed.
Next Obligation. by rewrite addnA. Qed.
Next Obligation.
rewrite /eq_rect.
destruct balanceR_obligation_18, balanceR_obligation_19 => //=.
by rewrite -Heq_r -Heq_r'.
Qed.
Next Obligation.
rewrite /eq_rect. destruct balanceR_obligation_27 => //=.
by rewrite -Heq_r.
Qed.
Program Fixpoint dins {num ones d c}
(B : tree w num ones d c)
(b : bool) (i : nat) {measure (size_of_tree B) } :
{ B' : near_tree w num.+1 (ones + b) d c |
dflatteni B' = insert1 (dflatten B) b i } :=
match B with
| Leaf s _ _ =>
let s' := insert1 s b i in
(* cannot use "if" due to bugs in Program *)
match (size s' == 2 * (w ^ 2)) with
| true => (* split the node *)
let n := (size s') %/ 2 in
let sl := take n s' in
let sr := drop n s' in
Good c (rnode (Leaf _ sl _ _) (Leaf _ sr _ _))
| false => Good c (Leaf _ s' _ _)
end
| Node lnum _ _ _ _ _ _ _ ok_l ok_r l r =>
if i < lnum
then (` (balanceL c (dins l b i) r _ ok_r))
else (` (balanceR c l (dins r b (i - lnum)) ok_l _))
end.
Next Obligation.
move/eqP/eqnP: Heq_anonymous => -> //=.
rewrite size_take size_insert1 mulKn //=.
case: ifP => H. exact: leq_div.
apply: leq_trans. apply: wildcard'.
by rewrite leq_eqVlt ltnSn orbT.
Qed.
Next Obligation.
move/eqP/eqnP: Heq_anonymous => -> //=.
rewrite size_take size_insert1 mulKn //=.
case: ifP => H. by rewrite ltn_Pmull //= expn_gt0 wordsize_gt0.
move/negbT: H. rewrite -leqNgt => H.
apply: leq_ltn_trans. apply: H.
by rewrite ltn_Pmull //= expn_gt0 wordsize_gt0.
Qed.
Next Obligation.
move/eqP/eqnP: Heq_anonymous => //=.
rewrite size_drop size_insert1 => ->.
by rewrite mulKn //= mulSn mul1n -addnBA // subnKC // leq_div.
Qed.
Next Obligation.
move/eqP/eqnP: Heq_anonymous => //=.
rewrite size_drop size_insert1 => ->.
rewrite mulKn //= mulSn mul1n -addnBA // subnKC //.
by rewrite -{1}[w ^ 2]addn0 ltn_add2l expn_gt0 wordsize_gt0.
Qed.
Next Obligation. by rewrite -size_cat cat_take_drop size_insert1. Qed.
Next Obligation.
rewrite -count_cat cat_take_drop /count_one count_insert1. by destruct b.
Qed.
Next Obligation.
rewrite /eq_rect.
destruct dins_func_obligation_7, dins_func_obligation_6, dins_func_obligation_5 => //=.
by rewrite cat_take_drop.
Qed.
Next Obligation.
rewrite size_insert1. exact: leq_trans.
Qed.
Next Obligation.
move/eqP/eqnP/eqP: Heq_anonymous => /=.
rewrite size_insert1 neq_ltn. case/orP => //.
by rewrite ltnNge wildcard'0.
Qed.
Next Obligation. by rewrite size_insert1. Qed.
Next Obligation. rewrite /count_one count_insert1. by destruct b. Qed.
Next Obligation.
rewrite /eq_rect.
by destruct dins_func_obligation_13, dins_func_obligation_12, dins_func_obligation_11.
Qed.
Next Obligation.
apply/ltP. by rewrite -Heq_B /= -[X in X < _]addn0 ltn_add2l size_of_tree_pos.
Qed.
Next Obligation. by destruct dins, x, c. Qed.
Next Obligation.
apply/ltP. by rewrite -Heq_B /= -[X in X < _]add0n ltn_add2r size_of_tree_pos.
Qed.
Next Obligation. by destruct dins, x, c. Qed.
Next Obligation. by rewrite -addn1 -[in RHS]addn1 addnAC addnA. Qed.
Next Obligation. by rewrite addnAC addnA. Qed.
Next Obligation. by rewrite addnAC. Qed.
Next Obligation.
case: ifP => /= H.
- set B' := balanceL _ _ _ _ _.
destruct dins_func_obligation_23.
rewrite (proj2_sig B') {B'}.
destruct dins => /=.
by rewrite e /insert1 /insert take_cat drop_cat size_dflatten H -!catA.
- set B' := balanceR _ _ _ _ _.
rewrite /dflatteni /eq_rect => //=.
destruct dins_func_obligation_23, dins_func_obligation_22, dins_func_obligation_21.
rewrite -/(dflatteni (proj1_sig B')) (proj2_sig B') {B'}.
destruct dins => //=.
by rewrite e /insert1 /insert take_cat drop_cat size_dflatten H -!catA.
Qed.
Program Definition paint_black {num ones d c} (B : tree w num ones d c) :
{ B' : tree w num ones (incr_black d (inv c)) Black |
dflatten B' = dflatten B } :=
match B with
| Leaf _ _ _ => B
| Node _ _ _ _ _ _ _ _ _ _ l r => bnode l r
end.
Next Obligation.
rewrite /eq_rect.
destruct paint_black_obligation_4 => //=. by rewrite -Heq_B.
Qed.
Next Obligation. by destruct c. Qed.
Next Obligation. rewrite /eq_rect. by destruct paint_black_obligation_6. Qed.
Definition dinsert {num ones d c}
(B : tree w num ones d c) (b : bool) (i : nat) :=
(` (paint_black (fix_near_tree (` (dins B b i))))).
Theorem dinsertK {num ones d c} (B : tree w num ones d c) (b : bool) (i : nat) :
dflatten (dinsert B b i) = insert1 (dflatten B) b i.
Proof.
rewrite /dinsert. destruct dins, paint_black => //=.
by rewrite e0 //= fix_near_treeK e.
Qed.
End insert.
Section query.
Fixpoint daccess {n m d c} (tr : tree w n m d c) i :=
match tr with
| Leaf s _ _ => nth false s i
| Node lnum _ _ _ _ _ _ _ _ _ l r =>
if i < lnum
then daccess l i
else daccess r (i - lnum)
end.
Fixpoint drank {n m d c} (tr : tree w n m d c) i :=
match tr with
| Leaf s _ _ => rank true i s
| Node lnum lones rnum rones _ _ _ _ _ _ l r =>
if i < lnum
then drank l i
else lones + drank r (i - lnum)
end.
Fixpoint dselect_0 {n m d c} (tr : tree w n m d c) i :=
match tr with
| Leaf s _ _ => select false i s
| Node s1 o1 s2 o2 _ _ _ _ _ _ l r =>
let zeroes := s1 - o1
in if i <= zeroes
then dselect_0 l i
else s1 + dselect_0 r (i - zeroes)
end.
Fixpoint dselect_1 {n m d c} (tr : tree w n m d c) i :=
match tr with
| Leaf s _ _ => select true i s
| Node s1 o1 s2 o2 _ _ _ _ _ _ l r =>
if i <= o1
then dselect_1 l i
else s1 + dselect_1 r (i - o1)
end.
Lemma daccessK nums ones d c (B : tree w nums ones d c) :
daccess B =1 access (dflatten B).
Proof.
rewrite /access.
elim: B => //= lnum o1 s2 o2 d0 cl cr c0 i i0 l IHl r IHr x.
by rewrite nth_cat size_dflatten -IHl -IHr.
Qed.
Lemma drankK nums ones d c (B : tree w nums ones d c) i :
drank B i = rank true i (dflatten B).
Proof.
elim: B i => //= lnum o1 s2 o2 d0 cl cr c0 i i0 l IHl r IHr x.
by rewrite rank_cat size_dflatten IHl -IHr -dflatten_rank.
Qed.
Lemma drank_ones num ones d c (B : tree w num ones d c) :
drank B num = ones.
Proof.
by rewrite [in RHS](dflatten_rank B) drankK.
Qed.
Lemma dselect1K nums ones d c (B : tree w nums ones d c) i :
dselect_1 B i = select true i (dflatten B).
Proof.
elim: B i => //= lnum o1 s2 o2 d0 cl cr c0 i i0 l IHl r IHr x.
by rewrite select_cat -dflatten_ones IHl IHr size_dflatten.
Qed.
Lemma dselect0K nums ones d c (B : tree w nums ones d c) i :
dselect_0 B i = select false i (dflatten B).
Proof.
elim: B i => //= lnum o1 s2 o2 d0 cl cr c0 i i0 l IHl r IHr x.
by rewrite select_cat -dflatten_zeroes IHl IHr size_dflatten.
Qed.
Lemma access_leq_count (s : seq bool) i : i < size s -> access s i <= count_one s.
Proof.
rewrite /access.
elim: s i => //= h s IHs i H.
case_eq i => [| i'] //= Hi'. by case: h.
apply: leq_trans. apply: IHs. move: H. by rewrite Hi' ltnS.
by rewrite leq_addl.
Qed.
Lemma daccess_leq_ones {num ones d c} (B : tree w num ones d c) i :
i < num -> daccess B i <= ones.
Proof.
elim: B i => [s w_wf size_wf | lnum lones rnum rones d' cl cr c' ok_l ok_r l IHl r IHr] //= i H.
by rewrite /count_one access_leq_count.
case: ifP => Hi.
apply: leq_trans. exact: IHl. exact: leq_addr.
apply: leq_trans. apply: IHr.
rewrite -(ltn_add2r lnum) addnC addnBA.
by rewrite addnC addnK addnC. by rewrite leqNgt Hi.
by rewrite leq_addl.
Qed.
End query.
Section set_clear.
Obligation Tactic := idtac.
Program Fixpoint bset {num ones d c} (B : tree w num ones d c) i
{measure (size_of_tree B)} :
{ B'b : tree w num (ones + (~~ (daccess B i)) && (i < num)) d c * bool
| dflatten (fst B'b) = bit_set (dflatten B) i/\snd B'b = ~~ daccess B i } :=
match B with
| Leaf s _ _ => (Leaf _ (bit_set s i) _ _, ~~ (access s i))
| Node lnum lones rnum rones _ _ _ _ col cor l r =>
match lt_dec i lnum with
| left H =>
let x := bset l i
in (Node col cor x.1 r, x.2)
| right H =>
let x := bset r (i - lnum)
in (Node col cor l x.1, x.2)
end
end.
Next Obligation. intros. by rewrite size_bit_set. Qed.
Next Obligation. intros. by rewrite size_bit_set. Qed.
Next Obligation. intros; apply: size_bit_set. Qed.
Next Obligation.
intros; case Hi: (i < size s).
rewrite /count_one /daccess (count_bit_set false). by rewrite andbT addnC.
by rewrite Hi.
rewrite andbF addn0. by rewrite /count_one /daccess bit_set_over //= leqNgt Hi.
Qed.
Next Obligation.
intros; subst; split => //.
by destruct bset_func_obligation_4 , bset_func_obligation_3 => /=.
Qed.
Next Obligation.
intros; subst. apply /ltP.
by rewrite -addn1 leq_add2l size_of_tree_pos.
Qed.
Next Obligation.
intros; move/ltP: (H) => Hi /=.
by rewrite Hi (ltn_addr _ Hi) addnAC.
Qed.
Next Obligation.
split; subst_eq; last first.
destruct bset as [[l' flip][Hl' Hf]] => /=.
move/ltP: (H) => ->.
by rewrite -Hf.
move=> /=.
move: (lones + rones + _) (bset_func_obligation_7 _ _ _ _ _ _) => ones Ho.
destruct Ho => /=.
destruct bset as [[l' flip][Hl' Hf]] => /=.
rewrite /= in Hl'.
move/ltP: (H).
rewrite Hl' /bit_set update_cat {1}size_dflatten => Hi.
by rewrite ifT.
Qed.
Next Obligation.
intros; subst. apply /ltP.
by rewrite -add1n leq_add2r size_of_tree_pos.
Qed.
Next Obligation.
intros; move/ltP: (H) => Hi /=.
rewrite -if_neg Hi !addnA.
by rewrite -(ltn_add2l lnum) subnKC // leqNgt.
Qed.
Next Obligation.
split; subst_eq; last first.
destruct bset as [[r' flip][Hr' Hf]] => /=.
move/ltP: (H) => Hi.
by rewrite -if_neg Hi -Hf.
move=> /=.
move: (lones + rones + _) (bset_func_obligation_10 _ _ _ _ _ _) => ones Ho.
destruct Ho => /=.
destruct bset as [[r' flip][Hr' Hf]] => /=.
rewrite /= in Hr'.
move/ltP: (H).
rewrite Hr' /bit_set update_cat size_dflatten => Hi.
by rewrite -if_neg Hi.
Qed.
Next Obligation. intuition. Qed.
Program Fixpoint bclear {num ones d c}
(B : tree w num ones d c) i
{ measure (size_of_tree B) } :
{ B'b : tree w num (ones - (daccess B i) && (i < num)) d c * bool |
dflatten B'b.1 = bit_clear (dflatten B) i /\ snd B'b = daccess B i } :=
match B with
| Leaf s _ _ => (Leaf _ (bit_clear s i) _ _, access s i)
| Node lnum lones rnum rones _ _ _ _ col cor l r =>
match lt_dec i lnum with
| left H =>
let l'b := bclear l i
in (Node col cor l'b.1 r, l'b.2)
| right H =>
let r'b := bclear r (i - lnum)
in (Node col cor l r'b.1, r'b.2)
end
end.
Next Obligation. intros. by rewrite size_bit_clear. Qed.
Next Obligation. intros. by rewrite size_bit_clear. Qed.
Next Obligation. intros. by rewrite size_bit_clear. Qed.
Next Obligation.
intros. case Hi: (i < size s).
+ by rewrite /count_one //= (count_bit_clear false) //= andbT.
+ rewrite bit_clear_over //=. by rewrite andbF subn0.
by rewrite leqNgt Hi.
Qed.
Next Obligation.
intros; subst. rewrite /eq_rect.
by destruct bclear_func_obligation_4, bclear_func_obligation_3.
Qed.
Next Obligation.
intros; subst. apply/ltP.
by rewrite -addn1 leq_add2l size_of_tree_pos.
Qed.
Next Obligation.
intros => //=.
move/ltP: (H) => Hi /=.
rewrite Hi ltn_addr //= andbT.
rewrite addnC addnBA. by rewrite addnC. by rewrite daccess_leq_ones.
Qed.
Next Obligation.
intros. rewrite /eq_rect.
destruct bclear_func_obligation_7 => //=.
move/ltP : (H) => Hi /=.
rewrite /proj1_sig; subst l'b. destruct bclear => //=.
split.
+ rewrite (proj1 a) /bit_clear update_cat size_dflatten.
by rewrite Hi.
+ by rewrite (proj2 a) Hi.
Qed.
Next Obligation.
intros; subst. apply/ltP.
by rewrite -[X in X < _]add0n ltn_add2r size_of_tree_pos.
Qed.
Next Obligation.
intros => //=.
move/ltP: (H) => Hi /=. rewrite -if_neg Hi.
case Hi': (i - lnum < rnum); move: (Hi'); rewrite -(ltn_add2l lnum) subnKC;
try move => ->.
+ rewrite andbT addnBA //= daccess_leq_ones //= Hi' //=.
by rewrite leqNgt Hi.
+ by rewrite andbF !subn0.
by rewrite leqNgt Hi.
Qed.
Next Obligation.
split; last first.
destruct bclear as [[r' tgt][Hr' Hf]] => /=.
move/ltP: (H) => Hi.
by rewrite -if_neg Hi -Hf.
move=> /=.
move: (lones + rones - _) (bclear_func_obligation_10 _ _ _ _ _ _)
=> ones' Ho.
destruct Ho => /=.
destruct bclear as [[r' tgt][Hr' Hf]] => /=.
rewrite /= in Hr'.
move/ltP: (H).
rewrite Hr' /bit_clear update_cat (size_dflatten l) => Hi.
by rewrite -if_neg Hi.
Qed.
Next Obligation. intuition. Qed.
End set_clear.
Section delete.
Inductive del_tree : nat -> nat -> nat -> color -> Type :=
| Stay : forall {num ones d c} pc,
color_ok c (inv pc) -> tree w num ones d c -> del_tree num ones d pc
| Down : forall {num ones d}, tree w num ones d Black -> del_tree num ones d.+1 Black.
Definition dflattend {num ones d c} (tr : del_tree num ones d c) :=
match tr with
| Stay _ _ _ _ _ _ t => dflatten t
| Down _ _ _ t => dflatten t
end.
(* NB: move to dynamic_dependent.v? *)
Lemma wordsize_sqrn_div2_gt0 : 0 < w ^ 2 %/ 2.
Proof.
rewrite lt0n. apply/eqP. exact: wordsize_sqrn_div2_neq0.
Qed.
Lemma wordsize_del_lt_twice : (w ^ 2 %/ 2 + (w ^ 2 %/ 2).-1 < 2 * w ^ 2).
Proof.
have Hw: (w ^ 2 %/ 2 + (w ^ 2 %/ 2).-1 < w ^ 2).
rewrite -subn1 addnBA divn2. rewrite addnn.
rewrite -[X in _ < X]odd_double_half subn1.
have Hw': (((w ^ 2)./2).*2.-1 < ((w ^ 2)./2).*2).
rewrite -subn1 -[X in _ < X]subn0 ltn_sub2l //=.
by rewrite -[X in X < _]double0 ltn_double -divn2 wordsize_sqrn_div2_gt0.
apply: leq_trans. apply: Hw'. apply: leq_addl.
by rewrite -divn2 wordsize_sqrn_div2_gt0.
apply: leq_trans. apply: Hw. exact: leq_pmull.
Qed.
Lemma cons_del_head s : size s > 0 -> access s 0 :: delete s 0 = s.
Proof.
case: s => //= h s H.
by rewrite /delete //= drop0.
Qed.
Local Obligation Tactic := idtac.
Program Definition merge_arrays
(s1 s2 : seq bool) (i : nat)
(w_ok1 : w ^ 2 %/ 2 == size s1)
(w_ok2 : w ^ 2 %/ 2 == size s2)
(Hi : i < size s1 + size s2) :
{ tr : tree w (size s1 + size s2 - 1)
(count_one s1 + count_one s2 - access (s1 ++ s2) i) 0 Black |
dflatten tr = delete (s1 ++ s2) i } :=
if i < size s1 is true
then Leaf _ ((rcons (delete s1 i) (access s2 0)) ++ (delete s2 0)) _ _
else Leaf _ (s1 ++ (delete s2 (i - size s1))) _ _.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
rewrite size_cat size_rcons.
case Hi': (i < size s1).
+ rewrite !size_delete. rewrite -(eqP w_ok1) -(eqP w_ok2).
rewrite prednK. exact: leq_addr. exact: wordsize_sqrn_div2_gt0.
by rewrite -(eqP w_ok2) wordsize_sqrn_div2_gt0. by rewrite Hi'.
+ rewrite delete_oversize //=. rewrite size_delete.
rewrite -[X in _ <= X + _]addn1 -(eqP w_ok1). rewrite addnAC -addnA.
apply: leq_addr. by rewrite -(eqP w_ok2) wordsize_sqrn_div2_gt0.
by rewrite leqNgt Hi'.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
rewrite size_cat size_rcons.
case Hi': (i < size s1).
+ rewrite !size_delete. rewrite -(eqP w_ok1) -(eqP w_ok2).
rewrite prednK. apply: wordsize_del_lt_twice.
apply: wordsize_sqrn_div2_gt0.
by rewrite -(eqP w_ok2) wordsize_sqrn_div2_gt0.
by rewrite Hi'.
+ rewrite delete_oversize. rewrite size_delete.
rewrite -(eqP w_ok1) -(eqP w_ok2).
rewrite addSnnS prednK.
by rewrite addnn mul2n ltn_double ltn_Pdiv //= wordsize_sqrn_gt0.
apply: wordsize_sqrn_div2_gt0. by rewrite -(eqP w_ok2) wordsize_sqrn_div2_gt0.
by rewrite leqNgt Hi'.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
rewrite size_cat size_rcons !size_delete ?prednK.
by rewrite -subn1 addnBA //= -(eqP w_ok2) wordsize_sqrn_div2_gt0.
by rewrite -(eqP w_ok1) wordsize_sqrn_div2_gt0.
by rewrite -?(eqP w_ok2) wordsize_sqrn_div2_gt0.
by rewrite -/filtered_var -Heq_anonymous.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
rewrite /count_one. rewrite -[in LHS]cats1 count_cat count_cat.
rewrite -/count_one -!count_delete.
rewrite /access nth_cat -/filtered_var -Heq_anonymous //= addn0.
rewrite -addnA eqb_id subnKC.
by rewrite addnC [in RHS]addnC addnBA //= -/access access_leq_count.
by rewrite access_leq_count //= -(eqP w_ok2) wordsize_sqrn_div2_gt0.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
rewrite /eq_rect.
destruct merge_arrays_obligation_3, merge_arrays_obligation_4 => //=.
have Hmatch: (match s2 with
| [::] => false
| x :: _ => x
end = access s2 0). by rewrite /access.
rewrite Hmatch -cats1 -catA cat1s. rewrite cons_del_head.
rewrite /delete take_cat -/filtered_var -Heq_anonymous -catA drop_cat.
case: ifP => Hi' //=.
rewrite drop_oversize //=.
have H: (i.+1 - size s1 == 0). by rewrite subn_eq0 -/filtered_var -Heq_anonymous.
by rewrite (eqP H) drop0.
move/negbT: Hi'. by rewrite -leqNgt => ->.
by rewrite -(eqP w_ok2) wordsize_sqrn_div2_gt0.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var Heq_anonymous.
by rewrite size_cat -(eqP w_ok1) leq_addr.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var wildcard' H Heq_anonymous.
rewrite size_cat. rewrite size_delete.
rewrite -(eqP w_ok1) -(eqP w_ok2).
apply: wordsize_del_lt_twice.
rewrite -(ltn_add2r (size s1)) subnK. by rewrite addnC.
by rewrite leqNgt -/filtered_var -Heq_anonymous; apply/negP; move/nesym in H.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var wildcard' H Heq_anonymous.
rewrite size_cat. rewrite size_delete.
by rewrite -subn1 addnBA //= -(eqP w_ok2) wordsize_sqrn_div2_gt0.
rewrite -(ltn_add2r (size s1)) subnK. by rewrite addnC.
by rewrite leqNgt -/filtered_var -Heq_anonymous; apply/negP; move/nesym in H.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var wildcard' n Heq_anonymous.
rewrite /count_one count_cat -/count_one. rewrite -count_delete.
rewrite /access. rewrite nth_cat.
rewrite ifF; last first.
by rewrite -/filtered_var -Heq_anonymous; destruct wildcard'.
rewrite addnBA //= -/access access_leq_count //=.
rewrite -(ltn_add2r (size s1)) subnK. by rewrite addnC.
by rewrite leqNgt -/filtered_var -Heq_anonymous; apply/negP; move/nesym in n.
Qed.
Next Obligation.
move=> s1 s2 i w_ok1 w_ok2 Hi filtered_var wildcard' n Heq_anonymous.
rewrite /eq_rect.
destruct merge_arrays_obligation_9, merge_arrays_obligation_8 => //=.
rewrite /delete take_cat.
rewrite ifF; last first.
by rewrite -/filtered_var -Heq_anonymous; destruct wildcard'.
rewrite drop_cat.
rewrite ltn_neqAle -/filtered_var -Heq_anonymous; destruct wildcard' => //.
by rewrite andbF catA subSn // leqNgt -/filtered_var -Heq_anonymous.
Qed.
Next Obligation. by []. Qed.
Local Obligation Tactic := program_simpl.
Definition delete_last (s : seq bool) := delete s (size s).-1.
Definition blast (s : seq bool) := access s (size s).-1.
Program Definition delete_from_leaves {lnum lones rnum rones : nat}
(pc : color)
(l : tree w lnum lones 0 Black)
(r : tree w rnum rones 0 Black)
(i : nat) :
{ B' : del_tree (lnum + rnum - (i < lnum + rnum))
(lones + rones - access (dflatten l ++ dflatten r) i)
(incr_black 0 pc) pc
| dflattend B' = delete (dflatten l ++ dflatten r) i} :=
match l with
| Leaf arr1 _ _ =>
match r with
| Leaf arr2 _ _ =>
if i < size arr1 is true
then if w ^ 2 %/ 2 == size arr1 is true
then if w ^ 2 %/ 2 == size arr2 is true
then
let ret := (` (merge_arrays arr1 arr2 i _ _ _)) in
match pc with
| Red => Stay Red _ ret
| Black => Down ret
end
else
match pc with
| Red => Stay Red _ (rnode
(Leaf _ (rcons (delete arr1 i)
(access arr2 0)) _ _)
(Leaf _ (delete arr2 0) _ _))
| Black => Stay Black _ (bnode
(Leaf _ (rcons (delete arr1 i)
(access arr2 0)) _ _)
(Leaf _ (delete arr2 0) _ _))
end
else
match pc with
| Red => Stay Red _ (rnode
(Leaf _ (delete arr1 i) _ _)
(Leaf _ arr2 _ _))
| Black => Stay Black _ (bnode
(Leaf _ (delete arr1 i) _ _)
(Leaf _ arr2 _ _))
end
else
if i < size arr1 + size arr2 is true
then if w ^ 2 %/ 2 == size arr2 is true
then if w ^ 2 %/ 2 == size arr1 is true
then
let ret := (` (merge_arrays arr1 arr2 i _ _ _)) in
match pc with
| Red => Stay Red _ ret
| Black => Down ret
end
else
match pc with
| Red => Stay Red _
(rnode (Leaf _ (delete_last arr1) _ _)
(Leaf _ ((blast arr1) ::
(delete arr2 (i - size arr1)))
_ _))
| Black => Stay Black _
(bnode (Leaf _ (delete_last arr1) _ _)
(Leaf _ ((blast arr1) ::
(delete arr2 (i - size arr1)))
_ _))
end
else
match pc with
| Red => Stay Red _ (rnode (Leaf _ arr1 _ _)
(Leaf _ (delete arr2 (i - size arr1)) _ _))
| Black => Stay Black _ (bnode (Leaf _ arr1 _ _)
(Leaf _ (delete arr2 (i - size arr1)) _ _))
end
else
match pc with
| Red => Stay Red _ (rnode (Leaf _ arr1 _ _) (Leaf _ arr2 _ _))
| Black => Stay Black _ (bnode (Leaf _ arr1 _ _) (Leaf _ arr2 _ _))
end
| Node _ _ _ _ _ _ _ _ _ _ _ _ => _
end
| Node _ _ _ _ _ _ _ _ _ _ _ _ => _
end.
Next Obligation.
have H: i < size arr1. by rewrite -Heq_anonymous4.
apply: leq_trans. apply: H. apply: leq_addr.
Qed.
Next Obligation.
have H: i < size arr1. by rewrite -Heq_anonymous4.
have H': (i < size arr1 + size arr2).
apply: leq_trans. apply: H. apply: leq_addr. by rewrite H'.
Qed.
Next Obligation.
by rewrite /access !nth_cat -Heq_l -Heq_anonymous4.
Qed.
Next Obligation.
rewrite /eq_rect.
destruct delete_from_leaves_obligation_6, delete_from_leaves_obligation_5 => //=.
destruct merge_arrays. by rewrite /proj1_sig //= -Heq_l -Heq_r.
Qed.
Next Obligation.
have H: i < size arr1. by rewrite -Heq_anonymous4.
have H': (i < size arr1 + size arr2).
apply: leq_trans. apply: H. apply: leq_addr. by rewrite H'.
Qed.
Next Obligation.
by rewrite /access !nth_cat -Heq_l -Heq_anonymous4.
Qed.
Next Obligation.
rewrite /eq_rect.
destruct delete_from_leaves_obligation_9, delete_from_leaves_obligation_8 => //=.
destruct merge_arrays. by rewrite /proj1_sig //= -Heq_l -Heq_r.
Qed.
Next Obligation.
rewrite -cats1 size_cat //= size_delete. rewrite addn1 prednK //=.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
by rewrite -Heq_anonymous4.
Qed.
Next Obligation.
rewrite -cats1 size_cat //= size_delete. rewrite addn1 prednK //=.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
by rewrite -Heq_anonymous4.
Qed.
Next Obligation.
move: (wildcard'3). rewrite leq_eqVlt.
move/eqP/eqnP/eqP/negbTE: H => -> //=.
have Harr2: 0 < size arr2.
apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
rewrite size_delete //=. move => Hw.
by rewrite -(leq_add2r 1) !addn1 prednK.
Qed.
Next Obligation.
have Harr2: 0 < size arr2.
apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
rewrite size_delete //=. apply: leq_ltn_trans.
apply: leq_pred. exact.
Qed.
Next Obligation.
have Harr2: 0 < size arr2.
apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
rewrite -cats1 size_cat //= !size_delete //= addn1 prednK //=.
have H': (i < size arr1 + size arr2).
move/eqP/eqnP/eqP: Heq_anonymous4 => H'.
apply: leq_trans. apply: H'. apply: leq_addr.
rewrite H' -subn1 addnBA //=.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
Qed.
Next Obligation.
rewrite -cats1. rewrite /count_one count_cat /=.
have Hmatch: (match arr2 with
| [::] => false
| x :: _ => x
end = access arr2 0). by rewrite /access.
rewrite /access nth_cat -Heq_l /= -Heq_anonymous4 Hmatch.
rewrite eqb_id /access addn0.
rewrite [in RHS]addnC -/access -/count_one.
rewrite -addnBA. rewrite count_delete.
rewrite -[X in _ + _ + X = _]count_delete.
rewrite -addnA subnKC. by rewrite addnC.
apply: access_leq_count. apply: leq_trans.
apply: wordsize_sqrn_div2_gt0. exact.
apply: access_leq_count. by rewrite -Heq_anonymous4.
Qed.
Next Obligation.
rewrite /eq_rect.
destruct delete_from_leaves_obligation_17, delete_from_leaves_obligation_16 => //=.
rewrite -cats1 delete_catL -Heq_l //= -Heq_r //= -catA.
destruct arr2 => //=.
have Habsurd: (~~ ((w ^ 2 %/ 2) <= 0)).
rewrite -ltnNge. apply: wordsize_sqrn_div2_gt0.
move: (wildcard'3) => //=. move/negbTE: Habsurd => -> //=.
by rewrite /delete /= drop0.
Qed.
Next Obligation.
rewrite -cats1 size_cat //= size_delete //= addn1 prednK //=.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
Qed.
Next Obligation.
rewrite -cats1 size_cat //= size_delete //= addn1 prednK //=.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
Qed.
Next Obligation.
move: (wildcard'3). rewrite leq_eqVlt.
move/eqP/eqnP/eqP/negbTE: H => -> //=.
have Harr2: 0 < size arr2.
apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
rewrite size_delete //=. move => Hw.
by rewrite -(leq_add2r 1) !addn1 prednK.
Qed.
Next Obligation.
rewrite size_delete. apply: leq_ltn_trans. apply: leq_pred. exact.
apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
Qed.
Next Obligation.
have H': i < size arr1. by rewrite -Heq_anonymous4.
have H1: 0 < size arr1.
move/eqP/eqnP: Heq_anonymous5 => <-. apply: wordsize_sqrn_div2_gt0.
have H2: 0 < size arr2. apply: leq_trans. apply: wordsize_sqrn_div2_gt0. exact.
rewrite -cats1 size_cat //= !size_delete //= addn1 prednK //=.
have H'': (i < size arr1 + size arr2).
apply: leq_trans. apply: H'. apply: leq_addr.
by rewrite H'' -subn1 //= addnBA.
Qed.
Next Obligation.
rewrite -cats1. rewrite /count_one count_cat /=.
have Hmatch: (match arr2 with
| [::] => false
| x :: _ => x
end = access arr2 0). by rewrite /access.
rewrite /access nth_cat -Heq_l /= -Heq_anonymous4 Hmatch.
rewrite eqb_id /access addn0.
rewrite [in RHS]addnC -/access -/count_one.
rewrite -addnBA. rewrite count_delete.
rewrite -[X in _ + _ + X = _]count_delete.
rewrite -addnA subnKC. by rewrite addnC.
apply: access_leq_count. apply: leq_trans.
apply: wordsize_sqrn_div2_gt0. exact.
apply: access_leq_count. by rewrite -Heq_anonymous4.