forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICSafeReduce.v
1501 lines (1399 loc) · 49.5 KB
/
PCUICSafeReduce.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 Bool String List Program BinPos Compare_dec Arith Lia
Classes.RelationClasses Omega.
From MetaCoq.Template
Require Import config Universes monad_utils utils BasicAst AstUtils UnivSubst.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICReflect PCUICLiftSubst PCUICUnivSubst PCUICTyping PCUICPosition
PCUICNormal PCUICInversion PCUICCumulativity PCUICSafeLemmata
PCUICGeneration PCUICValidity PCUICSR PCUICSN.
From Equations Require Import Equations.
Require Import Equations.Prop.DepElim.
Import MonadNotation.
Open Scope type_scope.
(** * Reduction machine for PCUIC without fuel
We subsume the reduction machine of PCUICChecker without relying on fuel.
Instead we assume strong normalisation of the system (for well-typed terms)
and proceed by well-founded induction.
Once extracted, this should roughly correspond to the ocaml implementation.
*)
Set Equations With UIP.
Local Set Keyed Unification.
(* We assume normalisation of the reduction.
We state is as well-foundedness of the reduction.
*)
Section Measure.
Context {cf : checker_flags}.
Context (flags : RedFlags.t).
Context (Σ : global_env_ext).
Definition R_aux Γ :=
dlexprod (cored Σ Γ) (@posR).
Definition R Γ u v :=
R_aux Γ (zip u ; stack_pos (fst u) (snd u))
(zip v ; stack_pos (fst v) (snd v)).
Lemma cored_wellformed :
forall {Γ u v},
∥ wf Σ ∥ -> wellformed Σ Γ u ->
cored (fst Σ) Γ v u ->
wellformed Σ Γ v.
Proof.
intros Γ u v HΣ h r.
destruct HΣ as [wΣ].
revert h. induction r ; intros h.
- destruct h as [A|A]; [left|right].
destruct A as [A HA]. exists A.
eapply subject_reduction1 ; eassumption.
sq; eapply isWfArity_red1 ; eassumption.
- specialize IHr with (1 := ltac:(eassumption)).
destruct IHr as [A|A]; [left|right].
destruct A as [A HA]. exists A.
eapply subject_reduction1 ; eassumption.
sq; eapply isWfArity_red1 ; eassumption.
Qed.
Lemma red_wellformed :
forall {Γ u v},
∥ wf Σ ∥ -> wellformed Σ Γ u ->
∥ red (fst Σ) Γ u v ∥ ->
wellformed Σ Γ v.
Proof.
intros hΣ Γ u v h [r]. apply red_cored_or_eq in r.
destruct r as [r|[]]; [|assumption].
eapply cored_wellformed; eassumption.
Qed.
Corollary R_Acc_aux :
forall Γ t p,
wf Σ -> wellformed Σ Γ t ->
Acc (R_aux Γ) (t ; p).
Proof.
intros Γ t p HΣ h.
eapply dlexprod_Acc.
- intros x. unfold well_founded.
eapply posR_Acc.
- eapply normalisation'; eassumption.
Qed.
Derive Signature for Acc.
Corollary R_Acc :
forall Γ t,
wf Σ -> wellformed Σ Γ (zip t) ->
Acc (R Γ) t.
Proof.
intros Γ t HΣ h.
pose proof (R_Acc_aux _ _ (stack_pos (fst t) (snd t)) HΣ h) as h'.
clear h. rename h' into h.
dependent induction h.
constructor. intros y hy.
now eapply H0.
Qed.
Lemma R_positionR :
forall Γ t1 t2 (p1 : pos t1) (p2 : pos t2),
t1 = t2 ->
positionR (` p1) (` p2) ->
R_aux Γ (t1 ; p1) (t2 ; p2).
Proof.
intros Γ t1 t2 p1 p2 e h.
subst. right. assumption.
Qed.
Definition Req Γ t t' :=
t = t' \/ R Γ t t'.
Lemma Rtrans :
forall Γ u v w,
R Γ u v ->
R Γ v w ->
R Γ u w.
Proof.
intros Γ u v w h1 h2.
eapply dlexprod_trans.
- intros ? ? ? ? ?. eapply cored_trans' ; eassumption.
- eapply posR_trans.
- eassumption.
- eassumption.
Qed.
Lemma Req_trans :
forall {Γ}, transitive (Req Γ).
Proof.
intros Γ u v w h1 h2.
destruct h1.
- subst. assumption.
- destruct h2.
+ subst. right. assumption.
+ right. eapply Rtrans ; eassumption.
Qed.
Lemma R_to_Req :
forall {Γ u v},
R Γ u v ->
Req Γ u v.
Proof.
intros Γ u v h.
right. assumption.
Qed.
Instance Req_refl : forall Γ, Reflexive (Req Γ).
Proof.
intros Γ.
left. reflexivity.
Qed.
Lemma R_Req_R :
forall {Γ u v w},
R Γ u v ->
Req Γ v w ->
R Γ u w.
Proof.
intros Γ u v w h1 h2.
destruct h2.
- subst. assumption.
- eapply Rtrans ; eassumption.
Qed.
End Measure.
Section Reduce.
Context {cf : checker_flags}.
Context (flags : RedFlags.t).
Context (Σ : global_env_ext).
Context (hΣ : ∥ wf Σ ∥).
Derive NoConfusion NoConfusionHom for option.
Derive NoConfusion NoConfusionHom for context_decl.
Existing Instance Req_refl.
Definition inspect {A} (x : A) : { y : A | y = x } := exist x eq_refl.
Definition Pr (t' : term * stack) π :=
snd (decompose_stack π) = snd (decompose_stack (snd t')).
Notation givePr := (_) (only parsing).
Definition Pr' (t' : term * stack) :=
isApp (fst t') = false /\
(RedFlags.beta flags -> isLambda (fst t') -> isStackApp (snd t') = false).
Notation givePr' := (conj _ (fun β hl => _)) (only parsing).
Notation rec reduce t π :=
(let smaller := _ in
let '(exist res (conj prf (conj h (conj h1 h2)))) := reduce t π smaller in
exist res (conj (Req_trans _ _ _ _ _ (R_to_Req _ smaller)) (conj givePr givePr'))
) (only parsing).
Notation give t π :=
(exist (t,π) (conj _ (conj givePr givePr'))) (only parsing).
Tactic Notation "zip" "fold" "in" hyp(h) :=
lazymatch type of h with
| context C[ zipc ?t ?π ] =>
let C' := context C[ zip (t,π) ] in
change C' in h
end.
Tactic Notation "zip" "fold" :=
lazymatch goal with
| |- context C[ zipc ?t ?π ] =>
let C' := context C[ zip (t,π) ] in
change C'
end.
Lemma Req_red :
forall Γ x y,
Req Σ Γ y x ->
∥ red Σ Γ (zip x) (zip y) ∥.
Proof.
intros Γ [t π] [t' π'] h. cbn.
dependent destruction h.
- repeat zip fold. rewrite H.
constructor. constructor.
- dependent destruction H.
+ eapply cored_red. assumption.
+ cbn in H0. inversion H0.
constructor. constructor.
Qed.
(* Show Obligation Tactic. *)
Ltac obTac :=
(* program_simpl ; *)
program_simplify ;
Tactics.equations_simpl ;
try program_solve_wf ;
try reflexivity.
Obligation Tactic := obTac.
Equations discr_construct (t : term) : Prop :=
discr_construct (tConstruct ind n ui) := False ;
discr_construct _ := True.
Inductive construct_view : term -> Set :=
| view_construct : forall ind n ui, construct_view (tConstruct ind n ui)
| view_other : forall t, discr_construct t -> construct_view t.
Equations construct_viewc t : construct_view t :=
construct_viewc (tConstruct ind n ui) := view_construct ind n ui ;
construct_viewc t := view_other t I.
(* Tailored view for _reduce_stack *)
Equations red_discr (t : term) (π : stack) : Prop :=
red_discr (tRel _) _ := False ;
red_discr (tLetIn _ _ _ _) _ := False ;
red_discr (tConst _ _) _ := False ;
red_discr (tApp _ _) _ := False ;
red_discr (tLambda _ _ _) (App _ _) := False ;
red_discr (tFix _ _) _ := False ;
red_discr (tCase _ _ _ _) _ := False ;
red_discr (tProj _ _) _ := False ;
red_discr _ _ := True.
Inductive red_view : term -> stack -> Set :=
| red_view_Rel c π : red_view (tRel c) π
| red_view_LetIn A b B c π : red_view (tLetIn A b B c) π
| red_view_Const c u π : red_view (tConst c u) π
| red_view_App f a π : red_view (tApp f a) π
| red_view_Lambda na A t a args : red_view (tLambda na A t) (App a args)
| red_view_Fix mfix idx π : red_view (tFix mfix idx) π
| red_view_Case ind par p c brs π : red_view (tCase (ind, par) p c brs) π
| red_view_Proj p c π : red_view (tProj p c) π
| red_view_other t π : red_discr t π -> red_view t π.
Equations red_viewc t π : red_view t π :=
red_viewc (tRel c) π := red_view_Rel c π ;
red_viewc (tLetIn A b B c) π := red_view_LetIn A b B c π ;
red_viewc (tConst c u) π := red_view_Const c u π ;
red_viewc (tApp f a) π := red_view_App f a π ;
red_viewc (tLambda na A t) (App a args) := red_view_Lambda na A t a args ;
red_viewc (tFix mfix idx) π := red_view_Fix mfix idx π ;
red_viewc (tCase (ind, par) p c brs) π := red_view_Case ind par p c brs π ;
red_viewc (tProj p c) π := red_view_Proj p c π ;
red_viewc t π := red_view_other t π I.
Equations discr_construct_cofix (t : term) : Prop :=
discr_construct_cofix (tConstruct ind n ui) := False ;
discr_construct_cofix (tCoFix mfix idx) := False ;
discr_construct_cofix _ := True.
Inductive construct_cofix_view : term -> Set :=
| ccview_construct : forall ind n ui, construct_cofix_view (tConstruct ind n ui)
| ccview_cofix : forall mfix idx, construct_cofix_view (tCoFix mfix idx)
| ccview_other : forall t, discr_construct_cofix t -> construct_cofix_view t.
Equations cc_viewc t : construct_cofix_view t :=
cc_viewc (tConstruct ind n ui) := ccview_construct ind n ui ;
cc_viewc (tCoFix mfix idx) := ccview_cofix mfix idx ;
cc_viewc t := ccview_other t I.
Equations _reduce_stack (Γ : context) (t : term) (π : stack)
(h : wellformed Σ Γ (zip (t,π)))
(reduce : forall t' π', R Σ Γ (t',π') (t,π) ->
{ t'' : term * stack | Req Σ Γ t'' (t',π') /\ Pr t'' π' /\ Pr' t'' })
: { t' : term * stack | Req Σ Γ t' (t,π) /\ Pr t' π /\ Pr' t' } :=
_reduce_stack Γ t π h reduce with red_viewc t π := {
| red_view_Rel c π with RedFlags.zeta flags := {
| true with inspect (nth_error (Γ ,,, stack_context π) c) := {
| @exist None eq := False_rect _ _ ;
| @exist (Some d) eq with inspect d.(decl_body) := {
| @exist None _ := give (tRel c) π ;
| @exist (Some b) H := rec reduce (lift0 (S c) b) π
}
} ;
| false := give (tRel c) π
} ;
| red_view_LetIn A b B c π with RedFlags.zeta flags := {
| true := rec reduce (subst10 b c) π ;
| false := give (tLetIn A b B c) π
} ;
| red_view_Const c u π with RedFlags.delta flags := {
| true with inspect (lookup_env (fst Σ) c) := {
| @exist (Some (ConstantDecl _ {| cst_body := Some body |})) eq :=
let body' := subst_instance_constr u body in
rec reduce body' π ;
| @exist (Some (InductiveDecl _ _)) eq := False_rect _ _ ;
| @exist (Some _) eq := give (tConst c u) π ;
| @exist None eq := False_rect _ _
} ;
| _ := give (tConst c u) π
} ;
| red_view_App f a π := rec reduce f (App a π) ;
| red_view_Lambda na A t a args with inspect (RedFlags.beta flags) := {
| @exist true eq1 := rec reduce (subst10 a t) args ;
| @exist false eq1 := give (tLambda na A t) (App a args)
} ;
| red_view_Fix mfix idx π with RedFlags.fix_ flags := {
| true with inspect (unfold_fix mfix idx) := {
| @exist (Some (narg, fn)) eq1 with inspect (decompose_stack_at π narg) := {
| @exist (Some (args, c, ρ)) eq2 with inspect (reduce c (Fix mfix idx args ρ) _) := {
| @exist (@exist (t, ρ') prf) eq3 with construct_viewc t := {
| view_construct ind n ui with inspect (decompose_stack ρ') := {
| @exist (l, θ) eq4 :=
rec reduce fn (appstack args (App (mkApps (tConstruct ind n ui) l) ρ))
} ;
| view_other t ht := give (tFix mfix idx) π
}
} ;
| _ := give (tFix mfix idx) π
} ;
| _ := give (tFix mfix idx) π
} ;
| false := give (tFix mfix idx) π
} ;
| red_view_Case ind par p c brs π with RedFlags.iota flags := {
| true with inspect (reduce c (Case (ind, par) p brs π) _) := {
| @exist (@exist (t,π') prf) eq with inspect (decompose_stack π') := {
| @exist (args, ρ) prf' with cc_viewc t := {
| ccview_construct ind' c' _ := rec reduce (iota_red par c' args brs) π ;
| ccview_cofix mfix idx with inspect (unfold_cofix mfix idx) := {
| @exist (Some (narg, fn)) eq' :=
rec reduce (tCase (ind, par) p (mkApps fn args) brs) π ;
| @exist None bot := False_rect _ _
} ;
| ccview_other t ht := give (tCase (ind, par) p (mkApps t args) brs) π
}
}
} ;
| false := give (tCase (ind, par) p c brs) π
} ;
| red_view_Proj (i, pars, narg) c π with RedFlags.iota flags := {
| true with inspect (reduce c (Proj (i, pars, narg) π) _) := {
| @exist (@exist (t,π') prf) eq with inspect (decompose_stack π') := {
| @exist (args, ρ) prf' with cc_viewc t := {
| ccview_construct ind' c' _
with inspect (nth_error args (pars + narg)) := {
| @exist (Some arg) eqa := rec reduce arg π ;
| @exist None eqa := False_rect _ _
} ;
| ccview_cofix mfix idx with inspect (unfold_cofix mfix idx) := {
| @exist (Some (narg, fn)) eq' :=
rec reduce (tProj (i, pars, narg) (mkApps fn args)) π ;
| @exist None bot := False_rect _ _
} ;
| ccview_other t ht := give (tProj (i, pars, narg) (mkApps t args)) π
}
}
} ;
| false := give (tProj (i, pars, narg) c) π
} ;
| red_view_other t π discr := give t π
}.
(* tRel *)
Next Obligation.
left.
econstructor.
eapply red1_context.
eapply red_rel. rewrite <- eq. cbn. f_equal.
symmetry. assumption.
Qed.
Next Obligation.
pose proof (wellformed_context _ hΣ _ _ h) as hc.
simpl in hc.
(* Should be a lemma! *)
destruct hΣ as [wΣ].
clear - eq hc wΣ.
revert c hc eq.
generalize (Γ ,,, stack_context π) as Δ. clear Γ π.
intro Γ.
induction Γ ; intros c hc eq.
- destruct hc as [[A h]|[[ctx [s [e _]]]]]; [|discriminate].
apply inversion_Rel in h as hh ; auto.
destruct hh as [? [? [e ?]]].
rewrite e in eq. discriminate eq.
- destruct c.
+ cbn in eq. discriminate.
+ cbn in eq. eapply IHΓ ; try eassumption.
destruct hc as [[A h]|[[ctx [s [e _]]]]] ; try discriminate.
apply inversion_Rel in h as hh ; auto.
destruct hh as [? [? [e ?]]].
cbn in e. rewrite e in eq. discriminate.
Qed.
(* tLetIn *)
Next Obligation.
left. econstructor.
eapply red1_context.
econstructor.
Qed.
(* tConst *)
Next Obligation.
left. econstructor. eapply red1_context.
econstructor.
(* Should be a lemma! *)
- unfold declared_constant. rewrite <- eq. f_equal.
f_equal. clear - eq.
revert c wildcard0 body wildcard1 wildcard2 eq.
set (Σ' := fst Σ). clearbody Σ'. clear Σ. rename Σ' into Σ.
induction Σ ; intros c na t body univ eq.
+ cbn in eq. discriminate.
+ cbn in eq. revert eq.
case_eq (ident_eq c (global_decl_ident a)).
* intros e eq. inversion eq. subst. clear eq.
cbn in e. revert e. destruct (ident_eq_spec c na) ; easy.
* intros e eq. eapply IHg. eassumption.
- cbn. reflexivity.
Qed.
Next Obligation.
destruct hΣ as [wΣ].
eapply wellformed_context in h ; auto. simpl in h.
destruct h as [[T h]|[[ctx [s [e _]]]]]; [|discriminate].
apply inversion_Const in h as [decl [? [d [? ?]]]] ; auto.
unfold declared_constant in d. rewrite <- eq in d.
discriminate.
Qed.
Next Obligation.
destruct hΣ as [wΣ].
eapply wellformed_context in h ; auto. simpl in h.
destruct h as [[T h]|[[ctx [s [e _]]]]]; [|discriminate].
apply inversion_Const in h as [decl [? [d [? ?]]]] ; auto.
unfold declared_constant in d. rewrite <- eq in d.
discriminate.
Qed.
(* tApp *)
Next Obligation.
right.
cbn. unfold posR. cbn.
eapply positionR_poscat_nonil. discriminate.
Qed.
Next Obligation.
unfold Pr. cbn.
unfold Pr in h. cbn in h.
case_eq (decompose_stack π). intros l ρ e.
cbn. rewrite e in h. cbn in h.
assumption.
Qed.
(* tLambda *)
Next Obligation.
left. econstructor.
cbn. eapply red1_context. econstructor.
Qed.
Next Obligation.
unfold Pr. cbn.
case_eq (decompose_stack args). intros l ρ e.
cbn. unfold Pr in h. rewrite e in h. cbn in h.
assumption.
Qed.
Next Obligation.
rewrite β in eq1. discriminate.
Qed.
(* tFix *)
Next Obligation.
symmetry in eq2.
pose proof (decompose_stack_at_eq _ _ _ _ _ eq2). subst.
eapply R_positionR.
- cbn. rewrite zipc_appstack. cbn. reflexivity.
- cbn. rewrite stack_position_appstack. cbn.
rewrite <- app_assoc.
eapply positionR_poscat.
constructor.
Qed.
Next Obligation.
case_eq (decompose_stack π). intros ll π' e.
pose proof (decompose_stack_eq _ _ _ e). subst.
clear eq3. symmetry in eq2.
pose proof (decompose_stack_at_eq _ _ _ _ _ eq2).
pose proof (decompose_stack_at_length _ _ _ _ _ eq2).
case_eq (decompose_stack ρ). intros l' θ' e'.
pose proof (decompose_stack_eq _ _ _ e'). subst.
rewrite H in e. rewrite decompose_stack_appstack in e.
cbn in e. rewrite e' in e. inversion e. subst. clear e.
case_eq (decompose_stack ρ'). intros ll s e1.
pose proof (decompose_stack_eq _ _ _ e1). subst.
eapply R_Req_R.
- instantiate (1 := (tFix mfix idx, appstack (args ++ (mkApps (tConstruct ind n ui) l) :: l') π')).
left. cbn. rewrite 2!zipc_appstack. cbn. rewrite zipc_appstack.
repeat zip fold. eapply cored_context.
assert (forall args l u v, mkApps (tApp (mkApps u args) v) l = mkApps u (args ++ v :: l)) as thm.
{ clear. intro args. induction args ; intros l u v.
- reflexivity.
- cbn. rewrite IHargs. reflexivity.
}
rewrite thm.
left. eapply red_fix.
+ eauto.
+ unfold is_constructor.
rewrite nth_error_app2 by eauto.
replace (#|args| - #|args|) with 0 by auto with arith.
cbn.
unfold isConstruct_app.
rewrite decompose_app_mkApps by reflexivity.
reflexivity.
- destruct r.
+ inversion H0. subst.
destruct ll.
* cbn in H3. subst. cbn in eq4. inversion eq4. subst.
reflexivity.
* cbn in H3. discriminate H3.
+ dependent destruction H0.
* cbn in H0. rewrite 2!zipc_appstack in H0.
rewrite decompose_stack_appstack in eq4.
case_eq (decompose_stack s). intros l0 s0 e2.
rewrite e2 in eq4. cbn in eq4.
destruct l0.
-- rewrite app_nil_r in eq4. inversion eq4. subst. clear eq4.
pose proof (decompose_stack_eq _ _ _ e2) as ee. cbn in ee.
symmetry in ee. subst.
right. left.
cbn. rewrite !zipc_appstack.
unfold Pr in p. cbn in p.
rewrite e1 in p. cbn in p. subst.
cbn in H0.
clear - H0.
match goal with
| |- ?A =>
let e := fresh "e" in
let B := type of H0 in
assert (A = B) as e ; [| rewrite e ; assumption ]
end.
set (t := tConstruct ind n ui). clearbody t.
set (f := tFix mfix idx). clearbody f.
f_equal.
++ clear. revert ll π' l' t f.
induction args ; intros ll π' l' t f.
** cbn. rewrite zipc_appstack. reflexivity.
** cbn. rewrite IHargs. reflexivity.
++ clear. revert π' l' c f.
induction args ; intros π' l' c f.
** cbn. reflexivity.
** cbn. rewrite IHargs. reflexivity.
-- pose proof (decompose_stack_eq _ _ _ e2) as ee. cbn in ee.
subst. exfalso.
eapply decompose_stack_not_app. eassumption.
* cbn in H1. inversion H1.
rewrite 2!zipc_appstack in H3.
unfold Pr in p. cbn in p.
rewrite e1 in p. cbn in p. subst.
cbn in H3. rewrite zipc_appstack in H3.
apply zipc_inj in H3.
apply PCUICAstUtils.mkApps_inj in H3.
inversion H3. subst.
rewrite e1 in eq4. inversion eq4. subst.
reflexivity.
Qed.
Next Obligation.
unfold Pr. cbn.
unfold Pr in h. cbn in h.
rewrite decompose_stack_appstack in h. cbn in h.
case_eq (decompose_stack ρ). intros l1 ρ1 e.
rewrite e in h. cbn in h. subst.
pose proof (decompose_stack_eq _ _ _ e). subst.
clear eq3. symmetry in eq2.
pose proof (decompose_stack_at_eq _ _ _ _ _ eq2).
subst.
rewrite decompose_stack_appstack. cbn.
rewrite e. cbn. reflexivity.
Qed.
(* tCase *)
Next Obligation.
right. unfold posR. cbn.
eapply positionR_poscat_nonil. discriminate.
Qed.
Next Obligation.
unfold Pr in p0. cbn in p0.
pose proof p0 as hh.
rewrite <- prf' in hh. cbn in hh. subst.
eapply R_Req_R.
- econstructor. econstructor. eapply red1_context.
eapply red_iota.
- instantiate (4 := ind'). instantiate (2 := p).
instantiate (1 := wildcard9).
destruct r.
+ inversion e.
subst.
cbn in prf'. inversion prf'. subst. clear prf'.
cbn.
assert (ind = ind').
{ clear - h flags hΣ.
apply wellformed_context in h ; auto.
simpl in h.
eapply Case_Construct_ind_eq with (args := []) ; eauto.
} subst.
reflexivity.
+ clear eq. dependent destruction r.
* cbn in H.
symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H.
cbn in H.
right. econstructor.
lazymatch goal with
| h : cored _ _ ?t _ |- _ =>
assert (wellformed Σ Γ t) as h'
end.
{ clear - h H flags hΣ.
eapply cored_wellformed ; try eassumption.
}
assert (ind = ind').
{ clear - h' flags hΣ H.
zip fold in h'.
apply wellformed_context in h'.
cbn in h'.
apply Case_Construct_ind_eq in h'. all: eauto.
} subst.
exact H.
* cbn in H0. inversion H0. subst. clear H0.
symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H2. cbn in H2.
apply zipc_inj in H2.
inversion H2. subst.
assert (ind = ind').
{ clear - h flags H hΣ.
apply wellformed_context in h.
cbn in h.
apply Case_Construct_ind_eq in h. all: eauto.
} subst.
reflexivity.
Qed.
Next Obligation.
unfold Pr in p0. cbn in p0.
pose proof p0 as hh.
rewrite <- prf' in hh. cbn in hh. subst.
dependent destruction r.
- inversion e. subst.
left. eapply cored_context.
constructor.
simpl in prf'. inversion prf'. subst.
eapply red_cofix_case with (args := []). eauto.
- clear eq.
dependent destruction r.
+ left.
symmetry in prf'. apply decompose_stack_eq in prf' as ?. subst.
cbn in H. rewrite zipc_appstack in H. cbn in H.
eapply cored_trans' ; try eassumption.
zip fold. eapply cored_context.
constructor. eapply red_cofix_case. eauto.
+ left.
cbn in H0. destruct y'. inversion H0. subst. clear H0.
symmetry in prf'. apply decompose_stack_eq in prf' as ?. subst.
rewrite zipc_appstack in H2. cbn in H2.
cbn. rewrite H2.
zip fold. eapply cored_context.
constructor. eapply red_cofix_case. eauto.
Qed.
Next Obligation.
destruct hΣ as [wΣ].
unfold Pr in p0. cbn in p0.
pose proof p0 as hh.
rewrite <- prf' in hh. cbn in hh. subst.
assert (h' : wellformed Σ Γ (zip (tCase (ind, par) p (mkApps (tCoFix mfix idx) args) brs, π))).
{ dependent destruction r.
- inversion e. subst.
simpl in prf'. inversion prf'. subst.
assumption.
- clear eq. dependent destruction r.
+ apply cored_red in H. destruct H as [r].
eapply red_wellformed ; eauto.
constructor.
symmetry in prf'. apply decompose_stack_eq in prf'. subst.
cbn in r. rewrite zipc_appstack in r. cbn in r.
assumption.
+ cbn in H0. destruct y'. inversion H0. subst. clear H0.
symmetry in prf'. apply decompose_stack_eq in prf'. subst.
rewrite zipc_appstack in H2. cbn in H2.
cbn. rewrite <- H2. assumption.
}
replace (zip (tCase (ind, par) p (mkApps (tCoFix mfix idx) args) brs, π))
with (zip (tCoFix mfix idx, appstack args (Case (ind, par) p brs π)))
in h'.
- destruct hΣ.
apply wellformed_context in h' ; auto. simpl in h'.
destruct h' as [[T h']|[[ctx [s [e _]]]]]; [|discriminate].
apply inversion_CoFix in h' ; auto.
destruct h' as [decl [? [e [? [? ?]]]]].
unfold unfold_cofix in bot.
rewrite e in bot. discriminate.
- cbn. rewrite zipc_appstack. reflexivity.
Qed.
Next Obligation.
clear eq reduce h.
destruct r.
- inversion H. subst.
clear H.
cbn in prf'. inversion prf'. subst. reflexivity.
- unfold Pr in p0. cbn in p0.
rewrite <- prf' in p0. cbn in p0. subst.
dependent destruction H.
+ cbn in H. symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H. cbn in H.
right. econstructor. assumption.
+ cbn in H0. inversion H0. subst. clear H0.
symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H2. cbn in H2.
apply zipc_inj in H2. inversion H2. subst.
reflexivity.
Qed.
(* tProj *)
Next Obligation.
right. unfold posR. cbn.
rewrite <- app_nil_r.
eapply positionR_poscat.
constructor.
Qed.
Next Obligation.
left.
apply Req_red in r as hr.
pose proof (red_wellformed _ hΣ h hr) as hh.
destruct hr as [hr].
eapply cored_red_cored ; try eassumption.
unfold Pr in p. simpl in p. pose proof p as p'.
rewrite <- prf' in p'. simpl in p'. subst.
symmetry in prf'. apply decompose_stack_eq in prf' as ?.
subst. cbn. rewrite zipc_appstack. cbn.
do 2 zip fold. eapply cored_context.
constructor.
cbn in hh. rewrite zipc_appstack in hh. cbn in hh.
zip fold in hh. apply wellformed_context in hh.
simpl in hh. apply Proj_Constuct_ind_eq in hh. all: eauto.
subst. constructor. eauto.
Qed.
Next Obligation.
unfold Pr in p. simpl in p.
pose proof p as p'.
rewrite <- prf' in p'. simpl in p'. subst.
symmetry in prf'. apply decompose_stack_eq in prf' as ?.
subst.
apply Req_red in r as hr.
pose proof (red_wellformed _ hΣ h hr) as hh.
cbn in hh. rewrite zipc_appstack in hh. cbn in hh.
zip fold in hh.
apply wellformed_context in hh. simpl in hh.
apply Proj_red_cond in hh. all: eauto.
Qed.
Next Obligation.
unfold Pr in p. simpl in p.
pose proof p as p'.
rewrite <- prf' in p'. simpl in p'. subst.
dependent destruction r.
- inversion e. subst.
left. eapply cored_context.
constructor.
simpl in prf'. inversion prf'. subst.
eapply red_cofix_proj with (args := []). eauto.
- clear eq.
dependent destruction r.
+ left.
symmetry in prf'. apply decompose_stack_eq in prf' as ?. subst.
cbn in H. rewrite zipc_appstack in H. cbn in H.
eapply cored_trans' ; try eassumption.
zip fold. eapply cored_context.
constructor. eapply red_cofix_proj. eauto.
+ left.
cbn in H0. destruct y'. inversion H0. subst. clear H0.
symmetry in prf'. apply decompose_stack_eq in prf' as ?. subst.
rewrite zipc_appstack in H2. cbn in H2.
cbn. rewrite H2.
zip fold. eapply cored_context.
constructor. eapply red_cofix_proj. eauto.
Qed.
Next Obligation.
destruct hΣ as [wΣ].
unfold Pr in p. simpl in p.
pose proof p as p'.
rewrite <- prf' in p'. simpl in p'. subst.
assert (h' : wellformed Σ Γ (zip (tProj (i, pars, narg) (mkApps (tCoFix mfix idx) args), π))).
{ dependent destruction r.
- inversion e. subst.
simpl in prf'. inversion prf'. subst.
assumption.
- clear eq. dependent destruction r.
+ apply cored_red in H. destruct H as [r].
eapply red_wellformed ; eauto.
constructor.
symmetry in prf'. apply decompose_stack_eq in prf'. subst.
cbn in r. rewrite zipc_appstack in r. cbn in r.
assumption.
+ cbn in H0. destruct y'. inversion H0. subst. clear H0.
symmetry in prf'. apply decompose_stack_eq in prf'. subst.
rewrite zipc_appstack in H2. cbn in H2.
cbn. rewrite <- H2. assumption.
}
replace (zip (tProj (i, pars, narg) (mkApps (tCoFix mfix idx) args), π))
with (zip (tCoFix mfix idx, appstack args (Proj (i, pars, narg) π)))
in h'.
- destruct hΣ.
apply wellformed_context in h' ; auto. simpl in h'.
destruct h' as [[T h']|[[ctx [s [e _]]]]]; [|discriminate].
apply inversion_CoFix in h' ; auto.
destruct h' as [decl [? [e [? [? ?]]]]].
unfold unfold_cofix in bot.
rewrite e in bot. discriminate.
- cbn. rewrite zipc_appstack. reflexivity.
Qed.
Next Obligation.
clear eq.
dependent destruction r.
- inversion H. subst. cbn in prf'. inversion prf'. subst.
cbn. reflexivity.
- unfold Pr in p. cbn in p.
rewrite <- prf' in p. cbn in p. subst.
dependent destruction H.
+ cbn in H. symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H. cbn in H.
right. econstructor. assumption.
+ cbn in H0. inversion H0. subst. clear H0.
symmetry in prf'.
pose proof (decompose_stack_eq _ _ _ prf'). subst.
rewrite zipc_appstack in H2. cbn in H2.
apply zipc_inj in H2. inversion H2. subst.
reflexivity.
Qed.
(* Other *)
Next Obligation.
revert discr.
apply_funelim (red_discr t π). all: auto.
easy.
Qed.
Next Obligation.
revert discr hl.
apply_funelim (red_discr t π). all: easy.
Qed.
(* Added by Julien Forest on 13/11/20 in Coq stdlib, adapted to subset case by M. Sozeau *)
Section Acc_sidecond_generator.
Context {A : Type} {R : A -> A -> Prop} {P : A -> Prop}.
Variable Pimpl : forall x y, P x -> R y x -> P y.
(* *Lazily* add 2^n - 1 Acc_intro on top of wf.
Needed for fast reductions using Function and Program Fixpoint
and probably using Fix and Fix_F_2
*)
Fixpoint Acc_intro_generator n (acc : forall t, P t -> Acc R t) : forall t, P t -> Acc R t :=
match n with
| O => acc
| S n => fun x Px =>
Acc_intro x (fun y Hy => Acc_intro_generator n (Acc_intro_generator n acc) y (Pimpl _ _ Px Hy))
end.
End Acc_sidecond_generator.
Lemma wellformed_R_pres Γ :
forall x y : term × stack, wellformed Σ Γ (zip x) -> R Σ Γ y x -> wellformed Σ Γ (zip y).
Proof. intros. todo "wellformed_R_pres proof"%string. Admitted.
Equations reduce_stack_full (Γ : context) (t : term) (π : stack)
(h : wellformed Σ Γ (zip (t,π))) : { t' : term * stack | Req Σ Γ t' (t, π) /\ Pr t' π /\ Pr' t' } :=
reduce_stack_full Γ t π h :=
Fix_F (R := R Σ Γ)
(fun x => wellformed Σ Γ (zip x) -> { t' : term * stack | Req Σ Γ t' x /\ Pr t' (snd x) /\ Pr' t' })
(fun t' f => _) (x := (t, π)) _ _.
Next Obligation.
eapply _reduce_stack.
- assumption.
- intros t' π' h'.
eapply f.
+ assumption.
+ simple inversion h'.
* cbn in H1. cbn in H2.
inversion H1. subst. inversion H2. subst. clear H1 H2.
intros.
destruct hΣ as [wΣ].
eapply cored_wellformed ; eassumption.
* cbn in H1. cbn in H2.
inversion H1. subst. inversion H2. subst. clear H1 H2.
intros. cbn. rewrite H3. assumption.
Defined.
Next Obligation.
destruct hΣ.
eapply R_Acc. all: assumption.
Defined.
(* Replace the last obligation by the following to run inside Coq. *)
(* Next Obligation.
destruct hΣ.
revert h. generalize (t, π).
refine (Acc_intro_generator
(R:=fun x y => R Σ Γ x y)
(P:=fun x => wellformed Σ Γ (zip x)) (fun x y Px Hy => _) 12 _).
simpl in *. eapply wellformed_R_pres; eauto.
intros; eapply R_Acc; eassumption.
Defined. *)
Definition reduce_stack Γ t π h :=
let '(exist ts _) := reduce_stack_full Γ t π h in ts.
Lemma reduce_stack_Req :
forall Γ t π h,
Req Σ Γ (reduce_stack Γ t π h) (t, π).
Proof.
intros Γ t π h.
unfold reduce_stack.
destruct (reduce_stack_full Γ t π h) as [[t' π'] [r _]].
assumption.
Qed.
Theorem reduce_stack_sound :
forall Γ t π h,
∥ red (fst Σ) Γ (zip (t, π)) (zip (reduce_stack Γ t π h)) ∥.
Proof.
intros Γ t π h.
eapply Req_red.
eapply reduce_stack_Req.
Qed.
Lemma reduce_stack_decompose :
forall Γ t π h,
snd (decompose_stack (snd (reduce_stack Γ t π h))) =
snd (decompose_stack π).
Proof.
intros Γ t π h.
unfold reduce_stack.
destruct (reduce_stack_full Γ t π h) as [[t' π'] [r [p p']]].
unfold Pr in p. symmetry. assumption.
Qed.
Lemma reduce_stack_context :
forall Γ t π h,
stack_context (snd (reduce_stack Γ t π h)) =
stack_context π.
Proof.
intros Γ t π h.
pose proof (reduce_stack_decompose Γ t π h) as hd.
case_eq (decompose_stack π). intros l ρ e1.
case_eq (decompose_stack (snd (reduce_stack Γ t π h))). intros l' ρ' e2.
rewrite e1 in hd. rewrite e2 in hd. cbn in hd. subst.
pose proof (decompose_stack_eq _ _ _ e1).