Debian Codegraph @

tactics/redexpr.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 Pp
12 open CErrors
13 open Util
14 open Names
15 open Constr
16 open EConstr
17 open Genredexpr
18 open Pattern
19 open Reductionops
20 open Tacred
21 open RedFlags
22
23 let warn_vm_disabled =
24 CWarnings.create ~name:"vm-compute-disabled" ~category:CWarnings.CoreCategories.bytecode_compiler
25 (fun () ->
26 strbrk "vm_compute disabled at configure time; falling back to cbv.")
27
28 (* call by value normalisation function using the virtual machine *)
29 let cbv_vm env sigma c =
30 if (Environ.typing_flags env).enable_VM then
31 let ctyp = Retyping.get_type_of env sigma c in
32 Vnorm.cbv_vm env sigma c ctyp
33 else begin
34 warn_vm_disabled ();
35 compute env sigma c
36 end
37
38 let warn_native_compute_disabled =
39 CWarnings.create_in Nativeconv.w_native_disabled
40 (fun () ->
41 strbrk "native_compute disabled at configure time; falling back to vm_compute.")
42
43 let cbv_native env sigma c =
44 if (Environ.typing_flags env).enable_native_compiler then
45 let ctyp = Retyping.get_type_of env sigma c in
46 Nativenorm.native_norm env sigma c ctyp
47 else
48 (warn_native_compute_disabled ();
49 cbv_vm env sigma c)
50
51 let { Goptions.get = simplIsCbn } =
52 Goptions.declare_bool_option_and_ref
53 ~key:["SimplIsCbn"]
54 ~value:false
55 ()
56
57 let set_strategy_one ref l =
58 Global.set_strategy (Evaluable.to_kevaluable ref) l
59
60 let cache_strategy (_,str) =
61 List.iter
62 (fun (lev,ql) -> List.iter (fun q -> set_strategy_one q lev) ql)
63 str
64
65 let subst_strategy (subs,(local,obj)) =
66 local,
67 List.Smart.map
68 (fun (k,ql as entry) ->
69 let ql' = List.Smart.map (Tacred.subst_evaluable_reference subs) ql in
70 if ql==ql' then entry else (k,ql'))
71 obj
72
73
74 let map_strategy f l =
75 let l' = List.fold_right
76 (fun (lev,ql) str ->
77 let ql' = List.fold_right
78 (fun q ql ->
79 match f q with
80 Some q' -> q' :: ql
81 | None -> ql) ql [] in
82 if List.is_empty ql' then str else (lev,ql')::str) l [] in
83 if List.is_empty l' then None else Some (false,l')
84
85 let classify_strategy (local,_) =
86 let open Libobject in
87 if local then Dispose else Substitute
88
89 let disch_ref ref =
90 match ref with
91 | Evaluable.EvalConstRef c -> Some ref
92 | Evaluable.EvalProjectionRef p ->
93 let p = Global.discharge_proj_repr p in
94 Some (Evaluable.EvalProjectionRef p)
95 | Evaluable.EvalVarRef id -> if Global.is_in_section (GlobRef.VarRef id) then None else Some ref
96
97 let discharge_strategy (local,obj) =
98 if local then None else
99 map_strategy disch_ref obj
100
101 type strategy_obj = bool * (Conv_oracle.level * Evaluable.t list) list
102
103 let inStrategy : strategy_obj -> Libobject.obj =
104 let open Libobject in
105 declare_object
106 {(default_object "STRATEGY") with
107 cache_function = cache_strategy;
108 load_function = (fun _ obj -> cache_strategy obj);
109 subst_function = subst_strategy;
110 discharge_function = discharge_strategy;
111 classify_function = classify_strategy;
112 }
113
114 let check_not_fully_opaque l ref =
115 match ref, l with
116 | Evaluable.EvalConstRef sp, Conv_oracle.Opaque -> ()
117 | Evaluable.EvalConstRef sp, _ ->
118 if Environ.mem_constant sp (Global.env ()) && Declareops.is_opaque (Global.lookup_constant sp) (* FIXME *) then
119 user_err
120 (str "Cannot make" ++ spc () ++
121 Nametab.pr_global_env Id.Set.empty (GlobRef.ConstRef sp) ++
122 spc () ++ str "transparent because it was declared opaque.")
123 | _ -> ()
124
125 let set_strategy local str =
126 List.iter (fun (l,refs) -> List.iter (check_not_fully_opaque l) refs) str;
127 Lib.add_leaf (inStrategy (local,str))
128
129 (* Generic reduction: reduction functions used in reduction tactics *)
130
131 module User =
132 struct
133
134 type ('raw, 'glb) user_red_spec = {
135 ured_intern : Constrintern.ltac_sign -> 'raw -> 'glb;
136 ured_subst : Mod_subst.substitution -> 'glb -> 'glb;
137 ured_eval : 'glb -> e_reduction_function * cast_kind;
138 ured_rprint : Environ.env -> Evd.evar_map -> 'raw -> Pp.t;
139 ured_gprint : Environ.env -> Evd.evar_map -> 'glb -> Pp.t;
140 }
141
142 type 'a spec = Spec : ('raw, 'glb) user_red_spec -> ('raw * 'glb) spec
143
144 module Dyn = Dyn.Make()
145
146 module Map = Dyn.Map(struct type 'a t = 'a spec end)
147
148 let map = ref Map.empty
149
150 type ('a, 'b) user_red = UserRed : ('a * 'b) Dyn.tag -> ('a, 'b) user_red
151
152 type 'a user_red_expr =
153 | RawRed : ('a * 'b) Dyn.tag * 'a -> Genarg.rlevel user_red_expr
154 | GlbRed : ('a * 'b) Dyn.tag * 'b -> Genarg.glevel user_red_expr
155
156 let create (type raw glb) name (red : (raw, glb) user_red_spec) =
157 let tag : (raw * glb) Dyn.tag = Dyn.create name in
158 let () = map := Map.add tag (Spec red) !map in
159 UserRed tag
160
161 let make (type a b) (tag : (a, b) user_red) (v : a) : Genarg.rlevel user_red_expr =
162 let UserRed tag = tag in
163 RawRed (tag, v)
164
165 let get tag =
166 let (Spec decl) = Map.find tag !map in
167 decl
168
169 end
170
171 type 'a user_red_expr = 'a User.user_red_expr
172
173 type raw_red_expr = Genarg.rlevel user_red_expr Genredexpr.raw_red_expr
174 type glob_red_expr = Genarg.glevel user_red_expr Genredexpr.glob_red_expr
175 type red_expr = (constr, Evaluable.t, constr_pattern, int, Genarg.glevel user_red_expr) red_expr_gen
176
177 type red_expr_val =
178 (constr, Evaluable.t, constr_pattern, int, strength * RedFlags.reds, Genarg.glevel user_red_expr) red_expr_gen0
179
180 let intern_user_red_expr ist usr =
181 let User.RawRed (tag, v) = usr in
182 let decl = User.get tag in
183 User.GlbRed (tag, decl.User.ured_intern ist v)
184
185 let subst_user_red_expr subst usr =
186 let User.GlbRed (tag, v) = usr in
187 let decl = User.get tag in
188 User.GlbRed (tag, decl.User.ured_subst subst v)
189
190 let eval_user_red_expr usr =
191 let User.GlbRed (tag, v) = usr in
192 let decl = User.get tag in
193 decl.User.ured_eval v
194
195 let pr_raw_user_red_expr env sigma usr =
196 let User.RawRed (tag, v) = usr in
197 let decl = User.get tag in
198 decl.User.ured_rprint env sigma v
199
200 let pr_glob_user_red_expr env sigma usr =
201 let User.GlbRed (tag, v) = usr in
202 let decl = User.get tag in
203 decl.User.ured_gprint env sigma v
204
205 let make_flag_constant = function
206 | Evaluable.EvalVarRef id -> [fVAR id]
207 | Evaluable.EvalConstRef sp ->
208 begin
209 match Structures.PrimitiveProjections.find_opt sp with
210 | None -> [fCONST sp]
211 | Some p -> [fCONST sp; fPROJ p]
212 end
213 | Evaluable.EvalProjectionRef p -> [fPROJ p; fCONST (Projection.Repr.constant p)]
214
215 let make_flag env f =
216 let red = no_red in
217 let red = if f.rBeta then red_add red fBETA else red in
218 let red = if f.rMatch then red_add red fMATCH else red in
219 let red = if f.rFix then red_add red fFIX else red in
220 let red = if f.rCofix then red_add red fCOFIX else red in
221 let red = if f.rZeta then red_add red fZETA else red in
222 let red =
223 if f.rDelta then (* All but rConst *)
224 let red = red_add red fDELTA in
225 let red = red_add_transparent red
226 (Conv_oracle.get_transp_state (Environ.oracle env)) in
227 List.fold_right
228 (fun v red -> red_sub_list red (make_flag_constant v))
229 f.rConst red
230 else (* Only rConst *)
231 let red = red_add red fDELTA in
232 List.fold_right
233 (fun v red -> red_add_list red (make_flag_constant v))
234 f.rConst red
235 in
236 f.rStrength, red
237
238 (* table of custom reductino fonctions, not synchronized,
239 filled via ML calls to [declare_reduction] *)
240 let reduction_tab = ref String.Map.empty
241
242 (* table of custom reduction expressions, synchronized,
243 filled by command Declare Reduction *)
244 let red_expr_tab = Summary.ref String.Map.empty ~name:"Declare Reduction"
245
246 let declare_reduction s f =
247 if String.Map.mem s !reduction_tab || String.Map.mem s !red_expr_tab
248 then user_err
249 (str "There is already a reduction expression of name " ++ str s ++ str ".")
250 else reduction_tab := String.Map.add s f !reduction_tab
251
252 let check_custom = function
253 | ExtraRedExpr s ->
254 if not (String.Map.mem s !reduction_tab || String.Map.mem s !red_expr_tab)
255 then user_err (str "Reference to undefined reduction expression " ++ str s ++ str ".")
256 |_ -> ()
257
258 let decl_red_expr s e =
259 if String.Map.mem s !reduction_tab || String.Map.mem s !red_expr_tab
260 then user_err
261 (str "There is already a reduction expression of name " ++ str s ++ str ".")
262 else begin
263 check_custom e;
264 red_expr_tab := String.Map.add s e !red_expr_tab
265 end
266
267 let out_arg = function
268 | Locus.ArgVar _ -> anomaly (Pp.str "Unevaluated or_var variable.")
269 | Locus.ArgArg x -> x
270
271 let check_occurrences occs =
272 match occs with
273 | Locus.OnlyOccurrences (n::_ as nl) when n < 0 ->
274 Locus.AllOccurrencesBut (List.map abs nl)
275 | Locus.OnlyOccurrences nl when List.exists (fun n -> n < 0) nl ->
276 CErrors.user_err Pp.(str "Illegal negative occurrence number.")
277 | Locus.OnlyOccurrences _ | Locus.AllOccurrencesBut _ | Locus.NoOccurrences
278 | Locus.AllOccurrences | Locus.AtLeastOneOccurrence -> occs
279
280 let out_occurrences occs =
281 let occs = Locusops.occurrences_map (List.map out_arg) occs in
282 check_occurrences occs
283
284 let out_with_occurrences (occs,c) =
285 (out_occurrences occs, c)
286
287 let e_red f env evm c = evm, f env evm c
288
289 let contextualize f = function
290 | Some (occs,c) ->
291 let l = check_occurrences occs in
292 let b,c = match c with
293 | Inl r -> true,PRef (global_of_evaluable_reference r)
294 | Inr c -> false,c in
295 e_red (contextually b (l,c) (fun _ -> f))
296 | None -> e_red f
297
298 let warn_simpl_unfolding_modifiers =
299 CWarnings.create ~name:"simpl-unfolding-modifiers" ~category:CWarnings.CoreCategories.tactics
300 (fun () ->
301 Pp.strbrk "The legacy simpl ignores constant unfolding modifiers.")
302
303 let rec eval_red_expr env = function
304 | Simpl (f, o) ->
305 let () =
306 if not (simplIsCbn () || List.is_empty f.rConst) then
307 warn_simpl_unfolding_modifiers () in
308 let f = if simplIsCbn () then make_flag env f else f.rStrength, RedFlags.all (* dummy *) in
309 Simpl (f, o)
310 | Cbv f -> Cbv (make_flag env f)
311 | Cbn f -> Cbn (make_flag env f)
312 | Lazy f -> Lazy (make_flag env f)
313 | ExtraRedExpr s ->
314 begin match String.Map.find s !red_expr_tab with
315 | e -> eval_red_expr env e
316 | exception Not_found -> ExtraRedExpr s (* delay to runtime interpretation *)
317 end
318 | (Red | Hnf | Unfold _ | Fold _ | Pattern _ | CbvVm _ | CbvNative _ | UserRed _) as e -> e
319
320 let red_product_exn env sigma c = match red_product env sigma c with
321 | None -> user_err Pp.(str "No head constant to reduce.")
322 | Some c -> c
323
324 let pattern_occs occs env sigma c = match pattern_occs occs env sigma c with
325 | NoChange -> sigma, c
326 | Changed (sigma, c) -> sigma, c
327
328 let reduction_of_red_expr_val = function
329 | Red -> (e_red red_product_exn, DEFAULTcast)
330 | Hnf -> (e_red hnf_constr,DEFAULTcast)
331 | Simpl ((w,f),o) ->
332 let am = match w, simplIsCbn () with
333 | Norm, true -> Cbn.norm_cbn f
334 | Norm, false -> simpl
335 | Head, true -> Cbn.whd_cbn f
336 | Head, false -> whd_simpl
337 in
338 (contextualize am o,DEFAULTcast)
339 | Cbv (Norm, f) -> (e_red (cbv_norm_flags ~strong:true f),DEFAULTcast)
340 | Cbv (Head, f) -> (e_red (cbv_norm_flags ~strong:false f),DEFAULTcast)
341 | Cbn (w,f) ->
342 let cbn = match w with Norm -> Cbn.norm_cbn | Head -> Cbn.whd_cbn in
343 (e_red (cbn f), DEFAULTcast)
344 | Lazy (w,f) ->
345 let redf = match w with Norm -> clos_norm_flags | Head -> clos_whd_flags in
346 (e_red (redf f),DEFAULTcast)
347 | Unfold ubinds -> (e_red (unfoldn (List.map (on_fst check_occurrences) ubinds)),DEFAULTcast)
348 | Fold cl -> (e_red (fold_commands cl),DEFAULTcast)
349 | Pattern lp -> (pattern_occs (List.map (on_fst check_occurrences) lp),DEFAULTcast)
350 | ExtraRedExpr s ->
351 (try (e_red (String.Map.find s !reduction_tab),DEFAULTcast)
352 with Not_found ->
353 user_err
354 (str "Unknown user-defined reduction \"" ++ str s ++ str "\"."))
355 | CbvVm o -> (contextualize cbv_vm o, VMcast)
356 | CbvNative o -> (contextualize cbv_native o, NATIVEcast)
357 | UserRed usr -> eval_user_red_expr usr
358
359 let reduction_of_red_expr env r =
360 reduction_of_red_expr_val (eval_red_expr env r)
361
362 (* Possibly equip a reduction with the occurrences mentioned in an
363 occurrence clause *)
364
365 let error_illegal_clause () =
366 CErrors.user_err Pp.(str "\"at\" clause not supported in presence of an occurrence clause.")
367
368 let error_multiple_patterns () =
369 CErrors.user_err Pp.(str "\"at\" clause in occurences not supported with multiple patterns or references.")
370
371 let error_illegal_non_atomic_clause () =
372 CErrors.user_err Pp.(str "\"at\" clause not supported in presence of a non atomic \"in\" clause.")
373
374 let error_at_in_occurrences_not_supported () =
375 CErrors.user_err Pp.(str "\"at\" clause not supported for this reduction tactic.")
376
377 let bind_red_expr_occurrences occs nbcl redexp =
378 let open Locus in
379 let has_at_clause = function
380 | Unfold l -> List.exists (fun (occl,_) -> occl != AllOccurrences) l
381 | Pattern l -> List.exists (fun (occl,_) -> occl != AllOccurrences) l
382 | Simpl (_,Some (occl,_)) -> occl != AllOccurrences
383 | _ -> false in
384 if occs == AllOccurrences then
385 if nbcl > 1 && has_at_clause redexp then
386 error_illegal_non_atomic_clause ()
387 else
388 redexp
389 else
390 match redexp with
391 | Unfold (_::_::_) ->
392 error_multiple_patterns ()
393 | Unfold [(occl,c)] ->
394 if occl != AllOccurrences then
395 error_illegal_clause ()
396 else
397 Unfold [(occs,c)]
398 | Pattern (_::_::_) ->
399 error_multiple_patterns ()
400 | Pattern [(occl,c)] ->
401 if occl != AllOccurrences then
402 error_illegal_clause ()
403 else
404 Pattern [(occs,c)]
405 | Simpl (f,Some (occl,c)) ->
406 if occl != AllOccurrences then
407 error_illegal_clause ()
408 else
409 Simpl (f,Some (occs,c))
410 | CbvVm (Some (occl,c)) ->
411 if occl != AllOccurrences then
412 error_illegal_clause ()
413 else
414 CbvVm (Some (occs,c))
415 | CbvNative (Some (occl,c)) ->
416 if occl != AllOccurrences then
417 error_illegal_clause ()
418 else
419 CbvNative (Some (occs,c))
420 | Red | Hnf | Cbv _ | Lazy _ | Cbn _
421 | ExtraRedExpr _ | Fold _ | Simpl (_,None) | CbvVm None | CbvNative None | UserRed _ ->
422 error_at_in_occurrences_not_supported ()
423 | Unfold [] | Pattern [] ->
424 assert false
425
426 let reduction_of_red_expr_val ?occs r =
427 let r = match occs with
428 | None -> r
429 | Some (occs, nbcl) -> bind_red_expr_occurrences occs nbcl r
430 in
431 reduction_of_red_expr_val r
432
433 let subst_mps subst c =
434 EConstr.of_constr (Mod_subst.subst_mps subst (EConstr.Unsafe.to_constr c))
435
436 let subst_red_expr subs =
437 let env = Global.env () in
438 let sigma = Evd.from_env env in
439 Redops.map_red_expr_gen
440 (subst_mps subs)
441 (Tacred.subst_evaluable_reference subs)
442 (Patternops.subst_pattern env sigma subs)
443 (subst_user_red_expr subs)
444
445 let inReduction : bool * string * red_expr -> Libobject.obj =
446 let open Libobject in
447 declare_object
448 {(default_object "REDUCTION") with
449 cache_function = (fun (_,s,e) -> decl_red_expr s e);
450 load_function = (fun _ (_,s,e) -> decl_red_expr s e);
451 subst_function =
452 (fun (subs,(b,s,e)) -> b,s,subst_red_expr subs e);
453 classify_function =
454 (fun ((b,_,_)) -> if b then Dispose else Substitute) }
455
456 let declare_red_expr locality s expr =
457 Lib.add_leaf (inReduction (locality,s,expr))
458
459 let make0 ?dyn name =
460 let wit = Genarg.make0 name in
461 let () = Geninterp.register_val0 wit dyn in
462 wit
463
464 let wit_red_expr = make0 "redexpr"
465
466 module Intern = struct
467 open CAst
468 open Constrexpr
469 open Libnames
470
471 let evalref_of_globref ?loc r =
472 let () =
473 (* only dump section variables not proof context variables
474 (broken if variables got renamed) *)
475 let is_proof_variable = match r with
476 | GlobRef.VarRef x -> (try ignore (Global.lookup_named x); false with Not_found -> true)
477 | _ -> false
478 in
479 if not is_proof_variable then Dumpglob.add_glob ?loc r
480 in
481 Tacred.soft_evaluable_of_global_reference ?loc r
482
483 type ('constr,'ref,'pat) intern_env = {
484 strict_check : bool;
485 local_ref : qualid -> 'ref option;
486 global_ref : ?short:lident -> Evaluable.t -> 'ref;
487 intern_constr : constr_expr -> 'constr;
488 ltac_sign : Constrintern.ltac_sign;
489 intern_pattern : constr_expr -> 'pat;
490 pattern_of_glob : Glob_term.glob_constr -> 'pat;
491 }
492
493 let intern_global_reference ist qid =
494 match ist.local_ref qid with
495 | Some v -> v
496 | None ->
497 let r =
498 try Smartlocate.locate_global_with_alias ~head:true qid
499 with
500 | Not_found as exn ->
501 if not ist.strict_check && qualid_is_ident qid then
502 let id = qualid_basename qid in
503 GlobRef.VarRef id
504 else
505 let _, info = Exninfo.capture exn in
506 Nametab.error_global_not_found ~info qid
507 in
508 let short =
509 if qualid_is_ident qid && not ist.strict_check then
510 Some (make ?loc:qid.CAst.loc @@ qualid_basename qid)
511 else None
512 in
513 let r = evalref_of_globref ?loc:qid.loc r in
514 ist.global_ref ?short r
515
516 let intern_evaluable ist = function
517 | {v=AN qid} -> intern_global_reference ist qid
518 | {v=ByNotation (ntn,sc);loc} ->
519 let check = GlobRef.(function ConstRef _ | VarRef _ -> true | _ -> false) in
520 let r = Notation.interp_notation_as_global_reference ?loc ~head:true check ntn sc in
521 let r = evalref_of_globref ?loc r in
522 ist.global_ref r
523
524 let intern_typed_pattern_or_ref_with_occurrences ist (l,p) =
525 let interp_ref r =
526 try Inl (intern_evaluable ist r)
527 with e when CErrors.noncritical e ->
528 (* Compatibility. In practice, this means that the code above
529 is useless. Still the idea of having either an evaluable
530 ref or a pattern seems interesting, with "head" reduction
531 in case of an evaluable ref, and "strong" reduction in the
532 subterm matched when a pattern *)
533 let r = match r with
534 | {CAst.v=AN r} -> r
535 | {loc} -> (qualid_of_path ?loc (Nametab.path_of_global (Smartlocate.smart_global r))) in
536 let loc = r.loc in
537 let c = Constrintern.interp_reference ist.ltac_sign r in
538 match DAst.get c with
539 | GRef (r,None) ->
540 let r = evalref_of_globref ?loc r in
541 Inl (ist.global_ref r)
542 | GVar id ->
543 let r = evalref_of_globref (GlobRef.VarRef id) in
544 Inl (ist.global_ref r)
545 | _ ->
546 Inr (ist.pattern_of_glob c)
547 in
548 let p = match p with
549 | Inl r -> interp_ref r
550 | Inr { v = CAppExpl((r,None),[]) } ->
551 (* We interpret similarly @ref and ref *)
552 interp_ref (make @@ AN r)
553 | Inr c ->
554 Inr (ist.intern_pattern c)
555 in
556 (l, p)
557
558 let intern_constr_with_occurrences ist (l,c) = (l,ist.intern_constr c)
559
560 let intern_flag ist red =
561 { red with rConst = List.map (intern_evaluable ist) red.rConst }
562
563 let intern_unfold ist (l,qid) = (l,intern_evaluable ist qid)
564
565 let intern_red_expr ist = function
566 | Unfold l -> Unfold (List.map (intern_unfold ist) l)
567 | Fold l -> Fold (List.map ist.intern_constr l)
568 | Cbv f -> Cbv (intern_flag ist f)
569 | Cbn f -> Cbn (intern_flag ist f)
570 | Lazy f -> Lazy (intern_flag ist f)
571 | Pattern l -> Pattern (List.map (intern_constr_with_occurrences ist) l)
572 | Simpl (f,o) ->
573 Simpl (intern_flag ist f,
574 Option.map (intern_typed_pattern_or_ref_with_occurrences ist) o)
575 | CbvVm o -> CbvVm (Option.map (intern_typed_pattern_or_ref_with_occurrences ist) o)
576 | CbvNative o -> CbvNative (Option.map (intern_typed_pattern_or_ref_with_occurrences ist) o)
577 | (Red | Hnf | ExtraRedExpr _ as r ) -> r
578 | UserRed usr -> UserRed (intern_user_red_expr ist.ltac_sign usr)
579
580 let intern_constr env c =
581 Constrintern.intern_gen WithoutTypeConstraint ~strict_check:true env (Evd.from_env env) c
582
583 let intern_pattern env c =
584 Constrintern.intern_gen WithoutTypeConstraint ~strict_check:true ~pattern_mode:true
585 env (Evd.from_env env) c
586
587 let from_env env = {
588 strict_check = true;
589 local_ref = (fun _ -> None);
590 global_ref = (fun ?short:_ r -> r);
591 intern_constr = intern_constr env;
592 ltac_sign = Constrintern.empty_ltac_sign;
593 pattern_of_glob = (fun c -> c);
594 intern_pattern = intern_pattern env;
595 }
596
597 end
598
599 module Interp = struct
600
601 type ('constr,'evref,'pat,'usr) interp_env = {
602 interp_occurrence_var : lident -> int list;
603 interp_constr : Environ.env -> Evd.evar_map -> 'constr -> Evd.evar_map * EConstr.constr;
604 interp_constr_list : Environ.env -> Evd.evar_map -> 'constr -> Evd.evar_map * EConstr.constr list;
605 interp_evaluable : Environ.env -> Evd.evar_map -> 'evref -> Evaluable.t;
606 interp_pattern : Environ.env -> Evd.evar_map -> 'pat -> constr_pattern;
607 interp_evaluable_or_pattern : Environ.env -> Evd.evar_map
608 -> 'evref -> (Evaluable.t, constr_pattern) Util.union;
609 }
610
611 let interp_occurrences ist occs =
612 let open Locus in
613 let map = function
614 | ArgArg x -> [x]
615 | ArgVar lid -> ist.interp_occurrence_var lid
616 in
617 Locusops.occurrences_map (List.concat_map map) occs
618
619 let interp_constr_with_occurrences ist env sigma (occs,c) =
620 let (sigma,c_interp) = ist.interp_constr env sigma c in
621 sigma , (interp_occurrences ist occs, c_interp)
622
623 let interp_evaluable ist env sigma r = ist.interp_evaluable env sigma r
624
625 let interp_closed_typed_pattern_with_occurrences ist env sigma (occs, p) =
626 let p = match p with
627 | Inr p -> Inr (ist.interp_pattern env sigma p)
628 | Inl r -> ist.interp_evaluable_or_pattern env sigma r
629 in
630 interp_occurrences ist occs, p
631
632 let interp_unfold ist env sigma (occs,qid) =
633 (interp_occurrences ist occs,interp_evaluable ist env sigma qid)
634
635 let interp_flag ist env sigma red =
636 { red with rConst = List.map (interp_evaluable ist env sigma) red.rConst }
637
638 let interp_red_expr ist env sigma = function
639 | Unfold l -> sigma , Unfold (List.map (interp_unfold ist env sigma) l)
640 | Fold l ->
641 let (sigma,l_interp) = List.fold_left_map (ist.interp_constr_list env) sigma l in
642 sigma , Fold (List.flatten l_interp)
643 | Cbv f -> sigma , Cbv (interp_flag ist env sigma f)
644 | Cbn f -> sigma , Cbn (interp_flag ist env sigma f)
645 | Lazy f -> sigma , Lazy (interp_flag ist env sigma f)
646 | Pattern l ->
647 let (sigma,l_interp) =
648 Evd.MonadR.List.map_right
649 (fun c sigma -> interp_constr_with_occurrences ist env sigma c) l sigma
650 in
651 sigma , Pattern l_interp
652 | Simpl (f,o) ->
653 sigma , Simpl (interp_flag ist env sigma f,
654 Option.map (interp_closed_typed_pattern_with_occurrences ist env sigma) o)
655 | CbvVm o ->
656 sigma , CbvVm (Option.map (interp_closed_typed_pattern_with_occurrences ist env sigma) o)
657 | CbvNative o ->
658 sigma , CbvNative (Option.map (interp_closed_typed_pattern_with_occurrences ist env sigma) o)
659 | (Red | Hnf | ExtraRedExpr _ | UserRed _ as r) -> sigma , r
660
661 let interp_constr env sigma c =
662 let flags = Pretyping.all_and_fail_flags in
663 Pretyping.understand_ltac flags env sigma Glob_ops.empty_lvar WithoutTypeConstraint c
664
665 let interp_constr_list env sigma c =
666 let sigma, c = interp_constr env sigma c in
667 sigma, [c]
668
669 let interp_pattern env sigma c =
670 let flags = { Pretyping.no_classes_no_fail_inference_flags with expand_evars = false } in
671 let sigma, c = Pretyping.understand_ltac flags env sigma Glob_ops.empty_lvar WithoutTypeConstraint c in
672 Patternops.legacy_bad_pattern_of_constr env sigma c
673
674 let without_ltac = {
675 interp_occurrence_var = (fun lid ->
676 user_err ?loc:lid.loc (str "Unbound variable " ++ Id.print lid.v ++ str "."));
677 interp_constr;
678 interp_constr_list;
679 interp_evaluable = (fun _ _ x -> x);
680 interp_pattern;
681 interp_evaluable_or_pattern = (fun _ _ r -> Inl r);
682 }
683
684 end
685
686 let interp_redexp_no_ltac env sigma r =
687 let r = Intern.(intern_red_expr (from_env env) r) in
688 Interp.(interp_red_expr without_ltac) env sigma r