Debian Codegraph @

kernel/environ.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 open Names
12 open Constr
13 open UVars
14 open Declarations
15 open Mod_declarations
16 open PConstraints
17
18 (** Unsafe environments. We define here a datatype for environments.
19 Since typing is not yet defined, it is not possible to check the
20 informations added in environments, and that is why we speak here
21 of ``unsafe'' environments. *)
22
23 (** Environments have the following components:
24 - a context for de Bruijn variables
25 - a context for de Bruijn variables vm values
26 - a context for section variables and goal assumptions
27 - a context for section variables and goal assumptions vm values
28 - a context for global constants and axioms
29 - a context for inductive definitions
30 - a set of universe constraints
31 - a flag telling if Set is, can be, or cannot be set impredicative *)
32
33 (** Linking information for the native compiler *)
34 type link_info =
35 | Linked of string
36 | NotLinked
37
38 type key = int CEphemeron.key option ref
39
40 type constant_key = constant_body * (link_info ref * key) * KerName.t
41
42 type mind_key = mutual_inductive_body * link_info ref * KerName.t
43
44 type named_context_val = private {
45 env_named_ctx : Constr.named_context;
46 env_named_map : Constr.named_declaration Id.Map.t;
47 (** Identifier-indexed version of [env_named_ctx] *)
48 env_named_idx : Constr.named_declaration Range.t;
49 (** Same as env_named_ctx but with a fast-access list. *)
50 }
51
52 type rel_context_val = private {
53 env_rel_ctx : Constr.rel_context;
54 env_rel_map : Constr.rel_declaration Range.t;
55 }
56
57 type env
58 (** Type of global environments. *)
59
60 type rewrule_not_allowed = Symb | Rule
61 exception RewriteRulesNotAllowed of rewrule_not_allowed
62
63 val oracle : env -> Conv_oracle.oracle
64 val set_oracle : env -> Conv_oracle.oracle -> env
65
66 val eq_named_context_val : named_context_val -> named_context_val -> bool
67
68 val empty_env : env
69
70 val universes : env -> UGraph.t
71 val rel_context : env -> Constr.rel_context
72 val rel_context_val : env -> rel_context_val
73 val named_context : env -> Constr.named_context
74 val named_context_val : env -> named_context_val
75
76 val set_universes : UGraph.t -> env -> env
77 val set_qualities : QGraph.t -> env -> env
78
79 val qualities : env -> QGraph.t
80 val qvars : env -> Sorts.QVar.Set.t
81
82 val typing_flags : env -> typing_flags
83 val is_impredicative_set : env -> bool
84 val type_in_type : env -> bool
85 val deactivated_guard : env -> bool
86 val indices_matter : env -> bool
87
88 val is_impredicative_sort : env -> Sorts.t -> bool
89
90 (** is the local context empty *)
91 val empty_context : env -> bool
92
93 (** {5 Context of de Bruijn variables ([rel_context]) } *)
94
95 val nb_rel : env -> int
96 val push_rel : Constr.rel_declaration -> env -> env
97 val push_rel_context : Constr.rel_context -> env -> env
98 val push_rec_types : rec_declaration -> env -> env
99
100 val push_rel_context_val : Constr.rel_declaration -> rel_context_val -> rel_context_val
101 val set_rel_context_val : rel_context_val -> env -> env
102 val empty_rel_context_val : rel_context_val
103
104 (** Looks up in the context of local vars referred by indice ([rel_context])
105 raises [Not_found] if the index points out of the context *)
106 val lookup_rel : int -> env -> Constr.rel_declaration
107 val evaluable_rel : int -> env -> bool
108 val env_of_rel : int -> env -> env
109
110 (** {6 Recurrence on [rel_context] } *)
111
112 val fold_rel_context :
113 (env -> Constr.rel_declaration -> 'a -> 'a) -> env -> init:'a -> 'a
114
115 (** {5 Context of variables (section variables and goal assumptions) } *)
116
117 val named_context_of_val : named_context_val -> Constr.named_context
118 val val_of_named_context : Constr.named_context -> named_context_val
119 val empty_named_context_val : named_context_val
120 val ids_of_named_context_val : named_context_val -> Id.Set.t
121
122
123 (** [map_named_val f ctxt] apply [f] to the body and the type of
124 each declarations.
125 *** /!\ *** [f t] should be convertible with t, and preserve the name *)
126 val map_named_val :
127 (named_declaration -> named_declaration) -> named_context_val -> named_context_val
128
129 val push_named : Constr.named_declaration -> env -> env
130 val push_named_context : Constr.named_context -> env -> env
131 val push_named_context_val :
132 Constr.named_declaration -> named_context_val -> named_context_val
133
134
135 val mem_named : variable -> env -> bool
136
137 (** Looks up in the context of local vars referred by names ([named_context])
138 raises [Not_found] if the Id.t is not found *)
139
140 val lookup_named : variable -> env -> Constr.named_declaration
141 val lookup_named_ctxt : variable -> named_context_val -> Constr.named_declaration
142 val evaluable_named : variable -> env -> bool
143 val named_type : variable -> env -> types
144 val named_body : variable -> env -> constr option
145
146 (** {6 Recurrence on [named_context]: older declarations processed first } *)
147
148 val fold_named_context :
149 (env -> Constr.named_declaration -> 'a -> 'a) -> env -> init:'a -> 'a
150
151 val match_named_context_val : named_context_val -> (named_declaration * named_context_val) option
152
153 (** Recurrence on [named_context] starting from younger decl *)
154 val fold_named_context_reverse :
155 ('a -> Constr.named_declaration -> 'a) -> init:'a -> env -> 'a
156
157 (** This forgets named and rel contexts *)
158 val reset_context : env -> env
159
160 (** This forgets rel context and sets a new named context *)
161 val reset_with_named_context : named_context_val -> env -> env
162
163 (** This removes the [n] last declarations from the rel context *)
164 val pop_rel_context : int -> env -> env
165
166 (** Useful for printing *)
167 val fold_constants : (Constant.t -> constant_body -> 'a -> 'a) -> env -> 'a -> 'a
168 val fold_inductives : (MutInd.t -> Declarations.mutual_inductive_body -> 'a -> 'a) -> env -> 'a -> 'a
169
170 (** {5 Global constants }
171 {6 Add entries to global environment } *)
172
173 val add_constant : Constant.t -> constant_body -> env -> env
174 val add_constant_key : Constant.t -> constant_body -> link_info ->
175 env -> env
176 val lookup_constant_key : Constant.t -> env -> constant_key
177
178 (** Looks up in the context of global constant names
179 raises an anomaly if the required path is not found *)
180 val lookup_constant : Constant.t -> env -> constant_body
181 val lookup_constant_opt : Constant.t -> env -> constant_body option
182 val evaluable_constant : Constant.t -> env -> bool
183 val constant_relevance : Constant.t -> env -> Sorts.relevance
184
185 val mem_constant : Constant.t -> env -> bool
186
187 val add_rewrite_rules : (Constant.t * machine_rewrite_rule) list -> env -> env
188 val lookup_rewrite_rules : Constant.t -> env -> machine_rewrite_rule list
189
190 (** New-style polymorphism *)
191 val polymorphic_constant : Constant.t -> env -> bool
192 val polymorphic_pconstant : pconstant -> env -> bool
193 val type_in_type_constant : Constant.t -> env -> bool
194
195 (** {6 ... } *)
196 (** [constant_value env c] raises [NotEvaluableConst Opaque] if
197 [c] is opaque, [NotEvaluableConst NoBody] if it has no
198 body, [NotEvaluableConst IsProj] if [c] is a projection,
199 [NotEvaluableConst (IsPrimitive p)] if [c] is primitive [p]
200 and an anomaly if it does not exist in [env] *)
201
202 type const_evaluation_result =
203 | NoBody
204 | Opaque
205 | IsPrimitive of Instance.t * CPrimitives.t
206 | HasRules of Instance.t * bool * machine_rewrite_rule list
207 exception NotEvaluableConst of const_evaluation_result
208
209 val constant_type : env -> Constant.t puniverses -> types constrained
210
211 val constant_value_and_type : env -> Constant.t puniverses ->
212 constr option * types * PConstraints.t
213 (** The universe context associated to the constant, empty if not
214 polymorphic *)
215 val constant_context : env -> Constant.t -> AbstractContext.t
216
217 (* These functions should be called under the invariant that [env]
218 already contains the constraints corresponding to the constant
219 application. *)
220 val constant_value_in : env -> Constant.t puniverses -> constr
221 val constant_type_in : env -> Constant.t puniverses -> types
222 val constant_opt_value_in : env -> Constant.t puniverses -> constr option
223
224 val is_symbol : env -> Constant.t -> bool
225 val is_primitive : env -> Constant.t -> bool
226 val get_primitive : env -> Constant.t -> CPrimitives.t option
227
228 val is_array_type : env -> Constant.t -> bool
229 val is_int63_type : env -> Constant.t -> bool
230 val is_float64_type : env -> Constant.t -> bool
231 val is_string_type : env -> Constant.t -> bool
232 val is_primitive_type : env -> Constant.t -> bool
233
234
235 (** {6 Primitive projections} *)
236
237 (** Checks that the number of parameters is correct. *)
238 val lookup_projection : Names.Projection.t -> env -> Sorts.relevance * types
239
240 (** Anomaly when not a primitive record or invalid proj_arg. *)
241 val get_projection : env -> inductive -> proj_arg:int -> Names.Projection.Repr.t * Sorts.relevance
242
243 val get_projections : env -> inductive -> (Names.Projection.Repr.t * Sorts.relevance) array option
244
245 (** {5 Inductive types } *)
246 val lookup_mind_key : MutInd.t -> env -> mind_key
247 val add_mind_key : MutInd.t -> mutual_inductive_body -> link_info -> env -> env
248 val add_mind : MutInd.t -> mutual_inductive_body -> env -> env
249
250 (** Looks up in the context of global inductive names
251 raises an anomaly if the required path is not found *)
252 val lookup_mind : MutInd.t -> env -> mutual_inductive_body
253
254 val mem_mind : MutInd.t -> env -> bool
255
256 val ind_relevance : inductive -> env -> Sorts.relevance
257
258 (** The universe context associated to the inductive, empty if not
259 polymorphic *)
260 val mind_context : env -> MutInd.t -> AbstractContext.t
261
262 (** New-style polymorphism *)
263 val polymorphic_ind : inductive -> env -> bool
264 val polymorphic_pind : pinductive -> env -> bool
265 val type_in_type_ind : inductive -> env -> bool
266
267 (** Old-style polymorphism *)
268 val template_polymorphic_ind : inductive -> env -> bool
269 val template_polymorphic_pind : pinductive -> env -> bool
270
271 (** {6 Changes of representation of Case nodes} *)
272
273 (** Given an inductive type and its parameters, builds the context of the return
274 clause, including the inductive being eliminated. The additional binder
275 array is only used to set the names of the context variables, we use the
276 less general type to make it easy to use this function on Case nodes. *)
277 val expand_arity : Declarations.mind_specif -> pinductive -> constr array ->
278 Name.t binder_annot array -> rel_context
279
280 (** Given an inductive type and its parameters, builds the context of the return
281 clause, including the inductive being eliminated. The additional binder
282 array is only used to set the names of the context variables, we use the
283 less general type to make it easy to use this function on Case nodes. *)
284 val expand_branch_contexts : Declarations.mind_specif -> UVars.Instance.t -> constr array ->
285 (Name.t binder_annot array * 'a) array -> rel_context array
286
287 (** [instantiate_context u subst nas ctx] applies both [u] and [subst]
288 to [ctx] while replacing names using [nas] (order reversed). In particular,
289 assumes that [ctx] and [nas] have the same length. *)
290 val instantiate_context : UVars.Instance.t -> Vars.substl -> Name.t binder_annot array ->
291 rel_context -> rel_context
292
293
294 (** {6 Name quotients} *)
295
296 module type QS =
297 sig
298 type t
299 val canonize : env -> t -> t
300 end
301
302 module type QMapS =
303 sig
304 type key
305 type (+'a) t
306 val empty: 'a t
307 val is_empty: 'a t -> bool
308 val mem: env -> key -> 'a t -> bool
309 val add: env -> key -> 'a -> 'a t -> 'a t
310 val remove: env -> key -> 'a t -> 'a t
311 val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
312 val merge: (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
313 val find: env -> key -> 'a t -> 'a
314 val find_opt : env -> key -> 'a t -> 'a option
315 end
316
317 module QMap (M : CSig.UMapS) (_ : QS with type t = M.key) : QMapS with type key = M.key
318
319 module type QNameS =
320 sig
321 type t
322 val equal : env -> t -> t -> bool
323 val compare : env -> t -> t -> int
324 val hash : env -> t -> int
325 val canonize : env -> t -> t
326 end
327
328 module QConstant : sig
329 include QNameS with type t = Constant.t
330 module Map : QMapS with type key = t
331 end
332
333 module QMutInd : sig
334 include QNameS with type t = MutInd.t
335 module Map : QMapS with type key = t
336 end
337
338 module QInd : sig
339 include QNameS with type t = Ind.t
340 module Map : QMapS with type key = t
341 end
342
343 module QConstruct : sig
344 include QNameS with type t = Construct.t
345 module Map : QMapS with type key = t
346 end
347
348 module QProjection :
349 sig
350 include QNameS with type t = Projection.t
351 module Map : QMapS with type key = t
352
353 module Repr : sig
354 include QNameS with type t = Projection.Repr.t
355 module Map : QMapS with type key = t
356 end
357 end
358
359 module QGlobRef : sig
360 include QNameS with type t = GlobRef.t
361 module Map : QMapS with type key = t
362 end
363
364 (** {5 Modules } *)
365
366 val add_modtype : ModPath.t -> module_type_body -> env -> env
367
368 (** [shallow_add_module] does not add module components *)
369 val shallow_add_module : ModPath.t -> module_body -> env -> env
370
371 val lookup_module : ModPath.t -> env -> module_body
372 val lookup_modtype : ModPath.t -> env -> module_type_body
373
374 (** {5 Universe constraints } *)
375
376 val check_univ_constraints : Univ.UnivConstraints.t -> env -> bool
377 (** Check universe constraints are satifiable in the environment. *)
378
379 val check_constraints : PConstraints.t -> env -> bool
380 (** Check constraints are satifiable in the environment. *)
381
382 val check_ucontext : UContext.t -> env -> unit
383 (** Check that the sort context of the argument satisfies the well-formedness
384 conditions ensuring the existence of an instance. *)
385
386 val push_context : ?strict:bool -> UContext.t -> env -> env
387 (** [push_context ?(strict=false) ctx env] pushes the universe context to the environment.
388 @raise UGraph.AlreadyDeclared if one of the universes is already declared. *)
389
390 val push_context_set : ?strict:bool -> Univ.ContextSet.t -> env -> env
391 (** [push_context_set ?(strict=false) src ctx env] pushes the universe
392 context set to the environment. It does not fail even if one of the
393 universes is already declared. *)
394
395 val push_qualities : rigid:bool -> Sorts.QContextSet.t -> env -> env
396 (** [push_qualities qs env] pushes the set of quality variables and constraints
397 in the environment. It fails if a quality variable is already
398 declared. *)
399
400 val push_subgraph : Univ.ContextSet.t -> env -> env
401 (** [push_subgraph univs env] adds the universes and constraints in
402 [univs] to [env] as [push_context_set ~strict:false univs env], and
403 also checks that they do not imply new transitive constraints
404 between pre-existing universes in [env]. *)
405
406 val set_typing_flags : typing_flags -> env -> env
407 val set_impredicative_set : bool -> env -> env
408 val set_type_in_type : bool -> env -> env
409 val set_allow_sprop : bool -> env -> env
410 val sprop_allowed : env -> bool
411 val allow_rewrite_rules : env -> env
412 val rewrite_rules_allowed : env -> bool
413
414 val same_flags : typing_flags -> typing_flags -> bool
415
416 (** [update_typing_flags ?typing_flags] may update env with optional typing flags *)
417 val update_typing_flags : ?typing_flags:typing_flags -> env -> env
418
419 val universes_of_global : env -> GlobRef.t -> AbstractContext.t
420
421 (** {6 Sets of referred section variables }
422 [global_vars_set env c] returns the list of [id]'s occurring either
423 directly as [Var id] in [c] or indirectly as a section variable
424 dependent in a global reference occurring in [c] *)
425
426 val global_vars_set : env -> constr -> Id.Set.t
427
428 val vars_of_global : env -> GlobRef.t -> Id.Set.t
429
430 (** closure of the input id set w.r.t. dependency *)
431 val really_needed : env -> Id.Set.t -> Id.Set.t
432
433 (** like [really_needed] but computes a well ordered named context *)
434 val keep_hyps : env -> Id.Set.t -> Constr.named_context
435
436 (** {5 Unsafe judgments. }
437 We introduce here the pre-type of judgments, which is
438 actually only a datatype to store a term with its type and the type of its
439 type. *)
440
441 type ('constr, 'types) punsafe_judgment = {
442 uj_val : 'constr;
443 uj_type : 'types }
444
445 val on_judgment : ('a -> 'b) -> ('a, 'a) punsafe_judgment -> ('b, 'b) punsafe_judgment
446 val on_judgment_value : ('c -> 'c) -> ('c, 't) punsafe_judgment -> ('c, 't) punsafe_judgment
447 val on_judgment_type : ('t -> 't) -> ('c, 't) punsafe_judgment -> ('c, 't) punsafe_judgment
448
449 type unsafe_judgment = (constr, types) punsafe_judgment
450
451 val make_judge : 'constr -> 'types -> ('constr, 'types) punsafe_judgment
452 val j_val : ('constr, 'types) punsafe_judgment -> 'constr
453 val j_type : ('constr, 'types) punsafe_judgment -> 'types
454
455 type ('types, 'sorts) punsafe_type_judgment = {
456 utj_val : 'types;
457 utj_type : 'sorts }
458
459 type unsafe_type_judgment = (types, Sorts.t) punsafe_type_judgment
460
461 exception Hyp_not_found
462
463 (** [apply_to_hyp sign id f] split [sign] into [tail::(id,_,_)::head] and
464 return [tail::(f head (id,_,_) (rev tail))::head].
465 the value associated to id should not change *)
466 val apply_to_hyp : named_context_val -> variable ->
467 (Constr.named_context -> Constr.named_declaration -> Constr.named_context -> Constr.named_declaration) ->
468 named_context_val
469
470 val remove_hyps : Id.Set.t -> (Constr.named_declaration -> Constr.named_declaration) -> named_context_val -> named_context_val
471
472 val is_polymorphic : env -> Names.GlobRef.t -> bool
473 val is_template_polymorphic : env -> GlobRef.t -> bool
474 val is_type_in_type : env -> GlobRef.t -> bool
475
476 (** {5 VM and native} *)
477
478 val vm_library : env -> Vmlibrary.t
479 val set_vm_library : Vmlibrary.t -> env -> env
480 val link_vm_library : Vmlibrary.on_disk -> env -> env
481 val lookup_vm_code : Vmlibrary.index -> env -> Vmemitcodes.to_patch
482
483 (** Native compiler *)
484 val no_link_info : link_info
485
486 (** Primitives *)
487 val set_retroknowledge : env -> Retroknowledge.retroknowledge -> env
488 val retroknowledge : env -> Retroknowledge.retroknowledge
489
490 module Internal : sig
491 (** Makes the qvars treated as above prop.
492 Do not use outside kernel inductive typechecking. *)
493 val push_template_context : UContext.t -> env -> env
494
495 val is_above_prop : env -> Sorts.QVar.t -> bool
496
497 module View :
498 sig
499 type t = {
500 env_constants : constant_key Cmap_env.t;
501 env_inductives : mind_key Mindmap_env.t;
502 env_modules : module_body ModPath.Map.t;
503 env_modtypes : module_type_body ModPath.Map.t;
504 env_named_context : named_context_val;
505 env_rel_context : rel_context_val;
506 env_universes : UGraph.t;
507 env_qualities : Sorts.QVar.Set.t;
508 env_symb_pats : machine_rewrite_rule list Cmap_env.t;
509 env_typing_flags : typing_flags;
510 }
511
512 val view : env -> t
513 end
514 (** View type only used by Serlib. Do not use otherwise. *)
515
516 val shallow_overwrite_module : ModPath.t -> module_body -> env -> env
517 (** Same as [shallow_add_module] but tolerates that the module is already
518 in the environment. Only used to bypass a dubious implementation in
519 extraction, do not use. *)
520
521 val overwrite_module : ModPath.t -> module_body -> env -> env
522 (** Overwriting variant of Modops.add_module, see above. *)
523
524 val overwrite_module_parameter : MBId.t -> module_type_body -> env -> env
525 (** Overwriting variant of Modops.add_module_parameter, see above. *)
526
527 val overwrite_structure : ModPath.t -> structure_body -> Mod_subst.delta_resolver -> env -> env
528 (** Overwriting variant of Modops.add_structure, see above. *)
529
530 end