-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambdaES_Infra.v
2999 lines (2729 loc) · 112 KB
/
LambdaES_Infra.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
(*******************************************************************************************
* Formalization of ES calculi *
* *
* Infrastructure for explicit substitutions, not specific to a calculus *
* *
* Arthur Chargueraud, 2007 *
* Fabien Renaud & Stephane Zimmerman, 2011 *
* Flavio L. C. de Moura & Daniel L. Ventura & Washington R. Segundo, 2017 *
********************************************************************************************)
Set Implicit Arguments.
Require Import Metatheory LambdaES_Defs.
Require Import Rewriting LambdaES_Tac LambdaES_FV.
Require Import Arith List.
(* ********************************************************************** *)
(** * Instanciation of tactics *)
(* Tactic [gather_vars] returns a set of variables occurring in
the context of proofs, including domain of environments and
free variables in terms mentionned in the context. *)
Ltac gather_vars :=
let A := gather_vars_with (fun x : vars => x) in
let B := gather_vars_with (fun x : var => {{ x }}) in
let D := gather_vars_with (fun x : pterm => fv x) in
constr:(A \u B \u D).
(* Tactic [pick_fresh x] adds to the context a new variable x
and a proof that it is fresh from all of the other variables
gathered by tactic [gather_vars]. *)
Ltac pick_fresh Y :=
let L := gather_vars in (pick_fresh_gen L Y).
(* Tactic [apply_fresh T as y] takes a lemma T of the form
[forall L ..., (forall x, x \notin L, P x) -> ... -> Q.]
instanciate L to be the set of variables occurring in the
context (by [gather_vars]), then introduces for the premise
with the cofinite quantification the name x as "y" (the second
parameter of the tactic), and the proof that x is not in L. *)
Tactic Notation "apply_fresh" constr(T) "as" ident(x) :=
apply_fresh_base T gather_vars x.
Tactic Notation "apply_fresh" "*" constr(T) "as" ident(x) :=
apply_fresh T as x; auto_star.
Tactic Notation "apply_fresh" constr(T) :=
apply_fresh_base T gather_vars ltac_no_arg.
Tactic Notation "apply_fresh" "*" constr(T) :=
apply_fresh T; auto_star.
(******************************************************)
(** Lemmas. *)
(* Open_var with fresh names is an injective operation *)
Lemma open_var_inj : forall (x:var) t1 t2, x \notin (fv t1) ->
x \notin (fv t2) -> (t1 ^ x = t2 ^ x) -> (t1 = t2).
Proof.
intros x t1. unfold open. generalize 0.
induction t1; intro k; destruct t2; simpl; intros; inversion H1;
try solve [ f_equal*
| do 2 try case_nat; inversions* H1; try notin_contradiction ].
rewrite IHt1_1 with (n:=k) (t2:=t2_1) ; auto.
rewrite IHt1_2 with (n:=k) (t2:=t2_2) ; auto.
rewrite IHt1_1 with (n:=S k) (t2:=t2_1) ; auto.
rewrite IHt1_2 with (n:=k) (t2:=t2_2) ; auto.
rewrite IHt1_1 with (n:=S k) (t2:=t2_1) ; auto.
rewrite IHt1_2 with (n:=k) (t2:=t2_2) ; auto.
Qed.
Lemma open_rec_term_core :forall t j v i u, i <> j ->
{j ~> v}t = {i ~> u}({j ~> v}t) ->
t = {i ~> u}t.
Proof.
induction t; introv Neq Equ; simpls; inversion* Equ; fequals*.
case_nat*. case_nat*.
Qed.
(** Open a locally closed term is the identity *)
Lemma open_rec_term : forall t u, term t -> forall k, t = {k ~> u}t.
Proof.
induction 1; intros; simpl; fequals*; unfolds open ;
pick_fresh x; apply~ (@open_rec_term_core t1 0 (pterm_fvar x)).
Qed.
(** Substitution for a fresh name is identity. *)
Lemma subst_fresh : forall x t u, x \notin fv t -> [x ~> u] t = t.
Proof.
intros. induction t; simpls; fequals*.
case_var*.
Qed.
(** Substitution distributes on the open operation. *)
Lemma subst_open_gen : forall x u t1 t2 k, term u ->
[x ~> u] ({k ~> t2}t1) = {k ~> ([x ~> u]t2)} ([x ~> u]t1).
Proof.
intros_all. gen k. induction t1; intros; simpl; fequals*.
case_nat*. case_var*. apply* open_rec_term.
Qed.
Lemma subst_open : forall x u t1 t2, term u ->
[x ~> u] (t1 ^^ t2) = ([x ~> u]t1) ^^ ([x ~> u]t2).
Proof. intros_all. apply subst_open_gen. exact H. Qed.
(** Substitution and open_var for distinct names commute. *)
Lemma subst_open_var : forall x y u t, y <> x -> term u ->
([x ~> u]t) ^ y = [x ~> u] (t ^ y).
Proof.
introv Neq Wu. rewrite* subst_open. simpl. case_var*.
Qed.
(** Open up t with a term u is the same as open it with a fresh free variable
x and then substitute u for x. *)
Lemma subst_intro : forall x t u,
x \notin (fv t) ->
t ^^ u = [x ~> u](t ^ x).
Proof.
introv H. unfold open. generalize 0. gen H.
induction t; simpl; intros; fequals*.
case_nat*.
simpl. case_var*.
case_var*.
Qed.
(** Terms are stable by substitution *)
Lemma subst_term : forall t z u,
term u -> term t -> term ([z ~> u]t).
Proof.
induction 2; simpls*.
case_var*.
apply_fresh term_abs. rewrite* subst_open_var.
apply_fresh term_sub. rewrite* subst_open_var. assumption.
Qed.
Hint Resolve subst_term.
(** Every term is a body *)
Lemma term_is_a_body : forall t, term t -> body t.
Proof.
intros. unfold body. exists {}. intros. unfold open. rewrite <- open_rec_term. auto. auto.
Qed.
(** Open a body with a term gives a term *)
Lemma body_open_term : forall t u, body t -> term u -> term (t ^^ u).
Proof.
intros. destruct H. pick_fresh y. rewrite* (@subst_intro y).
Qed.
Hint Resolve body_open_term.
(** Open a term with a term gives a term *)
Lemma term_open_term : forall t u, term t -> term u -> term (t ^^ u).
Proof. intros. apply* body_open_term. apply* term_is_a_body. Qed.
(** Conversion from locally closed abstractions and bodies *)
Lemma term_abs_to_body : forall t1, term (pterm_abs t1) -> body t1.
Proof. intros. unfold body. inversion* H. Qed.
Lemma body_to_term_abs : forall t1, body t1 -> term (pterm_abs t1).
Proof. intros. inversion* H. Qed.
Lemma term_sub_to_body : forall t s, term (t[s]) -> body t.
Proof. intros. unfold body. inversion* H. Qed.
Lemma term_sub_to_term : forall t s, term (t[s]) -> term s.
Proof. intros. inversion* H. Qed.
Lemma body_to_subs : forall t u, body t -> term u -> term (pterm_sub t u).
Proof. intros. inversion* H. Qed.
Lemma subs_to_body : forall t u, term (pterm_sub t u) -> (body t /\ term u).
Proof. intros. inversion* H. split; trivial.
unfold body. exists L. intros x Fr. apply H2; trivial. Qed.
Lemma term_to_subs : forall t u, term t -> term u -> term (pterm_sub t u).
Proof. intros. apply_fresh term_sub. apply term_open_term. assumption. auto. auto. Qed.
Lemma term_app_to_term_l : forall t1 t2, term (pterm_app t1 t2) -> term t1.
Proof. intros. inversion* H. Qed.
Lemma term_app_to_term_r : forall t1 t2, term (pterm_app t1 t2) -> term t2.
Proof. intros. inversion* H. Qed.
Lemma fvar_body : forall x, body (pterm_fvar x).
Proof. intro. unfold body. exists {}. intros. unfold open. simpl. apply term_var. Qed.
Hint Resolve term_abs_to_body body_to_term_abs term_sub_to_body body_to_subs fvar_body.
Lemma body_distribute_over_application : forall t u, body (pterm_app t u) <-> body t /\ body u.
Proof.
split.
(* -> *)
unfold body; unfold open; simpl ; intros; elim H; intros.
split ; exists x; intros; specialize (H0 x0); specialize (H0 H1) ;
inversion H0 ; assumption.
(* <- *)
intros. unfold body in H. unfold body. destruct H.
destruct H. destruct H0.
exists (x \u x0). intros.
unfold open. simpl. constructor.
specialize (H x1) . auto.
specialize (H0 x1) . auto.
Qed.
Lemma term_abs_term : forall t, term t -> term (pterm_abs t).
Proof.
intros. apply term_abs with (L:={}). intros. apply term_open_term. assumption. auto.
Qed.
Lemma body_abs : forall t, body t -> body (pterm_abs t).
Proof.
intros. unfold body. exists {}.
intros. apply* term_open_term.
Qed.
Lemma close_var_rec_open : forall t x y z i j , i <> j -> x <> y -> y \notin fv t ->
{i ~> pterm_fvar y}({j ~> pterm_fvar z} (close_rec j x t)) = {j ~> pterm_fvar z}(close_rec j x ({i ~> pterm_fvar y}t)).
Proof.
induction t; simpl; intros; try solve [ f_equal* ].
do 2 (case_nat; simpl); try solve [ case_var* | case_nat* ].
case_var*. simpl. case_nat*.
Qed.
Lemma open_close_var : forall x t, term t -> t = (close t x) ^ x.
Proof.
introv W. unfold close, open. generalize 0.
induction W; intros k; simpls; f_equal*.
case_var*. simpl. case_nat*.
let L := gather_vars in match goal with |- _ = ?t =>
destruct (var_fresh (L \u fv t)) as [y Fr] end.
apply* (@open_var_inj y).
unfolds open. rewrite* close_var_rec_open. VSD.fsetdec.
let L := gather_vars in match goal with |- _ = ?t =>
destruct (var_fresh (L \u fv t)) as [y Fr] end.
apply* (@open_var_inj y).
unfolds open. rewrite* close_var_rec_open. VSD.fsetdec.
Qed.
Lemma close_var_body : forall x t, term t -> body (close t x).
Proof.
introv W. exists {{x}}. intros y Fr.
unfold open, close. generalize 0. gen y.
induction W; intros y Fr k; simpls.
case_var; simpl. case_nat*.
auto*.
apply* term_app.
apply_fresh* term_abs.
unfolds open. rewrite* close_var_rec_open. VSD.fsetdec.
apply_fresh* term_sub. unfolds open. rewrite* close_var_rec_open. VSD.fsetdec.
Qed.
Lemma close_fresh : forall t x k, x \notin fv t -> close_rec k x t = t.
Proof.
intros t x k x_notin_t. unfold close. gen k.
induction t ; intro k ; simpls* ; try (fequals ; eauto).
case_var*.
Qed.
Lemma subst_close : forall t x y u,
x \notin fv u ->
y \notin fv u ->
x <> y ->
[y ~> u] (close t x) = close ([y ~> u]t) x.
Proof.
intros t x y u x_notin_u y_notin_u x_neq_y.
unfold close. generalize 0 as k.
induction t ; intro k ; simpls* ; try (fequals ; eauto).
case_var ; simpl ; case_var ; simpls.
case_var*.
rewrite* close_fresh.
case_var*.
Qed.
Lemma subst_as_close_open : forall t x u, term t -> [x ~> u] t = (close t x) ^^ u.
Proof.
intros t x u term_t. rewrite subst_intro with (x:=x).
rewrite <- open_close_var with (x:=x) ; auto.
apply notin_fv_close.
Qed.
(** Auxiliary lemmas. *)
Lemma term_distribute_over_application : forall t u, term (pterm_app t u) <-> term t /\ term u.
Proof.
split.
(* -> *)
intro. split;
inversion H; assumption.
(* <- *)
intro.
destruct H.
apply term_app; assumption.
Qed.
Hint Resolve body_open_term.
Lemma not_body_Sn: forall n, ~(body (pterm_bvar (S n))).
Proof.
intro n.
intro H.
elim H.
intro L.
intro H1.
pick_fresh z.
assert (z \notin L). auto.
assert (term (pterm_bvar (S n) ^ z)).
apply H1.
assumption.
inversion H2.
Qed.
Lemma body_to_term: forall t x, x \notin fv t -> body t -> term (t^x).
Proof.
intros.
inversion* H0.
Qed.
(*
Lemma term_to_body: forall t x, x \notin fv t -> term (t^x) -> body t.
Proof.
induction t.
intros.
unfold body.
simpl in H.
exists {}.
intros.
inversion H0.
inversion H3.
unfold open.
*)
(* ********************************************************************** *)
(** Induction Principles Part 1*)
(* Useful to show the induction principle term_size *)
Lemma peano_induction :
forall (P:nat->Prop),
(forall n, (forall m, m < n -> P m) -> P n) ->
(forall n, P n).
Proof.
introv H. cuts* K: (forall n m, m < n -> P m).
induction n; introv Le. inversion Le. apply H.
intros. apply IHn. omega.
Qed.
Lemma pterm_size_open_var : forall n t x, pterm_size t = pterm_size (open_rec n (pterm_fvar x) t).
Proof.
intros n t x.
generalize n; clear n; induction t.
(* bvar *)
intro n'; simpl; case_nat*.
(* fvar *)
intro n; simpl; trivial.
(* app *)
intro n; simpl; rewrite (IHt1 n); rewrite (IHt2 n); trivial.
(* abs *)
intro n; simpl; rewrite (IHt (S n)); trivial.
(* sub *)
intro n; simpl; rewrite (IHt1 (S n)); rewrite (IHt2 n); trivial.
(* sub' *)
intro n; simpl; rewrite (IHt1 (S n)); rewrite (IHt2 n); trivial.
Defined.
Lemma pterm_size_induction :
forall P : pterm -> Prop,
(forall n, P (pterm_bvar n)) ->
(forall x, P (pterm_fvar x)) ->
(forall t1,
(forall t2 x, x \notin fv t2 -> pterm_size t2 = pterm_size t1 ->
P (t2 ^ x)) -> P (pterm_abs t1)) ->
(forall t1 t2, P t1 -> P t2 -> P (pterm_app t1 t2)) ->
(forall t1 t3, P t3 ->
(forall t2 x, x \notin fv t2 -> pterm_size t2 = pterm_size t1 ->
P (t2 ^ x)) -> P (t1[t3])) ->
(forall t1 t3, P t3 ->
(forall t2 x, x \notin fv t2 -> pterm_size t2 = pterm_size t1 ->
P (t2 ^ x)) -> P (t1[[t3]])) ->
(forall t, P t).
Proof.
intros P Ha Hb Hc Hd He Hf t.
gen_eq n: (pterm_size t). gen t.
induction n using peano_induction.
introv Eq. subst. destruct t.
apply Ha.
apply Hb.
(* app *)
apply~ Hd. apply~ H. simpl; omega. apply~ H. simpl; omega.
(* abs *)
apply* Hc.
introv Fr Eq.
apply~ H. unfold open.
rewrite <- pterm_size_open_var. simpl. omega.
(* sub *)
apply* He.
apply~ H. simpl. omega.
introv Fr Eq.
apply~ H. unfold open.
rewrite <- pterm_size_open_var. simpl. omega.
(* sub' *)
apply* Hf.
apply~ H. simpl. omega.
introv Fr Eq.
apply~ H. unfold open.
rewrite <- pterm_size_open_var. simpl. omega.
Qed.
Lemma pterm_induction :
forall P : pterm -> Prop,
(forall n, P (pterm_bvar n)) ->
(forall x, P (pterm_fvar x)) ->
(forall t1, P t1 -> forall t2, P t2 -> P (pterm_app t1 t2)) ->
(forall t1, P (t1) -> P (pterm_abs t1)) ->
(forall t1, P t1 -> forall t2, P (t2) -> P (t1[t2])) ->
(forall t1, P t1 -> forall t2, P (t2) -> P (t1[[t2]])) ->
(forall t, P t).
Proof.
intros P Hbvar Hfvar Happ Habs Hsub Hsub' t.
gen t. simple induction t.
assumption. assumption.
apply Happ.
apply Habs.
apply Hsub.
apply Hsub'.
Qed.
(* ********************************************************************** *)
(** Equivalence of [term and [term'] *)
Lemma lc_at_Sn_n: forall n t, lc_at (S n) t -> ~(has_free_index n t) -> lc_at n t.
Proof.
intros n t. gen n. induction t.
- introv Hfi Hlc_at. simpl in *.
case_nat. apply False_ind. apply Hlc_at. auto.
apply lt_n_Sm_le in Hfi. apply le_lt_or_eq in Hfi. destruct Hfi.
+ assumption.
+ symmetry in H. contradiction.
- introv Hlc_at Hfi. simpl in *. auto.
- introv Hlc_at Hfi. simpl in *.
destruct Hlc_at as [Hlc_at_t1 Hlc_at_t2].
apply Decidable.not_or in Hfi.
destruct Hfi as [ Hfi_t1 Hfi_t2]. split.
+ apply IHt1; assumption.
+ apply IHt2; assumption.
- introv Hlc_at Hfi. simpl in *.
apply IHt; assumption.
- introv Hlc_at Hfi. simpl in *.
destruct Hlc_at as [Hlc_at_t1 Hlc_at_t2].
apply Decidable.not_or in Hfi.
destruct Hfi as [Hfi_t1 Hfi_t2]. split.
+ apply IHt1; assumption.
+ apply IHt2; assumption.
- introv Hlc_at Hfi. simpl in *. assumption.
Qed.
Lemma has_fi_subst: forall x n t u, term u -> has_free_index n t -> has_free_index n ([x ~> u]t).
Proof.
Admitted.
Lemma subst_has_fi: forall x n t u, term u -> has_free_index n ([x ~> u]t) -> has_free_index n t.
Proof.
Admitted.
Lemma lc_rec_open_var_rec : forall x t k,
lc_at k (open_rec k x t) -> lc_at (S k) t.
Proof.
induction t; simpl; introv H; auto*.
case_nat; simpls~.
Qed.
Lemma term_to_term' : forall t,
term t -> term' t.
Proof.
introv T. induction T; unfold term'; simple~.
pick_fresh x. apply~ (@lc_rec_open_var_rec (pterm_fvar x)). apply~ H0.
split.
pick_fresh x. apply~ (@lc_rec_open_var_rec (pterm_fvar x)). apply~ H0.
unfold term' in IHT. assumption.
Qed.
Lemma lc_at_open_var_rec : forall x t k,
lc_at (S k) t -> lc_at k (open_rec k (pterm_fvar x) t).
Proof.
induction t; simpl; introv H; auto*.
case_nat; simpls~.
unfold lt in *|-.
apply le_lt_or_eq in H.
case H.
intro H1. apply lt_S_n in H1; trivial.
intro H1. assert (n = k).
auto. assert (k = n). auto.
contradiction.
Qed.
Lemma term'_to_term : forall t,
term' t -> term t.
Proof.
introv. unfold term'.
apply pterm_size_induction with (t := t).
(* bvar *)
simpl. intros.
assert (~ n < 0). auto with arith.
contradiction.
(* fvar *)
simpl. intros.
apply term_var.
(* abs *)
simpl. intros.
apply term_abs with (L := fv t1).
intros x Fr.
apply (H t1); trivial.
unfold open.
apply lc_at_open_var_rec; trivial.
(* app *)
intros t1 t2 IHt1 IHt2 H.
simpl in H. apply term_app.
apply IHt1; apply H.
apply IHt2; apply H.
(* sub *)
intros. simpl in H1.
apply term_sub with (L := fv t1).
intros x Fr.
apply (H0 t1); trivial.
unfold open.
apply lc_at_open_var_rec. apply H1.
apply H. apply H1.
(* sub' *)
intros. simpl in H1. contradiction.
Qed.
Lemma term_eq_term' : forall t, term t <-> term' t.
Proof. intros. split. apply term_to_term'. apply term'_to_term. Qed.
Lemma lc_at_open_rec_rename: forall t x y m n, lc_at m (open_rec n (pterm_fvar x) t) -> lc_at m (open_rec n (pterm_fvar y) t).
Proof.
induction t. simpl. introv H. case_nat. constructor. assumption.
simpl. intros; trivial. simpl. introv H. destruct H.
apply (IHt1 x y) in H. apply (IHt2 x y) in H0.
split; assumption. simpl.
introv H. apply IHt with x. assumption. simpl.
introv H. destruct H. split. apply IHt1 with x; assumption. apply IHt2 with x; assumption.
simpl. trivial.
Qed.
Corollary term_open_rename: forall t x y, term (t^x) -> term (t^y).
Proof.
introv H. apply term_eq_term' in H. apply term_eq_term'.
apply lc_at_open_rec_rename with x. assumption.
Qed.
(* ********************************************************************** *)
(** Equivalence of [body] and [body'] *)
Lemma body_to_body' : forall t,
body t -> body' t.
Proof.
introv [L H]. pick_fresh x.
applys (@lc_rec_open_var_rec (pterm_fvar x)).
apply term_to_term'. apply~ H.
Qed.
Lemma body'_to_body : forall t,
body' t -> body t.
Proof.
introv B. exists ({}:vars). introv F.
apply term'_to_term. apply~ lc_at_open_var_rec.
Qed.
Lemma body_eq_body' : forall t, body t <-> body' t.
Proof. intros. split. apply body_to_body'. apply body'_to_body. Qed.
(* ********************************************************************** *)
(** Weakening property of [lc_at] *)
Lemma lc_at_weaken_ind : forall k1 k2 t,
lc_at k1 t -> k1 <= k2 -> lc_at k2 t.
Proof. introv. gen k1 k2. induction t; simpl; introv T Lt; auto*.
omega. apply (IHt (S k1) (S k2)); trivial; try omega.
case T; clear T; intros Tt1 Tt2. split.
apply (IHt1 (S k1) (S k2)); trivial; try omega.
apply (IHt2 k1 k2); trivial; try omega.
Qed.
Lemma lc_at_weaken : forall k t,
term' t -> lc_at k t.
Proof. introv H. apply~ (@lc_at_weaken_ind 0). omega. Qed.
(* ********************************************************************** *)
(** pterm lists *)
Fixpoint mult_app (t : pterm) (l : list pterm) {struct l} : pterm :=
match l with
| nil => t
| u::lv => pterm_app (mult_app t lv) u
end.
Notation "t // lu" := (mult_app t lu) (at level 66).
Fixpoint mult_sub (t : pterm) (l : list pterm) {struct l} : pterm :=
match l with
| nil => t
| u::lv => (mult_sub t lv)[u]
end.
Notation "t // [ lu ]" := (mult_sub t lu) (at level 66).
Fixpoint P_list (P : pterm -> Prop) (l : list pterm) {struct l} : Prop :=
match l with
| nil => True
| t::lu => (P t) /\ (P_list P lu)
end.
Notation "P %% lu" := (P_list P lu) (at level 66).
Lemma P_list_eq : forall (P : pterm -> Prop) l, (forall u, In u l -> P u) <-> (P %% l).
Proof.
intros P l. induction l; simpl; split; intros; trivial.
contradiction. split. apply H. left; trivial.
apply IHl. intros. apply H. right; trivial.
destruct H. destruct H0. rewrite <- H0. trivial.
apply IHl; trivial.
Qed.
(* ********************************************************************** *)
(** Normal Form **)
(*
Inductive NF_ind (R : pterm -> pterm -> Prop): pterm -> Prop :=
| NF_ind_app : forall x l, (forall u, In u l -> NF_ind R u) -> NF_ind R ((pterm_fvar x) // l)
| NF_ind_abs : forall t L, (forall x, x \notin L -> NF_ind R (t ^ x)) -> NF_ind R (pterm_abs t).
*)
Definition NF (R : pterm -> pterm -> Prop) (t : pterm) := forall t', ~ R t t'.
(*
Lemma mult_app_append : forall t1 t2 l, pterm_app t1 t2 // l = t1 // l ++ (t2 :: nil).
Proof.
intros. induction l; simpl; trivial.
rewrite IHl. trivial.
Qed.
Lemma mult_app_append' : forall t1 t2 l l', pterm_app (t1 // l') t2 // l = t1 // l ++ (t2 :: l').
Proof.
intros. generalize t1 t2 l; clear t1 t2 l. induction l'; intros.
simpl; apply mult_app_append.
replace (l ++ t2 :: a :: l') with ((l ++ (t2 :: nil)) ++ (a :: l')). rewrite <- IHl' .
simpl. rewrite mult_app_append. trivial.
rewrite <- app_assoc. simpl.
trivial.
Qed.
Lemma P_list_append : forall P l1 l2, P_list P (l1 ++ l2) <-> ((P_list P l1) /\ (P_list P l2)).
Proof.
intros. induction l1; simpl.
split. intro H. split; trivial.
intro H. destruct H; trivial.
split. intro H. split. split.
apply H. apply IHl1. apply H.
apply IHl1. apply H.
intro H. split. apply H.
apply IHl1. split; apply H.
Qed.
Lemma eqdec_nil : forall A (l: list A), (l = nil) \/ (l <> nil).
Proof.
intros A l. induction l.
left; trivial. right; discriminate.
Qed.
Lemma m_app_eq_app : forall t lu, lu <> nil ->
exists t', exists u', t // lu = pterm_app t' u'.
Proof.
intros. destruct lu. apply False_ind. apply H; trivial.
simpl. exists (t // lu). exists p. trivial.
Qed.
Lemma not_nil_append : forall A (l: list A), l <> nil ->
exists a, exists l', l = l' ++ (a :: nil).
Proof.
intros. induction l. apply False_ind. apply H; trivial.
case eqdec_nil with (l := l). intro H'. rewrite H'.
exists a. exists (nil (A := A)). simpl; trivial.
intro H'. case IHl; trivial; clear IHl.
intros a' H0. case H0; clear H0.
intros l' H0. rewrite H0.
exists a'. exists (a :: l'). simpl.
trivial.
Qed.
(** mult_app injectivity **)
Lemma mult_app_inj_l : forall t t' l, t // l = t' // l -> t = t'.
Proof.
intros t t' l H. induction l;
simpl in *|-*; inversion H; try apply IHl; trivial.
Qed.
Lemma mult_app_inj_r : forall t0 t t' l l', t0 // (l ++ (t :: l')) = t0 // (l ++ (t':: l')) -> t = t'.
Proof.
intros t0 t t' l l' H.
rewrite <- mult_app_append' in H. rewrite <- mult_app_append' in H.
apply mult_app_inj_l in H. inversion H. trivial.
Qed.
Lemma mult_app_inj_r'_aux : forall t l, t = t // l -> l = nil.
Proof.
intro t. induction t; intros; destruct l; trivial; simpl in H;
try inversion H. rewrite mult_app_append in H1.
apply IHt1 in H1. apply False_ind. clear t1 p IHt1 IHt2 H H2.
case eqdec_nil with (l := l); intros.
rewrite H in H1. simpl in H1. inversion H1.
apply app_eq_nil in H1. destruct H1.
contradiction.
Qed.
Lemma mult_app_inj_r' : forall t l l', t // l = t // l' -> l = l'.
Proof.
intros t l. generalize t; clear t. induction l; intros.
simpl in H. apply mult_app_inj_r'_aux in H. rewrite H. trivial.
destruct l'. replace (t // nil) with t in H.
assert (Q : a :: l = nil). apply mult_app_inj_r'_aux with (t := t). rewrite H; trivial.
trivial. simpl. trivial.
simpl in H. inversion H. apply IHl in H1.
rewrite H1. trivial.
Qed.
Lemma mult_app_inj_aux : forall l t t', t = t' // l -> ((t = t' /\ l = nil) \/
(exists l', exists u, l = l' ++ (u :: nil))).
Proof.
intro l. induction l; simpl; intros.
left; split; trivial. destruct t; try inversion H.
apply IHl in H1.
right. destruct H1. destruct H0. rewrite H1.
simpl. exists (nil (A := pterm)) a.
simpl; split; trivial.
case H0; clear H0; intros l0 H0.
case H0; clear H0; intros u0 H0.
rewrite H0. exists (a :: l0) u0.
simpl. trivial.
Qed.
Lemma length_0 : forall (A : Set) (l : list A), 0 = length l -> l = nil.
Proof.
intros. induction l; trivial.
simpl in H. inversion H.
Qed.
Lemma mult_app_eq_length_inj : forall t t' l l', (length l) = (length l') ->
t // l = t' // l' -> t = t' /\ l = l'.
Proof.
intros. generalize t t' l' H H0. clear t t' l' H H0. induction l; simpl; intros.
apply length_0 in H. rewrite H in *|-*. simpl in H0. split; trivial.
destruct l'; simpl in H. inversion H.
simpl in H0. inversion H0. apply IHl in H2.
destruct H2. rewrite H1. rewrite H2. split; trivial.
omega.
Qed.
Lemma mult_app_var_inj : forall l l' x x',
(pterm_fvar x) // l = (pterm_fvar x') // l' -> (x = x' /\ l = l').
Proof.
intro l. induction l; intros; destruct l'; simpl in H; inversion H.
split; trivial. apply IHl in H1. destruct H1. rewrite H1.
split; trivial.
Qed.
Lemma mult_app_diff_var_abs : forall l l' x t,
(pterm_fvar x) // l <> (pterm_abs t) // l'.
Proof.
intros l. induction l; destruct l'; simpl in *|-*; try discriminate.
intros. intro H. inversion H. generalize H1.
apply IHl.
Qed.
Lemma mult_app_diff_var_sub : forall l l' x t u,
(pterm_fvar x) // l <> (t[u]) // l'.
Proof.
intros l. induction l; destruct l'; simpl in *|-*; try discriminate.
intros. intro H. inversion H. generalize H1.
apply IHl.
Qed.
Lemma mult_app_abs_inj : forall l l' t t',
(pterm_abs t) // l = (pterm_abs t') // l' -> (t = t' /\ l = l').
Proof.
intro l. induction l; intros; destruct l'; simpl in H; inversion H.
split; trivial. apply IHl in H1. destruct H1. rewrite H1.
split; trivial.
Qed.
Lemma mult_app_diff_abs_sub : forall l l' t u v,
(pterm_abs t) // l <> (u[v]) // l'.
Proof.
intros l. induction l; destruct l'; simpl in *|-*; try discriminate.
intros. intro H. inversion H. generalize H1.
apply IHl.
Qed.
Lemma mult_app_sub_inj : forall l l' t u t' u',
(t[u]) // l = (t'[u']) // l' -> (t = t' /\ u = u' /\ l = l').
Proof.
intro l. induction l; intros; destruct l'; simpl in H; inversion H.
split; trivial. split; trivial. apply IHl in H1. destruct H1. destruct H1.
rewrite H0. rewrite H1. rewrite H3.
split; trivial. split; trivial.
Qed.
Lemma app_singleton : forall l (t : pterm) l1 l2,
l <> nil -> l1 ++ (t :: nil) = l2 ++ l -> exists l', l = l' ++ (t :: nil).
Proof.
induction l; simpl; intros. apply False_ind. apply H; trivial.
case eqdec_nil with (l := l). intro H1. rewrite H1 in *|-*.
apply app_inj_tail in H0. destruct H0. rewrite H2.
exists (nil (A := pterm)). simpl. trivial.
intro H1. case IHl with (t := t) (l1 := l1) (l2 := l2 ++ a :: nil); trivial.
rewrite H0. rewrite <- app_assoc; simpl; trivial.
intros l' H2. rewrite H2. exists (a :: l'). simpl. trivial.
Qed.
Lemma geq_app_list : forall (l4 l1 l2 l3 : list pterm), l1 ++ l2 = l3 ++ l4 ->
length l2 > length l4 -> (exists l, l2 = l ++ l4 /\ l3 = l1 ++ l).
Proof.
intro l4. induction l4; simpl; destruct l2; intros.
simpl in H0. assert (~ 0 > 0). auto with arith. contradiction.
exists (p :: l2). simpl.
rewrite app_nil_r in *|-*. rewrite H. split; trivial. simpl in H0.
assert (~ 0 > S (length l4)). auto with arith. contradiction.
simpl in H0. case IHl4 with (l1 := l1 ++ (p::nil)) (l2 := l2) (l3 := l3 ++ (a::nil)).
rewrite <- app_assoc. rewrite <- app_assoc. simpl. trivial. omega.
clear IHl4; intros l H1. destruct H1. rewrite H1. rewrite <- app_assoc in H2. simpl in H2.
case eqdec_nil with (l := l). intro H4.
rewrite H4 in H2. apply app_inj_tail in H2. destruct H2.
rewrite H2. rewrite H3. rewrite H4. exists (nil (A := pterm)).
rewrite app_nil_l. rewrite app_nil_l. rewrite app_nil_r. split; trivial.
replace (l1 ++ p :: l) with ((l1 ++ p :: nil) ++ l) in H2.
intros. generalize H2. intro H4. apply app_singleton in H4; trivial.
case H4; clear H4; intros l' H4. rewrite H4. rewrite H4 in H2. exists (p :: l'); split.
simpl. rewrite <- app_assoc. simpl. trivial. simpl in H2.
replace ((l1 ++ p :: nil) ++ l' ++ a :: nil) with ((l1 ++ p :: l') ++ a :: nil) in H2.
apply app_inj_tail in H2. destruct H2; trivial.
rewrite <- app_assoc. rewrite <- app_assoc; simpl; trivial.
rewrite <- app_assoc; simpl; trivial.
Qed.
Lemma P_eq_app_list : forall (P: pterm -> Prop) t1 t2 l1 l2 l3 l4,
P %% l2 -> P %% l4 -> ~ P t1 -> ~ P t2 -> l1 ++ t1 :: l2 = l3 ++ t2 :: l4 ->
(l1 = l3 /\ t1 = t2 /\ l2 = l4).
Proof.
intros. case (length l2 == length l4).
intro H4. gen_eq l2' : (t1 :: l2). gen_eq l4' : (t2 :: l4). intros H5 H6.
assert (H7 : length l2' = length l4').
rewrite H5. rewrite H6. simpl. rewrite H4. trivial.
clear H H0 H1 H2 H4.
assert (H8 : length l1 = length l3).
assert (Q : length (l1 ++ l2') = length l1 + length l2').
rewrite app_length; trivial.
assert (Q' : length (l3 ++ l4') = length l3 + length l4').
rewrite app_length; trivial.
rewrite H3 in Q. rewrite Q in Q'. rewrite H7 in Q'.
omega.
assert (H9: l1 = l3).
clear H5 H6 H7. generalize l3 l2' l4' H3 H8; clear t1 t2 l2' l2 l3 l4 l4' H3 H8.
induction l1; simpl; intros.
apply length_0 in H8. rewrite H8; trivial.
destruct l3. simpl in H8. inversion H8.
simpl in H3. inversion H3. fequals. simpl in H8.
apply IHl1 with (l2' := l2') (l4' := l4'); try omega; trivial.
split; trivial. rewrite H9 in H3.
apply app_inv_head in H3. rewrite H5 in H3. rewrite H6 in H3.
inversion H3. split; trivial.
intro H4.
assert (Q : length l2 > length l4 \/ length l4 > length l2).
clear H H0 H1 H2 H3. generalize l4 H4; clear l1 l3 l4 H4.
induction l2; destruct l4; simpl; intros; try omega.
apply False_ind. apply H4; trivial.
assert (Q'' : length l2 > length l4 \/ length l4 > length l2).
apply IHl2; try omega. intro H5. apply H4. rewrite H5; trivial.
destruct Q''. left. omega. right. omega.
clear H4. destruct Q.
apply geq_app_list with (l1 := l1 ++ (t1 :: nil)) (l3 := l3 ++ (t2 :: nil)) in H4; trivial.
case H4; clear H4. intros l H4. destruct H4. case eqdec_nil with (l := l). intro H6.
rewrite H6 in H5. rewrite app_nil_r in H5. apply app_inj_tail in H5. destruct H5.
rewrite H6 in H4. simpl in H4. rewrite H5. rewrite H7. split; trivial. split; trivial.
intro H6. apply app_singleton in H5; trivial. case H5; clear H5; intros l' H5.
assert (Q : In t2 l2). rewrite H4. rewrite H5. apply in_or_app. left. apply in_or_app. right.
simpl. left; trivial.
rewrite <- P_list_eq in H. apply H in Q. contradiction.
rewrite <- app_assoc. rewrite <- app_assoc; simpl; trivial.
symmetry in H3.
apply geq_app_list with (l3 := l1 ++ (t1 :: nil)) (l1 := l3 ++ (t2 :: nil)) in H4; trivial.
case H4; clear H4. intros l H4. destruct H4. case eqdec_nil with (l := l). intro H6.
rewrite H6 in H5. rewrite app_nil_r in H5. apply app_inj_tail in H5. destruct H5.
rewrite H6 in H4. simpl in H4. rewrite H5. rewrite H7. rewrite H4. split; trivial. split; trivial.
intro H6. apply app_singleton in H5; trivial. case H5; clear H5; intros l' H5.
assert (Q : In t1 l4). rewrite H4. rewrite H5. apply in_or_app. left. apply in_or_app. right.
simpl. left; trivial.
rewrite <- P_list_eq in H0. apply H0 in Q. contradiction.
rewrite <- app_assoc. rewrite <- app_assoc; simpl; trivial.
Qed.
*)
(* ********************************************************************** *)
(** About SN & NF **)
Lemma NF_to_SN0 : forall R t, NF R t -> SN_ind 0 R t.
Proof.
intros R t H.
apply SN_intro.
intros t' H'. unfold NF in H.
assert (Q : ~ R t t'). apply H.
contradiction.
Qed.
Lemma SN0_to_NF : forall R t, SN_ind 0 R t -> NF R t.
Proof.
intros R t H. unfold NF.
intros t' H'. inversion H.
case H0 with (t' := t'). assumption.
intros n H''. omega.
Qed.
Lemma NF_eq_SN0 : forall R t, NF R t <-> SN_ind 0 R t.
Proof. intros R t. split; [apply NF_to_SN0 | apply SN0_to_NF]. Qed.
Lemma WSN : forall n k R t, k <= n -> SN_ind k R t -> SN_ind n R t.
Proof.
intros n k R t H.
destruct H; trivial. intro H'.
apply SN_intro. intros t' H''. destruct H'.
case (H0 t'); trivial. intros n' H'''. exists n'.
split; try omega. apply H'''.
Qed.
Lemma SN_one_step : forall n (R : pterm -> pterm -> Prop) t u, R t u -> SN_ind (S n) R t -> SN_ind n R u.
Proof.
intros n R t u H H'.
destruct H'. case (H0 u); trivial.
intro n'. intro H'. apply WSN with (k := n'). omega.
apply H'.
Qed.
Lemma SN_trs : forall n R t u, trans_closure R t u ->
SN_ind n R t -> (exists k, k < n /\ SN_ind k R u).
Proof.
intros. generalize n H0; clear n H0.
induction H; intros. destruct n. apply False_ind. apply SN0_to_NF in H0.
apply (H0 u); trivial. apply SN_one_step with (u := u) in H0; trivial.
exists n. split; try omega; trivial.
destruct H1. case (H1 u); trivial; clear H1. intros n' H1. destruct H1.
case IHtrans_closure with (n := n'); trivial. intros k' H3. destruct H3.
exists k'. split; try omega; trivial.
Qed.
(** about SN and NF in lists
Lemma NF_eq_SN0_list : forall R lt, NF R %% lt <-> SN_ind 0 R %% lt.
Proof.
intros R lt. induction lt; simpl; split; trivial.
intro H. destruct H. split.
apply NF_to_SN0; trivial. apply IHlt; trivial.
intro H. destruct H. split.
apply SN0_to_NF; trivial. apply IHlt; trivial.
Qed.
Lemma WSN_list : forall n k R lt, k <= n -> SN_ind k R %% lt -> SN_ind n R %% lt.
Proof.
intros n k R lt H. induction lt; simpl; trivial.
intro H'. destruct H'. split. apply WSN with (k := k); trivial.
apply IHlt; trivial.
Qed.
Lemma SN_list : forall R lt, SN R %% lt <-> exists n, SN_ind n R %% lt.
Proof.
intros R lt. induction lt; simpl; split; intros; trivial.
exists 0; trivial. destruct IHlt. destruct H. case H0; clear H0; trivial.
intros n H0. case H; clear H. intros n' H. exists (n + n').
split. apply WSN with (k := n'); try omega; trivial.
apply WSN_list with (k := n); try omega; trivial. split.
case H; clear H. intros n H. destruct H. exists n; trivial.
case H; clear H. intros n H. destruct H. apply IHlt. exists n; trivial.
Qed.
*)
(* ********************************************************************** *)
(** Induction Principles Part 2 *)
Lemma term_size_induction :
forall P : pterm -> Prop,
(forall x, P (pterm_fvar x)) ->
(forall t1,
body t1 ->
(forall t2 x, x \notin fv t2 -> pterm_size t2 = pterm_size t1 ->
term (t2 ^ x) -> P (t2 ^ x)) ->
P (pterm_abs t1)) ->
(forall t1 t2,
term t1 -> P t1 -> term t2 -> P t2 ->
P (pterm_app t1 t2)) ->