Source file inductiveops.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
open CErrors
open Util
open Names
open Univ
open Term
open Constr
open Vars
open Context
open Declarations
open Declareops
open Environ
open Reductionops
open Context.Rel.Declaration
let type_of_inductive env (ind,u) =
let (mib,_ as specif) = Inductive.lookup_mind_specif env ind in
Typeops.check_hyps_inclusion env (GlobRef.IndRef ind) mib.mind_hyps;
let t = Inductive.type_of_inductive (specif,u) in
Arguments_renaming.rename_type t (IndRef ind)
let e_type_of_inductive env sigma (ind,u) =
let (mib,_ as specif) = Inductive.lookup_mind_specif env ind in
Reductionops.check_hyps_inclusion env sigma (GlobRef.IndRef ind) mib.mind_hyps;
let t = Inductive.type_of_inductive (specif, EConstr.Unsafe.to_instance u) in
EConstr.of_constr (Arguments_renaming.rename_type t (IndRef ind))
let type_of_constructor env (cstr,u) =
let (mib,_ as specif) =
Inductive.lookup_mind_specif env (inductive_of_constructor cstr) in
Typeops.check_hyps_inclusion env (GlobRef.ConstructRef cstr) mib.mind_hyps;
let t = Inductive.type_of_constructor (cstr,u) specif in
Arguments_renaming.rename_type t (ConstructRef cstr)
let e_type_of_constructor env sigma (cstr,u) =
let (mib,_ as specif) =
Inductive.lookup_mind_specif env (inductive_of_constructor cstr) in
Reductionops.check_hyps_inclusion env sigma (GlobRef.ConstructRef cstr) mib.mind_hyps;
let t = Inductive.type_of_constructor (cstr,EConstr.Unsafe.to_instance u) specif in
EConstr.of_constr (Arguments_renaming.rename_type t (ConstructRef cstr))
let type_of_constructors env (ind,u as indu) =
let specif = Inductive.lookup_mind_specif env ind in
Inductive.type_of_constructors indu specif
let arities_of_constructors env (ind,u as indu) =
let specif = Inductive.lookup_mind_specif env ind in
Inductive.arities_of_constructors indu specif
type inductive_family = pinductive * constr list
let make_ind_family (mis, params) = (mis,params)
let dest_ind_family (mis,params) : inductive_family = (mis,params)
let map_ind_family f (mis,params) = (mis, List.map f params)
let liftn_inductive_family n d = map_ind_family (liftn n d)
let lift_inductive_family n = liftn_inductive_family n 1
let substnl_ind_family l n = map_ind_family (substnl l n)
let relevance_of_inductive_family env ((ind,_),_ : inductive_family) =
Inductive.relevance_of_inductive env ind
type inductive_type = IndType of inductive_family * EConstr.constr list
let ind_of_ind_type = function IndType (((ind,_),_),_) -> ind
let make_ind_type (indf, realargs) = IndType (indf,realargs)
let dest_ind_type (IndType (indf,realargs)) = (indf,realargs)
let map_inductive_type f (IndType (indf, realargs)) =
let f' c = EConstr.Unsafe.to_constr (f (EConstr.of_constr c)) in
IndType (map_ind_family f' indf, List.map f realargs)
let liftn_inductive_type n d = map_inductive_type (EConstr.Vars.liftn n d)
let lift_inductive_type n = liftn_inductive_type n 1
let substnl_ind_type l n = map_inductive_type (EConstr.Vars.substnl l n)
let relevance_of_inductive_type env (IndType (indf, _)) =
relevance_of_inductive_family env indf
let mkAppliedInd (IndType ((ind,params), realargs)) =
let open EConstr in
let ind = on_snd EInstance.make ind in
applist (mkIndU ind, (List.map EConstr.of_constr params)@realargs)
let mis_is_recursive_subset listind rarg =
let one_is_rec rvec =
List.exists
(fun ra ->
match dest_recarg ra with
| Mrec (_,i) -> Int.List.mem i listind
| _ -> false) rvec
in
Array.exists one_is_rec (dest_subterms rarg)
let mis_is_recursive (ind,mib,mip) =
mis_is_recursive_subset (List.interval 0 (mib.mind_ntypes - 1))
mip.mind_recargs
let mis_nf_constructor_type ((_,j),u) (mib,mip) =
let nconstr = Array.length mip.mind_consnames in
if j > nconstr then user_err Pp.(str "Not enough constructors in the type.");
let (ctx, cty) = mip.mind_nf_lc.(j - 1) in
subst_instance_constr u (Term.it_mkProd_or_LetIn cty ctx)
let nconstructors env ind =
let (_,mip) = Inductive.lookup_mind_specif env ind in
Array.length mip.mind_consnames
let nconstructors_env env ind = nconstructors env ind
[@@ocaml.deprecated "Alias for Inductiveops.nconstructors"]
let constructors_nrealargs env ind =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealargs
let constructors_nrealargs_env env ind = constructors_nrealargs env ind
[@@ocaml.deprecated "Alias for Inductiveops.constructors_nrealargs"]
let constructors_nrealdecls env ind =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealdecls
let constructors_nrealdecls_env env ind = constructors_nrealdecls env ind
[@@ocaml.deprecated "Alias for Inductiveops.constructors_nrealdecls"]
let constructor_nallargs env (ind,j) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealargs.(j-1) + mib.mind_nparams
let constructor_nallargs_env env (indsp,j) = constructor_nallargs env (indsp,j)
[@@ocaml.deprecated "Alias for Inductiveops.constructor_nallargs"]
let constructor_nalldecls env (ind,j) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealdecls.(j-1) + Context.Rel.length (mib.mind_params_ctxt)
let constructor_nalldecls_env env (indsp,j) = constructor_nalldecls env (indsp,j)
[@@ocaml.deprecated "Alias for Inductiveops.constructor_nalldecls"]
let constructor_nrealargs env (ind,j) =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealargs.(j-1)
let constructor_nrealargs_env env (ind,j) = constructor_nrealargs env (ind,j)
[@@ocaml.deprecated "Alias for Inductiveops.constructor_nrealargs"]
let constructor_nrealdecls env (ind,j) =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_consnrealdecls.(j-1)
let constructor_nrealdecls_env env (ind,j) = constructor_nrealdecls env (ind,j)
[@@ocaml.deprecated "Alias for Inductiveops.constructor_nrealdecls"]
let inductive_nrealargs env ind =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_nrealargs
let inductive_nrealargs_env env ind = inductive_nrealargs env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nrealargs"]
let inductive_nrealdecls env ind =
let (_,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_nrealdecls
let inductive_nrealdecls_env env ind = inductive_nrealdecls env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nrealdecls"]
let inductive_nallargs env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
mib.mind_nparams + mip.mind_nrealargs
let inductive_nallargs_env env ind = inductive_nallargs env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nallargs"]
let inductive_nparams env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
mib.mind_nparams
let inductive_nparams_env env ind = inductive_nparams env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nparams"]
let inductive_nparamdecls env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Context.Rel.length mib.mind_params_ctxt
let inductive_nparamdecls_env env ind = inductive_nparamdecls env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nparamsdecls"]
let inductive_nalldecls env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Context.Rel.length (mib.mind_params_ctxt) + mip.mind_nrealdecls
let inductive_nalldecls_env env ind = inductive_nalldecls env ind
[@@ocaml.deprecated "Alias for Inductiveops.inductive_nalldecls"]
let inductive_paramdecls env (ind,u) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Inductive.inductive_paramdecls (mib,u)
let inductive_paramdecls_env env (ind,u) = inductive_paramdecls env (ind,u)
[@@ocaml.deprecated "Alias for Inductiveops.inductive_paramsdecls"]
let inductive_alldecls env (ind,u) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Vars.subst_instance_context u mip.mind_arity_ctxt
let inductive_alldecls_env env (ind,u) = inductive_alldecls env (ind,u)
[@@ocaml.deprecated "Alias for Inductiveops.inductive_alldecls"]
let inductive_alltags env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Context.Rel.to_tags mip.mind_arity_ctxt
let constructor_alltags env (ind,j) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Context.Rel.to_tags (fst mip.mind_nf_lc.(j-1))
let constructor_has_local_defs env (indsp,j) =
let (mib,mip) = Inductive.lookup_mind_specif env indsp in
let l1 = mip.mind_consnrealdecls.(j-1) + Context.Rel.length (mib.mind_params_ctxt) in
let l2 = recarg_length mip.mind_recargs j + mib.mind_nparams in
not (Int.equal l1 l2)
let inductive_has_local_defs env ind =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
let l1 = Context.Rel.length (mib.mind_params_ctxt) + mip.mind_nrealdecls in
let l2 = mib.mind_nparams + mip.mind_nrealargs in
not (Int.equal l1 l2)
let top_allowed_sort env (kn,i as ind) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
mip.mind_kelim
let sorts_below top =
List.filter (fun s -> Sorts.family_leq s top) Sorts.[InSProp;InProp;InSet;InType]
let has_dependent_elim (mib,mip) =
match mib.mind_record with
| PrimRecord _ -> mib.mind_finite == BiFinite || mip.mind_relevance == Irrelevant
| NotRecord | FakeRecord -> true
let make_case_info env ind r style =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
let ind_tags =
Context.Rel.to_tags (List.firstn mip.mind_nrealdecls mip.mind_arity_ctxt) in
let cstr_tags =
Array.map2 (fun (d, _) n ->
Context.Rel.to_tags (List.firstn n d))
mip.mind_nf_lc mip.mind_consnrealdecls in
let print_info = { ind_tags; cstr_tags; style } in
{ ci_ind = ind;
ci_npar = mib.mind_nparams;
ci_cstr_ndecls = mip.mind_consnrealdecls;
ci_cstr_nargs = mip.mind_consnrealargs;
ci_relevance = r;
ci_pp_info = print_info }
type constructor_summary = {
cs_cstr : pconstructor;
cs_params : constr list;
cs_nargs : int;
cs_args : Constr.rel_context;
cs_concl_realargs : constr array
}
let lift_constructor n cs = {
cs_cstr = cs.cs_cstr;
cs_params = List.map (lift n) cs.cs_params;
cs_nargs = cs.cs_nargs;
cs_args = Vars.lift_rel_context n cs.cs_args;
cs_concl_realargs = Array.map (liftn n (cs.cs_nargs+1)) cs.cs_concl_realargs
}
let instantiate_params t params sign =
let nnonrecpar = Context.Rel.nhyps sign - List.length params in
let _,sign = Context.Rel.chop_nhyps nnonrecpar sign in
let _,t = decompose_prod_n_decls (Context.Rel.length sign) t in
let subst = subst_of_rel_context_instance_list sign params in
substl subst t
let instantiate_constructor_params (_,u as cstru) (mib,_ as mind_specif) params =
let typi = mis_nf_constructor_type cstru mind_specif in
let ctx = Vars.subst_instance_context u mib.mind_params_ctxt in
instantiate_params typi params ctx
let get_constructor ((ind,u),mib,mip,params) j =
assert (j <= Array.length mip.mind_consnames);
let typi = instantiate_constructor_params ((ind,j),u) (mib,mip) params in
let (args,ccl) = decompose_prod_decls typi in
let (_,allargs) = decompose_app_list ccl in
let vargs = List.skipn (List.length params) allargs in
{ cs_cstr = (ith_constructor_of_inductive ind j,u);
cs_params = params;
cs_nargs = Context.Rel.length args;
cs_args = args;
cs_concl_realargs = Array.of_list vargs }
let get_constructors env (ind,params) =
let (mib,mip) = Inductive.lookup_mind_specif env (fst ind) in
Array.init (Array.length mip.mind_consnames)
(fun j -> get_constructor (ind,mib,mip,params) (j+1))
let get_projections = Environ.get_projections
let make_case_invert env (IndType (((ind,u),params),indices)) ci =
if Typeops.should_invert_case env ci
then CaseInvert {indices=Array.of_list indices}
else NoInvert
let make_project env sigma ind pred c branches ps =
let open EConstr in
assert(Array.length branches == 1);
let na, ty, t = destLambda sigma pred in
let mib, mip as specif = Inductive.lookup_mind_specif env ind in
let () =
if not (Vars.noccurn sigma 1 t) &&
not (has_dependent_elim specif) then
user_err
Pp.(str"Dependent case analysis not allowed" ++
str" on inductive type " ++ Termops.pr_global_env env (IndRef ind) ++ str ".")
in
let branch = branches.(0) in
let ctx, br = decompose_lambda_n_decls sigma mip.mind_consnrealdecls.(0) branch in
let proj = match EConstr.destRel sigma br with
| exception DestKO -> None
| i ->
begin match List.skipn (i-1) ctx with
| exception Failure _ -> None
| ctx -> match ctx with
| [] -> None
| LocalDef _ :: _ ->
None
| LocalAssum _ :: ctx ->
let p = ps.(Context.Rel.nhyps ctx) in
Some (mkProj (Projection.make p true, c))
end
in
match proj with
| Some proj -> proj
| None ->
let n, len, ctx =
List.fold_right
(fun decl (i, j, ctx) ->
match decl with
| LocalAssum (na, ty) ->
let t = mkProj (Projection.make ps.(i) true, mkRel j) in
(i + 1, j + 1, LocalDef (na, t, Vars.liftn 1 j ty) :: ctx)
| LocalDef (na, b, ty) ->
(i, j + 1, LocalDef (na, Vars.liftn 1 j b, Vars.liftn 1 j ty) :: ctx))
ctx (0, 1, [])
in
mkLetIn (na, c, ty, it_mkLambda_or_LetIn (Vars.liftn 1 (Array.length ps + 1) br) ctx)
let simple_make_case_or_project env sigma ci pred invert c branches =
let open EConstr in
let ind = ci.ci_ind in
let projs = get_projections env ind in
match projs with
| None -> mkCase (EConstr.contract_case env sigma (ci, pred, invert, c, branches))
| Some ps -> make_project env sigma ind pred c branches ps
let make_case_or_project env sigma indt ci pred c branches =
let open EConstr in
let IndType (((ind,_),_),_) = indt in
let projs = get_projections env ind in
match projs with
| None ->
let invert = make_case_invert env indt ci in
mkCase (EConstr.contract_case env sigma (ci, pred, invert, c, branches))
| Some ps -> make_project env sigma ind pred c branches ps
let substnl_rel_context subst n sign =
let rec aux n = function
| d::sign -> substnl_decl subst n d :: aux (n+1) sign
| [] -> []
in List.rev (aux n (List.rev sign))
let substl_rel_context subst = substnl_rel_context subst 0
let get_arity env ((ind,u),params) =
let (mib,mip) = Inductive.lookup_mind_specif env ind in
let parsign =
let nparams = List.length params in
if Int.equal nparams mib.mind_nparams then
Inductive.inductive_paramdecls (mib,u)
else begin
assert (Int.equal nparams mib.mind_nparams_rec);
snd (Inductive.inductive_nonrec_rec_paramdecls (mib,u))
end in
let arproperlength = List.length mip.mind_arity_ctxt - List.length parsign in
let arsign,_ = List.chop arproperlength mip.mind_arity_ctxt in
let subst = subst_of_rel_context_instance_list parsign params in
let arsign = Vars.subst_instance_context u arsign in
substl_rel_context subst arsign
let build_dependent_constructor cs =
applist
(mkConstructU cs.cs_cstr,
(List.map (lift cs.cs_nargs) cs.cs_params)
@(Context.Rel.instance_list mkRel 0 cs.cs_args))
let build_dependent_inductive env ((ind, params) as indf) =
let arsign = get_arity env indf in
let nrealargs = List.length arsign in
applist
(mkIndU ind,
(List.map (lift nrealargs) params)@(Context.Rel.instance_list mkRel 0 arsign))
let make_arity_signature env sigma dep ((ind,_), _ as indf) =
let arsign = get_arity env indf in
let r = Inductive.relevance_of_inductive env ind in
let anon = make_annot Anonymous r in
let arsign = List.map (fun d -> Termops.map_rel_decl EConstr.of_constr d) arsign in
if dep then
Namegen.name_context env sigma
((LocalAssum (anon,EConstr.of_constr (build_dependent_inductive env indf)))::arsign)
else
arsign
let make_arity env sigma dep indf s =
let open EConstr in
it_mkProd_or_LetIn (mkSort s) (make_arity_signature env sigma dep indf)
(** From a rel context describing the constructor arguments,
build an expansion function.
The term built is expecting to be substituted first by
a substitution of the form [params, x : ind params] *)
let compute_projections env (kn, i as ind) =
let open Term in
let mib = Environ.lookup_mind kn env in
let u = make_abstract_instance (Declareops.inductive_polymorphic_context mib) in
let x = match mib.mind_record with
| NotRecord | FakeRecord ->
anomaly Pp.(str "Trying to build primitive projections for a non-primitive record")
| PrimRecord info ->
let id, _, _, _ = info.(i) in
make_annot (Name id) mib.mind_packets.(i).mind_relevance
in
let pkt = mib.mind_packets.(i) in
let { mind_nparams = nparamargs; mind_params_ctxt = params } = mib in
let ctx, _ = pkt.mind_nf_lc.(0) in
let ctx, paramslet = List.chop pkt.mind_consnrealdecls.(0) ctx in
let indty =
let inst = Context.Rel.instance mkRel 0 paramslet in
let indu = mkIndU (ind, u) in
let ty = mkApp (indu, inst) in
ty
in
let projections decl (proj_arg, j, pbs, subst) =
match decl with
| LocalDef (na,c,t) ->
let c = liftn 1 j c in
let c1 = substl subst c in
let subst = c1 :: subst in
(proj_arg, j+1, pbs, subst)
| LocalAssum (na,t) ->
match na.binder_name with
| Name id ->
let lab = Label.of_id id in
let proj_relevant = match na.binder_relevance with
| Sorts.Irrelevant -> false
| Sorts.Relevant -> true
| Sorts.RelevanceVar _ -> assert false
in
let kn = Projection.Repr.make ind ~proj_relevant ~proj_npars:mib.mind_nparams ~proj_arg lab in
let t = liftn 1 j t in
let ty = substl subst t in
let term = mkProj (Projection.make kn true, mkRel 1) in
let fterm = mkProj (Projection.make kn false, mkRel 1) in
let etab = it_mkLambda_or_LetIn (mkLambda (x, indty, term)) params in
let etat = it_mkProd_or_LetIn (mkProd (x, indty, ty)) params in
let body = (etab, etat) in
(proj_arg + 1, j + 1, body :: pbs, fterm :: subst)
| Anonymous ->
anomaly Pp.(str "Trying to build primitive projections for a non-primitive record")
in
let (_, _, pbs, subst) =
List.fold_right projections ctx (0, 1, [], [])
in
Array.rev_of_list pbs
let sigma t =
let open EConstr in
let (t, l) = decompose_app_list sigma t in
match EConstr.kind sigma t with
| Ind ind -> (ind, l)
| _ -> raise Not_found
let find_mrectype_vect env sigma c =
let (t, l) = EConstr.decompose_app sigma (whd_all env sigma c) in
match EConstr.kind sigma t with
| Ind ind -> (ind, l)
| _ -> raise Not_found
let find_mrectype env sigma c =
let (ind, v) = find_mrectype_vect env sigma c in (ind, Array.to_list v)
let find_rectype env sigma c =
let open EConstr in
let (t, l) = decompose_app_list sigma (whd_all env sigma c) in
match EConstr.kind sigma t with
| Ind (ind,u) ->
let (mib,mip) = Inductive.lookup_mind_specif env ind in
if mib.mind_nparams > List.length l then raise Not_found;
let l = List.map EConstr.Unsafe.to_constr l in
let (par,rargs) = List.chop mib.mind_nparams l in
let indu = (ind, EInstance.kind sigma u) in
IndType((indu, par),List.map EConstr.of_constr rargs)
| _ -> raise Not_found
let find_inductive env sigma c =
let open EConstr in
let (t, l) = decompose_app_list sigma (whd_all env sigma c) in
match EConstr.kind sigma t with
| Ind ind
when (fst (Inductive.lookup_mind_specif env (fst ind))).mind_finite <> CoFinite ->
let l = List.map EConstr.Unsafe.to_constr l in
(ind, l)
| _ -> raise Not_found
let find_coinductive env sigma c =
let open EConstr in
let (t, l) = decompose_app_list sigma (whd_all env sigma c) in
match EConstr.kind sigma t with
| Ind ind
when (fst (Inductive.lookup_mind_specif env (fst ind))).mind_finite == CoFinite ->
let l = List.map EConstr.Unsafe.to_constr l in
(ind, l)
| _ -> raise Not_found
let arity_of_case_predicate env (ind,params) dep k =
let arsign = get_arity env (ind,params) in
let r = Inductive.relevance_of_inductive env (fst ind) in
let mind = build_dependent_inductive env (ind,params) in
let concl = if dep then mkArrow mind r (mkSort k) else mkSort k in
Term.it_mkProd_or_LetIn concl arsign
let univ_level_mem l s = match s with
| Prop | Set | SProp -> false
| Type u -> univ_level_mem l u
| QSort (_, u) -> assert false
let rec instantiate_universes env evdref scl is = function
| (LocalDef _ as d)::sign, exp ->
d :: instantiate_universes env evdref scl is (sign, exp)
| d::sign, None::exp ->
d :: instantiate_universes env evdref scl is (sign, exp)
| (LocalAssum (na,ty))::sign, Some l::exp ->
let ctx,_ = Reduction.dest_arity env ty in
let u = Univ.Universe.make l in
let s =
if univ_level_mem l is then
scl
else
let evm, s = Evd.new_sort_variable Evd.univ_flexible !evdref in
let evm = Evd.set_leq_sort env evm s (EConstr.ESorts.make (Sorts.sort_of_univ u)) in
evdref := evm; s
in
let s = EConstr.ESorts.kind !evdref s in
(LocalAssum (na,mkArity(ctx,s))) :: instantiate_universes env evdref scl is (sign, exp)
| sign, [] -> sign
| [], _ -> assert false
let type_of_inductive_knowing_conclusion env sigma ((mib,mip),u) conclty =
match mip.mind_arity with
| RegularArity s -> sigma, EConstr.of_constr (subst_instance_constr u s.mind_user_arity)
| TemplateArity ar ->
let templ = match mib.mind_template with
| None -> assert false
| Some t -> t
in
let _,scl = splay_arity env sigma conclty in
let ctx = List.rev mip.mind_arity_ctxt in
let evdref = ref sigma in
let ctx =
instantiate_universes
env evdref scl ar.template_level (ctx,templ.template_param_levels) in
let scl = EConstr.ESorts.kind !evdref scl in
!evdref, EConstr.of_constr (mkArity (List.rev ctx,scl))
let type_of_projection_constant env (p,u) =
let pty = lookup_projection p env in
Vars.subst_instance_constr u pty
let type_of_projection_knowing_arg env sigma p c ty =
let c = EConstr.Unsafe.to_constr c in
let IndType(pars,realargs) =
try find_rectype env sigma ty
with Not_found ->
raise (Invalid_argument "type_of_projection_knowing_arg_type: not an inductive type")
in
let (_,u), pars = dest_ind_family pars in
substl (c :: List.rev pars) (type_of_projection_constant env (p,u))
let control_only_guard env sigma c =
let c = Evarutil.nf_evar sigma c in
let check_fix_cofix e c =
let c = EConstr.Unsafe.to_constr c in
match kind c with
| CoFix (_,(_,_,_) as cofix) ->
Inductive.check_cofix e cofix
| Fix fix ->
Inductive.check_fix e fix
| _ -> ()
in
let rec iter env c =
check_fix_cofix env c;
EConstr.iter_with_full_binders env sigma EConstr.push_rel iter env c
in
iter env c