Skip to content

Commit 4eba947

Browse files
authored
Merge pull request #121 from SkySkimmer/indirect
Adapt to rocq-prover/rocq#18422 (indirect accessor handled through vernactypes)
2 parents 5b5ac50 + faa9d3c commit 4eba947

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

src/abstraction.mlg

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ VERNAC COMMAND EXTEND ParametricityDefined CLASSIFIED AS SIDEFF STATE program
4848
}
4949
END
5050

51-
VERNAC COMMAND EXTEND AbstractionReference CLASSIFIED AS SIDEFF
51+
VERNAC COMMAND EXTEND AbstractionReference CLASSIFIED AS SIDEFF STATE opaque_access
5252
| [ "Parametricity" ref(c) ] ->
5353
{
5454
command_reference default_arity (intern_reference_to_name c) None
@@ -79,7 +79,7 @@ VERNAC COMMAND EXTEND AbstractionReference CLASSIFIED AS SIDEFF
7979
}
8080
END
8181

82-
VERNAC COMMAND EXTEND AbstractionRecursive CLASSIFIED AS SIDEFF
82+
VERNAC COMMAND EXTEND AbstractionRecursive CLASSIFIED AS SIDEFF STATE opaque_access
8383
| [ "Parametricity" "Recursive" reference(c) ] ->
8484
{
8585
command_reference_recursive default_arity (intern_reference_to_name c)
@@ -98,7 +98,7 @@ VERNAC COMMAND EXTEND AbstractionRecursive CLASSIFIED AS SIDEFF
9898
}
9999
END
100100

101-
VERNAC COMMAND EXTEND Abstraction CLASSIFIED AS SIDEFF
101+
VERNAC COMMAND EXTEND Abstraction CLASSIFIED AS SIDEFF STATE opaque_access
102102
| [ "Parametricity" "Translation" constr(c) "as" ident(name)] ->
103103
{
104104
translate_command default_arity c name
@@ -113,30 +113,30 @@ VERNAC COMMAND EXTEND Abstraction CLASSIFIED AS SIDEFF
113113
}
114114
END
115115

116-
VERNAC COMMAND EXTEND TranslateModule CLASSIFIED AS SIDEFF
116+
VERNAC COMMAND EXTEND TranslateModule CLASSIFIED AS SIDEFF STATE opaque_access
117117
| [ "Parametricity" "Module" global(qid) ] ->
118118
{
119-
ignore (translate_module_command Parametricity.default_arity qid)
119+
translate_module_command Parametricity.default_arity qid
120120
}
121121
| [ "Parametricity" "Module" global(qid) "as" ident(name) ] ->
122122
{
123-
ignore (translate_module_command ~name Parametricity.default_arity qid)
123+
translate_module_command ~name Parametricity.default_arity qid
124124
}
125125
| [ "Parametricity" "Module" global(qid) "arity" integer(arity) ] ->
126126
{
127-
ignore (translate_module_command arity qid)
127+
translate_module_command arity qid
128128
}
129129
| [ "Parametricity" "Module" global(qid) "as" ident(name) "arity" integer(arity) ] ->
130130
{
131-
ignore (translate_module_command ~name arity qid)
131+
translate_module_command ~name arity qid
132132
}
133133
| [ "Parametricity" "Module" global(qid) "arity" integer(arity) "as" ident(name)] ->
134134
{
135-
ignore (translate_module_command ~name arity qid)
135+
translate_module_command ~name arity qid
136136
}
137137
END
138138

