Debian Codegraph @

tactics/autorewrite.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 open Equality
12 open Names
13 open Pp
14 open Constr
15 open CErrors
16 open Util
17 open Mod_subst
18 open Locus
19
20 (* Rewriting rules *)
21 type rew_rule = {
22 rew_id : KerName.t;
23 rew_lemma : constr;
24 rew_type: types;
25 rew_pat: constr;
26 rew_ctx: UnivGen.sort_context_set;
27 rew_l2r: bool;
28 rew_tac: Gentactic.glob_generic_tactic option;
29 }
30
31 module RewRule =
32 struct
33 type t = rew_rule
34 let rew_lemma r = (r.rew_ctx, r.rew_lemma)
35 let rew_l2r r = r.rew_l2r
36 let rew_tac r = r.rew_tac
37 end
38
39 module HintIdent =
40 struct
41 type t = rew_rule
42
43 let compare r1 r2 = KerName.compare r1.rew_id r2.rew_id
44
45 let constr_of t = t.rew_pat
46 end
47
48 (* Representation/approximation of terms to use in the dnet:
49 *
50 * - no meta or evar (use ['a pattern] for that)
51 *
52 * - [Rel]s and [Sort]s are not taken into account (that's why we need
53 * a second pass of linear filterin on the results - it's not a perfect
54 * term indexing structure)
55 *)
56
57 module DTerm =
58 struct
59
60 type 't t =
61 | DRel
62 | DSort
63 | DRef of GlobRef.t
64 | DProd
65 | DLet
66 | DLambda
67 | DApp
68 | DCase of case_info
69 | DFix of int array * int
70 | DCoFix of int
71 | DInt of Uint63.t
72 | DFloat of Float64.t
73 | DString of Pstring.t
74 | DArray
75
76 let compare_ci ci1 ci2 =
77 let c = Id.compare (MutInd.label @@ fst ci1.ci_ind) (MutInd.label @@ fst ci2.ci_ind) in
78 if c = 0 then
79 let c = Int.compare ci1.ci_npar ci2.ci_npar in
80 if c = 0 then
81 let c = Array.compare Int.compare ci1.ci_cstr_ndecls ci2.ci_cstr_ndecls in
82 if c = 0 then
83 Array.compare Int.compare ci1.ci_cstr_nargs ci2.ci_cstr_nargs
84 else c
85 else c
86 else c
87
88 let compare t1 t2 = match t1, t2 with
89 | DRel, DRel -> 0
90 | DRel, _ -> -1 | _, DRel -> 1
91 | DSort, DSort -> 0
92 | DSort, _ -> -1 | _, DSort -> 1
93 | DRef gr1, DRef gr2 -> GlobRef.UserOrd.compare gr1 gr2
94 | DRef _, _ -> -1 | _, DRef _ -> 1
95
96 | DProd, DProd -> 0
97 | DProd, _ -> -1 | _, DProd -> 1
98
99 | DLet, DLet -> 0
100 | DLet, _ -> -1 | _, DLet -> 1
101
102 | DLambda, DLambda
103 | DApp, DApp -> 0
104 | DLambda, _ -> -1 | _, DLambda -> 1
105 | DApp, _ -> -1 | _, DApp -> 1
106
107 | DCase ci1, DCase ci2 ->
108 compare_ci ci1 ci2
109 | DCase _, _ -> -1 | _, DCase _ -> 1
110
111 | DFix (i1, j1), DFix (i2, j2) ->
112 let c = Int.compare j1 j2 in
113 if c = 0 then
114 Array.compare Int.compare i1 i2
115 else c
116 | DFix _, _ -> -1 | _, DFix _ -> 1
117
118 | DCoFix i1, DCoFix i2 ->
119 Int.compare i1 i2
120 | DCoFix _, _ -> -1 | _, DCoFix _ -> 1
121
122 | DInt i1, DInt i2 -> Uint63.compare i1 i2
123
124 | DInt _, _ -> -1 | _, DInt _ -> 1
125
126 | DFloat f1, DFloat f2 -> Float64.total_compare f1 f2
127
128 | DFloat _, _ -> -1 | _, DFloat _ -> 1
129
130 | DString s1, DString s2 -> Pstring.compare s1 s2
131
132 | DString _, _ -> -1 | _, DString _ -> 1
133
134 | DArray, DArray -> 1
135
136 end
137
138 (*
139 * Terms discrimination nets
140 * Uses the general dnet datatype on DTerm.t
141 * (here you can restart reading)
142 *)
143
144 module HintDN :
145 sig
146 type t
147 type ident = HintIdent.t
148
149 val empty : t
150
151 (** [add c i dn] adds the binding [(c,i)] to [dn]. [c] can be a
152 closed term or a pattern (with untyped Evars). No Metas accepted *)
153 val add : Environ.env -> constr -> ident -> t -> t
154
155 (*
156 * High-level primitives describing specific search problems
157 *)
158
159 (** [search_pattern dn c] returns all terms/patterns in dn
160 matching/matched by c *)
161 val search_pattern : Environ.env -> t -> constr -> ident list
162
163 (** [find_all dn] returns all idents contained in dn *)
164 val find_all : t -> ident list
165
166 end
167 =
168 struct
169 module Ident = HintIdent
170 module PTerm =
171 struct
172 type t = unit DTerm.t
173 let compare = DTerm.compare
174 end
175 module TDnet = Dn.Make(PTerm)(Ident)
176
177 type t = TDnet.t
178
179 type ident = HintIdent.t
180
181 open DTerm
182 open TDnet
183
184 let pat_of_constr env c : (unit DTerm.t * Constr.t list) option =
185 let open GlobRef in
186 let rec pat_of_constr c = match Constr.kind c with
187 | Rel _ -> Some (DRel, [])
188 | Sort _ -> Some (DSort, [])
189 | Var i -> Some (DRef (VarRef i), [])
190 | Const (c,u) -> Some (DRef (ConstRef (Environ.QConstant.canonize env c)), [])
191 | Ind (i,u) -> Some (DRef (IndRef (Environ.QInd.canonize env i)), [])
192 | Construct (c,u)-> Some (DRef (ConstructRef (Environ.QConstruct.canonize env c)), [])
193 | Meta _ -> assert false
194 | Evar (i,_) -> None
195 | Case (ci,u1,pms1,(c1,_),_iv,c2,ca) ->
196 let f_ctx (_, p) = p in
197 Some (DCase(ci), [f_ctx c1; c2] @ Array.map_to_list f_ctx ca)
198 | Fix ((ia,i),(_,ta,ca)) ->
199 Some (DFix(ia,i), Array.to_list ta @ Array.to_list ca)
200 | CoFix (i,(_,ta,ca)) ->
201 Some (DCoFix(i), Array.to_list ta @ Array.to_list ca)
202 | Cast (c,_,_) -> pat_of_constr c
203 | Lambda (_,t,c) -> Some (DLambda, [t; c])
204 | Prod (_, t, u) -> Some (DProd, [t; u])
205 | LetIn (_, c, t, u) -> Some (DLet, [c; t; u])
206 | App (f,ca) ->
207 let len = Array.length ca in
208 let a = ca.(len - 1) in
209 let ca = Array.sub ca 0 (len - 1) in
210 Some (DApp, [mkApp (f, ca); a])
211 | Proj (p,_,c) ->
212 (* UnsafeMonomorphic is fine because the term will only be used
213 by pat_of_constr which ignores universes *)
214 pat_of_constr (mkApp (UnsafeMonomorphic.mkConst (Projection.constant p), [|c|]))
215 | Int i -> Some (DInt i, [])
216 | Float f -> Some (DFloat f, [])
217 | String s -> Some (DString s, [])
218 | Array (_u,t,def,ty) ->
219 Some (DArray, Array.to_list t @ [def ; ty])
220 in
221 pat_of_constr c
222
223 (*
224 * Basic primitives
225 *)
226
227 let empty = TDnet.empty
228
229 let add env (c:constr) (id:Ident.t) (dn:t) =
230 (* We used to consider the types of the product as well, but since the dnet
231 is only computing an approximation rectified by [filtering] we do not
232 anymore. *)
233 let (ctx, c) = Term.decompose_prod_decls c in
234 let c = TDnet.pattern (fun c -> pat_of_constr env c) c in
235 TDnet.add dn c id
236
237 (* App(c,[t1,...tn]) -> ([c,t1,...,tn-1],tn)
238 App(c,[||]) -> ([],c) *)
239 let split_app sigma c = match EConstr.kind sigma c with
240 App(c,l) ->
241 let len = Array.length l in
242 if Int.equal len 0 then ([],c) else
243 let last = Array.get l (len-1) in
244 let prev = Array.sub l 0 (len-1) in
245 c::(Array.to_list prev), last
246 | _ -> assert false
247
248 exception CannotFilter
249
250 let filtering env sigma ctx cv_pb c1 c2 =
251 let open EConstr in
252 let open Vars in
253 let evm = ref Evar.Map.empty in
254 let define cv_pb e1 ev c1 =
255 try let (e2,c2) = Evar.Map.find ev !evm in
256 let shift = e1 - e2 in
257 if Termops.constr_cmp env sigma cv_pb c1 (lift shift c2) then () else raise CannotFilter
258 with Not_found ->
259 evm := Evar.Map.add ev (e1,c1) !evm
260 in
261 let rec aux ctx cv_pb c1 c2 =
262 match EConstr.kind sigma c1, EConstr.kind sigma c2 with
263 | App _, App _ ->
264 let ((p1,l1),(p2,l2)) = (split_app sigma c1),(split_app sigma c2) in
265 let () = aux ctx cv_pb l1 l2 in
266 begin match p1, p2 with
267 | [], [] -> ()
268 | (h1 :: p1), (h2 :: p2) ->
269 aux ctx cv_pb (applist (h1, p1)) (applist (h2, p2))
270 | _ -> assert false
271 end
272 | Prod (n,t1,c1), Prod (_,t2,c2) ->
273 aux ctx cv_pb t1 t2;
274 aux (ctx + 1) cv_pb c1 c2
275 | _, Evar (ev,_) -> define cv_pb ctx ev c1
276 | Evar (ev,_), _ -> define cv_pb ctx ev c2
277 | _ ->
278 if Termops.compare_constr_univ env sigma
279 (fun pb c1 c2 -> aux ctx pb c1 c2; true) cv_pb c1 c2 then ()
280 else raise CannotFilter
281 (* TODO: le reste des binders *)
282 in
283 try let () = aux ctx cv_pb c1 c2 in true with CannotFilter -> false
284
285 let align_prod_letin sigma c a =
286 let (lc,_) = EConstr.decompose_prod_decls sigma c in
287 let (l,a) = EConstr.decompose_prod_decls sigma a in
288 let lc = List.length lc in
289 let n = List.length l in
290 if n < lc then invalid_arg "align_prod_letin";
291 let l1 = CList.firstn lc l in
292 n - lc, EConstr.it_mkProd_or_LetIn a l1
293
294 let decomp env pat = match pat_of_constr env pat with
295 | None -> Dn.Everything
296 | Some (lbl, args) -> Dn.Label (lbl, args)
297
298 let search_pattern env dn cpat =
299 let _dctx, dpat = Term.decompose_prod_decls cpat in
300 let whole_c = EConstr.of_constr cpat in
301 List.fold_left
302 (fun acc id ->
303 let c_id = EConstr.of_constr @@ Ident.constr_of id in
304 let (ctx,wc) =
305 try align_prod_letin Evd.empty whole_c c_id (* FIXME *)
306 with Invalid_argument _ -> 0, c_id in
307 if filtering env Evd.empty ctx Conversion.CUMUL whole_c wc then id :: acc
308 else acc
309 ) (TDnet.lookup dn (fun c -> decomp env c) dpat) []
310
311 let find_all dn = TDnet.lookup dn (fun () -> Everything) ()
312
313 end
314
315 type rewrite_db = {
316 rdb_hintdn : HintDN.t;
317 rdb_order : int KerName.Map.t;
318 rdb_maxuid : int;
319 }
320
321 let empty_rewrite_db = {
322 rdb_hintdn = HintDN.empty;
323 rdb_order = KerName.Map.empty;
324 rdb_maxuid = 0;
325 }
326
327 (* Summary and Object declaration *)
328 let rewtab =
329 Summary.ref (String.Map.empty : rewrite_db String.Map.t) ~name:"autorewrite"
330
331 let raw_find_base bas = String.Map.find bas !rewtab
332
333 let find_base bas =
334 try raw_find_base bas
335 with Not_found ->
336 user_err
337 (str "Rewriting base " ++ str bas ++ str " does not exist.")
338
339 let find_rewrites bas =
340 let db = find_base bas in
341 let sort r1 r2 = Int.compare (KerName.Map.find r2.rew_id db.rdb_order) (KerName.Map.find r1.rew_id db.rdb_order) in
342 List.sort sort (HintDN.find_all db.rdb_hintdn)
343
344 let find_matches env bas pat =
345 let base = find_base bas in
346 let res = HintDN.search_pattern env base.rdb_hintdn pat in
347 let sort r1 r2 = Int.compare (KerName.Map.find r2.rew_id base.rdb_order) (KerName.Map.find r1.rew_id base.rdb_order) in
348 List.sort sort res
349
350 let print_rewrite_hintdb bas =
351 let env = Global.env () in
352 let sigma = Evd.from_env env in
353 (str "Database " ++ str bas ++ fnl () ++
354 prlist_with_sep fnl
355 (fun h ->
356 str (if h.rew_l2r then "rewrite -> " else "rewrite <- ") ++
357 Printer.pr_lconstr_env env sigma h.rew_lemma ++ str " of type " ++ Printer.pr_lconstr_env env sigma h.rew_type ++
358 Option.cata (fun tac -> str " then use tactic " ++
359 Gentactic.print_glob env sigma tac) (mt ()) h.rew_tac)
360 (find_rewrites bas))
361
362 let tclMAP_rev f args =
363 List.fold_left (fun accu arg -> Tacticals.tclTHEN accu (f arg)) (Proofview.tclUNIT ()) args
364
365 (* Applies all the rules of one base *)
366 let one_base where conds tac_main bas =
367 let lrul = find_rewrites bas in
368 let rewrite dir c tac =
369 let c = (EConstr.of_constr c, Tactypes.NoBindings) in
370 general_rewrite ~where ~l2r:dir AllOccurrences ~freeze:true ~dep:false ~with_evars:false ~tac:(tac, conds) c
371 in
372 let try_rewrite h tc =
373 Proofview.Goal.enter begin fun gl ->
374 let sigma = Proofview.Goal.sigma gl in
375 let subst, ctx' = UnivGen.fresh_sort_context_instance h.rew_ctx in
376 let c' = Vars.subst_univs_level_constr subst h.rew_lemma in
377 let sigma = Evd.merge_sort_context_set Evd.univ_flexible ~src:UState.Internal sigma ctx' in
378 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (rewrite h.rew_l2r c' tc)
379 end in
380 let open Proofview.Notations in
381 Proofview.tclProofInfo [@ocaml.warning "-3"] >>= fun (_name, poly) ->
382 let eval h =
383 let tac = match h.rew_tac with
384 | None -> Proofview.tclUNIT ()
385 | Some tac -> Gentactic.interp tac
386 in
387 Tacticals.tclREPEAT_MAIN (Tacticals.tclTHENFIRST (try_rewrite h tac) tac_main)
388 in
389 let lrul = tclMAP_rev eval lrul in
390 Tacticals.tclREPEAT_MAIN (Proofview.tclPROGRESS lrul)
391
392 (* The AutoRewrite tactic *)
393 let autorewrite ?(conds=Naive) tac_main lbas =
394 Tacticals.tclREPEAT_MAIN (Proofview.tclPROGRESS
395 (tclMAP_rev (fun bas -> (one_base None conds tac_main bas)) lbas))
396
397 let autorewrite_multi_in ?(conds=Naive) idl tac_main lbas =
398 Proofview.Goal.enter begin fun gl ->
399 (* let's check at once if id exists (to raise the appropriate error) *)
400 let _ = List.map (fun id -> Tacmach.pf_get_hyp id gl) idl in
401 Tacticals.tclMAP (fun id ->
402 Tacticals.tclREPEAT_MAIN (Proofview.tclPROGRESS
403 (tclMAP_rev (fun bas -> (one_base (Some id) conds tac_main bas)) lbas)))
404 idl
405 end
406
407 let autorewrite_in ?(conds=Naive) id = autorewrite_multi_in ~conds [id]
408
409 let gen_auto_multi_rewrite conds tac_main lbas cl =
410 let try_do_hyps treat_id l =
411 autorewrite_multi_in ~conds (List.map treat_id l) tac_main lbas
412 in
413 let concl_tac = (if cl.concl_occs != NoOccurrences then autorewrite ~conds tac_main lbas else Proofview.tclUNIT ()) in
414 if not (Locusops.is_all_occurrences cl.concl_occs) &&
415 cl.concl_occs != NoOccurrences
416 then
417 let info = Exninfo.reify () in
418 Tacticals.tclZEROMSG ~info (str"The \"at\" syntax isn't available yet for the autorewrite tactic.")
419 else
420 match cl.onhyps with
421 | Some [] -> concl_tac
422 | Some l -> Tacticals.tclTHENFIRST concl_tac (try_do_hyps (fun ((_,id),_) -> id) l)
423 | None ->
424 let hyp_tac =
425 (* try to rewrite in all hypothesis (except maybe the rewritten one) *)
426 Proofview.Goal.enter begin fun gl ->
427 let ids = Tacmach.pf_ids_of_hyps gl in
428 try_do_hyps (fun id -> id) ids
429 end
430 in
431 Tacticals.tclTHENFIRST concl_tac hyp_tac
432
433 let auto_multi_rewrite ?(conds=Naive) lems cl =
434 Proofview.wrap_exceptions (fun () -> gen_auto_multi_rewrite conds (Proofview.tclUNIT()) lems cl)
435
436 (* Same hack as auto hints: we generate an essentially unique identifier for
437 rewrite hints. *)
438 let fresh_key =
439 let id = Summary.ref ~name:"REWHINT-COUNTER" 0 in
440 fun () ->
441 let cur = incr id; !id in
442 let lbl = Id.of_string ("_" ^ string_of_int cur) in
443 let kn = Lib.make_kn lbl in
444 let (mp, _) = KerName.repr kn in
445 (* We embed the full path of the kernel name in the label so that
446 the identifier should be unique. This ensures that including
447 two modules together won't confuse the corresponding labels. *)
448 let lbl = Id.of_string_soft (Printf.sprintf "%s#%i"
449 (ModPath.to_string mp) cur)
450 in
451 KerName.make mp lbl
452
453 let auto_multi_rewrite_with ?(conds=Naive) tac_main lbas cl =
454 let onconcl = match cl.Locus.concl_occs with NoOccurrences -> false | _ -> true in
455 match onconcl,cl.Locus.onhyps with
456 | false,Some [_] | true,Some [] | false,Some [] ->
457 (* autorewrite with .... in clause using tac n'est sur que
458 si clause represente soit le but soit UNE hypothese
459 *)
460 Proofview.wrap_exceptions (fun () -> gen_auto_multi_rewrite conds tac_main lbas cl)
461 | _ ->
462 let info = Exninfo.reify () in
463 Tacticals.tclZEROMSG ~info
464 (strbrk "autorewrite .. in .. using can only be used either with a unique hypothesis or on the conclusion.")
465
466 type db_obj = {
467 db_local : bool;
468 db_name : string;
469 }
470
471 let warn_create_hintdb =
472 CWarnings.create ~name:"already-declared-rewrite-hint-db" ~category:CWarnings.CoreCategories.automation
473 Pp.(fun db -> str "Rewrite hint database " ++ str db.db_name ++ str " already exists.")
474
475 let warn_implicit_create_hint_db =
476 CWarnings.create ~name:"implicit-create-rewrite-hint-db" ~category:Deprecation.Version.v9_2
477 (fun db -> strbrk "Implicitly declaring Rewrite hint databases is deprecated. Please explicitly create " ++ quote (str db))
478
479 let cache_db db = match String.Map.find_opt db.db_name !rewtab with
480 | None ->
481 rewtab := String.Map.add db.db_name empty_rewrite_db !rewtab
482 | Some _ -> warn_create_hintdb db
483
484 let load_db _ x = cache_db x
485
486 let classify_db db =
487 if db.db_local then Libobject.Dispose else Libobject.Substitute
488
489 let inDB : db_obj -> Libobject.obj =
490 let open Libobject in
491 declare_object {(default_object "AUTOREWRITE_DB") with
492 cache_function = cache_db;
493 load_function = load_db;
494 subst_function = (fun (_, x) -> x);
495 classify_function = classify_db; }
496
497 let create_rewrite_hint_db ~local ~name =
498 let hint = { db_local = local; db_name = name } in
499 Lib.add_leaf (inDB hint)
500
501 (* Functions necessary to the library object declaration *)
502 let cache_hintrewrite (rbase,lrl) =
503 let base = try raw_find_base rbase with Not_found -> empty_rewrite_db in
504 let fold accu r = {
505 rdb_hintdn = HintDN.add (Global.env ()) r.rew_pat r accu.rdb_hintdn;
506 rdb_order = KerName.Map.add r.rew_id accu.rdb_maxuid accu.rdb_order;
507 rdb_maxuid = accu.rdb_maxuid + 1;
508 } in
509 let base = List.fold_left fold base lrl in
510 rewtab := String.Map.add rbase base !rewtab
511
512 let subst_hintrewrite (subst,(rbase,list as node)) =
513 let subst_hint subst hint =
514 let id' = subst_kn subst hint.rew_id in
515 let cst' = subst_mps subst hint.rew_lemma in
516 let typ' = subst_mps subst hint.rew_type in
517 let pat' = subst_mps subst hint.rew_pat in
518 let t' = Option.Smart.map (Gentactic.subst subst) hint.rew_tac in
519 if hint.rew_id == id' && hint.rew_lemma == cst' && hint.rew_type == typ' &&
520 hint.rew_tac == t' && hint.rew_pat == pat' then hint else
521 { hint with
522 rew_lemma = cst'; rew_type = typ';
523 rew_pat = pat'; rew_tac = t' }
524 in
525 let list' = List.Smart.map (fun h -> subst_hint subst h) list in
526 if list' == list then node else
527 (rbase,list')
528
529 (* Declaration of the Hint Rewrite library object *)
530 let inHintRewrite : Libobject.locality * (string * rew_rule list) -> Libobject.obj =
531 let open Libobject in
532 declare_object @@ object_with_locality ~cat:Hints.hint_cat "HINT_REWRITE_GLOBAL"
533 ~cache:cache_hintrewrite
534 ~subst:(Some subst_hintrewrite)
535 ~discharge:(fun _ -> assert false)
536
537 type hypinfo = {
538 hyp_ty : EConstr.types;
539 hyp_pat : EConstr.constr;
540 }
541
542 let decompose_applied_relation env sigma c ctype left2right =
543 let find_rel ty =
544 (* FIXME: this is nonsense, we generate evars and then we drop the
545 corresponding evarmap. This sometimes works because [Term_dnet] performs
546 evar surgery via [Termops.filtering]. *)
547 let sigma, ty = EClause.make_evar_clause env sigma ty in
548 let (_, args) = EConstr.decompose_app sigma ty.EClause.cl_concl in
549 let len = Array.length args in
550 if 2 <= len then
551 let c1 = args.(len - 2) in
552 let c2 = args.(len - 1) in
553 Some (if left2right then c1 else c2)
554 else None
555 in
556 match find_rel ctype with
557 | Some c -> Some { hyp_pat = c; hyp_ty = ctype }
558 | None ->
559 let ctx,t' = Reductionops.whd_decompose_prod_decls env sigma ctype in (* Search for underlying eq *)
560 let ctype = EConstr.it_mkProd_or_LetIn t' ctx in
561 match find_rel ctype with
562 | Some c -> Some { hyp_pat = c; hyp_ty = ctype }
563 | None -> None
564
565 let find_applied_relation ?loc env sigma c left2right =
566 let ctype = Retyping.get_type_of env sigma (EConstr.of_constr c) in
567 match decompose_applied_relation env sigma c ctype left2right with
568 | Some c -> c
569 | None ->
570 user_err ?loc
571 (str"The type" ++ spc () ++ Printer.pr_econstr_env env sigma ctype ++
572 spc () ++ str"of this term does not end with an applied relation.")
573
574 type raw_rew_rule = ((constr * UnivGen.sort_context_set) * bool * Gentactic.raw_generic_tactic option) CAst.t
575
576 (* To add rewriting rules to a base *)
577 let add_rew_rules ~locality base (lrul:raw_rew_rule list) =
578 let () = Locality.check_locality_nodischarge locality in
579 let env = Global.env () in
580 let sigma = Evd.from_env env in
581 let intern tac = Gentactic.intern ~strict:true env tac in
582 let map {CAst.loc;v=((c,ctx),b,t)} =
583 let sigma = Evd.merge_sort_context_set Evd.univ_rigid ~src:UState.Internal sigma ctx in
584 let info = find_applied_relation ?loc env sigma c b in
585 let pat = EConstr.Unsafe.to_constr info.hyp_pat in
586 let uid = fresh_key () in
587 { rew_id = uid; rew_lemma = c; rew_type = EConstr.Unsafe.to_constr info.hyp_ty;
588 rew_pat = pat; rew_ctx = ctx; rew_l2r = b;
589 rew_tac = Option.map intern t }
590 in
591 let lrul = List.map map lrul in
592 Lib.add_leaf (inHintRewrite (locality,(base,lrul)))
593
594
595 let check_declared db =
596 if not (String.Map.mem db !rewtab) then warn_implicit_create_hint_db db
597
598 let add_rewrite_hint ~locality ~poly bases ort t lcsr =
599 let () = List.iter check_declared bases in
600 let env = Global.env() in
601 let sigma = Evd.from_env env in
602 let f ce =
603 let c, ctx = Constrintern.interp_constr env sigma ce in
604 let c = EConstr.to_constr sigma c in
605 let ctx =
606 let ctx = UState.sort_context_set ctx in
607 if PolyFlags.univ_poly poly then ctx
608 else (* This is a global universe context that shouldn't be
609 refreshed at every use of the hint, declare it globally. *)
610 let ((qs, us), (qcst, ucst)) = ctx in
611 let () = Global.push_context_set (us, ucst) in
612 UnivGen.empty_sort_context
613 in
614 CAst.make ?loc:(Constrexpr_ops.constr_loc ce) ((c, ctx), ort, t) in
615 let eqs = List.map f lcsr in
616 let add_hints base = add_rew_rules ~locality base eqs in
617 List.iter add_hints bases