Debian Codegraph @

plugins/ssr/ssrvernac.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 open Names
16 module CoqConstr = Constr
17 open CoqConstr
18 open Constrexpr
19 open Constrexpr_ops
20 open Procq
21 open Procq.Prim
22 open Procq.Constr
23 open Pvernac.Vernac_
24 open Ltac_plugin
25 open Glob_term
26 open Stdarg
27 open Pp
28 open Ppconstr
29 open Printer
30 open Util
31 open Ssrprinters
32 open Ssrcommon
33
34 }
35
36 DECLARE PLUGIN "rocq-runtime.plugins.ssreflect"
37
38 (* The user is supposed to Require Import ssreflect or Require ssreflect *)
39 (* and Import ssreflect.SsrSyntax to obtain these keywords and as a *)
40 (* consequence the extended ssreflect grammar. *)
41 IGNORE KEYWORDS
42
43 {
44
45 (* global syntactic changes and vernacular commands *)
46
47 (** Alternative notations for "match" and anonymous arguments. *)(* ************)
48
49 (* Syntax: *)
50 (* if <term> is <pattern> then ... else ... *)
51 (* if <term> is <pattern> [in ..] return ... then ... else ... *)
52 (* let: <pattern> := <term> in ... *)
53 (* let: <pattern> [in ...] := <term> return ... in ... *)
54 (* The scope of a top-level 'as' in the pattern extends over the *)
55 (* 'return' type (dependent if/let). *)
56 (* Note that the optional "in ..." appears next to the <pattern> *)
57 (* rather than the <term> in then "let:" syntax. The alternative *)
58 (* would lead to ambiguities in, e.g., *)
59 (* let: p1 := (*v---INNER LET:---v *) *)
60 (* let: p2 := let: p3 := e3 in k return t in k2 in k1 return t' *)
61 (* in b (*^--ALTERNATIVE INNER LET--------^ *) *)
62
63 (* Caveat : There is no pretty-printing support, since this would *)
64 (* require a modification to the Rocq kernel (adding a new match *)
65 (* display style -- why aren't these strings?); also, the v8.1 *)
66 (* pretty-printer only allows extension hooks for printing *)
67 (* integer or string literals. *)
68 (* Also note that in the v8 grammar "is" needs to be a keyword; *)
69 (* as this can't be done from an ML extension file, the new *)
70 (* syntax will only work when ssreflect.v is imported. *)
71
72 let no_ct = None, None and no_rt = None
73 let aliasvar = function
74 | [[{ CAst.v = CPatAlias (_, na); loc }]] -> Some na
75 | _ -> None
76 let mk_cnotype mp = aliasvar mp, None
77 let mk_ctype mp t = aliasvar mp, Some t
78 let mk_rtype t = Some t
79 let mk_dthen ?loc (mp, ct, rt) c = (CAst.make ?loc (mp, c)), ct, rt
80 let mk_let ?loc rt ct mp c1 =
81 CAst.make ?loc @@ CCases (LetPatternStyle, rt, ct, [CAst.make ?loc (mp, c1)])
82 let mk_pat c (na, t) = (c, na, t)
83
84 }
85
86 GRAMMAR EXTEND Gram
87 GLOBAL: binder_constr;
88 ssr_rtype: [[ "return"; t = term LEVEL "100" -> { mk_rtype t } ]];
89 ssr_mpat: [[ p = pattern -> { [[p]] } ]];
90 ssr_dpat: [
91 [ mp = ssr_mpat; "in"; t = pattern; rt = ssr_rtype -> { mp, mk_ctype mp t, rt }
92 | mp = ssr_mpat; rt = ssr_rtype -> { mp, mk_cnotype mp, rt }
93 | mp = ssr_mpat -> { mp, no_ct, no_rt }
94 ] ];
95 ssr_dthen: [[ dp = ssr_dpat; "then"; c = lconstr -> { mk_dthen ~loc dp c } ]];
96 ssr_elsepat: [[ "else" -> { [[CAst.make ~loc @@ CPatAtom None]] } ]];
97 ssr_else: [[ mp = ssr_elsepat; c = lconstr -> { CAst.make ~loc (mp, c) } ]];
98 binder_constr: TOP [
99 [ "if"; c = term LEVEL "200"; "is"; db1 = ssr_dthen; b2 = ssr_else ->
100 { let b1, ct, rt = db1 in CAst.make ~loc @@ CCases (MatchStyle, rt, [mk_pat c ct], [b1; b2]) }
101 | "if"; c = term LEVEL "200";"isn't";db1 = ssr_dthen; b2 = ssr_else ->
102 { let b1, ct, rt = db1 in
103 let b1, b2 = let open CAst in
104 let {loc=l1; v=(p1, r1)}, {loc=l2; v=(p2, r2)} = b1, b2 in
105 (make ?loc:l1 (p1, r2), make ?loc:l2 (p2, r1))
106 in
107 CAst.make ~loc @@ CCases (MatchStyle, rt, [mk_pat c ct], [b1; b2]) }
108 | "let"; ":"; mp = ssr_mpat; ":="; c = lconstr; "in"; c1 = lconstr ->
109 { mk_let ~loc no_rt [mk_pat c no_ct] mp c1 }
110 | "let"; ":"; mp = ssr_mpat; ":="; c = lconstr;
111 rt = ssr_rtype; "in"; c1 = lconstr ->
112 { mk_let ~loc rt [mk_pat c (mk_cnotype mp)] mp c1 }
113 | "let"; ":"; mp = ssr_mpat; "in"; t = pattern; ":="; c = lconstr;
114 rt = ssr_rtype; "in"; c1 = lconstr ->
115 { mk_let ~loc rt [mk_pat c (mk_ctype mp t)] mp c1 }
116 ] ];
117 END
118
119 GRAMMAR EXTEND Gram
120 GLOBAL: closed_binder;
121 closed_binder: TOP [
122 [ ["of" -> { () } | "&" -> { () } ]; c = term LEVEL "99" ->
123 { [CLocalAssum ([CAst.make ~loc Anonymous], None, Default Explicit, c)] }
124 ] ];
125 END
126
127 (** Vernacular commands: Prenex Implicits *)
128
129 (* This should really be implemented as an extension to the implicit *)
130 (* arguments feature, but unfortuately that API is sealed. The current *)
131 (* workaround uses a combination of notations that works reasonably, *)
132 (* with the following caveats: *)
133 (* - The pretty-printing always elides prenex implicits, even when *)
134 (* they are obviously needed. *)
135 (* - Prenex Implicits are NEVER exported from a module, because this *)
136 (* would lead to faulty pretty-printing and scoping errors. *)
137 (* - The command "Import Prenex Implicits" can be used to reassert *)
138 (* Prenex Implicits for all the visible constants that had been *)
139 (* declared as Prenex Implicits. *)
140
141 {
142
143 let declare_one_prenex_implicit locality f =
144 let fref =
145 try Smartlocate.global_with_alias f
146 with e when CErrors.noncritical e -> errorstrm (pr_qualid f ++ str " is not declared") in
147 let rec loop = function
148 | a :: args' when Impargs.is_status_implicit a ->
149 MaxImplicit :: loop args'
150 | args' when List.exists Impargs.is_status_implicit args' ->
151 errorstrm (str "Expected prenex implicits for " ++ pr_qualid f)
152 | _ -> [] in
153 let impls =
154 match Impargs.implicits_of_global fref with
155 | [cond,impls] -> impls
156 | [] -> errorstrm (str "Expected some implicits for " ++ pr_qualid f)
157 | _ -> errorstrm (str "Multiple implicits not supported") in
158 match loop impls with
159 | [] ->
160 errorstrm (str "Expected some implicits for " ++ pr_qualid f)
161 | impls ->
162 Impargs.set_implicits locality fref [List.map (fun imp -> (Anonymous,imp)) impls]
163
164 }
165
166 VERNAC COMMAND EXTEND Ssrpreneximplicits CLASSIFIED AS SIDEFF
167 | #[ locality = Attributes.locality; ] [ "Prenex" "Implicits" ne_global_list(fl) ]
168 -> {
169 let locality = Locality.make_section_locality locality in
170 List.iter (declare_one_prenex_implicit locality) fl;
171 }
172 END
173
174 (* Vernac grammar visibility patch *)
175
176 GRAMMAR EXTEND Gram
177 GLOBAL: gallina_ext;
178 gallina_ext: TOP
179 [ [ IDENT "Import"; IDENT "Prenex"; IDENT "Implicits" ->
180 { Vernacexpr.VernacSynterp (Vernacexpr.VernacSetOption (false, ["Printing"; "Implicit"; "Defensive"], Vernacexpr.OptionUnset)) }
181 ] ]
182 ;
183 END
184
185 (** View hint database and View application. *)(* ******************************)
186
187 (* There are three databases of lemmas used to mediate the application *)
188 (* of reflection lemmas: one for forward chaining, one for backward *)
189 (* chaining, and one for secondary backward chaining. *)
190
191 (* View hints *)
192
193 {
194
195 let pr_raw_ssrhintref env sigma prc _ _ = let open CAst in function
196 | { v = CAppExpl ((r,x), args) } when isCHoles args ->
197 prc env sigma (CAst.make @@ CRef (r,x)) ++ str "|" ++ int (List.length args)
198 | { v = CApp ({ v = CRef _ }, _) } as c -> prc env sigma c
199 | { v = CApp (c, args) } when isCxHoles args ->
200 prc env sigma c ++ str "|" ++ int (List.length args)
201 | c -> prc env sigma c
202
203 let pr_rawhintref env sigma c =
204 match DAst.get c with
205 | GApp (f, args) when isRHoles args ->
206 pr_glob_constr_env env sigma f ++ str "|" ++ int (List.length args)
207 | _ -> pr_glob_constr_env env sigma c
208
209 let pr_glob_ssrhintref env sigma _ _ _ (c, _) = pr_rawhintref env sigma c
210
211 let pr_ssrhintref env sigma prc _ _ = prc env sigma
212
213 let mkhintref ?loc c n = match c.CAst.v with
214 | CRef (r,x) -> CAst.make ?loc @@ CAppExpl ((r, x), mkCHoles ?loc n)
215 | _ -> mkAppC (c, mkCHoles ?loc n)
216
217 }
218
219 ARGUMENT EXTEND ssrhintref
220 TYPED AS constr
221 PRINTED BY { pr_ssrhintref env sigma }
222 RAW_PRINTED BY { pr_raw_ssrhintref env sigma }
223 GLOB_PRINTED BY { pr_glob_ssrhintref env sigma }
224 | [ constr(c) ] -> { c }
225 | [ constr(c) "|" natural(n) ] -> { mkhintref ~loc c n }
226 END
227
228 {
229
230 (* View purpose *)
231
232 let pr_viewpos = function
233 | Some Ssrview.AdaptorDb.Forward -> str " for move/"
234 | Some Ssrview.AdaptorDb.Backward -> str " for apply/"
235 | Some Ssrview.AdaptorDb.Equivalence -> str " for apply//"
236 | None -> mt ()
237
238 let pr_ssrviewpos _ _ _ = pr_viewpos
239
240 }
241
242 ARGUMENT EXTEND ssrviewpos PRINTED BY { pr_ssrviewpos }
243 | [ "for" "move" "/" ] -> { Some Ssrview.AdaptorDb.Forward }
244 | [ "for" "apply" "/" ] -> { Some Ssrview.AdaptorDb.Backward }
245 | [ "for" "apply" "/" "/" ] -> { Some Ssrview.AdaptorDb.Equivalence }
246 | [ "for" "apply" "//" ] -> { Some Ssrview.AdaptorDb.Equivalence }
247 | [ ] -> { None }
248 END
249
250 {
251
252 let pr_ssrviewposspc _ _ _ i = pr_viewpos i ++ spc ()
253
254 }
255
256 ARGUMENT EXTEND ssrviewposspc TYPED AS ssrviewpos PRINTED BY { pr_ssrviewposspc }
257 | [ ssrviewpos(i) ] -> { i }
258 END
259
260 {
261
262 let print_view_hints env sigma kind l =
263 let pp_viewname = str "Hint View" ++ pr_viewpos (Some kind) ++ str " " in
264 let pp_hints = pr_list spc (pr_rawhintref env sigma) l in
265 Feedback.msg_notice (pp_viewname ++ hov 0 pp_hints ++ Pp.cut ())
266
267 }
268
269 VERNAC COMMAND EXTEND PrintView CLASSIFIED AS QUERY
270 | [ "Print" "Hint" "View" ssrviewpos(i) ] ->
271 {
272 let env = Global.env () in
273 let sigma = Evd.from_env env in
274 (match i with
275 | Some k ->
276 print_view_hints env sigma k (Ssrview.AdaptorDb.get k)
277 | None ->
278 List.iter (fun k -> print_view_hints env sigma k (Ssrview.AdaptorDb.get k))
279 [ Ssrview.AdaptorDb.Forward;
280 Ssrview.AdaptorDb.Backward;
281 Ssrview.AdaptorDb.Equivalence ])
282 }
283 END
284
285 {
286
287 let glob_view_hints lvh =
288 List.map (Constrintern.intern_constr (Global.env ()) (Evd.from_env (Global.env ()))) lvh
289
290 }
291
292 VERNAC COMMAND EXTEND HintView CLASSIFIED AS SIDEFF
293 | [ "Hint" "View" ssrviewposspc(n) ne_ssrhintref_list(lvh) ] ->
294 { let hints = glob_view_hints lvh in
295 match n with
296 | None ->
297 Ssrview.AdaptorDb.declare Ssrview.AdaptorDb.Forward hints;
298 Ssrview.AdaptorDb.declare Ssrview.AdaptorDb.Backward hints
299 | Some k ->
300 Ssrview.AdaptorDb.declare k hints }
301 END
302
303 (** Search compatibility *)
304
305 {
306
307 open G_vernac
308 }
309
310 GRAMMAR EXTEND Gram
311 GLOBAL: query_command;
312
313 query_command: TOP
314 [ [ IDENT "Search"; s = search_query; l = search_queries; "." ->
315 { let (sl,m) = l in
316 fun g ->
317 Vernacexpr.VernacSearch (Vernacexpr.Search (s::sl),g, m) }
318 ] ]
319 ;
320 END
321
322 (** Keyword compatibility fixes. *)
323
324 (* Coq v8.1 notation uses "by" and "of" quasi-keywords, i.e., reserved *)
325 (* identifiers used as keywords. This is incompatible with ssreflect.v *)
326 (* which makes "by" and "of" true keywords, because of technicalities *)
327 (* in the internal lexer-parser API of Rocq. We patch this here by *)
328 (* adding new parsing rules that recognize the new keywords. *)
329 (* To make matters worse, the Rocq grammar for tactics fails to *)
330 (* export the non-terminals we need to patch. Fortunately, the CamlP5 *)
331 (* API provides a backdoor access (with loads of Obj.magic trickery). *)
332
333 (* Coq v8.3 defines "by" as a keyword, some hacks are not needed any *)
334 (* longer and thus comment out. Such comments are marked with v8.3 *)
335
336 {
337
338 open Pltac
339
340 }
341
342 GRAMMAR EXTEND Gram
343 GLOBAL: hypident;
344 hypident: TOP [
345 [ "("; IDENT "type"; "of"; id = Prim.identref; ")" -> { id, Locus.InHypTypeOnly }
346 | "("; IDENT "value"; "of"; id = Prim.identref; ")" -> { id, Locus.InHypValueOnly }
347 ] ];
348 END
349
350 GRAMMAR EXTEND Gram
351 GLOBAL: constr_eval;
352 constr_eval: TOP [
353 [ IDENT "type"; "of"; c = Constr.constr -> { Tacexpr.ConstrTypeOf c }]
354 ];
355 END
356
357 (* vim: set filetype=ocaml foldmethod=marker: *)