Debian Codegraph @

pretyping/libBinding.mli

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
12 (** This file contain a lightweight API for meta-programming: to traverse
13 binders and either keep them, create new ones.
14
15 With this API, variables are refered abstractly using [access_key].
16 For instance, to compute the term associated to a variable (debruijn variable)
17 one should use [get_term : acces_key -> constr t].
18 Such terms can easily be created using the monadic bind: [let* t = get_term k in ...].
19
20 So that the users do not have to create [access_key] themselves, and make mistakes,
21 the creation of [access_key] is directly handled by the functions creating binders.
22 For instance, to create a product [Prod A B], the body [B] has type
23 [access_key -> constr t] where the [access_key] of the variable [A].
24 To easily write code, it recomended to use the notation [let@]:
25 [let@ key_A = make_binder naming na A in (* def of B : constr t *) ].
26
27 It also provides facilities for naming variables, the different functions
28 taking [type naming_scheme = rel_declaration -> rel_declaration t] as arguments.
29 A few basic ones are provided.
30
31 To do so, the API uses a reader monad with a state that contains:
32 - the environment (Declarations + local context),
33 - the evar_map,
34 - the set of names already used,
35 - a substitution from the former context of the term being traversed,
36 to the context of the term being created which may differ due to
37 addition of new binders
38 *)
39
40
41 open Evd
42 open EConstr
43 open Names
44 open Declarations
45 open Environ
46
47 module State :
48 sig
49 type state
50
51 (* Monads *)
52 type 'a t
53 val return : 'a -> 'a t
54 val bind : 'a t -> ('a -> 'b t) -> 'b t
55 val map : ('a -> 'b) -> 'a t -> 'b t
56 val run_state : state -> evar_map -> 'a t -> evar_map * 'a
57 val run : env -> evar_map -> 'a t -> evar_map * 'a
58
59 (* Access the current state *)
60 val get_env : env t
61 val get_sigma : evar_map t
62 val get_names : Nameops.Fresh.t t
63 val get_state : state t
64 val get_context : rel_context t
65
66 (** {6 Weaken Functions From the Former Context to the New Context } *)
67
68 (** Weaken a term defined in the former context by applying [state.subst]. *)
69 val weaken : constr -> constr t
70
71 (** Weaken a declaration defined in the former context by applying [state.subst]. *)
72 val weaken_rel : rel_declaration -> rel_declaration t
73
74 (** Weaken a context defined in the former context by applying [state.subst]. *)
75 val weaken_context : rel_context -> rel_context t
76
77 (** {6 Access Key } *)
78
79 (** Type of access keys for the State API *)
80 type access_key
81 val make_key : int -> access_key t
82
83 (** {6 Push Functions } *)
84
85 (** Add a former definition [decl] to a state [s].
86 It returns the new state, and an access key to acces the declaration.
87 It supposes [decl] is well-typed in the current context. *)
88 val push_old_rel : rel_declaration -> (state * access_key) t
89
90 (** Add a new definition [decl] to a state [s].
91 It returns the new state, and an access key to acces the declaration.
92 It supposes [decl] is well-typed in the current context. *)
93 val push_fresh_rel : rel_declaration -> (state * access_key) t
94
95 (** {6 Access Functions } *)
96
97 (** {7 Access Terms } *)
98
99 (** Compute the debruijn variable associated
100 to an [access_key] in the context [s : state]. *)
101 val get_term : access_key -> constr t
102
103 (** Compute the debruijn variable associated
104 to the [i]-th [access_key] in the context [s : state]. *)
105 val geti_term : access_key list -> int -> constr t
106
107 (** Compute the debruijn variable associated
108 to the [i]-th, [j]-th [access_key] in the context [s : state]. *)
109 val getij_term : access_key list list -> int -> int -> constr t
110
111 (** Compute the debruijn variables associated
112 to a list of [access_key] in the context [s : state]. *)
113 val get_terms : access_key list -> constr array t
114
115 (** {7 Access Types } *)
116
117 (** Compute the type of the variable associated
118 to an [access_key] in the context [s : state]. *)
119 val get_type : access_key -> types t
120
121 (** Compute the type of the variable associated
122 to the [i]-th [access_key] in the context [s : state]. *)
123 val geti_type : access_key list -> int -> types t
124
125 (** Compute the type of the variable associated
126 to the [i]-th, [j]-th [access_key] in the context [s : state]. *)
127 val getij_type : access_key list list -> int -> int -> types t
128
129 (** Compute the type of the variable associated
130 to a list of [access_key] in the context [s : state]. *)
131 val get_types : access_key list -> types array t
132
133 (** {7 Access Binder Annotations } *)
134
135 (** Compute the [binder_annot] of the variable associated
136 to an [access_key] in the context [s : state]. *)
137 val get_aname : access_key -> Name.t binder_annot t
138
139 (** Compute the [binder_annot] of the variable associated
140 to the [i]-th [access_key] in the context [s : state]. *)
141 val geti_aname : access_key list -> int -> Name.t binder_annot t
142
143 (** Compute the [binder_annot] of the variable associated
144 to the [i]-th, [j]-th [access_key] in the context [s : state]. *)
145 val getij_aname : access_key list list -> int -> int -> Name.t binder_annot t
146
147 (** Compute the [binder_annot] of the variable associated to a list of
148 [access_key] in the context [s : state]. *)
149 val get_anames : access_key list -> Name.t binder_annot array t
150
151 (** {7 Check Keys } *)
152
153 val check_key_in : int -> access_key list -> int option t
154
155 (** {6 Debug Functions } *)
156 val list_mapi : (int -> 'a -> 'b t) -> 'a list -> 'b list t
157 val list_map2i : (int -> 'a -> 'b -> 'c t) -> 'a list -> 'b list -> 'c list t
158 val array_mapi : (int -> 'a -> 'b t) -> 'a array -> 'b array t
159 val array_map2i : (int -> 'a -> 'b -> 'c t) -> 'a array -> 'b array -> 'c array t
160
161 end
162
163 open State
164
165 (** {6 Fold Functions for State } *)
166
167 (** This function is meant to iterate a function creating binders.
168 ['c -> 'b list -> 'b list] explains which [access_key] to save, e.g. [::] to save them all. *)
169 val fold_right_state : ('c -> 'b list -> 'b list) -> 'a list ->
170 (int -> 'a -> ('c -> 'd t) -> 'd t) -> ('b list -> 'd t) -> 'd t
171
172 (** This function is meant to iterate a function creating binders: [int -> 'a -> ('c -> 'd t) -> 'd t].
173 ['c -> 'b list -> 'b list] explains which [access_key] to save, e.g. [::] to save them all. *)
174 val fold_left_state : ('c -> 'b list -> 'b list) -> 'a list ->
175 (int -> 'a -> ('c -> 'd t) -> 'd t) -> ('b list -> 'd t) -> 'd t
176
177 (** This function is meant to iterate a function creating binders.
178 ['c -> 'b list -> 'b list] explains which [access_key] to save, e.g. [::] to save them all. *)
179 val fold_right_state_3 : ('c -> 'b list -> 'b list) -> 'a list ->
180 (int -> 'a -> ('c * 'c * 'c -> 'd t) -> 'd t) -> ('b list * 'b list * 'b list -> 'd t) -> 'd t
181
182 (** This function is meant to iterate a function creating binders: [int -> 'a -> ('c -> 'd t) -> 'd t].
183 ['c -> 'b list -> 'b list] explains which [access_key] to save, e.g. [::] to save them all. *)
184 val fold_left_state_3 : ('c -> 'b list -> 'b list) -> 'a list ->
185 (int -> 'a -> ('c * 'c * 'c -> 'd t) -> 'd t) -> ('b list * 'b list * 'b list -> 'd t) -> 'd t
186
187 (** {6 Naming Schemes for Binders } *)
188
189 type naming_scheme = rel_declaration -> rel_declaration t
190
191 (** Identity namming *)
192 val naming_id : naming_scheme
193
194 (** Name the binder using the head of the declaration's type if it [Anonymous]. *)
195 val naming_hd : naming_scheme
196
197 (** [naming_dep] if [true], [naming_id] otherwise *)
198 val naming_hd_dep : bool -> naming_scheme
199
200 (** Uses the next fresh names available, using the head of the declaration's type if it [Anonymous]. *)
201 val naming_hd_fresh : naming_scheme
202
203 (** [naming_hd_fresh] if [true], [naming_id] otherwise *)
204 val naming_hd_fresh_dep : bool -> naming_scheme
205
206 (** {6 Functions on Terms } *)
207
208 val ind_relevance : one_inductive_body -> einstance -> erelevance t
209
210 (* Decompose and Manipulate Terms *)
211 val whd_decompose_prod_decls : constr -> (rel_context * constr) t
212 val decompose_lambda_decls : constr -> (rel_context * constr) t
213 val decompose_app : constr -> (constr * constr array) t
214 val eta_expand_instantiation : constr array -> rel_context -> constr array t
215 val fresh_global : GlobRef.t -> constr t
216 val fresh_inductive_instance : inductive -> einstance t
217 val fresh_sort_ql : ?sort_rigid:bool -> ?name:variable -> rigid -> (Sorts.QVar.t * Univ.Universe.t) t
218 val new_univ_level_variable : ?name:variable -> rigid -> Univ.Level.t t
219
220 (* Typing and Retyping *)
221 val typing_checked_appvect : constr -> constr array -> constr t
222 val typing_check_actual_type : EConstr.unsafe_judgment -> types -> unit t
223 val retyping_sort_of : constr -> esorts t
224 val retyping_judgment_of : constr -> EConstr.unsafe_judgment t
225
226 (* debug *)
227 val print_term : CDebug.t -> string -> constr -> unit t
228 val print_current_context : CDebug.t -> string -> unit t
229
230 (** {6 Create a New Lambda, Product, or LetIn } *)
231
232 (** Status of the variable to bind *)
233 type freshness = Fresh | Old
234
235 (** Which binders to bind *)
236 type binder = Lambda | Prod
237
238 val fid : ('a -> 'b) -> 'a -> 'b
239 val fleft : ('a -> 'b) -> 'a * 'c -> 'b * 'c
240 val fright : ('a -> 'b) -> 'c * 'a -> 'c * 'b
241 val fopt : ('a -> 'b) -> 'a option -> 'b option
242 val fropt : ('a -> 'b) -> ('c * 'a) option -> ('c * 'b) option
243
244 (** Add a declaration to the state given its [freshness] and a [naming_scheme],
245 for a body defined in the updated state. *)
246 val add_decl : freshness -> naming_scheme -> rel_declaration ->
247 (access_key -> 'a t) -> 'a t
248
249 (** Bind a declaration with [binder] or [LetIn], given its [freshness] and a [naming_scheme],
250 for a body defined in the updated state. *)
251 val build_binder : ((constr -> constr) -> 'a -> 'b) -> binder -> freshness -> naming_scheme -> rel_declaration ->
252 (access_key -> 'a t) -> 'b t
253
254 (** Create a new [binder] using a [naming_scheme], for a body in the updated state.
255 When creating a fresh binder, the type of the variable needs to type check in the current context. *)
256 val make_binder : ((constr -> constr) -> 'a -> 'b) -> binder -> naming_scheme -> Name.t binder_annot -> types ->
257 (access_key -> 'a t) -> 'b t
258
259 (** Keep a former [binder] using a [naming_scheme], for a body defined in the updated state. *)
260 val keep_binder : ((constr -> constr) -> 'a -> 'b) -> binder -> naming_scheme -> Name.t binder_annot -> types ->
261 (access_key -> 'a t) -> 'b t
262
263 (** {6 Create Lambda, Product, or LetIn for Context} *)
264
265 (** Add a context to the state given its [freshness] and a [naming_scheme],
266 for a body defined in the updated state [state * access_key list -> constr]. *)
267 val add_context : freshness -> naming_scheme -> rel_context ->
268 (access_key list -> 'a t) -> 'a t
269
270 (** Add a context to the state given its [freshness] and a [naming_scheme],
271 for a body defined in the updated state, where the [access_key] are split
272 between [key_vars, key_letin, key_both]. *)
273 val add_context_sep : freshness -> naming_scheme -> rel_context ->
274 (access_key list * access_key list * access_key list -> 'a t) -> 'a t
275
276 (** Bind a context with [binder] or [LetIn], given its [freshness] and a [naming_scheme],
277 for a body defined in the updated state. *)
278 val closure_context : ((constr -> constr) -> 'a -> 'a) -> binder -> freshness ->
279 naming_scheme -> rel_context -> (access_key list -> 'a t) -> 'a t
280
281 (** Bind a context with [binder] or [LetIn], given its [freshness] and a [naming_scheme],
282 for a body defined in the updated state, where the [access_key] are split
283 between [key_vars, key_letin, key_both]. *)
284 val closure_context_sep : ((constr -> constr) -> 'a -> 'a) -> binder -> freshness -> naming_scheme ->
285 rel_context -> (access_key list * access_key list * access_key list -> 'a t) -> 'a t
286
287 (** Decompose a [constr] as [binder(s) local hd], then rebind it as
288 [binder(s) local, binder hd, cc] *)
289 val rebind : ((constr -> constr) -> 'a -> 'a) -> binder -> freshness -> naming_scheme -> constr ->
290 (access_key list * access_key -> 'a t) -> 'a t
291
292 (** [reads cxt binder cc_letin cc_var] go through a context [cxt],
293 apply [binder] to each context declaration [decl] to it,
294 then apply [cc_letin] if [decl] is a [LetIn], and [cc_var] otherwise. *)
295 val read_by_decl :
296 rel_context ->
297 (rel_declaration -> (access_key -> constr t) -> constr t) ->
298 (int -> access_key -> (access_key list -> constr t) -> constr t) ->
299 (int -> access_key -> (access_key list -> constr t) -> constr t) ->
300 (access_key list -> constr t) -> constr t
301
302 (** Given a mutual inductive body and an instance, it recovers the context of argument
303 and the return indices of the constructor out of [mind_nf_lc] *)
304 val get_args : mutual_inductive_body -> einstance -> Constr.rel_context * Constr.t -> (rel_context * constr array) t
305
306 (** Iterate a binding function to each constructor or an inductive type. *)
307 val iterate_ctors : mutual_inductive_body -> one_inductive_body -> einstance ->
308 (int -> rel_context * constr array -> ('a -> 'b t) -> 'b t) ->
309 ('a list -> 'b t) -> 'b t
310
311 (** {6 Functions on Inductives} *)
312
313 (** Create a term refering to an inductive type given the [access_key]
314 for uniform paramters, non-uniform parameters, and indices. *)
315 val make_ind : inductive * EInstance.t -> access_key list -> access_key list -> access_key list -> constr t
316
317 (** Create a term refering to the constructor of an inductive type given the [access_key]
318 for uniform paramters, non-uniform parameters, and indices. *)
319 val make_cst : inductive * EInstance.t -> int -> access_key list -> access_key list -> constr array -> constr t
320
321 (** Create a term refering to an inductive type given the [access_key]
322 for uniform paramters, non-uniform parameters, and indices. *)
323 val make_fix : 'a list -> int -> (int -> 'a -> int) ->
324 (int -> 'a -> Name.t binder_annot) -> (int -> 'a -> types t) ->
325 (access_key list * int * 'a -> constr t) -> constr t
326
327 (** Recover the indices of an inductive block, and weaken them in the current context. *)
328 val get_indices : one_inductive_body -> einstance -> rel_context t
329
330 (** Create a new match given:
331 - A [naming_scheme] for the fresh indices, and arguments
332 - The [mutual_inductive_body], [inductive], [one_inductive_body], [instance] on which the match is performed
333 - The access keys for its uniform and non-uniform parameters
334 - The parameters [constr array], and indices [constr array] of the term being matched
335 - The return type of the match [access_key list -> access_key -> constr t]
336 - The [relevance] of the match
337 - The term being matched [constr]
338 *)
339 val make_case_or_projections : naming_scheme -> mutual_inductive_body -> inductive -> one_inductive_body ->
340 einstance -> access_key list -> access_key list -> constr array -> constr array ->
341 (access_key list -> access_key -> types t) -> erelevance -> constr ->
342 (access_key list * access_key list * access_key list * int -> constr t) -> constr t