Debian Codegraph @

plugins/ltac/g_ltac.mlg

raw
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 DECLARE PLUGIN "rocq-runtime.plugins.ltac"
12
13 {
14
15 open Util
16 open Pp
17 open Constrexpr
18 open Tacexpr
19 open Genarg
20 open Names
21 open Attributes
22
23 open Procq
24 open Procq.Prim
25 open Procq.Constr
26 open Pvernac.Vernac_
27 open Pltac
28
29 let fail_default_value = Locus.ArgArg 0
30
31 let arg_of_expr = function
32 { CAst.v=(TacArg v) } -> v
33 | e -> Tacexp (e:raw_tactic_expr)
34
35 let genarg_of_unit () = in_gen (rawwit Stdarg.wit_unit) ()
36 let genarg_of_int n = in_gen (rawwit Stdarg.wit_int) n
37 let genarg_of_ipattern pat = in_gen (rawwit Tacarg.wit_simple_intropattern) pat
38 let genarg_of_uconstr c = in_gen (rawwit Stdarg.wit_uconstr) c
39
40 let reference_to_id qid =
41 if Libnames.qualid_is_ident qid then
42 CAst.make ?loc:qid.CAst.loc @@ Libnames.qualid_basename qid
43 else
44 CErrors.user_err ?loc:qid.CAst.loc
45 (str "This expression should be a simple identifier.")
46
47 let tactic_mode = Entry.make "tactic_command"
48
49 let tacdef_body = Entry.make "tacdef_body"
50
51 (* Registers [tactic_mode] as a parser for proof editing *)
52 let classic_proof_mode = Pvernac.register_proof_mode "Classic"
53 (ProofMode {
54 command_entry = tactic_mode;
55 wit_tactic_expr = Tacarg.wit_ltac;
56 tactic_expr_entry = Pltac.tactic;
57 })
58
59 (* Tactics grammar rules *)
60
61 let hint = G_proofs.hint
62
63 let for_extraargs = ()
64
65 let goal_selector = G_vernac.goal_selector
66 let toplevel_selector = G_vernac.toplevel_selector
67 let subprf = G_vernac.subprf
68 let subprf_with_selector = G_vernac.subprf_with_selector
69
70 }
71
72 GRAMMAR EXTEND Gram
73 GLOBAL: tactic tacdef_body ltac_expr tactic_value command hint
74 tactic_mode constr_may_eval constr_eval
75 term subprf subprf_with_selector;
76
77 tactic_then_last:
78 [ [ "|"; lta = LIST0 (OPT ltac_expr) SEP "|" ->
79 { Array.map (function None -> CAst.make ~loc (TacId []) | Some t -> t) (Array.of_list lta) }
80 | -> { [||] }
81 ] ]
82 ;
83 for_each_goal:
84 [ RIGHTA [ ta = ltac_expr; "|"; tg = for_each_goal -> { let (first,last) = tg in (ta::first, last) }
85 | ta = ltac_expr; ".."; l = tactic_then_last -> { ([], Some (ta, l)) }
86 | ".."; l = tactic_then_last -> { ([], Some (CAst.make ~loc (TacId []), l)) }
87 | ta = ltac_expr -> { ([ta], None) }
88 | "|"; tg = for_each_goal -> { let (first,last) = tg in (CAst.make ~loc (TacId []) :: first, last) }
89 | -> { ([CAst.make ~loc (TacId [])], None) }
90 ] ]
91 ;
92 tactic_then_locality: (* [true] for the local variant [TacThens] and [false]
93 for [TacExtend] *)
94 [ [ "[" ; l = OPT">" -> { if Option.is_empty l then true else false } ] ]
95 ;
96 ltac_expr:
97 [ "5" RIGHTA
98 [ ]
99 | "4" LEFTA
100 [ ta0 = ltac_expr; ";"; ta1 = ltac_expr -> { CAst.make ~loc (TacThen (ta0,ta1)) }
101 | ta0 = ltac_expr; ";"; l = tactic_then_locality; tg = for_each_goal; "]" -> {
102 let (first,tail) = tg in
103 match l , tail with
104 | false , Some (t,last) -> CAst.make ~loc (TacThen (ta0,
105 CAst.make ~loc (TacExtendTac (Array.of_list first, t, last))))
106 | true , Some (t,last) -> CAst.make ~loc (TacThens3parts (ta0, Array.of_list first, t, last))
107 | false , None -> CAst.make ~loc (TacThen (ta0, CAst.make ~loc (TacDispatch first)))
108 | true , None -> CAst.make ~loc (TacThens (ta0,first)) } ]
109 | "3" RIGHTA
110 [ IDENT "try"; ta = ltac_expr -> { CAst.make ~loc (TacTry ta) }
111 | IDENT "do"; n = nat_or_var; ta = ltac_expr -> { CAst.make ~loc (TacDo (n,ta)) }
112 | IDENT "timeout"; n = nat_or_var; ta = ltac_expr -> { CAst.make ~loc (TacTimeout (n,ta)) }
113 | IDENT "time"; s = OPT string; ta = ltac_expr -> { CAst.make ~loc (TacTime (s,ta)) }
114 | IDENT "repeat"; ta = ltac_expr -> { CAst.make ~loc (TacRepeat ta) }
115 | IDENT "progress"; ta = ltac_expr -> { CAst.make ~loc (TacProgress ta) }
116 | IDENT "once"; ta = ltac_expr -> { CAst.make ~loc (TacOnce ta) }
117 | IDENT "exactly_once"; ta = ltac_expr -> { CAst.make ~loc (TacExactlyOnce ta) }
118 (*To do: put Abstract in Refiner*)
119 | IDENT "abstract"; tc = NEXT -> { CAst.make ~loc (TacAbstract (tc,None)) }
120 | IDENT "abstract"; tc = NEXT; "using"; s = ident ->
121 { CAst.make ~loc (TacAbstract (tc,Some s)) }
122 | IDENT "only"; sel = goal_selector; ":"; ta = ltac_expr -> { CAst.make ~loc (TacSelect (sel, ta)) } ]
123 (*End of To do*)
124 | "2" RIGHTA
125 [ ta0 = ltac_expr; "+"; ta1 = ltac_expr -> { CAst.make ~loc (TacOr (ta0,ta1)) }
126 | IDENT "tryif" ; ta = ltac_expr ;
127 "then" ; tat = ltac_expr ;
128 "else" ; tae = ltac_expr -> { CAst.make ~loc (TacIfThenCatch (ta,tat,tae)) }
129 | ta0 = ltac_expr; "||"; ta1 = ltac_expr -> { CAst.make ~loc (TacOrelse (ta0,ta1)) } ]
130 | "1l" LEFTA
131 [ ]
132 | "1" RIGHTA
133 [ "fun"; it = LIST1 input_fun ; "=>"; body = ltac_expr LEVEL "5" ->
134 { CAst.make ~loc (TacFun (it,body)) }
135 | "let"; isrec = [IDENT "rec" -> { true } | -> { false } ];
136 llc = LIST1 let_clause SEP "with"; "in";
137 body = ltac_expr LEVEL "5" -> { CAst.make ~loc (TacLetIn (isrec,llc,body)) }
138 | b = match_key; IDENT "goal"; "with"; mrl = match_context_list; "end" ->
139 { CAst.make ~loc (TacMatchGoal (b,false,mrl)) }
140 | b = match_key; IDENT "reverse"; IDENT "goal"; "with";
141 mrl = match_context_list; "end" ->
142 { CAst.make ~loc (TacMatchGoal (b,true,mrl)) }
143 | b = match_key; c = ltac_expr; "with"; mrl = match_list; "end" ->
144 { CAst.make ~loc (TacMatch (b,c,mrl)) }
145 | IDENT "first" ; "["; l = LIST0 ltac_expr SEP "|"; "]" ->
146 { CAst.make ~loc (TacFirst l) }
147 | IDENT "solve" ; "["; l = LIST0 ltac_expr SEP "|"; "]" ->
148 { CAst.make ~loc (TacSolve l) }
149 | IDENT "idtac"; l = LIST0 message_token -> { CAst.make ~loc (TacId l) }
150 | g=failkw; n = [ n = nat_or_var -> { n } | -> { fail_default_value } ];
151 l = LIST0 message_token -> { CAst.make ~loc (TacFail (g,n,l)) }
152 | st = simple_tactic -> { st }
153 | a = tactic_value -> { CAst.make ~loc (TacArg a) }
154 | r = reference; la = LIST0 tactic_arg ->
155 { CAst.make ~loc @@ TacArg (TacCall (CAst.make ~loc (r,la))) } ]
156 | "0"
157 [ "("; a = ltac_expr; ")" -> { a }
158 | "["; ">"; tg = for_each_goal; "]" -> {
159 let (tf,tail) = tg in
160 begin match tail with
161 | Some (t,tl) -> CAst.make ~loc (TacExtendTac (Array.of_list tf,t,tl))
162 | None -> CAst.make ~loc (TacDispatch tf)
163 end }
164 | a = tactic_atom -> { CAst.make ~loc (TacArg a) } ] ]
165 ;
166 failkw:
167 [ [ IDENT "fail" -> { TacLocal } | IDENT "gfail" -> { TacGlobal } ] ]
168 ;
169 (* Tactic arguments to the right of an application *)
170 tactic_arg:
171 [ [ a = tactic_value -> { a }
172 | c = Constr.constr -> { (match c with { CAst.v = CRef (r,None) } -> Reference r | c -> ConstrMayEval (ConstrTerm c)) }
173 (* Unambiguous entries: tolerated w/o "ltac:" modifier *)
174 | "()" -> { TacGeneric (None, genarg_of_unit ()) } ] ]
175 ;
176 (* Can be used as argument and at toplevel in tactic expressions. *)
177 tactic_value:
178 [ [ c = constr_eval -> { ConstrMayEval c }
179 | IDENT "fresh"; l = LIST0 fresh_id -> { TacFreshId l }
180 | IDENT "type_term"; c=uconstr -> { TacPretype c }
181 | IDENT "numgoals" -> { TacNumgoals } ] ]
182 ;
183 (* If a qualid is given, use its short name. TODO: have the shortest
184 non ambiguous name where dots are replaced by "_"? Probably too
185 verbose most of the time. *)
186 fresh_id:
187 [ [ s = STRING -> { Locus.ArgArg s (*| id = ident -> Locus.ArgVar (!@loc,id)*) }
188 | qid = qualid -> { Locus.ArgVar (CAst.make ~loc @@ Libnames.qualid_basename qid) } ] ]
189 ;
190 constr_eval:
191 [ [ IDENT "eval"; rtc = red_expr; "in"; c = Constr.constr ->
192 { ConstrEval (rtc,c) }
193 | IDENT "context"; id = identref; "["; c = Constr.lconstr; "]" ->
194 { ConstrContext (id,c) }
195 | IDENT "type"; IDENT "of"; c = Constr.constr ->
196 { ConstrTypeOf c } ] ]
197 ;
198 tactic_atom:
199 [ [ n = integer -> { TacGeneric (None, genarg_of_int n) }
200 | r = reference -> { TacCall (CAst.make ~loc (r,[])) }
201 | "()" -> { TacGeneric (None, genarg_of_unit ()) } ] ]
202 ;
203 match_key:
204 [ [ "match" -> { Once }
205 | IDENT "lazymatch" -> { Select }
206 | IDENT "multimatch" -> { General } ] ]
207 ;
208 input_fun:
209 [ [ "_" -> { Name.Anonymous }
210 | l = ident -> { Name.Name l } ] ]
211 ;
212 let_clause:
213 [ [ idr = identref; ":="; te = ltac_expr ->
214 { (CAst.map (fun id -> Name id) idr, arg_of_expr te) }
215 | na = ["_" -> { CAst.make ~loc Anonymous } ]; ":="; te = ltac_expr ->
216 { (na, arg_of_expr te) }
217 | idr = identref; args = LIST1 input_fun; ":="; te = ltac_expr ->
218 { (CAst.map (fun id -> Name id) idr, arg_of_expr (CAst.make ~loc (TacFun (args,te)))) } ] ]
219 ;
220 match_pattern:
221 [ [ IDENT "context"; oid = OPT Constr.ident;
222 "["; pc = Constr.cpattern; "]" ->
223 { Subterm (oid, pc) }
224 | pc = Constr.cpattern -> { Term pc } ] ]
225 ;
226 match_hyp:
227 [ [ na = name; ":"; mp = match_pattern -> { Hyp (na, mp) }
228 | na = name; ":="; "["; mpv = match_pattern; "]"; ":"; mpt = match_pattern -> { Def (na, mpv, mpt) }
229 | na = name; ":="; mpv = match_pattern ->
230 { let t, ty =
231 match mpv with
232 | Term t -> (match t with
233 | { CAst.v = CCast (t, Some DEFAULTcast, ty) } -> Term t, Some (Term ty)
234 | _ -> mpv, None)
235 | _ -> mpv, None
236 in Def (na, t, Option.default (Term (CAst.make @@ CHole (None))) ty) }
237 ] ]
238 ;
239 match_context_rule:
240 [ [ largs = LIST0 match_hyp SEP ","; "|-"; mp = match_pattern;
241 "=>"; te = ltac_expr -> { Pat (largs, mp, te) }
242 | "["; largs = LIST0 match_hyp SEP ","; "|-"; mp = match_pattern;
243 "]"; "=>"; te = ltac_expr -> { Pat (largs, mp, te) }
244 | "_"; "=>"; te = ltac_expr -> { All te } ] ]
245 ;
246 match_context_list:
247 [ [ mrl = LIST1 match_context_rule SEP "|" -> { mrl }
248 | "|"; mrl = LIST1 match_context_rule SEP "|" -> { mrl } ] ]
249 ;
250 match_rule:
251 [ [ mp = match_pattern; "=>"; te = ltac_expr -> { Pat ([],mp,te) }
252 | "_"; "=>"; te = ltac_expr -> { All te } ] ]
253 ;
254 match_list:
255 [ [ mrl = LIST1 match_rule SEP "|" -> { mrl }
256 | "|"; mrl = LIST1 match_rule SEP "|" -> { mrl } ] ]
257 ;
258 message_token:
259 [ [ id = identref -> { MsgIdent id }
260 | s = STRING -> { MsgString s }
261 | n = natural -> { MsgInt n } ] ]
262 ;
263
264 ltac_def_kind:
265 [ [ ":=" -> { false }
266 | "::=" -> { true } ] ]
267 ;
268
269 (* Definitions for tactics *)
270 tacdef_body:
271 [ [ name = Constr.global; it=LIST1 input_fun;
272 redef = ltac_def_kind; body = ltac_expr ->
273 { if redef then Tacexpr.TacticRedefinition (name, CAst.make ~loc (TacFun (it, body)))
274 else
275 let id = reference_to_id name in
276 Tacexpr.TacticDefinition (id, CAst.make ~loc (TacFun (it, body))) }
277 | name = Constr.global; redef = ltac_def_kind;
278 body = ltac_expr ->
279 { if redef then Tacexpr.TacticRedefinition (name, body)
280 else
281 let id = reference_to_id name in
282 Tacexpr.TacticDefinition (id, body) }
283 ] ]
284 ;
285 tactic:
286 [ [ tac = ltac_expr -> { tac } ] ]
287 ;
288 tactic_mode:
289 [ [ p = subprf -> { Vernacexpr.VernacSynPure p }
290 | g = OPT toplevel_selector; p = subprf_with_selector -> { Vernacexpr.VernacSynPure (p g) }
291 ] ]
292 ;
293 term: LEVEL "0"
294 [ [ IDENT "ltac"; ":"; "("; tac = Pltac.ltac_expr; ")" ->
295 { let arg = Genarg.in_gen (Genarg.rawwit Tacarg.wit_ltac_in_term) tac in
296 CAst.make ~loc @@ CGenarg arg } ] ]
297 ;
298 END
299
300 {
301
302 open Stdarg
303 open Tacarg
304 open Vernacextend
305 open Libnames
306
307 let pr_ltac_selector s = Pp.(Goal_select.pr_goal_selector s ++ str ":")
308
309 }
310
311 VERNAC ARGUMENT EXTEND ltac_selector PRINTED BY { pr_ltac_selector }
312 | [ toplevel_selector(s) ] -> { s }
313 END
314
315 {
316
317 let pr_ltac_info n = str "Info" ++ spc () ++ int n
318
319 }
320
321 VERNAC ARGUMENT EXTEND ltac_info PRINTED BY { pr_ltac_info }
322 | [ "Info" natural(n) ] -> { n }
323 END
324
325 {
326
327 let pr_ltac_use_default b =
328 if b.CAst.v then (* Bug: a space is inserted before "..." *) str ".." else mt ()
329
330 }
331
332 VERNAC ARGUMENT EXTEND ltac_use_default PRINTED BY { pr_ltac_use_default }
333 | [ "." ] -> { CAst.make ~loc false }
334 | [ "..." ] -> { CAst.make ~loc true }
335 END
336
337 {
338
339 let rm_abstract tac =
340 let (loc, tac2) = CAst.(tac.loc, tac.v) in
341 match tac2 with
342 | TacAbstract (t,_) -> t, true
343 | TacSolve [ {CAst.loc; v=TacAbstract(t,_)} ] -> CAst.make ?loc (TacSolve [t]), true
344 | _ -> tac, false
345
346 let is_explicit_terminator = function
347 | {CAst.v=(TacSolve _)} -> true
348 | _ -> false
349
350 }
351
352 VERNAC { tactic_mode } EXTEND VernacSolve STATE proof
353 | [ ltac_selector_opt(g) ltac_info_opt(info) tactic(t) ltac_use_default(with_end_tac) ] =>
354 { classify_as_proofstep } -> {
355 let g = Option.default (Goal_select.get_default_goal_selector ()) g in
356 let global =
357 let open Goal_select in
358 match g with
359 | SelectAll -> true
360 | SelectList [NthSelector _] -> false
361 | SelectList _ -> true
362 | _ -> false in
363 let t = Tacinterp.hide_interp { Tacinterp.global; ast = t; } in
364 ComTactic.solve g ~info t ~with_end_tac
365 }
366 END
367
368 VERNAC { tactic_mode } EXTEND VernacSolveParallel STATE proof
369 | [ "par" ":" ltac_info_opt(info) tactic(t) "." ] =>
370 {
371 let solving_tac = is_explicit_terminator t in
372 let pbr = if solving_tac then Some "par" else None in
373 VtProofStep{ proof_block_detection = pbr }
374 } -> {
375 let t, abstract = rm_abstract t in
376 let t = Tacinterp.hide_interp { Tacinterp.global = true; ast = t; } in
377 ComTactic.solve_parallel ~info t ~abstract
378 }
379 END
380
381 {
382
383 let pr_ltac_tactic_level n = str "(at level " ++ int n ++ str ")"
384
385 }
386
387 VERNAC ARGUMENT EXTEND ltac_tactic_level PRINTED BY { pr_ltac_tactic_level }
388 | [ "(" "at" "level" natural(n) ")" ] -> { n }
389 END
390
391 VERNAC ARGUMENT EXTEND ltac_production_sep
392 | [ "," string(sep) ] -> { sep }
393 END
394
395 {
396
397 let pr_ltac_production_item = function
398 | Tacentries.TacTerm s -> quote (str s)
399 | Tacentries.TacNonTerm (_, ((arg, None), None)) -> str arg
400 | Tacentries.TacNonTerm (_, ((arg, Some _), None)) -> assert false
401 | Tacentries.TacNonTerm (_, ((arg, sep), Some id)) ->
402 let sep = match sep with
403 | None -> mt ()
404 | Some sep -> str "," ++ spc () ++ quote (str sep)
405 in
406 str arg ++ str "(" ++ Id.print id ++ sep ++ str ")"
407
408 let check_non_empty_string ?loc s =
409 if String.is_empty s then CErrors.user_err ?loc (str "Invalid empty string.")
410
411 }
412
413 VERNAC ARGUMENT EXTEND ltac_production_item PRINTED BY { pr_ltac_production_item }
414 | [ string(s) ] -> { check_non_empty_string ~loc s; Tacentries.TacTerm s }
415 | [ ident(nt) "(" ident(p) ltac_production_sep_opt(sep) ")" ] ->
416 { Tacentries.TacNonTerm (Loc.tag ~loc ((Id.to_string nt, sep), Some p)) }
417 | [ ident(nt) ] ->
418 { Tacentries.TacNonTerm (Loc.tag ~loc ((Id.to_string nt, None), None)) }
419 END
420
421 VERNAC COMMAND EXTEND VernacTacticNotation
422 | #[ deprecation; locality; ]
423 [ "Tactic" "Notation" ltac_tactic_level_opt(n) ne_ltac_production_item_list(r) ":=" tactic(e) ] =>
424 { VtSideff ([], VtNow) } SYNTERP AS tacobj {
425 let n = Option.default 0 n in
426 let local = Locality.make_module_locality locality in
427 Tacentries.add_tactic_notation_syntax local n ?deprecation r
428 } ->
429 {
430 Tacentries.add_tactic_notation ?deprecation tacobj e
431 }
432 END
433
434 VERNAC COMMAND EXTEND VernacPrintLtac CLASSIFIED AS QUERY
435 | [ "Print" "Ltac" reference(r) ] ->
436 { Feedback.msg_notice (Tacentries.print_ltac r) }
437 END
438
439 VERNAC COMMAND EXTEND VernacLocateLtac CLASSIFIED AS QUERY
440 | [ "Locate" "Ltac" reference(r) ] ->
441 { Tacentries.print_located_tactic r }
442 END
443
444 {
445
446 let pr_ltac_ref = Libnames.pr_qualid
447
448 let pr_tacdef_body env sigma tacdef_body =
449 let id, redef, body =
450 match tacdef_body with
451 | TacticDefinition ({CAst.v=id}, body) -> Id.print id, false, body
452 | TacticRedefinition (id, body) -> pr_ltac_ref id, true, body
453 in
454 let idl, body =
455 match body with
456 | {CAst.v=(Tacexpr.TacFun (idl,b))} -> idl,b
457 | _ -> [], body in
458 id ++
459 prlist (function Name.Anonymous -> str " _"
460 | Name.Name id -> spc () ++ Id.print id) idl
461 ++ (if redef then str" ::=" else str" :=") ++ brk(1,1)
462 ++ Pptactic.pr_raw_tactic env sigma body
463
464 }
465
466 VERNAC ARGUMENT EXTEND ltac_tacdef_body
467 PRINTED BY { pr_tacdef_body env sigma }
468 | [ tacdef_body(t) ] -> { t }
469 END
470
471 VERNAC COMMAND EXTEND VernacDeclareTacticDefinition
472 | #[ raw_attributes; ] [ "Ltac" ne_ltac_tacdef_body_list_sep(l, "with") ] => {
473 VtSideff (List.map (function
474 | TacticDefinition ({CAst.v=r},_) -> r
475 | TacticRedefinition (qid,_) -> qualid_basename qid) l, VtLater)
476 } -> {
477 Tacentries.register_ltac raw_attributes l;
478 }
479 END
480
481 VERNAC COMMAND EXTEND VernacPrintLtacs CLASSIFIED AS QUERY
482 | [ "Print" "Ltac" "Signatures" ] -> { Tacentries.print_ltacs () }
483 END