-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTypeJudge.v
More file actions
2210 lines (1865 loc) · 62.3 KB
/
TypeJudge.v
File metadata and controls
2210 lines (1865 loc) · 62.3 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 Types.
Section Typage.
Inductive eq_typ : env -> term -> term -> term -> Prop :=
| type_prop : forall e,
wf e ->
eq_typ e (Srt prop) (Srt prop) (Srt kind)
| type_var :
forall e v t,
wf e ->
item_lift t e v ->
eq_typ e (Ref v) (Ref v) t
| type_abs :
forall e T T' M M' U U' s1 s2,
eq_typ e T T' (Srt s1) ->
eq_typ (T :: e) U U' (Srt s2) ->
eq_typ (T :: e) M M' U ->
eq_typ e (Abs T M) (Abs T' M') (Prod T U)
| type_app :
forall e u u' v v' V V' Ur Ur' s1 s2,
eq_typ e v v' V ->
eq_typ e V V' (Srt s1) ->
eq_typ e u u' (Prod V Ur) ->
eq_typ (V::e) Ur Ur' (Srt s2) ->
eq_typ e (App (Prod u Ur) v) (App (Prod u' Ur') v') (subst v Ur)
| type_prod :
forall e T T' U U' s1 s2,
eq_typ e T T' (Srt s1) ->
eq_typ (T :: e) U U' (Srt s2) ->
eq_typ e (Prod T U) (Prod T' U') (Srt s2)
| type_beta : forall e T T' M M' N N' U U' s1 s2,
eq_typ e N N' T ->
eq_typ e T T' (Srt s1) ->
eq_typ (T::e) M M' U ->
eq_typ (T::e) U U' (Srt s2) ->
eq_typ e (App (Prod (Abs T M) U) N) (subst N' M') (subst N U)
| type_red : forall e M M' T T' s,
eq_typ e M M' T ->
eq_typ e T T' (Srt s) ->
eq_typ e M M' T'
| type_exp : forall e M M' T T' s,
eq_typ e M M' T' ->
eq_typ e T T' (Srt s) ->
eq_typ e M M' T
with wf : env -> Prop :=
wf_nil : wf nil
| wf_var : forall e T T' s,
eq_typ e T T' (Srt s) ->
wf (T::e).
Scheme eq_typ_mind := Minimality for eq_typ Sort Prop
with wf_mind := Minimality for wf Sort Prop.
Definition eq_red e M M' T:=
clos_refl_trans _ (fun x y => eq_typ e x y T) M M'.
Definition eq_conv e M M' T:=
clos_refl_sym_trans _ (fun x y => eq_typ e x y T) M M'.
Lemma eq_conv_refl : forall e t T, eq_conv e t t T.
red; auto with sets.
Qed.
Hint Resolve eq_conv_refl : coc.
Lemma eq_conv_sym :
forall e t u T, eq_conv e t u T -> eq_conv e u t T.
red; intros; apply rst_sym; trivial.
Qed.
Lemma eq_conv_trans : forall e M M' M'' T,
eq_conv e M M' T ->
eq_conv e M' M'' T ->
eq_conv e M M'' T.
intros.
constructor 4 with M'; trivial.
Qed.
Lemma eq_red_conv : forall e M M' T, eq_red e M M' T -> eq_conv e M M' T.
unfold eq_red, eq_conv; intros; apply clos_rt_clos_rst; trivial.
Qed.
Lemma eq_conv_red : forall e T T' U,
eq_typ e T T' U -> eq_conv e T T' U.
Proof.
red; auto with sets.
Qed.
Lemma eq_conv_exp : forall e T T' U,
eq_typ e T T' U -> eq_conv e T' T U.
Proof.
red; intros; apply rst_sym; auto with sets.
Qed.
Hint Resolve eq_conv_red eq_conv_exp : coc.
Lemma type_eq_conv_gen : forall e M M' T T' s,
eq_conv e T T' (Srt s) ->
(eq_typ e M M' T -> eq_typ e M M' T') /\
(eq_typ e M M' T' -> eq_typ e M M' T).
induction 1; intros.
split; intros.
apply type_red with (2 := H); trivial.
apply type_exp with (2 := H); trivial.
auto.
destruct IHclos_refl_sym_trans; auto.
destruct IHclos_refl_sym_trans1.
destruct IHclos_refl_sym_trans2.
auto 10.
Qed.
Lemma type_eq_conv : forall e M M' T T' s,
eq_typ e M M' T -> eq_conv e T T' (Srt s) -> eq_typ e M M' T'.
intros.
elim type_eq_conv_gen with (M := M) (M' := M') (1 := H0); intros; auto.
Qed.
Lemma eq_conv_conv : forall (e : env) (M M' T T' : term) s,
eq_conv e M M' T -> eq_conv e T T' (Srt s) -> eq_conv e M M' T'.
induction 1; intros.
constructor 1.
apply type_eq_conv with T s; trivial.
red; apply rst_refl.
red; apply rst_sym.
apply IHclos_refl_sym_trans.
trivial.
red; apply rst_trans with y; auto.
apply IHclos_refl_sym_trans1; trivial.
apply IHclos_refl_sym_trans2; trivial.
Qed.
(* Basic metatheory: thinning, substitution *)
Lemma typ_wf : forall e t t' T, eq_typ e t t' T -> wf e.
Proof.
induction 1; trivial.
Qed.
Lemma typ_refl : forall e t t' T, eq_typ e t t' T -> eq_typ e t t T.
Proof.
induction 1; intros.
apply type_prop; trivial.
apply type_var; trivial.
apply type_abs with U s1 s2; trivial.
apply type_app with V V' s1 s2; trivial.
apply type_prod with s1; trivial.
apply type_app with T T s1 s2; trivial.
apply type_abs with U s1 s2; trivial.
apply type_red with T s; trivial.
apply type_exp with T' s; trivial.
Qed.
Lemma typ_thin :
forall g A e t t' T,
wf (A::g) ->
eq_typ e t t' T ->
forall n f,
ins_in_env A n e f ->
trunc n e g ->
eq_typ f (lift_rec 1 t n) (lift_rec 1 t' n) (lift_rec 1 T n).
intros g A e t t' T H H0.
elim H0 using eq_typ_mind with (P0:=fun e => forall n f, ins_in_env A n e f ->
trunc n e g -> wf f); simpl; intros.
constructor; eauto.
destruct (le_gt_dec n v); intros.
constructor; eauto.
apply ins_item_lift_ge with (1 := H4); trivial.
constructor; eauto.
apply ins_item_lift_lt with (1 := H4); trivial.
apply type_abs with (lift_rec 1 U' (S n)) s1 s2; eauto with coc.
rewrite distr_lift_subst.
apply type_app with (lift_rec 1 V n) (lift_rec 1 V' n) s1 s2;
eauto with coc.
apply type_prod with s1; eauto with coc.
repeat rewrite distr_lift_subst.
apply type_beta with (lift_rec 1 T' n) (lift_rec 1 U' (S n)) s1 s2;
auto with coc.
apply type_red with (lift_rec 1 T0 n) s; auto with coc.
apply type_exp with (lift_rec 1 T' n) s; auto with coc.
inversion_clear H1; inversion H2; subst g; trivial.
inversion H3; subst n; inversion H4; subst g.
trivial.
apply wf_var with (lift_rec 1 T' n0) s; auto.
Qed.
Lemma typ_thinning :
forall A e t t' T,
wf (A::e) ->
eq_typ e t t' T ->
eq_typ (A::e) (lift 1 t) (lift 1 t') (lift 1 T).
intros.
unfold lift.
apply typ_thin with (1 := H) (2 := H0); auto with coc.
Qed.
Lemma typ_thinning_n :
forall f t t' T n e,
wf e ->
eq_typ f t t' T ->
trunc n e f ->
eq_typ e (lift n t) (lift n t') (lift n T).
induction n; simpl; intros.
repeat rewrite lift0; inversion_clear H1; trivial.
rewrite simpl_lift.
pattern (lift (S n) t').
rewrite simpl_lift.
pattern (lift (S n) T).
rewrite simpl_lift.
inversion H1.
subst e.
apply typ_thinning; trivial.
apply IHn; auto.
inversion_clear H.
apply typ_wf with (1 := H4).
Qed.
Lemma eq_conv_lift : forall e T U A x,
eq_conv e T U A ->
wf (x::e) ->
eq_conv (x::e) (lift 1 T) (lift 1 U) (lift 1 A).
red; induction 1; intros; auto with sets.
apply eq_conv_red.
apply typ_thinning; trivial.
apply rst_sym; auto.
apply rst_trans with (lift 1 y).
apply IHclos_refl_sym_trans1; trivial.
apply IHclos_refl_sym_trans2; trivial.
Qed.
Lemma typ_sub :
forall g d t e u u' U,
eq_typ e u u' U ->
forall d' n f,
eq_typ g d d' t ->
sub_in_env d t n e f ->
trunc n f g ->
wf f ->
eq_typ f (subst_rec d u n) (subst_rec d' u' n) (subst_rec d U n).
induction 1; simpl in *; intros.
constructor; eauto.
destruct (lt_eq_lt_dec n v) as [[fvar| eq_var]| bvar].
constructor; eauto.
apply sub_item_lift_sup with (1 := H2); trivial.
subst v; rewrite sub_item_lift_eq with (1 := H2) (2 := H0).
apply typ_thinning_n with g; eauto.
constructor; eauto.
apply nth_sub_inf with (1 := H2); trivial.
assert (wf (subst_rec d T n :: f)).
apply wf_var with (subst_rec d' T' n) s1; eauto.
apply type_abs with (subst_rec d' U' (S n)) s1 s2; eauto with coc.
rewrite distr_subst.
assert (wf (subst_rec d V n :: f)).
apply wf_var with (subst_rec d' V' n) s1; eauto.
apply type_app with (subst_rec d V n) (subst_rec d' V' n) s1 s2;
eauto with coc.
assert (wf (subst_rec d T n :: f)).
apply wf_var with (subst_rec d' T' n) s1; eauto.
apply type_prod with s1; eauto with coc.
repeat rewrite distr_subst.
assert (wf (subst_rec d T n :: f)).
apply wf_var with (subst_rec d' T' n) s1; eauto.
apply type_beta with (subst_rec d' T' n) (subst_rec d' U' (S n)) s1 s2;
auto with coc.
apply type_red with (subst_rec d T n) s; eauto with coc.
apply IHeq_typ2; eauto.
apply typ_refl with (1 := H1).
apply type_exp with (subst_rec d T' n) s; eauto with coc.
apply IHeq_typ2; eauto.
apply typ_refl with (1 := H1).
Qed.
Theorem substitution :
forall e t u u' U d d',
eq_typ (t :: (e:env)) u u' U ->
eq_typ e d d' t ->
eq_typ e (subst d u) (subst d' u') (subst d U).
Proof.
intros.
unfold subst.
apply typ_sub with (1 := H) (2 := H0); auto with coc.
apply typ_wf with (1 := H0).
Qed.
Lemma eq_conv_subst_ty_l : forall e M M' T U s,
eq_conv e M M' T ->
eq_typ (T::e) U U (Srt s) ->
eq_conv e (subst M U) (subst M' U) (Srt s).
red; induction 1; intros; auto with sets.
apply rst_step.
change (Srt s) with (subst x (Srt s)).
apply substitution with T; trivial.
apply rst_sym.
auto.
apply rst_trans with (subst y U); auto.
Qed.
Lemma eq_conv_subst_r : forall e M T U U' A,
eq_typ e M M T ->
eq_conv (T::e) U U' A ->
eq_conv e (subst M U) (subst M U') (subst M A).
red; induction 2; intros; auto with sets.
apply rst_step.
apply substitution with T; trivial.
apply rst_trans with (subst M y); auto.
Qed.
Inductive red1_in_env : env -> env -> Prop :=
| red1_env_hd : forall e t u s, eq_conv e t u (Srt s) ->
red1_in_env (t :: e) (u :: e)
| red1_env_tl :
forall e f t, red1_in_env e f -> red1_in_env (t :: e) (t :: f).
Hint Constructors red1_in_env: coc.
Lemma red_item :
forall n t e f,
item_lift t e n ->
red1_in_env e f ->
wf f ->
item_lift t f n \/
(forall g, trunc (S n) e g -> trunc (S n) f g) /\
(exists2 u, exists s, eq_conv f t u (Srt s) & item_lift u f n).
simple induction n.
do 4 intro.
elim H.
do 3 intro.
rewrite H0.
inversion_clear H1.
intro.
inversion_clear H1; intros.
right.
split; intros.
inversion_clear H3; auto with coc.
exists (lift 1 u).
exists s.
change (Srt s) with (lift 1 (Srt s)).
apply eq_conv_lift; trivial.
exists u; auto with coc.
left.
exists x; auto with coc.
do 6 intro.
elim H0.
do 3 intro.
rewrite H1.
inversion_clear H2.
intro.
inversion_clear H2; intros.
left.
exists x; auto with coc.
elim H with (lift (S n0) x) l f0; auto with coc; intros.
left.
elim H5; intros.
exists x0; auto with coc.
rewrite simpl_lift.
pattern (lift (S (S n0)) x0).
rewrite simpl_lift.
elim H6; auto with coc.
right.
elim H5.
simple induction 2; intros.
split; intros.
inversion_clear H10; auto with coc.
elim H9; intros.
exists (lift (S (S n0)) x1).
rewrite simpl_lift.
pattern (lift (S (S n0)) x1).
rewrite simpl_lift.
destruct H8 as (s, H8).
exists s.
change (Srt s) with (lift 1 (Srt s)).
apply eq_conv_lift; trivial.
subst x0; trivial.
exists x1; auto with coc.
exists x; auto with coc.
inversion_clear H2.
apply typ_wf with (1 := H5).
Qed.
Lemma typ_red_env_raw :
forall e t t' T, eq_typ e t t' T -> forall f, red1_in_env e f -> wf f ->
eq_typ f t t' T.
induction 1; intros.
apply type_prop; trivial.
elim red_item with (1 := H0) (2 := H1); auto with coc; intros.
apply type_var; auto.
destruct H3.
destruct H4; destruct H4 as (s,H4).
apply type_eq_conv with x s.
apply type_var; trivial.
red; apply rst_sym; trivial.
assert (wf (T :: f)).
apply wf_var with T' s1; auto.
apply type_abs with U' s1 s2; auto with coc.
assert (wf (V :: f)).
apply wf_var with V' s1; auto.
apply type_app with V V' s1 s2; auto with coc.
assert (wf (T :: f)).
apply wf_var with T' s1; auto.
apply type_prod with s1; auto with coc.
assert (wf (T :: f)).
apply wf_var with T' s1; auto.
apply type_beta with T' U' s1 s2; auto with coc.
apply type_red with T s; auto with coc.
apply type_exp with T' s; auto with coc.
Qed.
Lemma typ_refl2 : forall e M M' T, eq_typ e M M' T -> eq_typ e M' M' T.
induction 1; intros.
apply type_prop; trivial.
apply type_var; trivial.
assert (wf (T' :: e)).
apply wf_var with T' s1; trivial.
assert (red1_in_env (T :: e) (T' :: e)).
constructor 1 with s1; auto with coc.
apply type_exp with (Prod T' U) s2; auto.
apply type_abs with U' s1 s2; auto.
apply typ_red_env_raw with (T :: e); auto.
apply typ_red_env_raw with (T :: e); auto.
apply type_prod with s1; auto.
apply typ_refl with U'; trivial.
assert (wf (V' :: e)).
apply wf_var with V' s1; trivial.
assert (red1_in_env (V :: e) (V' :: e)).
constructor 1 with s1; auto with coc.
apply type_exp with (subst v' Ur') s2.
apply type_app with V V s1 s2; auto.
apply typ_refl with V'; trivial.
apply type_red with (Prod V Ur) s2; trivial.
apply type_prod with s1; trivial.
apply typ_refl with V'; trivial.
change (Srt s2) with (subst v (Srt s2)).
apply substitution with V; trivial.
assert (wf (T' :: e)).
apply wf_var with T' s1; trivial.
assert (red1_in_env (T :: e) (T' :: e)).
constructor 1 with s1; auto with coc.
apply type_prod with s1; auto.
apply typ_red_env_raw with (T :: e); auto.
assert (wf (T' :: e)).
apply wf_var with T' s1; trivial.
assert (red1_in_env (T :: e) (T' :: e)).
constructor 1 with s1; auto with coc.
apply type_exp with (subst N' U) s2.
apply substitution with T; auto.
change (Srt s2) with (subst N (Srt s2)).
apply substitution with T; auto.
apply typ_refl with U'; trivial.
apply type_red with T s; auto.
apply type_exp with T' s; auto.
Qed.
Lemma eq_conv_inv : forall e T T' U,
eq_conv e T T' U ->
T = T' \/ eq_typ e T T U /\ eq_typ e T' T' U.
induction 1; intros; try firstorder subst; auto.
right.
split.
apply typ_refl with y; trivial.
apply typ_refl2 with x; trivial.
Qed.
Lemma eq_conv_inv2 : forall e T T' U,
eq_conv e T T' U ->
eq_typ e T T U -> eq_typ e T' T' U.
intros.
elim eq_conv_inv with (1 := H); firstorder.
subst; trivial.
Qed.
Lemma eq_red_inv : forall e T T' U,
eq_red e T T' U ->
T = T' \/ eq_typ e T T U /\ eq_typ e T' T' U.
intros; apply eq_conv_inv; apply eq_red_conv; trivial.
Qed.
Lemma eq_red_inv2 : forall e T T' U,
eq_red e T T' U ->
eq_typ e T T U -> eq_typ e T' T' U.
intros; apply eq_conv_inv2 with T; trivial; apply eq_red_conv; trivial.
Qed.
Lemma wf_red_env_trans :
forall e f,
clos_refl_trans _ red1_in_env e f ->
wf e ->
wf f.
induction 1; auto.
induction H; intros.
elim eq_conv_inv with (1 := H); intros.
subst u; trivial.
destruct H1.
apply wf_var with u s; trivial.
inversion_clear H0.
apply wf_var with T' s.
apply typ_red_env_raw with e; trivial.
apply IHred1_in_env.
eapply typ_wf; eauto.
Qed.
Lemma typ_red_env :
forall e f M M' T,
red1_in_env e f ->
eq_typ e M M' T ->
eq_typ f M M' T.
intros.
apply typ_red_env_raw with e; trivial.
apply wf_red_env_trans with e; auto with sets.
eapply typ_wf; eauto.
Qed.
Lemma typ_red_env_trans :
forall e f M M' T,
clos_refl_trans _ red1_in_env e f ->
eq_typ e M M' T ->
eq_typ f M M' T.
induction 1; intros; eauto.
apply typ_red_env with x; trivial.
Qed.
Lemma eq_conv_red_env : forall e f x y T,
red1_in_env e f ->
eq_conv e x y T ->
eq_conv f x y T.
intros.
elim H0; intros.
red; apply rst_step.
apply typ_red_env with e; trivial.
red; apply rst_refl.
red; apply rst_sym; trivial.
red; apply rst_trans with y0; trivial.
Qed.
(* Admissible rules for conversion *)
Lemma eq_conv_prod_r : forall e T U U' s1 s2,
eq_typ e T T (Srt s1) ->
eq_conv (T::e) U U' (Srt s2) ->
eq_conv e (Prod T U) (Prod T U') (Srt s2).
red; induction 2; intros; eauto with sets.
apply eq_conv_red.
apply type_prod with s1; trivial.
apply rst_trans with (Prod T y); auto.
Qed.
Lemma eq_conv_prod_l : forall e T T' U s1 s2,
eq_conv e T T' (Srt s1) ->
eq_typ (T::e) U U (Srt s2) ->
eq_conv e (Prod T U) (Prod T' U) (Srt s2).
red; induction 1; intros; auto with sets.
apply eq_conv_red.
apply type_prod with s1; trivial.
apply eq_conv_sym.
apply IHclos_refl_sym_trans.
apply typ_red_env with (y :: e); trivial.
constructor 1 with s1; apply eq_conv_sym; trivial.
apply eq_conv_trans with (Prod y U); red; auto.
apply IHclos_refl_sym_trans2.
apply typ_red_env with (x :: e); trivial.
constructor 1 with s1; trivial.
Qed.
Lemma eq_conv_prod : forall e T T' U U' s1 s2,
eq_typ e T T (Srt s1) ->
eq_typ (T::e) U U (Srt s2) ->
eq_conv e T T' (Srt s1) ->
eq_conv (T :: e) U U' (Srt s2) ->
eq_conv e (Prod T U) (Prod T' U') (Srt s2).
intros.
red; apply rst_trans with (Prod T U').
apply eq_conv_prod_r with s1; trivial.
apply eq_conv_prod_l with s1; trivial.
elim eq_conv_inv with (1 := H2); intros.
subst U'; trivial.
destruct H3; trivial.
Qed.
Lemma eq_conv_abs_r : forall e T M M' U s1 s2,
eq_typ e T T (Srt s1) ->
eq_typ (T::e) U U (Srt s2) ->
eq_conv (T::e) M M' U ->
eq_conv e (Abs T M) (Abs T M') (Prod T U).
red; induction 3; intros; eauto with sets.
apply rst_step.
apply type_abs with U s1 s2; trivial.
apply rst_trans with (Abs T y); trivial.
Qed.
Lemma eq_conv_abs_l : forall e T T' M U s1 s2,
eq_conv e T T' (Srt s1) ->
eq_typ (T::e) U U (Srt s2) ->
eq_typ (T::e) M M U ->
eq_conv e (Abs T M) (Abs T' M) (Prod T U).
red; induction 1; intros; auto with sets.
apply rst_step.
apply type_abs with U s1 s2; trivial.
apply rst_sym.
apply eq_conv_conv with (Prod x U) s2.
apply IHclos_refl_sym_trans.
apply typ_red_env with (y :: e); trivial.
constructor 1 with s1; apply eq_conv_sym; trivial.
apply typ_red_env with (y :: e); trivial.
constructor 1 with s1; apply eq_conv_sym; trivial.
apply eq_conv_prod_l with s1; trivial.
apply typ_red_env with (y :: e); trivial.
constructor 1 with s1; apply eq_conv_sym; trivial.
apply rst_trans with (Abs y M); auto.
apply eq_conv_conv with (Prod y U) s2.
apply IHclos_refl_sym_trans2.
apply typ_red_env with (x :: e); trivial.
constructor 1 with s1; trivial.
apply typ_red_env with (x :: e); trivial.
constructor 1 with s1; trivial.
apply eq_conv_sym; trivial.
apply eq_conv_prod_l with s1; trivial.
Qed.
Lemma eq_conv_abs : forall e T T' M M' U s1 s2,
eq_typ e T T (Srt s1) ->
eq_typ (T::e) U U (Srt s2) ->
eq_typ (T::e) M M U ->
eq_conv e T T' (Srt s1) ->
eq_conv (T::e) M M' U ->
eq_conv e (Abs T M) (Abs T' M') (Prod T U).
intros.
red; apply rst_trans with (Abs T M').
apply eq_conv_abs_r with s1 s2; trivial.
apply eq_conv_abs_l with s1 s2; trivial.
apply eq_conv_inv2 with M; trivial.
Qed.
Lemma eq_conv_app_r : forall e u v v' Ur V s1 s2,
eq_typ e V V (Srt s1) ->
eq_typ e u u (Prod V Ur) ->
eq_typ (V::e) Ur Ur (Srt s2) ->
eq_conv e v v' V ->
eq_typ e v v V ->
eq_conv e (App (Prod u Ur) v) (App (Prod u Ur) v') (subst v Ur).
red; induction 4; intros; eauto with sets.
apply rst_step.
apply type_app with V V s1 s2; trivial.
apply rst_sym.
apply eq_conv_conv with (subst x Ur) s2.
apply IHclos_refl_sym_trans; trivial.
apply eq_conv_inv2 with y; trivial.
apply eq_conv_sym; trivial.
apply eq_conv_subst_ty_l with V; trivial.
apply rst_trans with (App (Prod u Ur) y); auto.
apply eq_conv_conv with (subst y Ur) s2.
apply IHclos_refl_sym_trans2; trivial.
apply eq_conv_inv2 with x; trivial.
apply eq_conv_sym.
apply eq_conv_subst_ty_l with V; trivial.
Qed.
Lemma eq_conv_app_m : forall e u v T Ur Ur' s1 s2,
eq_typ e v v T ->
eq_typ e T T (Srt s1) ->
eq_conv (T::e) Ur Ur' (Srt s2) ->
eq_typ e u u (Prod T Ur) ->
eq_typ (T::e) Ur Ur (Srt s2) ->
eq_conv e (App (Prod u Ur) v) (App (Prod u Ur') v) (subst v Ur).
red; induction 3; intros; auto with sets.
apply rst_step.
apply type_app with T T s1 s2; trivial.
apply rst_sym.
apply eq_conv_conv with (subst v x) s2.
apply IHclos_refl_sym_trans; trivial.
apply type_eq_conv with (Prod T y) s2; trivial.
apply eq_conv_prod_r with s1; trivial.
apply eq_conv_sym; trivial.
apply eq_conv_inv2 with y; trivial.
apply eq_conv_sym; trivial.
change (Srt s2) with (subst v (Srt s2)).
apply eq_conv_subst_r with T; trivial.
apply rst_trans with (App (Prod u y) v); auto.
apply eq_conv_conv with (subst v y) s2.
apply IHclos_refl_sym_trans2; trivial.
apply type_eq_conv with (Prod T x) s2; trivial.
apply eq_conv_prod_r with s1; trivial.
apply eq_conv_inv2 with x; trivial.
change (Srt s2) with (subst v (Srt s2)).
apply eq_conv_subst_r with T; trivial.
apply eq_conv_sym; trivial.
Qed.
Lemma eq_conv_app_l : forall e u u' v T Ur s1 s2,
eq_typ e v v T ->
eq_typ e T T (Srt s1) ->
eq_typ (T::e) Ur Ur (Srt s2) ->
eq_conv e u u' (Prod T Ur) ->
eq_conv e (App (Prod u Ur) v) (App (Prod u' Ur) v) (subst v Ur).
red; induction 4; intros; auto with sets.
apply rst_step.
apply type_app with T T s1 s2; trivial.
apply rst_trans with (App (Prod y Ur) v); auto.
Qed.
Lemma eq_conv_app : forall e u u' v v' Ur Ur' V s1 s2,
eq_typ e v v V ->
eq_typ e V V (Srt s1) ->
eq_typ e u u (Prod V Ur) ->
eq_typ (V::e) Ur Ur (Srt s2) ->
eq_conv e u u' (Prod V Ur) ->
eq_conv e v v' V ->
eq_conv (V :: e) Ur Ur' (Srt s2) ->
eq_conv e (App (Prod u Ur) v) (App (Prod u' Ur') v') (subst v Ur).
intros.
red; apply rst_trans with (App (Prod u' Ur) v).
apply eq_conv_app_l with V s1 s2; trivial.
apply rst_trans with (App (Prod u' Ur') v).
apply eq_conv_app_m with V s1 s2; trivial.
apply eq_conv_inv2 with u; trivial.
apply eq_conv_conv with (subst v Ur') s2.
apply eq_conv_app_r with V s1 s2; trivial.
apply type_eq_conv with (Prod V Ur) s2.
apply eq_conv_inv2 with u; trivial.
apply eq_conv_prod_r with s1; trivial.
apply eq_conv_inv2 with Ur; trivial.
apply eq_conv_sym.
change (Srt s2) with (subst v (Srt s2)).
apply eq_conv_subst_r with V; trivial.
Qed.
Section Equivalence1.
Fixpoint unmark_app (t:term) : term :=
match t with
| App (Prod u _) v => App (unmark_app u) (unmark_app v)
| Abs T M => Abs (unmark_app T) (unmark_app M)
| Prod T U => Prod (unmark_app T) (unmark_app U)
| _ => t
end.
Definition unmark_env (e:env) : env := map unmark_app e.
Lemma unmark_env_cons_inv : forall e1 T e,
unmark_env e1 = T :: e ->
exists2 T', unmark_app T' = T &
exists2 e', unmark_env e' = e & e1 = T' :: e'.
intros.
destruct e1; try discriminate.
unfold unmark_env in *.
simpl in H.
injection H; intros.
exists t; trivial.
exists e1; trivial.
Qed.
Lemma unmark_sort_inv : forall T s, unmark_app T = Srt s -> T = Srt s.
destruct T; try (intros; discriminate).
auto.
destruct T1; intros; discriminate.
Qed.
Lemma unmark_prod_inv : forall T A B,
unmark_app T = Prod A B ->
exists2 A', unmark_app A' = A &
exists2 B', unmark_app B' = B & T = Prod A' B'.
destruct T; try (intros; discriminate).
destruct T1; intros; discriminate.
simpl; intros.
injection H; intros.
exists T1; trivial.
exists T2; trivial.
Qed.
Lemma unmark_lift : forall n t k,
unmark_app (lift_rec n t k) = lift_rec n (unmark_app t) k.
induction t; simpl; intros; trivial.
destruct (le_gt_dec k n0); simpl; trivial.
rewrite IHt1; rewrite IHt2; reflexivity.
destruct t1; simpl in *; auto.
destruct (le_gt_dec k n0); trivial.
assert (H := IHt1 k); clear IHt1.
injection H; intros.
rewrite IHt2; rewrite H1; reflexivity.
rewrite IHt1; rewrite IHt2; reflexivity.
Qed.
Lemma unmark_item_lift : forall t e n,
item_lift t e n -> item_lift (unmark_app t) (unmark_env e) n.
destruct 1; intros.
subst t.
unfold lift.
rewrite unmark_lift.
exists (unmark_app x); trivial.
unfold unmark_env.
elim H0; simpl; intros; auto with coc.
Qed.
Lemma unmark_env_item : forall e e' n t,
unmark_env e' = e ->
item_lift t e n ->
exists2 t', unmark_app t' = t & item_lift t' e' n.
destruct 2; intros.
subst t.
generalize e' H; clear e' H.
induction H1; intros.
destruct e'; try discriminate.
injection H; intros.
exists (lift 1 t).
unfold lift; rewrite unmark_lift.
rewrite H1; trivial.
exists t; auto.
destruct e'; try discriminate.
injection H; intros.
elim (IHitem _ H0); intros.
exists (lift 1 x0).
unfold lift; rewrite unmark_lift; rewrite H3.
change (lift 1 (lift (S n) x) = lift (S (S n)) x).
rewrite <- simpl_lift.
trivial.
unfold lift; apply ins_item_lift_ge with t e'; auto with coc arith.
Qed.
Lemma unmark_subst : forall N e M M' T,
eq_typ e M M' T ->
forall k,
unmark_app (subst_rec N M k) = subst_rec (unmark_app N) (unmark_app M) k.
induction 1; simpl; intros; auto.
destruct (lt_eq_lt_dec k v) as [[_| _]| _]; trivial.
unfold lift; apply unmark_lift.
rewrite IHeq_typ1; rewrite IHeq_typ3; trivial.
rewrite IHeq_typ1; rewrite IHeq_typ3; trivial.
rewrite IHeq_typ1; rewrite IHeq_typ2; trivial.
rewrite IHeq_typ1; rewrite IHeq_typ2; rewrite IHeq_typ3; trivial.
Qed.
Lemma unmark_subst0 : forall N e M M' T,
eq_typ e M M' T ->
unmark_app (subst N M) = subst (unmark_app N) (unmark_app M).
Proof (fun N e M M' T H => unmark_subst N e M M' T H 0).
Lemma unmark_subst2 : forall N e M M' T,
eq_typ e M M' T ->
forall k,
unmark_app (subst_rec N M' k) = subst_rec (unmark_app N) (unmark_app M') k.
intros.
apply unmark_subst with e M' T.
apply typ_refl2 with M; trivial.
Qed.
Lemma eq_typ_par_red0 : forall e M M' T,
eq_typ e M M' T -> par_red1 (unmark_app M) (unmark_app M').
induction 1; intros; simpl; auto with coc.
unfold subst.
rewrite unmark_subst2 with (1 := H1).
fold (subst (unmark_app N') (unmark_app M')).
auto with coc.
Qed.
Lemma eq_typ_typ : forall e M M' T,
eq_typ e M M' T ->
typ (unmark_env e) (unmark_app M) (unmark_app T).
intros.
elim H using eq_typ_mind with (P0 := fun e => Types.wf (unmark_env e));
simpl; intros; auto with coc.
constructor; trivial.
constructor; trivial.
apply unmark_item_lift; trivial.
apply Types.type_abs with s1 s2; trivial.
rewrite unmark_subst0 with (1 := H6).
apply Types.type_app with (unmark_app V); simpl; trivial.
apply Types.type_prod with s1; trivial.
rewrite unmark_subst0 with (1 := H6).
apply Types.type_app with (unmark_app T0); trivial.
apply Types.type_abs with s1 s2; trivial.
apply type_conv with (unmark_app T0) s; trivial.
apply red_conv.
apply par_red_red.
constructor 1.
apply eq_typ_par_red0 with (1 := H2).
apply subject_reduction with (unmark_app T0); trivial.
apply par_red_red.
constructor 1.
apply eq_typ_par_red0 with (1 := H2).
apply type_conv with (unmark_app T') s; trivial.
apply sym_conv.
apply red_conv.
apply par_red_red.
constructor 1.
apply eq_typ_par_red0 with (1 := H2).
constructor.
unfold unmark_env; simpl.
apply Types.wf_var with s; trivial.
Qed.
End Equivalence1.