| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open CErrors |
| 13 |
open Util |
| 14 |
open Names |
| 15 |
open Termops |
| 16 |
open Environ |
| 17 |
open Constr |
| 18 |
open Context |
| 19 |
open EConstr |
| 20 |
open Vars |
| 21 |
open Namegen |
| 22 |
open Evd |
| 23 |
open Printer |
| 24 |
open Reductionops |
| 25 |
open Inductiveops |
| 26 |
open Tacticals |
| 27 |
open Tactics |
| 28 |
open Context.Named.Declaration |
| 29 |
|
| 30 |
module NamedDecl = Context.Named.Declaration |
| 31 |
|
| 32 |
let no_inductive_inconstr env sigma constr = |
| 33 |
(str "Cannot recognize an inductive predicate in " ++ |
| 34 |
pr_leconstr_env env sigma constr ++ |
| 35 |
str "." ++ spc () ++ str "If there is one, may be the structure of the arity" ++ |
| 36 |
spc () ++ str "or of the type of constructors" ++ spc () ++ |
| 37 |
str "is hidden by constant definitions.") |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
let rec add_prods_sign env sigma t = |
| 117 |
match EConstr.kind sigma (whd_all env sigma t) with |
| 118 |
| Prod (na,c1,b) -> |
| 119 |
let id = id_of_name_using_hdchar env sigma t na.binder_name in |
| 120 |
let b'= subst1 (mkVar id) b in |
| 121 |
add_prods_sign (push_named (LocalAssum ({na with binder_name=id},c1)) env) sigma b' |
| 122 |
| LetIn (na,c1,t1,b) -> |
| 123 |
let id = id_of_name_using_hdchar env sigma t na.binder_name in |
| 124 |
let b'= subst1 (mkVar id) b in |
| 125 |
add_prods_sign (push_named (LocalDef ({na with binder_name=id},c1,t1)) env) sigma b' |
| 126 |
| _ -> (env,t) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
let compute_first_inversion_scheme env sigma ind sort dep_option = |
| 142 |
let indf,realargs = dest_ind_type ind in |
| 143 |
let allvars = vars_of_env env in |
| 144 |
let p = next_ident_away (Id.of_string "P") allvars in |
| 145 |
let pty,goal = |
| 146 |
if dep_option then |
| 147 |
let pty = make_arity env sigma true indf sort in |
| 148 |
let r = relevance_of_inductive_type env ind in |
| 149 |
let goal = |
| 150 |
mkProd |
| 151 |
(make_annot Anonymous r, mkAppliedInd ind, applist(mkVar p,realargs@[mkRel 1])) |
| 152 |
in |
| 153 |
pty,goal |
| 154 |
else |
| 155 |
let i = mkAppliedInd ind in |
| 156 |
let ivars = Termops.global_vars_set env sigma i in |
| 157 |
let revargs,ownsign = |
| 158 |
fold_named_context |
| 159 |
(fun env d (revargs,hyps) -> |
| 160 |
let d = EConstr.of_named_decl d in |
| 161 |
let id = NamedDecl.get_id d in |
| 162 |
if Id.Set.mem id ivars then |
| 163 |
((mkVar id)::revargs, Context.Named.add d hyps) |
| 164 |
else |
| 165 |
(revargs,hyps)) |
| 166 |
env ~init:([],[]) |
| 167 |
in |
| 168 |
let pty = it_mkNamedProd_or_LetIn sigma (mkSort sort) ownsign in |
| 169 |
let goal = mkArrow i ERelevance.relevant (applist(mkVar p, List.rev revargs)) in |
| 170 |
(pty,goal) |
| 171 |
in |
| 172 |
let npty = nf_all env sigma pty in |
| 173 |
let extenv = push_named (LocalAssum (make_annot p ERelevance.relevant,npty)) env in |
| 174 |
extenv, goal |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
let inversion_scheme ~name ~poly env sigma t sort dep_option inv_op = |
| 184 |
let (env,i) = add_prods_sign env sigma t in |
| 185 |
let ind = |
| 186 |
try find_rectype env sigma i |
| 187 |
with Not_found -> |
| 188 |
user_err (no_inductive_inconstr env sigma i) |
| 189 |
in |
| 190 |
let (invEnv,invGoal) = |
| 191 |
compute_first_inversion_scheme env sigma ind sort dep_option |
| 192 |
in |
| 193 |
assert |
| 194 |
(Id.Set.subset |
| 195 |
(Termops.global_vars_set env sigma invGoal) |
| 196 |
(ids_of_named_context_val (named_context_val invEnv))); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
let pf = Proof.start ~name ~poly (Evd.from_ctx (ustate sigma)) [invEnv,invGoal] in |
| 202 |
let pf, _, () = Proof.run_tactic env (tclTHEN intro (onLastHypId inv_op)) pf in |
| 203 |
let pfterm = List.hd (Proof.partial_proof pf) in |
| 204 |
let global_named_context = Global.named_context_val () in |
| 205 |
let ownSign = ref begin |
| 206 |
fold_named_context |
| 207 |
(fun env d sign -> |
| 208 |
let d = EConstr.of_named_decl d in |
| 209 |
if mem_named_context_val (NamedDecl.get_id d) global_named_context then sign |
| 210 |
else Context.Named.add d sign) |
| 211 |
invEnv ~init:Context.Named.empty |
| 212 |
end in |
| 213 |
let avoid = ref Id.Set.empty in |
| 214 |
let Proof.{sigma} = Proof.data pf in |
| 215 |
let sigma = Evd.minimize_universes sigma in |
| 216 |
let rec fill_holes c = |
| 217 |
match EConstr.kind sigma c with |
| 218 |
| Evar (e,args) -> |
| 219 |
let h = next_ident_away (Id.of_string "H") !avoid in |
| 220 |
let ty,inst = Evarutil.generalize_evar_over_rels sigma (e,args) in |
| 221 |
avoid := Id.Set.add h !avoid; |
| 222 |
ownSign := Context.Named.add (LocalAssum (make_annot h ERelevance.relevant,ty)) !ownSign; |
| 223 |
applist (mkVar h, inst) |
| 224 |
| _ -> EConstr.map sigma fill_holes c |
| 225 |
in |
| 226 |
let c = fill_holes pfterm in |
| 227 |
|
| 228 |
let invProof = it_mkNamedLambda_or_LetIn sigma c !ownSign in |
| 229 |
invProof, sigma |
| 230 |
|
| 231 |
let add_inversion_lemma ~poly (name:lident) env sigma t sort dep inv_op = |
| 232 |
let invProof, sigma = inversion_scheme ~name:name.v ~poly env sigma t sort dep inv_op in |
| 233 |
let cinfo = Declare.CInfo.make ?loc:name.loc ~name:name.v ~typ:None () in |
| 234 |
let info = Declare.Info.make ~poly ~kind:Decls.(IsProof Lemma) () in |
| 235 |
let _ : Names.GlobRef.t = |
| 236 |
Declare.declare_definition ~cinfo ~info ~opaque:false ~body:invProof sigma |
| 237 |
in |
| 238 |
() |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
let add_inversion_lemma_exn ~poly na com comsort bool tac = |
| 244 |
let env = Global.env () in |
| 245 |
let sigma = Evd.from_env env in |
| 246 |
let c, uctx = Constrintern.interp_type env sigma com in |
| 247 |
let sigma = Evd.from_ctx uctx in |
| 248 |
let sigma, sort = Evd.fresh_sort_in_quality ~rigid:univ_rigid sigma comsort in |
| 249 |
add_inversion_lemma ~poly na env sigma c sort bool tac |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
let lemInv id c = |
| 256 |
Proofview.Goal.enter begin fun gls -> |
| 257 |
let env = Proofview.Goal.env gls in |
| 258 |
let sigma = Proofview.Goal.sigma gls in |
| 259 |
let clause = Clenv.mk_clenv_from env sigma (c, Retyping.get_type_of env sigma c) in |
| 260 |
let mv = let mvs = Clenv.clenv_arguments clause in |
| 261 |
if List.is_empty mvs then |
| 262 |
CErrors.user_err |
| 263 |
Pp.(hov 0 (pr_econstr_env env sigma c ++ spc () |
| 264 |
++ str "does not refer to an inversion lemma.")) |
| 265 |
else List.last mvs |
| 266 |
in |
| 267 |
try |
| 268 |
let clause = Clenv.clenv_instantiate mv clause (EConstr.mkVar id, Typing.type_of_variable env id) in |
| 269 |
Clenv.res_pf clause ~flags:(Unification.elim_flags ()) ~with_evars:false |
| 270 |
with |
| 271 |
| Failure _ | UserError _ -> |
| 272 |
user_err |
| 273 |
(str "Cannot refine current goal with the lemma " ++ |
| 274 |
pr_leconstr_env env sigma c ++ str ".") |
| 275 |
end |
| 276 |
|
| 277 |
let lemInv_gen id c = try_intros_until (fun id -> lemInv id c) id |
| 278 |
|
| 279 |
let lemInvIn id c ids = |
| 280 |
Proofview.Goal.enter begin fun gl -> |
| 281 |
let hyps = List.map (fun id -> Tacmach.pf_get_hyp id gl) ids in |
| 282 |
let intros_replace_ids = |
| 283 |
let concl = Proofview.Goal.concl gl in |
| 284 |
let sigma = Proofview.Goal.sigma gl in |
| 285 |
let nb_of_new_hyp = nb_prod sigma concl - List.length ids in |
| 286 |
if nb_of_new_hyp < 1 then |
| 287 |
intros_replacing ids |
| 288 |
else |
| 289 |
(tclTHEN (tclDO nb_of_new_hyp intro) (intros_replacing ids)) |
| 290 |
in |
| 291 |
((tclTHEN (tclTHEN (Generalize.bring_hyps hyps) (lemInv id c)) |
| 292 |
(intros_replace_ids))) |
| 293 |
end |
| 294 |
|
| 295 |
let lemInvIn_gen id c l = try_intros_until (fun id -> lemInvIn id c l) id |
| 296 |
|
| 297 |
let lemInv_clause id c = function |
| 298 |
| [] -> lemInv_gen id c |
| 299 |
| l -> lemInvIn_gen id c l |