Debian Codegraph @

plugins/syntax/number_string.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 open Pp
12 open Util
13 open Names
14 open Libnames
15 open Glob_term
16 open Notation
17 open PrimNotations
18
19 module CSet = CSet.Make (Constr)
20 module CMap = CMap.Make (Constr)
21
22 let mkRef env sigma g =
23 let sigma, c = Evd.fresh_global env sigma g in
24 sigma, EConstr.Unsafe.to_constr c
25
26 let gref q = DAst.make (GRef (q,None))
27
28 (** * Number notation *)
29
30 type number_string_via = qualid * (bool * qualid * qualid) list
31 type number_option =
32 | After of numnot_option
33 | Via of number_string_via
34
35 let warn_abstract_large_num_no_op =
36 CWarnings.create ~name:"abstract-large-number-no-op" ~category:CWarnings.CoreCategories.numbers
37 (fun f ->
38 strbrk "The 'abstract after' directive has no effect when " ++
39 strbrk "the parsing function (" ++
40 Termops.pr_global_env (Global.env ()) f ++ strbrk ") targets an " ++
41 strbrk "option type.")
42
43 let get_constructors ind =
44 let mib,oib = Global.lookup_inductive ind in
45 let mc = oib.Declarations.mind_consnames in
46 Array.to_list
47 (Array.mapi (fun j c -> GlobRef.ConstructRef (ind, j + 1)) mc)
48
49 let unsafe_ref_ind q =
50 match q with
51 | GlobRef.IndRef i -> i
52 | _ -> raise Not_found
53
54 type 'kind target = {
55 kind : 'kind;
56 typ : glob_constr;
57 }
58
59 let locate_z () =
60 let zn = "num.Z.type" in
61 let pn = "num.pos.type" in
62 match Rocqlib.lib_ref zn, Rocqlib.lib_ref pn with
63 | exception Rocqlib.NotFoundRef _ -> []
64 | q_z, q_pos ->
65 let z_ty = { z_ty = unsafe_ref_ind q_z; pos_ty = unsafe_ref_ind q_pos; } in
66 [ { kind = Z z_ty; typ = gref q_z }; ]
67
68 let locate_number () =
69 let dint = "num.int.type" in
70 let duint = "num.uint.type" in
71 let dec = "num.decimal.type" in
72 let hint = "num.hexadecimal_int.type" in
73 let huint = "num.hexadecimal_uint.type" in
74 let hex = "num.hexadecimal.type" in
75 let int = "num.num_int.type" in
76 let uint = "num.num_uint.type" in
77 let num = "num.number.type" in
78 match Rocqlib.lib_ref dint, Rocqlib.lib_ref duint, Rocqlib.lib_ref dec
79 , Rocqlib.lib_ref hint, Rocqlib.lib_ref huint, Rocqlib.lib_ref hex
80 , Rocqlib.lib_ref int, Rocqlib.lib_ref uint, Rocqlib.lib_ref num
81 with
82 | exception Rocqlib.NotFoundRef _ -> []
83 | q_dint, q_duint, q_dec, q_hint, q_huint, q_hex, q_int, q_uint, q_num ->
84 let int_ty = {
85 dec_int = unsafe_ref_ind q_dint;
86 dec_uint = unsafe_ref_ind q_duint;
87 hex_int = unsafe_ref_ind q_hint;
88 hex_uint = unsafe_ref_ind q_huint;
89 int = unsafe_ref_ind q_int;
90 uint = unsafe_ref_ind q_uint;
91 } in
92 let num_ty = {
93 int = int_ty;
94 decimal = unsafe_ref_ind q_dec;
95 hexadecimal = unsafe_ref_ind q_hex;
96 number = unsafe_ref_ind q_num;
97 } in
98 [ { kind = Int int_ty; typ = gref q_int };
99 { kind = UInt int_ty; typ = gref q_uint };
100 { kind = Number num_ty; typ = gref q_num };
101 ]
102
103 let locate_int63 () =
104 let pos_neg_int63n = "num.int63.pos_neg_int63" in
105 match Rocqlib.lib_ref pos_neg_int63n with
106 | exception Rocqlib.NotFoundRef _ -> []
107 | pos_neg_int63 ->
108 [ { kind = Int63 {pos_neg_int63_ty = unsafe_ref_ind pos_neg_int63}; typ = gref pos_neg_int63 }; ]
109
110
111 let locate_float () =
112 let floatn = "num.float.type" in
113 match Rocqlib.lib_ref floatn with
114 | exception Rocqlib.NotFoundRef _ -> []
115 | q_float ->
116 [ { kind = Float64; typ = gref q_float }; ]
117
118 let has_type env sigma f ty =
119 let c = DAst.make @@ GCast (f, Some Constr.DEFAULTcast, ty) in
120 let flags = Pretyping.{ all_and_fail_flags with use_coercions = false } in
121 try let _ = Pretyping.understand ~flags env sigma c in true
122 with Pretype_errors.PretypeError _ -> false
123
124 let q_option () = Rocqlib.lib_ref_opt "core.option.type"
125
126 let q_result () = Rocqlib.lib_ref_opt "core.result.type"
127
128 let is_to_target env sigma f cty {kind; typ} =
129 let (>>?) opt f = match opt with
130 | None -> false
131 | Some x -> f x
132 in
133 let arrow x y =
134 DAst.make @@ GProd (Anonymous,None,Glob_term.Explicit, x, y)
135 in
136 let app x y = DAst.make @@ GApp (gref x,[y]) in
137 let app2 x y z = DAst.make @@ GApp (gref x,[y;z]) in
138 let ghole = DAst.make @@ GHole GInternalHole in
139 if has_type env sigma f (arrow typ cty) then
140 Some (kind, Direct)
141 else if q_option() >>? fun q_opt -> has_type env sigma f (arrow typ (app q_opt cty)) then
142 Some (kind, Option)
143 else if q_result() >>? fun q_result -> has_type env sigma f (arrow typ (app2 q_result cty ghole)) then
144 Some (kind, Error)
145 else None
146
147 let is_from_target env sigma g cty {kind; typ} =
148 let (>>?) opt f = match opt with
149 | None -> false
150 | Some x -> f x
151 in
152 let arrow x y =
153 DAst.make @@ GProd (Anonymous,None,Glob_term.Explicit, x, y)
154 in
155 let app x y = DAst.make @@ GApp (gref x,[y]) in
156 let app2 x y z = DAst.make @@ GApp (gref x,[y;z]) in
157 let ghole = DAst.make @@ GHole GInternalHole in
158 if has_type env sigma g (arrow cty typ) then
159 Some (kind, Direct)
160 else if q_option() >>? fun q_opt -> has_type env sigma g (arrow cty (app q_opt typ)) then
161 Some (kind, Option)
162 else if q_result() >>? fun q_result -> has_type env sigma g (arrow cty (app2 q_result typ ghole)) then
163 Some (kind, Error)
164 else None
165
166 let type_error_to f ty =
167 let ppty = pr_qualid ty in
168 CErrors.user_err
169 (pr_qualid f ++ str " should go from Number.int to " ++
170 ppty ++ str " or (option " ++ ppty ++ str "), or (result " ++ ppty ++ str " _)." ++
171 fnl () ++ strbrk "Instead of Number.int, the types Number.uint or Z or PrimInt63.pos_neg_int63 or PrimFloat.float or Number.number could be used (you may need to require BinNums or Number or PrimInt63 or PrimFloat first).")
172
173 let type_error_of g ty =
174 CErrors.user_err
175 (pr_qualid g ++ str " should go from " ++ pr_qualid ty ++
176 str " to Number.int or (option Number.int), or (result Number.int _)." ++ fnl () ++
177 strbrk "Instead of Number.int, the types Number.uint or Z or PrimInt63.pos_neg_int63 or PrimFloat.float or Number.number could be used (you may need to require BinNums or Number or PrimInt63 or PrimFloat first).")
178
179 let error_params ind =
180 CErrors.user_err
181 (str "Wrong number of parameters for inductive" ++ spc ()
182 ++ Printer.pr_global (GlobRef.IndRef ind) ++ str ".")
183
184 let remapping_error ?loc ty ty' ty'' =
185 CErrors.user_err ?loc
186 (Printer.pr_global ty
187 ++ str " was already mapped to" ++ spc () ++ Printer.pr_global ty'
188 ++ str " and cannot be remapped to" ++ spc () ++ Printer.pr_global ty''
189 ++ str ".")
190
191 let error_missing c =
192 CErrors.user_err
193 (str "Missing mapping for constructor " ++ Printer.pr_global c ++ str ".")
194
195 let warn_via_remapping =
196 CWarnings.create ~name:"via-type-remapping" ~category:CWarnings.CoreCategories.numbers
197 (fun (env, sigma, ty, ty', ty'') ->
198 let constr = Printer.pr_constr_env env sigma in
199 constr ty ++ str " was already mapped to" ++ spc () ++ constr ty'
200 ++ str ", mapping it also to" ++ spc () ++ constr ty''
201 ++ str " might yield ill typed terms when using the notation.")
202
203 let warn_via_type_mismatch =
204 CWarnings.create ~name:"via-type-mismatch" ~category:CWarnings.CoreCategories.numbers
205 (fun (env, sigma, g, g', exp, actual) ->
206 let constr = Printer.pr_constr_env env sigma in
207 str "Type of" ++ spc() ++ Printer.pr_global g
208 ++ str " seems incompatible with the type of" ++ spc ()
209 ++ Printer.pr_global g' ++ str "." ++ spc ()
210 ++ str "Expected type is: " ++ constr exp ++ spc ()
211 ++ str "instead of " ++ constr actual ++ str "." ++ spc ()
212 ++ str "This might yield ill typed terms when using the notation.")
213
214 let multiple_via_error () =
215 CErrors.user_err (Pp.str "Multiple 'via' options.")
216
217 let multiple_after_error () =
218 CErrors.user_err (Pp.str "Multiple 'warning after' or 'abstract after' options.")
219
220 let via_abstract_error () =
221 CErrors.user_err (Pp.str "'via' and 'abstract' cannot be used together.")
222
223 let locate_global_sort_inductive_or_constant env sigma qid =
224 let locate_sort qid =
225 match Nametab.locate_extended qid with
226 | Globnames.TrueGlobal _ -> raise Not_found
227 | Globnames.Abbrev kn ->
228 match Abbreviation.find_interp kn with
229 | [], Notation_term.NSort r ->
230 let sigma,c = Glob_ops.fresh_glob_sort_in_quality sigma r in
231 let c = EConstr.ESorts.kind sigma c in
232 sigma, Constr.mkSort c
233 | _ -> raise Not_found in
234 try locate_sort qid
235 with Not_found ->
236 let g = Smartlocate.global_with_alias qid in
237 let () = match g with
238 | IndRef _ | ConstRef _ -> ()
239 | VarRef _ | ConstructRef _ ->
240 CErrors.user_err Pp.(pr_qualid qid ++ spc() ++ str "is not an inductive type or a constant.")
241 in
242 mkRef env sigma g
243
244 let locate_global_constructor_inductive_or_constant env sigma qid =
245 let g = Smartlocate.global_with_alias qid in
246 let () = match g with
247 | ConstructRef _ | IndRef _ | ConstRef _ -> ()
248 | VarRef _ -> CErrors.user_err Pp.(pr_qualid qid ++ spc() ++ str "is a section variable.")
249 in
250 let sigma, c = mkRef env sigma g in
251 sigma, g, c
252
253 (* [get_type env sigma c] retrieves the type of [c] and returns a pair
254 [l, t] such that [c : l_0 -> ... -> l_n -> t]. *)
255 let get_type env sigma c =
256 (* inspired from [compute_implicit_names] in "interp/impargs.ml" *)
257 let rec aux env acc t =
258 let t = Reductionops.whd_all env sigma t in
259 match EConstr.kind sigma t with
260 | Constr.Prod (na, a, b) ->
261 let a = Reductionops.whd_all env sigma a in
262 let rel = Context.Rel.Declaration.LocalAssum (na, a) in
263 aux (EConstr.push_rel rel env) ((na, a) :: acc) b
264 | _ -> List.rev acc, t in
265 let t = Retyping.get_type_of env sigma (EConstr.of_constr c) in
266 let l, t = aux env [] t in
267 List.map (fun (na, a) -> EConstr.Unsafe.to_binder_annot na, EConstr.Unsafe.to_constr a) l,
268 EConstr.Unsafe.to_constr t
269
270 (* [elaborate_to_post_params env sigma ty_ind params] builds the
271 [to_post] translation (c.f., interp/notation.mli) for the numeral
272 notation to parse/print type [ty_ind]. This translation is the
273 identity ([ToPostCopy]) except that it checks ([ToPostCheck]) that
274 the parameters of the inductive type [ty_ind] match the ones given
275 in [params]. *)
276 let elaborate_to_post_params env sigma ty_ind params =
277 let to_post_for_constructor indc =
278 let sigma, c = match indc with
279 | GlobRef.ConstructRef _ -> mkRef env sigma indc
280 | _ -> assert false in (* c.f. get_constructors *)
281 let args, t = get_type env sigma c in
282 let params_indc = match Constr.kind t with
283 | Constr.App (_, a) -> Array.to_list a | _ -> [] in
284 let sz = List.length args in
285 let a = Array.make sz ToPostCopy in
286 if List.length params <> List.length params_indc then error_params ty_ind;
287 List.iter2 (fun param param_indc ->
288 match param, Constr.kind param_indc with
289 | Some p, Constr.Rel i when i <= sz -> a.(sz - i) <- ToPostCheck p
290 | _ -> ())
291 params params_indc;
292 indc, indc, Array.to_list a in
293 let pt_refs = get_constructors ty_ind in
294 let to_post_0 = List.map to_post_for_constructor pt_refs in
295 let to_post =
296 let only_copy (_, _, args) = List.for_all ((=) ToPostCopy) args in
297 if (List.for_all only_copy to_post_0) then [||] else [|to_post_0|] in
298 to_post, pt_refs
299
300 (* [elaborate_to_post_via env sigma ty_name ty_ind l] builds the [to_post]
301 translation (c.f., interp/notation.mli) for the number notation to
302 parse/print type [ty_name] through the inductive [ty_ind] according
303 to the pairs [constant, constructor] in the list [l]. *)
304 let elaborate_to_post_via env sigma ty_name ty_ind l =
305 let sigma, ty_name =
306 locate_global_sort_inductive_or_constant env sigma ty_name in
307 let sigma, ty_ind = mkRef env sigma (IndRef ty_ind) in
308 (* Retrieve constants and constructors mappings and their type.
309 For each constant [cnst] and inductive constructor [indc] in [l], retrieve:
310 * its location: [lcnst] and [lindc]
311 * its GlobRef: [cnst] and [indc]
312 * its type: [tcnst] and [tindc] (decomposed in product by [get_type] above)
313 * [impls] are the implicit arguments of [cnst] *)
314 let sigma, l =
315 let read sigma (consider_implicits, cnst, indc) =
316 let lcnst, lindc = cnst.CAst.loc, indc.CAst.loc in
317 let sigma, cnst, ccnst = locate_global_constructor_inductive_or_constant env sigma cnst in
318 let indc = GlobRef.ConstructRef (Smartlocate.global_constructor_with_alias indc) in
319 let sigma, cindc = mkRef env sigma indc in
320 let get_type_wo_params c =
321 (* ignore parameters of inductive types *)
322 let rm_params c = match Constr.kind c with
323 | Constr.App (c, _) when Constr.isInd c -> c
324 | _ -> c in
325 let lc, tc = get_type env sigma c in
326 List.map (fun (n, c) -> n, rm_params c) lc, rm_params tc in
327 let tcnst, tindc = get_type_wo_params ccnst, get_type_wo_params cindc in
328 let impls =
329 if not consider_implicits then [] else
330 Impargs.(select_stronger_impargs (implicits_of_global cnst)) in
331 sigma, (lcnst, cnst, tcnst, lindc, indc, tindc, impls) in
332 List.fold_left_map read sigma l
333 in
334 let eq_indc indc (_, _, _, _, indc', _, _) = Environ.QGlobRef.equal env indc indc' in
335 (* Collect all inductive types involved.
336 That is [ty_ind] and all final codomains of [tindc] above. *)
337 let inds =
338 List.fold_left (fun s (_, _, _, _, _, tindc, _) -> CSet.add (snd tindc) s)
339 (CSet.singleton ty_ind) l in
340 (* And for each inductive, retrieve its constructors. *)
341 let constructors =
342 CSet.fold (fun ind m ->
343 let inductive, _ = Constr.destInd ind in
344 CMap.add ind (get_constructors inductive) m)
345 inds CMap.empty in
346 (* Error if one [constructor] in some inductive in [inds]
347 doesn't appear exactly once in [l] *)
348 let _ : _ list = (* check_for duplicate constructor and error *)
349 List.fold_left (fun already_seen (_, cnst, _, loc, indc, _, _) ->
350 try
351 let cnst' = List.assoc_f (fun c1 c2 -> Environ.QGlobRef.equal env c1 c2) indc already_seen in
352 remapping_error ?loc indc cnst' cnst
353 with Not_found -> (indc, cnst) :: already_seen)
354 [] l in
355 let () = (* check for missing constructor and error *)
356 CMap.iter (fun _ ctors ->
357 List.iter (fun cstr ->
358 if not (List.exists (eq_indc cstr) l) then error_missing cstr)
359 ctors)
360 constructors in
361 (* Perform some checks on types and warn if they look strange.
362 These checks are neither sound nor complete, so we only warn. *)
363 let () =
364 (* associate inductives to types, and check that this mapping is one to one
365 and maps [ty_ind] to [ty_name] *)
366 let ind2ty, ty2ind =
367 let add loc ckey cval m =
368 match CMap.find_opt ckey m with
369 | None -> CMap.add ckey cval m
370 | Some old_cval ->
371 if not (Constr.eq_constr_nounivs old_cval cval) then
372 warn_via_remapping ?loc (env, sigma, ckey, old_cval, cval);
373 m in
374 List.fold_left
375 (fun (ind2ty, ty2ind) (lcnst, _, (_, tcnst), lindc, _, (_, tindc), _) ->
376 add lcnst tindc tcnst ind2ty, add lindc tcnst tindc ty2ind)
377 CMap.(singleton ty_ind ty_name, singleton ty_name ty_ind) l in
378 (* check that type of constants and constructors mapped in [l]
379 match modulo [ind2ty] *)
380 let rm_impls impls (l, t) =
381 let rec aux impls l = match impls, l with
382 | Some _ :: impls, _ :: b -> aux impls b
383 | None :: impls, (n, a) :: b -> (n, a) :: aux impls b
384 | _ -> l in
385 aux impls l, t in
386 let replace m (l, t) =
387 let apply_m c = try CMap.find c m with Not_found -> c in
388 List.fold_right (fun (na, a) b -> Constr.mkProd (na, (apply_m a), b))
389 l (apply_m t) in
390 List.iter (fun (_, cnst, tcnst, loc, indc, tindc, impls) ->
391 let tcnst = rm_impls impls tcnst in
392 let tcnst' = replace CMap.empty tcnst in
393 if not (Constr.eq_constr_nounivs tcnst' (replace ind2ty tindc)) then
394 let actual = replace CMap.empty tindc in
395 let expected = replace ty2ind tcnst in
396 warn_via_type_mismatch ?loc (env, sigma, indc, cnst, expected, actual))
397 l in
398 (* Associate an index to each inductive, starting from 0 for [ty_ind]. *)
399 let ind2num, num2ind, nb_ind =
400 CMap.fold (fun ind _ (ind2num, num2ind, i) ->
401 CMap.add ind i ind2num, Int.Map.add i ind num2ind, i + 1)
402 (CMap.remove ty_ind constructors)
403 (CMap.singleton ty_ind 0, Int.Map.singleton 0 ty_ind, 1) in
404 (* Finally elaborate [to_post] *)
405 let to_post =
406 let rec map_prod impls tindc = match impls with
407 | (Some _ as i) :: impls -> ToPostHole (Impargs.name_of_implicit i) :: map_prod impls tindc
408 | _ ->
409 match tindc with
410 | [] -> []
411 | (_, a) :: b ->
412 let t = match CMap.find_opt a ind2num with
413 | Some i -> ToPostAs i
414 | None -> ToPostCopy in
415 let impls = match impls with [] -> [] | _ :: t -> t in
416 t :: map_prod impls b in
417 Array.init nb_ind (fun i ->
418 List.map (fun indc ->
419 let _, cnst, _, _, _, tindc, impls = List.find (eq_indc indc) l in
420 indc, cnst, map_prod impls (fst tindc))
421 (CMap.find (Int.Map.find i num2ind) constructors)) in
422 (* and use constants mapped to constructors of [ty_ind] as triggers. *)
423 let pt_refs = List.map (fun (_, cnst, _) -> cnst) (to_post.(0)) in
424 to_post, pt_refs
425
426 type target_type =
427 | TargetInd of (inductive * Constr.t option list)
428 | TargetPrim of GlobRef.t * GlobRef.t list * required_module
429
430 let locate_global_inductive_with_params allow_params qid =
431 if not allow_params then raise Not_found else
432 match Nametab.locate_extended qid with
433 | Globnames.TrueGlobal _ -> raise Not_found
434 | Globnames.Abbrev kn ->
435 match Abbreviation.find_interp kn with
436 | [], Notation_term.(NApp (NRef (GlobRef.IndRef i,None), l)) ->
437 i,
438 List.map (function
439 | Notation_term.NHole _ -> None
440 | n ->
441 let g = Notation_ops.glob_constr_of_notation_constr n in
442 let c, _ =
443 let env = Global.env () in
444 let sigma = Evd.from_env env in
445 Pretyping.understand env sigma g in
446 Some (EConstr.Unsafe.to_constr c)) l
447 | _ -> raise Not_found
448
449 let locate_global_inductive_or_int63_or_float env allow_params qid =
450 try TargetInd (locate_global_inductive_with_params allow_params qid)
451 with Not_found ->
452 let int63n = "num.int63.type" in
453 let int63c = "num.int63.wrap_int" in
454 let int63w = "num.int63.int_wrapper" in
455 let floatn = "num.float.type" in
456 let floatc = "num.float.wrap_float" in
457 let floatw = "num.float.float_wrapper" in
458 if allow_params && Rocqlib.check_ref int63n (Smartlocate.global_with_alias qid)
459 then TargetPrim (Rocqlib.lib_ref int63w, [Rocqlib.lib_ref int63c],
460 (Nametab.path_of_global (Rocqlib.lib_ref int63n), []))
461 else if allow_params && Rocqlib.check_ref floatn (Smartlocate.global_with_alias qid)
462 then TargetPrim (Rocqlib.lib_ref floatw, [Rocqlib.lib_ref floatc],
463 (Nametab.path_of_global (Rocqlib.lib_ref floatn), []))
464 else TargetInd (Smartlocate.global_inductive_with_alias qid, [])
465
466 let intern_cref env sigma r =
467 Constrintern.intern_constr env sigma (CAst.make @@ Constrexpr.CAppExpl ((r,None),[]))
468
469 let vernac_number_notation local ty f g opts scope =
470 let rec parse_opts = function
471 | [] -> None, Nop
472 | h :: opts ->
473 let via, opts = parse_opts opts in
474 let via = match h, via with
475 | Via _, Some _ -> multiple_via_error ()
476 | Via v, None -> Some v
477 | _ -> via in
478 let opts = match h, opts with
479 | After _, (Warning _ | Abstract _) -> multiple_after_error ()
480 | After a, Nop -> a
481 | _ -> opts in
482 via, opts in
483 let via, opts = parse_opts opts in
484 (match via, opts with Some _, Abstract _ -> via_abstract_error () | _ -> ());
485 let env = Global.env () in
486 let sigma = Evd.from_env env in
487 let targets = List.concat [
488 locate_number ();
489 locate_z ();
490 locate_int63 ();
491 locate_float ();
492 ]
493 in
494 let ty_name = ty in
495 let ty, via =
496 match via with None -> ty, via | Some (ty', a) -> ty', Some (ty, a) in
497 let tyc_params = locate_global_inductive_or_int63_or_float env (via = None) ty in
498 let to_ty = Smartlocate.global_with_alias f in
499 let of_ty = Smartlocate.global_with_alias g in
500 let cty = intern_cref env sigma ty in
501 let f_name, f = f, intern_cref env sigma f in
502 let g_name, g = g, intern_cref env sigma g in
503 (* Check the type of f *)
504 let to_kind =
505 match List.find_map (fun target -> is_to_target env sigma f cty target) targets with
506 | Some v -> v
507 | None -> type_error_to f_name ty
508 in
509 (* Check the type of g *)
510 let cty = match tyc_params with TargetPrim (c, _, _) -> gref c | TargetInd _ -> cty in
511 let of_kind =
512 match List.find_map (fun target -> is_from_target env sigma g cty target) targets with
513 | Some v -> v
514 | None -> type_error_of g_name ty
515 in
516 let to_post, pt_required, pt_refs = match tyc_params with
517 | TargetPrim (_, refs, path) -> [||], path, refs
518 | TargetInd (tyc, params) ->
519 let to_post, pt_refs =
520 match via with
521 | None -> elaborate_to_post_params env sigma tyc params
522 | Some (ty, l) -> elaborate_to_post_via env sigma ty tyc l in
523 to_post, (Nametab.path_of_global (GlobRef.IndRef tyc), []), pt_refs in
524 let o = { to_kind; to_ty; to_post; of_kind; of_ty; ty_name;
525 warning = opts }
526 in
527 (match opts, to_kind with
528 | Abstract _, (_, Option) -> warn_abstract_large_num_no_op o.to_ty
529 | _ -> ());
530 let i =
531 { pt_local = local;
532 pt_scope = scope;
533 pt_interp_info = NumberNotation o;
534 pt_required;
535 pt_refs;
536 pt_in_match = true }
537 in
538 enable_prim_token_interpretation i
539
540 (** * String notation *)
541
542 let locate_global_inductive_or_pstring env allow_params qid =
543 try TargetInd (locate_global_inductive_with_params allow_params qid)
544 with Not_found ->
545 let pstringn = "strings.pstring.type" in
546 let pstringc = "strings.pstring.wrap_string" in
547 let pstringw = "strings.pstring.string_wrapper" in
548 if allow_params && Rocqlib.check_ref pstringn (Smartlocate.global_with_alias qid)
549 then TargetPrim (Rocqlib.lib_ref pstringw, [Rocqlib.lib_ref pstringc],
550 (Nametab.path_of_global (Rocqlib.lib_ref pstringn), []))
551 else TargetInd (Smartlocate.global_inductive_with_alias qid, [])
552
553 let locate_pstring () =
554 match Rocqlib.lib_ref_opt "strings.pstring.type" with
555 | None -> []
556 | Some q_pstring ->
557 [ { kind = PString; typ = gref q_pstring }; ]
558
559 let locate_bytestring () =
560 match Rocqlib.lib_ref_opt "core.byte.type" with
561 | None -> []
562 | Some q_byte ->
563 let byte_target = { kind = Byte; typ = gref q_byte } in
564 match Rocqlib.lib_ref_opt "core.list.type" with
565 | None -> [ byte_target ]
566 | Some q_list ->
567 let app x y = DAst.make @@ GApp (x,[y]) in
568 [ { kind = ListByte; typ = app (gref q_list) (gref q_byte) };
569 byte_target;
570 ]
571
572 let type_error_to f ty =
573 let ppty = pr_qualid ty in
574 CErrors.user_err
575 (pr_qualid f ++ str " should go from Byte.byte, (list Byte.byte), or PrimString.string to " ++
576 ppty ++ str " or (option " ++ ppty ++ str "), or (result " ++ ppty ++ str " _).")
577
578 let type_error_of g ty =
579 CErrors.user_err
580 (pr_qualid g ++ str " should go from " ++ pr_qualid ty ++
581 strbrk " to T or (option T) or (result T _), where T is either Byte.byte, (list Byte.byte), or PrimString.string.")
582
583 let vernac_string_notation local ty f g via scope =
584 let env = Global.env () in
585 let sigma = Evd.from_env env in
586 let targets = List.concat [
587 locate_pstring ();
588 locate_bytestring ();
589 ]
590 in
591 let ty_name = ty in
592 let ty, via =
593 match via with None -> ty, via | Some (ty', a) -> ty', Some (ty, a) in
594 let tyc_params = locate_global_inductive_or_pstring env (via = None) ty in
595 let to_ty = Smartlocate.global_with_alias f in
596 let of_ty = Smartlocate.global_with_alias g in
597 let f_name, f = f, intern_cref env sigma f in
598 let g_name, g = g, intern_cref env sigma g in
599 let cty = intern_cref env sigma ty in
600 (* Check the type of f *)
601 let to_kind =
602 match List.find_map (fun target -> is_to_target env sigma f cty target) targets with
603 | Some v -> v
604 | None -> type_error_to f_name ty
605 in
606 (* Check the type of g *)
607 let of_kind =
608 match List.find_map (fun target -> is_from_target env sigma g cty target) targets with
609 | Some v -> v
610 | None -> type_error_of g_name ty
611 in
612 let to_post, pt_required, pt_refs = match tyc_params with
613 | TargetPrim (_, refs, path) -> [||], path, refs
614 | TargetInd (tyc, params) ->
615 let to_post, pt_refs =
616 match via with
617 | None -> elaborate_to_post_params env sigma tyc params
618 | Some (ty, l) -> elaborate_to_post_via env sigma ty tyc l in
619 to_post, (Nametab.path_of_global (GlobRef.IndRef tyc), []), pt_refs in
620 let o = { to_kind; to_ty; to_post; of_kind; of_ty; ty_name; warning = () } in
621 let i =
622 { pt_local = local;
623 pt_scope = scope;
624 pt_interp_info = StringNotation o;
625 pt_required;
626 pt_refs;
627 pt_in_match = true }
628 in
629 enable_prim_token_interpretation i