ChoreographyCompiler.v 84.7 KB
Newer Older
Andrew Hirsch's avatar
Andrew Hirsch committed
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 Export Expression.
Require Export Locations.
Require Import LocationMap.
Require Export Choreography.
Require Export ConcurrentLambda.

Require Import Coq.Arith.Arith.
Require Import Coq.Lists.List.
Require Import Permutation.

From Equations Require Import Equations.

Import ListNotations.

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

  Module ICL := (InternalConcurrentLambda E L LM).
  Import ICL.

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

    Fixpoint ProjectChor (C : C.Chor) (l : Loc) : option ConExpr :=
    match C with
    | C.Done p e =>
      if L.eq_dec l p
      then Some (Ret e)
      else Some Unit
    | C.Var x => Some (Var x)
    | C.Send l1 e l2 C =>
      if L.eq_dec l l1
      then if L.eq_dec l l2
           then None
           else match ProjectChor C l with
                | Some E => Some (Send l2 e E)
                | None => None
                end
      else if L.eq_dec l l2
           then match ProjectChor C l with
                | Some E => Some (Recv l1 E)
                | None => None
                end
           else ProjectChor C l
    | C.If l' e C1 C2 =>
      match ProjectChor C1 l with
      | Some E1 =>
        match ProjectChor C2 l with
        | Some E2 =>
          if L.eq_dec l l'
          then Some (If e E1 E2)
          else ConExprMerge E1 E2
        | None => None
        end
      | None => None
      end
    | C.Sync p LR q C =>
      if L.eq_dec l p
      then if L.eq_dec l q
           then None
           else match ProjectChor C l with
                | Some E => Some (Choose q LR E)
                | None => None
                end
      else if L.eq_dec l q
           then match ProjectChor C l with
                | Some E => match LR with
                           | C.LChoice => Some (AllowChoiceL p E)
                           | C.RChoice => Some (AllowChoiceR p E)
                           end
                | None => None
                end
           else ProjectChor C l
    | C.DefLocal p C1 C2 =>
      if L.eq_dec l p
      then match ProjectChor C1 l, ProjectChor C2 l with
           | Some E1, Some E2 => Some (LetRet E1 E2)
           | _, _ => None
           end
      else match ProjectChor C1 l, ProjectChor C2 l with
           | Some E1, Some E2 => Some (AppGlobal (RecGlobal (E2 ceg| fun n => 2 + n)) E1) (* Use renaming to make sure nothing binds to this binder *)
           | _, _ => None
           end
    | C.RecLocal p C =>
      if L.eq_dec l p
      then match ProjectChor C l with
           | Some E => Some (RecLocal E)
           | None => None
           end
      else match ProjectChor C l with
           | Some E => Some (RecGlobal (E ceg| S)) (* Global so we can feed it a dummy value *)
           | None => None
           end
    | C.RecGlobal C =>
      match ProjectChor C l with
      | Some E => Some (RecGlobal E)
      | None => None
      end
    | C.AppLocal p C e =>
      if L.eq_dec p l
      then match ProjectChor C l with
           | Some E => Some (AppLocal E e)
           | None => None
           end
      else match ProjectChor C l with
           | Some E => Some (AppGlobal E Unit)
           | None => None
           end
    | C.AppGlobal C1 C2 =>
      match ProjectChor C1 l, ProjectChor C2 l with
      | Some E1, Some E2 => Some (AppGlobal E1 E2)
      | _, _ => None
      end
    end.

  Theorem ProjectChorEquivToEq : forall C1 C2 l,
      C.equiv C1 C2 -> ProjectChor C1 l = ProjectChor C2 l.
  Proof using.
    intros C1 C2 l eqv; revert l; induction eqv; intro p; cbn; auto;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ H : ?a <> ?a |- _ ] => inversion H
             | [ d : C.LRChoice |- _ ] => destruct d; cbn in *
             | [ H : Some _ = None |- _ ] => inversion H
             | [ H : None = Some _ |- _ ] => inversion H
             | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [ H1 : ?a = Some _, H2 : ?a = None |- _ ] =>
               rewrite H2 in H1; inversion H1
             | [ |- context[L.eq_dec ?a ?b]] =>
               tryif unify a b
               then let n := fresh in
                    destruct (L.eq_dec a b) as [_ | n];
                      [| destruct (n eq_refl)]
               else lazymatch goal with
                    | [e : a = b |- _ ] =>
                      let n := fresh in
                      destruct (L.eq_dec a b) as [_ | n];
                        [| destruct (n e)]
                    | [e : b = a |- _ ] =>
                      let n := fresh in
                      destruct (L.eq_dec a b) as [_ | n];
                        [| destruct (n (eq_sym e))]
                    | [n : a <> b |- _ ] =>
                      let e := fresh in
                      destruct (L.eq_dec a b) as [e | _];
                        [destruct (n e)|]
                    | [n : b <> a |- _ ] =>
                      let e := fresh in
                      destruct (L.eq_dec a b) as [e | _];
                        [destruct (n (eq_sym e))|]
                    | _ => let eq := fresh "eq" in
                          let neq := fresh "neq" in
                          destruct (L.eq_dec a b) as [eq|neq]; subst
                    end
             | [ |- context[ExprEqDec ?a ?b]] =>
               tryif unify a b
               then let n := fresh in
                    destruct (ExprEqDec a b) as [_ | n];
                      [| destruct (n eq_refl)]
               else lazymatch goal with
                    | [e : a = b |- _ ] =>
                      let n := fresh in
                      destruct (ExprEqDec a b) as [_ | n];
                        [| destruct (n e)]
                    | [e : b = a |- _ ] =>
                      let n := fresh in
                      destruct (ExprEqDec a b) as [_ | n];
                        [| destruct (n (eq_sym e))]
                    | [n : a <> b |- _ ] =>
                      let e := fresh in
                      destruct (ExprEqDec a b) as [e | _];
                        [destruct (n e)|]
                    | [n : b <> a |- _ ] =>
                      let e := fresh in
                      destruct (ExprEqDec a b) as [e | _];
                        [destruct (n (eq_sym e))|]
                    | _ => let eq := fresh "eq" in
                          let neq := fresh "neq" in
                          destruct (ExprEqDec a b) as [eq|neq]; subst
                    end
             | [ IH : forall l, ProjectChor ?C1 l = ProjectChor ?C2 l |- context[ProjectChor ?C1 ?l]] =>
               rewrite (IH l)
             | [ |- context[ProjectChor ?C ?l]] =>
               lazymatch goal with
               | [e : ProjectChor C l = _ |- _ ] => rewrite e; cbn
               | _ => let eq := fresh "eq" in
                     destruct (ProjectChor C l) eqn:eq; cbn
               end
             | [ |- context[ConExprMerge ?E1 ?E2]] =>
               lazymatch goal with
               | [e : ConExprMerge E1 E2 = _ |- _ ] => rewrite e; cbn
               | _ => let eq := fresh "eq" in
                     destruct (ConExprMerge E1 E2) eqn:eq; cbn
               end
             | [H1 : ConExprMerge ?E1 ?E2 = Some ?E5,
                H2 : ConExprMerge ?E3 ?E4 = Some ?E6,
                H3 : ConExprMerge ?E1 ?E3 = Some ?E7,                
                H4 : ConExprMerge ?E2 ?E4 = Some ?E8,
                H5 : ConExprMerge ?E5 ?E6 = ?a,
                H6 : ConExprMerge ?E7 ?E8 = ?b |- _ ] =>
               lazymatch goal with
               | [ _ : a = b  |- _ ] => fail
               | _ => let H := fresh in
                     pose proof (MergeConExprIfs E1 E2 E3 E4 E5 E6 E7 E8 H1 H2 H3 H4) as H;
                       rewrite H5 in H; rewrite H6 in H
               end
             | [ H : ConExprMerge ?a ?b = ?c |- _ ] =>
               lazymatch goal with
               | [_ : ConExprMerge b a = c |- _ ] => fail
               | _ => let H' := fresh in pose proof (MergeComm a b) as H'; rewrite H in H';
                                         symmetry in H'
               end
             | [ H1 : ConExprMerge ?E1 ?E2 = None, H2 : ConExprMerge ?E2 ?E3 = Some ?E4 |- _ ] =>
               lazymatch goal with
               | [ _ : ConExprMerge E1 E4 = None |- _ ] => fail
               | _ => pose proof (MergeAssocNone E1 E2 E3 E4 H1 H2)
               end
             end.
  Qed.        


  Theorem ProjectChorExprSubst : forall C l E σ,
      ProjectChor C l = Some E ->
      ProjectChor (C.ChorExprSubst C σ) l = Some (E [cel| σ l]).
  Proof using.
    intros C; C.ChorInduction C; intros p E σ eq; cbn in *;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ H : None = Some _ |- _ ] => inversion H
             | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [ H : context[L.eq_dec ?a ?b] |- _ ] =>
               destruct (L.eq_dec a b); subst; cbn in *
             | [ H : context[ProjectChor ?C ?l] |- _ ] =>
               lazymatch type of H with
               | ProjectChor C l = _ => fail
               | _ => lazymatch goal with
                     | [ H' : ProjectChor C l = _ |- _ ] => rewrite H' in H
                     | _ => let eq := fresh "eq" in
                           destruct (ProjectChor C l) eqn:eq; subst; cbn in *
                     end
               end
             | [ eq: ProjectChor ?C ?l = Some ?E,
                     IH : forall l E σ, ProjectChor ?C l = Some E -> ProjectChor (C.ChorExprSubst ?C σ) l = Some (E [cel| σ l]) |- context[ProjectChor (C.ChorExprSubst ?C ?σ) ?l] ] =>
               rewrite (IH l E σ eq)
             | [ n : ?p <> ?q |- context[C.ChorUpExprSubst σ ?p ?q]] =>
               unfold C.ChorUpExprSubst;
                 destruct (L.eq_dec p q) as [e |_];
                 [destruct (n (eq_sym e))|]
             | [ n : ?q <> ?p |- context[C.ChorUpExprSubst σ ?p ?q]] =>
               let e := fresh "e" in 
               unfold C.ChorUpExprSubst;
                 destruct (L.eq_dec p q) as [e |_];
                 [destruct (n (eq_sym e))|]
             | [ |- context[C.ChorUpExprSubst σ ?p ?p]] =>
               let n := fresh "n" in 
               unfold C.ChorUpExprSubst;
                 destruct (L.eq_dec p p) as [_ |n];
                 [|destruct (n eq_refl)];
                 unfold ExprUpSubst
             end; try reflexivity.
    - erewrite MergeExprSubst; eauto.
    - destruct d; inversion eq; subst; auto.
    - rewrite LocalSubstGlobalRenameComm; reflexivity.
    - rewrite LocalSubstGlobalRenameComm; reflexivity.
  Qed.

  Theorem ProjectUpSubstForExprNeq : forall σ l l' σ',
      (forall n, ProjectChor (σ n) l = Some (σ' n)) ->
      l <> l' ->
      forall n, ProjectChor (C.ChorUpSubstForExpr σ l' n) l = Some (σ' n).
  Proof using.
    intros σ l l' σ' eq neq n.
    unfold C.ChorUpSubstForExpr. rewrite C.ChorExprRenameSpec.
    rewrite ProjectChorExprSubst with (E := σ' n); auto.
    destruct (L.eq_dec l' l) as [e | _ ]; [destruct (neq (eq_sym e))|].
    rewrite ConExprLocalIdSubst. reflexivity.
  Qed.

  Theorem ProjectUpSubstForExprEq : forall σ l σ',
      (forall n, ProjectChor (σ n) l = Some (σ' n)) ->
      forall n, ProjectChor (C.ChorUpSubstForExpr σ l n) l = Some (σ' n cel| S).
  Proof using.
    intros σ l σ' H n.
    unfold C.ChorUpSubstForExpr. rewrite C.ChorExprRenameSpec.
    rewrite ProjectChorExprSubst with (E := σ' n); auto.
    destruct (L.eq_dec l l) as [_ | neq]; [| destruct (neq eq_refl)].
    rewrite ConExprLocalRenameSpec; auto.
  Qed.
  
  Theorem ProjectChorRename : forall C l E ξ,
      ProjectChor C l = Some E ->
      ProjectChor (C.ChorRename C ξ) l = Some (E ceg| ξ⟩).
  Proof using.
    intros C; C.ChorInduction C; intros p E ξ eq; cbn in *;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ d : C.LRChoice |- _ ] => destruct d
             | [ H : None = Some _ |- _ ] => inversion H
             | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [ H : context[L.eq_dec ?a ?b] |- _ ] =>
               destruct (L.eq_dec a b); subst; cbn in *
             | [ H : context[ProjectChor ?C ?l] |- _ ] =>
               lazymatch type of H with
               | ProjectChor C l = _ => fail
               | _ => lazymatch goal with
                     | [ H' : ProjectChor C l = _ |- _ ] => rewrite H' in H
                     | _ => let eq := fresh "eq" in
                           destruct (ProjectChor C l) eqn:eq; subst; cbn in *
                     end
               end
             | [ IH: forall l E ξ, ProjectChor ?C l = Some E -> ProjectChor (C.ChorRename ?C ξ) l = Some (E ceg| ξ⟩), H : ProjectChor ?C ?l = Some ?E |- context[ProjectChor (C.ChorRename ?C ?ξ) ?l]] =>
               rewrite (IH l E ξ H)
             end.
    
    - repeat rewrite GlobalRenameSpec. apply MergeSubst; auto.
    - repeat rewrite ConExprGlobalRenameFusion; cbn. reflexivity.
    - unfold C.ChorUpRename. unfold UpRename. reflexivity.
    - repeat rewrite ConExprGlobalRenameFusion; cbn. unfold C.ChorUpRename. unfold UpRename.
      reflexivity.
    - repeat unfold C.ChorUpRename. repeat unfold UpRename. reflexivity.
  Qed.

                     

  Theorem ProjectUpSubst : forall σ l σ',
      (forall n, ProjectChor (σ n) l = Some (σ' n)) ->
      forall n, ProjectChor (C.ChorUpSubst σ n) l = Some (GlobalUpSubst σ' n).
  Proof using.
    intros σ l σ' H n.
    unfold C.ChorUpSubst; destruct n; cbn; auto.
    apply ProjectChorRename; auto.
  Qed.

  Theorem ProjectChorSubst : forall C l E σ σ',
      (forall n, ProjectChor (σ n) l = Some (σ' n)) ->
      ProjectChor C l = Some E ->
      ProjectChor (C.ChorSubst C σ) l = Some (E [ceg| σ']).
  Proof using.
    intro C; C.ChorInduction C; intros p E σ σ' eq_σ eq_C; cbn in *;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ d : C.LRChoice |- _ ] => destruct d
             | [ H : None = Some _ |- _ ] => inversion H
             | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H; cbn in *
             | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
             | [ H : context[L.eq_dec ?a ?b] |- _ ] =>
               destruct (L.eq_dec a b); subst; cbn in *
             | [ H : context[ProjectChor ?C ?l] |- _ ] =>
               lazymatch type of H with
               | ProjectChor C l = _ => fail
               | _ => lazymatch goal with
                     | [ H' : ProjectChor C l = _ |- _ ] => rewrite H' in H
                     | _ => let eq := fresh "eq" in
                           destruct (ProjectChor C l) eqn:eq; subst; cbn in *
                     end
               end
             | [ IH : (forall l E σ σ', (forall n, ProjectChor (σ n) l = Some (σ' n)) -> ProjectChor ?C l = Some E -> ProjectChor (C.ChorSubst ?C σ) l = Some (E [ceg| σ'])),
                      H : ProjectChor ?C ?l = Some ?E,
                          H' : (forall n, ProjectChor (?σ n) ?l = Some (?σ' n))
                 |- context[ProjectChor (C.ChorSubst ?C ?σ) ?l]] =>
               rewrite (IH l E σ σ' H' H)
             | [ H : (forall n, ProjectChor (?σ n) ?l = Some (?σ' n)),
                     neq : ?l <> ?l' |- context[C.ChorUpSubstForExpr ?σ ?l'] ] =>
               lazymatch goal with
               | [ _ : forall n, ProjectChor (C.ChorUpSubstForExpr σ l' n) l = Some (σ' n) |- _ ] =>
                 fail
               | _ => pose proof (ProjectUpSubstForExprNeq σ l l' σ' H neq)
               end
             | [ H : (forall n, ProjectChor (?σ n) ?l = Some (?σ' n)),
                     neq : ?l' <> ?l |- context[C.ChorUpSubstForExpr ?σ ?l'] ] =>
               lazymatch goal with
               | [ _ : forall n, ProjectChor (C.ChorUpSubstForExpr σ l' n) l = Some (σ' n) |- _ ] =>
                 fail
               | _ =>
                 pose proof (ProjectUpSubstForExprNeq σ l l' σ' H ltac:(let eq := fresh in intro eq; destruct (neq (eq_sym eq))))
               end
             | [ H : (forall n, ProjectChor (?σ n) ?l = Some (σ' n))
                 |- context[C.ChorUpSubstForExpr ?σ ?l]] =>
               lazymatch goal with
               | [ _ : forall n, ProjectChor (C.ChorUpSubstForExpr σ l n) l = Some ((fun m => σ' m cel| S) n) |- _ ] => fail
               | _ =>  assert (forall n, ProjectChor (C.ChorUpSubstForExpr σ l n) l = Some ((fun m => σ' m cel| S) n)) by (apply ProjectUpSubstForExprEq; auto)
               end
             end.
    - apply eq_σ.
    - apply MergeSubst; auto.
    - apply f_equal.
      rewrite ConExprGlobalRenameSubstFusion.
      rewrite ConExprGlobalSubstRenameFusion. cbn.
      erewrite ConExprGlobalSubstExt; [reflexivity|].
      intro m; cbn. rewrite ConExprGlobalRenameFusion; reflexivity.
    - rewrite IHC with (E := c) (σ' := GlobalUpSubst (fun n => σ' n cel| S)); auto.
      apply ProjectUpSubst. intro n; apply ProjectUpSubstForExprEq; auto.
    - rewrite IHC with (E := c) (σ' := GlobalUpSubst σ'); auto.
      2: { apply ProjectUpSubst. apply ProjectUpSubstForExprNeq; auto. }
      rewrite ConExprGlobalSubstRenameFusion.
      rewrite ConExprGlobalRenameSubstFusion; reflexivity.
    - rewrite IHC with (E := c) (σ' := GlobalUpSubst (GlobalUpSubst σ')); auto.
      repeat apply ProjectUpSubst; auto.
  Qed.      

  Definition ProjectRedex (R : C.Redex) (l : Loc) : option Label :=
    match R with
    | C.RDone p _ _ =>
      if L.eq_dec l p
      then Some Tau
      else None
    | C.RIfE p _ _ =>
      if L.eq_dec l p
      then Some Tau
      else None
    | C.RIfTT p =>
      if L.eq_dec l p
      then Some Tau
      else None
    | C.RIfFF p =>
      if L.eq_dec l p
      then Some Tau
      else None
    | C.RSendE p e1 e2 q =>
      if L.eq_dec l p
      then Some Tau
      else None 
    | C.RSendV p v q =>
      if L.eq_dec l p
      then if L.eq_dec l q
           then None
           else Some (SendLabel q v)
      else if L.eq_dec l q
           then Some (RecvLabel p v)
           else None
    | C.RSync p LR q =>
      if L.eq_dec l p
      then if L.eq_dec l q
           then None
           else Some (ChooseLabel q LR)
      else if L.eq_dec l q
           then Some (AllowChoiceLabel p LR)
           else None 
    | C.RDefLocal p _ => Some Tau
    | C.RAppLocalE p _ _ =>
      if L.eq_dec l p
      then Some Tau
      else None
    | C.RAppLocal _ _ =>
      Some Tau
    | C.RAppGlobal => Some Tau
    end.

  Theorem ProjectRedexBlocked : forall C1 R B C2 l,
      C.ChorStep R B C1 C2 ->
      In l B ->
      ProjectRedex R l = None.
  Proof using.
    intros C1 R B C2 l step; revert l; induction step; intros p i; cbn;
      repeat match goal with
             | [ |- ?a = ?a ] => reflexivity
             | [ H : ?P |- ?P ] => exact H
             | [H : ?P, H' : ~?P |- _ ] => destruct (H' H)
             | [H : In _ [] |- _ ] => inversion H
             | [|- context[L.eq_dec ?a ?b]] => destruct (L.eq_dec a b); subst
             | [ i : In ?p B, IH : forall l, In l B -> _ |- _ ] =>
               specialize (IH p i)
             | [ i : In ?p B, IH : forall l, In l (_ :: B) -> _ |- _ ] =>
               specialize (IH p ltac:(right; exact i))
             | [ i : In ?p B, IH : forall l, In l (_ :: _ :: B) -> _ |- _ ] =>
               specialize (IH p ltac:(right; right; exact i))
             end.
  Qed.

  Theorem ProjectChorValue : forall V l E,
      C.ChorVal V ->
      ProjectChor V l = Some E ->
      ConExprVal E.
  Proof using.
    intros V l E H H0; inversion H; subst; cbn in H0.
    destruct (L.eq_dec l l0); subst; cbn in H0; inversion H0; subst; clear H0; 
      constructor; auto.
    destruct (L.eq_dec l l0); destruct (ProjectChor C l) eqn:eq; subst;
      inversion H0; subst; clear H0; constructor; auto.
    destruct (ProjectChor C l); inversion H0; subst; clear H0; constructor; auto.
  Qed.
  
  Theorem ProjectChorStep1 : forall C1 R B C2 l E1 E2 L,
      ProjectChor C1 l = Some E1 ->
      ProjectChor C2 l = Some E2 ->
      ProjectRedex R l = Some L ->
      C.ChorStep R B C1 C2 ->
      ConExprStep E1 L E2.
  Proof using.
    intros C1 R B C2 l E1 E2 L eqC1 eqC2 eqR step;
      revert l E1 E2 L eqC1 eqC2 eqR; induction step; intros p E1 E2 L eqC1 eqC2 eqR;
        cbn in *;
        repeat match goal with
               | [ H : Some _ = None |- _ ] => inversion H
               | [ H : None = Some _ |- _ ] => inversion H
               | [ H : Some _ = Some _ |- _ ] => inversion H; subst; clear H
               | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
               | [ H : ?a = Some _, H' : ?a = None |- _ ] =>
                 rewrite H in H'; inversion H'
               | [ H : context[L.eq_dec ?a ?b] |- _ ] => destruct (L.eq_dec a b); subst
               | [ H : context[ProjectChor ?C ?l] |- _ ] =>
                 lazymatch type of H with
                 | ProjectChor C l = _ => fail
                 | _ => lazymatch goal with
                       | [ H' : ProjectChor C l = _ |- _ ] => rewrite H' in H
                       | _ => let eq := fresh "eq" in
                             destruct (ProjectChor C l) eqn:eq
                       end
                 end
               | [ H : C.ChorStep ?R (?p :: ?B) ?C1 ?C2 |- _ ] =>
                 lazymatch goal with
                 | [_ : ProjectRedex R p = None |- _ ] => fail
                 | _ => pose proof (ProjectRedexBlocked C1 R (p :: B) C2 p H ltac:(left;reflexivity))
                 end
               | [ H : C.ChorStep ?R (?p :: ?q :: ?B) ?C1 ?C2 |- _ ] =>
                 lazymatch goal with
                 | [_ : ProjectRedex R p = None, _ : ProjectRedex R q = None |- _ ] => fail
                 | _ => pose proof (ProjectRedexBlocked C1 R (p :: q :: B) C2 p H ltac:(left;reflexivity));
                         pose proof (ProjectRedexBlocked C1 R (p :: q :: B) C2 q H ltac:(right; left;reflexivity))
                 end
               | [ H :context[ProjectChor (C.ChorExprSubst ?C ?σ) ?l],
                      H' : ProjectChor ?C ?l = Some ?E |- _] =>
                 rewrite (ProjectChorExprSubst C l E σ H') in H
               end.
    all: try (econstructor; eauto; fail).
    all: try (eapply IHstep; eauto; fail).
    - unfold C.ValueSubst.
      destruct (L.eq_dec l2 l1) as [eq'|_]; [destruct (n (eq_sym eq'))|].
      rewrite ConExprLocalIdSubst; constructor; auto.
    - unfold C.ValueSubst.
      destruct (L.eq_dec l2 l2) as [_|neq]; [| destruct (neq eq_refl)].
      fold (ValSubst v). constructor; auto.
    - apply (MergeStep c1 c2 c c0); auto.
      eapply IHstep1; eauto. eapply IHstep2; eauto.
    - unfold C.ValueSubst. destruct (L.eq_dec l l) as [_|neq];[|destruct (neq eq_refl)].
      fold (ValSubst v). constructor; auto.
    - unfold C.ValueSubst. destruct (L.eq_dec l p) as [e |_]; [destruct (n (eq_sym e))|].
      rewrite ConExprLocalIdSubst.
      pose proof (AppGlobalStep (c ceg| fun n => S (S n)) Unit ltac:(constructor)).
      rewrite ConExprGlobalRenameSubstFusion in H0; cbn in H0.
      rewrite GlobalIdSubstSpec in H0. auto.
    - pose proof (ProjectChorExprSubst C p c (C.ValueSubst p v) eq).
      assert (forall n : nat, ProjectChor (C.AppLocalSubst p C n) p = Some (RecLocalSubst c n)) as eq'
          by (intro n; destruct n; cbn; auto;
              destruct (L.eq_dec p p) as [_|neq];[| destruct (neq eq_refl)];
              rewrite eq; reflexivity).
      pose proof (ProjectChorSubst _ p _ (C.AppLocalSubst p C) (RecLocalSubst c) eq' H0).
      rewrite H1 in eqC2; inversion eqC2; subst; clear eqC2.
      unfold C.ValueSubst. destruct (L.eq_dec p p) as [_|neq]; [|destruct (neq eq_refl)].
      fold (ValSubst v).
      apply AppLocalStep; auto.
    - pose proof (ProjectChorExprSubst C p c (C.ValueSubst l v) eq).
      assert (forall n, ProjectChor (C.AppLocalSubst l C n) p =
                   Some ((fun m => match m with
                                | 0 => RecGlobal (c ceg| S)
                                | S m => Var m
                                end) n)).
      intro m. unfold C.AppLocalSubst. destruct m. cbn.
      destruct (L.eq_dec p l) as [eq'|_];[destruct (n0 eq')|].
      rewrite eq; auto.
      cbn; auto.
      pose proof (ProjectChorSubst _ p _ (C.AppLocalSubst l C) (fun n => match n with
                                                                      | 0 => RecGlobal (c ceg| S)
                                                                      | S m => Var m
                                                                      end) H1 H0).
      rewrite H2 in eqC2; inversion eqC2; subst; clear eqC2.
      pose proof (AppGlobalStep (c ceg| S) Unit UnitVal).
      unfold C.ValueSubst. destruct (L.eq_dec l p) as [eq' | _ ]; [destruct (n eq')|].
      rewrite ConExprLocalIdSubst.
      assert ((c ceg| S) [ceg|RecGlobalSubst (cceg| S) Unit] =
              c [ceg| fun m => match m with
                            | 0 => RecGlobal (c ceg| S)
                            | S m => Var m
                            end]).
      unfold RecGlobalSubst.
      rewrite ConExprGlobalRenameSubstFusion. reflexivity.
      rewrite <- H4. constructor. constructor.
    - assert (forall n, ProjectChor (C.AppGlobalSubst C1 C2 n) p = Some (match n with
                                                                    | 0 => c0
                                                                    | 1 => RecGlobal c
                                                                    | S (S n) => Var n
                                                                    end)).
      intro n; destruct n; cbn; auto.
      destruct n; auto. cbn; rewrite eq; reflexivity.
      pose proof (ProjectChorSubst _ p _ _ _ H0 eq).
      rewrite H1 in eqC2; inversion eqC2; subst; clear eqC2.
      fold (RecGlobalSubst c c0). constructor. eapply ProjectChorValue; eauto.
    - destruct d; inversion eqC1; subst; clear eqC1;
        constructor.
  Qed.

  Lemma ProjectChorBlocked : forall R B C1 C2 l E1 E2,
      In l B ->
      C.ChorStep R B C1 C2 ->
      ProjectChor C1 l = Some E1 ->
      ProjectChor C2 l = Some E2 ->
      LessNondet E2 E1.
  Proof using.
    intros R B C1 C2 l E1 E2 i step; revert l E1 E2 i; induction step;
      intros p E1 E2 i eq1 eq2; cbn in *;
        repeat match goal with
               | [ |- ?a = ?a ] => reflexivity
               | [ H :?P |- ?P ] => exact H
               | [ |- LessNondet ?a ?a ] => apply LessNondetRefl
               | [ H1 : ?P, H2 : ~?P |- _ ] => destruct (H2 H1)
               | [ H : ?a <> ?a |- _ ] => destruct (H eq_refl)
               | [ H : In ?a [] |- _ ] => inversion H
               | [ H : False |- _ ] => destruct H
               | [ H : Some _ = None |- _ ] => inversion H
               | [ H : None = Some _ |- _ ] => inversion H
               | [ H : Some _ = Some _ |- _ ] => inversion H; clear H; subst
               | [ H: context[L.eq_dec ?a ?b] |- _ ] =>
                 tryif unify a b
                 then
                   let neq := fresh in
                   destruct (L.eq_dec a b) as [_|neq]; [|destruct (neq eq_refl)]
                 else
                   lazymatch goal with
                   | [ e : a = b |- _ ] =>
                     let neq := fresh in
                     destruct (L.eq_dec a b) as [_|neq];[| destruct (neq e)]
                   | [ e : b = a |- _ ] =>
                     let neq := fresh in
                     destruct (L.eq_dec a b) as [_|neq];[|destruct (neq (eq_sym e))]
                   | [ n : a <> b |- _ ] =>
                     let eq := fresh in
                     destruct (L.eq_dec a b) as [eq|_]; [destruct (n eq)|]
                   | [ n : b <> a |- _ ] =>
                     let eq := fresh in
                     destruct (L.eq_dec a b) as [eq|_];[destruct (n (eq_sym eq))|]
                   | _ =>
                     let eq := fresh "eq" in
                     let neq := fresh "neq" in
                     destruct (L.eq_dec a b) as [eq|neq]; [subst|]
                   end
               | [ H: context[ProjectChor ?C ?l] |- _] =>
                   lazymatch type of H with
                   | ProjectChor C l = _ => fail
                   | _ =>
                     lazymatch goal with
                     | [ eq : ProjectChor C l = _ |- _ ] => rewrite eq in H; cbn in H
                     | _ =>
                       let eq := fresh "eq" in
                       destruct (ProjectChor C l) eqn:eq; cbn in *
                     end
                   end
               | [ H1 : ProjectChor ?C ?l = Some ?E1,
                        H2 : ProjectChor ?C ?l = Some ?E2 |- _ ] =>
                 tryif unify E1 E2
                 then clear H2
                 else rewrite H1 in H2; inversion H2; clear H2; subst
               | [ H1 : ConExprMerge ?E1 ?E2 = Some ?E,
                        H2 : ConExprMerge ?E1 ?E2 = Some ?E' |- _ ] =>
                 tryif unify E E'
                 then clear H2
                 else rewrite H1 in H2; inversion H2; clear H2; subst
               | [ IH : forall l E1 E2,
                     ?l1 = l \/ _ -> ProjectChor ?C1 l = Some E1 -> ProjectChor ?C2 l = Some E2 ->
                     LessNondet E2 E1,
                     H1 : ProjectChor ?C1 ?l1 = Some ?E1,
                     H2 : ProjectChor ?C2 ?l2 = Some ?E2 |- _ ] =>
                 lazymatch goal with
                 | [_ : LessNondet E2 E1 |- _ ] => fail
                 | _ => pose proof (IH l1 E1 E2 ltac:(left; reflexivity) H1 H2)
                 end
               | [ IH : forall l E1 E2,
                     _ \/ In l ?B -> ProjectChor ?C1 l = Some E1 -> ProjectChor ?C2 l = Some E2 ->
                     LessNondet E2 E1,
                     i : In ?l1 ?B,
                     H1 : ProjectChor ?C1 ?l1 = Some ?E1,
                     H2 : ProjectChor ?C2 ?l2 = Some ?E2 |- _ ] =>
                 lazymatch goal with
                 | [_ : LessNondet E2 E1 |- _ ] => fail
                 | _ => pose proof (IH l1 E1 E2 ltac:(right; exact i) H1 H2)
                 end
               end; try (eauto with ConExpr; fail).
    - rewrite ProjectChorExprSubst with (E := E1) in eq2; auto;
        inversion eq2; clear eq2; subst; unfold C.ValueSubst.
      destruct (L.eq_dec l2 p) as [eq|_]; [destruct (neq0 (eq_sym eq))|].
      rewrite ConExprLocalIdSubst; apply LessNondetRefl.
    - apply (LessNondetMerge c c0 c1 c2); auto.
    - apply MergeLessNondet in eq1; auto.
    - rewrite MergeComm in eq1; apply MergeLessNondet in eq1; auto.
    - destruct d; inversion eq1; clear eq1; subst; inversion eq2; clear eq2; subst;
        eauto with ConExpr.
  Qed.



  Lemma ProjectChorRedex : forall C1 R B C2 l E1 E2,
      ProjectChor C1 l = Some E1 ->
      ProjectChor C2 l = Some E2 ->
      ProjectRedex R l = None ->
      C.ChorStep R B C1 C2 ->
      LessNondet E2 E1.
  Proof using.
    intros C1 R B C2 l E1 E2 eq1 eq2 eq3 step;
      revert l E1 E2 eq1 eq2 eq3; induction step; cbn;
        intros p E1 E2 eq1 eq2 eq3;
        repeat (try discriminate;
                match goal with
                | [ H: Some _ = Some _ |- _ ] => inversion H; clear H; subst
                | [ H : context[L.eq_dec ?a ?b] |- _ ] =>
                  tryif unify a b
                  then let n := fresh in
                       destruct (L.eq_dec a b) as [_ | n];
                       [| destruct (n eq_refl)]
                  else lazymatch goal with
                       | [e : a = b |- _ ] =>
                         let n := fresh in
                         destruct (L.eq_dec a b) as [_ | n];
                         [| destruct (n e)]
                       | [e : b = a |- _ ] =>
                         let n := fresh in
                         destruct (L.eq_dec a b) as [_ | n];
                         [| destruct (n (eq_sym e))]
                       | [n : a <> b |- _ ] =>
                         let e := fresh in
                         destruct (L.eq_dec a b) as [e | _];
                         [destruct (n e)|]
                       | [n : b <> a |- _ ] =>
                         let e := fresh in
                         destruct (L.eq_dec a b) as [e | _];
                         [destruct (n (eq_sym e))|]
                       | _ => let eq := fresh "eq" in
                             let neq := fresh "neq" in
                             destruct (L.eq_dec a b) as [eq|neq]; subst
                       end
                | [ H : context[ProjectChor ?C ?l] |- _ ] =>
                  lazymatch type of H with
                  | ProjectChor C l = _ => fail
                  | _ => lazymatch goal with
                        | [ H' : ProjectChor C l = _ |- _ ] => rewrite H' in H
                        | _ => let eq := fresh "eq" in destruct (ProjectChor C l) eqn:eq
                        end
                  end
                | [H1 : ProjectChor ?C ?p = _, H2 : ProjectChor ?C ?p = _ |- _ ] =>
                  rewrite H1 in H2
                | [H1 : ConExprMerge ?C ?p = _, H2 : ConExprMerge ?C ?p = _ |- _ ] =>
                  rewrite H1 in H2
                | [ IH : forall l E1 E2, ProjectChor ?C1 l = Some E1 ->
                                    ProjectChor ?C2 l = Some E2 ->
                                    ProjectRedex ?R l = None ->
                                    LessNondet E2 E1,
                      H1 : ProjectChor ?C1 ?l = Some ?E1,
                      H2 : ProjectChor ?C2 ?l = Some ?E2,
                      H3 : ProjectRedex ?R ?l = None |- _ ] =>
                  lazymatch goal with
                  | [ _ : LessNondet E2 E1 |- _ ] => fail
                  | _ => pose proof (IH l E1 E2 H1 H2 H3)
                  end
                end); auto with ConExpr.
    - rewrite ProjectChorExprSubst with (E := E1) in eq2; auto;
        inversion eq2; clear eq2; subst.
      unfold C.ValueSubst.
      destruct (L.eq_dec l2 p) as [e | _]; [destruct (neq0 (eq_sym e))|].
      rewrite ConExprLocalIdSubst; reflexivity.
    - apply (LessNondetMerge c0 c c2 c1); auto.
      all: rewrite MergeComm; auto.
    - apply MergeLessNondet in eq1; auto.
    - rewrite MergeComm in eq1; apply MergeLessNondet in eq1; auto.
    -  destruct d; inversion eq2; inversion eq1; clear eq1 eq2; subst;
         auto with ConExpr.
  Qed.

  Fixpoint SystemOfNames (C : C.Chor) (nms : list L.t) : option ConSystem :=
    match nms with
    | nil => Some LM.empty
    | cons l nms => match ProjectChor C l, SystemOfNames C nms with
                   | Some E, Some M => Some (LM.add l E M)
                   | _, _ => None
                   end
    end.


  Lemma SystemOfNamesLookup : forall C nms Π, SystemOfNames C nms = Some Π ->
                                         forall l E, LM.MapsTo l E Π <->
                                                (In l nms /\ ProjectChor C l = Some E).
  Proof using.
    intros C nms; revert C; induction nms as [| l nms]; intros C Π eq l' E; cbn in eq.
    - inversion eq; subst; clear eq. split; [intro mt | intro H; destruct H as [i eq]].
      exfalso; apply LM.empty_1 in mt; assumption. inversion i.
    - destruct (ProjectChor C l) as [E'|] eqn:eq0; try discriminate.
      destruct (SystemOfNames C nms) as [Π'|] eqn:eq1; inversion eq; subst; clear eq.
      destruct (L.eq_dec l l'); subst.
      all: split; [intro mt; split | intros [i eq2]]; cbn in *; auto.
      pose proof (LMF.MapsToUnique mt (LM.add_1 Π' l' E')); subst; auto.
      rewrite eq2 in eq0; inversion eq0; subst; apply LM.add_1.
      right.
      1,2: apply LM.add_3 in mt; auto; pose proof (proj1 (IHnms C Π' eq1 l' E) mt) as H;
        destruct H; auto.
      destruct i as [eq | i]; [destruct (n eq)|].
      apply LM.add_2; auto.
      eapply IHnms; eauto.
  Qed.

  Lemma SystemOfNamesLookupNone : forall C nms l,
      In l nms ->
      ProjectChor C l = None ->
      SystemOfNames C nms = None.
  Proof using.
    intros C nms; revert C; induction nms as [| l' nms]; intros C l i eq.
    inversion i.
    destruct i as [H | i]; subst; cbn.
    rewrite eq; auto.
    destruct (ProjectChor C l'); [| reflexivity].
    rewrite IHnms with (l := l); auto.
  Qed.


  Fixpoint InLocList (l : Loc) (nms : list Loc) : bool :=
    match nms with
    | [] => false
    | l' :: nms => if L.eq_dec l l'
                 then true
                 else InLocList l nms
    end.

  Lemma In_InLocList : forall l nms, In l nms -> InLocList l nms = true.
  Proof using.
    intros l nms; revert l; induction nms as [| l' nms]; intros l i; cbn.
    inversion i. destruct i as [eq|i]; subst.
    destruct (L.eq_dec l l) as [_|n]; [| destruct (n eq_refl)]; reflexivity.
    destruct (L.eq_dec l l'); try reflexivity. apply IHnms; auto.
  Qed.

  Lemma InLocList_In : forall l nms, InLocList l nms = true -> In l nms.
  Proof using.
    intros l nms; induction nms as [| l' nms]; intro eq; cbn in eq; [inversion eq|].
    destruct (L.eq_dec l l'); [left; auto|]. right; apply IHnms; auto.
  Qed.

  Lemma NotIn_InLocList : forall l nms, ~ In l nms -> InLocList l nms = false.
  Proof using.
    intros l nms; induction nms as [| l' nms]; intro ni; cbn; try reflexivity.
    destruct (L.eq_dec l l'). exfalso; apply ni; left; auto. apply IHnms.
    intro i; apply ni; right; auto.
  Qed.

  Lemma InLocList_NotIn : forall l nms, InLocList l nms = false -> ~ In l nms.
  Proof using.
    intros l nms; induction nms as [| l' nms]; intro eq; cbn in eq. intro i; inversion i.
    destruct (L.eq_dec l l'); [inversion eq|]. intro i; destruct i.
    apply n; auto. apply IHnms; auto.
  Qed.

  Fixpoint RepeatVar (x : nat) (nms : list L.t) : ConSystem :=
    match nms with
    | [] => LM.empty
    | l :: nms => LM.add l (Var x) (RepeatVar x nms)
    end.

  Fixpoint RepeatUnits (nms : list L.t) : ConSystem :=
    match nms with
    | [] => LM.empty
    | l :: nms => LM.add l Unit (RepeatUnits nms)
    end.
  
  Fixpoint NamesWithout (nms : list L.t) (l : L.t) : list L.t :=
    match nms with
    | [] => []
    | l' :: nms => if L.eq_dec l l'
                 then NamesWithout nms l
                 else l' :: (NamesWithout nms l)
    end.

  Lemma InNamesWithout : forall (nms : list L.t) (l : L.t) (l' : L.t),
      In l' (NamesWithout nms l) <-> In l' nms /\ l <> l'.
  Proof using.
    intro nms; induction nms as [|l'' nms]; intros l l'.
    all: split; [intro i; cbn in i | intros [i neq]; cbn].
    destruct i. inversion i.
    all: destruct (L.eq_dec l l''); subst.
    apply IHnms in i; destruct i as [i neq]; split; [right|]; auto.
    destruct i as [eq|i]; subst. split; [left|]; auto.
    apply IHnms in i; destruct i as [i neq]; split; [right|]; auto.
    destruct i as [eq|i]; [destruct (neq eq)|]. apply IHnms; auto.
    destruct i as [eq|i]; [left |right; apply IHnms]; auto.
  Qed.

  Fixpoint MergeSystemsAtNames (Π1 Π2 : ConSystem) (nms : list L.t) : option ConSystem :=
    match nms with
    | [] => Some (LM.empty)
    | l :: nms => match LM.find l Π1, LM.find l Π2 with
                | Some E1, Some E2 => match ConExprMerge E1 E2 with
                                     | Some E => match (MergeSystemsAtNames Π1 Π2 nms) with
                                                | Some Π => Some (LM.add l E Π)
                                                | None => None
                                                end
                                     | None => None
                                     end
                | _, _ => None
                end
    end.

  (* Fixpoint SystemOfNamesDirect (C : C.Chor) (nms : list L.t) : option ConSystem := *)
  (*   match C with *)
  (*   | C.Done l e => *)
  (*     if InLocList l nms *)
  (*     then Some (LM.add l (Ret e) (RepeatUnits nms)) *)
  (*     else Some (RepeatUnits nms) *)
  (*   | C.Var x => *)
  (*     Some (RepeatVar x nms) *)
  (*   | C.Send l1 e l2 C => *)
  (*     if InLocList l1 nms *)
  (*     then if InLocList l2 nms *)
  (*          then if L.eq_dec l1 l2 *)
  (*               then None *)
  (*               else match SystemOfNamesDirect C nms with *)
  (*                    | Some Π => match LM.find l1 Π with *)
  (*                               | Some E1 => match LM.find l2 Π with *)
  (*                                           | Some E2 => *)
  (*                                             Some (LM.add l1 (Send l2 e E1) *)
  (*                                                          (LM.add l2 (Recv l1 E2) Π)) *)
  (*                                           | None => None *)
  (*                                           end *)
  (*                               | None => None *)
  (*                               end *)
  (*                    | None => None *)
  (*                    end *)
  (*          else match SystemOfNamesDirect C nms with *)
  (*               | Some Π => match LM.find l1 Π with *)
  (*                          | Some E1 => Some (LM.add l1 (Send l2 e E1) Π) *)
  (*                          | None => None *)
  (*                          end *)
  (*               | None => None *)
  (*               end *)
  (*     else if InLocList l2 nms *)
  (*          then match SystemOfNamesDirect C nms with *)
  (*               | Some Π => match LM.find l2 Π with *)
  (*                          | Some E2 => Some (LM.add l2 (Recv l2 E2) Π) *)
  (*                          | None => None *)
  (*                          end *)
  (*               | None => None *)
  (*               end *)
  (*          else SystemOfNamesDirect C nms *)
  (*   | C.If l e C1 C2 => *)
  (*     if InLocList l nms *)
  (*     then match SystemOfNamesDirect C1 nms, SystemOfNamesDirect C2 nms with *)
  (*          | Some Π1, Some Π2 => *)
  (*            match LM.find l Π1, LM.find l Π2 with *)
  (*            | Some E1, Some E2 => *)
  (*              match MergeSystemsAtNames Π1 Π2 (NamesWithout nms l) with *)
  (*              | Some Π => Some (LM.add l (If e E1 E2) Π) *)
  (*              | None => None *)
  (*              end *)
  (*            | _, _ => None *)
  (*            end *)
  (*          | _ , _ => None *)
  (*          end *)
  (*     else match SystemOfNamesDirect C1 nms, SystemOfNamesDirect C2 nms with *)
  (*          | Some Π1, Some Π2 => MergeSystemsAtNames Π1 Π2 nms *)
  (*          | _, _ => None *)
  (*          end *)
  (*   | C.Sync l1 d l2 C => *)
  (*     if InLocList l1 nms *)
  (*     then if InLocList l2 nms *)
  (*          then if L.eq_dec l1 l2 *)
  (*               then None *)
  (*               else match SystemOfNamesDirect C nms with *)
  (*                    | Some Π => match LM.find l1 Π, LM.find l2 Π with *)
  (*                               | Some E1, Some E2 => *)
  (*                                 match d with *)
  (*                                 | C.LChoice => *)
  (*                                   Some (LM.add l1 (Choose l2 C.LChoice E1) *)
  (*                                                (LM.add l2 (AllowChoiceL l1 E2) Π)) *)
  (*                                 | C.RChoice => *)
  (*                                   Some (LM.add l1 (Choose l2 C.RChoice E1) *)
  (*                                                (LM.add l2 (AllowChoiceR l1 E2) Π)) *)
  (*                                 end *)
  (*                               | _, _ => None *)
  (*                               end *)
  (*                    | None => None *)
  (*                    end *)
  (*          else match SystemOfNamesDirect C nms with *)
  (*               | Some Π => match LM.find l1 Π with *)
  (*                          | Some E1 => Some (LM.add l1 (Choose l2 d E1) Π) *)
  (*                          | None => None *)
  (*                          end *)
  (*               | None => None *)
  (*               end *)
  (*     else if InLocList l2 nms *)
  (*          then match SystemOfNamesDirect C nms with *)
  (*               | Some Π => match LM.find l2 Π with *)
  (*                          | Some E2 => *)
  (*                            match d with *)
  (*                            | C.LChoice => Some (LM.add l2 (AllowChoiceL l1 E2) Π) *)
  (*                            | C.RChoice => Some (LM.add l2 (AllowChoiceR l1 E2) Π) *)
  (*                            end *)
  (*                          | None => None *)
  (*                          end *)
  (*               | None => None *)
  (*               end *)
  (*          else SystemOfNamesDirect C nms *)
  (*   | C.DefLocal l C1 C2 => *)
      
  (*   | C.RecLocal x x0 => _ *)
  (*   | C.RecGlobal x => _ *)