-
Notifications
You must be signed in to change notification settings - Fork 7
/
quadtree.v
1388 lines (1250 loc) · 56.5 KB
/
quadtree.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
From Equations Require Import Equations.
From Coq Require Import ssreflect ssrbool ssrfun.
From mathcomp Require Import eqtype choice ssrnat div bigop ssrAC seq ssralg.
From favssr Require Import prelude.
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Section RegionQuadtreesGen.
Context {A : Type}.
Inductive qtree A := L of A | Q of qtree A & qtree A & qtree A & qtree A.
Fixpoint height (t : qtree A) : nat :=
match t with
| L _ => 0
| Q t0 t1 t2 t3 => (maxn (maxn (maxn (height t0) (height t1)) (height t2)) (height t3)).+1 (* TODO bigop? *)
end.
Definition select {T} (x y : bool) (a b c d : T) : T :=
if x then
if y then a else b
else
if y then c else d.
Fixpoint get (n : nat) (t : qtree A) (i j : nat) (a0 : A) : A :=
match t, n with
| L a, _ => a
| Q t0 t1 t2 t3, 0 => a0
| Q t0 t1 t2 t3, n'.+1 =>
get n' (select (i < 2 ^ n') (j < 2 ^ n') t0 t1 t2 t3)
(i %% (2 ^ n')) (j %% (2 ^ n')) a0
end.
Lemma get_L n a i j a0 : get n (L a) i j a0 = a.
Proof. by case: n. Qed.
End RegionQuadtreesGen.
Section RegionQuadtrees.
Context {A : eqType}.
Definition same_leaf (t1 t2 : qtree A) : bool :=
match t1, t2 with
| L a1, L a2 => a1 == a2
| _, _ => false
end.
Fixpoint compressed (t : qtree A) : bool :=
match t with
| L _ => true
| Q t0 t1 t2 t3 => [&& compressed t0, compressed t1, compressed t2 & compressed t3] &&
~~ [&& same_leaf t0 t1, same_leaf t0 t2 & same_leaf t0 t3]
end.
Definition Qc (t0 t1 t2 t3 : qtree A) : qtree A :=
match t0, t1, t2, t3 with
| L x0, L x1, L x2, L x3 =>
if [&& x0 == x1, x1 == x2 & x2 == x3] then L x0 else Q t0 t1 t2 t3
| _, _, _, _ => Q t0 t1 t2 t3
end.
(* can be written as: *)
(* if [&& same_leaf t0 t1, same_leaf t0 t2 & same_leaf t0 t3] then t0 else Q t0 t1 t2 t3 *)
Arguments Qc : simpl never.
Lemma compressed_Qc t0 t1 t2 t3 :
compressed t0 -> compressed t1 -> compressed t2 -> compressed t3 ->
compressed (Qc t0 t1 t2 t3).
Proof.
case: t0 => [x0 _|t00 t01 t02 t03] /=; last by case/andP=>->->->->->.
case: t1 => [x1 _|t10 t11 t12 t13] /=; last by case/andP=>->->->->.
case: t2 => [x2 _|t20 t21 t22 t23] /=; last by case/andP=>->->->/=; rewrite andbF.
case: t3 => [x3 _|t30 t31 t32 t33] /=; last by case/andP=>->->/=; rewrite !andbF.
rewrite /Qc; case: ifP=>//= /negbT.
by rewrite !negb_and; case: eqP=>//=->; case: eqP=>//=->.
Qed.
Lemma get_Qc n t0 t1 t2 t3 i j a0 :
height (Q t0 t1 t2 t3) <= n
-> get n (Qc t0 t1 t2 t3) i j a0 = get n (Q t0 t1 t2 t3) i j a0.
Proof.
case: n=>//= n; rewrite ltnS.
case: t0 => [x0|t00 t01 t02 t03] //=; rewrite max0n.
case: t1 => [x1|t10 t11 t12 t13] //=; rewrite max0n.
case: t2 => [x2|t20 t21 t22 t23] //=; rewrite max0n.
case: t3 => [x3|t30 t31 t32 t33] //= _.
rewrite /Qc; case: ifP=>//; case/and3P=>/eqP->/eqP->/eqP->.
by case: ltnP=>_; case: ltnP=>_ /=; case: n.
Qed.
Lemma compressedQD t0 t1 t2 t3 :
compressed (Q t0 t1 t2 t3)
-> [&& compressed t0, compressed t1, compressed t2 & compressed t3].
Proof. by case/andP. Qed.
Lemma height_Qc_Q n t0 t1 t2 t3 :
height t0 <= n -> height t1 <= n -> height t2 <= n -> height t3 <= n
-> height (Qc t0 t1 t2 t3) <= n.+1.
Proof.
case: t0 => /= [x0 H0|t00 t01 t02 t03]; case: t1 => /= [x1 H1|t10 t11 t12 t13];
case: t2 => /= [x2 H2|t20 t21 t22 t23]; case: t3 => /= [x3 H3|t30 t31 t32 t33]; rewrite ?max0n ?maxn0 ?maxnSS ?ltnS //.
- by rewrite /Qc; case: ifP.
- by move=>Ha Hb; rewrite gtn_max Ha.
- by move=>_ Ha; rewrite gtn_max H2.
- by move=>Ha _; rewrite gtn_max H3.
- by move=>Ha Hb Hc; rewrite gtn_max Hc andbT gtn_max Ha.
- by move=>_ Ha; rewrite gtn_max H1.
- by move=>Ha _; rewrite gtn_max H1.
- by move=>_ Ha Hb; rewrite gtn_max Hb andbT gtn_max H1.
- by move=>_ _; rewrite gtn_max H2.
- by move=>Ha _ Hb; rewrite gtn_max Hb andbT gtn_max H2.
- by move=>Ha Hb _; rewrite gtn_max Hb andbT gtn_max H3.
by move=>Ha Hb Hc Hd; rewrite gtn_max Hd andbT gtn_max Hc andbT gtn_max Ha.
Qed.
(* TODO is it useful? *)
Definition sq (n : nat) : rel nat :=
[rel x y | (x < 2 ^ n) && (y < 2 ^ n)].
Definition modify (f : qtree A -> qtree A) (x y : bool) (t0 t1 t2 t3 : qtree A) : qtree A :=
if x then
if y then Qc (f t0) t1 t2 t3 else Qc t0 (f t1) t2 t3
else
if y then Qc t0 t1 (f t2) t3 else Qc t0 t1 t2 (f t3).
Fixpoint put (i j : nat) (a : A) (n : nat) (t : qtree A) : qtree A :=
match n, t with
| 0, _ => L a
| n'.+1, L b =>
modify (put (i %% (2 ^ n')) (j %% (2 ^ n')) a n')
(i < 2 ^ n') (j < 2 ^ n')
(L b) (L b) (L b) (L b)
| n'.+1, Q t0 t1 t2 t3 =>
modify (put (i %% (2 ^ n')) (j %% (2 ^ n')) a n')
(i < 2 ^ n') (j < 2 ^ n')
t0 t1 t2 t3
end.
Lemma height_put i j a n t :
height t <= n -> height (put i j a n t) <= n.
Proof.
elim: n=>//= n IH in i j t *; case: t=>[b _|t0 t1 t2 t3] /=.
- by case: ltnP=>Hi; case: ltnP=>Hj /=; apply: height_Qc_Q=>//; apply: IH.
rewrite !gtn_max !ltnS -!andbA; case/and4P=>H1 H2 H3 H4.
by case: ltnP=>Hi; case: ltnP=>Hj /=; apply: height_Qc_Q=>//=; apply: IH.
Qed.
Lemma compressed_put i j a n t :
height t <= n -> compressed t
-> compressed (put i j a n t).
Proof.
elim: n => //= n IH in i j t *; case: t=>[b _ _|t0 t1 t2 t3] /=.
- by case: ltnP=>Hi; case: ltnP=>Hj /=; apply: compressed_Qc=>//=; apply: IH.
rewrite !gtn_max !ltnS -!andbA; case/and4P=>H1 H2 H3 H4; case/and4P=>Hc1 Hc2 Hc3; case/andP=>Hc4 H.
by case: ltnP=>Hi; case: ltnP=>Hj /=; apply: compressed_Qc=>//=; apply: IH.
Qed.
(* TODO find a better formulation? *)
Lemma get_put n t i j i' j' a0 :
height t <= n -> sq n i j -> sq n i' j'
-> get n (put i j a0 n t) i' j' a0 = if (i' == i) && (j' == j) then a0 else get n t i' j' a0.
Proof.
elim: n=>[|n IH] in i j i' j' t *.
- move=>_; rewrite /sq /= expn0 !ltnS !leqn0.
by do 2!case/andP=>/eqP->/eqP->.
move=>Hn; rewrite /sq; case/andP=>Hi Hj; case/andP=>Hi' Hj'; move: Hn.
case: t=>[b _|t0 t1 t2 t3].
- (* 16 combinations *)
rewrite [get]lock /=; case: ltnP=>Hi0; case: ltnP=>Hj0 /=; unlock;
rewrite get_Qc /= ?maxn0 ?max0n; try by apply: height_put.
- rewrite modn_small // modn_small //.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- rewrite !modn_small // IH //; try by apply/andP.
by case: ifP=>// _; apply: get_L.
- rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
- rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
- rewrite modn_small // mod_minus; last by rewrite Hj0 /= -mul2n -expnS.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
- rewrite modn_small // mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite eqn_sub2rE //; case: ifP=>// _; apply: get_L.
- rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
- rewrite mod_minus; last by rewrite Hi0 /= -mul2n -expnS.
rewrite modn_small //.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
- rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite modn_small // IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite eqn_sub2rE //; case: ifP=>// _; apply: get_L.
rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
rewrite mod_minus; last by rewrite Hi0 /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj0 /= -mul2n -expnS.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- rewrite get_L; case: ifP=>//.
by case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- rewrite get_L; case: ifP=>//.
by case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite !eqn_sub2rE //; case: ifP=>// _; apply: get_L.
rewrite [get]lock /= !gtn_max !ltnS -!andbA; case/and4P=>HH0 HH1 HH2 HH3.
(* another 16 combinations *)
case: ltnP=>Hi0; case: ltnP=>Hj0 /=; unlock;
rewrite get_Qc /= ?gtn_max ?ltnS -?andbA; try by apply/and4P; split=>//; apply: height_put.
- rewrite modn_small // modn_small //.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- by rewrite !modn_small // IH //; apply/andP.
- by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
- by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
- rewrite modn_small // mod_minus; last by rewrite Hj0 /= -mul2n -expnS.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
- rewrite modn_small // mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite eqn_sub2rE.
- by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt Hi0.
- rewrite mod_minus; last by rewrite Hi0 /= -mul2n -expnS.
rewrite modn_small //.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
- rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite modn_small // IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite eqn_sub2rE.
by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt Hj0.
rewrite mod_minus; last by rewrite Hi0 /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj0 /= -mul2n -expnS.
case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- by case: ifP=>//; case/andP=>/eqP Ei _; move: Hi0'; rewrite Ei leqNgt ltnS Hi0.
- by case: ifP=>//; case/andP=>_ /eqP Ej; move: Hj0'; rewrite Ej leqNgt ltnS Hj0.
rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite IH //; try by apply/andP; split=>//; rewrite ltn_subLR // addnn -muln2 -expnSr.
by rewrite !eqn_sub2rE.
Qed.
End RegionQuadtrees.
Section BooleanQuadtrees.
Definition qtb := qtree bool.
Fixpoint inter (t1 t2 : qtb) : qtb :=
match t1 with
| L b1 => if b1 then t2 else L false
| Q s1 s2 s3 s4 =>
match t2 with
| L b2 => if b2 then t1 else L false
| Q t1 t2 t3 t4 => Qc (inter s1 t1) (inter s2 t2) (inter s3 t3) (inter s4 t4)
end
end.
Lemma height_inter t1 t2 : height (inter t1 t2) <= maxn (height t1) (height t2).
Proof.
elim: t1 t2=>[b1|t10 IH0 t11 IH1 t12 IH2 t13 IH3][b2|t20 t21 t22 t23] /=.
- by case: b1.
- by rewrite max0n; case: b1.
- by rewrite maxn0; case: b2.
rewrite maxnSS; apply: height_Qc_Q.
- apply: leq_trans; first by exact: IH0.
by rewrite [X in _<=X](AC (4*4)%AC ((1*5)*(2*3*4*6*7*8))%AC) /=; apply: leq_maxl.
- apply: leq_trans; first by exact: IH1.
by rewrite [X in _<=X](AC (4*4)%AC ((2*6)*(1*3*4*5*7*8))%AC) /=; apply: leq_maxl.
- apply: leq_trans; first by exact: IH2.
by rewrite [X in _<=X](AC (4*4)%AC ((3*7)*(1*2*4*5*6*8))%AC) /=; apply: leq_maxl.
apply: leq_trans; first by exact: IH3.
by rewrite [X in _<=X](AC (4*4)%AC ((4*8)*(1*2*3*5*6*7))%AC) /=; apply: leq_maxl.
Qed.
Lemma get_inter t1 t2 n i j a0 :
height t1 <= n -> height t2 <= n
-> get n (inter t1 t2) i j a0 = get n t1 i j a0 && get n t2 i j a0.
Proof.
elim: t1 t2 n i j=>[b1|t10 IH0 t11 IH1 t12 IH2 t13 IH3][b2|t20 t21 t22 t23] /= n i j.
- by move=>_ _; case: b1; rewrite !get_L.
- by move=>_ H; case: b1; rewrite !get_L.
- by move=>H _; case: b2; rewrite !get_L ?andbT ?andbF.
rewrite !gtn_max -!andbA; case/and4P=>H10 H11 H12 H13; case/and4P=>H20 H21 H22 H23.
rewrite get_Qc /=; last first.
- by rewrite !gtn_max -!andbA; apply/and4P; split; apply: leq_ltn_trans;
(try by exact: height_inter); rewrite gtn_max ?H10 ?H11 ?H12 ?H13.
case: n H10 H11 H12 H13 H20 H21 H22 H23=>//= n.
rewrite !ltnS=>H10 H11 H12 H13 H20 H21 H22 H23.
by case: ltnP=>Hi; case: ltnP=>Hj /=; [rewrite IH0 | rewrite IH1 | rewrite IH2 | rewrite IH3].
Qed.
Lemma compressed_inter t1 t2 :
compressed t1 -> compressed t2
-> compressed (inter t1 t2).
Proof.
elim: t1 t2=>[b1|t10 IH0 t11 IH1 t12 IH2 t13 IH3][b2|t20 t21 t22 t23] /=.
- by move=>_ _; case: b1.
- by move=>_ H; case: b1.
- by move=>H _; case: b2.
case/andP=>/and4P [H10 H11 H12 H13] _; case/andP=>/and4P [H20 H21 H22 H23] _.
by apply: compressed_Qc; [apply: IH0 | apply: IH1 | apply: IH2 | apply: IH3].
Qed.
(* Exercise 13.1 *)
Fixpoint union (t1 t2 : qtb) : qtb := t1. (* FIXME *)
Lemma height_union t1 t2 : height (union t1 t2) <= maxn (height t1) (height t2).
Proof.
Admitted.
Lemma get_union t1 t2 n i j a0 :
maxn (height t1) (height t2) <= n
-> get n (union t1 t2) i j a0 = get n t1 i j a0 || get n t2 i j a0.
Proof.
Admitted.
Lemma compressed_union t1 t2 :
compressed t1 -> compressed t2
-> compressed (union t1 t2).
Proof.
Admitted.
(* this might get in handy *)
Fixpoint negate (t : qtb) : qtb := t. (* FIXME *)
Fixpoint diff (t1 t2 : qtb) : qtb := t1. (* FIXME *)
Lemma height_diff t1 t2 : height (diff t1 t2) <= maxn (height t1) (height t2).
Proof.
Admitted.
Lemma get_diff t1 t2 n i j a0 :
maxn (height t1) (height t2) <= n
-> get n (diff t1 t2) i j a0 = get n t1 i j a0 && ~~ get n t2 i j a0.
Proof.
Admitted.
Lemma compressed_diff t1 t2 :
compressed t1 -> compressed t2
-> compressed (diff t1 t2).
Proof.
Admitted.
End BooleanQuadtrees.
Section RegionQuadtreesMisc.
Context {A : eqType}.
Definition Qf (q : qtree A -> qtree A -> qtree A -> qtree A -> qtree A)
(f : nat -> nat -> qtree A) (i j d : nat) : qtree A :=
q (f i j) (f i (j+d)) (f (i+d) j) (f (i+d) (j+d)).
Equations? get_sq (n : nat) (t : qtree A) (m : nat) (a0 : A) (i j : nat) : qtree A by wf (n + m) lt :=
get_sq n (L b) m a0 i j => L b;
get_sq n t 0 a0 i j => L (get n t i j a0);
get_sq 0 t m'.+1 a0 i j => t;
get_sq n'.+1 (Q t0 t1 t2 t3) m'.+1 a0 i j =>
if (i %% 2 ^ n' + 2 ^ (m'.+1) <= 2 ^ n') && (j %% 2 ^ n' + 2 ^ (m'.+1) <= 2 ^ n')
then get_sq n' (select (i < 2 ^ n') (j < 2 ^ n') t0 t1 t2 t3) (m'.+1) a0 (i %% 2 ^ n') (j %% 2 ^ n')
else Qf Qc (fun x y => get_sq (n'.+1) (Q t0 t1 t2 t3) m' a0 x y) i j (2 ^ m').
Proof. by apply: ssrnat.ltP; rewrite ltn_add2l. Qed.
Lemma height_get_sq n t m a0 i j :
m <= n -> height (get_sq n t m a0 i j) <= m.
Proof.
funelim (get_sq n t m a0 i j); simp get_sq=>//=.
rewrite ltnS=>Hmn; case: ifP; last first.
- by move=>_; apply: height_Qc_Q; apply: H0=>//=; apply: leqW.
case/andP=>Hi Hj; apply: H.
move: Hmn; rewrite leq_eqVlt=>/orP[/eqP E|H] //.
exfalso; move: Hi; rewrite E; apply: negP;
rewrite -ltnNge expnSr muln2 -addnn addnA -[X in X<_]add0n ltn_add2r addn_gt0.
by apply/orP; right; rewrite expn_gt0.
Qed.
Lemma get_get_sq n t m a0 i j i' j' :
height t <= n
-> i + 2 ^ m <= 2 ^ n -> j + 2 ^ m <= 2 ^ n
-> i' < 2 ^ m -> j' < 2 ^ m (* TODO sq? *)
-> get m (get_sq n t m a0 i j) i' j' a0 = get n t (i+i') (j+j') a0.
Proof.
funelim (get_sq n t m a0 i j); simp get_sq=>//.
- by move=>_ _ _ _ _; unlock; rewrite !get_L.
- by unlock; rewrite expn0 !addn1 !ltnS !leqn0=>_ _ _ /eqP {i'}-> /eqP {j'}->; rewrite !addn0.
rewrite [get]lock /= ltnS !geq_max -!andbA; case/and4P=>H1 H2 H3 H4 Hi Hj Hi' Hj'; case: ifP.
- case/andP=>Ha Hb; unlock; rewrite H //=; last by case: ltnP=>Hi0; case: ltnP=>Hj0.
rewrite !modnD; try by rewrite expn_gt0.
have Hi'' : i' < 2 ^ n' by apply: (leq_trans Hi'); apply/leq_trans/Ha; exact: leq_addl.
have Hj'' : j' < 2 ^ n' by apply: (leq_trans Hj'); apply/leq_trans/Hb; exact: leq_addl.
rewrite (modn_small (m := i')) // (modn_small (m := j')) //.
have Hi''' : i %% 2 ^ n' + i' < 2 ^ n' by apply/leq_trans/Ha; rewrite ltn_add2l.
have Hj''' : j %% 2 ^ n' + j' < 2 ^ n' by apply/leq_trans/Hb; rewrite ltn_add2l.
rewrite !(leqNgt (2 ^ n')) Hi''' Hj''' /= mul0n !subn0.
case: ltnP=>Hi0 /=.
- move: Hi'''; rewrite modn_small // =>-> /=.
case: ltnP=>Hj0 /=.
- by move: Hj'''; rewrite modn_small // =>-> /=.
suff: 2 ^ n' <= j + j' by rewrite ltnNge=>->.
by apply: (leq_trans Hj0); apply: leq_addr.
rewrite (ltnNge (i + i')); have ->/=: 2 ^ n' <= i + i' by apply: (leq_trans Hi0); apply: leq_addr.
case: ltnP=>Hj0 /=.
- by move: Hj'''; rewrite modn_small // =>-> /=.
suff: 2 ^ n' <= j + j' by rewrite ltnNge=>->.
by apply: (leq_trans Hj0); apply: leq_addr.
move/negbT; rewrite negb_and -!ltnNge => H'.
unlock; rewrite /Qf get_Qc; last first.
- have Hmn: m' <= n'.+1.
- apply: leq_trans; first by exact: leqnSn.
by rewrite -(leq_exp2l (m:=2)) //; apply/leq_trans/Hi; apply: leq_addl.
by rewrite /= ltnS !geq_max -!andbA; apply/and4P; split; apply: height_get_sq.
rewrite /=; case: ltnP=>Hi0'; case: ltnP=>Hj0' /=.
- rewrite modn_small // modn_small //; apply: H0=>//=.
- by rewrite ltnS !geq_max -!andbA; apply/and4P; split.
- by apply/leq_trans/Hi; rewrite leq_add2l expnS mul2n -addnn; exact: leq_addl.
by apply/leq_trans/Hj; rewrite leq_add2l expnS mul2n -addnn; exact: leq_addl.
- rewrite modn_small // mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite (H0 i (j + 2 ^ m')) //=.
- suff: j + 2 ^ m' + (j' - 2 ^ m') = j + j' by move=>->.
by rewrite addnBA // addnAC addnK.
- by rewrite ltnS !geq_max -!andbA; apply/and4P; split.
- by apply/leq_trans/Hi; rewrite leq_add2l expnS mul2n -addnn; exact: leq_addl.
- by apply/leq_trans/Hj; rewrite -addnA leq_add2l addnn -mul2n -expnS.
by rewrite ltn_subLR // addnn -mul2n -expnS.
- rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite modn_small //.
rewrite (H0 (i + 2 ^ m') j) //=.
- suff: i + 2 ^ m' + (i' - 2 ^ m') = i + i' by move=>->.
by rewrite addnBA // addnAC addnK.
- by rewrite ltnS !geq_max -!andbA; apply/and4P; split.
- by apply/leq_trans/Hi; rewrite -addnA leq_add2l addnn -mul2n -expnS.
- by apply/leq_trans/Hj; rewrite leq_add2l expnS mul2n -addnn; exact: leq_addl.
by rewrite ltn_subLR // addnn -mul2n -expnS.
rewrite mod_minus; last by rewrite Hi0' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj0' /= -mul2n -expnS.
rewrite (H0 (i + 2 ^ m') (j + 2 ^ m')) //=.
- have ->: i + 2 ^ m' + (i' - 2 ^ m') = i + i' by rewrite addnBA // addnAC addnK.
suff: j + 2 ^ m' + (j' - 2 ^ m') = j + j' by move=>->.
by rewrite addnBA // addnAC addnK.
- by rewrite ltnS !geq_max -!andbA; apply/and4P; split.
- by apply/leq_trans/Hi; rewrite -addnA leq_add2l addnn -mul2n -expnS.
- by apply/leq_trans/Hj; rewrite -addnA leq_add2l addnn -mul2n -expnS.
- by rewrite ltn_subLR // addnn -mul2n -expnS.
by rewrite ltn_subLR // addnn -mul2n -expnS.
Qed.
Lemma compressed_get_sq n t m a0 i j :
height t <= n -> compressed t
-> compressed (get_sq n t m a0 i j).
Proof.
funelim (get_sq n t m a0 i j); simp get_sq=>//=.
rewrite ltnS !geq_max -!andbA; case/and4P=>H1 H2 H3 H4.
case/and5P=>[H10 H11 H12 H13 N]; case: ifP.
- by case/andP => Hi Hj; apply: H; case: ltnP=>Hi0; case: ltnP=>Hj0.
move/negbT; rewrite negb_and -!ltnNge=>H'.
by rewrite /Qf; apply: compressed_Qc; apply: H0=>//=; (try by rewrite ltnS !geq_max -!andbA; apply/and4P);
apply/andP; split=>//; apply/and4P.
Qed.
Definition mx A := seq (seq A).
Definition sq_mx (n : nat) (m : mx A) : bool :=
(size m == 2 ^ n) && all (fun l => size l == 2 ^ n) m.
Definition Qmx (mx0 mx1 mx2 mx3 : mx A) : mx A :=
map2 cat mx0 mx1 ++ map2 cat mx2 mx3.
Fixpoint mx_of (t : qtree A) (n : nat) : mx A :=
match t with
| L x => nseq (2 ^ n) (nseq (2 ^ n) x)
| Q t0 t1 t2 t3 =>
match n with
| 0 => [::]
| n'.+1 => Qmx (mx_of t0 n') (mx_of t1 n') (mx_of t2 n') (mx_of t3 n')
end
end.
Lemma nth_Qmx_select (mx0 mx1 mx2 mx3 : mx A) n i j a0 :
sq_mx n mx0 -> sq_mx n mx1 -> sq_mx n mx2 -> sq_mx n mx3
-> i < 2 ^ n.+1 -> j < 2 ^ n.+1 (* TODO sq? *)
-> nth a0 (nth [::] (Qmx mx0 mx1 mx2 mx3) i) j = nth a0 (nth [::] (select (i < 2^n) (j < 2^n) mx0 mx1 mx2 mx3) (i %% 2^n)) (j %% 2^n).
Proof.
case/andP=>/eqP H00 H01; case/andP=>/eqP H10 H11; case/andP=>/eqP H20 H21; case/andP=>/eqP H30 H31 Hi Hj.
rewrite /Qmx nth_cat size_map2 H00 H10 minnn.
case: ltnP=>Hi' /=.
- rewrite modn_small // (@nth_map _ ([::],[::])); last first.
- by rewrite size_zip H00 H10 minnn.
rewrite nth_zip; last by rewrite H00.
rewrite nth_cat.
have->: size (nth [::] mx0 i) = 2 ^ n.
- by apply/eqP; move/allP: H01; apply; apply: mem_nth; rewrite H00.
case: ltnP=>Hj'; first by rewrite modn_small.
by rewrite mod_minus // Hj' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hi' /= -mul2n -expnS.
rewrite (@nth_map _ ([::],[::])); last first.
- by rewrite size_zip H20 H30 minnn ltn_subLR // addnn -mul2n -expnS.
rewrite nth_zip; last by rewrite H20.
rewrite nth_cat.
have->: size (nth [::] mx2 (i - 2 ^ n)) = 2 ^ n.
- apply/eqP; move/allP: H21; apply; apply: mem_nth; rewrite H20.
by rewrite ltn_subLR // addnn -mul2n -expnS.
case: ltnP=>Hj'; first by rewrite modn_small.
by rewrite mod_minus // Hj' /= -mul2n -expnS.
Qed.
Lemma sq_mx_mx_of n t :
height t <= n -> sq_mx n (mx_of t n).
Proof.
elim: t n=>[x|t0 IH0 t1 IH1 t2 IH2 t3 IH3] n /=.
- move=>_; apply/andP; split; first by rewrite size_nseq.
apply/allP=>l; rewrite mem_nseq; case/andP=>_ /eqP->.
by rewrite size_nseq.
rewrite !gtn_max -!andbA; case/and4P; case: n=>// n.
rewrite !ltnS => /IH0 /andP {IH0}[/eqP H00 H01] /IH1 /andP {IH1}[/eqP H10 H11]
/IH2 /andP {IH2}[/eqP H20 H21] /IH3 /andP {IH3}[/eqP H30 H31].
rewrite /Qmx; apply/andP; split.
- by rewrite size_cat !size_map2 H00 H10 H20 H30 minnn addnn -mul2n -expnS.
rewrite all_cat; apply/andP; split.
- rewrite all_map; apply/allP; case=>l1 l2 /= /in_zip [H1 H2]; rewrite size_cat.
move/allP: H01 =>/(_ _ H1)/eqP->; move/allP: H11 =>/(_ _ H2)/eqP->.
by rewrite addnn -mul2n -expnS.
rewrite all_map; apply/allP; case=>l1 l2 /= /in_zip [H1 H2]; rewrite size_cat.
move/allP: H21 =>/(_ _ H1)/eqP->; move/allP: H31 =>/(_ _ H2)/eqP->.
by rewrite addnn -mul2n -expnS.
Qed.
Lemma mx_of_get n t i j a0 :
height t <= n
-> i < 2 ^ n -> j < 2 ^ n (* TODO sq? *)
-> nth a0 (nth [::] (mx_of t n) i) j = get n t i j a0.
Proof.
elim: t n i j=>[x|t0 IH0 t1 IH1 t2 IH2 t3 IH3] n i j /=.
- by move=>_ Hi Hj; rewrite nth_nseq Hi nth_nseq Hj get_L.
case: n=>// n; rewrite ltnS !geq_max -!andbA; case/and4P=>H0 H1 H2 H3 Hi Hj /=.
rewrite (nth_Qmx_select (n:=n)) //; try by apply: sq_mx_mx_of.
case: ltnP=>Hi'; case: ltnP=>Hj' /=.
- by rewrite modn_small // modn_small //; apply: IH0.
- rewrite modn_small // mod_minus; last by rewrite Hj' /= -mul2n -expnS.
by rewrite IH1 // ltn_subLR // addnn -mul2n -expnS.
- rewrite mod_minus; last by rewrite Hi' /= -mul2n -expnS.
by rewrite modn_small // IH2 // ltn_subLR // addnn -mul2n -expnS.
rewrite mod_minus; last by rewrite Hi' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj' /= -mul2n -expnS.
by rewrite IH3 // ltn_subLR // addnn -mul2n -expnS.
Qed.
Definition decomp (n : nat) (m : mx A) : mx A * mx A * mx A * mx A :=
let mx01 := take (2 ^ n) m in
let mx23 := drop (2 ^ n) m in
(map (take (2 ^ n)) mx01, map (drop (2 ^ n)) mx01, map (take (2 ^ n)) mx23, map (drop (2 ^ n)) mx23).
Fixpoint qt_of (n: nat) (m : mx A) (a0 : A) : qtree A :=
match n with
| 0 => L (nth a0 (nth [::] m 0) 0)
| n'.+1 =>
let '(mx0, mx1, mx2, mx3) := decomp n' m in
Qc (qt_of n' mx0 a0) (qt_of n' mx1 a0) (qt_of n' mx2 a0) (qt_of n' mx3 a0)
end.
Lemma sq_decomp n m :
sq_mx n.+1 m
-> let '(mx0, mx1, mx2, mx3) := decomp n m in
[/\ sq_mx n mx0, sq_mx n mx1, sq_mx n mx2 & sq_mx n mx3].
Proof.
have H1': 2 ^ n < 2 ^ n.+1
by rewrite expnSr muln2 -addnn -[X in X<_]add0n ltn_add2r expn_gt0.
have H2': 2 ^ n.+1 - 2 ^ n = 2 ^ n
by rewrite expnSr muln2 -addnn addnK.
case/andP => /eqP H0 /allP H1; rewrite /decomp; split; apply/andP; split.
- by rewrite size_map size_take H0 H1'.
- apply/allP=> l /= /mapP [x] /mem_take /H1 /eqP Hx {l}->.
by rewrite size_take Hx H1'.
- by rewrite size_map size_take H0 H1'.
- apply/allP=> l /= /mapP [x] /mem_take /H1 /eqP Hx {l}->.
by rewrite size_drop Hx H2'.
- by rewrite size_map size_drop H0 H2'.
- apply/allP=> l /= /mapP [x] /mem_drop /H1 /eqP Hx {l}->.
by rewrite size_take Hx H1'.
- by rewrite size_map size_drop H0 H2'.
- apply/allP=> l /= /mapP [x] /mem_drop /H1 /eqP Hx {l}->.
by rewrite size_drop Hx H2'.
Qed.
Lemma height_qt_of n m a0 :
sq_mx n m -> height (qt_of n m a0) <= n.
Proof.
elim: n m=>[|n IH] m //= /sq_decomp; rewrite /decomp; case=>H1 H2 H3 H4.
by apply: height_Qc_Q; apply: IH.
Qed.
Lemma get_qt_of n m a0 i j :
sq_mx n m
-> i < 2 ^ n -> j < 2 ^ n (* TODO sq? *)
-> get n (qt_of n m a0) i j a0 = nth a0 (nth [::] m i) j.
Proof.
elim: n m i j=>[|n IH] m i j.
- by move=>_ /=; rewrite expn0 !ltnS !leqn0=>/eqP -> /eqP ->.
have H1': 2 ^ n < 2 ^ n.+1
by rewrite expnSr muln2 -addnn -[X in X<_]add0n ltn_add2r expn_gt0.
have H2': 2 ^ n.+1 - 2 ^ n = 2 ^ n
by rewrite expnSr muln2 -addnn addnK.
move=>/[dup]/andP [/eqP Hs /allP Ha] /sq_decomp [H1 H2 H3 H4] Hi Hj; rewrite [get]lock /=; unlock.
rewrite get_Qc /=; last first.
- by rewrite ltnS !geq_max -!andbA; apply/and4P; split; apply: height_qt_of.
case: ltnP=>Hi'; case: ltnP=>Hj' /=; rewrite IH //; try by rewrite ltn_mod expn_gt0.
- rewrite modn_small // modn_small //.
rewrite (@nth_map _ [::]); last by rewrite size_take Hs H1'.
by rewrite !nth_take.
- rewrite modn_small // mod_minus; last by rewrite Hj' /= -mul2n -expnS.
rewrite (@nth_map _ [::]); last by rewrite size_take Hs H1'.
rewrite nth_take // nth_drop; congr (nth a0 (nth [::] m i)).
by rewrite addnBCA // subnn addn0.
- rewrite mod_minus; last by rewrite Hi' /= -mul2n -expnS.
rewrite modn_small //.
rewrite (@nth_map _ [::]); last first.
- rewrite size_drop Hs H2' ltn_psubLR; last by rewrite expn_gt0.
by rewrite addnn -mul2n -expnS.
rewrite nth_drop nth_take //.
suff: 2 ^ n + (i - 2 ^ n) = i by move =>->.
by rewrite addnBCA // subnn addn0.
rewrite mod_minus; last by rewrite Hi' /= -mul2n -expnS.
rewrite mod_minus; last by rewrite Hj' /= -mul2n -expnS.
rewrite (@nth_map _ [::]); last first.
- rewrite size_drop Hs H2' ltn_psubLR; last by rewrite expn_gt0.
by rewrite addnn -mul2n -expnS.
rewrite !nth_drop.
suff: 2 ^ n + (i - 2 ^ n) = i /\ 2 ^ n + (j - 2 ^ n) = j by case=>->->.
by split; rewrite addnBCA // subnn addn0.
Qed.
Lemma compressed_qt_of n m a0 :
sq_mx n m -> compressed (qt_of n m a0).
Proof.
elim: n m=>[|n IH] m //= /sq_decomp; rewrite /decomp; case=>H1 H2 H3 H4.
by apply: compressed_Qc; apply: IH.
Qed.
(* Exercise 13.2 *)
Definition shift_mx (f : nat -> nat -> A) (x y i j : nat) : A := f (i+x) (j+y).
Fixpoint qt_of_fun (f : nat -> nat -> A) (n : nat) : qtree A := L (f 0 0). (* FIXME *)
Lemma height_qt_of_fun f n :
height (qt_of_fun f n) <= n.
Proof.
Admitted.
Lemma get_qt_of_fun f n i j a0 :
i < 2 ^ n -> j < 2 ^ n (* TODO sq? *)
-> get n (qt_of_fun f n) i j a0 = f i j.
Proof.
Admitted.
End RegionQuadtreesMisc.
(* we can use arbitrary rings instead of reals *)
(* TODO weaken to semirings after upgrading Mathcomp? *)
Section MatrixQuadtrees.
Context {A : ringType}.
Open Scope ring_scope.
Import GRing.Theory.
(* mathcomp has a similar definition of matrices but it's indexed by finite ordinals *)
Definition ma : Type := nat -> nat -> A.
Definition sq_ma (n : nat) (a : ma) : Prop :=
forall i j, 2 ^ n <= i \/ 2 ^ n <= j -> a i j = 0.
Definition mk_sq (n : nat) (m : ma) : ma :=
fun i j => if (i < 2 ^ n) && (j < 2 ^ n) then m i j else 0.
Definition D (n : nat) (x : A) : ma :=
mk_sq n (fun i j => if i == j then x else 0).
Definition Qma (n : nat) (a b c d : ma) : ma :=
fun i j =>
let m := 2 ^ n in
if i < m then
if j < m then a i j
else b i (j - m)%N
else
if j < m then c (i - m)%N j
else d (i - m)%N (j - m)%N.
Lemma D1_Qma n x :
D n.+1 x = Qma n (D n x) (D n 0) (D n 0) (D n x).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /D /Qma /mk_sq /= !ltn_psubLR; try by rewrite expn_gt0.
rewrite addnn -mul2n -expnS; case: ifP.
- case/andP=>Hi Hj; case: ltnP=>Hi'; case: ltnP=>Hj' //=; last by rewrite eqn_sub2rE.
- rewrite !if_same; case: eqP=>// E; exfalso.
by rewrite leqNgt -E Hi' in Hj'.
rewrite !if_same; case: eqP=>// E; exfalso.
by rewrite leqNgt E Hj' in Hi'.
move=>H; rewrite !if_same; case: ltnP=>Hi' //=; case: ltnP=>Hj' //=.
exfalso; move/negP: H; apply; apply/andP; split.
- by apply: (leq_trans Hi'); rewrite expnS mul2n -addnn; exact: leq_addl.
by apply: (leq_trans Hj'); rewrite expnS mul2n -addnn; exact: leq_addl.
Qed.
Fixpoint mabs (n : nat) (q : qtree A) : ma :=
match q with
| L x => D n x
| Q t0 t1 t2 t3 =>
match n with
| 0 => D 0 0
| n'.+1 => Qma n' (mabs n' t0) (mabs n' t1) (mabs n' t2) (mabs n' t3)
end
end.
Lemma mabsL n x : mabs n (L x) = D n x.
Proof. by case: n. Qed.
Definition add_ma (a b : ma) : ma :=
fun i j => a i j + b i j.
Definition mul_ma (n : nat) (a b : ma) : ma :=
fun i j => \sum_(0 <= k < 2 ^ n) (a i k * b k j). (* TODO can we get rid of 0 <= ? *)
Lemma add_ma_D n x y : add_ma (D n x) (D n y) = D n (x + y).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /D /mk_sq /=; case: ifP=>_; last by rewrite addr0.
by case: ifP=>// _; rewrite addr0.
Qed.
Lemma add_ma_D0l n a : add_ma (D n 0) a = a.
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /D /mk_sq /=; case: ifP=>_; last by rewrite add0r.
by rewrite if_same add0r.
Qed.
Lemma add_ma_D0r n a : add_ma a (D n 0) = a.
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /D /mk_sq /=; case: ifP=>_; last by rewrite addr0.
by rewrite if_same addr0.
Qed.
Lemma mul_ma_D0l n a : mul_ma n (D n 0) a = D n 0.
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /mul_ma /D /mk_sq /=; rewrite !if_same.
have E: \sum_(0 <= k < 2 ^ n) 0 = 0 :> A
by rewrite big_const_nat subn0; apply: iter_fix; rewrite addr0.
by rewrite -{}[RHS]E; apply: eq_big_nat=> k _; rewrite !if_same mul0r.
Qed.
Lemma mul_ma_D0r n a : mul_ma n a (D n 0) = D n 0.
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /mul_ma /D /mk_sq /=; rewrite !if_same.
have E: \sum_(0 <= k < 2 ^ n) 0 = 0 :> A
by rewrite big_const_nat subn0; apply: iter_fix; rewrite addr0.
by rewrite -{}[RHS]E; apply: eq_big_nat=> k _; rewrite !if_same mulr0.
Qed.
Lemma mul_ma_D n x y : mul_ma n (D n x) (D n y) = D n (x * y).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
have E: forall a b, \sum_(a <= k < b) 0 = 0 :> A.
by move=>a b; rewrite big_const_nat; apply: iter_fix; rewrite addr0.
rewrite /mul_ma /D /mk_sq /=; case: ifP; last first.
- move/negbT; rewrite negb_and=>H.
rewrite -{}[RHS](E 0%N (2 ^ n)); apply: eq_big_nat=> k /andP [_ ->] /=; rewrite andbT.
by case/orP: H=>/negbTE->; [rewrite mul0r | rewrite mulr0].
- case/andP => Hi Hj; rewrite Hi Hj /=; case: ifP=>/eqP Ei; last first.
- rewrite -{}[RHS](E 0%N (2 ^ n)); apply: eq_big_nat=> k /andP [_ ->] /=.
case: ifP=>[/eqP<-|Ni]; last by rewrite mul0r.
by move/eqP: Ei=>/negbTE->; rewrite mulr0.
rewrite -{j Hj}Ei.
have E': \sum_(0 <= k < 2 ^ n) (if k == i then x * y else 0) = x * y.
- rewrite (@big_cat_nat _ _ _ i) //=; last by apply: ltnW.
rewrite -[RHS]add0r; congr (_ + _).
- rewrite -{}[RHS](E 0%N i); apply: eq_big_nat=> k /andP [_ Hk] /=.
by rewrite ltn_eqF.
rewrite (@big_cat_nat _ _ _ i.+1) //= big_nat1 eqxx -[RHS]addr0; congr (x * y + _).
rewrite -{}[RHS](E i.+1 (2 ^ n)); apply: eq_big_nat=> k /andP [Hk _] /=.
by rewrite gtn_eqF.
rewrite -E'; apply: eq_big_nat=> k /andP [_ ->] /=.
case: ifP=>[/eqP<-|N]; first by rewrite eqxx.
by rewrite mul0r eq_sym N.
Qed.
Lemma add_ma_Qma n a1 b1 c1 d1 a2 b2 c2 d2 :
add_ma (Qma n a1 b1 c1 d1) (Qma n a2 b2 c2 d2) = Qma n (add_ma a1 a2) (add_ma b1 b2) (add_ma c1 c2) (add_ma d1 d2).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
by rewrite /add_ma /Qma /mk_sq /=; case: ifP=>Hi; case: ifP=>Hj.
Qed.
Lemma add_ma_D_Qma n x a b c d :
add_ma (D n.+1 x) (Qma n a b c d) = Qma n (add_ma (D n x) a) b c (add_ma (D n x) d).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /D /Qma /mk_sq /= !ltn_psubLR; try by rewrite expn_gt0.
rewrite addnn -mul2n -expnS; case: ifP.
- case/andP=>Hi Hj.
- case: ltnP=>Hi'; case: ltnP=>Hj' //=; last by rewrite eqn_sub2rE.
- case: eqP=>[E|N]; last by rewrite add0r.
by rewrite leqNgt -E Hi' in Hj'.
case: eqP=>[E|N]; last by rewrite add0r.
by rewrite leqNgt E Hj' in Hi'.
move=>H; rewrite add0r; case: ltnP=>Hi; case: ltnP=>Hj //=; last by rewrite add0r.
exfalso; move/negP: H; apply; apply/andP; split.
- by apply: (leq_trans Hi); rewrite expnS mul2n -addnn; exact: leq_addl.
by apply: (leq_trans Hj); rewrite expnS mul2n -addnn; exact: leq_addl.
Qed.
Lemma add_ma_Qma_D n x a b c d :
add_ma (Qma n a b c d) (D n.+1 x) = Qma n (add_ma a (D n x)) b c (add_ma d (D n x)).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /D /Qma /mk_sq /= !ltn_psubLR; try by rewrite expn_gt0.
rewrite addnn -mul2n -expnS. case: (ifP ((i < 2 ^ n.+1) && (j < 2 ^ n.+1))).
- case/andP=>Hi Hj.
- case: ltnP=>Hi'; case: ltnP=>Hj' //=; last by rewrite eqn_sub2rE.
- case: eqP=>[E|N]; last by rewrite addr0.
by rewrite leqNgt -E Hi' in Hj'.
case: eqP=>[E|N]; last by rewrite addr0.
by rewrite leqNgt E Hj' in Hi'.
move=>H; rewrite addr0; case: ltnP=>Hi; case: ltnP=>Hj //=; last by rewrite addr0.
exfalso; move/negP: H; apply; apply/andP; split.
- by apply: (leq_trans Hi); rewrite expnS mul2n -addnn; exact: leq_addl.
by apply: (leq_trans Hj); rewrite expnS mul2n -addnn; exact: leq_addl.
Qed.
Lemma mult_ma_Qma_Qma n a1 b1 c1 d1 a2 b2 c2 d2 :
mul_ma n.+1 (Qma n a1 b1 c1 d1) (Qma n a2 b2 c2 d2) =
Qma n (add_ma (mul_ma n a1 a2) (mul_ma n b1 c2))
(add_ma (mul_ma n a1 b2) (mul_ma n b1 d2))
(add_ma (mul_ma n c1 a2) (mul_ma n d1 c2))
(add_ma (mul_ma n c1 b2) (mul_ma n d1 d2)).
Proof.
apply/functional_extensionality=>i; apply/functional_extensionality=>j.
rewrite /add_ma /mul_ma /Qma /mk_sq /= (@big_cat_nat _ _ _ (2 ^ n)) //=; last by rewrite expnS mul2n -addnn; exact: leq_addr.
case: ltnP=>Hi; case: ltnP=>Hj.
(* TODO repetition *)
- congr (_ + _); first by apply: eq_big_nat=> k /andP [_ ->].
rewrite -{1}(add0n (2 ^ n)) big_addn expnS mul2n -addnn addnK.
by apply: eq_big_nat=> k _; rewrite ltnNge leq_addl /= addnK.
- congr (_ + _); first by apply: eq_big_nat=> k /andP [_ ->].
rewrite -{1}(add0n (2 ^ n)) big_addn expnS mul2n -addnn addnK.
by apply: eq_big_nat=> k _; rewrite ltnNge leq_addl /= addnK.
- congr (_ + _); first by apply: eq_big_nat=> k /andP [_ ->].
rewrite -{1}(add0n (2 ^ n)) big_addn expnS mul2n -addnn addnK.
by apply: eq_big_nat=> k _; rewrite ltnNge leq_addl /= addnK.
congr (_ + _); first by apply: eq_big_nat=> k /andP [_ ->].
rewrite -{1}(add0n (2 ^ n)) big_addn expnS mul2n -addnn addnK.
by apply: eq_big_nat=> k _; rewrite ltnNge leq_addl /= addnK.
Qed.
Definition QcM (q1 q2 q3 q4 : qtree A) : qtree A :=
match q1, q2, q3, q4 with
| L x0, L x1, L x2, L x3 =>
if [&& x1 == 0, x2 == 0 & x0 == x3] then L x0 else Q (L x0) (L x1) (L x2) (L x3)
| _, _, _, _ => Q q1 q2 q3 q4
end.
Lemma mabs_QcM n t0 t1 t2 t3 :
mabs n.+1 (QcM t0 t1 t2 t3) = mabs n.+1 (Q t0 t1 t2 t3).
Proof.
case: t0 t1 t2 t3=>[x0|x0 x1 x2 x3][y0|y0 y1 y2 y3][z0|z0 z1 z2 z3][w0|w0 w1 w2 w3] //=.
case: ifP=>// /and3P [/eqP-> /eqP -> /eqP <-].
by rewrite D1_Qma // !mabsL.
Qed.
Lemma height_QcM_Q t0 t1 t2 t3 :
height (QcM t0 t1 t2 t3) <= height (Q t0 t1 t2 t3).
Proof.
case: t0 t1 t2 t3=>[x0|x0 x1 x2 x3][y0|y0 y1 y2 y3][z0|z0 z1 z2 z3][w0|w0 w1 w2 w3] //=.
by rewrite !maxn0; case: ifP.
Qed.
Fixpoint compressedM (q : qtree A) : bool :=
match q with
| L _ => true
| Q (L x0) (L x1) (L x2) (L x3) => ~~ [&& x1 == 0, x2 == 0 & x0 == x3]
| Q t0 t1 t2 t3 => [&& compressedM t0, compressedM t1, compressedM t2 & compressedM t3]
end.
Lemma compressedM_Q t0 t1 t2 t3 :
compressedM (Q t0 t1 t2 t3)
-> [&& compressedM t0, compressedM t1, compressedM t2 & compressedM t3].
Proof.
by case: t0 t1 t2 t3=>[x0|x0 x1 x2 x3][y0|y0 y1 y2 y3][z0|z0 z1 z2 z3][w0|w0 w1 w2 w3].
Qed.
Lemma compressed_QcM t0 t1 t2 t3 :
compressedM (QcM t0 t1 t2 t3) = [&& compressedM t0, compressedM t1, compressedM t2 & compressedM t3].
Proof.
case: t0 t1 t2 t3=>[x0|x0 x1 x2 x3][y0|y0 y1 y2 y3][z0|z0 z1 z2 z3][w0|w0 w1 w2 w3] //=; rewrite ?andbT -?andbA //.
by case: ifP=>//= ->.
Qed.
Equations? add_qtr (a b : qtree A) : qtree A by wf (height a + height b)%N lt :=
add_qtr (L x) (L y) => L (x + y);
add_qtr (L x) (Q b0 b1 b2 b3) => QcM (add_qtr (L x) b0) b1 b2 (add_qtr (L x) b3);
add_qtr (Q a0 a1 a2 a3) (L y) => QcM (add_qtr a0 (L y)) a1 a2 (add_qtr a3 (L y));
add_qtr (Q a0 a1 a2 a3) (Q b0 b1 b2 b3) => QcM (add_qtr a0 b0) (add_qtr a1 b1) (add_qtr a2 b2) (add_qtr a3 b3).
Proof.
all: apply: ssrnat.ltP; rewrite ?addn0 ?add0n ?addSn ltnS.
- by rewrite -!maxnA; exact: leq_maxl.
- by exact: leq_maxr.
- by rewrite -!maxnA; exact: leq_maxl.
- by exact: leq_maxr.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add;
by rewrite -!maxnA; exact: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add;
by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add;
by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
apply: ltnW; rewrite addnS ltnS; apply: leq_add;
by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
Qed.
Equations? mul_qtr (a b : qtree A) : qtree A by wf (height a + height b)%N lt :=
mul_qtr (L x) (L y) => L (x * y);
mul_qtr (L x) (Q b0 b1 b2 b3) => QcM (mul_qtr (L x) b0) (mul_qtr (L x) b1) (mul_qtr (L x) b2) (mul_qtr (L x) b3);
mul_qtr (Q a0 a1 a2 a3) (L y) => QcM (mul_qtr a0 (L y)) (mul_qtr a1 (L y)) (mul_qtr a2 (L y)) (mul_qtr a3 (L y));
mul_qtr (Q a0 a1 a2 a3) (Q b0 b1 b2 b3) => QcM (add_qtr (mul_qtr a0 b0) (mul_qtr a1 b2))
(add_qtr (mul_qtr a0 b1) (mul_qtr a1 b3))
(add_qtr (mul_qtr a2 b0) (mul_qtr a3 b2))
(add_qtr (mul_qtr a2 b1) (mul_qtr a3 b3)).
Proof.
all: apply: ssrnat.ltP; rewrite ?addn0 ?add0n ?addSn ltnS.
- by rewrite -!maxnA; exact: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
- by rewrite -!maxnA; exact: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
- by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add;
by rewrite -!maxnA; exact: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite -!maxnA; exact: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
by rewrite -!maxnA; exact: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
- apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (3*(1*2*4))%AC) /=; apply: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (2*(1*3*4))%AC) /=; apply: leq_maxl.
apply: ltnW; rewrite addnS ltnS; apply: leq_add.
- by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
by rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; apply: leq_maxl.
Qed.
Lemma height_add s t : height (add_qtr s t) <= maxn (height s) (height t).
Proof.
funelim (add_qtr s t); simp add_qtr => //=; rewrite ?max0n ?maxn0 /= in H H0 *.
- apply: leq_trans; first by exact: height_QcM_Q.
rewrite /= ltnS geq_max; apply/andP; split; last first.
- by apply: (leq_trans H0); rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; exact: leq_maxl.
rewrite -![X in X<=_]maxnA geq_max; apply/andP; split.
- by apply: (leq_trans H); rewrite -!maxnA; exact: leq_maxl.
by rewrite [X in _<=X](AC 4%AC ((2*3)*(1*4))%AC) /=; exact: leq_maxl.
- apply: leq_trans; first by exact: height_QcM_Q.
rewrite /= ltnS geq_max; apply/andP; split; last first.
- by apply: (leq_trans H0); rewrite [X in _<=X](AC 4%AC (4*(1*2*3))%AC) /=; exact: leq_maxl.
rewrite -![X in X<=_]maxnA geq_max; apply/andP; split.
- by apply: (leq_trans H); rewrite -!maxnA; exact: leq_maxl.
by rewrite [X in _<=X](AC 4%AC ((2*3)*(1*4))%AC) /=; exact: leq_maxl.
apply: leq_trans; first by exact: height_QcM_Q.
rewrite /= maxnSS ltnS !geq_max -!andbA; apply/and4P; split.
- by apply: (leq_trans H); rewrite [X in _<=X](AC (4*4)%AC ((1*5)*(2*3*4*6*7*8))%AC) /=; exact: leq_maxl.
- by apply: (leq_trans H0); rewrite [X in _<=X](AC (4*4)%AC ((2*6)*(1*3*4*5*7*8))%AC) /=; exact: leq_maxl.
- by apply: (leq_trans H1); rewrite [X in _<=X](AC (4*4)%AC ((3*7)*(1*2*4*5*6*8))%AC) /=; exact: leq_maxl.
by apply: (leq_trans H2); rewrite [X in _<=X](AC (4*4)%AC ((4*8)*(1*2*3*5*6*7))%AC) /=; exact: leq_maxl.
Qed.