| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Tac2types |
| 15 |
open Genredexpr |
| 16 |
open Proofview.Notations |
| 17 |
|
| 18 |
let return = Proofview.tclUNIT |
| 19 |
let thaw (f:unit -> _ Proofview.tactic) = f () |
| 20 |
|
| 21 |
let tactic_infer_flags with_evar = Pretyping.{ |
| 22 |
use_coercions = true; |
| 23 |
use_typeclasses = UseTC; |
| 24 |
solve_unification_constraints = true; |
| 25 |
fail_evar = not with_evar; |
| 26 |
expand_evars = true; |
| 27 |
program_mode = false; |
| 28 |
poly = PolyFlags.default; |
| 29 |
undeclared_evars_rr = false; |
| 30 |
unconstrained_sorts = false; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
let delayed_of_tactic tac env sigma = |
| 35 |
let _, pv = Proofview.init sigma [] in |
| 36 |
let name, poly = Id.of_string "ltac2_delayed", PolyFlags.default in |
| 37 |
let c, pv, _, _, _ = Proofview.apply ~name ~poly env tac pv in |
| 38 |
let _, sigma = Proofview.proofview pv in |
| 39 |
(sigma, c) |
| 40 |
|
| 41 |
let delayed_of_thunk r tac env sigma = |
| 42 |
delayed_of_tactic (thaw tac) env sigma |
| 43 |
|
| 44 |
let mk_bindings = function |
| 45 |
| ImplicitBindings l -> Tactypes.ImplicitBindings l |
| 46 |
| ExplicitBindings l -> |
| 47 |
let l = List.map CAst.make l in |
| 48 |
Tactypes.ExplicitBindings l |
| 49 |
| NoBindings -> Tactypes.NoBindings |
| 50 |
|
| 51 |
let mk_with_bindings (x, b) = (x, mk_bindings b) |
| 52 |
|
| 53 |
let rec mk_intro_pattern = function |
| 54 |
| IntroForthcoming b -> CAst.make @@ Tactypes.IntroForthcoming b |
| 55 |
| IntroNaming ipat -> CAst.make @@ Tactypes.IntroNaming (mk_intro_pattern_naming ipat) |
| 56 |
| IntroAction ipat -> CAst.make @@ Tactypes.IntroAction (mk_intro_pattern_action ipat) |
| 57 |
|
| 58 |
and mk_intro_pattern_naming = function |
| 59 |
| IntroIdentifier id -> Namegen.IntroIdentifier id |
| 60 |
| IntroFresh id -> Namegen.IntroFresh id |
| 61 |
| IntroAnonymous -> Namegen.IntroAnonymous |
| 62 |
|
| 63 |
and mk_intro_pattern_action = function |
| 64 |
| IntroWildcard -> Tactypes.IntroWildcard |
| 65 |
| IntroOrAndPattern ipat -> Tactypes.IntroOrAndPattern (mk_or_and_intro_pattern ipat) |
| 66 |
| IntroInjection ipats -> Tactypes.IntroInjection (List.map mk_intro_pattern ipats) |
| 67 |
| IntroApplyOn (c, ipat) -> |
| 68 |
let c = CAst.make @@ delayed_of_thunk Tac2ffi.constr c in |
| 69 |
Tactypes.IntroApplyOn (c, mk_intro_pattern ipat) |
| 70 |
| IntroRewrite b -> Tactypes.IntroRewrite b |
| 71 |
|
| 72 |
and mk_or_and_intro_pattern = function |
| 73 |
| IntroOrPattern ipatss -> |
| 74 |
Tactypes.IntroOrPattern (List.map (fun ipat -> List.map mk_intro_pattern ipat) ipatss) |
| 75 |
| IntroAndPattern ipats -> |
| 76 |
Tactypes.IntroAndPattern (List.map mk_intro_pattern ipats) |
| 77 |
|
| 78 |
let mk_intro_patterns ipat = List.map mk_intro_pattern ipat |
| 79 |
|
| 80 |
let mk_occurrences = function |
| 81 |
| AllOccurrences -> Locus.AllOccurrences |
| 82 |
| AllOccurrencesBut l -> Locus.AllOccurrencesBut l |
| 83 |
| NoOccurrences -> Locus.NoOccurrences |
| 84 |
| OnlyOccurrences l -> Locus.OnlyOccurrences l |
| 85 |
|
| 86 |
let mk_occurrences_expr occs = |
| 87 |
let occs = mk_occurrences occs in |
| 88 |
Locusops.occurrences_map (List.map (fun i -> Locus.ArgArg i)) occs |
| 89 |
|
| 90 |
let mk_hyp_location (id, occs, h) = |
| 91 |
((mk_occurrences_expr occs, id), h) |
| 92 |
|
| 93 |
let mk_clause cl = { |
| 94 |
Locus.onhyps = Option.map (fun l -> List.map mk_hyp_location l) cl.onhyps; |
| 95 |
Locus.concl_occs = mk_occurrences_expr cl.concl_occs; |
| 96 |
} |
| 97 |
|
| 98 |
let intros_patterns ev ipat = |
| 99 |
let ipat = mk_intro_patterns ipat in |
| 100 |
Tactics.intros_patterns ev ipat |
| 101 |
|
| 102 |
let apply adv ev cb cl = |
| 103 |
let map c = |
| 104 |
let c = thaw c >>= fun p -> return (mk_with_bindings p) in |
| 105 |
None, CAst.make c |
| 106 |
in |
| 107 |
let cb = List.map map cb in |
| 108 |
match cl with |
| 109 |
| None -> Tactics.apply_with_delayed_bindings_gen adv ev cb |
| 110 |
| Some (id, cl) -> |
| 111 |
let cl = Option.map mk_intro_pattern cl in |
| 112 |
Tactics.apply_delayed_in adv ev id cb cl Tacticals.tclIDTAC |
| 113 |
|
| 114 |
let mk_destruction_arg = function |
| 115 |
| ElimOnConstr c -> |
| 116 |
let c = c >>= fun c -> return (mk_with_bindings c) in |
| 117 |
Tactics.ElimOnConstr (delayed_of_tactic c) |
| 118 |
| ElimOnIdent id -> Tactics.ElimOnIdent CAst.(make id) |
| 119 |
| ElimOnAnonHyp n -> Tactics.ElimOnAnonHyp n |
| 120 |
|
| 121 |
let mk_induction_clause (arg, eqn, as_, occ) = |
| 122 |
let eqn = Option.map (fun ipat -> CAst.make @@ mk_intro_pattern_naming ipat) eqn in |
| 123 |
let as_ = Option.map (fun ipat -> CAst.make @@ mk_or_and_intro_pattern ipat) as_ in |
| 124 |
let occ = Option.map mk_clause occ in |
| 125 |
((None, mk_destruction_arg arg), (eqn, as_), occ) |
| 126 |
|
| 127 |
let induction_destruct isrec ev (ic : induction_clause list) using = |
| 128 |
let ic = List.map mk_induction_clause ic in |
| 129 |
let using = Option.map mk_with_bindings using in |
| 130 |
Induction.induction_destruct isrec ev (ic, using) |
| 131 |
|
| 132 |
let elim ev c copt = |
| 133 |
let c = mk_with_bindings c in |
| 134 |
let copt = Option.map mk_with_bindings copt in |
| 135 |
Tactics.elim ev None c copt |
| 136 |
|
| 137 |
let generalize pl = |
| 138 |
let mk_occ occs = mk_occurrences occs in |
| 139 |
let pl = List.map (fun (c, occs, na) -> (mk_occ occs, c), na) pl in |
| 140 |
Generalize.new_generalize_gen pl |
| 141 |
|
| 142 |
let general_case_analysis ev c = |
| 143 |
let c = mk_with_bindings c in |
| 144 |
Tactics.general_case_analysis ev None c |
| 145 |
|
| 146 |
let constructor_tac ev n i bnd = |
| 147 |
let bnd = mk_bindings bnd in |
| 148 |
Tactics.constructor_tac ev n i bnd |
| 149 |
|
| 150 |
let left_with_bindings ev bnd = |
| 151 |
let bnd = mk_bindings bnd in |
| 152 |
Tactics.left_with_bindings ev bnd |
| 153 |
|
| 154 |
let right_with_bindings ev bnd = |
| 155 |
let bnd = mk_bindings bnd in |
| 156 |
Tactics.right_with_bindings ev bnd |
| 157 |
|
| 158 |
let split_with_bindings ev bnd = |
| 159 |
let bnd = mk_bindings bnd in |
| 160 |
Tactics.split_with_bindings ev [bnd] |
| 161 |
|
| 162 |
let specialize c pat = |
| 163 |
let c = mk_with_bindings c in |
| 164 |
let pat = Option.map mk_intro_pattern pat in |
| 165 |
Tactics.specialize c pat |
| 166 |
|
| 167 |
let change pat c cl = |
| 168 |
Proofview.Goal.enter begin fun gl -> |
| 169 |
let c subst env sigma = |
| 170 |
let subst = Array.map_of_list snd (Id.Map.bindings subst) in |
| 171 |
Tacred.Changed (delayed_of_tactic (c subst) env sigma) |
| 172 |
in |
| 173 |
let cl = mk_clause cl in |
| 174 |
Tactics.change ~check:true pat c cl |
| 175 |
end |
| 176 |
|
| 177 |
let rewrite ev rw cl by = |
| 178 |
let map_rw (orient, repeat, c) = |
| 179 |
let c = c >>= fun c -> return (mk_with_bindings c) in |
| 180 |
(Option.default true orient, repeat, None, delayed_of_tactic c) |
| 181 |
in |
| 182 |
let rw = List.map map_rw rw in |
| 183 |
let cl = mk_clause cl in |
| 184 |
let by = Option.map (fun tac -> Tacticals.tclCOMPLETE (thaw tac), Equality.Naive) by in |
| 185 |
Equality.general_multi_rewrite ev rw cl by |
| 186 |
|
| 187 |
let setoid_rewrite orient c occs id = |
| 188 |
let c = c >>= fun c -> return (mk_with_bindings c) in |
| 189 |
let occs = mk_occurrences occs in |
| 190 |
Rewrite.cl_rewrite_clause (delayed_of_tactic c) orient occs id |
| 191 |
|
| 192 |
let rewrite_strat strat clause = |
| 193 |
Rewrite.cl_rewrite_clause_strat strat clause |
| 194 |
|
| 195 |
module RewriteStrats = |
| 196 |
struct |
| 197 |
let fix f = |
| 198 |
let f s = Proofview.Monad.map Tac2ffi.to_rewstrategy (Tac2val.apply f [Tac2ffi.of_rewstrategy s]) in |
| 199 |
Rewrite.Strategies.fix_tac f |
| 200 |
|
| 201 |
let hints i = |
| 202 |
Rewrite.Strategies.hints (Id.to_string i) |
| 203 |
|
| 204 |
let old_hints i = |
| 205 |
Rewrite.Strategies.old_hints (Id.to_string i) |
| 206 |
|
| 207 |
let one_lemma c l2r = |
| 208 |
let c env sigma = Pretyping.understand_uconstr env sigma c in |
| 209 |
Rewrite.Strategies.one_lemma c l2r None AllOccurrences |
| 210 |
|
| 211 |
let lemmas cs = |
| 212 |
let mk_c c = (); fun env sigma -> Pretyping.understand_uconstr env sigma c in |
| 213 |
let mk_c c = (mk_c c, true, None) in |
| 214 |
let cs = List.map mk_c cs in |
| 215 |
Rewrite.Strategies.lemmas cs |
| 216 |
end |
| 217 |
|
| 218 |
let symmetry cl = |
| 219 |
let cl = mk_clause cl in |
| 220 |
Tactics.intros_symmetry cl |
| 221 |
|
| 222 |
let forward fst tac ipat c = |
| 223 |
let ipat = Option.map mk_intro_pattern ipat in |
| 224 |
Tactics.forward fst tac ipat c |
| 225 |
|
| 226 |
let assert_ = function |
| 227 |
| AssertValue (id, c) -> |
| 228 |
let ipat = CAst.make @@ Tactypes.IntroNaming (Namegen.IntroIdentifier id) in |
| 229 |
Tactics.forward true None (Some ipat) c |
| 230 |
| AssertType (ipat, c, tac) -> |
| 231 |
let ipat = Option.map mk_intro_pattern ipat in |
| 232 |
let tac = Option.map (fun tac -> thaw tac) tac in |
| 233 |
Tactics.forward true (Some tac) ipat c |
| 234 |
|
| 235 |
let letin_pat_tac ev ipat na c cl = |
| 236 |
let ipat = Option.map (fun (b, ipat) -> (b, CAst.make @@ mk_intro_pattern_naming ipat)) ipat in |
| 237 |
let cl = mk_clause cl in |
| 238 |
Tactics.letin_pat_tac ev ipat na c cl |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
let map_pattern_with_occs (pat, occ) = match pat with |
| 245 |
| Pattern.PRef (GlobRef.ConstRef cst) -> (mk_occurrences occ, Inl (Evaluable.EvalConstRef cst)) |
| 246 |
| Pattern.PRef (GlobRef.VarRef id) -> (mk_occurrences occ, Inl (Evaluable.EvalVarRef id)) |
| 247 |
| _ -> (mk_occurrences occ, Inr pat) |
| 248 |
|
| 249 |
let get_evaluable_reference = function |
| 250 |
| GlobRef.VarRef id -> Proofview.tclUNIT (Evaluable.EvalVarRef id) |
| 251 |
| GlobRef.ConstRef cst -> Proofview.tclUNIT (Evaluable.EvalConstRef cst) |
| 252 |
| r -> Proofview.tclZERO (Tacred.NotEvaluableRef r) |
| 253 |
|
| 254 |
let mk_flags flags = |
| 255 |
Proofview.Monad.map |
| 256 |
(fun rConst -> { flags with rConst }) |
| 257 |
(Proofview.Monad.List.map get_evaluable_reference flags.rConst) |
| 258 |
|
| 259 |
let reduce_in red cl = |
| 260 |
let cl = mk_clause cl in |
| 261 |
Tactics.reduce red cl |
| 262 |
|
| 263 |
let reduce_constr red c = |
| 264 |
Tac2core.pf_apply begin fun env sigma -> |
| 265 |
let (redfun, _) = Redexpr.reduction_of_red_expr env red in |
| 266 |
let (sigma, ans) = redfun env sigma c in |
| 267 |
Proofview.Unsafe.tclEVARS sigma >>= fun () -> |
| 268 |
Proofview.tclUNIT ans |
| 269 |
end |
| 270 |
|
| 271 |
let simpl flags where = |
| 272 |
Proofview.Monad.map |
| 273 |
(fun flags -> |
| 274 |
let where = Option.map map_pattern_with_occs where in |
| 275 |
(Simpl (flags, where))) |
| 276 |
(mk_flags flags) |
| 277 |
|
| 278 |
let cbv flags = |
| 279 |
Proofview.Monad.map |
| 280 |
(fun flags -> Cbv flags) |
| 281 |
(mk_flags flags) |
| 282 |
|
| 283 |
let cbn flags = |
| 284 |
Proofview.Monad.map |
| 285 |
(fun flags -> Cbn flags) |
| 286 |
(mk_flags flags) |
| 287 |
|
| 288 |
let lazy_ flags = |
| 289 |
Proofview.Monad.map |
| 290 |
(fun flags -> Lazy flags) |
| 291 |
(mk_flags flags) |
| 292 |
|
| 293 |
let unfold occs = |
| 294 |
let map (gr, occ) = |
| 295 |
let occ = mk_occurrences occ in |
| 296 |
get_evaluable_reference gr >>= fun gr -> Proofview.tclUNIT (occ, gr) |
| 297 |
in |
| 298 |
Proofview.Monad.map |
| 299 |
(fun occs -> Unfold occs) |
| 300 |
(Proofview.Monad.List.map map occs) |
| 301 |
|
| 302 |
let pattern where = |
| 303 |
let where = List.map (fun (c, occ) -> (mk_occurrences occ, c)) where in |
| 304 |
Pattern where |
| 305 |
|
| 306 |
let vm where = |
| 307 |
let where = Option.map map_pattern_with_occs where in |
| 308 |
CbvVm where |
| 309 |
|
| 310 |
let native where = |
| 311 |
let where = Option.map map_pattern_with_occs where in |
| 312 |
CbvNative where |
| 313 |
|
| 314 |
let on_destruction_arg tac ev arg = |
| 315 |
Proofview.Goal.enter begin fun gl -> |
| 316 |
match arg with |
| 317 |
| None -> tac ev None |
| 318 |
| Some (clear, arg) -> |
| 319 |
let arg = match arg with |
| 320 |
| ElimOnConstr c -> |
| 321 |
let env = Proofview.Goal.env gl in |
| 322 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 323 |
c >>= fun (c, lbind) -> |
| 324 |
let lbind = mk_bindings lbind in |
| 325 |
Proofview.tclEVARMAP >>= fun sigma' -> |
| 326 |
let flags = tactic_infer_flags ev in |
| 327 |
let (sigma', c) = Tactics.finish_evar_resolution ~flags env sigma' (Some sigma, c) in |
| 328 |
Proofview.tclUNIT (Some sigma', Tactics.ElimOnConstr (c, lbind)) |
| 329 |
| ElimOnIdent id -> Proofview.tclUNIT (None, Tactics.ElimOnIdent CAst.(make id)) |
| 330 |
| ElimOnAnonHyp n -> Proofview.tclUNIT (None, Tactics.ElimOnAnonHyp n) |
| 331 |
in |
| 332 |
arg >>= fun (sigma', arg) -> |
| 333 |
let arg = Some (clear, arg) in |
| 334 |
match sigma' with |
| 335 |
| None -> tac ev arg |
| 336 |
| Some sigma' -> |
| 337 |
Tacticals.tclWITHHOLES ev (tac ev arg) sigma' |
| 338 |
end |
| 339 |
|
| 340 |
let discriminate ev arg = |
| 341 |
let arg = Option.map (fun arg -> None, arg) arg in |
| 342 |
on_destruction_arg Equality.discr_tac ev arg |
| 343 |
|
| 344 |
let injection ev ipat arg = |
| 345 |
let arg = Option.map (fun arg -> None, arg) arg in |
| 346 |
let ipat = Option.map mk_intro_patterns ipat in |
| 347 |
let tac ev arg = Equality.injClause None ipat ev arg in |
| 348 |
on_destruction_arg tac ev arg |
| 349 |
|
| 350 |
let autorewrite ~all by ids cl = |
| 351 |
let conds = if all then Some Equality.AllMatches else None in |
| 352 |
let ids = List.map Id.to_string ids in |
| 353 |
let cl = mk_clause cl in |
| 354 |
match by with |
| 355 |
| None -> Autorewrite.auto_multi_rewrite ?conds ids cl |
| 356 |
| Some by -> |
| 357 |
let by = thaw by in |
| 358 |
Autorewrite.auto_multi_rewrite_with ?conds by ids cl |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
let delayed_of_globref gr = (); fun env sigma -> |
| 363 |
Evd.fresh_global env sigma gr |
| 364 |
|
| 365 |
let trivial debug lems dbs = |
| 366 |
let lems = List.map delayed_of_globref lems in |
| 367 |
let dbs = Option.map (fun l -> List.map Id.to_string l) dbs in |
| 368 |
Auto.gen_trivial ~debug lems dbs |
| 369 |
|
| 370 |
let auto debug n lems dbs = |
| 371 |
let lems = List.map delayed_of_globref lems in |
| 372 |
let dbs = Option.map (fun l -> List.map Id.to_string l) dbs in |
| 373 |
Auto.gen_auto ~debug n lems dbs |
| 374 |
|
| 375 |
let eauto debug n lems dbs = |
| 376 |
let lems = List.map delayed_of_globref lems in |
| 377 |
let dbs = Option.map (fun l -> List.map Id.to_string l) dbs in |
| 378 |
Eauto.gen_eauto ~debug ?depth:n lems dbs |
| 379 |
|
| 380 |
let typeclasses_eauto strategy depth dbs = |
| 381 |
let only_classes, dbs = match dbs with |
| 382 |
| None -> |
| 383 |
true, [Class_tactics.typeclasses_db] |
| 384 |
| Some dbs -> |
| 385 |
let dbs = List.map Id.to_string dbs in |
| 386 |
false, dbs |
| 387 |
in |
| 388 |
Class_tactics.typeclasses_eauto ~only_classes ?strategy ~depth dbs |
| 389 |
|
| 390 |
let unify x y = Tactics.unify x y |
| 391 |
|
| 392 |
let current_transparent_state () = |
| 393 |
Proofview.tclENV >>= fun env -> |
| 394 |
let state = Conv_oracle.get_transp_state (Environ.oracle env) in |
| 395 |
Proofview.tclUNIT state |
| 396 |
|
| 397 |
let evarconv_unify state x y = Tactics.evarconv_unify ~state x y |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
let inversion knd arg pat ids = |
| 402 |
let ids = match ids with |
| 403 |
| None -> [] |
| 404 |
| Some l -> l |
| 405 |
in |
| 406 |
begin match pat with |
| 407 |
| None -> Proofview.tclUNIT None |
| 408 |
| Some (IntroAction (IntroOrAndPattern p)) -> |
| 409 |
Proofview.tclUNIT (Some (CAst.make @@ mk_or_and_intro_pattern p)) |
| 410 |
| Some _ -> |
| 411 |
Tacticals.tclZEROMSG (str "Inversion only accept disjunctive patterns") |
| 412 |
end >>= fun pat -> |
| 413 |
let inversion _ arg = |
| 414 |
begin match arg with |
| 415 |
| None -> assert false |
| 416 |
| Some (_, Tactics.ElimOnAnonHyp n) -> |
| 417 |
Inv.inv_clause knd pat ids (AnonHyp n) |
| 418 |
| Some (_, Tactics.ElimOnIdent id) -> |
| 419 |
Inv.inv_clause knd pat ids (NamedHyp id) |
| 420 |
| Some (_, Tactics.ElimOnConstr c) -> |
| 421 |
let open Tactypes in |
| 422 |
let anon = CAst.make @@ IntroNaming Namegen.IntroAnonymous in |
| 423 |
Tactics.specialize c (Some anon) >>= fun () -> |
| 424 |
Tacticals.onLastHypId (fun id -> Inv.inv_clause knd pat ids (NamedHyp (CAst.make id))) |
| 425 |
end |
| 426 |
in |
| 427 |
on_destruction_arg inversion true (Some (None, arg)) |
| 428 |
|
| 429 |
let contradiction c = |
| 430 |
let c = Option.map mk_with_bindings c in |
| 431 |
Contradiction.contradiction c |
| 432 |
|
| 433 |
let congruence n l = Cc_core_plugin.Cctac.congruence_tac n (Option.default [] l) |
| 434 |
|
| 435 |
let simple_congruence n l = Cc_core_plugin.Cctac.simple_congruence_tac n (Option.default [] l) |
| 436 |
|
| 437 |
let f_equal = Cc_core_plugin.Cctac.f_equal |