Debian Codegraph @

plugins/ltac/tacintern.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 CAst
14 open Glob_term
15 open Util
16 open Names
17 open Libnames
18 open Smartlocate
19 open Constrexpr
20 open Termops
21 open Tacexpr
22 open Genarg
23 open Stdarg
24 open Tacarg
25 open Namegen
26 open Tactypes
27 open Tactics
28 open Locus
29
30 (** Globalization of tactic expressions :
31 Conversion from [raw_tactic_expr] to [glob_tactic_expr] *)
32
33 let error_tactic_expected ?loc =
34 user_err ?loc (str "Tactic expected.")
35
36 (** Generic arguments *)
37
38 type glob_sign = Genintern.glob_sign = {
39 ltacvars : Id.Set.t;
40 (* ltac variables and the subset of vars introduced by Intro/Let/... *)
41 genv : Environ.env;
42 extra : Genintern.Store.t;
43 intern_sign : Genintern.intern_variable_status;
44 strict_check : bool;
45 }
46
47 let make_empty_glob_sign ~strict = Genintern.empty_glob_sign ~strict (Global.env ())
48
49 (* We have identifier <| global_reference <| constr *)
50
51 let find_ident id ist =
52 Id.Set.mem id ist.ltacvars ||
53 Id.List.mem id (ids_of_named_context (Environ.named_context ist.genv))
54
55 (* a "var" is a ltac var or a var introduced by an intro tactic *)
56 let find_var id ist = Id.Set.mem id ist.ltacvars
57
58 let find_hyp id ist =
59 Id.List.mem id (ids_of_named_context (Environ.named_context ist.genv))
60
61 (* Globalize a name introduced by Intro/LetTac/... ; it is allowed to *)
62 (* be fresh in which case it is binding later on *)
63 let intern_ident s ist id =
64 (* We use identifier both for variables and new names; thus nothing to do *)
65 if not (find_ident id ist) then s := Id.Set.add id !s;
66 id
67
68 let intern_name l ist = function
69 | Anonymous -> Anonymous
70 | Name id -> Name (intern_ident l ist id)
71
72 (* Globalize a name which must be bound -- actually just check it is bound *)
73 let intern_hyp ist ({loc;v=id} as locid) =
74 if not ist.strict_check then
75 locid
76 else if find_ident id ist then
77 make id
78 else
79 CErrors.user_err ?loc Pp.(str "Hypothesis" ++ spc () ++ Id.print id ++ spc() ++
80 str "was not found in the current environment.")
81
82 let intern_or_var f ist = function
83 | ArgVar locid -> ArgVar (intern_hyp ist locid)
84 | ArgArg x -> ArgArg (f x)
85
86 let intern_int_or_var = intern_or_var (fun (n : int) -> n)
87 let intern_string_or_var = intern_or_var (fun (s : string) -> s)
88
89 let intern_global_reference ist qid =
90 if qualid_is_ident qid && find_var (qualid_basename qid) ist then
91 ArgVar (make ?loc:qid.CAst.loc @@ qualid_basename qid)
92 else if qualid_is_ident qid && find_hyp (qualid_basename qid) ist then
93 let id = qualid_basename qid in
94 ArgArg (qid.CAst.loc, GlobRef.VarRef id)
95 else
96 let r =
97 try locate_global_with_alias ~head:true qid
98 with
99 | Not_found as exn ->
100 if not ist.strict_check && qualid_is_ident qid then
101 let id = qualid_basename qid in
102 GlobRef.VarRef id
103 else
104 let _, info = Exninfo.capture exn in
105 Nametab.error_global_not_found ~info qid
106 in
107 ArgArg (qid.CAst.loc, r)
108
109 let intern_ltac_variable ist qid =
110 if qualid_is_ident qid && find_var (qualid_basename qid) ist then
111 (* A local variable of any type *)
112 ArgVar (make ?loc:qid.CAst.loc @@ qualid_basename qid)
113 else raise Not_found
114
115 let intern_constr_reference ist qid =
116 let id = qualid_basename qid in
117 if qualid_is_ident qid && not ist.strict_check && find_hyp (qualid_basename qid) ist then
118 (DAst.make @@ GVar id), Some (make @@ CRef (qid,None))
119 else if qualid_is_ident qid && find_var (qualid_basename qid) ist then
120 (DAst.make @@ GVar id), if ist.strict_check then None else Some (make @@ CRef (qid,None))
121 else
122 DAst.make @@ GRef (locate_global_with_alias qid,None),
123 if ist.strict_check then None else Some (make @@ CRef (qid,None))
124
125 (* Internalize an isolated reference in position of tactic *)
126
127 let warn_deprecated_tactic =
128 Deprecation.create_warning ~object_name:"Tactic"
129 ~warning_name_if_no_since:"deprecated-tactic"
130 pr_qualid
131
132 let warn_deprecated_alias =
133 Deprecation.create_warning ~object_name:"Tactic Notation"
134 ~warning_name_if_no_since:"deprecated-tactic-notation"
135 Pptactic.pr_alias_key
136
137 let intern_isolated_global_tactic_reference qid =
138 let loc = qid.CAst.loc in
139 let kn = Tacenv.locate_tactic qid in
140 Option.iter (fun depr -> warn_deprecated_tactic ?loc (qid,depr)) @@
141 Tacenv.tac_deprecation kn;
142 TacCall (CAst.make ?loc (ArgArg (loc,kn),[]))
143
144 let intern_isolated_tactic_reference ist qid =
145 (* An ltac reference *)
146 try Reference (intern_ltac_variable ist qid)
147 with Not_found ->
148 (* A global tactic *)
149 try intern_isolated_global_tactic_reference qid
150 with Not_found ->
151 (* Tolerance for compatibility, allow not to use "constr:" *)
152 try ConstrMayEval (ConstrTerm (intern_constr_reference ist qid))
153 with Not_found as exn ->
154 (* Reference not found *)
155 let _, info = Exninfo.capture exn in
156 Nametab.error_global_not_found ~info qid
157
158 (* Internalize an applied tactic reference *)
159
160 let intern_applied_global_tactic_reference qid =
161 let loc = qid.CAst.loc in
162 let kn = Tacenv.locate_tactic qid in
163 Option.iter (fun depr -> warn_deprecated_tactic ?loc (qid,depr)) @@
164 Tacenv.tac_deprecation kn;
165 ArgArg (loc,kn)
166
167 let intern_applied_tactic_reference ist qid =
168 (* An ltac reference *)
169 try intern_ltac_variable ist qid
170 with Not_found ->
171 (* A global tactic *)
172 try intern_applied_global_tactic_reference qid
173 with Not_found as exn ->
174 (* Reference not found *)
175 let _, info = Exninfo.capture exn in
176 Nametab.error_global_not_found ~info qid
177
178 (* Intern a reference parsed in a non-tactic entry *)
179
180 let intern_non_tactic_reference ist qid =
181 (* An ltac reference *)
182 try Reference (intern_ltac_variable ist qid)
183 with Not_found ->
184 (* A constr reference *)
185 try ConstrMayEval (ConstrTerm (intern_constr_reference ist qid))
186 with Not_found ->
187 (* Tolerance for compatibility, allow not to use "ltac:" *)
188 try intern_isolated_global_tactic_reference qid
189 with Not_found as exn ->
190 (* By convention, use IntroIdentifier for unbound ident, when not in a def *)
191 if qualid_is_ident qid && not ist.strict_check then
192 let id = qualid_basename qid in
193 let ipat = in_gen (glbwit wit_intropattern) (make ?loc:qid.CAst.loc @@ IntroNaming (IntroIdentifier id)) in
194 TacGeneric (None,ipat)
195 else
196 (* Reference not found *)
197 let _, info = Exninfo.capture exn in
198 Nametab.error_global_not_found ~info qid
199
200 let intern_message_token ist = function
201 | (MsgString _ | MsgInt _ as x) -> x
202 | MsgIdent id -> MsgIdent (intern_hyp ist id)
203
204 let intern_message ist = List.map (intern_message_token ist)
205
206 let intern_quantified_hypothesis ist = function
207 | AnonHyp n -> AnonHyp n
208 | NamedHyp id ->
209 (* Uncomment to disallow "intros until n" in ltac when n is not bound *)
210 NamedHyp ((*snd (intern_hyp ist (dloc,*)id(* ))*))
211
212 let intern_binding_name ist x =
213 (* We use identifier both for variables and binding names *)
214 (* Todo: consider the body of the lemma to which the binding refer
215 and if a term w/o ltac vars, check the name is indeed quantified *)
216 x
217
218 let intern_constr_gen pattern_mode isarity {ltacvars=lfun; genv=env; extra; intern_sign; strict_check} c =
219 let scope = if isarity then Pretyping.IsType else Pretyping.WithoutTypeConstraint in
220 let ltacvars = {
221 Constrintern.ltac_vars = lfun;
222 ltac_bound = Id.Set.empty;
223 ltac_extra = extra;
224 } in
225 let c' = Constrintern.intern_core scope ~strict_check ~pattern_mode ~ltacvars env Evd.(from_env env) intern_sign c in
226 (c',if strict_check then None else Some c)
227
228 let intern_constr = intern_constr_gen false false
229 let intern_type = intern_constr_gen false true
230
231 (* Globalize bindings *)
232 let intern_binding ist = map (fun (b,c) ->
233 intern_binding_name ist b,intern_constr ist c)
234
235 let intern_bindings ist = function
236 | NoBindings -> NoBindings
237 | ImplicitBindings l -> ImplicitBindings (List.map (intern_constr ist) l)
238 | ExplicitBindings l -> ExplicitBindings (List.map (intern_binding ist) l)
239
240 let intern_constr_with_bindings ist (c,bl) =
241 (intern_constr ist c, intern_bindings ist bl)
242
243 let intern_constr_with_bindings_arg ist (clear,c) =
244 (clear,intern_constr_with_bindings ist c)
245
246 let rec intern_intro_pattern lf ist = map (function
247 | IntroNaming pat ->
248 IntroNaming (intern_intro_pattern_naming lf ist pat)
249 | IntroAction pat ->
250 IntroAction (intern_intro_pattern_action lf ist pat)
251 | IntroForthcoming _ as x -> x)
252
253 and intern_intro_pattern_naming lf ist = function
254 | IntroIdentifier id ->
255 IntroIdentifier (intern_ident lf ist id)
256 | IntroFresh id ->
257 IntroFresh (intern_ident lf ist id)
258 | IntroAnonymous as x -> x
259
260 and intern_intro_pattern_action lf ist = function
261 | IntroOrAndPattern l ->
262 IntroOrAndPattern (intern_or_and_intro_pattern lf ist l)
263 | IntroInjection l ->
264 IntroInjection (List.map (intern_intro_pattern lf ist) l)
265 | IntroWildcard | IntroRewrite _ as x -> x
266 | IntroApplyOn ({loc;v=c},pat) ->
267 IntroApplyOn (make ?loc @@ intern_constr ist c, intern_intro_pattern lf ist pat)
268
269 and intern_or_and_intro_pattern lf ist = function
270 | IntroAndPattern l ->
271 IntroAndPattern (List.map (intern_intro_pattern lf ist) l)
272 | IntroOrPattern ll ->
273 IntroOrPattern (List.map (List.map (intern_intro_pattern lf ist)) ll)
274
275 let intern_or_and_intro_pattern_loc lf ist = function
276 | ArgVar {v=id} as x ->
277 if find_var id ist then x
278 else user_err Pp.(str "Disjunctive/conjunctive introduction pattern expected.")
279 | ArgArg ll -> ArgArg (map (fun l -> intern_or_and_intro_pattern lf ist l) ll)
280
281 let intern_intro_pattern_naming_loc lf ist = map (fun pat ->
282 intern_intro_pattern_naming lf ist pat)
283
284 (* TODO: catch ltac vars *)
285 let intern_destruction_arg ist = function
286 | clear,ElimOnConstr c -> clear,ElimOnConstr (intern_constr_with_bindings ist c)
287 | clear,ElimOnAnonHyp n as x -> x
288 | clear,ElimOnIdent {loc;v=id} ->
289 if ist.strict_check then
290 (* If in a defined tactic, no intros-until *)
291 let c, p = intern_constr ist (make @@ CRef (qualid_of_ident id, None)) in
292 match DAst.get c with
293 | GVar id -> clear,ElimOnIdent (make ?loc:c.loc id)
294 | _ -> clear,ElimOnConstr ((c, p), NoBindings)
295 else
296 clear,ElimOnIdent (make ?loc id)
297
298 let evalref_of_globref ?loc r =
299 let () =
300 (* only dump section variables not proof context variables
301 (broken if variables got renamed) *)
302 let is_proof_variable = match r with
303 | GlobRef.VarRef x -> (try ignore (Global.lookup_named x); false with Not_found -> true)
304 | _ -> false
305 in
306 if not is_proof_variable then Dumpglob.add_glob ?loc r
307 in
308 Tacred.soft_evaluable_of_global_reference ?loc r
309
310 let intern_smart_global ist = function
311 | {v=AN r} -> intern_global_reference ist r
312 | {v=ByNotation (ntn,sc);loc} ->
313 ArgArg (loc, (Notation.interp_notation_as_global_reference ?loc ~head:true
314 GlobRef.(function ConstRef _ | VarRef _ -> true | _ -> false) ntn sc))
315
316 let intern_constr_with_occurrences ist (l,c) = (l,intern_constr ist c)
317
318 let intern_constr_pattern ist ~as_type ~ltacvars pc =
319 let ltacvars = {
320 Constrintern.ltac_vars = ltacvars;
321 ltac_bound = Id.Set.empty;
322 ltac_extra = ist.extra;
323 } in
324 let metas,pat = Constrintern.intern_constr_pattern
325 ist.genv Evd.(from_env ist.genv) ~as_type ~ltacvars pc
326 in
327 let (glob,_ as c) = intern_constr_gen true false ist pc in
328 let bound_names = Glob_ops.bound_glob_vars glob in
329 metas,(bound_names,c,pat)
330
331 let intern_typed_pattern ist p =
332 (* we cannot ensure in non strict mode that the pattern is closed *)
333 (* keeping a constr_expr copy is too complicated and we want anyway to *)
334 (* type it, so we remember the pattern as a glob_constr only *)
335 let metas =
336 if ist.strict_check then
337 let ltacvars = {
338 Constrintern.ltac_vars = ist.ltacvars;
339 ltac_bound = Id.Set.empty;
340 ltac_extra = ist.extra;
341 } in
342 fst @@ Constrintern.intern_constr_pattern ist.genv Evd.(from_env ist.genv) ~ltacvars p
343 else
344 Id.Set.empty in
345 let c = intern_constr_gen true false ist p in
346 metas,c
347
348 let intern_local_evalref ist qid =
349 if qualid_is_ident qid && find_var (qualid_basename qid) ist then
350 Some (ArgVar (make ?loc:qid.CAst.loc @@ qualid_basename qid))
351 else if qualid_is_ident qid && find_hyp (qualid_basename qid) ist then
352 let id = qualid_basename qid in
353 let loc = qid.loc in
354 let r = GlobRef.VarRef id in
355 let r = evalref_of_globref ?loc r in
356 let short = if ist.strict_check then None else Some (CAst.make ?loc id) in
357 Some (ArgArg (r, short))
358 else None
359
360 let intern_red_expr ist r =
361 let ltac_sign = {
362 Constrintern.ltac_vars = ist.ltacvars;
363 ltac_bound = Id.Set.empty;
364 ltac_extra = ist.extra;
365 }
366 in
367 let ist = {
368 Redexpr.Intern.strict_check = ist.strict_check;
369 local_ref = intern_local_evalref ist;
370 global_ref = (fun ?short r -> ArgArg (r, short));
371 intern_constr = intern_constr ist;
372 ltac_sign;
373 pattern_of_glob = (fun c -> (c, None));
374 intern_pattern = (fun c -> snd @@ intern_typed_pattern ist c);
375 }
376 in
377 Redexpr.Intern.intern_red_expr ist r
378
379 let intern_hyp_list ist = List.map (intern_hyp ist)
380
381 let intern_in_hyp_as ist lf (idl,ipat) =
382 (intern_hyp ist idl, Option.map (intern_intro_pattern lf ist) ipat)
383
384 let intern_inversion_strength lf ist = function
385 | NonDepInversion (k,idl,ids) ->
386 NonDepInversion (k,intern_hyp_list ist idl,
387 Option.map (intern_or_and_intro_pattern_loc lf ist) ids)
388 | DepInversion (k,copt,ids) ->
389 DepInversion (k, Option.map (intern_constr ist) copt,
390 Option.map (intern_or_and_intro_pattern_loc lf ist) ids)
391 | InversionUsing (c,idl) ->
392 InversionUsing (intern_constr ist c, intern_hyp_list ist idl)
393
394 (* Interprets an hypothesis name *)
395 let intern_hyp_location ist ((occs,id),hl) =
396 ((Locusops.occurrences_map (List.map (intern_int_or_var ist)) occs,
397 intern_hyp ist id), hl)
398
399 (* Reads a pattern *)
400 let intern_pattern ist ?(as_type=false) ltacvars = function
401 | Subterm (ido,pc) ->
402 let (metas,pc) = intern_constr_pattern ist ~as_type:false ~ltacvars pc in
403 ido, metas, Subterm (ido,pc)
404 | Term pc ->
405 let (metas,pc) = intern_constr_pattern ist ~as_type ~ltacvars pc in
406 None, metas, Term pc
407
408 let intern_constr_may_eval ist = function
409 | ConstrEval (r,c) -> ConstrEval (intern_red_expr ist r,intern_constr ist c)
410 | ConstrContext (locid,c) ->
411 ConstrContext (intern_hyp ist locid,intern_constr ist c)
412 | ConstrTypeOf c -> ConstrTypeOf (intern_constr ist c)
413 | ConstrTerm c -> ConstrTerm (intern_constr ist c)
414
415 let name_cons accu = function
416 | Anonymous -> accu
417 | Name id -> Id.Set.add id accu
418
419 let opt_cons accu = function
420 | None -> accu
421 | Some id -> Id.Set.add id accu
422
423 (* Reads the hypotheses of a "match goal" rule *)
424 let rec intern_match_goal_hyps ist ?(as_type=false) lfun = function
425 | (Hyp ({v=na} as locna,mp))::tl ->
426 let ido, metas1, pat = intern_pattern ist ~as_type:true lfun mp in
427 let lfun, metas2, hyps = intern_match_goal_hyps ist lfun tl in
428 let lfun' = name_cons (opt_cons lfun ido) na in
429 lfun', Id.Set.union metas1 metas2, Hyp (locna,pat)::hyps
430 | (Def ({v=na} as locna,mv,mp))::tl ->
431 let ido, metas1, patv = intern_pattern ist ~as_type:false lfun mv in
432 let ido', metas2, patt = intern_pattern ist ~as_type:true lfun mp in
433 let lfun, metas3, hyps = intern_match_goal_hyps ist ~as_type lfun tl in
434 let lfun' = name_cons (opt_cons (opt_cons lfun ido) ido') na in
435 lfun', Id.Set.union metas1 (Id.Set.union metas2 metas3), Def (locna,patv,patt)::hyps
436 | [] -> lfun, Id.Set.empty, []
437
438 (* Utilities *)
439 let extract_let_names lrc =
440 let fold accu ({loc;v=name}, _) =
441 Nameops.Name.fold_right (fun id accu ->
442 if Id.Set.mem id accu then
443 user_err ?loc (str "This variable is bound several times.")
444 else Id.Set.add id accu)
445 name
446 accu
447 in
448 List.fold_left fold Id.Set.empty lrc
449
450 let clause_app f = function
451 { onhyps=None; concl_occs=nl } ->
452 { onhyps=None; concl_occs=nl }
453 | { onhyps=Some l; concl_occs=nl } ->
454 { onhyps=Some(List.map f l); concl_occs=nl}
455
456 (* Globalizes tactics : raw_tactic_expr -> glob_tactic_expr *)
457 let rec intern_atomic lf ist x =
458 match (x:raw_atomic_tactic_expr) with
459 (* Basic tactics *)
460 | TacIntroPattern (ev,l) ->
461 TacIntroPattern (ev,List.map (intern_intro_pattern lf ist) l)
462 | TacApply (a,ev,cb,inhyp) ->
463 TacApply (a,ev,List.map (intern_constr_with_bindings_arg ist) cb,
464 List.map (intern_in_hyp_as ist lf) inhyp)
465 | TacElim (ev,cb,cbo) ->
466 TacElim (ev,intern_constr_with_bindings_arg ist cb,
467 Option.map (intern_constr_with_bindings ist) cbo)
468 | TacCase (ev,cb) -> TacCase (ev,intern_constr_with_bindings_arg ist cb)
469 | TacMutualFix (id,n,l) ->
470 let f (id,n,c) = (intern_ident lf ist id,n,intern_type ist c) in
471 TacMutualFix (intern_ident lf ist id, n, List.map f l)
472 | TacMutualCofix (id,l) ->
473 let f (id,c) = (intern_ident lf ist id,intern_type ist c) in
474 TacMutualCofix (intern_ident lf ist id, List.map f l)
475 | TacAssert (ev,b,otac,ipat,c) ->
476 TacAssert (ev,b,Option.map (Option.map (intern_pure_tactic ist)) otac,
477 Option.map (intern_intro_pattern lf ist) ipat,
478 intern_constr_gen false (not (Option.is_empty otac)) ist c)
479 | TacGeneralize cl ->
480 TacGeneralize (List.map (fun (c,na) ->
481 intern_constr_with_occurrences ist c,
482 intern_name lf ist na) cl)
483 | TacLetTac (ev,na,c,cls,b,eqpat) ->
484 let na = intern_name lf ist na in
485 TacLetTac (ev,na,intern_constr ist c,
486 (clause_app (intern_hyp_location ist) cls),b,
487 (Option.map (intern_intro_pattern_naming_loc lf ist) eqpat))
488
489 (* Derived basic tactics *)
490 | TacInductionDestruct (ev,isrec,(l,el)) ->
491 TacInductionDestruct (ev,isrec,(List.map (fun (c,(ipato,ipats),cls) ->
492 (intern_destruction_arg ist c,
493 (Option.map (intern_intro_pattern_naming_loc lf ist) ipato,
494 Option.map (intern_or_and_intro_pattern_loc lf ist) ipats),
495 Option.map (clause_app (intern_hyp_location ist)) cls)) l,
496 Option.map (intern_constr_with_bindings ist) el))
497 (* Conversion *)
498 | TacReduce (r,cl) ->
499 TacReduce (intern_red_expr ist r, clause_app (intern_hyp_location ist) cl)
500 | TacChange (check,None,c,cl) ->
501 let is_onhyps = match cl.onhyps with
502 | None | Some [] -> true
503 | _ -> false
504 in
505 let is_onconcl = match cl.concl_occs with
506 | AtLeastOneOccurrence | AllOccurrences | NoOccurrences -> true
507 | _ -> false
508 in
509 TacChange (check,None,
510 (if is_onhyps && is_onconcl
511 then intern_type ist c else intern_constr ist c),
512 clause_app (intern_hyp_location ist) cl)
513 | TacChange (check,Some p,c,cl) ->
514 let metas,pat = intern_typed_pattern ist p in
515 let ltacvars = Id.Set.union ist.ltacvars metas in
516 let ist' = { ist with ltacvars } in
517 TacChange (check,Some pat,intern_constr ist' c,
518 clause_app (intern_hyp_location ist) cl)
519
520 (* Equality and inversion *)
521 | TacRewrite (ev,l,cl,by) ->
522 TacRewrite
523 (ev,
524 List.map (fun (b,m,c) -> (b,m,intern_constr_with_bindings_arg ist c)) l,
525 clause_app (intern_hyp_location ist) cl,
526 Option.map (intern_pure_tactic ist) by)
527 | TacInversion (inv,hyp) ->
528 TacInversion (intern_inversion_strength lf ist inv,
529 intern_quantified_hypothesis ist hyp)
530
531 and intern_tactic onlytac ist tac = snd (intern_tactic_seq onlytac ist tac)
532
533 and intern_tactic_seq onlytac ist tac =
534 let (loc, tac) = CAst.(tac.loc, tac.v) in
535 match tac with
536 | TacAtom t ->
537 let lf = ref ist.ltacvars in
538 let t = intern_atomic lf ist t in
539 !lf, CAst.make ?loc (TacAtom t)
540 | TacFun tacfun -> ist.ltacvars, (CAst.make ?loc (TacFun (intern_tactic_fun ist tacfun)))
541 | TacLetIn (isrec,l,u) ->
542 let ltacvars = Id.Set.union (extract_let_names l) ist.ltacvars in
543 let ist' = { ist with ltacvars } in
544 let l = List.map (fun (n,b) ->
545 (n,intern_tacarg false (if isrec then ist' else ist) b)) l in
546 ist.ltacvars, CAst.make ?loc (TacLetIn (isrec,l,intern_tactic onlytac ist' u))
547
548 | TacMatchGoal (lz,lr,lmr) ->
549 ist.ltacvars, CAst.make ?loc (TacMatchGoal (lz,lr, intern_match_rule onlytac ist ~as_type:true lmr))
550 | TacMatch (lz,c,lmr) ->
551 ist.ltacvars,
552 CAst.make ?loc (TacMatch (lz,intern_tactic_or_tacarg ist c,intern_match_rule onlytac ist lmr))
553 | TacId l -> ist.ltacvars, CAst.make ?loc (TacId (intern_message ist l))
554 | TacFail (g,n,l) ->
555 ist.ltacvars, CAst.make ?loc (TacFail (g,intern_int_or_var ist n,intern_message ist l))
556 | TacProgress tac -> ist.ltacvars, CAst.make ?loc (TacProgress (intern_pure_tactic ist tac))
557 | TacAbstract (tac,s) ->
558 ist.ltacvars, CAst.make ?loc (TacAbstract (intern_pure_tactic ist tac,s))
559 | TacThen (t1,t2) ->
560 let lfun', t1 = intern_tactic_seq onlytac ist t1 in
561 let lfun'', t2 = intern_tactic_seq onlytac { ist with ltacvars = lfun' } t2 in
562 lfun'', CAst.make ?loc (TacThen (t1,t2))
563 | TacDispatch tl ->
564 ist.ltacvars , CAst.make ?loc (TacDispatch (List.map (intern_pure_tactic ist) tl))
565 | TacExtendTac (tf,t,tl) ->
566 ist.ltacvars, CAst.make ?loc (
567 TacExtendTac (Array.map (intern_pure_tactic ist) tf,
568 intern_pure_tactic ist t,
569 Array.map (intern_pure_tactic ist) tl))
570 | TacThens3parts (t1,tf,t2,tl) ->
571 let lfun', t1 = intern_tactic_seq onlytac ist t1 in
572 let ist' = { ist with ltacvars = lfun' } in
573 (* Que faire en cas de (tac complexe avec Match et Thens; tac2) ?? *)
574 lfun', CAst.make ?loc (TacThens3parts
575 (t1,Array.map (intern_pure_tactic ist') tf,intern_pure_tactic ist' t2,
576 Array.map (intern_pure_tactic ist') tl))
577 | TacThens (t,tl) ->
578 let lfun', t = intern_tactic_seq true ist t in
579 let ist' = { ist with ltacvars = lfun' } in
580 (* Que faire en cas de (tac complexe avec Match et Thens; tac2) ?? *)
581 lfun', CAst.make ?loc (TacThens (t, List.map (intern_pure_tactic ist') tl))
582 | TacDo (n,tac) ->
583 ist.ltacvars, CAst.make ?loc (TacDo (intern_int_or_var ist n,intern_pure_tactic ist tac))
584 | TacTry tac -> ist.ltacvars, CAst.make ?loc (TacTry (intern_pure_tactic ist tac))
585 | TacRepeat tac -> ist.ltacvars, CAst.make ?loc (TacRepeat (intern_pure_tactic ist tac))
586 | TacTimeout (n,tac) ->
587 ist.ltacvars, CAst.make ?loc (TacTimeout (intern_int_or_var ist n,intern_tactic onlytac ist tac))
588 | TacTime (s,tac) ->
589 ist.ltacvars, CAst.make ?loc (TacTime (s,intern_tactic onlytac ist tac))
590 | TacOr (tac1,tac2) ->
591 ist.ltacvars, CAst.make ?loc (TacOr (intern_pure_tactic ist tac1,intern_pure_tactic ist tac2))
592 | TacOnce tac ->
593 ist.ltacvars, CAst.make ?loc (TacOnce (intern_pure_tactic ist tac))
594 | TacExactlyOnce tac ->
595 ist.ltacvars, CAst.make ?loc (TacExactlyOnce (intern_pure_tactic ist tac))
596 | TacIfThenCatch (tac,tact,tace) ->
597 ist.ltacvars,
598 CAst.make ?loc (TacIfThenCatch (
599 intern_pure_tactic ist tac,
600 intern_pure_tactic ist tact,
601 intern_pure_tactic ist tace))
602 | TacOrelse (tac1,tac2) ->
603 ist.ltacvars, CAst.make ?loc (TacOrelse (intern_pure_tactic ist tac1,intern_pure_tactic ist tac2))
604 | TacFirst l -> ist.ltacvars, CAst.make ?loc (TacFirst (List.map (intern_pure_tactic ist) l))
605 | TacSolve l -> ist.ltacvars, CAst.make ?loc (TacSolve (List.map (intern_pure_tactic ist) l))
606 | TacArg a -> ist.ltacvars, intern_tactic_as_arg loc onlytac ist a
607 | TacSelect (sel, tac) ->
608 ist.ltacvars, CAst.make ?loc (TacSelect (sel, intern_pure_tactic ist tac))
609
610 (* For extensions *)
611 | TacAlias (s,l) ->
612 let alias = Tacenv.interp_alias s in
613 Option.iter (fun o -> warn_deprecated_alias ?loc (s,o)) @@ alias.Tacenv.alias_deprecation;
614 let l = List.map (intern_tacarg false ist) l in
615 ist.ltacvars, CAst.make ?loc (TacAlias (s,l))
616 | TacML (opn,l) ->
617 let _ignore = Tacenv.interp_ml_tactic opn in
618 ist.ltacvars, CAst.make ?loc (TacML (opn,List.map (intern_tacarg false ist) l))
619
620 and intern_tactic_as_arg loc onlytac ist a =
621 match intern_tacarg onlytac ist a with
622 | TacCall _ | Reference _
623 | TacGeneric _ as a -> CAst.make ?loc (TacArg a)
624 | Tacexp a -> a
625 | ConstrMayEval _ | TacFreshId _ | TacPretype _ | TacNumgoals as a ->
626 if onlytac then error_tactic_expected ?loc else CAst.make ?loc (TacArg a)
627
628 and intern_tactic_or_tacarg ist = intern_tactic false ist
629
630 and intern_pure_tactic ist = intern_tactic true ist
631
632 and intern_tactic_fun ist (var,body) =
633 let lfun = List.fold_left name_cons ist.ltacvars var in
634 (var,intern_tactic_or_tacarg { ist with ltacvars = lfun } body)
635
636 and intern_tacarg onlytac ist = function
637 | Reference r -> intern_non_tactic_reference ist r
638 | ConstrMayEval c -> ConstrMayEval (intern_constr_may_eval ist c)
639 | TacCall { loc; v=(f,[]) } -> intern_isolated_tactic_reference ist f
640 | TacCall { loc; v=(f,l) } ->
641 TacCall (CAst.make ?loc (
642 intern_applied_tactic_reference ist f,
643 List.map (intern_tacarg false ist) l))
644 | TacFreshId x -> TacFreshId (List.map (intern_string_or_var ist) x)
645 | TacPretype c -> TacPretype (intern_constr ist c)
646 | TacNumgoals -> TacNumgoals
647 | Tacexp t -> Tacexp (intern_tactic onlytac ist t)
648 | TacGeneric (isquot,arg) ->
649 let arg = intern_genarg ist arg in
650 TacGeneric (isquot,arg)
651
652 (* Reads the rules of a Match Context or a Match *)
653 and intern_match_rule onlytac ist ?(as_type=false) = function
654 | (All tc)::tl ->
655 All (intern_tactic onlytac ist tc) :: (intern_match_rule onlytac ist ~as_type tl)
656 | (Pat (rl,mp,tc))::tl ->
657 let {ltacvars=lfun; genv=env} = ist in
658 let lfun',metas1,hyps = intern_match_goal_hyps ist ~as_type lfun rl in
659 let ido,metas2,pat = intern_pattern ist ~as_type lfun mp in
660 let ltacvars = Id.Set.union (opt_cons lfun' ido) metas1 in
661 let ltacvars = Id.Set.union ltacvars metas2 in
662 let ist' = { ist with ltacvars } in
663 Pat (hyps,pat,intern_tactic onlytac ist' tc) :: (intern_match_rule onlytac ist ~as_type tl)
664 | [] -> []
665
666 and intern_genarg ist (GenArg (Rawwit wit, x)) =
667 match wit with
668 | ListArg wit ->
669 let map x =
670 let ans = intern_genarg ist (in_gen (rawwit wit) x) in
671 out_gen (glbwit wit) ans
672 in
673 in_gen (glbwit (wit_list wit)) (List.map map x)
674 | OptArg wit ->
675 let ans = match x with
676 | None -> in_gen (glbwit (wit_opt wit)) None
677 | Some x ->
678 let s = out_gen (glbwit wit) (intern_genarg ist (in_gen (rawwit wit) x)) in
679 in_gen (glbwit (wit_opt wit)) (Some s)
680 in
681 ans
682 | PairArg (wit1, wit2) ->
683 let p, q = x in
684 let p = out_gen (glbwit wit1) (intern_genarg ist (in_gen (rawwit wit1) p)) in
685 let q = out_gen (glbwit wit2) (intern_genarg ist (in_gen (rawwit wit2) q)) in
686 in_gen (glbwit (wit_pair wit1 wit2)) (p, q)
687 | ExtraArg s ->
688 snd (Genintern.generic_intern ist (in_gen (rawwit wit) x))
689
690 (* ntnvars are also in ltacvars so using Genintern.with_used_ntnvars
691 may not see them even if they are used *)
692 let used_all_ntnvars ntnvars =
693 let set_used status = match status.Genintern.ntnvar_used with
694 | [] | true :: _ -> ()
695 | false :: rest -> status.ntnvar_used <- true :: rest
696 in
697 let ntnvars = Id.Map.filter (fun _ status -> match status.Genintern.ntnvar_typ with
698 | NtnInternTypeAny _ -> set_used status; true
699 | NtnInternTypeOnlyBinder -> false)
700 ntnvars
701 in
702 Id.Map.domain ntnvars
703
704 let intern_ltac_in_term ist tac =
705 let tac = intern_tactic_or_tacarg ist tac in
706 used_all_ntnvars ist.intern_sign.notation_variable_status, tac
707
708 (** Other entry points *)
709
710 let glob_tactic x =
711 intern_pure_tactic (make_empty_glob_sign ~strict:true) x
712
713 let glob_tactic_env l env x =
714 let ltacvars =
715 List.fold_left (fun accu x -> Id.Set.add x accu) Id.Set.empty l in
716 intern_pure_tactic { (Genintern.empty_glob_sign ~strict:true env) with ltacvars } x
717
718 let intern_strategy ist s =
719 let rec aux stratvars = function
720 | Rewrite.StratVar x ->
721 (* We could make this whole branch assert false, since it's
722 unreachable except from plugins. But maybe it's useful if any
723 plug-in wants to craft a strategy by hand. *)
724 if Id.Set.mem x.v stratvars then Rewrite.StratVar x.v
725 else CErrors.user_err ?loc:x.loc Pp.(str "Unbound strategy" ++ spc() ++ Id.print x.v)
726 | StratConstr ({ v = CRef (qid, None) }, true) when idset_mem_qualid qid stratvars ->
727 let (_, x) = repr_qualid qid in Rewrite.StratVar x
728 | StratConstr (c, b) -> StratConstr (intern_constr ist c, b)
729 | StratFix (x, s) -> StratFix (x.v, aux (Id.Set.add x.v stratvars) s)
730 | StratId | StratFail | StratRefl as s -> s
731 | StratUnary (s, str) -> StratUnary (s, aux stratvars str)
732 | StratBinary (s, str, str') -> StratBinary (s, aux stratvars str, aux stratvars str')
733 | StratNAry (s, strs) -> StratNAry (s, List.map (aux stratvars) strs)
734 | StratTerms l -> StratTerms (List.map (intern_constr ist) l)
735 | StratHints (b, id) -> StratHints (b, id)
736 | StratEval r -> StratEval (intern_red_expr ist r)
737 | StratFold c -> StratFold (intern_constr ist c)
738 in
739 aux Id.Set.empty s
740
741 (** Registering *)
742
743 let lift intern = (); fun ist x -> (ist, intern ist x)
744
745 let () =
746 let intern_intro_pattern ist pat =
747 let lf = ref Id.Set.empty in
748 let ans = intern_intro_pattern lf ist pat in
749 let ist = { ist with ltacvars = !lf } in
750 (ist, ans)
751 in
752 Genintern.register_intern0 wit_intropattern intern_intro_pattern [@warning "-3"];
753 Genintern.register_intern0 wit_simple_intropattern intern_intro_pattern
754
755 let () =
756 let intern_clause ist cl =
757 let ans = clause_app (intern_hyp_location ist) cl in
758 (ist, ans)
759 in
760 Genintern.register_intern0 wit_clause_dft_concl intern_clause
761
762 let intern_ident' ist id =
763 let lf = ref Id.Set.empty in
764 (ist, intern_ident lf ist id)
765
766 let intern_ltac ist tac =
767 intern_pure_tactic ist tac
768
769 let () =
770 Genintern.register_intern0 wit_int_or_var (lift intern_int_or_var);
771 Genintern.register_intern0 wit_nat_or_var (lift intern_int_or_var);
772 Genintern.register_intern0 wit_smart_global (lift intern_smart_global);
773 Genintern.register_intern0 wit_ref (lift intern_global_reference);
774 Genintern.register_intern0 wit_pre_ident (fun ist c -> (ist,c));
775 Genintern.register_intern0 wit_ident intern_ident';
776 Genintern.register_intern0 wit_hyp (lift intern_hyp);
777 Genintern.register_intern0 wit_tactic (lift intern_tactic_or_tacarg);
778 Genintern.register_intern0 wit_ltac_in_term (lift intern_ltac_in_term);
779 Genintern.register_intern0 wit_ltac (lift intern_ltac);
780 Genintern.register_intern0 wit_quant_hyp (lift intern_quantified_hypothesis);
781 Genintern.register_intern0 wit_constr (fun ist c -> (ist,intern_constr ist c));
782 Genintern.register_intern0 wit_uconstr (fun ist c -> (ist,intern_constr ist c));
783 Genintern.register_intern0 wit_open_constr (fun ist c -> (ist,intern_constr ist c));
784 Genintern.register_intern0 Redexpr.wit_red_expr (lift intern_red_expr);
785 Genintern.register_intern0 wit_bindings (lift intern_bindings);
786 Genintern.register_intern0 wit_constr_with_bindings (lift intern_constr_with_bindings);
787 Genintern.register_intern0 wit_destruction_arg (lift intern_destruction_arg);
788 ()
789
790 (** Substitution for notations containing tactic-in-terms *)
791
792 let notation_subst ntnvars bindings (used_ntnvars,tac) =
793 let fold id accu =
794 match bindings id with
795 | exception (Nametab.GlobalizationError _) | None -> accu
796 | Some c ->
797 let loc = Glob_ops.loc_of_glob_constr c in
798 let c = ConstrMayEval (ConstrTerm (c, None)) in
799 (make ?loc @@ Name id, c) :: accu
800 in
801 let bindings = Id.Set.fold fold used_ntnvars [] in
802 (* This is theoretically not correct due to potential variable
803 capture, but Ltac has no true variables so one cannot simply
804 substitute *)
805 if List.is_empty bindings then Id.Set.empty, tac
806 else used_all_ntnvars ntnvars, CAst.make (TacLetIn (false, bindings, tac))
807
808 let () = Genintern.register_ntn_subst0 wit_ltac_in_term notation_subst