-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathContexts.v
1604 lines (1436 loc) · 49 KB
/
Contexts.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Export QuantumLib.Prelim.
Require Export Monoid.
Open Scope list_scope.
(*** Context Definitions ***)
(** Types **)
Inductive WType := Qubit | Bit | One | Tensor : WType -> WType -> WType.
Declare Scope circ_scope.
Notation " W1 ⊗ W2 " := (Tensor W1 W2) (at level 40, left associativity)
: circ_scope.
Open Scope circ_scope.
Fixpoint size_wtype (W : WType) : nat :=
match W with
| One => 0
| Qubit => 1
| Bit => 1
| W1 ⊗ W2 => size_wtype W1 + size_wtype W2
end.
(* Coq interpretations of wire types *)
Fixpoint interpret (w:WType) : Set :=
match w with
| Qubit => bool
| Bit => bool
| One => unit
| w1 ⊗ w2 => (interpret w1) * (interpret w2)
end.
(* Large tensor product. Right associative with a trailing One *)
Fixpoint NTensor (n : nat) (W : WType) :=
match n with
| 0 => One
| S n' => W ⊗ NTensor n' W
end.
Infix "⨂" := NTensor (at level 30) : circ_scope.
Lemma size_ntensor : forall n W, size_wtype (n ⨂ W) = (n * size_wtype W)%nat.
Proof.
intros n W.
induction n; trivial.
simpl.
rewrite IHn.
reflexivity.
Qed.
Close Scope circ_scope.
(** Variables **)
Definition Var := nat.
Definition Ctx := list (option WType).
Inductive OCtx :=
| Invalid : OCtx
| Valid : Ctx -> OCtx.
Lemma ctx_octx : forall Γ Γ', Valid Γ = Valid Γ' <-> Γ = Γ'.
Proof. intuition; congruence. Defined.
(* The size of a context is the number of wires it holds *)
Fixpoint size_ctx (Γ : Ctx) : nat :=
match Γ with
| [] => 0
| None :: Γ' => size_ctx Γ'
| Some _ :: Γ' => S (size_ctx Γ')
end.
Definition size_octx (Γ : OCtx) : nat :=
match Γ with
| Invalid => 0
| Valid Γ' => size_ctx Γ'
end.
Lemma size_ctx_size_octx : forall (Γ : Ctx), size_ctx Γ = size_octx (Valid Γ).
Proof. easy. Qed.
Lemma size_ctx_app : forall (Γ1 Γ2 : Ctx),
size_ctx (Γ1 ++ Γ2) = (size_ctx Γ1 + size_ctx Γ2)%nat.
Proof.
induction Γ1; intros; simpl; auto.
destruct a; trivial.
rewrite IHΓ1; easy.
Qed.
(**********************)
(* Singleton Contexts *)
(**********************)
Inductive SingletonCtx : Var -> WType -> Ctx -> Prop :=
| SingletonHere : forall w, SingletonCtx 0 w [Some w]
| SingletonLater : forall x w Γ, SingletonCtx x w Γ -> SingletonCtx (S x) w (None::Γ).
Inductive SingletonOCtx x w : OCtx -> Prop :=
| SingletonValid : forall Γ, SingletonCtx x w Γ -> SingletonOCtx x w (Valid Γ).
Lemma Singleton_size : forall x w Γ, SingletonCtx x w Γ -> size_ctx Γ = 1%nat.
Proof. induction 1; auto. Qed.
Fixpoint singleton (x : Var) (W : WType) : Ctx :=
match x with
| O => [Some W]
| S x => None :: singleton x W
end.
Lemma singleton_singleton : forall x W,
SingletonCtx x W (singleton x W).
Proof.
induction x; intros W.
- constructor.
- simpl. constructor. apply IHx.
Defined.
Lemma singleton_equiv : forall x W Γ,
SingletonCtx x W Γ -> Γ = singleton x W.
Proof.
induction 1; trivial.
simpl. rewrite IHSingletonCtx. reflexivity.
Defined.
Lemma singleton_size : forall x w, size_ctx (singleton x w) = 1%nat.
Proof. induction x; auto. Qed.
(***********)
(* Merging *)
(***********)
Definition merge_wire (o1 o2 : option WType) : OCtx :=
match o1, o2 with
| None, o2 => Valid [o2]
| Some w1, None => Valid [Some w1]
| _, _ => Invalid
end.
Fixpoint merge' (Γ1 Γ2 : Ctx) : OCtx :=
match Γ1, Γ2 with
| [], _ => Valid Γ2
| _, [] => Valid Γ1
| o1 :: Γ1', o2 :: Γ2' => match merge_wire o1 o2 with
| Invalid => Invalid
| Valid Γ0 => match merge' Γ1' Γ2' with
| Invalid => Invalid
| Valid Γ' => Valid (Γ0 ++ Γ')
end
end
end.
Definition merge (Γ1 Γ2 : OCtx) : OCtx :=
match Γ1, Γ2 with
| Valid Γ1', Valid Γ2' => merge' Γ1' Γ2'
| _, _ => Invalid
end.
(* Merge will generally be opaque outside of this file *)
Lemma merge_shadow : merge = fun Γ1 Γ2 =>
match Γ1 with
| Invalid => Invalid
| Valid Γ1' => match Γ2 with
| Invalid => Invalid
| Valid Γ2' => merge' Γ1' Γ2'
end
end. Proof. reflexivity. Qed.
Ltac unlock_merge := rewrite merge_shadow in *.
Notation "∅" := (Valid []).
Infix "⋓" := merge (left associativity, at level 50).
Coercion Valid : Ctx >-> OCtx.
(*** Properties of ⋓ ***)
Lemma merge_merge' : forall (Γ1 Γ2 : Ctx), Γ1 ⋓ Γ2 = (merge' Γ1 Γ2).
Proof. reflexivity. Defined.
(* Note that the reverse is not always true - we would need to
check validity and not-emptyness of the contexts *)
Lemma merge_cancel_l : forall Γ Γ1 Γ2 , Γ1 = Γ2 -> Γ ⋓ Γ1 = Γ ⋓ Γ2.
Proof. intros; subst; reflexivity. Defined.
Lemma merge_cancel_r : forall Γ Γ1 Γ2 , Γ1 = Γ2 -> Γ1 ⋓ Γ = Γ2 ⋓ Γ.
Proof. intros; subst; reflexivity. Defined.
Lemma merge_I_l : forall Γ, Invalid ⋓ Γ = Invalid. Proof. reflexivity. Defined.
Lemma merge_I_r : forall Γ, Γ ⋓ Invalid = Invalid. Proof. destruct Γ; reflexivity.
Defined.
(*
Lemma merge_valid : forall (Γ1 Γ2 : OCtx) (Γ : Ctx),
Γ1 ⋓ Γ2 = Valid Γ ->
(exists Γ1', Γ1 = Valid Γ1') /\ (exists Γ2', Γ2 = Valid Γ2').
Proof.
intros Γ1 Γ2 Γ M.
destruct Γ1 as [|Γ1'], Γ2 as [|Γ2']; inversion M.
eauto.
Qed.
*)
Lemma merge_valid : forall (Γ1 Γ2 : OCtx) (Γ : Ctx),
Γ1 ⋓ Γ2 = Valid Γ ->
{Γ1' : Ctx & Γ1 = Valid Γ1'} * {Γ2' : Ctx & Γ2 = Valid Γ2'}.
Proof.
intros Γ1 Γ2 Γ M.
destruct Γ1 as [|Γ1'], Γ2 as [|Γ2']; inversion M.
eauto.
Defined.
Lemma merge_invalid_iff : forall (o1 o2 : option WType) (Γ1 Γ2 : Ctx),
Valid (o1 :: Γ1) ⋓ Valid (o2 :: Γ2) = Invalid <->
merge_wire o1 o2 = Invalid \/ Γ1 ⋓ Γ2 = Invalid.
Proof.
intros o1 o2 Γ1 Γ2.
split.
+ intros H.
destruct o1, o2; auto; right; simpl in H.
- rewrite <- merge_merge' in H.
destruct (Γ1 ⋓ Γ2); trivial.
inversion H.
- rewrite <- merge_merge' in H.
destruct (Γ1 ⋓ Γ2); trivial.
inversion H.
- rewrite <- merge_merge' in H.
destruct (Γ1 ⋓ Γ2); trivial.
inversion H.
+ intros H.
inversion H.
simpl. rewrite H0; reflexivity.
simpl.
destruct (merge_wire o1 o2); trivial.
rewrite merge_merge' in H0.
rewrite H0.
reflexivity.
Defined.
Lemma merge_nil_l : forall Γ, ∅ ⋓ Γ = Γ. Proof. destruct Γ; reflexivity. Defined.
Lemma merge_nil_r : forall Γ, Γ ⋓ ∅ = Γ.
Proof. destruct Γ; trivial. destruct c; trivial. Defined.
Lemma merge_comm : forall Γ1 Γ2, Γ1 ⋓ Γ2 = Γ2 ⋓ Γ1.
Proof.
intros Γ1 Γ2.
destruct Γ1 as [|Γ1], Γ2 as [|Γ2]; trivial.
generalize dependent Γ2.
induction Γ1.
+ destruct Γ2; trivial.
+ destruct Γ2; trivial.
simpl.
unfold merge in IHΓ1.
rewrite IHΓ1.
destruct a, o; reflexivity.
Defined.
Lemma merge_assoc : forall Γ1 Γ2 Γ3, Γ1 ⋓ (Γ2 ⋓ Γ3) = Γ1 ⋓ Γ2 ⋓ Γ3.
Proof.
intros Γ1 Γ2 Γ3.
destruct Γ1 as [|Γ1], Γ2 as [|Γ2], Γ3 as [|Γ3]; repeat (rewrite merge_I_r); trivial.
generalize dependent Γ3. generalize dependent Γ1.
induction Γ2 as [| o2 Γ2'].
+ intros. rewrite merge_nil_l, merge_nil_r; reflexivity.
+ intros Γ1 Γ3.
destruct Γ1 as [|o1 Γ1'], Γ3 as [| o3 Γ3'] ; trivial.
- rewrite 2 merge_nil_l.
reflexivity.
- rewrite 2 merge_nil_r.
reflexivity.
- destruct o1, o2, o3; trivial.
* simpl. destruct (merge' Γ2' Γ3'); reflexivity.
* simpl. destruct (merge' Γ1' Γ2'), (merge' Γ2' Γ3'); reflexivity.
* simpl. destruct (merge' Γ1' Γ2') eqn:M12, (merge' Γ2' Γ3') eqn:M23.
reflexivity.
rewrite <- merge_merge' in *.
rewrite <- M23.
rewrite IHΓ2'.
rewrite M12.
reflexivity.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
reflexivity.
destruct (merge' Γ1' c0) eqn:M123.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
assumption.
simpl.
rewrite <- merge_merge' in *.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23, M123.
reflexivity.
* simpl. destruct (merge' Γ1' Γ2'), (merge' Γ2' Γ3'); reflexivity.
* simpl. destruct (merge' Γ1' Γ2') eqn:M12, (merge' Γ2' Γ3') eqn:M23.
reflexivity.
rewrite <- merge_merge' in *.
rewrite <- M23.
rewrite IHΓ2'.
rewrite M12.
reflexivity.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
reflexivity.
destruct (merge' Γ1' c0) eqn:M123.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
assumption.
simpl.
rewrite <- merge_merge' in *.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23, M123.
reflexivity.
* simpl. destruct (merge' Γ1' Γ2') eqn:M12, (merge' Γ2' Γ3') eqn:M23.
reflexivity.
rewrite <- merge_merge' in *.
rewrite <- M23.
rewrite IHΓ2'.
rewrite M12.
reflexivity.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
reflexivity.
destruct (merge' Γ1' c0) eqn:M123.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
assumption.
simpl.
rewrite <- merge_merge' in *.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23, M123.
reflexivity.
* simpl. destruct (merge' Γ1' Γ2') eqn:M12, (merge' Γ2' Γ3') eqn:M23.
reflexivity.
rewrite <- merge_merge' in *.
rewrite <- M23.
rewrite IHΓ2'.
rewrite M12.
reflexivity.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
reflexivity.
destruct (merge' Γ1' c0) eqn:M123.
rewrite <- merge_merge' in *.
symmetry. apply merge_invalid_iff. right.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23.
assumption.
simpl.
rewrite <- merge_merge' in *.
rewrite <- M12.
rewrite <- IHΓ2'.
rewrite M23, M123.
reflexivity.
Defined.
Definition cons_o (o : option WType) (Γ : OCtx) : OCtx :=
match Γ with
| Invalid => Invalid
| Valid Γ' => Valid (o :: Γ')
end.
Lemma cons_distr_merge : forall Γ1 Γ2,
cons_o None (Γ1 ⋓ Γ2) = cons_o None Γ1 ⋓ cons_o None Γ2.
Proof. destruct Γ1; destruct Γ2; simpl; auto. Defined.
Lemma merge_nil_inversion' : forall (Γ1 Γ2 : Ctx), Γ1 ⋓ Γ2 = ∅ -> (Γ1 = []) * (Γ2 = []).
Proof.
induction Γ1 as [ | o Γ1]; intros Γ2; try inversion 1; auto.
destruct Γ2 as [ | o' Γ2]; try solve [inversion H1].
destruct o, o', (merge' Γ1 Γ2); inversion H1.
Defined.
Lemma merge_nil_inversion : forall (Γ1 Γ2 : OCtx), Γ1 ⋓ Γ2 = ∅ -> (Γ1 = ∅) * (Γ2 = ∅).
Proof.
intros Γ1 Γ2 eq.
destruct Γ1 as [|Γ1], Γ2 as [|Γ2]; try solve [inversion eq].
apply merge_nil_inversion' in eq.
intuition; congruence.
Defined.
Lemma ctx_cons_inversion : forall (Γ Γ1 Γ2 : Ctx) o o1 o2,
Valid (o1 :: Γ1) ⋓ Valid (o2 :: Γ2) = Valid (o :: Γ) ->
(Γ1 ⋓ Γ2 = Valid Γ) * (merge_wire o1 o2 = Valid [o]).
Proof.
intros Γ Γ1 Γ2 o o1 o2 H.
inversion H.
destruct (merge_wire o1 o2) eqn:Eq1. inversion H1.
rewrite <- merge_merge' in H1.
destruct (Γ1 ⋓ Γ2) eqn:Eq2. inversion H1.
destruct o1, o2; simpl in Eq1. inversion Eq1.
- apply ctx_octx in Eq1. rewrite <- Eq1 in *.
simpl in H1.
inversion H1; subst; auto.
- apply ctx_octx in Eq1. rewrite <- Eq1 in *.
simpl in H1.
inversion H1; subst; auto.
- apply ctx_octx in Eq1. rewrite <- Eq1 in *.
simpl in H1.
inversion H1; subst; auto.
Defined.
Lemma merge_singleton_append : forall W (Γ : Ctx),
Γ ⋓ (singleton (length Γ) W) = Valid (Γ ++ [Some W]).
Proof.
induction Γ.
- simpl. reflexivity.
- simpl in *.
destruct a; simpl; rewrite IHΓ; reflexivity.
Qed.
Lemma merge_offset : forall (n : nat) (Γ1 Γ2 Γ : Ctx), Valid Γ = Γ1 ⋓ Γ2 ->
Valid (repeat None n ++ Γ1) ⋓ Valid (repeat None n ++ Γ2) =
Valid (repeat None n ++ Γ).
Proof.
intros n Γ1 Γ2 Γ H.
induction n.
- simpl. auto.
- simpl in *.
rewrite IHn.
reflexivity.
Qed.
(*** OContexts are a PCM ***)
(* Partial Commutative Monoids *)
Instance PCM_OCtx : PCM OCtx :=
{ one := ∅;
zero := Invalid;
m := merge
}.
Instance PCM_Laws_OCtx : PCM_Laws OCtx :=
{ M_unit := merge_nil_r
; M_assoc := merge_assoc
; M_comm := merge_comm
; M_absorb := merge_I_r
}.
#[export] Hint Resolve PCM_OCtx : core.
#[export] Hint Resolve PCM_Laws_OCtx : core.
(*** Validity ***)
Definition is_valid (Γ : OCtx) : Prop := exists Γ', Γ = Valid Γ'.
Record valid_merge Γ1 Γ2 Γ :=
{ pf_valid : is_valid Γ
; pf_merge : Γ = Γ1 ⋓ Γ2 }.
Notation "Γ == Γ1 ∙ Γ2" := (valid_merge Γ1 Γ2 Γ) (at level 20).
Lemma valid_valid : forall Γ, is_valid (Valid Γ). Proof. intros. exists Γ. reflexivity. Defined.
Lemma valid_empty : is_valid ∅. Proof. unfold is_valid; eauto. Defined.
Lemma not_valid : not (is_valid Invalid). Proof. intros [Γ F]; inversion F. Defined.
Lemma valid_l : forall Γ1 Γ2, is_valid (Γ1 ⋓ Γ2) -> is_valid Γ1.
Proof.
intros Γ1 Γ2 V.
unfold is_valid in *.
destruct V as [Γ' V].
apply merge_valid in V as [[Γ1'] [Γ2']].
eauto.
Defined.
Lemma valid_r : forall Γ1 Γ2, is_valid (Γ1 ⋓ Γ2) -> is_valid Γ2.
Proof.
intros Γ1 Γ2 V.
unfold is_valid in *.
destruct V as [Γ' V].
apply merge_valid in V as [[Γ1'] [Γ2']].
eauto.
Defined.
Lemma valid_cons : forall (o1 o2 : option WType) (Γ1 Γ2 : Ctx),
is_valid (Valid (o1 :: Γ1) ⋓ Valid (o2 :: Γ2)) <->
(is_valid (merge_wire o1 o2) /\ is_valid (Γ1 ⋓ Γ2)).
Proof.
intros o1 o2 Γ1 Γ2. split.
- intros [Γ V].
inversion V.
destruct (merge_wire o1 o2). inversion H0.
simpl. destruct (merge' Γ1 Γ2). inversion H0.
unfold is_valid; split; eauto.
- intros [[W Vo] [Γ V]].
simpl in *.
rewrite Vo, V.
unfold is_valid; eauto.
Defined.
Lemma valid_join : forall Γ1 Γ2 Γ3, is_valid (Γ1 ⋓ Γ2) -> is_valid (Γ1 ⋓ Γ3) -> is_valid (Γ2 ⋓ Γ3) ->
is_valid (Γ1 ⋓ Γ2 ⋓ Γ3).
Proof.
destruct Γ1 as [|Γ1]. intros Γ2 Γ3 [Γ12 V12]; inversion V12.
induction Γ1 as [|o1 Γ1].
+ intros Γ2 Γ3 V12 V13 V23. rewrite merge_nil_l. assumption.
+ intros Γ2 Γ3 V12 V13 V23.
destruct Γ2 as [|Γ2], Γ3 as [|Γ3]; try solve [inversion V23; inversion H].
destruct Γ2 as [|o2 Γ2], Γ3 as [|o3 Γ3]; try (rewrite merge_nil_r in *; auto).
destruct o1, o2, o3; try solve [inversion V12; inversion H];
try solve [inversion V13; inversion H];
try solve [inversion V23; inversion H].
- apply valid_cons in V12 as [_ [Γ12 V12]].
apply valid_cons in V13 as [_ [Γ13 V13]].
apply valid_cons in V23 as [_ [Γ23 V23]].
destruct (IHΓ1 (Valid Γ2) (Valid Γ3)) as [Γ V123]; unfold is_valid; eauto.
exists (Some w :: Γ).
simpl in *. rewrite V12.
simpl in *. rewrite V12 in V123. simpl in V123. rewrite V123.
reflexivity.
- apply valid_cons in V12 as [_ [Γ12 V12]].
apply valid_cons in V13 as [_ [Γ13 V13]].
apply valid_cons in V23 as [_ [Γ23 V23]].
destruct (IHΓ1 (Valid Γ2) (Valid Γ3)) as [Γ V123]; unfold is_valid; eauto.
exists (Some w :: Γ).
simpl in *. rewrite V12.
simpl in *. rewrite V12 in V123. simpl in V123. rewrite V123.
reflexivity.
- apply valid_cons in V12 as [_ [Γ12 V12]].
apply valid_cons in V13 as [_ [Γ13 V13]].
apply valid_cons in V23 as [_ [Γ23 V23]].
destruct (IHΓ1 (Valid Γ2) (Valid Γ3)) as [Γ V123]; unfold is_valid; eauto.
exists (Some w :: Γ).
simpl in *. rewrite V12.
simpl in *. rewrite V12 in V123. simpl in V123. rewrite V123.
reflexivity.
- apply valid_cons in V12 as [_ [Γ12 V12]].
apply valid_cons in V13 as [_ [Γ13 V13]].
apply valid_cons in V23 as [_ [Γ23 V23]].
destruct (IHΓ1 (Valid Γ2) (Valid Γ3)) as [Γ V123]; unfold is_valid; eauto.
exists (None :: Γ).
simpl in *. rewrite V12.
simpl in *. rewrite V12 in V123. simpl in V123. rewrite V123.
reflexivity.
Defined.
Lemma valid_split : forall Γ1 Γ2 Γ3, is_valid (Γ1 ⋓ Γ2 ⋓ Γ3) ->
is_valid (Γ1 ⋓ Γ2) /\ is_valid (Γ1 ⋓ Γ3) /\ is_valid (Γ2 ⋓ Γ3).
Proof.
intros Γ1 Γ2 Γ3 [Γ V].
unfold is_valid.
intuition.
+ destruct (Γ1 ⋓ Γ2); [inversion V | eauto].
+ rewrite (merge_comm Γ1 Γ2), <- merge_assoc in V.
destruct (Γ1 ⋓ Γ3); [rewrite merge_I_r in V; inversion V | eauto].
+ rewrite <- merge_assoc in V.
destruct (Γ2 ⋓ Γ3); [rewrite merge_I_r in V; inversion V | eauto].
Defined.
Lemma size_octx_merge : forall (Γ1 Γ2 : OCtx), is_valid (Γ1 ⋓ Γ2) ->
size_octx (Γ1 ⋓ Γ2) = (size_octx Γ1 + size_octx Γ2)%nat.
Proof.
intros Γ1 Γ2 V.
destruct Γ1 as [ | Γ1], Γ2 as [ | Γ2]; try apply not_valid in V; try easy.
revert Γ2 V.
induction Γ1 as [ | [W1 | ] Γ1]; intros Γ2 V;
[rewrite merge_nil_l; auto | | ];
(destruct Γ2 as [ | [W2 | ] Γ2]; [rewrite merge_nil_r; auto | |]);
[ absurd (is_valid Invalid); auto; apply not_valid | | |].
- specialize IHΓ1 with Γ2.
simpl in *.
destruct (merge' Γ1 Γ2) as [ | Γ] eqn:H;
[absurd (is_valid Invalid); auto; apply not_valid | ].
simpl in *. rewrite IHΓ1; auto. apply valid_valid.
- specialize IHΓ1 with Γ2.
simpl in *.
destruct (merge' Γ1 Γ2) as [ | Γ] eqn:H;
[absurd (is_valid Invalid); auto; apply not_valid | ].
simpl in *. rewrite IHΓ1; auto. apply valid_valid.
- specialize IHΓ1 with Γ2.
simpl in *.
destruct (merge' Γ1 Γ2) as [ | Γ] eqn:H;
[absurd (is_valid Invalid); auto; apply not_valid | ].
simpl in *. rewrite IHΓ1; auto. apply valid_valid.
Defined.
(**************************)
(* Basic validity Tactics *)
(**************************)
Ltac simplify_invalid := repeat rewrite merge_I_l in *;
repeat rewrite merge_I_r in *.
Ltac invalid_contradiction :=
(* don't want any of the proofs to be used in conclusion *)
absurd False; [ inversion 1 | ];
repeat match goal with
| [ H : ?Γ == ?Γ1 ∙ ?Γ2 |- _ ] => destruct H
end;
subst; simplify_invalid;
match goal with
| [ H : is_valid Invalid |- _ ] => apply (False_rect _ (not_valid H))
| [ H : Valid _ = Invalid |- _ ] => inversion H
end.
(**********************************)
(* Inductive predicate for proofs *)
(**********************************)
Inductive merge_o {A : Set} : option A -> option A -> option A -> Set :=
| merge_None : merge_o None None None
| merge_Some_l : forall w, merge_o (Some w) None (Some w)
| merge_Some_r : forall w, merge_o None (Some w) (Some w).
Inductive merge_ind : OCtx -> OCtx -> OCtx -> Set :=
| m_nil_l : forall Γ, merge_ind ∅ (Valid Γ) (Valid Γ)
| m_nil_r : forall Γ, merge_ind (Valid Γ) ∅ (Valid Γ)
| m_cons : forall o1 o2 o Γ1 Γ2 Γ,
merge_o o1 o2 o ->
merge_ind (Valid Γ1) (Valid Γ2) (Valid Γ) ->
merge_ind (Valid (o1 :: Γ1)) (Valid (o2 :: Γ2)) (Valid (o :: Γ)).
Lemma merge_o_ind_fun : forall o1 o2 o,
merge_o o1 o2 o -> merge_wire o1 o2 = Valid [o].
Proof. induction 1; auto. Qed.
Lemma merge_ind_fun : forall Γ1 Γ2 Γ,
merge_ind Γ1 Γ2 Γ ->
Γ == Γ1 ∙ Γ2.
Proof.
induction 1.
* split; [apply valid_valid | auto ].
* split; [apply valid_valid | rewrite merge_nil_r; auto ].
* destruct IHmerge_ind.
split; [apply valid_valid | ].
simpl. erewrite merge_o_ind_fun; [ | eauto].
unfold merge in pf_merge0.
rewrite <- pf_merge0.
auto.
Qed.
Lemma merge_o_fun_ind : forall o1 o2 o,
merge_wire o1 o2 = Valid [o] -> merge_o o1 o2 o.
Proof.
intros [w1 | ] [w2 | ] [w | ]; simpl; inversion 1; constructor.
Qed.
Lemma merge_fun_ind : forall Γ1 Γ2 Γ,
Γ == Γ1 ∙ Γ2 ->
merge_ind Γ1 Γ2 Γ.
Proof.
intros [ | Γ1] [ | Γ2] [ | Γ]; intros; try invalid_contradiction.
generalize dependent Γ.
generalize dependent Γ2.
induction Γ1 as [ | o1 Γ1]; intros Γ2 Γ [pf_valid pf_merge].
* simpl in pf_merge. rewrite pf_merge. constructor.
* destruct Γ2 as [ | o2 Γ2].
+ rewrite merge_nil_r in pf_merge.
rewrite pf_merge.
constructor.
+ destruct o1 as [w1 | ], o2 as [w2 | ].
- simpl in *. invalid_contradiction.
- simpl in pf_merge.
destruct (merge' Γ1 Γ2) as [ | Γ'] eqn:H_Γ'; [invalid_contradiction | ].
rewrite pf_merge.
constructor; [apply merge_o_fun_ind; auto | ].
apply IHΓ1.
split; [apply valid_valid | auto].
- simpl in pf_merge.
destruct (merge' Γ1 Γ2) as [ | Γ'] eqn:H_Γ'; [invalid_contradiction | ].
rewrite pf_merge.
constructor; [apply merge_o_fun_ind; auto | ].
apply IHΓ1.
split; [apply valid_valid | auto].
- simpl in pf_merge.
destruct (merge' Γ1 Γ2) as [ | Γ'] eqn:H_Γ'; [invalid_contradiction | ].
rewrite pf_merge.
constructor; [apply merge_o_fun_ind; auto | ].
apply IHΓ1.
split; [apply valid_valid | auto].
Qed.
Lemma merge_intersection : forall Γ1 Γ2 Γ3 Γ4,
is_valid (Γ1 ⋓ Γ2) ->
(Γ1 ⋓ Γ2) = (Γ3 ⋓ Γ4) ->
{ Γ13 : OCtx & { Γ14 : OCtx & { Γ23 : OCtx & { Γ24 : OCtx &
Γ1 == Γ13 ∙ Γ14 /\ Γ2 == Γ23 ∙ Γ24 /\ Γ3 == Γ13 ∙ Γ23 /\ Γ4 == Γ14 ∙ Γ24}}}}.
Proof.
intros Γ1 Γ2 Γ3 Γ4 V M.
assert (H : (Γ1 ⋓ Γ2) == Γ3 ∙ Γ4). constructor; assumption.
clear M V.
apply merge_fun_ind in H.
remember (Γ1 ⋓ Γ2) as Γ.
generalize dependent Γ2.
generalize dependent Γ1.
induction H.
- intros Γ1 Γ2.
exists ∅, Γ1, ∅, Γ2.
repeat split; try apply valid_valid.
destruct Γ1; [invalid_contradiction|apply valid_valid].
rewrite merge_nil_l; reflexivity.
destruct Γ2; [invalid_contradiction| apply valid_valid].
rewrite merge_nil_l; reflexivity.
assumption.
- intros Γ1 Γ2.
exists Γ1, ∅, Γ2, ∅.
repeat split; try apply valid_valid.
destruct Γ1; [invalid_contradiction|apply valid_valid].
rewrite merge_nil_r; reflexivity.
destruct Γ2; [invalid_contradiction| apply valid_valid].
rewrite merge_nil_r; reflexivity.
assumption.
- rename Γ1 into Γ3. rename Γ2 into Γ4. rename o1 into o3. rename o2 into o4.
intros Γ1 Γ2 M.
destruct Γ1 as [|Γ1]. invalid_contradiction.
destruct Γ2 as [|Γ2]. invalid_contradiction.
destruct Γ1 as [|o1 Γ1], Γ2 as [|o2 Γ2].
+ inversion M.
+ rewrite merge_nil_l in M. inversion M. subst.
exists ∅, ∅, (Valid (o3 :: Γ3)), (Valid (o4 :: Γ4)).
repeat split; try apply valid_valid.
apply merge_ind_fun.
constructor; assumption.
+ rewrite merge_nil_r in M. inversion M. subst.
exists (Valid (o3 :: Γ3)), (Valid (o4 :: Γ4)), ∅, ∅.
repeat split; try apply valid_valid.
apply merge_ind_fun.
constructor; assumption.
+ assert (M12 : (Valid (o :: Γ) == Valid (o1 :: Γ1) ∙ Valid (o2 :: Γ2))).
constructor. apply valid_valid. assumption.
clear M.
apply merge_fun_ind in M12.
inversion M12. subst. clear M12.
destruct (IHmerge_ind (Valid Γ1) (Valid Γ2)) as [Γ13 [Γ14 [Γ23 [Γ24 pf]]]].
apply merge_ind_fun in H7 as [V M]. assumption.
destruct pf as [pf1 [pf2 [pf3 pf4]]].
destruct Γ13 as [|Γ13]. invalid_contradiction.
destruct Γ14 as [|Γ14]. invalid_contradiction.
destruct Γ23 as [|Γ23]. invalid_contradiction.
destruct Γ24 as [|Γ24]. invalid_contradiction.
destruct pf1 as [_ M1], pf2 as [_ M2], pf3 as [_ M3], pf4 as [_ M4].
simpl in *.
inversion m; subst; inversion H3; subst.
* exists (Valid (None :: Γ13)), (Valid (None :: Γ14)),
(Valid (None :: Γ23)), (Valid (None :: Γ24)).
repeat split; try apply valid_valid; simpl.
rewrite <- M1; reflexivity.
rewrite <- M2; reflexivity.
rewrite <- M3; reflexivity.
rewrite <- M4; reflexivity.
* exists (Valid (Some w :: Γ13)), (Valid (None :: Γ14)),
(Valid (None :: Γ23)), (Valid (None :: Γ24)).
repeat split; try apply valid_valid; simpl.
rewrite <- M1; reflexivity.
rewrite <- M2; reflexivity.
rewrite <- M3; reflexivity.
rewrite <- M4; reflexivity.
* exists (Valid (None :: Γ13)), (Valid (None :: Γ14)),
(Valid (Some w :: Γ23)), (Valid (None :: Γ24)).
repeat split; try apply valid_valid; simpl.
rewrite <- M1; reflexivity.
rewrite <- M2; reflexivity.
rewrite <- M3; reflexivity.
rewrite <- M4; reflexivity.
* exists (Valid (None :: Γ13)), (Valid (Some w :: Γ14)),
(Valid (None :: Γ23)), (Valid (None :: Γ24)).
repeat split; try apply valid_valid; simpl.
rewrite <- M1; reflexivity.
rewrite <- M2; reflexivity.
rewrite <- M3; reflexivity.
rewrite <- M4; reflexivity.
* exists (Valid (None :: Γ13)), (Valid (None :: Γ14)),
(Valid (None :: Γ23)), (Valid (Some w :: Γ24)).
repeat split; try apply valid_valid; simpl.
rewrite <- M1; reflexivity.
rewrite <- M2; reflexivity.
rewrite <- M3; reflexivity.
rewrite <- M4; reflexivity.
Qed.
(*** Disjointness ***)
(* I don't think we use these. If not we should get rid of them *)
Definition Disjoint Γ1 Γ2 : Prop :=
match Γ1, Γ2 with
| Invalid, _ => True
| _, Invalid => True
| Valid _, Valid _ => is_valid (Γ1 ⋓ Γ2)
end.
Lemma disjoint_nil_r : forall Γ, Disjoint Γ ∅.
Proof.
destruct Γ as [ | Γ]; [exact I | ].
unfold Disjoint. rewrite merge_nil_r. exists Γ. reflexivity.
Defined.
Lemma disjoint_valid : forall Γ1 Γ2, Disjoint Γ1 Γ2 -> is_valid Γ1 -> is_valid Γ2 -> is_valid (Γ1 ⋓ Γ2).
Proof.
intros Γ1 Γ2 disj [Γ1' valid1] [Γ2' valid2].
rewrite valid1 in *; rewrite valid2 in *; auto.
Defined.
Lemma disjoint_merge : forall Γ Γ1 Γ2,
Disjoint Γ Γ1 -> Disjoint Γ Γ2 -> Disjoint Γ (Γ1 ⋓ Γ2).
Proof.
intros Γ Γ1 Γ2 disj1 disj2.
remember (Γ1 ⋓ Γ2) as Γ'.
destruct Γ as [ | Γ]; [exact I | ].
destruct Γ' as [ | Γ']; [exact I | ].
assert (valid0 : is_valid Γ). { apply valid_valid. }
assert (valid1 : is_valid Γ1).
{ destruct Γ1 as [ | Γ1]; [inversion HeqΓ' | ]. apply valid_valid. }
assert (valid2 : is_valid Γ2).
{ destruct Γ2 as [ | Γ2]; [rewrite merge_I_r in *; inversion HeqΓ' | ]. apply valid_valid. }
assert (valid1' : is_valid (Γ ⋓ Γ1)). { apply disjoint_valid; auto. }
assert (valid2' : is_valid (Γ ⋓ Γ2)). { apply disjoint_valid; auto. }
unfold Disjoint.
rewrite HeqΓ'.
rewrite merge_assoc.
apply valid_join; auto.
exists Γ'; auto.
Defined.
Lemma disjoint_split : forall Γ1 Γ2 Γ, is_valid Γ1 -> is_valid Γ2 ->
Disjoint Γ1 Γ2 -> Disjoint (Γ1 ⋓ Γ2) Γ
-> Disjoint Γ1 Γ /\ Disjoint Γ2 Γ.
Proof.
intros Γ1 Γ2 Γ [Γ1' valid1] [Γ2' valid2] disj disj'.
subst. unfold Disjoint in disj.
destruct Γ as [ | Γ]; [split; exact I | ].
unfold Disjoint.
destruct disj as [Γ' is_valid].
rewrite is_valid in disj'.
unfold Disjoint in disj'.
rewrite <- is_valid in disj'.
apply valid_split in disj'.
destruct disj' as [H1 [H2 H3]]; split; auto.
Defined.
(**************************)
(* Extra helper functions *)
(**************************)
Definition xor_option {a} (o1 : option a) (o2 : option a) : option a :=
match o1, o2 with
| Some a1, None => Some a1
| None, Some a2 => Some a2
| _ , _ => None
end.
(* index into an OCtx *)
(* NOTE: May need to tell Coq to unfold. *)
Definition index (Γ : OCtx) (i : nat) : option WType :=
match Γ with
| Invalid => None
| Valid Γ' => maybe (Γ' !! i) None
end.
Lemma nth_nil : forall {A} x, ([] : list A) !! x = None.
Proof.
destruct x; auto.
Qed.
Lemma index_invalid : forall i, index Invalid i = None.
Proof.
auto.
Qed.
Lemma index_empty : forall i, index ∅ i = None.
Proof.
intros.
simpl.
rewrite nth_nil.
auto.
Qed.
Lemma singleton_index : forall x w Γ,
SingletonCtx x w Γ ->
index Γ x = Some w.
Proof.
induction 1; simpl; auto.
Qed.
(********************)
(* Empty Contexts *)
(********************)
Inductive empty_ctx : Ctx -> Prop :=
| empty_nil : empty_ctx []
| empty_cons : forall Γ, empty_ctx Γ -> empty_ctx (None :: Γ)
.
Lemma empty_ctx_size : forall Γ, empty_ctx Γ -> size_ctx Γ = 0%nat.
Proof. induction 1; auto. Qed.
Lemma eq_dec_empty_ctx : forall Γ, {empty_ctx Γ} + {~empty_ctx Γ}.
Proof.
intros.
induction Γ.
- left; constructor.
- destruct a.
+ right. easy.
+ destruct IHΓ.
* left; constructor; easy.
* right. intros F. apply n. inversion F. easy.
Qed.
Lemma merge_empty : forall (Γ Γ1 Γ2 : Ctx),
Γ == Γ1 ∙ Γ2 ->
empty_ctx Γ ->
empty_ctx Γ1 /\ empty_ctx Γ2.
Proof.
intros Γ Γ1 Γ2 M E.
apply merge_fun_ind in M.
dependent induction M.
- split; trivial; constructor.
- split; trivial; constructor.
- inversion E; subst.
inversion m; subst.
specialize (IHM Γ0 Γ3 Γ4 eq_refl eq_refl eq_refl H0) as [EΓ3 EΓ4].
split; constructor; easy.
Qed.
(********************)
(* Trimmed Contexts *)
(********************)
(* Removes Nones from end *)
Fixpoint trim (Γ : Ctx) : Ctx :=
match Γ with
| [] => []
| None :: Γ' => match trim Γ' with
| [] => []
| Γ'' => None :: Γ''
end
| Some w :: Γ' => Some w :: trim Γ'
end.
Definition otrim (Γ : OCtx) :=
match Γ with
| Invalid => Invalid
| Valid Γ => Valid (trim Γ)
end.
Lemma trim_otrim : forall (Γ : Ctx), Valid (trim Γ) = otrim Γ.
Proof. easy. Qed.
Lemma size_ctx_trim : forall Γ, size_ctx (trim Γ) = size_ctx Γ.
Proof.
induction Γ; auto.
destruct a; simpl.
rewrite IHΓ; easy.
simpl.
destruct (trim Γ); easy.
Qed.
Lemma size_octx_trim : forall Γ, size_octx (trim Γ) = size_octx Γ.
Proof. apply size_ctx_trim. Qed.
Lemma index_trim : forall Γ i,
index (trim Γ) i = index Γ i.
Proof.
induction Γ as [ | [w | ] Γ]; intros i.
* simpl. auto.
* simpl. destruct i; simpl; auto.
apply IHΓ.
* simpl. remember (trim Γ) as Γ'.
destruct Γ' as [ | o Γ']; auto.
+ rewrite nth_nil.
destruct i; simpl; auto.
simpl in IHΓ.
rewrite <- IHΓ.