| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Names |
| 13 |
open Univ |
| 14 |
open UVars |
| 15 |
open Term |
| 16 |
open Constr |
| 17 |
open Declarations |
| 18 |
open Environ |
| 19 |
open Entries |
| 20 |
open Type_errors |
| 21 |
open Context.Rel.Declaration |
| 22 |
|
| 23 |
type inductive_arity = { user_arity : Constr.types; sort : Sorts.t } |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
let check_constructors_names env idset ids = |
| 34 |
let rec check idset = function |
| 35 |
| [] -> idset |
| 36 |
| c::cl -> |
| 37 |
if Id.Set.mem c idset then |
| 38 |
raise (InductiveError (env, SameNamesConstructors c)) |
| 39 |
else |
| 40 |
check (Id.Set.add c idset) cl |
| 41 |
in |
| 42 |
check idset ids |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
let mind_check_names env mie = |
| 49 |
let rec check indset cstset = function |
| 50 |
| [] -> () |
| 51 |
| ind::inds -> |
| 52 |
let id = ind.mind_entry_typename in |
| 53 |
let cl = ind.mind_entry_consnames in |
| 54 |
if Id.Set.mem id indset then |
| 55 |
raise (InductiveError (env, SameNamesTypes id)) |
| 56 |
else |
| 57 |
let cstset' = check_constructors_names env cstset cl in |
| 58 |
check (Id.Set.add id indset) cstset' inds |
| 59 |
in |
| 60 |
check Id.Set.empty Id.Set.empty mie.mind_entry_inds |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
type record_arg_info = |
| 71 |
| NoRelevantArg |
| 72 |
| HasRelevantArg |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
type univ_info = |
| 77 |
{ ind_squashed : squash_info option |
| 78 |
; record_arg_info : record_arg_info |
| 79 |
; ind_template : bool |
| 80 |
; ind_univ : Sorts.t |
| 81 |
; missing : Sorts.t list |
| 82 |
} |
| 83 |
|
| 84 |
let add_squash q info = |
| 85 |
match info.ind_squashed with |
| 86 |
| None -> { info with ind_squashed = Some (SometimesSquashed (Sorts.Quality.Set.singleton q)) } |
| 87 |
| Some AlwaysSquashed -> info |
| 88 |
| Some (SometimesSquashed qs) -> |
| 89 |
|
| 90 |
{ info with ind_squashed = Some (SometimesSquashed (Sorts.Quality.Set.add q qs)) } |
| 91 |
|
| 92 |
let compute_elim_squash ?(is_real_arg=false) env u info = |
| 93 |
let open Sorts.Quality in |
| 94 |
let info = if not is_real_arg then info |
| 95 |
else match info.record_arg_info with |
| 96 |
| HasRelevantArg -> info |
| 97 |
| NoRelevantArg -> match u with |
| 98 |
| Sorts.SProp -> info |
| 99 |
| QSort (q,_) -> |
| 100 |
if Environ.Internal.is_above_prop env q |
| 101 |
|| equal (QVar q) (Sorts.quality info.ind_univ) |
| 102 |
then { info with record_arg_info = HasRelevantArg } |
| 103 |
else info |
| 104 |
| Prop | Set | Type _ -> { info with record_arg_info = HasRelevantArg } |
| 105 |
in |
| 106 |
if (Environ.type_in_type env) then info |
| 107 |
else |
| 108 |
let indu = info.ind_univ |
| 109 |
and check_univ_consistency f induu uu = |
| 110 |
if UGraph.check_leq (universes env) uu induu |
| 111 |
then f info |
| 112 |
else { info with missing = u :: info.missing } in |
| 113 |
if Inductive.eliminates_to (Environ.qualities env) (Sorts.quality indu) (Sorts.quality u) then |
| 114 |
if Sorts.Quality.is_impredicative (Sorts.quality indu) |
| 115 |
then |
| 116 |
match u with |
| 117 |
| Type _ | Set -> { info with ind_squashed = Some AlwaysSquashed } |
| 118 |
| QSort (q, _) -> add_squash (Sorts.Quality.QVar q) info |
| 119 |
| SProp | Prop -> info |
| 120 |
else check_univ_consistency (fun x -> x) |
| 121 |
(Sorts.univ_of_sort indu) |
| 122 |
(Sorts.univ_of_sort u) |
| 123 |
else |
| 124 |
let check_univ_consistency_squash quality = |
| 125 |
check_univ_consistency (add_squash quality) in |
| 126 |
match indu, u with |
| 127 |
| QSort (_, indu), Type uu -> |
| 128 |
check_univ_consistency_squash qtype indu uu |
| 129 |
| QSort (_, indu), QSort (cq, uu) -> |
| 130 |
check_univ_consistency_squash (QVar cq) indu uu |
| 131 |
| QSort (q, indu), Set -> |
| 132 |
if Environ.Internal.is_above_prop env q then info |
| 133 |
else check_univ_consistency_squash qtype indu Universe.type0 |
| 134 |
| (SProp | Prop), QSort (q, _) -> |
| 135 |
add_squash (QVar q) info |
| 136 |
| QSort (q, _), (SProp | Prop) -> |
| 137 |
if Environ.Internal.is_above_prop env q then info |
| 138 |
else add_squash (Sorts.quality u) info |
| 139 |
| _, _ -> { info with ind_squashed = Some AlwaysSquashed } |
| 140 |
|
| 141 |
let check_context_univs ~ctor env info ctx = |
| 142 |
let check_one d (info,env) = |
| 143 |
let info = match d with |
| 144 |
| LocalAssum (_,t) -> |
| 145 |
|
| 146 |
let tj = Typeops.infer_type env t in |
| 147 |
compute_elim_squash ~is_real_arg:ctor env tj.utj_type info |
| 148 |
| LocalDef _ -> info |
| 149 |
in |
| 150 |
info, push_rel d env |
| 151 |
in |
| 152 |
fst (Context.Rel.fold_outside ~init:(info,env) check_one ctx) |
| 153 |
|
| 154 |
let check_indices_matter env_params info indices = |
| 155 |
if not (indices_matter env_params) then info |
| 156 |
else check_context_univs ~ctor:false env_params info indices |
| 157 |
|
| 158 |
|
| 159 |
let check_arity ~template env_params env_ar ind = |
| 160 |
let {utj_val=arity;utj_type=_} = Typeops.infer_type env_params ind.mind_entry_arity in |
| 161 |
let indices, ind_sort = Reduction.dest_arity env_params arity in |
| 162 |
let univ_info = { |
| 163 |
ind_squashed=None; |
| 164 |
record_arg_info=NoRelevantArg; |
| 165 |
ind_template = template; |
| 166 |
ind_univ=ind_sort; |
| 167 |
missing=[]; |
| 168 |
} |
| 169 |
in |
| 170 |
let univ_info = check_indices_matter env_params univ_info indices in |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let arity = it_mkProd_or_LetIn arity (Environ.rel_context env_params) in |
| 176 |
let x = Context.make_annot (Name ind.mind_entry_typename) (Sorts.relevance_of_sort ind_sort) in |
| 177 |
push_rel (LocalAssum (x, arity)) env_ar, |
| 178 |
(arity, indices, univ_info) |
| 179 |
|
| 180 |
let check_constructor_univs env_ar_par info (args,_) = |
| 181 |
|
| 182 |
check_context_univs ~ctor:true env_ar_par info args |
| 183 |
|
| 184 |
let check_constructors env_ar_par isrecord params lc (arity,indices,univ_info) = |
| 185 |
let lc = Array.map_of_list (fun c -> (Typeops.infer_type env_ar_par c).utj_val) lc in |
| 186 |
let splayed_lc = Array.map (Reduction.whd_decompose_prod_decls env_ar_par) lc in |
| 187 |
let univ_info = |
| 188 |
|
| 189 |
if isrecord then univ_info |
| 190 |
else match Array.length lc with |
| 191 |
|
| 192 |
| 0 -> compute_elim_squash env_ar_par Sorts.sprop univ_info |
| 193 |
|
| 194 |
| 1 -> |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if (Environ.typing_flags env_ar_par).allow_uip |
| 199 |
&& fst (splayed_lc.(0)) = [] |
| 200 |
&& List.for_all Context.Rel.Declaration.is_local_assum params |
| 201 |
&& Sorts.is_sprop univ_info.ind_univ |
| 202 |
then univ_info |
| 203 |
|
| 204 |
|
| 205 |
else compute_elim_squash env_ar_par Sorts.prop univ_info |
| 206 |
|
| 207 |
|
| 208 |
| _ -> compute_elim_squash env_ar_par Sorts.set univ_info |
| 209 |
in |
| 210 |
let univ_info = Array.fold_left (check_constructor_univs env_ar_par) univ_info splayed_lc in |
| 211 |
let () = if univ_info.ind_template then match univ_info.ind_squashed with |
| 212 |
| None | Some AlwaysSquashed -> () |
| 213 |
| Some (SometimesSquashed _) -> |
| 214 |
CErrors.user_err Pp.(str "Cannot handle sometimes squashed template polymorphic type.") |
| 215 |
in |
| 216 |
|
| 217 |
let lc = Array.map (fun c -> Term.it_mkProd_or_LetIn c params) lc in |
| 218 |
(arity, lc), (indices, splayed_lc), univ_info |
| 219 |
|
| 220 |
module NotPrimRecordReason = struct |
| 221 |
|
| 222 |
type t = |
| 223 |
| MustNotBeSquashed |
| 224 |
| MustHaveRelevantProj |
| 225 |
| MustHaveProj |
| 226 |
| MustNotHaveAnonProj |
| 227 |
|
| 228 |
end |
| 229 |
|
| 230 |
|
| 231 |
let check_record data = |
| 232 |
let open NotPrimRecordReason in |
| 233 |
List.fold_left (fun res (_, (_, splayed_lc), info) -> |
| 234 |
if Result.is_error res then res |
| 235 |
else if Option.has_some info.ind_squashed |
| 236 |
|
| 237 |
then Result.Error MustNotBeSquashed |
| 238 |
else |
| 239 |
let res = match splayed_lc with |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
| [|ctx,_|] -> |
| 246 |
let module D = Context.Rel.Declaration in |
| 247 |
if not @@ List.exists D.is_local_assum ctx |
| 248 |
then Some MustHaveProj |
| 249 |
else if List.exists (fun d -> D.is_local_assum d && Name.is_anonymous (D.get_name d)) ctx |
| 250 |
then Some MustNotHaveAnonProj |
| 251 |
else None |
| 252 |
| _ -> CErrors.anomaly ~label:"Indtyping.check_record" Pp.(str "not 1 constructor") |
| 253 |
in |
| 254 |
match res with |
| 255 |
| Some reason -> Result.Error reason |
| 256 |
| None -> |
| 257 |
match info.record_arg_info with |
| 258 |
| HasRelevantArg -> Result.Ok AlwaysEta |
| 259 |
| NoRelevantArg -> |
| 260 |
|
| 261 |
match info.ind_univ with |
| 262 |
| SProp -> Result.Ok AlwaysEta |
| 263 |
| Set | Type _ | Prop -> Result.Ok NoEta |
| 264 |
| QSort _ -> Result.Ok NoEta |
| 265 |
) |
| 266 |
(Result.Ok NoEta) |
| 267 |
data |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
let check_unbounded_from_below (univs, csts) = |
| 276 |
Univ.UnivConstraints.iter (fun (l,d,r) -> |
| 277 |
let bad = match d with |
| 278 |
| UnivConstraint.Eq | UnivConstraint.Lt -> |
| 279 |
if Level.Set.mem l univs then Some l |
| 280 |
else if Level.Set.mem r univs then Some r |
| 281 |
else None |
| 282 |
| UnivConstraint.Le -> if Level.Set.mem r univs then Some r else None |
| 283 |
in |
| 284 |
bad |> Option.iter (fun bad -> |
| 285 |
CErrors.user_err Pp.(str "Universe level " ++ Level.raw_pr bad ++ |
| 286 |
str " cannot be template because it appears in constraint " ++ |
| 287 |
Level.raw_pr l ++ UnivConstraint.pr_kind d ++ Level.raw_pr r))) |
| 288 |
csts |
| 289 |
|
| 290 |
let check_not_appearing_univs ~template_univs univs = |
| 291 |
let univs = Level.Set.inter template_univs univs in |
| 292 |
if Level.Set.is_empty univs then () |
| 293 |
else |
| 294 |
CErrors.user_err |
| 295 |
Pp.(str "Template " ++ |
| 296 |
str (CString.plural (Level.Set.cardinal univs) "universe") ++ |
| 297 |
spc() ++ Level.Set.pr Level.raw_pr univs ++ spc() ++ |
| 298 |
str "appear in illegal positions.") |
| 299 |
|
| 300 |
let get_template_binding_arity ~template_univs c = |
| 301 |
let decls, c = Term.decompose_prod_decls c in |
| 302 |
let check_level u = match Universe.level u with |
| 303 |
| None -> |
| 304 |
let () = check_not_appearing_univs ~template_univs (Universe.levels u) in |
| 305 |
None |
| 306 |
| Some l -> if Level.Set.mem l template_univs then Some l else None |
| 307 |
in |
| 308 |
match kind c with |
| 309 |
| Sort (Type u as s) -> |
| 310 |
Some (decls, None, check_level u, s) |
| 311 |
| Sort (QSort (q, u) as s) -> |
| 312 |
|
| 313 |
Some (decls, Some q, check_level u, s) |
| 314 |
| _ -> None |
| 315 |
|
| 316 |
let check_no_increment ~template_univs u = |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
let has_increment = |
| 321 |
Universe.exists (fun (u,n) -> |
| 322 |
if Level.Set.mem u template_univs then |
| 323 |
not (Int.equal n 0) |
| 324 |
else false) u |
| 325 |
in |
| 326 |
if has_increment then |
| 327 |
CErrors.user_err |
| 328 |
Pp.(str "Template polymorphism with conclusion strictly larger than a bound universe not supported.") |
| 329 |
|
| 330 |
let make_template_univ_names (u:UVars.Instance.t) : UVars.bound_names = |
| 331 |
let qlen, ulen = UVars.Instance.length u in |
| 332 |
{quals = Array.make qlen Anonymous; univs = Array.make ulen Anonymous} |
| 333 |
|
| 334 |
let get_template (mie:mutual_inductive_entry) = match mie.mind_entry_universes with |
| 335 |
| Monomorphic_ind_entry | Polymorphic_ind_entry _ -> mie, None, None |
| 336 |
| Template_ind_entry {uctx; default_univs} -> |
| 337 |
let ((template_qvars, _), (template_univs, _ as template_uctx) as template_context) = |
| 338 |
UVars.UContext.to_context_set uctx |
| 339 |
in |
| 340 |
let params = mie.mind_entry_params in |
| 341 |
let ind = |
| 342 |
match mie.mind_entry_inds with |
| 343 |
| [ind] -> ind |
| 344 |
| _ -> CErrors.user_err Pp.(str "Template-polymorphism not allowed with mutual inductives.") |
| 345 |
in |
| 346 |
let () = check_unbounded_from_below template_uctx in |
| 347 |
|
| 348 |
let template_context = |
| 349 |
UVars.UContext.of_context_set make_template_univ_names template_context |
| 350 |
in |
| 351 |
let template_abstract, template_context = |
| 352 |
let inst, ctx = UVars.abstract_universes template_context in |
| 353 |
UVars.make_instance_subst inst, ctx |
| 354 |
in |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
let check_not_appearing c = |
| 361 |
let qs, us = Vars.sort_and_universes_of_constr c in |
| 362 |
let qappearing = Sorts.QVar.Set.inter qs template_qvars in |
| 363 |
if not (Sorts.QVar.Set.is_empty qappearing) then |
| 364 |
CErrors.user_err |
| 365 |
Pp.(str "Template " ++ |
| 366 |
str (if Int.equal 1 (Sorts.QVar.Set.cardinal qappearing) then "quality" else "qualities") ++ |
| 367 |
spc() ++ prlist_with_sep spc Sorts.QVar.raw_pr (Sorts.QVar.Set.elements qappearing) ++ spc() ++ |
| 368 |
str "appear in illegal positions.") |
| 369 |
else check_not_appearing_univs ~template_univs us |
| 370 |
in |
| 371 |
let check_not_appearing_rel_ctx ctx = |
| 372 |
List.iter (Context.Rel.Declaration.iter_constr check_not_appearing) ctx |
| 373 |
in |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
let template_params = |
| 378 |
CList.map (fun param -> |
| 379 |
match param with |
| 380 |
| LocalDef (_,b,t) -> |
| 381 |
check_not_appearing b; |
| 382 |
check_not_appearing t; |
| 383 |
None |
| 384 |
| LocalAssum (_,t) -> |
| 385 |
match get_template_binding_arity ~template_univs t with |
| 386 |
| None | Some (_, None, None, _) -> |
| 387 |
check_not_appearing t; |
| 388 |
Some None |
| 389 |
| Some (decls, qopt, lopt, s) -> |
| 390 |
let () = check_not_appearing_rel_ctx decls in |
| 391 |
Some (Some (qopt, lopt, s))) |
| 392 |
params |
| 393 |
in |
| 394 |
let qbound, ubound = |
| 395 |
List.fold_left (fun (qbound, ubound as bound_in_params) -> function |
| 396 |
| None | Some None -> bound_in_params |
| 397 |
| Some (Some (qopt,lopt,_)) -> |
| 398 |
let ubound = match lopt with |
| 399 |
| None -> ubound |
| 400 |
| Some l -> |
| 401 |
if Level.Set.mem l ubound then |
| 402 |
CErrors.user_err Pp.(str "Non-linear template level " ++ Level.raw_pr l) |
| 403 |
else Level.Set.add l ubound |
| 404 |
in |
| 405 |
let qbound = Option.fold_right Sorts.QVar.Set.add qopt qbound in |
| 406 |
qbound, ubound) |
| 407 |
(Sorts.QVar.Set.empty,Level.Set.empty) |
| 408 |
template_params |
| 409 |
in |
| 410 |
let q_unbound = Sorts.QVar.Set.diff template_qvars qbound in |
| 411 |
let () = if not (Sorts.QVar.Set.is_empty q_unbound) then |
| 412 |
CErrors.user_err |
| 413 |
Pp.(str "Template " ++ |
| 414 |
str (if Int.equal 1 (Sorts.QVar.Set.cardinal q_unbound) then "quality" else "qualities") ++ spc() ++ |
| 415 |
prlist_with_sep spc Sorts.QVar.raw_pr (Sorts.QVar.Set.elements q_unbound) ++ spc() ++ |
| 416 |
str "not bound by parameters.") |
| 417 |
|
| 418 |
in |
| 419 |
let u_unbound = Level.Set.diff template_univs ubound in |
| 420 |
let () = if not (Level.Set.is_empty u_unbound) then |
| 421 |
CErrors.user_err |
| 422 |
Pp.(str "Template " ++ |
| 423 |
str (CString.plural (Level.Set.cardinal u_unbound) "universe") ++ |
| 424 |
spc() ++ Level.Set.pr Level.raw_pr u_unbound ++ spc() ++ |
| 425 |
str "not bound by parameters.") |
| 426 |
|
| 427 |
in |
| 428 |
|
| 429 |
|
| 430 |
let template_concl = |
| 431 |
|
| 432 |
let (decls, s) = Term.decompose_prod_decls ind.mind_entry_arity in |
| 433 |
let () = if not (isSort s) then |
| 434 |
CErrors.user_err Pp.(str "Template polymorphic inductive's type must be a syntactic arity.") |
| 435 |
in |
| 436 |
check_not_appearing_rel_ctx decls; |
| 437 |
let s = destSort s in |
| 438 |
let () = match s with |
| 439 |
| SProp | Prop | Set -> () |
| 440 |
| QSort (_, u) -> |
| 441 |
|
| 442 |
check_no_increment ~template_univs u; |
| 443 |
() |
| 444 |
| Type u -> |
| 445 |
check_no_increment ~template_univs u; |
| 446 |
() |
| 447 |
in |
| 448 |
UVars.subst_sort_level_sort template_abstract s |
| 449 |
in |
| 450 |
|
| 451 |
|
| 452 |
let () = List.iter check_not_appearing ind.mind_entry_lc in |
| 453 |
|
| 454 |
let template_param_arguments = |
| 455 |
let assums = CList.filter_map (fun x -> x) template_params in |
| 456 |
List.rev_map |
| 457 |
(Option.map (fun (_, _, s) -> |
| 458 |
UVars.subst_sort_level_sort template_abstract s)) |
| 459 |
assums |
| 460 |
in |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
let template_usubst : UVars.sort_level_subst = |
| 466 |
let bind_instance = UVars.UContext.instance uctx in |
| 467 |
let () = if not UVars.(eq_sizes (Instance.length bind_instance) (Instance.length default_univs)) |
| 468 |
then CErrors.anomaly Pp.(str "Inorrect default template universes declaration.") |
| 469 |
in |
| 470 |
let bind_qs, bind_us = UVars.Instance.to_array bind_instance in |
| 471 |
let default_qs, default_us = UVars.Instance.to_array default_univs in |
| 472 |
let qsubst = Array.fold_left2 (fun qsubst bind_q default_q -> |
| 473 |
let open Sorts.Quality in |
| 474 |
match bind_q, default_q with |
| 475 |
| QConstant _, _ -> assert false |
| 476 |
| QVar bind_q, QConstant QType -> |
| 477 |
Sorts.QVar.Map.add bind_q default_q qsubst |
| 478 |
| QVar _, _ -> CErrors.anomaly Pp.(str "Default template quality must be QType.")) |
| 479 |
Sorts.QVar.Map.empty |
| 480 |
bind_qs default_qs |
| 481 |
in |
| 482 |
let usubst = Array.fold_left2 (fun usubst bind_u default_u -> |
| 483 |
assert (not @@ Level.is_set bind_u); |
| 484 |
Level.Map.add bind_u default_u usubst) |
| 485 |
Level.Map.empty |
| 486 |
bind_us default_us |
| 487 |
in |
| 488 |
qsubst, usubst |
| 489 |
in |
| 490 |
|
| 491 |
mie, Some template_usubst, Some { |
| 492 |
template_param_arguments; |
| 493 |
template_context; |
| 494 |
template_concl; |
| 495 |
template_defaults = default_univs; |
| 496 |
} |
| 497 |
|
| 498 |
let abstract_packets env usubst ((arity,lc),(indices,splayed_lc),univ_info) = |
| 499 |
if not (List.is_empty univ_info.missing) |
| 500 |
then raise (InductiveError (env, MissingUnivConstraints (univ_info.missing,univ_info.ind_univ))); |
| 501 |
let arity = Vars.subst_univs_level_constr usubst arity in |
| 502 |
let lc = Array.map (Vars.subst_univs_level_constr usubst) lc in |
| 503 |
let indices = Vars.subst_univs_level_context usubst indices in |
| 504 |
let splayed_lc = Array.map (fun (args,out) -> |
| 505 |
let args = Vars.subst_univs_level_context usubst args in |
| 506 |
let out = Vars.subst_univs_level_constr usubst out in |
| 507 |
args,out) |
| 508 |
splayed_lc |
| 509 |
in |
| 510 |
let ind_univ = UVars.subst_sort_level_sort usubst univ_info.ind_univ in |
| 511 |
|
| 512 |
let arity = {user_arity = arity; sort = ind_univ} in |
| 513 |
|
| 514 |
let squashed = Option.map (function |
| 515 |
| AlwaysSquashed -> AlwaysSquashed |
| 516 |
| SometimesSquashed qs -> |
| 517 |
let qs = Sorts.Quality.Set.fold (fun q qs -> |
| 518 |
Sorts.Quality.Set.add (UVars.subst_sort_level_quality usubst q) qs) |
| 519 |
qs |
| 520 |
Sorts.Quality.Set.empty |
| 521 |
in |
| 522 |
SometimesSquashed qs) |
| 523 |
univ_info.ind_squashed |
| 524 |
in |
| 525 |
|
| 526 |
(arity,lc), (indices,splayed_lc), squashed |
| 527 |
|
| 528 |
let typecheck_inductive env ~sec_univs (mie:mutual_inductive_entry) = |
| 529 |
let () = match mie.mind_entry_inds with |
| 530 |
| [] -> CErrors.anomaly Pp.(str "empty inductive types declaration.") |
| 531 |
| _ -> () |
| 532 |
in |
| 533 |
|
| 534 |
mind_check_names env mie; |
| 535 |
assert (List.is_empty (Environ.rel_context env)); |
| 536 |
|
| 537 |
|
| 538 |
let mie, template_usubst, template = get_template mie in |
| 539 |
|
| 540 |
let env_univs = |
| 541 |
match mie.mind_entry_universes with |
| 542 |
| Template_ind_entry {uctx; default_univs=_} -> |
| 543 |
Environ.Internal.push_template_context uctx env |
| 544 |
| Monomorphic_ind_entry -> env |
| 545 |
| Polymorphic_ind_entry ctx -> |
| 546 |
let () = check_ucontext ctx env in |
| 547 |
let env = push_context ctx env in |
| 548 |
env |
| 549 |
in |
| 550 |
|
| 551 |
let has_template_poly = match mie.mind_entry_universes with |
| 552 |
| Template_ind_entry _ -> true |
| 553 |
| Monomorphic_ind_entry | Polymorphic_ind_entry _ -> false |
| 554 |
in |
| 555 |
|
| 556 |
|
| 557 |
let env_params, params = Typeops.check_context env_univs mie.mind_entry_params in |
| 558 |
|
| 559 |
|
| 560 |
let env_ar, data = List.fold_left_map (check_arity ~template:has_template_poly env_params) env_univs mie.mind_entry_inds in |
| 561 |
let env_ar_par = push_rel_context params env_ar in |
| 562 |
|
| 563 |
|
| 564 |
let isrecord = match mie.mind_entry_record with |
| 565 |
| Some (Some _) -> true |
| 566 |
| Some None | None -> false |
| 567 |
in |
| 568 |
let data = List.map2 (fun ind data -> |
| 569 |
check_constructors env_ar_par isrecord params ind.mind_entry_lc data) |
| 570 |
mie.mind_entry_inds data |
| 571 |
in |
| 572 |
|
| 573 |
let record = mie.mind_entry_record in |
| 574 |
let data, record, not_prim_reason_or_has_eta = match record with |
| 575 |
| None | Some None -> data, record, None |
| 576 |
| Some (Some _) -> |
| 577 |
|
| 578 |
match check_record data with |
| 579 |
| Result.Ok has_eta -> data, record, Some (Result.Ok has_eta) |
| 580 |
| Result.Error _ as reason -> |
| 581 |
|
| 582 |
|
| 583 |
let data = List.map (fun (a, b, univs) -> |
| 584 |
a, b, compute_elim_squash env_ar_par Sorts.prop univs) |
| 585 |
data |
| 586 |
in |
| 587 |
data, Some None, Some reason |
| 588 |
in |
| 589 |
|
| 590 |
let variance = match mie.mind_entry_variance with |
| 591 |
| None -> None |
| 592 |
| Some variances -> |
| 593 |
match mie.mind_entry_universes with |
| 594 |
| Monomorphic_ind_entry | Template_ind_entry _ -> |
| 595 |
CErrors.user_err Pp.(str "Inductive cannot be both monomorphic and universe cumulative.") |
| 596 |
| Polymorphic_ind_entry uctx -> |
| 597 |
|
| 598 |
let _qualities, univs = Instance.to_array @@ UContext.instance uctx in |
| 599 |
let univs = Array.map2 (fun a b -> a,b) univs variances in |
| 600 |
let univs = match sec_univs with |
| 601 |
| None -> univs |
| 602 |
| Some sec_univs -> |
| 603 |
|
| 604 |
let _, sec_univs = UVars.Instance.to_array sec_univs in |
| 605 |
let sec_univs = Array.map (fun u -> u, None) sec_univs in |
| 606 |
Array.append sec_univs univs |
| 607 |
in |
| 608 |
let variances = InferCumulativity.infer_inductive ~env_params ~env_ar_par |
| 609 |
~arities:(List.map (fun e -> e.mind_entry_arity) mie.mind_entry_inds) |
| 610 |
~ctors:(List.map (fun e -> e.mind_entry_lc) mie.mind_entry_inds) |
| 611 |
univs |
| 612 |
in |
| 613 |
Some variances |
| 614 |
in |
| 615 |
|
| 616 |
|
| 617 |
let usubst, univs = match mie.mind_entry_universes with |
| 618 |
| Monomorphic_ind_entry -> |
| 619 |
UVars.empty_sort_subst, Monomorphic |
| 620 |
| Template_ind_entry _ -> |
| 621 |
let usubst = Option.get template_usubst in |
| 622 |
usubst, Monomorphic |
| 623 |
| Polymorphic_ind_entry uctx -> |
| 624 |
let (inst, auctx) = UVars.abstract_universes uctx in |
| 625 |
let inst = UVars.make_instance_subst inst in |
| 626 |
(inst, Polymorphic auctx) |
| 627 |
in |
| 628 |
let params = Vars.subst_univs_level_context usubst params in |
| 629 |
let data = List.map (abstract_packets env usubst) data in |
| 630 |
|
| 631 |
let env_ar_par = |
| 632 |
let ctx = Environ.rel_context env_ar_par in |
| 633 |
let ctx = Vars.subst_univs_level_context usubst ctx in |
| 634 |
let env = Environ.pop_rel_context (Environ.nb_rel env_ar_par) env_ar_par in |
| 635 |
Environ.push_rel_context ctx env |
| 636 |
in |
| 637 |
|
| 638 |
env_ar_par, univs, template, variance, record, not_prim_reason_or_has_eta, params, Array.of_list data |