Debian Codegraph @

tactics/ind_tables.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* File created by Vincent Siles, Oct 2007, extended into a generic
12 support for generation of inductive schemes by Hugo Herbelin, Nov 2009 *)
13
14 (* This file provides support for registering inductive scheme builders,
15 declaring schemes and generating schemes on demand *)
16
17 open Names
18 open Nameops
19 open Declarations
20 open Constr
21 open Util
22
23
24 (**********************************************************************)
25 (* Registering schemes in the environment *)
26
27 type handle = Evd.side_effects
28
29 let push_handle eff =
30 let open Proofview.Notations in
31 Proofview.tclEVARMAP >>= fun sigma ->
32 let sigma = Evd.emit_side_effects eff sigma in
33 Proofview.Unsafe.tclEVARS sigma
34
35 type mutual_scheme_object_function =
36 Environ.env -> handle -> MutInd.t -> constr array Evd.in_ustate
37 type individual_scheme_object_function =
38 Environ.env -> handle -> inductive -> constr Evd.in_ustate
39
40 type 'a scheme_kind = string
41
42 let pr_scheme_kind = Pp.str
43
44 (**********************************************************************)
45 (* The table of scheme building functions *)
46
47 type individual
48 type mutual
49
50 type scheme_dependency =
51 | SchemeMutualDep of MutInd.t * mutual scheme_kind
52 | SchemeIndividualDep of inductive * individual scheme_kind
53
54 type scheme_object_function =
55 | MutualSchemeFunction of mutual_scheme_object_function * (Environ.env -> MutInd.t -> scheme_dependency list) option
56 | IndividualSchemeFunction of individual_scheme_object_function * (Environ.env -> inductive -> scheme_dependency list) option
57
58 let scheme_object_table =
59 (Hashtbl.create 17 : (string, string * scheme_object_function) Hashtbl.t)
60
61 let declare_scheme_object key ?(suff=key) f =
62 let () =
63 if not (Id.is_valid ("ind_" ^ suff)) then
64 CErrors.user_err Pp.(str ("Illegal induction scheme suffix: " ^ suff))
65 in
66 if Hashtbl.mem scheme_object_table key then
67 CErrors.user_err
68 Pp.(str "Scheme object " ++ str key ++ str " already declared.")
69 else begin
70 Hashtbl.add scheme_object_table key (suff,f);
71 key
72 end
73
74 let declare_mutual_scheme_object key ?suff ?deps f =
75 declare_scheme_object key ?suff (MutualSchemeFunction (f, deps))
76
77 let declare_individual_scheme_object key ?suff ?deps f =
78 declare_scheme_object key ?suff (IndividualSchemeFunction (f, deps))
79
80 let is_declared_scheme_object key = Hashtbl.mem scheme_object_table key
81
82 let scheme_kind_name (key : _ scheme_kind) : string = key
83
84 (**********************************************************************)
85 (* Defining/retrieving schemes *)
86
87 let is_visible_name id =
88 try ignore (Nametab.locate (Libnames.qualid_of_ident id)); true
89 with Not_found ->
90 (* FIXME: due to private constant declaration being imperative, we have to
91 also check in the global env *)
92 Global.exists_objlabel id
93
94 let compute_name internal id avoid =
95 if internal then
96 let visible id = is_visible_name id || Id.Set.mem id avoid in
97 Namegen.next_ident_away_from (add_prefix "internal_" id) visible
98 else id
99
100 let declare_definition_scheme = ref (fun ~univs ~role ~name ~effs c ->
101 CErrors.anomaly (Pp.str "scheme declaration not registered"))
102
103 let register_definition_scheme = ref (fun ~internal ~name ~const ~univs ?loc () ->
104 CErrors.anomaly (Pp.str "scheme registering not registered"))
105
106 let lookup_scheme kind ind =
107 try Some (DeclareScheme.lookup_scheme kind ind) with Not_found -> None
108
109 type schemes = {
110 sch_eff : Evd.side_effects;
111 sch_reg : (Id.t * Constant.t * Loc.t option * UState.named_universes_entry) list;
112 }
113
114 let empty_schemes eff = {
115 sch_eff = eff;
116 sch_reg = [];
117 }
118
119 let redeclare_schemes { sch_eff = eff } =
120 let fold c role accu = match role with
121 | Evd.Schema (ind, kind) ->
122 try
123 let _ = DeclareScheme.lookup_scheme kind ind in
124 accu
125 with Not_found ->
126 let old = try String.Map.find kind accu with Not_found -> [] in
127 String.Map.add kind ((ind, GlobRef.ConstRef c) :: old) accu
128 in
129 let schemes = Cmap_env.fold fold (Evd.seff_roles eff) String.Map.empty in
130 let iter kind defs = List.iter (DeclareScheme.declare_scheme SuperGlobal kind) defs in
131 String.Map.iter iter schemes
132
133 let local_lookup_scheme eff kind ind = match lookup_scheme kind ind with
134 | Some _ as ans -> ans
135 | None ->
136 let exception Found of Constant.t in
137 let iter c role = match role with
138 | Evd.Schema (i, k) ->
139 if String.equal k kind && Ind.UserOrd.equal i ind then raise (Found c)
140 in
141 (* Inefficient O(n), but the number of locally declared schemes is small and
142 this is very rarely called *)
143 try let _ = Cmap_env.iter iter (Evd.seff_roles eff) in None with Found c -> Some (GlobRef.ConstRef c)
144
145 let local_check_scheme kind ind { sch_eff = eff } =
146 Option.has_some (local_lookup_scheme eff kind ind)
147
148 let define ?loc internal role id c poly uctx sch =
149 let avoid = Safe_typing.constants_of_private (Evd.seff_private sch.sch_eff) in
150 let avoid = Id.Set.of_list @@ List.map (fun cst -> Constant.label cst) avoid in
151 let id = compute_name internal id avoid in
152 let uctx = UState.collapse_above_prop_sort_variables ~to_prop:true uctx in
153 let uctx = UState.normalize_variables uctx in
154 let uctx = UState.minimize uctx in
155 let c = UState.nf_universes uctx c in
156 let uctx = UState.restrict uctx (Vars.universes_of_constr c) in
157 let univs = UState.univ_entry ~poly uctx in
158 let effs = sch.sch_eff in
159 let cst, effs = !declare_definition_scheme ~univs ~role ~name:id ~effs c in
160 let reg = (id, cst, loc, univs) :: sch.sch_reg in
161 cst, { sch_eff = effs; sch_reg = reg }
162
163 module Locmap : sig
164
165 type t
166
167 val default : Loc.t option -> t
168 val make : ?default:Loc.t -> MutInd.t -> Loc.t option list -> t
169 val lookup : locmap:t -> Names.inductive -> Loc.t option
170
171 end = struct
172
173 type t = {
174 default : Loc.t option;
175 mind : MutInd.t option;
176 ind_to_loc : Loc.t Int.Map.t;
177 }
178
179 let lookup ~locmap:{ ind_to_loc; default; mind } (m, i) =
180 let () = match mind with
181 | None -> ()
182 | Some mind -> assert (MutInd.UserOrd.equal mind m)
183 in
184 Int.Map.find_opt i ind_to_loc |> fun loc ->
185 Option.append loc default
186
187 let default default = { default; ind_to_loc = Int.Map.empty; mind = None }
188
189 let make ?default mind locs =
190 let default, ind_to_loc =
191 CList.fold_left_i (fun i (default,m) loc ->
192 let m = match loc with
193 | None -> m
194 | Some loc -> Int.Map.add i loc m
195 in
196 let default = if Option.has_some default then default else loc in
197 default, m)
198 0 (default, Int.Map.empty) locs in
199 { default; ind_to_loc; mind = Some mind }
200
201 end
202
203 let get_env sch =
204 Safe_typing.env_of_safe_env (Evd.get_senv_side_effects sch.sch_eff)
205
206 let globally_declare_schemes sch =
207 Global.Internal.reset_safe_env (Evd.get_senv_side_effects sch.sch_eff)
208
209 (* Assumes that dependencies are already defined *)
210 let rec define_individual_scheme_base ?loc kind suff f ~internal idopt (mind,i as ind) eff =
211 let env = get_env eff in
212 let (c, ctx) = f env eff.sch_eff ind in
213 let mib = Environ.lookup_mind mind env in
214 let id = match idopt with
215 | Some id -> id
216 | None -> add_suffix mib.mind_packets.(i).mind_typename ("_"^suff) in
217 let role = Evd.Schema (ind, kind) in
218 let poly, cumulative = Declareops.inductive_is_polymorphic mib, Declareops.inductive_is_cumulative mib in
219 let poly = PolyFlags.make ~univ_poly:poly ~cumulative ~collapse_sort_variables:true in
220 let const, eff = define ?loc internal role id c poly ctx eff in
221 const, eff
222
223 and define_individual_scheme ?loc kind ~internal names (mind,i as ind) eff =
224 match Hashtbl.find scheme_object_table kind with
225 | _,MutualSchemeFunction _ -> assert false
226 | s,IndividualSchemeFunction (f, deps) ->
227 let env = get_env eff in
228 let deps = match deps with None -> [] | Some deps -> deps env ind in
229 let eff = List.fold_left (fun eff dep -> declare_scheme_dependence eff dep) eff deps in
230 let _, eff = define_individual_scheme_base ?loc kind s f ~internal names ind eff in
231 eff
232
233 (* Assumes that dependencies are already defined *)
234 and define_mutual_scheme_base ?(locmap=Locmap.default None) kind suff f ~internal names mind eff =
235 let env = get_env eff in
236 let (cl, ctx) = f env eff.sch_eff mind in
237 let mib = Environ.lookup_mind mind env in
238 let ids = Array.init (Array.length mib.mind_packets) (fun i ->
239 try Int.List.assoc i names
240 with Not_found -> add_suffix mib.mind_packets.(i).mind_typename ("_"^suff)) in
241 let fold i effs id cl =
242 let role = Evd.Schema ((mind, i), kind)in
243 let loc = Locmap.lookup ~locmap (mind,i) in
244 (* FIXME cumulativity not supported? *)
245 let poly = PolyFlags.of_univ_poly (Declareops.inductive_is_polymorphic mib) in
246 let cst, effs = define ?loc internal role id cl poly ctx effs in
247 (effs, cst)
248 in
249 let (eff, consts) = Array.fold_left2_map_i fold eff ids cl in
250 consts, eff
251
252 and define_mutual_scheme ?locmap kind ~internal names mind eff =
253 match Hashtbl.find scheme_object_table kind with
254 | _,IndividualSchemeFunction _ -> assert false
255 | s,MutualSchemeFunction (f, deps) ->
256 let env = get_env eff in
257 let deps = match deps with None -> [] | Some deps -> deps env mind in
258 let eff = List.fold_left (fun eff dep -> declare_scheme_dependence eff dep) eff deps in
259 let _, eff = define_mutual_scheme_base ?locmap kind s f ~internal names mind eff in
260 eff
261
262 and declare_scheme_dependence eff sd =
263 match sd with
264 | SchemeIndividualDep (ind, kind) ->
265 if local_check_scheme kind ind eff then eff
266 else define_individual_scheme kind ~internal:true None ind eff
267 | SchemeMutualDep (mind, kind) ->
268 if local_check_scheme kind (mind, 0) eff then eff
269 else define_mutual_scheme kind ~internal:true [] mind eff
270
271 let find_scheme kind (mind,i as ind) =
272 let open Proofview.Notations in
273 Proofview.tclEVARMAP >>= fun sigma ->
274 let s = local_lookup_scheme (Evd.eval_side_effects sigma) kind ind in
275 Proofview.tclUNIT s
276
277 let force_find_scheme kind (mind,i as ind) =
278 let open Proofview.Notations in
279 Proofview.tclEVARMAP >>= fun sigma ->
280 let eff = Evd.eval_side_effects sigma in
281 match local_lookup_scheme eff kind ind with
282 | Some s ->
283 Proofview.tclUNIT s
284 | None ->
285 let senv = Evd.get_senv_side_effects eff in
286 try
287 let eff, ans = match Hashtbl.find scheme_object_table kind with
288 | s,IndividualSchemeFunction (f, deps) ->
289 let env = Safe_typing.env_of_safe_env senv in
290 let deps = match deps with None -> [] | Some deps -> deps env ind in
291 let sch = empty_schemes eff in
292 let eff = List.fold_left (fun eff dep -> declare_scheme_dependence eff dep) sch deps in
293 let c, eff = define_individual_scheme_base kind s f ~internal:true None ind eff in
294 eff, c
295 | s,MutualSchemeFunction (f, deps) ->
296 let env = Safe_typing.env_of_safe_env senv in
297 let deps = match deps with None -> [] | Some deps -> deps env mind in
298 let sch = empty_schemes eff in
299 let eff = List.fold_left (fun eff dep -> declare_scheme_dependence eff dep) sch deps in
300 let ca, eff = define_mutual_scheme_base kind s f ~internal:true [] mind eff in
301 eff, ca.(i)
302 in
303 let sigma = Evd.emit_side_effects eff.sch_eff sigma in
304 Proofview.Unsafe.tclEVARS sigma <*> Proofview.tclUNIT (GlobRef.ConstRef ans)
305 with Rocqlib.NotFoundRef _ as e ->
306 let e, info = Exninfo.capture e in
307 Proofview.tclZERO ~info e
308
309 let register_schemes sch =
310 let iter (id, kn, loc, univs) =
311 !register_definition_scheme ~internal:false ~name:id ~const:kn ~univs ?loc ()
312 in
313 List.iter iter (List.rev sch.sch_reg)
314
315 let define_individual_scheme ?loc kind names ind =
316 let sch = empty_schemes Evd.empty_side_effects in
317 let eff = define_individual_scheme ?loc kind ~internal:false names ind sch in
318 let () = globally_declare_schemes eff in
319 let () = register_schemes eff in
320 redeclare_schemes eff
321
322 let define_mutual_scheme ?locmap kind names mind =
323 let sch = empty_schemes Evd.empty_side_effects in
324 let eff = define_mutual_scheme ?locmap kind ~internal:false names mind sch in
325 let () = globally_declare_schemes eff in
326 let () = register_schemes eff in
327 redeclare_schemes eff