Skip to content

Commit 5b747a8

Browse files
committed
Ewellformed islambda (#735)
* Constructors as blocks improvement: they always have the right arity * Include "isLambda" for fixpoint bodies in the ewellformed predicate. * Derive an eliminator to reason on the evaluation relation when cstrs_as_blocks = true, preserving well-formedness
1 parent 5f9421e commit 5b747a8

16 files changed

+1144
-273
lines changed

erasure/_CoqProject.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ theories/ERemoveParams.v
3434
theories/EInlineProjections.v
3535
theories/ETransform.v
3636
theories/EConstructorsAsBlocks.v
37+
theories/EWcbvEvalCstrsAsBlocksInd.v
3738
theories/Erasure.v

erasure/theories/EConstructorsAsBlocks.v

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -468,62 +468,37 @@ Definition env_flags_blocks :=
468468

469469
Local Existing Instance env_flags.
470470

471-
Lemma Qpreserves_wellformed Σ : wf_glob Σ -> Qpreserves (fun n x => wellformed Σ n x) Σ.
472-
Proof.
473-
intros clΣ.
474-
split.
475-
- red. move=> n t.
476-
destruct t; cbn; intuition auto; try solve [constructor; auto].
477-
eapply on_letin; rtoProp; intuition auto.
478-
eapply on_app; rtoProp; intuition auto.
479-
eapply on_case; rtoProp; intuition auto. solve_all.
480-
eapply on_fix. solve_all. move/andP: H => [] _ ha. solve_all.
481-
- red. intros kn decl.
482-
move/(lookup_env_wellformed clΣ).
483-
unfold wf_global_decl. destruct cst_body => //.
484-
- red. move=> hasapp n t args. rewrite wellformed_mkApps //.
485-
split; intros; rtoProp; intuition auto; solve_all.
486-
- red. cbn => //.
487-
(* move=> hascase n ci discr brs. simpl.
488-
destruct lookup_inductive eqn:hl => /= //.
489-
split; intros; rtoProp; intuition auto; solve_all. *)
490-
- red. move=> hasproj n p discr. now cbn in hasproj.
491-
- red. move=> t args clt cll.
492-
eapply wellformed_substl. solve_all. now rewrite Nat.add_0_r.
493-
- red. move=> n mfix idx. cbn. unfold wf_fix.
494-
split; intros; rtoProp; intuition auto; solve_all. now apply Nat.ltb_lt.
495-
- red. move=> n mfix idx. cbn.
496-
split; intros; rtoProp; intuition auto; solve_all.
497-
Qed.
498-
499471
Definition block_wcbv_flags :=
500472
{| with_prop_case := false ; with_guarded_fix := false ; with_constructor_as_block := true |}.
501473

502474
Local Hint Resolve wellformed_closed : core.
503475

504-
Lemma wellformed_lookup_inductive_pars Σ kn mdecl :
476+
Lemma wellformed_lookup_inductive_pars {efl : EEnvFlags} Σ kn mdecl :
477+
has_cstr_params = false ->
505478
wf_glob Σ ->
506479
lookup_minductive Σ kn = Some mdecl -> mdecl.(ind_npars) = 0.
507480
Proof.
481+
intros hasp.
508482
induction 1; cbn => //.
509483
case: eqb_spec => [|].
510484
- intros ->. destruct d => //. intros [= <-].
511485
cbn in H0. unfold wf_minductive in H0.
512-
rtoProp. cbn in H0. now eapply eqb_eq in H0.
486+
rtoProp. cbn in H0. rewrite hasp in H0; now eapply eqb_eq in H0.
513487
- intros _. eapply IHwf_glob.
514488
Qed.
515489

516-
Lemma wellformed_lookup_constructor_pars {Σ kn c mdecl idecl cdecl} :
490+
Lemma wellformed_lookup_constructor_pars {efl : EEnvFlags} {Σ kn c mdecl idecl cdecl} :
491+
has_cstr_params = false ->
517492
wf_glob Σ ->
518493
lookup_constructor Σ kn c = Some (mdecl, idecl, cdecl) -> mdecl.(ind_npars) = 0.
519494
Proof.
520-
intros wf. cbn -[lookup_minductive].
495+
intros hasp wf. cbn -[lookup_minductive].
521496
destruct lookup_minductive eqn:hl => //.
522497
do 2 destruct nth_error => //.
523498
eapply wellformed_lookup_inductive_pars in hl => //. congruence.
524499
Qed.
525500

526-
Lemma lookup_constructor_pars_args_spec {Σ ind n mdecl idecl cdecl} :
501+
Lemma lookup_constructor_pars_args_spec {efl : EEnvFlags} {Σ ind n mdecl idecl cdecl} :
527502
wf_glob Σ ->
528503
lookup_constructor Σ ind n = Some (mdecl, idecl, cdecl) ->
529504
lookup_constructor_pars_args Σ ind n = Some (mdecl.(ind_npars), cdecl.(cstr_nargs)).
@@ -533,23 +508,25 @@ Proof.
533508
intros [= -> -> <-]. cbn. f_equal.
534509
Qed.
535510

536-
Lemma wellformed_lookup_constructor_pars_args {Σ ind n block_args} :
511+
Lemma wellformed_lookup_constructor_pars_args {efl : EEnvFlags} {Σ ind n block_args} :
537512
wf_glob Σ ->
513+
has_cstr_params = false ->
538514
wellformed Σ 0 (EAst.tConstruct ind n block_args) ->
539515
∑ args, lookup_constructor_pars_args Σ ind n = Some (0, args).
540516
Proof.
541-
intros wfΣ wf. cbn -[lookup_constructor] in wf.
517+
intros wfΣ hasp wf. cbn -[lookup_constructor] in wf.
542518
destruct lookup_constructor as [[[mdecl idecl] cdecl]|] eqn:hl => //.
543519
exists cdecl.(cstr_nargs).
544-
pose proof (wellformed_lookup_constructor_pars wfΣ hl).
520+
pose proof (wellformed_lookup_constructor_pars hasp wfΣ hl).
545521
eapply lookup_constructor_pars_args_spec in hl => //. congruence.
522+
destruct has_tConstruct => //.
546523
Qed.
547524

548-
Lemma constructor_isprop_pars_decl_params {Σ ind c b pars cdecl} :
549-
wf_glob Σ ->
525+
Lemma constructor_isprop_pars_decl_params {efl : EEnvFlags} {Σ ind c b pars cdecl} :
526+
has_cstr_params = false -> wf_glob Σ ->
550527
constructor_isprop_pars_decl Σ ind c = Some (b, pars, cdecl) -> pars = 0.
551528
Proof.
552-
intros hwf.
529+
intros hasp hwf.
553530
rewrite /constructor_isprop_pars_decl /lookup_constructor /lookup_inductive.
554531
destruct lookup_minductive as [mdecl|] eqn:hl => /= //.
555532
do 2 destruct nth_error => //.
@@ -601,7 +578,7 @@ Proof.
601578
+ eapply EEtaExpandedFix.decompose_app_tApp_split in da as [Ha Ht].
602579
cbn in wf.
603580
move: wf => /andP[]. rewrite Ha wellformed_mkApps // => /andP[] wfc wfl wft.
604-
destruct (wellformed_lookup_constructor_pars_args wfΣ wfc).
581+
destruct (wellformed_lookup_constructor_pars_args wfΣ eq_refl wfc).
605582
rewrite e. cbn.
606583
destruct chop eqn:eqch => //.
607584
intros. apply H1. intuition auto.
@@ -674,14 +651,17 @@ Proof.
674651
Qed.
675652

676653
Lemma lookup_constructor_transform_blocks Σ ind c :
677-
lookup_constructor (transform_blocks_env Σ) ind c =
678-
lookup_constructor Σ ind c.
654+
lookup_constructor (transform_blocks_env Σ) ind c =
655+
lookup_constructor Σ ind c.
679656
Proof.
680657
unfold lookup_constructor, lookup_inductive, lookup_minductive in *.
681658
rewrite lookup_env_transform_blocks.
682659
destruct lookup_env as [ [] | ]; cbn; congruence.
683660
Qed.
684661

662+
Lemma isLambda_transform_blocks Σ c : isLambda c -> isLambda (transform_blocks Σ c).
663+
Proof. destruct c => //. Qed.
664+
685665
Lemma transform_wellformed' Σ n t :
686666
wf_glob Σ ->
687667
@wellformed env_flags Σ n t ->
@@ -692,9 +672,12 @@ Proof.
692672
all: rewrite ?map_InP_spec; toAll; eauto; try now solve_all.
693673
- destruct H1. unfold isEtaExp_app in H1. unfold lookup_constructor_pars_args in *.
694674
destruct (lookup_constructor Σ) as [[[]] | ]; try congruence; cbn - [transform_blocks].
695-
2: eauto. split; auto.
675+
2: eauto. split; auto. cbn in H1. eapply Nat.leb_le in H1.
676+
apply/eqb_spec. lia.
696677
- destruct H4. solve_all.
697-
- unfold wf_fix in *. rtoProp. solve_all. len. solve_all. len. destruct x.
678+
- unfold wf_fix in *. rtoProp. solve_all. now eapply isLambda_transform_blocks.
679+
- unfold wf_fix in *. rtoProp. solve_all.
680+
len. solve_all. len. destruct x.
698681
cbn -[transform_blocks isEtaExp] in *. rtoProp. eauto.
699682
- rewrite !wellformed_mkApps in Hw |- * => //. rtoProp. intros.
700683
eapply isEtaExp_mkApps in H3. rewrite decompose_app_mkApps in H3; eauto.
@@ -708,16 +691,18 @@ Proof.
708691
rewrite ?lookup_constructor_transform_blocks; eauto.
709692
* destruct lookup_constructor as [ [[]] | ] eqn:E; cbn -[transform_blocks] in *; eauto.
710693
invs Heq. rewrite chop_firstn_skipn in Ec. invs Ec.
711-
rewrite firstn_length. len. eapply Nat.leb_le in H2. eapply Nat.leb_le.
712-
destruct lookup_env as [ [] | ] eqn:E'; try congruence.
713-
eapply lookup_env_wellformed in E'; eauto.
714-
cbn in E'. red in E'. unfold wf_minductive in E'.
715-
rewrite andb_true_iff in E'.
716-
cbn in E'. destruct E'.
717-
eapply Nat.eqb_eq in H6.
718-
destruct nth_error; invs E.
719-
destruct nth_error; invs H9.
720-
rewrite H6. lia.
694+
rewrite firstn_length. len. eapply Nat.leb_le in H2.
695+
apply/eqb_spec.
696+
assert (ind_npars m0 = 0).
697+
{ destruct lookup_env as [ [] | ] eqn:E'; try congruence.
698+
eapply lookup_env_wellformed in E'; eauto.
699+
cbn in E'. red in E'. unfold wf_minductive in E'.
700+
rewrite andb_true_iff in E'.
701+
cbn in E'. destruct E'.
702+
eapply Nat.eqb_eq in H6.
703+
destruct nth_error; invs E.
704+
now destruct nth_error; invs H9. }
705+
lia.
721706
* rewrite chop_firstn_skipn in Ec. invs Ec.
722707
solve_all. eapply All_firstn. solve_all.
723708
* rewrite chop_firstn_skipn in Ec. invs Ec.
@@ -994,11 +979,18 @@ Proof.
994979
eapply All2_length in X0 as Hlen.
995980
cbn.
996981
rewrite !skipn_all Hlen skipn_all firstn_all. cbn.
997-
eapply eval_mkApps_Construct_block; tea. eauto.
982+
eapply eval_construct_block; tea. eauto.
998983
now rewrite lookup_constructor_transform_blocks.
999-
constructor. cbn [atom]. now rewrite lookup_constructor_transform_blocks H.
1000-
len. unfold cstr_arity. lia.
1001-
solve_all. destruct H6; eauto.
1002-
- intros. econstructor. destruct t; try solve [cbn in H, H0 |- *; try congruence].
1003-
cbn -[lookup_constructor] in H |- *. destruct l => //. now rewrite lookup_constructor_transform_blocks H.
984+
unfold cstr_arity. cbn. rewrite H4; len.
985+
solve_all. clear -X0. eapply All2_All2_Set. solve_all.
986+
apply H.
987+
- intros. destruct t; try solve [constructor; cbn in H, H0 |- *; try congruence].
988+
cbn -[lookup_constructor] in H |- *. destruct l => //.
989+
destruct lookup_constructor eqn:hl => //.
990+
destruct p as [[mdecl idecl] cdecl].
991+
eapply eval_construct_block => //.
992+
now rewrite lookup_constructor_transform_blocks hl.
993+
simp_eta in H1. cbn in H1. unfold isEtaExp_app in H1.
994+
rewrite (lookup_constructor_pars_args_spec wfΣ hl) andb_true_r in H1.
995+
apply Nat.leb_le in H1; cbn; unfold cstr_arity. lia.
1004996
Qed.

erasure/theories/EDeps.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Proof.
281281
+ intuition auto.
282282
apply erases_deps_mkApps_inv in H4.
283283
now apply Forall_rev, Forall_skipn.
284-
+ eapply nth_error_forall in e1; [|now eauto].
284+
+ eapply nth_error_forall in e2; [|now eauto].
285285
assumption.
286286
- congruence.
287287
- depelim er.
@@ -333,7 +333,7 @@ Proof.
333333
intuition auto.
334334
apply erases_deps_mkApps_inv in H3 as (? & ?).
335335
apply IHev2.
336-
now eapply nth_error_forall in e2.
336+
now eapply nth_error_forall in e3.
337337
- congruence.
338338
- constructor.
339339
- depelim er.

erasure/theories/EEtaExpandedFix.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ Proof.
13921392
destruct s.
13931393
* destruct p; solve_discr. noconf H3.
13941394
right. len.
1395-
move: e; unfold isEtaExp_fixapp.
1395+
move: e1; unfold isEtaExp_fixapp.
13961396
unfold EGlobalEnv.cunfold_fix. destruct nth_error eqn:hnth => //.
13971397
intros [=]. rewrite H3. rewrite -(All2_length a0). eapply Nat.ltb_lt; lia.
13981398
* right. len. eapply isEtaExp_fixapp_mon; tea. lia.
@@ -1404,7 +1404,7 @@ Proof.
14041404
destruct s.
14051405
* destruct p; solve_discr. noconf H2.
14061406
left. split.
1407-
unfold isStuckFix'; rewrite e. len. eapply Nat.leb_le. lia.
1407+
unfold isStuckFix'; rewrite e1. len. eapply Nat.leb_le. lia.
14081408
now rewrite -[tApp _ _](mkApps_app _ _ [av]).
14091409
* right. len. eapply isEtaExp_fixapp_mon; tea. lia.
14101410
+ eapply mkApps_eq in H1 as [? []] => //; subst.
@@ -1571,7 +1571,7 @@ Proof.
15711571
- pose proof (eval_trans' H H0_0). subst a'. econstructor; tea.
15721572
- pose proof (eval_trans' H H0_0). subst av. eapply eval_fix; tea.
15731573
- pose proof (eval_trans' H H0_0). subst av. eapply eval_fix_value; tea.
1574-
- eapply value_final in X. pose proof (eval_trans' X H0_). subst f.
1574+
- eapply value_final in X. pose proof (eval_trans' X H0_). subst f7.
15751575
pose proof (eval_trans' H H0_0). subst av.
15761576
eapply eval_fix'; tea.
15771577
- eapply eval_construct; tea.
@@ -1854,7 +1854,7 @@ Proof.
18541854
rewrite (remove_last_last l0 a hl).
18551855
rewrite -[tApp _ _](mkApps_app _ _ [a']).
18561856
eapply eval_mkApps_Construct; tea.
1857-
{ constructor. cbn [atom]; rewrite e0 //. }
1857+
{ constructor. cbn [atom]; rewrite e e0 //. }
18581858
{ len. rewrite (All2_length hargs). lia. }
18591859
eapply All2_app.
18601860
eapply forallb_remove_last, forallb_All in etal.

erasure/theories/EInlineProjections.v

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ Section optimize.
112112

113113
(* move to globalenv *)
114114

115-
115+
Lemma isLambda_optimize t : isLambda t -> isLambda (optimize t).
116+
Proof. destruct t => //. Qed.
117+
Lemma isBox_optimize t : isBox t -> isBox (optimize t).
118+
Proof. destruct t => //. Qed.
119+
116120
Lemma wf_optimize t k :
117121
wf_glob Σ ->
118122
wellformed Σ k t -> wellformed Σ k (optimize t).
@@ -135,6 +139,7 @@ Section optimize.
135139
rewrite IHt //=; len. apply Nat.ltb_lt.
136140
lia.
137141
- len. rtoProp; solve_all. rewrite forallb_map; solve_all.
142+
now eapply isLambda_optimize. solve_all.
138143
- len. rtoProp; solve_all. rewrite forallb_map; solve_all.
139144
Qed.
140145

@@ -161,7 +166,7 @@ Section optimize.
161166
have arglen := wellformed_projection_args wfΣ hl.
162167
case: Nat.compare_spec. lia. lia.
163168
auto.
164-
- f_equal. move/andP: wft => [hidx hb].
169+
- f_equal. move/andP: wft => [hlam /andP[] hidx hb].
165170
solve_all. unfold map_def. f_equal.
166171
eapply a0. now rewrite -Nat.add_assoc.
167172
- f_equal. move/andP: wft => [hidx hb].
@@ -222,7 +227,7 @@ Section optimize.
222227
intros wfΣ hfix.
223228
unfold cunfold_fix.
224229
rewrite nth_error_map.
225-
cbn in hfix. move/andP: hfix => [] hidx hfix.
230+
cbn in hfix. move/andP: hfix => [] hlam /andP[] hidx hfix.
226231
destruct nth_error eqn:hnth => //.
227232
intros [= <- <-] => /=. f_equal.
228233
rewrite optimize_substl //. eapply wellformed_fix_subst => //.
@@ -512,23 +517,23 @@ Proof.
512517
eapply nth_error_forallb in wfbrs; tea.
513518
rewrite Nat.add_0_r in wfbrs.
514519
forward IHev2. eapply wellformed_iota_red; tea => //.
515-
rewrite optimize_iota_red in IHev2 => //. now rewrite e3.
520+
rewrite optimize_iota_red in IHev2 => //. now rewrite e4.
516521
econstructor; eauto.
517522
rewrite -is_propositional_cstr_optimize //. tea.
518-
rewrite nth_error_map e1 //. len. len.
523+
rewrite nth_error_map e2 //. len. len.
519524

520525
- congruence.
521526

522527
- move/andP => [] /andP[] hl wfd wfbrs.
523528
forward IHev2. eapply wellformed_substl; tea => //.
524529
rewrite forallb_repeat //. len.
525-
rewrite e0 /= Nat.add_0_r in wfbrs. now move/andP: wfbrs.
530+
rewrite e1 /= Nat.add_0_r in wfbrs. now move/andP: wfbrs.
526531
rewrite optimize_substl in IHev2 => //.
527532
rewrite forallb_repeat //. len.
528-
rewrite e0 /= Nat.add_0_r in wfbrs. now move/andP: wfbrs.
533+
rewrite e1 /= Nat.add_0_r in wfbrs. now move/andP: wfbrs.
529534
eapply eval_iota_sing => //; eauto.
530535
rewrite -is_propositional_optimize //.
531-
rewrite e0 //. simpl.
536+
rewrite e1 //. simpl.
532537
rewrite map_repeat in IHev2 => //.
533538

534539
- move/andP => [] clf cla. rewrite optimize_mkApps in IHev1.
@@ -600,7 +605,7 @@ Proof.
600605
move/wf_mkApps: ev1 => [] wfc wfargs.
601606
destruct lookup_projection as [[[[mdecl idecl] cdecl'] pdecl]|] eqn:hl' => //.
602607
pose proof (lookup_projection_lookup_constructor hl').
603-
rewrite (constructor_isprop_pars_decl_constructor H) in e0. noconf e0.
608+
rewrite (constructor_isprop_pars_decl_constructor H) in e1. noconf e1.
604609
forward IHev1 by auto.
605610
forward IHev2. eapply nth_error_forallb in wfargs; tea.
606611
rewrite optimize_mkApps /= in IHev1.
@@ -617,11 +622,11 @@ Proof.
617622
rewrite nth_error_rev. len. rewrite skipn_length. lia.
618623
rewrite List.rev_involutive. len. rewrite skipn_length.
619624
rewrite nth_error_skipn nth_error_map.
620-
rewrite e1 -H1.
625+
rewrite e2 -H1.
621626
assert((ind_npars mdecl + cstr_nargs cdecl - ind_npars mdecl) = cstr_nargs cdecl) by lia.
622627
rewrite H3.
623-
eapply (f_equal (option_map (optimize Σ))) in e2.
624-
cbn in e2. rewrite -e2. f_equal. f_equal. lia.
628+
eapply (f_equal (option_map (optimize Σ))) in e3.
629+
cbn in e3. rewrite -e3. f_equal. f_equal. lia.
625630

626631
- congruence.
627632

@@ -630,7 +635,7 @@ Proof.
630635
destruct lookup_projection as [[[[mdecl idecl] cdecl'] pdecl]|] eqn:hl' => //.
631636
pose proof (lookup_projection_lookup_constructor hl').
632637
simpl in H.
633-
move: e. rewrite /inductive_isprop_and_pars.
638+
move: e0. rewrite /inductive_isprop_and_pars.
634639
rewrite (lookup_constructor_lookup_inductive H) /=.
635640
intros [= eq <-].
636641
eapply eval_iota_sing => //; eauto.
@@ -680,11 +685,6 @@ Qed.
680685

681686
From MetaCoq.Erasure Require Import EEtaExpanded.
682687

683-
Lemma isLambda_optimize Σ t : isLambda t -> isLambda (optimize Σ t).
684-
Proof. destruct t => //. Qed.
685-
Lemma isBox_optimize Σ t : isBox t -> isBox (optimize Σ t).
686-
Proof. destruct t => //. Qed.
687-
688688
Lemma optimize_expanded {Σ : GlobalContextMap.t} t : expanded Σ t -> expanded Σ (optimize Σ t).
689689
Proof.
690690
induction 1 using expanded_ind.
@@ -829,7 +829,7 @@ Proof.
829829
rewrite hrel IHt //= andb_true_r.
830830
have hargs' := wellformed_projection_args wfΣ hl'.
831831
apply Nat.ltb_lt. len.
832-
- cbn. unfold wf_fix; rtoProp; intuition auto; solve_all. now len.
832+
- cbn. unfold wf_fix; rtoProp; intuition auto; solve_all. now eapply isLambda_optimize. now len.
833833
unfold test_def in *. len. eauto.
834834
- cbn. unfold wf_fix; rtoProp; intuition auto; solve_all. now len.
835835
unfold test_def in *. len. eauto.

0 commit comments

Comments
 (0)