| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
open Names |
| 14 |
open Inductiveops |
| 15 |
open Declarations |
| 16 |
open Constr |
| 17 |
open Context |
| 18 |
open EConstr |
| 19 |
open Vars |
| 20 |
open Tactics |
| 21 |
open Typing |
| 22 |
open Ccalgo |
| 23 |
open Ccproof |
| 24 |
open Pp |
| 25 |
open Util |
| 26 |
open Proofview.Notations |
| 27 |
|
| 28 |
module RelDecl = Context.Rel.Declaration |
| 29 |
module NamedDecl = Context.Named.Declaration |
| 30 |
|
| 31 |
let _f_equal = lazy (Rocqlib.lib_ref "core.eq.congr") |
| 32 |
let _eq_rect = lazy (Rocqlib.lib_ref "core.eq.rect") |
| 33 |
let _refl_equal = lazy (Rocqlib.lib_ref "core.eq.refl") |
| 34 |
let _sym_eq = lazy (Rocqlib.lib_ref "core.eq.sym") |
| 35 |
let _trans_eq = lazy (Rocqlib.lib_ref "core.eq.trans") |
| 36 |
let _eq = lazy (Rocqlib.lib_ref "core.eq.type") |
| 37 |
let _False = lazy (Rocqlib.lib_ref "core.False.type") |
| 38 |
let _not = lazy (Rocqlib.lib_ref "core.not.type") |
| 39 |
|
| 40 |
let whd env sigma t = |
| 41 |
Reductionops.clos_whd_flags RedFlags.betaiotazeta env sigma t |
| 42 |
|
| 43 |
let whd_delta env sigma t = |
| 44 |
Reductionops.clos_whd_flags RedFlags.all env sigma t |
| 45 |
|
| 46 |
let whd_all env sigma c = Reductionops.whd_all env sigma c |
| 47 |
|
| 48 |
let whd_in_concl = |
| 49 |
reduct_in_concl ~cast:true ~check:false (whd_all, DEFAULTcast) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
let sf_of env sigma c = ESorts.kind sigma (snd (sort_of env sigma c)) |
| 55 |
|
| 56 |
let rec decompose_term env sigma t = |
| 57 |
match EConstr.kind sigma (whd env sigma t) with |
| 58 |
App (f,args)-> |
| 59 |
let tf=decompose_term env sigma f in |
| 60 |
let targs=Array.map (decompose_term env sigma) args in |
| 61 |
Array.fold_left (fun s t-> ATerm.mkAppli (s,t)) tf targs |
| 62 |
| Prod (_,a,_b) when noccurn sigma 1 _b -> |
| 63 |
let b = Termops.pop _b in |
| 64 |
let sort_b = sf_of env sigma b in |
| 65 |
let sort_a = sf_of env sigma a in |
| 66 |
ATerm.mkAppli (ATerm.mkAppli (ATerm.mkProduct (sort_a,sort_b), |
| 67 |
decompose_term env sigma a), |
| 68 |
decompose_term env sigma b) |
| 69 |
| Construct ((ind, _ as cstr), u) -> |
| 70 |
let u = EInstance.kind sigma u in |
| 71 |
let oib = Environ.lookup_mind (fst ind) env in |
| 72 |
let nargs = constructor_nallargs env cstr in |
| 73 |
ATerm.mkConstructor env {ci_constr = Construct (cstr, u); |
| 74 |
ci_arity=nargs; |
| 75 |
ci_nhyps=nargs-oib.mind_nparams} |
| 76 |
| Ind c -> |
| 77 |
let (mind,i_ind),u = c in |
| 78 |
let u = EInstance.kind sigma u in |
| 79 |
let canon_mind = MutInd.make1 (MutInd.canonical mind) in |
| 80 |
let canon_ind = canon_mind,i_ind in ATerm.mkSymb (Constr.mkIndU (canon_ind,u)) |
| 81 |
| Const (c,u) -> |
| 82 |
let u = EInstance.kind sigma u in |
| 83 |
let canon_const = Constant.make1 (Constant.canonical c) in |
| 84 |
ATerm.mkSymb (Constr.mkConstU (canon_const,u)) |
| 85 |
| Proj (p, _, c) -> |
| 86 |
let canon_mind kn = MutInd.make1 (MutInd.canonical kn) in |
| 87 |
let p' = Projection.map canon_mind p in |
| 88 |
let c = Retyping.expand_projection env sigma p' c [] in |
| 89 |
decompose_term env sigma c |
| 90 |
| String s -> |
| 91 |
ATerm.mkConstructor env { ci_constr = String s; ci_arity = 0; ci_nhyps = 0 } |
| 92 |
| Int i -> |
| 93 |
ATerm.mkConstructor env { ci_constr = Int i; ci_arity = 0; ci_nhyps = 0 } |
| 94 |
| Float f -> |
| 95 |
ATerm.mkConstructor env { ci_constr = Float f; ci_arity = 0; ci_nhyps = 0 } |
| 96 |
| _ -> |
| 97 |
let t = Termops.strip_outer_cast sigma t in |
| 98 |
if closed0 sigma t then ATerm.mkSymb (EConstr.to_constr ~abort_on_undefined_evars:false sigma t) else raise Not_found |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
let atom_of_constr b env sigma term = |
| 103 |
let wh = (if b then whd else whd_delta) env sigma term in |
| 104 |
let kot = EConstr.kind sigma wh in |
| 105 |
match kot with |
| 106 |
App (f,args)-> |
| 107 |
if isRefX env sigma (Lazy.force _eq) f && Int.equal (Array.length args) 3 |
| 108 |
then `Eq (args.(0), |
| 109 |
decompose_term env sigma args.(1), |
| 110 |
decompose_term env sigma args.(2)) |
| 111 |
else `Other (decompose_term env sigma term) |
| 112 |
| _ -> `Other (decompose_term env sigma term) |
| 113 |
|
| 114 |
let rec pattern_of_constr env sigma c = |
| 115 |
match EConstr.kind sigma (whd env sigma c) with |
| 116 |
App (f,args)-> |
| 117 |
let pargs,lrels = List.split |
| 118 |
(Array.map_to_list (pattern_of_constr env sigma) args) in |
| 119 |
begin match EConstr.kind sigma f with |
| 120 |
Rel i -> |
| 121 |
PVar (i, List.rev pargs), |
| 122 |
List.fold_left Int.Set.union (Int.Set.singleton i) lrels |
| 123 |
| _ -> |
| 124 |
let pf = decompose_term env sigma f in |
| 125 |
PApp (pf,List.rev pargs), |
| 126 |
List.fold_left Int.Set.union Int.Set.empty lrels |
| 127 |
end |
| 128 |
| Prod (_,a,_b) when noccurn sigma 1 _b -> |
| 129 |
let b = Termops.pop _b in |
| 130 |
let pa,sa = pattern_of_constr env sigma a in |
| 131 |
let pb,sb = pattern_of_constr env sigma b in |
| 132 |
let sort_b = sf_of env sigma b in |
| 133 |
let sort_a = sf_of env sigma a in |
| 134 |
PApp(ATerm.mkProduct (sort_a,sort_b), |
| 135 |
[pa;pb]),(Int.Set.union sa sb) |
| 136 |
| Rel i -> PVar (i, []),Int.Set.singleton i |
| 137 |
| _ -> |
| 138 |
let pf = decompose_term env sigma c in |
| 139 |
PApp (pf,[]),Int.Set.empty |
| 140 |
|
| 141 |
let non_trivial = function |
| 142 |
PVar (_, []) -> false |
| 143 |
| _ -> true |
| 144 |
|
| 145 |
let rec has_open_head = function |
| 146 |
PVar (_, _::_) -> true |
| 147 |
| PApp (_, args) -> List.exists has_open_head args |
| 148 |
| _ -> false |
| 149 |
|
| 150 |
let patterns_of_constr b env sigma nrels term = |
| 151 |
let f,args= |
| 152 |
try destApp sigma ((if b then whd else whd_delta) env sigma term) with DestKO -> raise Not_found in |
| 153 |
if isRefX env sigma (Lazy.force _eq) f && Int.equal (Array.length args) 3 |
| 154 |
then |
| 155 |
let patt1,rels1 = pattern_of_constr env sigma args.(1) |
| 156 |
and patt2,rels2 = pattern_of_constr env sigma args.(2) in |
| 157 |
let valid1 = |
| 158 |
if not (Int.equal (Int.Set.cardinal rels1) nrels) then Creates_variables |
| 159 |
else if has_open_head patt1 then Creates_variables |
| 160 |
else if non_trivial patt1 then Normal |
| 161 |
else Trivial (EConstr.to_constr ~abort_on_undefined_evars:false sigma args.(0)) |
| 162 |
and valid2 = |
| 163 |
if not (Int.equal (Int.Set.cardinal rels2) nrels) then Creates_variables |
| 164 |
else if has_open_head patt2 then Creates_variables |
| 165 |
else if non_trivial patt2 then Normal |
| 166 |
else Trivial (EConstr.to_constr ~abort_on_undefined_evars:false sigma args.(0)) in |
| 167 |
if valid1 != Creates_variables |
| 168 |
|| valid2 != Creates_variables then |
| 169 |
nrels,valid1,patt1,valid2,patt2 |
| 170 |
else raise Not_found |
| 171 |
else raise Not_found |
| 172 |
|
| 173 |
let rec quantified_atom_of_constr b env sigma nrels term = |
| 174 |
match EConstr.kind sigma ((if b then whd else whd_delta) env sigma term) with |
| 175 |
Prod (id,atom,ff) -> |
| 176 |
if isRefX env sigma (Lazy.force _False) ff then |
| 177 |
let patts=patterns_of_constr b env sigma nrels atom in |
| 178 |
`Nrule patts |
| 179 |
else |
| 180 |
quantified_atom_of_constr b (EConstr.push_rel (RelDecl.LocalAssum (id,atom)) env) sigma (succ nrels) ff |
| 181 |
| App (f,[|atom|]) when isRefX env sigma (Lazy.force _not) f -> |
| 182 |
let patts=patterns_of_constr b env sigma nrels atom in |
| 183 |
`Nrule patts |
| 184 |
| _ -> |
| 185 |
let patts=patterns_of_constr b env sigma nrels term in |
| 186 |
`Rule patts |
| 187 |
|
| 188 |
let litteral_of_constr b env sigma term = |
| 189 |
match EConstr.kind sigma ((if b then whd else whd_delta) env sigma term) with |
| 190 |
| Prod (id,atom,ff) -> |
| 191 |
if isRefX env sigma (Lazy.force _False) ff then |
| 192 |
match (atom_of_constr b env sigma atom) with |
| 193 |
`Eq(t,a,b) -> `Neq(t,a,b) |
| 194 |
| `Other(p) -> `Nother(p) |
| 195 |
else |
| 196 |
begin |
| 197 |
try |
| 198 |
quantified_atom_of_constr b (EConstr.push_rel (RelDecl.LocalAssum (id,atom)) env) sigma 1 ff |
| 199 |
with Not_found -> |
| 200 |
`Other (decompose_term env sigma term) |
| 201 |
end |
| 202 |
| App (f,[|atom|]) when isRefX env sigma (Lazy.force _not) f -> |
| 203 |
begin match (atom_of_constr b env sigma atom) with |
| 204 |
`Eq(t,a,b) -> `Neq(t,a,b) |
| 205 |
| `Other(p) -> `Nother(p) |
| 206 |
end |
| 207 |
| _ -> |
| 208 |
atom_of_constr b env sigma term |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
let make_prb env sigma concl depth additional_terms b = |
| 214 |
let state = empty env sigma depth in |
| 215 |
let pos_hyps = ref [] in |
| 216 |
let neg_hyps = ref [] in |
| 217 |
List.iter |
| 218 |
(fun c -> |
| 219 |
let t = decompose_term env sigma c in |
| 220 |
ignore (add_aterm state t)) additional_terms; |
| 221 |
List.iter |
| 222 |
(fun decl -> |
| 223 |
let id = NamedDecl.get_id decl in |
| 224 |
begin |
| 225 |
match litteral_of_constr b env sigma (NamedDecl.get_type decl) with |
| 226 |
`Eq (t,a,b) -> add_equality state id a b |
| 227 |
| `Neq (t,a,b) -> add_disequality state (Hyp (Constr.mkVar id)) a b |
| 228 |
| `Other ph -> |
| 229 |
List.iter |
| 230 |
(fun (idn,nh) -> |
| 231 |
add_disequality state (HeqnH (id, idn)) ph nh) |
| 232 |
!neg_hyps; |
| 233 |
pos_hyps:=(id,ph):: !pos_hyps |
| 234 |
| `Nother nh -> |
| 235 |
List.iter |
| 236 |
(fun (idp,ph) -> |
| 237 |
add_disequality state (HeqnH (idp, id)) ph nh) |
| 238 |
!pos_hyps; |
| 239 |
neg_hyps:=(id,nh):: !neg_hyps |
| 240 |
| `Rule patts -> add_quant state id true patts |
| 241 |
| `Nrule patts -> add_quant state id false patts |
| 242 |
end) (named_context env); |
| 243 |
begin |
| 244 |
match atom_of_constr b env sigma concl with |
| 245 |
`Eq (t,a,b) -> add_disequality state Goal a b |
| 246 |
| `Other g -> |
| 247 |
List.iter |
| 248 |
(fun (idp,ph) -> |
| 249 |
add_disequality state (HeqG idp) ph g) !pos_hyps |
| 250 |
end; |
| 251 |
state |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
let fresh_id env id = |
| 256 |
Namegen.next_ident_away id (Environ.ids_of_named_context_val @@ Environ.named_context_val env) |
| 257 |
|
| 258 |
let app_global f args k = |
| 259 |
Tacticals.pf_constr_of_global (Lazy.force f) >>= fun fc -> k (mkApp (fc, args)) |
| 260 |
|
| 261 |
let assert_before n c = |
| 262 |
Proofview.Goal.enter begin fun gl -> |
| 263 |
let env = Proofview.Goal.env gl in |
| 264 |
let sigma = Proofview.Goal.sigma gl in |
| 265 |
let sigma, _ = type_of env sigma c in |
| 266 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (assert_before n c) |
| 267 |
end |
| 268 |
|
| 269 |
let refresh_type env evm ty = |
| 270 |
Evarsolve.refresh_universes ~status:Evd.univ_flexible ~refreshset:true |
| 271 |
(Some false) env evm ty |
| 272 |
|
| 273 |
let type_and_refresh c = |
| 274 |
Proofview.Goal.enter_one ~__LOC__ begin fun gl -> |
| 275 |
let env = Proofview.Goal.env gl in |
| 276 |
let evm = Proofview.Goal.sigma gl in |
| 277 |
|
| 278 |
let evm, ty = Typing.type_of env evm c in |
| 279 |
let evm, ty = refresh_type env evm ty in |
| 280 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS evm) (Proofview.tclUNIT ty) |
| 281 |
end |
| 282 |
|
| 283 |
let type_and_refresh_ env sigma c = |
| 284 |
let sigma, ty = Typing.type_of env sigma c in |
| 285 |
let sigma, ty = refresh_type env sigma ty in |
| 286 |
sigma, ty |
| 287 |
|
| 288 |
let constr_of_term c = EConstr.of_constr (ATerm.constr c) |
| 289 |
|
| 290 |
let app_global_ env sigma ref args = |
| 291 |
let (sigma, c) = Evd.fresh_global env sigma (Lazy.force ref) in |
| 292 |
Typing.checked_appvect env sigma c args |
| 293 |
|
| 294 |
: typ |
| 295 |
|
| 296 |
let rec proof_term env sigma (typ, lhs, rhs) p = match p.p_rule with |
| 297 |
| Ax c -> |
| 298 |
let c = EConstr.of_constr @@ constr_of_axiom c in |
| 299 |
let sigma, expected = app_global_ env sigma _eq [|typ; lhs; rhs|] in |
| 300 |
let sigma = Typing.check env sigma c expected in |
| 301 |
sigma, c |
| 302 |
| SymAx c -> |
| 303 |
let c = EConstr.of_constr @@ constr_of_axiom c in |
| 304 |
let sigma, expected = app_global_ env sigma _eq [|typ; rhs; lhs|] in |
| 305 |
let sigma = Typing.check env sigma c expected in |
| 306 |
app_global_ env sigma _sym_eq [|typ; rhs; lhs; c|] |
| 307 |
| Refl t -> |
| 308 |
let t = constr_of_term t in |
| 309 |
app_global_ env sigma _refl_equal [|typ; t|] |
| 310 |
| Trans (p1, p2) -> |
| 311 |
let t1 = constr_of_term p1.p_lhs in |
| 312 |
let t2 = constr_of_term p1.p_rhs in |
| 313 |
let t3 = constr_of_term p2.p_rhs in |
| 314 |
let sigma, p1 = proof_term env sigma (typ, t1, t2) p1 in |
| 315 |
let sigma, p2 = proof_term env sigma (typ, t2, t3) p2 in |
| 316 |
app_global_ env sigma _trans_eq [|typ; t1; t2; t3; p1; p2|] |
| 317 |
| Congr (p1, p2) -> |
| 318 |
*) |
| 319 |
*) |
| 320 |
let f = constr_of_term p1.p_lhs in |
| 321 |
let g = constr_of_term p1.p_rhs in |
| 322 |
let t = constr_of_term p2.p_lhs in |
| 323 |
let u = constr_of_term p2.p_rhs in |
| 324 |
let sigma, funty = type_and_refresh_ env sigma f in |
| 325 |
let sigma, argty = type_and_refresh_ env sigma t in |
| 326 |
let id = fresh_id env (Id.of_string "f") in |
| 327 |
let appf = mkLambda (make_annot (Name id) ERelevance.relevant, funty, mkApp (mkRel 1, [|t|])) in |
| 328 |
let sigma, p1 = proof_term env sigma (funty, f, g) p1 in |
| 329 |
let sigma, p2 = proof_term env sigma (argty, t, u) p2 in |
| 330 |
*) |
| 331 |
let sigma, lemma1 = app_global_ env sigma _f_equal [|funty; typ; appf; f; g; p1|] in |
| 332 |
} *) |
| 333 |
let sigma, lemma2 = |
| 334 |
try app_global_ env sigma _f_equal [|argty; typ; g; t; u; p2|] |
| 335 |
with e when CErrors.noncritical e -> |
| 336 |
u *) |
| 337 |
begin match Evarconv.unify_delay env sigma (mkApp (g, [|t|])) (mkApp (g, [|u|])) with |
| 338 |
| sigma -> |
| 339 |
app_global_ env sigma _refl_equal [|typ; mkApp (g, [|t|])|] |
| 340 |
| exception Evarconv.UnableToUnify _ -> |
| 341 |
CErrors.user_err (Pp.str "I don't know how to handle dependent equality") |
| 342 |
end |
| 343 |
in |
| 344 |
app_global_ env sigma _trans_eq [|typ; mkApp (f, [|t|]); mkApp (g, [|t|]); mkApp (g, [|u|]); lemma1; lemma2|] |
| 345 |
| Inject (prf, cstr, nargs, argind) -> |
| 346 |
*) |
| 347 |
let ti = constr_of_term prf.p_lhs in |
| 348 |
let tj = constr_of_term prf.p_rhs in |
| 349 |
let default = constr_of_term p.p_lhs in |
| 350 |
let special = mkRel (1 + nargs - argind) in |
| 351 |
let sigma, argty = type_and_refresh_ env sigma ti in |
| 352 |
let sigma, proj = Ccprojectability.build_projection env sigma cstr argind typ default special argty in |
| 353 |
let sigma, prf = proof_term env sigma (argty, ti, tj) prf in |
| 354 |
app_global_ env sigma _f_equal [|argty; typ; proj; ti; tj; prf|] |
| 355 |
|
| 356 |
let proof_tac (typ, lhs, rhs) p : unit Proofview.tactic = |
| 357 |
Proofview.Goal.enter begin fun gl -> |
| 358 |
let env = Proofview.Goal.env gl in |
| 359 |
let sigma = Proofview.Goal.sigma gl in |
| 360 |
let concl = Proofview.Goal.concl gl in |
| 361 |
let sigma, p = proof_term env sigma (typ, lhs, rhs) p in |
| 362 |
let sigma = Typing.check env sigma p concl in |
| 363 |
Proofview.Unsafe.tclEVARS sigma <*> exact_no_check p |
| 364 |
end |
| 365 |
|
| 366 |
let refute_tac c t1 t2 p = |
| 367 |
Proofview.Goal.enter begin fun gl -> |
| 368 |
let tt1=constr_of_term t1 and tt2=constr_of_term t2 in |
| 369 |
let hid = Tacmach.pf_get_new_id (Id.of_string "Heq") gl in |
| 370 |
let false_t=mkApp (c,[|mkVar hid|]) in |
| 371 |
let k intype = |
| 372 |
let neweq= app_global _eq [|intype;tt1;tt2|] in |
| 373 |
Tacticals.tclTHENS (neweq (assert_before (Name hid))) |
| 374 |
[proof_tac (intype, tt1, tt2) p; simplest_elim false_t] |
| 375 |
in type_and_refresh tt1 >>= k |
| 376 |
end |
| 377 |
|
| 378 |
let refine_exact_check c = |
| 379 |
Proofview.Goal.enter begin fun gl -> |
| 380 |
let env = Proofview.Goal.env gl in |
| 381 |
let sigma = Proofview.Goal.sigma gl in |
| 382 |
let sigma, _ = type_of env sigma c in |
| 383 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (exact_check c) |
| 384 |
end |
| 385 |
|
| 386 |
let convert_to_goal_tac c t1 t2 p = |
| 387 |
Proofview.Goal.enter begin fun gl -> |
| 388 |
let tt1=constr_of_term t1 and tt2=constr_of_term t2 in |
| 389 |
let k sort = |
| 390 |
let neweq= app_global _eq [|sort;tt1;tt2|] in |
| 391 |
let e = Tacmach.pf_get_new_id (Id.of_string "e") gl in |
| 392 |
let x = Tacmach.pf_get_new_id (Id.of_string "X") gl in |
| 393 |
let identity=mkLambda (make_annot (Name x) ERelevance.relevant,sort,mkRel 1) in |
| 394 |
let endt = app_global _eq_rect [|sort; tt1; identity; mkVar c; tt2; mkVar e|] in |
| 395 |
Tacticals.tclTHENS (neweq (assert_before (Name e))) |
| 396 |
[proof_tac (sort, tt1, tt2) p; endt refine_exact_check] |
| 397 |
in type_and_refresh tt2 >>= k |
| 398 |
end |
| 399 |
|
| 400 |
let convert_to_hyp_tac c1 t1 c2 t2 p = |
| 401 |
Proofview.Goal.enter begin fun gl -> |
| 402 |
let tt2=constr_of_term t2 in |
| 403 |
let h = Tacmach.pf_get_new_id (Id.of_string "H") gl in |
| 404 |
let false_t=mkApp (mkVar c2,[|mkVar h|]) in |
| 405 |
Tacticals.tclTHENS (assert_before (Name h) tt2) |
| 406 |
[convert_to_goal_tac c1 t1 t2 p; |
| 407 |
simplest_elim false_t] |
| 408 |
end |
| 409 |
|
| 410 |
|
| 411 |
let discriminate_tac cstru p = |
| 412 |
Proofview.Goal.enter begin fun gl -> |
| 413 |
let lhs=constr_of_term p.p_lhs and rhs=constr_of_term p.p_rhs in |
| 414 |
let env = Proofview.Goal.env gl in |
| 415 |
let evm = Proofview.Goal.sigma gl in |
| 416 |
let evm, intype = Typing.type_of env evm lhs in |
| 417 |
let evm, intype = refresh_type env evm intype in |
| 418 |
let hid = Tacmach.pf_get_new_id (Id.of_string "Heq") gl in |
| 419 |
let neweq=app_global _eq [|intype;lhs;rhs|] in |
| 420 |
Tacticals.tclTHEN (Proofview.Unsafe.tclEVARS evm) |
| 421 |
(Tacticals.tclTHENS (neweq (assert_before (Name hid))) |
| 422 |
[proof_tac (intype, lhs, rhs) p; Equality.discrHyp hid]) |
| 423 |
end |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
let cc_tactic depth additional_terms b = |
| 428 |
Proofview.Goal.enter begin fun gl -> |
| 429 |
let env = Proofview.Goal.env gl in |
| 430 |
let sigma = Proofview.Goal.sigma gl in |
| 431 |
let concl = Proofview.Goal.concl gl in |
| 432 |
Rocqlib.(check_required_library logic_module_name); |
| 433 |
let _ = debug_congruence (fun () -> Pp.str "Reading goal ...") in |
| 434 |
let state = make_prb env sigma concl depth additional_terms b in |
| 435 |
let _ = debug_congruence (fun () -> Pp.str "Problem built, solving ...") in |
| 436 |
let sol = execute true state in |
| 437 |
let _ = debug_congruence (fun () -> Pp.str "Computation completed.") in |
| 438 |
let uf=forest state in |
| 439 |
match sol with |
| 440 |
None -> Tacticals.tclFAIL (str (if b then "simple congruence failed" else "congruence failed")) |
| 441 |
| Some reason -> |
| 442 |
Proofview.tclORELSE (debug_congruence (fun () -> Pp.str "Goal solved, generating proof ..."); |
| 443 |
match reason with |
| 444 |
Discrimination (i,ipac,j,jpac) -> |
| 445 |
let p=build_proof env sigma uf (`Discr (i,ipac,j,jpac)) in |
| 446 |
let cstr=(get_constructor_info uf ipac.cnode).ci_constr in |
| 447 |
discriminate_tac cstr p |
| 448 |
| Incomplete terms_to_complete -> |
| 449 |
let open Glob_term in |
| 450 |
let env = Proofview.Goal.env gl in |
| 451 |
let hole = DAst.make @@ GHole (GInternalHole) in |
| 452 |
let pr_missing (c, missing) = |
| 453 |
let flags = PrintingFlags.Detype.current() in |
| 454 |
let c = Detyping.detype ~flags Detyping.Now env sigma c in |
| 455 |
let holes = List.init missing (fun _ -> hole) in |
| 456 |
Printer.pr_glob_constr_env env sigma (DAst.make @@ GApp (c, holes)) |
| 457 |
in |
| 458 |
let msg = Pp.(str "Goal is solvable by congruence but some arguments are missing." |
| 459 |
++ fnl () ++ |
| 460 |
str " Try " ++ |
| 461 |
hov 8 |
| 462 |
begin |
| 463 |
str "\"congruence with (" ++ |
| 464 |
prlist_with_sep |
| 465 |
(fun () -> str ")" ++ spc () ++ str "(") |
| 466 |
pr_missing terms_to_complete ++ |
| 467 |
str ")\"," |
| 468 |
end ++ |
| 469 |
fnl() ++ str " replacing metavariables by arbitrary terms") |
| 470 |
in |
| 471 |
Tacticals.tclFAIL msg |
| 472 |
| Contradiction dis -> |
| 473 |
let env = Proofview.Goal.env gl in |
| 474 |
let p=build_proof env sigma uf (`Prove (dis.lhs,dis.rhs)) in |
| 475 |
let ta=aterm uf dis.lhs and tb=aterm uf dis.rhs in |
| 476 |
match dis.rule with |
| 477 |
| Goal -> |
| 478 |
let lhs = constr_of_term ta in |
| 479 |
let rhs = constr_of_term tb in |
| 480 |
let sigma, typ = type_and_refresh_ env sigma lhs in |
| 481 |
Proofview.Unsafe.tclEVARS sigma <*> proof_tac (typ, lhs, rhs) p |
| 482 |
| Hyp id -> refute_tac (EConstr.of_constr id) ta tb p |
| 483 |
| HeqG id -> |
| 484 |
convert_to_goal_tac id ta tb p |
| 485 |
| HeqnH (ida,idb) -> |
| 486 |
convert_to_hyp_tac ida ta idb tb p) |
| 487 |
begin function (e, info) -> match e with |
| 488 |
| TacticErrors.NotConvertible -> |
| 489 |
Tacticals.tclFAIL |
| 490 |
(str (if b then "simple congruence failed" else "congruence failed") ++ |
| 491 |
str " (cannot build a well-typed proof)") |
| 492 |
| e -> Proofview.tclZERO ~info e |
| 493 |
end |
| 494 |
end |
| 495 |
|
| 496 |
let id t = mkLambda (make_annot Anonymous ERelevance.relevant, t, mkRel 1) |
| 497 |
|
| 498 |
|
| 499 |
let mk_neg_ty ff t nt = |
| 500 |
mkArrowR (mkArrowR ff ff) (mkArrowR t nt) |
| 501 |
|
| 502 |
|
| 503 |
let mk_neg_tm ff t nt = |
| 504 |
mkLambda (make_annot Anonymous ERelevance.relevant, mk_neg_ty ff t nt, |
| 505 |
mkLambda (make_annot Anonymous ERelevance.relevant, t, |
| 506 |
mkApp (mkRel 2,[|id ff; mkRel 1; mkRel 1|]))) |
| 507 |
|
| 508 |
|
| 509 |
let negative_concl_introf = |
| 510 |
Proofview.Goal.enter begin fun gl -> |
| 511 |
let sigma = Proofview.Goal.sigma gl in |
| 512 |
let env = Proofview.Goal.env gl in |
| 513 |
let concl = Proofview.Goal.concl gl in |
| 514 |
let nt = whd env sigma concl in |
| 515 |
match EConstr.kind sigma nt with |
| 516 |
Prod (_,_,ff) when isRefX env sigma (Lazy.force _False) ff -> introf |
| 517 |
| App (f,[|t|]) when isRefX env sigma (Lazy.force _not) f -> |
| 518 |
Tacticals.pf_constr_of_global (Lazy.force _False) >>= fun ff -> |
| 519 |
Refine.refine ~typecheck:true begin fun sigma -> |
| 520 |
let sigma, e = Evarutil.new_evar env sigma (mk_neg_ty ff t nt) in sigma, (mkApp (mk_neg_tm ff t nt, [|e|])) |
| 521 |
end >>= fun _ -> intro >>= fun _ -> intro |
| 522 |
| _ -> Tacticals.tclIDTAC |
| 523 |
end |
| 524 |
|
| 525 |
let congruence_tac depth l = |
| 526 |
let depth = Option.default 1000 depth in |
| 527 |
Tacticals.tclTHEN |
| 528 |
(Tacticals.tclREPEAT (Tacticals.tclFIRST [intro; Tacticals.tclTHEN whd_in_concl intro])) |
| 529 |
(cc_tactic depth l false) |
| 530 |
|
| 531 |
|
| 532 |
let simple_congruence_tac depth l = |
| 533 |
let depth = Option.default 1000 depth in |
| 534 |
Tacticals.tclTHENLIST [ |
| 535 |
Tacticals.tclREPEAT intro; |
| 536 |
negative_concl_introf; |
| 537 |
cc_tactic depth l true] |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
let mk_eq f c1 c2 k = |
| 551 |
Tacticals.pf_constr_of_global (Lazy.force f) >>= fun fc -> |
| 552 |
Proofview.Goal.enter begin fun gl -> |
| 553 |
let env = Proofview.Goal.env gl in |
| 554 |
let sigma = Proofview.Goal.sigma gl in |
| 555 |
let sigma, ty = type_of env sigma c1 in |
| 556 |
let sigma, ty = Evarsolve.refresh_universes (Some false) env sigma ty in |
| 557 |
let term = mkApp (fc, [| ty; c1; c2 |]) in |
| 558 |
let sigma, _ = type_of env sigma term in |
| 559 |
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (k term) |
| 560 |
end |
| 561 |
|
| 562 |
let f_equal = |
| 563 |
Proofview.Goal.enter begin fun gl -> |
| 564 |
let concl = Proofview.Goal.concl gl in |
| 565 |
let env = Proofview.Goal.env gl in |
| 566 |
let sigma = Proofview.Goal.sigma gl in |
| 567 |
let cut_eq c1 c2 = |
| 568 |
Tacticals.tclTHENS |
| 569 |
(mk_eq _eq c1 c2 Tactics.cut) |
| 570 |
[Proofview.tclUNIT ();Tacticals.tclTRY ((app_global _refl_equal [||]) apply)] |
| 571 |
in |
| 572 |
Proofview.tclORELSE |
| 573 |
begin match EConstr.kind sigma concl with |
| 574 |
| App (r,[|_;t;t'|]) when isRefX env sigma (Lazy.force _eq) r -> |
| 575 |
begin match EConstr.kind sigma t, EConstr.kind sigma t' with |
| 576 |
| App (f,v), App (f',v') when Int.equal (Array.length v) (Array.length v') -> |
| 577 |
let rec cuts i = |
| 578 |
if i < 0 then Tacticals.tclTRY (congruence_tac None []) |
| 579 |
else Tacticals.tclTHENFIRST (cut_eq v.(i) v'.(i)) (cuts (i-1)) |
| 580 |
in cuts (Array.length v - 1) |
| 581 |
| _ -> Proofview.tclUNIT () |
| 582 |
end |
| 583 |
| _ -> Proofview.tclUNIT () |
| 584 |
end |
| 585 |
begin function (e, info) -> match e with |
| 586 |
| Pretype_errors.PretypeError _ | Type_errors.TypeError _ -> Proofview.tclUNIT () |
| 587 |
| e -> Proofview.tclZERO ~info e |
| 588 |
end |
| 589 |
end |