| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open Names |
| 14 |
open Termops |
| 15 |
open Tactics |
| 16 |
open Proofview.Notations |
| 17 |
open Hints |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let compute_secvars gl = |
| 28 |
let hyps = Proofview.Goal.hyps gl in |
| 29 |
secvars_of_hyps hyps |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
open Unification |
| 35 |
|
| 36 |
let auto_core_unif_flags_of st1 st2 = { |
| 37 |
modulo_conv_on_closed_terms = Some st1; |
| 38 |
use_metas_eagerly_in_conv_on_closed_terms = true; |
| 39 |
use_evars_eagerly_in_conv_on_closed_terms = false; |
| 40 |
modulo_delta = st2; |
| 41 |
modulo_delta_types = TransparentState.full; |
| 42 |
check_applied_meta_types = false; |
| 43 |
use_pattern_unification = false; |
| 44 |
use_meta_bound_pattern_unification = true; |
| 45 |
allowed_evars = Evarsolve.AllowedEvars.all; |
| 46 |
restrict_conv_on_strict_subterms = false; |
| 47 |
modulo_betaiota = false; |
| 48 |
modulo_eta = true; |
| 49 |
} |
| 50 |
|
| 51 |
let auto_unif_flags_of st1 st2 = |
| 52 |
let flags = auto_core_unif_flags_of st1 st2 in { |
| 53 |
core_unify_flags = flags; |
| 54 |
merge_unify_flags = flags; |
| 55 |
subterm_unify_flags = { flags with modulo_delta = TransparentState.empty }; |
| 56 |
allow_K_in_toplevel_higher_order_unification = false; |
| 57 |
resolve_evars = true |
| 58 |
} |
| 59 |
|
| 60 |
let auto_unif_flags = |
| 61 |
auto_unif_flags_of TransparentState.full TransparentState.empty |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
let unify_resolve flags h = Hints.hint_res_pf ~flags h |
| 66 |
let unify_resolve_nodelta h = Hints.hint_res_pf ~flags:auto_unif_flags h |
| 67 |
|
| 68 |
let exact h = |
| 69 |
Proofview.Goal.enter begin fun gl -> |
| 70 |
let env = Proofview.Goal.env gl in |
| 71 |
let sigma = Proofview.Goal.sigma gl in |
| 72 |
let sigma, c = Hints.fresh_hint env sigma h in |
| 73 |
let sigma, t = Typing.type_of env sigma c in |
| 74 |
let concl = Proofview.Goal.concl gl in |
| 75 |
if occur_existential sigma t || occur_existential sigma concl then |
| 76 |
try |
| 77 |
let _, sigma = Unification.w_unify env sigma CONV ~flags:auto_unif_flags concl t in |
| 78 |
Proofview.Unsafe.tclEVARSADVANCE sigma <*> |
| 79 |
exact_no_check c |
| 80 |
with e when CErrors.noncritical e -> Proofview.tclZERO e |
| 81 |
else Proofview.Unsafe.tclEVARS sigma <*> exact_check c |
| 82 |
end |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
n |
| 88 |
|
| 89 |
|
| 90 |
ne |
| 91 |
|
| 92 |
me |
| 93 |
. |
| 94 |
|
| 95 |
|
| 96 |
) |
| 97 |
|
| 98 |
let conclPattern concl pat tac = |
| 99 |
let constr_bindings env sigma = |
| 100 |
match pat with |
| 101 |
| None -> Proofview.tclUNIT Id.Map.empty |
| 102 |
| Some pat -> |
| 103 |
try |
| 104 |
Proofview.tclUNIT (Constr_matching.matches env sigma pat concl) |
| 105 |
with Constr_matching.PatternMatchingFailure as exn -> |
| 106 |
let _, info = Exninfo.capture exn in |
| 107 |
Tacticals.tclZEROMSG ~info (str "pattern-matching failed") |
| 108 |
in |
| 109 |
Proofview.Goal.enter begin fun gl -> |
| 110 |
let env = Proofview.Goal.env gl in |
| 111 |
let sigma = Proofview.Goal.sigma gl in |
| 112 |
constr_bindings env sigma >>= fun constr_bindings -> |
| 113 |
let inj c = Geninterp.Val.inject (Geninterp.val_tag (Genarg.topwit Stdarg.wit_constr)) c in |
| 114 |
let fold id c accu = Id.Map.add id (inj c) accu in |
| 115 |
let lfun = Id.Map.fold fold constr_bindings Id.Map.empty in |
| 116 |
Gentactic.interp ~lfun tac |
| 117 |
end |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
let global_debug_trivial = ref false |
| 128 |
let global_debug_auto = ref false |
| 129 |
let global_info_trivial = ref false |
| 130 |
let global_info_auto = ref false |
| 131 |
|
| 132 |
let add_option ls refe = |
| 133 |
Goptions.(declare_bool_option |
| 134 |
{ optstage = Summary.Stage.Interp; |
| 135 |
optdepr = None; |
| 136 |
optkey = ls; |
| 137 |
optread = (fun () -> !refe); |
| 138 |
optwrite = (:=) refe }) |
| 139 |
|
| 140 |
let () = |
| 141 |
add_option ["Debug";"Trivial"] global_debug_trivial; |
| 142 |
add_option ["Debug";"Auto"] global_debug_auto; |
| 143 |
add_option ["Info";"Trivial"] global_info_trivial; |
| 144 |
add_option ["Info";"Auto"] global_info_auto |
| 145 |
|
| 146 |
type debug_kind = ReportForTrivial | ReportForAuto |
| 147 |
|
| 148 |
let no_dbg (_,whatfor,_,_) = (Off,whatfor,0,ref []) |
| 149 |
|
| 150 |
let mk_trivial_dbg debug = |
| 151 |
let d = |
| 152 |
if debug == Debug || !global_debug_trivial then Debug |
| 153 |
else if debug == Info || !global_info_trivial then Info |
| 154 |
else Off |
| 155 |
in (d,ReportForTrivial,0,ref []) |
| 156 |
|
| 157 |
let mk_auto_dbg debug = |
| 158 |
let d = |
| 159 |
if debug == Debug || !global_debug_auto then Debug |
| 160 |
else if debug == Info || !global_info_auto then Info |
| 161 |
else Off |
| 162 |
in (d,ReportForAuto,0,ref []) |
| 163 |
|
| 164 |
let incr_dbg = function (dbg,whatfor,depth,trace) -> (dbg,whatfor,depth+1,trace) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
let tclLOG (dbg,_,depth,trace) pp tac = |
| 169 |
match dbg with |
| 170 |
| Off -> tac |
| 171 |
| Debug -> |
| 172 |
|
| 173 |
let s = String.make (depth+1) '*' in |
| 174 |
Proofview.(tclIFCATCH ( |
| 175 |
tac >>= fun v -> |
| 176 |
tclENV >>= fun env -> |
| 177 |
tclEVARMAP >>= fun sigma -> |
| 178 |
Feedback.msg_notice (str s ++ spc () ++ pp env sigma ++ str ". (*success*)"); |
| 179 |
tclUNIT v |
| 180 |
) tclUNIT |
| 181 |
(fun (exn, info) -> |
| 182 |
tclENV >>= fun env -> |
| 183 |
tclEVARMAP >>= fun sigma -> |
| 184 |
Feedback.msg_notice (str s ++ spc () ++ pp env sigma ++ str ". (*fail*)"); |
| 185 |
tclZERO ~info exn)) |
| 186 |
| Info -> |
| 187 |
|
| 188 |
Proofview.(tclIFCATCH ( |
| 189 |
tac >>= fun v -> |
| 190 |
trace := (depth, Some pp) :: !trace; |
| 191 |
tclUNIT v |
| 192 |
) Proofview.tclUNIT |
| 193 |
(fun (exn, info) -> |
| 194 |
trace := (depth, None) :: !trace; |
| 195 |
tclZERO ~info exn)) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
let rec cleanup_info_trace depth acc = function |
| 205 |
| [] -> acc |
| 206 |
| (d,Some pp) :: l -> cleanup_info_trace d ((d,pp)::acc) l |
| 207 |
| l -> cleanup_info_trace depth acc (erase_subtree depth l) |
| 208 |
|
| 209 |
and erase_subtree depth = function |
| 210 |
| [] -> [] |
| 211 |
| (d,_) :: l -> if Int.equal d depth then l else erase_subtree depth l |
| 212 |
|
| 213 |
let pr_info_atom env sigma (d,pp) = |
| 214 |
str (String.make d ' ') ++ pp env sigma ++ str "." |
| 215 |
|
| 216 |
let pr_info_trace env sigma = function |
| 217 |
| (Info,_,_,{contents=(d,Some pp)::l}) -> |
| 218 |
Feedback.msg_notice (prlist_with_sep fnl (pr_info_atom env sigma) (cleanup_info_trace d [(d,pp)] l)) |
| 219 |
| _ -> () |
| 220 |
|
| 221 |
let pr_info_nop = function |
| 222 |
| (Info,_,_,_) -> Feedback.msg_notice (str "idtac.") |
| 223 |
| _ -> () |
| 224 |
|
| 225 |
let pr_dbg_header = function |
| 226 |
| (Off,_,_,_) -> () |
| 227 |
| (Debug,ReportForTrivial,_,_) -> Feedback.msg_notice (str "(* debug trivial: *)") |
| 228 |
| (Debug,ReportForAuto,_,_) -> Feedback.msg_notice (str "(* debug auto: *)") |
| 229 |
| (Info,ReportForTrivial,_,_) -> Feedback.msg_notice (str "(* info trivial: *)") |
| 230 |
| (Info,ReportForAuto,_,_) -> Feedback.msg_notice (str "(* info auto: *)") |
| 231 |
|
| 232 |
let tclTRY_dbg d tac = |
| 233 |
let delay f = Proofview.tclUNIT () >>= fun () -> f () in |
| 234 |
let tac = |
| 235 |
delay (fun () -> pr_dbg_header d; tac) >>= fun () -> |
| 236 |
Proofview.tclENV >>= fun env -> |
| 237 |
Proofview.tclEVARMAP >>= fun sigma -> |
| 238 |
pr_info_trace env sigma d; |
| 239 |
Proofview.tclUNIT () in |
| 240 |
let after = delay (fun () -> pr_info_nop d; Proofview.tclUNIT ()) in |
| 241 |
Tacticals.tclORELSE0 tac after |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
ase |
| 249 |
s *) |
| 250 |
|
| 251 |
let auto_flags_of_state st = |
| 252 |
auto_unif_flags_of TransparentState.full st |
| 253 |
|
| 254 |
let hintmap_of env sigma secvars hdc concl = |
| 255 |
match hdc with |
| 256 |
| None -> Hint_db.map_none ~secvars |
| 257 |
| Some hdc -> |
| 258 |
if occur_existential sigma concl then |
| 259 |
(fun db -> match Hint_db.map_eauto env sigma ~secvars hdc concl db with |
| 260 |
| ModeMatch (_, l) -> l |
| 261 |
| ModeMismatch -> []) |
| 262 |
else Hint_db.map_auto env sigma ~secvars hdc concl |
| 263 |
|
| 264 |
let exists_evaluable_reference env = function |
| 265 |
| Evaluable.EvalConstRef _ -> true |
| 266 |
| Evaluable.EvalProjectionRef _ -> true |
| 267 |
| Evaluable.EvalVarRef v -> try ignore(Environ.lookup_named v env); true with Not_found -> false |
| 268 |
|
| 269 |
let dbg_intro dbg = tclLOG dbg (fun _ _ -> str "intro") intro |
| 270 |
let dbg_assumption dbg = tclLOG dbg (fun _ _ -> str "assumption") assumption |
| 271 |
|
| 272 |
let intro_register dbg kont db = |
| 273 |
Proofview.tclTHEN (dbg_intro dbg) @@ |
| 274 |
Proofview.Goal.enter begin fun gl -> |
| 275 |
let extend_local_db decl db = |
| 276 |
let env = Proofview.Goal.env gl in |
| 277 |
let sigma = Proofview.Goal.sigma gl in |
| 278 |
push_resolve_hyp env sigma (Context.Named.Declaration.get_id decl) db |
| 279 |
in |
| 280 |
Tacticals.onLastDecl (fun decl -> kont (extend_local_db decl db)) |
| 281 |
end |
| 282 |
|
| 283 |
let rec trivial_fail_db dbg db_list local_db = |
| 284 |
Proofview.tclINDEPENDENT @@ |
| 285 |
Tacticals.tclORELSE0 (dbg_assumption dbg) @@ |
| 286 |
Tacticals.tclORELSE0 (intro_register dbg (trivial_fail_db dbg db_list) local_db) @@ |
| 287 |
Proofview.Goal.enter begin fun gl -> |
| 288 |
let env = Proofview.Goal.env gl in |
| 289 |
let sigma = Proofview.Goal.sigma gl in |
| 290 |
let concl = Proofview.Goal.concl gl in |
| 291 |
let secvars = compute_secvars gl in |
| 292 |
let hdc = try Some (decompose_app_bound sigma concl) with Bound -> None in |
| 293 |
let hintmap = hintmap_of env sigma secvars hdc concl in |
| 294 |
let hinttac = tac_of_hint dbg db_list local_db concl in |
| 295 |
(local_db::db_list) |
| 296 |
|> List.map_append (fun db -> try hintmap db with Not_found -> []) |
| 297 |
|> List.filter_map begin fun h -> |
| 298 |
if Int.equal (FullHint.priority h) 0 then |
| 299 |
Some (Tacticals.tclCOMPLETE (hinttac h)) |
| 300 |
else None |
| 301 |
end |
| 302 |
|> Tacticals.tclFIRST |
| 303 |
end |
| 304 |
|
| 305 |
and tac_of_hint dbg db_list local_db concl = |
| 306 |
let tactic = function |
| 307 |
| Res_pf h -> unify_resolve_nodelta h |
| 308 |
| ERes_pf _ -> Proofview.Goal.enter (fun gl -> |
| 309 |
let info = Exninfo.reify () in |
| 310 |
Tacticals.tclZEROMSG ~info (str "eres_pf")) |
| 311 |
| Give_exact h -> exact h |
| 312 |
| Res_pf_THEN_trivial_fail h -> |
| 313 |
Tacticals.tclTHEN |
| 314 |
(unify_resolve_nodelta h) |
| 315 |
|
| 316 |
|
| 317 |
(trivial_fail_db (no_dbg dbg) db_list local_db) |
| 318 |
| Unfold_nth c -> |
| 319 |
Proofview.Goal.enter begin fun gl -> |
| 320 |
if exists_evaluable_reference (Proofview.Goal.env gl) c then |
| 321 |
Tacticals.tclPROGRESS (reduce (Unfold [AllOccurrences,c]) Locusops.onConcl) |
| 322 |
else |
| 323 |
let info = Exninfo.reify () in |
| 324 |
Tacticals.tclFAIL ~info (str"Unbound reference") |
| 325 |
end |
| 326 |
| Extern (p, tacast) -> |
| 327 |
conclPattern concl p tacast |
| 328 |
in |
| 329 |
let pr_hint h env sigma = |
| 330 |
let origin = match FullHint.database h with |
| 331 |
| None -> mt () |
| 332 |
| Some n -> str " (in " ++ str n ++ str ")" |
| 333 |
in |
| 334 |
FullHint.print env sigma h ++ origin |
| 335 |
in |
| 336 |
fun h -> tclLOG dbg (pr_hint h) (FullHint.run h tactic) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
let gen_trivial ?(debug=Off) lems dbnames = |
| 342 |
Proofview.Goal.enter begin fun gl -> |
| 343 |
let env = Proofview.Goal.env gl in |
| 344 |
let sigma = Proofview.Goal.sigma gl in |
| 345 |
let db_list = |
| 346 |
match dbnames with |
| 347 |
| Some dbnames -> make_db_list dbnames |
| 348 |
| None -> current_pure_db () |
| 349 |
in |
| 350 |
let d = mk_trivial_dbg debug in |
| 351 |
let local_db = make_local_hint_db env sigma false lems in |
| 352 |
tclTRY_dbg d (trivial_fail_db d db_list local_db) |
| 353 |
end |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
exception SearchBound |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
let search d n db_list lems = |
| 365 |
let make_local_db gl = |
| 366 |
let env = Proofview.Goal.env gl in |
| 367 |
let sigma = Proofview.Goal.sigma gl in |
| 368 |
make_local_hint_db env sigma false lems |
| 369 |
in |
| 370 |
let rec search d n local_db = |
| 371 |
if Int.equal n 0 then |
| 372 |
let info = Exninfo.reify () in |
| 373 |
Proofview.tclZERO ~info SearchBound |
| 374 |
else |
| 375 |
Tacticals.tclORELSE0 (dbg_assumption d) @@ |
| 376 |
Tacticals.tclORELSE0 (intro_register d (search d n) local_db) @@ |
| 377 |
Proofview.Goal.enter begin fun gl -> |
| 378 |
let env = Proofview.Goal.env gl in |
| 379 |
let sigma = Proofview.Goal.sigma gl in |
| 380 |
let concl = Proofview.Goal.concl gl in |
| 381 |
let hyps = Proofview.Goal.hyps gl in |
| 382 |
let d' = incr_dbg d in |
| 383 |
let secvars = compute_secvars gl in |
| 384 |
let hdc = try Some (decompose_app_bound sigma concl) with Bound -> None in |
| 385 |
let hintmap = hintmap_of env sigma secvars hdc concl in |
| 386 |
let hinttac = tac_of_hint d db_list local_db concl in |
| 387 |
(local_db::db_list) |
| 388 |
|> List.map_append (fun db -> try hintmap db with Not_found -> []) |
| 389 |
|> List.map begin fun h -> |
| 390 |
Proofview.tclTHEN (hinttac h) @@ |
| 391 |
Proofview.Goal.enter begin fun gl -> |
| 392 |
let hyps' = Proofview.Goal.hyps gl in |
| 393 |
let local_db' = |
| 394 |
|
| 395 |
if hyps' == hyps then local_db else make_local_db gl |
| 396 |
in |
| 397 |
search d' (n-1) local_db' |
| 398 |
end |
| 399 |
end |
| 400 |
|> Tacticals.tclFIRST |
| 401 |
end |
| 402 |
in |
| 403 |
Proofview.Goal.enter begin fun gl -> |
| 404 |
search d n (make_local_db gl) |
| 405 |
end |
| 406 |
|
| 407 |
let default_search_depth = 5 |
| 408 |
|
| 409 |
let gen_auto ?(debug=Off) n lems dbnames = |
| 410 |
Proofview.Goal.enter begin fun gl -> |
| 411 |
let n = match n with None -> default_search_depth | Some n -> n in |
| 412 |
let db_list = |
| 413 |
match dbnames with |
| 414 |
| Some dbnames -> make_db_list dbnames |
| 415 |
| None -> current_pure_db () |
| 416 |
in |
| 417 |
let d = mk_auto_dbg debug in |
| 418 |
tclTRY_dbg d (search d n db_list lems) |
| 419 |
end |
| 420 |
|
| 421 |
let auto ?(debug=Off) n lems dbnames = gen_auto ~debug (Some n) lems (Some dbnames) |
| 422 |
|
| 423 |
let default_auto = auto default_search_depth [] [] |