forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICLiftSubst.v
1643 lines (1412 loc) · 52.9 KB
/
PCUICLiftSubst.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
(* Distributed under the terms of the MIT license. *)
From Coq Require Import List Program.
From MetaCoq.Template Require Import utils.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction.
From Coq Require Import BinPos Arith.Compare_dec Bool Lia.
Require Import PeanoNat.
Import Nat.
Require Import ssreflect.
(** * Lifting and substitution for the AST
Along with standard commutation lemmas.
Definition of [closedn] (boolean) predicate for checking if
a term is closed. *)
Set Asymmetric Patterns.
(** Shift a renaming [f] by [k]. *)
Definition shiftn k f :=
fun n => if Nat.ltb n k then n else k + (f (n - k)).
Fixpoint rename f t : term :=
match t with
| tRel i => tRel (f i)
| tEvar ev args => tEvar ev (List.map (rename f) args)
| tLambda na T M => tLambda na (rename f T) (rename (shiftn 1 f) M)
| tApp u v => tApp (rename f u) (rename f v)
| tProd na A B => tProd na (rename f A) (rename (shiftn 1 f) B)
| tLetIn na b t b' => tLetIn na (rename f b) (rename f t) (rename (shiftn 1 f) b')
| tCase ind p c brs =>
let brs' := List.map (on_snd (rename f)) brs in
tCase ind (rename f p) (rename f c) brs'
| tProj p c => tProj p (rename f c)
| tFix mfix idx =>
let mfix' := List.map (map_def (rename f) (rename (shiftn (List.length mfix) f))) mfix in
tFix mfix' idx
| tCoFix mfix idx =>
let mfix' := List.map (map_def (rename f) (rename (shiftn (List.length mfix) f))) mfix in
tCoFix mfix' idx
| x => x
end.
Fixpoint lift n k t : term :=
match t with
| tRel i => if Nat.leb k i then tRel (n + i) else tRel i
| tEvar ev args => tEvar ev (List.map (lift n k) args)
| tLambda na T M => tLambda na (lift n k T) (lift n (S k) M)
| tApp u v => tApp (lift n k u) (lift n k v)
| tProd na A B => tProd na (lift n k A) (lift n (S k) B)
| tLetIn na b t b' => tLetIn na (lift n k b) (lift n k t) (lift n (S k) b')
| tCase ind p c brs =>
let brs' := List.map (on_snd (lift n k)) brs in
tCase ind (lift n k p) (lift n k c) brs'
| tProj p c => tProj p (lift n k c)
| tFix mfix idx =>
let k' := List.length mfix + k in
let mfix' := List.map (map_def (lift n k) (lift n k')) mfix in
tFix mfix' idx
| tCoFix mfix idx =>
let k' := List.length mfix + k in
let mfix' := List.map (map_def (lift n k) (lift n k')) mfix in
tCoFix mfix' idx
| x => x
end.
Notation lift0 n := (lift n 0).
(** Parallel substitution: it assumes that all terms in the substitution live in the
same context *)
Fixpoint subst s k u :=
match u with
| tRel n =>
if Nat.leb k n then
match nth_error s (n - k) with
| Some b => lift0 k b
| None => tRel (n - List.length s)
end
else tRel n
| tEvar ev args => tEvar ev (List.map (subst s k) args)
| tLambda na T M => tLambda na (subst s k T) (subst s (S k) M)
| tApp u v => tApp (subst s k u) (subst s k v)
| tProd na A B => tProd na (subst s k A) (subst s (S k) B)
| tLetIn na b ty b' => tLetIn na (subst s k b) (subst s k ty) (subst s (S k) b')
| tCase ind p c brs =>
let brs' := List.map (on_snd (subst s k)) brs in
tCase ind (subst s k p) (subst s k c) brs'
| tProj p c => tProj p (subst s k c)
| tFix mfix idx =>
let k' := List.length mfix + k in
let mfix' := List.map (map_def (subst s k) (subst s k')) mfix in
tFix mfix' idx
| tCoFix mfix idx =>
let k' := List.length mfix + k in
let mfix' := List.map (map_def (subst s k) (subst s k')) mfix in
tCoFix mfix' idx
| x => x
end.
(** Substitutes [t1 ; .. ; tn] in u for [Rel 0; .. Rel (n-1)] *in parallel* *)
Notation subst0 t := (subst t 0).
Definition subst1 t k u := subst [t] k u.
Notation subst10 t := (subst1 t 0).
Notation "M { j := N }" := (subst1 N j M) (at level 10, right associativity).
Fixpoint closedn k (t : term) : bool :=
match t with
| tRel i => Nat.ltb i k
| tEvar ev args => List.forallb (closedn k) args
| tLambda _ T M | tProd _ T M => closedn k T && closedn (S k) M
| tApp u v => closedn k u && closedn k v
| tLetIn na b t b' => closedn k b && closedn k t && closedn (S k) b'
| tCase ind p c brs =>
let brs' := List.forallb (test_snd (closedn k)) brs in
closedn k p && closedn k c && brs'
| tProj p c => closedn k c
| tFix mfix idx =>
let k' := List.length mfix + k in
List.forallb (test_def (closedn k) (closedn k')) mfix
| tCoFix mfix idx =>
let k' := List.length mfix + k in
List.forallb (test_def (closedn k) (closedn k')) mfix
| x => true
end.
Notation closed t := (closedn 0 t).
Create HintDb terms.
Ltac arith_congr := repeat (try lia; progress f_equal).
Ltac easy0 :=
let rec use_hyp H :=
(match type of H with
| _ /\ _ => exact H || destruct_hyp H
| _ * _ => exact H || destruct_hyp H
| _ => try (solve [ inversion H ])
end)
with do_intro := (let H := fresh in
intro H; use_hyp H)
with destruct_hyp H := (case H; clear H; do_intro; do_intro)
in
let rec use_hyps :=
(match goal with
| H:_ /\ _ |- _ => exact H || (destruct_hyp H; use_hyps)
| H:_ * _ |- _ => exact H || (destruct_hyp H; use_hyps)
| H:_ |- _ => solve [ inversion H ]
| _ => idtac
end)
in
let do_atom := (solve [ trivial with eq_true | reflexivity | symmetry; trivial | contradiction | congruence]) in
let rec do_ccl := (try do_atom; repeat (do_intro; try do_atom); try arith_congr; (solve [ split; do_ccl ])) in
(solve [ do_atom | use_hyps; do_ccl ]) || fail "Cannot solve this goal".
Hint Extern 10 (_ < _)%nat => lia : terms.
Hint Extern 10 (_ <= _)%nat => lia : terms.
Hint Extern 10 (@eq nat _ _) => lia : terms.
Ltac easy ::= easy0 || solve [intuition eauto 3 with core terms].
Notation subst_rec N M k := (subst N k M) (only parsing).
Lemma lift_rel_ge :
forall k n p, p <= n -> lift k p (tRel n) = tRel (k + n).
Proof.
intros; simpl in |- *.
now elim (leb_spec p n).
Qed.
Lemma lift_rel_lt : forall k n p, p > n -> lift k p (tRel n) = tRel n.
Proof.
intros; simpl in |- *.
now elim (leb_spec p n).
Qed.
Lemma lift_rel_alt : forall n k i, lift n k (tRel i) = tRel (if Nat.leb k i then n + i else i).
Proof.
intros; simpl. now destruct leb.
Qed.
Lemma subst_rel_lt : forall u n k, k > n -> subst u k (tRel n) = tRel n.
Proof.
simpl in |- *; intros.
elim (leb_spec k n); intro Hcomp; easy.
Qed.
Lemma subst_rel_gt :
forall u n k, n >= k + length u -> subst u k (tRel n) = tRel (n - length u).
Proof.
simpl in |- *; intros.
elim (leb_spec k n). intros. destruct nth_error eqn:Heq.
assert (n - k < length u) by (apply nth_error_Some; congruence). lia. reflexivity.
lia.
Qed.
Lemma subst_rel_eq :
forall (u : list term) n i t p,
List.nth_error u i = Some t -> p = n + i ->
subst u n (tRel p) = lift0 n t.
Proof.
intros; simpl in |- *. subst p.
elim (leb_spec n (n + i)). intros. assert (n + i - n = i) by lia. rewrite H1 H.
reflexivity. intros. lia.
Qed.
Hint Extern 0 (_ = _) => progress f_equal : all.
Hint Unfold on_snd snd : all.
Lemma on_snd_eq_id_spec {A B} (f : B -> B) (x : A * B) :
f (snd x) = snd x <->
on_snd f x = x.
Proof.
destruct x; simpl; unfold on_snd; simpl. split; congruence.
Qed.
Hint Resolve -> on_snd_eq_id_spec : all.
Hint Resolve -> on_snd_eq_spec : all.
Lemma map_def_eq_spec {A B : Set} (f f' g g' : A -> B) (x : def A) :
f (dtype x) = g (dtype x) ->
f' (dbody x) = g' (dbody x) ->
map_def f f' x = map_def g g' x.
Proof.
intros. unfold map_def; f_equal; auto.
Qed.
Hint Resolve map_def_eq_spec : all.
Lemma map_def_id_spec {A : Set} (f f' : A -> A) (x : def A) :
f (dtype x) = (dtype x) ->
f' (dbody x) = (dbody x) ->
map_def f f' x = x.
Proof.
intros. rewrite (map_def_eq_spec _ _ id id); auto. destruct x; auto.
Qed.
Hint Resolve map_def_id_spec : all.
Hint Extern 10 (_ < _)%nat => lia : all.
Hint Extern 10 (_ <= _)%nat => lia : all.
Hint Extern 10 (@eq nat _ _) => lia : all.
Ltac change_Sk :=
repeat match goal with
|- context [S (?x + ?y)] => progress change (S (x + y)) with (S x + y)
end.
Ltac all_simpl :=
progress (unfold compose; simpl).
Hint Extern 10 => all_simpl : all.
Ltac solve_all :=
unfold tCaseBrsProp, tFixProp in *;
repeat toAll; try All_map; try close_All;
change_Sk; auto with all;
intuition eauto 4 with all.
Ltac nth_leb_simpl :=
match goal with
|- context [leb ?k ?n] => elim (leb_spec_Set k n); try lia; intros; simpl
| |- context [nth_error ?l ?n] => elim (nth_error_spec l n); rewrite -> ?app_length, ?map_length;
try lia; intros; simpl
| H : context[nth_error (?l ++ ?l') ?n] |- _ =>
(rewrite -> (nth_error_app_ge l l' n) in H by lia) ||
(rewrite -> (nth_error_app_lt l l' n) in H by lia)
| H : nth_error ?l ?n = Some _, H' : nth_error ?l ?n' = Some _ |- _ =>
replace n' with n in H' by lia; rewrite -> H in H'; injection H'; intros; subst
| _ => lia || congruence || solve [repeat (f_equal; try lia)]
end.
Lemma lift0_id : forall M k, lift 0 k M = M.
Proof.
intros M.
elim M using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try (f_equal; auto; solve_all).
- now elim (leb k n).
Qed.
Lemma lift0_p : forall M, lift0 0 M = M.
intros; unfold lift in |- *.
apply lift0_id; easy.
Qed.
Lemma simpl_lift :
forall M n k p i,
i <= k + n ->
k <= i -> lift p i (lift n k M) = lift (p + n) k M.
Proof.
intros M.
elim M using term_forall_list_ind;
intros; simpl;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length;
try (rewrite -> H, ?H0, ?H1; auto); try (f_equal; auto; solve_all).
- elim (leb_spec k n); intros.
now rewrite lift_rel_ge.
now rewrite lift_rel_lt.
Qed.
Lemma simpl_lift0 : forall M n, lift0 (S n) M = lift0 1 (lift0 n M).
now intros; rewrite simpl_lift.
Qed.
Lemma permute_lift :
forall M n k p i,
i <= k ->
lift p i (lift n k M) = lift n (k + p) (lift p i M).
Proof.
intros M.
elim M using term_forall_list_ind;
intros; simpl;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length, ?Nat.add_assoc;
try solve [f_equal; auto; solve_all]; repeat nth_leb_simpl.
Qed.
Lemma permute_lift0 :
forall M k, lift0 1 (lift 1 k M) = lift 1 (S k) (lift0 1 M).
intros.
change (lift 1 0 (lift 1 k M) = lift 1 (1 + k) (lift 1 0 M))
in |- *.
rewrite permute_lift; easy.
Qed.
Lemma lift_isApp n k t : ~ isApp t = true -> ~ isApp (lift n k t) = true.
Proof.
induction t; auto.
intros.
simpl. destruct leb; auto.
Qed.
Lemma map_non_nil {A B} (f : A -> B) l : l <> nil -> map f l <> nil.
Proof.
intros. intro.
destruct l; try discriminate.
contradiction.
Qed.
Lemma isLambda_lift n k (bod : term) :
isLambda bod = true -> isLambda (lift n k bod) = true.
Proof. destruct bod; simpl; try congruence. Qed.
Hint Resolve lift_isApp map_non_nil isLambda_lift : all.
Hint Unfold compose.
Hint Transparent compose.
Lemma simpl_subst_rec :
forall M N n p k,
p <= n + k ->
k <= p -> subst N p (lift (List.length N + n) k M) = lift n k M.
Proof.
intros M. induction M using term_forall_list_ind;
intros; simpl;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length;
try solve [f_equal; auto; solve_all]; repeat nth_leb_simpl.
Qed.
Lemma simpl_subst :
forall N M n p, p <= n -> subst N p (lift0 (length N + n) M) = lift0 n M.
Proof. intros. rewrite simpl_subst_rec; auto. now rewrite Nat.add_0_r. lia. Qed.
Lemma lift_mkApps n k t l : lift n k (mkApps t l) = mkApps (lift n k t) (map (lift n k) l).
Proof.
revert n k t; induction l; intros n k t. auto.
simpl. rewrite (IHl n k (tApp t a)). reflexivity.
Qed.
Lemma commut_lift_subst_rec :
forall M N n p k,
k <= p ->
lift n k (subst N p M) = subst N (p + n) (lift n k M).
Proof.
intros M.
elim M using term_forall_list_ind;
intros; simpl; try easy;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length, ?Nat.add_assoc;
try solve [f_equal; auto; solve_all].
- repeat nth_leb_simpl.
rewrite -> simpl_lift by easy. f_equal; lia.
Qed.
Lemma commut_lift_subst :
forall M N k, subst N (S k) (lift0 1 M) = lift0 1 (subst N k M).
now intros; rewrite commut_lift_subst_rec.
Qed.
Lemma distr_lift_subst_rec :
forall M N n p k,
lift n (p + k) (subst N p M) =
subst (List.map (lift n k) N) p (lift n (p + length N + k) M).
Proof.
intros M.
elim M using term_forall_list_ind;
intros; match goal with
|- context [tRel _] => idtac
| |- _ => cbn -[plus]
end; try easy;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length, ?Nat.add_assoc;
try solve [f_equal; auto; solve_all].
- unfold subst at 1. unfold lift at 4.
repeat nth_leb_simpl.
rewrite nth_error_map in e0. rewrite e in e0.
revert e0. intros [= <-].
now rewrite (permute_lift x n0 k p 0).
Qed.
Lemma distr_lift_subst :
forall M N n k,
lift n k (subst0 N M) = subst0 (map (lift n k) N) (lift n (length N + k) M).
Proof.
intros. pattern k at 1 3 in |- *.
replace k with (0 + k); try easy.
apply distr_lift_subst_rec.
Qed.
Lemma distr_lift_subst10 :
forall M N n k,
lift n k (subst10 N M) = subst10 (lift n k N) (lift n (S k) M).
Proof.
intros; unfold subst in |- *.
pattern k at 1 3 in |- *.
replace k with (0 + k); try easy.
apply distr_lift_subst_rec.
Qed.
Lemma subst_mkApps u k t l :
subst u k (mkApps t l) = mkApps (subst u k t) (map (subst u k) l).
Proof.
revert u k t; induction l; intros u k t; auto.
intros. simpl mkApps at 1. simpl subst at 1 2.
now rewrite IHl.
Qed.
Lemma subst1_mkApps u k t l : subst1 u k (mkApps t l) = mkApps (subst1 u k t) (map (subst1 u k) l).
Proof.
apply subst_mkApps.
Qed.
Lemma distr_subst_rec :
forall M N (P : list term) n p,
subst P (p + n) (subst N p M) =
subst (map (subst P n) N) p (subst P (p + length N + n) M).
Proof.
intros M.
elim M using term_forall_list_ind;
intros; match goal with
|- context [tRel _] => idtac
| |- _ => simpl
end; try easy;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length, ?Nat.add_assoc;
try solve [f_equal; auto; solve_all].
- unfold subst at 2.
elim (leb_spec p n); intros; try easy.
+ destruct (nth_error_spec N (n - p)).
++ rewrite -> subst_rel_lt by lia.
erewrite subst_rel_eq; try easy.
2:rewrite -> nth_error_map, e; reflexivity.
now rewrite commut_lift_subst_rec. lia.
++ unfold subst at 4.
elim (leb_spec (p + length N + n0) n); intros; subst; try easy.
destruct (nth_error_spec P (n - (p + length N + n0))).
+++ erewrite subst_rel_eq. 2:eauto. 2:lia.
assert (p + length N + n0 = length (map (subst P n0) N) + (p + n0))
by (rewrite map_length; lia).
rewrite H1. rewrite simpl_subst_rec; eauto; try lia.
+++ rewrite !subst_rel_gt; rewrite ?map_length; try lia. f_equal; lia.
+++ rewrite subst_rel_lt; try easy.
rewrite -> subst_rel_gt; rewrite map_length. trivial. lia.
+ rewrite !subst_rel_lt; try easy.
Qed.
Lemma distr_subst :
forall P N M k,
subst P k (subst0 N M) = subst0 (map (subst P k) N) (subst P (length N + k) M).
Proof.
intros.
pattern k at 1 3 in |- *.
change k with (0 + k). hnf.
apply distr_subst_rec.
Qed.
Lemma lift_closed n k t : closedn k t -> lift n k t = t.
Proof.
revert k.
elim t using term_forall_list_ind; intros; try easy;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length;
unfold test_def in *;
simpl closed in *; try solve [simpl lift; simpl closed; f_equal; auto; toProp; solve_all]; try easy.
- rewrite lift_rel_lt; auto.
revert H. elim (Nat.ltb_spec n0 k); intros; try easy.
- simpl lift. f_equal. solve_all. unfold test_def in b. toProp. solve_all.
- simpl lift. f_equal. solve_all. unfold test_def in b. toProp. solve_all.
Qed.
Lemma closed_upwards {k t} k' : closedn k t -> k' >= k -> closedn k' t.
Proof.
revert k k'.
elim t using term_forall_list_ind; intros; try lia;
rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length;
simpl closed in *; unfold test_snd, test_def in *;
try solve [(try f_equal; simpl; repeat (toProp; solve_all); eauto)].
- elim (ltb_spec n k'); auto. intros.
apply ltb_lt in H. lia.
Qed.
Lemma subst_empty k a : subst [] k a = a.
Proof.
induction a in k |- * using term_forall_list_ind; simpl; try congruence;
try solve [f_equal; eauto; solve_all].
- elim (Nat.compare_spec k n); destruct (Nat.leb_spec k n); intros; try easy.
subst. rewrite Nat.sub_diag. simpl. rewrite Nat.sub_0_r. reflexivity.
assert (n - k > 0) by lia.
assert (exists n', n - k = S n'). exists (pred (n - k)). lia.
destruct H2. rewrite H2. simpl. now rewrite Nat.sub_0_r.
Qed.
Lemma lift_to_extended_list_k Γ k : forall k',
to_extended_list_k Γ (k' + k) = map (lift0 k') (to_extended_list_k Γ k).
Proof.
unfold to_extended_list_k.
intros k'. rewrite !reln_alt_eq !app_nil_r.
induction Γ in k, k' |- *; simpl; auto.
destruct a as [na [body|] ty].
now rewrite <- Nat.add_assoc, (IHΓ (k + 1) k').
simpl. now rewrite <- Nat.add_assoc, (IHΓ (k + 1) k'), map_app.
Qed.
Lemma simpl_subst_k (N : list term) (M : term) :
forall k p, p = #|N| -> subst N k (lift p k M) = M.
Proof.
intros. subst p. rewrite <- (Nat.add_0_r #|N|).
rewrite -> simpl_subst_rec, lift0_id; auto.
Qed.
Lemma subst_app_decomp l l' k t :
subst (l ++ l') k t = subst l' k (subst (List.map (lift0 (length l')) l) k t).
Proof.
induction t in k |- * using term_forall_list_ind; simpl; auto;
rewrite ?subst_mkApps; try change_Sk;
try (f_equal; rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length;
eauto; solve_all).
- repeat nth_leb_simpl.
rewrite nth_error_map in e0. rewrite e in e0.
injection e0; intros <-.
rewrite -> permute_lift by auto.
rewrite <- (Nat.add_0_r #|l'|).
rewrite -> simpl_subst_rec, lift0_id; auto with wf; try lia.
Qed.
Lemma subst_app_simpl l l' k t :
subst (l ++ l') k t = subst l k (subst l' (k + length l) t).
Proof.
induction t in k |- * using term_forall_list_ind; simpl; eauto;
rewrite ?subst_mkApps; try change_Sk;
try (f_equal; rewrite -> ?map_map_compose, ?compose_on_snd, ?compose_map_def, ?map_length, ?Nat.add_assoc;
eauto; solve_all; eauto).
- repeat nth_leb_simpl.
rewrite -> Nat.add_comm, simpl_subst; eauto.
Qed.
Lemma isLambda_subst (s : list term) k (bod : term) :
isLambda bod = true -> isLambda (subst s k bod) = true.
Proof.
intros. destruct bod; try discriminate. reflexivity.
Qed.
Lemma map_vass_map_def g l n k :
(mapi (fun i (d : def term) => vass (dname d) (lift0 i (dtype d)))
(map (map_def (lift n k) g) l)) =
(mapi (fun i d => map_decl (lift n (i + k)) d) (mapi (fun i (d : def term) => vass (dname d) (lift0 i (dtype d))) l)).
Proof.
rewrite mapi_mapi mapi_map. apply mapi_ext.
intros. unfold map_decl, vass; simpl; f_equal.
rewrite -> permute_lift. f_equal; lia. lia.
Qed.
Definition fix_context (m : mfixpoint term) : context :=
List.rev (mapi (fun i d => vass d.(dname) (lift0 i d.(dtype))) m).
Lemma shiftn_ext n f f' : (forall i, f i = f' i) -> forall t, shiftn n f t = shiftn n f' t.
Proof.
intros.
unfold shiftn. destruct Nat.ltb; congruence.
Qed.
Lemma rename_ext f f' : (forall i, f i = f' i) -> (forall t, rename f t = rename f' t).
Proof.
intros. revert f f' H.
elim t0 using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try solve [f_equal; solve_all].
- f_equal; auto. apply H0. intros.
now apply shiftn_ext.
- f_equal; auto. now apply H0, shiftn_ext.
- f_equal; auto. now apply H1, shiftn_ext.
- f_equal; auto. red in X. solve_all.
eapply map_def_eq_spec; auto. now apply b, shiftn_ext.
- f_equal; auto. red in X. solve_all.
eapply map_def_eq_spec; auto. now apply b, shiftn_ext.
Qed.
Definition lift_renaming n k :=
fun i =>
if Nat.leb k i then (* Lifted *) n + i
else i.
Lemma shiftn_lift_renaming n m k : forall i, shiftn m (lift_renaming n k) i = lift_renaming n (m + k) i.
Proof.
unfold lift_renaming, shiftn. intros i.
destruct (ltb_spec i m).
destruct (ltb_spec (m + k) i). lia.
destruct (leb_spec (m + k) i). lia. lia.
destruct (leb_spec (m + k) i).
destruct (leb_spec k (i - m)). lia. lia.
destruct (leb_spec k (i - m)). lia. lia.
Qed.
Lemma lift_rename n k t : lift n k t = rename (lift_renaming n k) t.
Proof.
revert n k.
elim t using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try solve [f_equal; solve_all].
- unfold lift_renaming.
elim (leb_spec k n); reflexivity.
- f_equal; eauto.
rewrite H0. eapply rename_ext. intros i. now rewrite shiftn_lift_renaming.
- f_equal; eauto.
rewrite H0. eapply rename_ext. intros i. now rewrite shiftn_lift_renaming.
- f_equal; eauto.
rewrite H1. eapply rename_ext. intros i. now rewrite shiftn_lift_renaming.
- f_equal; auto.
red in X. solve_all. apply map_def_eq_spec; auto.
rewrite b. apply rename_ext => i; now rewrite shiftn_lift_renaming.
- f_equal; auto.
red in X. solve_all. apply map_def_eq_spec; auto.
rewrite b. apply rename_ext => i; now rewrite shiftn_lift_renaming.
Qed.
Definition up k (s : nat -> term) :=
fun i =>
if k <=? i then rename (add k) (s (i - k))
else tRel i.
Require Import Morphisms.
Notation "`=1`" := (pointwise_relation _ Logic.eq) (at level 80).
Infix "=1" := (pointwise_relation _ Logic.eq) (at level 90).
Lemma shiftn_compose n f f' : shiftn n f ∘ shiftn n f' =1 shiftn n (f ∘ f').
Proof.
unfold shiftn. intros x.
elim (Nat.ltb_spec x n) => H.
- now rewrite (proj2 (Nat.ltb_lt x n)).
- destruct (Nat.ltb_spec (n + f' (x - n)) n).
lia.
assert (n + f' (x - n) - n = f' (x - n)) as ->. lia.
reflexivity.
Qed.
Lemma rename_compose f f' : rename f ∘ rename f' =1 rename (f ∘ f').
Proof.
intros x.
induction x in f, f' |- * using term_forall_list_ind; simpl; f_equal; auto; solve_all.
- rewrite map_map_compose. apply All_map_eq. solve_all.
- rewrite IHx2. apply rename_ext, shiftn_compose.
- rewrite IHx2. apply rename_ext, shiftn_compose.
- rewrite IHx3. apply rename_ext, shiftn_compose.
- rewrite map_map_compose; apply All_map_eq. solve_all.
unfold compose; rewrite on_snd_on_snd.
apply on_snd_eq_spec, H.
- rewrite map_map_compose; apply All_map_eq. solve_all.
unfold compose; rewrite map_def_map_def map_length.
apply map_def_eq_spec; auto.
rewrite b. apply rename_ext, shiftn_compose.
- rewrite map_map_compose; apply All_map_eq. solve_all.
unfold compose; rewrite map_def_map_def map_length.
apply map_def_eq_spec; auto.
rewrite b. apply rename_ext, shiftn_compose.
Qed.
Lemma up_up k k' s : up k (up k' s) =1 up (k + k') s.
Proof.
red. intros x. unfold up.
elim (Nat.leb_spec k x) => H.
- elim (Nat.leb_spec (k + k') x) => H'.
+ elim (Nat.leb_spec k' (x - k)) => H''.
++ rewrite Nat.sub_add_distr.
rewrite -> rename_compose. apply rename_ext. intros. lia.
++ simpl. lia.
+ edestruct (Nat.leb_spec k' (x - k)). lia.
simpl. f_equal. lia.
- elim (Nat.leb_spec (k + k') x) => H'; try f_equal; lia.
Qed.
Fixpoint inst s u :=
match u with
| tRel n => s n
| tEvar ev args => tEvar ev (List.map (inst s) args)
| tLambda na T M => tLambda na (inst s T) (inst (up 1 s) M)
| tApp u v => tApp (inst s u) (inst s v)
| tProd na A B => tProd na (inst s A) (inst (up 1 s) B)
| tLetIn na b ty b' => tLetIn na (inst s b) (inst s ty) (inst (up 1 s) b')
| tCase ind p c brs =>
let brs' := List.map (on_snd (inst s)) brs in
tCase ind (inst s p) (inst s c) brs'
| tProj p c => tProj p (inst s c)
| tFix mfix idx =>
let mfix' := List.map (map_def (inst s) (inst (up (List.length mfix) s))) mfix in
tFix mfix' idx
| tCoFix mfix idx =>
let mfix' := List.map (map_def (inst s) (inst (up (List.length mfix) s))) mfix in
tCoFix mfix' idx
| x => x
end.
Definition subst_fn (l : list term) :=
fun i =>
match List.nth_error l i with
| None => tRel (i - List.length l)
| Some t => t
end.
Lemma up_ext k s s' : s =1 s' -> up k s =1 up k s'.
Proof.
unfold up. intros Hs t. elim (Nat.leb_spec k t) => H; auto.
f_equal. apply Hs.
Qed.
Lemma inst_ext s s' : s =1 s' -> inst s =1 inst s'.
Proof.
intros Hs t. revert s s' Hs.
elim t using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try solve [f_equal; solve_all].
- f_equal; eauto. apply H0. now apply up_ext.
- f_equal; eauto. now apply H0, up_ext.
- f_equal; eauto. now apply H1, up_ext.
- f_equal; eauto. red in X. solve_all.
apply map_def_eq_spec; auto. now apply b, up_ext.
- f_equal; eauto. red in X. solve_all.
apply map_def_eq_spec; auto. now apply b, up_ext.
Qed.
Definition ren (f : nat -> nat) : nat -> term :=
fun i => tRel (f i).
Lemma ren_shiftn n f : up n (ren f) =1 ren (shiftn n f).
Proof.
unfold ren, up, shiftn.
intros i.
elim (Nat.ltb_spec i n) => H; elim (Nat.leb_spec n i) => H'; try lia; trivial.
Qed.
Instance proper_inst : Proper (`=1` ==> Logic.eq ==> Logic.eq) inst.
Proof.
intros f f' Hff' t t' ->. now apply inst_ext.
Qed.
Instance proper_ext_eq {A B} : Proper (`=1` ==> `=1` ==> iff) (@pointwise_relation A _ (@Logic.eq B)).
Proof.
intros f f' Hff' g g' Hgg'. split; intros.
- intros x. now rewrite <- Hff', <- Hgg'.
- intros x. now rewrite Hff' Hgg'.
Qed.
Lemma rename_inst f : rename f =1 inst (ren f).
Proof.
intros t. revert f.
elim t using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try solve [f_equal; solve_all].
- f_equal; eauto. now rewrite H0 -ren_shiftn.
- f_equal; eauto. now rewrite H0 -ren_shiftn.
- f_equal; eauto. now rewrite H1 -ren_shiftn.
- f_equal; eauto. solve_all. apply map_def_eq_spec; auto.
now rewrite b ren_shiftn.
- f_equal; eauto. solve_all. apply map_def_eq_spec; auto.
now rewrite b ren_shiftn.
Qed.
Hint Rewrite @rename_inst : sigma.
Instance rename_proper : Proper (`=1` ==> Logic.eq ==> Logic.eq) rename.
Proof. intros f f' Hff' t t' ->. now apply rename_ext. Qed.
(** Show the σ-calculus equations.
Additional combinators: [idsn n] for n-identity, [consn] for consing a parallel substitution.
*)
Delimit Scope sigma_scope with sigma.
Local Open Scope sigma_scope.
Definition substitution := nat -> term.
Bind Scope sigma_scope with substitution.
Notation "t '.[' σ ]" := (inst σ t) (at level 6, format "t .[ σ ]") : sigma_scope.
Definition subst_cons (t : term) (f : nat -> term) :=
fun i =>
match i with
| 0 => t
| S n => f n
end.
Notation " t ⋅ s " := (subst_cons t s) (at level 90) : sigma_scope.
Instance subst_cons_proper : Proper (Logic.eq ==> `=1` ==> `=1`) subst_cons.
Proof. intros x y -> f f' Hff'. intros i. destruct i; simpl; trivial. Qed.
Definition shift : nat -> term := tRel ∘ S.
Notation "↑" := shift : sigma_scope.
Definition subst_compose (σ τ : nat -> term) :=
fun i => (σ i).[τ].
Infix "∘" := subst_compose : sigma_scope.
Instance subst_compose_proper : Proper (`=1` ==> `=1` ==> `=1`) subst_compose.
Proof.
intros f f' Hff' g g' Hgg'. intros x. unfold subst_compose.
now rewrite Hgg' Hff'.
Qed.
Definition Up σ : substitution := tRel 0 ⋅ (σ ∘ ↑).
Notation "⇑ s" := (Up s) (at level 20).
Lemma up_Up σ : up 1 σ =1 ⇑ σ.
Proof.
unfold up.
intros i.
elim (Nat.leb_spec 1 i) => H.
- unfold subst_cons, shift. destruct i.
-- lia.
-- simpl. rewrite Nat.sub_0_r.
unfold subst_compose.
now rewrite rename_inst.
- red in H. depelim H. reflexivity.
depelim H.
Qed.
(** Simplify away [up 1] *)
Hint Rewrite up_Up : sigma.
Definition ids (x : nat) := tRel x.
Definition ren_id (x : nat) := x.
Lemma ren_id_ids : ren ren_id =1 ids.
Proof. reflexivity. Qed.
Lemma shiftn_id n : shiftn n ren_id =1 ren_id.
Proof.
intros i; unfold shiftn.
elim (Nat.ltb_spec i n) => H. reflexivity.
unfold ren_id. lia.
Qed.
Lemma rename_ren_id : rename ren_id =1 id.
Proof.
intros t. unfold id.
elim t using term_forall_list_ind; simpl in |- *; intros; try easy ;
try (try rewrite H; try rewrite H0 ; try rewrite H1 ; easy);
try solve [f_equal; solve_all].
- f_equal; auto. now rewrite shiftn_id.
- f_equal; auto. now rewrite shiftn_id.
- f_equal; auto. now rewrite shiftn_id.
- f_equal; auto. solve_all.
apply map_def_id_spec; auto.
now rewrite shiftn_id.
- f_equal; auto. solve_all.
apply map_def_id_spec; auto.
now rewrite shiftn_id.
Qed.
Lemma subst_ids t : t.[ids] = t.
Proof.
now rewrite -ren_id_ids -rename_inst rename_ren_id.
Qed.
Hint Rewrite subst_ids : sigma.
Lemma compose_ids_r σ : σ ∘ ids =1 σ.
Proof.
unfold subst_compose. intros i; apply subst_ids.
Qed.
Lemma compose_ids_l σ : ids ∘ σ =1 σ.
Proof. reflexivity. Qed.
Hint Rewrite compose_ids_r compose_ids_l : sigma.
Definition shiftk (k : nat) (x : nat) := tRel (k + x).
Notation "↑^ k" := (shiftk k) (at level 30, k at level 2, format "↑^ k") : sigma_scope.
Lemma shiftk_0 : shiftk 0 =1 ids.
Proof.
intros i. reflexivity.
Qed.
Definition subst_consn {A} (l : list A) (σ : nat -> A) :=
fun i =>
match List.nth_error l i with
| None => σ (i - List.length l)
| Some t => t
end.
Notation " t ⋅n s " := (subst_consn t s) (at level 40) : sigma_scope.
Lemma subst_consn_nil {A} (σ : nat -> A) : nil ⋅n σ =1 σ.
Proof.
intros i. unfold subst_consn. rewrite nth_error_nil.
now rewrite Nat.sub_0_r.
Qed.
Lemma subst_consn_subst_cons t l σ : (t :: l) ⋅n σ =1 (t ⋅ subst_consn l σ).
Proof.
intros i. unfold subst_consn. induction i; simpl; trivial.
Qed.
Lemma subst_consn_tip t σ : [t] ⋅n σ =1 (t ⋅ σ).
Proof. now rewrite subst_consn_subst_cons subst_consn_nil. Qed.
Instance subst_consn_proper {A} : Proper (Logic.eq ==> `=1` ==> `=1`) (@subst_consn A).
Proof.
intros ? l -> f f' Hff' i.
unfold subst_consn. destruct nth_error eqn:Heq; auto.
Qed.
Instance subst_consn_proper_ext {A} : Proper (Logic.eq ==> `=1` ==> Logic.eq ==> Logic.eq) (@subst_consn A).
Proof.
intros ? l -> f f' Hff' i i' <-.
unfold subst_consn. destruct nth_error eqn:Heq; auto.
Qed.
Fixpoint idsn n : list term :=
match n with
| 0 => []
| S n => idsn n ++ [tRel n]
end.
Definition Upn n σ := idsn n ⋅n (σ ∘ ↑^n).
Notation "⇑^ n σ" := (Upn n σ) (at level 30, n at level 2, format "⇑^ n σ") : sigma_scope.
Lemma Upn_eq n σ : Upn n σ = idsn n ⋅n (σ ∘ ↑^n).
Proof. reflexivity. Qed.
Lemma Upn_proper : Proper (Logic.eq ==> `=1` ==> `=1`) Upn.
Proof. intros ? ? -> f g Hfg. unfold Upn. now rewrite Hfg. Qed.
Definition subst_cons_gen {A} (t : A) (f : nat -> A) :=
fun i =>
match i with
| 0 => t
| S n => f n
end.
Instance subst_cons_gen_proper {A} : Proper (Logic.eq ==> `=1` ==> `=1`) (@subst_cons_gen A).
Proof. intros x y <- f g Hfg i. destruct i; simpl; auto. Qed.
Lemma subst_consn_subst_cons_gen {A} (t : A) l σ : subst_consn (t :: l) σ =1 (subst_cons_gen t (l ⋅n σ)).
Proof.
intros i. unfold subst_consn. induction i; simpl; trivial.
Qed.
Lemma subst_consn_app {A} {l l' : list A} {σ} : (l ++ l') ⋅n σ =1 l ⋅n (l' ⋅n σ).
Proof.
induction l; simpl; auto.
- now rewrite subst_consn_nil.
- now rewrite !subst_consn_subst_cons_gen IHl.
Qed.
Lemma subst_consn_ge {A} {l : list A} {i σ} : #|l| <= i -> (l ⋅n σ) i = σ (i - #|l|).
Proof.
induction l in i, σ |- *; simpl.
- now rewrite subst_consn_nil.
- rewrite subst_consn_subst_cons_gen.
intros H. destruct i; depelim H. simpl.
unfold subst_consn.
now rewrite (proj2 (nth_error_None l #|l|)).
simpl. now apply IHl.
Qed.
Lemma subst_consn_lt {A} {l : list A} {i} :