forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICParallelReductionConfluence.v
4434 lines (4029 loc) · 177 KB
/
PCUICParallelReductionConfluence.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. *)
Set Warnings "-notation-overridden".
Require Import ssreflect ssrbool.
From MetaCoq Require Import LibHypsNaming.
From Equations Require Import Equations.
From Coq Require Import Bool String List Program BinPos Compare_dec Omega Utf8 String Lia.
From MetaCoq.Template Require Import config utils.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction PCUICSize
PCUICLiftSubst PCUICUnivSubst PCUICTyping PCUICReduction PCUICWeakening PCUICSubstitution
PCUICReflect PCUICClosed PCUICParallelReduction.
(* Type-valued relations. *)
Require Import CRelationClasses.
Require CMorphisms.
Require Import Equations.Type.Relation Equations.Type.Relation_Properties.
Require Import Equations.Prop.DepElim.
Derive Signature for pred1 All2_local_env.
Local Set Keyed Unification.
Set Asymmetric Patterns.
Notation "'∃' x .. y , P" := (sigT (fun x => .. (sigT (fun y => P%type)) ..))
(at level 200, x binder, y binder, right associativity,
format "'[ ' '[ ' ∃ x .. y ']' , '/' P ']'") : type_scope.
Require Import Morphisms.
Instance ren_ext : Morphisms.Proper (`=1` ==> `=1`)%signature ren.
Proof.
reduce_goal. unfold ren. now rewrite H.
Qed.
Lemma shiftn0 r : shiftn 0 r =1 r.
Proof.
intros x.
unfold shiftn. destruct (Nat.ltb_spec x 0). lia.
rewrite Nat.sub_0_r. lia.
Qed.
Lemma shiftnS n r : shiftn (S n) r =1 shiftn 1 (shiftn n r).
Proof.
intros x. unfold shiftn.
destruct x. simpl. auto.
simpl. rewrite Nat.sub_0_r.
destruct (Nat.ltb_spec x n).
destruct (Nat.ltb_spec (S x) (S n)); auto. lia.
destruct (Nat.ltb_spec (S x) (S n)); auto. lia.
Qed.
Definition rename_context r Γ :=
fold_context (fun k => rename (shiftn k r)) Γ.
Local Open Scope sigma_scope.
Definition inst_context s Γ :=
fold_context (fun k => inst (⇑^k s)) Γ.
Definition rename_context_snoc0 r Γ d : rename_context r (d :: Γ) = rename_context r Γ ,, map_decl (rename (shiftn #|Γ| r)) d.
Proof. unfold rename_context. now rewrite fold_context_snoc0. Qed.
Hint Rewrite rename_context_snoc0 : lift.
Lemma rename_context_snoc r Γ d : rename_context r (Γ ,, d) = rename_context r Γ ,, map_decl (rename (shiftn #|Γ| r)) d.
Proof.
unfold snoc. apply rename_context_snoc0.
Qed.
Hint Rewrite rename_context_snoc : lift.
Lemma rename_context_alt r Γ :
rename_context r Γ =
mapi (fun k' d => map_decl (rename (shiftn (Nat.pred #|Γ| - k') r)) d) Γ.
Proof.
unfold rename_context. apply fold_context_alt.
Qed.
Definition inst_context_snoc0 s Γ d :
inst_context s (d :: Γ) =
inst_context s Γ ,, map_decl (inst (⇑^#|Γ| s)) d.
Proof. unfold inst_context. now rewrite fold_context_snoc0. Qed.
Hint Rewrite inst_context_snoc0 : lift.
Lemma inst_context_snoc s Γ d : inst_context s (Γ ,, d) = inst_context s Γ ,, map_decl (inst (⇑^#|Γ| s)) d.
Proof.
unfold snoc. apply inst_context_snoc0.
Qed.
Hint Rewrite inst_context_snoc : lift.
Lemma inst_context_alt s Γ :
inst_context s Γ =
mapi (fun k' d => map_decl (inst (⇑^(Nat.pred #|Γ| - k') s)) d) Γ.
Proof.
unfold inst_context. apply fold_context_alt.
Qed.
Lemma inst_context_length s Γ : #|inst_context s Γ| = #|Γ|.
Proof. apply fold_context_length. Qed.
Hint Rewrite @subst_consn_nil @subst_consn_tip : sigma.
Lemma subst_consn_shiftn n l σ : #|l| = n -> ↑^n ∘ (l ⋅n σ) =1 σ.
Proof.
induction n in l |- *; simpl; intros; autorewrite with sigma.
- destruct l; try discriminate. simpl; autorewrite with sigma. reflexivity.
- destruct l; try discriminate. simpl in *.
rewrite subst_consn_subst_cons.
simpl; autorewrite with sigma. apply IHn. lia.
Qed.
Lemma shiftn_consn_idsn n σ : ↑^n ∘ ⇑^n σ =1 σ ∘ ↑^n.
Proof.
unfold Upn. rewrite subst_consn_shiftn. reflexivity.
now rewrite idsn_length.
Qed.
Lemma subst10_inst a b τ : b {0 := a}.[τ] = (b.[⇑ τ] {0 := a.[τ]}).
Proof.
unfold subst10. simpl. rewrite !subst_inst.
now unfold Upn, Up; autorewrite with sigma.
Qed.
Lemma lift_renaming_0 k : ren (lift_renaming k 0) = ren (Nat.add k).
Proof. reflexivity. Qed.
Lemma lift0_inst n t : lift0 n t = t.[↑^n].
Proof. by rewrite lift_rename rename_inst lift_renaming_0 -ren_shiftk. Qed.
Lemma map_vass_map_def g l r :
(mapi (fun i (d : def term) => vass (dname d) (lift0 i (dtype d)))
(map (map_def (rename r) g) l)) =
(mapi (fun i d => map_decl (rename (shiftn i r)) 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 !lift0_inst. rewrite !rename_inst.
autorewrite with sigma. rewrite -ren_shiftn up_Upn.
rewrite shiftn_consn_idsn. reflexivity.
Qed.
Lemma rename_fix_context r :
∀ (mfix : list (def term)),
fix_context (map (map_def (rename r) (rename (shiftn #|mfix| r))) mfix) =
rename_context r (fix_context mfix).
Proof.
intros mfix. unfold fix_context.
rewrite map_vass_map_def rev_mapi.
fold (fix_context mfix).
rewrite (rename_context_alt r (fix_context mfix)).
unfold map_decl. now rewrite mapi_length fix_context_length.
Qed.
Lemma map_vass_map_def_inst g l s :
(mapi (fun i (d : def term) => vass (dname d) (lift0 i (dtype d)))
(map (map_def (inst s) g) l)) =
(mapi (fun i d => map_decl (inst (⇑^i s)) 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 !lift0_inst.
autorewrite with sigma.
rewrite shiftn_consn_idsn. reflexivity.
Qed.
Lemma inst_fix_context:
forall (mfix : list (def term)) s,
fix_context (map (map_def (inst s) (inst (⇑^#|mfix| s))) mfix) =
inst_context s (fix_context mfix).
Proof.
intros mfix s. unfold fix_context.
rewrite map_vass_map_def_inst rev_mapi.
fold (fix_context mfix).
rewrite (inst_context_alt s (fix_context mfix)).
now rewrite /subst_decl mapi_length fix_context_length.
Qed.
Lemma mkApps_eq_decompose_app_rec {f args t l} :
mkApps f args = t ->
~~ isApp f ->
match decompose_app_rec t l with
| (f', args') => f' = f /\ mkApps t l = mkApps f' args'
end.
Proof.
revert f t l.
induction args using rev_ind; simpl.
- intros * -> **. rewrite atom_decompose_app; auto.
- intros. rewrite <- mkApps_nested in H.
specialize (IHargs f).
destruct (isApp t) eqn:Heq.
destruct t; try discriminate.
simpl in Heq. noconf H. simpl.
specialize (IHargs (mkApps f args) (x :: l) eq_refl H0).
destruct decompose_app_rec. intuition.
subst t.
simpl in Heq. discriminate.
Qed.
Lemma mkApps_eq_decompose' {f args t} :
mkApps f args = t ->
~~ isApp f ->
match decompose_app t with
| (f', args') => f' = f /\ t = mkApps f' args'
end.
Proof.
intros. apply (@mkApps_eq_decompose_app_rec f args t []); auto.
Qed.
Lemma fst_decompose_app_rec t l : fst (decompose_app_rec t l) = fst (decompose_app t).
Proof.
induction t in l |- *; simpl; auto. rewrite IHt1.
unfold decompose_app. simpl. now rewrite (IHt1 [t2]).
Qed.
Section FoldFix.
Context (rho : context -> term -> term).
Context (Γ : context).
Fixpoint fold_fix_context acc m :=
match m with
| [] => acc
| def :: fixd =>
fold_fix_context (vass def.(dname) (lift0 #|acc| (rho Γ def.(dtype))) :: acc) fixd
end.
Fixpoint fold_ctx_over Γ' :=
match Γ' with
| [] => []
| {| decl_name := na; decl_body := None; decl_type := T |} :: Γ' =>
let Γ'' := fold_ctx_over Γ' in
vass na (rho (Γ ,,, Γ'') T) :: Γ''
| {| decl_name := na; decl_body := Some b; decl_type := T |} :: Γ' =>
let Γ'' := fold_ctx_over Γ' in
vdef na (rho (Γ ,,, Γ'') b) (rho (Γ ,,, Γ'') T) :: Γ''
end.
End FoldFix.
Lemma term_forall_ctx_list_ind :
forall (P : term -> Type),
(forall (n : nat), P (tRel n)) ->
(forall (i : ident), P (tVar i)) ->
(forall (n : nat) (l : list term), All (P) l -> P (tEvar n l)) ->
(forall s, P (tSort s)) ->
(forall (n : name) (t : term), P t -> forall t0 : term, P t0 -> P (tProd n t t0)) ->
(forall (n : name) (t : term), P t -> forall t0 : term, P t0 -> P (tLambda n t t0)) ->
(forall (n : name) (t : term),
P t -> forall t0 : term, P t0 -> forall t1 : term, P t1 ->
P (tLetIn n t t0 t1)) ->
(forall (t u : term),
(forall t', size t' < size (tApp t u) -> P t') ->
P t -> P u -> P (tApp t u)) ->
(forall (s : String.string) (u : list Level.t), P (tConst s u)) ->
(forall (i : inductive) (u : list Level.t), P (tInd i u)) ->
(forall (i : inductive) (n : nat) (u : list Level.t), P (tConstruct i n u)) ->
(forall (p : inductive * nat) (t : term),
P t -> forall t0 : term, P t0 -> forall l : list (nat * term),
tCaseBrsProp (P) l -> P (tCase p t t0 l)) ->
(forall (s : projection) (t : term), P t -> P (tProj s t)) ->
(forall (m : mfixpoint term) (n : nat),
tFixProp P P m -> P (tFix m n)) ->
(forall (m : mfixpoint term) (n : nat),
tFixProp (P) P m -> P (tCoFix m n)) ->
forall (t : term), P t.
Proof.
intros.
revert t. set(foo:=Tactics.the_end_of_the_section). intros.
Subterm.rec_wf_rel aux t (MR lt size). simpl. clear H0.
assert (auxl : forall {A} (l : list A) (f : A -> term), list_size (fun x => size (f x)) l < size pr1 ->
All (fun x => P (f x)) l).
{ induction l; constructor. eapply aux. red. simpl in H. lia. apply IHl. simpl in H. lia. }
assert (forall m, list_size (fun x : def term => size (dtype x)) m < S (mfixpoint_size size m)).
{ clear. unfold mfixpoint_size, def_size. induction m. simpl. auto. simpl. lia. }
assert (forall m, list_size (fun x : def term => size (dbody x)) m < S (mfixpoint_size size m)).
{ clear. unfold mfixpoint_size, def_size. induction m. simpl. auto. simpl. lia. }
move aux at top. move auxl at top.
!destruct pr1; eauto;
try match reverse goal with
|- context [tFix _ _] => idtac
| H : _ |- _ => solve [apply H; (eapply aux || eapply auxl); red; simpl; try lia]
end.
eapply X12; try (apply aux; red; simpl; lia).
red. apply All_pair. split; apply auxl; simpl; auto.
eapply X13; try (apply aux; red; simpl; lia).
red. apply All_pair. split; apply auxl; simpl; auto.
Defined.
Lemma atom_mkApps {t l} : atom (mkApps t l) -> atom t /\ l = [].
Proof.
induction l in t |- *; simpl; auto.
intros. destruct (IHl _ H). discriminate.
Qed.
Lemma pred_atom_mkApps {t l} : pred_atom (mkApps t l) -> pred_atom t /\ l = [].
Proof.
induction l in t |- *; simpl; auto.
intros. destruct (IHl _ H). discriminate.
Qed.
Ltac finish_discr :=
repeat PCUICAstUtils.finish_discr ||
match goal with
| [ H : atom (mkApps _ _) |- _ ] => apply atom_mkApps in H; intuition subst
| [ H : pred_atom (mkApps _ _) |- _ ] => apply pred_atom_mkApps in H; intuition subst
end.
Definition application_atom t :=
match t with
| tVar _
| tSort _
| tInd _ _
| tConstruct _ _ _
| tLambda _ _ _ => true
| _ => false
end.
Lemma application_atom_mkApps {t l} : application_atom (mkApps t l) -> application_atom t /\ l = [].
Proof.
induction l in t |- *; simpl; auto.
intros. destruct (IHl _ H). discriminate.
Qed.
Ltac solve_discr :=
(try (progress (prepare_discr; finish_discr; cbn [mkApps] in * )));
(try (match goal with
| [ H : is_true (application_atom _) |- _ ] => discriminate
| [ H : is_true (atom _) |- _ ] => discriminate
| [ H : is_true (atom (mkApps _ _)) |- _ ] => destruct (atom_mkApps H); subst; try discriminate
| [ H : is_true (pred_atom _) |- _ ] => discriminate
| [ H : is_true (pred_atom (mkApps _ _)) |- _ ] => destruct (pred_atom_mkApps H); subst; try discriminate
| [ H : is_true (application_atom (mkApps _ _)) |- _ ] =>
destruct (application_atom_mkApps H); subst; try discriminate
end)).
Section Confluence.
Lemma pred_mkApps Σ Γ Δ M0 M1 N0 N1 :
pred1 Σ Γ Δ M0 M1 ->
All2 (pred1 Σ Γ Δ) N0 N1 ->
pred1 Σ Γ Δ (mkApps M0 N0) (mkApps M1 N1).
Proof.
intros.
induction X0 in M0, M1, X |- *. auto.
simpl. eapply IHX0. now eapply pred_app.
Qed.
Lemma pred_snd_nth:
∀ (Σ : global_env) (Γ Δ : context) (c : nat) (brs1 brs' : list (nat * term)),
All2
(on_Trel (pred1 Σ Γ Δ) snd) brs1
brs' ->
pred1_ctx Σ Γ Δ ->
pred1 Σ Γ Δ
(snd (nth c brs1 (0, tDummy)))
(snd (nth c brs' (0, tDummy))).
Proof.
intros Σ Γ Δ c brs1 brs' brsl. intros.
induction brsl in c |- *; simpl. destruct c; now apply pred1_refl_gen.
destruct c; auto.
Qed.
Lemma mkApps_eq_decompose_app {t t' l l'} :
mkApps t l = mkApps t' l' ->
decompose_app_rec t l = decompose_app_rec t' l'.
Proof.
induction l in t, t', l' |- *; simpl.
- intros ->. rewrite !decompose_app_rec_mkApps.
now rewrite app_nil_r.
- intros H. apply (IHl _ _ _ H).
Qed.
Lemma pred1_mkApps_tConstruct (Σ : global_env) (Γ Δ : context)
ind pars k (args : list term) c :
pred1 Σ Γ Δ (mkApps (tConstruct ind pars k) args) c ->
{args' : list term & (c = mkApps (tConstruct ind pars k) args') * (All2 (pred1 Σ Γ Δ) args args') }%type.
Proof with solve_discr.
revert c. induction args using rev_ind; intros; simpl in *.
depelim X... exists []. intuition auto.
intros. rewrite <- mkApps_nested in X.
depelim X... simpl in H; noconf H. solve_discr.
prepare_discr. apply mkApps_eq_decompose_app in H.
rewrite !decompose_app_rec_mkApps in H. noconf H.
destruct (IHargs _ X1) as [args' [-> Hargs']].
exists (args' ++ [N1])%list.
rewrite <- mkApps_nested. intuition auto.
eapply All2_app; auto.
Qed.
Lemma pred1_mkApps_refl_tConstruct (Σ : global_env) Γ Δ i k u l l' :
pred1 Σ Γ Δ (mkApps (tConstruct i k u) l) (mkApps (tConstruct i k u) l') ->
All2 (pred1 Σ Γ Δ) l l'.
Proof.
intros.
eapply pred1_mkApps_tConstruct in X. destruct X.
destruct p. now eapply mkApps_eq_inj in e as [_ <-].
Qed.
Lemma pred1_mkApps_tInd (Σ : global_env) (Γ Δ : context)
ind u (args : list term) c :
pred1 Σ Γ Δ (mkApps (tInd ind u) args) c ->
{args' : list term & (c = mkApps (tInd ind u) args') * (All2 (pred1 Σ Γ Δ) args args') }%type.
Proof with solve_discr.
revert c. induction args using rev_ind; intros; simpl in *.
depelim X... exists []. intuition auto.
intros. rewrite <- mkApps_nested in X.
depelim X... simpl in H; noconf H. solve_discr.
prepare_discr. apply mkApps_eq_decompose_app in H.
rewrite !decompose_app_rec_mkApps in H. noconf H.
destruct (IHargs _ X1) as [args' [-> Hargs']].
exists (args' ++ [N1])%list.
rewrite <- mkApps_nested. intuition auto.
eapply All2_app; auto.
Qed.
Lemma pred1_mkApps_tConst_axiom (Σ : global_env) (Γ Δ : context)
cst u (args : list term) cb c :
declared_constant Σ cst cb -> cst_body cb = None ->
pred1 Σ Γ Δ (mkApps (tConst cst u) args) c ->
{args' : list term & (c = mkApps (tConst cst u) args') * (All2 (pred1 Σ Γ Δ) args args') }%type.
Proof with solve_discr.
revert c. induction args using rev_ind; intros; simpl in *.
depelim X...
- red in H, isdecl. rewrite isdecl in H; noconf H.
simpl in H. injection H. intros. subst. congruence.
- exists []. intuition auto.
- rewrite <- mkApps_nested in X.
depelim X...
* simpl in H1; noconf H1. solve_discr.
* prepare_discr. apply mkApps_eq_decompose_app in H1.
rewrite !decompose_app_rec_mkApps in H1. noconf H1.
* destruct (IHargs _ H H0 X1) as [args' [-> Hargs']].
exists (args' ++ [N1])%list.
rewrite <- mkApps_nested. intuition auto.
eapply All2_app; auto.
Qed.
Lemma pred1_mkApps_tFix_inv (Σ : global_env) (Γ Δ : context)
mfix0 idx (args0 : list term) c :
pred1 Σ Γ Δ (mkApps (tFix mfix0 idx) args0) c ->
({ mfix1 & { args1 : list term &
(c = mkApps (tFix mfix1 idx) args1) *
All2_prop2_eq Γ Δ (Γ ,,, fix_context mfix0) (Δ ,,, fix_context mfix1)
dtype dbody
(fun x => (dname x, rarg x))
(pred1 Σ) mfix0 mfix1 *
(All2 (pred1 Σ Γ Δ) ) args0 args1 } }%type) +
({ mfix1 & { args1 & { narg & { fn &
((unfold_fix mfix1 idx = Some (narg, fn)) *
All2_prop2_eq Γ Δ (Γ ,,, fix_context mfix0) (Δ ,,, fix_context mfix1)
dtype dbody
(fun x => (dname x, rarg x))
(pred1 Σ) mfix0 mfix1 *
(is_constructor narg args1 = true) *
(All2 (pred1 Σ Γ Δ) args0 args1) *
(c = mkApps fn args1)) } } } })%type.
Proof with solve_discr.
intros pred. remember (mkApps _ _) as fixt. revert mfix0 idx args0 Heqfixt.
induction pred; intros; solve_discr.
- right. exists mfix1, args1, narg, fn. intuition eauto.
- destruct args0 using rev_ind. noconf Heqfixt. clear IHargs0.
rewrite <- mkApps_nested in Heqfixt. noconf Heqfixt.
clear IHpred2. specialize (IHpred1 _ _ _ eq_refl).
destruct IHpred1 as [[? [? ?]]|[? [? [? [? ?]]]]].
-- left. eexists _. eexists (_ ++ [N1])%list. rewrite <- mkApps_nested.
intuition eauto. simpl. subst M1. reflexivity.
eapply All2_app; eauto.
-- right. eexists x, (x0 ++ [N1])%list, x1, x2. intuition auto.
clear -b1.
generalize [N1]. unfold is_constructor in *.
destruct nth_error eqn:Heq.
pose proof (nth_error_Some_length Heq).
intros. rewrite nth_error_app_lt; auto. rewrite Heq; auto.
intros. discriminate.
eapply All2_app; eauto.
now rewrite <- mkApps_nested, b.
- left.
eexists mfix1, []. intuition auto.
- subst t. solve_discr.
Qed.
Lemma pred1_mkApps_tFix_refl_inv (Σ : global_env) (Γ Δ : context)
mfix0 mfix1 idx0 idx1 (args0 args1 : list term) :
pred1 Σ Γ Δ (mkApps (tFix mfix0 idx0) args0) (mkApps (tFix mfix1 idx1) args1) ->
(All2_prop2_eq Γ Δ (Γ ,,, fix_context mfix0) (Δ ,,, fix_context mfix1)
dtype dbody
(fun x => (dname x, rarg x))
(pred1 Σ) mfix0 mfix1 *
(All2 (pred1 Σ Γ Δ) ) args0 args1).
Proof with solve_discr.
remember (mkApps _ _) as fixt.
remember (mkApps _ args1) as fixt'.
intros pred. revert mfix0 mfix1 idx0 idx1 args0 args1 Heqfixt Heqfixt'.
induction pred; intros; solve_discr.
- (* body not a lambda *)
move: e. rewrite /unfold_fix.
destruct nth_error eqn:Hnth => //.
case isl: isLambda => // [=] => ? ?; subst.
destruct (dbody d); try discriminate.
simpl in Heqfixt'.
eapply mkApps_eq_inj in Heqfixt' => //.
intuition discriminate.
- destruct args0 using rev_ind; noconf Heqfixt. clear IHargs0.
rewrite <- mkApps_nested in Heqfixt. noconf Heqfixt.
destruct args1 using rev_ind; noconf Heqfixt'. clear IHargs1.
rewrite <- mkApps_nested in Heqfixt'. noconf Heqfixt'.
clear IHpred2. specialize (IHpred1 _ _ _ _ _ _ eq_refl eq_refl).
destruct IHpred1 as [? ?]. red in a.
unfold All2_prop2_eq. split. apply a. apply All2_app; auto.
- constructor; auto.
- subst. solve_discr.
Qed.
Lemma pred1_mkApps_tCoFix_inv (Σ : global_env) (Γ Δ : context)
mfix0 idx (args0 : list term) c :
pred1 Σ Γ Δ (mkApps (tCoFix mfix0 idx) args0) c ->
∃ mfix1 args1,
(c = mkApps (tCoFix mfix1 idx) args1) *
All2_prop2_eq Γ Δ (Γ ,,, fix_context mfix0) (Δ ,,, fix_context mfix1) dtype dbody
(fun x => (dname x, rarg x))
(pred1 Σ) mfix0 mfix1 *
(All2 (pred1 Σ Γ Δ) ) args0 args1.
Proof with solve_discr.
intros pred. remember (mkApps _ _) as fixt. revert mfix0 idx args0 Heqfixt.
induction pred; intros; solve_discr.
- destruct args0 using rev_ind. noconf Heqfixt. clear IHargs0.
rewrite <- mkApps_nested in Heqfixt. noconf Heqfixt.
clear IHpred2. specialize (IHpred1 _ _ _ eq_refl).
destruct IHpred1 as [? [? [[-> ?] ?]]].
eexists x, (x0 ++ [N1])%list. intuition auto.
now rewrite <- mkApps_nested.
eapply All2_app; eauto.
- exists mfix1, []; intuition (simpl; auto).
- subst t; solve_discr.
Qed.
Lemma pred1_mkApps_tCoFix_refl_inv (Σ : global_env) (Γ Δ : context)
mfix0 mfix1 idx (args0 args1 : list term) :
pred1 Σ Γ Δ (mkApps (tCoFix mfix0 idx) args0) (mkApps (tCoFix mfix1 idx) args1) ->
All2_prop2_eq Γ Δ (Γ ,,, fix_context mfix0) (Δ ,,, fix_context mfix1) dtype dbody
(fun x => (dname x, rarg x))
(pred1 Σ) mfix0 mfix1 *
(All2 (pred1 Σ Γ Δ)) args0 args1.
Proof with solve_discr.
intros pred. remember (mkApps _ _) as fixt.
remember (mkApps _ args1) as fixt'.
revert mfix0 mfix1 idx args0 args1 Heqfixt Heqfixt'.
induction pred; intros; symmetry in Heqfixt; solve_discr.
- destruct args0 using rev_ind. noconf Heqfixt. clear IHargs0.
rewrite <- mkApps_nested in Heqfixt. noconf Heqfixt.
clear IHpred2.
symmetry in Heqfixt'.
destruct args1 using rev_ind. discriminate. rewrite <- mkApps_nested in Heqfixt'.
noconf Heqfixt'.
destruct (IHpred1 _ _ _ _ _ eq_refl eq_refl) as [H H'].
unfold All2_prop2_eq. split. apply H. apply All2_app; auto.
- repeat finish_discr.
unfold All2_prop2_eq. intuition (simpl; auto).
- subst t; solve_discr.
Qed.
Hint Constructors pred1 : pcuic.
Lemma All2_prop_eq_All2 {A B} {Σ Γ Δ} {f : A -> term} {g : A -> B} args0 args1 args3 :
All2_prop_eq Γ Δ f g
(λ (Γ Δ : context) (x y : term),
(pred1 Σ Γ Δ x y *
(∀ Δ' r, pred1 Σ Γ Δ' x r →
∃ Δ'' v, pred1 Σ Δ Δ'' y v * pred1 Σ Δ' Δ'' r v))%type)
args0 args1 ->
All2 (on_Trel_eq (pred1 Σ Γ Δ) f g) args0 args3 ->
All2 (fun x y =>
(∃ Δ'' v, (pred1 Σ Δ Δ'' (f x) v * pred1 Σ Δ Δ'' (f y) v)) * (g x = g y))%type args1 args3.
Proof.
intros HP H. red in HP.
induction HP in args3, H |- *; depelim H; constructor; eauto.
unfold on_Trel in *. destruct r as [[pr Hr] Heq].
destruct p as [pr0 eq0]. !intros.
destruct (Hr _ _ pr0). split. exists x0. destruct s. exists x1. intuition auto.
now rewrite <- Heq.
Qed.
Lemma All2_prop_All2 {Σ Γ Δ} args0 args1 args3 :
All2_prop Γ
(λ (Γ : context) (x y : term),
(pred1 Σ Γ Δ x y *
(∀ Δ' r, pred1 Σ Γ Δ' x r →
∃ Δ'' v, pred1 Σ Δ Δ'' y v * pred1 Σ Δ' Δ'' r v))%type)
args0 args1 ->
All2 (pred1 Σ Γ Δ) args0 args3 ->
All2 (fun x y =>
∃ Δ'' v, pred1 Σ Δ Δ'' x v * pred1 Σ Δ Δ'' y v)%type args1 args3.
Proof.
intros HP H. red in HP.
induction HP in args3, H |- *; depelim H; constructor; eauto.
unfold on_Trel in *.
!intros.
destruct r as [r Hr].
exact (Hr _ _ p).
Qed.
Definition on_Trel2 {A B} (R : A -> A -> Type) (f : B -> A) : B -> A -> Type :=
fun x y => R (f x) y.
Lemma All2_on_Trel_eq_impl {A B} Σ Γ Δ {f : A -> term} {g : A -> B} {x y} :
All2 (on_Trel_eq (pred1 Σ Γ Δ) f g) x y ->
All2 (on_Trel (pred1 Σ Γ Δ) f) x y.
Proof.
induction 1; constructor; intuition auto.
Qed.
Hint Resolve All2_on_Trel_eq_impl : pcuic.
Lemma isConstruct_app_inv t :
isConstruct_app t = true ->
∃ ind k u args, t = mkApps (tConstruct ind k u) args.
Proof.
induction t; try discriminate.
- unfold isConstruct_app. unfold decompose_app. simpl.
rewrite fst_decompose_app_rec. intros.
specialize (IHt1 H). destruct IHt1 as [ind [k [u [args ->]]]].
rewrite decompose_app_mkApps in H; auto. simpl in H.
exists ind, k, u, (args ++ [t2])%list.
now rewrite <- mkApps_nested.
- intros H.
now exists ind, n, ui, [].
Qed.
Derive NoConfusion for global_decl.
Hint Resolve pred1_refl : pcuic.
Lemma All2_nth_error_Some_right {A} {P : A -> A -> Type} {l l'} n t :
All2 P l l' ->
nth_error l' n = Some t ->
{ t' : A & (nth_error l n = Some t') * P t' t}%type.
Proof.
intros Hall. revert n.
induction Hall; destruct n; simpl; try congruence. intros [= ->]. exists x. intuition auto.
eauto.
Qed.
Lemma All2_local_env_skipn P l l' n :
All2_local_env P l l' ->
All2_local_env P (skipn n l) (skipn n l').
Proof.
induction n in l, l' |- *. auto.
intros.
destruct l; depelim X.
- constructor.
- apply IHn; auto.
- apply IHn; auto.
Qed.
Equations construct_cofix_discr (t : term) : bool :=
construct_cofix_discr (tConstruct _ _ _) => true;
construct_cofix_discr (tCoFix _ _) => true;
construct_cofix_discr _ => false.
Transparent construct_cofix_discr.
Equations discr_construct_cofix (t : term) : Prop :=
{ | tConstruct _ _ _ => False;
| tCoFix _ _ => False;
| _ => True }.
Transparent discr_construct_cofix.
Inductive construct_cofix_view : term -> Set :=
| construct_cofix_construct c u i : construct_cofix_view (tConstruct c u i)
| construct_cofix_cofix mfix idx : construct_cofix_view (tCoFix mfix idx)
| construct_cofix_other t : discr_construct_cofix t -> construct_cofix_view t.
Equations view_construct_cofix (t : term) : construct_cofix_view t :=
{ | tConstruct ind u i => construct_cofix_construct ind u i;
| tCoFix mfix idx => construct_cofix_cofix mfix idx;
| t => construct_cofix_other t I }.
Equations isConstruct (t : term) : bool :=
isConstruct (tConstruct _ _ _) => true;
isConstruct _ => false.
Transparent isConstruct.
Inductive construct_view : term -> Set :=
| construct_construct c u i : construct_view (tConstruct c u i)
| construct_other t : ~~ isConstruct t -> construct_view t.
Equations view_construct (t : term) : construct_view t :=
{ | tConstruct ind u i => construct_construct ind u i;
| t => construct_other t eq_refl }.
Fixpoint isFix_app (t : term) : bool :=
match t with
| tApp (tFix _ _) _ => true
| tApp f _ => isFix_app f
| _ => false
end.
Inductive fix_app_view : term -> Set :=
| fix_app_fix mfix i l : l <> [] -> fix_app_view (mkApps (tFix mfix i) l)
| fix_app_other t : ~~ isFix_app t -> fix_app_view t.
Lemma view_fix_app (t : term) : fix_app_view t.
Proof.
induction t; try solve [apply fix_app_other; simpl; auto].
destruct IHt1. pose proof (fix_app_fix mfix i (l ++ [t2])).
forward H. intros eq. destruct l; discriminate.
now rewrite -mkApps_nested in H.
destruct t; try solve [apply fix_app_other; simpl; auto].
apply (fix_app_fix mfix idx [t2]). congruence.
Qed.
Definition isFixLambda (t : term) : bool :=
match t with
| tFix _ _ => true
| tLambda _ _ _ => true
| _ => false
end.
Inductive fix_lambda_view : term -> Set :=
| fix_lambda_lambda na b t : fix_lambda_view (tLambda na b t)
| fix_lambda_fix mfix i : fix_lambda_view (tFix mfix i)
| fix_lambda_other t : ~~ isFixLambda t -> fix_lambda_view t.
Lemma view_fix_lambda (t : term) : fix_lambda_view t.
Proof.
destruct t; repeat constructor.
Qed.
Section All2_telescope.
Context (P : forall (Γ Γ' : context), option (term * term) -> term -> term -> Type).
Definition telescope := context.
Inductive All2_telescope (Γ Γ' : context) : telescope -> telescope -> Type :=
| telescope2_nil : All2_telescope Γ Γ' [] []
| telescope2_cons_abs na t t' Δ Δ' :
P Γ Γ' None t t' ->
All2_telescope (Γ ,, vass na t) (Γ' ,, vass na t') Δ Δ' ->
All2_telescope Γ Γ' (vass na t :: Δ) (vass na t' :: Δ')
| telescope2_cons_def na b b' t t' Δ Δ' :
P Γ Γ' (Some (b, b')) t t' ->
All2_telescope (Γ ,, vdef na b t) (Γ' ,, vdef na b' t') Δ Δ' ->
All2_telescope Γ Γ' (vdef na b t :: Δ) (vdef na b' t' :: Δ').
End All2_telescope.
Section All2_telescope_n.
Context (P : forall (Γ Γ' : context), option (term * term) -> term -> term -> Type).
Context (f : nat -> term -> term).
Inductive All2_telescope_n (Γ Γ' : context) (n : nat) : telescope -> telescope -> Type :=
| telescope_n_nil : All2_telescope_n Γ Γ' n [] []
| telescope_n_cons_abs na t t' Δ Δ' :
P Γ Γ' None (f n t) (f n t') ->
All2_telescope_n (Γ ,, vass na (f n t)) (Γ' ,, vass na (f n t')) (S n) Δ Δ' ->
All2_telescope_n Γ Γ' n (vass na t :: Δ) (vass na t' :: Δ')
| telescope_n_cons_def na b b' t t' Δ Δ' :
P Γ Γ' (Some (f n b, f n b')) (f n t) (f n t') ->
All2_telescope_n (Γ ,, vdef na (f n b) (f n t)) (Γ' ,, vdef na (f n b') (f n t'))
(S n) Δ Δ' ->
All2_telescope_n Γ Γ' n (vdef na b t :: Δ) (vdef na b' t' :: Δ').
End All2_telescope_n.
Lemma All2_telescope_mapi {P} Γ Γ' Δ Δ' (f : nat -> term -> term) k :
All2_telescope_n (on_decl P) f Γ Γ' k Δ Δ' ->
All2_telescope (on_decl P) Γ Γ' (mapi_rec (fun n => map_decl (f n)) Δ k)
(mapi_rec (fun n => map_decl (f n)) Δ' k).
Proof.
induction 1; simpl; constructor; auto.
Qed.
Lemma local_env_telescope P Γ Γ' Δ Δ' :
All2_telescope (on_decl P) Γ Γ' Δ Δ' ->
All2_local_env_over P Γ Γ' (List.rev Δ) (List.rev Δ').
Proof.
induction 1. simpl. constructor.
- simpl. eapply All2_local_env_over_app. constructor. constructor.
simpl. apply p.
revert IHX.
generalize (List.rev Δ) (List.rev Δ'). induction 1. constructor.
constructor. auto. red in p0. red. red. red. red in p0.
rewrite !app_context_assoc. cbn. apply p0.
constructor. auto. destruct p0. unfold on_decl_over in *. simpl.
rewrite !app_context_assoc. cbn. intuition.
- simpl. eapply All2_local_env_over_app. constructor. constructor.
simpl. unfold on_decl_over, on_decl in *. destruct p. split; intuition auto.
revert IHX.
generalize (List.rev Δ) (List.rev Δ'). induction 1. constructor.
constructor. auto. red in p0. red. red. red. red in p0.
rewrite !app_context_assoc. cbn. apply p0.
constructor. auto. destruct p0. unfold on_decl_over in *. simpl.
rewrite !app_context_assoc. cbn. intuition.
Qed.
Lemma lookup_env_cst_inv {Σ c k cst} :
lookup_env Σ c = Some (ConstantDecl k cst) -> c = k.
Proof.
induction Σ. simpl. discriminate.
simpl. destruct AstUtils.ident_eq eqn:Heq. intros [= ->]. simpl in Heq.
now destruct (AstUtils.ident_eq_spec c k). auto.
Qed.
Definition isLambda_or_Fix_app t :=
match fst (decompose_app t) with
| tLambda _ _ _ => true
| tFix _ _ => true
| _ => false
end.
Lemma decompose_app_rec_head t l f : fst (decompose_app_rec t l) = f ->
~~ isApp f.
Proof.
induction t; simpl; try intros [= <-]; auto.
intros. apply IHt1. now rewrite !fst_decompose_app_rec.
Qed.
Lemma isLambda_or_Fix_app_decompose_app t :
~~ isLambda_or_Fix_app t ->
forall l', ~~ isLambda_or_Fix_app (fst (decompose_app_rec t l')).
Proof.
unfold isLambda_or_Fix_app, decompose_app. generalize (@nil term).
induction t; simpl;
try intros ? H ? [= <- <-]; simpl; try congruence.
intros. rewrite !fst_decompose_app_rec. rewrite fst_decompose_app_rec in H.
apply IHt1. apply H.
Qed.
Section TriangleFn.
Context (Σ : global_env).
Definition map_fix (rho : context -> term -> term) Γ mfixctx (mfix : mfixpoint term) :=
(map (map_def (rho Γ) (rho (Γ ,,, mfixctx))) mfix).
(* Equations rho (Γ : context) (t : term) : term by struct t := *)
(* { rho Γ (tApp t u) with t := *)
(* { | tLambda na T b := (rho (vass na (rho Γ T) :: Γ) b) {0 := rho Γ u}; *)
(* | _ => tApp (rho Γ t) (rho Γ u) }; *)
(* rho Γ (tLetIn na d t b) => (subst10 (rho Γ d) (rho (vdef na (rho Γ d) (rho Γ t) :: Γ) b)); *)
(* rho Γ (tRel i) with option_map decl_body (nth_error Γ i) := { *)
(* | Some (Some body) => (lift0 (S i) body); *)
(* | Some None => tRel i; *)
(* | None => tRel i }; *)
(* rho Γ (tCase (ind, pars) p x brs) with decompose_app x, decompose_app (rho Γ x) := *)
(* { | (tConstruct ind' c u, args) | (tConstruct ind'' c' u', args') *)
(* with AstUtils.eq_ind ind ind' := *)
(* { | true => *)
(* let p' := rho Γ p in *)
(* let x' := rho Γ x in *)
(* let brs' := map_brs Γ brs in *)
(* iota_red pars c args' brs'; *)
(* | false => *)
(* let p' := rho Γ p in *)
(* let x' := rho Γ x in *)
(* let brs' := map_brs Γ brs in *)
(* tCase (ind, pars) p' x' brs' }; *)
(* | (tCoFix mfix idx, args) | (tCoFix mfix' idx', args') with unfold_cofix mfix' idx := { *)
(* | Some (narg, fn) => *)
(* let p' := rho Γ p in *)
(* let x' := rho Γ x in *)
(* let brs' := map_brs Γ brs in *)
(* tCase (ind, pars) p' (mkApps fn args') brs'; *)
(* | None => *)
(* let p' := rho Γ p in *)
(* let x' := rho Γ x in *)
(* let brs' := map_brs Γ brs in *)
(* tCase (ind, pars) p' (mkApps (tCoFix mfix' idx) args') brs' }; *)
(* | _ | _ => *)
(* let p' := rho Γ p in *)
(* let x' := rho Γ x in *)
(* let brs' := map_brs Γ brs in *)
(* tCase (ind, pars) p' x' brs' }; *)
(* rho Γ (tProj (i, pars, narg) x) with decompose_app x, decompose_app (rho Γ x) := { *)
(* | (tConstruct ind c u, args) | (tConstruct ind' c' u', args') with *)
(* nth_error args' (pars + narg) := { *)
(* | Some arg1 => *)
(* if AstUtils.eq_ind i ind' then arg1 *)
(* else tProj (i, pars, narg) (rho Γ x); *)
(* | None => tProj (i, pars, narg) (rho Γ x) }; *)
(* | (tCoFix mfix idx, args) | (tCoFix mfix' idx', args') with unfold_cofix mfix' idx := { *)
(* | Some (narg, fn) => tProj (i, pars, narg) (mkApps fn args'); *)
(* | None => tProj (i, pars, narg) (mkApps (tCoFix mfix' idx') args') }; *)
(* | _ | _ => tProj (i, pars, narg) (rho Γ x) }; *)
(* rho Γ (tConst c u) with lookup_env Σ c := { *)
(* | Some (ConstantDecl id decl) with decl.(cst_body) := { *)
(* | Some body => subst_instance_constr u body; *)
(* | None => tConst c u }; *)
(* | _ => tConst c u }; *)
(* rho Γ (tLambda na t u) => tLambda na (rho Γ t) (rho (vass na (rho Γ t) :: Γ) u); *)
(* rho Γ (tProd na t u) => tProd na (rho Γ t) (rho (vass na (rho Γ t) :: Γ) u); *)
(* rho Γ (tVar i) => tVar i; *)
(* rho Γ (tEvar n l) => tEvar n (map_terms Γ l); *)
(* rho Γ (tSort s) => tSort s; *)
(* rho Γ (tFix mfix idx) => *)
(* let mfixctx := fold_fix_context rho Γ [] mfix in *)
(* tFix (map_fix rho Γ mfixctx mfix) idx; *)
(* rho Γ (tCoFix mfix idx) => *)
(* let mfixctx := fold_fix_context rho Γ [] mfix in *)
(* tCoFix (map_fix rho Γ mfixctx mfix) idx; *)
(* rho Γ x => x } *)
(* where map_terms (Γ : context) (l : list term) : list term by struct l := *)
(* { map_terms Γ nil := nil; *)
(* map_terms Γ (cons p l) := cons (rho Γ p) (map_terms Γ l) } *)
(* where map_brs (Γ : context) (l : list (nat * term)) : list (nat * term) by struct l := *)
(* { map_brs Γ nil := nil; *)
(* map_brs Γ (cons p l) := (fst p, rho Γ (snd p)) :: map_brs Γ l }. *)
(* Lemma map_terms_map Γ l : map_terms Γ l = map (rho Γ) l. *)
(* Proof. induction l; simpl; rewrite ?IHl; auto. Qed. *)
(* Hint Rewrite map_terms_map : rho. *)
Fixpoint rho Γ t : term :=
match t with
| tApp (tLambda na T b) u => (rho (vass na (rho Γ T) :: Γ) b) {0 := rho Γ u}
| tLetIn na d t b => (subst10 (rho Γ d) (rho (vdef na (rho Γ d) (rho Γ t) :: Γ) b))
| tRel i =>
match option_map decl_body (nth_error Γ i) with
| Some (Some body) => (lift0 (S i) body)
| Some None => tRel i
| None => tRel i
end
| tCase (ind, pars) p x brs =>
let p' := rho Γ p in
let x' := rho Γ x in
let brs' := List.map (fun x => (fst x, rho Γ (snd x))) brs in
match decompose_app x, decompose_app x' with
| (tConstruct ind' c u, args), (tConstruct ind'' c' u', args') =>
if eqb ind ind' then
iota_red pars c args' brs'
else tCase (ind, pars) p' x' brs'
| (tCoFix mfix idx, args), (tCoFix mfix' idx', args') =>
match unfold_cofix mfix' idx with
| Some (narg, fn) =>
tCase (ind, pars) p' (mkApps fn args') brs'
| None => tCase (ind, pars) p' (mkApps (tCoFix mfix' idx) args') brs'
end
| _, _ => tCase (ind, pars) p' x' brs'
end
| tProj ((i, pars, narg) as p) x =>
let x' := rho Γ x in
match decompose_app x, decompose_app x' with
| (tConstruct ind c u, args), (tConstruct ind' c' u', args') =>
match nth_error args' (pars + narg) with
| Some arg1 =>
if eqb i ind' then arg1
else tProj p x'
| None => tProj p x'
end
| (tCoFix mfix idx, args), (tCoFix mfix' idx', args') =>
match unfold_cofix mfix' idx with
| Some (narg, fn) => tProj p (mkApps fn args')
| None => tProj p (mkApps (tCoFix mfix' idx') args')
end
| _, _ => tProj p x'
end
| tConst c u =>
match lookup_env Σ c with
| Some (ConstantDecl id decl) =>
match decl.(cst_body) with
| Some body => subst_instance_constr u body
| None => tConst c u
end
| _ => tConst c u
end
| tApp t u =>
let t' := rho Γ t in
let u' := rho Γ u in
match decompose_app (tApp t u), decompose_app (tApp t' u') with
| (tFix mfix0 idx0, args0), (tFix mfix1 idx1, args1) =>
match unfold_fix mfix1 idx1 with
| Some (rarg, fn) =>
if is_constructor rarg args1 then
mkApps fn args1
else tApp t' u'
| None => tApp t' u'
end
| _, _ => tApp t' u'
end
| tLambda na t u => tLambda na (rho Γ t) (rho (vass na (rho Γ t) :: Γ) u)
| tProd na t u => tProd na (rho Γ t) (rho (vass na (rho Γ t) :: Γ) u)
| tVar i => tVar i
| tEvar n l => tEvar n (map (rho Γ) l)
| tSort s => tSort s
| tFix mfix idx =>
let mfixctx := fold_fix_context rho Γ [] mfix in
tFix (map_fix rho Γ mfixctx mfix) idx
| tCoFix mfix idx =>
let mfixctx := fold_fix_context rho Γ [] mfix in
tCoFix (map_fix rho Γ mfixctx mfix) idx
| tInd _ _ | tConstruct _ _ _ => t
end.
Transparent rho.