ConcurrentLambda.v 113 KB
Newer Older
1
2
3
4
5
6
7
8
Require Export Expression.
Require Export Locations.
Require Import LocationMap.
Require Export Choreography.

Require Import Coq.Arith.Arith.
Require Import Coq.Lists.List.
Require Import Permutation.
Andrew Hirsch's avatar
Andrew Hirsch committed
9
Require Import Coq.Classes.RelationClasses.
10
11
12
13
14
15
16
17

From Equations Require Import Equations.

Import ListNotations.

Module InternalConcurrentLambda (Import E : Expression) (L : Locations) (LM : LocationMap L).

  Module C := (Choreography E L).
Andrew Hirsch's avatar
Andrew Hirsch committed
18
  Module LMF := (LocationMapFacts L LM).
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

  Definition Loc := L.t.
  
  Definition LRChoice : Set := C.LRChoice.

  (* Concurrent expressions.
     I'm going to use the convention that `E` is a concurrent expression, while `e` is a local expression.
   *)
  Inductive ConExpr : Set :=
  | Var : nat -> ConExpr
  | Unit : ConExpr
  | Ret : Expr -> ConExpr
  | If : Expr -> ConExpr -> ConExpr -> ConExpr
  | Send : Loc -> Expr -> ConExpr -> ConExpr
  | Recv : Loc -> ConExpr -> ConExpr
  | Choose : Loc -> LRChoice -> ConExpr -> ConExpr (* Choose LR for l; E *)
  | AllowChoiceL : Loc -> ConExpr -> ConExpr (* Allow l choice { L |-> E } *)
  | AllowChoiceR : Loc -> ConExpr -> ConExpr(* Allow l choice { R |-> E } *)
  | AllowChoiceLR : Loc -> ConExpr -> ConExpr -> ConExpr(* Allow l choice { L |-> E1; R |-> E2 } *)
  | LetRet : ConExpr -> ConExpr -> ConExpr (* Let Ret x := E1 in E2 *)
  | RecLocal : ConExpr -> ConExpr (* rec f(x) := E *)
  | AppLocal : ConExpr -> Expr -> ConExpr (* rec f(X) := E *) (* Note that these bind two variables, f and (x | X) *)
  | RecGlobal : ConExpr -> ConExpr
  | AppGlobal : ConExpr -> ConExpr -> ConExpr.
  Global Hint Constructors ConExpr : ConExpr.

