summaryrefslogtreecommitdiff
path: root/backend/Merge_WL.v
blob: d9b27e90f376fcc7da9e8334a2711b2d140a0f22 (plain)
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
Require Import FSets.
Require Import InterfGraphMapImp.
Require Import Simplify_WL.
Require Import IRC_graph.
Require Import WS.
Require Import Edges.
Require Import Affinity_relation.
Require Import Interference_adjacency.
Require Import Remove_Vertex_WL.
Require Import Merge_Degree.

Import Edge RegFacts Props OTFacts.

Definition classify x g' palette wl :=
if is_precolored x g' then wl else
let a := get_spillWL wl in
let b := get_freezeWL wl in
let c := get_simplifyWL wl in
let d := get_movesWL wl in
if has_low_degree g' palette x then 
   if move_related g' x then (a, VertexSet.add x b, c, d)
   else (a, VertexSet.remove x b, VertexSet.add x c, d)
else (VertexSet.add x a, VertexSet.remove x b, c, d).

Definition merge_wl3 e ircg g' (p : In_graph_edge e (irc_g ircg)) (q : aff_edge e) :=
let wl := (irc_wl ircg) in
let g := (irc_g ircg) in
let palette := (pal ircg) in
let k := (irc_k ircg) in
let simplifyWL := get_simplifyWL wl in
let freezeWL := get_freezeWL wl in
let spillWL := get_spillWL wl in
let movesWL := get_movesWL wl in
let pre := precolored g in
let iadj_fst_ := interference_adj (fst_ext e) g in
let iadj_fst := VertexSet.diff iadj_fst_ pre in
let iadj_snd_ := interference_adj (snd_ext e) g in
let iadj_snd := VertexSet.diff iadj_snd_ pre in
let padj_fst_ := preference_adj (fst_ext e) g in
let padj_fst := VertexSet.diff padj_fst_ pre in
let padj_snd_ := preference_adj (snd_ext e) g in
let padj_snd := VertexSet.diff padj_snd_ pre in
let iadj_interf := VertexSet.inter iadj_fst iadj_snd in
let iadj_padj_interf := VertexSet.union
                           (VertexSet.inter padj_fst iadj_snd)
                           (VertexSet.inter padj_snd iadj_fst) in
let N := VertexSet.filter 
         (fun x => eq_K k (VertexSet.cardinal (interference_adj x g)))
         iadj_interf in
let (mr, nmr) := VertexSet.partition (move_related g) N in
let M := VertexSet.filter 
         (fun x => eq_K 1 (VertexSet.cardinal (preference_adj x g)) && has_low_degree g k x)
         iadj_padj_interf in
let simplifyWL_ := VertexSet.union simplifyWL nmr in
let simplifyWL' := VertexSet.union simplifyWL_ M in
let freezeWL__ := VertexSet.diff freezeWL M in
let freezeWL_ := VertexSet.union freezeWL__ mr in
let freezeWL' := VertexSet.remove (fst_ext e) (VertexSet.remove (snd_ext e) freezeWL_) in
let spillWL_ := VertexSet.diff spillWL N in
let spillWL' := VertexSet.remove (fst_ext e) (VertexSet.remove (snd_ext e) spillWL_) in
let movesWL' := AE_merge_up (preference_adj (fst_ext e) g')
                             padj_fst_ padj_snd_ e movesWL in
classify (fst_ext e) g' k (spillWL', freezeWL', simplifyWL', movesWL').


Lemma ws_merge3 : forall e ircg H H0,
WS_properties (merge e (irc_g ircg) H H0) 
              (VertexSet.cardinal (pal ircg))
              (merge_wl3 e ircg (merge e (irc_g ircg) H H0) H H0).

