-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITypingLemmata.v
1443 lines (1382 loc) · 45.3 KB
/
ITypingLemmata.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
From Coq Require Import Bool String List BinPos Compare_dec Lia Arith.
Require Import Equations.Prop.DepElim.
From Equations Require Import Equations.
From Translation
Require Import util SAst SLiftSubst SCommon Equality ITyping ITypingInversions.
Import ListNotations.
(* Lemmata about typing *)
Open Scope type_scope.
Open Scope i_scope.
Section Lemmata.
Context `{Sort_notion : Sorts.notion}.
(* Typing up to equality *)
Lemma meta_ctx_conv :
forall {Σ Γ Δ t A},
Σ ;;; Γ |-i t : A ->
Γ = Δ ->
Σ ;;; Δ |-i t : A.
Proof.
intros Σ Γ Δ t A h e.
rewrite <- e. exact h.
Defined.
Lemma meta_conv :
forall {Σ Γ t A B},
Σ ;;; Γ |-i t : A ->
A = B ->
Σ ;;; Γ |-i t : B.
Proof.
intros Σ Γ t A B h e.
rewrite <- e. exact h.
Defined.
Lemma typing_wf :
forall {Σ Γ t T},
Σ ;;; Γ |-i t : T ->
wf Σ Γ.
Proof.
intros Σ Γ t T H. induction H ; easy.
Defined.
Fact type_ctx_closed_above :
forall {Σ Γ t T},
Σ ;;; Γ |-i t : T ->
closed_above #|Γ| t = true.
Proof.
intros Σ Γ t T h.
dependent induction h.
all: try (cbn in * ; repeat erewrite_assumption ; reflexivity).
apply nth_error_Some_length in H0.
unfold closed_above. case_eq (n <? #|Γ|) ; intro e ; bprop e ; try mylia.
Defined.
Fact type_ctxempty_closed :
forall {Σ t T},
Σ ;;; [] |-i t : T ->
closed t.
Proof.
intros Σ t T h.
unfold closed. eapply @type_ctx_closed_above with (Γ := []). eassumption.
Defined.
(* TODO: Move the 6 next constructions away. *)
Fact substl_cons :
forall {a l t}, substl (a :: l) t = (substl l (t{ 0 := a })).
Proof.
reflexivity.
Defined.
Inductive closed_list : list sterm -> Type :=
| closed_list_nil : closed_list nil
| closed_list_cons a l :
closed_above #|l| a = true ->
closed_list l ->
closed_list (a :: l).
Derive Signature for closed_list.
Fact closed_substl :
forall {l},
closed_list l ->
forall {t},
closed_above #|l| t = true ->
closed (substl l t).
Proof.
intros l cl. induction cl ; intros t h.
- cbn in *. assumption.
- rewrite substl_cons. apply IHcl.
apply closed_above_subst.
+ mylia.
+ assumption.
+ replace (#|l| - 0) with #|l| by mylia. assumption.
Defined.
Fact rev_cons :
forall {A} {l} {a : A},
rev (a :: l) = (rev l ++ [a])%list.
Proof.
intro A.
unfold rev.
match goal with
| |- forall l a, ?faux _ _ = _ => set (aux := faux)
end.
assert (h : forall l acc, aux l acc = (aux l [] ++ acc)%list).
{ intro l. induction l ; intro acc.
- cbn. reflexivity.
- cbn. rewrite (IHl [a]). rewrite IHl.
change (a :: acc) with ([a] ++ acc)%list.
auto with datatypes.
}
intros l a.
apply h.
Defined.
Fact rev_map_cons :
forall {A B} {f : A -> B} {l} {a : A},
rev_map f (a :: l) = (rev_map f l ++ [f a])%list.
Proof.
intros A B f.
unfold rev_map.
match goal with
| |- forall l a, ?faux _ _ = _ => set (aux := faux)
end.
assert (h : forall l acc, aux l acc = (aux l [] ++ acc)%list).
{ intro l. induction l ; intro acc.
- cbn. reflexivity.
- cbn. rewrite (IHl [f a]). rewrite IHl.
change (f a :: acc) with ([f a] ++ acc)%list.
auto with datatypes.
}
intros l a.
apply h.
Defined.
Fact rev_length :
forall {A} {l : list A},
#|rev l| = #|l|.
Proof.
intro A.
unfold rev.
match goal with
| |- context [ #|?faux _ _| ] => set (aux := faux)
end.
assert (h : forall l acc, #|aux l acc| = (#|acc| + #|l|)%nat).
{ intro l. induction l ; intro acc.
- cbn. mylia.
- cbn. rewrite IHl. cbn. mylia.
}
intro l. apply h.
Defined.
Fact rev_map_length :
forall {A B} {f : A -> B} {l : list A},
#|rev_map f l| = #|l|.
Proof.
intros A B f.
unfold rev_map.
match goal with
| |- context [ #|?faux _ _| ] => set (aux := faux)
end.
assert (h : forall l acc, #|aux l acc| = (#|acc| + #|l|)%nat).
{ intro l. induction l ; intro acc.
- cbn. mylia.
- cbn. rewrite IHl. cbn. mylia.
}
intro l. apply h.
Defined.
Fact xcomp_lift :
forall {t}, Xcomp t ->
forall {n k}, Xcomp (lift n k t).
Proof.
intros t h. induction h ; intros m k.
all: try (cbn ; constructor ; easy).
cbn. destruct (k <=? n) ; constructor.
Defined.
Fact xcomp_subst :
forall {t}, Xcomp t ->
forall {u}, Xcomp u ->
forall {n}, Xcomp (t{ n := u}).
Proof.
intros t ht. induction ht ; intros t' ht' m.
all: try (cbn ; constructor; easy).
cbn. case_eq (m ?= n) ; try constructor.
intro e. apply xcomp_lift. assumption.
Defined.
Lemma ident_eq_spec x y : reflect (x = y) (ident_eq x y).
Proof.
unfold ident_eq.
destruct (string_dec x y); now constructor.
Defined.
Fact ident_neq_fresh :
forall {Σ id ty d},
lookup_glob Σ id = Some ty ->
fresh_glob (dname d) Σ ->
ident_eq id (dname d) = false.
Proof.
intro Σ. induction Σ ; intros id ty d h hf.
- cbn in h. discriminate h.
- cbn in h. inversion_clear hf.
unfold ident_eq in *.
destruct (string_dec id (dname a)).
destruct (string_dec id (dname d)) ; congruence.
exact (IHΣ _ _ _ h H).
Defined.
Fixpoint weak_glob_type {Σ Γ t A} (h : Σ ;;; Γ |-i t : A) :
forall {d},
fresh_glob (dname d) Σ ->
(d::Σ) ;;; Γ |-i t : A
with weak_glob_wf {Σ Γ} (h : wf Σ Γ) :
forall {d},
fresh_glob (dname d) Σ ->
wf (d::Σ) Γ.
Proof.
(* weak_glob_type *)
- { dependent destruction h ; intros d fd.
all: try (econstructor ; try apply weak_glob_wf ;
try apply weak_glob_type ;
eassumption
).
eapply type_Ax.
- eapply weak_glob_wf ; eassumption.
- cbn. erewrite ident_neq_fresh by eassumption.
assumption.
}
(* weak_glob_wf *)
- { dependent destruction h ; intros fd.
- constructor.
- econstructor.
+ apply weak_glob_wf ; assumption.
+ apply weak_glob_type ; eassumption.
}
Defined.
Corollary weak_glob_isType :
forall {Σ Γ A} (h : isType Σ Γ A) {d},
fresh_glob (dname d) Σ ->
isType (d::Σ) Γ A.
Proof.
intros Σ Γ A h d hf.
destruct h as [s h].
exists s. eapply weak_glob_type ; eassumption.
Defined.
Fact typed_ax_type :
forall {Σ}, type_glob Σ ->
forall {id ty},
lookup_glob Σ id = Some ty ->
isType Σ [] ty.
Proof.
intros Σ hg. dependent induction hg ; intros id ty h.
- cbn in h. discriminate h.
- cbn in h.
case_eq (ident_eq id (dname d)).
+ intro e. rewrite e in h. inversion h. subst.
eapply weak_glob_isType ; eassumption.
+ intro e. rewrite e in h.
specialize (IHhg _ _ h).
eapply weak_glob_isType ; eassumption.
Defined.
Fact xcomp_ax_type :
forall {Σ}, type_glob Σ ->
forall {id ty},
lookup_glob Σ id = Some ty ->
Xcomp ty.
Proof.
intros Σ hg. dependent induction hg ; intros id ty h.
- cbn in h. discriminate h.
- cbn in h.
case_eq (ident_eq id (dname d)).
+ intro e. rewrite e in h. inversion h. subst.
assumption.
+ intro e. rewrite e in h.
specialize (IHhg _ _ h).
assumption.
Defined.
Fact lift_ax_type :
forall {Σ},
type_glob Σ ->
forall {id ty},
lookup_glob Σ id = Some ty ->
forall n k, lift n k ty = ty.
Proof.
intros Σ hg id ty isd n k.
destruct (typed_ax_type hg isd).
eapply closed_lift.
eapply type_ctxempty_closed. eassumption.
Defined.
Lemma nth_error_lift_context :
forall Γ k n A,
nth_error Γ n = Some A ->
nth_error (lift_context k Γ) n = Some (lift k (#|Γ| - S n) A).
Proof.
intros Γ k n A e.
induction Γ in k, n, A, e |- *.
- destruct n. all: discriminate.
- destruct n.
+ cbn in e. inversion e. subst. clear e.
cbn. f_equal. f_equal. mylia.
+ cbn. cbn in e. eapply IHΓ in e. rewrite e. reflexivity.
Defined.
Ltac ih h :=
lazymatch goal with
| [ type_lift :
forall (Σ : sglobal_context) (Γ Δ Ξ : scontext) (t A : sterm),
Σ;;; Γ ,,, Ξ |-i t : A ->
type_glob Σ ->
wf Σ (Γ ,,, Δ) ->
Σ;;; Γ ,,, Δ ,,, lift_context #|Δ| Ξ
|-i lift #|Δ| #|Ξ| t : lift #|Δ| #|Ξ| A
|- _ ] =>
lazymatch type of h with
| _ ;;; ?Γ' ,,, ?Ξ' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_lift with (Γ := Γ') (Ξ := Ξ') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
| _ ;;; (?Γ' ,,, ?Ξ'),, ?d' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_lift with (Γ := Γ') (Ξ := Ξ',, d') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
| _ ;;; (?Γ' ,,, ?Ξ'),, ?d',, ?d'' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_lift with (Γ := Γ') (Ξ := (Ξ',, d'),, d'') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
end ; try (cbn ; reflexivity)
| _ => fail "Cannot retrieve type_lift"
end.
Ltac eih :=
lazymatch goal with
| h : _ ;;; _ |-i ?t : _ |- _ ;;; _ |-i lift _ _ ?t : _ => ih h
| _ => fail "Not handled by eih"
end.
Fixpoint type_lift {Σ Γ Δ Ξ t A} (h : Σ ;;; Γ ,,, Ξ |-i t : A) {struct h} :
type_glob Σ ->
wf Σ (Γ ,,, Δ) ->
Σ ;;; Γ ,,, Δ ,,, lift_context #|Δ| Ξ |-i lift #|Δ| #|Ξ| t : lift #|Δ| #|Ξ| A
with wf_lift {Σ Γ Δ Ξ} (h : wf Σ (Γ ,,, Ξ)) {struct h} :
type_glob Σ ->
wf Σ (Γ ,,, Δ) ->
wf Σ (Γ ,,, Δ ,,, lift_context #|Δ| Ξ)
.
Proof.
- { dependent destruction h ; intros hΣ hwf.
- cbn. case_eq (#|Ξ| <=? n) ; intro e ; bprop e.
+ rewrite liftP3 by mylia.
replace (#|Δ| + S n)%nat with (S (#|Δ| + n)) by mylia.
eapply type_Rel.
* eapply wf_lift ; assumption.
* unfold ",,,". rewrite nth_error_app2.
2:{ rewrite lift_context_length. mylia. }
rewrite lift_context_length.
rewrite nth_error_app2 by mylia.
replace (#|Δ| + n - #|Ξ| - #|Δ|) with (n - #|Ξ|) by mylia.
unfold ",,," in H0. rewrite nth_error_app2 in H0 by auto.
assumption.
+ eapply meta_conv.
* eapply type_Rel.
1: eapply wf_lift ; assumption.
unfold ",,,". rewrite nth_error_app1.
2:{ rewrite lift_context_length. auto. }
eapply nth_error_lift_context.
unfold ",,," in H0. rewrite nth_error_app1 in H0 by mylia.
eassumption.
* rewrite <- liftP2 by mylia. f_equal. mylia.
- cbn. apply type_Sort. now apply wf_lift.
- cbn. eapply type_Prod ; eih.
- cbn. eapply type_Lambda ; eih.
- cbn.
change (lift #|Δ| #|Ξ| (B {0 := u}))
with (lift #|Δ| (0 + #|Ξ|) (B { 0 := u })).
rewrite substP1.
eapply type_App ; eih.
- cbn. eapply type_Sum ; eih.
- cbn. eapply type_Pair ; eih.
change (#|Ξ|) with (0 + #|Ξ|)%nat.
rewrite substP1. reflexivity.
- cbn. eapply type_Pi1 ; eih.
- cbn.
change (#|Ξ|) with (0 + #|Ξ|)%nat.
rewrite substP1. cbn.
eapply type_Pi2 ; eih.
- cbn. apply type_Eq ; eih.
- cbn. eapply type_Refl ; eih.
- change (#|Ξ|) with (0 + #|Ξ|)%nat.
rewrite substP1.
replace (S (0 + #|Ξ|)) with (1 + #|Ξ|)%nat by mylia.
rewrite substP1.
cbn. eapply type_J ; try eih.
+ cbn. unfold ssnoc. cbn.
f_equal. f_equal.
* replace (S #|Ξ|) with (1 + #|Ξ|)%nat by mylia.
apply liftP2. mylia.
* replace (S #|Ξ|) with (1 + #|Ξ|)%nat by mylia.
apply liftP2. mylia.
+ replace (S (S #|Ξ|)) with (1 + (S (0 + #|Ξ|)))%nat by mylia.
rewrite <- substP1.
replace (1 + (0 + #|Ξ|))%nat with (S (0 + #|Ξ|))%nat by mylia.
change (sRefl (lift #|Δ| #|Ξ| A0) (lift #|Δ| #|Ξ| u))
with (lift #|Δ| #|Ξ| (sRefl A0 u)).
rewrite <- substP1. reflexivity.
- cbn. eapply type_Transport ; eih.
- cbn.
change (lift #|Δ| #|Ξ| (B {0 := u}))
with (lift #|Δ| (0 + #|Ξ|) (B { 0 := u })).
change (lift #|Δ| #|Ξ| (t0 {0 := u}))
with (lift #|Δ| (0 + #|Ξ|) (t0 { 0 := u })).
rewrite 2!substP1.
cbn. eapply type_Beta ; eih.
- cbn. eapply type_Heq ; eih.
- cbn. eapply type_HeqToEq ; eih.
- cbn. eapply type_HeqRefl ; eih.
- cbn. eapply type_HeqSym ; eih.
- cbn. eapply @type_HeqTrans with (B := lift #|Δ| #|Ξ| B) (b := lift #|Δ| #|Ξ| b) ; eih.
- cbn. eapply type_HeqTransport ; eih.
- cbn. eapply type_CongProd ; try eih.
cbn. f_equal.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn. eapply type_CongLambda ; try eih.
+ cbn. f_equal.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ cbn. f_equal.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn.
change (lift #|Δ| #|Ξ| (B1 {0 := v1}))
with (lift #|Δ| (0 + #|Ξ|) (B1 { 0 := v1 })).
change (lift #|Δ| #|Ξ| (B2 {0 := v2}))
with (lift #|Δ| (0 + #|Ξ|) (B2 { 0 := v2 })).
rewrite 2!substP1.
eapply type_CongApp ; eih.
cbn. f_equal.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn. eapply type_CongSum ; try eih.
cbn. f_equal.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn. eapply type_CongPair ; eih.
+ cbn. f_equal.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
* rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ cbn. f_equal.
* change #|Ξ| with (0 + #|Ξ|)%nat.
rewrite substP1. reflexivity.
* change #|Ξ| with (0 + #|Ξ|)%nat.
rewrite substP1. reflexivity.
+ change #|Ξ| with (0 + #|Ξ|)%nat.
rewrite substP1. reflexivity.
+ change #|Ξ| with (0 + #|Ξ|)%nat.
rewrite substP1. reflexivity.
- cbn. eapply type_CongPi1 ; eih.
cbn. f_equal.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn.
change #|Ξ| with (0 + #|Ξ|)%nat.
rewrite 2!substP1.
eapply type_CongPi2 ; eih.
cbn. f_equal.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
+ rewrite <- liftP2 by mylia.
change (S #|Ξ|) with (0 + (S #|Ξ|))%nat at 1.
rewrite substP1. cbn. reflexivity.
- cbn. eapply type_CongEq ; eih.
- cbn. eapply type_CongRefl ; eih.
- cbn. eapply type_EqToHeq ; eih.
- cbn. eapply type_HeqTypeEq ; eih.
- cbn. eapply type_Pack ; eih.
- cbn. eapply @type_ProjT1 with (A2 := lift #|Δ| #|Ξ| A2) ; eih.
- cbn. eapply @type_ProjT2 with (A1 := lift #|Δ| #|Ξ| A1) ; eih.
- cbn. eapply type_ProjTe ; eih.
- cbn. erewrite lift_ax_type by eassumption.
eapply type_Ax.
+ now apply wf_lift.
+ assumption.
- eapply type_rename.
+ eih.
+ eapply nl_lift. assumption.
}
(* wf_lift *)
- { intros hg hwf.
destruct Ξ.
- cbn. assumption.
- dependent destruction h.
cbn. econstructor.
+ apply wf_lift ; assumption.
+ instantiate (1 := s0). cbn. change (sSort s0) with (lift #|Δ| #|Ξ| (sSort s0)).
apply type_lift ; assumption.
}
Unshelve.
all: try rewrite !length_cat ; try rewrite length_cat in isdecl ;
try rewrite lift_context_length ; mylia.
Defined.
Corollary typing_lift01 :
forall {Σ Γ t A B s},
type_glob Σ ->
Σ ;;; Γ |-i t : A ->
Σ ;;; Γ |-i B : sSort s ->
Σ ;;; Γ ,, B |-i lift0 1 t : lift0 1 A.
Proof.
intros Σ Γ t A B s hg ht hB.
apply (@type_lift _ _ [ B ] nil _ _ ht hg).
econstructor.
- eapply typing_wf. eassumption.
- eassumption.
Defined.
Corollary typing_lift02 :
forall {Σ Γ t A B s C s'},
type_glob Σ ->
Σ ;;; Γ |-i t : A ->
Σ ;;; Γ |-i B : sSort s ->
Σ ;;; Γ ,, B |-i C : sSort s' ->
Σ ;;; Γ ,, B ,, C |-i lift0 2 t : lift0 2 A.
Proof.
intros Σ Γ t A B s C s' hg ht hB hC.
assert (eq : forall t, lift0 2 t = lift0 1 (lift0 1 t)).
{ intro u. rewrite lift_lift. reflexivity. }
rewrite !eq. eapply typing_lift01.
- assumption.
- eapply typing_lift01 ; eassumption.
- eassumption.
Defined.
Fact subst_ax_type :
forall {Σ},
type_glob Σ ->
forall {id ty},
lookup_glob Σ id = Some ty ->
forall n u, ty{ n := u } = ty.
Proof.
intros Σ hg id ty isd n k.
destruct (typed_ax_type hg isd).
eapply closed_subst.
eapply type_ctxempty_closed. eassumption.
Defined.
Lemma nth_error_subst_context :
forall Γ u n A,
nth_error Γ n = Some A ->
nth_error (subst_context u Γ) n = Some (A{ #|Γ| - S n := u }).
Proof.
intros Γ u n A e.
induction Γ in u, n, A, e |- *.
- destruct n. all: discriminate.
- destruct n.
+ cbn in e. inversion e. subst. clear e.
cbn. f_equal. f_equal. mylia.
+ cbn. cbn in e. eapply IHΓ in e. rewrite e. reflexivity.
Defined.
Ltac sh h :=
lazymatch goal with
| [ type_subst :
forall (Σ : sglobal_context) (Γ Δ : scontext) (t A : sterm)
(B u : sterm),
Σ;;; Γ,, B ,,, Δ |-i t : A ->
type_glob Σ ->
Σ;;; Γ |-i u : B -> Σ;;; Γ ,,, subst_context u Δ |-i
t {#|Δ| := u} : A {#|Δ| := u}
|- _ ] =>
lazymatch type of h with
| _ ;;; ?Γ' ,, ?B' ,,, ?Δ' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_subst with (Γ := Γ') (Δ := Δ') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
| _ ;;; (?Γ' ,, ?B' ,,, ?Δ') ,, ?d' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_subst with (Γ := Γ') (Δ := Δ' ,, d') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
| _ ;;; (?Γ' ,, ?B' ,,, ?Δ') ,, ?d',, ?d'' |-i _ : ?T' =>
eapply meta_conv ; [
eapply meta_ctx_conv ; [
eapply type_subst with (Γ := Γ') (Δ := (Δ' ,, d') ,, d'') (A := T') ; [
exact h
| assumption
| assumption
]
| .. ]
| .. ]
end ; try (cbn ; reflexivity)
| _ => fail "cannot find type_subst"
end.
Ltac esh :=
lazymatch goal with
| h : _ ;;; _ |-i ?t : _ |- _ ;;; _ |-i ?t{ _ := _ } : _ => sh h
| _ => fail "not handled by esh"
end.
Fixpoint type_subst {Σ Γ Δ t A B u}
(h : Σ ;;; Γ ,, B ,,, Δ |-i t : A) {struct h} :
type_glob Σ ->
Σ ;;; Γ |-i u : B ->
Σ ;;; Γ ,,, subst_context u Δ |-i t{ #|Δ| := u } : A{ #|Δ| := u }
with wf_subst {Σ Γ Δ B u}
(h : wf Σ (Γ ,, B ,,, Δ)) {struct h} :
type_glob Σ ->
Σ ;;; Γ |-i u : B ->
wf Σ (Γ ,,, subst_context u Δ)
.
Proof.
(* type_subst *)
- { intros hg hu.
dependent destruction h.
- cbn. case_eq (#|Δ| ?= n) ; intro e ; bprop e.
+ rewrite substP3 by mylia.
rewrite <- e0.
replace #|Δ| with #|subst_context u Δ|.
2:{ rewrite subst_context_length. reflexivity. }
unfold ",,," in H0. rewrite nth_error_app2 in H0 by mylia.
replace (n - #|Δ|) with 0 in H0 by mylia.
cbn in H0. inversion H0. subst.
eapply @type_lift with (Ξ := []) (Δ := subst_context u Δ).
* cbn. assumption.
* assumption.
* eapply wf_subst ; eassumption.
+ rewrite substP3 by mylia.
destruct n. 1: lia.
cbn. unfold ",,," in H0. rewrite nth_error_app2 in H0 by mylia.
unfold ",," in H0. change (B :: Γ) with ([B] ++ Γ) in H0.
rewrite nth_error_app2 in H0.
2:{ unfold length at 1. mylia. }
unfold length at 2 in H0.
replace (S n - #|Δ| - 1) with (n - #|Δ|) in H0 by mylia.
eapply type_Rel.
* eapply wf_subst. all: eassumption.
* unfold ",,,". rewrite nth_error_app2.
2:{ rewrite subst_context_length. mylia. }
rewrite subst_context_length. assumption.
+ match goal with
| |- _ ;;; _ |-i _ : ?t{?d := ?u} =>
replace (subst u d t) with (t{((S n) + (#|Δ| - (S n)))%nat := u})
by (f_equal ; mylia)
end.
rewrite substP2 by mylia.
eapply type_Rel.
* eapply wf_subst ; eassumption.
* unfold ",,,". rewrite nth_error_app1.
2:{ rewrite subst_context_length. mylia. }
eapply nth_error_subst_context.
unfold ",,," in H0. rewrite nth_error_app1 in H0 by mylia.
assumption.
- cbn. apply type_Sort. eapply wf_subst ; eassumption.
- cbn. eapply type_Prod ; esh.
- cbn. eapply type_Lambda ; esh.
- cbn.
change ((B0 {0 := u0}) {#|Δ| := u})
with ((B0 {0 := u0}) {0 + #|Δ| := u}).
rewrite substP4. cbn.
eapply type_App ; esh.
- cbn. eapply type_Sum ; esh.
- cbn. eapply type_Pair ; esh.
change (#|Δ|) with (0 + #|Δ|)%nat.
rewrite substP4. reflexivity.
- cbn. eapply type_Pi1 ; esh.
- cbn.
change (#|Δ|) with (0 + #|Δ|)%nat.
rewrite substP4. cbn.
eapply type_Pi2 ; esh.
- cbn. eapply type_Eq ; esh.
- cbn. eapply type_Refl ; esh.
- cbn.
change (#|Δ|) with (0 + #|Δ|)%nat.
rewrite substP4.
replace (S (0 + #|Δ|)) with (1 + #|Δ|)%nat by mylia.
rewrite substP4.
eapply type_J ; esh.
+ cbn. unfold ssnoc. cbn.
f_equal. f_equal.
* replace (S #|Δ|) with (1 + #|Δ|)%nat by mylia.
apply substP2. mylia.
* replace (S #|Δ|) with (1 + #|Δ|)%nat by mylia.
apply substP2. mylia.
+ replace (S (S #|Δ|)) with (1 + (S (0 + #|Δ|)))%nat by mylia.
rewrite <- substP4.
replace (1 + (0 + #|Δ|))%nat with (S (0 + #|Δ|))%nat by mylia.
change (sRefl (A0 {0 + #|Δ| := u}) (u0 {0 + #|Δ| := u}))
with ((sRefl A0 u0){ 0 + #|Δ| := u}).
rewrite <- substP4. reflexivity.
- cbn. eapply type_Transport ; esh.
- cbn.
change ((B0 {0 := u0}) {#|Δ| := u})
with ((B0 {0 := u0}) {0 + #|Δ| := u}).
change ((t0 {0 := u0}) {#|Δ| := u})
with ((t0 {0 := u0}) {0 + #|Δ| := u}).
rewrite 2!substP4. cbn.
eapply type_Beta ; esh.
- cbn. eapply type_Heq ; esh.
- cbn. eapply type_HeqToEq ; esh.
- cbn. eapply type_HeqRefl ; esh.
- cbn. eapply type_HeqSym ; esh.
- cbn.
eapply type_HeqTrans
with (B := B0{ #|Δ| := u }) (b := b{ #|Δ| := u }) ; esh.
- cbn. eapply type_HeqTransport ; esh.
- cbn. eapply type_CongProd ; esh.
cbn. f_equal.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn. eapply type_CongLambda ; esh.
+ cbn. f_equal.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ cbn. f_equal.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn.
change #|Δ| with (0 + #|Δ|)%nat.
rewrite 2!substP4. cbn.
eapply type_CongApp ; esh.
cbn. f_equal.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn. eapply type_CongSum ; esh.
cbn. f_equal.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn. eapply type_CongPair ; esh.
+ cbn. f_equal.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
* rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ cbn. f_equal.
* change #|Δ| with (0 + #|Δ|)%nat.
rewrite substP4. reflexivity.
* change #|Δ| with (0 + #|Δ|)%nat.
rewrite substP4. reflexivity.
+ change #|Δ| with (0 + #|Δ|)%nat.
rewrite substP4. reflexivity.
+ change #|Δ| with (0 + #|Δ|)%nat.
rewrite substP4. reflexivity.
- cbn. eapply type_CongPi1 ; esh.
cbn. f_equal.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn.
change #|Δ| with (0 + #|Δ|)%nat.
rewrite 2!substP4. cbn.
eapply type_CongPi2 ; esh.
cbn. f_equal.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
+ rewrite <- substP2 by mylia.
change (S #|Δ|) with (0 + (S #|Δ|))%nat at 1.
rewrite substP4. cbn. reflexivity.
- cbn. eapply type_CongEq ; esh.
- cbn. eapply type_CongRefl ; esh.
- cbn. eapply type_EqToHeq ; esh.
- cbn. eapply type_HeqTypeEq ; esh.
- cbn. eapply type_Pack ; esh.
- cbn. eapply @type_ProjT1 with (A2 := A2{#|Δ| := u}) ; esh.
- cbn. eapply @type_ProjT2 with (A1 := A1{#|Δ| := u}) ; esh.
- cbn. eapply type_ProjTe ; esh.
- cbn. erewrite subst_ax_type by eassumption.
eapply type_Ax.
+ now eapply wf_subst.
+ assumption.
- eapply type_rename.
+ esh.
+ eapply nl_subst ; try assumption. reflexivity.
}
(* wf_subst *)
- { intros hg hu.
destruct Δ.
- cbn. dependent destruction h. assumption.
- dependent destruction h. cbn. econstructor.
+ eapply wf_subst ; eassumption.
+ esh.
}
Unshelve.
all: try rewrite !length_cat ; try rewrite !subst_context_length ; mylia.
Defined.
Corollary typing_subst :
forall {Σ Γ t A B u},
type_glob Σ ->
Σ ;;; Γ ,, A |-i t : B ->
Σ ;;; Γ |-i u : A ->
Σ ;;; Γ |-i t{ 0 := u } : B{ 0 := u }.
Proof.
intros Σ Γ t A B u hg ht hu.
eapply @type_subst with (Δ := []) ; eassumption.
Defined.
Corollary typing_subst2 :
forall {Σ Γ t A B C u v},
type_glob Σ ->
Σ ;;; Γ ,, A ,, B |-i t : C ->
Σ ;;; Γ |-i u : A ->
Σ ;;; Γ |-i v : B{ 0 := u } ->
Σ ;;; Γ |-i t{ 1 := u }{ 0 := v } : C{ 1 := u }{ 0 := v }.
Proof.
intros Σ Γ t A B C u v hg ht hu hv.
eapply @type_subst with (Δ := []).
- eapply @type_subst with (Δ := [ B ]).
+ exact ht.
+ assumption.
+ assumption.
- assumption.
- cbn. assumption.
Defined.
Inductive typed_list Σ Γ : list sterm -> scontext -> Type :=
| typed_list_nil : typed_list Σ Γ [] []
| typed_list_cons A l Δ T :
typed_list Σ Γ l Δ ->
Σ ;;; Γ ,,, Δ |-i A : T ->
typed_list Σ Γ (A :: l) (Δ ,, T).
Corollary type_substl :
forall {Σ l Γ Δ},
type_glob Σ ->
typed_list Σ Γ l Δ ->
forall {t T},
Σ ;;; Γ ,,, Δ |-i t : T ->
Σ ;;; Γ |-i substl l t : substl l T.
Proof.
intros Σ l Γ Δ hg tl.
induction tl ; intros u C h.
- cbn. assumption.
- rewrite !substl_cons. apply IHtl.
eapply typing_subst.
+ assumption.
+ exact h.
+ assumption.
Defined.
Fact substl_sort :
forall {l s}, substl l (sSort s) = sSort s.
Proof.
intro l. induction l ; intro s.
- cbn. reflexivity.
- rewrite substl_cons. cbn. apply IHl.
Defined.
Fact nth_error_error :
forall {A} {l : list A} {n},
nth_error l n = None ->
n >= #|l|.
Proof.
intros A l. induction l.
- intros. cbn. mylia.
- intros n h. cbn.
destruct n.
+ cbn in h. inversion h.
+ inversion h as [e].
specialize (IHl n e).
mylia.
Defined.
Fact rev_map_nth_error :
forall {A B} {f : A -> B} {l n a},
nth_error l n = Some a ->
nth_error (rev_map f l) (#|l| - S n) = Some (f a).
Proof.
intros A B f l. induction l ; intros n x hn.
- destruct n ; inversion hn.
- destruct n.
+ cbn in hn. inversion hn.
rewrite rev_map_cons.
rewrite nth_error_app2.
* cbn. rewrite rev_map_length.
replace (#|l| - 0 - #|l|) with 0 by mylia.
cbn. reflexivity.
* rewrite rev_map_length. cbn. mylia.
+ cbn in hn.
rewrite rev_map_cons.
rewrite nth_error_app1.
* cbn. erewrite IHl by eassumption. reflexivity.
* rewrite rev_map_length. cbn.
apply nth_error_Some_length in hn.
mylia.
Defined.