Skip to content

Commit 13dc7ce

Browse files
authored
Merge pull request #1089 from MetaCoq/force-lazy-evaluation
Add the `force (lazy t)` evaluation rule to LambdaBox
2 parents f55eb7c + a4cd12c commit 13dc7ce

18 files changed

+249
-73
lines changed

erasure-plugin/theories/ETransform.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ Program Definition coinductive_to_inductive_transformation (efl : EEnvFlags)
976976
{has_cstrblocks : cstr_as_blocks = true} :
977977
Transform.t _ _ EAst.term EAst.term _ _
978978
(eval_eprogram_env block_wcbv_flags) (eval_eprogram block_wcbv_flags) :=
979-
{| name := "transforming co-inductive to inductive types";
979+
{| name := "transforming co-inductive to lazy inductive types";
980980
transform p _ := ECoInductiveToInductive.trans_program p ;
981981
pre p := wf_eprogram_env efl p ;
982982
post p := wf_eprogram efl p ;

erasure/theories/EAstUtils.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,25 @@ Definition is_box c :=
318318
| _ => false
319319
end.
320320

321+
Definition isLazy c :=
322+
match c with
323+
| tLazy _ => true
324+
| _ => false
325+
end.
326+
321327
Definition isFixApp t := isFix (head t).
322328
Definition isConstructApp t := isConstruct (head t).
323329
Definition isPrimApp t := isPrim (head t).
330+
Definition isLazyApp t := isLazy (head t).
324331

325332
Lemma isFixApp_mkApps f l : isFixApp (mkApps f l) = isFixApp f.
326333
Proof. rewrite /isFixApp head_mkApps //. Qed.
327334
Lemma isConstructApp_mkApps f l : isConstructApp (mkApps f l) = isConstructApp f.
328335
Proof. rewrite /isConstructApp head_mkApps //. Qed.
329336
Lemma isPrimApp_mkApps f l : isPrimApp (mkApps f l) = isPrimApp f.
330337
Proof. rewrite /isPrimApp head_mkApps //. Qed.
338+
Lemma isLazyApp_mkApps f l : isLazyApp (mkApps f l) = isLazyApp f.
339+
Proof. rewrite /isLazyApp head_mkApps //. Qed.
331340

332341
Lemma is_box_mkApps f a : is_box (mkApps f a) = is_box f.
333342
Proof.
@@ -347,6 +356,8 @@ Lemma nisBox_mkApps f args : ~~ isBox f -> ~~ isBox (mkApps f args).
347356
Proof. destruct args using rev_case => //. rewrite mkApps_app /= //. Qed.
348357
Lemma nisPrim_mkApps f args : ~~ isPrim f -> ~~ isPrim (mkApps f args).
349358
Proof. destruct args using rev_case => //. rewrite mkApps_app /= //. Qed.
359+
Lemma nisLazy_mkApps f args : ~~ isLazy f -> ~~ isLazy (mkApps f args).
360+
Proof. destruct args using rev_case => //. rewrite mkApps_app /= //. Qed.
350361

351362
Definition string_of_def {A : Set} (f : A -> string) (def : def A) :=
352363
"(" ^ string_of_name (dname def) ^ "," ^ f (dbody def) ^ ","

erasure/theories/EConstructorsAsBlocks.v

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,28 @@ Proof.
649649
eapply IHt1. cbn in Hwf'. rtoProp. intuition.
650650
Qed.
651651

652+
Lemma transform_blocks_isLazyApp {efl : EEnvFlags} {Σ : GlobalContextMap.t} t :
653+
has_cstr_params = false ->
654+
wf_glob Σ -> wellformed Σ 0 t ->
655+
isLazyApp (transform_blocks Σ t) = isLazyApp t.
656+
Proof.
657+
intros haspars Hwf Hwf'.
658+
induction t; try now cbn; eauto.
659+
eapply transform_blocks_tApp; eauto.
660+
destruct decompose_app.
661+
destruct construct_viewc.
662+
- rewrite GlobalContextMap.lookup_constructor_pars_args_spec.
663+
destruct lookup_constructor_pars_args as [ [[]] | ]; eauto.
664+
cbn. destruct chop. intros (? & ? & ?). subst.
665+
rewrite -[tApp _ _](mkApps_app _ _ [t2]).
666+
rewrite !isLazyApp_mkApps. cbn. reflexivity.
667+
- change (tApp t1 t2) with (mkApps t1 [t2]).
668+
change (tApp (transform_blocks Σ t1) (transform_blocks Σ t2)) with
669+
(mkApps (transform_blocks Σ t1) [transform_blocks Σ t2]).
670+
rewrite !isLazyApp_mkApps.
671+
eapply IHt1. cbn in Hwf'. rtoProp. intuition.
672+
Qed.
673+
652674
Lemma lookup_env_transform_blocks {Σ : GlobalContextMap.t} kn :
653675
lookup_env (transform_blocks_env Σ) kn =
654676
option_map (transform_blocks_decl Σ) (lookup_env Σ kn).
@@ -1125,17 +1147,20 @@ Proof.
11251147
* rewrite GlobalContextMap.lookup_constructor_pars_args_spec;
11261148
destruct lookup_constructor_pars_args as [ [[]] | ] eqn:hpa; eauto.
11271149
cbn [plus]. destruct chop eqn:heqch.
1128-
intros [hl [ht ha]]. rewrite ht in H1. rewrite isConstructApp_mkApps isPrimApp_mkApps orb_true_r in H1 => //.
1150+
intros [hl [ht ha]]. rewrite ht in H1. rewrite isConstructApp_mkApps
1151+
isPrimApp_mkApps isLazyApp_mkApps orb_true_r in H1 => //.
11291152
* eapply eval_app_cong; eauto.
11301153
revert H1.
11311154
destruct f'; try now cbn; tauto.
11321155
intros H. cbn in H.
11331156
rewrite transform_blocks_isConstructApp; eauto.
11341157
rewrite transform_blocks_isPrimApp; eauto.
1135-
rewrite negb_or in H. move/andP: H => [] ncstr nprim.
1158+
rewrite transform_blocks_isLazyApp; eauto.
1159+
rewrite !negb_or in H. move/andP: H => [] /andP [] ncstr nprim nlazy.
11361160
destruct (isConstructApp (tApp f'1 f'2)) eqn:heq'.
11371161
-- cbn in ncstr. congruence.
1138-
-- eapply transform_blocks_tApp; eauto. clear -nprim.
1162+
-- eapply transform_blocks_tApp; eauto. clear -nprim nlazy.
1163+
move/negbTE: nlazy ->. move/negbTE: nprim -> => /=.
11391164
destruct decompose_app.
11401165
destruct construct_viewc; try now cbn; eauto.
11411166
rewrite GlobalContextMap.lookup_constructor_pars_args_spec;
@@ -1186,6 +1211,11 @@ Proof.
11861211
eapply All2_over_undep in a. eapply All2_Set_All2 in ev. eapply All2_All2_Set. solve_all.
11871212
now destruct b.
11881213
now destruct a0.
1214+
- intros evl evt' [evt wft wflt etat etalt].
1215+
intros [evt'' wft' wfv etat' etav].
1216+
simp transform_blocks; rewrite -!transform_blocks_equation_1.
1217+
econstructor; eauto.
1218+
now simp transform_blocks in evt; rewrite -!transform_blocks_equation_1 in evt.
11891219
- intros. destruct t; try solve [constructor; cbn in H, H0 |- *; try congruence].
11901220
cbn -[lookup_constructor] in H |- *. destruct args => //.
11911221
destruct lookup_constructor eqn:hl => //.

erasure/theories/EDeps.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ Proof.
354354
- depelim er; depelim X; constructor; eauto.
355355
eapply All2_over_undep in a0. solve_all.
356356
- easy.
357+
- easy.
357358
Qed.
358359

359360
#[global]

erasure/theories/EEtaExpandedFix.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ Lemma eval_etaexp {fl : WcbvFlags} {efl : EEnvFlags} {wcon : with_constructor_as
10561056
Proof.
10571057
intros etaΣ wfΣ.
10581058
induction 1 as [ | ? ? ? ? ? ? ? ? IHs | | | | | ? ? ? ? ? ? ? ? ? ? ? IHs | ? ? ? ? ? ? ? ? ? ? ? IHs
1059-
| ? ? ? ? ? ? ? ? ? ? IHs | | | | | | | | | | | ] using eval_mkApps_rect; try now congruence.
1059+
| ? ? ? ? ? ? ? ? ? ? IHs | | | | | | | | | | | | ] using eval_mkApps_rect; try now congruence.
10601060
all:try simp isEtaExp; rewrite -?isEtaExp_equation_1 => //.
10611061
6:{
10621062
move/isEtaExp_tApp'.
@@ -1375,6 +1375,7 @@ Proof.
13751375
- solve_all.
13761376
depelim X; solve_all. eapply All2_over_undep in a. subst a0 a';
13771377
depelim H; constructor; solve_all. solve_all.
1378+
- simp_eta in IHeval1. eauto.
13781379
Qed.
13791380

13801381
Lemma isEtaExp_fixapp_mon {mfix idx n n'} : n <= n' -> isEtaExp_fixapp mfix idx n -> isEtaExp_fixapp mfix idx n'.
@@ -1961,6 +1962,8 @@ Proof.
19611962
- intros hexp. simp_eta in hexp. depelim X; repeat constructor; eauto.
19621963
eapply All2_over_undep in a. subst a0 a'. solve_all. depelim hexp; cbn in *. destruct p.
19631964
eapply All2_All2_Set. solve_all. solve_all. depelim hexp. destruct p. solve_all.
1965+
- intros hexp. simp_eta in hexp. econstructor; eauto. apply IHeval2.
1966+
specialize (IHeval1 hexp). eapply eval_etaexp in IHeval1. now simp_eta in IHeval1. all:eauto.
19641967
- intros hexp. now eapply eval_atom.
19651968
Unshelve. all: eauto.
19661969
Qed.

erasure/theories/EImplementBox.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,15 @@ Proof.
388388
rewrite (isPrimApp_mkApps _ [_]). eauto.
389389
Qed.
390390

391+
Lemma implement_box_isLazyApp {efl : EEnvFlags} {Σ : global_declarations} t :
392+
isLazyApp (implement_box t) = isLazyApp t.
393+
Proof.
394+
induction t; try now cbn; eauto.
395+
simp implement_box.
396+
rewrite (isLazyApp_mkApps _ [t2]).
397+
rewrite (isLazyApp_mkApps _ [_]). eauto.
398+
Qed.
399+
391400
Lemma lookup_env_implement_box {Σ : global_declarations} kn :
392401
lookup_env (implement_box_env Σ) kn =
393402
option_map (implement_box_decl) (lookup_env Σ kn).
@@ -670,6 +679,7 @@ Proof.
670679
intros H. cbn in H.
671680
rewrite implement_box_isConstructApp; eauto.
672681
rewrite implement_box_isPrimApp; eauto.
682+
rewrite implement_box_isLazyApp; eauto.
673683
- intros; repeat match goal with [H : MCProd.and3 _ _ _ |- _] => destruct H end.
674684
simp implement_box in *.
675685
eapply eval_construct_block; tea. eauto.
@@ -680,6 +690,9 @@ Proof.
680690
- intros wf H; depelim H; simp implement_box; repeat constructor.
681691
destruct a0. eapply All2_over_undep in a. eapply All2_All2_Set, All2_map.
682692
cbn -[implement_box]. solve_all. now destruct H. now destruct a0.
693+
- intros evt evt' [] [].
694+
simp implement_box. simp implement_box in e.
695+
econstructor; eauto.
683696
- intros. destruct t; try solve [constructor; cbn in H, H0 |- *; try congruence].
684697
cbn -[lookup_constructor] in H |- *. destruct args => //.
685698
Qed.

erasure/theories/EInlineProjections.v

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,13 +710,14 @@ Proof.
710710
* destruct with_guarded_fix.
711711
+ move: i.
712712
rewrite !negb_or.
713-
rewrite optimize_mkApps !isFixApp_mkApps !isConstructApp_mkApps !isPrimApp_mkApps.
713+
rewrite optimize_mkApps !isFixApp_mkApps !isConstructApp_mkApps !isPrimApp_mkApps
714+
!isLazyApp_mkApps.
714715
destruct args using rev_case => // /=. rewrite map_app !mkApps_app /= //.
715716
rewrite !andb_true_r.
716717
rtoProp; intuition auto; destruct v => /= //.
717718
+ move: i.
718719
rewrite !negb_or.
719-
rewrite optimize_mkApps !isConstructApp_mkApps !isPrimApp_mkApps.
720+
rewrite optimize_mkApps !isConstructApp_mkApps !isPrimApp_mkApps !isLazyApp_mkApps.
720721
destruct args using rev_case => // /=. rewrite map_app !mkApps_app /= //.
721722
destruct v => /= //.
722723
- intros; rtoProp; intuition eauto.
@@ -725,6 +726,8 @@ Proof.
725726
eapply All2_Set_All2 in ev. eapply All2_All2_Set. primProp.
726727
subst a0 a'; cbn in *. depelim H0; cbn in *. intuition auto; solve_all.
727728
primProp; depelim H0; intuition eauto.
729+
- intros wf; econstructor; eauto. eapply IHev2.
730+
eapply eval_wellformed in ev1; tea => //.
728731
- destruct t => //.
729732
all:constructor; eauto.
730733
cbn [atom optimize] in i |- *.

erasure/theories/EOptimizePropDiscr.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,22 +715,24 @@ Proof.
715715
* destruct with_guarded_fix.
716716
+ move: i.
717717
rewrite !negb_or.
718-
rewrite remove_match_on_box_mkApps !isFixApp_mkApps !isConstructApp_mkApps !isPrimApp_mkApps.
718+
rewrite remove_match_on_box_mkApps !isFixApp_mkApps !isConstructApp_mkApps !isPrimApp_mkApps !isLazyApp_mkApps.
719719
destruct args using rev_case => // /=. rewrite map_app !mkApps_app /= //.
720720
rewrite !andb_true_r.
721721
rtoProp; intuition auto.
722722
destruct v => /= //.
723723
destruct v => /= //.
724724
destruct v => /= //.
725+
destruct v => /= //.
725726
+ move: i.
726727
rewrite !negb_or.
727-
rewrite remove_match_on_box_mkApps !isConstructApp_mkApps !isPrimApp_mkApps.
728+
rewrite remove_match_on_box_mkApps !isConstructApp_mkApps !isPrimApp_mkApps !isLazyApp_mkApps.
728729
destruct args using rev_case => // /=. rewrite map_app !mkApps_app /= //.
729730
destruct v => /= //.
730731
- depelim X; repeat constructor.
731732
eapply All2_over_undep in a. eapply All2_All2_Set. eapply All2_Set_All2 in ev. subst a0 a'; cbn in *.
732733
rewrite /test_array_model /= in H. rtoProp; intuition auto; solve_all. eapply e.
733734
cbn in H. rewrite /test_array_model /= in H. now move/andP: H => [].
735+
- intros. econstructor; eauto. eapply IHev2. eapply eval_closed in ev1; tea.
734736
- destruct t => //.
735737
all:constructor; eauto. cbn [atom remove_match_on_box] in i |- *.
736738
rewrite -lookup_constructor_remove_match_on_box //. destruct args => //.

erasure/theories/ERemoveParams.v

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,14 @@ Proof.
779779
all:rewrite !isPrimApp_mkApps //.
780780
Qed.
781781

782+
Lemma strip_isLazyApp Σ f :
783+
isLazyApp f = isLazyApp (strip Σ f).
784+
Proof.
785+
funelim (strip Σ f); cbn -[strip] => //.
786+
all:rewrite map_InP_spec.
787+
all:rewrite !isLazyApp_mkApps //.
788+
Qed.
789+
782790
Lemma lookup_inductive_pars_is_prop_and_pars {Σ ind b pars} :
783791
inductive_isprop_and_pars Σ ind = Some (b, pars) ->
784792
lookup_inductive_pars Σ (inductive_mind ind) = Some pars.
@@ -1011,8 +1019,8 @@ Proof.
10111019
- rewrite !strip_tApp //.
10121020
eapply eval_app_cong; tea.
10131021
move: H1. eapply contraNN.
1014-
rewrite -strip_isLambda -strip_isConstructApp -strip_isFixApp -strip_isBox -strip_isPrimApp //.
1015-
rewrite -strip_isFix //.
1022+
rewrite -strip_isLambda -strip_isConstructApp -strip_isFixApp -strip_isBox -strip_isPrimApp
1023+
-strip_isLazyApp // -strip_isFix //.
10161024

10171025
- rewrite !strip_mkApps // /=.
10181026
rewrite (lookup_constructor_lookup_inductive_pars H).
@@ -1030,6 +1038,8 @@ Proof.
10301038
eapply All2_over_undep in a. eapply All2_Set_All2 in ev. eapply All2_All2_Set. solve_all. now destruct b.
10311039
now destruct a0.
10321040

1041+
- simp_strip. simp_strip in e0. econstructor; tea.
1042+
10331043
- destruct t => //.
10341044
all:constructor; eauto. simp strip.
10351045
cbn [atom strip] in H |- *.

erasure/theories/EWcbvEval.v

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Definition atom `{wfl : WcbvFlags} Σ t :=
3838
| tBox
3939
| tCoFix _ _
4040
| tLambda _ _
41+
| tLazy _
4142
| tFix _ _ => true
4243
| tConstruct ind c [] => negb with_constructor_as_block && isSome (lookup_constructor Σ ind c)
4344
| _ => false
@@ -258,7 +259,7 @@ Section Wcbv.
258259
| eval_app_cong f f' a a' :
259260
eval f f' ->
260261
~~ (isLambda f' || (if with_guarded_fix then isFixApp f' else isFix f') || isBox f' || isConstructApp f'
261-
|| isPrimApp f') ->
262+
|| isPrimApp f' || isLazyApp f') ->
262263
eval a a' ->
263264
eval (tApp f a) (tApp f' a')
264265

@@ -272,13 +273,12 @@ Section Wcbv.
272273
eval_primitive eval p p' ->
273274
eval (tPrim p) (tPrim p')
274275

275-
(*
276-
| eval_lazy : eval (tLazy t) (tLazy t)
277-
| eval_force t v v' : eval t (tLazy v) ->
276+
| eval_force t v v' :
277+
eval t (tLazy v) ->
278278
eval v v' ->
279-
eval (tForce t) v' *)
279+
eval (tForce t) v'
280280

281-
(** Atoms are values (includes abstractions, cofixpoints and constructors) *)
281+
(** Atoms are values (includes abstractions, cofixpoints, constructors and lazy) *)
282282
| eval_atom t : atom Σ t -> eval t t.
283283

284284
Hint Constructors eval : core.
@@ -615,7 +615,7 @@ Section eval_rect.
615615
isBox f'
616616
||
617617
isConstructApp f'
618-
|| isPrimApp f'))
618+
|| isPrimApp f' || isLazyApp f'))
619619
(e0 : eval Σ a a'),
620620
P a a' e0
621621
→ P (tApp f16 a)
@@ -626,14 +626,17 @@ Section eval_rect.
626626

627627
(forall p p' (ev : eval_primitive (eval Σ) p p'),
628628
eval_primitive_ind _ P _ _ ev ->
629-
P (tPrim p) (tPrim p') (eval_prim _ _ _ ev))
629+
P (tPrim p) (tPrim p') (eval_prim _ _ _ ev)) ->
630+
(forall t t' v (ev1 : eval Σ t (tLazy t')) (ev2 : eval Σ t' v),
631+
P _ _ ev1 -> P _ _ ev2 ->
632+
P (tForce t) v (eval_force _ t t' v ev1 ev2))
630633

631-
→ (∀ (t : term) (i : atom Σ t),
634+
→ (∀ (t : term) (i : atom Σ t),
632635
P t t (eval_atom Σ t i))
633636
→ ∀ (t t0 : term) (e : eval Σ t t0),
634637
P t t0 e.
635638
Proof using Type.
636-
intros ?????????????????????? H.
639+
intros ??????????????????????? H.
637640
revert t t0 H.
638641
fix aux 3.
639642
move aux at top.
@@ -785,6 +788,7 @@ Section Wcbv.
785788
+ destruct with_guarded_fix.
786789
now cbn in i1. now cbn in i1.
787790
+ constructor.
791+
+ now destruct with_guarded_fix; cbn in i1.
788792
+ cbn in i. destruct with_guarded_fix; cbn in i; rtoProp; intuition auto.
789793
* destruct b0 as (ind & c & mdecl & idecl & cdecl & args & [H1 H2 H3 H4]).
790794
rewrite -[tApp _ _](mkApps_app _ (firstn n l) [a']).
@@ -1058,7 +1062,7 @@ Section Wcbv.
10581062
rewrite !mkApps_app /=.
10591063
eapply eval_app_cong; tea.
10601064
eapply IHargs => //.
1061-
rewrite isFixApp_mkApps // /= isConstructApp_mkApps // !negb_or isPrimApp_mkApps.
1065+
rewrite isFixApp_mkApps // /= isConstructApp_mkApps // !negb_or isPrimApp_mkApps isLazyApp_mkApps.
10621066
rtoProp; intuition auto.
10631067
apply nisLambda_mkApps => //.
10641068
destruct with_guarded_fix => //; eapply nisFix_mkApps => //.
@@ -1304,15 +1308,15 @@ Section Wcbv.
13041308
cbn in i. rtoProp; intuition auto.
13051309
+ exfalso. rewrite !negb_or in i. specialize (IHev1 _ ev'1); noconf IHev1.
13061310
cbn in i. rewrite guarded in i. rtoProp; intuition auto.
1307-
rewrite isFixApp_mkApps in H3 => //.
1311+
rewrite isFixApp_mkApps in H4 => //.
13081312
+ exfalso. rewrite !negb_or in i. specialize (IHev1 _ ev'1); noconf IHev1.
13091313
cbn in i. rewrite guarded in i. rtoProp; intuition auto.
1310-
rewrite isFixApp_mkApps in H3 => //.
1314+
rewrite isFixApp_mkApps in H4 => //.
13111315
+ exfalso. rewrite !negb_or in i. specialize (IHev1 _ ev'1); noconf IHev1.
13121316
cbn in i. rewrite unguarded in i. now cbn in i.
13131317
+ exfalso. rewrite !negb_or in i. specialize (IHev1 _ ev'1); noconf IHev1.
13141318
cbn in i. rtoProp; intuition auto.
1315-
now rewrite isConstructApp_mkApps in H1.
1319+
now rewrite isConstructApp_mkApps in H2.
13161320
+ specialize (IHev1 _ ev'1); noconf IHev1.
13171321
specialize (IHev2 _ ev'2); noconf IHev2.
13181322
now assert (i0 = i) as -> by now apply uip.
@@ -1329,6 +1333,7 @@ Section Wcbv.
13291333
induction ev in a, ev0 |- *; depelim ev0; eauto.
13301334
destruct a.
13311335
f_equal; eauto. specialize (e0 _ e). now noconf e0.
1336+
- depelim ev'; go.
13321337
- depelim ev'; try go.
13331338
2: now assert (i0 = i) as -> by now apply uip.
13341339
exfalso. invs i. rewrite e in H0. destruct args; cbn in H0; invs H0.

0 commit comments

Comments
 (0)