| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 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 |
|
| 129 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
type eliminator = |
| 196 |
| ElimConstant of (Constant.t * EInstance.t) |
| 197 |
|
| 198 |
| ElimClause of EConstr.constr with_bindings |
| 199 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 250 |
|
| 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 |
|
| 280 |
let expand_hyp id = |
| 281 |
Tacticals.tclTRY (Tactics.unfold_body id) <*> clear_for_destruct [id] |
| 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 |
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 = |
| 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 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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 |
| 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 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 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 |
|
| 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 |
as |
| 459 |
es |
| 460 |
|
| 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 |
) |
| 472 |
|
| 473 |
let atomize_param_of_ind env sigma (hd, params, indices) = |
| 474 |
let params' = List.map (expand_projections env sigma) params in |
| 475 |
*) |
| 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 |
|
| 485 |
|
| 486 |
|
| 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 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
atomize_one accu (c::args) (c'::args') avoid rem |
| 500 |
else |
| 501 |
|
| 502 |
|
| 503 |
|
| 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 |
|
| 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 |
exception Shunt of Id.t move_location |
| 580 |
|
| 581 |
let cook_sign hyp0_opt inhyps indvars env sigma = |
| 582 |
|
| 583 |
|
| 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 |
|
| 599 |
|
| 600 |
toclear := hyp::!toclear; |
| 601 |
MoveFirst |
| 602 |
end else if Id.Set.mem hyp indvars then begin |
| 603 |
|
| 604 |
|
| 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 |
| 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 |
|
| 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) |
| 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 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
type elim_scheme = { |
| 683 |
elimt: types; |
| 684 |
indref: GlobRef.t option; |
| 685 |
params: rel_context; |
| 686 |
nparams: int; |
| 687 |
predicates: rel_context; |
| 688 |
npredicates: int; |
| 689 |
branches: rel_context; |
| 690 |
nbranches: int; |
| 691 |
args: rel_context; |
| 692 |
nargs: int; |
| 693 |
indarg: rel_declaration option; |
| 694 |
|
| 695 |
concl: types; |
| 696 |
|
| 697 |
indarg_in_concl: bool; |
| 698 |
farg_in_concl: bool; |
| 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 |
|
| 723 |
|
| 724 |
Id.of_string (atompart_of_id (make_ident (Id.to_string id) (Some 0))) |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 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 || Int.equal n 0 then Id.Set.empty |
| 742 |
else |
| 743 |
|
| 744 |
|
| 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 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 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 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 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 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 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 |
|
| 832 |
let res = { empty_scheme with |
| 833 |
|
| 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 |
|
| 840 |
args = args_indargs; nargs = List.length args_indargs; } in |
| 841 |
|
| 842 |
if res.farg_in_concl then res |
| 843 |
|
| 844 |
else if Int.equal res.nargs 0 then res |
| 845 |
|
| 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 |
|
| 851 |
let hi_is_ind = match EConstr.kind sigma hi_ind with |
| 852 |
| Ind _ | Var _ | Const _ | Construct _ -> true |
| 853 |
| _ -> false |
| 854 |
in |
| 855 |
|
| 856 |
let hi_args_enough = Int.equal (Array.length hi_args) (List.length params + res.nargs -1) in |
| 857 |
|
| 858 |
if not (hi_is_ind && hi_args_enough) then res |
| 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 |
) |
| 881 |
let cond, check_concl = |
| 882 |
match scheme.indarg with |
| 883 |
| Some (LocalDef _) -> error NotAnInductionSchemeLetIn |
| 884 |
| None -> |
| 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)) -> |
| 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 |
|
| 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 |
|
| 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 |
| 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 |
|
| 1010 |
|
| 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 |
|
| 1031 |
|
| 1032 |
Option.is_empty scheme.indarg |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
let recolle_clenv i params args elimclause 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 |
|
| 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 |
|
| 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 |
|
| 1055 |
|
| 1056 |
|
| 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 |
|
| 1070 |
let elimclause = make_clenv_binding env sigma clause bindings in |
| 1071 |
|
| 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 |
|
| 1084 |
|
| 1085 |
|
| 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 |
|
| 1103 |
|
| 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, true, tac, indsign |
| 1122 |
| ElimUsingList ((elim, elimt, indsign), params, realindvars, patts) -> |
| 1123 |
let tac = Tacticals.tclTHENLIST [ |
| 1124 |
|
| 1125 |
Tactics.reduce (Pattern (List.map inj_with_occurrences patts)) onConcl; |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
induction_tac with_evars params realindvars (ElimClause elim, elimt); |
| 1131 |
] in |
| 1132 |
sigma, 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 |
|
| 1143 |
if deps = [] then Proofview.tclUNIT () else Tactics.apply_type ~typecheck:false tmpcl deps_cstr; |
| 1144 |
|
| 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 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 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 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 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 |
|
| 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 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 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 |
|
| 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 None |
| 1238 |
else |
| 1239 |
|
| 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 |
|
| 1250 |
match elim with |
| 1251 |
| None -> |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
let sign,t = whd_decompose_prod env sigma typ in it_mkProd t sign |
| 1258 |
| Some (elimc, _) -> |
| 1259 |
|
| 1260 |
|
| 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 |
|
| 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 |
|
| 1287 |
|
| 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 |
|
| 1300 |
match elim with |
| 1301 |
| None -> |
| 1302 |
|
| 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 |
|
| 1311 |
|
| 1312 |
fun _ -> true |
| 1313 |
| Some _ -> |
| 1314 |
|
| 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 |
|
| 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 |
|
| 1341 |
|
| 1342 |
let flags = tactic_infer_flags (with_evars && false) in |
| 1343 |
let (sigma, c0) = finish_evar_resolution ~flags env sigma (pending,c0) in |
| 1344 |
let tac = |
| 1345 |
(if isrec then |
| 1346 |
|
| 1347 |
Tacticals.tclTHENFIRST |
| 1348 |
else |
| 1349 |
|
| 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 |
|
| 1369 |
|
| 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 |
|
| 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 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 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 |
|
| 1419 |
|
| 1420 |
let id = |
| 1421 |
|
| 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 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 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 () -> |
| 1475 |
induction_without_atomization isrec with_evars elim names !newlc) |
| 1476 |
] |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
let induction_destruct isrec with_evars (lc,elim) = |
| 1484 |
match lc with |
| 1485 |
| [] -> assert false |
| 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 |
|
| 1493 |
|
| 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 |
|
| 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 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
let (a,b,cl) = List.hd lc in |
| 1516 |
let l = List.tl lc in |
| 1517 |
|
| 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 |
|
| 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 |
| 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 |
|
| 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 |