139-
VERNAC COMMAND EXTEND Realizer CLASSIFIED AS SIDEFF
139+
VERNAC COMMAND EXTEND Realizer CLASSIFIED AS SIDEFF STATE opaque_access
140140
| [ "Realizer" constr(c) "as" ident(name) ":=" constr(t) ] ->
141141
{
142142
realizer_command Parametricity.default_arity (Some name) c t

src/declare_translation.ml

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,19 @@ let add_definition ~opaque ~hook ~poly ~scope ~kind ~tactic name env evd term ty
7070
Some lemma
7171
end
7272

73-
let declare_abstraction ?(opaque = false) ?(continuation = default_continuation) ~poly ~scope ~kind arity evdr env a name =
73+
let declare_abstraction ~opaque_access ?(opaque = false) ?(continuation = default_continuation) ~poly ~scope ~kind arity evdr env a name =
7474
Debug.debug_evar_map Debug.all "declare_abstraction, evd = " env !evdr;
7575
debug [`Abstraction] "declare_abstraction, a =" env !evdr a;
7676
let b = Retyping.get_type_of env !evdr a in
7777
debug [`Abstraction] "declare_abstraction, b =" env !evdr b;
7878
let b = Retyping.get_type_of env !evdr a in
79-
let b_R = relation arity evdr env b in
79+
let module P = WithOpaqueAccess(struct let access = opaque_access end) in
80+
let b_R = P.relation arity evdr env b in
8081
let sub = range (fun k -> prime !evdr arity k a) arity in
8182
let b_R = EConstr.Vars.substl sub b_R in
8283
let a_R = fun evd ->
8384
let evdr = ref evd in
84-
let a_R = translate arity evdr env a in
85+
let a_R = P.translate arity evdr env a in
8586
debug [`Abstraction] "a_R = " env !evdr a_R;
8687
debug_evar_map Debug.all "abstraction, evar_map = " env !evdr;
8788
!evdr, a_R
@@ -104,10 +105,11 @@ let declare_abstraction ?(opaque = false) ?(continuation = default_continuation)
104105
let tactic = Relations.get_parametricity_tactic () in
105106
add_definition ~tactic ~opaque ~poly ~scope ~kind ~hook name env evd a_R b_R
106107

107-
let declare_inductive name ?(continuation = default_continuation) arity evd env (((mut_ind, _) as ind, inst)) =
108+
let declare_inductive ~opaque_access name ?(continuation = default_continuation) arity evd env (((mut_ind, _) as ind, inst)) =
108109
let mut_body, _ = Inductive.lookup_mind_specif env ind in
109110
debug_string [`Inductive] "Translating mind body ...";
110-
let translation_entry = Parametricity.translate_mind_body name arity evd env mut_ind mut_body inst in
111+
let module P = Parametricity.WithOpaqueAccess(struct let access = opaque_access end) in
112+
let translation_entry = P.translate_mind_body name arity evd env mut_ind mut_body inst in
111113
debug_string [`Inductive] ("Translating mind body ... done.");
112114
debug_evar_map [`Inductive] "evar_map inductive " env !evd;
113115
let size = Declarations.(Array.length mut_body.mind_packets) in
@@ -135,14 +137,15 @@ let translate_inductive_command arity c name =
135137
let evd = ref sigma in
136138
declare_inductive name arity evd env pind
137139

138-
let declare_realizer ?(continuation = default_continuation) ?kind ?real arity evd env name (var : constr) =
140+
let declare_realizer ~opaque_access ?(continuation = default_continuation) ?kind ?real arity evd env name (var : constr) =
139141
let gref = (match EConstr.kind !evd var with
140142
| Var id -> Names.GlobRef.VarRef id
141143
| Const (cst, _) -> Names.GlobRef.ConstRef cst
142144
| _ -> error (Pp.str "Realizer works only for variables and constants.")) in
143145
let evd', typ = Typing.type_of env !evd var in
144146
evd := evd';
145-
let typ_R = Parametricity.relation arity evd env typ in
147+
let module P = Parametricity.WithOpaqueAccess(struct let access = opaque_access end) in
148+
let typ_R = P.relation arity evd env typ in
146149
let sub = range (fun _ -> var) arity in
147150
let typ_R = Vars.substl sub typ_R in
148151
let cpt = ref 0 in
@@ -182,24 +185,24 @@ let declare_realizer ?(continuation = default_continuation) ?kind ?real arity ev
182185
let tactic = Relations.get_parametricity_tactic () in
183186
add_definition ~tactic ~opaque:false ~poly ~scope ~kind ~hook name env sigma real typ_R
184187

185-
let realizer_command arity name var real =
188+
let realizer_command ~opaque_access arity name var real =
186189
let env = Global.env () in
187190
let sigma = Evd.from_env env in
188191
let (sigma, var) = Constrintern.interp_open_constr env sigma var in
189192
RetrieveObl.check_evars env sigma;
190193
let real = fun sigma -> Constrintern.interp_open_constr env sigma real in
191-
ignore(declare_realizer arity (ref sigma) env name var ~real)
194+
ignore(declare_realizer ~opaque_access arity (ref sigma) env name var ~real)
192195

193196
let rec list_continuation final f l _ = match l with [] -> final ()
194197
| hd::tl -> f (list_continuation final f tl) hd
195198

196-
let rec translate_module_command ?name arity r =
199+
let rec translate_module_command ~opaque_access ?name arity r =
197200
check_nothing_ongoing ();
198201
let qid = r in
199202
let mb = try Global.lookup_module (Nametab.locate_module qid)
200203
with Not_found -> error Pp.(str "Unknown Module " ++ pr_qualid qid)
201204
in
202-
declare_module ?name arity mb
205+
declare_module ~opaque_access ?name arity mb
203206

204207
and id_of_module_path mp =
205208
let open Names in
@@ -209,7 +212,7 @@ and id_of_module_path mp =
209212
| MPfile dp -> List.hd (DirPath.repr dp)
210213
| MPbound id -> MBId.to_id id
211214

212-
and declare_module ?(continuation = ignore) ?name arity mb =
215+
and declare_module ~opaque_access ?(continuation = ignore) ?name arity mb =
213216
debug_string [`Module] "--> declare_module";
214217
let open Declarations in
215218
let mp = mb.mod_mp in
@@ -242,7 +245,7 @@ and declare_module ?(continuation = ignore) ?name arity mb =
242245
Evd.(with_sort_context_set univ_rigid evd (UnivGen.fresh_constant_instance env cst))
243246
in
244247
let evdr = ref evd in
245-
ignore(declare_realizer ~continuation arity evdr env None (mkConstU (fst ucst, EInstance.make (snd ucst))))
248+
ignore(declare_realizer ~opaque_access ~continuation arity evdr env None (mkConstU (fst ucst, EInstance.make (snd ucst))))
246249

247250
| (lab, SFBconst cb) ->
248251
let opaque =
@@ -270,7 +273,7 @@ and declare_module ?(continuation = ignore) ?name arity mb =
270273
debug [`Module] "type :" env !evdr typ
271274
with e -> error (Pp.str (Printexc.to_string e)));
272275
debug_string [`Module] (Printf.sprintf "constant field: '%s'." (Names.Label.to_string lab));
273-
ignore(declare_abstraction ~opaque ~continuation ~poly ~scope ~kind arity evdr env c lab_R)
276+
ignore(declare_abstraction ~opaque_access ~opaque ~continuation ~poly ~scope ~kind arity evdr env c lab_R)
274277

275278
| (lab, SFBmind _) ->
276279
let env = Global.env () in
@@ -292,14 +295,14 @@ and declare_module ?(continuation = ignore) ?name arity mb =
292295
@@ Names.MutInd.label
293296
@@ mut_ind
294297
in
295-
declare_inductive ind_name ~continuation arity evdr env pind
298+
declare_inductive ~opaque_access ind_name ~continuation arity evdr env pind
296299
end
297300
| (lab, SFBmodule mb') when
298301
match mb'.mod_type with NoFunctor _ ->
299302
(match mb'.mod_expr with FullStruct | Algebraic _ -> true | _ -> false)
300303
| _ -> false
301304
->
302-
declare_module ~continuation arity mb'
305+
declare_module ~opaque_access ~continuation arity mb'
303306

304307
| (lab, _) ->
305308
Pp.(Flags.if_verbose msg_info (str (Printf.sprintf "Ignoring field '%s'." (Names.Label.to_string lab))));
@@ -332,7 +335,7 @@ let translateFullName ~fullname arity (kername : Names.KerName.t) : string =
332335
(String.concat "_o_" (plstr@[nstr]))
333336
else nstr
334337

335-
let command_constant ?(continuation = default_continuation) ~fullname arity constant names =
338+
let command_constant ~opaque_access ?(continuation = default_continuation) ~fullname arity constant names =
336339
let env = Global.env () in
337340
let evd = Evd.from_env env in
338341
let poly, opaque =
@@ -354,9 +357,10 @@ let command_constant ?(continuation = default_continuation) ~fullname arity cons
354357
Evd.(with_sort_context_set univ_rigid evd (UnivGen.fresh_constant_instance env constant))
355358
in
356359
let constr = mkConstU (fst pconst, EInstance.make @@ snd pconst) in
357-
declare_abstraction ~continuation ~opaque ~poly ~scope ~kind arity (ref evd) env constr name
360+
declare_abstraction ~opaque_access ~continuation ~opaque ~poly ~scope ~kind
361+
arity (ref evd) env constr name
358362

359-
let command_inductive ?(continuation = default_continuation) ~fullname arity inductive names =
363+
let command_inductive ~opaque_access ?(continuation = default_continuation) ~fullname arity inductive names =
360364
let env = Global.env () in
361365
let evd = Evd.from_env env in
362366
let evd, pind =
@@ -372,36 +376,37 @@ let command_inductive ?(continuation = default_continuation) ~fullname arity ind
372376
@@ pind
373377
| Some name -> name
374378
in
375-
declare_inductive name ~continuation arity (ref evd) env pind
379+
declare_inductive ~opaque_access name ~continuation arity (ref evd) env pind
376380

377381
let command_constructor ?(continuation = default_continuation) arity gref names =
378382
let open Pp in
379383
error ((str "'")
380384
++ (Printer.pr_global gref)
381385
++ (str "' is a constructor. To generate its parametric translation, please translate its inductive first."))
382386

383-
let command_reference ?(continuation = default_continuation) ?(fullname = false) arity gref names =
387+
let command_reference ~opaque_access ?(continuation = default_continuation) ?(fullname = false)
388+
arity gref names =
384389
check_nothing_ongoing ();
385390
let open Names.GlobRef in
386391
(* We ignore proofs for now *)
387392
let _pstate = match gref with
388393
| VarRef variable ->
389394
command_variable ~continuation arity variable names
390395
| ConstRef constant ->
391-
command_constant ~continuation ~fullname arity constant names
396+
command_constant ~opaque_access ~continuation ~fullname arity constant names
392397
| IndRef inductive ->
393-
command_inductive ~continuation ~fullname arity inductive names;
398+
command_inductive ~opaque_access ~continuation ~fullname arity inductive names;
394399
None
395400
| ConstructRef constructor ->
396401
command_constructor ~continuation arity gref names
397402
in ()
398403

399-
let command_reference_recursive ?(continuation = default_continuation) ?(fullname = false) arity gref =
404+
let command_reference_recursive ~opaque_access ?(continuation = default_continuation) ?(fullname = false) arity gref =
400405
let gref= Globnames.canonical_gr gref in
401406
let label = Names.Label.of_id (Nametab.basename_of_global gref) in
402407
(* Assumptions doesn't care about the universes *)
403408
let c, _ = UnivGen.fresh_global_instance (Global.env()) gref in
404-
let (direct, graph, _) = Assumptions.traverse label c in
409+
let (direct, graph, _) = Assumptions.traverse opaque_access label c in
405410
let inductive_of_constructor ref =
406411
let open Globnames in
407412
let ref= Globnames.canonical_gr ref in
@@ -428,9 +433,9 @@ let command_reference_recursive ?(continuation = default_continuation) ?(fullnam
428433
(* Pp.(msg_info (str "DepRefs:"));
429434
* List.iter (fun x -> msg_info (Printer.pr_global x)) dep_refs; *)
430435
list_continuation continuation (fun continuation gref ->
431-
command_reference ~continuation ~fullname arity gref None) dep_refs ()
436+
command_reference ~opaque_access ~continuation ~fullname arity gref None) dep_refs ()
432437

433-
let translate_command arity c name =
438+
let translate_command ~opaque_access arity c name =
434439
if !ongoing_translation then error (Pp.str "On going translation.");
435440
(* Same comment as above *)
436441
let env = Global.env () in
@@ -450,5 +455,5 @@ let translate_command arity c name =
450455
in
451456
let scope = Locality.(Global ImportDefaultBehavior) in
452457
let kind = Decls.(IsDefinition Definition) in
453-
let _ : Declare.Proof.t option = declare_abstraction ~opaque ~poly ~scope ~kind arity (ref evd) env c name in
458+
let _ : Declare.Proof.t option = declare_abstraction ~opaque_access ~opaque ~poly ~scope ~kind arity (ref evd) env c name in
454459
()

src/parametricity.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ let lamn n env b =
281281
(* compose_lam [xn:Tn;..;x1:T1] b = [x1:T1]..[xn:Tn]b *)
282282
let compose_lam l b = lamn (List.length l) l b
283283

284+
(* use a functor to avoid having to thread this everywhere *)
285+
module WithOpaqueAccess (Access:sig val access : Global.indirect_accessor end) = struct
286+
284287
(* G |- t ---> |G|, x1, x2 |- [x1,x2] in |t| *)
285288
let rec relation order evd env (t : constr) : constr =
286289
debug_string [`Relation] (Printf.sprintf "relation %d evd env t" order);
@@ -469,7 +472,7 @@ and translate_constant order (evd : Evd.evar_map ref) env cst : constr =
469472
(* let evd' = Evd.add_constraints !evd cte_constraints in *)
470473
(* evd := evd'; *)
471474
let fold = mkConstU cst in
472-
let (def, _) = Global.force_proof Library.indirect_accessor op in
475+
let (def, _) = Global.force_proof Access.access op in
473476
let def = CVars.subst_instance_constr names def in
474477
let etyp = of_constr typ in
475478
let edef = of_constr def in
@@ -1224,3 +1227,4 @@ and translate_mind_inductive name order evdr env ikn mut_entry inst (env_params,
12241227
List.map (to_constr !evdr) result
12251228
end
12261229
}
1230+
end

0 commit comments

Comments
 (0)