Debian Codegraph @

plugins/ssr/ssrparser.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 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 {
14
15 let defaultCast = Constr.DEFAULTcast
16 open Names
17 open Pp
18 open Procq
19 open Ltac_plugin
20 open Stdarg
21 open Tacarg
22 open Libnames
23 open Util
24 open Locus
25 open Tacexpr
26 open Pltac
27 open Ppconstr
28
29 open Namegen
30 open Tactypes
31 open Constrexpr
32 open Constrexpr_ops
33
34 open Proofview.Notations
35
36 open Ssrmatching_plugin.Ssrmatching
37
38 open Ssrprinters
39 open Ssrcommon
40 open Ssrequality
41 open Ssripats
42 open Libobject
43
44 (** Ssreflect load check. *)
45
46 (* To allow ssrcoq to be fully compatible with the "plain" Rocq, we only *)
47 (* turn on its incompatible features (the new rewrite syntax, and the *)
48 (* reserved identifiers) when the theory library (ssreflect.v) has *)
49 (* has actually been required, or is being defined. Because this check *)
50 (* needs to be done often (for each identifier lookup), we implement *)
51 (* some caching, repeating the test only when the environment changes. *)
52 (* We check for protect_term because it is the first constant loaded; *)
53 (* ssr_have would ultimately be a better choice. *)
54
55 let is_ssr_loaded = Pptactic.ssr_loaded
56
57 }
58
59 DECLARE PLUGIN "rocq-runtime.plugins.ssreflect"
60
61 (* The user is supposed to Require Import ssreflect or Require ssreflect *)
62 (* and Import ssreflect.SsrSyntax to obtain these keywords and as a *)
63 (* consequence the extended ssreflect grammar. *)
64 IGNORE KEYWORDS
65
66 {
67
68 let ssrtac_name name = {
69 mltac_plugin = "rocq-runtime.plugins.ssreflect";
70 mltac_tactic = "ssr" ^ name;
71 }
72
73 let ssrtac_entry name = {
74 mltac_name = ssrtac_name name;
75 mltac_index = 0;
76 }
77
78 let cache_tactic_notation (key, body, parule) =
79 Tacenv.register_alias key body;
80 Pptactic.declare_notation_tactic_pprule key parule
81
82 type tactic_grammar_obj = KerName.t * Tacenv.alias_tactic * Pptactic.pp_tactic
83
84 let inSsrGrammar : tactic_grammar_obj -> obj =
85 declare_object {(default_object "SsrGrammar") with
86 load_function = (fun _ -> cache_tactic_notation);
87 cache_function = cache_tactic_notation;
88 classify_function = (fun x -> Keep)}
89
90 let path = MPfile (DirPath.make @@ List.map Id.of_string ["ssreflect"; "ssr"; "Corelib"])
91
92 let register_ssrtac name f prods =
93 let open Pptactic in
94 Tacenv.register_ml_tactic (ssrtac_name name) [|f|];
95 let map id = Reference (Locus.ArgVar (CAst.make id)) in
96 let get_id = function
97 | TacTerm s -> None
98 | TacNonTerm (_, (_, ido)) -> ido in
99 let ids = List.map_filter get_id prods in
100 let tac = CAst.make (TacML (ssrtac_entry name, List.map map ids)) in
101 let key = KerName.make path (Id.of_string ("ssrparser_" ^ name)) in
102 let body = Tacenv.{ alias_args = ids; alias_body = tac; alias_deprecation = None } in
103 let parule = {
104 pptac_level = 0;
105 pptac_prods = prods
106 } in
107 let obj () =
108 Lib.add_leaf (inSsrGrammar (key, body, parule)) in
109 Mltop.(declare_cache_obj_full (interp_only_obj obj) "rocq-runtime.plugins.ssreflect");
110 key
111
112 let cast_arg wit v = Taccoerce.Value.cast (Genarg.topwit wit) v
113
114 }
115
116 {
117
118 let tacltop = (LevelLe 5)
119
120 let pr_ssrtacarg env sigma _ _ prt = prt env sigma tacltop
121
122 }
123
124 ARGUMENT EXTEND ssrtacarg TYPED AS tactic PRINTED BY { pr_ssrtacarg env sigma }
125 END
126 GRAMMAR EXTEND Gram
127 GLOBAL: ssrtacarg;
128 ssrtacarg: TOP [[ tac = ltac_expr LEVEL "5" -> { tac } ]];
129 END
130
131 (* Copy of ssrtacarg with LEVEL "3", useful for: "under ... do ..." *)
132 ARGUMENT EXTEND ssrtac3arg TYPED AS tactic PRINTED BY { pr_ssrtacarg env sigma }
133 END
134 GRAMMAR EXTEND Gram
135 GLOBAL: ssrtac3arg;
136 ssrtac3arg: TOP [[ tac = ltac_expr LEVEL "3" -> { tac } ]];
137 END
138
139 {
140
141 (* Lexically closed tactic for tacticals. *)
142 let pr_ssrtclarg env sigma _ _ prt tac = prt env sigma tacltop tac
143
144 }
145
146 ARGUMENT EXTEND ssrtclarg TYPED AS ssrtacarg
147 PRINTED BY { pr_ssrtclarg env sigma }
148 | [ ssrtacarg(tac) ] -> { tac }
149 END
150
151 {
152
153 open Genarg
154
155 (** Adding a new uninterpreted generic argument type *)
156 let add_genarg tag pr =
157 let wit = Genarg.make0 tag in
158 let tag = Geninterp.Val.create tag in
159 let glob ist x = (ist, x) in
160 let subst _ x = x in
161 let interp ist x = Ftactic.return (Geninterp.Val.Dyn (tag, x)) in
162 let gen_pr env sigma _ _ _ = pr env sigma in
163 let () = Genintern.register_intern0 wit glob in
164 let () = Gensubst.register_subst0 wit subst in
165 let () = Geninterp.register_interp0 wit interp in
166 let () = Geninterp.register_val0 wit (Some (Geninterp.Val.Base tag)) in
167 Pptactic.declare_extra_genarg_pprule wit gen_pr gen_pr gen_pr;
168 wit
169
170 open Ssrast
171 let pr_id = Ppconstr.pr_id
172 let pr_name = function Name id -> pr_id id | Anonymous -> str "_"
173 let pr_spc () = str " "
174 let pr_list = prlist_with_sep
175
176 (**************************** ssrhyp **************************************)
177
178 let pr_ssrhyp _ _ _ = pr_hyp
179
180 let wit_ssrhyprep = add_genarg "ssrhyprep" (fun env sigma -> pr_hyp)
181
182 let intern_hyp ist (SsrHyp (loc, id) as hyp) =
183 let _ = Tacintern.intern_genarg ist (in_gen (rawwit wit_hyp) CAst.(make ?loc id)) in
184 if not_section_id id then hyp else
185 hyp_err ?loc "Can't clear section hypothesis " id
186
187 open Procq.Prim
188
189 }
190
191 ARGUMENT EXTEND ssrhyp TYPED AS ssrhyprep PRINTED BY { pr_ssrhyp }
192 INTERPRETED BY { interp_hyp }
193 GLOBALIZED BY { intern_hyp }
194 | [ ident(id) ] -> { SsrHyp (Loc.tag ~loc id) }
195 END
196
197 {
198
199 let pr_hoi = hoik pr_hyp
200 let pr_ssrhoi _ _ _ = pr_hoi
201
202 let wit_ssrhoirep = add_genarg "ssrhoirep" (fun env sigma -> pr_hoi)
203
204 let intern_ssrhoi ist = function
205 | Hyp h -> Hyp (intern_hyp ist h)
206 | Id (SsrHyp (_, id)) as hyp ->
207 let _ = Tacintern.intern_genarg ist (in_gen (rawwit wit_ident) id) in
208 hyp
209
210 let interp_ssrhoi ist env sigma = function
211 | Hyp h -> let h' = interp_hyp ist env sigma h in Hyp h'
212 | Id (SsrHyp (loc, id)) ->
213 let id' = Tacinterp.interp_ident ist env sigma id in
214 Id (SsrHyp (loc, id'))
215
216 }
217
218 ARGUMENT EXTEND ssrhoi_hyp TYPED AS ssrhoirep PRINTED BY { pr_ssrhoi }
219 INTERPRETED BY { interp_ssrhoi }
220 GLOBALIZED BY { intern_ssrhoi }
221 | [ ident(id) ] -> { Hyp (SsrHyp(Loc.tag ~loc id)) }
222 END
223 ARGUMENT EXTEND ssrhoi_id TYPED AS ssrhoirep PRINTED BY { pr_ssrhoi }
224 INTERPRETED BY { interp_ssrhoi }
225 GLOBALIZED BY { intern_ssrhoi }
226 | [ ident(id) ] -> { Id (SsrHyp(Loc.tag ~loc id)) }
227 END
228
229 (** Rewriting direction *)
230
231 {
232
233 let wit_ssrdir = add_genarg "ssrdir" (fun env sigma -> pr_dir)
234
235 (** Simpl switch *)
236
237 let pr_ssrsimpl _ _ _ = pr_simpl
238
239 let wit_ssrsimplrep = add_genarg "ssrsimplrep" (fun env sigma -> pr_simpl)
240
241 let test_ssrslashnum b1 b2 kwstate strm =
242 match LStream.peek_nth kwstate 0 strm with
243 | Some (Tok.KEYWORD "/") ->
244 (match LStream.peek_nth kwstate 1 strm with
245 | Some (Tok.NUMBER _) when b1 ->
246 (match LStream.peek_nth kwstate 2 strm with
247 | Some (Tok.KEYWORD ("=" | "/=")) when not b2 -> Ok ()
248 | Some (Tok.KEYWORD "/") ->
249 if not b2 then Ok () else begin
250 match LStream.peek_nth kwstate 3 strm with
251 | Some (Tok.NUMBER _) -> Ok ()
252 | _ -> Error ()
253 end
254 | _ -> Error ())
255 | Some (Tok.KEYWORD "/") when not b1 ->
256 (match LStream.peek_nth kwstate 2 strm with
257 | Some (Tok.KEYWORD "=") when not b2 -> Ok ()
258 | Some (Tok.NUMBER _) when b2 ->
259 (match LStream.peek_nth kwstate 3 strm with
260 | Some (Tok.KEYWORD "=") -> Ok ()
261 | _ -> Error ())
262 | _ when not b2 -> Ok ()
263 | _ -> Error ())
264 | Some (Tok.KEYWORD "=") when not b1 && not b2 -> Ok ()
265 | _ -> Error ())
266 | Some (Tok.KEYWORD "//") when not b1 ->
267 (match LStream.peek_nth kwstate 1 strm with
268 | Some (Tok.KEYWORD "=") when not b2 -> Ok ()
269 | Some (Tok.NUMBER _) when b2 ->
270 (match LStream.peek_nth kwstate 2 strm with
271 | Some (Tok.KEYWORD "=") -> Ok ()
272 | _ -> Error ())
273 | _ when not b2 -> Ok ()
274 | _ -> Error ())
275 | _ -> Error ()
276
277 let test_ssrslashnum10 = test_ssrslashnum true false
278 let test_ssrslashnum11 = test_ssrslashnum true true
279 let test_ssrslashnum01 = test_ssrslashnum false true
280 let test_ssrslashnum00 = test_ssrslashnum false false
281
282 let negate_parser f x y =
283 match f x y with
284 | Error () -> Ok ()
285 | Ok _ -> Error ()
286
287 let test_not_ssrslashnum =
288 Procq.Entry.(of_parser
289 "test_not_ssrslashnum" { parser_fun = negate_parser test_ssrslashnum10 })
290 let test_ssrslashnum00 =
291 Procq.Entry.(of_parser "test_ssrslashnum01" { parser_fun = test_ssrslashnum00 })
292 let test_ssrslashnum10 =
293 Procq.Entry.(of_parser "test_ssrslashnum10" { parser_fun = test_ssrslashnum10 })
294 let test_ssrslashnum11 =
295 Procq.Entry.(of_parser "test_ssrslashnum11" { parser_fun = test_ssrslashnum11 })
296 let test_ssrslashnum01 =
297 Procq.Entry.(of_parser "test_ssrslashnum01" { parser_fun = test_ssrslashnum01 })
298
299 }
300
301 ARGUMENT EXTEND ssrsimpl_ne TYPED AS ssrsimplrep PRINTED BY { pr_ssrsimpl }
302 | [ "//=" ] -> { SimplCut (~-1,~-1) }
303 | [ "/=" ] -> { Simpl ~-1 }
304 END
305
306 (* Procq.Prim. *)
307 GRAMMAR EXTEND Gram
308 GLOBAL: ssrsimpl_ne;
309 ssrsimpl_ne: TOP [
310 [ test_ssrslashnum11; "/"; n = natural; "/"; m = natural; "=" -> { SimplCut(n,m) }
311 | test_ssrslashnum10; "/"; n = natural; "/" -> { Cut n }
312 | test_ssrslashnum10; "/"; n = natural; "=" -> { Simpl n }
313 | test_ssrslashnum10; "/"; n = natural; "/=" -> { SimplCut (n,~-1) }
314 | test_ssrslashnum10; "/"; n = natural; "/"; "=" -> { SimplCut (n,~-1) }
315 | test_ssrslashnum01; "//"; m = natural; "=" -> { SimplCut (~-1,m) }
316 | test_ssrslashnum00; "//" -> { Cut ~-1 }
317 ]];
318
319 END
320
321 {
322
323 let pr_ssrclear _ _ _ = pr_clear mt
324
325 }
326
327 ARGUMENT EXTEND ssrclear_ne TYPED AS ssrhyp list PRINTED BY { pr_ssrclear }
328 | [ "{" ne_ssrhyp_list(clr) "}" ] -> { check_hyps_uniq [] clr; clr }
329 END
330
331 ARGUMENT EXTEND ssrclear TYPED AS ssrclear_ne PRINTED BY { pr_ssrclear }
332 | [ ssrclear_ne(clr) ] -> { clr }
333 | [ ] -> { [] }
334 END
335
336 (** Indexes *)
337
338 (* Since SSR indexes are always positive numbers, we use the 0 value *)
339 (* to encode an omitted index. We reuse the in or_var type, but we *)
340 (* supply our own interpretation function, which checks for non *)
341 (* positive values, and allows the use of constr numerals, so that *)
342 (* e.g., "let n := eval compute in (1 + 3) in (do n!clear)" works. *)
343
344 {
345
346 let pr_index = function
347 | ArgVar {CAst.v=id} -> pr_id id
348 | ArgArg n when n > 0 -> int n
349 | _ -> mt ()
350 let pr_ssrindex _ _ _ = pr_index
351
352 let noindex = ArgArg 0
353
354 let check_index ?loc i =
355 if i > 0 then i else CErrors.user_err ?loc (str"Index not positive")
356 let mk_index ?loc = function
357 | ArgArg i -> ArgArg (check_index ?loc i)
358 | iv -> iv
359
360 let interp_index ist env sigma idx =
361 match idx with
362 | ArgArg _ -> idx
363 | ArgVar id ->
364 let i =
365 try
366 let v = Id.Map.find id.CAst.v ist.Tacinterp.lfun in
367 begin match Tacinterp.Value.to_int v with
368 | Some i -> i
369 | None ->
370 begin match Tacinterp.Value.to_constr v with
371 | Some c ->
372 let flags = PrintingFlags.Detype.current() in
373 let rc = Detyping.detype Detyping.Now ~flags env sigma c in
374 begin match Notation.uninterp_prim_token ~print_float:false rc ([], []) with
375 | Constrexpr.Number n, _ when NumTok.Signed.is_int n ->
376 int_of_string (NumTok.Signed.to_string n)
377 | _ -> raise Not_found
378 end
379 | None -> raise Not_found
380 end end
381 with e when CErrors.noncritical e -> CErrors.user_err ?loc:id.CAst.loc (str"Index not a number") in
382 ArgArg (check_index ?loc:id.CAst.loc i)
383
384 open Pltac
385
386 }
387
388 ARGUMENT EXTEND ssrindex PRINTED BY { pr_ssrindex }
389 INTERPRETED BY { interp_index }
390 END
391
392
393 (** Occurrence switch *)
394
395 (* The standard syntax of complemented occurrence lists involves a single *)
396 (* initial "-", e.g., {-1 3 5}. An initial *)
397 (* "+" may be used to indicate positive occurrences (the default). The *)
398 (* "+" is optional, except if the list of occurrences starts with a *)
399 (* variable or is empty (to avoid confusion with a clear switch). The *)
400 (* empty positive switch "{+}" selects no occurrences, while the empty *)
401 (* negative switch "{-}" selects all occurrences explicitly; this is the *)
402 (* default, but "{-}" prevents the implicit clear, and can be used to *)
403 (* force dependent elimination -- see ndefectelimtac below. *)
404
405 {
406
407 let pr_ssrocc _ _ _ = pr_occ
408
409 open Procq.Prim
410
411 }
412
413 ARGUMENT EXTEND ssrocc TYPED AS (bool * int list) option PRINTED BY { pr_ssrocc }
414 | [ natural(n) natural_list(occ) ] -> {
415 Some (false, List.map (check_index ~loc) (n::occ)) }
416 | [ "-" natural_list(occ) ] -> { Some (true, occ) }
417 | [ "+" natural_list(occ) ] -> { Some (false, occ) }
418 END
419
420
421 (* modality *)
422
423 {
424
425 let pr_mmod = function May -> str "?" | Must -> str "!" | Once -> mt ()
426
427 let wit_ssrmmod = add_genarg "ssrmmod" (fun env sigma -> pr_mmod)
428 let ssrmmod = Procq.create_generic_entry2 "ssrmmod" (Genarg.rawwit wit_ssrmmod);;
429
430 }
431
432 GRAMMAR EXTEND Gram
433 GLOBAL: ssrmmod;
434 ssrmmod: [[ "!" -> { Must } | LEFTQMARK -> { May } | "?" -> { May } ]];
435 END
436
437 (** Rewrite multiplier: !n ?n *)
438
439 {
440
441 let pr_mult (n, m) =
442 if n > 0 && m <> Once then int n ++ pr_mmod m else pr_mmod m
443
444 let pr_ssrmult _ _ _ = pr_mult
445
446 }
447
448 ARGUMENT EXTEND ssrmult_ne TYPED AS (int * ssrmmod) PRINTED BY { pr_ssrmult }
449 | [ natural(n) ssrmmod(m) ] -> { check_index ~loc n, m }
450 | [ ssrmmod(m) ] -> { notimes, m }
451 END
452
453 ARGUMENT EXTEND ssrmult TYPED AS ssrmult_ne PRINTED BY { pr_ssrmult }
454 | [ ssrmult_ne(m) ] -> { m }
455 | [ ] -> { nomult }
456 END
457
458 {
459
460 (** Discharge occ switch (combined occurrence / clear switch *)
461
462 let pr_docc = function
463 | None, occ -> pr_occ occ
464 | Some clr, _ -> pr_clear mt clr
465
466 let pr_ssrdocc _ _ _ = pr_docc
467
468 }
469
470 ARGUMENT EXTEND ssrdocc TYPED AS (ssrclear option * ssrocc) PRINTED BY { pr_ssrdocc }
471 | [ "{" ssrocc(occ) "}" ] -> { mkocc occ }
472 | [ "{" ssrhyp_list(clr) "}" ] -> { mkclr clr }
473 END
474
475 {
476
477 (* Old kinds of terms *)
478
479 let input_ssrtermkind kwstate strm = match LStream.peek_nth kwstate 0 strm with
480 | Some (Tok.KEYWORD "(") -> Ok InParens
481 | Some (Tok.KEYWORD "@") -> Ok WithAt
482 | Some _ -> Ok NoFlag
483 | None -> Error () (* NB this never happens, instead we get a EOI token *)
484
485 let ssrtermkind = Procq.Entry.(of_parser "ssrtermkind" { parser_fun = input_ssrtermkind })
486
487 (* New kinds of terms *)
488
489 let input_term_annotation kwstate strm =
490 match LStream.npeek kwstate 2 strm with
491 | Tok.KEYWORD "(" :: Tok.KEYWORD "(" :: _ -> Ok `DoubleParens
492 | Tok.KEYWORD "(" :: _ -> Ok `Parens
493 | Tok.KEYWORD "@" :: _ -> Ok `At
494 | _ -> Ok `None
495 let term_annotation =
496 Procq.Entry.(of_parser "term_annotation" { parser_fun = input_term_annotation })
497
498 (* terms *)
499
500 (** Terms parsing. ********************************************************)
501
502 (* Because we allow wildcards in term references, we need to stage the *)
503 (* interpretation of terms so that it occurs at the right time during *)
504 (* the execution of the tactic (e.g., so that we don't report an error *)
505 (* for a term that isn't actually used in the execution). *)
506 (* The term representation tracks whether the concrete initial term *)
507 (* started with an opening paren, which might avoid a conflict between *)
508 (* the ssrreflect term syntax and Gallina notation. *)
509
510 (* Old terms *)
511 let pr_ssrterm _ _ _ = pr_term
512 let glob_ssrterm gs = function
513 | k, (_, Some c) -> k, Tacintern.intern_constr gs c
514 | ct -> ct
515 let subst_ssrterm s (k, c) = k, Tacsubst.subst_glob_constr_and_expr s c
516 let interp_ssrterm _ _ _ t = t
517
518 open Procq.Constr
519
520 }
521
522 ARGUMENT EXTEND ssrterm
523 PRINTED BY { pr_ssrterm }
524 INTERPRETED BY { interp_ssrterm }
525 GLOBALIZED BY { glob_ssrterm } SUBSTITUTED BY { subst_ssrterm }
526 RAW_PRINTED BY { pr_ssrterm }
527 GLOB_PRINTED BY { pr_ssrterm }
528 END
529
530 GRAMMAR EXTEND Gram
531 GLOBAL: ssrterm;
532 ssrterm: TOP [[ k = ssrtermkind; c = Procq.Constr.constr -> { mk_term k c } ]];
533 END
534
535 (* New terms *)
536
537 {
538
539 let pp_ast_closure_term _ _ _ = pr_ast_closure_term
540
541 }
542
543 ARGUMENT EXTEND ast_closure_term
544 PRINTED BY { pp_ast_closure_term }
545 INTERPRETED BY { interp_ast_closure_term }
546 GLOBALIZED BY { glob_ast_closure_term }
547 SUBSTITUTED BY { subst_ast_closure_term }
548 RAW_PRINTED BY { pp_ast_closure_term }
549 GLOB_PRINTED BY { pp_ast_closure_term }
550 | [ term_annotation(a) constr(c) ] -> { mk_ast_closure_term a c }
551 END
552 ARGUMENT EXTEND ast_closure_lterm
553 PRINTED BY { pp_ast_closure_term }
554 INTERPRETED BY { interp_ast_closure_term }
555 GLOBALIZED BY { glob_ast_closure_term }
556 SUBSTITUTED BY { subst_ast_closure_term }
557 RAW_PRINTED BY { pp_ast_closure_term }
558 GLOB_PRINTED BY { pp_ast_closure_term }
559 | [ term_annotation(a) lconstr(c) ] -> { mk_ast_closure_term a c }
560 END
561
562 (* Old Views *)
563
564 {
565
566 let pr_view = pr_list mt (fun c -> str "/" ++ pr_term c)
567
568 let pr_ssrbwdview _ _ _ = pr_view
569
570 }
571
572 ARGUMENT EXTEND ssrbwdview TYPED AS ssrterm list
573 PRINTED BY { pr_ssrbwdview }
574 END
575
576 (* Procq *)
577 GRAMMAR EXTEND Gram
578 GLOBAL: ssrbwdview;
579 ssrbwdview: TOP [
580 [ test_not_ssrslashnum; "/"; c = Procq.Constr.constr -> { [mk_term NoFlag c] }
581 | test_not_ssrslashnum; "/"; c = Procq.Constr.constr; w = ssrbwdview -> {
582 (mk_term NoFlag c) :: w } ]];
583 END
584
585 (* New Views *)
586
587 {
588
589 type ssrfwdview = ast_closure_term list
590
591 let pr_ssrfwdview _ _ _ = pr_view2
592
593 }
594
595 ARGUMENT EXTEND ssrfwdview TYPED AS ast_closure_term list
596 PRINTED BY { pr_ssrfwdview }
597 END
598
599 (* Procq *)
600 GRAMMAR EXTEND Gram
601 GLOBAL: ssrfwdview;
602 ssrfwdview: TOP [
603 [ test_not_ssrslashnum; "/"; c = ast_closure_term -> { [c] }
604 | test_not_ssrslashnum; "/"; c = ast_closure_term; w = ssrfwdview -> { c :: w } ]];
605 END
606
607 (* ipats *)
608
609 {
610
611 let remove_loc x = x.CAst.v
612
613 let ipat_of_intro_pattern p = Tactypes.(
614 let rec ipat_of_intro_pattern = function
615 | IntroNaming (IntroIdentifier id) -> IPatId id
616 | IntroAction IntroWildcard -> IPatAnon Drop
617 | IntroAction (IntroOrAndPattern (IntroOrPattern iorpat)) ->
618 IPatCase (Regular(
619 List.map (List.map ipat_of_intro_pattern)
620 (List.map (List.map remove_loc) iorpat)))
621 | IntroAction (IntroOrAndPattern (IntroAndPattern iandpat)) ->
622 IPatCase
623 (Regular [List.map ipat_of_intro_pattern (List.map remove_loc iandpat)])
624 | IntroNaming IntroAnonymous -> IPatAnon (One None)
625 | IntroAction (IntroRewrite b) -> IPatRewrite (allocc, if b then L2R else R2L)
626 | IntroNaming (IntroFresh id) -> IPatAnon (One None)
627 | IntroAction (IntroApplyOn _) -> (* to do *) CErrors.user_err (Pp.str "TO DO")
628 | IntroAction (IntroInjection ips) ->
629 IPatInj [List.map ipat_of_intro_pattern (List.map remove_loc ips)]
630 | IntroForthcoming _ ->
631 (* Unable to determine which kind of ipat interp_introid could
632 * return [HH] *)
633 assert false
634 in
635 ipat_of_intro_pattern p
636 )
637
638 let rec map_ipat map_id map_ssrhyp map_ast_closure_term = function
639 | (IPatSimpl _ | IPatAnon _ | IPatRewrite _ | IPatNoop | IPatFastNondep) as x -> x
640 | IPatId id -> IPatId (map_id id)
641 | IPatAbstractVars l -> IPatAbstractVars (List.map map_id l)
642 | IPatClear clr -> IPatClear (List.map map_ssrhyp clr)
643 | IPatCase (Regular iorpat) -> IPatCase (Regular (List.map (List.map (map_ipat map_id map_ssrhyp map_ast_closure_term)) iorpat))
644 | IPatCase (Block(hat)) -> IPatCase (Block(map_block map_id hat))
645 | IPatDispatch (Regular iorpat) -> IPatDispatch (Regular (List.map (List.map (map_ipat map_id map_ssrhyp map_ast_closure_term)) iorpat))
646 | IPatDispatch (Block (hat)) -> IPatDispatch (Block(map_block map_id hat))
647 | IPatInj iorpat -> IPatInj (List.map (List.map (map_ipat map_id map_ssrhyp map_ast_closure_term)) iorpat)
648 | IPatView v -> IPatView (List.map map_ast_closure_term v)
649 and map_block map_id = function
650 | Prefix id -> Prefix (map_id id)
651 | SuffixId id -> SuffixId (map_id id)
652 | SuffixNum _ as x -> x
653
654 type ssripatrep = ssripat
655 let wit_ssripatrep = add_genarg "ssripatrep" (fun env sigma -> pr_ipat)
656
657 let pr_ssripat _ _ _ = pr_ipat
658 let pr_ssripats _ _ _ = pr_ipats
659 let pr_ssriorpat _ _ _ = pr_iorpat
660
661 let intern_ipat ist =
662 map_ipat
663 (fun id -> id)
664 (intern_hyp ist)
665 (glob_ast_closure_term ist)
666
667 let intern_ipats ist = List.map (intern_ipat ist)
668
669 let interp_intro_pattern = Tacinterp.interp_intro_pattern
670
671 let interp_introid ist env sigma id =
672 try IntroNaming (IntroIdentifier (hyp_id (interp_hyp ist env sigma (SsrHyp (Loc.tag id)))))
673 with e when CErrors.noncritical e ->
674 (interp_intro_pattern ist env sigma (CAst.make @@ IntroNaming (IntroIdentifier id))).CAst.v
675
676 let get_intro_id = function
677 | IntroNaming (IntroIdentifier id) -> id
678 | _ -> assert false
679
680 let rec add_intro_pattern_hyps ipat hyps =
681 let {CAst.loc; v=ipat} = ipat in
682 match ipat with
683 | IntroNaming (IntroIdentifier id) ->
684 if not_section_id id then SsrHyp (loc, id) :: hyps else
685 hyp_err ?loc "Can't delete section hypothesis " id
686 | IntroAction IntroWildcard -> hyps
687 | IntroAction (IntroOrAndPattern (IntroOrPattern iorpat)) ->
688 List.fold_right (List.fold_right add_intro_pattern_hyps) iorpat hyps
689 | IntroAction (IntroOrAndPattern (IntroAndPattern iandpat)) ->
690 List.fold_right add_intro_pattern_hyps iandpat hyps
691 | IntroNaming IntroAnonymous -> []
692 | IntroNaming (IntroFresh _) -> []
693 | IntroAction (IntroRewrite _) -> hyps
694 | IntroAction (IntroInjection ips) -> List.fold_right add_intro_pattern_hyps ips hyps
695 | IntroAction (IntroApplyOn (c,pat)) -> add_intro_pattern_hyps pat hyps
696 | IntroForthcoming _ ->
697 (* As in ipat_of_intro_pattern, was unable to determine which kind
698 of ipat interp_introid could return [HH] *) assert false
699
700 (* We interp the ipat using the standard ltac machinery for ids, since
701 * we have no clue what a name could be bound to (maybe another ipat) *)
702 let interp_ipat ist env sigma =
703 let ltacvar id = Id.Map.mem id ist.Tacinterp.lfun in
704 let interp_block = function
705 | Prefix id when ltacvar id ->
706 begin match interp_introid ist env sigma id with
707 | IntroNaming (IntroIdentifier id) -> Prefix id
708 | _ -> Ssrcommon.errorstrm Pp.(str"Variable " ++ Id.print id ++ str" in block intro pattern should be bound to an identifier.")
709 end
710 | SuffixId id when ltacvar id ->
711 begin match interp_introid ist env sigma id with
712 | IntroNaming (IntroIdentifier id) -> SuffixId id
713 | _ -> Ssrcommon.errorstrm Pp.(str"Variable " ++ Id.print id ++ str" in block intro pattern should be bound to an identifier.")
714 end
715 | x -> x in
716 let rec interp = function
717 | IPatId id when ltacvar id ->
718 ipat_of_intro_pattern (interp_introid ist env sigma id)
719 | IPatId _ as x -> x
720 | IPatClear clr ->
721 let add_hyps (SsrHyp (loc, id) as hyp) hyps =
722 if not (ltacvar id) then hyp :: hyps else
723 add_intro_pattern_hyps CAst.(make ?loc (interp_introid ist env sigma id)) hyps in
724 let clr' = List.fold_right add_hyps clr [] in
725 check_hyps_uniq [] clr';
726 IPatClear clr'
727 | IPatCase(Regular iorpat) ->
728 IPatCase(Regular(List.map (List.map interp) iorpat))
729 | IPatCase(Block(hat)) -> IPatCase(Block(interp_block hat))
730
731 | IPatDispatch(Regular iorpat) ->
732 IPatDispatch(Regular (List.map (List.map interp) iorpat))
733 | IPatDispatch(Block(hat)) -> IPatDispatch(Block(interp_block hat))
734
735 | IPatInj iorpat -> IPatInj (List.map (List.map interp) iorpat)
736 | IPatAbstractVars l ->
737 IPatAbstractVars (List.map get_intro_id (List.map (interp_introid ist env sigma) l))
738 | IPatView l -> IPatView (List.map (fun x -> interp_ast_closure_term ist
739 env sigma x) l)
740 | (IPatSimpl _ | IPatAnon _ | IPatRewrite _ | IPatNoop | IPatFastNondep) as x -> x
741 in
742 interp
743
744 let interp_ipats ist env sigma l = List.map (interp_ipat ist env sigma) l
745
746 let pushIPatRewrite = function
747 | pats :: orpat -> (IPatRewrite (allocc, L2R) :: pats) :: orpat
748 | [] -> []
749
750 let pushIPatNoop = function
751 | pats :: orpat -> (IPatNoop :: pats) :: orpat
752 | [] -> []
753
754 let test_ident_no_do =
755 let open Procq.Lookahead in
756 to_entry "test_ident_no_do" begin
757 lk_ident_except ["do"]
758 end
759
760 }
761
762 ARGUMENT EXTEND ident_no_do PRINTED BY { fun _ _ _ -> Names.Id.print }
763 END
764
765
766 GRAMMAR EXTEND Gram
767 GLOBAL: ident_no_do;
768 ident_no_do: TOP [ [ test_ident_no_do; id = IDENT -> { Id.of_string id } ] ];
769 END
770
771 ARGUMENT EXTEND ssripat TYPED AS ssripatrep list PRINTED BY { pr_ssripats }
772 INTERPRETED BY { interp_ipats }
773 GLOBALIZED BY { intern_ipats }
774 | [ "_" ] -> { [IPatAnon Drop] }
775 | [ "*" ] -> { [IPatAnon All] }
776 | [ ">" ] -> { [IPatFastNondep] }
777 | [ ident_no_do(id) ] -> { [IPatId id] }
778 | [ "?" ] -> { [IPatAnon (One None)] }
779 | [ "+" ] -> { [IPatAnon Temporary] }
780 | [ "++" ] -> { [IPatAnon Temporary; IPatAnon Temporary] }
781 | [ ssrsimpl_ne(sim) ] -> { [IPatSimpl sim] }
782 | [ ssrdocc(occ) "->" ] -> { match occ with
783 | Some [], _ -> CErrors.user_err ~loc (str"occ_switch expected")
784 | None, occ -> [IPatRewrite (occ, L2R)]
785 | Some clr, _ -> [IPatClear clr; IPatRewrite (allocc, L2R)] }
786 | [ ssrdocc(occ) "<-" ] -> { match occ with
787 | Some [], _ -> CErrors.user_err ~loc (str"occ_switch expected")
788 | None, occ -> [IPatRewrite (occ, R2L)]
789 | Some clr, _ -> [IPatClear clr; IPatRewrite (allocc, R2L)] }
790 | [ ssrdocc(occ) ] -> { match occ with
791 | Some cl, _ -> check_hyps_uniq [] cl; [IPatClear cl]
792 | _ -> CErrors.user_err ~loc (str"Only identifiers are allowed here") }
793 | [ "->" ] -> { [IPatRewrite (allocc, L2R)] }
794 | [ "<-" ] -> { [IPatRewrite (allocc, R2L)] }
795 | [ "-" ] -> { [IPatNoop] }
796 | [ "-/" "=" ] -> { [IPatNoop;IPatSimpl(Simpl ~-1)] }
797 | [ "-/=" ] -> { [IPatNoop;IPatSimpl(Simpl ~-1)] }
798 | [ "-/" "/" ] -> { [IPatNoop;IPatSimpl(Cut ~-1)] }
799 | [ "-//" ] -> { [IPatNoop;IPatSimpl(Cut ~-1)] }
800 | [ "-/" integer(n) "/" ] -> { [IPatNoop;IPatSimpl(Cut n)] }
801 | [ "-/" "/=" ] -> { [IPatNoop;IPatSimpl(SimplCut (~-1,~-1))] }
802 | [ "-//" "=" ] -> { [IPatNoop;IPatSimpl(SimplCut (~-1,~-1))] }
803 | [ "-//=" ] -> { [IPatNoop;IPatSimpl(SimplCut (~-1,~-1))] }
804 | [ "-/" integer(n) "/=" ] -> { [IPatNoop;IPatSimpl(SimplCut (n,~-1))] }
805 | [ "-/" integer(n) "/" integer (m) "=" ] ->
806 { [IPatNoop;IPatSimpl(SimplCut(n,m))] }
807 | [ ssrfwdview(v) ] -> { [IPatView v] }
808 | [ "[" ":" ident_list(idl) "]" ] -> { [IPatAbstractVars idl] }
809 | [ "[:" ident_list(idl) "]" ] -> { [IPatAbstractVars idl] }
810 END
811
812 ARGUMENT EXTEND ssripats TYPED AS ssripat PRINTED BY { pr_ssripats }
813 | [ ssripat(i) ssripats(tl) ] -> { i @ tl }
814 | [ ] -> { [] }
815 END
816
817 ARGUMENT EXTEND ssriorpat TYPED AS ssripat list PRINTED BY { pr_ssriorpat }
818 | [ ssripats(pats) "|" ssriorpat(orpat) ] -> { pats :: orpat }
819 | [ ssripats(pats) "|-" ">" ssriorpat(orpat) ] -> { pats :: pushIPatRewrite orpat }
820 | [ ssripats(pats) "|-" ssriorpat(orpat) ] -> { pats :: pushIPatNoop orpat }
821 | [ ssripats(pats) "|->" ssriorpat(orpat) ] -> { pats :: pushIPatRewrite orpat }
822 | [ ssripats(pats) "||" ssriorpat(orpat) ] -> { pats :: [] :: orpat }
823 | [ ssripats(pats) "|||" ssriorpat(orpat) ] -> { pats :: [] :: [] :: orpat }
824 | [ ssripats(pats) "||||" ssriorpat(orpat) ] -> { [pats; []; []; []] @ orpat }
825 | [ ssripats(pats) ] -> { [pats] }
826 END
827
828 {
829
830 let test_leftsquarebracket_equal =
831 let open Procq.Lookahead in
832 to_entry "test_leftsquarebracket_equal" begin
833 lk_kw "[" >> lk_kw "=" >> check_no_space
834 end
835
836 let reject_ssrhid kwstate strm =
837 match LStream.peek_nth kwstate 0 strm with
838 | Some (Tok.KEYWORD "[") ->
839 (match LStream.peek_nth kwstate 1 strm with
840 | Some (Tok.KEYWORD ":") -> Error ()
841 | _ -> Ok ())
842 | _ -> Ok ()
843
844 let test_nohidden = Procq.Entry.(of_parser "test_ssrhid" { parser_fun = reject_ssrhid })
845
846 let rec reject_binder crossed_paren k kwstate s =
847 match LStream.peek_nth kwstate k s with
848 | Some (Tok.KEYWORD "(") when not crossed_paren -> reject_binder true (k+1) kwstate s
849 | Some (Tok.IDENT _) when crossed_paren -> reject_binder true (k+1) kwstate s
850 | Some (Tok.KEYWORD ":" | Tok.KEYWORD ":=") when crossed_paren -> Error ()
851 | Some (Tok.KEYWORD ")") when crossed_paren -> Error ()
852 | _ -> if crossed_paren then Ok () else Error ()
853
854 let _test_nobinder = Procq.Entry.(of_parser "test_nobinder" { parser_fun = reject_binder false 0 })
855
856 }
857
858 ARGUMENT EXTEND ssrcpat TYPED AS ssripatrep PRINTED BY { pr_ssripat }
859 INTERPRETED BY { interp_ipat }
860 GLOBALIZED BY { intern_ipat }
861
862 END
863
864 (* Procq *)
865 GRAMMAR EXTEND Gram
866 GLOBAL: ssrcpat;
867 hat: [
868 [ "^"; id = ident -> { Prefix id }
869 | "^"; "~"; id = ident -> { SuffixId id }
870 | "^"; "~"; n = natural -> { SuffixNum n }
871 | "^~"; id = ident -> { SuffixId id }
872 | "^~"; n = natural -> { SuffixNum n }
873 ]];
874 ssrcpat: TOP [
875 [ test_leftsquarebracket_equal; test_nohidden; "["; "="; iorpat = ssriorpat; "]" -> {
876 IPatInj iorpat }
877 | test_nohidden; "["; hat_id = hat; "]" -> {
878 IPatCase (Block(hat_id)) }
879 | test_nohidden; "["; iorpat = ssriorpat; "]" -> {
880 IPatCase (Regular iorpat) } ]];
881 END
882
883 GRAMMAR EXTEND Gram
884 GLOBAL: ssripat;
885 ssripat: TOP [[ pat = ssrcpat -> { [pat] } ]];
886 END
887
888 ARGUMENT EXTEND ssripats_ne TYPED AS ssripat PRINTED BY { pr_ssripats }
889 | [ ssripat(i) ssripats(tl) ] -> { i @ tl }
890 END
891
892 (* subsets of patterns *)
893
894 {
895
896 (* TODO: review what this function does, it looks suspicious *)
897 let check_ssrhpats loc w_binders ipats =
898 let err_loc s = CErrors.user_err ~loc s in
899 let clr, ipats =
900 let opt_app = function None -> fun l -> Some l
901 | Some l1 -> fun l2 -> Some (l1 @ l2) in
902 let rec aux clr = function
903 | IPatClear cl :: tl -> aux (opt_app clr cl) tl
904 | tl -> clr, tl
905 in aux None ipats in
906 let simpl, ipats =
907 match List.rev ipats with
908 | IPatSimpl _ as s :: tl -> [s], List.rev tl
909 | _ -> [], ipats in
910 if simpl <> [] && not w_binders then
911 err_loc (str "No s-item allowed here: " ++ pr_ipats simpl);
912 let ipat, binders =
913 let rec loop ipat = function
914 | [] -> ipat, []
915 | ( IPatId _| IPatAnon _| IPatCase _ | IPatDispatch _ | IPatRewrite _ as i) :: tl ->
916 if w_binders then
917 if simpl <> [] && tl <> [] then
918 err_loc(str"binders XOR s-item allowed here: "++pr_ipats(tl@simpl))
919 else if not (List.for_all (function IPatId _ -> true | _ -> false) tl)
920 then err_loc (str "Only binders allowed here: " ++ pr_ipats tl)
921 else ipat @ [i], tl
922 else
923 if tl = [] then ipat @ [i], []
924 else err_loc (str "No binder or s-item allowed here: " ++ pr_ipats tl)
925 | hd :: tl -> loop (ipat @ [hd]) tl
926 in loop [] ipats in
927 ((clr, ipat), binders), simpl
928
929 let pr_clear_opt sep = function None -> mt () | Some x -> pr_clear sep x
930
931 let pr_hpats (((clr, ipat), binders), simpl) =
932 pr_clear_opt mt clr ++ pr_ipats ipat ++ pr_ipats binders ++ pr_ipats simpl
933 let pr_ssrhpats _ _ _ = pr_hpats
934 let pr_ssrhpats_wtransp _ _ _ (_, x) = pr_hpats x
935
936 }
937
938 ARGUMENT EXTEND ssrhpats TYPED AS (((ssrclear option * ssripat) * ssripat) * ssripat)
939 PRINTED BY { pr_ssrhpats }
940 | [ ssripats(i) ] -> { check_ssrhpats loc true i }
941 END
942
943 ARGUMENT EXTEND ssrhpats_wtransp
944 TYPED AS (bool * (((ssrclear option * ssripats) * ssripats) * ssripats))
945 PRINTED BY { pr_ssrhpats_wtransp }
946 | [ ssripats(i) ] -> { false,check_ssrhpats loc true i }
947 | [ ssripats(i) "@" ssripats(j) ] -> { true,check_ssrhpats loc true (i @ j) }
948 END
949
950 ARGUMENT EXTEND ssrhpats_nobs
951 TYPED AS (((ssrclear option * ssripats) * ssripats) * ssripats) PRINTED BY { pr_ssrhpats }
952 | [ ssripats(i) ] -> { check_ssrhpats loc false i }
953 END
954
955 ARGUMENT EXTEND ssrrpat TYPED AS ssripatrep PRINTED BY { pr_ssripat }
956 | [ "->" ] -> { IPatRewrite (allocc, L2R) }
957 | [ "<-" ] -> { IPatRewrite (allocc, R2L) }
958 END
959
960 {
961
962 let pr_intros sep intrs =
963 if intrs = [] then mt() else sep () ++ str "=>" ++ sep () ++ pr_ipats intrs
964 let pr_ssrintros _ _ _ = pr_intros mt
965
966 }
967
968 ARGUMENT EXTEND ssrintros_ne TYPED AS ssripat
969 PRINTED BY { pr_ssrintros }
970 | [ "=>" ssripats_ne(pats) ] -> { pats }
971 (* TODO | [ "=>" ">" ssripats_ne(pats) ] -> { IPatFastMode :: pats }
972 | [ "=>>" ssripats_ne(pats) ] -> [ IPatFastMode :: pats ] *)
973 END
974
975 ARGUMENT EXTEND ssrintros TYPED AS ssrintros_ne PRINTED BY { pr_ssrintros }
976 | [ ssrintros_ne(intrs) ] -> { intrs }
977 | [ ] -> { [] }
978 END
979
980 {
981
982 let pr_ssrintrosarg env sigma _ _ prt (tac, ipats) =
983 prt env sigma tacltop tac ++ pr_intros spc ipats
984
985 }
986
987 ARGUMENT EXTEND ssrintrosarg TYPED AS (tactic * ssrintros)
988 PRINTED BY { pr_ssrintrosarg env sigma }
989 END
990
991 {
992
993 (** Defined identifier *)
994 let pr_ssrfwdid id = pr_spc () ++ pr_id id
995
996 let pr_ssrfwdidx _ _ _ = pr_ssrfwdid
997
998 }
999
1000 (* We use a primitive parser for the head identifier of forward *)
1001 (* tactis to avoid syntactic conflicts with basic Rocq tactics. *)
1002 ARGUMENT EXTEND ssrfwdid TYPED AS ident PRINTED BY { pr_ssrfwdidx }
1003 END
1004
1005 {
1006
1007 let test_ssrfwdid =
1008 let open Procq.Lookahead in
1009 to_entry "test_ssrfwdid" begin
1010 lk_ident >> (lk_ident <+> lk_kws [":"; ":="; "("])
1011 end
1012
1013 }
1014
1015 GRAMMAR EXTEND Gram
1016 GLOBAL: ssrfwdid;
1017 ssrfwdid: TOP [[ test_ssrfwdid; id = Prim.ident -> { id } ]];
1018 END
1019
1020
1021 (* by *)
1022 (** Tactical arguments. *)
1023
1024 (* We have four kinds: simple tactics, [|]-bracketed lists, hints, and swaps *)
1025 (* The latter two are used in forward-chaining tactics (have, suffice, wlog) *)
1026 (* and subgoal reordering tacticals (; first & ; last), respectively. *)
1027
1028 {
1029
1030 let pr_ortacs env sigma prt =
1031 let rec pr_rec = function
1032 | [None] -> spc() ++ str "|" ++ spc()
1033 | None :: tacs -> spc() ++ str "|" ++ pr_rec tacs
1034 | Some tac :: tacs -> spc() ++ str "| " ++ prt env sigma tacltop tac ++ pr_rec tacs
1035 | [] -> mt() in
1036 function
1037 | [None] -> spc()
1038 | None :: tacs -> pr_rec tacs
1039 | Some tac :: tacs -> prt env sigma tacltop tac ++ pr_rec tacs
1040 | [] -> mt()
1041 let pr_ssrortacs env sigma _ _ = pr_ortacs env sigma
1042
1043 }
1044
1045 ARGUMENT EXTEND ssrortacs TYPED AS tactic option list PRINTED BY { pr_ssrortacs env sigma }
1046 | [ ssrtacarg(tac) "|" ssrortacs(tacs) ] -> { Some tac :: tacs }
1047 | [ ssrtacarg(tac) "|" ] -> { [Some tac; None] }
1048 | [ ssrtacarg(tac) ] -> { [Some tac] }
1049 | [ "|" ssrortacs(tacs) ] -> { None :: tacs }
1050 | [ "|" ] -> { [None; None] }
1051 END
1052
1053 {
1054
1055 let pr_hintarg env sigma prt = function
1056 | true, tacs -> hv 0 (str "[ " ++ pr_ortacs env sigma prt tacs ++ str " ]")
1057 | false, [Some tac] -> prt env sigma tacltop tac
1058 | _, _ -> mt()
1059
1060 let pr_ssrhintarg env sigma _ _ = pr_hintarg env sigma
1061
1062 }
1063
1064 ARGUMENT EXTEND ssrhintarg TYPED AS (bool * ssrortacs) PRINTED BY { pr_ssrhintarg env sigma }
1065 | [ "[" "]" ] -> { nullhint }
1066 | [ "[" ssrortacs(tacs) "]" ] -> { mk_orhint tacs }
1067 | [ ssrtacarg(arg) ] -> { mk_hint arg }
1068 END
1069
1070 (* Copy of ssrhintarg with LEVEL "3", useful for: "under ... do ..." *)
1071 ARGUMENT EXTEND ssrhint3arg TYPED AS (bool * ssrortacs) PRINTED BY { pr_ssrhintarg env sigma }
1072 | [ "[" "]" ] -> { nullhint }
1073 | [ "[" ssrortacs(tacs) "]" ] -> { mk_orhint tacs }
1074 | [ ssrtac3arg(arg) ] -> { mk_hint arg }
1075 END
1076
1077 ARGUMENT EXTEND ssrortacarg TYPED AS ssrhintarg PRINTED BY { pr_ssrhintarg env sigma }
1078 | [ "[" ssrortacs(tacs) "]" ] -> { mk_orhint tacs }
1079 END
1080
1081 {
1082
1083 let pr_hint env sigma prt arg =
1084 if arg = nohint then mt() else spc () ++ str "by" ++ spc () ++ pr_hintarg env sigma prt arg
1085 let pr_ssrhint env sigma _ _ = pr_hint env sigma
1086
1087 }
1088
1089 ARGUMENT EXTEND ssrhint TYPED AS ssrhintarg PRINTED BY { pr_ssrhint env sigma }
1090 | [ ] -> { nohint }
1091 END
1092 (** The "in" pseudo-tactical *)
1093
1094 (* We can't make "in" into a general tactical because this would create a *)
1095 (* crippling conflict with the ltac let .. in construct. Hence, we add *)
1096 (* explicitly an "in" suffix to all the extended tactics for which it is *)
1097 (* relevant (including move, case, elim) and to the extended do tactical *)
1098 (* below, which yields a general-purpose "in" of the form do [...] in ... *)
1099
1100 (* This tactical needs to come before the intro tactics because the latter *)
1101 (* must take precautions in order not to interfere with the discharged *)
1102 (* assumptions. This is especially difficult for discharged "let"s, which *)
1103 (* the default simpl and unfold tactics would erase blindly. *)
1104
1105 {
1106
1107 open Ssrmatching_plugin.G_ssrmatching
1108
1109 let pr_wgen = function
1110 | (clr, Some((id,k),None)) -> spc() ++ pr_clear mt clr ++ str k ++ pr_hoi id
1111 | (clr, Some((id,k),Some p)) ->
1112 spc() ++ pr_clear mt clr ++ str"(" ++ str k ++ pr_hoi id ++ str ":=" ++
1113 pr_cpattern p ++ str ")"
1114 | (clr, None) -> spc () ++ pr_clear mt clr
1115 let pr_ssrwgen _ _ _ = pr_wgen
1116
1117 }
1118
1119 (* no globwith for char *)
1120 ARGUMENT EXTEND ssrwgen
1121 TYPED AS (ssrclear * ((ssrhoi_hyp * string) * cpattern option) option)
1122 PRINTED BY { pr_ssrwgen }
1123 | [ ssrclear_ne(clr) ] -> { clr, None }
1124 | [ ssrhoi_hyp(hyp) ] -> { [], Some((hyp, " "), None) }
1125 | [ "@" ssrhoi_hyp(hyp) ] -> { [], Some((hyp, "@"), None) }
1126 | [ "(" ssrhoi_id(id) ":=" lcpattern(p) ")" ] ->
1127 { [], Some ((id," "),Some p) }
1128 | [ "(" ssrhoi_id(id) ")" ] -> { [], Some ((id,"("), None) }
1129 | [ "(@" ssrhoi_id(id) ":=" lcpattern(p) ")" ] ->
1130 { [], Some ((id,"@"),Some p) }
1131 | [ "(" "@" ssrhoi_id(id) ":=" lcpattern(p) ")" ] ->
1132 { [], Some ((id,"@"),Some p) }
1133 END
1134
1135 {
1136
1137 let pr_clseq = function
1138 | InGoal | InHyps -> mt ()
1139 | InSeqGoal -> str "|- *"
1140 | InHypsSeqGoal -> str " |- *"
1141 | InHypsGoal -> str " *"
1142 | InAll -> str "*"
1143 | InHypsSeq -> str " |-"
1144 | InAllHyps -> str "* |-"
1145
1146 let wit_ssrclseq = add_genarg "ssrclseq" (fun env sigma -> pr_clseq)
1147 let pr_clausehyps = pr_list pr_spc pr_wgen
1148 let pr_ssrclausehyps _ _ _ = pr_clausehyps
1149
1150 }
1151
1152 ARGUMENT EXTEND ssrclausehyps
1153 TYPED AS ssrwgen list PRINTED BY { pr_ssrclausehyps }
1154 | [ ssrwgen(hyp) "," ssrclausehyps(hyps) ] -> { hyp :: hyps }
1155 | [ ssrwgen(hyp) ssrclausehyps(hyps) ] -> { hyp :: hyps }
1156 | [ ssrwgen(hyp) ] -> { [hyp] }
1157 END
1158
1159 {
1160
1161 (* type ssrclauses = ssrahyps * ssrclseq *)
1162
1163 let pr_clauses (hyps, clseq) =
1164 if clseq = InGoal then mt ()
1165 else str "in " ++ pr_clausehyps hyps ++ pr_clseq clseq
1166 let pr_ssrclauses _ _ _ = pr_clauses
1167
1168 }
1169
1170 ARGUMENT EXTEND ssrclauses TYPED AS (ssrwgen list * ssrclseq)
1171 PRINTED BY { pr_ssrclauses }
1172 | [ "in" ssrclausehyps(hyps) "|-" "*" ] -> { hyps, InHypsSeqGoal }
1173 | [ "in" ssrclausehyps(hyps) "|-" ] -> { hyps, InHypsSeq }
1174 | [ "in" ssrclausehyps(hyps) "*" ] -> { hyps, InHypsGoal }
1175 | [ "in" ssrclausehyps(hyps) ] -> { hyps, InHyps }
1176 | [ "in" "|-" "*" ] -> { [], InSeqGoal }
1177 | [ "in" "*" ] -> { [], InAll }
1178 | [ "in" "*" "|-" ] -> { [], InAllHyps }
1179 | [ ] -> { [], InGoal }
1180 END
1181
1182
1183 {
1184
1185 (** Definition value formatting *)
1186
1187 (* We use an intermediate structure to correctly render the binder list *)
1188 (* abbreviations. We use a list of hints to extract the binders and *)
1189 (* base term from a term, for the two first levels of representation of *)
1190 (* of constr terms. *)
1191
1192 let pr_binder prl = function
1193 | Bvar x ->
1194 pr_name x
1195 | Bdecl (xs, t) ->
1196 str "(" ++ pr_list pr_spc pr_name xs ++ str " : " ++ prl t ++ str ")"
1197 | Bdef (x, None, v) ->
1198 str "(" ++ pr_name x ++ str " := " ++ prl v ++ str ")"
1199 | Bdef (x, Some t, v) ->
1200 str "(" ++ pr_name x ++ str " : " ++ prl t ++
1201 str " := " ++ prl v ++ str ")"
1202 | Bstruct x ->
1203 str "{struct " ++ pr_name x ++ str "}"
1204 | Bcast t ->
1205 str ": " ++ prl t
1206
1207 let rec format_local_binders h0 bl0 = match h0, bl0 with
1208 | BFvar :: h, CLocalAssum ([{CAst.v=x}], _, _, _) :: bl ->
1209 Bvar x :: format_local_binders h bl
1210 | BFdecl _ :: h, CLocalAssum (lxs, _, _, t) :: bl ->
1211 Bdecl (List.map (fun x -> x.CAst.v) lxs, t) :: format_local_binders h bl
1212 | BFdef :: h, CLocalDef ({CAst.v=x}, _, v, oty) :: bl ->
1213 Bdef (x, oty, v) :: format_local_binders h bl
1214 | _ -> []
1215
1216 let rec format_constr_expr h0 c0 = let open CAst in match h0, c0 with
1217 | BFvar :: h, { v = CLambdaN ([CLocalAssum([{CAst.v=x}], _, _, _)], c) } ->
1218 let bs, c' = format_constr_expr h c in
1219 Bvar x :: bs, c'
1220 | BFdecl _:: h, { v = CLambdaN ([CLocalAssum(lxs, _, _, t)], c) } ->
1221 let bs, c' = format_constr_expr h c in
1222 Bdecl (List.map (fun x -> x.CAst.v) lxs, t) :: bs, c'
1223 | BFdef :: h, { v = CLetIn({CAst.v=x}, v, oty, c) } ->
1224 let bs, c' = format_constr_expr h c in
1225 Bdef (x, oty, v) :: bs, c'
1226 | [BFcast], { v = CCast (c, defaultCast, t) } ->
1227 [Bcast t], c
1228 | BFrec (has_str, has_cast) :: h,
1229 { v = CFix ( _, [_, _, Some {CAst.v = CStructRec locn}, bl, t, c]) } ->
1230 let bs = format_local_binders h bl in
1231 let bstr = if has_str then [Bstruct (Name locn.CAst.v)] else [] in
1232 bs @ bstr @ (if has_cast then [Bcast t] else []), c
1233 | BFrec (_, has_cast) :: h, { v = CCoFix ( _, [_, _, bl, t, c]) } ->
1234 format_local_binders h bl @ (if has_cast then [Bcast t] else []), c
1235 | _, c ->
1236 [], c
1237
1238 (** Forward chaining argument *)
1239
1240 (* There are three kinds of forward definitions: *)
1241 (* - Hint: type only, cast to Type, may have proof hint. *)
1242 (* - Have: type option + value, no space before type *)
1243 (* - Pose: binders + value, space before binders. *)
1244
1245 let pr_fwdkind = function
1246 | FwdHint (s,_) -> str (s ^ " ") | _ -> str " :=" ++ spc ()
1247 let pr_fwdfmt (fk, _ : ssrfwdfmt) = pr_fwdkind fk
1248
1249 let wit_ssrfwdfmt = add_genarg "ssrfwdfmt" (fun env sigma -> pr_fwdfmt)
1250
1251 (* type ssrfwd = ssrfwdfmt * ssrterm *)
1252
1253 let mkFwdVal fk c = ((fk, []), c)
1254 let mkssrFwdVal fk c = ((fk, []), (c,None))
1255
1256 let same_ist { interp_env = x } { interp_env = y } =
1257 match x,y with
1258 | None, None -> true
1259 | Some a, Some b -> a == b
1260 | _ -> false
1261
1262 let mkFwdCast fk ?loc ?c t =
1263 let c = match c with
1264 | None -> mkCHole loc
1265 | Some c -> assert (same_ist t c); c.body in
1266 ((fk, [BFcast]),
1267 { t with annotation = `None;
1268 body = (CAst.make ?loc @@ CCast (c, Some defaultCast, t.body)) })
1269
1270 let mkssrFwdCast fk loc t c = ((fk, [BFcast]), (c, Some t))
1271
1272 let mkFwdHint s t =
1273 let loc = Constrexpr_ops.constr_loc t.body in
1274 mkFwdCast (FwdHint (s,false)) ?loc t
1275 let mkFwdHintNoTC s t =
1276 let loc = Constrexpr_ops.constr_loc t.body in
1277 mkFwdCast (FwdHint (s,true)) ?loc t
1278
1279 let pr_gen_fwd prval prc prlc fk (bs, c) =
1280 let prc s = str s ++ spc () ++ prval prc prlc c in
1281 match fk, bs with
1282 | FwdHint (s,_), [Bcast t] -> str s ++ spc () ++ prlc t
1283 | FwdHint (s,_), _ -> prc (s ^ "(* typeof *)")
1284 | FwdHave, [Bcast t] -> str ":" ++ spc () ++ prlc t ++ prc " :="
1285 | _, [] -> prc " :="
1286 | _, _ -> spc () ++ pr_list spc (pr_binder prlc) bs ++ prc " :="
1287
1288 let pr_fwd_guarded prval prval' = function
1289 | (fk, h), c ->
1290 pr_gen_fwd prval pr_constr_expr prl_constr_expr fk (format_constr_expr h c.body)
1291
1292 let pr_unguarded prc prlc = prlc
1293
1294 let pr_fwd = pr_fwd_guarded pr_unguarded pr_unguarded
1295 let pr_ssrfwd _ _ _ = pr_fwd
1296
1297 }
1298
1299 ARGUMENT EXTEND ssrfwd TYPED AS (ssrfwdfmt * ast_closure_lterm) PRINTED BY { pr_ssrfwd }
1300 | [ ":=" ast_closure_lterm(c) ] -> { mkFwdVal FwdPose c }
1301 | [ ":" ast_closure_lterm (t) ":=" ast_closure_lterm(c) ] -> { mkFwdCast FwdPose ~loc t ~c }
1302 END
1303
1304 (** Independent parsing for binders *)
1305
1306 (* The pose, pose fix, and pose cofix tactics use these internally to *)
1307 (* parse argument fragments. *)
1308
1309 {
1310
1311 let pr_ssrbvar env sigma prc _ _ v = prc env sigma v
1312
1313 }
1314
1315 ARGUMENT EXTEND ssrbvar TYPED AS constr PRINTED BY { pr_ssrbvar env sigma }
1316 | [ ident(id) ] -> { mkCVar ~loc id }
1317 | [ "_" ] -> { mkCHole (Some loc) }
1318 END
1319
1320 {
1321
1322 let bvar_lname = let open CAst in function
1323 | { v = CRef (qid, _) } when qualid_is_ident qid ->
1324 CAst.make ?loc:qid.CAst.loc @@ Name (qualid_basename qid)
1325 | { loc = loc } -> CAst.make ?loc Anonymous
1326
1327 let pr_ssrbinder env sigma prc _ _ (_, c) = prc env sigma c
1328
1329 }
1330
1331 ARGUMENT EXTEND ssrbinder TYPED AS (ssrfwdfmt * constr) PRINTED BY { pr_ssrbinder env sigma }
1332 | [ ssrbvar(bv) ] ->
1333 { let { CAst.loc=xloc } as x = bvar_lname bv in
1334 (FwdPose, [BFvar]),
1335 CAst.make ~loc @@ CLambdaN ([CLocalAssum([x],None,Default Glob_term.Explicit,mkCHole xloc)],mkCHole (Some loc)) }
1336 | [ "(" ssrbvar(bv) ")" ] ->
1337 { let { CAst.loc=xloc } as x = bvar_lname bv in
1338 (FwdPose, [BFvar]),
1339 CAst.make ~loc @@ CLambdaN ([CLocalAssum([x],None,Default Glob_term.Explicit,mkCHole xloc)],mkCHole (Some loc)) }
1340 | [ "(" ssrbvar(bv) ":" lconstr(t) ")" ] ->
1341 { let x = bvar_lname bv in
1342 (FwdPose, [BFdecl 1]),
1343 CAst.make ~loc @@ CLambdaN ([CLocalAssum([x], None, Default Glob_term.Explicit, t)], mkCHole (Some loc)) }
1344 | [ "(" ssrbvar(bv) ne_ssrbvar_list(bvs) ":" lconstr(t) ")" ] ->
1345 { let xs = List.map bvar_lname (bv :: bvs) in
1346 let n = List.length xs in
1347 (FwdPose, [BFdecl n]),
1348 CAst.make ~loc @@ CLambdaN ([CLocalAssum (xs, None, Default Glob_term.Explicit, t)], mkCHole (Some loc)) }
1349 | [ "(" ssrbvar(id) ":" lconstr(t) ":=" lconstr(v) ")" ] ->
1350 { (FwdPose,[BFdef]), CAst.make ~loc @@ CLetIn (bvar_lname id, v, Some t, mkCHole (Some loc)) }
1351 | [ "(" ssrbvar(id) ":=" lconstr(v) ")" ] ->
1352 { (FwdPose,[BFdef]), CAst.make ~loc @@ CLetIn (bvar_lname id, v, None, mkCHole (Some loc)) }
1353 END
1354
1355 GRAMMAR EXTEND Gram
1356 GLOBAL: ssrbinder;
1357 ssrbinder: TOP [
1358 [ ["of" -> { () } | "&" -> { () } ]; c = term LEVEL "99" -> {
1359 (FwdPose, [BFvar]),
1360 CAst.make ~loc @@ CLambdaN ([CLocalAssum ([CAst.make ~loc Anonymous],None,Default Glob_term.Explicit,c)],mkCHole (Some loc)) } ]
1361 ];
1362 END
1363
1364 {
1365
1366 let rec binders_fmts = function
1367 | ((_, h), _) :: bs -> h @ binders_fmts bs
1368 | _ -> []
1369
1370 let push_binders c2 bs =
1371 let loc2 = constr_loc c2 in let mkloc loc1 = Loc.merge_opt loc1 loc2 in
1372 let open CAst in
1373 let rec loop ty c = function
1374 | (_, { loc = loc1; v = CLambdaN (b, _) } ) :: bs when ty ->
1375 CAst.make ?loc:(mkloc loc1) @@ CProdN (b, loop ty c bs)
1376 | (_, { loc = loc1; v = CLambdaN (b, _) } ) :: bs ->
1377 CAst.make ?loc:(mkloc loc1) @@ CLambdaN (b, loop ty c bs)
1378 | (_, { loc = loc1; v = CLetIn (x, v, oty, _) } ) :: bs ->
1379 CAst.make ?loc:(mkloc loc1) @@ CLetIn (x, v, oty, loop ty c bs)
1380 | [] -> c
1381 | _ -> anomaly "binder not a lambda nor a let in" in
1382 match c2 with
1383 | { loc; v = CCast (ct, defaultCast, cty) } ->
1384 CAst.make ?loc @@ (CCast (loop false ct bs, defaultCast, (loop true cty bs)))
1385 | ct -> loop false ct bs
1386
1387 let rec fix_binders = let open CAst in function
1388 | (_, { v = CLambdaN ([CLocalAssum(xs, _, _, t)], _) } ) :: bs ->
1389 CLocalAssum (xs, None, Default Glob_term.Explicit, t) :: fix_binders bs
1390 | (_, { v = CLetIn (x, v, oty, _) } ) :: bs ->
1391 CLocalDef (x, None, v, oty) :: fix_binders bs
1392 | _ -> []
1393
1394 let pr_ssrstruct _ _ _ = function
1395 | Some id -> str "{struct " ++ pr_id id ++ str "}"
1396 | None -> mt ()
1397
1398 }
1399
1400 ARGUMENT EXTEND ssrstruct TYPED AS ident option PRINTED BY { pr_ssrstruct }
1401 | [ "{" "struct" ident(id) "}" ] -> { Some id }
1402 | [ ] -> { None }
1403 END
1404
1405 (** The "pose" tactic *)
1406
1407 (* The plain pose form. *)
1408
1409 {
1410
1411 let bind_fwd bs ((fk, h), c) =
1412 (fk,binders_fmts bs @ h), { c with body = push_binders c.body bs }
1413
1414 }
1415
1416 ARGUMENT EXTEND ssrposefwd TYPED AS ssrfwd PRINTED BY { pr_ssrfwd }
1417 | [ ssrbinder_list(bs) ssrfwd(fwd) ] -> { bind_fwd bs fwd }
1418 END
1419
1420 (* The pose fix form. *)
1421
1422 {
1423
1424 let pr_ssrfixfwd _ _ _ (id, fwd) = str " fix " ++ pr_id id ++ pr_fwd fwd
1425
1426 let bvar_locid = function
1427 | { CAst.v = CRef (qid, _) } when qualid_is_ident qid ->
1428 CAst.make ?loc:qid.CAst.loc (qualid_basename qid)
1429 | _ -> CErrors.user_err (Pp.str "Missing identifier after \"(co)fix\"")
1430
1431 }
1432
1433 ARGUMENT EXTEND ssrfixfwd TYPED AS (ident * ssrfwd) PRINTED BY { pr_ssrfixfwd }
1434 | [ "fix" ssrbvar(bv) ssrbinder_list(bs) ssrstruct(sid) ssrfwd(fwd) ] ->
1435 { let { CAst.v=id } as lid = bvar_locid bv in
1436 let (fk, h), ac = fwd in
1437 let c = ac.body in
1438 let has_cast, t', c' = match format_constr_expr h c with
1439 | [Bcast t'], c' -> true, t', c'
1440 | _ -> false, mkCHole (constr_loc c), c in
1441 let lb = fix_binders bs in
1442 let has_struct, i =
1443 let rec loop = function
1444 | {CAst.loc=l'; v=Name id'} :: _ when Option.equal Id.equal sid (Some id') ->
1445 true, CAst.make ?loc:l' id'
1446 | [{CAst.loc=l';v=Name id'}] when sid = None ->
1447 false, CAst.make ?loc:l' id'
1448 | _ :: bn -> loop bn
1449 | [] -> CErrors.user_err (Pp.str "Bad structural argument") in
1450 loop (names_of_local_assums lb) in
1451 let h' = BFrec (has_struct, has_cast) :: binders_fmts bs in
1452 let fix = CAst.make ~loc @@ CFix (lid, [lid, None, (Some (CAst.make (CStructRec i))), lb, t', c']) in
1453 id, ((fk, h'), { ac with body = fix }) }
1454 END
1455
1456
1457 (* The pose cofix form. *)
1458
1459 {
1460
1461 let pr_ssrcofixfwd _ _ _ (id, fwd) = str " cofix " ++ pr_id id ++ pr_fwd fwd
1462
1463 }
1464
1465 ARGUMENT EXTEND ssrcofixfwd TYPED AS ssrfixfwd PRINTED BY { pr_ssrcofixfwd }
1466 | [ "cofix" ssrbvar(bv) ssrbinder_list(bs) ssrfwd(fwd) ] ->
1467 { let { CAst.v=id } as lid = bvar_locid bv in
1468 let (fk, h), ac = fwd in
1469 let c = ac.body in
1470 let has_cast, t', c' = match format_constr_expr h c with
1471 | [Bcast t'], c' -> true, t', c'
1472 | _ -> false, mkCHole (constr_loc c), c in
1473 let h' = BFrec (false, has_cast) :: binders_fmts bs in
1474 let cofix = CAst.make ~loc @@ CCoFix (lid, [lid, None, fix_binders bs, t', c']) in
1475 id, ((fk, h'), { ac with body = cofix })
1476 }
1477 END
1478
1479 {
1480
1481 let pr_ssrsetfwd _ _ _ (((fk,_),(c,t)), docc) =
1482 let t = Option.default [] @@ Option.map (fun t -> [Bcast t]) t in
1483 pr_gen_fwd (fun _ _ c -> Pp.(pr_docc docc ++ pr_cpattern c))
1484 (fun _ -> mt()) pr_ast_closure_term fk (t, c)
1485
1486 }
1487
1488 ARGUMENT EXTEND ssrsetfwd
1489 TYPED AS ((ssrfwdfmt * (lcpattern * ast_closure_lterm option)) * ssrdocc)
1490 PRINTED BY { pr_ssrsetfwd }
1491 | [ ":" ast_closure_lterm(t) ":=" "{" ssrocc(occ) "}" cpattern(c) ] ->
1492 { mkssrFwdCast FwdPose loc t c, mkocc occ }
1493 | [ ":" ast_closure_lterm(t) ":=" lcpattern(c) ] ->
1494 { mkssrFwdCast FwdPose loc t c, nodocc }
1495 | [ ":=" "{" ssrocc(occ) "}" cpattern(c) ] ->
1496 { mkssrFwdVal FwdPose c, mkocc occ }
1497 | [ ":=" lcpattern(c) ] -> { mkssrFwdVal FwdPose c, nodocc }
1498 END
1499
1500 {
1501
1502 let pr_ssrhavefwd env sigma _ _ prt (fwd, hint) = pr_fwd fwd ++ pr_hint env sigma prt hint
1503
1504 }
1505
1506 ARGUMENT EXTEND ssrhavefwd TYPED AS (ssrfwd * ssrhint) PRINTED BY { pr_ssrhavefwd env sigma }
1507 | [ ":" ast_closure_lterm(t) ssrhint(hint) ] -> { mkFwdHint ":" t, hint }
1508 | [ ":" ast_closure_lterm(t) ":=" ast_closure_lterm(c) ] -> { mkFwdCast FwdHave ~loc t ~c, nohint }
1509 | [ ":" ast_closure_lterm(t) ":=" ] -> { mkFwdHintNoTC ":" t, nohint }
1510 | [ ":=" ast_closure_lterm(c) ] -> { mkFwdVal FwdHave c, nohint }
1511 END
1512
1513 {
1514
1515 let intro_id_to_binder = List.map (function
1516 | IPatId id ->
1517 let { CAst.loc=xloc } as x = bvar_lname (mkCVar id) in
1518 (FwdPose, [BFvar]),
1519 CAst.make @@ CLambdaN ([CLocalAssum([x], None, Default Glob_term.Explicit, mkCHole xloc)],
1520 mkCHole None)
1521 | _ -> anomaly "non-id accepted as binder")
1522
1523 let binder_to_intro_id = CAst.(List.map (function
1524 | (FwdPose, [BFvar]), { v = CLambdaN ([CLocalAssum(ids,_,_,_)],_) }
1525 | (FwdPose, [BFdecl _]), { v = CLambdaN ([CLocalAssum(ids,_,_,_)],_) } ->
1526 List.map (function {v=Name id} -> IPatId id | _ -> IPatAnon (One None)) ids
1527 | (FwdPose, [BFdef]), { v = CLetIn ({v=Name id},_,_,_) } -> [IPatId id]
1528 | (FwdPose, [BFdef]), { v = CLetIn ({v=Anonymous},_,_,_) } -> [IPatAnon (One None)]
1529 | _ -> anomaly "ssrbinder is not a binder"))
1530
1531 let pr_ssrhavefwdwbinders env sigma _ _ prt (tr,((hpats, (fwd, hint)))) =
1532 pr_hpats hpats ++ pr_fwd fwd ++ pr_hint env sigma prt hint
1533
1534 }
1535
1536 ARGUMENT EXTEND ssrhavefwdwbinders
1537 TYPED AS (bool * (ssrhpats * (ssrfwd * ssrhint)))
1538 PRINTED BY { pr_ssrhavefwdwbinders env sigma }
1539 | [ ssrhpats_wtransp(trpats) ssrbinder_list(bs) ssrhavefwd(fwd) ] ->
1540 { let tr, pats = trpats in
1541 let ((clr, pats), binders), simpl = pats in
1542 let allbs = intro_id_to_binder binders @ bs in
1543 let allbinders = binders @ List.flatten (binder_to_intro_id bs) in
1544 let hint = bind_fwd allbs (fst fwd), snd fwd in
1545 tr, ((((clr, pats), allbinders), simpl), hint) }
1546 END
1547
1548 {
1549
1550 let pr_ssrdoarg env sigma prc _ prt (((n, m), tac), clauses) =
1551 pr_index n ++ pr_mmod m ++ pr_hintarg env sigma prt tac ++ pr_clauses clauses
1552
1553 }
1554
1555 ARGUMENT EXTEND ssrdoarg
1556 TYPED AS (((ssrindex * ssrmmod) * ssrhintarg) * ssrclauses)
1557 PRINTED BY { pr_ssrdoarg env sigma }
1558 END
1559
1560 {
1561
1562 (* type ssrseqarg = ssrindex * (ssrtacarg * ssrtac option) *)
1563
1564 let pr_seqtacarg env sigma prt = function
1565 | (is_first, []), _ -> str (if is_first then "first" else "last")
1566 | tac, Some dtac ->
1567 hv 0 (pr_hintarg env sigma prt tac ++ spc() ++ str "|| " ++ prt env sigma tacltop dtac)
1568 | tac, _ -> pr_hintarg env sigma prt tac
1569
1570 let pr_ssrseqarg env sigma _ _ prt = function
1571 | ArgArg 0, tac -> pr_seqtacarg env sigma prt tac
1572 | i, tac -> pr_index i ++ str " " ++ pr_seqtacarg env sigma prt tac
1573
1574 }
1575
1576 (* We must parse the index separately to resolve the conflict with *)
1577 (* an unindexed tactic. *)
1578 ARGUMENT EXTEND ssrseqarg TYPED AS (ssrindex * (ssrhintarg * tactic option))
1579 PRINTED BY { pr_ssrseqarg env sigma }
1580
1581 END
1582
1583 {
1584
1585 let sq_brace_tacnames =
1586 ["first"; "solve"; "do"; "rewrite"; "have"; "suffices"; "wlog"]
1587 (* "by" is a keyword *)
1588
1589 let test_ssrseqvar =
1590 let open Procq.Lookahead in
1591 to_entry "test_ssrseqvar" begin
1592 lk_ident_except sq_brace_tacnames >> (lk_kws ["[";"first";"last"])
1593 end
1594
1595 let swaptacarg (loc, b) = (b, []), Some (CAst.make ~loc (TacId []))
1596
1597 let ssrorelse = Entry.make "ssrorelse"
1598
1599 }
1600
1601 GRAMMAR EXTEND Gram
1602 GLOBAL: ssrorelse ssrseqarg;
1603 ssrseqidx: [
1604 [ test_ssrseqvar; id = Prim.ident -> { ArgVar (CAst.make ~loc id) }
1605 | n = Prim.natural -> { ArgArg (check_index ~loc n) }
1606 ] ];
1607 ssrswap: [[ IDENT "first" -> { loc, true } | IDENT "last" -> { loc, false } ]];
1608 ssrorelse: [[ "||"; tac = ltac_expr LEVEL "2" -> { tac } ]];
1609 ssrseqarg: TOP [
1610 [ arg = ssrswap -> { noindex, swaptacarg arg }
1611 | i = ssrseqidx; tac = ssrortacarg; def = OPT ssrorelse -> { i, (tac, def) }
1612 | i = ssrseqidx; arg = ssrswap -> { i, swaptacarg arg }
1613 | tac = ltac_expr LEVEL "3" -> { noindex, (mk_hint tac, None) }
1614 ] ];
1615 END
1616
1617 {
1618
1619 let ltac_expr = Pltac.ltac_expr
1620
1621 }
1622
1623 (** 1. Utilities *)
1624
1625 (** Name generation *)
1626
1627 (* Since Rocq now does repeated internal checks of its external lexical *)
1628 (* rules, we now need to carve ssreflect reserved identifiers out of *)
1629 (* out of the user namespace. We use identifiers of the form _id_ for *)
1630 (* this purpose, e.g., we "anonymize" an identifier id as _id_, adding *)
1631 (* an extra leading _ if this might clash with an internal identifier. *)
1632 (* We check for ssreflect identifiers in the ident grammar rule; *)
1633 (* when the ssreflect Module is present this is normally an error, *)
1634 (* but we provide a compatibility flag to reduce this to a warning. *)
1635
1636 {
1637
1638 let { Goptions.get = ssr_reserved_ids } =
1639 Goptions.declare_bool_option_and_ref ~stage:Synterp ~key:["SsrIdents"] ~value:true ()
1640
1641 let is_ssr_reserved s =
1642 let n = String.length s in n > 2 && s.[0] = '_' && s.[n - 1] = '_'
1643
1644 let ssr_id_of_string loc s =
1645 if is_ssr_reserved s && is_ssr_loaded () then begin
1646 if ssr_reserved_ids() then
1647 CErrors.user_err ~loc (str ("The identifier " ^ s ^ " is reserved."))
1648 else if is_internal_name s then
1649 Feedback.msg_warning (str ("Conflict between " ^ s ^ " and ssreflect internal names."))
1650 else Feedback.msg_warning (str (
1651 "The name " ^ s ^ " fits the _xxx_ format used for anonymous variables.\n"
1652 ^ "Scripts with explicit references to anonymous variables are fragile."))
1653 end; Id.of_string s
1654
1655 let ssr_null_entry = Procq.Entry.(of_parser "ssr_null" { parser_fun = fun _ _ -> Ok () })
1656
1657 }
1658
1659 GRAMMAR EXTEND Gram
1660 GLOBAL: Prim.ident;
1661 Prim.ident: TOP [[ s = IDENT; ssr_null_entry -> { ssr_id_of_string loc s } ]];
1662 END
1663
1664 {
1665
1666 let perm_tag = "_perm_Hyp_"
1667 let _ = add_internal_name (is_tagged perm_tag)
1668
1669 }
1670
1671 (* We must not anonymize context names discharged by the "in" tactical. *)
1672
1673 (** Tactical extensions. *)
1674
1675 {
1676
1677 let ssrtac_expr ?loc key args =
1678 CAst.make ?loc (TacAlias (key, (List.map (fun x -> Tacexpr.TacGeneric (None, x)) args)))
1679
1680 let mk_non_term wit id =
1681 let open Pptactic in
1682 TacNonTerm (None, (Extend.Uentry (Genarg.ArgT.Any (Genarg.get_arg_tag wit)), Some id))
1683
1684 let tclintroskey =
1685 let prods =
1686 [ mk_non_term wit_ssrintrosarg (Names.Id.of_string "arg") ] in
1687 let tac = begin fun args ist -> match args with
1688 | [arg] ->
1689 let arg = cast_arg wit_ssrintrosarg arg in
1690 let tac, intros = arg in
1691 ssrevaltac ist tac <*> tclIPATssr intros
1692 | _ -> assert false
1693 end in
1694 register_ssrtac "tclintros" tac prods
1695
1696 let tclintros_expr ?loc tac ipats =
1697 let args = [in_gen (rawwit wit_ssrintrosarg) (tac, ipats)] in
1698 ssrtac_expr ?loc tclintroskey args
1699
1700 }
1701
1702 GRAMMAR EXTEND Gram
1703 GLOBAL: ltac_expr;
1704 ltac_expr: LEVEL "1l" [
1705 [ tac = ltac_expr; intros = ssrintros_ne -> { tclintros_expr ~loc tac intros }
1706 ] ];
1707 END
1708
1709
1710 (** Bracketing tactical *)
1711
1712 (* The tactic pretty-printer doesn't know that some extended tactics *)
1713 (* are actually tacticals. To prevent it from improperly removing *)
1714 (* parentheses we override the parsing rule for bracketed tactic *)
1715 (* expressions so that the pretty-print always reflects the input. *)
1716 (* (Removing user-specified parentheses is dubious anyway). *)
1717
1718 GRAMMAR EXTEND Gram
1719 GLOBAL: ltac_expr;
1720 ssrparentacarg: [[ "("; tac = ltac_expr; ")" -> { CAst.make ~loc (Tacexp tac) } ]];
1721 ltac_expr: LEVEL "0" [[ arg = ssrparentacarg -> { CAst.make ~loc (TacArg CAst.(arg.v)) } ]];
1722 END
1723
1724 {
1725 type ssreqid = ssripatrep option
1726
1727 type ssrarg = ssrfwdview * (ssreqid * (cpattern ssragens * ssripats))
1728
1729 }
1730
1731 {
1732
1733 module Internal = struct
1734 let register_ssrtac = register_ssrtac
1735 let mk_index = mk_index
1736 let noindex = noindex
1737 let tclintros_expr = tclintros_expr
1738 let intern_ipat = intern_ipat
1739 let interp_ipat = interp_ipat
1740 let pr_intros = pr_intros
1741 let pr_view = pr_view
1742 let pr_mult = pr_mult
1743 let is_ssr_loaded = is_ssr_loaded
1744 let pr_hpats = pr_hpats
1745 let pr_fwd = pr_fwd
1746 let pr_hint = pr_hint
1747 let intro_id_to_binder = intro_id_to_binder
1748 let binder_to_intro_id = binder_to_intro_id
1749 let mkFwdHint = mkFwdHint
1750 let bind_fwd = bind_fwd
1751 let pr_wgen = pr_wgen
1752 end
1753
1754 }
1755
1756 (* vim: set filetype=ocaml foldmethod=marker: *)