Andrew Hirsch's avatar
Andrew Hirsch committed
45
46
47
48
49
50
51
52
  Inductive ConExprVal : ConExpr -> Prop :=
    RetVal : forall v, ExprVal v -> ConExprVal (Ret v)
  | UnitVal : ConExprVal Unit
  | RecLocalVal : forall B, ConExprVal (RecLocal B)
  | RecGlobalVal : forall B, ConExprVal (RecGlobal B).
  Global Hint Constructors ConExprVal : ConExpr.


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
  Instance ConExprEqDec : EqDec ConExpr.
  Proof using.
    unfold EqDec; decide equality;
      try (first [apply Nat.eq_dec | apply ExprEqDec | apply ChorEqDec | apply L.eq_dec]).
    destruct l0; destruct l2; try (left; auto; fail); try (right; intro H'; inversion H').
  Defined.

  Definition UpRename : (nat -> nat) -> nat -> nat := fun ξ n => match n with
                                                  | 0 => 0
                                                  | S n => S (ξ n)
                                                  end.

  Definition IdRenaming := fun n : nat => n.

  Lemma UpIdRenaming : forall n, IdRenaming n = UpRename IdRenaming n.
  Proof using.
    intro n; destruct n; unfold IdRenaming; cbn; reflexivity.
  Qed.
  Lemma UpRenameExt : forall ξ1 ξ2, (forall n, ξ1 n = ξ2 n) -> forall n, UpRename ξ1 n = UpRename ξ2 n.
  Proof using.
    intros ξ1 ξ2 ext_eq n; destruct n; cbn; auto.
  Qed.
  
  Fixpoint ConExprLocalRename (E : ConExpr) (ξ : nat -> nat) :=
    match E with
    | Var x => Var x
    | Unit => Unit
    | Ret e => Ret (e e| ξ⟩)
    | If e E1 E2 => If (e e| ξ⟩) (ConExprLocalRename E1 ξ) (ConExprLocalRename E2 ξ)
    | Send l e E => Send l (e e| ξ⟩) (ConExprLocalRename E ξ)
    | Recv l E => Recv l (ConExprLocalRename E (UpRename ξ))
    | Choose l LR E => Choose l LR (ConExprLocalRename E ξ)
    | AllowChoiceL l E => AllowChoiceL l (ConExprLocalRename E ξ)
    | AllowChoiceR l E => AllowChoiceR l (ConExprLocalRename E ξ)
    | AllowChoiceLR l E1 E2 =>
      AllowChoiceLR l (ConExprLocalRename E1 ξ) (ConExprLocalRename E2 ξ)
    | LetRet E1 E2 => LetRet (ConExprLocalRename E1 ξ)(ConExprLocalRename E2 (UpRename ξ))
    | RecLocal E => RecLocal (ConExprLocalRename E (UpRename ξ))
    | AppLocal E e => AppLocal (ConExprLocalRename E ξ) (e e| ξ⟩)
    | RecGlobal E => RecGlobal (ConExprLocalRename E ξ)
    | AppGlobal E1 E2 => AppGlobal (ConExprLocalRename E1 ξ) (ConExprLocalRename E2 ξ)
    end.
  Notation "E ⟨cel| x ⟩" := (ConExprLocalRename E x) (at level 20).

  Lemma ConExprLocalRenameExt : forall E ξ1 ξ2, (forall n, ξ1 n = ξ2 n) -> E cel| ξ1 = E cel| ξ2.
  Proof using.
    intro E; induction E; intros ξ1 ξ2 ext_eq; cbn; auto.
    all: (repeat rewrite ExprRenameExt with (ξ2 := ξ2); auto).
    all: try (erewrite IHE1; eauto; erewrite IHE2; eauto; fail).
    all: try (erewrite IHE; eauto; fail).
    - erewrite IHE; [reflexivity|]. apply UpRenameExt; auto.
    - erewrite IHE1; [|auto]. erewrite IHE2; [reflexivity|]. apply UpRenameExt; auto.
    - erewrite IHE; [reflexivity|]. apply UpRenameExt; auto.
  Qed.

  Lemma ConExprLocalIdRename : forall E, E cel| IdRenaming = E.
  Proof using.
    intro E; induction E; cbn; auto.
    all: try rewrite ExprIdRenamingSpec; auto.
    all: try rewrite IHE1; try rewrite IHE2; try rewrite IHE; auto.
    1,3: rewrite ConExprLocalRenameExt with (ξ2 := IdRenaming); [rewrite IHE; reflexivity|];
      intro n; symmetry; apply UpIdRenaming.
    rewrite ConExprLocalRenameExt with (ξ2 := IdRenaming); [rewrite IHE2; reflexivity|].
    intro n; symmetry; apply UpIdRenaming.
  Qed.

  Lemma ConExprRenameFusion : forall E ξ1 ξ2, (E cel| ξ1) cel| ξ2 = E cel| fun n => ξ2 (ξ1 n).
  Proof using.
    intro E; induction E; intros ξ1 ξ2; cbn; auto.
    all: try rewrite ExprRenameFusion; try rewrite IHE;
      try rewrite IHE1; try rewrite IHE2; auto.
    1,3: erewrite ConExprLocalRenameExt; eauto.
    3: erewrite ConExprLocalRenameExt with (ξ1 := fun n => UpRename ξ2 (UpRename ξ1 n)); eauto.
    all: intro n; destruct n; cbn; auto.
  Qed.

                                                                                            Fixpoint ConExprGlobalRename (E : ConExpr) (ξ : nat -> nat) : ConExpr :=
    match E with
    | Var x => Var (ξ x)
    | Unit => Unit
    | Ret e => Ret e
    | If e E1 E2 => If e (ConExprGlobalRename E1 ξ) (ConExprGlobalRename E2 ξ)
    | Send l e E => Send l e (ConExprGlobalRename E ξ)
    | Recv l E => Recv l (ConExprGlobalRename E ξ)
    | Choose l LR E => Choose l LR (ConExprGlobalRename E ξ)
    | AllowChoiceL l E => AllowChoiceL l (ConExprGlobalRename E ξ)
    | AllowChoiceR l E => AllowChoiceR l (ConExprGlobalRename E ξ)
    | AllowChoiceLR l E1 E2 =>
      AllowChoiceLR l (ConExprGlobalRename E1 ξ) (ConExprGlobalRename E2 ξ)
    | LetRet E1 E2 =>
      LetRet (ConExprGlobalRename E1 ξ) (ConExprGlobalRename E2 ξ)
    | RecLocal E => RecLocal (ConExprGlobalRename E (UpRename ξ)) (* f is a global var *)
    | AppLocal E1 e => AppLocal (ConExprGlobalRename E1 ξ) e
    | RecGlobal E => RecGlobal (ConExprGlobalRename E (UpRename (UpRename ξ)))
                                                   (* f and X are both global *) 
    | AppGlobal E1 E2 => AppGlobal (ConExprGlobalRename E1 ξ) (ConExprGlobalRename E2 ξ)
    end.
  Notation "E ⟨ceg| x ⟩" := (ConExprGlobalRename E x) (at level 20).
  

  Theorem ConExprGlobalRenameExt : forall E ξ1 ξ2,
      (forall n, ξ1 n = ξ2 n) -> E ceg| ξ1 = E ceg| ξ2.
  Proof using.
    intro E; induction E; intros ξ1 ξ2 ext_eq; cbn; auto.
    all: try (erewrite IHE1; eauto; erewrite IHE2; eauto; fail).
    all: try (erewrite IHE; eauto; fail).
    - erewrite IHE; [reflexivity|]; apply UpRenameExt; auto.
    - erewrite IHE; [reflexivity|]. apply UpRenameExt; apply UpRenameExt; auto.
  Qed.  

  Theorem ConExprGlobalIdRename : forall E, E ceg| IdRenaming = E.
  Proof using.
    intros E; induction E; cbn; auto.
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    all: erewrite ConExprGlobalRenameExt; [rewrite IHE; reflexivity|].
    all: intro n; symmetry.
    2: rewrite UpRenameExt with (ξ2 := IdRenaming); [| intro m; symmetry].
    all: apply UpIdRenaming.
  Qed.

  Theorem ConExprGlobalRenameFusion :
    forall E ξ1 ξ2, (E ceg| ξ1) ceg| ξ2 = E ceg| fun n => ξ2 (ξ1 n).
  Proof using.
    intro E; induction E; intros ξ1 ξ2; cbn; auto.
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    all: erewrite ConExprGlobalRenameExt; [reflexivity|].
    all: intro n; destruct n; cbn; auto; destruct n; cbn; auto.
  Qed.

  Fixpoint ConExprLocalSubst (E : ConExpr) (σ : nat -> Expr) :=
    match E with
    | Var x => Var x
    | Unit => Unit
    | Ret e => Ret (e [e| σ])
    | If e E1 E2 => If (e [e| σ]) (ConExprLocalSubst E1 σ) (ConExprLocalSubst E2 σ)
    | Send l e E => Send l (e [e| σ]) (ConExprLocalSubst E σ)
    | Recv l E => Recv l (ConExprLocalSubst E (ExprUpSubst σ))
    | Choose l LR E => Choose l LR (ConExprLocalSubst E σ)
    | AllowChoiceL l E => AllowChoiceL l (ConExprLocalSubst E σ)
    | AllowChoiceR l E => AllowChoiceR l (ConExprLocalSubst E σ)
    | AllowChoiceLR l E1 E2 =>
      AllowChoiceLR l (ConExprLocalSubst E1 σ) (ConExprLocalSubst E2 σ)
    | LetRet E1 E2 =>
      LetRet (ConExprLocalSubst E1 σ) (ConExprLocalSubst E2 (ExprUpSubst σ))
    | RecLocal E => RecLocal (ConExprLocalSubst E (ExprUpSubst σ))
    | AppLocal E e => AppLocal (ConExprLocalSubst E σ) (e [e| σ])
    | RecGlobal E => RecGlobal (ConExprLocalSubst E σ)
    | AppGlobal E1 E2 => AppGlobal (ConExprLocalSubst E1 σ) (ConExprLocalSubst E2 σ)
    end.
  Notation "E [cel| s ]" := (ConExprLocalSubst E s) (at level 20).

  Lemma ConExprLocalSubstExt : forall E σ1 σ2, (forall n, σ1 n = σ2 n) -> E [cel| σ1] = E [cel| σ2].
  Proof using.
    intro E; induction E; cbn; intros σ1 σ2 ext_eq; auto.
    all: try erewrite ExprSubstExt; eauto.
    all: try erewrite IHE1; eauto; try erewrite IHE2; eauto; try erewrite IHE; eauto.
    all: intro n; destruct n; cbn; auto; rewrite ext_eq; reflexivity.
  Qed.

  
  Lemma ConExprLocalIdSubst : forall E, E [cel| ExprIdSubst] = E.
  Proof using.
    intro E; induction E; cbn; auto.
    all: try rewrite ExprIdentitySubstSpec; try rewrite IHE;
      try rewrite IHE1; try rewrite IHE2; auto.
    1,3: erewrite ConExprLocalSubstExt; [rewrite IHE; reflexivity|].
    3: erewrite ConExprLocalSubstExt; [rewrite IHE2; reflexivity|].
    all: intro n; symmetry; apply ExprUpSubstId.
  Qed.

  Theorem ConExprLocalRenameSpec : forall E ξ, E cel| ξ⟩ = E [cel| (fun n => ExprVar (ξ  n))].
  Proof using.
    intro E; induction E; cbn; intro ξ; auto.
    all: try rewrite ExprRenameSpec; try rewrite IHE;
      try rewrite IHE1; try rewrite IHE2; auto.
    1,3: erewrite ConExprLocalSubstExt; [reflexivity|].
    3: erewrite ConExprLocalSubstExt with (σ1 := fun n => ExprVar (UpRename ξ n));
      [reflexivity|].
    all: intro n; destruct n; cbn; auto; rewrite ExprRenameVar; reflexivity.
  Qed.

  Definition GlobalUpSubst : (nat -> ConExpr) -> nat -> ConExpr :=
    fun f n => match n with
            | 0 => Var 0
            | S n => (f n) ceg| S
            end.

  Definition GlobalIdSubst : nat -> ConExpr := Var.

  Lemma GlobalUpIdSubst : forall n, GlobalUpSubst GlobalIdSubst n = GlobalIdSubst n.
  Proof using.
    intro n; destruct n; cbn; unfold GlobalIdSubst; reflexivity.
  Qed.

  Lemma GlobalUpSubstExt : forall σ1 σ2, (forall n, σ1 n = σ2 n) ->
                                    forall n, GlobalUpSubst σ1 n = GlobalUpSubst σ2 n.
  Proof using.
    intros σ1 σ2 ext_eq n; destruct n; cbn; auto; rewrite ext_eq; auto.
  Qed.

  Fixpoint ConExprGlobalSubst (E : ConExpr) (σ : nat -> ConExpr) :=
    match E with
    | Var x => σ x
    | Ret e => Ret e
    | Unit => Unit
    | If e E1 E2 => If e (ConExprGlobalSubst E1 σ) (ConExprGlobalSubst E2 σ)
    | Send l e E => Send l e (ConExprGlobalSubst E σ)
    | Recv l E => Recv l (ConExprGlobalSubst E (fun n => σ n cel| S))
    | Choose l LR E => Choose l LR (ConExprGlobalSubst E σ)
    | AllowChoiceL l E => AllowChoiceL l (ConExprGlobalSubst E σ)
    | AllowChoiceR l E => AllowChoiceR l (ConExprGlobalSubst E σ)
    | AllowChoiceLR l E1 E2 =>
      AllowChoiceLR l (ConExprGlobalSubst E1 σ) (ConExprGlobalSubst E2 σ)
    | LetRet E1 E2 => LetRet (ConExprGlobalSubst E1 σ) (ConExprGlobalSubst E2 (fun n => σ n cel| S))
    | RecLocal E => RecLocal (ConExprGlobalSubst E (GlobalUpSubst (fun n => σ n cel| S))) (* f is global *)
    | AppLocal E e => AppLocal (ConExprGlobalSubst E σ) e
    | RecGlobal E => RecGlobal (ConExprGlobalSubst E (GlobalUpSubst (GlobalUpSubst σ)))
                              (* f, X are both global *)
    | AppGlobal E1 E2 => AppGlobal (ConExprGlobalSubst E1 σ) (ConExprGlobalSubst E2 σ)
    end.
  Notation "E [ceg| s ]" := (ConExprGlobalSubst E s) (at level 20).

  Lemma ConExprGlobalSubstExt : forall E σ1 σ2, (forall n, σ1 n = σ2 n) -> E [ceg| σ1] = E [ceg| σ2].
  Proof using.
    intro E; induction E; cbn; intros σ1 σ2 ext_eq; auto.
    all: try erewrite IHE1; eauto; try erewrite IHE2; eauto.
    all: try erewrite IHE; eauto.
    all: destruct n; cbn; auto; try rewrite ext_eq; auto. destruct n; cbn; auto.
    rewrite ext_eq; reflexivity.
  Qed.

  Lemma GlobalIdSubstSpec : forall E, E [ceg| GlobalIdSubst ] = E.
  Proof using.
    intro E; induction E; cbn; auto.
    all: try (fold (GlobalIdSubst)).
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    2: rewrite ConExprGlobalSubstExt with (σ2 := GlobalIdSubst); [rewrite IHE2|]; auto.
    all: rewrite ConExprGlobalSubstExt with (σ2 := GlobalIdSubst); [rewrite IHE|]; auto.  
    apply GlobalUpIdSubst.
    intro n; rewrite GlobalUpSubstExt with (σ2 := GlobalIdSubst); apply GlobalUpIdSubst.
  Qed.

  Lemma ConExprGlobalRenameSubstFusion : forall E ξ σ,
      (E ceg| ξ⟩) [ceg| σ] = E [ceg| fun n => σ (ξ n)].
  Proof using.
    intro E; induction E; cbn; intros ξ σ; auto.
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    all: apply f_equal.
    all: apply ConExprGlobalSubstExt.
    all: intro n; unfold GlobalUpSubst; unfold UpRename; cbn; auto.
    all: destruct n; auto.
    destruct n; auto.
  Qed.    

  Lemma LocalSubstGlobalRenameComm : forall E σ ξ, (E [cel| σ]) ceg| ξ⟩ = (E ceg| ξ⟩) [cel|σ].
  Proof using.
    intro E; induction E; cbn; intros σ ξ; auto.
    all: try rewrite IHE1; try rewrite IHE; try rewrite IHE2; auto.
  Qed.
  
  Lemma GlobalRenameSpec : forall E ξ, E ceg| ξ⟩ = E [ceg| fun n => Var (ξ n)].
  Proof using.
    intro E; induction E; cbn; intro ξ; auto.
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    all: erewrite ConExprGlobalSubstExt; [reflexivity|].
    all: intro n; destruct n; cbn; auto; destruct n; cbn; auto.
  Qed.

  Lemma ConExprGlobalSubstRenameFusion : forall E σ ξ,
      (E [ceg| σ]) ceg| ξ⟩ = E [ceg| fun n => (σ n) ceg| ξ⟩].
  Proof using.
    intro E; induction E; intros σ ξ; cbn; auto.
    all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto.
    all: apply f_equal; apply ConExprGlobalSubstExt; intro n.
    1,2: repeat rewrite ConExprLocalRenameSpec; rewrite LocalSubstGlobalRenameComm; auto.
    all: repeat unfold GlobalUpSubst; repeat unfold UpRename; destruct n; auto.
    2: destruct n; auto.
    all: rewrite ConExprGlobalRenameFusion.
    1,2: repeat rewrite ConExprLocalRenameSpec.
    - repeat rewrite LocalSubstGlobalRenameComm.
      rewrite ConExprGlobalRenameFusion; reflexivity.
    - repeat rewrite ConExprGlobalRenameFusion; reflexivity.
  Qed.
  
  (* Lemma ConExprGlobalSubstFusion : forall E σ1 σ2, *)
  (*     (E [ceg| σ1]) [ceg| σ2] = E [ceg| fun n => σ1 n [ceg| σ2]]. *)
  (* Proof using. *)
  (*   intro E; induction E; cbn; intros σ1 σ2; auto. *)
  (*   all: try rewrite IHE; try rewrite IHE1; try rewrite IHE2; auto. *)
  (*   - rewrite LocalSubstGlobal *)
      

  Inductive ConExprMergeRel : ConExpr -> ConExpr -> ConExpr -> Prop :=
  | MergeVar : forall n, ConExprMergeRel (Var n) (Var n) (Var n)
  | MergeUnit : ConExprMergeRel Unit Unit Unit
  | MergeRet : forall e, ConExprMergeRel (Ret e) (Ret e) (Ret e)
  | MergeIf : forall e E11 E12 E21 E22 E1 E2,
      ConExprMergeRel E11 E21 E1 ->
      ConExprMergeRel E12 E22 E2 ->
      ConExprMergeRel (If e E11 E12) (If e E21 E22) (If e E1 E2)
  | MergeSend : forall l e E1 E2 E,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (Send l e E1) (Send l e E2) (Send l e E)
  | MergeRecv : forall l E1 E2 E,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (Recv l E1) (Recv l E2) (Recv l E)
  | MergeChoose : forall l LR E1 E2 E,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (Choose l LR E1) (Choose l LR E2) (Choose l LR E)
  | MergeAllowChoiceLL : forall l E1 E2 E,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (AllowChoiceL l E1) (AllowChoiceL l E2) (AllowChoiceL l E)
  | MergeAllowChoiceLR : forall l E1 E2,
      ConExprMergeRel (AllowChoiceL l E1) (AllowChoiceR l E2) (AllowChoiceLR l E1 E2)
  | MergeAllowChoiceLLR : forall l E11 E12 E1 E2,
      ConExprMergeRel E11 E12 E1 ->
      ConExprMergeRel (AllowChoiceL l E11) (AllowChoiceLR l E12 E2) (AllowChoiceLR l E1 E2)
  | MergeAllowChoiceRL : forall l E1 E2,
      ConExprMergeRel (AllowChoiceR l E1) (AllowChoiceL l E2) (AllowChoiceLR l E2 E1)
  | MergeAllowChoiceRR : forall l E1 E2 E,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (AllowChoiceR l E1) (AllowChoiceR l E2) (AllowChoiceR l E)
  | MergeAllowChoiceRLR : forall l E21 E1 E22 E2,
      ConExprMergeRel E21 E22 E2 ->
      ConExprMergeRel (AllowChoiceR l E21) (AllowChoiceLR l E1 E22) (AllowChoiceLR l E1 E2)
  | MergeAllowChoiceLRL : forall l E11 E2 E12 E1,
      ConExprMergeRel E11 E12 E1 ->
      ConExprMergeRel (AllowChoiceLR l E11 E2) (AllowChoiceL l E12) (AllowChoiceLR l E1 E2)
  | MergeAllowChoiceLRR : forall l E1 E21 E22 E2,
      ConExprMergeRel E21 E22 E2 ->
      ConExprMergeRel (AllowChoiceLR l E1 E21) (AllowChoiceR l E22) (AllowChoiceLR l E1 E2)
  | MergeAllowChoiceLRLR : forall l E11 E21 E12 E22 E1 E2,
      ConExprMergeRel E11 E12 E1 ->
      ConExprMergeRel E21 E22 E2 ->
      ConExprMergeRel (AllowChoiceLR l E11 E21) (AllowChoiceLR l E12 E22) (AllowChoiceLR l E1 E2)
  | MergeLetRet : forall E11 E21 E12 E22 E1 E2,
      ConExprMergeRel E11 E12 E1 ->
      ConExprMergeRel E21 E22 E2 ->
      ConExprMergeRel (LetRet E11 E21) (LetRet E12 E22) (LetRet E1 E2)
  | MergeRecLocal : forall E,
      ConExprMergeRel (RecLocal E) (RecLocal E) (RecLocal E)
  | MergeAppLocal : forall E1 E2 E e,
      ConExprMergeRel E1 E2 E ->
      ConExprMergeRel (AppLocal E1 e) (AppLocal E2 e) (AppLocal E e)
  | MergeRecGlobal : forall E,
      ConExprMergeRel (RecGlobal E) (RecGlobal E) (RecGlobal E)
  | MergeAppGlobal : forall E11 E21 E12 E22 E1 E2,
      ConExprMergeRel E11 E21 E1 ->
      ConExprMergeRel E12 E22 E2 ->
      ConExprMergeRel (AppGlobal E11 E12) (AppGlobal E21 E22) (AppGlobal E1 E2).
  Global Hint Constructors ConExprMergeRel : ConExpr.

  Fixpoint ConExprMerge (E1 E2 : ConExpr) : option ConExpr :=
    match E1 with
    | Var x =>
      match E2 with
      | Var y => if Nat.eq_dec x y
                then Some (Var x)
                else None
      | _ => None
      end
    | Unit =>
      match E2 with
      | Unit => Some Unit
      | _ => None
      end
    | Ret e1 =>
      match E2 with
      | Ret e2 => if ExprEqDec e1 e2
                 then Some (Ret e1)
                 else None
      | _ => None
      end
    | If e1 E11 E12 =>
      match E2 with
      | If e2 E21 E22 =>
        if ExprEqDec e1 e2
        then match ConExprMerge E11 E21, ConExprMerge E12 E22 with
             | Some E1', Some E2' => Some (If e1 E1' E2')
             | _, _ => None
             end
        else None
      | _ => None
      end
    | Send l1 e1 E1 =>
      match E2 with
      | Send l2 e2 E2 =>
        if L.eq_dec l1 l2
        then if ExprEqDec e1 e2
             then match ConExprMerge E1 E2 with
                  | Some E => Some (Send l1 e1 E)
                  | None => None
                  end
             else None
        else None
      | _ => None
      end
    | Recv l1 E1 =>
      match E2 with
      | Recv l2 E2 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E1 E2 with
             | Some E => Some (Recv l1 E)
             | None => None
             end
        else None
      | _ => None
      end
    | Choose l1 LR1 E1 =>
      match E2 with
      | Choose l2 LR2 E2 =>
        if L.eq_dec l1 l2
        then if C.ChoiceEqDec LR1 LR2
             then match ConExprMerge E1 E2 with
                  | Some E => Some (Choose l1 LR1 E)
                  | None => None
                  end
             else None
        else None
      | _ => None
      end
    | AllowChoiceL l1 E1 =>
      match E2 with
      | AllowChoiceL l2 E2 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E1 E2 with
             | Some E => Some (AllowChoiceL l1 E)
             | None => None
             end
        else None
      | AllowChoiceR l2 E2 =>
        if L.eq_dec l1 l2
        then Some (AllowChoiceLR l1 E1 E2)
        else None
      | AllowChoiceLR l2 E21 E22 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E1 E21 with
             | Some E => Some (AllowChoiceLR l1 E E22)
             | None => None
             end
        else None
      | _ => None
      end
    | AllowChoiceR l1 E1 =>
      match E2 with
      | AllowChoiceL l2 E2 =>
        if L.eq_dec l1 l2
        then Some (AllowChoiceLR l1 E2 E1)
        else None
      | AllowChoiceR l2 E2 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E1 E2 with
             | Some E => Some (AllowChoiceR l1 E)
             | None => None
             end
        else None
      | AllowChoiceLR l2 E21 E22 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E1 E22 with
             | Some E => Some (AllowChoiceLR l1 E21 E)
             | None => None
             end
        else None
      | _ => None
      end
    | AllowChoiceLR l1 E11 E12 =>
      match E2 with
      | AllowChoiceL l2 E2 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E11 E2 with
             | Some E => Some (AllowChoiceLR l1 E E12)
             | None => None
             end
        else None
      | AllowChoiceR l2 E2 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E12 E2 with
             | Some E => Some (AllowChoiceLR l1 E11 E)
             | None => None
             end
        else None
      | AllowChoiceLR l2 E21 E22 =>
        if L.eq_dec l1 l2
        then match ConExprMerge E11 E21, ConExprMerge E12 E22 with
             | Some E1, Some E2 => Some (AllowChoiceLR l1 E1 E2)
             | _, _ => None
             end
        else None
      | _ => None
      end
    | LetRet E11 E12 =>
      match E2 with
      | LetRet E21 E22 =>
        match ConExprMerge E11 E21, ConExprMerge E12 E22 with
        | Some E1, Some E2 => Some (LetRet E1 E2)
        | _, _ => None
        end
      | _ => None
      end
    | RecLocal E1 =>
      match E2 with
      | RecLocal E2 =>
        if ConExprEqDec E1 E2
        then Some (RecLocal E1)
        else None
      | _ => None
      end
    | AppLocal E1 e1 =>
      match E2 with
      | AppLocal E2 e2 =>
        if ExprEqDec e1 e2
        then match ConExprMerge E1 E2 with
             | Some E => Some (AppLocal E e1)
             | None => None
             end
        else None
      | _ => None
      end
    | RecGlobal E1 =>
      match E2 with
      | RecGlobal E2 =>
        if ConExprEqDec E1 E2
        then Some (RecGlobal E1)
        else None
      | _ => None
      end
    | AppGlobal E11 E12 =>
      match E2 with
      | AppGlobal E21 E22 =>
        match ConExprMerge E11 E21, ConExprMerge E12 E22 with
        | Some E1, Some E2 => Some (AppGlobal E1 E2)
        | _, _ => None
        end
      | _ => None
      end
    end.

  Lemma ConExprMergeRelSpec1 : forall E1 E2 E, ConExprMergeRel E1 E2 E ->
                                          ConExprMerge E1 E2 = Some E.
  Proof using.
    intros E1 E2 E R; induction R; cbn; auto;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ |- context[Nat.eq_dec ?n ?n] ] =>
               let neq := fresh in
               destruct (Nat.eq_dec n n) as [_|neq]; [| destruct (neq eq_refl)]
             | [ |- context[ExprEqDec ?e ?e] ] =>
               let neq := fresh in
               destruct (ExprEqDec e e) as [_|neq]; [| destruct (neq eq_refl)]
             | [ |- context[L.eq_dec ?l ?l] ] =>
               let neq := fresh in
               destruct (L.eq_dec l l) as [_|neq]; [| destruct (neq eq_refl)]
             | [ |- context[C.ChoiceEqDec ?c ?c] ] =>
               let neq := fresh in
               destruct (C.ChoiceEqDec c c) as [_|neq]; [| destruct (neq eq_refl)]
             | [ |- context[ConExprEqDec ?E ?E] ] =>
               let neq := fresh in
               destruct (ConExprEqDec E E) as [_|neq]; [| destruct (neq eq_refl)]
             | [ H : ConExprMerge ?E1 ?E2 = Some ?E |- context[ConExprMerge ?E1 ?E2]] =>
               rewrite H
             end.
  Qed.

  Lemma ConExprMergeRelSpec2 : forall E1 E2 E, ConExprMerge E1 E2 = Some E ->
                                          ConExprMergeRel E1 E2 E.
  Proof using.
    intro E1; induction E1; intros E2 E eq; destruct E2; cbn in *;
      repeat match goal with
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [ H : Some _ = None |- _ ] => inversion H
             | [ H : None = Some _ |- _ ] => inversion H
             | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H
             | [ H : context[Nat.eq_dec ?n ?m] |- _ ] =>
               destruct (Nat.eq_dec n m); subst
             | [ H : context[ExprEqDec ?e1 ?e2] |- _ ] =>
               destruct (ExprEqDec e1 e2); subst
             | [ H : context[L.eq_dec ?l1 ?l2] |- _ ] =>
               destruct (L.eq_dec l1 l2); subst
             | [ H : context[C.ChoiceEqDec ?c1 ?c2] |- _ ] =>
               destruct (C.ChoiceEqDec c1 c2); subst
             | [ H : context[ConExprEqDec ?E1 ?E2] |- _ ] =>
               destruct (ConExprEqDec E1 E2); subst
             | [ H : context[ConExprMerge ?E1 ?E2 ] |- _ ] =>
               lazymatch type of H with
               | ConExprMerge E1 E2 = _ => fail
               | _ => lazymatch goal with
                     | [ eq : ConExprMerge E1 E2 = _ |- _ ] => rewrite eq in H
                     | _ => let eq := fresh "eq" in destruct (ConExprMerge E1 E2) eqn:eq
                     end
               end

             end; try (econstructor; eauto).
  Qed.
  

  Lemma MergeIdempotent : forall E, ConExprMerge E E = Some E.
  Proof using.
    intro E; induction E; cbn; auto;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ |- context[Nat.eq_dec ?a ?a]] =>
               let n := fresh "n" in 
               destruct (Nat.eq_dec a a) as [_ | n]; [| destruct (n eq_refl)]
             | [ |- context[L.eq_dec ?a ?a]] =>
               let n := fresh "n" in 
               destruct (L.eq_dec a a) as [_ | n]; [| destruct (n eq_refl)]
             | [ |- context[ExprEqDec ?a ?a]] =>
               let n := fresh "n" in 
               destruct (ExprEqDec a a) as [_ | n]; [| destruct (n eq_refl)]
             | [ |- context[ConExprEqDec ?a ?a]] =>
               let n := fresh "n" in 
               destruct (ConExprEqDec a a) as [_ | n]; [| destruct (n eq_refl)]
             | [ |- context[C.ChoiceEqDec ?a ?a]] =>
               let n := fresh "n" in 
               destruct (C.ChoiceEqDec a a) as [_ | n]; [| destruct (n eq_refl)]
             | [ H : ConExprMerge ?a ?b = _ |- context[ConExprMerge ?a ?b]] => rewrite H
             end.
  Qed.

  Lemma MergeComm : forall E1 E2, ConExprMerge E1 E2 = ConExprMerge E2 E1.
  Proof using.
    intro E1; induction E1; intro E2; destruct E2; cbn; auto;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [|- context[Nat.eq_dec ?a ?b]] => destruct (Nat.eq_dec a b); subst
             | [|- context[ExprEqDec ?a ?b]] => destruct (ExprEqDec a b); subst
             | [|- context[ConExprEqDec ?a ?b]] => destruct (ConExprEqDec a b); subst
             | [|- context[L.eq_dec ?a ?b]] => destruct (L.eq_dec a b); subst
             | [|- context[C.ChoiceEqDec ?a ?b]] => destruct (C.ChoiceEqDec a b); subst
             | [IH :forall E2, ConExprMerge ?E1 E2 = ConExprMerge E2 ?E1 |- context[ConExprMerge ?E1 ?E2]] =>
               rewrite (IH E2)
             | [ |- context[ConExprMerge ?E1 ?E2]] =>
               lazymatch goal with
               | [ H : ConExprMerge E1 E2 = _ |- _] => rewrite H
               | _ => let H := fresh in
                     destruct (ConExprMerge E1 E2) eqn:H; cbn in *
               end
             end.
  Qed.

  Lemma MergeAssoc : forall E1 E2 E3 E4 E5,
      ConExprMerge E1 E2 = Some E4 -> ConExprMerge E2 E3 = Some E5 ->
      ConExprMerge E1 E5 = ConExprMerge E4 E3.
  Proof using.
    intros E1; induction E1; intros E2 E3 E4 E5 eq1 eq2; destruct E2; destruct E3; cbn in *.
    all:
      repeat match goal with
             | [H : None = Some _ |- _ ] => inversion H
             | [H : Some _ = None |- _ ] => inversion H
             | [H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [ |- ?a = ?a ] => reflexivity
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [|- context[Nat.eq_dec ?a ?b]] => destruct (Nat.eq_dec a b); subst
             | [|- context[ExprEqDec ?a ?b]] => destruct (ExprEqDec a b); subst
             | [|- context[ConExprEqDec ?a ?b]] => destruct (ConExprEqDec a b); subst
             | [|- context[L.eq_dec ?a ?b]] => destruct (L.eq_dec a b); subst
             | [|- context[C.ChoiceEqDec ?a ?b]] => destruct (C.ChoiceEqDec a b); subst
             | [|- context[ConExprMerge ?a ?b]] =>
               lazymatch goal with
               | [ H : ConExprMerge a b = _ |- _ ] => rewrite H
               | _ => let H := fresh in destruct (ConExprMerge a b) eqn:H; cbn in *
               end
             | [H : context[Nat.eq_dec ?a ?b] |- _] => destruct (Nat.eq_dec a b); subst
             | [H : context[ExprEqDec ?a ?b] |- _] => destruct (ExprEqDec a b); subst
             | [H : context[ConExprEqDec ?a ?b] |- _] => destruct (ConExprEqDec a b); subst
             | [H : context[L.eq_dec ?a ?b] |- _] => destruct (L.eq_dec a b); subst
             | [H : context[C.ChoiceEqDec ?a ?b] |- _] => destruct (C.ChoiceEqDec a b); subst
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = Some ?x, H3 : ConExprMerge ?c ?d = Some ?y |- _ ] =>
               lazymatch goal with
               | [H : x = y |- _ ] => fail
               | _ => tryif unify x y
                 then fail
                 else let H1' := fresh in
                      pose proof H1 as H1';
                        rewrite H2 in H1'; rewrite H3 in H1';
                          inversion H1'; subst; clear H1'
               end
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = Some ?x, H3 : ConExprMerge ?c ?d = None |- _ ] =>
               rewrite H2 in H1; rewrite H3 in H1; inversion H1
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = None, H3 : ConExprMerge ?c ?d = Some _ |- _ ] =>
               rewrite H2 in H1; rewrite H3 in H1; inversion H1
             | [H : context[ConExprMerge ?a ?b] |- _ ] =>
               lazymatch type of H with
               | ConExprMerge _ _ = _ => fail
               | _ =>
                 lazymatch goal with
                 | [ H' : ConExprMerge a b = ?c |- _ ] => rewrite H' in H
                 | _ => let H' := fresh in destruct (ConExprMerge a b) eqn:H'; cbn in *
                 end
               end
             | [IH : forall E2 E3 E4 E5,
                   ConExprMerge ?E1 E2 = Some E4 ->
                   ConExprMerge E2 E3 = Some E5 -> ConExprMerge ?E1 E5 = ConExprMerge E4 E3,
                  H : ConExprMerge ?E1 ?E2 = Some ?E4, H' : ConExprMerge ?E2 ?E3 = Some ?E5 |- _ ]=>
               lazymatch goal with
               | [ _ : ConExprMerge E1 E5 = ConExprMerge E4 E3 |- _ ] => fail
               | _ => pose proof (IH E2 E3 E4 E5 H H')
               end
             end.
  Qed.

  Lemma MergeAssocNone : forall E1 E2 E3 E4,
      ConExprMerge E1 E2 = None -> ConExprMerge E2 E3 = Some E4 ->
      ConExprMerge E1 E4 = None.
  Proof using.
    intros E1; induction E1; intros E2 E3 E4 eq1 eq2; destruct E2; destruct E3; cbn in *.
    all: repeat match goal with
             | [H : None = Some _ |- _ ] => inversion H
             | [H : Some _ = None |- _ ] => inversion H
             | [H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [H1 : ?a = Some _, H2 : ?a = None |- _ ] =>
               rewrite H2 in H1; inversion H1
             | [ |- ?a = ?a ] => reflexivity
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [|- context[Nat.eq_dec ?a ?b]] => destruct (Nat.eq_dec a b); subst
             | [|- context[ExprEqDec ?a ?b]] => destruct (ExprEqDec a b); subst
             | [|- context[ConExprEqDec ?a ?b]] => destruct (ConExprEqDec a b); subst
             | [|- context[L.eq_dec ?a ?b]] => destruct (L.eq_dec a b); subst
             | [|- context[C.ChoiceEqDec ?a ?b]] => destruct (C.ChoiceEqDec a b); subst
             | [|- context[ConExprMerge ?a ?b]] =>
               lazymatch goal with
               | [ H : ConExprMerge a b = _ |- _ ] => rewrite H
               | _ => let H := fresh in destruct (ConExprMerge a b) eqn:H; cbn in *
               end
             | [H : context[Nat.eq_dec ?a ?b] |- _] => destruct (Nat.eq_dec a b); subst
             | [H : context[ExprEqDec ?a ?b] |- _] => destruct (ExprEqDec a b); subst
             | [H : context[ConExprEqDec ?a ?b] |- _] => destruct (ConExprEqDec a b); subst
             | [H : context[L.eq_dec ?a ?b] |- _] => destruct (L.eq_dec a b); subst
             | [H : context[C.ChoiceEqDec ?a ?b] |- _] => destruct (C.ChoiceEqDec a b); subst
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = Some ?x, H3 : ConExprMerge ?c ?d = Some ?y |- _ ] =>
               lazymatch goal with
               | [H : x = y |- _ ] => fail
               | _ => tryif unify x y
                 then fail
                 else let H1' := fresh in
                      pose proof H1 as H1';
                        rewrite H2 in H1'; rewrite H3 in H1';
                          inversion H1'; subst; clear H1'
               end
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = Some ?x, H3 : ConExprMerge ?c ?d = None |- _ ] =>
               rewrite H2 in H1; rewrite H3 in H1; inversion H1
             | [ H1 : ConExprMerge ?a ?b = ConExprMerge ?c ?d,
                      H2 : ConExprMerge ?a ?b = None, H3 : ConExprMerge ?c ?d = Some _ |- _ ] =>
               rewrite H2 in H1; rewrite H3 in H1; inversion H1
             | [H : context[ConExprMerge ?a ?b] |- _ ] =>
               lazymatch type of H with
               | ConExprMerge _ _ = _ => fail
               | _ =>
                 lazymatch goal with
                 | [ H' : ConExprMerge a b = ?c |- _ ] => rewrite H' in H
                 | _ => let H' := fresh in destruct (ConExprMerge a b) eqn:H'; cbn in *
                 end
               end
             | [ IH : forall E2 E3 E4,
                   ConExprMerge ?E1 E2 = None ->
                   ConExprMerge E2 E3 = Some E4 -> ConExprMerge ?E1 E4 = None,
                   H1 : ConExprMerge ?E1 ?E2 = None, H2 : ConExprMerge ?E2 ?E3 = Some ?E4 |- _ ] =>
               lazymatch goal with
               | [_ : ConExprMerge E1 E4 = None |- _ ] => fail
               | _ => pose proof (IH E2 E3 E4 H1 H2)
               end
                end.
  Qed.

  Inductive NEConExprList : Set :=
  | SingletonNEList : ConExpr -> NEConExprList
  | ConsNEList : ConExpr -> NEConExprList -> NEConExprList.

  Fixpoint NEConExprListToList (l : NEConExprList) : list ConExpr :=
    match l with
    | SingletonNEList E => [E]
    | ConsNEList E Es => E :: NEConExprListToList Es
    end.

  Fixpoint ListToNEConExprList (l : list ConExpr) : option NEConExprList :=
    match l with
    | [] => None
    | E :: [] => Some (SingletonNEList E)
    | E :: l => match ListToNEConExprList l with
                 | Some Es => Some (ConsNEList E Es)
                 | None => None
                 end
    end.
  
  Fixpoint MergeConExprList (Es : list ConExpr) : option ConExpr :=
    match Es with
    | [] => None
    | E1 :: [] => Some E1
    | E1 :: Es => match MergeConExprList Es with
                   | Some E2 => ConExprMerge E1 E2
                   | None => None
                   end
    end.

  Fixpoint MergeNEConExprList (Es : NEConExprList) : option ConExpr :=
    match Es with
    | SingletonNEList E => Some E
    | ConsNEList E1 Es => match MergeNEConExprList Es with
                         | Some E2 => ConExprMerge E1 E2
                         | None => None
                         end
    end.

  Lemma MergeConExprListToNE : forall Es Es',
      ListToNEConExprList Es = Some Es' ->
      MergeConExprList Es = MergeNEConExprList Es'.
  Proof using.
    intro Es; induction Es; intros Es' eq; cbn in *; try discriminate.
    destruct Es. inversion eq; subst; clear eq. cbn; reflexivity.
    destruct (ListToNEConExprList (c :: Es)); inversion eq; subst; clear eq.
    specialize (IHEs n eq_refl). rewrite IHEs. cbn; reflexivity.
  Qed.

  Lemma OnlyNilNotNEList : forall Es, ListToNEConExprList Es = None -> Es = [].
  Proof using.
    intros Es; induction Es; cbn; intros es; auto.
    destruct Es. inversion es. destruct (ListToNEConExprList (c :: Es)); try discriminate.
    specialize (IHEs eq_refl). inversion IHEs.
  Qed.

  Theorem MergeConExprListPerm : forall Es1 Es2 : list ConExpr,
      Permutation Es1 Es2 -> MergeConExprList Es1 = MergeConExprList Es2.
  Proof using.
    intros Es1 Es2 perm; induction perm; cbn; auto.
    - destruct l; destruct l'; auto.
      destruct (Permutation_nil_cons perm).
      destruct (Permutation_nil_cons (Permutation_sym perm)).
      rewrite IHperm; destruct (MergeConExprList l'); reflexivity.
    - destruct l; auto. apply MergeComm.
      destruct (MergeConExprList (c :: l)); auto.
      destruct (ConExprMerge x c0) eqn:eq1. all:destruct (ConExprMerge y c0) eqn:eq2.
      symmetry. rewrite MergeComm with (E1 := y). apply MergeAssoc with (E2 := c0); auto.
      rewrite MergeComm; auto.
      apply (MergeAssocNone y c0 x c1); auto. rewrite MergeComm; auto.
      symmetry; apply (MergeAssocNone x c0 y c1); auto. rewrite MergeComm; auto.
      reflexivity.
    - rewrite IHperm1. rewrite IHperm2. auto.
  Qed.

  Theorem ConExprMergeCons : forall E Es E', MergeConExprList Es = Some E' ->
                                        MergeConExprList (E :: Es) = ConExprMerge E E'.
  Proof using.
    intros E Es; revert E; induction Es; intros E E' eq; cbn in *; try discriminate.
    destruct Es; [inversion eq; auto|].
    destruct (MergeConExprList (c :: Es)) eqn:eq0; try discriminate.
    rewrite eq; auto.
  Qed.

  Fixpoint NEApp (Es1 Es2 : NEConExprList) : NEConExprList :=
    match Es1 with
    | SingletonNEList E => ConsNEList E Es2
    | ConsNEList E Es1 => ConsNEList E (NEApp Es1 Es2)
    end.

  Theorem AppToNEApp : forall Es1 Es2 Es1' Es2',
      ListToNEConExprList Es1 = Some Es1' ->
      ListToNEConExprList Es2 = Some Es2' ->
      ListToNEConExprList (Es1 ++ Es2) = Some (NEApp Es1' Es2').
  Proof using.
    intro Es1; induction Es1; intros Es2 Es1' Es2' eq1 eq2; cbn in *; try discriminate.
    destruct Es1; cbn in *.
    destruct Es2; cbn in eq2; try discriminate.
    inversion eq1; subst; clear eq1; cbn.
    rewrite eq2; auto.
    destruct (match Es1 with
              | [] => Some (SingletonNEList c)
              | _ :: _ =>
                match ListToNEConExprList Es1 with
                | Some Es => Some (ConsNEList c Es)
                | None => None
                end
              end) eqn:eq3; try discriminate.
    specialize (IHEs1 Es2 n Es2' eq_refl eq2). rewrite IHEs1.
    inversion eq1; subst; cbn; reflexivity.
  Qed.

  Theorem NEAppToApp : forall Es1 Es2,
      NEConExprListToList (NEApp Es1 Es2) = (NEConExprListToList Es1) ++ (NEConExprListToList Es2).
  Proof using.
    intros Es1; induction Es1; intro Es2; cbn; auto.
    rewrite IHEs1; auto.
  Qed.

  Theorem ConExprNEMergeApp : forall Es1 Es2 E1 E2,
      MergeNEConExprList Es1 = Some E1 ->
      MergeNEConExprList Es2 = Some E2 ->
      MergeNEConExprList (NEApp Es1 Es2) = ConExprMerge E1 E2.
  Proof using.
    intro Es1; induction Es1; intros Es2 E1 E2 eq1 eq2; cbn in *.
    - rewrite eq2; inversion eq1; auto.
    - destruct (MergeNEConExprList Es1) eqn:eq3; try discriminate.
      specialize (IHEs1 Es2 c0 E2 eq_refl eq2). rewrite IHEs1.
      destruct (ConExprMerge c0 E2) eqn:eq4.
      apply (MergeAssoc c c0 E2 E1 c1 eq1 eq4).
      symmetry; rewrite MergeComm; rewrite MergeComm in eq1; rewrite MergeComm in eq4;
        apply (MergeAssocNone E2 c0 c E1 eq4 eq1).
  Qed.      
  
  Theorem ConExprMergeApp : forall Es1 Es2 E1 E2,
      MergeConExprList Es1 = Some E1 ->
      MergeConExprList Es2 = Some E2 ->
      MergeConExprList (Es1 ++ Es2) = ConExprMerge E1 E2.
  Proof using.
    intros Es1 Es2 E1 E2 H H0.
    destruct (ListToNEConExprList Es1) eqn:eq1;
      [|apply OnlyNilNotNEList in eq1; subst; cbn in H; inversion H].
    destruct (ListToNEConExprList Es2) eqn:eq2;
      [|apply OnlyNilNotNEList in eq2; subst; cbn in H0; inversion H0].
    pose proof (MergeConExprListToNE Es1 n eq1).
    pose proof (MergeConExprListToNE Es2 n0 eq2).
    rewrite H in H1; symmetry in H1; rewrite H0 in H2; symmetry in H2.
    pose proof (ConExprNEMergeApp n n0 E1 E2 H1 H2).
    pose proof (AppToNEApp Es1 Es2 n n0 eq1 eq2).
    pose proof (MergeConExprListToNE (Es1 ++ Es2) (NEApp n n0) H4).
    etransitivity; eauto.
  Qed.

  Theorem MergeConExprIfs : forall E1 E2 E3 E4 E5 E6 E7 E8,
      ConExprMerge E1 E2 = Some E5 ->
      ConExprMerge E3 E4 = Some E6 ->
      ConExprMerge E1 E3 = Some E7 ->
      ConExprMerge E2 E4 = Some E8 ->
      ConExprMerge E5 E6 = ConExprMerge E7 E8.
  Proof using.
    intros E1 E2 E3 E4 E5 E6 E7 E8 H H0 H1 H2.
    assert (Permutation [E1; E2; E3; E4] [E1; E3; E2; E4]) as p
      by (repeat constructor; fail);
      pose proof (MergeConExprListPerm [E1; E2; E3; E4] [E1; E3; E2; E4] p); clear p.
    assert ([E1; E2] ++ [E3; E4] = [E1; E2; E3; E4]) as H4 by auto; rewrite <- H4 in H3;
       clear H4; erewrite ConExprMergeApp in H3; cbn; repeat rewrite MergeIdRight; eauto.
    assert ([E1; E3] ++ [E2; E4] = [E1; E3; E2; E4]) as H4 by auto; rewrite <- H4 in H3;
       clear H4; erewrite ConExprMergeApp in H3; cbn; repeat rewrite MergeIdRight; eauto.
  Qed.
    

  Theorem MergeExprSubst : forall E1 E2 E σ,
      ConExprMerge E1 E2 = Some E ->
      ConExprMerge (E1 [cel| σ]) (E2 [cel| σ]) = Some (E [cel| σ]).
  Proof using.
    intro E1; induction E1; intros E2 E σ eq; destruct E2; cbn in *.
    all: repeat match goal with
                | [ |- ?a = ?a ] => reflexivity