Skip to content

Commit 9e3e264

Browse files
authored
Implement tLazy and tForce in EAst (#1058)
* Add tLazy and tForce constructors With no evaluation semantics yet. Use them in ECoInductiveToInductive to allow efficient (unverified) implementation of cofixpoints in target languages. * Install archive file for static linking
1 parent f23a18c commit 9e3e264

28 files changed

+279
-154
lines changed

erasure/theories/EAst.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Inductive term : Set :=
4040
| tProj (p : projection) (c : term)
4141
| tFix (mfix : mfixpoint term) (idx : nat)
4242
| tCoFix (mfix : mfixpoint term) (idx : nat)
43-
| tPrim (prim : prim_val term).
43+
| tPrim (prim : prim_val term)
44+
| tLazy (t : term)
45+
| tForce (t : term).
4446

4547
Derive NoConfusion for term.
4648

erasure/theories/EAstUtils.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ Fixpoint string_of_term (t : term) : string :=
374374
| tFix l n => "Fix(" ^ (string_of_list (string_of_def string_of_term) l) ^ "," ^ string_of_nat n ^ ")"
375375
| tCoFix l n => "CoFix(" ^ (string_of_list (string_of_def string_of_term) l) ^ "," ^ string_of_nat n ^ ")"
376376
| tPrim p => "Prim(" ^ EPrimitive.string_of_prim string_of_term p ^ ")"
377+
| tLazy t => "Lazy(" ^ string_of_term t ^ ")"
378+
| tForce t => "Force(" ^ string_of_term t ^ ")"
377379
end.
378380

379381
(** Compute all the global environment dependencies of the term *)
@@ -409,5 +411,7 @@ Fixpoint term_global_deps (t : term) :=
409411
KernameSet.union (KernameSet.singleton (inductive_mind p.(proj_ind)))
410412
(term_global_deps c)
411413
| tPrim p => prim_global_deps term_global_deps p
414+
| tLazy t => term_global_deps t
415+
| tForce t => term_global_deps t
412416
| _ => KernameSet.empty
413417
end.

erasure/theories/ECSubst.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Fixpoint csubst t k u :=
3838
tCoFix mfix' idx
3939
| tConstruct ind n args => tConstruct ind n (map (csubst t k) args)
4040
| tPrim p => tPrim (map_prim (csubst t k) p)
41+
| tLazy u => tLazy (csubst t k u)
42+
| tForce u => tForce (csubst t k u)
4143
| x => x
4244
end.
4345

erasure/theories/ECoInductiveToInductive.v

Lines changed: 41 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,9 @@ Axiom trust_cofix : forall {A}, A.
3333
#[global]
3434
Hint Constructors eval : core.
3535

36-
Module Thunk.
37-
Definition make (t : term) : term :=
38-
tLambda (nNamed "thunk") (lift 1 0 t).
39-
40-
Definition force (t : term) :=
41-
tApp t tBox.
42-
43-
Definition make_name (x : string) (n : nat) : string :=
44-
(x ++ string_of_nat n)%bs.
45-
46-
(* Thunk an n-ary function:
47-
[t] is supposed to be of type T0 -> ... -> Tn -> C here
48-
and we want to produce an expansion:
49-
λ x0 .. xn (), t x0 xn () *)
50-
Equations make_n_aux (n : nat) (t : term) (acc : list term) : term :=
51-
make_n_aux 0 t acc => tLambda
52-
(nNamed "thunk")
53-
(mkApps (lift0 1 t) (rev (tRel 0 :: map (lift0 1) acc)));
54-
make_n_aux (S n) t acc =>
55-
tLambda
56-
(nNamed (make_name "x" (S n)))
57-
(make_n_aux n (lift0 1 t) (tRel 0 :: map (lift0 1) acc)).
58-
59-
Definition make_n (n : nat) (t : term) := make_n_aux n t [].
60-
61-
(* Eval compute in make_n 2 (tRel 0). *)
62-
63-
End Thunk.
64-
6536
Section trans.
6637
Context (Σ : GlobalContextMap.t).
6738

68-
Definition trans_cofix (d : def term) :=
69-
{| dname := d.(dname);
70-
dbody := Thunk.make_n d.(rarg) d.(dbody);
71-
rarg := d.(rarg) |}.
72-
7339
Fixpoint trans (t : term) : term :=
7440
match t with
7541
| tRel i => tRel i
@@ -81,33 +47,31 @@ Section trans.
8147
let brs' := List.map (on_snd trans) brs in
8248
match GlobalContextMap.lookup_inductive_kind Σ (fst ind).(inductive_mind) with
8349
| Some CoFinite =>
84-
tCase ind (Thunk.force (trans c)) brs'
50+
tCase ind (tForce (trans c)) brs'
8551
| _ => tCase ind (trans c) brs'
8652
end
8753
| tProj p c =>
8854
match GlobalContextMap.lookup_inductive_kind Σ p.(proj_ind).(inductive_mind) with
89-
| Some CoFinite => tProj p (Thunk.force (trans c))
55+
| Some CoFinite => tProj p (tForce (trans c))
9056
| _ => tProj p (trans c)
9157
end
9258
| tFix mfix idx =>
9359
let mfix' := List.map (map_def trans) mfix in
9460
tFix mfix' idx
9561
| tCoFix mfix idx =>
9662
let mfix' := List.map (map_def trans) mfix in
97-
let mfix' := List.map trans_cofix mfix' in
98-
match nth_error mfix' idx with
99-
| Some d => Thunk.make_n d.(rarg) (tFix mfix' idx)
100-
| None => tCoFix mfix' idx
101-
end
63+
tFix mfix' idx
10264
| tBox => t
10365
| tVar _ => t
10466
| tConst _ => t
10567
| tConstruct ind i args =>
10668
match GlobalContextMap.lookup_inductive_kind Σ ind.(inductive_mind) with
107-
| Some CoFinite => Thunk.make (tConstruct ind i (map trans args))
69+
| Some CoFinite => tLazy (tConstruct ind i (map trans args))
10870
| _ => tConstruct ind i (map trans args)
10971
end
11072
| tPrim p => tPrim (map_prim trans p)
73+
| tLazy t => tLazy (trans t)
74+
| tForce t => tForce (trans t)
11175
end.
11276

11377
(* cofix succ x := match x with Stream x xs => Stream (x + 1) (succ xs) ->
@@ -160,14 +124,9 @@ Section trans.
160124
unfold test_def in *;
161125
simpl closed in *; try solve [simpl subst; simpl closed; f_equal; auto; rtoProp; solve_all]; try easy.
162126
- destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //; solve_all.
163-
rewrite -Nat.add_1_r. now eapply closedn_lift.
164127
- move/andP: H => [] clt clargs.
165128
destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //; rtoProp; solve_all; solve_all.
166129
- destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //; rtoProp; solve_all; solve_all.
167-
- solve_all. destruct nth_error eqn:hnth.
168-
* apply trust_cofix.
169-
* cbn. unfold trans_cofix. len. solve_all.
170-
unfold test_def. cbn. apply trust_cofix.
171130
- primProp. solve_all_k 6.
172131
Qed.
173132

@@ -219,25 +178,13 @@ Section trans.
219178
- destruct (k ?= n)%nat; auto.
220179
- destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //.
221180
1,3,4:f_equal; rewrite map_map_compose; solve_all.
222-
unfold Thunk.make. f_equal. cbn.
223-
rewrite !map_map_compose. f_equal; solve_all.
224-
specialize (H k cl). rewrite H.
225-
rewrite closed_subst. now apply closed_trans.
226-
rewrite closed_subst. now apply closed_trans.
227-
now rewrite commut_lift_subst_rec.
181+
do 2 f_equal; solve_all.
228182
- destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //.
229183
all:f_equal; eauto; try (rewrite /on_snd map_map_compose; solve_all).
230-
unfold Thunk.force. f_equal; eauto.
184+
f_equal. eauto.
231185
- destruct GlobalContextMap.lookup_inductive_kind as [[]|] => /= //.
232186
all:f_equal; eauto; try (rewrite /on_snd map_map_compose; solve_all).
233-
unfold Thunk.force. f_equal; eauto.
234-
- f_equal. solve_all.
235-
rewrite !nth_error_map. destruct nth_error eqn:hnth => //=.
236-
2:{ f_equal. rewrite map_map_compose. eapply map_ext_in => x hin.
237-
rewrite /trans_cofix /map_def //=. f_equal. len.
238-
rewrite /Thunk.make_n. apply trust_cofix.
239-
}
240-
apply trust_cofix.
187+
f_equal; eauto.
241188
Qed.
242189

243190
Lemma trans_substl s t :
@@ -284,19 +231,19 @@ Section trans.
284231
discriminate.
285232
Qed.
286233

287-
Lemma trans_cunfold_cofix mfix idx n f :
234+
(* Lemma trans_cunfold_cofix mfix idx n f :
288235
forallb (closedn 0) (EGlobalEnv.cofix_subst mfix) ->
289236
cunfold_cofix mfix idx = Some (n, f) ->
290237
exists d, nth_error mfix idx = Some d /\
291-
cunfold_fix (map trans_cofix mfix) idx = Some (n, substl (fix_subst (map trans_cofix mfix)) (Thunk.make_n (rarg d) (dbody d))).
238+
cunfold_fix mfix idx = Some (n, substl (fix_subst mfix) (dbody d)).
292239
Proof using Type.
293240
intros hcofix.
294241
unfold cunfold_cofix, cunfold_fix.
295242
rewrite nth_error_map.
296243
destruct nth_error.
297244
intros [= <- <-] => /=. f_equal. exists d. split => //.
298245
discriminate.
299-
Qed.
246+
Qed. *)
300247

301248
Lemma trans_nth {n l d} :
302249
trans (nth n l d) = nth n (map trans l) (trans d).
@@ -450,7 +397,6 @@ Proof.
450397
unfold lookup_inductive in hl.
451398
destruct lookup_minductive => //.
452399
rewrite (IHt _ H2 _ H0 H1) //.
453-
- apply trust_cofix.
454400
Qed.
455401

456402
Lemma wellformed_trans_decl_extends {wfl: EEnvFlags} {Σ : GlobalContextMap.t} t :
@@ -611,11 +557,6 @@ Proof.
611557
exists mdecl, idecl; split => //.
612558
Qed.
613559

614-
Lemma isLambda_make_n n t : isLambda (Thunk.make_n n t).
615-
Proof.
616-
induction n; cbn => //.
617-
Qed.
618-
619560
Lemma value_trans {efl : EEnvFlags} {fl : WcbvFlags} {hasc : cstr_as_blocks = true} {wcon : with_constructor_as_block = true} {Σ : GlobalContextMap.t} {c} :
620561
has_tApp -> wf_glob Σ ->
621562
wellformed Σ 0 c ->
@@ -628,11 +569,6 @@ Proof.
628569
all:try solve [intros; repeat constructor => //].
629570
destruct args => //.
630571
move=> /andP[] wc. now rewrite wcon in wc.
631-
move=> _ /andP [] hascof /andP[] /Nat.ltb_lt /nth_error_Some hnth hm.
632-
destruct nth_error => //.
633-
pose proof (isLambda_make_n (rarg d) (tFix (map trans_cofix mfix) idx)).
634-
destruct Thunk.make_n => //. apply trust_cofix.
635-
(* do 3 constructor. *)
636572
- intros p pv IH wf. cbn. constructor. constructor 2.
637573
cbn in wf. move/andP: wf => [hasp tp].
638574
primProp. depelim tp; depelim pv; depelim IH; constructor; cbn in *; rtoProp; intuition auto; solve_all.
@@ -641,7 +577,8 @@ Proof.
641577
cbn -[GlobalContextMap.lookup_inductive_kind].
642578
destruct GlobalContextMap.lookup_inductive_kind as [[]|] eqn:hl' => //.
643579
1,3,4:eapply value_constructor; tea; [erewrite <-lookup_constructor_trans; tea|now len|solve_all].
644-
now do 2 constructor.
580+
apply trust_cofix.
581+
(* now do 2 constructor. *)
645582
- intros f args vh harglen hargs ihargs.
646583
rewrite wellformed_mkApps // => /andP[] wff wfargs.
647584
rewrite trans_mkApps.
@@ -669,6 +606,7 @@ Ltac destruct_times :=
669606
From MetaCoq.Erasure Require Import EWcbvEvalCstrsAsBlocksInd.
670607
Lemma trans_correct {efl : EEnvFlags} {fl} {wcon : with_constructor_as_block = true}
671608
{wcb : cstr_as_blocks = true}
609+
{wpc : with_prop_case = false} (* Avoid tLazy tBox values *)
672610
{Σ : GlobalContextMap.t} t v :
673611
has_tApp ->
674612
wf_glob Σ ->
@@ -712,7 +650,8 @@ Proof.
712650
1,3,4: eauto.
713651
+ now rewrite -is_propositional_cstr_trans.
714652
+ rewrite nth_error_map H2 //.
715-
+ eapply eval_beta. eapply e0; eauto.
653+
+ eapply trust_cofix.
654+
(* eapply eval_beta. eapply e0; eauto.
716655
constructor; eauto.
717656
rewrite closed_subst // simpl_subst_k //.
718657
eapply EWcbvEval.eval_to_value in H.
@@ -724,27 +663,19 @@ Proof.
724663
instantiate (1 := map (trans Σ) args).
725664
eapply All2_All2_Set.
726665
eapply values_final. solve_all.
727-
unshelve eapply value_trans; tea.
666+
unshelve eapply value_trans; tea.*)
728667
+ now len.
729668
+ now len.
730-
+ exact e.
669+
+ apply trust_cofix.
731670

732-
- subst brs.
733-
cbn -[lookup_constructor lookup_constructor_pars_args
734-
GlobalContextMap.lookup_inductive_kind] in e0 |- *.
735-
rewrite GlobalContextMap.lookup_inductive_kind_spec.
736-
rewrite trans_substl ?map_repeat /= in e.
737-
{ now apply forallb_repeat. }
738-
destruct lookup_inductive_kind as [[]|] eqn:hl => //.
739-
1,3,4:eapply eval_iota_sing; [eauto|eauto|
740-
now rewrite -is_propositional_trans|reflexivity|
741-
rewrite /= ?trans_substl //; simpl; eauto].
742-
eapply eval_iota_sing; eauto.
743-
eapply eval_box; eauto.
744-
rewrite -is_propositional_trans //.
745-
reflexivity.
671+
- now rewrite H in wpc.
746672

747673
- apply trust_cofix.
674+
(*rewrite trans_mkApps /= in e1.
675+
cbn; eapply eval_fix => //; tea.
676+
len. apply trust_cofix*)
677+
678+
748679
- apply trust_cofix.
749680
- apply trust_cofix.
750681
- apply trust_cofix.
@@ -910,13 +841,22 @@ Proof.
910841
destruct lookup_inductive_kind as [[]|] => /= //.
911842
2-3:constructor; eauto; solve_all.
912843
constructor; eauto; solve_all. cbn.
913-
unfold Thunk.force.
914-
eapply isEtaExp_expanded.
915-
all:apply trust_cofix.
916-
- apply trust_cofix.
917-
- apply trust_cofix.
918-
- apply trust_cofix.
919-
- apply trust_cofix.
844+
now constructor.
845+
constructor; eauto; solve_all.
846+
- cbn -[GlobalContextMap.lookup_inductive_kind].
847+
rewrite GlobalContextMap.lookup_inductive_kind_spec.
848+
destruct lookup_inductive_kind as [[]|] => /= //.
849+
2-3:constructor; eauto; solve_all.
850+
constructor; eauto; solve_all. cbn.
851+
now constructor.
852+
constructor; eauto; solve_all.
853+
- cbn. econstructor; eauto. solve_all. now eapply isLambda_trans.
854+
- cbn. econstructor; eauto; solve_all. apply trust_cofix.
855+
- cbn -[GlobalContextMap.lookup_inductive_kind].
856+
rewrite GlobalContextMap.lookup_inductive_kind_spec.
857+
destruct lookup_inductive_kind as [[]|] => /= //.
858+
1,3,4:eapply expanded_tConstruct_app; eauto; solve_all.
859+
apply trust_cofix. (* Needs constructor_as_blocks = true invariant *)
920860
Qed.
921861
(*cbn.
922862
eapply isEtaExp_substl. eapply forallb_repeat => //.

erasure/theories/EConstructorsAsBlocks.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ Section transform_blocks.
6262
| tVar n => EAst.tVar n
6363
| tConst n => EAst.tConst n
6464
| tConstruct ind i block_args => EAst.tConstruct ind i []
65-
| tPrim p => EAst.tPrim (map_primIn p (fun x H => transform_blocks x)) }.
65+
| tPrim p => EAst.tPrim (map_primIn p (fun x H => transform_blocks x))
66+
| tLazy t => EAst.tLazy (transform_blocks t)
67+
| tForce t => EAst.tForce (transform_blocks t) }.
6668
Proof.
6769
all:try lia.
6870
all:try apply (In_size); tea.

erasure/theories/EDeps.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Proof.
8686
now apply e.
8787
- depelim X; depelim er; constructor; cbn. solve_all.
8888
destruct p. solve_all.
89+
- depelim er.
90+
- depelim er.
8991
Qed.
9092

9193
Lemma erases_deps_subst Σ Σ' s k t :
@@ -137,6 +139,8 @@ Proof.
137139
constructor; [|easy].
138140
now apply e.
139141
- depelim X; depelim er; constructor; cbn; intuition auto; solve_all.
142+
- depelim er.
143+
- depelim er.
140144
Qed.
141145

142146
Lemma erases_deps_subst1 Σ Σ' t k u :
@@ -195,6 +199,8 @@ Proof.
195199
constructor; [|easy].
196200
now apply e.
197201
- depelim X; depelim er; constructor; cbn; intuition auto; solve_all.
202+
- depelim er.
203+
- depelim er.
198204
Qed.
199205

200206
Lemma erases_deps_substl Σ Σ' s t :

0 commit comments

Comments
 (0)