Skip to content

Commit b41a14c

Browse files
committed
fix collision by generating a fresh name
1 parent 26c180b commit b41a14c

File tree

22 files changed

+138
-110
lines changed

22 files changed

+138
-110
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- New `coq.env.term-dependencies` computing all the `grefs` occurring in a term.
1313
- New `coq.redflag` and `coq.redflags` types for `@redflags!` option understood
1414
by `coq.reduction.lazy.*` `and coq.reduction.cbv.norm`
15+
- New `coq.env.fresh-global-id`
1516

1617
### APPS
1718
- Change `derive` usage.

apps/derive/elpi/bcongr.elpi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ main-constructor Lno Prefix K Kt Clause :- do! [
110110
% we build the comparison function
111111
bo-param Lno Kn Kt R,
112112
std.assert-ok! (coq.typecheck R RT) "derive.bcongr generates illtyped term",
113-
Name is Prefix ^ {coq.gref->id (indc K)},
114-
coq.env.add-const Name R RT @opaque! Cong,
113+
Name is Prefix ^ "bcongr_" ^ {coq.gref->id (indc K)},
114+
coq.ensure-fresh-global-id Name FName,
115+
coq.env.add-const FName R RT @opaque! Cong,
115116

116117
% we register it as a clause
117118
Clause = (bcongr-db K (global (const Cong)) :- !),

apps/derive/elpi/derive.elpi

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,25 @@ pred indt-or-const i:gref.
8989
indt-or-const (indt _).
9090
indt-or-const (const _).
9191

92-
pred main i:gref, i:string, o:list prop.
93-
main T Prefix CL :- get-option "recursive" tt, !,
92+
pred main i:gref, i:bool, o:list prop.
93+
main T P CL :- get-option "recursive" tt, !,
9494
coq.env.dependencies T _ AllDeps,
9595
coq.gref.set.elements AllDeps AllDepsL,
9696
std.filter AllDepsL indt-or-const Deps,
97-
main.aux Deps Prefix [] CL1,
98-
CL1 => main1 T Prefix CL2,
97+
main.aux Deps [] CL1,
98+
CL1 => main1 T P CL2,
9999
std.append CL1 CL2 CL.
100-
pred main.aux i:list gref, i:string, i:list prop, o:list prop.
101-
main.aux [] _ X X.
102-
main.aux [T|TS] Prefix Acc CL :-
103-
(pi X\get-option "only" X :- !, fail) => Acc => main T Prefix CL1,
104-
main.aux TS Prefix {std.append CL1 Acc} CL.
100+
pred main.aux i:list gref, i:list prop, o:list prop.
101+
main.aux [] X X.
102+
main.aux [T|TS] Acc CL :-
103+
/* (pi X\get-option "only" X :- !, fail) => */ Acc => main T tt CL1,
104+
main.aux TS {std.append CL1 Acc} CL.
105105

106-
main T Prefix CL :- main1 T Prefix CL.
106+
main T P CL :- main1 T P CL.
107107

108-
pred main1 i:gref, i:string, o:list prop.
109-
main1 T Prefix CL :-
108+
pred main1 i:gref, i:bool, o:list prop.
109+
main1 T P CL :-
110+
if (P = tt) (Prefix is {coq.gref->id T} ^ "_") (Prefix = ""),
110111
std.findall (derivation T Prefix _) L,
111112
if (L = [])
112113
(coq.error "no derivation found, did you Import derive.std?")
@@ -125,7 +126,7 @@ decl+main DS :- std.do! [
125126
if-verbose (coq.say "Declaring inductive" D),
126127
coq.env.add-indt D I,
127128
if-verbose (coq.say "Deriving"),
128-
main (indt I) "" CL,
129+
main (indt I) ff CL,
129130
if-verbose (coq.say "Done"),
130131
coq.env.end-module _,
131132

apps/derive/elpi/eqb.elpi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ derive.eqb.main (indt I) Prefix CL :- std.do! [
1212
derive.eqb.eqbf.main FI FI [] [] R,
1313
std.assert-ok! (coq.typecheck R Rty) "derive.eqbf generates illtyped term",
1414
Name is Prefix ^ "eqb_fields",
15-
coq.env.add-const Name R Rty ff C,
15+
coq.ensure-fresh-global-id Name FName,
16+
coq.env.add-const FName R Rty ff C,
1617
EQBF = global (const C),
1718

1819
derive.eqb.eqb.main FI FI [] [] EQBF R1Skel,
1920
std.assert-ok! (coq.elaborate-skeleton R1Skel R1ty R1) "derive.eqb generates illtyped term", % need elaborate for prim record
2021
Name1 is Prefix ^ "eqb",
21-
coq.env.add-const Name1 R1 R1ty ff C1,
22+
coq.ensure-fresh-global-id Name1 FName1,
23+
coq.env.add-const FName1 R1 R1ty ff C1,
2224
EQB = global (const C1),
2325

2426
% populate dbs
@@ -31,8 +33,9 @@ derive.eqb.main (const C) Prefix CL :- std.do! [
3133
coq.env.const C (some T) _,
3234
std.assert! (eqb-for T T EQB) "cannot derive eqb",
3335
Name is Prefix ^ "eqb",
36+
coq.ensure-fresh-global-id Name FName,
3437
X = global (const C),
35-
coq.env.add-const Name EQB {{ lp:X -> lp:X -> bool }} @transparent! EQBC,
38+
coq.env.add-const FName EQB {{ lp:X -> lp:X -> bool }} @transparent! EQBC,
3639
CL = [eqb-done (const C), eqb-for (global (const C)) (global (const C)) (global (const EQBC))],
3740
std.forall CL (x\ coq.elpi.accumulate _ "derive.eqb.db" (clause _ (before "eqb-for:whd") x)),
3841
].

apps/derive/elpi/eqbcorrect.elpi

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ main (indt I) Prefix CLs :- std.do! [
3333
%std.assert! (ground_term R) "ww",
3434
std.assert-ok! (coq.typecheck R Ty) "derive.eqbcorrect: common/correct generates ill typed term",
3535
Name is Prefix ^ "eqb_correct",
36-
coq.env.add-const Name R Ty @opaque! Correct,
36+
coq.ensure-fresh-global-id Name FName,
37+
coq.env.add-const FName R Ty @opaque! Correct,
3738

3839
if (has-params? FI) (
3940

@@ -42,7 +43,8 @@ main (indt I) Prefix CLs :- std.do! [
4243

4344
std.assert-ok! (coq.typecheck Rx Tyx) "derive.eqbcorrect: common-aux/corect generates ill typed term",
4445
Namex is Prefix ^ "eqb_correct_aux",
45-
coq.env.add-const Namex Rx Tyx @opaque! Correctx,
46+
coq.ensure-fresh-global-id Namex FNamex,
47+
coq.env.add-const FNamex Rx Tyx @opaque! Correctx,
4648
CL_CORRECT = [correct-lemma-for (global (indt I)) (global (const Correctx))]
4749

4850
) (CL_CORRECT = [correct-lemma-for (global (indt I)) (global (const Correct))]),
@@ -54,7 +56,8 @@ main (indt I) Prefix CLs :- std.do! [
5456

5557
std.assert-ok! (coq.typecheck Rr Tyr) "derive.eqbcorrect: common/refl generates ill typed term",
5658
Namer is Prefix ^ "eqb_refl",
57-
coq.env.add-const Namer Rr Tyr @opaque! Refl,
59+
coq.ensure-fresh-global-id Namer FNamer,
60+
coq.env.add-const FNamer Rr Tyr @opaque! Refl,
5861

5962
if (has-params? FI) (
6063

@@ -63,7 +66,8 @@ main (indt I) Prefix CLs :- std.do! [
6366

6467
std.assert-ok! (coq.typecheck Rrx Tyrx) "derive.eqbcorrect: common-aux/refl generates ill typed term",
6568
Namerx is Prefix ^ "eqb_refl_aux",
66-
coq.env.add-const Namerx Rrx Tyrx @opaque! Reflx,
69+
coq.ensure-fresh-global-id Namerx FNamerx,
70+
coq.env.add-const FNamerx Rrx Tyrx @opaque! Reflx,
6771
CL_REFL = [refl-lemma-for (global (indt I)) (global (const Reflx))]
6872

6973
) (CL_REFL = [refl-lemma-for (global (indt I)) (global (const Refl))]),
@@ -83,8 +87,10 @@ main (const C) Prefix [Clause] :- std.do! [
8387
NameR is Prefix ^ "eqb_refl",
8488
NameC is Prefix ^ "eqb_correct",
8589
X = global (const C),
86-
coq.env.add-const NameC Correct {{ @eqb_correct lp:X lp:F }} @transparent! CC,
87-
coq.env.add-const NameR Refl {{ @eqb_reflexive lp:X lp:F }} @transparent! CR,
90+
coq.ensure-fresh-global-id NameR FNameR,
91+
coq.ensure-fresh-global-id NameC FNameC,
92+
coq.env.add-const FNameC Correct {{ @eqb_correct lp:X lp:F }} @transparent! CC,
93+
coq.env.add-const FNameR Refl {{ @eqb_reflexive lp:X lp:F }} @transparent! CR,
8894
Clause = (eqcorrect-for (const C) CC CR),
8995
coq.elpi.accumulate _ "derive.eqbcorrect.db" (clause _ _ Clause),
9096
].

apps/derive/elpi/fields.elpi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,27 @@ main I Prefix AllCL :- std.do! [
2929
CLB => fields_t.main FI (global (indt I)) Body_t,
3030
std.assert-ok! (coq.typecheck Body_t Ty_t) "derive.fields generates illtyped fields_t",
3131
Name_t is Prefix ^ "fields_t",
32-
coq.env.add-const Name_t Body_t Ty_t ff Fields_t,
32+
coq.ensure-fresh-global-id Name_t FName_t,
33+
coq.env.add-const FName_t Body_t Ty_t ff Fields_t,
3334

3435
CLB => fields.main FI (global (indt I)) (global (const Fields_t)) (global (const Tag)) BodySkel,
3536
% we elaborate only for primitive records...
3637
std.assert-ok! (coq.elaborate-skeleton BodySkel Ty Body) "derive.fields generates illtyped fields",
3738
Name is Prefix ^ "fields",
38-
coq.env.add-const Name Body Ty ff Fields,
39+
coq.ensure-fresh-global-id Name FName,
40+
coq.env.add-const FName Body Ty ff Fields,
3941

4042
CLB => construct.main FI (global (indt I)) Fields_t Body_c,
4143
std.assert-ok! (coq.typecheck Body_c Ty_c) "derive.fields generates illtyped construct",
4244
Name_c is Prefix ^ "construct",
43-
coq.env.add-const Name_c Body_c Ty_c ff Construct,
45+
coq.ensure-fresh-global-id Name_c FName_c,
46+
coq.env.add-const FName_c Body_c Ty_c ff Construct,
4447

4548
coq.bind-ind-arity (global (indt I)) Arity (case-refl Tag Fields Construct) Body_PSkel,
4649
std.assert-ok! (coq.elaborate-skeleton Body_PSkel Ty_P Body_P) "derive.fields generates illtyped constructP",
4750
Name_P is Prefix ^ "constructP",
48-
coq.env.add-const Name_P Body_P Ty_P @opaque! ConstructP,
51+
coq.ensure-fresh-global-id Name_P FName_P,
52+
coq.env.add-const FName_P Body_P Ty_P @opaque! ConstructP,
4953

5054
AllCL = [fields-for I Fields_t Fields Construct ConstructP|CLB],
5155
std.forall AllCL (x\ coq.elpi.accumulate _ "derive.fields.db" (clause _ _ x)),

apps/derive/elpi/induction.elpi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ params 0 Ity K KT Arity (fun `P` Pty p\ Bo p) :-
117117
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118118

119119
pred main i:inductive, i:string, o:list prop.
120-
main GR Name [Clause] :- do! [
120+
main GR Prefix [Clause] :- do! [
121121
T = global (indt GR),
122122

123123
if (coq.env.informative? GR) (Informative = [informative]) (Informative = []),
@@ -134,7 +134,9 @@ main GR Name [Clause] :- do! [
134134
% coq.say {coq.term->string R},
135135
std.assert-ok! (coq.typecheck R RT) "derive.induction generates illtyped term",
136136

137-
coq.env.add-const Name R RT _ I,
137+
Name is Prefix ^ "induction",
138+
coq.ensure-fresh-global-id Name FName,
139+
coq.env.add-const FName R RT _ I,
138140

139141
% we register it as a clause
140142
Clause = (induction-db GR (global (const I)) :- !),

apps/derive/elpi/map.elpi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,17 @@ mk-clause N Lno Ity1 Ity2 X Todo Map C :- whd1 X X1, !,
170170

171171
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172172
pred main i:inductive, i:string, o:list prop.
173-
main GR Name C :- do! [
173+
main GR Prefix C :- do! [
174174
T = global (indt GR),
175175
coq.env.indt GR _Ind Lno Luno Arity _ _,
176176
assert! (Lno = Luno) "derive.map: Non-uniform parameters not supported",
177177

178178
% generate map and add to the env
179179
bo-params 0 Lno T T Arity Arity [] RSkel,
180180
std.assert-ok! (coq.elaborate-skeleton RSkel Rty R) "derive.map generates illtyped term",
181-
coq.env.add-const Name R Rty @transparent! Funct,
181+
Name is Prefix ^ "map",
182+
coq.ensure-fresh-global-id Name FName,
183+
coq.env.add-const FName R Rty @transparent! Funct,
182184

183185
% generate clause and add to the db
184186
mk-clause 0 Lno T T Arity [] (global (const Funct)) Clause,

apps/derive/elpi/tag.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ main I Prefix CL :- do! [
2323
std.assert-ok! (elaborate-skeleton BodyR Ty Body) "derive.tag generates illtyped code",
2424

2525
% save constant
26-
Name is Prefix ^ "tag",
26+
coq.ensure-fresh-global-id (Prefix ^ "tag") Name,
2727
coq.env.add-const Name Body Ty ff C,
2828

2929
% store in the DB the tag function, so that other Elpi commands can find it

apps/derive/examples/usage.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Check rtree.induction : (* this is the key *)
5353
#[verbose, only(lens_laws, eqb)] derive
5454
Record Box A := { contents : A; tag : nat }.
5555

56-
Check Box.eq :
56+
Check Box.eqb :
5757
forall A, (A -> A -> bool) -> Box A -> Box A -> bool.
5858

5959
Import lens.
@@ -76,15 +76,16 @@ Check Box._tag_contents_exchange : (* another one *)
7676

7777
derive nat.
7878

79-
Check nat_eq_OK :
80-
forall x y, reflect (x = y) (nat_eq x y).
79+
Check nat_eqb_OK :
80+
forall x y, reflect (x = y) (nat_eqb x y).
8181

8282
(** Once can also run derive recursively, but this has the same bad effect,
8383
all generated concepts will be out of place *)
8484

8585
Inductive a := A.
86-
Inductive b := B : A -> B.
86+
Inductive b := B : a -> b.
8787

88-
#[recursive] derive b.
88+
#[recursive, only(eqbOK)] derive b.
8989

90-
Check b.eqb.
90+
Check a_eqb.
91+
Check b_eqb.

0 commit comments

Comments
 (0)