Proof.
intros e ircg HH HH0.
unfold merge_wl3. unfold WS_properties. rewrite <-(Hk ircg) in *. 
set (wl := irc_wl ircg) in *.
set (g := irc_g ircg) in *.
set (g' := merge e g HH HH0) in *.
set (palette := (pal ircg)) in *.
set (simplify := get_simplifyWL wl) in *.
set (freeze := get_freezeWL wl) in *.
set (spill := get_spillWL wl) in *.
set (moves := get_movesWL wl) in *.
set (k := VertexSet.cardinal palette) in *.
set (pre := precolored g) in *.
set (iadj_fst_ := interference_adj (fst_ext e) g) in *.
set (iadj_snd_ := interference_adj (snd_ext e) g) in *.
set (padj_fst_ := preference_adj (fst_ext e) g) in *.
set (padj_snd_ := preference_adj (snd_ext e) g) in *.
set (iadj_fst := VertexSet.diff iadj_fst_ pre) in *.
set (iadj_snd := VertexSet.diff iadj_snd_ pre) in *.
set (padj_fst := VertexSet.diff padj_fst_ pre) in *.
set (padj_snd := VertexSet.diff padj_snd_ pre) in *.
set (inter_interf := VertexSet.inter iadj_fst iadj_snd) in *.
set (iadj_padj_interf := VertexSet.union
                         (VertexSet.inter padj_fst iadj_snd)
                         (VertexSet.inter padj_snd iadj_fst)) in *.
set (N := VertexSet.filter
          (fun x => eq_K k (VertexSet.cardinal (interference_adj x g)))
          inter_interf) in *.
set (M := VertexSet.filter
          (fun x => eq_K 1 (VertexSet.cardinal (preference_adj x g))
           && has_low_degree g k x)
           iadj_padj_interf) in *.
set (mrnmr := VertexSet.partition (move_related g) N) in *.
case_eq mrnmr. intros mr nmr Hmr. unfold mrnmr in Hmr.
set (simplifyWL_ := VertexSet.union simplify nmr) in *.
set (simplifyWL' := VertexSet.union simplifyWL_ M) in *.
set (freezeWL__ := VertexSet.diff freeze M) in *.
set (freezeWL_ := VertexSet.union freezeWL__ mr) in *.
set (freezeWL' := VertexSet.remove (fst_ext e) (VertexSet.remove (snd_ext e) freezeWL_)) in *.
set (spillWL_ := VertexSet.diff spill N) in *.
set (spillWL' := VertexSet.remove (fst_ext e) (VertexSet.remove (snd_ext e) spillWL_)) in *.
set (movesWL' := AE_merge_up (preference_adj (fst_ext e) g') padj_fst_ padj_snd_ e moves) in *.

unfold get_spillWL. unfold get_freezeWL. unfold get_simplifyWL. unfold get_movesWL. simpl.
generalize (HWS_irc ircg). intro HWS. rewrite <-(Hk ircg) in HWS.
generalize HH0. intro Haffa. destruct Haffa as [x2 Haffa].

cut (VertexSet.Equal mr (VertexSet.filter (move_related g) N)).
intro HMR.
cut (VertexSet.Equal nmr (VertexSet.filter (fun x => negb (move_related g x)) N)).
intro HNMR.

split.
intro x.
(* spillWL for x = fst_ext e *)
destruct (Register.eq_dec x (fst_ext e)); split; intros.
(* spillWL => for x = fst_ext e *)
unfold classify in H.
unfold get_simplifyWL, get_freezeWL, get_spillWL, get_movesWL in H. simpl in H.
(* fst_ext e is precolored => False *)
case_eq (is_precolored (fst_ext e) g'); intros; rewrite H0 in H.
simpl in H.
unfold spillWL' in H. generalize (VertexSet.remove_3 (VertexSet.remove_3 H)). intro.
unfold spillWL_ in H1. generalize (VertexSet.diff_1 H1). intro.
generalize (In_spill_props _ _ _ _ _ _ _ _ H2 (refl_equal _) HWS). intro.
elim (proj2 (proj2 H3)).
apply VertexSet.remove_3 with (x:=snd_ext e).
rewrite <-(precolored_merge e (irc_g ircg) HH HH0). rewrite precolored_equiv. split.
rewrite is_precolored_ext with (y := fst_ext e); eassumption.
rewrite In_merge_vertex. split. intuition.
intro. elim (In_graph_edge_diff_ext e g). assumption.
rewrite <-e0. rewrite <-H4. auto.
(* fst_ext e is not precolored *)
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H.
(* fst_ext e is of low-degree in g' *)
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H;
simpl in H; unfold spillWL' in H; elim (VertexSet.remove_1 (Register.eq_sym _ _ e0) H).
(* fst_ext e is of high-degree in g' *)
simpl in H. split.
rewrite (compat_bool_low _ _ _ _ e0). assumption.
split. unfold g'. rewrite In_merge_vertex. split.
rewrite e0. apply (proj1 (In_graph_edge_in_ext _ _ HH)).
intro. elim (In_graph_edge_diff_ext e g). assumption.
rewrite <-e0. rewrite <-H2. auto.
rewrite precolored_equiv. intro. destruct H2.
rewrite (is_precolored_ext _ _ g' e0) in H2. rewrite H0 in H2. inversion H2.
(* spillWL <= for x = fst_ext e *)
destruct H. destruct H0.
unfold classify, get_spillWL, get_simplifyWL, get_freezeWL, get_movesWL. simpl.
case_eq (is_precolored (fst_ext e) g'); intros; simpl.
(* fst_ext e is precolored => False *)
elim H1. rewrite precolored_equiv. split.
rewrite (is_precolored_ext _ _ g' e0). assumption. assumption.
(* fst_ext e is not precolored *)
case_eq (has_low_degree g' k (fst_ext e)); intros; simpl.
(* fst_ext e is of low-degree in g' *)
rewrite (compat_bool_low _ _ _ _ e0) in H. rewrite H in H3. inversion H3.
(* fst_ext e is of high-degree in g' *)
apply VertexSet.add_1. intuition.

(* spillWL : x is different from fst_ext e *)
assert (VertexSet.In x spillWL').
unfold classify, get_simplifyWL, get_movesWL, get_freezeWL, get_spillWL in H. simpl in H.
case_eq (is_precolored (fst_ext e) g'); simpl in H; intro; rewrite H0 in H.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H; simpl in H.
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H; simpl in H; assumption.
apply VertexSet.add_3 with (x:=fst_ext e). auto. assumption.
generalize H0. clear H H0. intro H.
unfold spillWL' in H. generalize (VertexSet.remove_3 H). clear H. intro H.
assert (~Register.eq x (snd_ext e)).
intro. elim (VertexSet.remove_1 (Register.eq_sym _ _ H0) H).
generalize (VertexSet.remove_3 H). clear H. intro H.
unfold spillWL_ in H.
generalize (VertexSet.diff_1 H). intro.
generalize (VertexSet.diff_2 H). clear H. intro H.
generalize (In_spill_props _ _ _ _ _ _ _ _ H1 (refl_equal _) HWS). intro.
destruct H2. destruct H3. fold palette in H2. fold k in H2.
split.
case_eq (has_low_degree g' k x); intros.
generalize (merge_dec_interf _ _ _ _ HH HH0 H2 H5 n H0). intro.
generalize (merge_dec_K _ _ _ _ HH HH0 n H0 H2 H5). intro.
destruct H6. elim H. unfold N.
apply VertexSet.filter_3.
apply eq_K_compat.
unfold inter_interf. apply VertexSet.inter_3.
apply VertexSet.diff_3; assumption.
apply VertexSet.diff_3; assumption.
apply eq_K_1. assumption.
reflexivity.
split. unfold g'. rewrite In_merge_vertex. split; assumption.
unfold g'. rewrite precolored_merge.
intro. elim H4. apply (VertexSet.remove_3 H5).
(* spillWL <= for x different from fst_ext e *)
destruct H. destruct H0.
assert (VertexSet.In x spillWL').
unfold spillWL'. apply VertexSet.remove_2. auto.
apply VertexSet.remove_2. intro.
unfold g' in H0. rewrite In_merge_vertex in H0. destruct H0. elim H3. auto.
unfold spillWL_. apply VertexSet.diff_3.
WS_apply HWS.
split.
apply (merge_low_1 g e x k HH0 HH). assumption.
intro. rewrite H2 in H0.
unfold g' in H0. rewrite In_merge_vertex in H0. destruct H0. elim H3. auto.
assumption.
split. unfold g' in H0. rewrite In_merge_vertex in H0. intuition.
unfold g' in H1. rewrite precolored_merge in H1.
intro. elim H1. apply VertexSet.remove_2. intro.
rewrite <-H3 in H0.
unfold g' in H0. rewrite In_merge_vertex in H0. destruct H0. elim H4. auto.
assumption.
intro. unfold N in H2.
generalize (VertexSet.filter_1 (eq_K_compat k g) H2). intro.
generalize (VertexSet.filter_2 (eq_K_compat k g) H2). clear H2. intro.
generalize (eq_K_2 _ _ H2). clear H2. intro H2.
assert (has_low_degree g' k x = true).
apply merge_dec_low. auto.
generalize (VertexSet.inter_1 H3). intro. apply (VertexSet.diff_1 H4).
generalize (VertexSet.inter_2 H3). intro. apply (VertexSet.diff_1 H4).
rewrite H in H4. inversion H4.
unfold classify, get_simplifyWL, get_freezeWL, get_movesWL, get_spillWL. simpl.
case_eq (is_precolored (fst_ext e) g'); intros.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros.
case_eq (move_related g' (fst_ext e)); intros.
assumption.
assumption.
simpl. apply VertexSet.add_2. assumption.

(* freezeWL *)
split.
intro x. destruct (Register.eq_dec x (fst_ext e)).
(* x = fst_ext e *)
split; intro.
(* => *)
unfold classify, get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL in H. simpl in H.
case_eq (is_precolored (fst_ext e) g'); intros; rewrite H0 in H; simpl in H.
unfold freezeWL' in H. elim (VertexSet.remove_1 (Register.eq_sym _ _ e0) H).
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H; simpl in H.
split.
rewrite (compat_bool_low _ _ _ _ e0). assumption.
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H; simpl in H.
split.
rewrite (compat_bool_move _ _ _ e0). assumption.
rewrite precolored_equiv. intro. destruct H3.
rewrite (is_precolored_ext _ _ g' e0) in H3. rewrite H0 in H3. inversion H3.
elim (VertexSet.remove_1 (Register.eq_sym _ _ e0) H).
elim (VertexSet.remove_1 (Register.eq_sym _ _ e0) H).
(* <= *)
destruct H. destruct H0.
unfold classify, get_simplifyWL, get_spillWL, get_freezeWL, get_movesWL. simpl.
case_eq (is_precolored (fst_ext e) g'); intros; simpl.
elim H1. rewrite precolored_equiv. split.
rewrite (is_precolored_ext _ _ g' e0). assumption.
apply move_related_in_graph. assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros; simpl.
case_eq (move_related g' (fst_ext e)); intros; simpl.
apply VertexSet.add_1. intuition.
rewrite (compat_bool_move _ _ _ e0) in H0. rewrite H0 in H4. inversion H4.
rewrite (compat_bool_low _ _ _ _ e0) in H. rewrite H3 in H. inversion H.

(* x <> fst_ext e => *)
split; intro.
assert (VertexSet.In x freezeWL').
unfold classify, get_spillWL, get_simplifyWL, get_freezeWL, get_movesWL in H. simpl in H.
case_eq (is_precolored (fst_ext e) g'); intros; rewrite H0 in H.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H.
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H.
simpl in H. apply VertexSet.add_3 with (x:= fst_ext e). auto. assumption.
simpl in H. apply (VertexSet.remove_3 H).
simpl in H. apply (VertexSet.remove_3 H).
generalize H0. clear H H0. intro H.
unfold freezeWL' in H.
generalize (VertexSet.remove_3 H). clear H. intro.
assert (~Register.eq x (snd_ext e)).
intro. elim (VertexSet.remove_1 (Register.eq_sym _ _ H0) H).
generalize (VertexSet.remove_3 H). clear H. intro.
unfold freezeWL_ in H.
destruct (VertexSet.union_1 H).
unfold freezeWL__ in H1.
generalize (VertexSet.diff_1 H1). intro.
generalize (VertexSet.diff_2 H1). clear H1. intro.
unfold freeze in H2.
generalize (In_freeze_props _ _ _ _ _ _ _ _ H2 (refl_equal _) HWS). intro.
destruct H3. destruct H4. destruct H5.
fold palette in H3. fold k in H3.
split.
apply low_dec; assumption.
split.
case_eq (move_related g' x); intros.
reflexivity.
elim H1. unfold M.
apply VertexSet.filter_3.
apply compat_move_up.
Require Import Merge_Move.
Require Import Graph_Facts.
generalize (move_merge_not_move x e g HH HH0 n H0 H7 H4). intro.
destruct H8.
apply VertexSet.union_3.
apply VertexSet.inter_3.
apply VertexSet.diff_3. apply (VertexSet.inter_2 H8). assumption.
apply VertexSet.diff_3. apply (VertexSet.inter_1 H8). assumption.
apply VertexSet.union_2.
apply VertexSet.inter_3.
apply VertexSet.diff_3. apply (VertexSet.inter_2 H8). assumption.
apply VertexSet.diff_3. apply (VertexSet.inter_1 H8). assumption.
generalize (move_related_dec_1 x e g HH HH0 n H0 H4 H7). intro.
unfold pref_degree in H8. rewrite (eq_K_1 _ _ H8). simpl. assumption.
unfold g'. rewrite precolored_merge. intro. elim H6.
apply (VertexSet.remove_3 H7).

(* x in mr *)
rewrite HMR in H1.
generalize (VertexSet.filter_1 (compat_bool_move _) H1). intro.
generalize (VertexSet.filter_2 (compat_bool_move _) H1). clear H1. intro H1.
unfold N in H2.
generalize (VertexSet.filter_1 (eq_K_compat _ _) H2). intro.
generalize (VertexSet.filter_2 (eq_K_compat _ _) H2). clear H2. intro.
generalize (eq_K_2 _ _ H2). clear H2. intro.
split.
apply merge_dec_low. auto.
apply (VertexSet.diff_1 (VertexSet.inter_1 H3)).
apply (VertexSet.diff_1 (VertexSet.inter_2 H3)).
split.
apply move_related_merge_true; auto.
intro.
generalize (VertexSet.inter_1 H4). intro.
generalize (VertexSet.inter_2 H4). clear H4. intro.
generalize (VertexSet.inter_1 H3). intro.
generalize (VertexSet.inter_2 H3). clear H3. intro.
generalize (VertexSet.diff_1 H3). clear H3. intro.
generalize (VertexSet.diff_1 H6). intro.
generalize (VertexSet.diff_2 H6). clear H6. intro.
elim (interf_pref_conflict x (snd_ext e) g).
split.
rewrite in_pref in H4. destruct H4.
unfold Prefere. exists x0. assumption.
unfold Interfere. rewrite <-in_interf. assumption.
intro.
generalize (VertexSet.inter_1 H4). intro.
generalize (VertexSet.inter_2 H4). clear H4. intro.
generalize (VertexSet.inter_1 H3). intro.
generalize (VertexSet.inter_2 H3). clear H3. intro.
generalize (VertexSet.diff_1 H3). clear H3. intro.
generalize (VertexSet.diff_1 H6). intro.
generalize (VertexSet.diff_2 H6). clear H6. intro.
elim (interf_pref_conflict x (fst_ext e) g).
split.
rewrite in_pref in H4. destruct H4.
unfold Prefere. exists x0. assumption.
unfold Interfere. unfold iadj_fst_ in H7. rewrite in_interf in H7. auto.
unfold g'. rewrite precolored_merge.
intro. elim (VertexSet.diff_2 (VertexSet.inter_1 H3)).
apply (VertexSet.remove_3 H4).

(* <= *)
assert (VertexSet.In x freezeWL').
destruct H. destruct H0.
unfold freezeWL'.
apply VertexSet.remove_2. auto.
apply VertexSet.remove_2.
intro. elim (not_in_merge_snd e g HH HH0).
apply move_related_in_graph. rewrite (compat_bool_move _ _ _ H2). assumption.
unfold freezeWL_.
case_eq (has_low_degree g k x); intros.
apply VertexSet.union_2.
unfold freezeWL__.
apply VertexSet.diff_3.
WS_apply HWS.
split. assumption.
split.
apply (move_related_merge_move_related g e x HH HH0). assumption.
unfold g' in H1. rewrite precolored_merge in H1. intro. elim H1.
apply VertexSet.remove_2. intro. elim (not_in_merge_snd _ _ HH HH0).
apply move_related_in_graph. rewrite (compat_bool_move _ _ _ H4). assumption. assumption.
intro. unfold M in H3.
generalize (VertexSet.filter_2 (compat_move_up _ _) H3). intro.
assert (move_related g' x = false).
apply move_related_change_charac.
generalize (VertexSet.filter_1 (compat_move_up _ _) H3). intro.
unfold iadj_padj_interf in H5.
destruct (VertexSet.union_1 H5).
apply VertexSet.union_3.
apply VertexSet.inter_3.
apply (VertexSet.diff_1 (VertexSet.inter_2 H6)).
apply (VertexSet.diff_1 (VertexSet.inter_1 H6)).
apply VertexSet.union_2.
apply VertexSet.inter_3.
apply (VertexSet.diff_1 (VertexSet.inter_2 H6)).
apply (VertexSet.diff_1 (VertexSet.inter_1 H6)).
case_eq (eq_K 1 (VertexSet.cardinal (preference_adj x g))); intros.
symmetry. apply (eq_K_2 _ _ H5). rewrite H5 in H4. inversion H4.
rewrite H0 in H5. inversion H5.
apply VertexSet.union_3.
rewrite HMR. apply VertexSet.filter_3.
apply compat_bool_move.
unfold N. apply VertexSet.filter_3.
apply eq_K_compat.
generalize (merge_dec_interf x e k g HH HH0 H2 H n). intro.
destruct H3.
intro. elim (not_in_merge_snd e g HH HH0). apply move_related_in_graph.
rewrite (compat_bool_move _ _ _ (Register.eq_sym _ _ H3)). assumption.
apply VertexSet.inter_3. 
apply VertexSet.diff_3. assumption. unfold pre. 
unfold g' in H1. rewrite precolored_merge in H1. intro. elim H1.
apply VertexSet.remove_2. intro.
elim (not_in_merge_snd _ _ HH HH0). rewrite H6. apply move_related_in_graph. assumption.
assumption.
apply VertexSet.diff_3. assumption. unfold pre. 
unfold g' in H1. rewrite precolored_merge in H1. intro. elim H1.
apply VertexSet.remove_2. intro.
elim (not_in_merge_snd _ _ HH HH0). rewrite H6. apply move_related_in_graph. assumption.
assumption.
apply eq_K_1. apply (merge_dec_K x e k g HH HH0); auto.
intro. elim (not_in_merge_snd e g HH HH0).
apply move_related_in_graph. rewrite (compat_bool_move _ _ _ H3) in H0. assumption.
apply (move_related_merge_move_related g e x HH HH0). assumption.

unfold classify, get_spillWL, get_freezeWL, get_simplifyWL, get_movesWL; simpl.
case_eq (is_precolored (fst_ext e) g'); intro; simpl.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intro; simpl.
case_eq (move_related g' (fst_ext e)); intro; simpl.
apply VertexSet.add_2. assumption.
apply VertexSet.remove_2. auto. assumption.
apply VertexSet.remove_2. auto. assumption.

(* simplifyWL *)
split. intro x.
(* x = fst_ext e *)
destruct (Register.eq_dec x (fst_ext e)).
(* => *)
split; intros.
unfold classify, get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL in H. simpl in H.
case_eq (is_precolored (fst_ext e) g'); intros; rewrite H0 in H.
simpl in H. unfold simplifyWL' in H.
destruct (VertexSet.union_1 H).
unfold simplifyWL_ in H1.
destruct (VertexSet.union_1 H1).
generalize (In_simplify_props _ _ _ _ _ _ _ _ H2 (refl_equal _) HWS). intro.
destruct H3. destruct H4. destruct H5.
elim H6. apply VertexSet.remove_3 with (x:=snd_ext e). 
rewrite <-(precolored_merge e (irc_g ircg) HH HH0). rewrite precolored_equiv.
rewrite (is_precolored_ext _ _ (merge e (irc_g ircg) HH HH0) e0). split. eassumption.
rewrite In_merge_vertex. split. assumption. 
intro. elim (In_graph_edge_diff_ext e g). assumption.
rewrite <-H7. assumption.
rewrite HNMR in H2.
generalize (VertexSet.filter_1 (compat_not_compat (compat_bool_move _)) H2). intro.
unfold N in H3.
generalize (VertexSet.filter_1 (eq_K_compat _ _) H3). intro.
generalize (VertexSet.inter_1 H4). intro.
generalize (VertexSet.inter_2 H4). clear H4. intro.
elim (VertexSet.diff_2 H5). unfold pre.
apply VertexSet.remove_3 with (x:=snd_ext e). 
rewrite <-(precolored_merge e g HH HH0). rewrite precolored_equiv.
split. rewrite (is_precolored_ext _ _ (merge e g HH HH0) e0). eassumption.
rewrite In_merge_vertex. split. 
rewrite e0. apply (proj1 (In_graph_edge_in_ext _ _ HH)).
intro. elim (not_in_interf_self (snd_ext e) g). rewrite H6 in H4.
apply (VertexSet.diff_1 H4).
unfold M in H1.
generalize (VertexSet.filter_1 (compat_move_up _ _) H1). intro.
unfold iadj_padj_interf in H2.
destruct (VertexSet.union_1 H2).
generalize (VertexSet.inter_1 H3). intro. generalize (VertexSet.diff_2 H4). intro.
elim H5. unfold pre. 
apply VertexSet.remove_3 with (x:=snd_ext e). rewrite <-(precolored_merge e g HH HH0).
rewrite precolored_equiv. split.
rewrite (is_precolored_ext _ _ (merge e g HH HH0) e0). eassumption.
rewrite e0. rewrite In_merge_vertex. split.
apply (proj1 (In_graph_edge_in_ext _ _ HH)). 
intro. elim (In_graph_edge_diff_ext e g HH). auto.
generalize (VertexSet.inter_1 H3). intro. generalize (VertexSet.diff_2 H4). intro.
elim H5. unfold pre. 
apply VertexSet.remove_3 with (x:=snd_ext e). rewrite <-(precolored_merge e g HH HH0).
rewrite precolored_equiv. split.
rewrite (is_precolored_ext _ _ (merge e g HH HH0) e0). eassumption.
rewrite e0. rewrite In_merge_vertex. split.
apply (proj1 (In_graph_edge_in_ext _ _ HH)).
intro. elim (In_graph_edge_diff_ext e g HH). auto.

(* fst_ext e is not precolored *)
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H.
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H.
simpl in H. unfold simplifyWL' in H.
destruct (VertexSet.union_1 H).
unfold simplifyWL_ in H3.
destruct (VertexSet.union_1 H3).
assert (move_related g x = false).
apply (proj1 (proj2 (In_simplify_props _ _ _ _ _ _ _ _ H4 (refl_equal _) HWS))).
assert (move_related g x = true).
rewrite (compat_bool_move _ _ _ e0).
apply (proj1 (Aff_edge_aff _ _ HH HH0)).
rewrite H5 in H6. inversion H6.
rewrite HNMR in H4.
generalize (VertexSet.filter_1 (compat_not_compat (compat_bool_move _)) H4). intro.
generalize (VertexSet.filter_2 (compat_not_compat (compat_bool_move _)) H4). clear H4. intro H4.
unfold N in H5.
generalize (VertexSet.filter_1 (eq_K_compat _ _) H5). intro.
generalize (VertexSet.filter_2 (eq_K_compat _ _) H5). clear H5. intro.
generalize (eq_K_2 _ _ H5). clear H5. intro.
assert (move_related g x = true).
rewrite (compat_bool_move _ _ _ e0).
apply (proj1 (Aff_edge_aff _ _ HH HH0)).
rewrite H7 in H4. inversion H4.
unfold M in H3.
generalize (VertexSet.filter_1 (compat_move_up _ _) H3). intro.
unfold iadj_padj_interf in H4.
destruct (VertexSet.union_1 H4).
elim (not_in_pref_self (fst_ext e) g). rewrite e0 in H5.
apply (VertexSet.diff_1 (VertexSet.inter_1 H5)).
elim (not_in_interf_self (fst_ext e) g). rewrite e0 in H5.
apply (VertexSet.diff_1 (VertexSet.inter_2 H5)).
simpl in H.
split. rewrite (compat_bool_low _ _ _ _ e0). assumption.
split. rewrite (compat_bool_move _ _ _ e0). assumption.
split. unfold g'. rewrite In_merge_vertex. split. 
rewrite e0. apply (proj1 (In_graph_edge_in_ext e g HH)).
intro. elim (In_graph_edge_diff_ext e g HH). rewrite <-H3. assumption.

rewrite precolored_equiv. intro. destruct H3.
rewrite (is_precolored_ext _ _ g' e0) in H3. rewrite H0 in H3. inversion H3.
simpl in H.
unfold simplifyWL' in H.
destruct (VertexSet.union_1 H).
unfold simplifyWL_ in H2.
destruct (VertexSet.union_1 H2).
generalize (In_simplify_props _ _ _ _ _ _ _ _ H3 (refl_equal _) HWS). intro.
destruct H4. destruct H5. destruct H6.
generalize (proj1 (Aff_edge_aff _ _ HH HH0)). intro.
rewrite (compat_bool_move _ _ _ e0) in H5. fold g in H5. rewrite H5 in H8. inversion H8.
rewrite HNMR in H3.
generalize (VertexSet.filter_2 (compat_not_compat (compat_bool_move _)) H3). intro.
generalize (proj1 (Aff_edge_aff _ _ HH HH0)). intro.
rewrite (compat_bool_move _ _ _ e0) in H4. rewrite H5 in H4. inversion H4.
unfold M in H2.
generalize (VertexSet.filter_1 (compat_move_up _ _) H2). intro.
unfold iadj_padj_interf in H3.
destruct (VertexSet.union_1 H3).
elim (not_in_pref_self (fst_ext e) g). rewrite e0 in H4.
apply (VertexSet.diff_1 (VertexSet.inter_1 H4)).
elim (not_in_interf_self (fst_ext e) g). rewrite e0 in H4.
apply (VertexSet.diff_1 (VertexSet.inter_2 H4)).

(* <= *)
destruct H. destruct H0. destruct H1.
unfold classify, get_spillWL, get_simplifyWL, get_freezeWL, get_movesWL. simpl.
case_eq (is_precolored (fst_ext e) g'); intros; simpl.
rewrite precolored_equiv in H2. elim H2. 
split. rewrite (is_precolored_ext _ _ g' e0). assumption. assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros; simpl.
case_eq (move_related g' (fst_ext e)); intros; simpl.
rewrite (compat_bool_move _ _ _ e0) in H0. rewrite H0 in H5. inversion H5.
apply VertexSet.add_1. intuition.
rewrite (compat_bool_low _ _ _ _ e0) in H. rewrite H in H4. inversion H4.

(* x <> fst_ext e *)
(* simplifyWL => *)
split; intros.
assert (VertexSet.In x simplifyWL').
unfold classify, get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL in H. simpl in H.
case_eq (is_precolored (fst_ext e) g'); intros; rewrite H0 in H.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros; rewrite H1 in H.
case_eq (move_related g' (fst_ext e)); intros; rewrite H2 in H.
assumption.
simpl in H. apply VertexSet.add_3 with (x:=fst_ext e). auto. assumption.
assumption.
generalize H0. clear H H0. intro H.
unfold simplifyWL' in H.
destruct (VertexSet.union_1 H).
unfold simplifyWL_ in H0.
destruct (VertexSet.union_1 H0).
generalize (In_simplify_props _ _ _ _ _ _ _ _ H1 (refl_equal _) HWS). intro.
destruct H2. destruct H3. destruct H4.
split.
apply low_dec; auto.
intro. generalize (proj2 (Aff_edge_aff e g HH HH0)). intro.
rewrite (compat_bool_move _ _ _ H6) in H3. fold g in H3.
rewrite H3 in H7. inversion H7.
split.
apply move_merge_false; auto.
split. unfold g'. rewrite In_merge_vertex. split. assumption.
intro. generalize (proj2 (Aff_edge_aff e g HH HH0)). intro.
rewrite (compat_bool_move _ _ _ H6) in H3. fold g in H3.
rewrite H3 in H7. inversion H7.
unfold g'. rewrite precolored_merge. intro. elim H5.
apply (VertexSet.remove_3 H6).
rewrite HNMR in H1.
generalize (VertexSet.filter_1 (compat_not_compat (compat_bool_move _)) H1). intro.
generalize (VertexSet.filter_2 (compat_not_compat (compat_bool_move _)) H1). clear H1. intro.
unfold N in H2.
generalize (VertexSet.filter_1 (eq_K_compat _ _) H2). intro.
generalize (VertexSet.filter_2 (eq_K_compat _ _) H2). clear H2. intro.
generalize (eq_K_2 _ _ H2). clear H2. intro.
split.
apply merge_dec_low. auto.
apply (VertexSet.diff_1 (VertexSet.inter_1 H3)).
apply (VertexSet.diff_1 (VertexSet.inter_2 H3)).
split.
apply move_merge_false; auto. case_eq (move_related g x); intros.
rewrite H4 in H1. inversion H1. reflexivity.
split. unfold g'. rewrite In_merge_vertex. split.
apply (in_interf_in x (fst_ext e) g).
apply (VertexSet.diff_1 (VertexSet.inter_1 H3)).
intro. generalize (proj2 (Aff_edge_aff e g HH HH0)). intro.
rewrite (compat_bool_move _ _ _ (Register.eq_sym _ _ H4)) in H5.
rewrite H5 in H1. inversion H1.
unfold g'. rewrite precolored_merge. intro.
elim (VertexSet.diff_2 (VertexSet.inter_1 H3)). apply (VertexSet.remove_3 H4).
unfold M in H0.
generalize (VertexSet.filter_1 (compat_move_up _ _) H0). intro.
generalize (VertexSet.filter_2 (compat_move_up _ _) H0). clear H0. intro.
assert (VertexSet.cardinal (preference_adj x g) = 1).
case_eq (eq_K 1 (VertexSet.cardinal (preference_adj x g))); intros.
generalize (eq_K_2 _ _ H2). clear H2. intro. auto.
rewrite H2 in H0. inversion H0.
generalize (eq_K_1 _ _ H2). intro. rewrite H3 in H0. simpl in H0.
split.
apply low_dec; auto.
intro.
destruct (VertexSet.union_1 H1).
elim (not_in_interf_self (snd_ext e) g).
rewrite H4 in H5. apply (VertexSet.diff_1 (VertexSet.inter_2 H5)).
elim (not_in_pref_self (snd_ext e) g).
rewrite H4 in H5. apply (VertexSet.diff_1 (VertexSet.inter_1 H5)).
split.
apply move_related_change_charac.
destruct (VertexSet.union_1 H1).
apply VertexSet.union_3.
apply VertexSet.inter_3.
apply (VertexSet.diff_1 (VertexSet.inter_2 H4)).
apply (VertexSet.diff_1 (VertexSet.inter_1 H4)).
apply VertexSet.union_2.
apply VertexSet.inter_3.
apply (VertexSet.diff_1 (VertexSet.inter_2 H4)).
apply (VertexSet.diff_1 (VertexSet.inter_1 H4)).
assumption.
split.
unfold g'. rewrite In_merge_vertex. split.
destruct (VertexSet.union_1 H1).
apply (in_interf_in x (snd_ext e) g). apply (VertexSet.diff_1 (VertexSet.inter_2 H4)).
apply (in_interf_in x (fst_ext e) g). apply (VertexSet.diff_1 (VertexSet.inter_2 H4)).
intro. destruct (VertexSet.union_1 H1).
elim (not_in_interf_self (snd_ext e) g).
rewrite H4 in H5. apply (VertexSet.diff_1 (VertexSet.inter_2 H5)).
elim (not_in_pref_self (snd_ext e) g).
rewrite H4 in H5. apply (VertexSet.diff_1 (VertexSet.inter_1 H5)).
unfold g'. rewrite precolored_merge.
intro.
destruct (VertexSet.union_1 H1).
elim (VertexSet.diff_2 (VertexSet.inter_1 H5)). apply (VertexSet.remove_3 H4).
elim (VertexSet.diff_2 (VertexSet.inter_1 H5)). apply (VertexSet.remove_3 H4).

(* <= *)
destruct H. destruct H0. destruct H1.
assert (VertexSet.In x simplifyWL').
unfold simplifyWL'.
case_eq (move_related g x); intros.
apply VertexSet.union_3.
unfold M.
apply VertexSet.filter_3.
apply compat_move_up.
assert (~Register.eq x (snd_ext e)) as Hsnd.
intro. elim (not_in_merge_snd e g HH HH0). rewrite H4 in H1. assumption. 
generalize (move_merge_not_move _ _ _ _ HH0 n Hsnd H0 H3). intro.
destruct H4.
generalize (VertexSet.inter_1 H4). generalize (VertexSet.inter_2 H4). intros.
apply VertexSet.union_3.
apply VertexSet.inter_3.
apply VertexSet.diff_3. assumption.
unfold g' in H2. rewrite precolored_merge in H2. intro. elim H2.
apply VertexSet.remove_2. intro. elim (not_in_merge_snd _ _ HH HH0).
rewrite H8. assumption.
assumption.
apply VertexSet.diff_3. assumption.
unfold g' in H2. rewrite precolored_merge in H2. intro. elim H2.
apply VertexSet.remove_2. intro. elim (not_in_merge_snd _ _ HH HH0).
rewrite H8. assumption.
assumption.
generalize (VertexSet.inter_1 H4). generalize (VertexSet.inter_2 H4). intros.
apply VertexSet.union_2.
apply VertexSet.inter_3.
apply VertexSet.diff_3. assumption.
unfold g' in H2.
rewrite precolored_merge in H2. intro. elim H2.
apply VertexSet.remove_2. intro. elim (not_in_merge_snd _ _ HH HH0).
rewrite H8. assumption.
assumption.
apply VertexSet.diff_3. assumption.
unfold g' in H2.
rewrite precolored_merge in H2. intro. elim H2.
apply VertexSet.remove_2. intro. elim (not_in_merge_snd _ _ HH HH0).
rewrite H8. assumption.
assumption.

assert (~Register.eq x (snd_ext e)) as Hsnd.
intro. rewrite H4 in H1. elim (not_in_merge_snd e g HH HH0 H1).
generalize (move_related_dec_1 _ _ _ HH HH0 n Hsnd H3 H0). intro.
unfold pref_degree in H4. rewrite (eq_K_1 _ _ H4). simpl.
case_eq (has_low_degree g k x); intros.
reflexivity.
generalize (move_merge_not_move _ _ _ _ HH0 n Hsnd H0 H3). intro.
assert (~Register.eq x (snd_ext e)).
intro. rewrite H7 in H1. elim (not_in_merge_snd e g HH HH0 H1).
generalize (merge_dec_interf _ _ _ _ HH HH0 H5 H n H7). intro.
destruct H8. destruct H6.
generalize (VertexSet.inter_2 H6). intro.
elim (interf_pref_conflict x (snd_ext e) g).
rewrite in_pref in H10.
split.
unfold Prefere. assumption.
unfold Interfere. rewrite <-in_interf. assumption.
elim (interf_pref_conflict x (fst_ext e) g).
generalize (VertexSet.inter_2 H6). intro.
rewrite in_pref in H10.
split.
unfold Prefere. assumption.
unfold Interfere. rewrite <-in_interf. auto.
apply VertexSet.union_2.
unfold simplifyWL_.
case_eq (has_low_degree g k x); intros.
apply VertexSet.union_2.

WS_apply HWS.
split. assumption.
split. assumption.
split. unfold g' in H1. rewrite In_merge_vertex in H1. intuition.
unfold g' in H2. rewrite precolored_merge in H2.
intro. elim H2. apply VertexSet.remove_2.
intro. apply (not_in_merge_snd _ _ HH HH0). rewrite H6. assumption.
assumption.

apply VertexSet.union_3.
rewrite HNMR.
apply VertexSet.filter_3.
apply compat_not_compat. apply compat_bool_move.
unfold N. apply VertexSet.filter_3.
apply eq_K_compat.
assert (~Register.eq x (snd_ext e)).
intro. rewrite H5 in H1. elim (not_in_merge_snd e g HH HH0 H1).
generalize (merge_dec_interf _ _ _ _ HH HH0 H4 H n H5).
intro. destruct H6. apply VertexSet.inter_3.
apply VertexSet.diff_3. assumption.
unfold g' in H2. rewrite precolored_merge in H2.
intro. elim H2. apply VertexSet.remove_2. auto.
assumption.
apply VertexSet.diff_3. assumption.
unfold g' in H2. rewrite precolored_merge in H2.
intro. elim H2. apply VertexSet.remove_2. auto.
assumption.
apply eq_K_1.
apply (merge_dec_K x e k g HH HH0 n).
intro. rewrite H5 in H1. elim (not_in_merge_snd e g HH HH0 H1).
assumption.
assumption.
rewrite H3. auto.

unfold classify, get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL; simpl.
case_eq (is_precolored (fst_ext e) g'); intros.
assumption.
case_eq (has_low_degree g' k (fst_ext e)); intros.
case_eq (move_related g' (fst_ext e)); intros.
assumption.
simpl. apply VertexSet.add_2; auto.
assumption.

(* moves !!!!! *)
split; intros.
assert (EdgeSet.In e0 movesWL').
unfold classify,get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL in H; simpl in H;
destruct (is_precolored (fst_ext e) g');
destruct (has_low_degree g' k (fst_ext e));
destruct (move_related g' (fst_ext e)); simpl; assumption.
unfold movesWL' in H0.
unfold g'. rewrite <-AE_merge_wl. eassumption.
intros. rewrite <-In_graph_aff_edge_in_AE.
rewrite moves_AE. unfold moves. reflexivity. eassumption.

destruct H. simpl.
assert (EdgeSet.In e0 movesWL').
unfold movesWL'. unfold g', padj_fst_, padj_snd_. rewrite AE_merge_wl.
split; assumption.

intros. rewrite <-In_graph_aff_edge_in_AE.
rewrite moves_AE. unfold moves. reflexivity. eassumption.
unfold classify,get_spillWL, get_simplifyWL, get_movesWL, get_freezeWL; simpl;
destruct (is_precolored (fst_ext e) g');
destruct (has_low_degree g' k (fst_ext e));
destruct (move_related g' (fst_ext e)); simpl; assumption.

rewrite <-VertexSet.partition_2. rewrite Hmr. intuition.
apply compat_bool_move.
rewrite <-VertexSet.partition_1. rewrite Hmr. intuition.
apply compat_bool_move.
Qed.

Lemma WS_coalesce : forall e ircg H H0,
WS_properties (merge e (irc_g ircg) H H0) 
                        (irc_k ircg)
                        (merge_wl3 e ircg (merge e (irc_g ircg) H H0) H H0).

Proof.
intros. rewrite <-(Hk ircg). apply ws_merge3.
Qed.