Debian Codegraph @

tactics/induction.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 module CVars = Vars
12
13 open Pp
14 open CErrors
15 open Util
16 open Names
17 open Nameops
18 open Constr
19 open Context
20 open Termops
21 open Environ
22 open EConstr
23 open Vars
24 open Namegen
25 open Declarations
26 open Reductionops
27 open Tacred
28 open Genredexpr
29 open Logic
30 open Clenv
31 open Tacticals
32 open Rocqlib
33 open Evarutil
34 open Elimschemes
35 open Unification
36 open Locus
37 open Locusops
38 open Tactypes
39 open Proofview.Notations
40 open Tactics
41
42 module NamedDecl = Context.Named.Declaration
43
44 let tclEVARS = Proofview.Unsafe.tclEVARS
45
46 let inj_with_occurrences e = (AllOccurrences,e)
47
48 let typ_of env sigma c =
49 let open Retyping in
50 try get_type_of ~lax:true env sigma c
51 with RetypeError e ->
52 user_err (print_retype_error e)
53
54 (*********************************************)
55 (* Errors *)
56 (*********************************************)
57
58 exception AlreadyUsed of Id.t
59 exception SchemeDontApply
60 exception NeedFullyAppliedArgument
61 exception NotAnInductionScheme of string
62 exception NotAnInductionSchemeLetIn
63 exception CannotFindInductiveArgument
64 exception MentionConclusionDependentOn of Id.t
65 exception DontKnowWhatToDoWith of intro_pattern_naming_expr
66 exception UnsupportedWithClause
67 exception UnsupportedEqnClause
68 exception UnsupportedInClause of bool
69 exception DontKnowWhereToFindArgument
70 exception MultipleAsAndUsingClauseOnlyList
71
72 let error ?loc e =
73 Loc.raise ?loc e
74
75 exception Unhandled
76
77 let wrap_unhandled f e =
78 try Some (f e)
79 with Unhandled -> None
80
81 let tactic_interp_error_handler = function
82 | AlreadyUsed id ->
83 Id.print id ++ str " is already used."
84 | SchemeDontApply ->
85 str "Scheme cannot be applied."
86 | NeedFullyAppliedArgument ->
87 str "Need a fully applied argument."
88 | NotAnInductionScheme s ->
89 let s = if not (String.is_empty s) then s^" " else s in
90 str "Cannot recognize " ++ str s ++ str "an induction scheme."
91 | NotAnInductionSchemeLetIn ->
92 str "Strange letin, cannot recognize an induction scheme."
93 | CannotFindInductiveArgument ->
94 str "Cannot find inductive argument of elimination scheme."
95 | MentionConclusionDependentOn id ->
96 str "Conclusion must be mentioned: it depends on " ++ Id.print id ++ str "."
97 | DontKnowWhatToDoWith id ->
98 str "Do not know what to do with " ++ Miscprint.pr_intro_pattern_naming id
99 | UnsupportedEqnClause ->
100 str "'eqn' clause not supported here."
101 | UnsupportedWithClause ->
102 str "'with' clause not supported here."
103 | UnsupportedInClause b ->
104 str (if b then "'in' clause not supported here."
105 else "'eqn' clause not supported here.")
106 | DontKnowWhereToFindArgument ->
107 str "Don't know where to find some argument."
108 | MultipleAsAndUsingClauseOnlyList ->
109 str "'as' clause with multiple arguments and 'using' clause can only occur last."
110 | _ -> raise Unhandled
111
112 let _ = CErrors.register_handler (wrap_unhandled tactic_interp_error_handler)
113
114 let fresh_id_in_env avoid id env =
115 let avoid' = ids_of_named_context_val (named_context_val env) in
116 let avoid = if Id.Set.is_empty avoid then avoid' else Id.Set.union avoid' avoid in
117 next_ident_away_in_goal (Global.env ()) id avoid
118
119 let new_fresh_id avoid id gl =
120 fresh_id_in_env avoid id (Proofview.Goal.env gl)
121
122 let intros_until_n n =
123 Tactics.try_intros_until (fun _ -> tclIDTAC) (AnonHyp n)
124
125 let try_intros_until_id_check id =
126 Tactics.try_intros_until (fun _ -> tclIDTAC) (NamedHyp (CAst.make id))
127
128 (* Apply a tactic on a quantified hypothesis, an hypothesis in context
129 or a term with bindings *)
130
131 let tactic_infer_flags with_evar = Pretyping.{
132 use_coercions = true;
133 use_typeclasses = UseTC;
134 solve_unification_constraints = true;
135 fail_evar = not with_evar;
136 expand_evars = true;
137 program_mode = false;
138 poly = PolyFlags.default;
139 undeclared_evars_rr = false;
140 unconstrained_sorts = false;
141 }
142
143 let onOpenInductionArg env sigma tac = function
144 | clear_flag,ElimOnConstr f ->
145 let (sigma', cbl) = f env sigma in
146 Tacticals.tclTHEN
147 (Proofview.Unsafe.tclEVARS sigma')
148 (tac clear_flag (Some sigma,cbl))
149 | clear_flag,ElimOnAnonHyp n ->
150 Tacticals.tclTHEN
151 (intros_until_n n)
152 (Tacticals.onLastHyp
153 (fun c ->
154 Proofview.Goal.enter begin fun gl ->
155 let sigma = Proofview.Goal.sigma gl in
156 tac clear_flag (Some sigma,(c,NoBindings))
157 end))
158 | clear_flag,ElimOnIdent {CAst.v=id} ->
159 (* A quantified hypothesis *)
160 Tacticals.tclTHEN
161 (try_intros_until_id_check id)
162 (Proofview.Goal.enter begin fun gl ->
163 let sigma = Proofview.Goal.sigma gl in
164 tac clear_flag (Some sigma,(mkVar id,NoBindings))
165 end)
166
167 let with_no_bindings (c, lbind) =
168 if lbind != NoBindings then error UnsupportedWithClause;
169 c
170
171 (********************************************)
172 (* Elimination tactics *)
173 (********************************************)
174
175 let index_of_ind_arg sigma t =
176 let rec aux i j t = match EConstr.kind sigma t with
177 | LetIn (_, _, _, t) -> aux i j t
178 | Prod (_,t,u) ->
179 (* heuristic *)
180 if isInd sigma (fst (decompose_app sigma t)) then aux (Some j) (j+1) u
181 else aux i (j+1) u
182 | _ -> match i with
183 | Some i -> i
184 | None -> error CannotFindInductiveArgument
185 in aux None 0 t
186
187 (*
188 * Elimination tactic with bindings and using an arbitrary
189 * elimination constant called elimc. This constant should end
190 * with a clause (x:I)(P .. ), where P is a bound variable.
191 * The term c is of type t, which is a product ending with a type
192 * matching I, lbindc are the expected terms for c arguments
193 *)
194
195 type eliminator =
196 | ElimConstant of (Constant.t * EInstance.t)
197 (* Constant generated by a scheme function *)
198 | ElimClause of EConstr.constr with_bindings
199 (* Arbitrary expression provided by the user *)
200
201 let is_nonrec env mind = (Environ.lookup_mind (fst mind) env).mind_finite == Declarations.BiFinite
202
203 let find_ind_eliminator env sigma ind s =
204 let c = Elimschemes.lookup_eliminator env ind s in
205 let sigma, c = EConstr.fresh_global env sigma c in
206 sigma, destConst sigma c
207
208 let clear_wildcards = Tactics.Internal.clear_wildcards
209
210 (*****************************)
211 (* Decomposing introductions *)
212 (*****************************)
213
214 let insert_before decls lasthyp env =
215 match lasthyp with
216 | None -> push_named_context decls env
217 | Some id ->
218 Environ.fold_named_context
219 (fun _ d env ->
220 let d = EConstr.of_named_decl d in
221 let env = if Id.equal id (NamedDecl.get_id d) then push_named_context decls env else env in
222 push_named d env)
223 ~init:(reset_context env) env
224
225 let mk_eq_name env id {CAst.loc;v=ido} =
226 match ido with
227 | IntroAnonymous -> fresh_id_in_env (Id.Set.singleton id) (add_prefix "Heq" id) env
228 | IntroFresh heq_base -> fresh_id_in_env (Id.Set.singleton id) heq_base env
229 | IntroIdentifier id ->
230 if List.mem id (ids_of_named_context (named_context env)) then
231 error (AlreadyUsed id);
232 id
233
234 (* unsafe *)
235
236 let mkletin_goal env sigma with_eq dep (id,lastlhyp,ccl,c) ty =
237 let open Context.Named.Declaration in
238 let t = match ty with Some t -> t | _ -> typ_of env sigma c in
239 let r = Retyping.relevance_of_type env sigma t in
240 let decl = if dep then LocalDef (make_annot id r,c,t)
241 else LocalAssum (make_annot id r,t)
242 in
243 match with_eq with
244 | Some (lr,heq) ->
245 let eqdata = build_rocq_eq_data () in
246 let args = if lr then [mkVar id;c] else [c;mkVar id]in
247 let (sigma, eq) = Evd.fresh_global env sigma eqdata.eq in
248 let refl = mkRef (eqdata.refl, snd (destRef sigma eq)) in
249 (* NB we are not in the right env for [id] so we only check the partial application.
250 This is enough to produce the desired univ constraint between univ of eq and univ of t *)
251 let sigma, eq = Typing.checked_applist env sigma eq [t] in
252 let eq = applist (eq,args) in
253 let refl = applist (refl, [t;mkVar id]) in
254 let newenv = insert_before [LocalAssum (make_annot heq ERelevance.relevant,eq); decl] lastlhyp env in
255 let (sigma, x) = new_evar newenv sigma ccl in
256 (sigma, mkNamedLetIn sigma (make_annot id r) c t
257 (mkNamedLetIn sigma (make_annot heq ERelevance.relevant) refl eq x),
258 Some (fst @@ destEvar sigma x))
259 | None ->
260 let newenv = insert_before [decl] lastlhyp env in
261 let (sigma, x) = new_evar newenv sigma ccl in
262 (sigma, mkNamedLetIn sigma (make_annot id r) c t x, Some (fst @@ destEvar sigma x))
263
264 let warn_cannot_remove_as_expected =
265 CWarnings.create ~name:"cannot-remove-as-expected" ~category:CWarnings.CoreCategories.tactics
266 (fun (id,inglobal) ->
267 let pp = match inglobal with
268 | None -> mt ()
269 | Some ref -> str ", it is used implicitly in " ++ Printer.pr_global ref in
270 str "Cannot remove " ++ Id.print id ++ pp ++ str ".")
271
272 let clear_for_destruct ids =
273 Proofview.tclORELSE
274 (Tactics.Internal.clear_gen (fun env sigma id err inglobal -> raise (ClearDependencyError (id,err,inglobal))) ids)
275 (function
276 | ClearDependencyError (id,err,inglobal),_ -> warn_cannot_remove_as_expected (id,inglobal); Proofview.tclUNIT ()
277 | e -> Exninfo.iraise e)
278
279 (* Either unfold and clear if defined or simply clear if not a definition *)
280 let expand_hyp id =
281 Tacticals.tclTRY (Tactics.unfold_body id) <*> clear_for_destruct [id]
282
283 (*****************************)
284 (* High-level induction *)
285 (*****************************)
286
287 (*
288 * A "natural" induction tactic
289 *
290 - [H0:T0, ..., Hi:Ti, hyp0:P->I(args), Hi+1:Ti+1, ..., Hn:Tn |-G] is the goal
291 - [hyp0] is the induction hypothesis
292 - we extract from [args] the variables which are not rigid parameters
293 of the inductive type, this is [indvars] (other terms are forgotten);
294 - we look for all hyps depending of [hyp0] or one of [indvars]:
295 this is [dephyps] of types [deptyps] respectively
296 - [statuslist] tells for each hyps in [dephyps] after which other hyp
297 fixed in the context they must be moved (when induction is done)
298 - [hyp0succ] is the name of the hyp fixed in the context after which to
299 move the subterms of [hyp0succ] in the i-th branch where it is supposed
300 to be the i-th constructor of the inductive type.
301
302 Strategy: (cf in [induction_with_atomization_of_ind_arg])
303 - requantify and clear all [dephyps]
304 - apply induction on [hyp0]
305 - clear those of [indvars] that are variables and [hyp0]
306 - in the i-th subgoal, intro the arguments of the i-th constructor
307 of the inductive type after [hyp0succ] (done in
308 [induct_discharge]) let the induction hypotheses on top of the
309 hyps because they may depend on variables between [hyp0] and the
310 top. A counterpart is that the dep hyps programmed to be intro-ed
311 on top must now be intro-ed after the induction hypotheses
312 - move each of [dephyps] at the right place following the
313 [statuslist]
314
315 *)
316
317 let warn_unused_intro_pattern =
318 CWarnings.create ~name:"unused-intro-pattern" ~category:CWarnings.CoreCategories.tactics
319 (fun (env,sigma,names) ->
320 strbrk"Unused introduction " ++ str (String.plural (List.length names) "pattern") ++
321 str": " ++ prlist_with_sep spc
322 (Miscprint.pr_intro_pattern
323 (fun c -> Printer.pr_econstr_env env sigma (snd (c env sigma)))) names)
324
325 let check_unused_names env sigma names =
326 if not (List.is_empty names) then
327 warn_unused_intro_pattern (env, sigma, names)
328
329 let intropattern_of_name gl avoid = function
330 | Anonymous -> IntroNaming IntroAnonymous
331 | Name id -> IntroNaming (IntroIdentifier (new_fresh_id avoid id gl))
332
333 let rec consume_pattern avoid na isdep gl = let open CAst in function
334 | [] -> ((CAst.make @@ intropattern_of_name gl avoid na), [])
335 | {loc;v=IntroForthcoming true}::names when not isdep ->
336 consume_pattern avoid na isdep gl names
337 | {loc;v=IntroForthcoming _}::names as fullpat ->
338 (CAst.make ?loc @@ intropattern_of_name gl avoid na, fullpat)
339 | {loc;v=IntroNaming IntroAnonymous}::names ->
340 (CAst.make ?loc @@ intropattern_of_name gl avoid na, names)
341 | {loc;v=IntroNaming (IntroFresh id')}::names ->
342 (CAst.make ?loc @@ IntroNaming (IntroIdentifier (new_fresh_id avoid id' gl)), names)
343 | pat::names -> (pat,names)
344
345 let re_intro_dependent_hypotheses (lstatus,rstatus) (_,tophyp) =
346 let tophyp = match tophyp with None -> MoveLast | Some hyp -> MoveAfter hyp in
347 let newlstatus = (* if some IH has taken place at the top of hyps *)
348 List.map (function (hyp,MoveLast) -> (hyp,tophyp) | x -> x) lstatus
349 in
350 Tacticals.tclTHEN
351 (Tactics.intros_move rstatus)
352 (Tactics.intros_move newlstatus)
353
354 let dest_intro_patterns = Tactics.Internal.dest_intro_patterns
355
356 let safe_dest_intro_patterns with_evars avoid thin dest pat tac =
357 Proofview.tclORELSE
358 (dest_intro_patterns with_evars avoid thin dest pat tac)
359 begin function (e, info) -> match e with
360 | CannotMoveHyp _ ->
361 (* May happen e.g. with "destruct x using s" with an hypothesis
362 which is morally an induction hypothesis to be "MoveLast" if
363 known as such but which is considered instead as a subterm of
364 a constructor to be move at the place of x. *)
365 dest_intro_patterns with_evars avoid thin MoveLast pat tac
366 | e -> Proofview.tclZERO ~info e
367 end
368
369 type elim_arg_kind = RecArg | IndArg | OtherArg
370
371 type branch_argument = {
372 ba_kind : elim_arg_kind;
373 ba_assum : bool;
374 ba_dep : bool;
375 ba_name : Id.t;
376 }
377
378 type recarg_position =
379 | AfterFixedPosition of Id.t option (* None = top of context *)
380
381 let update_dest (recargdests,tophyp as dests) = function
382 | [] -> dests
383 | hyp::_ ->
384 (match recargdests with
385 | AfterFixedPosition None -> AfterFixedPosition (Some hyp)
386 | x -> x),
387 (match tophyp with None -> Some hyp | x -> x)
388
389 let get_recarg_dest (recargdests,tophyp) =
390 match recargdests with
391 | AfterFixedPosition None -> MoveLast
392 | AfterFixedPosition (Some id) -> MoveAfter id
393
394 (* Current policy re-introduces recursive arguments of destructed
395 variable at the place of the original variable while induction
396 hypothesese are introduced at the top of the context. Since in the
397 general case of an inductive scheme, the induction hypotheses can
398 arrive just after the recursive arguments (e.g. as in "forall
399 t1:tree, P t1 -> forall t2:tree, P t2 -> P (node t1 t2)", we need
400 to update the position for t2 after "P t1" is introduced if ever t2
401 had to be introduced at the top of the context).
402 *)
403
404 let induct_discharge with_evars dests avoid' tac (avoid,ra) names =
405 let avoid = Id.Set.union avoid' (Id.Set.union avoid (Tactics.Internal.explicit_intro_names names)) in
406 let rec peel_tac ra dests names thin =
407 match ra with
408 | ({ ba_kind = RecArg } as rarg) :: ({ ba_kind = IndArg } as iarg) :: ra' ->
409 Proofview.Goal.enter begin fun gl ->
410 let (recpat,names) = match names with
411 | [{CAst.loc;v=IntroNaming (IntroIdentifier id)} as pat] ->
412 let id' = new_fresh_id avoid (add_prefix "IH" id) gl in
413 (pat, [CAst.make @@ IntroNaming (IntroIdentifier id')])
414 | _ -> consume_pattern avoid (Name rarg.ba_name) rarg.ba_dep gl names in
415 let dest = get_recarg_dest dests in
416 dest_intro_patterns with_evars avoid thin dest [recpat] (fun ids thin ->
417 Proofview.Goal.enter begin fun gl ->
418 let (hyprec,names) =
419 consume_pattern avoid (Name iarg.ba_name) iarg.ba_dep gl names
420 in
421 dest_intro_patterns with_evars avoid thin MoveLast [hyprec] (fun ids' thin ->
422 peel_tac ra' (update_dest dests ids') names thin)
423 end)
424 end
425 | ({ ba_kind = IndArg } as iarg) :: ra' ->
426 Proofview.Goal.enter begin fun gl ->
427 (* Rem: does not happen in Rocq schemes, only in user-defined schemes *)
428 let pat,names =
429 consume_pattern avoid (Name iarg.ba_name) iarg.ba_dep gl names in
430 dest_intro_patterns with_evars avoid thin MoveLast [pat] (fun ids thin ->
431 peel_tac ra' (update_dest dests ids) names thin)
432 end
433 | ({ ba_kind = RecArg } as rarg) :: ra' ->
434 Proofview.Goal.enter begin fun gl ->
435 let (pat,names) =
436 consume_pattern avoid (Name rarg.ba_name) rarg.ba_dep gl names in
437 let dest = get_recarg_dest dests in
438 dest_intro_patterns with_evars avoid thin dest [pat] (fun ids thin ->
439 peel_tac ra' dests names thin)
440 end
441 | ({ ba_kind = OtherArg } as oarg) :: ra' ->
442 Proofview.Goal.enter begin fun gl ->
443 let (pat,names) = consume_pattern avoid Anonymous oarg.ba_dep gl names in
444 let dest = get_recarg_dest dests in
445 safe_dest_intro_patterns with_evars avoid thin dest [pat] (fun ids thin ->
446 peel_tac ra' dests names thin)
447 end
448 | [] ->
449 Proofview.Goal.enter begin fun gl ->
450 let env = Proofview.Goal.env gl in
451 let sigma = Proofview.Goal.sigma gl in
452 check_unused_names env sigma names;
453 Tacticals.tclTHEN (clear_wildcards thin) (tac dests)
454 end
455 in
456 peel_tac ra (dests, None) names []
457
458 (* - le recalcul de indtyp à chaque itération de atomize_one est pour ne pas
459 s'embêter à regarder si un letin_tac ne fait pas des
460 substitutions aussi sur l'argument voisin *)
461
462 let expand_projections env sigma c =
463 let rec aux env c =
464 match EConstr.kind sigma c with
465 | Proj (p, _, c) -> Retyping.expand_projection env sigma p (aux env c) []
466 | _ -> map_constr_with_full_binders env sigma push_rel aux env c
467 in
468 aux env c
469
470
471 (* Marche pas... faut prendre en compte l'occurrence précise... *)
472
473 let atomize_param_of_ind env sigma (hd, params, indices) =
474 let params' = List.map (expand_projections env sigma) params in
475 (* le gl est important pour ne pas préévaluer *)
476 let rec atomize_one accu args args' avoid = function
477 | [] ->
478 let t = applist (hd, params@args) in
479 (List.rev accu, avoid, t)
480 | c :: rem ->
481 match EConstr.kind sigma c with
482 | Var id when not (List.exists (fun c -> occur_var env sigma id c) args') &&
483 not (List.exists (fun c -> occur_var env sigma id c) params') ->
484 (* Based on the knowledge given by the user, all
485 constraints on the variable are generalizable in the
486 current environment so that it is clearable after destruction *)
487 atomize_one accu (c::args) (c::args') (Id.Set.add id avoid) rem
488 | _ ->
489 let c' = expand_projections env sigma c in
490 let dependent t = dependent sigma c t in
491 if List.exists dependent params' ||
492 List.exists dependent args'
493 then
494 (* This is a case where the argument is constrained in a
495 way which would require some kind of inversion; we
496 follow the (old) discipline of not generalizing over
497 this term, since we don't try to invert the
498 constraint anyway. *)
499 atomize_one accu (c::args) (c'::args') avoid rem
500 else
501 (* We reason blindly on the term and do as if it were
502 generalizable, ignoring the constraints coming from
503 its structure *)
504 let id = match EConstr.kind sigma c with
505 | Var id -> id
506 | _ ->
507 let ty = Retyping.get_type_of env sigma c in
508 id_of_name_using_hdchar env sigma ty Anonymous
509 in
510 let x = fresh_id_in_env avoid id env in
511 let accu = (x, c) :: accu in
512 atomize_one accu (mkVar x::args) (mkVar x::args') (Id.Set.add x avoid) rem
513 in
514 atomize_one [] [] [] Id.Set.empty (List.rev indices)
515
516 (* [cook_sign] builds the lists [beforetoclear] (preceding the
517 ind. var.) and [aftertoclear] (coming after the ind. var.) of hyps
518 that must be erased, the lists of hyps to be generalize [decldeps] on the
519 goal together with the places [(lstatus,rstatus)] where to re-intro
520 them after induction. To know where to re-intro the dep hyp, we
521 remember the name of the hypothesis [lhyp] after which (if the dep
522 hyp is more recent than [hyp0]) or [rhyp] before which (if older
523 than [hyp0]) its equivalent must be moved when the induction has
524 been applied. Since computation of dependencies and [rhyp] is from
525 more ancient (on the right) to more recent hyp (on the left) but
526 the computation of [lhyp] progresses from the other way, [cook_hyp]
527 is in two passes (an alternative would have been to write an
528 higher-order algorithm). We use references to reduce
529 the accumulation of arguments.
530
531 To summarize, the situation looks like this
532
533 Goal(n,x) -| H6:(Q n); x:A; H5:True; H4:(le O n); H3:(P n); H2:True; n:nat
534 Left Right
535
536 Induction hypothesis is H4 ([hyp0])
537 Variable parameters of (le O n) is the singleton list with "n" ([indvars])
538 The dependent hyps are H3 and H6 ([dephyps])
539 For H3 the memorized places are H5 ([lhyp]) and H2 ([rhyp])
540 because these names are among the hyp which are fixed through the induction
541 For H6 the neighbours are None ([lhyp]) and H5 ([rhyp])
542 For H3, because on the right of H4, we remember rhyp (here H2)
543 For H6, because on the left of H4, we remember lhyp (here None)
544 For H4, we remember lhyp (here H5)
545
546 The right neighbour is then translated into the left neighbour
547 because move_hyp tactic needs the name of the hyp _after_ which we
548 move the hyp to move.
549
550 But, say in the 2nd subgoal of the hypotheses, the goal will be
551
552 (m:nat)((P m)->(Q m)->(Goal m)) -> (P Sm)-> (Q Sm)-> (Goal Sm)
553 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
554 both go where H4 was goes where goes where
555 H3 was H6 was
556
557 We have to intro and move m and the recursive hyp first, but then
558 where to move H3 ??? Only the hyp on its right is relevant, but we
559 have to translate it into the name of the hyp on the left
560
561 Note: this case where some hyp(s) in [dephyps] has(have) the same
562 left neighbour as [hyp0] is the only problematic case with right
563 neighbours. For the other cases (e.g. an hyp H1:(R n) between n and H2
564 would have posed no problem. But for uniformity, we decided to use
565 the right hyp for all hyps on the right of H4.
566
567 Other solutions are welcome
568
569 PC 9 fev 06: Adapted to accept multi argument principle with no
570 main arg hyp. hyp0 is now optional, meaning that it is possible
571 that there is no main induction hypotheses. In this case, we
572 consider the last "parameter" (in [indvars]) as the limit between
573 "left" and "right", BUT it must be included in indhyps.
574
575 Other solutions are still welcome
576
577 *)
578
579 exception Shunt of Id.t move_location
580
581 let cook_sign hyp0_opt inhyps indvars env sigma =
582 (* First phase from L to R: get [toclear], [decldep] and [statuslist]
583 for the hypotheses before (= more ancient than) hyp0 (see above) *)
584 let toclear = ref [] in
585 let avoid = ref Id.Set.empty in
586 let decldeps = ref [] in
587 let ldeps = ref [] in
588 let rstatus = ref [] in
589 let lstatus = ref [] in
590 let before = ref true in
591 let maindep = ref false in
592 let seek_deps env decl rhyp =
593 let decl = EConstr.of_named_decl decl in
594 let hyp = NamedDecl.get_id decl in
595 if (match hyp0_opt with Some hyp0 -> Id.equal hyp hyp0 | _ -> false)
596 then begin
597 before:=false;
598 (* Note that if there was no main induction hypotheses, then hyp
599 is one of indvars too *)
600 toclear := hyp::!toclear;
601 MoveFirst (* fake value *)
602 end else if Id.Set.mem hyp indvars then begin
603 (* The variables in indvars are such that they don't occur any
604 more after generalization, so declare them to clear. *)
605 toclear := hyp::!toclear;
606 rhyp
607 end else
608 let dephyp0 = not !before && List.is_empty inhyps &&
609 (Option.cata (fun id -> occur_var_in_decl env sigma id decl) false hyp0_opt)
610 in
611 let depother = List.is_empty inhyps &&
612 (Id.Set.exists (fun id -> occur_var_in_decl env sigma id decl) indvars ||
613 List.exists (fun decl' -> occur_var_in_decl env sigma (NamedDecl.get_id decl') decl) !decldeps)
614 in
615 if not (List.is_empty inhyps) && Id.List.mem hyp inhyps
616 || dephyp0 || depother
617 then begin
618 decldeps := decl::!decldeps;
619 avoid := Id.Set.add hyp !avoid;
620 maindep := dephyp0 || !maindep;
621 if !before then begin
622 toclear := hyp::!toclear;
623 rstatus := (hyp,rhyp)::!rstatus
624 end
625 else begin
626 toclear := hyp::!toclear;
627 ldeps := hyp::!ldeps (* status computed in 2nd phase *)
628 end;
629 MoveBefore hyp end
630 else
631 MoveBefore hyp
632 in
633 let _ = fold_named_context seek_deps env ~init:MoveFirst in
634 (* 2nd phase from R to L: get left hyp of [hyp0] and [lhyps] *)
635 let compute_lstatus lhyp decl =
636 let hyp = NamedDecl.get_id decl in
637 if (match hyp0_opt with Some hyp0 -> Id.equal hyp hyp0 | _ -> false) then
638 raise (Shunt lhyp);
639 if Id.List.mem hyp !ldeps then begin
640 lstatus := (hyp,lhyp)::!lstatus;
641 lhyp
642 end else
643 if Id.List.mem hyp !toclear then lhyp else MoveAfter hyp
644 in
645 try
646 let _ =
647 fold_named_context_reverse compute_lstatus ~init:MoveLast env in
648 raise (Shunt MoveLast) (* ?? FIXME *)
649 with Shunt lhyp0 ->
650 let lhyp0 = match lhyp0 with
651 | MoveLast -> None
652 | MoveAfter hyp -> Some hyp
653 | _ -> assert false in
654 let statuslists = (!lstatus,List.rev !rstatus) in
655 let recargdests = AfterFixedPosition (if Option.is_empty hyp0_opt then None else lhyp0) in
656 (statuslists, recargdests, !toclear, !decldeps, !avoid, !maindep)
657
658 (*
659 The general form of an induction principle is the following:
660
661 forall prm1 prm2 ... prmp, (induction parameters)
662 forall Q1...,(Qi:Ti_1 -> Ti_2 ->...-> Ti_ni),...Qq, (predicates)
663 branch1, branch2, ... , branchr, (branches of the principle)
664 forall (x1:Ti_1) (x2:Ti_2) ... (xni:Ti_ni), (induction arguments)
665 (HI: I prm1..prmp x1...xni) (optional main induction arg)
666 -> (Qi x1...xni HI (f prm1...prmp x1...xni)).(conclusion)
667 ^^ ^^^^^^^^^^^^^^^^^^^^^^^^
668 optional optional argument added if
669 even if HI principle generated by functional
670 present above induction, only if HI does not exist
671 [indarg] [farg]
672
673 HI is not present when the induction principle does not come directly from an
674 inductive type (like when it is generated by functional induction for
675 example). HI is present otherwise BUT may not appear in the conclusion
676 (dependent principle). HI and (f...) cannot be both present.
677
678 Principles taken from functional induction have the final (f...).*)
679
680 (* [rel_contexts] and [rel_declaration] actually contain triples, and
681 lists are actually in reverse order to fit [compose_prod]. *)
682 type elim_scheme = {
683 elimt: types;
684 indref: GlobRef.t option;
685 params: rel_context; (* (prm1,tprm1);(prm2,tprm2)...(prmp,tprmp) *)
686 nparams: int; (* number of parameters *)
687 predicates: rel_context; (* (Qq, (Tq_1 -> Tq_2 ->...-> Tq_nq)), (Q1,...) *)
688 npredicates: int; (* Number of predicates *)
689 branches: rel_context; (* branchr,...,branch1 *)
690 nbranches: int; (* Number of branches *)
691 args: rel_context; (* (xni, Ti_ni) ... (x1, Ti_1) *)
692 nargs: int; (* number of arguments *)
693 indarg: rel_declaration option; (* Some (H,I prm1..prmp x1...xni)
694 if HI is in premisses, None otherwise *)
695 concl: types; (* Qi x1...xni HI (f...), HI and (f...)
696 are optional and mutually exclusive *)
697 indarg_in_concl: bool; (* true if HI appears at the end of conclusion *)
698 farg_in_concl: bool; (* true if (f...) appears at the end of conclusion *)
699 }
700
701 let empty_scheme =
702 {
703 elimt = mkProp;
704 indref = None;
705 params = [];
706 nparams = 0;
707 predicates = [];
708 npredicates = 0;
709 branches = [];
710 nbranches = 0;
711 args = [];
712 nargs = 0;
713 indarg = None;
714 concl = mkProp;
715 indarg_in_concl = false;
716 farg_in_concl = false;
717 }
718
719 let make_base n id =
720 if Int.equal n 0 || Int.equal n 1 then id
721 else
722 (* This extends the name to accept new digits if it already ends with *)
723 (* digits *)
724 Id.of_string (atompart_of_id (make_ident (Id.to_string id) (Some 0)))
725
726 (* Builds two different names from an optional inductive type and a
727 number, also deals with a list of names to avoid. If the inductive
728 type is None, then hyprecname is IHi where i is a number. *)
729 let make_up_names n ind_opt cname =
730 let is_hyp = String.equal (atompart_of_id cname) "H" in
731 let base = Id.to_string (make_base n cname) in
732 let ind_prefix = "IH" in
733 let base_ind =
734 if is_hyp then
735 match ind_opt with
736 | None -> Id.of_string ind_prefix
737 | Some ind_id -> add_prefix ind_prefix (Nametab.basename_of_global ind_id)
738 else add_prefix ind_prefix cname in
739 let hyprecname = make_base n base_ind in
740 let avoid =
741 if Int.equal n 1 (* Only one recursive argument *) || Int.equal n 0 then Id.Set.empty
742 else
743 (* Forbid to use cname, cname0, hyprecname and hyprecname0 *)
744 (* in order to get names such as f1, f2, ... *)
745 let avoid =
746 Id.Set.add (make_ident (Id.to_string hyprecname) None)
747 (Id.Set.singleton (make_ident (Id.to_string hyprecname) (Some 0))) in
748 if not (String.equal (atompart_of_id cname) "H") then
749 Id.Set.add (make_ident base (Some 0)) (Id.Set.add (make_ident base None) avoid)
750 else avoid in
751 Id.of_string base, hyprecname, avoid
752
753 let error_ind_scheme s = error (NotAnInductionScheme s)
754
755 let occur_rel sigma n c =
756 let res = not (noccurn sigma n c) in
757 res
758
759 (* This function splits the products of the induction scheme [elimt] into four
760 parts:
761 - branches, easily detectable (they are not referred by rels in the subterm)
762 - what was found before branches (acc1) that is: parameters and predicates
763 - what was found after branches (acc3) that is: args and indarg if any
764 if there is no branch, we try to fill in acc3 with args/indargs.
765 We also return the conclusion.
766 *)
767 let decompose_paramspred_branch_args sigma elimt =
768 let open Context.Rel.Declaration in
769 let rec cut_noccur elimt acc2 =
770 match EConstr.kind sigma elimt with
771 | Prod(nme,tpe,elimt') ->
772 let hd_tpe,_ = decompose_app sigma (snd (decompose_prod_decls sigma tpe)) in
773 if not (occur_rel sigma 1 elimt') && isRel sigma hd_tpe
774 then cut_noccur elimt' (LocalAssum (nme,tpe)::acc2)
775 else let acc3,ccl = decompose_prod_decls sigma elimt in acc2 , acc3 , ccl
776 | App(_, _) | Rel _ -> acc2 , [] , elimt
777 | _ -> error_ind_scheme "" in
778 let rec cut_occur elimt acc1 =
779 match EConstr.kind sigma elimt with
780 | Prod(nme,tpe,c) when occur_rel sigma 1 c -> cut_occur c (LocalAssum (nme,tpe)::acc1)
781 | Prod(nme,tpe,c) -> let acc2,acc3,ccl = cut_noccur elimt [] in acc1,acc2,acc3,ccl
782 | App(_, _) | Rel _ -> acc1,[],[],elimt
783 | _ -> error_ind_scheme "" in
784 let acc1, acc2 , acc3, ccl = cut_occur elimt [] in
785 (* Particular treatment when dealing with a dependent empty type elim scheme:
786 if there is no branch, then acc1 contains all hyps which is wrong (acc1
787 should contain parameters and predicate only). This happens for an empty
788 type (See for example Empty_set_ind, as False would actually be ok). Then
789 we must find the predicate of the conclusion to separate params_pred from
790 args. We suppose there is only one predicate here. *)
791 match acc2 with
792 | [] ->
793 let hyps,ccl = decompose_prod_decls sigma elimt in
794 let hd_ccl_pred,_ = decompose_app sigma ccl in
795 begin match EConstr.kind sigma hd_ccl_pred with
796 | Rel i -> let acc3,acc1 = List.chop (i-1) hyps in acc1 , [] , acc3 , ccl
797 | _ -> error_ind_scheme ""
798 end
799 | _ -> acc1, acc2 , acc3, ccl
800
801
802 let exchange_hd_app sigma subst_hd t =
803 let hd,args= decompose_app sigma t in mkApp (subst_hd, args)
804
805 (* Builds an elim_scheme from its type and calling form (const+binding). We
806 first separate branches. We obtain branches, hyps before (params + preds),
807 hyps after (args <+ indarg if present>) and conclusion. Then we proceed as
808 follows:
809
810 - separate parameters and predicates in params_preds. For that we build:
811 forall (x1:Ti_1)(xni:Ti_ni) (HI:I prm1..prmp x1...xni), DUMMY x1...xni HI/farg
812 ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^
813 optional opt
814 Free rels appearing in this term are parameters (branches should not
815 appear, and the only predicate would have been Qi but we replaced it by
816 DUMMY). We guess this heuristic catches all params. TODO: generalize to
817 the case where args are merged with branches (?) and/or where several
818 predicates are cited in the conclusion.
819
820 - finish to fill in the elim_scheme: indarg/farg/args and finally indref. *)
821 let compute_elim_sig sigma elimt =
822 let open Context.Rel.Declaration in
823 let params_preds,branches,args_indargs,conclusion =
824 decompose_paramspred_branch_args sigma elimt in
825
826 let ccl = exchange_hd_app sigma (mkVar (Id.of_string "__QI_DUMMY__")) conclusion in
827 let concl_with_args = it_mkProd_or_LetIn ccl args_indargs in
828 let nparams = Int.Set.cardinal (free_rels sigma concl_with_args) in
829 let preds,params = List.chop (List.length params_preds - nparams) params_preds in
830
831 (* A first approximation, further analysis will tweak it *)
832 let res = { empty_scheme with
833 (* This fields are ok: *)
834 elimt = elimt; concl = conclusion;
835 predicates = preds; npredicates = List.length preds;
836 branches = branches; nbranches = List.length branches;
837 farg_in_concl = isApp sigma ccl && isApp sigma (last_arg sigma ccl);
838 params = params; nparams = nparams;
839 (* all other fields are unsure at this point. Including these:*)
840 args = args_indargs; nargs = List.length args_indargs; } in
841 (* 1- First see if (f x...) is in the conclusion. *)
842 if res.farg_in_concl then res
843 (* 2- If no args_indargs (=!res.nargs at this point) then no indarg *)
844 else if Int.equal res.nargs 0 then res
845 (* 3- Look at last arg: is it the indarg? *)
846 else match List.hd args_indargs with
847 | LocalDef (hiname, _, hi) -> error_ind_scheme ""
848 | LocalAssum (hiname, hi) ->
849 let hi_ind, hi_args = decompose_app sigma hi in
850 (* has hi a globalizable type? *)
851 let hi_is_ind = match EConstr.kind sigma hi_ind with
852 | Ind _ | Var _ | Const _ | Construct _ -> true
853 | _ -> false
854 in
855 (* has hi the right number of arguments? *)
856 let hi_args_enough = Int.equal (Array.length hi_args) (List.length params + res.nargs -1) in
857 (* FIXME: these two tests are not enough *)
858 if not (hi_is_ind && hi_args_enough) then res (* No indarg *)
859 else
860 let ind, indarg, args = match res.args with
861 | [] -> failwith "hd"
862 | LocalDef _ :: _ -> error_ind_scheme ""
863 | LocalAssum (_, ind) as indarg :: args -> ind, indarg, args
864 in
865 let indhd, indargs = decompose_app sigma ind in
866 let indref = match destRef sigma indhd with
867 | (indref, _) -> indref
868 | exception DestKO -> error CannotFindInductiveArgument
869 in
870 { res with
871 indarg = Some indarg;
872 indarg_in_concl = occur_rel sigma 1 ccl;
873 args = args; nargs = res.nargs - 1;
874 indref = Some indref;
875 }
876
877 let compute_scheme_signature evd scheme names_info ind_type_guess =
878 let open Context.Rel.Declaration in
879 let f,l = decompose_app evd scheme.concl in
880 (* Vérifier que les arguments de Qi sont bien les xi. *)
881 let cond, check_concl =
882 match scheme.indarg with
883 | Some (LocalDef _) -> error NotAnInductionSchemeLetIn
884 | None -> (* Non standard scheme *)
885 let cond hd = EConstr.eq_constr evd hd ind_type_guess && not scheme.farg_in_concl
886 in (cond, fun _ _ -> ())
887 | Some (LocalAssum (_,ind)) -> (* Standard scheme from an inductive type *)
888 let indhd,indargs = decompose_app_list evd ind in
889 let cond hd = EConstr.eq_constr evd hd indhd in
890 let check_concl is_pred p =
891 (* Check again conclusion *)
892 let ccl_arg_ok = is_pred (p + scheme.nargs + 1) f == IndArg in
893 let ind_is_ok =
894 List.equal (fun c1 c2 -> EConstr.eq_constr evd c1 c2)
895 (List.lastn scheme.nargs indargs)
896 (Context.Rel.instance_list mkRel 0 scheme.args) in
897 if not (ccl_arg_ok && ind_is_ok) then
898 error_ind_scheme "the conclusion of"
899 in (cond, check_concl)
900 in
901 let is_pred n c =
902 let hd = fst (decompose_app evd c) in
903 match EConstr.kind evd hd with
904 | Rel q when n < q && q <= n+scheme.npredicates -> IndArg
905 | _ when cond hd -> RecArg
906 | _ -> OtherArg
907 in
908 let rec check_branch p c =
909 match EConstr.kind evd c with
910 | Prod (_,t,c) ->
911 (is_pred p t, true, not (Vars.noccurn evd 1 c)) :: check_branch (p+1) c
912 | LetIn (_,_,_,c) ->
913 (OtherArg, false, not (Vars.noccurn evd 1 c)) :: check_branch (p+1) c
914 | _ when is_pred p c == IndArg -> []
915 | _ -> raise_notrace Exit
916 in
917 let rec find_branches p lbrch =
918 match lbrch with
919 | LocalAssum (_,t) :: brs ->
920 begin match check_branch p t with
921 | lchck_brch ->
922 let n = List.count (fun (b, _, _) -> b == RecArg) lchck_brch in
923 let recvarname, hyprecname, avoid = make_up_names n scheme.indref names_info in
924 let map (b, is_assum, dep) = {
925 ba_kind = b;
926 ba_assum = is_assum;
927 ba_dep = dep;
928 ba_name = if b == IndArg then hyprecname else recvarname;
929 } in
930 let namesign = List.map map lchck_brch in
931 (avoid, namesign) :: find_branches (p+1) brs
932 | exception Exit -> error_ind_scheme "the branches of"
933 end
934 | LocalDef _ :: _ -> error_ind_scheme "the branches of"
935 | [] -> check_concl is_pred p; []
936 in
937 Array.of_list (find_branches 0 (List.rev scheme.branches))
938
939 let compute_case_signature env mind dep names_info =
940 let indref = GlobRef.IndRef mind in
941 let rec check_branch c = match Constr.kind c with
942 | Prod (_,t,c) ->
943 let hd, _ = Constr.decompose_app t in
944 (* no recursive call in case analysis *)
945 let arg = match Constr.kind hd with
946 | Ind (ind, _) when Environ.QInd.equal env mind ind -> RecArg
947 | _ -> OtherArg
948 in
949 (arg, true, not (CVars.noccurn 1 c)) :: check_branch c
950 | LetIn (_,_,_,c) ->
951 (OtherArg, false, not (CVars.noccurn 1 c)) :: check_branch c
952 | _ -> []
953 in
954 let (mib, mip) = Inductive.lookup_mind_specif env mind in
955 let find_branches k =
956 let (ctx, typ) = mip.mind_nf_lc.(k) in
957 let argctx = List.firstn mip.mind_consnrealdecls.(k) ctx in
958 let _, args = Constr.decompose_app typ in
959 let _, indices = Array.chop mib.mind_nparams args in
960 let base =
961 if dep then Array.append indices (Context.Rel.instance Constr.mkRel 0 argctx)
962 else indices
963 in
964 let base = Constr.mkApp (Constr.mkProp, base) in (* only used for noccurn *)
965 let lchck_brch = check_branch (Term.it_mkProd_or_LetIn base argctx) in
966 let n = List.count (fun (b, _, _) -> b == RecArg) lchck_brch in
967 let recvarname, hyprecname, avoid = make_up_names n (Some indref) names_info in
968 let map (b, is_assum, dep) = {
969 ba_kind = b;
970 ba_assum = is_assum;
971 ba_dep = dep;
972 ba_name = recvarname;
973 } in
974 let namesign = List.map map lchck_brch in
975 (avoid, namesign)
976 in
977 Array.init (Array.length mip.mind_consnames) find_branches
978
979 let guess_elim_shape env sigma isrec s hyp0 =
980 let tmptyp0 = Typing.type_of_variable env hyp0 in
981 let (mind, u), typ = Tacred.reduce_to_atomic_ind env sigma tmptyp0 in
982 let is_elim = isrec && not (is_nonrec env mind) in
983 let nparams =
984 let mib = Environ.lookup_mind (fst mind) env in
985 if is_elim then mib.mind_nparams_rec
986 else mib.mind_nparams
987 in
988 let hd, args = decompose_app_list sigma typ in
989 let (params, indices) = List.chop nparams args in
990 is_elim, (mind, u), (hd, params, indices)
991
992 let given_elim env sigma hyp0 (elimc,lbind as e) =
993 let tmptyp0 = Typing.type_of_variable env hyp0 in
994 let ind_type_guess,_ = decompose_app sigma (snd (decompose_prod sigma tmptyp0)) in
995 let sigma, elimt = Typing.type_of env sigma elimc in
996 sigma, (e, elimt), ind_type_guess
997
998 type scheme_signature = (Id.Set.t * branch_argument list) array
999
1000 type eliminator_source =
1001 | CaseOver of Id.t * (inductive * EInstance.t)
1002 | ElimOver of Id.t * (inductive * EInstance.t)
1003 | ElimUsing of Id.t * (Evd.econstr with_bindings * EConstr.types * scheme_signature)
1004 | ElimUsingList of (Evd.econstr with_bindings * EConstr.types * scheme_signature) * Id.t list * Id.t list * EConstr.t list
1005
1006 let find_induction_type env sigma isrec elim hyp0 sort = match elim with
1007 | None ->
1008 let is_elim, ind, typ = guess_elim_shape env sigma isrec sort hyp0 in
1009 (* We drop the scheme and elimc/elimt waiting to know if it is dependent, this
1010 needs no update to sigma at this point. *)
1011 let elim = if is_elim then ElimOver (hyp0, ind) else CaseOver (hyp0, ind) in
1012 sigma, typ, elim
1013 | Some (elimc, lbind as e) ->
1014 let sigma, elimt = Typing.type_of env sigma elimc in
1015 let scheme = compute_elim_sig sigma elimt in
1016 let () = if Option.is_empty scheme.indarg then error CannotFindInductiveArgument in
1017 let ref = match scheme.indref with
1018 | None -> error_ind_scheme ""
1019 | Some ref -> ref
1020 in
1021 let tmptyp0 = Typing.type_of_variable env hyp0 in
1022 let indtyp = reduce_to_atomic_ref env sigma ref tmptyp0 in
1023 let hd, args = decompose_app_list sigma indtyp in
1024 let indsign = compute_scheme_signature sigma scheme hyp0 hd in
1025 let (params, indices) = List.chop scheme.nparams args in
1026 sigma, (hd, params, indices), ElimUsing (hyp0, (e, elimt, indsign))
1027
1028 let is_functional_induction env sigma elimc =
1029 let scheme = compute_elim_sig sigma (Retyping.get_type_of env sigma (fst elimc)) in
1030 (* The test is not safe: with non-functional induction on non-standard
1031 induction scheme, this may fail *)
1032 Option.is_empty scheme.indarg
1033
1034 (* Instantiate all meta variables of elimclause using lid, some elts
1035 of lid are parameters (first ones), the other are
1036 arguments. Returns the clause obtained. *)
1037 let recolle_clenv i params args elimclause gl = (* XXX don't take a gl *)
1038 let lindmv = Array.of_list (clenv_arguments elimclause) in
1039 let k = match i with None -> Array.length lindmv - List.length args | Some i -> i in
1040 (* parameters correspond to first elts of lid. *)
1041 let clauses_params = List.mapi (fun i id -> id, lindmv.(i)) params in
1042 let clauses_args = List.mapi (fun i id -> id, lindmv.(k+i)) args in
1043 let clauses = clauses_params@clauses_args in
1044 (* iteration of clenv_instantiate with all infos we have. *)
1045 List.fold_right
1046 (fun e acc ->
1047 let x, i = e in
1048 let y = Tacmach.pf_get_hyp_typ x gl in
1049 let elimclause' = clenv_instantiate i acc (mkVar x, y) in
1050 elimclause')
1051 (List.rev clauses)
1052 elimclause
1053
1054 (* Unification of the goal and the principle applied to meta variables:
1055 (elimc ?i ?j ?k...?l). This solves partly meta variables (and may
1056 produce new ones). Then refine with the resulting term with holes.
1057 *)
1058 let induction_tac with_evars params indvars (elim, elimt) =
1059 Proofview.Goal.enter begin fun gl ->
1060 let env = Proofview.Goal.env gl in
1061 let sigma = Proofview.Goal.sigma gl in
1062 let clause, bindings, index = match elim with
1063 | ElimConstant c ->
1064 let i = index_of_ind_arg sigma elimt in
1065 (mkConstU c, elimt), NoBindings, Some i
1066 | ElimClause (elimc, lbindelimc) ->
1067 (elimc, elimt), lbindelimc, None
1068 in
1069 (* elimclause contains this: (elimc ?i ?j ?k...?l) *)
1070 let elimclause = make_clenv_binding env sigma clause bindings in
1071 (* elimclause' is built from elimclause by instantiating all args and params. *)
1072 let elimclause = recolle_clenv index params indvars elimclause gl in
1073 Clenv.res_pf ~with_evars ~flags:(elim_flags ()) elimclause
1074 end
1075
1076 let destruct_tac with_evars indvar dep =
1077 Proofview.Goal.enter begin fun gl ->
1078 let env = Proofview.Goal.env gl in
1079 let ty = Typing.type_of_variable env indvar in
1080 Clenv.case_pf ~with_evars ~dep (mkVar indvar, ty)
1081 end
1082
1083 (* Apply induction "in place" taking into account dependent
1084 hypotheses from the context, replacing the main hypothesis on which
1085 induction applies with the induction hypotheses *)
1086
1087 let apply_induction_in_context with_evars inhyps elim indvars names =
1088 Proofview.Goal.enter begin fun gl ->
1089 let sigma = Proofview.Goal.sigma gl in
1090 let env = Proofview.Goal.env gl in
1091 let concl = Proofview.Goal.concl gl in
1092 let hyp0 = match elim with
1093 | ElimUsing (hyp0, _) | ElimOver (hyp0, _) | CaseOver (hyp0, _) -> Some hyp0
1094 | ElimUsingList _ -> None
1095 in
1096 let statuslists,lhyp0,toclear,deps,avoid,dep_in_hyps = cook_sign hyp0 inhyps indvars env sigma in
1097 let tmpcl = it_mkNamedProd_or_LetIn sigma concl deps in
1098 let s = Retyping.get_sort_quality_of env sigma tmpcl in
1099 let deps_cstr =
1100 List.fold_left
1101 (fun a decl -> if NamedDecl.is_local_assum decl then (mkVar (NamedDecl.get_id decl))::a else a) [] deps in
1102 (* Wait the last moment to guess the eliminator so as to know if we
1103 need a dependent one or not *)
1104 let (sigma, isrec, induct_tac, indsign) = match elim with
1105 | CaseOver (id, (mind, u)) ->
1106 let dep_in_concl = occur_var env sigma id concl in
1107 let dep = dep_in_hyps || dep_in_concl in
1108 let dep = dep || default_case_analysis_dependence env mind in
1109 let indsign = compute_case_signature env mind dep id in
1110 let tac = destruct_tac with_evars id dep in
1111 sigma, false, tac, indsign
1112 | ElimOver (id, (mind, u)) ->
1113 let sigma, ind = find_ind_eliminator env sigma mind s in
1114 let elimt = Retyping.get_type_of env sigma (mkConstU ind) in
1115 let scheme = compute_elim_sig sigma elimt in
1116 let indsign = compute_scheme_signature sigma scheme id (mkIndU (mind, u)) in
1117 let tac = induction_tac with_evars [] [id] (ElimConstant ind, elimt) in
1118 sigma, true, tac, indsign
1119 | ElimUsing (hyp0, (elim, elimt, indsign)) ->
1120 let tac = induction_tac with_evars [] [hyp0] (ElimClause elim, elimt) in
1121 sigma, (* bugged, should be computed *) true, tac, indsign
1122 | ElimUsingList ((elim, elimt, indsign), params, realindvars, patts) ->
1123 let tac = Tacticals.tclTHENLIST [
1124 (* pattern to make the predicate appear. *)
1125 Tactics.reduce (Pattern (List.map inj_with_occurrences patts)) onConcl;
1126 (* Induction by "refine (indscheme ?i ?j ?k...)" + resolution of all
1127 possible holes using arguments given by the user (but the
1128 functional one). *)
1129 (* FIXME: Tester ca avec un principe dependant et non-dependant *)
1130 induction_tac with_evars params realindvars (ElimClause elim, elimt);
1131 ] in
1132 sigma, (* bugged, should be computed *) true, tac, indsign
1133 in
1134 let branchletsigns =
1135 let f ba = ba.ba_assum in
1136 Array.map (fun (_,l) -> List.map f l) indsign in
1137 let names = compute_induction_names true branchletsigns names in
1138 let () = Array.iter (Tactics.Internal.check_name_unicity env toclear []) names in
1139 let tac =
1140 (if isrec then Tacticals.tclTHENFIRSTn else Tacticals.tclTHENLASTn)
1141 (Tacticals.tclTHENLIST [
1142 (* Generalize dependent hyps (but not args) *)
1143 if deps = [] then Proofview.tclUNIT () else Tactics.apply_type ~typecheck:false tmpcl deps_cstr;
1144 (* side-conditions in elim (resp case) schemes come last (resp first) *)
1145 induct_tac;
1146 Tacticals.tclMAP expand_hyp toclear;
1147 ])
1148 (Array.map2
1149 (induct_discharge with_evars lhyp0 avoid
1150 (re_intro_dependent_hypotheses statuslists))
1151 indsign names)
1152 in
1153 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) tac
1154 end
1155
1156 let induction_with_atomization_of_ind_arg isrec with_evars elim names hyp0 inhyps =
1157 Proofview.Goal.enter begin fun gl ->
1158 let env = Proofview.Goal.env gl in
1159 let sigma = Proofview.Goal.sigma gl in
1160 let concl = Proofview.Goal.concl gl in
1161 let sort = Retyping.get_sort_quality_of env sigma concl in
1162 let sigma, ty, elim_info = find_induction_type env sigma isrec elim hyp0 sort in
1163 let letins, avoid, t = atomize_param_of_ind env sigma ty in
1164 let letins = tclMAP (fun (na, c) -> Tactics.letin_tac None (Name na) c None allHypsAndConcl) letins in
1165 Tacticals.tclTHENLIST [
1166 Proofview.Unsafe.tclEVARS sigma;
1167 letins;
1168 Tactics.change_in_hyp ~check:false None (Tactics.make_change_arg t) (hyp0, InHypTypeOnly);
1169 apply_induction_in_context with_evars inhyps elim_info avoid names
1170 ]
1171 end
1172
1173 let msg_not_right_number_induction_arguments scheme =
1174 str"Not the right number of induction arguments (expected " ++
1175 pr_enum (fun x -> x) [
1176 if scheme.farg_in_concl then str "the function name" else mt();
1177 if scheme.nparams != 0 then int scheme.nparams ++ str (String.plural scheme.nparams " parameter") else mt ();
1178 if scheme.nargs != 0 then int scheme.nargs ++ str (String.plural scheme.nargs " argument") else mt ()] ++ str ")."
1179
1180 (* Induction on a list of induction arguments. Analyze the elim
1181 scheme (which is mandatory for multiple ind args), check that all
1182 parameters and arguments are given (mandatory too).
1183 Main differences with induction_from_context is that there is no
1184 main induction argument. On the other hand, all args and params
1185 must be given, so we help a bit the unifier by making the "pattern"
1186 by hand before calling induction_tac *)
1187 let induction_without_atomization isrec with_evars elim names lid =
1188 Proofview.Goal.enter begin fun gl ->
1189 let env = Proofview.Goal.env gl in
1190 let sigma = Proofview.Goal.sigma gl in
1191 let hyp0 = List.hd lid in
1192 (* Check that the elimination scheme has a form similar to the
1193 elimination schemes built by Rocq. Schemes may have the standard
1194 form computed from an inductive type OR (feb. 2006) a non standard
1195 form. That is: with no main induction argument and with an optional
1196 extra final argument of the form (f x y ...) in the conclusion. In
1197 the non standard case, naming of generated hypos is slightly
1198 different. *)
1199 let (sigma, (elimc, elimt), ind_type_guess) = given_elim env sigma hyp0 elim in
1200 let scheme = compute_elim_sig sigma elimt in
1201 let indsign = compute_scheme_signature sigma scheme hyp0 ind_type_guess in
1202 let nargs_indarg_farg =
1203 scheme.nargs + (if scheme.farg_in_concl then 1 else 0) in
1204 if not (Int.equal (List.length lid) (scheme.nparams + nargs_indarg_farg))
1205 then
1206 let info = Exninfo.reify () in
1207 Tacticals.tclZEROMSG ~info (msg_not_right_number_induction_arguments scheme)
1208 else
1209 let indvars,lid_params = List.chop nargs_indarg_farg lid in
1210 (* terms to patternify we must patternify indarg or farg if present in concl *)
1211 let realindvars = List.rev (if scheme.farg_in_concl then List.tl indvars else indvars) in
1212 let lidcstr = List.map mkVar (List.rev indvars) in
1213 let params = List.rev lid_params in
1214 let indvars =
1215 (* Temporary hack for compatibility, while waiting for better
1216 analysis of the form of induction schemes: a scheme like
1217 gt_wf_rec was taken as a functional scheme with no parameters,
1218 but by chance, because of the addition of at least hyp0 for
1219 cook_sign, it behaved as if there was a real induction arg. *)
1220 if List.is_empty indvars then Id.Set.singleton (List.hd lid_params) else Id.Set.of_list indvars in
1221 let elim = ElimUsingList ((elimc, scheme.elimt, indsign), params, realindvars, lidcstr) in
1222 apply_induction_in_context with_evars [] elim indvars names
1223 end
1224
1225 (* assume that no occurrences are selected *)
1226 let clear_unselected_context id inhyps cls =
1227 Proofview.Goal.enter begin fun gl ->
1228 let env = Proofview.Goal.env gl in
1229 let sigma = Proofview.Goal.sigma gl in
1230 let concl = Proofview.Goal.concl gl in
1231 if occur_var env sigma id concl && cls.concl_occs == NoOccurrences
1232 then error (MentionConclusionDependentOn id);
1233 match cls.onhyps with
1234 | Some hyps ->
1235 let to_erase d =
1236 let id' = NamedDecl.get_id d in
1237 if Id.List.mem id' inhyps then (* if selected, do not erase *) None
1238 else
1239 (* erase if not selected and dependent on id or selected hyps *)
1240 let test id = occur_var_in_decl env sigma id d in
1241 if List.exists test (id::inhyps) then Some id' else None in
1242 let ids = List.map_filter to_erase (Proofview.Goal.hyps gl) in
1243 clear ids
1244 | None -> Proofview.tclUNIT ()
1245 end
1246
1247 let use_bindings env sigma elim must_be_closed (c,lbind) typ =
1248 let typ =
1249 (* Normalize [typ] until the induction reference becomes plainly visible *)
1250 match elim with
1251 | None ->
1252 (* w/o an scheme, the term has to be applied at least until
1253 obtaining an inductive type (even though the arity might be
1254 known only by pattern-matching, as in the case of a term of
1255 the form "nat_rect ?A ?o ?s n", with ?A to be inferred by
1256 matching. *)
1257 let sign,t = whd_decompose_prod env sigma typ in it_mkProd t sign
1258 | Some (elimc, _) ->
1259 (* Otherwise, we compute the induction reference of the scheme
1260 and go looking for that. *)
1261 let sigma, elimt = Typing.type_of env sigma elimc in
1262 let scheme = compute_elim_sig sigma elimt in
1263 match scheme.indref with
1264 | None -> error CannotFindInductiveArgument
1265 | Some indref ->
1266 Tacred.reduce_to_quantified_ref ~allow_failure:true env sigma indref typ
1267 in
1268 let rec find_clause typ =
1269 try
1270 let indclause = make_clenv_binding env sigma (c,typ) lbind in
1271 if must_be_closed && occur_meta (clenv_evd indclause) (clenv_value indclause) then
1272 error NeedFullyAppliedArgument;
1273 (* We lose the possibility of coercions in with-bindings *)
1274 let metas = Clenv.clenv_meta_list indclause in
1275 let sigma, metas, term = pose_all_metas_as_evars ~metas env (clenv_evd indclause) (clenv_value indclause) in
1276 let sigma, metas, typ = pose_all_metas_as_evars ~metas env sigma (clenv_type indclause) in
1277 sigma, term, typ
1278 with e when noncritical e ->
1279 match red_product env sigma typ with
1280 | None -> raise e
1281 | Some typ -> find_clause typ
1282 in
1283 find_clause typ
1284
1285 let check_expected_type env sigma (elimc,bl) elimt =
1286 (* Compute the expected template type of the term in case a using
1287 clause is given *)
1288 let sign,_ = whd_decompose_prod env sigma elimt in
1289 let n = List.length sign in
1290 if n == 0 then error SchemeDontApply;
1291 let sigma,cl = EClause.make_evar_clause env sigma ~len:(n - 1) elimt in
1292 let sigma = EClause.solve_evar_clause env sigma true cl bl in
1293 let (_,u,_) = destProd sigma (whd_all env sigma cl.cl_concl) in
1294 fun t -> match Evarconv.unify_leq_delay env sigma t u with
1295 | _sigma -> true
1296 | exception Evarconv.UnableToUnify _ -> false
1297
1298 let check_enough_applied env sigma elim =
1299 (* A heuristic to decide whether the induction arg is enough applied *)
1300 match elim with
1301 | None ->
1302 (* No eliminator given *)
1303 fun u ->
1304 let t,_ = decompose_app sigma (whd_all env sigma u) in isInd sigma t
1305 | Some elimc ->
1306 let elimt = Retyping.get_type_of env sigma (fst elimc) in
1307 let scheme = compute_elim_sig sigma elimt in
1308 match scheme.indref with
1309 | None ->
1310 (* in the absence of information, do not assume it may be
1311 partially applied *)
1312 fun _ -> true
1313 | Some _ ->
1314 (* Last argument is supposed to be the induction argument *)
1315 check_expected_type env sigma elimc elimt
1316
1317 let guard_no_unifiable = Proofview.guard_no_unifiable >>= function
1318 | None -> Proofview.tclUNIT ()
1319 | Some l ->
1320 Proofview.tclENV >>= function env ->
1321 Proofview.tclEVARMAP >>= function sigma ->
1322 let info = Exninfo.reify () in
1323 Proofview.tclZERO ~info (RefinerError (env, sigma, UnresolvedBindings l))
1324
1325 let pose_induction_arg_then isrec with_evars (is_arg_pure_hyp,from_prefix) elim
1326 id ((pending,(c0,lbind)),(eqname,names)) t0 inhyps cls tac =
1327 Proofview.Goal.enter begin fun gl ->
1328 let sigma = Proofview.Goal.sigma gl in
1329 let env = Proofview.Goal.env gl in
1330 let ccl = Proofview.Goal.concl gl in
1331 let check = check_enough_applied env sigma elim in
1332 let sigma', c, _ = use_bindings env sigma elim false (c0,lbind) t0 in
1333 let abs = AbstractPattern (from_prefix,check,Name id,(pending,c),cls) in
1334 let (id,sign,_,lastlhyp,ccl,res) = make_abstraction env sigma' ccl abs in
1335 match res with
1336 | None ->
1337 (* pattern not found *)
1338 let with_eq = Option.map (fun eq -> (false,mk_eq_name env id eq)) eqname in
1339 let inhyps = if List.is_empty inhyps then inhyps else Option.fold_left (fun inhyps (_,heq) -> heq::inhyps) inhyps with_eq in
1340 (* we restart using bindings after having tried type-class
1341 resolution etc. on the term given by the user *)
1342 let flags = tactic_infer_flags (with_evars && (* do not give a success semantics to edestruct on an open term yet *) false) in
1343 let (sigma, c0) = finish_evar_resolution ~flags env sigma (pending,c0) in
1344 let tac =
1345 (if isrec then
1346 (* Historically, induction has side conditions last *)
1347 Tacticals.tclTHENFIRST
1348 else
1349 (* and destruct has side conditions first *)
1350 Tacticals.tclTHENLAST)
1351 (Tacticals.tclTHENLIST [
1352 Refine.refine_with_principal ~typecheck:false begin fun sigma ->
1353 let b = not with_evars && with_eq != None in
1354 let sigma, c, t = use_bindings env sigma elim b (c0,lbind) t0 in
1355 mkletin_goal env sigma with_eq false (id,lastlhyp,ccl,c) (Some t)
1356 end;
1357 if with_evars then Proofview.shelve_unifiable else guard_no_unifiable;
1358 if is_arg_pure_hyp
1359 then Proofview.tclEVARMAP >>= fun sigma -> Tacticals.tclTRY (clear [destVar sigma c0])
1360 else Proofview.tclUNIT ();
1361 if isrec then Proofview.cycle (-1) else Proofview.tclUNIT ()
1362 ])
1363 (tac inhyps)
1364 in
1365 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) tac
1366
1367 | Some (sigma', c) ->
1368 (* pattern found *)
1369 (* TODO: if ind has predicate parameters, use JMeq instead of eq *)
1370 let env = reset_with_named_context sign env in
1371 let with_eq = Option.map (fun eq -> (false,mk_eq_name env id eq)) eqname in
1372 let inhyps = if List.is_empty inhyps then inhyps else Option.fold_left (fun inhyps (_,heq) -> heq::inhyps) inhyps with_eq in
1373 let tac =
1374 Tacticals.tclTHENLIST [
1375 Refine.refine_with_principal ~typecheck:false begin fun sigma ->
1376 mkletin_goal env sigma with_eq true (id,lastlhyp,ccl,c) None
1377 end;
1378 (tac inhyps)
1379 ]
1380 in
1381 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma') tac
1382 end
1383
1384 let has_generic_occurrences_but_goal cls id env sigma ccl =
1385 clause_with_generic_context_selection cls &&
1386 (* TODO: whd_evar of goal *)
1387 (cls.concl_occs != NoOccurrences || not (occur_var env sigma id ccl))
1388
1389 let induction_gen ~clear_flag ~isrec ~with_evars elim
1390 ((_pending,(c,lbind)),(eqname,names) as arg) cls =
1391 let inhyps = match cls with
1392 | Some {onhyps=Some hyps} -> List.map (fun ((_,id),_) -> id) hyps
1393 | _ -> [] in
1394 Proofview.Goal.enter begin fun gl ->
1395 let env = Proofview.Goal.env gl in
1396 let evd = Proofview.Goal.sigma gl in
1397 let ccl = Proofview.Goal.concl gl in
1398 let cls = Option.default allHypsAndConcl cls in
1399 let t = typ_of env evd c in
1400 let is_arg_pure_hyp =
1401 isVar evd c && not (mem_named_context_val (destVar evd c) (Global.named_context_val ()))
1402 && lbind == NoBindings && not with_evars && Option.is_empty eqname
1403 && clear_flag == None
1404 && has_generic_occurrences_but_goal cls (destVar evd c) env evd ccl in
1405 let enough_applied = check_enough_applied env evd elim t in
1406 if is_arg_pure_hyp && enough_applied then
1407 (* First case: induction on a variable already in an inductive type and
1408 with maximal abstraction over the variable.
1409 This is a situation where the induction argument is a
1410 clearable variable of the goal w/o occurrence selection
1411 and w/o equality kept: no need to generalize *)
1412 let id = destVar evd c in
1413 Tacticals.tclTHEN
1414 (clear_unselected_context id inhyps cls)
1415 (induction_with_atomization_of_ind_arg
1416 isrec with_evars elim names id inhyps)
1417 else
1418 (* Otherwise, we look for the pattern, possibly adding missing arguments and
1419 declaring the induction argument as a new local variable *)
1420 let id =
1421 (* Type not the right one if partially applied but anyway for internal use*)
1422 let avoid = match eqname with
1423 | Some {CAst.v=IntroIdentifier id} -> Id.Set.singleton id
1424 | _ -> Id.Set.empty in
1425 let x = id_of_name_using_hdchar env evd t Anonymous in
1426 new_fresh_id avoid x gl in
1427 let info_arg = (is_arg_pure_hyp, not enough_applied) in
1428 pose_induction_arg_then
1429 isrec with_evars info_arg elim id arg t inhyps cls
1430 (induction_with_atomization_of_ind_arg
1431 isrec with_evars elim names id)
1432 end
1433
1434 (* Induction on a list of arguments. First make induction arguments
1435 atomic (using letins), then do induction. The specificity here is
1436 that all arguments and parameters of the scheme are given
1437 (mandatory for the moment), so we don't need to deal with
1438 parameters of the inductive type as in induction_gen. *)
1439 let induction_gen_l isrec with_evars elim names lc =
1440 let newlc = ref [] in
1441 let lc = List.map (function
1442 | (c,None) -> c
1443 | (c,Some{CAst.loc;v=eqname}) ->
1444 error ?loc (DontKnowWhatToDoWith eqname)) lc in
1445 let rec atomize_list l =
1446 match l with
1447 | [] -> Proofview.tclUNIT ()
1448 | c::l' ->
1449 Proofview.tclEVARMAP >>= fun sigma ->
1450 match EConstr.kind sigma c with
1451 | Var id when not (mem_named_context_val id (Global.named_context_val ()))
1452 && not with_evars ->
1453 let () = newlc:= id::!newlc in
1454 atomize_list l'
1455
1456 | _ ->
1457 Proofview.Goal.enter begin fun gl ->
1458 let env = Proofview.Goal.env gl in
1459 let sigma = Proofview.Goal.sigma gl in
1460 let sigma, t = Typing.type_of env sigma c in
1461 let x = id_of_name_using_hdchar env sigma t Anonymous in
1462 let id = new_fresh_id Id.Set.empty x gl in
1463 let newl' = List.map (fun r -> replace_term sigma c (mkVar id) r) l' in
1464 let () = newlc:=id::!newlc in
1465 Tacticals.tclTHENLIST [
1466 tclEVARS sigma;
1467 Tactics.letin_tac None (Name id) c None allHypsAndConcl;
1468 atomize_list newl';
1469 ]
1470 end in
1471 Tacticals.tclTHENLIST
1472 [
1473 (atomize_list lc);
1474 (Proofview.tclUNIT () >>= fun () -> (* ensure newlc has been computed *)
1475 induction_without_atomization isrec with_evars elim names !newlc)
1476 ]
1477
1478 (* Induction either over a term, over a quantified premisse, or over
1479 several quantified premisses (like with functional induction
1480 principles).
1481 TODO: really unify induction with one and induction with several
1482 args *)
1483 let induction_destruct isrec with_evars (lc,elim) =
1484 match lc with
1485 | [] -> assert false (* ensured by syntax, but if called inside caml? *)
1486 | [c,(eqname,names as allnames),cls] ->
1487 Proofview.Goal.enter begin fun gl ->
1488 let env = Proofview.Goal.env gl in
1489 let sigma = Proofview.Goal.sigma gl in
1490 match elim with
1491 | Some elim when is_functional_induction env sigma elim ->
1492 (* Standard induction on non-standard induction schemes *)
1493 (* will be removable when is_functional_induction will be more clever *)
1494 if not (Option.is_empty cls) then error (UnsupportedInClause true);
1495 let _,c = force_destruction_arg false env sigma c in
1496 onInductionArg
1497 (fun _clear_flag c ->
1498 induction_gen_l isrec with_evars elim names
1499 [with_no_bindings c,eqname]) c
1500 | _ ->
1501 (* standard induction *)
1502 onOpenInductionArg env sigma
1503 (fun clear_flag c -> induction_gen ~clear_flag ~isrec ~with_evars elim (c,allnames) cls) c
1504 end
1505 | _ ->
1506 Proofview.Goal.enter begin fun gl ->
1507 let env = Proofview.Goal.env gl in
1508 let sigma = Proofview.Goal.sigma gl in
1509 match elim with
1510 | None ->
1511 (* Several arguments, without "using" clause *)
1512 (* TODO: Do as if the arguments after the first one were called with *)
1513 (* "destruct", but selecting occurrences on the initial copy of *)
1514 (* the goal *)
1515 let (a,b,cl) = List.hd lc in
1516 let l = List.tl lc in
1517 (* TODO *)
1518 Tacticals.tclTHEN
1519 (onOpenInductionArg env sigma (fun clear_flag a ->
1520 induction_gen ~clear_flag ~isrec ~with_evars None (a,b) cl) a)
1521 (Tacticals.tclMAP (fun (a,b,cl) ->
1522 Proofview.Goal.enter begin fun gl ->
1523 let env = Proofview.Goal.env gl in
1524 let sigma = Proofview.Goal.sigma gl in
1525 onOpenInductionArg env sigma (fun clear_flag a ->
1526 induction_gen ~clear_flag ~isrec:false ~with_evars None (a,b) cl) a
1527 end) l)
1528 | Some elim ->
1529 (* Several induction hyps with induction scheme *)
1530 let lc = List.map (on_pi1 (fun c -> snd (force_destruction_arg false env sigma c))) lc in
1531 let newlc =
1532 List.map (fun (x,(eqn,names),cls) ->
1533 if cls != None then error UnsupportedEqnClause;
1534 match x with (* FIXME: should we deal with ElimOnIdent? *)
1535 | _clear_flag,ElimOnConstr x ->
1536 if eqn <> None then error (UnsupportedInClause false);
1537 (with_no_bindings x,names)
1538 | _ -> error DontKnowWhereToFindArgument)
1539 lc in
1540 (* Check that "as", if any, is given only on the last argument *)
1541 let names,rest = List.sep_last (List.map snd newlc) in
1542 if List.exists (fun n -> not (Option.is_empty n)) rest then
1543 error MultipleAsAndUsingClauseOnlyList;
1544 let newlc = List.map (fun (x,_) -> (x,None)) newlc in
1545 induction_gen_l isrec with_evars elim names newlc
1546 end
1547
1548 let induction ev clr c l e =
1549 induction_gen ~clear_flag:clr ~isrec:true ~with_evars:ev e
1550 ((None,(c,NoBindings)),(None,l)) None
1551
1552 let destruct ev clr c l e =
1553 induction_gen ~clear_flag:clr ~isrec:false ~with_evars:ev e
1554 ((None,(c,NoBindings)),(None,l)) None