| 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 | |
| 14 | (** This module defines the internal representation of global |
| 15 | declarations. This includes global constants/axioms, mutual |
| 16 | inductive definitions, modules and module types *) |
| 17 | |
| 18 | (** {6 Representation of constants (Definition/Axiom) } *) |
| 19 | |
| 20 | (** Template polymorphism: provides universe polymorphism and sort |
| 21 | polymorphism (restricted to above prop qualities), with implicit instances |
| 22 | and inferring the universes from the types of parameters instead. |
| 23 | |
| 24 | The data other than [mind_template] in the inductive bodies is |
| 25 | instantiated to the default universes (e.g. [mind_user_arity], |
| 26 | [mind_sort], the constructor data, etc). |
| 27 | *) |
| 28 | |
| 29 | type template_universes = { |
| 30 | (** For each LocalAssum parameter (in regular order, not rel_context order), |
| 31 | tell whether it binds a quality or a universe level |
| 32 | (read from the sort, NB it is possible to bind a qvar next to a constant universe) |
| 33 | (the binders are DeBruijn levels as with universe polymorphism, |
| 34 | but may be instantiated with algebraics) *) |
| 35 | template_param_arguments : Sorts.t option list; |
| 36 | (** Inductive sort with abstracted universes. *) |
| 37 | template_concl : Sorts.t; |
| 38 | template_context : UVars.AbstractContext.t; |
| 39 | (** Template_defaults qualities are all QType. |
| 40 | Also the universes are all levels so we can use Instance. *) |
| 41 | template_defaults : UVars.Instance.t; |
| 42 | } |
| 43 | |
| 44 | (** Inlining level of parameters at functor applications. |
| 45 | None means no inlining *) |
| 46 | |
| 47 | type inline = int option |
| 48 | |
| 49 | (** A constant can have no body (axiom/parameter), or a |
| 50 | transparent body, or an opaque one *) |
| 51 | |
| 52 | (* Global declarations (i.e. constants) can be either: *) |
| 53 | type ('a, 'opaque, 'rules) constant_def = |
| 54 | | Undef of inline (** a global assumption *) |
| 55 | | Def of 'a (** or a transparent global definition *) |
| 56 | | OpaqueDef of 'opaque (** or an opaque global definition *) |
| 57 | | Primitive of CPrimitives.t (** or a primitive operation *) |
| 58 | | Symbol of 'rules (** or a symbol *) |
| 59 | |
| 60 | type universes = |
| 61 | | Monomorphic |
| 62 | | Polymorphic of UVars.AbstractContext.t |
| 63 | |
| 64 | (** The [typing_flags] are instructions to the type-checker which |
| 65 | modify its behaviour. The typing flags used in the type-checking |
| 66 | of a constant are tracked in their {!constant_body} so that they |
| 67 | can be displayed to the user. *) |
| 68 | type typing_flags = { |
| 69 | check_guarded : bool; |
| 70 | (** If [false] then fixed points and co-fixed points are assumed to |
| 71 | be total. *) |
| 72 | |
| 73 | check_positive : bool; |
| 74 | (** If [false] then inductive types are assumed positive and co-inductive |
| 75 | types are assumed productive. *) |
| 76 | |
| 77 | check_universes : bool; |
| 78 | (** If [false] universe constraints are not checked *) |
| 79 | |
| 80 | conv_oracle : Conv_oracle.oracle; |
| 81 | (** Unfolding strategies for conversion *) |
| 82 | |
| 83 | share_reduction : bool; |
| 84 | (** Use by-need reduction algorithm *) |
| 85 | |
| 86 | enable_VM : bool; |
| 87 | (** If [false], all VM conversions fall back to interpreted ones *) |
| 88 | |
| 89 | enable_native_compiler : bool; |
| 90 | (** If [false], all native conversions fall back to VM ones *) |
| 91 | |
| 92 | indices_matter: bool; |
| 93 | (** The universe of an inductive type must be above that of its indices. *) |
| 94 | |
| 95 | impredicative_set: bool; |
| 96 | (** Predicativity of the [Set] universe. *) |
| 97 | |
| 98 | sprop_allowed: bool; |
| 99 | (** If [false], error when encountering [SProp]. *) |
| 100 | |
| 101 | allow_uip: bool; |
| 102 | (** Allow definitional UIP (breaks termination) *) |
| 103 | |
| 104 | } |
| 105 | |
| 106 | (** {6 Representation of definitions/assumptions in the kernel } *) |
| 107 | |
| 108 | (* some contraints are in constant_constraints, some other may be in |
| 109 | * the OpaqueDef *) |
| 110 | type ('opaque, 'bytecode) pconstant_body = { |
| 111 | const_hyps : Constr.named_context; (** younger hyp at top *) |
| 112 | const_univ_hyps : UVars.Instance.t; |
| 113 | const_body : (Constr.t, 'opaque, bool) constant_def; |
| 114 | (** [bool] is for [unfold_fix] in symbols *) |
| 115 | const_type : types; |
| 116 | const_relevance : Sorts.relevance; |
| 117 | const_body_code : 'bytecode; |
| 118 | const_universes : universes; |
| 119 | const_inline_code : bool; |
| 120 | const_typing_flags : typing_flags; (** The typing options which |
| 121 | were used for |
| 122 | type-checking. *) |
| 123 | } |
| 124 | |
| 125 | type constant_body = (Opaqueproof.opaque, Vmlibrary.indirect_code option) pconstant_body |
| 126 | |
| 127 | (** {6 Representation of mutual inductive types in the kernel } *) |
| 128 | |
| 129 | type recarg_type = |
| 130 | | RecArgInd of inductive |
| 131 | | RecArgPrim of Constant.t |
| 132 | |
| 133 | type recarg = |
| 134 | | Norec |
| 135 | | Mrec of recarg_type |
| 136 | |
| 137 | type wf_paths = recarg Rtree.t |
| 138 | |
| 139 | (** |
| 140 | {v |
| 141 | Inductive I1 (params) : U1 := c11 : T11 | ... | c1p1 : T1p1 |
| 142 | ... |
| 143 | with In (params) : Un := cn1 : Tn1 | ... | cnpn : Tnpn |
| 144 | v} |
| 145 | *) |
| 146 | |
| 147 | (** Record information: |
| 148 | If the type is not a record, then NotRecord |
| 149 | If the type is a non-primitive record, then FakeRecord |
| 150 | If it is a primitive record, PrimRecord of: |
| 151 | - The identifier for the binder name of the record in primitive projections. |
| 152 | - The constants associated to each projection. |
| 153 | - The projection types (under parameters). |
| 154 | - The recheck at eta conversion flag |
| 155 | |
| 156 | The kernel does not exploit the difference between [NotRecord] and |
| 157 | [FakeRecord]. It is mostly used by extraction, and should be extruded from |
| 158 | the kernel at some point. |
| 159 | *) |
| 160 | type has_eta = AlwaysEta | NoEta |
| 161 | |
| 162 | type record_info = |
| 163 | | NotRecord |
| 164 | | FakeRecord |
| 165 | | PrimRecord of { |
| 166 | id : Id.t; |
| 167 | projections : Id.t array; |
| 168 | relevances : Sorts.relevance array; |
| 169 | tys : types array; |
| 170 | has_eta : has_eta; |
| 171 | } |
| 172 | |
| 173 | type squash_info = |
| 174 | | AlwaysSquashed |
| 175 | | SometimesSquashed of Sorts.Quality.Set.t |
| 176 | (** A sort polymorphic inductive [I@{...|...|...} : ... -> Type@{ s|...}] |
| 177 | is squashed at a given instantiation if any quality in the list is not smaller than [s]. |
| 178 | |
| 179 | NB: if [s] is a variable SometimesSquashed contains SProp |
| 180 | ie non ground instantiations are squashed. *) |
| 181 | |
| 182 | (** {7 Datas specific to a single type of a block of mutually inductive type } *) |
| 183 | type one_inductive_body = { |
| 184 | (** {8 Primitive datas } *) |
| 185 | |
| 186 | mind_typename : Id.t; (** Name of the type: [Ii] *) |
| 187 | |
| 188 | mind_arity_ctxt : Constr.rel_context; |
| 189 | (** Arity context of [Ii]. It includes the context of parameters, |
| 190 | that is, it has the form [paramdecls, realdecls_i] such that [Ui] |
| 191 | (see above) is [forall realdecls_i, si] for some sort [si] and |
| 192 | such that [Ii] has thus type [forall paramdecls, forall |
| 193 | realdecls_i, si]. The context itself is represented internally as |
| 194 | a list in reverse order |
| 195 | [[realdecl_i{r_i};...;realdecl_i1;paramdecl_m;...;paramdecl_1]]. *) |
| 196 | |
| 197 | mind_sort : Sorts.t; (** Arity sort *) |
| 198 | |
| 199 | mind_user_arity : types; |
| 200 | (** Original user arity, convertible to [mkArity (mind_arity_ctxt, mind_sort)]. |
| 201 | As such it contains the parameters. |
| 202 | (not necessarily a syntactic arity, eg [relation A] instead of [A -> A -> Prop]) *) |
| 203 | |
| 204 | mind_record : record_info; (** The record information *) |
| 205 | |
| 206 | mind_consnames : Id.t array; (** Names of the constructors: [cij] *) |
| 207 | |
| 208 | mind_user_lc : types array; |
| 209 | (** Types of the constructors with parameters: [forall params, Tij], |
| 210 | where the recursive occurrences of the inductive types in [Tij] |
| 211 | (i.e. in the type of the j-th constructor of the i-th types of |
| 212 | the block a shown above) have the form [Ind ((mind,0),u)], ..., |
| 213 | [Ind ((mind,n-1),u)] for [u] the canonical abstract instance |
| 214 | associated to [mind_universes] and [mind] the name to which the |
| 215 | inductive block is bound in the environment. *) |
| 216 | |
| 217 | (** {8 Derived datas } *) |
| 218 | |
| 219 | mind_nrealargs : int; (** Number of expected real arguments of the type (no let, no params) *) |
| 220 | |
| 221 | mind_nrealdecls : int; (** Length of realargs context (with let, no params) *) |
| 222 | |
| 223 | mind_squashed : squash_info option; |
| 224 | (** Is elimination restricted to the inductive's sort? *) |
| 225 | |
| 226 | mind_nf_lc : (rel_context * types) array; |
| 227 | (** Head normalized constructor types so that their conclusion |
| 228 | exposes the inductive type. It includes the parameters, i.e. each |
| 229 | component of the array has the form [(decls_ij, Ii params realargs_ij)] |
| 230 | where [decls_ij] is the concatenation of the context of parameters |
| 231 | (possibly with let-ins) and of the arguments of the constructor |
| 232 | (possibly with let-ins). This context is internally represented |
| 233 | as a list [[cstrdecl_ij{q_ij};...;cstrdecl_ij1;paramdecl_m;...;paramdecl_1]] |
| 234 | such that the constructor in fine has type [forall paramdecls, |
| 235 | forall cstrdecls_ij, Ii params realargs_ij] with [params] referring to |
| 236 | the assumptions of [paramdecls] and [realargs_ij] being the |
| 237 | "indices" specific to the constructor. *) |
| 238 | |
| 239 | mind_consnrealargs : int array; |
| 240 | (** Number of expected proper arguments of the constructors (w/o params) *) |
| 241 | |
| 242 | mind_consnrealdecls : int array; |
| 243 | (** Length of the signature of the constructors (with let, w/o params) *) |
| 244 | |
| 245 | mind_recargs : wf_paths; (** Signature of recursive arguments in the constructors *) |
| 246 | |
| 247 | mind_relevance : Sorts.relevance; |
| 248 | (* XXX this is redundant with mind_sort, is it actually worth keeping? *) |
| 249 | |
| 250 | (** {8 Datas for bytecode compilation } *) |
| 251 | |
| 252 | mind_nb_constant : int; (** number of constant constructor *) |
| 253 | |
| 254 | mind_nb_args : int; (** number of no constant constructor *) |
| 255 | |
| 256 | mind_reloc_tbl : Vmvalues.reloc_table; |
| 257 | } |
| 258 | |
| 259 | type recursivity_kind = |
| 260 | | Finite (** = inductive *) |
| 261 | | CoFinite (** = coinductive *) |
| 262 | | BiFinite (** = non-recursive, like in "Record" definitions *) |
| 263 | |
| 264 | (** {7 Datas associated to a full block of mutually inductive types } *) |
| 265 | |
| 266 | type mutual_inductive_body = { |
| 267 | |
| 268 | mind_packets : one_inductive_body array; (** The component of the mutual inductive block *) |
| 269 | |
| 270 | mind_finite : recursivity_kind; (** Whether the type is inductive, coinductive or non-recursive *) |
| 271 | |
| 272 | mind_hyps : Constr.named_context; (** Section hypotheses on which the block depends *) |
| 273 | |
| 274 | mind_univ_hyps : UVars.Instance.t; (** Section polymorphic universes. *) |
| 275 | |
| 276 | mind_nparams : int; (** Number of expected parameters including non-uniform ones (i.e. length of mind_params_ctxt w/o let-in) *) |
| 277 | |
| 278 | mind_nparams_rec : int; (** Number of recursively uniform (i.e. ordinary) parameters *) |
| 279 | |
| 280 | mind_params_ctxt : Constr.rel_context; (** The context of parameters (includes let-in declaration) *) |
| 281 | |
| 282 | mind_universes : universes; (** Information about monomorphic/polymorphic/cumulative inductives and their universes *) |
| 283 | |
| 284 | mind_template : template_universes option; |
| 285 | |
| 286 | mind_variance : UVars.Variance.t array option; (** Variance info, [None] when non-cumulative. *) |
| 287 | |
| 288 | mind_sec_variance : UVars.Variance.t array option; |
| 289 | (** Variance info for section polymorphic universes. [None] |
| 290 | outside sections. The final variance once all sections are |
| 291 | discharged is [mind_sec_variance ++ mind_variance]. *) |
| 292 | |
| 293 | mind_private : bool option; (** allow pattern-matching: Some true ok, Some false blocked *) |
| 294 | |
| 295 | mind_typing_flags : typing_flags; (** typing flags at the time of the inductive creation *) |
| 296 | } |
| 297 | |
| 298 | type mind_specif = mutual_inductive_body * one_inductive_body |
| 299 | |
| 300 | (** {6 Rewrite rules } *) |
| 301 | |
| 302 | type 'q quality_pattern = 'q Sorts.Quality.pattern = |
| 303 | | PQVar of 'q | PQConstant of Sorts.Quality.constant |
| 304 | |
| 305 | type ('q, 'u) instance_mask = ('q, 'u) UVars.Instance.mask |
| 306 | |
| 307 | type ('q, 'u) sort_pattern = ('q, 'u) Sorts.pattern = |
| 308 | | PSProp | PSSProp | PSSet | PSType of 'u | PSQSort of 'q * 'u |
| 309 | |
| 310 | (** Patterns are internally represented as pairs of a head-pattern and a list of eliminations |
| 311 | Eliminations correspond to elements of the stack in a reduction machine, |
| 312 | they represent a pattern with a hole, to be filled with the head-pattern |
| 313 | *) |
| 314 | type head_pattern = |
| 315 | | PHRel of int |
| 316 | | PHSort of (int option, int option) sort_pattern |
| 317 | | PHSymbol of Constant.t * (int option, int option) instance_mask |
| 318 | | PHInd of inductive * (int option, int option) instance_mask |
| 319 | | PHConstr of constructor * (int option, int option) instance_mask |
| 320 | | PHInt of Uint63.t |
| 321 | | PHFloat of Float64.t |
| 322 | | PHString of Pstring.t |
| 323 | | PHLambda of pattern_argument array * head_elimination |
| 324 | | PHProd of pattern_argument array * pattern_argument |
| 325 | |
| 326 | and pattern_elimination = |
| 327 | | PEApp of pattern_argument array |
| 328 | | PECase of inductive * pattern_argument * pattern_argument array |
| 329 | | PEProj of Projection.Repr.t |
| 330 | |
| 331 | and head_elimination = head_pattern * pattern_elimination list |
| 332 | |
| 333 | and pattern_argument = |
| 334 | | EHole of int |
| 335 | | EHoleIgnored |
| 336 | | ERigid of head_elimination |
| 337 | |
| 338 | type machine_rewrite_rule = { |
| 339 | nvars : int * int * int; |
| 340 | lhs_pat : (int option, int option) instance_mask * pattern_elimination list; |
| 341 | rhs : constr; |
| 342 | } |
| 343 | |
| 344 | (** {6 Representation of rewrite rules in the kernel } *) |
| 345 | |
| 346 | (** [(c, { lhs_pat = (u, elims); rhs })] in this list stands for [(PHSymbol (c,u), elims) ==> rhs] *) |
| 347 | type rewrite_rules_body = { |
| 348 | rewrules_rules : (Constant.t * machine_rewrite_rule) list; |
| 349 | } |
| 350 | |
| 351 | (** {6 Module declarations } *) |
| 352 | |
| 353 | type mod_body = [ `ModBody ] |
| 354 | type mod_type = [ `ModType ] |
| 355 | |
| 356 | (** Functor expressions are forced to be on top of other expressions *) |
| 357 | |
| 358 | type ('ty,'a) functorize = |
| 359 | | NoFunctor of 'a |
| 360 | | MoreFunctor of MBId.t * 'ty * ('ty,'a) functorize |
| 361 | |
| 362 | (** The fully-algebraic module expressions : names, applications, 'with ...'. |
| 363 | They correspond to the user entries of non-interactive modules. |
| 364 | They will be later expanded into module structures in [Mod_typing], |
| 365 | and won't play any role into the kernel after that : they are kept |
| 366 | only for short module printing and for extraction. *) |
| 367 | |
| 368 | type 'uconstr with_declaration = |
| 369 | | WithMod of Id.t list * ModPath.t |
| 370 | | WithDef of Id.t list * 'uconstr |
| 371 | |
| 372 | type 'uconstr module_alg_expr = |
| 373 | | MEident of ModPath.t |
| 374 | | MEapply of 'uconstr module_alg_expr * ModPath.t |
| 375 | | MEwith of 'uconstr module_alg_expr * 'uconstr with_declaration |
| 376 | |
| 377 | type 'uconstr functor_alg_expr = |
| 378 | | MENoFunctor of 'uconstr module_alg_expr |
| 379 | | MEMoreFunctor of 'uconstr functor_alg_expr |
| 380 | |
| 381 | (** A module expression is an algebraic expression, possibly functorized. *) |
| 382 | |
| 383 | type module_expression = (constr * UVars.AbstractContext.t option) functor_alg_expr |
| 384 | |
| 385 | (** A component of a module structure *) |
| 386 | |
| 387 | type ('mod_body, 'mod_type) structure_field_body = |
| 388 | | SFBconst of constant_body |
| 389 | | SFBmind of mutual_inductive_body |
| 390 | | SFBrules of rewrite_rules_body |
| 391 | | SFBmodule of 'mod_body |
| 392 | | SFBmodtype of 'mod_type |
| 393 | |
| 394 | (** A module structure is a list of labeled components. |
| 395 | |
| 396 | Note : we may encounter now (at most) twice the same label in |
| 397 | a [structure_body], once for a module ([SFBmodule] or [SFBmodtype]) |
| 398 | and once for an object ([SFBconst] or [SFBmind]) *) |
| 399 | |
| 400 | and ('mod_body, 'mod_type) structure_body = (Id.t * ('mod_body, 'mod_type) structure_field_body) list |