Debian Codegraph @

interp/genintern.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 Names
12 open Genarg
13
14 module Store = Store.Make ()
15
16 type ntnvar_status = {
17 mutable ntnvar_used : bool list;
18 mutable ntnvar_used_as_binder : bool;
19 mutable ntnvar_scopes : Notation_term.subscopes option;
20 mutable ntnvar_binding_ids : Notation_term.notation_var_binders option;
21 ntnvar_typ : Notation_term.notation_var_internalization_type;
22 }
23
24 type intern_variable_status = {
25 intern_ids : Id.Set.t;
26 notation_variable_status : ntnvar_status Id.Map.t;
27 }
28
29 type glob_sign = {
30 ltacvars : Id.Set.t;
31 genv : Environ.env;
32 extra : Store.t;
33 intern_sign : intern_variable_status;
34 strict_check : bool;
35 }
36
37 let empty_intern_sign = {
38 intern_ids = Id.Set.empty;
39 notation_variable_status = Id.Map.empty;
40 }
41
42 let empty_glob_sign ~strict env = {
43 ltacvars = Id.Set.empty;
44 genv = env;
45 extra = Store.empty;
46 intern_sign = empty_intern_sign;
47 strict_check = strict;
48 }
49
50 (** In globalize tactics, we need to keep the initial [constr_expr] to recompute
51 in the environment by the effective calls to Intro, Inversion, etc
52 The [constr_expr] field is [None] in TacDef though *)
53 type glob_constr_and_expr = Glob_term.glob_constr * Constrexpr.constr_expr option
54 type glob_constr_pattern_and_expr = Id.Set.t * glob_constr_and_expr * Pattern.uninstantiated_pattern
55
56 type ('raw, 'glb) intern_fun = glob_sign -> 'raw -> glob_sign * 'glb
57 type 'glb ntn_subst_fun = ntnvar_status Id.Map.t -> (Id.t -> Glob_term.glob_constr option) -> 'glb -> 'glb
58
59 module InternObj =
60 struct
61 type ('raw, 'glb, 'top) obj = ('raw, 'glb) intern_fun
62 let name = "intern"
63 let default _ = None
64 end
65
66 module NtnSubstObj =
67 struct
68 type ('raw, 'glb, 'top) obj = 'glb ntn_subst_fun
69 let name = "notation_subst"
70 let default _ = None
71 end
72
73 module Intern = Register (InternObj)
74 module NtnSubst = Register (NtnSubstObj)
75
76 let intern = Intern.obj
77 let register_intern0 = Intern.register0
78
79 let generic_intern ist (GenArg (Rawwit wit, v)) =
80 let (ist, v) = intern wit ist v in
81 (ist, in_gen (glbwit wit) v)
82
83 type ('raw,'glb) intern_pat_fun = ?loc:Loc.t -> ('raw,'glb) intern_fun
84
85 module InternPatObj = struct
86 type ('raw, 'glb, 'top) obj = ('raw, 'glb) intern_pat_fun
87 let name = "intern_pat"
88 let default tag =
89 Some (fun ?loc ->
90 let name = Genarg.(ArgT.repr tag) in
91 CErrors.user_err ?loc Pp.(str "This quotation is not supported in tactic patterns (" ++ str name ++ str ")"))
92 end
93
94 module InternPat = Register (InternPatObj)
95
96 let intern_pat = InternPat.obj
97
98 let register_intern_pat = InternPat.register0
99
100 let generic_intern_pat ?loc ist (GenArg (Rawwit wit, v)) =
101 let (ist, v) = intern_pat wit ?loc ist v in
102 (ist, in_gen (glbwit wit) v)
103
104 (** Notation substitution *)
105
106 let substitute_notation = NtnSubst.obj
107 let register_ntn_subst0 = NtnSubst.register0
108
109 let generic_substitute_notation avoid env (GenArg (Glbwit wit, v) as orig) =
110 let v' = substitute_notation wit avoid env v in
111 if v' == v then orig else in_gen (glbwit wit) v'
112
113 let with_used_ntnvars ntnvars f =
114 let () = Id.Map.iter (fun _ status ->
115 status.ntnvar_used <- false:: status.ntnvar_used)
116 ntnvars
117 in
118 match f () with
119 | v ->
120 let used = Id.Map.fold (fun id status acc -> match status.ntnvar_used with
121 | [] -> assert false
122 | false :: rest -> status.ntnvar_used <- rest; acc
123 | true :: rest ->
124 let rest = match rest with
125 | [] | true :: _ -> rest
126 | false :: rest -> true :: rest
127 in
128 status.ntnvar_used <- rest;
129 Id.Set.add id acc)
130 ntnvars
131 Id.Set.empty
132 in
133 used, v
134 | exception e ->
135 let e = Exninfo.capture e in
136 let () = Id.Map.iter (fun _ status -> status.ntnvar_used <- List.tl status.ntnvar_used) ntnvars in
137 Exninfo.iraise e