-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_binding.v
More file actions
3795 lines (3467 loc) · 156 KB
/
lambda_binding.v
File metadata and controls
3795 lines (3467 loc) · 156 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Coq.Lists.List.
Require Import FV.utils.
Require Import Coq.Sorting.Permutation.
Require Import Bool.
Require Import Compare_dec.
Require Import Relation_Definitions.
Require Import Relation_Operators.
Require Import RelationClasses.
Require Import Operators_Properties.
Require Import Setoid.
Require Import Morphisms.
Require Import Coq.micromega.Lia.
Import ListNotations.
Module Type Naming_module_type.
Parameter C : Type. (** constant **)
Parameter V: Type. (** variable, may have many types **)
Parameter VS: Type.
Parameter varsort: V -> VS. (** A function that returns the variable's type **)
Parameter newvar: list V -> VS -> V. (** Given a list of vars, return a var that is not in the list with given type **)
Axiom var_eq_dec:forall v1 v2: V, {v1 = v2} + {v1 <> v2}.
Axiom constant_eq_dec: forall v1 v2: C, {v1 = v2} + {v1 <> v2}.
Axiom newvar_fresh: forall (xs:list V) (v:VS), ~ In (newvar xs v) xs.
Axiom newvar_Permutation: forall (l1 l2:list V)(v:VS), Permutation l1 l2 -> newvar l1 v = newvar l2 v.
Axiom newvar_sort: forall xs T, varsort (newvar xs T) = T.
End Naming_module_type.
Module General_lambda(Name:Naming_module_type).
Export Name.
Corollary in_vars_dec: forall (x:V) xs, {In x xs} + {~In x xs}.
Proof. intros. apply (in_dec var_eq_dec). Qed.
(* ***************************************************************** *)
(* *)
(* *)
(* Basic Definition *)
(* *)
(* *)
(* ***************************************************************** *)
Inductive tm: Type:=
| cons (c:C)
| var (s:V)
| app: tm -> tm -> tm
| abs: V -> tm -> tm.
Coercion var : V >-> tm.
Coercion cons: C >-> tm.
Notation " 'λ' x . t" := (abs x t) (at level 22, no associativity, only printing).
Notation " M N":=(app M N)(at level 20, left associativity, only printing).
Fixpoint rank (t:tm):=
match t with
| cons c => 1
| var s => 1
| app t1 t2 => rank t1 + rank t2
| abs s t0 => S (rank t0)
end.
Lemma rank_positive: forall t,
0<rank t.
Proof.
induction t; simpl; try constructor.
+ apply PeanoNat.Nat.add_pos_r; tauto.
+ apply Lt.lt_le_S; tauto.
Qed.
Lemma lt_app_rank_l: forall t1 t2,
rank t1 < rank (app t1 t2).
Proof.
intros. simpl. apply PeanoNat.Nat.lt_add_pos_r. apply rank_positive.
Qed.
Lemma lt_app_rank_r: forall t1 t2,
rank t2 < rank (app t1 t2).
Proof.
intros. simpl. apply PeanoNat.Nat.lt_add_pos_l. apply rank_positive.
Qed.
Theorem rank_induction: forall P:tm -> Prop,
(forall t, (forall x, rank x < rank t -> P x) -> P t) ->
forall t, P t.
Proof.
intros.
assert (forall t n, rank t < n -> P t) as H_ind.
{
intros. generalize dependent t0. induction n; intros.
+ inversion H0.
+ apply H. intros. apply IHn. apply Lt.lt_n_Sm_le in H0.
inversion H0;subst. tauto.
apply PeanoNat.Nat.lt_le_trans with (rank t0);tauto.
}
intros. apply H_ind with (S (rank t)). constructor.
Qed.
Definition var_occur (x:V)(s:V):nat:=
if var_eq_dec x s then (S O) else O.
Fixpoint free_occur (x:V)(t:tm):nat:=
match t with
| var s => var_occur x s
| app t1 t2 => free_occur x t1 + (free_occur x t2)
| abs s t0 => if var_eq_dec x s then O else free_occur x t0
| cons c => O
end.
(* ***************************************************************** *)
(* *)
(* *)
(* Substitution *)
(* *)
(* *)
(* ***************************************************************** *)
Definition subst_task: Type := list (V*tm).
Definition domain (st:subst_task): list V:=
map fst st.
Definition range (st:subst_task): list tm:=
map snd st.
Definition distinct (st:subst_task):=
NoDup (domain st).
Fixpoint st_occur (x:V)(st:subst_task):nat:=
match st with
| nil=> O
| (s,t)::st0 => var_occur x s + free_occur x t + st_occur x st0
end.
Fixpoint st_tm_occur (x:V)(st:subst_task):nat:=
match st with
| nil=> O
| (s,t)::st0 => free_occur x t + st_tm_occur x st0
end.
Fixpoint var_sub (x:V)(st:subst_task):tm:=
match st with
| nil => x
| (x',t')::st' => if var_eq_dec x x' then t'
else var_sub x st'
end.
Fixpoint tm_var (t:tm):list V:=
match t with
| var s => [s]
| app t1 t2 => tm_var t1++tm_var t2
| abs s t0 => s::(tm_var t0)
| cons c => nil
end.
Definition task_var (st: subst_task):=
flat_map (fun p => fst p :: tm_var (snd p)) st.
Definition reduce (x:V)(st:subst_task):=
filter (fun p=> if var_eq_dec x (fst p) then false else true) st.
Fixpoint list_reduce (xs:list V)(st:subst_task):subst_task:=
match xs with
| nil => st
| x::xs0 => reduce x (list_reduce xs0 st)
end.
Fixpoint subst (st:subst_task) (t:tm):tm:=
match t with
| var s => var_sub s st
| cons c => cons c
| app t1 t2 => app (subst st t1) (subst st t2)
| abs x t1 => let st':= reduce x st in
match st_tm_occur x st' with
| O => abs x (subst st' t1)
| _ => let x':= newvar (tm_var t++task_var st') (varsort x) in (** Brand new var **)
abs x' (subst ((x,var x')::st') t1)
end
end.
(**** Basic Lemmas About String and Substitution****)
Lemma var_occur_eq_dec: forall x y,
var_occur x y = 1 <-> x = y.
Proof.
split; intros ;[unfold var_occur in H|unfold var_occur]; destruct(var_eq_dec x y); congruence.
Qed.
Lemma var_occur_not_eq_dec: forall x y,
var_occur x y = 0 <-> x <> y.
Proof.
split; intros ;[unfold var_occur in H|unfold var_occur]; destruct(var_eq_dec x y); congruence.
Qed.
Lemma var_occur_eq: forall x y,
nat_reflect (x=y) (var_occur x y).
Proof.
intros. unfold var_occur. destruct (var_eq_dec x y);constructor; tauto.
Qed.
(** Discuss name **)
Ltac destruct_eq_dec:=
simpl; repeat ( simpl st_tm_occur; simpl free_occur; match goal with
| |- ?t = ?t => reflexivity
| H: ?t = ?t |- _ => clear H
| H: context [var_occur _ _] |- _ => unfold var_occur in H
| |- context [var_occur _ _] => unfold var_occur
| |- context [if (var_eq_dec ?x ?x) then _ else _ ] => destruct (var_eq_dec x x);[|congruence]
| H: ?x = ?y |- context [if (var_eq_dec ?x ?y) then _ else _ ] => destruct (var_eq_dec x y);[|congruence]
| H: ?x = ?y |- context [if (var_eq_dec ?x ?y) then _ else _ ] => destruct (var_eq_dec x y);[|congruence]
| H: ?x <> ?y |- context [if (var_eq_dec ?x ?y) then _ else _ ] => destruct (var_eq_dec x y);[congruence|]
| H: ?y <> ?x |- context [if (var_eq_dec ?x ?y) then _ else _ ] => destruct (var_eq_dec x y);[congruence|]
| |- context [if (var_eq_dec _ _) then _ else _] => destruct (var_eq_dec _ _);simpl;[subst|]
| |- context [var_occur ?x ?y] => destruct (var_occur_eq x y);[subst|]
| H: context [if (var_eq_dec ?x ?y) then _ else _] |- _ => destruct (var_eq_dec x y) in H; subst; try congruence
| H: ?x = 0 |- _ => rewrite H
| |- _ => try tauto
end).
Lemma var_sub_range: forall s st,
In (var_sub s st) (range st) \/ var_sub s st = s.
Proof. induction st. now right. destruct a. destruct_eq_dec. Qed.
Lemma no_st_occur_zero_no_st_tm_occur: forall x st,
st_occur x st = 0 -> st_tm_occur x st = 0.
Proof.
intros. induction st ; try easy.
destruct a as [s t]. simpl in *. zero_r H.
Qed.
Lemma reduce_no_st_tm_occur: forall x y st,
st_tm_occur x st = 0 -> st_tm_occur x (reduce y st) = 0.
Proof.
intros. induction st. reflexivity.
destruct a. simpl in H. zero_r H.
apply IHst in H0. destruct_eq_dec.
Qed.
Lemma reduce_app: forall x st st',
reduce x (st++st') = reduce x st ++ reduce x st'.
Proof. intros. apply filter_app. Qed.
Lemma list_reduce_cons: forall x l st,
reduce x (list_reduce l st) = list_reduce (x::l) st.
Proof. intros. easy. Qed.
Lemma task_var_app: forall st1 st2,
task_var (st1++st2) = task_var st1 ++ task_var st2.
Proof. intros. apply flat_map_app. Qed.
Lemma st_tm_occur_app: forall x st1 st2,
st_tm_occur x (st1++st2) = st_tm_occur x st1 + st_tm_occur x st2.
Proof.
intros. induction st1.
+ easy.
+ simpl. rewrite IHst1. destruct a. now rewrite <- Plus.plus_assoc_reverse.
Qed.
Lemma domain_app: forall l1 l2,
domain(l1++l2) = domain l1 ++ domain l2.
Proof. intros. apply map_app. Qed.
Lemma tm_var_eq: forall M N,
(var M) = (var N) ->
M = N.
Proof. intros. congruence. Qed.
Lemma Permutation_keep_distinct: forall l1 l2,
Permutation l1 l2 ->
distinct l1 ->
distinct l2.
Proof.
unfold distinct. intros.
assert (Permutation (domain l1) (domain l2)).
unfold domain. perm. eapply Permutation_NoDup;eauto.
Qed.
Lemma reduce_Permutation: forall st1 st2 x,
Permutation st1 st2 ->
Permutation (reduce x st1) (reduce x st2).
Proof.
intros. induction H.
+ constructor.
+ destruct x0 as [x0 t0]. destruct_eq_dec. perm.
+ destruct x0 as [x0 t0]. destruct y as [x1 t1]. simpl. destruct_eq_dec; perm.
+ perm.
Qed.
Lemma list_reduce_Permutation: forall st1 st2 xs,
Permutation st1 st2 ->
Permutation (list_reduce xs st1) (list_reduce xs st2).
Proof. intros. induction xs. easy. now apply reduce_Permutation. Qed.
Lemma st_tm_occur_Permutation: forall st1 st2 x,
Permutation st1 st2 ->
st_tm_occur x st1 = st_tm_occur x st2.
Proof.
intros. induction H.
+ reflexivity.
+ simpl. rewrite IHPermutation. reflexivity.
+ destruct x0 as [x0 t0]. destruct y as [x1 t1].
simpl. now rewrite PeanoNat.Nat.add_shuffle3.
+ congruence.
Qed.
Lemma task_var_Permutation: forall st1 st2,
Permutation st1 st2 ->
Permutation (task_var st1) (task_var st2).
Proof.
intros. induction H.
+ constructor.
+ destruct x as [x t]. simpl. perm.
+ destruct x. destruct y. simpl. perm.
+ perm.
Qed.
Lemma not_in_tm_var_no_free_occur: forall x t,
~ In x (tm_var t) -> free_occur x t = 0.
Proof.
induction t; intros; simpl in H.
+ reflexivity.
+ destruct_eq_dec.
+ des_notin H. simpl. rewrite IHt1; tauto.
+ des_notin H. destruct_eq_dec.
Qed.
Lemma not_in_task_var_no_st_occur: forall x st,
~ In x (task_var st) -> st_occur x st =0.
Proof.
induction st; intros. easy.
destruct a. des_notin H.
apply not_in_tm_var_no_free_occur in H0.
simpl. rewrite H0. destruct_eq_dec.
Qed.
Lemma no_reduce: forall x st,
~ In x (domain st) ->
reduce x st = st.
Proof. intros. induction st; try easy. destruct a. des_notin H. destruct_eq_dec. f_equal. tauto. Qed.
Lemma not_in_domain_reduce_other : forall x x0 st,
x<>x0 ->
~ In x0 (domain st) ->
~ In x0 (domain (reduce x st)).
Proof. intros. induction st. easy. destruct a as [x1 t1]. des_notin H0. destruct_eq_dec. Qed.
Lemma not_in_task_var_not_in_reduced_task_var: forall x y st,
~ In x (task_var st) ->
~ In x (task_var (reduce y st)).
Proof.
intros. induction st.
+ easy.
+ destruct a. des_notin H. destruct_eq_dec.
apply deMorgan2. split;[tauto|]. apply not_in_app. tauto.
Qed.
Lemma reduce_swap: forall x y st,
reduce x (reduce y st) = reduce y (reduce x st).
Proof. induction st. easy. destruct a as [s t]. destruct_eq_dec. now rewrite IHst. Qed.
Lemma reduce_list_reduce_swap: forall x xs st,
reduce x (list_reduce xs st) = list_reduce xs (reduce x st).
Proof.
intros. induction xs.
+ easy.
+ simpl. rewrite <- IHxs. apply reduce_swap.
Qed.
Lemma reduce_distinct: forall st x,
distinct st ->
distinct (reduce x st).
Proof.
unfold distinct.
intros. induction st.
+ constructor.
+ destruct a as [x0 t0]. simpl. destruct_eq_dec.
- apply IHst. simpl in H. now rewrite NoDup_cons_iff in H.
- simpl in H. apply NoDup_cons.
* rewrite NoDup_cons_iff in H. destruct H.
apply not_in_domain_reduce_other;tauto.
* apply IHst. rewrite NoDup_cons_iff in H. tauto.
Qed.
(* Lemma NoDup_app: forall (l1 l2:list Str),
NoDup l1 ->
NoDup l2 ->
(forall x , ~ In x l1 \/ ~ In x l2) ->
NoDup (l1++l2).
Proof.
intros. induction l1. tauto.
assert (H2:=H1). specialize H2 with a. destruct H2.
simpl in H2. apply deMorgan1 in H2. destruct H2. congruence.
simpl. rewrite NoDup_cons_iff. split.
+ rewrite NoDup_cons_iff in H. destruct H. apply not_in_app. tauto.
+ apply IHl1.
- rewrite NoDup_cons_iff in H. tauto.
- intro. specialize H1 with x. destruct H1;try tauto.
left. rewrite not_in_cons in H1. tauto.
Qed. *)
Lemma not_in_Forall: forall (x:V) l,
~ In x l <-> Forall (fun s => var_occur x s = 0) l.
Proof.
split; intros.
+ rewrite Forall_forall. intros. destruct_eq_dec.
+ rewrite Forall_forall in H. induction l. easy. apply not_in_cons. split.
- specialize H with a. destruct_eq_dec.
cut (1=0).
* intros. discriminate.
* apply H. now left.
- apply IHl. intros. apply H. now right.
Qed.
Lemma not_in_domain_reduce_self: forall x st,
~ In x (domain (reduce x st)).
Proof. induction st. easy. destruct a. destruct_eq_dec. now apply not_in_cons. Qed.
Lemma not_in_domain_list_reduce_containing_self:forall x l st,
In x l ->
~ In x (domain (list_reduce l st)).
Proof.
intros. induction l.
+ inversion H.
+ simpl. destruct (var_eq_dec x a).
- subst a. apply not_in_domain_reduce_self.
- destruct H;try congruence. apply not_in_domain_reduce_other;[congruence|tauto].
Qed.
Lemma in_reduce_domain: forall (x y:V) st,
In x (domain (reduce y st)) ->
In x (domain st) /\ x<>y.
Proof.
intros. destruct (var_eq_dec x y).
+ subst x. pose proof not_in_domain_reduce_self y st. contradiction.
+ split;[|exact n]. induction st.
- inversion H.
- destruct a. simpl in *. destruct_eq_dec. simpl in H. tauto.
Qed.
Lemma reduce_and_add_same_var_keep_distinct: forall x t st,
distinct st ->
distinct ((x,t)::(reduce x st)).
Proof.
unfold distinct. intros. simpl.
induction st.
+ simpl. constructor;[tauto|constructor].
+ destruct a. simpl. destruct_eq_dec; simpl in H.
- rewrite NoDup_cons_iff in H. tauto.
- simpl. rewrite NoDup_cons_iff in H. destruct H.
apply IHst in H0. rewrite NoDup_cons_iff in H0.
apply NoDup_cons. rewrite not_in_cons. tauto.
apply NoDup_cons. 2:tauto.
clear H0 IHst. induction st.
easy. destruct a. des_notin H. destruct_eq_dec.
Qed.
Lemma distinct_cons_iff: forall x t st,
distinct ((x,t)::st) <->
~ In x (domain st) /\ distinct st.
Proof. unfold distinct. intros. simpl. now rewrite NoDup_cons_iff. Qed.
(** Tactic that deals with occurence **)
Ltac solve_notin:=
try match goal with
| |- free_occur _ _ = 0 => apply not_in_tm_var_no_free_occur
| |- st_tm_occur _ _ = 0 => apply no_st_occur_zero_no_st_tm_occur; apply not_in_task_var_no_st_occur
| |- st_occur _ _ = 0 => apply not_in_task_var_no_st_occur
end;
try match goal with
| H:?z = newvar ?a ?b |- ~ In ?z _ => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H';
rewrite H; specialize H' with a b
| H:?z = newvar ?a ?b |- ?z <> _ => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H';
rewrite H; specialize H' with a b
| H:?z = newvar ?a ?b |- _ <> ?z => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H';
rewrite H; specialize H' with a b
| |- ~ In (newvar ?a ?b) _ => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H'; specialize H' with a b
| |- _ <> newvar ?a ?b => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H'; specialize H' with a b
| |- newvar ?a ?b <> _ => let H' := fresh "newvar_fresh" in pose proof newvar_fresh as H'; specialize H' with a b
end;
(* try repeat match goal with *)
try repeat ( simpl in *; match goal with
| H: ~ In _ (_::_) |- _ => des_notin H
| H: ~ In _ (_++_) |- _ => des_notin H
| H: ~ (_ \/ _) |- _ => des_notin H
| H: context[task_var (_ ++ _) ] |- _ => rewrite task_var_app in H
| H: context[task_var [(?x,?t)]] |- _ => simpl task_var in H
| H: context [reduce _ (_++_)] |- _ => rewrite reduce_app in H
| H: ?x <> ?x |- _ => congruence
end);
try repeat match goal with
| H: context[Name.newvar] |- _ => fold newvar in H
end;
try repeat match goal with
| |- context[task_var (_++_)] => rewrite task_var_app
| |- context[task_var [(?x,?t)] ] => unfold task_var
| |- context [reduce _ (_++_)] => rewrite reduce_app
| |- context [list_reduce (?x::_) _] => rewrite <- list_reduce_cons
| |- ~ (_\/_) => apply deMorgan2; split
| |- ~ In _ (task_var ((?x,?t)::?l))=> apply deMorgan2; split
| |- ~ In _ (_::_) => apply not_in_cons; split
| |- ~ In _ (_++_) => apply not_in_app; split
| |- ~ In _ _ => tauto
| _ => try congruence; tauto
end.
Lemma str_not_in_domain_then_var_sub_is_self: forall s st,
~ In s (domain st) ->
var_sub s st = s.
Proof. intros. induction st. easy. destruct a. des_notin H. destruct_eq_dec. Qed.
Corollary var_sub_with_task_reduce_self: forall s st,
var_sub s (reduce s st) = s.
Proof. intros. apply str_not_in_domain_then_var_sub_is_self. apply not_in_domain_reduce_self. Qed.
Lemma var_sub_with_task_reduce_other: forall s x st,
s <> x ->
var_sub s st = var_sub s (reduce x st).
Proof. intros. induction st. easy. destruct a. repeat destruct_eq_dec. Qed.
Lemma var_sub_with_task_list_reduce_not_containing_self: forall xs st (x:V),
~ In x xs ->
subst st x = subst (list_reduce xs st) x.
Proof.
intros. induction xs.
+ reflexivity.
+ simpl. des_notin H. apply IHxs in H0. clear IHxs. simpl in H0.
pose proof var_sub_with_task_reduce_other x a (list_reduce xs st).
rewrite <- H1. tauto. congruence.
Qed.
Lemma list_reduce_cons_with_var_in_reduced_list: forall x t xs st,
In x xs ->
list_reduce xs ((x,t)::st) = list_reduce xs st.
Proof.
intros. induction xs as [|s xs ?].
+ inversion H.
+ destruct H. subst x. simpl. rewrite reduce_list_reduce_swap.
destruct_eq_dec. now rewrite reduce_list_reduce_swap.
simpl. f_equal. tauto.
Qed.
Lemma list_reduce_cons_with_var_not_in_reduced_list: forall x t xs st,
~ In x xs ->
list_reduce xs ((x,t)::st) = (x,t)::list_reduce xs st.
Proof.
induction xs; intros.
+ easy.
+ des_notin H. simpl. rewrite IHxs; try easy. destruct_eq_dec.
Qed.
Theorem subst_exchangebale: forall st1 st2 t,
distinct st1 ->
Permutation st1 st2 ->
subst st1 t = subst st2 t.
Proof.
intros. generalize dependent st2.
generalize dependent st1.
induction t; intros.
+ easy.
+ induction H0;subst.
- easy.
- destruct x. destruct_eq_dec. apply distinct_cons_iff in H. tauto.
- destruct x. destruct y. destruct_eq_dec. unfold distinct in H.
simpl in H. inversion H; subst. des_notin H2. congruence.
- assert (H0:=H). apply IHPermutation1 in H0. clear IHPermutation1.
assert (distinct l'). apply Permutation_keep_distinct with l;tauto.
apply IHPermutation2 in H1. congruence.
+ simpl. erewrite IHt1;eauto. erewrite IHt2; eauto.
+ simpl. assert (st_tm_occur v (reduce v st1) = st_tm_occur v (reduce v st2)).
apply st_tm_occur_Permutation. now apply reduce_Permutation.
rewrite <- H1. destruct (st_tm_occur v (reduce v st1)).
- specialize IHt with (reduce v st1) (reduce v st2).
rewrite IHt; try easy. now apply reduce_distinct. now apply reduce_Permutation.
- specialize IHt with ((v, var (newvar (v :: tm_var t ++ task_var (reduce v st1)) (varsort v))) :: reduce v st1)
((v, var (newvar (v :: tm_var t ++ task_var (reduce v st2)) (varsort v))) :: reduce v st2).
rewrite IHt.
* assert (newvar (v :: tm_var t ++ task_var (reduce v st1)) (varsort v) = newvar (v :: tm_var t ++ task_var (reduce v st2) ) (varsort v)).
apply newvar_Permutation. perm.
apply task_var_Permutation. apply reduce_Permutation. tauto. now rewrite H2.
* apply reduce_and_add_same_var_keep_distinct; tauto.
* assert (newvar (v :: tm_var t ++ task_var (reduce v st1)) (varsort v)= newvar (v :: tm_var t ++ task_var (reduce v st2)) (varsort v)).
apply newvar_Permutation. perm.
apply task_var_Permutation. now apply reduce_Permutation.
rewrite H2. constructor. now apply reduce_Permutation.
Qed.
Lemma subst_nil: forall t, subst [] t = t.
Proof. induction t; simpl; try rewrite IHt; try rewrite IHt1; try rewrite IHt2; congruence. Qed.
Lemma subst_var_with_itself: forall s t, subst [(s,var s)] t = t.
Proof.
induction t; destruct_eq_dec; try congruence.
+ now rewrite subst_nil.
+ simpl. congruence.
Qed.
Lemma var_sub_keep_no_occur: forall x (s:V) st,
s <> x ->
st_tm_occur s st = 0 ->
free_occur s (subst st x) = 0.
Proof. intros. induction st. destruct_eq_dec. destruct a. zero_r H0. destruct_eq_dec. Qed.
Corollary var_sub_keep_no_occur_st_occur: forall x s st,
s<>x ->
st_occur s st = 0 ->
free_occur s (subst st x) =0.
Proof. intros. apply no_st_occur_zero_no_st_tm_occur in H0. now apply var_sub_keep_no_occur. Qed.
Corollary var_sub_keep_no_occur_task_var: forall x s st,
s<>x ->
~ In s (task_var st) ->
free_occur s (subst st x) = 0.
Proof. intros. apply not_in_task_var_no_st_occur in H0. now apply var_sub_keep_no_occur_st_occur. Qed.
Lemma var_sub_with_var_not_in_app_domain_l: forall st1 st2 (x:V),
~ In x (domain st1) ->
subst (st1++st2) x = subst st2 x.
Proof.
intros. induction st1.
+ easy.
+ destruct a. des_notin H. destruct_eq_dec.
Qed.
Lemma var_sub_with_var_not_in_app_domain_r: forall st1 st2 (x:V),
~In x (domain st2) ->
subst (st1++st2) x = subst st1 x.
Proof.
intros. induction st1.
+ simpl. now apply str_not_in_domain_then_var_sub_is_self.
+ destruct a. destruct_eq_dec.
Qed.
Lemma subst_reduce_swap_varong: forall x t l1 l2 xs l M,
subst ( l++ list_reduce xs (reduce x l1 ++ (x,t)::l2)) M = subst ( l ++ list_reduce xs ((x,t)::(reduce x l1)++l2)) M.
Proof.
intros. revert x t l1 l2 xs l. induction M; intros.
+ easy.
+ destruct (in_vars_dec s (domain l)).
- induction l as [|u us IHl].
* simpl in i. tauto.
* destruct u. simpl in i. destruct i; subst; destruct_eq_dec.
- rewrite 2 var_sub_with_var_not_in_app_domain_l; try tauto.
induction l1.
* easy.
* destruct a. simpl.
destruct_eq_dec. destruct (in_vars_dec x xs); destruct (in_vars_dec v xs).
++ repeat rewrite list_reduce_cons_with_var_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_in_reduced_list in IHl1;tauto.
++ rewrite list_reduce_cons_with_var_not_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_not_in_reduced_list; try tauto.
simpl. destruct_eq_dec. rewrite list_reduce_cons_with_var_in_reduced_list in IHl1;tauto.
++ rewrite list_reduce_cons_with_var_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_not_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_not_in_reduced_list in IHl1; try tauto.
++ repeat rewrite list_reduce_cons_with_var_not_in_reduced_list; try tauto.
rewrite list_reduce_cons_with_var_not_in_reduced_list in IHl1; try tauto.
simpl in *. destruct_eq_dec.
+ simpl. rewrite IHM1. now rewrite IHM2.
+ unfold subst.
assert (st_tm_occur v (reduce v (l++ list_reduce xs (reduce x l1 ++ (x, t) :: l2))) =
st_tm_occur v (reduce v (l ++ list_reduce xs ((x, t) :: reduce x l1 ++ l2)))).
{ apply st_tm_occur_Permutation. apply reduce_Permutation. perm.
apply list_reduce_Permutation. symmetry. apply Permutation_middle. }
rewrite <- H. destruct (st_tm_occur v (reduce v (l++ list_reduce xs (reduce x l1 ++ (x, t) :: l2)))); fold subst.
- f_equal. rewrite 2 reduce_app. rewrite 2 list_reduce_cons. apply IHM.
- assert (newvar (tm_var (abs v M) ++ task_var (reduce v (l ++ list_reduce xs (reduce x l1 ++ (x, t) :: l2))))
(varsort v)=newvar (tm_var (abs v M) ++ task_var (reduce v (l ++ list_reduce xs ((x, t) :: reduce x l1 ++ l2))))
(varsort v)).
{ apply newvar_Permutation. perm. apply task_var_Permutation.
apply reduce_Permutation. apply Permutation_app_head. apply list_reduce_Permutation. symmetry. apply Permutation_middle.
}
rewrite H0. remember (newvar
(tm_var (abs v M) ++
task_var (reduce v (l ++ list_reduce xs ((x, t) :: reduce x l1 ++ l2))))
(varsort v)) as z. clear H0. f_equal. rewrite 2 reduce_app. rewrite 2 list_reduce_cons.
specialize IHM with x t l1 l2 (v::xs) ((v, var z) :: reduce v l). tauto.
Qed.
Corollary subst_reduce_swap: forall x t l1 l2 M,
subst (reduce x l1 ++ (x,t)::l2) M = subst ((x,t)::(reduce x l1)++l2) M.
Proof. intros. apply subst_reduce_swap_varong with (xs:=nil) (l:=nil). Qed.
Lemma subst_keep_no_free_occur: forall x st M,
st_tm_occur x st = 0 ->
free_occur x M = 0 ->
free_occur x (subst st M) = 0.
Proof.
intros. generalize dependent st. induction M;intros.
+ reflexivity.
+ simpl in H0. rewrite var_occur_not_eq_dec in H0. now apply var_sub_keep_no_occur.
+ simpl. zero_r H0. rewrite IHM1; try tauto. now rewrite IHM2.
+ simpl. simpl in H0. destruct (st_tm_occur v (reduce v st)) eqn:E.
- destruct_eq_dec. apply IHM. tauto. now apply reduce_no_st_tm_occur.
- destruct_eq_dec.
* apply reduce_no_st_tm_occur with v v st in H.
rewrite H in E. discriminate.
* apply IHM. easy.
simpl. rewrite <- var_occur_not_eq_dec in n1. rewrite n1.
simpl. now apply reduce_no_st_tm_occur.
Qed.
Corollary subst_one_term_keep_no_free_occur: forall x y M N,
free_occur x M = 0 ->
free_occur x N = 0 ->
free_occur x (subst [(y, N)] M) = 0.
Proof. intros. apply subst_keep_no_free_occur. simpl. now rewrite H0. easy. Qed.
(* ***************************************************************** *)
(* *)
(* *)
(* Alpha Equivalence *)
(* *)
(* *)
(* ***************************************************************** *)
Inductive is_var: tm -> Prop :=
| str_is_var: forall s:V, is_var (var s).
(** Match binders at the same location, true means valid, false means it does not matter (not valid)**)
Definition binder_pair:Type:=V*V*bool.
Definition binder_list:Type:=list binder_pair.
Definition binder_update (x:V)(y:V)(bd:binder_pair):binder_pair:=
let '(x0,y0,b):= bd in
if zerop (var_occur x x0 + var_occur y y0) then bd else (x0,y0,false).
Definition update (x:V)(y:V)(st:binder_list):=
map (fun bd => binder_update x y bd) st.
Definition binder_l (st:binder_list):list V:=
map (fun x => fst (fst x)) st.
Definition binder_r (st:binder_list): list V:=
map (fun x => snd (fst x)) st.
(** Parse the two terms from top to bottom, two terms are alpha equivalent if
alpha_eq nil M N **)
Inductive alpha_eq: binder_list -> tm -> tm -> Prop:=
| cons_aeq: forall bd (c:C), alpha_eq bd c c
| str_aeq1: forall bd (s1 s2:V), (** Living binders **)
varsort s1 = varsort s2 ->
In (s1,s2, true) bd ->
alpha_eq bd (var s1) (var s2)
| str_aeq2: forall bd (s:V), (** Not binder, free variable **)
~ In s (binder_l bd) ->
~ In s (binder_r bd) ->
alpha_eq bd s s
| app_aeq: forall bd t1 t2 t3 t4,
alpha_eq bd t1 t3 ->
alpha_eq bd t2 t4 ->
alpha_eq bd (app t1 t2)(app t3 t4)
| abs_aeq: forall x x' bd t1 t2, (** Record a new binder relation, clear invalid ones**)
varsort x = varsort x' ->
alpha_eq ((x, x', true)::(update x x' bd)) t1 t2 ->
alpha_eq bd (abs x t1)(abs x' t2)
.
Definition aeq: relation tm:= fun t1 t2 => alpha_eq nil t1 t2.
Notation "x =a= y" := (aeq x y) (at level 42, no associativity).
Inductive valid_binder: binder_list -> Prop:=
| valid_nil: valid_binder nil
| valid_cons: forall x y l,
varsort x = varsort y ->
valid_binder l ->
valid_binder ((x,y,true)::(update x y l))
.
Lemma irrelevant_update: forall (x y x0 y0:V) (b:bool) st,
x <> x0 /\ y <> y0 ->
In (x,y,b) st <-> In (x,y,b) (update x0 y0 st).
Proof.
split;intros.
+ induction st.
- inversion H0.
- destruct H0.
* subst a. left. simpl. assert(var_occur x0 x + var_occur y0 y=0).
destruct H. destruct_eq_dec. now rewrite H0.
* right. tauto.
+ induction st. simpl in H0. tauto.
destruct H0.
- destruct a as [[x1 y1] b1]. unfold binder_update in H0. unfold var_occur in H0.
destruct (var_eq_dec x0 x1); destruct (var_eq_dec y0 y1);simpl in H0;
try inversion H0; try subst; try destruct H; try congruence. now left.
- right. tauto.
Qed.
Lemma binder_not_in_list_update_l: forall x y z st,
~ In (x,y,true) (update x z st).
Proof.
induction st. easy.
destruct a as [[s1 s2] b].
simpl. unfold var_occur.
destruct (var_eq_dec x s1); destruct (var_eq_dec z s2);subst;simpl;apply deMorgan2;
split;try congruence; tauto.
Qed.
Lemma binder_not_in_list_update_r: forall x y z st,
~ In (x,y,true) (update z y st).
Proof.
induction st. tauto.
destruct a as [[s1 s2] b].
simpl. unfold var_occur.
destruct (var_eq_dec z s1); destruct (var_eq_dec y s2);subst;simpl;apply deMorgan2;
split;try congruence; tauto.
Qed.
Lemma true_binder_in_updated_list: forall x y x0 y0 st,
In (x,y,true) (update x0 y0 st) -> In (x,y,true) st /\ x<>x0 /\ y <> y0.
Proof.
intros. induction st. inversion H.
destruct a as [[s1 s2] b]. destruct H.
simpl in H. unfold var_occur in H. destruct (var_eq_dec x0 s1); destruct (var_eq_dec y0 s2);
subst;simpl in H;inversion H;subst.
+ split;[left;tauto|split;congruence].
+ apply IHst in H. split;[right;tauto|tauto].
Qed.
(***This lemma is wrong!***)
(* Lemma valid_binder_false: forall x y st,
valid_binder st ->
In (x,y,false) st -> (exists z, (In (x,z,true) st \/ In (z,y,true) st)).
*)
Lemma valid_binder_injective_l: forall x y z st,
valid_binder st ->
In (x,y,true) st -> In (x,z,true) st -> y=z.
Proof.
intros. induction H. inversion H0.
destruct H0;destruct H1.
+ inversion H0;inversion H1;subst. easy.
+ inversion H0;subst. pose proof binder_not_in_list_update_l x z y l. contradiction.
+ inversion H1;subst. pose proof binder_not_in_list_update_l x y z l. contradiction.
+ apply true_binder_in_updated_list in H0. apply true_binder_in_updated_list in H1. tauto.
Qed.
Lemma valid_binder_injective_r: forall x y z st,
valid_binder st ->
In (x,y,true) st -> In (z,y,true) st -> x=z.
Proof.
intros. induction H. inversion H0.
destruct H0;destruct H1.
+ inversion H0;inversion H1;subst. easy.
+ inversion H0;subst. pose proof binder_not_in_list_update_r z y x l. contradiction.
+ inversion H1;subst. pose proof binder_not_in_list_update_r x y z l. contradiction.
+ apply true_binder_in_updated_list in H0. apply true_binder_in_updated_list in H1. tauto.
Qed.
Lemma update_valid: forall x y l,
varsort x = varsort y ->
valid_binder l ->
valid_binder ((x,y,true)::(update x y l)).
Proof. intros. now constructor. Qed.
Lemma update_length: forall x y st,
length st = length (update x y st).
Proof. induction st. easy. simpl. now rewrite IHst. Qed.
Lemma update_app: forall x y l1 l2,
update x y (l1++l2) = update x y l1 ++ (update x y l2).
Proof. intros. apply map_app. Qed.
Lemma binder_l_app: forall l1 l2,
binder_l (l1++l2) = binder_l l1 ++ binder_l l2.
Proof. intros. apply map_app. Qed.
Lemma binder_r_app: forall l1 l2,
binder_r (l1++l2) = binder_r l1 ++ binder_r l2.
Proof. intros. apply map_app. Qed.
Lemma in_binder_l: forall x y (b:bool) st,
In (x, y ,b) st ->
In x (binder_l st).
Proof.
intros. induction st.
+ inversion H.
+ destruct H. subst a. now left.
destruct a. destruct p. simpl. tauto.
Qed.
Lemma in_binder_r: forall x y (b:bool) st,
In (x, y, b) st ->
In y (binder_r st).
Proof.
intros. induction st.
+ inversion H.
+ destruct H. subst a. now left.
destruct a. destruct p. simpl. tauto.
Qed.
Lemma update_binder_l: forall x y st,
binder_l st = (binder_l (update x y st)).
Proof.
induction st.
+ easy.
+ destruct a. destruct p. simpl.
destruct (var_occur x v + var_occur y v0); rewrite IHst;reflexivity.
Qed.
Lemma update_binder_r: forall x y st,
binder_r st = binder_r (update x y st).
Proof.
induction st.
+ easy.
+ destruct a. destruct p. simpl.
destruct (var_occur x v + var_occur y v0); rewrite IHst; reflexivity.
Qed.
Ltac shorten_update:=
repeat match goal with
| [|- context [binder_l (update ?x ?y ?l)]] => rewrite <- update_binder_l
| [|- context [binder_r (update ?x ?y ?l)]] => rewrite <- update_binder_r
| [|- context [length (update ?x ?y ?l)]] => rewrite <- update_length
| [H: context [binder_l (update ?x ?y ?l)]|- _] => rewrite <- update_binder_l in H
| [H: context [binder_r (update ?x ?y ?l)]|- _] => rewrite <- update_binder_r in H
| [H: context [length (update ?x ?y ?l)]|- _] => rewrite <- update_length in H
end.
(** Alpha reflexivity **)
Inductive same_binder: V* V * bool->Prop:=
| same_binder_constructor: forall (s:V) (b:bool), same_binder (s,s,b)
.
Lemma update_same_binder: forall s l,
Forall same_binder l <->
Forall same_binder (update s s l).
Proof.
split;intros.
+ induction l.
- tauto.
- destruct a. rewrite <- Forall_cons_iff in H.
destruct H. inversion H; subst.
simpl. destruct (var_occur s s0 + var_occur s s0); apply Forall_cons; try constructor;tauto.
+ induction l.
- constructor.
- destruct a as [[s1 s2] b].
simpl in H. inversion H; subst. clear H.
apply Forall_cons;[|tauto]. destruct (var_occur s s1 + var_occur s s2).
* tauto.
* inversion H2;subst. constructor.
Qed.
Lemma same_binder_same_list: forall l,
Forall same_binder l ->
binder_l l = binder_r l.
Proof.
intros. induction l.
+ reflexivity.
+ rewrite <- Forall_cons_iff in H. destruct H.
destruct a. inversion H;subst.
apply IHl in H0. simpl. congruence.
Qed.
Lemma aeq_refl_varong: forall l t,
Forall same_binder l ->
valid_binder l ->
alpha_eq l t t.
Proof.
intros. generalize dependent l. induction t;intros.
+ constructor.
+ induction H0.
- now apply str_aeq2.
- inversion H; subst. clear H. inversion H4;subst. clear H4.
destruct (var_eq_dec y s).
* subst y. apply str_aeq1. easy. now left.
* apply update_same_binder in H5. apply IHvalid_binder in H5. clear IHvalid_binder.
inversion H5;subst.
++ apply str_aeq1. easy. right. apply irrelevant_update;[split;congruence|tauto].
++ apply str_aeq2;simpl;apply deMorgan2;split;try congruence; shorten_update;tauto.
+ constructor; [apply IHt1;tauto| apply IHt2;tauto].
+ constructor. specialize IHt with ((v, v, true) :: update v v l). easy.
apply IHt. apply Forall_cons. constructor. now apply update_same_binder. now apply update_valid.
Qed.
Fact aeq_refl: Reflexive aeq.
Proof. unfold Reflexive. intros. apply aeq_refl_varong with (l:=nil); [apply Forall_nil| constructor]. Qed.
(** Alpha Symmetry **)
Inductive sym_binder: binder_list -> binder_list -> Prop:=
| sym_nil: sym_binder nil nil
| sym_cons: forall x y b bd1 bd2,
sym_binder bd1 bd2 ->
sym_binder ((x,y,b)::bd1) ((y,x,b)::bd2).
Lemma in_sym_binder: forall x y b bd1 bd2,
sym_binder bd1 bd2 ->
In (x,y,b) bd1 <-> In (y,x,b) bd2.
Proof.
intros. generalize dependent bd2. induction bd1; intros.
+ inversion H; subst. tauto.
+ split; intros.
- inversion H0; subst;inversion H; subst.
* now left.