| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open Util |
| 14 |
open Names |
| 15 |
open Printer |
| 16 |
open Constr |
| 17 |
open Context |
| 18 |
open Termops |
| 19 |
open Tactypes |
| 20 |
|
| 21 |
open Ssrmatching_plugin |
| 22 |
open Ssrmatching |
| 23 |
|
| 24 |
open Ssrast |
| 25 |
open Ssrprinters |
| 26 |
open Ssrcommon |
| 27 |
|
| 28 |
open Proofview.Notations |
| 29 |
|
| 30 |
module RelDecl = Context.Rel.Declaration |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
let analyze_eliminator elimty env sigma = |
| 38 |
let open EConstr in |
| 39 |
let rec loop ctx t = match kind_of_type sigma t with |
| 40 |
| AtomicType (hd, args) when isRel sigma hd -> |
| 41 |
ctx, destRel sigma hd, not (Vars.noccurn sigma 1 t), Array.length args, t |
| 42 |
| CastType (t, _) -> loop ctx t |
| 43 |
| ProdType (x, ty, t) -> loop (RelDecl.LocalAssum (x, ty) :: ctx) t |
| 44 |
| LetInType (x,b,ty,t) -> loop (RelDecl.LocalDef (x, b, ty) :: ctx) (Vars.subst1 b t) |
| 45 |
| _ -> |
| 46 |
let env' = push_rel_context ctx env in |
| 47 |
let t' = Reductionops.whd_all env' sigma t in |
| 48 |
if not (eq_constr sigma t t') then loop ctx t' else |
| 49 |
errorstrm Pp.(str"The eliminator has the wrong shape."++spc()++ |
| 50 |
str"A (applied) bound variable was expected as the conclusion of "++ |
| 51 |
str"the eliminator's"++Pp.cut()++str"type:"++spc()++pr_econstr_env env' sigma elimty) in |
| 52 |
let ctx, pred_id, elim_is_dep, n_pred_args,concl = loop [] elimty in |
| 53 |
let n_elim_args = Context.Rel.nhyps ctx in |
| 54 |
let is_rec_elim = |
| 55 |
let count_occurn n term = |
| 56 |
let count = ref 0 in |
| 57 |
let rec occur_rec n c = match EConstr.kind sigma c with |
| 58 |
| Rel m -> if m = n then incr count |
| 59 |
| Evar (_, args) -> SList.Skip.iter (fun c -> occur_rec n c) args |
| 60 |
| _ -> EConstr.iter_with_binders sigma succ occur_rec n c |
| 61 |
in |
| 62 |
occur_rec n term; !count in |
| 63 |
let occurr2 n t = count_occurn n t > 1 in |
| 64 |
not (List.for_all_i |
| 65 |
(fun i (_,rd) -> pred_id <= i || not (occurr2 (pred_id - i) rd)) |
| 66 |
1 (assums_of_rel_context ctx)) |
| 67 |
in |
| 68 |
n_elim_args - pred_id, n_elim_args, is_rec_elim, elim_is_dep, n_pred_args, |
| 69 |
(ctx,concl) |
| 70 |
|
| 71 |
let subgoals_tys sigma (relctx, concl) = |
| 72 |
let rec aux cur_depth acc = function |
| 73 |
| hd :: rest -> |
| 74 |
let ty = Context.Rel.Declaration.get_type hd in |
| 75 |
if EConstr.Vars.noccurn sigma cur_depth concl && |
| 76 |
List.for_all_i (fun i -> function |
| 77 |
| Context.Rel.Declaration.LocalAssum(_, t) -> |
| 78 |
EConstr.Vars.noccurn sigma i t |
| 79 |
| Context.Rel.Declaration.LocalDef (_, b, t) -> |
| 80 |
EConstr.Vars.noccurn sigma i t && EConstr.Vars.noccurn sigma i b) 1 rest |
| 81 |
then aux (cur_depth - 1) (ty :: acc) rest |
| 82 |
else aux (cur_depth - 1) acc rest |
| 83 |
| [] -> Array.of_list (List.rev acc) |
| 84 |
in |
| 85 |
aux (List.length relctx) [] (List.rev relctx) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
let get_eq_type env sigma = |
| 105 |
Evd.fresh_global env sigma Rocqlib.(lib_ref "core.eq.type") |
| 106 |
|
| 107 |
type elim_what = |
| 108 |
| EConstr of |
| 109 |
Ssrast.ssrhyp list * Ssrmatching.occ * |
| 110 |
EConstr.constr |
| 111 |
| EGen of |
| 112 |
((Ssrast.ssrhyp list option * |
| 113 |
Ssrmatching.occ) * |
| 114 |
Ssrmatching.cpattern) |
| 115 |
|
| 116 |
let check_elim sigma has_elim = function |
| 117 |
| EConstr(_,_,t) when EConstr.isEvar sigma t -> |
| 118 |
anomaly "elim called on a constr evar" |
| 119 |
| EGen (_, g) when not has_elim && is_wildcard g -> |
| 120 |
errorstrm Pp.(str"Indeterminate pattern and no eliminator") |
| 121 |
| EGen ((Some clr,occ), g) when is_wildcard g -> |
| 122 |
Proofview.tclUNIT (None, clr, occ, None) |
| 123 |
| EGen ((None, occ), g) when is_wildcard g -> |
| 124 |
Proofview.tclUNIT (None,[],occ,None) |
| 125 |
| EGen ((_, occ), p as gen) -> |
| 126 |
Proofview.Goal.enter_one begin fun gl -> |
| 127 |
let env = Proofview.Goal.env gl in |
| 128 |
let sigma = Proofview.Goal.sigma gl in |
| 129 |
let concl = Proofview.Goal.concl gl in |
| 130 |
let sigma, (_, c, clr) = interp_gen env sigma ~concl true gen in |
| 131 |
Proofview.Unsafe.tclEVARS sigma <*> Proofview.tclUNIT (Some c, clr, occ, Some p) |
| 132 |
end |
| 133 |
| EConstr (clr, occ, c) -> |
| 134 |
Proofview.tclUNIT (Some c, clr, occ, None) |
| 135 |
|
| 136 |
let match_pat env sigma0 p occ h cl = |
| 137 |
debug_ssr (fun () -> Pp.(str"matching: " ++ pr_occ occ ++ pp_pattern env p)); |
| 138 |
let (c,ucst), cl = |
| 139 |
fill_occ_pattern ~raise_NoMatch:true env sigma0 cl p occ h in |
| 140 |
debug_ssr (fun () -> Pp.(str" got: " ++ pr_econstr_env env sigma0 c)); |
| 141 |
c, cl, ucst |
| 142 |
|
| 143 |
let fire_subst sigma t = Reductionops.nf_evar sigma t |
| 144 |
|
| 145 |
let mkTpat env sigma0 (sigma, t) = |
| 146 |
let t, evs, ucst = abs_evars env sigma0 (sigma, fire_subst sigma t) in |
| 147 |
let t, _, _, sigma = saturate ~beta:true env sigma t (List.length evs) in |
| 148 |
{ pat_sigma = Evd.merge_universe_context sigma ucst; pat_pat = T t } |
| 149 |
|
| 150 |
let redex_of_pattern env p = match redex_of_pattern p with |
| 151 |
| None -> CErrors.anomaly (Pp.str "pattern without redex.") |
| 152 |
| Some (sigma, e) -> Evarutil.nf_evar sigma e, Evd.ustate sigma |
| 153 |
|
| 154 |
let unif_redex env sigma0 nsigma p t = |
| 155 |
let t, evs, ucst = abs_evars env sigma0 (nsigma, fire_subst nsigma t) in |
| 156 |
let t, _, _, sigma = saturate ~beta:true env p.pat_sigma t (List.length evs) in |
| 157 |
let sigma = Evd.merge_universe_context sigma ucst in |
| 158 |
match p.pat_pat with |
| 159 |
| X_In_T p -> { pat_sigma = sigma; pat_pat = E_As_X_In_T (t, p) } |
| 160 |
| _ -> |
| 161 |
try { p with pat_sigma = unify_HO env sigma t (fst (redex_of_pattern env p)) } |
| 162 |
with e when CErrors.noncritical e -> p |
| 163 |
|
| 164 |
let get_nth_arg n args = |
| 165 |
List.find_map_exn (fun (i, x, _) -> if Int.equal i n then Some x else None) args |
| 166 |
|
| 167 |
let find_eliminator env sigma ~concl ~is_case ?elim oc c_gen = |
| 168 |
match elim with |
| 169 |
| Some elim -> |
| 170 |
let sigma, elimty = Typing.type_of env sigma elim in |
| 171 |
let elimty = |
| 172 |
let rename_elimty r = |
| 173 |
EConstr.of_constr |
| 174 |
(Arguments_renaming.rename_type env |
| 175 |
(EConstr.to_constr ~abort_on_undefined_evars:false sigma |
| 176 |
elimty) r) in |
| 177 |
match EConstr.kind sigma elim with |
| 178 |
| Constr.Var kn -> rename_elimty (GlobRef.VarRef kn) |
| 179 |
| Constr.Const (kn,_) -> rename_elimty (GlobRef.ConstRef kn) |
| 180 |
| _ -> elimty |
| 181 |
in |
| 182 |
let pred_id, n_elim_args, is_rec, elim_is_dep, n_pred_args,ctx_concl = |
| 183 |
analyze_eliminator elimty env sigma in |
| 184 |
let seed = subgoals_tys sigma ctx_concl in |
| 185 |
let elim, elimty, elim_args, sigma = |
| 186 |
saturate ~beta:is_case env sigma elim ~ty:elimty n_elim_args in |
| 187 |
let pred = get_nth_arg pred_id elim_args in |
| 188 |
let elimty = Reductionops.whd_all env sigma elimty in |
| 189 |
let cty, sigma = |
| 190 |
if Option.is_empty oc then None, sigma |
| 191 |
else |
| 192 |
let c = Option.get oc in |
| 193 |
let sigma, c_ty = Typing.type_of env sigma c in |
| 194 |
let pc = match c_gen with |
| 195 |
| Some p -> interp_cpattern env sigma p None |
| 196 |
| _ -> mkTpat env sigma (sigma, c) in |
| 197 |
Some(c, c_ty, pc), sigma in |
| 198 |
sigma, seed, cty, elim, elimty, elim_args, n_elim_args, elim_is_dep, is_rec, pred |
| 199 |
| None -> |
| 200 |
let c = Option.get oc in |
| 201 |
let sigma, c_ty = Typing.type_of env sigma c in |
| 202 |
let ((kn, i),_ as indu), unfolded_c_ty = |
| 203 |
Tacred.reduce_to_quantified_ind env sigma c_ty in |
| 204 |
let sort = Retyping.get_sort_of env sigma concl in |
| 205 |
let sigma, elim, elimty = |
| 206 |
if not is_case then |
| 207 |
let sigma, elim = Evd.fresh_global env sigma |
| 208 |
(Elimschemes.lookup_eliminator env (kn,i) (EConstr.ESorts.quality_or_set sigma sort)) |
| 209 |
in |
| 210 |
let elimty = Retyping.get_type_of env sigma elim in |
| 211 |
sigma, elim, elimty |
| 212 |
else |
| 213 |
Indrec.build_case_analysis_scheme env sigma indu true sort |
| 214 |
in |
| 215 |
let pred_id,n_elim_args,is_rec,elim_is_dep,n_pred_args,ctx_concl = |
| 216 |
analyze_eliminator elimty env sigma in |
| 217 |
let seed = |
| 218 |
if is_case then |
| 219 |
let mind,indb = Inductive.lookup_mind_specif env (kn,i) in |
| 220 |
let tys = indb.Declarations.mind_nf_lc in |
| 221 |
let renamed_tys = |
| 222 |
Array.mapi (fun j (ctx, cty) -> |
| 223 |
let t = Term.it_mkProd_or_LetIn cty ctx in |
| 224 |
debug_ssr (fun () -> Pp.(str "Search" ++ Printer.pr_constr_env env sigma t)); |
| 225 |
let t = Arguments_renaming.rename_type env t |
| 226 |
(GlobRef.ConstructRef((kn,i),j+1)) in |
| 227 |
debug_ssr (fun () -> Pp.(str"Done Search " ++ Printer.pr_constr_env env sigma t)); |
| 228 |
t) |
| 229 |
tys |
| 230 |
in |
| 231 |
let drop_params x = |
| 232 |
snd @@ EConstr.decompose_prod_n_decls sigma |
| 233 |
mind.Declarations.mind_nparams (EConstr.of_constr x) in |
| 234 |
Array.map drop_params renamed_tys |
| 235 |
else |
| 236 |
subgoals_tys sigma ctx_concl |
| 237 |
in |
| 238 |
let rctx = fst (EConstr.decompose_prod_decls sigma unfolded_c_ty) in |
| 239 |
let n_c_args = Context.Rel.length rctx in |
| 240 |
let c, c_ty, t_args, sigma = saturate env sigma c ~ty:c_ty n_c_args in |
| 241 |
let elim, elimty, elim_args, sigma = |
| 242 |
saturate ~beta:is_case env sigma elim ~ty:elimty n_elim_args in |
| 243 |
let pred = get_nth_arg pred_id elim_args in |
| 244 |
let pc = match n_c_args, c_gen with |
| 245 |
| 0, Some p -> interp_cpattern env sigma p None |
| 246 |
| _ -> mkTpat env sigma (sigma, c) in |
| 247 |
let cty = Some (c, c_ty, pc) in |
| 248 |
let elimty = Reductionops.whd_all env sigma elimty in |
| 249 |
sigma, seed, cty, elim, elimty, elim_args, n_elim_args, elim_is_dep, is_rec, pred |
| 250 |
|
| 251 |
let saturate_until env sigma c c_ty f = |
| 252 |
let rec loop n = try |
| 253 |
let c, c_ty, _, sigma = saturate env sigma c ~ty:c_ty n in |
| 254 |
let sigma' = f sigma c c_ty in |
| 255 |
Some (c, c_ty, sigma, sigma') |
| 256 |
with |
| 257 |
| NotEnoughProducts -> None |
| 258 |
| e when CErrors.noncritical e -> loop (n+1) in loop 0 |
| 259 |
|
| 260 |
let get_head_pattern env sigma elim_is_dep elim_args n_elim_args inf_deps_r cty = match cty with |
| 261 |
| None -> sigma, true |
| 262 |
| Some (c, c_ty, _) -> |
| 263 |
let rec first = function |
| 264 |
| [] -> |
| 265 |
errorstrm Pp.(str"Unable to apply the eliminator to the term"++ |
| 266 |
spc()++pr_econstr_env env sigma c++spc()) |
| 267 |
| x :: rest -> |
| 268 |
match x () with |
| 269 |
| None -> first rest |
| 270 |
| Some (sigma, b) -> sigma, b |
| 271 |
in |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
let unify_HO_rigid env sigma a b = |
| 276 |
let is_applied_evar x = match EConstr.kind sigma x with |
| 277 |
| App(x,_) -> EConstr.isEvar sigma x |
| 278 |
| _ -> false in |
| 279 |
if is_applied_evar a || is_applied_evar b then |
| 280 |
raise Evarconv.(UnableToUnify(sigma, |
| 281 |
Pretype_errors.ProblemBeyondCapabilities)) |
| 282 |
else unify_HO env sigma a b in |
| 283 |
let try_c_last_arg () = |
| 284 |
|
| 285 |
if elim_is_dep then None else |
| 286 |
let arg = get_nth_arg (n_elim_args - 1) elim_args in |
| 287 |
let sigma, arg_ty = Typing.type_of env sigma arg in |
| 288 |
match saturate_until env sigma c c_ty (fun sigma c c_ty -> |
| 289 |
unify_HO env (unify_HO_rigid env sigma c_ty arg_ty) arg c) with |
| 290 |
| Some (c, _, _, sigma) -> Some (sigma, false) |
| 291 |
| None -> None in |
| 292 |
let try_c_last_pattern () = |
| 293 |
|
| 294 |
if inf_deps_r = [] then None else |
| 295 |
let inf_arg = List.hd inf_deps_r in |
| 296 |
let sigma, inf_arg_ty = Typing.type_of env sigma inf_arg in |
| 297 |
match saturate_until env sigma c c_ty (fun sigma _ c_ty -> |
| 298 |
unify_HO_rigid env sigma c_ty inf_arg_ty) with |
| 299 |
| Some (c, _, _, sigma) -> Some(sigma, true) |
| 300 |
| None -> None in |
| 301 |
first [try_c_last_arg;try_c_last_pattern] |
| 302 |
|
| 303 |
let check_pattern_instantiated env sigma patterns = |
| 304 |
let evars_of_term = Evarutil.undefined_evars_of_term sigma in |
| 305 |
let patterns = List.map (fun (_,_,t,_) -> Reductionops.nf_evar sigma t) patterns in |
| 306 |
let patterns_ev = List.map evars_of_term patterns in |
| 307 |
let ev = List.fold_left Evar.Set.union Evar.Set.empty patterns_ev in |
| 308 |
let ty_ev = Evar.Set.fold (fun i e -> |
| 309 |
let ex = i in |
| 310 |
let i_ty = Evd.evar_concl (Evd.find_undefined sigma ex) in |
| 311 |
Evar.Set.union e (evars_of_term i_ty)) |
| 312 |
ev Evar.Set.empty in |
| 313 |
let inter = Evar.Set.inter ev ty_ev in |
| 314 |
if not (Evar.Set.is_empty inter) then begin |
| 315 |
let i = Evar.Set.choose inter in |
| 316 |
let pat = List.find (fun t -> Evar.Set.mem i (evars_of_term t)) patterns in |
| 317 |
errorstrm Pp.(str"Pattern"++spc()++pr_econstr_pat env sigma pat++spc()++ |
| 318 |
str"was not completely instantiated and one of its variables"++spc()++ |
| 319 |
str"occurs in the type of another non-instantiated pattern variable"); |
| 320 |
end |
| 321 |
|
| 322 |
let is_undef_pat { pat_sigma = sigma; pat_pat = pat } = match pat with |
| 323 |
| T t -> EConstr.isEvar sigma t |
| 324 |
| _ -> false |
| 325 |
|
| 326 |
let generate_pred env sigma0 ~concl patterns predty eqid is_rec deps elim_args n_elim_args c_is_head_p clr sigma = |
| 327 |
let concl0 = concl in |
| 328 |
let error sigma t inf_t = errorstrm Pp.(str"The given pattern matches the term"++ |
| 329 |
spc()++pr_econstr_env env sigma t++spc()++str"while the inferred pattern"++ |
| 330 |
spc()++pr_econstr_pat env sigma (fire_subst sigma inf_t)++spc()++ str"doesn't") in |
| 331 |
let match_or_postpone (cl, sigma, post) (h, p, inf_t, occ) = |
| 332 |
let p = unif_redex env sigma0 sigma p inf_t in |
| 333 |
if is_undef_pat p then |
| 334 |
let () = debug_ssr (fun () -> Pp.(str"postponing " ++ pp_pattern env p)) in |
| 335 |
cl, sigma, post @ [h, p, inf_t, occ] |
| 336 |
else try |
| 337 |
let c, cl, ucst = match_pat env sigma0 p occ h cl in |
| 338 |
let sigma = Evd.merge_universe_context sigma ucst in |
| 339 |
let sigma = try unify_HO env sigma inf_t c |
| 340 |
with exn when CErrors.noncritical exn -> error sigma c inf_t in |
| 341 |
cl, sigma, post |
| 342 |
with |
| 343 |
| NoMatch | NoProgress -> |
| 344 |
let e, ucst = redex_of_pattern env p in |
| 345 |
let sigma = Evd.merge_universe_context sigma ucst in |
| 346 |
let e, evs, _ucst = abs_evars env sigma (p.pat_sigma, e) in |
| 347 |
let e, _, _, sigma = saturate ~beta:true env sigma e (List.length evs) in |
| 348 |
let sigma = try unify_HO env sigma inf_t e |
| 349 |
with exn when CErrors.noncritical exn -> error sigma e inf_t in |
| 350 |
cl, sigma, post |
| 351 |
in |
| 352 |
let rec match_all concl sigma patterns = |
| 353 |
let concl, sigma, postponed = |
| 354 |
List.fold_left match_or_postpone (concl, sigma, []) patterns in |
| 355 |
if postponed = [] then concl, sigma |
| 356 |
else if List.length postponed = List.length patterns then |
| 357 |
errorstrm Pp.(str "Some patterns are undefined even after all"++spc()++ |
| 358 |
str"the defined ones matched") |
| 359 |
else match_all concl sigma postponed in |
| 360 |
let concl, sigma = match_all concl sigma patterns in |
| 361 |
let pred_rctx, _ = EConstr.decompose_prod_decls sigma (fire_subst sigma predty) in |
| 362 |
let sigma, concl, gen_eq_tac, clr = match eqid with |
| 363 |
| Some (IPatId _) when not is_rec -> |
| 364 |
let k = List.length deps in |
| 365 |
let c = fire_subst sigma (get_nth_arg (n_elim_args - k - 1) elim_args) in |
| 366 |
let sigma, t = Typing.type_of env sigma c in |
| 367 |
let sigma, eq = get_eq_type env sigma in |
| 368 |
let sigma, gen_eq_tac, eq_ty = |
| 369 |
let refl = EConstr.mkApp (eq, [|t; c; c|]) in |
| 370 |
let new_concl = EConstr.mkArrow refl EConstr.ERelevance.relevant (EConstr.Vars.lift 1 concl0) in |
| 371 |
let new_concl = fire_subst sigma new_concl in |
| 372 |
let sigma, erefl = mkRefl env sigma t c in |
| 373 |
let erefl = fire_subst sigma erefl in |
| 374 |
let erefl_ty = Retyping.get_type_of env sigma erefl in |
| 375 |
let eq_ty = Retyping.get_type_of env sigma erefl_ty in |
| 376 |
let ucst = Evd.ustate sigma in |
| 377 |
let evds = Evar.Map.bind (Evd.find_undefined sigma) @@ |
| 378 |
Evd.evars_of_term sigma new_concl in |
| 379 |
let gen_eq_tac = |
| 380 |
let open Proofview.Notations in |
| 381 |
Proofview.Goal.enter begin fun s -> |
| 382 |
let sigma = Proofview.Goal.sigma s in |
| 383 |
let sigma = Evd.merge_universe_context sigma ucst in |
| 384 |
let sigma, shelve = Evar.Map.fold (fun e info (sigma, shelve) -> |
| 385 |
if not @@ Evd.mem sigma e then |
| 386 |
Evd.add sigma e info, e::shelve else |
| 387 |
(sigma, shelve)) |
| 388 |
evds (sigma, []) in |
| 389 |
Proofview.Unsafe.tclEVARS sigma <*> |
| 390 |
Proofview.shelve_goals @@ shelve <*> |
| 391 |
Tactics.apply_type ~typecheck:true new_concl [erefl] |
| 392 |
end |
| 393 |
in |
| 394 |
sigma, gen_eq_tac, eq_ty |
| 395 |
in |
| 396 |
let rel = k + if c_is_head_p then 1 else 0 in |
| 397 |
let sigma, src = mkProt env sigma eq_ty EConstr.(mkApp (eq,[|t; c; mkRel rel|])) in |
| 398 |
let concl = EConstr.mkArrow src EConstr.ERelevance.relevant (EConstr.Vars.lift 1 concl) in |
| 399 |
let clr = if deps <> [] then clr else [] in |
| 400 |
sigma, concl, gen_eq_tac, clr |
| 401 |
| _ -> sigma, concl, Tacticals.tclIDTAC, clr in |
| 402 |
let mk_lam t r = EConstr.mkLambda_or_LetIn r t in |
| 403 |
let concl = List.fold_left mk_lam concl pred_rctx in |
| 404 |
let sigma, concl = |
| 405 |
if eqid <> None && is_rec then |
| 406 |
let sigma, concls = Typing.type_of env sigma concl in |
| 407 |
let sigma, concl = mkProt env sigma concls concl in |
| 408 |
let sigma, _ = Typing.type_of env sigma concl in |
| 409 |
sigma, concl |
| 410 |
else sigma, concl in |
| 411 |
sigma, concl, gen_eq_tac, clr |
| 412 |
|
| 413 |
let compute_patterns env sigma0 what c_is_head_p cty deps inf_deps_r occ orig_clr eqid sigma = |
| 414 |
let rec loop patterns clr i = function |
| 415 |
| [],[] -> patterns, clr, sigma |
| 416 |
| ((oclr, occ), t):: deps, inf_t :: inf_deps -> |
| 417 |
let p = interp_cpattern env sigma0 t None in |
| 418 |
let clr_t = |
| 419 |
interp_clr sigma (oclr,(tag_of_cpattern t, fst (redex_of_pattern env p))) in |
| 420 |
|
| 421 |
let clr_t = if deps = [] && eqid <> None then [] else clr_t in |
| 422 |
let p = if is_undef_pat p then mkTpat env sigma0 (sigma, inf_t) else p in |
| 423 |
loop (patterns @ [i, p, inf_t, occ]) |
| 424 |
(clr_t @ clr) (i+1) (deps, inf_deps) |
| 425 |
| [], c :: inf_deps -> |
| 426 |
debug_ssr (fun () -> Pp.(str"adding inf pattern " ++ pr_econstr_pat env sigma c)); |
| 427 |
loop (patterns @ [i, mkTpat env sigma0 (sigma, c), c, allocc]) |
| 428 |
clr (i+1) ([], inf_deps) |
| 429 |
| _::_, [] -> errorstrm Pp.(str "Too many dependent abstractions") in |
| 430 |
let deps, head_p, inf_deps_r = match what, c_is_head_p, cty with |
| 431 |
| EConstr _, _, None -> anomaly "Simple elim with no term" |
| 432 |
| _, false, _ -> deps, [], inf_deps_r |
| 433 |
| EGen gen, true, None -> deps @ [gen], [], inf_deps_r |
| 434 |
| _, true, Some (c, _, pc) -> |
| 435 |
let occ = if occ = None then allocc else occ in |
| 436 |
let inf_p, inf_deps_r = List.hd inf_deps_r, List.tl inf_deps_r in |
| 437 |
deps, [1, pc, inf_p, occ], inf_deps_r in |
| 438 |
let patterns, clr, sigma = |
| 439 |
loop [] orig_clr (List.length head_p+1) (List.rev deps, inf_deps_r) in |
| 440 |
sigma, head_p @ patterns, Util.List.uniquize clr |
| 441 |
|
| 442 |
let ssrelim ?(is_case=false) deps what ?elim eqid elim_intro_tac = |
| 443 |
let open Proofview.Notations in |
| 444 |
Proofview.tclEVARMAP >>= begin fun sigma -> |
| 445 |
|
| 446 |
check_elim sigma (Option.has_some elim) what end >>= |
| 447 |
|
| 448 |
fun (oc, orig_clr, occ, c_gen) -> |
| 449 |
Proofview.Goal.enter begin fun gl -> |
| 450 |
let env = Proofview.Goal.env gl in |
| 451 |
let sigma = Proofview.Goal.sigma gl in |
| 452 |
let concl = Proofview.Goal.concl gl in |
| 453 |
let sigma0 = sigma in |
| 454 |
debug_ssr (fun () -> (Pp.str(if is_case then "==CASE==" else "==ELIM=="))); |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
let sigma, seed, cty, elim, elimty, elim_args, n_elim_args, elim_is_dep, is_rec, pred = |
| 461 |
find_eliminator env sigma ~concl ~is_case ?elim oc c_gen |
| 462 |
in |
| 463 |
let () = |
| 464 |
debug_ssr (fun () -> Pp.(str"elim= "++ pr_econstr_pat env sigma elim)); |
| 465 |
debug_ssr (fun () -> Pp.(str"elimty= "++ pr_econstr_pat env sigma elimty)) in |
| 466 |
let open EConstr in |
| 467 |
let inf_deps_r = match kind_of_type sigma elimty with |
| 468 |
| AtomicType (_, args) -> List.rev (Array.to_list args) |
| 469 |
| _ -> assert false in |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
let sigma, c_is_head_p = get_head_pattern env sigma elim_is_dep elim_args n_elim_args inf_deps_r cty in |
| 474 |
debug_ssr (fun () -> Pp.(str"c_is_head_p= " ++ bool c_is_head_p)); |
| 475 |
let sigma, predty = Typing.type_of env sigma pred in |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
let sigma, patterns, clr = compute_patterns env sigma0 what c_is_head_p cty deps inf_deps_r occ orig_clr eqid sigma in |
| 480 |
let pp_pat (_,p,_,occ) = Pp.(pr_occ occ ++ pp_pattern env p) in |
| 481 |
let pp_inf_pat (_,_,t,_) = pr_econstr_pat env sigma (fire_subst sigma t) in |
| 482 |
debug_ssr (fun () -> Pp.(pp_concat (str"patterns=") (List.map pp_pat patterns))); |
| 483 |
debug_ssr (fun () -> Pp.(pp_concat (str"inf. patterns=") (List.map pp_inf_pat patterns))); |
| 484 |
|
| 485 |
|
| 486 |
let sigma, elim_pred, gen_eq_tac, clr = |
| 487 |
generate_pred env sigma0 ~concl patterns predty eqid is_rec deps elim_args n_elim_args c_is_head_p clr sigma |
| 488 |
in |
| 489 |
let sigma, pty = Typing.type_of env sigma elim_pred in |
| 490 |
let sigma = List.fold_left (fun sigma (_, arg, argty) -> Typing.check env sigma arg argty) sigma elim_args in |
| 491 |
debug_ssr (fun () -> Pp.(str"elim_pred=" ++ pr_econstr_env env sigma elim_pred)); |
| 492 |
debug_ssr (fun () -> Pp.(str"elim_pred_ty=" ++ pr_econstr_env env sigma pty)); |
| 493 |
let sigma = unify_HO env sigma pred elim_pred in |
| 494 |
let elim = fire_subst sigma elim in |
| 495 |
let sigma = resolve_typeclasses env sigma ~where:elim ~fail:false in |
| 496 |
|
| 497 |
let () = check_pattern_instantiated env sigma patterns in |
| 498 |
|
| 499 |
let elim = sigma, elim in |
| 500 |
let seed = |
| 501 |
Array.map (fun ty -> |
| 502 |
let ctx,_ = EConstr.decompose_prod_decls sigma ty in |
| 503 |
CList.rev_map Context.Rel.Declaration.get_name ctx) seed in |
| 504 |
|
| 505 |
let elim_tac = |
| 506 |
Tacticals.tclTHENLIST [ |
| 507 |
refine_with ~with_evars:false elim; |
| 508 |
cleartac clr] in |
| 509 |
Tacticals.tclTHENLIST [gen_eq_tac; elim_intro_tac ?seed:(Some seed) what eqid elim_tac is_rec clr] |
| 510 |
end |
| 511 |
|
| 512 |
let elimtac x = |
| 513 |
let k ?seed:_ _what _eqid elim_tac _is_rec _clr = elim_tac in |
| 514 |
ssrelim ~is_case:false [] (EConstr ([],None,x)) None k |
| 515 |
|
| 516 |
let casetac x k = |
| 517 |
let k ?seed _what _eqid elim_tac _is_rec _clr = k ?seed elim_tac in |
| 518 |
ssrelim ~is_case:true [] (EConstr ([],None,x)) None k |
| 519 |
|
| 520 |
let rev_id = mk_internal_id "rev concl" |
| 521 |
let injecteq_id = mk_internal_id "injection equation" |
| 522 |
|
| 523 |
let revtoptac n0 = |
| 524 |
Proofview.Goal.enter begin fun gl -> |
| 525 |
let sigma = Proofview.Goal.sigma gl in |
| 526 |
let concl = Proofview.Goal.concl gl in |
| 527 |
let env = Proofview.Goal.env gl in |
| 528 |
let n = nb_prod sigma concl - n0 in |
| 529 |
let dc, cl = EConstr.decompose_prod_n_decls sigma n concl in |
| 530 |
let ty = EConstr.it_mkProd_or_LetIn cl (List.rev dc) in |
| 531 |
let dc' = dc @ [Context.Rel.Declaration.LocalAssum(make_annot (Name rev_id) EConstr.ERelevance.relevant, ty)] in |
| 532 |
Refine.refine ~typecheck:true begin fun sigma -> |
| 533 |
let f = EConstr.it_mkLambda_or_LetIn (mkEtaApp (EConstr.mkRel (n + 1)) (-n) 1) dc' in |
| 534 |
let sigma, ev = Evarutil.new_evar env sigma ty in |
| 535 |
sigma, (EConstr.mkApp (f, [|ev|])) |
| 536 |
end |
| 537 |
end |
| 538 |
|
| 539 |
let nothing_to_inject = |
| 540 |
CWarnings.create ~name:"spurious-ssr-injection" ~category:CWarnings.CoreCategories.ssr |
| 541 |
(fun (sigma, env, ty) -> |
| 542 |
Pp.(str "SSReflect: cannot obtain new equations out of" ++ fnl() ++ |
| 543 |
str" " ++ Printer.pr_econstr_env env sigma ty ++ fnl() ++ |
| 544 |
str "Did you write an extra [] in the intro pattern?")) |
| 545 |
|
| 546 |
let equality_inj l b id c = Proofview.Goal.enter begin fun gl -> |
| 547 |
Proofview.tclORELSE (Equality.inj None ~injection_in_context:false l b None c) |
| 548 |
(function |
| 549 |
| (Equality.NothingToInject,_) -> |
| 550 |
let open Proofview.Notations in |
| 551 |
Ssrcommon.tacTYPEOF (EConstr.mkVar id) >>= fun ty -> |
| 552 |
nothing_to_inject (Proofview.Goal.sigma gl, Proofview.Goal.env gl, ty); |
| 553 |
discharge_hyp (id, (id, "")) |
| 554 |
| (e,info) -> Proofview.tclZERO ~info e) |
| 555 |
end |
| 556 |
|
| 557 |
let injectidl2rtac id c = |
| 558 |
Proofview.Goal.enter begin fun gl -> |
| 559 |
let sigma = Proofview.Goal.sigma gl in |
| 560 |
let concl = Proofview.Goal.concl gl in |
| 561 |
Tacticals.tclTHEN (equality_inj None true id c) (revtoptac (nb_prod sigma concl)) |
| 562 |
end |
| 563 |
|
| 564 |
let injectl2rtac sigma c = match EConstr.kind sigma c with |
| 565 |
| Var id -> injectidl2rtac id (EConstr.mkVar id, NoBindings) |
| 566 |
| _ -> |
| 567 |
let id = injecteq_id in |
| 568 |
let xhavetac id c = Tactics.pose_proof (Name id) c in |
| 569 |
Tacticals.tclTHENLIST [xhavetac id c; injectidl2rtac id (EConstr.mkVar id, NoBindings); Tactics.clear [id]] |
| 570 |
|
| 571 |
let is_injection_case env sigma c = |
| 572 |
let sigma, cty = Typing.type_of env sigma c in |
| 573 |
let (mind,_) = Tacred.eval_to_quantified_ind env sigma cty in |
| 574 |
Rocqlib.check_ref "core.eq.type" (GlobRef.IndRef mind) |
| 575 |
|
| 576 |
let perform_injection c = |
| 577 |
let open Proofview.Notations in |
| 578 |
Proofview.Goal.enter begin fun gl -> |
| 579 |
let env = Proofview.Goal.env gl in |
| 580 |
let sigma = Proofview.Goal.sigma gl in |
| 581 |
let sigma, cty = Typing.type_of env sigma c in |
| 582 |
let mind, t = Tacred.reduce_to_quantified_ind env sigma cty in |
| 583 |
let dc, eqt = EConstr.decompose_prod sigma t in |
| 584 |
if dc = [] then injectl2rtac sigma c else |
| 585 |
if not (EConstr.Vars.closed0 sigma eqt) then |
| 586 |
CErrors.user_err (Pp.str "can't decompose a quantified equality") else |
| 587 |
let cl = Proofview.Goal.concl gl in |
| 588 |
let n = List.length dc in |
| 589 |
let c_eq = mkEtaApp c n 2 in |
| 590 |
let cl1 = EConstr.mkLambda EConstr.(make_annot Anonymous ERelevance.relevant, mkArrow eqt ERelevance.relevant cl, mkApp (mkRel 1, [|c_eq|])) in |
| 591 |
let id = injecteq_id in |
| 592 |
let id_with_ebind = (EConstr.mkVar id, NoBindings) in |
| 593 |
let injtac = Tacticals.tclTHEN (introid id) (injectidl2rtac id id_with_ebind) in |
| 594 |
Proofview.Unsafe.tclEVARS sigma <*> |
| 595 |
Tacticals.tclTHENLAST (Tactics.apply (EConstr.it_mkLambda cl1 dc)) injtac |
| 596 |
end |
| 597 |
|
| 598 |
let ssrscase_or_inj_tac c = |
| 599 |
Proofview.Goal.enter begin fun gl -> |
| 600 |
let env = Proofview.Goal.env gl in |
| 601 |
let sigma = Proofview.Goal.sigma gl in |
| 602 |
if is_injection_case env sigma c then perform_injection c |
| 603 |
else casetac c (fun ?seed:_ k -> k) |
| 604 |
end |