Debian Codegraph @

plugins/ltac2/tac2extravals.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 Util
12 open Pp
13 open Names
14 open Genarg
15 open Tac2ffi
16 open Tac2env
17 open Tac2expr
18 open Tac2entries.Pltac
19 open Proofview.Notations
20
21 open Tac2quote.Refs
22
23 let return x = Proofview.tclUNIT x
24
25 (** ML types *)
26
27 (** Embed all Ltac2 data into Values *)
28 let to_lvar ist =
29 let open Glob_ops in
30 let lfun = Tac2interp.set_env ist Id.Map.empty in
31 { empty_lvar with Ltac_pretype.ltac_genargs = lfun }
32
33 let gtypref kn = GTypRef (Other kn, [])
34
35 let of_glob_constr (c:Glob_term.glob_constr) =
36 match DAst.get c with
37 | GGenarg (GenArg (Glbwit tag, v)) ->
38 begin match genarg_type_eq tag wit_ltac2_var_quotation with
39 | Some Refl ->
40 begin match (fst v) with
41 | ConstrVar -> GlbTacexpr (GTacVar (snd v))
42 | _ -> GlbVal c
43 end
44 | None -> GlbVal c
45 end
46 | _ -> GlbVal c
47
48 let intern_constr ist c =
49 let {Genintern.ltacvars=lfun; genv=env; extra; intern_sign; strict_check} = ist in
50 let scope = Pretyping.WithoutTypeConstraint in
51 let ltacvars = {
52 Constrintern.ltac_vars = lfun;
53 ltac_bound = Id.Set.empty;
54 ltac_extra = extra;
55 } in
56 let c' = Constrintern.intern_core scope ~strict_check ~ltacvars env (Evd.from_env env) intern_sign c in
57 c'
58
59 let intern_constr_tacexpr ist c =
60 let c = intern_constr ist c in
61 let v = of_glob_constr c in
62 (v, gtypref t_constr)
63
64 let interp_constr flags ist c =
65 let open Pretyping in
66 let ist = to_lvar ist in
67 Tac2core.pf_apply ~catch_exceptions:true begin fun env sigma ->
68 let (sigma, c) = understand_ltac flags env sigma ist WithoutTypeConstraint c in
69 let c = Tac2ffi.of_constr c in
70 Proofview.Unsafe.tclEVARS sigma >>= fun () ->
71 Proofview.tclUNIT c
72 end
73
74 let () =
75 let intern = intern_constr_tacexpr in
76 let interp ist c = interp_constr Tac2core.constr_flags ist c in
77 let print env sigma c = str "constr:(" ++ Printer.pr_lglob_constr_env env sigma c ++ str ")" in
78 let raw_print env sigma c =
79 str "constr:(" ++ Ppconstr.(pr_lconstr_expr ~flags:(current_flags())) env sigma c ++ str ")"
80 in
81 let subst subst c = Detyping.subst_glob_constr (Global.env()) subst c in
82 let obj = {
83 ml_intern = intern;
84 ml_subst = subst;
85 ml_interp = interp;
86 ml_print = print;
87 ml_raw_print = raw_print;
88 } in
89 define_ml_object Tac2quote.wit_constr obj
90
91 let () =
92 let intern = intern_constr_tacexpr in
93 let interp ist c = interp_constr Tac2core.open_constr_no_classes_flags ist c in
94 let print env sigma c = str "open_constr:(" ++ Printer.pr_lglob_constr_env env sigma c ++ str ")" in
95 let raw_print env sigma c =
96 str "open_constr:(" ++ Ppconstr.(pr_lconstr_expr ~flags:(current_flags())) env sigma c ++ str ")"
97 in
98 let subst subst c = Detyping.subst_glob_constr (Global.env()) subst c in
99 let obj = {
100 ml_intern = intern;
101 ml_subst = subst;
102 ml_interp = interp;
103 ml_print = print;
104 ml_raw_print = raw_print;
105 } in
106 define_ml_object Tac2quote.wit_open_constr obj
107
108 let () =
109 let interp _ id = return (Tac2ffi.of_ident id) in
110 let print _ _ id = str "ident:(" ++ Id.print id ++ str ")" in
111 let obj = {
112 ml_intern = (fun _ id -> GlbVal id, gtypref t_ident);
113 ml_interp = interp;
114 ml_subst = (fun _ id -> id);
115 ml_print = print;
116 ml_raw_print = print;
117 } in
118 define_ml_object Tac2quote.wit_ident obj
119
120 let () =
121 let intern {Genintern.ltacvars=lfun; genv=env; extra; intern_sign=_; strict_check} c =
122 let sigma = Evd.from_env env in
123 let ltacvars = {
124 Constrintern.ltac_vars = lfun;
125 ltac_bound = Id.Set.empty;
126 ltac_extra = extra;
127 }
128 in
129 let _, pat = Constrintern.intern_constr_pattern env sigma ~strict_check ~as_type:false ~ltacvars c in
130 GlbVal pat, gtypref t_pattern
131 in
132 let subst subst c =
133 let env = Global.env () in
134 let sigma = Evd.from_env env in
135 Patternops.subst_uninstantiated_pattern env sigma subst c
136 in
137 let print env sigma pat = str "pat:(" ++ Printer.pr_uninstantiated_lconstr_pattern_env env sigma pat ++ str ")" in
138 let raw_print env sigma pat =
139 str "pat:(" ++ Ppconstr.(pr_lconstr_pattern_expr ~flags:(current_flags())) env sigma pat ++ str ")"
140 in
141 let interp env c =
142 let ist = to_lvar env in
143 Tac2core.pf_apply ~catch_exceptions:true begin fun env sigma ->
144 let c = Patternops.interp_pattern env sigma ist c in
145 return (Tac2ffi.of_pattern c)
146 end
147 in
148 let obj = {
149 ml_intern = intern;
150 ml_interp = interp;
151 ml_subst = subst;
152 ml_print = print;
153 ml_raw_print = raw_print;
154 } in
155 define_ml_object Tac2quote.wit_pattern obj
156
157 let () =
158 let intern ist c =
159 let c = intern_constr ist c in
160 (GlbVal (Id.Set.empty,c), gtypref t_preterm)
161 in
162 let interp env (ids,c) =
163 let open Ltac_pretype in
164 let get_preterm id = match Id.Map.find_opt id env.env_ist with
165 | Some v -> to_preterm v
166 | None -> assert false
167 in
168 let closure = {
169 idents = Id.Map.empty;
170 typed = Id.Map.empty;
171 untyped = Id.Map.bind get_preterm ids;
172 genargs = Tac2interp.set_env env Id.Map.empty;
173 } in
174 let c = { closure; term = c } in
175 return (Tac2ffi.of_preterm c)
176 in
177 let subst subst (ids,c) = ids, Detyping.subst_glob_constr (Global.env()) subst c in
178 let print env sigma (ids,c) =
179 let ppids = if Id.Set.is_empty ids then mt()
180 else prlist_with_sep spc Id.print (Id.Set.elements ids) ++ spc() ++ str "|-" ++ spc()
181 in
182 hov 2 (str "preterm:(" ++ ppids ++ Printer.pr_lglob_constr_env env sigma c ++ str ")")
183 in
184 let raw_print env sigma c =
185 str "preterm:(" ++ Ppconstr.(pr_lconstr_expr ~flags:(current_flags())) env sigma c ++ str ")"
186 in
187 let obj = {
188 ml_intern = intern;
189 ml_interp = interp;
190 ml_subst = subst;
191 ml_print = print;
192 ml_raw_print = raw_print;
193 } in
194 define_ml_object Tac2quote.wit_preterm obj
195
196 let () =
197 let intern ist ref = match ref.CAst.v with
198 | Tac2qexpr.QHypothesis id ->
199 GlbVal (GlobRef.VarRef id), gtypref t_reference
200 | Tac2qexpr.QReference qid ->
201 let gr =
202 try Smartlocate.locate_global_with_alias qid
203 with Not_found as exn ->
204 let _, info = Exninfo.capture exn in
205 Nametab.error_global_not_found ~info qid
206 in
207 GlbVal gr, gtypref t_reference
208 in
209 let subst s c = Globnames.subst_global_reference s c in
210 let interp _ gr = return (Tac2ffi.of_reference gr) in
211 let print _ _ = function
212 | GlobRef.VarRef id -> str "reference:(" ++ str "&" ++ Id.print id ++ str ")"
213 | r -> str "reference:(" ++ Printer.pr_global r ++ str ")"
214 in
215 let raw_print _ _ r = match r.CAst.v with
216 | Tac2qexpr.QReference qid -> str "reference:(" ++ Libnames.pr_qualid qid ++ str ")"
217 | Tac2qexpr.QHypothesis id -> str "reference:(&" ++ Id.print id ++ str ")"
218 in
219 let obj = {
220 ml_intern = intern;
221 ml_subst = subst;
222 ml_interp = interp;
223 ml_print = print;
224 ml_raw_print = raw_print;
225 } in
226 define_ml_object Tac2quote.wit_reference obj
227
228 let () =
229 let intern ist qid =
230 let m =
231 try Nametab.Modules.locate qid
232 with Not_found ->
233 try Nametab.ModTypes.locate qid
234 with Not_found ->
235 match Nametab.OpenMods.locate qid with
236 | DirOpenModule m | DirOpenModtype m -> m
237 | DirOpenSection _ ->
238 CErrors.user_err ?loc:qid.loc Pp.(Libnames.pr_qualid qid ++ str " is a section, expected a module.")
239 | exception Not_found ->
240 CErrors.user_err ?loc:qid.loc Pp.(str "Unknown module " ++ Libnames.pr_qualid qid ++ str ".")
241 in
242 GlbVal m, gtypref t_module
243 in
244 let subst s c = Mod_subst.subst_mp s c in
245 let interp _ m = return (Tac2ffi.of_modpath m) in
246 (* XXX nametab based print? share code with tac2core if so *)
247 let print _ _ m = str "module:(" ++ ModPath.print m ++ str ")" in
248 let raw_print _ _ r = str "module:(" ++ Libnames.pr_qualid r ++ str ")" in
249 let obj = {
250 ml_intern = intern;
251 ml_subst = subst;
252 ml_interp = interp;
253 ml_print = print;
254 ml_raw_print = raw_print;
255 } in
256 define_ml_object Tac2quote.wit_module obj
257
258 (** Ltac2 in terms *)
259
260 let () =
261 let interp ?loc ~poly env sigma tycon (ids, tac) =
262 (* Syntax prevents bound notation variables in constr quotations *)
263 let ist = Tac2interp.get_env @@ GlobEnv.lfun env in
264 let () = assert (Id.Set.subset ids (Id.Map.domain ist.env_ist)) in
265 let tac = Proofview.tclIGNORE (Tac2interp.interp ist tac) in
266 let name = Id.of_string "ltac2" in
267 let sigma, concl = match tycon with
268 | Some ty -> sigma, ty
269 | None -> GlobEnv.new_type_evar env sigma ~src:(loc,Evar_kinds.InternalHole)
270 in
271 let c, sigma = Subproof.refine_by_tactic ~name ~poly (GlobEnv.renamed_env env) sigma concl tac in
272 let j = { Environ.uj_val = c; Environ.uj_type = concl } in
273 (j, sigma)
274 in
275 GlobEnv.register_constr_interp0 wit_ltac2_constr interp
276
277 let check_judge ?loc env sigma tycon j =
278 match tycon with
279 | None ->
280 j, sigma
281 | Some ty ->
282 let sigma =
283 try Evarconv.unify_leq_delay env sigma j.Environ.uj_type ty
284 with Evarconv.UnableToUnify (sigma,e) ->
285 Pretype_errors.error_actual_type ?loc env sigma j ty e
286 in
287 {j with Environ.uj_type = ty}, sigma
288
289 let interp_constr_var_as_constr ?loc env sigma tycon id =
290 let ist = Tac2interp.get_env @@ GlobEnv.lfun env in
291 let env = GlobEnv.renamed_env env in
292 let c = Id.Map.find id ist.env_ist in
293 let c = Tac2ffi.to_constr c in
294 let t = Retyping.get_type_of env sigma c in
295 let j = { Environ.uj_val = c; uj_type = t } in
296 check_judge ?loc env sigma tycon j
297
298 let interp_preterm_var_as_constr ?loc env sigma tycon id =
299 let open Ltac_pretype in
300 let ist = Tac2interp.get_env @@ GlobEnv.lfun env in
301 let env = GlobEnv.renamed_env env in
302 let c = Id.Map.find id ist.env_ist in
303 let {closure; term} = Tac2ffi.to_preterm c in
304 let vars = {
305 ltac_constrs = closure.typed;
306 ltac_uconstrs = closure.untyped;
307 ltac_idents = closure.idents;
308 ltac_genargs = closure.genargs;
309 }
310 in
311 let flags = Tac2core.preterm_flags in
312 let tycon = let open Pretyping in match tycon with
313 | Some ty -> OfType ty
314 | None -> WithoutTypeConstraint
315 in
316 let sigma, t, ty = Pretyping.understand_ltac_ty flags env sigma vars tycon term in
317 Environ.make_judge t ty, sigma
318
319 exception NotFoundHypVar of Id.t * Id.t
320
321 let () = CErrors.register_handler @@ function
322 | NotFoundHypVar (id0,id) ->
323 Some
324 Pp.(fmt "Hypothesis %t (value of ltac2 variable %t) not found."
325 (fun () -> quote (Id.print id))
326 (fun () -> quote (Id.print id0)))
327 | _ -> None
328
329 let interp_hyp_var_as_constr ?loc globenv sigma tycon id0 =
330 let ist = Tac2interp.get_env @@ GlobEnv.lfun globenv in
331 let env = GlobEnv.renamed_env globenv in
332 let v = Id.Map.find id0 ist.env_ist in
333 let id = Tac2ffi.to_ident v in
334 let c = try GlobEnv.lookup_renamed globenv id
335 with Not_found ->
336 Loc.raise ?loc (NotFoundHypVar (id0,id))
337 in
338 let j = Retyping.get_judgment_of env sigma c in
339 check_judge ?loc env sigma tycon j
340
341 let () =
342 let interp ?loc ~poly env sigma tycon (kind,id) =
343 let f = match kind with
344 | ConstrVar -> interp_constr_var_as_constr
345 | PretermVar -> interp_preterm_var_as_constr
346 | HypVar -> interp_hyp_var_as_constr
347 | PatternVar -> assert false
348 in
349 f ?loc env sigma tycon id
350 in
351 GlobEnv.register_constr_interp0 wit_ltac2_var_quotation interp
352
353 let () =
354 let interp _ist tac =
355 (* XXX should we be doing something with the ist? *)
356 let tac = Tac2interp.(interp empty_environment) tac in
357 Proofview.tclBIND tac (fun _ ->
358 Ftactic.return (Geninterp.Val.inject (Geninterp.val_tag (topwit Stdarg.wit_unit)) ()))
359 in
360 Geninterp.register_interp0 wit_ltac2_tac interp
361
362 let () =
363 let interp env sigma ist (kind,id) =
364 let () = match kind with
365 | ConstrVar -> assert false (* checked at intern time *)
366 | PretermVar -> assert false
367 | HypVar -> assert false
368 | PatternVar -> ()
369 in
370 let ist = Tac2interp.get_env ist.Ltac_pretype.ltac_genargs in
371 let c = Id.Map.find id ist.env_ist in
372 let c = Tac2ffi.to_pattern c in
373 c
374 in
375 Patternops.register_interp_pat wit_ltac2_var_quotation interp
376
377 let () =
378 let pr_raw (kind,id) = Genprint.PrinterBasic (fun _env _sigma ->
379 let ppkind =
380 match kind with
381 | None -> mt()
382 | Some kind -> Id.print kind.CAst.v ++ str ":"
383 in
384 str "$" ++ ppkind ++ Id.print id.CAst.v)
385 in
386 let pr_glb (kind,id) = Genprint.PrinterBasic (fun _env _sigma ->
387 let ppkind = match kind with
388 | ConstrVar -> mt()
389 | PretermVar -> str "preterm:"
390 | PatternVar -> str "pattern:"
391 | HypVar -> str "hyp:"
392 in
393 str "$" ++ ppkind ++ Id.print id) in
394 Genprint.register_noval_print0 wit_ltac2_var_quotation pr_raw pr_glb
395
396 let () =
397 let subs ntnvars globs (ids, tac as orig) =
398 if Id.Set.is_empty ids then
399 (* closed tactic *)
400 orig
401 else
402 (* Let-bind the notation terms inside the tactic *)
403 let fold id (used_ntnvars, accu) =
404 let used, c = Genintern.with_used_ntnvars ntnvars (fun () -> globs id) in
405 match c with
406 | None ->
407 CErrors.user_err Pp.(str "Notation variable " ++ Id.print id ++ str " cannot be used in ltac2.")
408 | Some c ->
409 let c = GTacExt (Tac2quote.wit_preterm, (used, c)) in
410 Id.Set.union used_ntnvars used, (Name id, c) :: accu
411 in
412 let used, bnd = Id.Set.fold fold ids (Id.Set.empty, []) in
413 let tac = if List.is_empty bnd then tac else GTacLet (false, bnd, tac) in
414 (used, tac)
415 in
416 Genintern.register_ntn_subst0 wit_ltac2_constr subs
417
418 let () =
419 let pr_raw e = Genprint.PrinterBasic (fun _env _sigma ->
420 let avoid = Id.Set.empty in
421 (* FIXME avoid set, same as pr_glb *)
422 let e = Tac2intern.debug_globalize_allow_ext avoid e in
423 Tac2print.pr_rawexpr_gen ~avoid E5 e) in
424 let pr_glb (ids, e) =
425 let ids =
426 let ids = Id.Set.elements ids in
427 if List.is_empty ids then mt ()
428 else hov 0 (pr_sequence Id.print ids ++ str " |-") ++ spc()
429 in
430 (* FIXME avoid set
431 eg "Ltac2 bla foo := constr:(ltac2:(foo X.foo))"
432 gets incorrectly printed as "fun foo => constr:(ltac2:(foo foo))"
433 NB: can't pass through evar map store as the evar map we get is a dummy,
434 see Ppconstr.pr_genarg
435 *)
436 Genprint.PrinterBasic Pp.(fun _env _sigma -> ids ++ Tac2print.pr_glbexpr ~avoid:Id.Set.empty e)
437 in
438 Genprint.register_noval_print0 wit_ltac2_constr pr_raw pr_glb
439
440 let () =
441 let pr_raw e = Genprint.PrinterBasic (fun _ _ ->
442 let e = Tac2intern.debug_globalize_allow_ext Id.Set.empty e in
443 Tac2print.pr_rawexpr_gen ~avoid:Id.Set.empty E5 e)
444 in
445 let pr_glb e = Genprint.PrinterBasic (fun _ _ -> Tac2print.pr_glbexpr ~avoid:Id.Set.empty e) in
446 let pr_top () = assert false in
447 Genprint.register_print0 wit_ltac2_tac pr_raw pr_glb pr_top
448
449 (** Built-in notation entries *)
450
451 let add_syntax_class_full s f =
452 Tac2entries.register_syntax_class (Id.of_string s) f
453
454 let add_syntax_class s intern f =
455 add_syntax_class_full s {
456 intern_synclass = (fun s -> Tac2entries.no_used_levels, intern s);
457 interp_synclass = (fun s -> f s);
458 }
459
460 let syntax_class_fail s args =
461 let args = str "(" ++ prlist_with_sep (fun () -> str ", ") Tac2print.pr_syntax_class args ++ str ")" in
462 CErrors.user_err (str "Invalid arguments " ++ args ++ str " in syntactic class " ++ str s)
463
464 let q_unit = CAst.make @@ CTacCst (AbsKn (Tuple 0))
465
466 let add_expr_syntax_class name entry f =
467 add_syntax_class name begin function
468 | [] -> ()
469 | arg -> syntax_class_fail name arg
470 end begin fun () ->
471 Tac2entries.SyntaxRule (Procq.Symbol.nterm entry, f)
472 end
473
474 let add_generic_syntax_class s entry arg =
475 add_expr_syntax_class s entry (fun x -> CAst.make @@ CTacExt (arg, x))
476
477 open CAst
478
479 let () = add_syntax_class "keyword" begin function
480 | [SexprStr {loc;v=s}] -> s
481 | arg -> syntax_class_fail "keyword" arg
482 end begin fun s ->
483 let syntax_class = Procq.Symbol.token (Tok.PKEYWORD s) in
484 Tac2entries.SyntaxRule (syntax_class, (fun _ -> q_unit))
485 end
486
487 let () = add_syntax_class "terminal" begin function
488 | [SexprStr {loc;v=s}] -> s
489 | arg -> syntax_class_fail "terminal" arg
490 end begin fun s ->
491 let syntax_class = Procq.Symbol.token (CLexer.terminal s) in
492 Tac2entries.SyntaxRule (syntax_class, (fun _ -> q_unit))
493 end
494
495 let () = add_syntax_class_full "list0" {
496 intern_synclass = begin function
497 | [tok] ->
498 let used, subclass = Tac2entries.intern_syntax_class tok in
499 used, (subclass, None)
500 | [tok; SexprStr {v=str}] ->
501 let used, subclass = Tac2entries.intern_syntax_class tok in
502 used, (subclass, Some str)
503 | arg -> syntax_class_fail "list0" arg
504 end;
505 interp_synclass = begin function
506 | subclass, None ->
507 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
508 let syntax_class = Procq.Symbol.list0 syntax_class in
509 let act l = Tac2quote.of_list act l in
510 Tac2entries.SyntaxRule (syntax_class, act)
511 | subclass, Some str ->
512 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
513 let sep = Procq.Symbol.tokens [Procq.TPattern (CLexer.terminal str)] in
514 let syntax_class = Procq.Symbol.list0sep syntax_class sep in
515 let act l = Tac2quote.of_list act l in
516 Tac2entries.SyntaxRule (syntax_class, act)
517 end;
518 }
519
520 let () = add_syntax_class_full "list1" {
521 intern_synclass = begin function
522 | [tok] ->
523 let used, subclass = Tac2entries.intern_syntax_class tok in
524 used, (subclass, None)
525 | [tok; SexprStr {v=str}] ->
526 let used, subclass = Tac2entries.intern_syntax_class tok in
527 used, (subclass, Some str)
528 | arg -> syntax_class_fail "list1" arg
529 end;
530 interp_synclass = begin function
531 | subclass, None ->
532 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
533 let syntax_class = Procq.Symbol.list1 syntax_class in
534 let act l = Tac2quote.of_list act l in
535 Tac2entries.SyntaxRule (syntax_class, act)
536 | subclass, Some str ->
537 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
538 let sep = Procq.Symbol.tokens [Procq.TPattern (CLexer.terminal str)] in
539 let syntax_class = Procq.Symbol.list1sep syntax_class sep in
540 let act l = Tac2quote.of_list act l in
541 Tac2entries.SyntaxRule (syntax_class, act)
542 end;
543 }
544
545 let () = add_syntax_class_full "opt" {
546 intern_synclass = begin function
547 | [tok] ->
548 let used, subclass = Tac2entries.intern_syntax_class tok in
549 used, subclass
550 | arg -> syntax_class_fail "opt" arg
551 end;
552 interp_synclass = begin fun subclass ->
553 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
554 let syntax_class = Procq.Symbol.opt syntax_class in
555 let act opt = match opt with
556 | None ->
557 CAst.make @@ CTacCst (AbsKn (Other c_none))
558 | Some x ->
559 CAst.make @@ CTacApp (CAst.make @@ CTacCst (AbsKn (Other c_some)), [act x])
560 in
561 Tac2entries.SyntaxRule (syntax_class, act)
562 end;
563 }
564
565 let () = add_syntax_class "self" begin function
566 | [] -> ()
567 | arg -> syntax_class_fail "self" arg
568 end begin fun () ->
569 let syntax_class = Procq.Symbol.self in
570 let act tac = tac in
571 Tac2entries.SyntaxRule (syntax_class, act)
572 end
573
574 let () = add_syntax_class "next" begin function
575 | [] -> ()
576 | arg -> syntax_class_fail "next" arg
577 end begin fun () ->
578 let syntax_class = Procq.Symbol.next in
579 let act tac = tac in
580 Tac2entries.SyntaxRule (syntax_class, act)
581 end
582
583 let () = add_syntax_class "tactic" begin function
584 | [] ->
585 (* Default to level 5 parsing *)
586 5
587 | [SexprInt {loc;v=n}] as arg ->
588 let () = if n < 0 || n > 6 then syntax_class_fail "tactic" arg in
589 n
590 | arg -> syntax_class_fail "tactic" arg
591 end begin fun lev ->
592 let syntax_class = Procq.Symbol.nterml ltac2_expr (string_of_int lev) in
593 let act tac = tac in
594 Tac2entries.SyntaxRule (syntax_class, act)
595 end
596
597 let () = add_syntax_class_full "thunk" {
598 intern_synclass = begin function
599 | [tok] ->
600 let used, subclass = Tac2entries.intern_syntax_class tok in
601 used, subclass
602 | arg -> syntax_class_fail "thunk" arg
603 end;
604 interp_synclass = begin fun subclass ->
605 let Tac2entries.SyntaxRule (syntax_class, act) = Tac2entries.interp_syntax_class subclass in
606 let act e = Tac2quote.thunk (act e) in
607 Tac2entries.SyntaxRule (syntax_class, act)
608 end;
609 }
610
611 let warn_unqualified_delimiters =
612 let w = CWarnings.create_warning ~name:"deprecated-ltac2-unqualified-delimiters"
613 ~from:[CWarnings.CoreCategories.ltac2; Deprecation.Version.v9_2]
614 ()
615 in
616 CWarnings.create_in w
617 Pp.(fun (s,delims) ->
618 let delims () = prlist_with_sep pr_comma Id.print @@ List.rev delims in
619 fmt "Delimiter arguments to %s must be qualified using \"delimiters\"@
620 (e.g. \"%s(delimiters(%t))\")@ unless there is a unique delimiter argument." s s delims)
621
622 let delimiters_qid = Libnames.qualid_of_string "delimiters"
623 let level_qid = Libnames.qualid_of_string "level"
624 let custom_qid = Libnames.qualid_of_string "custom"
625
626 let constr_args s arg =
627 let fail () = syntax_class_fail s arg in
628 let extract_id = function
629 | SexprRec (_, { v = Some qid }, []) when Libnames.qualid_is_ident qid ->
630 Some (Libnames.qualid_basename qid)
631 | _ -> None
632 in
633 let force_id arg = match extract_id arg with
634 | Some id -> id
635 | None -> fail ()
636 in
637 let lev, custom, raw_delims, all_delims =
638 List.fold_left (fun (lev,custom,raw_delims,all_delims) -> function
639 | SexprRec (_, { v = Some qid }, [])
640 when Libnames.qualid_is_ident qid ->
641 let id = Libnames.qualid_basename qid in
642 lev, custom, id :: raw_delims, [id] :: all_delims
643 | SexprRec (_, { v = Some qid }, subargs) when Libnames.qualid_eq qid delimiters_qid ->
644 lev, custom, raw_delims, (List.map force_id subargs) :: all_delims
645 | SexprRec (_, { v = Some qid }, [SexprInt lev'])
646 when Libnames.qualid_eq qid level_qid ->
647 if Option.has_some lev then fail()
648 else Some lev', custom, raw_delims, all_delims
649 | SexprRec (_, { v = Some qid }, [SexprRec (_, { v = Some custom' }, [])])
650 when Libnames.qualid_eq qid custom_qid ->
651 if Option.has_some custom then fail()
652 else lev, Some custom', raw_delims, all_delims
653 | _ -> fail())
654 (None,None,[],[])
655 arg
656 in
657 let all_delims = List.concat (List.rev all_delims) in
658 let () = match raw_delims, arg with
659 | [], _ -> ()
660 | [delim], [arg] when Option.equal Id.equal (Some delim) (extract_id arg) -> ()
661 | _ -> warn_unqualified_delimiters (s,raw_delims)
662 in
663 lev, custom, List.rev all_delims
664
665 let constr_delimiters s arg =
666 let lev, custom, scopes = constr_args s arg in
667 let () = if Option.has_some lev then
668 CErrors.user_err Pp.(str "Specifying level not allowed for syntax class " ++ str s ++ str ".")
669 in
670 let () = if Option.has_some custom then
671 CErrors.user_err Pp.(str "Custom entry not allowed for syntax class " ++ str s ++ str ".")
672 in
673 scopes
674
675 let constr_args s arg =
676 let lev, custom, scopes = constr_args s arg in
677 let custom = custom
678 |> Option.map (fun custom ->
679 try Nametab.CustomEntries.locate custom
680 with Not_found -> CErrors.user_err ?loc:custom.loc Pp.(str "Unknown custom entry " ++ Libnames.pr_qualid custom ++ str "."))
681 in
682 let () = lev |> Option.iter (fun lev -> if lev.v < 0 then CErrors.user_err ?loc:lev.loc Pp.(str "Level must be nonnegative.")) in
683 let lev = lev |> Option.map (fun lev -> string_of_int lev.v) in
684 (lev, custom), scopes
685
686 let constr_symb (lev,custom) =
687 let custom = Option.map (fun custom -> fst @@ Egramrocq.find_custom_entry custom) custom in
688 match lev, custom with
689 | None, None -> Procq.Symbol.nterm Procq.Constr.constr
690 | Some lev, None -> Procq.Symbol.nterml Procq.Constr.term lev
691 | None, Some custom -> Procq.Symbol.nterm custom
692 | Some lev, Some custom ->
693 Procq.Symbol.nterml custom lev
694
695 let add_constr_classes (name,lname) quote =
696 let () =
697 let s = name in
698 add_syntax_class s (constr_args s) begin function (symb,delimiters) ->
699 let act e = quote ?delimiters:(Some delimiters) e in
700 Tac2entries.SyntaxRule (constr_symb symb, act)
701 end
702 in
703 let () =
704 let s = lname in
705 add_syntax_class s (constr_delimiters s) begin function delimiters ->
706 let act e = quote ?delimiters:(Some delimiters) e in
707 Tac2entries.SyntaxRule (Procq.Symbol.nterm Procq.Constr.lconstr, act)
708 end
709 in
710 ()
711
712 let () = add_constr_classes ("constr","lconstr") Tac2quote.of_constr
713
714 let () = add_constr_classes ("open_constr","open_lconstr") Tac2quote.of_open_constr
715
716 let () = add_constr_classes ("preterm","lpreterm") Tac2quote.of_preterm
717
718 let () = add_expr_syntax_class "ident" q_ident (fun id -> Tac2quote.of_anti Tac2quote.of_ident id)
719 let () = add_expr_syntax_class "bindings" q_bindings Tac2quote.of_bindings
720 let () = add_expr_syntax_class "with_bindings" q_with_bindings Tac2quote.of_bindings
721 let () = add_expr_syntax_class "intropattern" q_intropattern Tac2quote.of_intro_pattern
722 let () = add_expr_syntax_class "intropatterns" q_intropatterns Tac2quote.of_intro_patterns
723 let () = add_expr_syntax_class "destruction_arg" q_destruction_arg Tac2quote.of_destruction_arg
724 let () = add_expr_syntax_class "induction_clause" q_induction_clause Tac2quote.of_induction_clause
725 let () = add_expr_syntax_class "conversion" q_conversion Tac2quote.of_conversion
726 let () = add_expr_syntax_class "orient" q_orient Tac2quote.of_orient
727 let () = add_expr_syntax_class "rewriting" q_rewriting Tac2quote.of_rewriting
728 let () = add_expr_syntax_class "clause" q_clause Tac2quote.of_clause
729 let () = add_expr_syntax_class "hintdb" q_hintdb Tac2quote.of_hintdb
730 let () = add_expr_syntax_class "occurrences" q_occurrences Tac2quote.of_occurrences
731 let () = add_expr_syntax_class "dispatch" q_dispatch Tac2quote.of_dispatch
732 let () = add_expr_syntax_class "strategy" q_strategy_flag Tac2quote.of_strategy_flag
733 let () = add_expr_syntax_class "reference" q_reference Tac2quote.of_reference
734 let () = add_expr_syntax_class "move_location" q_move_location Tac2quote.of_move_location
735 let () = add_expr_syntax_class "pose" q_pose Tac2quote.of_pose
736 let () = add_expr_syntax_class "assert" q_assert Tac2quote.of_assertion
737 let () = add_expr_syntax_class "constr_matching" q_constr_matching Tac2quote.of_constr_matching
738 let () = add_expr_syntax_class "goal_matching" q_goal_matching Tac2quote.of_goal_matching
739 let () = add_expr_syntax_class "format" Procq.Prim.lstring Tac2quote.of_format
740 let () = add_expr_syntax_class "module" Procq.Prim.qualid Tac2quote.of_module
741
742 let () = add_generic_syntax_class "pattern" Procq.Constr.constr Tac2quote.wit_pattern
743
744 (** seq syntax_class, a bit hairy *)
745
746 open Procq
747
748 type _ converter =
749 | CvNil : (Loc.t -> raw_tacexpr) converter
750 | CvCns : 'act converter * ('a -> raw_tacexpr) option -> ('a -> 'act) converter
751
752 let rec apply : type a. a converter -> raw_tacexpr list -> a = function
753 | CvNil -> fun accu loc -> Tac2quote.of_tuple ~loc accu
754 | CvCns (c, None) -> fun accu x -> apply c accu
755 | CvCns (c, Some f) -> fun accu x -> apply c (f x :: accu)
756
757 type seqrule =
758 | Seqrule : (Tac2expr.raw_tacexpr, Gramlib.Grammar.norec, 'act, Loc.t -> raw_tacexpr) Rule.t * 'act converter -> seqrule
759
760 let rec make_seq_rule = function
761 | [] ->
762 Seqrule (Procq.Rule.stop, CvNil)
763 | (skip,tok) :: rem ->
764 let Tac2entries.SyntaxRule (syntax_class, f) = Tac2entries.interp_syntax_class tok in
765 let syntax_class =
766 match Procq.generalize_symbol syntax_class with
767 | None ->
768 CErrors.user_err (str "Recursive symbols (self / next) are not allowed in local rules")
769 | Some syntax_class -> syntax_class
770 in
771 let Seqrule (r, c) = make_seq_rule rem in
772 let r = Procq.Rule.next_norec r syntax_class in
773 let f = if skip then None else Some f in
774 Seqrule (r, CvCns (c, f))
775
776 let interp_seq_rule toks =
777 let Seqrule (r, c) = make_seq_rule (List.rev toks) in
778 let syntax_class =
779 Procq.(Symbol.rules [Rules.make r (apply c [])])
780 in
781 Tac2entries.SyntaxRule (syntax_class, (fun e -> e))
782
783 let intern_seq_rule toks =
784 List.fold_left_map (fun used tok ->
785 let used', rule = Tac2entries.intern_syntax_class tok in
786 let skip = match tok with
787 | SexprStr _ -> true (* Leave out mere strings *)
788 | _ -> false
789 in
790 Tac2entries.union_used_levels used used', (skip, rule))
791 Tac2entries.no_used_levels
792 toks
793
794 let () = add_syntax_class_full "seq" {
795 intern_synclass = intern_seq_rule;
796 interp_synclass = interp_seq_rule;
797 }