Debian Codegraph @

src/rocq_elpi_arg_syntax.mlg

raw
1 (* rocq-elpi: Coq terms as the object language of elpi *)
2 (* license: GNU Lesser General Public License Version 2.1 or later *)
3 (* ------------------------------------------------------------------------- *)
4
5 DECLARE PLUGIN "rocq-elpi.elpi"
6
7 {
8
9 open Ltac_plugin
10 open Gramlib
11
12 open Procq.Constr
13 open Procq.Prim
14 open Pvernac.Vernac_
15 open Pltac
16
17 module EA = Rocq_elpi_arg_HOAS
18 module U = Rocq_elpi_utils
19
20 (* Arguments ************************************************************* *)
21 let pr_elpi_string _ _ _ (s : Elpi.API.Ast.Loc.t * string) = Pp.str (snd s)
22
23 let trim_loc loc =
24 let open Loc in
25 { loc with bp = loc.bp + 1; ep = loc.ep - 1 }
26
27 let rec strip_curly loc s =
28 if s.[0] = '\123' then strip_curly (trim_loc loc) String.(sub s 1 (length s - 2))
29 else Rocq_elpi_utils.of_coq_loc loc, s
30 let rec strip_round loc s =
31 if s.[0] = '(' then strip_round (trim_loc loc) String.(sub s 1 (length s - 2))
32 else Rocq_elpi_utils.of_coq_loc loc, s
33 let rec strip_square loc s =
34 if s.[0] = '[' then strip_square (trim_loc loc) String.(sub s 1 (length s - 2))
35 else Rocq_elpi_utils.of_coq_loc loc, s
36
37 [%%if coq = "9.0" || coq = "9.1"]
38 let streamOk x = x
39 let streamFail () = raise Gramlib.Stream.Failure
40 [%%else]
41 let streamOk x = Ok x
42 let streamFail () = Error ()
43 [%%endif]
44
45 [%%if coq = "9.0" || coq = "9.1" || coq = "9.2" || coq = "9.3"]
46 let get_next_loc strm = LStream.get_loc (LStream.count strm) strm
47 [%%else]
48 let get_next_loc strm = LStream.get_relative_loc 0 strm
49 [%%endif]
50
51 }
52
53 ARGUMENT EXTEND elpi_string PRINTED BY { pr_elpi_string }
54 END
55 GRAMMAR EXTEND Gram GLOBAL: elpi_string;
56 elpi_string : FIRST [
57 [ s = QUOTATION "lp:" -> {
58 let loc = { loc with Loc.bp = loc.Loc.bp + 3 } in
59 if s.[0] = '\123' then strip_curly loc s
60 else if s.[0] = '(' then strip_round loc s
61 else if s.[0] = '[' then strip_square loc s
62 else Rocq_elpi_utils.of_coq_loc loc, s
63 }
64 | s = STRING -> {
65 Rocq_elpi_utils.of_coq_loc loc, s
66 }
67 ]];
68 END
69
70 {
71 let pr_fp _ _ _ (_,x) = U.pr_qualified_name x
72 let pp_elpi_loc _ _ _ (l : Loc.t) = Pp.mt ()
73
74 let any_qname kwstate strm =
75 let re = Str.regexp "[A-Za-z][A-Za-z0-9]*\\(\\.?[A-Za-z][A-Za-z0-9]*\\)*" in
76 match LStream.peek kwstate strm with
77 | Some (Tok.KEYWORD sym) when Str.string_match re sym 0 ->
78 let _, ep = Loc.unloc (get_next_loc strm) in
79 LStream.junk kwstate strm;
80 let v = match LStream.peek kwstate strm with
81 | Some (Tok.IDENT id) ->
82 let bp, _ = Loc.unloc (get_next_loc strm) in
83 if Int.equal ep bp then
84 (LStream.junk kwstate strm; sym ^ id)
85 else
86 sym
87 | _ -> sym
88 in
89 streamOk v
90 | _ -> streamFail ()
91 let any_qname = Procq.Entry.(of_parser "any_qname" { parser_fun = any_qname })
92
93 }
94
95 ARGUMENT EXTEND qualified_name PRINTED BY { pr_fp }
96 END
97 GRAMMAR EXTEND Gram GLOBAL: qualified_name;
98 qualified_name : FIRST
99 [ [ i = IDENT; s = LIST0 FIELD -> { loc, i :: s }
100 | "_"; s = LIST0 FIELD -> { loc, match s with [] -> [] | _ -> "_" :: s }
101 | v = any_qname -> { loc, Str.split_delim (Str.regexp_string ".") v } ]
102 ];
103 END
104
105 ARGUMENT EXTEND elpi_loc
106 PRINTED BY { pp_elpi_loc }
107 | [ ] -> { loc }
108 END
109
110 {
111
112 let telescope = Procq.Entry.make "elpi:telescope"
113 let colon_sort = Procq.Entry.make "elpi:colon_sort"
114 let colon_constr = Procq.Entry.make "elpi:colon_constr"
115
116 let any_attribute : Attributes.vernac_flags Attributes.attribute =
117 Attributes.make_attribute (fun x -> [],x)
118
119 let ignore_unknown_attributes extra =
120 CWarnings.with_warn "unsupported-attributes" Attributes.unsupported_attributes extra
121
122 let scope_attribute : Rocq_elpi_utils.clause_scope Attributes.attribute =
123 let open Attributes.Notations in
124 let open Rocq_elpi_utils in
125 Attributes.attribute_of_list
126 ["local",
127 (fun ?loc old -> function
128 | Attributes.VernacFlagEmpty -> Local
129 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[local]"));
130 "global",
131 (fun ?loc old -> function
132 | Attributes.VernacFlagEmpty -> Global
133 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[global]"));
134 "superglobal",
135 (fun ?loc old -> function
136 | Attributes.VernacFlagEmpty -> SuperGlobal
137 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[superglobal]"));
138 ] |> map (Option.default Regular)
139
140
141 let skip_attribute : (Str.regexp list option * Str.regexp list option) Attributes.attribute =
142 let open Attributes.Notations in
143 let o2l = function None -> [] | Some l -> l in
144 Attributes.attribute_of_list
145 ["skip",
146 fun ?loc old -> function
147 | Attributes.VernacFlagLeaf (Attributes.FlagString rex) -> Str.regexp rex :: o2l old
148 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[skip=\"rex\"]")]
149 ++
150 Attributes.attribute_of_list
151 ["only",
152 fun ?loc old -> function
153 | Attributes.VernacFlagLeaf (Attributes.FlagString rex) -> Str.regexp rex :: o2l old
154 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[only=\"rex\"]")]
155
156 let synterp_attribute : EA.phase option Attributes.attribute =
157 let open EA in
158 Attributes.attribute_of_list
159 ["phase",
160 (fun ?loc old -> function
161 | Attributes.VernacFlagLeaf (Attributes.FlagString "parsing") -> Synterp
162 | Attributes.VernacFlagLeaf (Attributes.FlagString "execution") -> Interp
163 | Attributes.VernacFlagLeaf (Attributes.FlagString "both") -> Both
164 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[phase=\"parsing\"] or #[phase=\"execution\"] or #[phase=\"both\"]"))
165 ;"phases",
166 (fun ?loc old -> function
167 | Attributes.VernacFlagLeaf (Attributes.FlagString "both") -> Both
168 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[phases=\"both\"]"))
169 ;"synterp",
170 (fun ?loc old -> function
171 | Attributes.VernacFlagEmpty -> Synterp
172 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[synterp]"))
173 ;"interp",
174 (fun ?loc old -> function
175 | Attributes.VernacFlagEmpty -> Interp
176 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[interp]"))
177 ;"both",
178 (fun ?loc old -> function
179 | Attributes.VernacFlagEmpty -> Both
180 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[both]"))
181
182 ]
183
184 let proof_attribute : EA.proof option Attributes.attribute =
185 let open EA in
186 let open Attributes in
187 attribute_of_list
188 ["proof",
189 (fun ?loc old -> function
190 | VernacFlagLeaf (FlagString "begin") -> Begin None
191 | VernacFlagList [CAst.{ v = "begin_if",VernacFlagLeaf (FlagString s) }] -> Begin (Some s)
192 | VernacFlagLeaf (FlagString "end") -> End
193 | _ -> CErrors.user_err ?loc (Pp.str "Syntax error, use #[proof=\"begin\"] or #[proof=\"end\"]"))
194
195 ]
196
197
198
199 let skip_and_synterp_attributes = Attributes.Notations.(skip_attribute ++ synterp_attribute)
200 let scope_and_skip_and_synterp_attributes = Attributes.Notations.(scope_attribute ++ skip_attribute ++ synterp_attribute)
201
202 let raw_args_attributes =
203 Attributes.(qualify_attribute "arguments" (bool_attribute ~name:"raw"))
204
205 let validate_attributes a flags =
206 let extra, raw_args = Attributes.parse_with_extra a flags in
207 ignore_unknown_attributes extra;
208 raw_args
209
210 let coq_kwd_or_symbol = Procq.Entry.make "elpi:kwd_or_symbol"
211
212 let opt2list = function None -> [] | Some l -> l
213
214 let any_kwd kwstate strm =
215 match LStream.peek kwstate strm with
216 | Some (Tok.KEYWORD sym) when sym <> "." -> LStream.junk kwstate strm; streamOk sym
217 | _ -> streamFail ()
218 let any_kwd = Procq.Entry.(of_parser "any_symbols_or_kwd" { parser_fun = any_kwd })
219
220 let pr_attributes _ _ _ atts =
221 Pp.(prlist_with_sep (fun () -> str ",") Attributes.pr_vernac_flag atts)
222
223 let wit_elpi_ftactic_arg = EA.Tac.wit
224
225 let def_body = G_vernac.def_body
226
227 let of_coq_inductive_declaration ~atts kind id =
228 let open Vernacentries in let open Preprocessed_Mind_decl in
229 match preprocess_inductive_decl ~atts kind [id] with
230 | Inductive i -> i
231 | Record _ -> assert false
232
233 let of_coq_record_declaration ~atts kind id =
234 let open Vernacentries in let open Preprocessed_Mind_decl in
235 match preprocess_inductive_decl ~atts kind [id] with
236 | Inductive _ -> assert false
237 | Record r -> r
238
239 let of_coq_definition ~loc ~atts name udecl def =
240 match def with
241 | Vernacexpr.DefineBody(bl,red,c,ty) ->
242 EA.Cmd.(ConstantDecl { name = snd name; atts; udecl; typ = (bl,ty); red; body = Some c })
243 | Vernacexpr.ProveBody(bl,ty) ->
244 EA.Cmd.(ConstantDecl { name = snd name; atts; udecl; typ = (bl,Some ty); red = None; body = None })
245
246 }
247
248 GRAMMAR EXTEND Gram
249 GLOBAL: telescope colon_sort colon_constr coq_kwd_or_symbol pipe_telescope;
250
251 telescope:
252 [ [ bl = binders -> { bl } ] ];
253
254 colon_sort:
255 [ [ ":"; s = sort -> { s } ] ];
256
257 colon_constr:
258 [ [ ":"; s = lconstr -> { s } ] ];
259
260 coq_kwd_or_symbol:
261 [ [ the_kwd = any_kwd -> { the_kwd }] ];
262 END
263
264 ARGUMENT EXTEND attributes
265 PRINTED BY { pr_attributes }
266 END
267 GRAMMAR EXTEND Gram GLOBAL: attributes;
268 my_attribute_list:
269 [ [ a = LIST0 my_attribute SEP "," -> { a } ]
270 ]
271 ;
272 my_attribute:
273 [ [ k = ident ; v = my_attr_value -> { CAst.make ~loc (Names.Id.to_string k, v) }
274 (* Required because "ident" is declared a keyword when loading Ltac. *)
275 | IDENT "using" ; v = my_attr_value -> { CAst.make ~loc ("using", v) } ]
276 ]
277 ;
278 my_attr_value:
279 [ [ "=" ; v = string -> { Attributes.VernacFlagLeaf (Attributes.FlagString v) }
280 | "=" ; v = qualid -> { Attributes.VernacFlagLeaf (Attributes.FlagQualid v) }
281 | "(" ; a = my_attribute_list ; ")" -> { Attributes.VernacFlagList a }
282 | -> { Attributes.VernacFlagEmpty } ]
283 ]
284 ;
285
286 attributes : FIRST [[ atts = LIST1 my_attribute SEP "," -> { atts } ]];
287
288 END
289
290 ARGUMENT EXTEND elpi_cmd_arg
291 PRINTED BY { fun _ _ _ -> EA.Cmd.pp_top env sigma }
292 INTERPRETED BY { EA.Cmd.interp }
293 GLOBALIZED BY { EA.Cmd.glob }
294 SUBSTITUTED BY { EA.Cmd.subst }
295 RAW_PRINTED BY { fun _ _ _ -> EA.Cmd.pp_raw env sigma }
296 GLOB_PRINTED BY { fun _ _ _ -> EA.Cmd.pp_glob env sigma }
297 | [ qualified_name(s) ] -> { EA.Cmd.String (String.concat "." (snd s)) }
298 | [ integer(n) ] -> { EA.Cmd.Int n }
299 | [ string(s) ] -> { EA.Cmd.String s }
300
301 | [ "Inductive" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts:[] Vernacexpr.Inductive_kw id) }
302 | [ "#[" attributes(atts) "]" "Inductive" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts Vernacexpr.Inductive_kw id) }
303
304 | [ "CoInductive" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts:[] Vernacexpr.CoInductive id) }
305 | [ "#[" attributes(atts) "]" "CoInductive" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts Vernacexpr.CoInductive id) }
306
307 | [ "Variant" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts:[] Vernacexpr.Variant id) }
308 | [ "#[" attributes(atts) "]" "Variant" inductive_or_record_definition(id) ] -> { EA.Cmd.IndtDecl (of_coq_inductive_declaration ~atts Vernacexpr.Variant id) }
309
310 | [ "Record" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts:[] Vernacexpr.Record id) }
311 | [ "#[" attributes(atts) "]" "Record" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts Vernacexpr.Record id) }
312
313 | [ "Class" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts:[] Vernacexpr.(Class true) id) }
314 | [ "#[" attributes(atts) "]" "Class" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts Vernacexpr.(Class true) id) }
315
316 | [ "Structure" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts:[] Vernacexpr.Structure id) }
317 | [ "#[" attributes(atts) "]" "Structure" inductive_or_record_definition(id) ] -> { EA.Cmd.RecordDecl (of_coq_record_declaration ~atts Vernacexpr.Structure id) }
318
319 | [ "Definition" qualified_name(name) univ_decl_opt(udecl) def_body(def) ] -> { of_coq_definition ~loc ~atts:[] name udecl def }
320 | [ "#[" attributes(atts) "]" "Definition" qualified_name(name) univ_decl_opt(udecl) def_body(def) ] -> { of_coq_definition ~loc ~atts name udecl def }
321
322 | [ "Lemma" qualified_name(name) univ_decl_opt(udecl) def_body(def) ] -> { of_coq_definition ~loc ~atts:[] name udecl def }
323 | [ "#[" attributes(atts) "]" "Lemma" qualified_name(name) univ_decl_opt(udecl) def_body(def) ] -> { of_coq_definition ~loc ~atts name udecl def }
324
325
326 | [ "Axiom" qualified_name(name) univ_decl_opt(udecl) telescope(typ) colon_constr(t) ] -> {
327 EA.Cmd.(ConstantDecl { name = snd name; atts = []; udecl; typ = (typ,Some t); red = None; body = None }) }
328 | [ "#[" attributes(atts) "]" "Axiom" qualified_name(name) univ_decl_opt(udecl) telescope(typ) colon_constr(t) ] -> {
329 EA.Cmd.(ConstantDecl { name = snd name; atts; udecl; typ = (typ,Some t); red = None; body = None }) }
330
331
332 | [ "Context" telescope(ty) ] -> { EA.Cmd.Context ty }
333
334 | [ "(" lconstr(t) ")" ] -> { EA.Cmd.Term t }
335
336 | [ coq_kwd_or_symbol(x) ] -> { EA.Cmd.String x }
337 END
338
339 ARGUMENT EXTEND elpi_tactic_arg
340 TYPED AS elpi_ftactic_arg
341 | [ qualified_name(s) ] -> { EA.Tac.String (String.concat "." (snd s)) }
342 | [ integer(n) ] -> { EA.Tac.Int n }
343 | [ string(s) ] -> { EA.Tac.String s }
344 | [ "(" lconstr(t) ")" ] -> { EA.Tac.Term t }
345 | [ "`(" lconstr(t) ")" ] -> { EA.Tac.OpenTerm t }
346 | [ "ltac_string" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.String, (CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
347 | [ "ltac_string_list" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.List EA.Tac.String, (CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
348 | [ "ltac_int" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.Int, (CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
349 | [ "ltac_int_list" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.List EA.Tac.Int, (CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
350 | [ "ltac_term" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.Term, CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None)) }
351 | [ "ltac_term_list" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.List EA.Tac.Term,(CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
352 | [ "ltac_open_term" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.OpenTerm, CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None)) }
353 | [ "ltac_open_term_list" ":" "(" ident(t) ")" ] -> { EA.Tac.LTac(EA.Tac.List EA.Tac.OpenTerm,(CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string t,None))) }
354 | [ "ltac_tactic" ":" "(" ltac_expr(t) ")" ] -> { EA.Tac.LTacTactic t }
355 END
356
357 ARGUMENT EXTEND ltac_attributes
358 PRINTED BY { pr_attributes }
359 INTERPRETED BY { fun ist env evd x -> match DAst.get x with
360 | Glob_term.GVar id ->
361 Ltac_plugin.Tacinterp.interp_ltac_var (Ltac_plugin.Tacinterp.Value.cast (Genarg.topwit wit_attributes)) ist None (CAst.make id)
362 | _ -> assert false }
363 GLOBALIZED BY { fun gsig t -> fst @@ Ltac_plugin.Tacintern.intern_constr gsig t }
364 SUBSTITUTED BY { fun x -> Detyping.subst_glob_constr (Global.env()) x }
365 RAW_PRINTED BY { fun _ _ _ -> U.pr_constr_expr env sigma }
366 GLOB_PRINTED BY { fun _ _ _ -> Printer.pr_glob_constr_env env sigma }
367 | [ ident(v) ] -> { (CAst.make ~loc @@ Constrexpr.CRef (Libnames.qualid_of_string ~loc @@ Names.Id.to_string v,None)) }
368 END
369