Debian Codegraph @

tactics/indrec.ml

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 CErrors
12 open Util
13 open Names
14 open Constr
15 open EConstr
16 open Declarations
17 open Context
18 open Inductive
19 open Environ
20 open LibBinding
21 open State
22 open AllScheme
23 open Retyping
24
25 type dep_flag = bool
26
27 (* Errors related to recursors building *)
28 type recursion_scheme_error =
29 | NotMutualInScheme of inductive * inductive
30 | DuplicateInductiveBlock of inductive
31
32 exception RecursionSchemeError of env * recursion_scheme_error
33
34 let (let@) x f = x f
35 let (let*) x f = State.bind x f
36 let dbg = CDebug.create ~name:"generate_eliminators" ()
37
38 (* ************************************************************************** *)
39 (* Functions on Parameters *)
40 (* ************************************************************************** *)
41
42 let split_uparans_nuparams mib params =
43 let (uparams, nuparams) = Context.Rel.chop_nhyps mib.mind_nparams_rec (List.rev params) in
44 (List.rev uparams, List.rev nuparams)
45
46 (** Generalize parameters for template and univ poly, and split uniform and non-uniform parameters *)
47 let get_params_sep sigma mib u =
48 let (sigma, params, _) = Inductiveops.paramdecls_fresh_template sigma (mib, u) in
49 let (uparams, nuparams) = split_uparans_nuparams mib params in
50 (sigma, uparams, nuparams)
51
52 (** Closure of uniform parameters forgetting letins *)
53 let closure_uparams binder naming_scheme uparams cc =
54 closure_context fid binder Old naming_scheme uparams cc
55
56 (** Closure of non-uniform parameters if [b], forgetting letins *)
57 let closure_nuparams_opt ~quantify binder naming_scheme nuparams cc =
58 if quantify
59 then closure_context fid binder Old naming_scheme nuparams (fun x -> cc (Some x))
60 else cc None
61
62 (** Closure of non-uniform parameters if [key_uparams_opt = None], forgetting letins *)
63 let closure_nuparams binder naming_scheme nuparams key_uparams_opt cc =
64 let@ key_uparams_opt' = closure_nuparams_opt ~quantify:(Option.is_empty key_uparams_opt)
65 binder naming_scheme nuparams in
66 match key_uparams_opt, key_uparams_opt' with
67 | Some z, None | None, Some z -> cc z
68 | _, _ -> assert false
69
70 (** Get the position in ind_bodies out of the position of mind_packets *)
71 let find_opt_pos p l =
72 let rec aux i l =
73 match l with
74 | [] -> None
75 | h::_ when p h -> Some (i, h)
76 | _::t -> aux (1+i) t
77 in
78 aux 0 l
79
80 (** Closure for indices. They are considered as [Fresh] as they are not in the context of the arguments *)
81 let closure_indices binder naming_scheme indb u f =
82 let* i = get_indices indb u in
83 closure_context fid binder Fresh naming_scheme i f
84
85
86 (* ************************************************************************** *)
87 (* Generate Eliminators' Type *)
88 (* ************************************************************************** *)
89
90 (* Convention for comments:
91 - A1 ... An : uniform parameters = uparams
92 - B0 ... Bm : non-uniform parameters = nuparams
93 - i0 ... il : indices
94 *)
95
96 (** Builds the type of the predicate for the i-th block
97 forall (B0 ... Bm : nuparams),
98 forall (i1 ... tl : indices),
99 (Ind A1 ... An B0 ... Bm i1 ... il) -> U) *)
100 let make_type_pred kn u (pos_ind, ind, dep, sort) key_uparams nuparams key_nuparams_opt =
101 let@ key_nuparams = closure_nuparams Prod naming_hd_fresh nuparams key_nuparams_opt in
102 let@ key_indices = closure_indices Prod (naming_hd_fresh_dep dep) ind u in
103 (* NOT DEP: return the sort *)
104 if not dep then
105 return @@ mkSort sort
106 else
107 (* DEP: bind the inductive, and return the sort *)
108 let* ind_rev = ind_relevance ind u in
109 let ind_name = make_annot Anonymous ind_rev in
110 let* tind = make_ind ((kn, pos_ind), u) key_uparams key_nuparams key_indices in
111 let@ _ = make_binder fid Prod naming_hd_fresh ind_name tind in
112 return @@ mkSort sort
113
114 (** Make the predicate P B0 ... Bm i0 ... il t *)
115 let make_pred rec_hyp key_preds focus dep inst_nuparams inst_indices t =
116 let* pred_var = geti_term key_preds focus in
117 let pred =
118 if rec_hyp
119 then mkApp (pred_var, Array.concat [inst_nuparams; inst_indices])
120 else mkApp (pred_var, inst_indices) in
121 if not dep
122 then return pred
123 else return @@ mkApp (pred, [| t |])
124
125 (** Closure Predicates *)
126 let closure_preds kn u ind_bodies binder key_uparams nuparams key_nuparams_opt cc =
127 fold_right_state (fun a l -> a :: l) ind_bodies (fun _ ind cc ->
128 let pred_name = make_annot (Name (Id.of_string "P")) ERelevance.relevant in
129 let* pred_type = make_type_pred kn u ind key_uparams nuparams key_nuparams_opt in
130 make_binder fid binder naming_hd_fresh pred_name pred_type cc
131 ) cc
132
133 (** Recursively compute the predicate, returns [None] if it is not nested *)
134 let compute_pred to_compute f i x =
135 if to_compute then
136 let* (locs, head) = decompose_lambda_decls x in
137 let@ key_loc = closure_context fopt Lambda Fresh naming_id locs in
138 (* create new variable *)
139 let* head_sort = retyping_sort_of head in
140 let arg_rev = relevance_of_sort head_sort in
141 let arg_name = make_annot Anonymous arg_rev in
142 (* let name_var = make_annot Anonymous ERelevance.relevant in *)
143 let@ key_arg = make_binder fopt Lambda naming_id arg_name head in
144 let* arg_type = State.get_type key_arg in
145 (* compute rec call *)
146 let* res = f key_arg arg_type in
147 return @@ res
148 else return None
149
150 (** Recursively compute the predicate, returns [None] if it is not nested *)
151 let compute_pred_eta to_compute f i x =
152 let* res = compute_pred to_compute f i x in
153 let* sigma = get_sigma in
154 let res = Option.map (Reductionops.shrink_eta sigma) res in
155 return res
156
157 (** Compute the type of the recursive call *)
158 let rec make_rec_call_hyp kn pos_ind mib ind_bodies key_preds key_arg arg_type =
159 (* Decompose the argument, rebind local variables and compute the argument *)
160 let* (locs, head) = view_argument kn mib [] [] arg_type in
161 let@ key_locals = closure_context fopt Prod Fresh naming_id locs in
162 let* arg_term = get_term key_arg in
163 let* locs_term = get_terms key_locals in
164 let inst_arg = mkApp (arg_term, locs_term) in
165 (* Match head *)
166 match head with
167 | ArgIsInd (pos_ind_block, inst_nuparams, inst_indices) ->
168 begin
169 (* generate recursion hypotheses only for the blocks that are used *)
170 match find_opt_pos (fun (i,_,_,_) -> i = pos_ind_block) ind_bodies with
171 | None -> return None
172 | Some (pred_pos, (_, _, pred_dep, sort)) ->
173 let* rec_hyp = make_pred true key_preds pred_pos pred_dep inst_nuparams inst_indices inst_arg in
174 return (Some (rec_hyp))
175 end
176 | ArgIsNested (kn_nested, pos_nested, mib_nested, mib_nested_strpos, ind_nested,
177 inst_uparams, inst_nuparams_indices) ->
178 (* eta expand arguments *)
179 let uparams_nested = of_rel_context @@ fst @@
180 split_uparans_nuparams mib_nested mib_nested.mind_params_ctxt in
181 let* inst_uparams = eta_expand_instantiation inst_uparams uparams_nested in
182 (* Compute the recursive predicates *)
183 let compute_pred i x b = compute_pred_eta b (make_rec_call_hyp kn pos_ind mib ind_bodies key_preds) i x in
184 let* rec_preds = array_map2i compute_pred inst_uparams (Array.of_list mib_nested_strpos) in
185 (* If at least one argument is nested, lookup the sparse parametricity *)
186 let args_are_nested = Array.map Option.has_some rec_preds in
187 if Array.for_all not args_are_nested then
188 return None
189 else begin
190 match lookup_all_theorem (kn, pos_ind) (kn_nested, pos_nested) (Array.to_list args_are_nested) with
191 | None -> return None
192 | Some (partial_nesting, ref_pred, _) ->
193 let* rec_hyp = make_all_predicate ~partial_nesting ref_pred mib_nested_strpos
194 inst_uparams rec_preds inst_nuparams_indices inst_arg in
195 (* return *)
196 return (Some (rec_hyp))
197 end
198 | _ -> return None
199
200 (** Create and bind the recursive call, if [rec_hyp] and if any *)
201 let make_rec_call_cc rec_hyp kn pos_ind mib ind_bodies key_preds _ key_arg cc =
202 let* arg = State.get_type key_arg in
203 if rec_hyp then begin
204 let* rec_call = make_rec_call_hyp kn pos_ind mib ind_bodies key_preds key_arg arg in
205 match rec_call with
206 | None -> cc [key_arg]
207 | Some rec_hyp_type ->
208 (* Compute the relevance after the instantiation *)
209 let* rec_hyp_sort = retyping_sort_of rec_hyp_type in
210 let rec_hyp_rev = relevance_of_sort rec_hyp_sort in
211 let rec_hyp_name = make_annot Anonymous rec_hyp_rev in
212 let@ _ = make_binder fid Prod naming_id rec_hyp_name rec_hyp_type in
213 cc [key_arg]
214 end
215 else cc [key_arg]
216
217 (** Closure of the args, and of the rec call if [rec_hyp], and if any *)
218 let closure_args_and_rec_call rec_hyp kn pos_ind u mib ind_bodies dep key_preds args =
219 read_by_decl args
220 (build_binder fid Prod Old (naming_hd_dep dep))
221 (fun _ _ cc -> cc [])
222 (make_rec_call_cc rec_hyp kn pos_ind mib ind_bodies key_preds)
223
224 (** Generates the type associated to a constructor
225 forall (B0 ... Bm : nuparams),
226 forall x0 : arg0, [P x0], ..., xn : argn, [P n],
227 P B0 ... Bm f0 ... fl (cst A0 ... An B0 ... Bm x0 ... xl) *)
228 let make_type_ctor kn u mib ind_bodies pos_list (pos_ind, ind, dep, sort)
229 pos_ctor (args, indices) key_uparams nuparams key_nuparams_opt key_preds =
230 let rec_hyp = Option.is_empty key_nuparams_opt in
231 let@ key_nuparams = closure_nuparams Prod naming_id nuparams key_nuparams_opt in
232 let@ key_args = closure_args_and_rec_call rec_hyp kn pos_ind u mib ind_bodies dep key_preds args in
233 (* make cst *)
234 let* cst_args = get_terms key_args in
235 let* cst = make_cst ((kn, pos_ind), u) pos_ctor key_uparams key_nuparams cst_args in
236 (* make pred cst *)
237 let* inst_nuparams = get_terms key_nuparams in
238 let* inst_indices = array_mapi (fun _ -> weaken) indices in
239 make_pred rec_hyp key_preds pos_list dep inst_nuparams inst_indices cst
240
241 (** Closure assumptions functions over all the ctors *)
242 let closure_ctors rec_hyp kn mib u ind_bodies binder key_uparams nuparams key_nuparams_opt key_preds =
243 fold_right_state (fun a l -> a :: l) ind_bodies (
244 fun pos_list (pos_ind, ind, dep, sort) cc ->
245 iterate_ctors mib ind u (
246 fun pos_ctor ctor cc ->
247 let assum_name = make_annot (Name ind.mind_consnames.(pos_ctor)) (relevance_of_sort sort) in
248 let* assum_type = make_type_ctor kn u mib ind_bodies pos_list (pos_ind, ind, dep, sort)
249 pos_ctor ctor key_uparams nuparams key_nuparams_opt key_preds in
250 make_binder fid binder naming_hd_fresh assum_name assum_type cc
251 ) cc
252 )
253
254 (** Make the type of the conclusion
255 P B0 ... Bm i0 ... il x *)
256 let make_ccl rec_hyp key_preds focus dep key_nuparams key_indices key_VarMatch =
257 let* inst_nuparams = get_terms key_nuparams in
258 let* inst_indices = get_terms key_indices in
259 let* var = get_term key_VarMatch in
260 make_pred rec_hyp key_preds focus dep inst_nuparams inst_indices var
261
262
263 (** Make the return type
264 forall (B0 ... Bm : nuparams),
265 forall (i1 ... il : indices),
266 forall (x : Ind A0 ... An B0 ... Bm i0 ... il),
267 P B0 ... Bm i0 ... il x *)
268 let make_return_type kn u ind_bodies focus key_uparams nuparams key_nuparams_opt key_preds =
269 let (pos_ind, ind, dep, sort) = List.nth ind_bodies focus in
270 let@ key_nuparams = closure_nuparams Prod naming_hd_fresh nuparams key_nuparams_opt in
271 let@ key_indices = closure_indices Prod naming_hd_fresh ind u in
272 let* ind_rev = ind_relevance ind u in
273 let ind_name = make_annot Anonymous ind_rev in
274 let* ind_tm = make_ind ((kn, pos_ind), u) key_uparams key_nuparams key_indices in
275 let@ (key_VarMatch) = make_binder fid Prod naming_hd_fresh ind_name ind_tm in
276 let rec_hyp = Option.is_empty key_nuparams_opt in
277 make_ccl rec_hyp key_preds focus dep key_nuparams key_indices key_VarMatch
278
279 (** Generate the type of the recursor *)
280 let gen_elim_type print_constr rec_hyp kn u mib uparams nuparams ind_bodies focus =
281 let t =
282 let@ key_uparams = closure_uparams Prod naming_hd_fresh uparams in
283 let@ key_nuparams_opt = closure_nuparams_opt ~quantify:(not rec_hyp) Prod naming_hd_fresh nuparams in
284 let@ key_preds = closure_preds kn u ind_bodies Prod key_uparams nuparams key_nuparams_opt in
285 let@ key_ctors = closure_ctors rec_hyp kn mib u ind_bodies Prod key_uparams nuparams key_nuparams_opt key_preds in
286 make_return_type kn u ind_bodies focus key_uparams nuparams key_nuparams_opt key_preds
287 in
288 (* DEBUG *)
289 let* t = t in
290 let* env = get_env in
291 let* sigma = get_sigma in
292 dbg Pp.(fun () ->
293 str "Eliminator's Type for "
294 ++ str "(" ++ str (MutInd.to_string kn) ++ str ", " ++ int focus ++ str ") "
295 ++ (
296 if rec_hyp then
297 str "with induction hypotheses"
298 else
299 str "without induction hypotheses"
300 )
301 ++ fnl () ++ print_constr env sigma t ++ fnl ()
302 );
303 return t
304
305
306 (* ************************************************************************** *)
307 (* Generate Eliminators' Body *)
308 (* ************************************************************************** *)
309
310 (** Compute the recursive call *)
311 let rec make_rec_call_proof kn pos_ind mib ind_bodies key_preds key_fixs key_arg arg_type =
312 (* Decompose the argument, rebind local variables and compute the argument *)
313 let* (locs, head) = view_argument kn mib [] [] arg_type in
314 let@ key_locals = closure_context fopt Lambda Fresh naming_id locs in
315 let* arg_term = get_term key_arg in
316 let* locs_term = get_terms key_locals in
317 let inst_arg = mkApp (arg_term, locs_term) in
318 (* Match head *)
319 match head with
320 | ArgIsInd (pos_ind_block, inst_nuparams, inst_indices) ->
321 begin
322 (* generate recursion hypotheses only for the blocks that are used *)
323 match find_opt_pos (fun (i,_,_,_) -> i = pos_ind_block) ind_bodies with
324 | None -> return None
325 | Some (pred_pos, _) ->
326 (* Fi B0 ... Bm i0 ... il (x a0 ... an) *)
327 let* fix = geti_term key_fixs pred_pos in
328 return @@ Some (mkApp (fix, Array.concat [inst_nuparams; inst_indices; [|inst_arg|]]))
329 end
330 | ArgIsNested (kn_nested, pos_nested, mib_nested, mib_nested_strpos, ind_nested,
331 inst_uparams, inst_nuparams_indices) ->
332 (* eta expand arguments *)
333 let uparams_nested = of_rel_context @@ fst @@
334 split_uparans_nuparams mib_nested mib_nested.mind_params_ctxt in
335 let* inst_uparams = eta_expand_instantiation inst_uparams uparams_nested in
336 (* Compute the recursive predicates, and their proofs *)
337 let compute_pred_preds i x b = compute_pred_eta b (make_rec_call_hyp kn pos_ind mib ind_bodies key_preds) i x in
338 let* rec_preds = array_map2i compute_pred_preds inst_uparams (Array.of_list mib_nested_strpos) in
339 let compute_pred_holds i x b = compute_pred_eta b (make_rec_call_proof kn pos_ind mib ind_bodies key_preds key_fixs) i x in
340 let* rec_preds_hold = array_map2i compute_pred_holds inst_uparams (Array.of_list mib_nested_strpos) in
341 (* If at least one argument is nested, lookup the local fundamental theorem *)
342 let args_are_nested = Array.map Option.has_some rec_preds_hold in
343 if Array.for_all not args_are_nested then
344 return None
345 else begin
346 match lookup_all_theorem (kn, pos_ind) (kn_nested, pos_nested) (Array.to_list args_are_nested) with
347 | None -> return None
348 | Some (partial_nesting, _, ref_thm) ->
349 let* rec_hyp = make_all_theorem ~partial_nesting ref_thm mib_nested_strpos inst_uparams
350 rec_preds rec_preds_hold inst_nuparams_indices inst_arg in
351 return @@ Some rec_hyp
352 end
353 | _ -> return None
354
355 (** Compute the arguments of the rec call *)
356 let compute_args_fix rec_hyp kn pos_ind mib ind_bodies pos_list key_preds key_fixs key_args =
357 CList.fold_right_i (fun pos_arg key_arg acc ->
358 let* acc = acc in
359 let* arg_term = get_term key_arg in
360 if rec_hyp then
361 let* arg_type = State.get_type key_arg in
362 let* rec_call = make_rec_call_proof kn pos_ind mib ind_bodies key_preds key_fixs key_arg arg_type in
363 match rec_call with
364 | Some rc_tm -> return @@ arg_term :: rc_tm :: acc
365 | None -> return @@ arg_term :: acc
366 else
367 return @@ arg_term :: acc
368 ) 0 key_args (return [])
369
370 let gen_elim_term print_constr rec_hyp kn u mib uparams nuparams ind_bodies focus =
371
372 let t =
373
374 (* 1. Closure Uparams / preds / ctors *)
375 let@ key_uparams = closure_uparams Lambda naming_hd_fresh uparams in
376 let@ key_nuparams_opt = closure_nuparams_opt ~quantify:(not rec_hyp) Lambda naming_hd_fresh nuparams in
377 let@ key_preds = closure_preds kn u ind_bodies Lambda key_uparams nuparams key_nuparams_opt in
378 let@ key_ctors = closure_ctors rec_hyp kn mib u ind_bodies Lambda key_uparams nuparams key_nuparams_opt key_preds in
379 (* 2. Fixpoint *)
380 let fix_name pos_list (_,_,_,sort) = make_annot (Name (Id.of_string "F")) (relevance_of_sort sort) in
381 let fix_type pos_list _ = make_return_type kn u ind_bodies pos_list key_uparams nuparams key_nuparams_opt key_preds in
382 let fix_rarg pos_list (_,ind,_,_) = (mib.mind_nparams - mib.mind_nparams_rec) + ind.mind_nrealargs in
383 let is_rec =
384 let (_, ind, _, _) = List.hd ind_bodies in
385 List.length ind_bodies > 1 || (rec_hyp && Inductiveops.mis_is_recursive ind) in
386 let@ (key_fixs, pos_list, (pos_ind, ind, dep, sort)) =
387 (* Doe not create a fix if it is not-recursive and only has one inductive body *)
388 if is_rec
389 then make_fix ind_bodies focus fix_rarg fix_name fix_type
390 else fun cc -> cc ([], 0, List.hd ind_bodies) in
391 (* 3. Closure Nuparams / Indices / Var *)
392 let@ key_nuparams = closure_nuparams Lambda naming_hd_fresh nuparams key_nuparams_opt in
393 let@ key_indices = closure_indices Lambda naming_hd_fresh ind u in
394 let* ind_rev = ind_relevance ind u in
395 let ind_name = make_annot Anonymous ind_rev in
396 let* tind = make_ind ((kn, pos_ind), u) key_uparams key_nuparams key_indices in
397 let@ key_VarMatch = make_binder fid Lambda naming_hd_fresh ind_name tind in
398 let ccl =
399 (* 4 Match to prove P ... x *)
400 let* inst_params = get_terms (key_uparams @ key_nuparams )in
401 let case_pred = make_ccl rec_hyp key_preds pos_list dep key_nuparams in
402 let* var_match = get_term key_VarMatch in
403 let* inst_indices = get_terms key_indices in
404 let@ (key_args, _, _, pos_ctor) =
405 make_case_or_projections naming_hd_fresh mib (kn, pos_ind) ind u key_uparams key_nuparams inst_params
406 inst_indices case_pred (relevance_of_sort sort) var_match in
407 (* 5 Body of the branch *)
408 let* hyp = getij_term key_ctors pos_list pos_ctor in
409 let* inst_nuparams = get_terms key_nuparams in
410 let* cfix = compute_args_fix rec_hyp kn pos_ind mib ind_bodies pos_list key_preds key_fixs key_args in
411 if is_rec then
412 typing_checked_appvect hyp (Array.concat [inst_nuparams; Array.of_list cfix])
413 else
414 typing_checked_appvect hyp (Array.of_list cfix)
415 in
416 (* 6. If it is not-recursive, has primitive projections and is dependent => add a cast *)
417 let* env = get_env in
418 let projs = Environ.get_projections env (kn, pos_ind) in
419 if is_rec || Option.is_empty projs || not dep then
420 ccl
421 else
422 let* arg_type = make_ccl rec_hyp key_preds pos_list dep key_nuparams key_indices key_VarMatch in
423 let* ccl = ccl in
424 return @@ mkCast (ccl, DEFAULTcast, arg_type)
425
426 in
427 (* DEBUG *)
428 let* t = t in
429 let* env = get_env in
430 let* sigma = get_sigma in
431 dbg Pp.(fun () ->
432 str "Eliminator's for "
433 ++ str "(" ++ str (MutInd.to_string kn) ++ str ", " ++ int focus ++ str ") "
434 ++ (
435 if rec_hyp then
436 str "with induction hypotheses"
437 else
438 str "without induction hypotheses"
439 )
440 ++ fnl () ++ print_constr env sigma t ++ fnl ()
441 );
442 return t
443
444 (**********************************************************************)
445 (* build the eliminators mutual and individual *)
446
447 (** Check all dependent eliminations are allowed *)
448 let check_valid_elimination env sigma (kn, n) mib u lrecspec rec_hyp =
449 (* Check the mutual can be eliminated. *)
450 if mib.mind_private = Some true
451 then user_err (Pp.str "case analysis on a private inductive type is not allowed");
452 (* Check all the blocks can be eliminated *)
453 List.iter (fun ((kni, ni),dep,s) ->
454 (* Check that all the blocks can be eliminated to s *)
455 let () = if not @@ Inductiveops.is_allowed_elimination sigma ((mib,mib.mind_packets.(ni)),u) s then
456 raise (Pretype_errors.error_not_allowed_elimination env sigma rec_hyp s ((kn, ni), u)) in
457 (* Check if dep elim is allowed: rec (co)ind records with prim proj can not be eliminated dependently *)
458 if dep && not (Inductiveops.has_dependent_elim (mib, mib.mind_packets.(ni))) then
459 raise (Pretype_errors.error_not_allowed_dependent_elimination env sigma rec_hyp (kni, ni))
460 ) lrecspec
461
462 (** Check all the blocks are mutual, and not given twice *)
463 let check_valid_mutual env sigma (kn, n) mib u lrecspec =
464 let _ : int list =
465 List.fold_left (fun ln ((kni, ni),dep,s) ->
466 (* Check all the blocks are mutual *)
467 let () = if not (QMutInd.equal env kn kni) then
468 raise (RecursionSchemeError (env, NotMutualInScheme ((kn, n), (kni, ni)))) in
469 (* Check none is given twice *)
470 if Int.List.mem ni ln then
471 raise (RecursionSchemeError (env, DuplicateInductiveBlock (kn, ni)))
472 else ni::ln)
473 [n] lrecspec
474 in
475 ()
476
477 let build_mutual_induction_scheme_gen rec_hyp env sigma lrecspec u =
478 match lrecspec with
479 | (mind,dep,s)::tail ->
480 let mib, mip = lookup_mind_specif env mind in
481 (* Check the blocks are all mutual, different, and can be eliminated dependently *)
482 let () = check_valid_mutual env sigma mind mib u tail in
483 let () = check_valid_elimination env sigma mind mib u lrecspec rec_hyp in
484 (* Compute values for gen_elim *)
485 let listdepkind = (snd mind, mip, dep, s) ::
486 List.map (fun ((_,ni), dep, s) -> (ni, mib.mind_packets.(ni), dep, s)) tail in
487 (* Get parameters, and generalized them for UnivPoly + TemplatePoly *)
488 let (sigma, uparams, nuparams) = get_params_sep sigma mib u in
489 (* Compute eliminators' types and terms *)
490 let f =
491 let* recs_ty = list_mapi
492 (fun i _ -> gen_elim_type Termops.Internal.print_constr_env rec_hyp (fst mind) u mib uparams nuparams listdepkind i)
493 (List.init (List.length listdepkind) (fun _ -> 2))
494 in
495 let* recs_tm = list_mapi
496 (fun i _ -> gen_elim_term Termops.Internal.print_constr_env rec_hyp (fst mind) u mib uparams nuparams listdepkind i)
497 (List.init (List.length listdepkind) (fun _ -> 2))
498 in
499 return @@ (recs_ty, recs_tm)
500 in run env sigma f
501 | _ -> anomaly (Pp.str "build_mutual_induction_scheme expects a non empty list of inductive types.")
502
503 let build_mutual_induction_scheme env sigma ?(force_mutual=false) lrecspec u =
504 let (sigma, (_, recs_tm)) = build_mutual_induction_scheme_gen true env sigma lrecspec u in
505 sigma, recs_tm
506
507 let build_induction_scheme env sigma (ind, u) dep kind =
508 let (sigma, (_, recs_tm)) = build_mutual_induction_scheme_gen true env sigma [(ind, dep, kind)] u in
509 (sigma, List.hd recs_tm)
510
511 let build_case_analysis_scheme env sigma (ind, u) dep kind =
512 let (sigma, (recs_ty, recs_tm)) = build_mutual_induction_scheme_gen false env sigma [(ind, dep, kind)] u in
513 (sigma, List.hd recs_tm, List.hd recs_ty)