-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnionFind.v
1840 lines (1703 loc) · 81.8 KB
/
UnionFind.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
Require Import RGref.DSL.DSL.
Require Import RGref.DSL.Concurrency.
Require Import RGref.DSL.Fields.
Require Import FinResults.
Require Import Utf8.
(** * Lock-Free Linearizable Union-Find
We're following Anderson and Woll's STOC'91 paper
"Wait-free Parallel Algorithms for the Union Find Problem."
*)
(** ** Basic structures, field maps *)
Inductive cell (n:nat) : Set :=
| mkCell : nat -> Fin.t n -> cell n.
Instance ir_cell {n:nat} : ImmediateReachability (cell n) :=
{ imm_reachable_from_in := fun T P R G r x => False }.
Instance foldcell {n:nat} : rel_fold (cell n) :=
{ rgfold := fun _ _ => cell n;
fold := fun _ _ x => x }.
Instance containcell {n:nat} : Containment (cell n) :=
{ contains := fun _ => True }.
Definition uf (n:nat) := Array n (ref{cell n|any}[local_imm,local_imm]).
Inductive F : Set := rank | parent.
Instance fielding {n:nat} : FieldTyping (cell n) F.
Instance cell_rank {n:nat} : FieldType (cell n) F rank nat :=
{ getF := fun x => match x with mkCell r p => r end;
setF := fun x v => match x with mkCell r p => mkCell _ v p end }.
Instance cell_parent {n:nat} : FieldType (cell n) F parent (Fin.t n) :=
{ getF := fun x => match x with mkCell r p => p end;
setF := fun x v => match x with mkCell r p => mkCell _ r v end }.
(** ** Useful predicates and properties of the union-find array structure *)
Inductive terminating_ascent (n:nat) (x:uf n) (h:heap) (i:Fin.t n) : Prop :=
| self_ascent : (getF (h[x <| i |>])) = i ->
terminating_ascent n x h i
| trans_ascent : forall t,
t = (getF (h[x <| i |>])) ->
(getF (h[x <| i |>])) ≤ (getF (h[x <| t |>])) ->
(@eq nat (getF (h[x <| i |>])) (getF (h[x <| t |>])) ->
proj1_sig (to_nat i) < proj1_sig (to_nat t)) ->
terminating_ascent n x h t ->
terminating_ascent n x h i.
Inductive chase (n:nat) (x:uf n) (h:heap) (i : Fin.t n) : Fin.t n -> Prop :=
| self_chase : (*(getF (h[x<|i|>])) = i ->*)
chase n x h i i
| trans_chase : forall t f,
chase n x h t f ->
(getF (h[x<|i|>])) = t ->
chase n x h i f
.
Inductive φ (n:nat) : hpred (uf n) :=
pfφ : forall x h,
(forall i, terminating_ascent n x h i) ->
φ n x h.
(** ** Change relations and meta properties. *)
Inductive δ (n:nat) : hrel (uf n) :=
| path_compression : forall x f c h h' (rt:Fin.t n),
φ n x h ->
(*root n x h f rt ->
root n x h (getF (h[c])) rt ->*)
(* The chase assumption means we're not permuting reachability,
which means we're not introducing a cycle. It also implies the
increasing rank. *)
@eq nat (getF (h[x <| f |>])) (getF (h[c])) -> (* preserve rank *)
(* This old chase theory is too strong. consider reading the grandparent for a CAS, then the parent is looked up and completely bumped (so the links now skip the was-grandparent). Then the CAS on the original cell would succeed, but actually extend the path at the moment of the write (subsequently it would be compressed again by the parent-lookup process, but... *)
(*chase n x h f (getF (h[c])) -> *)
(* Instead, we need to
1) prevent cycles (i.e. not h[c].parent-->f)
2) at least perform an update within the same set (common ancestor for h[c].parent and f)
Since not h[c].parent-->f, h[c].parent's path to the root is not affected by updating f,
f's new path is f-->h[c].parent-->...->root, and anything reaching f is <whatever>-->f->...
Thus terminating_ascent is preserved.
We require (2) to ensure we don't spuriously munge sets...
Note that this permits /extending/ the path to the root! This is true of the original paper as well.
We may do all of the reads for the compression CAS on an element i, and just before the CAS, several other
threads perform lookups on the parent, and each completes /the parent's/ *first* compression. This moves the parent
several links closer to the root, without affecting the (original) grandparent-of-i's distance from the root.
Then the CAS in the original lookup of i succeeds, swapping i's parent from the original parent (now much closer to the root)
to the original grandparent, which is (momentarily) further from the root than the original parent!
*)
(exists Y, ~chase n x h (getF (h[c])) f /\
chase n x h (getF (h[c])) Y /\
chase n x h f Y
) ->
getF (h[c]) ≤ getF (h[x<|(getF (h[c]))|>]) ->
(@eq nat (getF (h[c])) (getF (h[x<|(getF (h[c]))|>])) ->
proj1_sig (to_nat f) < proj1_sig (to_nat (getF (h[c])))) ->
δ n x (array_write x f c) h h'
(** Union sets the parent and rank of a self-parent *)
| path_union : forall A x xr c h h' y xr' yr,
h[(array_read A x)] = mkCell n xr x ->
h[c] = mkCell n xr' y ->
xr ≤ xr' ->
(*h[(array_read A y)] = mkCell n yr y -> ; TOO STRONG: y may not be root by the time we hit the CAS *)
getF (h[(array_read A y)]) = yr ->
(* Update according to paper's x ≺ y ; point lower-rank to higher rank, or break tie with index order *)
xr' < yr \/ (xr'=yr /\ (proj1_sig (to_nat x) < proj1_sig (to_nat y))) ->
δ n A (array_write A x c) h h'
(** Sometimes union just attempts to bump the rank of a root node; using ≤ also gives easy reflexivity. *)
| bump_rank : forall A x xr xr' h h' c,
h[array_read A x] = mkCell n xr x ->
xr ≤ xr' ->
h[c] = mkCell n xr' x ->
δ n A (array_write A x c) h h'
| uf_id : forall A h h', δ n A A h h'
.
Ltac fcong i j :=
unfold fin in *;
let H := fresh in
assert (H : i = j) by congruence;
rewrite H in *;
clear dependent i.
Lemma chase_rank' : forall n h x i j t,
terminating_ascent n x h i ->
getF (h[x<|i|>]) = t ->
chase n x h t j ->
getF (h[x <| i |>]) ≤ getF (h[x <| j |>]).
Proof.
intros.
Require Import Coq.Program.Equality.
generalize dependent i.
dependent induction H1; intros.
dependent induction H. rewrite H in H0. subst i0. auto.
fcong t i. rewrite H3 in *. assumption.
dependent induction H0.
fcong i0 i. fcong t i.
apply IHchase; eauto. apply self_ascent; eauto.
fcong i t0.
etransitivity; try eassumption.
apply IHchase; eauto.
Qed.
Lemma trans_chase' : forall n x h f i j, j=getF(h[x<|i|>]) -> chase n x h f i -> chase n x h f j.
Proof.
intros.
induction H0. eapply trans_chase; eauto. rewrite <- H. constructor.
eapply trans_chase. apply IHchase. assumption. assumption.
Qed.
Lemma Hself_ref : forall n x h,
(forall i, terminating_ascent n x h i) ->
forall i0 i1, getF (h [x <| i0 |>]) = i1 -> getF (h [x <| i1 |>]) = i0 ->
i1 = i0.
intros n x h H.
intros A.
induction (H A).
intros. fcong i1 i; eauto.
intros. fcong t i1.
symmetry. apply IHt; eauto.
Qed.
Lemma Hdouble : forall n x h,
(forall i, terminating_ascent n x h i) ->
forall X Z, chase n x h X Z -> chase n x h Z X -> X = Z.
Proof.
intros n x h H.
intro X. induction (H X); intros.
induction H1; induction H2; auto.
fcong t f.
apply IHchase; eauto. eapply trans_chase. eassumption. assumption.
assert (chase n x h Z t).
induction H4. eapply trans_chase. constructor. intuition.
eapply trans_chase'. eassumption. eapply trans_chase. eassumption. assumption.
induction H3. reflexivity.
fcong t t1.
assert (Htmp := IHt _ H3 H5). rewrite Htmp in *.
symmetry. apply IHt; try eassumption. eapply trans_chase. constructor. assumption.
Qed.
Definition imm_vals' : forall h h' T P r, h[r]=h'[r] :=
fun h h' T P => immutable_vals T P h h'.
Ltac swap h h' := repeat rewrite (imm_vals' h h') in *.
Ltac arrays h h' :=
swap h h';
repeat (unfold fin in *;
match goal with
| [ |- context[ array_read (array_write ?A ?x ?y) ?x ] ] =>
rewrite read_updated_cell
| [ H : ?x ≠ ?z |- context[ array_read (array_write ?A ?x ?y) ?z ]] =>
rewrite read_past_updated_cell; auto
| [ H : ?z ≠ ?x |- context[ array_read (array_write ?A ?x ?y) ?z ]] =>
rewrite read_past_updated_cell; auto
end).
Lemma self_loop_chase : forall n x h i j,
chase n x h i j ->
getF (h[x<|i|>]) = i ->
i = j.
Proof. intros. induction H; eauto. fcong t i. firstorder. Qed.
Lemma ascend_new_heap : forall n x h h', (forall i, terminating_ascent n x h i) ->
forall i, terminating_ascent n x h' i.
Proof.
intros.
induction (H i); arrays h h'.
apply self_ascent; eauto.
eapply trans_ascent; eauto.
Qed.
Lemma ascend_new_heap' : forall n x h h' i, terminating_ascent n x h i ->
terminating_ascent n x h' i.
Proof.
intros. induction H; arrays h h'. apply self_ascent; eauto. eapply trans_ascent; eauto.
Qed.
Lemma chase_new_heap : forall n x h h' i j, chase n x h i j -> chase n x h' i j.
Proof.
intros. induction H; try constructor.
arrays h h'. eapply trans_chase; eauto.
Qed.
Lemma chase_dec : forall n x h i j, i≠j -> φ n x h -> chase n x h i j \/ ~chase n x h i j.
Proof.
intros. generalize dependent j.
rename H0 into H.
intros j Hne.
induction H. intros.
induction (H i).
induction (fin_dec _ i j). subst j. left. constructor.
right. intros Hbad. induction Hbad. contradiction b. auto.
assert (i = t) by congruence. rewrite H2 in *. firstorder.
clear H1.
induction (fin_dec _ t j). subst j.
left. eapply trans_chase; try constructor. symmetry; assumption.
induction (IHt b).
left. eapply trans_chase; eauto.
right.
intro Hbad. apply H1.
clear IHt H1.
inversion Hbad. subst j. contradiction Hne; eauto.
subst j. unfold fin in *.
fcong t1 t. assumption.
Qed.
Lemma chase_step : forall n x h a b c,
chase n x h a c -> a ≠ c -> getF(h[x<|a|>])=b -> chase n x h b c.
Proof. intros.
generalize dependent b.
induction H. exfalso; eauto.
intros. fcong t b. clear H2.
induction (fin_dec _ b f). subst f. constructor.
firstorder.
Qed.
Lemma chase_dec': forall n x h i j, φ n x h -> chase n x h i j \/ ~chase n x h i j.
Proof.
intros. induction (fin_dec _ i j). subst. left. constructor.
apply chase_dec; eauto.
Qed.
Lemma chase_two_ordering : forall n x h a b c, chase n x h a b ->
chase n x h a c ->
chase n x h b c \/ chase n x h c b.
Proof.
intros.
generalize dependent c.
induction H.
+ firstorder.
+ intros. induction H1.
- right. eapply trans_chase; eauto.
- fcong t0 t.
apply IHchase. assumption.
Qed.
Lemma no_chase_step : forall n x h a b c,
~chase n x h a c -> a≠c -> getF(h[x<|a|>])=b -> ~chase n x h b c.
Proof. intros. intro Hbad. apply H. eapply trans_chase; eauto. Qed.
Lemma no_chase_irrefl : forall n x h i j, ~chase n x h i j -> i ≠ j.
Proof. intros. intro Hbad. subst. apply H. constructor. Qed.
Lemma no_chase_irrefl' : forall n x h i j, ~chase n x h i j -> j ≠ i.
Proof. intros. intro Hbad. subst. apply H. constructor. Qed.
Lemma sort_equiv_rank : forall n x h,
(forall i, terminating_ascent n x h i) ->
forall a b,
chase n x h a b ->
a ≠ b ->
@eq nat (getF(h[x<|a|>])) (getF(h[x<|b|>])) ->
proj1_sig (to_nat a) < proj1_sig (to_nat b).
Proof.
intros n x h H a.
induction (H a); intros.
exfalso. apply H2. eapply self_loop_chase; eauto.
assert ((getF(h[x<|b|>])) ≤ (getF(h[x<|t|>]))).
rewrite <- H5. assumption.
assert (chase n x h t b). eapply chase_step; eauto.
assert ((getF(h[x<|t|>])) ≤ (getF(h[x<|b|>]))).
clear H5 H6 H3 IHt. clear H0 H2. induction H7. reflexivity.
etransitivity; try apply IHchase; eauto.
induction (H i0). fcong i0 t. reflexivity.
fcong t1 t. firstorder.
induction (H i0). fcong t i0. assumption.
fcong t1 t. etransitivity; eauto.
assert (@eq nat (getF(h[x<|t|>])) (getF(h[x<|b|>]))).
apply Le.le_antisym; eauto.
rewrite H9 in *. rewrite H5 in *.
induction (fin_dec _ t b). rewrite a in *. firstorder.
etransitivity; eauto.
Qed.
Lemma chase_rank : forall n x h,
(forall i, terminating_ascent n x h i) ->
forall a b, chase n x h a b ->
(getF(h[x<|a|>])) ≤ (getF(h[x<|b|>])).
Proof. intros. induction H0. reflexivity.
induction (H i). fcong t i. assumption.
fcong t0 t. etransitivity; eauto.
Qed.
Lemma chase_rank_strict : forall n x h,
(forall i, terminating_ascent n x h i) ->
forall a b, a ≠ b -> chase n x h a b ->
@eq nat (getF(h[x<|a|>])) (getF(h[x<|b|>])) ->
proj1_sig (to_nat a) < proj1_sig (to_nat b).
Proof.
intros. induction H1. contradiction H0; eauto.
induction (fin_dec _ t f). subst f.
induction (H i). fcong t i. contradiction H0; eauto.
fcong t0 t. firstorder.
assert (@eq nat (getF(h[x<|i|>])) (getF(h[x<|t|>]))).
induction (H i). fcong t i. auto.
fcong t0 t.
assert ((getF(h[x<|t|>])) ≤ (getF(h[x<|f|>]))).
apply chase_rank; eauto. eapply Le.le_antisym; eauto. rewrite H2; eauto.
induction (fin_dec _ i t).
rewrite <- a in *. clear dependent t. firstorder.
induction (H i). fcong t i. contradiction b0; eauto.
fcong t0 t.
etransitivity; try apply IHchase; eauto. rewrite <- H4. assumption.
Qed.
(* When chase_dec deduces a certain index doesn't chase a path to the updated
array index, we simply recycle the old ascent proof *)
Lemma unaffected_update : forall n x h f c,
φ n x h ->
@eq nat (getF (h[x<|f|>])) (getF (h[c])) ->
forall b,
~ chase n x h b f ->
terminating_ascent n (array_write x f c) h b.
Proof.
intros n x h f c H Hrank.
destruct H.
intros b; induction (H b).
intros Hbad. apply self_ascent. rewrite read_past_updated_cell; eauto. eapply no_chase_irrefl'; eauto.
intros Hbad. assert (Htmp := no_chase_irrefl' _ _ _ _ _ Hbad).
assert (Hch : ~chase n x h t f). eauto using no_chase_step.
assert (Htmp' := no_chase_irrefl' _ _ _ _ _ Hch).
apply trans_ascent with (t:=t); arrays h h'.
apply IHt; eauto.
Qed.
Lemma unaffected_update_le : forall n x h f c,
φ n x h ->
(getF (h[x<|f|>])) ≤ (getF (h[c])) ->
forall b,
~ chase n x h b f ->
terminating_ascent n (array_write x f c) h b.
Proof.
intros n x h f c H Hrank.
destruct H.
intros b; induction (H b).
intros Hbad. apply self_ascent. rewrite read_past_updated_cell; eauto. eapply no_chase_irrefl'; eauto.
intros Hbad. assert (Htmp := no_chase_irrefl' _ _ _ _ _ Hbad).
assert (Hch : ~chase n x h t f). eauto using no_chase_step.
assert (Htmp' := no_chase_irrefl' _ _ _ _ _ Hch).
apply trans_ascent with (t:=t); arrays h h'.
apply IHt; eauto.
Qed.
Lemma update_ascent : forall n x h f c jmp,
φ n x h ->
@eq nat (getF (h[x<|f|>])) (getF (h[c])) ->
getF (h[c]) ≤ getF (h[x<|jmp|>]) ->
(@eq nat (getF (h[c])) (getF (h[x<|jmp|>])) ->
proj1_sig (to_nat f) < proj1_sig (to_nat jmp)) ->
getF(h[c]) = jmp ->
~chase n x h jmp f ->
terminating_ascent n (array_write x f c) h f.
Proof.
intros.
assert (Htmp := no_chase_irrefl' _ _ _ _ _ H4).
apply trans_ascent with (t:=jmp); arrays h h'.
congruence.
eauto using unaffected_update.
Qed.
Lemma update_ascent_le : forall n x h f c jmp,
φ n x h ->
(getF (h[x<|f|>])) ≤ (getF (h[c])) ->
getF (h[c]) ≤ getF (h[x<|jmp|>]) ->
(@eq nat (getF (h[c])) (getF (h[x<|jmp|>])) ->
proj1_sig (to_nat f) < proj1_sig (to_nat jmp)) ->
getF(h[c]) = jmp ->
~chase n x h jmp f ->
terminating_ascent n (array_write x f c) h f.
Proof.
intros.
assert (Htmp := no_chase_irrefl' _ _ _ _ _ H4).
apply trans_ascent with (t:=jmp); arrays h h'.
congruence.
eapply unaffected_update_le; eauto.
Qed.
Lemma affected_update : forall n x h f c,
φ n x h ->
@eq nat (getF (h[x<|f|>])) (getF (h[c])) ->
terminating_ascent n (array_write x f c) h f ->
forall b,
chase n x h b f ->
terminating_ascent n (array_write x f c) h b.
Proof.
intros n x h f c H Hrank. intros. destruct H.
induction (H b).
induction H1; eauto.
induction H1; eauto. fcong i0 i. eauto. fcong i0 i. fcong t i. firstorder.
(* trans *)
induction H1; eauto.
induction (fin_dec _ i f).
rewrite a in *. assumption.
fcong t1 t.
induction (fin_dec _ t f). rewrite a in *. clear dependent t.
eapply trans_ascent with (t:=f); arrays h h'; eauto.
rewrite <- Hrank in *. firstorder.
rewrite <- Hrank in *. firstorder.
eapply trans_ascent with (t:=t); arrays h h'; eauto.
Qed.
Lemma affected_update_le : forall n x h f c,
φ n x h ->
(getF (h[x<|f|>])) ≤ (getF (h[c])) ->
terminating_ascent n (array_write x f c) h f ->
forall b,
chase n x h b f ->
terminating_ascent n (array_write x f c) h b.
Proof.
intros n x h f c H Hrank. intros. destruct H.
induction (H b).
induction H1; eauto.
induction H1; eauto. fcong i0 i. eauto. fcong i0 i. fcong t i. firstorder.
(* trans *)
induction H1; eauto.
induction (fin_dec _ i f).
rewrite a in *. assumption.
fcong t1 t.
induction (fin_dec _ t f). rewrite a in *. clear dependent t.
eapply trans_ascent with (t:=f); arrays h h'; eauto.
rewrite <- Hrank in *. firstorder.
intro Heq. arrays h h'. rewrite <- Heq in Hrank.
assert (Heq' : @eq nat (getF (h[x<|i|>])) (getF (h[x<|f|>]))).
eapply Le.le_antisym; eauto.
firstorder.
eapply trans_ascent with (t:=t); arrays h h'; eauto.
Qed.
(* TODO: Now, some of these lemmas should be provable by inducting on the use of
chase_dec, then in the affected cases, inducting on whether or not the affected index is the updated cell or not, then applying one of the previous 3 lemmas.
*)
Lemma sameset_acyclic_update_preserves_term_ascent :
forall h h' n x f c newparent i,
@eq nat (getF (h[x <| f |>])) (getF (h[c])) ->
(forall i, terminating_ascent n x h i) ->
getF (h[c]) = newparent ->
(exists Y, ~chase n x h (getF (h[c])) f /\
chase n x h (getF (h[c])) Y /\
chase n x h f Y) ->
getF (h[c]) ≤ getF (h[x<|newparent|>]) ->
(@eq nat (getF (h[c])) (getF (h[x<|newparent|>])) ->
proj1_sig (to_nat f) < proj1_sig (to_nat newparent)) ->
terminating_ascent n (array_write x f c) h' i.
Proof.
intros h h' n x f c newparent i Hrank H.
intros Hparent.
intros [Y [Hnocycle [HcY HfY]]].
intros Hparent_rank Hparent_sort.
fold fin in *. (* some of the lemmas are stated w.r.t. Fin.t... *)
Check chase_dec.
induction (fin_dec _ i f).
+ subst f. rewrite Hparent in *.
assert (newparent <> i). eapply no_chase_irrefl; eauto. auto.
eapply trans_ascent with (t:=newparent).
rewrite read_updated_cell. arrays h h'. auto.
rewrite read_updated_cell.
rewrite read_past_updated_cell; auto. arrays h h'; auto.
rewrite read_updated_cell. rewrite read_past_updated_cell; try solve[auto].
arrays h h'. intro Heq. specialize (Hparent_sort Heq).
auto.
eapply unaffected_update. constructor. intros. eapply ascend_new_heap'. eauto. arrays h h'. assumption.
intro Hbad. assert (chase n x h newparent i).
eapply chase_new_heap. eassumption. auto.
+
induction (chase_dec n x h i f); try solve[constructor; eauto]; eauto.
- apply (affected_update n x h' f c); eauto.
constructor; eauto using ascend_new_heap.
arrays h h'; eassumption.
rewrite Hparent in *.
assert (newparent <> f). eapply no_chase_irrefl; eauto. auto.
eapply trans_ascent with (t:=newparent).
rewrite read_updated_cell. arrays h h'; auto.
rewrite read_updated_cell. rewrite read_past_updated_cell.
repeat rewrite (imm_vals' h h') in *. tauto.
solve[auto].
rewrite read_updated_cell. rewrite read_past_updated_cell; auto.
repeat rewrite (imm_vals' h h') in *.
intro Heq; specialize (Hparent_sort Heq). tauto.
(* newparent is "unaffected" *)
eapply unaffected_update.
constructor. eauto using ascend_new_heap.
arrays h h'; eassumption.
intro Hbad. apply Hnocycle.
eauto using chase_new_heap.
eauto using chase_new_heap.
- (* unaffected: i is "above" f or in another set *)
assert (sol := unaffected_update n x h f c (pfφ _ _ h H) Hrank i H0).
Check ascend_new_heap.
apply (ascend_new_heap' n _ h h'). eauto.
Qed.
(* Notes: Once an index is non-root, it remains non-root.
Non-root ranks are fixed forever.
--> If we observe a path x-->y for x<>y,
then forever onwards, x.rank <= y.rank.
In locations where we read out a parent link,
we can generate this. Then again for the grandparent, using transitivity of <=.
*)
Lemma chase_update_preserves_term_ascent :
forall h h' n x f i mid c,
@eq nat (getF (h[x <| f |>])) (getF (h[c])) ->
(forall i, terminating_ascent n x h i) ->
chase n x h f mid ->
getF (h [c]) = mid ->
terminating_ascent n (array_write x f c) h' i.
Proof.
intros h h' n x f i mid c Hrank H.
intros Hc Hf.
induction (H i).
(* self *)
induction (fin_dec _ f i).
(* f = i *)
subst i.
inversion Hc.
apply self_ascent. rewrite read_updated_cell. erewrite immutable_vals; eassumption.
subst f0.
fcong t f. clear H1 H2.
induction (H mid).
(* self *)
induction (fin_dec _ f i). subst f. apply self_ascent. rewrite read_updated_cell. erewrite immutable_vals; eassumption.
exfalso. apply b. clear Hf Hrank.
induction Hc; eauto.
fcong t i. firstorder.
(* trans *)
assert (f = i). eapply self_loop_chase; eauto.
rewrite H4 in *.
apply self_ascent; arrays h h'; eauto.
(* f ≠ i *)
apply self_ascent. rewrite read_past_updated_cell; auto. erewrite immutable_vals; eassumption; auto.
(* trans *)
induction (fin_dec _ f i). subst i.
induction (fin_dec _ f mid). rewrite <- a in *. clear dependent mid.
apply self_ascent; arrays h h'; eauto.
apply trans_ascent with (t:=mid). rewrite read_updated_cell.
rewrite immutable_vals with (h':=h).
rewrite <- Hf. reflexivity.
rewrite read_updated_cell.
induction (fin_dec _ f mid).
rewrite <- a. rewrite read_updated_cell. reflexivity.
rewrite read_past_updated_cell; auto.
repeat rewrite <- immutable_vals with (h:=h)(h':=h').
rewrite <- Hrank.
assert (chase n x h t mid).
clear H1. clear IHt. clear t0.
inversion Hc. contradiction b.
unfold fin in *.
subst f0.
fcong t t0. assumption.
eapply chase_rank'; auto.
rewrite H0 in H3. assumption.
(* New case for rank= -> < *)
intro H'. arrays h h'.
rewrite read_updated_cell in H'; eauto.
rewrite read_past_updated_cell in H'; eauto.
rewrite <- Hrank in H'.
eapply chase_rank_strict; arrays h h'; eauto.
eapply unaffected_update.
constructor; eauto using ascend_new_heap.
arrays h h'. eauto.
intro Hbad. apply b. eapply Hdouble; eauto using chase_new_heap.
induction (fin_dec _ f t).
rewrite a in *. clear dependent f.
apply trans_ascent with (t:= t); arrays h h'.
rewrite <- Hrank; eauto.
intro H'. apply H2. rewrite <- Hrank in *. assumption.
assumption.
apply trans_ascent with (t:=t); arrays h h'.
assumption.
Qed.
Lemma no_chase_extend : forall n x h s m,
chase n x h s m ->
forall f, ~ chase n x h s f ->
~ chase n x h m f.
Proof.
intros n x h s m H.
induction H. tauto.
intros.
apply IHchase. intro.
apply H1. eapply trans_chase. eassumption. eauto.
Qed.
Lemma union_identity : forall n x h f y (c:ref{cell n|any}[local_imm,local_imm]),
f≠y ->
(*getF(h[c])=y -> *)
terminating_ascent n x h y ->
~chase n x h y f ->
getF(h[x<|f|>]) = f ->
forall y', chase n x h y y' -> f≠y' ->
terminating_ascent n (array_write x f c) h y'.
Proof.
intros n x h f y c. intro.
intro.
intros Hchase.
intros Hupdate_root.
induction H0.
(* self *) intros. induction H1. apply self_ascent; arrays h h'; eauto.
assert (i = t) by congruence. rewrite H4 in *. firstorder.
(* trans *)
intros.
(* back patch *) rename H2 into Hcond. rename H3 into H2. rename H4 into H3. rename H5 into H4.
induction H3.
induction (fin_dec _ f t). rewrite a in *.
exfalso. apply Hchase. eapply trans_chase; try constructor; eauto.
apply trans_ascent with (t:=t); arrays h h'.
apply IHterminating_ascent; eauto.
eapply no_chase_extend; eauto. eapply trans_chase; try solve[symmetry;eassumption]. constructor.
constructor.
unfold fin in *. assert (t = t0) by congruence. rewrite H6 in *. clear dependent t.
assert (f ≠ t0). intro Hbad. rewrite Hbad in *. exfalso. apply Hchase. eapply trans_chase. constructor. symmetry; eauto.
apply IHterminating_ascent; eauto.
eapply no_chase_extend; try eassumption. eapply trans_chase; try solve[symmetry;eassumption]. constructor.
Qed.
Require Import Coq.Arith.Lt.
Lemma stable_φ_δ : forall n, stable (φ n) (δ n).
Proof.
intros. red. intros.
induction H0.
(* Compression *)
destruct H. constructor. intros.
(*eapply chase_update_preserves_term_ascent; eauto.*)
eapply sameset_acyclic_update_preserves_term_ascent; eauto.
(* Union *)
destruct H. constructor.
cut ( (* Omitting n, x0, H, x *)
h[x0<|x|>] = mkCell n xr x ->
h[c]=mkCell n xr' y ->
xr ≤ xr' ->
getF (h[x0<|y|>]) = yr ->
xr ≤ yr ->
forall i, terminating_ascent n (array_write x0 x c) h' i).
intros Hgeneralized.
apply Hgeneralized; eauto.
etransitivity; eauto. induction H4. eauto with arith. destruct H4. subst xr'. reflexivity.
intros.
induction (fin_dec _ i x).
rewrite a in *. clear dependent i.
induction (fin_dec _ x y). subst y.
apply self_ascent; arrays h h'; eauto. rewrite H1. reflexivity.
apply trans_ascent with (t:=y); arrays h h'; eauto.
rewrite H1. reflexivity.
rewrite H1. rewrite H3. simpl.
induction H4. eauto with arith. destruct H4. subst xr'. reflexivity.
(* new cond case *)
rewrite H1. rewrite H8. simpl. intro Hcond. rewrite Hcond in *; clear dependent xr'.
induction H4. exfalso. eapply Lt.lt_irrefl; eauto.
destruct H4. assumption.
(* Now we're pointing to y, which doesn't chase back to x in x0 *)
eapply union_identity; eauto.
eapply ascend_new_heap; eauto.
(* BETTER be provable that ~ chase y x *)
Require Import Coq.Arith.Le.
Require Import Coq.Arith.Lt.
intro X. induction H4.
(* xr' < yr *)
induction X. apply b; reflexivity.
assert (Hch' := chase_new_heap n x0 h' h _ _ X).
arrays h' h.
assert (Htmp := chase_rank' n h x0 _ _ _ (H i) H10 Hch').
arrays h h'. rewrite H3 in Htmp. rewrite H0 in Htmp. simpl in Htmp.
assert (yr ≤ xr'). etransitivity; eauto.
assert (h'' := le_not_lt _ _ H11). apply h''. assumption.
(* xr'=yr, x < y *)
destruct H4. subst xr'.
induction X. apply b; reflexivity.
assert (Hch' := chase_new_heap n x0 h' h _ _ X).
arrays h' h.
assert (Htmp := chase_rank' n h x0 _ _ _ (H i) H4 Hch').
rewrite H0 in Htmp. rewrite H8 in Htmp. simpl in Htmp.
assert (heq := le_antisym _ _ Htmp H9). subst xr.
(* now the ranks must be == from i->t->...->f. and f < i.
but by inducting on term_ascent i, which reaches f, if
the ranks are equal then i < f, which is a contradiction. *)
induction (H i).
(* self *) assert (t=i) by congruence. rewrite H12 in *.
clear H6 H5 H2 H7 H9 Htmp H11 H12 t IHX H8.
assert (i = f). induction X; try reflexivity.
arrays h h'. assert (i=t) by congruence. rewrite <- H5 in *. clear H5. clear t.
firstorder.
subst f. eapply lt_irrefl; eauto.
(* trans *) fcong t0 t.
assert (getF (h[x0<|t|>]) ≤ getF (h[x0<|f|>])).
induction (fin_dec _ t f). subst f. reflexivity.
eapply chase_rank'; eauto.
induction Hch'. exfalso; intuition.
clear IHHch'.
rewrite H14. assumption.
assert (getF (h[x0<|t|>]) = yr).
rewrite H3 in H12.
rewrite H0 in H14. unfold getF at 2 in H14. unfold cell_rank in *.
eapply le_antisym; eauto.
(* Looks like I need to add a hyp to the inductive
term_ascent ctor... if the ranks are = then child < parent *)
rewrite H15 in *.
assert (Htmp' := chase_rank_strict n x0 h H i f).
rewrite H3 in *. rewrite H0 in *. simpl in Htmp'.
assert (proj1_sig (to_nat i) < proj1_sig (to_nat f)).
apply Htmp'; eauto. eapply trans_chase with (t0:=t); eauto.
assert (Htmp'' := Lt.lt_not_le _ _ H16). apply Htmp''. eauto with arith.
arrays h h'; rewrite H0; reflexivity.
constructor.
(* i ≠ x *)
einduction (chase_dec n x0 h _ _ b).
eapply affected_update_le; eauto using chase_new_heap.
constructor. eauto using ascend_new_heap.
arrays h h'. rewrite H0; rewrite H1. simpl.
assert (Htmp := chase_rank n x0 h H i x H10).
assumption.
eapply update_ascent_le; eauto.
constructor; eauto using ascend_new_heap.
arrays h h'; rewrite H0. rewrite H6. simpl. assumption.
arrays h h'. rewrite H1. unfold getF at 3. unfold getF at 1. unfold cell_parent. unfold cell_rank.
assert (xr' ≤ yr). induction H4. eauto with arith. destruct H4. rewrite H4. reflexivity.
rewrite <- H8 in H11. assumption.
arrays h h'. repeat rewrite H1.
unfold getF at 1. unfold getF at 2. unfold cell_rank; unfold cell_parent.
intro Heq. assert (xr' = yr). rewrite <- H8. assumption.
rewrite H11 in *. unfold getF.
induction H4. exfalso; eapply Lt.lt_irrefl; eauto. destruct H4. assumption.
arrays h h'; rewrite H1. simpl.
intro Hbad.
assert (getF(h[x0<|y|>]) ≤ getF(h[x0<|x|>])).
eapply chase_rank; eauto using ascend_new_heap, chase_new_heap.
arrays h h'. rewrite H8 in H11. rewrite H0 in H11. simpl in H11.
assert (xr = yr). eapply Le.le_antisym; eauto.
subst xr.
induction H4.
assert (Htmp := Lt.lt_not_le _ _ H4). firstorder.
destruct H4. subst xr'.
assert (Htmp := Lt.lt_not_le _ _ H12). apply Htmp.
Check chase_rank_strict.
assert (proj1_sig (to_nat y) < proj1_sig (to_nat x)).
eapply chase_rank_strict; eauto.
induction (fin_dec _ y x); eauto. subst x. exfalso. apply Htmp. reflexivity.
eapply chase_new_heap; eauto.
arrays h h'. rewrite H8. rewrite H0. reflexivity.
eauto with arith.
eapply union_identity; eauto.
eapply ascend_new_heap; eauto.
intro X. apply H10. eapply chase_new_heap; eauto.
arrays h h'; rewrite H0; reflexivity.
constructor.
constructor. assumption.
(* Rank bump *)
constructor. intros. destruct H.
induction (fin_dec n x i).
subst. apply self_ascent. rewrite read_updated_cell; eauto.
rewrite <- immutable_vals with (h := h); eauto. rewrite H2. simpl. auto.
induction (H i).
apply self_ascent. rewrite read_past_updated_cell; eauto.
rewrite <- immutable_vals with (h := h); eauto.
induction (fin_dec n x t).
subst x.
apply trans_ascent with (t := t);
try rewrite read_past_updated_cell with (f1 := t) (f2 := i); eauto.
rewrite <- immutable_vals with (h := h); eauto.
rewrite read_updated_cell; eauto.
rewrite <- immutable_vals with (h := h); eauto.
rewrite H0 in *. rewrite immutable_vals with (h' := h') in H2. rewrite H2.
unfold getF at 2. unfold cell_rank.
unfold getF at 2 in H4. unfold cell_rank in H4.
etransitivity. eassumption. eauto.
(* new conditional goal *)
arrays h h'. intro Heq. rewrite H2 in Heq. unfold getF at 2 in Heq. unfold cell_rank in *.
rewrite Heq in *. rewrite H0 in *. unfold getF in H4. unfold getF in H5.
rewrite Lt.le_lt_or_eq_iff in H1.
induction H1.
apply H5. eapply Le.le_antisym; eauto with arith.
subst xr'. firstorder.
apply self_ascent. rewrite read_updated_cell; eauto.
rewrite immutable_vals with (h' := h') in H2.
rewrite H2. reflexivity.
apply trans_ascent with (t := t);
try rewrite read_past_updated_cell with (f1 := x) (f2 := i); eauto.
rewrite <- immutable_vals with (h := h); eauto.
rewrite <- immutable_vals with (h := h); eauto.
rewrite read_past_updated_cell; eauto.
etransitivity. eassumption. rewrite immutable_vals with (h' := h').
reflexivity.
(* new conditional goal *)
arrays h h'.
(* refl *)
destruct H; constructor; eauto using ascend_new_heap.
Qed.
Hint Resolve stable_φ_δ.
Lemma precise_φ : forall n, precise_pred (φ n).
Proof.
intros; red; intros.
induction H; constructor; intros.
induction (H i).
constructor. rewrite <- H0; eauto. constructor.
red in x. compute. eexists; reflexivity.
eapply trans_ascent. rewrite <- H0; eauto.
constructor. compute. eexists; reflexivity.
rewrite <- immutable_vals with (h:=h). etransitivity. eassumption.
rewrite immutable_vals with (h':=h'). reflexivity.
intro. arrays h h'. eauto.
assumption.
Qed.
Lemma precise_chase : forall n i j, precise_pred (fun x h => chase n x h i j).
Proof.
intros. red; intros.
induction H.
constructor; intros. (*rewrite immutable_fields with (h' := h). auto.*)
eapply trans_chase. eassumption.
rewrite immutable_fields with (h' := h).
auto.
Qed.
Lemma precise_δ : forall n, precise_rel (δ n).
intros. red. intros.
induction H1.
assert (H' := precise_chase). red in H'.
assert (Htmp := precise_φ n). red in Htmp.
eapply path_compression; eauto.
repeat rewrite immutable_vals with (h:=h2)(h':=h). assumption.
destruct H3 as [Y [Hno [HcY HfY]]].
exists Y.
repeat rewrite immutable_vals with (h:=h2)(h':=h).
intuition; eauto using chase_new_heap.
repeat rewrite immutable_vals with (h:=h2)(h':=h). assumption.
repeat rewrite immutable_vals with (h:=h2)(h':=h). assumption.
rewrite H in H1. rewrite (immutable_vals _ _ h h2) in H2. rewrite H in H4.
eapply path_union; eauto.
constructor. repeat red. exists y. compute; reflexivity.
constructor. repeat red. exists x. compute; reflexivity.
rewrite (immutable_vals _ _ h h2) in H1.
rewrite (immutable_vals _ _ h h2) in H3.
eapply bump_rank; eauto.
(* refl *)
eapply uf_id.
Qed.
Hint Resolve precise_φ precise_δ.
Lemma refl_δ : forall n, hreflexive (δ (S n)).
Proof.
intros; red; intros. constructor.
Qed.
Hint Resolve refl_δ.
Instance read_uf {n:nat} : readable_at (uf n) (δ n) (δ n) := id_fold.
Lemma chase_append : forall n x h a b c,
chase n x h a b ->
chase n x h b c ->
chase n x h a c.
Proof.
intros.
induction H. assumption.
eapply trans_chase. apply IHchase. assumption. assumption.
Qed.
Definition sameset_chasing {n:nat} i j : hpred (uf n) :=
λ x h,
exists Y,
chase n x h i Y /\
chase n x h j Y.
Lemma sameset_chasing_stable :
forall n i j, stable (@sameset_chasing n i j) (δ n).
Proof.
intros. red. intros. unfold sameset_chasing in *.
destruct H as [Y [HiY HjY]].
induction H0.
+ (* compression *)
(* If Y-->f (Y below f) or i and j both above f, then unaffected.
if f is on the path from i or j to Y, then ex Y' that the new stuff points to, and Y-->Y'.
*)
assert (chase n x h Y f ->
forall b, chase n x h b Y -> chase n (array_write x f c) h b Y).
clear H0. clear H1. clear H2. clear H3.
intros.
induction H0.
induction H1. constructor.
induction (fin_dec _ f i0). subst i0. constructor.
eapply trans_chase. apply IHchase; eauto.
rewrite read_past_updated_cell; eauto.
induction H1. constructor.
induction (fin_dec _ f i0).
subst i0. assert (f = t). eapply Hdouble; eauto. destruct H; eassumption.
eapply trans_chase; try eassumption.
eapply trans_chase'; try eassumption. auto.
subst t.
assert (f = f0). eapply Hdouble; eauto. destruct H; eassumption.
eapply trans_chase; eauto.
eapply trans_chase'; eauto. constructor.
subst f0. constructor.
eapply trans_chase; eauto.
rewrite read_past_updated_cell; eauto.
rewrite H3. apply IHchase0; eauto.
intros.
specialize (IHchase HiY0 HjY0).
assert (chase n x h i0 t). eapply trans_chase; eauto.
specialize (IHchase H5).
induction (fin_dec _ i0 t). subst i0.
assert (t0 = t). eapply Hdouble; eauto. destruct H; eassumption.
eapply trans_chase; eauto. constructor.
rewrite H6. constructor.
eapply chase_step; eauto. clear IHchase0.
rewrite read_past_updated_cell; eauto.
induction (chase_dec' n x h Y f); eauto.
exists Y. split; eauto using chase_new_heap.
assert (Y <> f). eauto using no_chase_irrefl.
destruct H1 as [Y' [Hnochase [HcY' HfY']]].
assert (getF(h[c]) <> f). eauto using no_chase_irrefl.
(** When would we need to change the choice of common ancestor?
For the choice of common ancestor to become invalid, it would require that overwriting f would break either i or j's path to Y, AND that the actual value we overwrite with doesn't restore the path. So this means we only need to change Y when BOTH of the following hold:
1. i->f->Y and/or j->f->Y, AND
2. not (getF(h[c]) -> Y)
If neither of i or j reaches f, the i-j-Y triad is either above f (we already checked not(Y->f), so they're not below f),
below getF(h[c]), or in another set entirely, so the whole of both paths can be reused.
If i or j reaches f but getF(h[c])->Y, we can reuse the path up until f, then stick on the new (unaffected) path.
When we are in the case of the write breaking the paths to Y, we know f->Y, f->Y', getF(h[c])->Y', and not(getF(h[c])->Y).
Clearly Y<>Y'.
Since f->Y and f->Y', Y->Y' or Y'->Y (new lemma).
Y'->Y and getF(h[c])->Y' implies getF(h[c])->Y, which is a contradiction, so just Y->Y'. (all in original heap/array)
This means that i->Y' and j->Y' in the original heap.
So we can choose Y' as the new common ancestor,
and reuse the {i,j}->Y' path until we hit f, then tack on the unaffected getF(h[c])->Y' path.
Now I just have to get the induction structure to work out.
*)
induction (chase_dec' n x h (getF(h[c])) Y); try assumption.
assert (chase n (array_write x f c) h' (getF(h[c])) Y).
apply chase_new_heap with (h:=h).
clear H4 H3 H2 HcY' HfY' HiY HjY.
induction H7. constructor.
eapply trans_chase.
eapply IHchase. eapply no_chase_step. apply Hnochase. eapply no_chase_irrefl. eassumption. assumption. assumption. assumption.
rewrite read_past_updated_cell. assumption.
assert (i0 <> f). eauto using no_chase_irrefl.
auto.
exists Y. split.
clear dependent j. clear H2 H3 H4.
induction HiY. constructor.
specialize (IHHiY H5 H6 H7 H8).
induction (fin_dec _ f i). subst i.
eapply trans_chase. apply H8.
rewrite read_updated_cell. arrays h h'. reflexivity.
eapply trans_chase. apply IHHiY.
rewrite read_past_updated_cell. arrays h h'. auto.
assumption.
clear dependent i. clear H2 H3 H4.