Debian Codegraph @

vernac/attributes.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** The type of parsing attribute data *)
12 type vernac_flag_type =
13 | FlagQualid of Libnames.qualid
14 | FlagString of string
15
16 type vernac_flags = vernac_flag list
17 and vernac_flag = (string * vernac_flag_value) CAst.t
18 and vernac_flag_value =
19 | VernacFlagEmpty
20 | VernacFlagLeaf of vernac_flag_type
21 | VernacFlagList of vernac_flags
22
23 let pr_vernac_flag_leaf = function
24 | FlagString s -> Pp.(quote (str s))
25 | FlagQualid p -> Libnames.pr_qualid p
26
27 let rec pr_vernac_flag_value = let open Pp in function
28 | VernacFlagEmpty -> mt ()
29 | VernacFlagLeaf l -> str "=" ++ pr_vernac_flag_leaf l
30 | VernacFlagList s -> surround (prlist_with_sep pr_comma pr_vernac_flag s)
31 and pr_vernac_flag_r (s, arguments) =
32 let open Pp in
33 str s ++ (pr_vernac_flag_value arguments)
34 and pr_vernac_flag {CAst.v} = pr_vernac_flag_r v
35
36 let warn_unsupported_attributes =
37 CWarnings.create ~name:"unsupported-attributes" ~category:CWarnings.CoreCategories.parsing ~default:CWarnings.AsError
38 (fun atts ->
39 let keys = List.map (fun x -> fst x.CAst.v) atts in
40 let keys = List.sort_uniq String.compare keys in
41 let conj = match keys with [_] -> "this attribute: " | _ -> "these attributes: " in
42 Pp.(str "This command does not support " ++ str conj ++ prlist_with_sep (fun () -> strbrk ", ") str keys ++ str"."))
43
44 let unsupported_attributes = function
45 | [] -> ()
46 | atts ->
47 let loc = List.fold_left (fun loc att -> Loc.merge_opt loc att.CAst.loc) None atts in
48 warn_unsupported_attributes ?loc atts
49
50 type 'a key_parser = ?loc:Loc.t -> 'a option -> vernac_flag_value -> 'a
51
52 type 'a attribute = vernac_flags -> vernac_flags * 'a
53
54 let parse_with_extra (p:'a attribute) (atts:vernac_flags) : vernac_flags * 'a =
55 p atts
56
57 let parse_drop_extra att atts =
58 snd (parse_with_extra att atts)
59
60 let parse (p:'a attribute) atts : 'a =
61 let extra, v = parse_with_extra p atts in
62 unsupported_attributes extra;
63 v
64
65 let make_attribute x = x
66
67 module Notations = struct
68
69 type 'a t = 'a attribute
70
71 let return x = fun atts -> atts, x
72
73 let (>>=) att f =
74 fun atts ->
75 let atts, v = att atts in
76 f v atts
77
78 let (>>) p1 p2 =
79 fun atts ->
80 let atts, () = p1 atts in
81 p2 atts
82
83 let map f att =
84 fun atts ->
85 let atts, v = att atts in
86 atts, f v
87
88 let (++) (p1:'a attribute) (p2:'b attribute) : ('a*'b) attribute =
89 fun atts ->
90 let atts, v1 = p1 atts in
91 let atts, v2 = p2 atts in
92 atts, (v1, v2)
93
94 end
95 open Notations
96
97 let assert_empty ?loc k v =
98 if v <> VernacFlagEmpty
99 then CErrors.user_err ?loc
100 Pp.(str "Attribute " ++ str k ++ str " does not accept arguments")
101
102 let error_twice ?loc ~name : 'a =
103 CErrors.user_err ?loc
104 Pp.(str "Attribute for " ++ str name ++ str " specified twice.")
105
106 let assert_once ?loc ~name prev =
107 if Option.has_some prev then
108 error_twice ?loc ~name
109
110 let attribute_of_list (l:(string * 'a key_parser) list) : 'a option attribute =
111 let rec p extra v = function
112 | [] -> List.rev extra, v
113 | ({CAst.v=key, attv; loc} as att) :: rem ->
114 (match CList.assoc_f String.equal key l with
115 | exception Not_found -> p (att::extra) v rem
116 | parser ->
117 let v = Some (parser ?loc v attv) in
118 p extra v rem)
119 in
120 p [] None
121
122 let single_key_parser ~name ~key v ?loc prev args =
123 assert_empty ?loc key args;
124 assert_once ?loc ~name prev;
125 v
126
127 let pr_possible_values ~values =
128 Pp.(str "{" ++ prlist_with_sep pr_comma str (List.map fst values) ++ str "}")
129
130 (** [key_value_attribute ~key ~default ~values] parses a attribute [key=value]
131 with possible [key] [value] in [values], [empty] is for compatibility for users
132 doing [qualif(key)] which is parsed as [qualif(key=empty)] *)
133 let key_value_attribute ~key ?empty ~(values : (string * 'a) list) : 'a option attribute =
134 let parser ?loc = function
135 | Some v ->
136 CErrors.user_err ?loc Pp.(str "key '" ++ str key ++ str "' has been already set.")
137 | None ->
138 begin function
139 | VernacFlagLeaf (FlagQualid q) when Libnames.qualid_is_ident q ->
140 let b = Names.Id.to_string @@ Libnames.qualid_basename q in
141 begin match CList.assoc_f String.equal b values with
142 | exception Not_found ->
143 CErrors.user_err ?loc
144 Pp.(str "Invalid value '" ++ str b ++ str "' for key " ++ str key ++ fnl () ++
145 str "use one of " ++ pr_possible_values ~values)
146 | value -> value
147 end
148 | VernacFlagEmpty when Option.has_some empty ->
149 Option.get empty
150 | err ->
151 CErrors.user_err ?loc
152 Pp.(str "Invalid syntax " ++ pr_vernac_flag_r (key, err) ++ str ", try "
153 ++ str key ++ str "=" ++ pr_possible_values ~values ++ str " instead.")
154 end
155 in
156 attribute_of_list [key, parser]
157
158 let bool_attribute ~name : bool option attribute =
159 let values = ["yes", true; "no", false] in
160 key_value_attribute ~key:name ~empty:true ~values
161
162 (* Variant of the [bool] attribute with only two values (bool has three). *)
163 let qualid_is_this_ident fp id =
164 Libnames.qualid_is_ident fp &&
165 Names.Id.to_string @@ Libnames.qualid_basename fp = id
166
167 let get_bool_value ?loc ~key ~default =
168 function
169 | VernacFlagEmpty -> default
170 | VernacFlagLeaf (FlagQualid q) when qualid_is_this_ident q "yes" ->
171 true
172 | VernacFlagLeaf (FlagQualid q) when qualid_is_this_ident q "no" ->
173 false
174 | _ ->
175 CErrors.user_err ?loc
176 Pp.(str "Attribute " ++ str key ++ str " only accepts boolean values.")
177
178 let enable_attribute ~key ~default : bool attribute =
179 fun atts ->
180 let this, extra = List.partition (fun {CAst.v=k, _} -> String.equal key k) atts in
181 extra,
182 match this with
183 | [] -> default ()
184 | [ {CAst.v=_, value; loc} ] -> get_bool_value ?loc ~key ~default:true value
185 | _ :: {CAst.loc} :: _ ->
186 (* We report the location of the 2nd item *)
187 error_twice ?loc ~name:key
188
189 let qualify_attribute qual (parser:'a attribute) : 'a attribute =
190 fun atts ->
191 let rec extract extra qualified = function
192 | [] -> List.rev extra, List.flatten (List.rev qualified)
193 | {CAst.v=key,attv; loc} :: rem when String.equal key qual ->
194 (match attv with
195 | VernacFlagEmpty | VernacFlagLeaf _ ->
196 CErrors.user_err ?loc
197 Pp.(str "Malformed attribute " ++ str qual ++ str ": attribute list expected.")
198 | VernacFlagList atts ->
199 extract extra (atts::qualified) rem)
200 | att :: rem -> extract (att::extra) qualified rem
201 in
202 let extra, qualified = extract [] [] atts in
203 let rem, v = parser qualified in
204 let rem = List.rev_map (fun rem -> CAst.make ?loc:rem.CAst.loc (qual, VernacFlagList [rem])) rem in
205 let extra = List.rev_append rem extra in
206 extra, v
207
208 (** [program_mode] tells that Program mode has been activated, either
209 globally via [Set Program] or locally via the Program command prefix. *)
210
211 let program_mode_option_name = ["Program";"Mode"]
212 let program_mode = ref false
213
214 let () = let open Goptions in
215 declare_bool_option
216 { optstage = Summary.Stage.Interp;
217 optdepr = None;
218 optkey = program_mode_option_name;
219 optread = (fun () -> !program_mode);
220 optwrite = (fun b -> program_mode:=b) }
221
222 let program =
223 enable_attribute ~key:"program" ~default:(fun () -> !program_mode)
224
225 (* This is a bit complex as the grammar in g_vernac.mlg doesn't
226 distingish between the boolean and ternary case.*)
227 let option_locality_parser =
228 let name = "Locality" in
229 attribute_of_list [
230 ("local", single_key_parser ~name ~key:"local" Goptions.OptLocal);
231 ("global", single_key_parser ~name ~key:"global" Goptions.OptGlobal);
232 ("export", single_key_parser ~name ~key:"export" Goptions.OptExport);
233 ]
234
235 let option_locality =
236 option_locality_parser >>= function
237 | None -> return Goptions.OptDefault
238 | Some l -> return l
239
240 let explicit_hint_locality =
241 let open Hints in
242 let name = "Locality" in
243 attribute_of_list [
244 ("local", single_key_parser ~name ~key:"local" Local);
245 ("global", single_key_parser ~name ~key:"global" SuperGlobal);
246 ("export", single_key_parser ~name ~key:"export" Export);
247 ]
248
249 let default_hint_locality () =
250 if Lib.sections_are_opened () then Hints.Local else Hints.Export
251
252 let hint_locality = explicit_hint_locality >>= function
253 | Some v -> return v
254 | None -> return (default_hint_locality())
255
256 let hint_locality_no_sections =
257 explicit_hint_locality >>= function
258 | Some v ->
259 let () = if v <> Local && Lib.sections_are_opened() then
260 CErrors.user_err Pp.(str "This command does not support this locality in sections.")
261 in
262 return v
263 | None -> return (default_hint_locality())
264
265 let hint_locality_default_superglobal = explicit_hint_locality >>= function
266 | Some v -> return v
267 | None -> return Hints.SuperGlobal
268
269 (* locality is supposed to be true when local, false when global *)
270 let locality =
271 let name = "Locality" in
272 attribute_of_list [
273 ("local", single_key_parser ~name ~key:"local" true);
274 ("global", single_key_parser ~name ~key:"global" false);
275 ]
276
277 let ukey = "universes"
278
279 let universe_polymorphism_option_name = ["Universe"; "Polymorphism"]
280 let is_universe_polymorphism =
281 let b = ref false in
282 let () = let open Goptions in
283 declare_bool_option
284 { optstage = Summary.Stage.Interp;
285 optdepr = None;
286 optkey = universe_polymorphism_option_name;
287 optread = (fun () -> !b);
288 optwrite = ((:=) b) }
289 in
290 fun () -> !b
291
292 let polymorphic =
293 qualify_attribute ukey (bool_attribute ~name:"polymorphic") >>= function
294 | Some b -> return b
295 | None -> return (is_universe_polymorphism())
296
297 let { Goptions.get = is_polymorphic_inductive_cumulativity } =
298 Goptions.declare_bool_option_and_ref ~key:["Polymorphic"; "Inductive"; "Cumulativity"] ~value:false ()
299
300 let cumulative kind =
301 match kind with
302 | PolyFlags.Inductive -> qualify_attribute ukey (bool_attribute ~name:"cumulative")
303 | PolyFlags.Assumption | PolyFlags.Definition ->
304 (* Not yet supported *)
305 return None
306
307 let poly kind =
308 (polymorphic ++ cumulative kind) >>= fun (univ_poly, cumulative) ->
309 let cumulative =
310 match cumulative with
311 | None -> if univ_poly then is_polymorphic_inductive_cumulativity() else false
312 | Some b ->
313 if b && not univ_poly then
314 CErrors.user_err Pp.(str "Cannot set polymorphic inductive cumulativity status when not in universe polymorphism mode.")
315 else b
316 in
317 return (PolyFlags.make ~univ_poly ~cumulative ~collapse_sort_variables:true)
318
319 let poly_def = poly PolyFlags.Definition
320
321 let template =
322 qualify_attribute ukey
323 (bool_attribute ~name:"template")
324
325 let unfold_fix =
326 enable_attribute ~key:"unfold_fix" ~default:(fun () -> false)
327
328 let only_locality atts = parse locality atts
329
330 let only_polymorphism atts = parse polymorphic atts
331
332 let vernac_polymorphic_flag loc =
333 CAst.make ?loc (ukey, VernacFlagList [CAst.make ?loc ("polymorphic", VernacFlagEmpty)])
334 let vernac_monomorphic_flag loc =
335 CAst.make ?loc (ukey, VernacFlagList [CAst.make ?loc ("polymorphic", VernacFlagLeaf (FlagQualid (Libnames.qualid_of_string "no")))])
336
337 let reversible = bool_attribute ~name:"reversible"
338
339 let canonical_field =
340 enable_attribute ~key:"canonical" ~default:(fun () -> true)
341 let canonical_instance =
342 enable_attribute ~key:"canonical" ~default:(fun () -> false)
343
344 let payload_parser ?cat ~name : string key_parser = fun ?loc orig args ->
345 match args with
346 | VernacFlagLeaf (FlagString str) -> begin match orig, cat with
347 | None, _ -> str
348 | Some orig, Some cat -> cat orig str
349 | Some _, None -> error_twice ?loc ~name
350 end
351 | _ -> CErrors.user_err ?loc Pp.(str "Ill formed \"" ++ str name ++ str"\" attribute (string expected)")
352
353 let qualid_parser ~name : Libnames.qualid key_parser = fun ?loc orig args ->
354 match args with
355 | VernacFlagLeaf (FlagQualid str) -> begin match orig with
356 | None -> str
357 | Some _ -> error_twice ?loc ~name
358 end
359 | _ -> CErrors.user_err ?loc Pp.(str "Ill formed \"" ++ str name ++ str"\" attribute (qualid expected)")
360
361 let payload_attribute ?cat ~name = attribute_of_list [name, payload_parser ?cat ~name]
362
363 let using = payload_attribute ?cat:None ~name:"using"
364
365 let deprecation_parser parse_use : _ key_parser = fun ?loc orig args ->
366 assert_once ?loc ~name:"deprecation" orig;
367 match args with
368 | VernacFlagList l ->
369 let subatt name = payload_attribute ~name in
370 let use_att = attribute_of_list ["use", qualid_parser ~name:"use"] in
371 let (note, since), use = parse (subatt "note" ++ subatt "since" ++ use_att) l in
372 let depr = Deprecation.make ?since ?note () in
373 parse_use depr use
374 | _ ->
375 CErrors.user_err ?loc
376 Pp.(str "Ill formed “deprecated” attribute:" ++ spc() ++
377 str "expected “deprecated(since = \"since\", note = \"note\")“.")
378
379 let no_use_allowed depr = function
380 | None -> depr
381 | Some _ -> CErrors.user_err Pp.(str "Attribute use not allowed")
382
383 let extended_globref_allowed depr = function
384 | None -> Deprecation.make_with_qf depr ()
385 | Some p ->
386 let use_instead =
387 try Nametab.locate_extended p
388 with Not_found -> CErrors.user_err ?loc:p.CAst.loc Pp.(Libnames.pr_qualid p ++ str " not found.")
389 in
390 Deprecation.make_with_qf depr ~use_instead ()
391
392 let deprecation_gen parse_use = attribute_of_list ["deprecated",deprecation_parser parse_use]
393 let deprecation = deprecation_gen no_use_allowed
394 let deprecation_with_use_globref_instead = deprecation_gen extended_globref_allowed
395
396 let user_warn_parser : UserWarn.warn list key_parser = fun ?loc orig args ->
397 let orig = Option.default [] orig in
398 match args with
399 | VernacFlagList l ->
400 let subatt name = payload_attribute ~name in
401 let note, cats = parse (subatt "note" ++ subatt "cats") l in
402 let note = match note with
403 | Some x -> x
404 | None -> CErrors.user_err Pp.(str "Ill formed “warn” attribute (\"note\" is required).")
405 in
406 UserWarn.make_warn ~note ?cats () :: orig
407 | _ -> CErrors.user_err ?loc (Pp.str "Ill formed “warn” attribute (expected \"note\" and optionally \"cats\" sub-attributes).")
408
409 let user_warn_warn =
410 attribute_of_list ["warn",user_warn_parser] >>= function
411 | None -> return []
412 | Some l -> return (List.rev l)
413
414 let user_warns =
415 (deprecation ++ user_warn_warn) >>= function
416 | None, [] -> return None
417 | depr, warn -> return (Some UserWarn.{ depr; warn })
418
419 let user_warns_with_use_globref_instead =
420 (deprecation_with_use_globref_instead ++ user_warn_warn) >>= function
421 | None, [] -> return None
422 | depr_qf, warn_qf -> return (Some UserWarn.{ depr_qf; warn_qf })
423
424 let process_typing_att ?loc ~typing_flags att disable =
425 let enable = not disable in
426 match att with
427 | "universes" ->
428 { typing_flags with
429 Declarations.check_universes = enable
430 }
431 | "guard" ->
432 { typing_flags with
433 Declarations.check_guarded = enable
434 }
435 | "positivity" ->
436 { typing_flags with
437 Declarations.check_positive = enable
438 }
439 | att ->
440 CErrors.user_err ?loc Pp.(str "Unknown “typing” attribute: " ++ str att)
441
442 let process_typing_disable ?loc ~key = function
443 | VernacFlagEmpty -> true
444 | VernacFlagLeaf (FlagQualid q) when qualid_is_this_ident q "yes" ->
445 true
446 | VernacFlagLeaf (FlagQualid q) when qualid_is_this_ident q "no" ->
447 false
448 | _ ->
449 CErrors.user_err ?loc Pp.(str "Ill-formed attribute value, must be " ++ str key ++ str "={yes, no}")
450
451 let typing_flags_parser : Declarations.typing_flags key_parser = fun ?loc orig args ->
452 let rec flag_parser typing_flags = function
453 | [] -> typing_flags
454 | {CAst.v=typing_att, disable; loc} :: rest ->
455 let disable = process_typing_disable ?loc ~key:typing_att disable in
456 let typing_flags = process_typing_att ?loc ~typing_flags typing_att disable in
457 flag_parser typing_flags rest
458 in
459 match args with
460 | VernacFlagList atts ->
461 let typing_flags = Global.typing_flags () in
462 flag_parser typing_flags atts
463 | att ->
464 CErrors.user_err ?loc Pp.(str "Ill-formed “typing” attribute: " ++ pr_vernac_flag_value att)
465
466 let typing_flags =
467 attribute_of_list ["bypass_check", typing_flags_parser]
468
469 let bind_scope_where =
470 let name = "where to bind scope" in
471 attribute_of_list [
472 ("add_top", single_key_parser ~name ~key:"add_top" Notation.AddScopeTop);
473 ("add_bottom", single_key_parser ~name ~key:"add_bottom" Notation.AddScopeBottom);
474 ]
475
476 let raw_attributes : _ attribute = fun flags -> [], flags