| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
open Util |
| 18 |
open Names |
| 19 |
open Namegen |
| 20 |
open Constr |
| 21 |
open Context |
| 22 |
open EConstr |
| 23 |
open Declarations |
| 24 |
open Tactics |
| 25 |
open Tacticals |
| 26 |
open Auto |
| 27 |
open Constr_matching |
| 28 |
open Hipattern |
| 29 |
open Proofview.Notations |
| 30 |
open Tactypes |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
let clear_last = |
| 55 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 56 |
(onLastHyp (fun c -> (clear [destVar sigma c]))) |
| 57 |
|
| 58 |
let choose_eq eqonleft = |
| 59 |
if eqonleft then |
| 60 |
left_with_bindings false NoBindings |
| 61 |
else |
| 62 |
right_with_bindings false NoBindings |
| 63 |
let choose_noteq eqonleft = |
| 64 |
if eqonleft then |
| 65 |
right_with_bindings false NoBindings |
| 66 |
else |
| 67 |
left_with_bindings false NoBindings |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
type dectype = { |
| 73 |
eqonleft : bool; |
| 74 |
op : EConstr.t; |
| 75 |
eq1 : EConstr.t; |
| 76 |
eq2 : EConstr.t; |
| 77 |
noteq : EConstr.t; |
| 78 |
} |
| 79 |
|
| 80 |
let mk_dectype { eqonleft; op; eq1; eq2; noteq } t x y = |
| 81 |
let eq = mkApp (eq1,[|t;x;y|]) in |
| 82 |
let neq = mkApp (noteq,[|mkApp (eq2,[|t;x;y|])|]) in |
| 83 |
if eqonleft then mkApp (op,[|eq;neq|]) else mkApp (op,[|neq;eq|]) |
| 84 |
|
| 85 |
let generalize_right dty typ c1 c2 = |
| 86 |
Proofview.Goal.enter begin fun gl -> |
| 87 |
let env = Proofview.Goal.env gl in |
| 88 |
Refine.refine_with_principal ~typecheck:false begin fun sigma -> |
| 89 |
let na = Name (next_name_away_with_default "x" Anonymous (Termops.vars_of_env env)) in |
| 90 |
let r = Retyping.relevance_of_type env sigma typ in |
| 91 |
let newconcl = mkProd (make_annot na r, typ, mk_dectype dty typ c1 (mkRel 1)) in |
| 92 |
let (sigma, x) = Evarutil.new_evar env sigma newconcl in |
| 93 |
(sigma, mkApp (x, [|c2|]), Some (fst @@ destEvar sigma x)) |
| 94 |
end |
| 95 |
end |
| 96 |
|
| 97 |
let mkBranches (dty, c1, c2, typ) = |
| 98 |
tclTHENLIST |
| 99 |
[generalize_right dty typ c1 c2; |
| 100 |
Simple.elim c1; |
| 101 |
intros; |
| 102 |
onLastHyp Simple.case; |
| 103 |
clear_last; |
| 104 |
intros] |
| 105 |
|
| 106 |
let inj_flags = Some { |
| 107 |
Equality.keep_proof_equalities = true; |
| 108 |
Equality.injection_pattern_l2r_order = true; |
| 109 |
} |
| 110 |
|
| 111 |
let discrHyp id = |
| 112 |
let c env sigma = (sigma, (mkVar id, NoBindings)) in |
| 113 |
let tac c = Equality.discr_tac false (Some (None, ElimOnConstr c)) in |
| 114 |
Tacticals.tclDELAYEDWITHHOLES false c tac |
| 115 |
|
| 116 |
let solveNoteqBranch side = |
| 117 |
tclTHEN (choose_noteq side) |
| 118 |
(tclTHEN introf |
| 119 |
(onLastHypId (fun id -> discrHyp id))) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
let mkDecideEqGoal eqonleft (op,eq,neg) rectype c1 c2 = |
| 124 |
let equality = mkApp(eq, [|rectype; c1; c2|]) in |
| 125 |
let disequality = mkApp(neg, [|equality|]) in |
| 126 |
if eqonleft then mkApp(op, [|equality; disequality |]) |
| 127 |
else mkApp(op, [|disequality; equality |]) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
let idx = Id.of_string "x" |
| 133 |
let idy = Id.of_string "y" |
| 134 |
|
| 135 |
let mkGenDecideEqGoal rectype ops g = |
| 136 |
let sigma = Proofview.Goal.sigma g in |
| 137 |
let hypnames = Tacmach.pf_ids_set_of_hyps g in |
| 138 |
let xname = next_ident_away idx hypnames in |
| 139 |
let yname = next_ident_away idy (Id.Set.add xname hypnames) in |
| 140 |
(mkNamedProd sigma (make_annot xname ERelevance.relevant) rectype |
| 141 |
(mkNamedProd sigma (make_annot yname ERelevance.relevant) rectype |
| 142 |
(mkDecideEqGoal true ops |
| 143 |
rectype (mkVar xname) (mkVar yname)))) |
| 144 |
|
| 145 |
let rec rewrite_and_clear hyps = match hyps with |
| 146 |
| [] -> Proofview.tclUNIT () |
| 147 |
| id :: hyps -> |
| 148 |
tclTHENLIST [ |
| 149 |
Equality.rewriteLR (mkVar id); |
| 150 |
clear [id]; |
| 151 |
rewrite_and_clear hyps; |
| 152 |
] |
| 153 |
|
| 154 |
let eqCase tac = |
| 155 |
tclTHEN intro (onLastHypId tac) |
| 156 |
|
| 157 |
let injHyp id = |
| 158 |
let c env sigma = (sigma, (mkVar id, NoBindings)) in |
| 159 |
let tac c = Equality.injClause inj_flags None false (Some (None, ElimOnConstr c)) in |
| 160 |
Tacticals.tclDELAYEDWITHHOLES false c tac |
| 161 |
|
| 162 |
let diseqCase hyps eqonleft = |
| 163 |
let diseq = Id.of_string "diseq" in |
| 164 |
let absurd = Id.of_string "absurd" in |
| 165 |
(intro_using_then diseq (fun diseq -> |
| 166 |
tclTHEN (choose_noteq eqonleft) |
| 167 |
(tclTHEN (rewrite_and_clear (List.rev hyps)) |
| 168 |
(tclTHEN (red_in_concl) |
| 169 |
(intro_using_then absurd (fun absurd -> |
| 170 |
tclTHEN (Simple.apply (mkVar diseq)) |
| 171 |
(tclTHEN (injHyp absurd) |
| 172 |
(Auto.gen_trivial [] None)))))))) |
| 173 |
|
| 174 |
open Proofview.Notations |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
let match_eqdec env sigma c = |
| 179 |
try |
| 180 |
let (eqonleft,_,c1,c2,ty) = match_eqdec env sigma c in |
| 181 |
let (op,eq1,noteq,eq2) = |
| 182 |
match EConstr.kind sigma c with |
| 183 |
| App (op,[|ty1;ty2|]) -> |
| 184 |
let ty1, ty2 = if eqonleft then ty1, ty2 else ty2, ty1 in |
| 185 |
(match EConstr.kind sigma ty1, EConstr.kind sigma ty2 with |
| 186 |
| App (eq1,_), App (noteq,[|neq|]) -> |
| 187 |
(match EConstr.kind sigma neq with |
| 188 |
| App (eq2,_) -> op,eq1,noteq,eq2 |
| 189 |
| _ -> assert false) |
| 190 |
| _ -> assert false) |
| 191 |
| _ -> assert false in |
| 192 |
let dty = { eqonleft; op; eq1; eq2; noteq } in |
| 193 |
Proofview.tclUNIT (dty, c1, c2, ty) |
| 194 |
with PatternMatchingFailure as exn -> |
| 195 |
let _, info = Exninfo.capture exn in |
| 196 |
Proofview.tclZERO ~info PatternMatchingFailure |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
let elim_type dty rectype a1 a2 = |
| 201 |
Proofview.Goal.enter begin fun gl -> |
| 202 |
let env = Proofview.Goal.env gl in |
| 203 |
let sigma = Proofview.Goal.sigma gl in |
| 204 |
let concl = Proofview.Goal.concl gl in |
| 205 |
let (ind, _) = Tacred.reduce_to_atomic_ind env sigma dty.op in |
| 206 |
let s = Retyping.get_sort_quality_of env sigma concl in |
| 207 |
let elimc = Elimschemes.lookup_eliminator env (fst ind) s in |
| 208 |
|
| 209 |
|
| 210 |
let sigma, elimc = EConstr.fresh_global env sigma elimc in |
| 211 |
let elimc = |
| 212 |
let { eqonleft; eq1; eq2; noteq } = dty in |
| 213 |
let eq = mkApp (eq1,[|rectype;a1;a2|]) in |
| 214 |
let neq = mkApp (noteq,[|mkApp (eq2,[|rectype;a1;a2|])|]) in |
| 215 |
if eqonleft then mkApp (elimc, [|eq; neq|]) else mkApp (elimc, [|neq; eq|]) |
| 216 |
in |
| 217 |
let elimt = Retyping.get_type_of env sigma elimc in |
| 218 |
let clause = Clenv.mk_clenv_from env sigma (elimc, elimt) in |
| 219 |
Proofview.Unsafe.tclEVARS sigma <*> Clenv.res_pf clause ~with_evars:false |
| 220 |
end |
| 221 |
|
| 222 |
let rec solveArg hyps dty largs rargs = match largs, rargs with |
| 223 |
| [], [] -> |
| 224 |
tclTHENLIST [ |
| 225 |
choose_eq dty.eqonleft; |
| 226 |
rewrite_and_clear (List.rev hyps); |
| 227 |
intros_reflexivity; |
| 228 |
] |
| 229 |
| a1 :: largs, a2 :: rargs -> |
| 230 |
Proofview.Goal.enter begin fun gl -> |
| 231 |
let env = Proofview.Goal.env gl in |
| 232 |
let sigma = Proofview.Goal.sigma gl in |
| 233 |
let sigma, rectype = Typing.type_of env sigma a1 in |
| 234 |
let tac hyp = solveArg (hyp :: hyps) dty largs rargs in |
| 235 |
let subtacs = |
| 236 |
if dty.eqonleft then [eqCase tac;diseqCase hyps dty.eqonleft;default_auto] |
| 237 |
else [diseqCase hyps dty.eqonleft;eqCase tac;default_auto] in |
| 238 |
tclTHEN (Proofview.Unsafe.tclEVARS sigma) (tclTHENS (elim_type dty rectype a1 a2) subtacs) |
| 239 |
end |
| 240 |
| _ -> invalid_arg "List.fold_right2" |
| 241 |
|
| 242 |
let solveEqBranch rectype = |
| 243 |
Proofview.tclORELSE |
| 244 |
begin |
| 245 |
Proofview.Goal.enter begin fun gl -> |
| 246 |
let env = Proofview.Goal.env gl in |
| 247 |
let sigma = Proofview.Goal.sigma gl in |
| 248 |
let concl = Proofview.Goal.concl gl in |
| 249 |
match_eqdec env sigma concl >>= fun (dty, lhs, rhs,_) -> |
| 250 |
let (mib,mip) = Inductive.lookup_mind_specif env rectype in |
| 251 |
let nparams = mib.mind_nparams in |
| 252 |
let getargs l = List.skipn nparams (snd (decompose_app_list sigma l)) in |
| 253 |
let rargs = getargs rhs |
| 254 |
and largs = getargs lhs in |
| 255 |
|
| 256 |
solveArg [] dty largs rargs |
| 257 |
end |
| 258 |
end |
| 259 |
begin function (e, info) -> match e with |
| 260 |
| PatternMatchingFailure -> Tacticals.tclZEROMSG (Pp.str"Unexpected conclusion!") |
| 261 |
| e -> Proofview.tclZERO ~info e |
| 262 |
end |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
let hd_app sigma c = match EConstr.kind sigma c with |
| 267 |
| App (h,_) -> h |
| 268 |
| _ -> c |
| 269 |
|
| 270 |
let decideGralEquality = |
| 271 |
Proofview.tclORELSE |
| 272 |
begin |
| 273 |
Proofview.Goal.enter begin fun gl -> |
| 274 |
let env = Proofview.Goal.env gl in |
| 275 |
let sigma = Proofview.Goal.sigma gl in |
| 276 |
let concl = Proofview.Goal.concl gl in |
| 277 |
match_eqdec env sigma concl >>= fun (dty, c1, c2, typ as data) -> |
| 278 |
let headtyp = hd_app sigma (Tacred.whd_compute env sigma typ) in |
| 279 |
begin match EConstr.kind sigma headtyp with |
| 280 |
| Ind (mi,_) -> Proofview.tclUNIT mi |
| 281 |
| _ -> tclZEROMSG (Pp.str"This decision procedure only works for inductive objects.") |
| 282 |
end >>= fun rectype -> |
| 283 |
(tclTHEN |
| 284 |
(mkBranches data) |
| 285 |
(tclORELSE (solveNoteqBranch dty.eqonleft) (solveEqBranch rectype))) |
| 286 |
end |
| 287 |
end |
| 288 |
begin function (e, info) -> match e with |
| 289 |
| PatternMatchingFailure -> |
| 290 |
Tacticals.tclZEROMSG (Pp.str"The goal must be of the form {x<>y}+{x=y} or {x=y}+{x<>y}.") |
| 291 |
| e -> Proofview.tclZERO ~info e |
| 292 |
end |
| 293 |
|
| 294 |
let decideEqualityGoal = tclTHEN intros decideGralEquality |
| 295 |
|
| 296 |
let decideEquality rectype ops = |
| 297 |
Proofview.Goal.enter begin fun gl -> |
| 298 |
let decide = mkGenDecideEqGoal rectype ops gl in |
| 299 |
(tclTHENS (cut decide) [default_auto;decideEqualityGoal]) |
| 300 |
end |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
let compare c1 c2 = |
| 306 |
let open Rocqlib in |
| 307 |
pf_constr_of_global (lib_ref "core.sumbool.type") >>= fun opc -> |
| 308 |
pf_constr_of_global (lib_ref "core.eq.type") >>= fun eqc -> |
| 309 |
pf_constr_of_global (lib_ref "core.not.type") >>= fun notc -> |
| 310 |
Proofview.Goal.enter begin fun gl -> |
| 311 |
let env = Proofview.Goal.env gl in |
| 312 |
let sigma = Proofview.Goal.sigma gl in |
| 313 |
let sigma, rectype = Typing.type_of env sigma c1 in |
| 314 |
let ops = (opc,eqc,notc) in |
| 315 |
let decide = mkDecideEqGoal true ops rectype c1 c2 in |
| 316 |
tclTHEN (Proofview.Unsafe.tclEVARS sigma) |
| 317 |
(tclTHENS (cut decide) |
| 318 |
[(tclTHEN intro |
| 319 |
(tclTHEN (onLastHyp simplest_case) clear_last)); |
| 320 |
decideEquality rectype ops]) |
| 321 |
end |