-
Notifications
You must be signed in to change notification settings - Fork 2
/
GenModel.v
1204 lines (1013 loc) · 26.9 KB
/
GenModel.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import basic.
Require Import Models TypModels List.
(** A general model construction of a model of CC given an
abstract model. *)
Module MakeModel(M : CC_Model) <: Judge.
Import M.
Lemma eq_fun_sym : forall x f1 f2, eq_fun x f1 f2 -> eq_fun x f2 f1.
Proof.
unfold eq_fun in |- *; intros.
symmetry in |- *.
apply H.
rewrite <- H1; trivial.
symmetry in |- *; trivial.
Qed.
Lemma eq_fun_trans : forall x f1 f2 f3,
eq_fun x f1 f2 -> eq_fun x f2 f3 -> eq_fun x f1 f3.
Proof.
unfold eq_fun in |- *; intros.
transitivity (f2 y1); auto.
apply H; trivial.
reflexivity.
Qed.
(** Valuations *)
Module Xeq.
Definition t := X.
Definition eq := eqX.
Definition eq_equiv : Equivalence eq := eqX_equiv.
Existing Instance eq_equiv.
End Xeq.
Require Import VarMap.
Module V := VarMap.Make(Xeq).
Notation val := V.map.
Notation eq_val := V.eq_map.
Definition vnil : val := V.nil props.
Import V.
Existing Instance cons_morph.
Existing Instance cons_morph'.
Hint Unfold eq_val.
(** * Pseudo-Terms *)
Module T.
Definition term :=
option {f:val -> X|Proper (eq_val ==> eqX) f}.
Definition eq_term (x y:term) :=
match x, y with
| Some f, Some g => (eq_val ==> eqX)%signature (proj1_sig f) (proj1_sig g)
| None, None => True
| _, _ => False
end.
Global Instance eq_term_refl : Reflexive eq_term.
red; intros.
destruct x; simpl; trivial.
destruct s; trivial.
Qed.
Global Instance eq_term_sym : Symmetric eq_term.
red; intros.
destruct x; destruct y; simpl in *; auto.
symmetry; trivial.
Qed.
Global Instance eq_term_trans : Transitive eq_term.
red; intros.
destruct x; destruct y; try contradiction; destruct z; simpl in *; auto.
transitivity (proj1_sig s0); trivial.
Qed.
(** Denotation as value *)
Definition int (t:term) (i:val) : X :=
match t with
| Some f => proj1_sig f (fun k => i k)
| None => props
end.
Global Instance int_morph : Proper (eq_term ==> eq_val ==> eqX) int.
unfold int; do 3 red; intros.
destruct x; destruct y; simpl in *; (contradiction||reflexivity||auto).
Qed.
Definition Op1 (f:X->X) {fm:Proper(eqX==>eqX) f} (t:term) : term.
(* begin show *)
left; exists (fun i => f (int t i)).
(*end show*)
do 2 red; intros.
apply fm; apply int_morph; auto with *.
Defined.
Definition Op2 (f:X->X->X) {fm:Proper(eqX==>eqX==>eqX) f} (t1 t2:term) : term.
(* begin show *)
left; exists (fun i => f (int t1 i) (int t2 i)).
(*end show*)
do 2 red; intros.
apply fm; apply int_morph; auto with *.
Defined.
(** Denotation as type *)
Definition el (t:term) (i:val) (x:X) :=
match t with
| Some _ => x ∈ int t i
| None => True
end.
Global Instance el_morph : Proper (eq_term ==> eq_val ==> eqX ==> iff) el.
apply morph_impl_iff3; auto with *.
unfold el; do 5 red; intros.
destruct y; trivial; destruct x; (contradiction||simpl in *).
rewrite <- H1.
rewrite <- (H (fun k => x0 k) (fun k => y0 k)); auto.
Qed.
Lemma in_int_el : forall i x T,
x ∈ int T i -> el T i x.
destruct T as [(T,Tm)|]; simpl; trivial.
Qed.
Lemma in_int_not_kind T i x :
el T i x ->
T <> None ->
x ∈ int T i.
destruct T as [(T,Tm)|]; simpl; intros; trivial.
elim H0; trivial.
Qed.
(** Injecting sets into the model *)
Definition cst (x:X) : term.
(*begin show*)
left; exists (fun _ => x).
(*end show*)
do 2 red; reflexivity.
Defined.
(** General substitutions *)
Record sub_ := mkSub {
sub_f :> val -> val;
sub_m : Proper (eq_val ==> eq_val) sub_f
}.
Definition sub := sub_.
Existing Instance sub_m.
Definition eq_sub (s1 s2:sub) :=
(eq_val==>eq_val)%signature s1 s2.
Instance eq_sub_equiv : Equivalence eq_sub.
split; red; intros.
apply x.
do 2 red; intros.
symmetry; apply H.
symmetry; trivial.
do 2 red; intros.
transitivity (y y0); auto.
transitivity (y x0); auto.
apply y; symmetry; trivial.
Qed.
Definition Sub (t:term) (s:sub) : term.
(* begin show *)
destruct t as [(t,tm)|];
[left; exists (fun i => t (s i))|right].
(* end show *)
do 2 red; intros; auto.
apply tm; apply sub_m; trivial.
Defined.
Instance Sub_morph : Proper (eq_term ==> eq_sub ==> eq_term) Sub.
do 3 red; intros.
destruct x as [(x,xm)|]; destruct y as [(y,ym)|];simpl in *; try contradiction; trivial.
red; intros.
apply H.
apply H0; trivial.
Qed.
Definition sub_id : sub.
exists (fun x => x).
do 2 red; auto.
Defined.
Lemma eq_sub_id t : eq_term (Sub t sub_id) t.
destruct t as [(t,tm)|]; simpl; trivial.
Qed.
Definition sub_comp (s1 s2:sub) : sub.
exists (fun i => s1 (s2 i)).
do 2 red; intros.
apply sub_m.
apply sub_m.
trivial.
Defined.
Definition sub_lift (m:nat) (s:sub) : sub.
exists (V.lams m s).
do 2 red; intros.
apply V.lams_morph; trivial.
apply sub_m.
Defined.
Instance sub_lift_morph : Proper (eq ==> eq_sub ==> eq_sub) sub_lift.
do 4 red; simpl; intros.
red; intros.
apply V.lams_morph; auto with *.
Qed.
Definition sub_shift (n:nat) : sub.
exists (V.shift n); auto with *.
apply V.shift_morph; trivial.
Defined.
Definition sub_cons (t:term) (s:sub) : sub.
exists (fun i => V.cons (int t i) (s i)).
do 2 red; intros.
apply V.cons_morph.
apply int_morph; auto with *.
apply sub_m; trivial.
Defined.
Lemma eq_sub_comp t s1 s2 :
Sub (Sub t s1) s2 = Sub t (sub_comp s1 s2).
destruct t as [(t,tm)|]; simpl; reflexivity.
Qed.
Lemma sub_lift_split m n s :
eq_sub (sub_lift (m+n) s) (sub_lift m (sub_lift n s)).
red; simpl.
red; intros.
rewrite <- V.lams_split.
apply V.lams_morph; trivial.
apply sub_m.
apply sub_m.
Qed.
Lemma sub_lift0 s : eq_sub (sub_lift 0 s) s.
red; red; intros.
simpl.
rewrite V.lams0.
apply sub_m; trivial.
Qed.
Lemma int_Sub_eq T s i :
int (Sub T s) i = int T (s i).
intros; destruct T as [(T,Tm)|]; simpl; reflexivity.
Qed.
Lemma sub_nk t s :
t <> None <->
Sub t s <> None.
destruct t as [(t,tm)|]; simpl; auto with *.
split; intros; discriminate.
Qed.
(** Relocations *)
Section Lift.
Definition lift_rec (n m:nat) (t:term) : term.
(*begin show*)
destruct t as [(t,tm)|]; [left|exact None].
exists (fun i => t (V.lams m (V.shift n) i)).
(*end show*)
do 2 red; intros.
rewrite H; reflexivity.
Defined.
Global Instance lift_rec_morph n k : Proper (eq_term ==> eq_term) (lift_rec n k).
do 3 red; intros.
destruct x as [(x,xm)|]; destruct y as [(y,ym)|]; simpl in H|-*; try contradiction; trivial.
red; intros.
apply H.
rewrite H0; reflexivity.
Qed.
Lemma lift_rec_equiv n k t :
eq_term (lift_rec n k t) (Sub t (sub_lift k (sub_shift n))).
destruct t as [(t,tm)|]; simpl; trivial.
red; intros.
rewrite H.
reflexivity.
Qed.
Lemma lift_rec_nk n t k :
t <> None <->
lift_rec n k t <> None.
destruct t as [(t,tm)|]; simpl; auto with *.
split; intros; discriminate.
Qed.
Definition lift1 n := lift_rec n 1.
Lemma lift10: forall T, eq_term (lift1 0 T) T.
unfold lift1.
destruct T as [(T,Tm)|]; simpl; trivial.
red; intros.
apply Tm; do 2 red; intros.
unfold V.lams, V.shift; destruct a; simpl.
apply H.
replace (a-0) with a; auto with arith.
Qed.
Lemma int_lift_rec_eq : forall n k T i,
int (lift_rec n k T) i == int T (V.lams k (V.shift n) i).
intros; destruct T as [(T,Tm)|]; simpl; reflexivity.
Qed.
Definition lift n := lift_rec n 0.
Global Instance lift_morph : forall k, Proper (eq_term ==> eq_term) (lift k).
do 3 red; simpl; intros.
destruct x as [(x,xm)|]; destruct y as [(y,ym)|];
simpl in *; (contradiction||trivial).
red; intros.
apply H.
rewrite H0; reflexivity.
Qed.
Lemma lift0_term T : eq_term (lift 0 T) T.
destruct T as [(T,Tm)|]; simpl; trivial.
red; intros.
apply Tm.
rewrite V.lams0.
rewrite V.shift0.
do 2 red; apply H.
Qed.
Lemma eq_term_liftS n t : eq_term (lift (S n) t) (lift 1 (lift n t)).
destruct t as [(t,tm)|]; simpl; trivial.
red; intros.
apply tm.
rewrite V.lams0.
rewrite V.lams0.
rewrite V.lams0.
rewrite V.shiftS_split.
apply V.shift_morph; trivial.
apply V.shift_morph; trivial.
Qed.
Lemma simpl_int_lift : forall i n x T,
int (lift (S n) T) (V.cons x i) == int (lift n T) i.
intros.
destruct T as [(T,Tm)|]; simpl; auto with *.
apply Tm.
red; red; intros; reflexivity.
Qed.
Lemma simpl_int_lift1 : forall i x T,
int (lift 1 T) (V.cons x i) == int T i.
intros.
rewrite simpl_int_lift; rewrite lift0_term; reflexivity.
Qed.
Lemma simpl_lift1 : forall i n x y T,
int (lift1 (S n) T) (V.cons x (V.cons y i)) == int (lift1 n T) (V.cons x i).
unfold lift1; simpl; intros.
do 2 rewrite int_lift_rec_eq.
repeat rewrite <- V.cons_lams.
reflexivity.
do 2 red; intros; rewrite H; reflexivity.
do 2 red; intros; rewrite H; reflexivity.
Qed.
Lemma eqterm_lift_cst : forall n k c,
eq_term (lift_rec n k (cst c)) (cst c).
red; simpl; intros.
red; reflexivity.
Qed.
End Lift.
(** Substitution *)
Section Substitution.
Definition subst_rec (arg:term) (m:nat) (t:term) : term.
(*begin show*)
destruct t as [(body,bm)|]; [left|right].
exists (fun i => body (V.lams m (V.cons (int arg (V.shift m i))) i)).
(*end show*)
do 2 red; intros.
rewrite H; reflexivity.
Defined.
Global Instance subst_rec_morph : Proper (eq_term ==> eq ==> eq_term ==> eq_term) subst_rec.
do 4 red; intros.
destruct x1 as [(x1,x1m)|]; destruct y1 as [(y1,y1m)|]; simpl in H1|-*; try contradiction; trivial.
red; intros.
apply H1.
rewrite H; rewrite H0; rewrite H2; reflexivity.
Qed.
Lemma subst_rec_equiv a k t :
eq_term (subst_rec a k t) (Sub t (sub_lift k (sub_cons a sub_id))).
destruct t as [(t,tm)|]; simpl; trivial.
red; intros.
apply tm.
unfold V.lams; do 2 red; intros.
destruct (le_gt_dec k a0); auto with *.
rewrite H; reflexivity.
Qed.
Lemma subst_rec_nk a t k :
t <> None <->
subst_rec a k t <> None.
destruct t as [(t,tm)|]; simpl; auto with *.
split; intros; discriminate.
Qed.
Lemma int_subst_rec_eq : forall arg k T i,
int (subst_rec arg k T) i == int T (V.lams k (V.cons (int arg (V.shift k i))) i).
intros; destruct T as [(T,Tm)|]; simpl; reflexivity.
Qed.
Definition subst arg := subst_rec arg 0.
Global Instance subst_morph : Proper (eq_term ==> eq_term ==> eq_term) subst.
do 3 red; simpl; intros.
destruct x0 as [(x0,xm0)|]; destruct y0 as [(y0,ym0)|];
simpl in *; (contradiction||trivial).
red; intros.
apply H0.
rewrite H; rewrite H1; reflexivity.
Qed.
Lemma int_subst_eq : forall N M i,
int (subst N M) i == int M (V.cons (int N i) i).
destruct M as [(M,Mm)|]; simpl; intros.
2:reflexivity.
rewrite V.lams0.
rewrite V.shift0.
reflexivity.
Qed.
Lemma el_subst_eq : forall N M i x,
el (subst N M) i x <-> el M (V.cons (int N i) i) x.
destruct M as [(M,Mm)|]; simpl; intros.
2:reflexivity.
rewrite V.lams0.
rewrite V.shift0.
reflexivity.
Qed.
End Substitution.
(** Syntax of the Calculus of Constructions *)
Definition prop := cst props.
Definition kind : term := None.
Definition Ref (n:nat) : term.
(*begin show*)
left; exists (fun i => i n).
(*end show*)
do 2 red; simpl; auto.
Defined.
Definition App (u v:term) : term.
(*begin show*)
left; exists (fun i => app (int u i) (int v i)).
(*end show*)
do 2 red; simpl; intros.
rewrite H; reflexivity.
Defined.
Global Instance App_morph : Proper (eq_term ==> eq_term ==> eq_term) App.
unfold App; do 3 red; simpl; intros.
red; intros.
rewrite H; rewrite H0; rewrite H1; reflexivity.
Qed.
Lemma eq_Sub_app a b s :
eq_term (Sub (App a b) s) (App (Sub a s) (Sub b s)).
red; simpl.
red; intros.
apply app_ext.
rewrite H.
rewrite int_Sub_eq; reflexivity.
rewrite H.
rewrite int_Sub_eq; reflexivity.
Qed.
Lemma eqterm_subst_App : forall N u v,
eq_term (subst N (App u v)) (App (subst N u) (subst N v)).
red; simpl; intros.
red; intros.
unfold subst.
do 2 rewrite int_subst_rec_eq.
rewrite H.
reflexivity.
Qed.
Definition Abs (A M:term) : term.
(*begin show*)
left; exists (fun i => lam (int A i) (fun x => int M (V.cons x i))).
(*end show*)
do 2 red; simpl; intros.
apply lam_ext.
rewrite H; reflexivity.
(**)
red; intros.
rewrite H; rewrite H1; reflexivity.
Defined.
Global Instance Abs_morph : Proper (eq_term ==> eq_term ==> eq_term) Abs.
unfold Abs; do 5 red; simpl; intros.
apply lam_ext.
apply int_morph; auto.
red; intros.
rewrite H0; rewrite H1; rewrite H3; reflexivity.
Qed.
Lemma eq_sub_Abs a b s :
eq_term (Sub (Abs a b) s) (Abs (Sub a s) (Sub b (sub_lift 1 s))).
red; simpl.
red; intros.
apply lam_ext.
rewrite H.
rewrite int_Sub_eq; reflexivity.
red; intros.
rewrite int_Sub_eq; simpl.
rewrite <- V.cons_lams.
2:apply sub_m.
rewrite V.lams0.
apply int_morph; auto with *.
apply V.cons_morph; auto.
apply sub_m; trivial.
Qed.
Lemma eq_lift_abs : forall n A B k,
eq_term (lift_rec n k (Abs A B))
(Abs (lift_rec n k A) (lift_rec n (S k) B)).
do 5 red; simpl; intros.
apply lam_ext; intros.
rewrite int_lift_rec_eq.
rewrite H; reflexivity.
red; intros.
rewrite int_lift_rec_eq.
rewrite <- V.cons_lams; auto with *.
rewrite H1; rewrite H; reflexivity.
do 2 red; intros.
rewrite H2; reflexivity.
Qed.
Definition Prod (A B:term) : term.
(*begin show*)
left; exists (fun i => prod (int A i) (fun x => int B (V.cons x i))).
(*end show*)
do 2 red; simpl; intros.
apply prod_ext.
rewrite H; reflexivity.
(**)
red; intros.
rewrite H; rewrite H1; reflexivity.
Defined.
Global Instance Prod_morph : Proper (eq_term ==> eq_term ==> eq_term) Prod.
unfold Prod; do 5 red; simpl; intros.
apply prod_ext.
apply int_morph; auto.
red; intros.
rewrite H0; rewrite H1; rewrite H3; reflexivity.
Qed.
Lemma eq_Sub_prod s A B :
eq_term (Sub (Prod A B) s) (Prod (Sub A s) (Sub B (sub_lift 1 s))).
simpl.
red; intros.
apply prod_ext.
rewrite int_Sub_eq.
rewrite H; reflexivity.
red; intros.
rewrite int_Sub_eq.
simpl; rewrite <- V.cons_lams.
apply int_morph; auto with *.
apply V.cons_morph; trivial.
rewrite V.lams0; apply sub_m; trivial.
apply sub_m.
Qed.
Lemma eq_lift_prod : forall n A B k,
eq_term (lift_rec n k (Prod A B))
(Prod (lift_rec n k A) (lift_rec n (S k) B)).
do 5 red; simpl; intros.
apply prod_ext; intros.
rewrite int_lift_rec_eq.
rewrite H; reflexivity.
red; intros.
rewrite int_lift_rec_eq.
rewrite <- V.cons_lams; auto with *.
rewrite H1; rewrite H; reflexivity.
do 2 red; intros.
rewrite H2; reflexivity.
Qed.
Lemma eq_subst_prod : forall u A B k,
eq_term (subst_rec u k (Prod A B))
(Prod (subst_rec u k A) (subst_rec u (S k) B)).
do 5 red; simpl; intros.
apply prod_ext; intros.
rewrite int_subst_rec_eq.
rewrite H; reflexivity.
red; intros.
rewrite int_subst_rec_eq.
rewrite <- V.cons_lams; auto with *.
rewrite H1; rewrite H; reflexivity.
do 2 red; intros.
rewrite H2; reflexivity.
Qed.
End T.
Import T.
(** * Environments *)
Definition env := list term.
Definition val_ok (e:env) (i:val) :=
forall n T, nth_error e n = value T -> el (lift (S n) T) i (i n).
Lemma val_ok_shift1 e ty i :
val_ok (ty::e) i ->
val_ok e (V.shift 1 i).
intros iok n T itm.
generalize (iok (S n) T itm).
destruct T as [(T,Tm)|]; simpl; auto.
Qed.
Instance val_ok_morph : Proper (list_eq eq_term ==> eq_val ==> iff) val_ok.
apply morph_impl_iff2; auto with *.
do 4 red.
induction 1; simpl; intros.
red; intros.
destruct n; discriminate.
red; intros.
destruct n; simpl in *.
generalize (H2 0 _ eq_refl).
injection H3; intros; subst T.
revert H5; apply el_morph; symmetry; auto.
apply lift_morph; trivial.
red in IHForall2.
apply (V.shift_morph 1 _ eq_refl) in H1.
apply val_ok_shift1 in H2.
specialize IHForall2 with (1:=H1)(2:=H2)(3:=H3).
destruct T as [(T,Tm)|]; simpl in *; auto.
Qed.
Lemma vcons_add_var0 : forall e T i x,
val_ok e i -> el T i x -> val_ok (T::e) (V.cons x i).
unfold val_ok; simpl; intros.
destruct n; simpl in *.
injection H1; clear H1; intro; subst; simpl in *.
destruct T0 as [(T0,Tm)|]; simpl in *; trivial.
rewrite V.lams0; assumption.
apply H in H1; simpl in H1; trivial.
destruct T0 as [(T0,Tm)|]; simpl in *; trivial.
Qed.
Lemma vcons_add_var : forall e T i x,
val_ok e i -> x ∈ int T i -> val_ok (T::e) (V.cons x i).
intros.
apply vcons_add_var0; trivial.
destruct T as[(T,Tm)|]; simpl in *; trivial.
Qed.
Lemma add_var_eq_fun : forall T U U' i,
(forall x, el T i x -> int U (V.cons x i) == int U' (V.cons x i)) ->
eq_fun (int T i)
(fun x => int U (V.cons x i))
(fun x => int U' (V.cons x i)).
red; intros.
rewrite <- H1.
apply H.
destruct T as [(T,Tm)|]; simpl in *; trivial.
Qed.
(** * Judgements *)
Module J.
(** Typing *)
Definition typ (e:env) (M T:term) :=
forall i, val_ok e i -> el T i (int M i).
(** Equality *)
Definition eq_typ (e:env) (M M':term) :=
forall i, val_ok e i -> int M i == int M' i.
(** Subtyping *)
Definition sub_typ (e:env) (M M':term) :=
forall i x, val_ok e i -> x ∈ int M i -> x ∈ int M' i.
(** Alternative equality (with kind=kind) *)
Definition eq_typ' e M M' :=
eq_typ e M M' /\ match M,M' with None,None => True|Some _,Some _=>True|_,_=>False end.
(** Subtyping as inclusion of values *)
Definition sub_typ' (e:env) (M M':term) :=
forall i x, val_ok e i -> el M i x -> el M' i x.
Definition typ_sub (e:env) (s:sub) (f:env) :=
forall i, val_ok e i -> val_ok f (s i).
Global Instance typ_morph : forall e, Proper (eq_term ==> eq_term ==> iff) (typ e).
intros.
apply morph_impl_iff2; auto with *.
unfold typ; do 4 red; intros.
rewrite <- H; rewrite <- H0; auto.
Qed.
Global Instance eq_typ_morph : forall e, Proper (eq_term ==> eq_term ==> iff) (eq_typ e).
intros.
apply morph_impl_iff2; auto with *.
unfold eq_typ; do 4 red; intros.
rewrite <- H; rewrite <- H0; auto.
Qed.
(*
Instance eq_typ'_morph : forall e, Proper (eq_term ==> eq_term ==> iff) (eq_typ' e).
unfold eq_typ'; split; simpl; intros.
destruct x; destruct y; try contradiction.
rewrite <- H; rewrite <- H0; auto.
rewrite H; rewrite H0; auto.
Qed.
*)
Global Instance sub_typ_morph : forall e, Proper (eq_term ==> eq_term ==> iff) (sub_typ e).
intros.
apply morph_impl_iff2; auto with *.
unfold sub_typ; do 4 red; intros.
rewrite <- H in H3; rewrite <- H0; auto.
Qed.
Global Instance sub_typ'_morph : forall e, Proper (eq_term ==> eq_term ==> iff) (sub_typ' e).
intros.
apply morph_impl_iff2; auto with *.
unfold sub_typ'; do 4 red; intros.
rewrite <- H in H3; rewrite <- H0; auto.
Qed.
Instance typ_sub_morph :
Proper (list_eq eq_term ==> eq_sub ==> list_eq eq_term ==> iff) typ_sub.
do 4 red; intros.
unfold typ_sub.
apply fa_morph; intros i.
apply impl_morph.
apply val_ok_morph; trivial.
reflexivity.
intros.
apply val_ok_morph; trivial.
apply H0; reflexivity.
Qed.
End J.
Import J.
Lemma typ_elim e M T i :
typ e M T ->
T <> kind ->
val_ok e i ->
int M i ∈ int T i.
intros.
apply H in H1.
apply in_int_not_kind in H1; trivial.
Qed.
(** * Inference rules *)
Module R.
(** Equality rules *)
Lemma refl : forall e M, eq_typ e M M.
red; simpl; reflexivity.
Qed.
Lemma sym : forall e M M', eq_typ e M M' -> eq_typ e M' M.
unfold eq_typ; symmetry; auto.
Qed.
Lemma trans : forall e M M' M'', eq_typ e M M' -> eq_typ e M' M'' -> eq_typ e M M''.
unfold eq_typ; intros.
transitivity (int M' i); auto.
Qed.
Global Instance eq_typ_equiv : forall e, Equivalence (eq_typ e).
split.
exact (refl e).
exact (sym e).
exact (trans e).
Qed.
Lemma eq_typ_app : forall e M M' N N',
eq_typ e M M' ->
eq_typ e N N' ->
eq_typ e (App M N) (App M' N').
unfold eq_typ; simpl; intros.
apply app_ext; auto.
Qed.
Lemma eq_typ_abs : forall e T T' M M',
eq_typ e T T' ->
eq_typ (T::e) M M' ->
eq_typ e (Abs T M) (Abs T' M').
Proof.
unfold eq_typ; simpl; intros.
apply lam_ext; auto.
apply add_var_eq_fun; trivial.
intros.
apply H0.
apply vcons_add_var0; trivial.
Qed.
Lemma eq_typ_prod : forall e T T' U U',
eq_typ e T T' ->
eq_typ (T::e) U U' ->
eq_typ e (Prod T U) (Prod T' U').
unfold eq_typ; simpl; intros.
apply prod_ext; auto.
apply add_var_eq_fun; trivial.
intros.
apply H0.
apply vcons_add_var0; trivial.
Qed.
Lemma eq_typ_beta : forall e T M M' N N',
eq_typ (T::e) M M' ->
eq_typ e N N' ->
typ e N T -> (* Typed reduction! *)
T <> kind ->
eq_typ e (App (Abs T M) N) (subst N' M').
Proof.
unfold eq_typ, typ, App, Abs; simpl; intros.
rewrite int_subst_eq.
assert (eq_fun (int T i) (fun x => int M (V.cons x i)) (fun x => int M (V.cons x i))).
apply add_var_eq_fun with (T:=T); intros; trivial; reflexivity.
assert (int N i ∈ int T i).
destruct T as [(T,Tm)|]; [clear H2;simpl in *;auto|elim H2;reflexivity].
rewrite beta_eq; auto.
rewrite <- H0; auto.
apply H.
apply vcons_add_var0; simpl; auto.
Qed.
(** Typing rules *)
Lemma typ_prop : forall e, typ e prop kind.
red; simpl; trivial.
Qed.
Lemma typ_var : forall e n T,
nth_error e n = value T -> typ e (Ref n) (lift (S n) T).
unfold lift; red; simpl; intros.
apply H0 in H.
destruct T; simpl in *; trivial.
Qed.
Lemma typ_app : forall e u v V Ur,
typ e v V ->
typ e u (Prod V Ur) ->
V <> kind ->
typ e (App u v) (subst v Ur).
unfold typ, App, Prod; simpl;
intros e u v V Ur ty_v ty_u not_tops i is_val.
specialize (ty_v _ is_val).
specialize (ty_u _ is_val).
destruct V as [(V,Vm)|]; [clear not_tops;simpl in *|elim not_tops;reflexivity].
apply in_int_el.
rewrite int_subst_eq.
apply prod_elim with (dom := V (fun k => i k)) (F:=fun x => int Ur (V.cons x i)); trivial.
red; intros.
rewrite H0; reflexivity.
Qed.
Lemma typ_abs : forall e T M U,
typ (T :: e) M U ->
U <> kind ->
typ e (Abs T M) (Prod T U).
Proof.
unfold typ, Abs, Prod; simpl; intros e T M U ty_M not_tops i is_val.
apply prod_intro.
apply add_var_eq_fun; trivial; intros; reflexivity.
apply add_var_eq_fun; trivial; intros; reflexivity.
intros.
destruct U as[U|]; [clear not_tops; simpl in *|elim not_tops; reflexivity].
apply ty_M.
apply vcons_add_var; trivial.
Qed.
Lemma typ_beta : forall e T M N U,
typ e N T ->
typ (T::e) M U ->
T <> kind ->
U <> kind ->
typ e (App (Abs T M) N) (subst N U).
Proof.
intros.
apply typ_app with T; trivial.
apply typ_abs; trivial.
Qed.
Lemma typ_prod : forall e T U s2,
s2 = kind \/ s2 = prop ->
typ (T :: e) U s2 ->
typ e (Prod T U) s2.
Proof.
unfold typ, Prod; simpl; red; intros e T U s2 is_srt ty_U i is_val.
destruct s2 as [(s2,sm)|]; trivial; simpl in *.
destruct is_srt as [is_srt|is_srt];
[discriminate|injection is_srt;clear is_srt; intro; subst s2].
apply impredicative_prod.
red; intros.
rewrite H0; reflexivity.
intros.
apply ty_U.
apply vcons_add_var; trivial.
Qed.
Lemma typ_conv : forall e M T T',
typ e M T ->
eq_typ e T T' ->
T <> kind ->
typ e M T'.
Proof.
unfold typ, eq_typ; simpl; intros.
destruct T as [(T,Tm)|]; [simpl in *; clear H1|elim H1; reflexivity].
destruct T' as [(T',T'm)|]; simpl in *; trivial.
rewrite <- H0; auto.
Qed.
(** Extendability *)
Lemma typ_cst_ty e x :
typ e (cst x) kind.
red; simpl; intros; trivial.
Qed.
Lemma typ_cst e x y :
x ∈ y ->
typ e (cst x) (cst y).
red; simpl; intros; trivial.
Qed.
Lemma eq_typ_cst e x y :
x == y ->
eq_typ e (cst x) (cst y).
red; simpl; intros; trivial.
Qed.
(** Weakening *)
Lemma weakening : forall e M T A,
typ e M T ->
typ (A::e) (lift 1 M) (lift 1 T).
unfold typ; intros.
destruct T as [(T,Tm)|]; simpl in *; trivial.
unfold lift.
rewrite int_lift_rec_eq.
apply H.
unfold val_ok in *.
intros.
specialize (H0 (S n) _ H1).
destruct T0 as [(T0,T0m)|]; simpl in *; trivial.
unfold V.lams at 1, V.shift at 1 2; simpl.
replace (n-0) with n; auto with arith.
Qed.
Lemma weakening0 : forall e M T,
typ e M T ->