@@ -731,13 +731,33 @@ Section PCUICInv.
731
731
732
732
End PCUICInv.
733
733
734
- (*
735
734
Section ErasureFunction.
736
- Import EAst EAstUtils EWcbvEval.
735
+ Import PCUICAst PCUICAst.PCUICEnvironment PCUIC.PCUICConversion PCUICArities PCUICSpine PCUICOnFreeVars PCUICWellScopedCumulativity.
736
+ Import EAst EAstUtils EWcbvEval EArities.
737
737
738
- Definition isFunction (t : EAst.term) := EAst.isLambda t || isFix t .
738
+ Definition isFunction (t : EAst.term) := EAst.isLambda t || isFixApp t || isCoFix (head t) .
739
739
740
- Lemma erase_function`{WcbvFlags} (cf:=config.extraction_checker_flags) (Σ:global_env_ext_map) f v v' na A B
740
+ Lemma typing_spine_isArity {Σ : global_env_ext} {Γ T args T'} {wfΣ : wf Σ} :
741
+ wf_local Σ Γ ->
742
+ typing_spine Σ Γ T args T' ->
743
+ isArity T ->
744
+ exists ar, ∥ typing_spine Σ Γ T args ar ∥ /\ isArity ar.
745
+ Proof .
746
+ intros wfΓ sp isa.
747
+ unshelve epose proof (PCUICClassification.isArity_typing_spine _ sp); eauto.
748
+ forward H.
749
+ { exists T. split; eauto. sq.
750
+ pose proof (typing_spine_isType_dom sp).
751
+ eapply PCUICContextConversion.closed_red_refl; fvs. }
752
+ destruct H as [ar [[redar] isa']]. exists ar; split => //. sq.
753
+ eapply typing_spine_weaken_concl; tea. now eapply red_ws_cumul_pb.
754
+ eapply typing_spine_isType_codom in sp. eapply isType_red; tea.
755
+ eapply redar.
756
+ Qed .
757
+
758
+ Lemma erase_function (wfl := default_wcbv_flags)
759
+ {guard_impl : abstract_guard_impl}
760
+ (cf:=config.extraction_checker_flags) (Σ:global_env_ext_map) f v v' na A B
741
761
(wf : ∥ Σ ;;; [] |- f : PCUICAst.tProd na A B ∥) pr :
742
762
∥ nisErasable Σ [] f ∥ ->
743
763
PCUICWcbvEval.eval Σ f v ->
@@ -749,37 +769,98 @@ Section ErasureFunction.
749
769
unfold transform, erase_transform.
750
770
destruct pr as [pre [[expΣ expt] [norm norm']]]. destruct pre as [[wfΣ wft]].
751
771
eapply nisErasable_eval in ne; tea.
752
- eapply nisErasable_spec in ne.
772
+ eapply nisErasable_spec in ne; eauto .
753
773
eapply PCUICClassification.subject_reduction_eval in hty as hty'; tea.
754
774
unfold erase_program, erase_pcuic_program.
755
775
set (Σ' := build_wf_env_from_env _ _).
756
- set (env := make _ _). set (obl := (fun Σ (wfΣ : _) => _)) at 4. clearbody obl.
757
- set (obl' := (fun Σ => _)) at 1. clearbody obl'.
758
776
set (env' := PCUICWfEnv.abstract_make_wf_env_ext _ _ _).
759
- cbn -[env']. set (deps := term_global_deps _). clearbody deps.
760
- set (eg := erase_global_fast _ _ _ _ _).
777
+ set (obl := (fun Σ (wfΣ : _) => _)) at 5.
778
+ set (obl' := (fun Σ => _)) at 12.
779
+ cbn -[env'].
780
+ set (wtf := (fun Σ => _)) at 13. change obl' with wtf. clear obl'. clearbody wtf.
781
+ set (eqdecls := (fun Σ => _)) at 9. clearbody eqdecls.
782
+ set (deps := term_global_deps _).
783
+ set (nin := (fun (n : nat) => _)). clearbody nin.
784
+ epose proof (@erase_global_deps_fast_erase_global_deps deps optimized_abstract_env_impl Σ' (PCUICAst.PCUICEnvironment.declarations Σ) nin) as [nin2 eq].
785
+ rewrite /erase_global_fast. erewrite eq. clear eq nin.
786
+ set (eg := erase_global_deps _ _ _ _ _ _).
761
787
762
- unshelve epose proof (erase_correct optimized_abstract_env_impl Σ' Σ.2 _ _ _ _ _ deps _ _ _ eq_refl _ eq_refl _ Σ eq_refl).
763
- 1-10:shelve. cbn. intros ? ->. exact evf.
764
- destruct H0 as [v'' [ervv'' [ev]]].
788
+ unshelve epose proof (erase_correct optimized_abstract_env_impl Σ' Σ.2 _ f v _ _ deps _ _ _ eq_refl _ eq_refl _ Σ eq_refl); eauto.
789
+ { eapply Kernames.KernameSet.subset_spec. rewrite /deps -/env'. cbn [fst snd]. apply Kernames.KernameSetProp.subset_refl. }
790
+ { cbn => ? -> //. }
791
+ destruct H as [v'' [ervv'' [ev]]].
792
+ eset (eg' := erase_global_deps _ _ _ _ _ _) in ev.
793
+ replace eg with eg'. 2:{ rewrite /eg /eg'. eapply reflexivity. }
765
794
intros ev'.
766
- assert (v' = v''). { epose proof (eval_deterministic ev). }
767
- subst v''.
795
+ assert (v' = v''). { epose proof (eval_deterministic ev). symmetry. eapply H.
796
+ set(er := erase _ _ _ _ _) in ev'.
797
+ set(er' := erase _ _ _ _ _).
798
+ replace er' with er. exact ev'.
799
+ rewrite /er /er' /env'. apply reflexivity. }
800
+ subst v''. cbn in ev.
768
801
eapply pcuic_eval_function in evf; tea.
769
802
destruct (PCUICAstUtils.decompose_app v) eqn:da.
770
803
eapply PCUICAstUtils.decompose_app_inv in da. cbn in da.
771
804
move: evf. destruct t0 => //; cbn in da; subst v. 1:destruct l => //. 1-4:intros _.
772
805
- clear -ne ervv''. depelim ervv''. cbn => //. elim ne. sq. exact X.
773
- - clear -wfΣ hty' ne. elim ne. sq.
774
- red. eexists; split; tea. left.
775
- - clear -ne ervv''.
776
- -
777
- - exact wfΣ.
778
- - constructor.
779
- Unshelve.
806
+ - clear -wfΣ hty' ne. elim ne.
807
+ assert (exists mdecl idecl, PCUICAst.declared_inductive Σ ind mdecl idecl) as [mdecl [idecl decli]].
808
+ { eapply PCUICValidity.inversion_mkApps in hty' as [? [hty _]]. clear -hty.
809
+ depind hty; eauto. }
810
+ eapply PCUICInductives.invert_type_mkApps_ind in hty' as [sp cu]; eauto.
811
+ pose proof (typing_spine_isArity ltac:(constructor) sp).
812
+ forward H.
813
+ { apply (PCUICUnivSubstitutionTyp.isArity_subst_instance ui).
814
+ now eapply isArity_ind_type. }
815
+ destruct H as [ar [[spar] isa']].
816
+ sq.
817
+ eexists; cbn. split.
818
+ eapply PCUICSpine.type_mkApps. econstructor; eauto with pcuic. exact spar.
819
+ now left.
820
+ - clear -wfΣ hty' ne ervv''.
821
+ assert (exists mfix' l', v' = EAst.mkApps (EAst.tFix mfix' idx) l') as [mfix' [l' ->]].
822
+ { eapply ErasureProperties.erases_mkApps_inv in ervv'' as [[L1 [L2 [L2' [? [? [? []]]]]]]|[f'[L'[eq [erf erargs]]]]].
823
+ - subst l v'. elim ne. destruct H0. rewrite PCUICAstUtils.mkApps_app. eapply Is_type_app; [eauto|eauto| |eauto].
824
+ now rewrite -PCUICAstUtils.mkApps_app.
825
+ - depelim erf. do 2 eexists; trea. subst v'.
826
+ elim ne. eapply Is_type_app; eauto. }
827
+ now rewrite /isFunction /isFixApp !head_mkApps //= orb_true_r.
828
+ - clear -wfΣ hty' ne ervv''.
829
+ assert (exists mfix' l', v' = EAst.mkApps (EAst.tCoFix mfix' idx) l') as [mfix' [l' ->]].
830
+ { eapply ErasureProperties.erases_mkApps_inv in ervv'' as [[L1 [L2 [L2' [? [? [? []]]]]]]|[f'[L'[eq [erf erargs]]]]].
831
+ - subst l v'. elim ne. destruct H0. rewrite PCUICAstUtils.mkApps_app. eapply Is_type_app; [eauto|eauto| |eauto].
832
+ now rewrite -PCUICAstUtils.mkApps_app.
833
+ - depelim erf. do 2 eexists; trea. subst v'.
834
+ elim ne. eapply Is_type_app; eauto. }
835
+ now rewrite /isFunction /isFixApp !head_mkApps //= orb_true_r.
836
+ Qed .
837
+
838
+ Lemma pcuic_function_value (wfl := default_wcbv_flags)
839
+ {guard_impl : abstract_guard_impl}
840
+ (cf:=config.extraction_checker_flags) (Σ:global_env_ext) f na A B
841
+ (wfΣ : wf_ext Σ) (axfree : axiom_free Σ) (wf : Σ ;;; [] |- f : PCUICAst.tProd na A B) : { v & PCUICWcbvEval.eval Σ f v }.
842
+ Proof .
843
+ eapply (PCUICNormalization.wcbv_normalization wfΣ axfree wf). Unshelve.
844
+ exact PCUICSN.extraction_normalizing.
845
+ now eapply PCUICSN.normalization.
780
846
Qed .
847
+
848
+ Lemma erase_function_to_function (wfl := default_wcbv_flags)
849
+ {guard_impl : abstract_guard_impl}
850
+ (cf:=config.extraction_checker_flags) (Σ:global_env_ext_map) f v' na A B
851
+ (wf : ∥ Σ ;;; [] |- f : PCUICAst.tProd na A B ∥) pr :
852
+ axiom_free Σ ->
853
+ ∥ nisErasable Σ [] f ∥ ->
854
+ let (Σ', f') := transform erase_transform (Σ, f) pr in
855
+ eval Σ' f' v' -> isFunction v' = true.
856
+ Proof .
857
+ intros axfree nise.
858
+ destruct pr as [[]]. destruct wf.
859
+ epose proof (pcuic_function_value Σ f na A B w.1 axfree X) as [v hv].
860
+ eapply erase_function; tea. now sq.
861
+ Qed .
862
+
781
863
End ErasureFunction.
782
- *)
783
864
784
865
Module ETransformPresAppLam.
785
866
Section Opt.
@@ -1120,7 +1201,14 @@ Proof.
1120
1201
now move/andP: H0 => []. move/andP: H1 => [] etactx etaapp. apply/andP => //. split => //.
1121
1202
now apply EEtaExpanded.isEtaExp_tApp_arg in etaapp.
1122
1203
reflexivity.
1123
- - intros [Σ t] pr; cbn. destruct t => //.
1204
+ - intros [Σ t] pr; cbn. clear pr. move: t.
1205
+ apply: EInduction.MkAppsInd.rec => //= t u.
1206
+ rewrite /isFunction /isFixApp head_mkApps EInlineProjections.optimize_mkApps !head_mkApps; rtoProp; intuition auto.
1207
+ destruct u using rev_case => //. rewrite ?map_app !mkApps_app /= in H2 *.
1208
+ rewrite -!orb_assoc in H1.
1209
+ forward H1. apply/or3P. move/orP in H2; intuition auto. now constructor 2.
1210
+ now constructor 3.
1211
+ apply/orP. move/or3P: H1 => []; intuition auto. destruct t => //.
1124
1212
Qed .
1125
1213
1126
1214
#[global] Instance remove_match_on_box_pres {fl : WcbvFlags} {efl : EEnvFlags} {wcon : with_constructor_as_block = false}
@@ -1172,7 +1260,15 @@ Proof.
1172
1260
{ destruct pr as [[] pr']; move/andP: pr' => [] etactx; split => //. split => //. cbn in H0. now move/andP: H0 => [] /andP [].
1173
1261
apply/andP. split => //. now apply EEtaExpanded.isEtaExp_tApp_arg in b. }
1174
1262
now rewrite /EOptimizePropDiscr.remove_match_on_box_program /=.
1175
- - intros [Σ t] pr; cbn. destruct t => //.
1263
+ - intros [Σ t] pr; cbn.
1264
+ clear -t. revert t.
1265
+ apply: EInduction.MkAppsInd.rec => //= t u.
1266
+ rewrite /isFunction /isFixApp head_mkApps EOptimizePropDiscr.remove_match_on_box_mkApps !head_mkApps; rtoProp; intuition auto.
1267
+ destruct u using rev_case => //. rewrite ?map_app !mkApps_app /= in H2 *.
1268
+ rewrite -!orb_assoc in H1.
1269
+ forward H1. apply/or3P. move/orP in H2; intuition auto. now constructor 2.
1270
+ now constructor 3.
1271
+ apply/orP. move/or3P: H1 => []; intuition auto. destruct t => //.
1176
1272
Qed .
1177
1273
1178
1274
#[global] Instance remove_params_optimization_pres {fl : WcbvFlags} {wcon : with_constructor_as_block = false} :
@@ -1214,7 +1310,18 @@ Proof.
1214
1310
apply/andP. split => //. now apply EEtaExpanded.isEtaExp_tApp_arg in b. }
1215
1311
rewrite /ERemoveParams.strip_program /=. f_equal.
1216
1312
rewrite (ERemoveParams.strip_mkApps_etaexp _ [u]) //.
1217
- - intros [Σ t] pr; cbn. destruct t => //.
1313
+ - intros [Σ t] pr. cbn [fst snd transform remove_params_optimization].
1314
+ clear -t. revert t.
1315
+ apply: EInduction.MkAppsInd.rec => //= t u.
1316
+ rewrite /isFunction /isFixApp !head_mkApps.
1317
+ intros napp nnil.
1318
+ intros IH IHargs isl.
1319
+ rewrite ERemoveParams.strip_mkApps //.
1320
+ destruct EEtaExpanded.construct_viewc => //=; cbn in isl;
1321
+ rewrite OptimizeCorrectness.isLambda_mkApps //= in isl *.
1322
+ rewrite OptimizeCorrectness.isLambda_mkApps. len.
1323
+ rewrite !head_mkApps.
1324
+ destruct t0 => //=.
1218
1325
Qed .
1219
1326
1220
1327
#[global] Instance constructors_as_blocks_transformation_pres {efl : EWellformed.EEnvFlags}
@@ -1258,7 +1365,20 @@ Proof.
1258
1365
apply/andP. split => //. now apply EEtaExpanded.isEtaExp_tApp_arg in exp'. }
1259
1366
simpl. rewrite /EConstructorsAsBlocks.transform_blocks_program /=. f_equal.
1260
1367
rewrite (EConstructorsAsBlocks.transform_blocks_mkApps_eta_fn _ _ [u]) //.
1261
- - intros [Σ t] pr; cbn. destruct t => //.
1368
+ - intros [Σ t] pr; cbn [fst snd transform constructors_as_blocks_transformation].
1369
+ destruct pr as [_ h]. move/andP: h => [] _ /=.
1370
+ clear -t.
1371
+ destruct (decompose_app t) as [f l] eqn:da.
1372
+ pose proof (decompose_app_notApp _ _ _ da).
1373
+ eapply decompose_app_inv in da. subst t.
1374
+ rewrite /isFunction /isFixApp !head_mkApps.
1375
+ rewrite OptimizeCorrectness.isLambda_mkApps //=.
1376
+ rewrite EEtaExpanded.isEtaExp_mkApps_napp //=.
1377
+ destruct EEtaExpanded.construct_viewc => //.
1378
+ move/andP => [etat etal].
1379
+ rewrite !(EConstructorsAsBlocks.transform_blocks_mkApps_eta_fn _ _ l) // !head_mkApps /=.
1380
+ destruct t0 => //=. rewrite !orb_false_r. move/Nat.eqb_eq.
1381
+ destruct l => //=. all:now rewrite !orb_false_r !orb_true_r.
1262
1382
Qed .
1263
1383
1264
1384
#[global] Instance guarded_to_unguarded_fix_pres {efl : EWellformed.EEnvFlags}
0 commit comments