File ‹Tools/Meson/meson.ML›
signature MESON =
sig
type simp_options = {if_simps : bool, let_simps : bool}
val simp_options_all_true : simp_options
val trace : bool Config.T
val max_clauses : int Config.T
val first_order_resolve : Proof.context -> thm -> thm -> thm
val size_of_subgoals: thm -> int
val has_too_many_clauses: Proof.context -> term -> bool
val make_cnf: thm list -> thm -> Proof.context -> thm list * Proof.context
val finish_cnf: thm list -> thm list
val presimplified_consts : string list
val presimplify: simp_options -> Proof.context -> thm -> thm
val make_nnf: simp_options -> Proof.context -> thm -> thm
val choice_theorems : theory -> thm list
val skolemize_with_choice_theorems : simp_options -> Proof.context -> thm list -> thm -> thm
val skolemize : simp_options -> Proof.context -> thm -> thm
val cong_extensionalize_thm : Proof.context -> thm -> thm
val abs_extensionalize_conv : Proof.context -> conv
val abs_extensionalize_thm : Proof.context -> thm -> thm
val make_clauses_unsorted: Proof.context -> thm list -> thm list
val make_clauses: Proof.context -> thm list -> thm list
val make_horns: thm list -> thm list
val best_prolog_tac: Proof.context -> (thm -> int) -> thm list -> tactic
val depth_prolog_tac: Proof.context -> thm list -> tactic
val gocls: thm list -> thm list
val skolemize_prems_tac : simp_options -> Proof.context -> thm list -> int -> tactic
val MESON:
tactic -> (thm list -> thm list) -> (thm list -> tactic) -> Proof.context
-> int -> tactic
val best_meson_tac: (thm -> int) -> Proof.context -> int -> tactic
val safe_best_meson_tac: Proof.context -> int -> tactic
val depth_meson_tac: Proof.context -> int -> tactic
val prolog_step_tac': Proof.context -> thm list -> int -> tactic
val iter_deepen_prolog_tac: Proof.context -> thm list -> tactic
val iter_deepen_meson_tac: Proof.context -> thm list -> int -> tactic
val make_meta_clause: Proof.context -> thm -> thm
val make_meta_clauses: Proof.context -> thm list -> thm list
val meson_tac: Proof.context -> thm list -> int -> tactic
end
structure Meson : MESON =
struct
type simp_options = {if_simps : bool, let_simps : bool}
val simp_options_all_true = {if_simps = true, let_simps = true}
val trace = Attrib.setup_config_bool \<^binding>‹meson_trace› (K false)
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
val max_clauses = Attrib.setup_config_int \<^binding>‹meson_max_clauses› (K 60)
val iter_deepen_limit = 50;
val disj_forward = @{thm disj_forward};
val disj_forward2 = @{thm disj_forward2};
val make_pos_rule = @{thm make_pos_rule};
val make_pos_rule' = @{thm make_pos_rule'};
val make_pos_goal = @{thm make_pos_goal};
val make_neg_rule = @{thm make_neg_rule};
val make_neg_rule' = @{thm make_neg_rule'};
val make_neg_goal = @{thm make_neg_goal};
val conj_forward = @{thm conj_forward};
val all_forward = @{thm all_forward};
val ex_forward = @{thm ex_forward};
val not_conjD = @{thm not_conjD};
val not_disjD = @{thm not_disjD};
val not_notD = @{thm not_notD};
val not_allD = @{thm not_allD};
val not_exD = @{thm not_exD};
val imp_to_disjD = @{thm imp_to_disjD};
val not_impD = @{thm not_impD};
val iff_to_disjD = @{thm iff_to_disjD};
val not_iffD = @{thm not_iffD};
val conj_exD1 = @{thm conj_exD1};
val conj_exD2 = @{thm conj_exD2};
val disj_exD = @{thm disj_exD};
val disj_exD1 = @{thm disj_exD1};
val disj_exD2 = @{thm disj_exD2};
val disj_assoc = @{thm disj_assoc};
val disj_comm = @{thm disj_comm};
val disj_FalseD1 = @{thm disj_FalseD1};
val disj_FalseD2 = @{thm disj_FalseD2};
fun first_order_resolve ctxt thA thB =
(case
\<^try>‹
let val thy = Proof_Context.theory_of ctxt
val tmA = Thm.concl_of thA
val \<^Const_>‹Pure.imp for tmB _› = Thm.prop_of thB
val tenv =
Pattern.first_order_match thy (tmB, tmA)
(Vartab.empty, Vartab.empty) |> snd
val insts = Vartab.fold (fn (xi, (_, t)) => cons (xi, Thm.cterm_of ctxt t)) tenv [];
in thA RS (infer_instantiate ctxt insts thB) end› of
SOME th => th
| NONE => raise THM ("first_order_resolve", 0, [thA, thB]))
val protect_prefix = "Meson_xyzzy"
fun protect_bound_var_names (t $ u) =
protect_bound_var_names t $ protect_bound_var_names u
| protect_bound_var_names (Abs (s, T, t')) =
Abs (protect_prefix ^ s, T, protect_bound_var_names t')
| protect_bound_var_names t = t
fun fix_bound_var_names old_t new_t =
let
fun quant_of \<^const_name>‹All› = SOME true
| quant_of \<^const_name>‹Ball› = SOME true
| quant_of \<^const_name>‹Ex› = SOME false
| quant_of \<^const_name>‹Bex› = SOME false
| quant_of _ = NONE
val flip_quant = Option.map not
fun some_eq (SOME x) (SOME y) = x = y
| some_eq _ _ = false
fun add_names quant (Const (quant_s, _) $ Abs (s, _, t')) =
add_names quant t' #> some_eq quant (quant_of quant_s) ? cons s
| add_names quant \<^Const_>‹Not for t› = add_names (flip_quant quant) t
| add_names quant \<^Const_>‹implies for t1 t2› =
add_names (flip_quant quant) t1 #> add_names quant t2
| add_names quant (t1 $ t2) = fold (add_names quant) [t1, t2]
| add_names _ _ = I
fun lost_names quant =
subtract (op =) (add_names quant new_t []) (add_names quant old_t [])
fun aux ((t1 as Const (quant_s, _)) $ (Abs (s, T, t'))) =
t1 $ Abs (s |> String.isPrefix protect_prefix s
? perhaps (try (fn _ => hd (lost_names (quant_of quant_s)))),
T, aux t')
| aux (t1 $ t2) = aux t1 $ aux t2
| aux t = t
in aux new_t end
fun rename_bound_vars_RS th rl =
let
val t = Thm.concl_of th
val r = Thm.concl_of rl
val th' = th RS Thm.rename_boundvars r (protect_bound_var_names r) rl
val t' = Thm.concl_of th'
in Thm.rename_boundvars t' (fix_bound_var_names t t') th' end
fun tryres (th, rls) =
let fun tryall [] = raise THM("tryres", 0, th::rls)
| tryall (rl::rls) =
(rename_bound_vars_RS th rl handle THM _ => tryall rls)
in tryall rls end;
fun quant_resolve_tac ctxt th i st =
case (Thm.concl_of st, Thm.prop_of th) of
(\<^Const_>‹Trueprop for ‹Var _ $ (c as Free _)››, \<^Const_>‹Trueprop for _›) =>
let
val cc = Thm.cterm_of ctxt c
val ct = Thm.dest_arg (Thm.cprop_of th)
in resolve_tac ctxt [th] i (Thm.instantiate' [] [SOME (Thm.lambda cc ct)] st) end
| _ => resolve_tac ctxt [th] i st
fun forward_res ctxt nf st =
let
fun tacf [prem] = quant_resolve_tac ctxt (nf prem) 1
| tacf prems =
error (cat_lines
("Bad proof state in forward_res, please inform lcp@cl.cam.ac.uk:" ::
Thm.string_of_thm ctxt st ::
"Premises:" :: map (Thm.string_of_thm ctxt) prems))
in
case Seq.pull (ALLGOALS (Misc_Legacy.METAHYPS ctxt tacf) st) of
SOME (th, _) => th
| NONE => raise THM ("forward_res", 0, [st])
end;
fun has_conns bs =
let fun has (Const _) = false
| has \<^Const_>‹Trueprop for p› = has p
| has \<^Const_>‹Not for p› = has p
| has \<^Const_>‹disj for p q› = member (op =) bs \<^const_name>‹disj› orelse has p orelse has q
| has \<^Const_>‹conj for p q› = member (op =) bs \<^const_name>‹conj› orelse has p orelse has q
| has \<^Const_>‹All _ for ‹Abs(_,_,p)›› = member (op =) bs \<^const_name>‹All› orelse has p
| has \<^Const_>‹Ex _ for ‹Abs(_,_,p)›› = member (op =) bs \<^const_name>‹Ex› orelse has p
| has _ = false
in has end;
fun literals \<^Const_>‹Trueprop for P› = literals P
| literals \<^Const_>‹disj for P Q› = literals P @ literals Q
| literals \<^Const_>‹Not for P› = [(false,P)]
| literals P = [(true,P)];
val nliterals = length o literals;
fun signed_lits_aux \<^Const_>‹disj for P Q› (poslits, neglits) =
signed_lits_aux Q (signed_lits_aux P (poslits, neglits))
| signed_lits_aux \<^Const_>‹Not for P› (poslits, neglits) = (poslits, P::neglits)
| signed_lits_aux P (poslits, neglits) = (P::poslits, neglits);
fun signed_lits th = signed_lits_aux (HOLogic.dest_Trueprop (Thm.concl_of th)) ([],[]);
fun taut_poslit \<^Const_>‹HOL.eq _ for t u› = t aconv u
| taut_poslit \<^Const_>‹True› = true
| taut_poslit _ = false;
fun is_taut th =
let val (poslits,neglits) = signed_lits th
in exists taut_poslit poslits
orelse
exists (member (op aconv) neglits) (\<^term>‹False› :: poslits)
end
handle TERM _ => false;
val not_refl_disj_D = @{thm not_refl_disj_D};
fun eliminable (t as Var _, u) = t aconv u orelse not (Logic.occs(t,u))
| eliminable (u, t as Var _) = t aconv u orelse not (Logic.occs(t,u))
| eliminable _ = false;
fun refl_clause_aux 0 th = th
| refl_clause_aux n th =
case HOLogic.dest_Trueprop (Thm.concl_of th) of
\<^Const_>‹disj for \<^Const_>‹disj for _ _› _› =>
refl_clause_aux n (th RS disj_assoc)
| \<^Const_>‹disj for \<^Const_>‹Not for \<^Const_>‹HOL.eq _ for t u›› _› =>
if eliminable(t,u)
then refl_clause_aux (n-1) (th RS not_refl_disj_D)
else refl_clause_aux (n-1) (th RS disj_comm)
| \<^Const>‹disj for _ _› => refl_clause_aux n (th RS disj_comm)
| _ => th;
fun notequal_lits_count \<^Const_>‹disj for P Q› = notequal_lits_count P + notequal_lits_count Q
| notequal_lits_count \<^Const_>‹Not for \<^Const_>‹HOL.eq _ for _ _›› = 1
| notequal_lits_count _ = 0;
fun refl_clause th =
let val neqs = notequal_lits_count (HOLogic.dest_Trueprop (Thm.concl_of th))
in zero_var_indexes (refl_clause_aux neqs th) end
handle TERM _ => th;
fun forward_res2 ctxt nf hyps st =
case Seq.pull
(REPEAT
(Misc_Legacy.METAHYPS ctxt
(fn major::minors => resolve_tac ctxt [nf (minors @ hyps) major] 1) 1)
st)
of SOME(th,_) => th
| NONE => raise THM("forward_res2", 0, [st]);
fun nodups_aux ctxt rls th = nodups_aux ctxt rls (th RS disj_assoc)
handle THM _ => tryres(th,rls)
handle THM _ => tryres(forward_res2 ctxt (nodups_aux ctxt) rls (th RS disj_forward2),
[disj_FalseD1, disj_FalseD2, asm_rl])
handle THM _ => th;
fun nodups ctxt th =
if has_duplicates (op =) (literals (Thm.prop_of th))
then nodups_aux ctxt [] th
else th;
fun estimated_num_clauses bound t =
let
fun sum x y = if x < bound andalso y < bound then x+y else bound
fun prod x y = if x < bound andalso y < bound then x*y else bound
fun signed_nclauses b \<^Const_>‹Trueprop for t› = signed_nclauses b t
| signed_nclauses b \<^Const_>‹Not for t› = signed_nclauses (not b) t
| signed_nclauses b \<^Const_>‹conj for t u› =
if b then sum (signed_nclauses b t) (signed_nclauses b u)
else prod (signed_nclauses b t) (signed_nclauses b u)
| signed_nclauses b \<^Const_>‹disj for t u› =
if b then prod (signed_nclauses b t) (signed_nclauses b u)
else sum (signed_nclauses b t) (signed_nclauses b u)
| signed_nclauses b \<^Const_>‹implies for t u› =
if b then prod (signed_nclauses (not b) t) (signed_nclauses b u)
else sum (signed_nclauses (not b) t) (signed_nclauses b u)
| signed_nclauses b \<^Const_>‹HOL.eq ‹T› for t u› =
if T = HOLogic.boolT then
if b then sum (prod (signed_nclauses (not b) t) (signed_nclauses b u))
(prod (signed_nclauses (not b) u) (signed_nclauses b t))
else sum (prod (signed_nclauses b t) (signed_nclauses b u))
(prod (signed_nclauses (not b) t) (signed_nclauses (not b) u))
else 1
| signed_nclauses b \<^Const_>‹Ex _ for ‹Abs (_,_,t)›› = signed_nclauses b t
| signed_nclauses b \<^Const_>‹All _ for ‹Abs (_,_,t)›› = signed_nclauses b t
| signed_nclauses _ _ = 1;
in signed_nclauses true t end
fun has_too_many_clauses ctxt t =
let val max_cl = Config.get ctxt max_clauses in
estimated_num_clauses (max_cl + 1) t > max_cl
end
local
val spec_var =
Thm.dest_arg (Thm.dest_arg (#2 (Thm.dest_implies (Thm.cprop_of spec))))
|> Thm.term_of |> dest_Var;
fun name_of \<^Const_>‹All _ for ‹Abs(x, _, _)›› = x | name_of _ = Name.uu;
in
fun freeze_spec th ctxt =
let
val ([x], ctxt') =
Variable.variant_fixes [name_of (HOLogic.dest_Trueprop (Thm.concl_of th))] ctxt;
val spec' = spec
|> Thm.instantiate
(TVars.empty, Vars.make1 (spec_var, Thm.cterm_of ctxt' (Free (x, snd spec_var))));
in (th RS spec', ctxt') end
end;
fun apply_skolem_theorem ctxt (th, rls) =
let
fun tryall [] = raise THM ("apply_skolem_theorem", 0, th::rls)
| tryall (rl :: rls) = first_order_resolve ctxt th rl handle THM _ => tryall rls
in tryall rls end
fun cnf old_skolem_ths ctxt (th, ths) =
let val ctxt_ref = Unsynchronized.ref ctxt
fun cnf_aux (th,ths) =
if not (can HOLogic.dest_Trueprop (Thm.prop_of th)) then ths
else if not (has_conns [\<^const_name>‹All›, \<^const_name>‹Ex›, \<^const_name>‹HOL.conj›] (Thm.prop_of th))
then nodups ctxt th :: ths
else case head_of (HOLogic.dest_Trueprop (Thm.concl_of th)) of
\<^Const_>‹conj› =>
cnf_aux (th RS conjunct1, cnf_aux (th RS conjunct2, ths))
| \<^Const_>‹All _› =>
let val (th', ctxt') = freeze_spec th (! ctxt_ref)
in ctxt_ref := ctxt'; cnf_aux (th', ths) end
| \<^Const_>‹Ex _› =>
cnf_aux (apply_skolem_theorem (! ctxt_ref) (th, old_skolem_ths), ths)
| \<^Const_>‹disj› =>
let val tac = Misc_Legacy.METAHYPS ctxt (fn [prem] => resolve_tac ctxt (cnf_nil prem) 1) 1
in Seq.list_of ((tac THEN tac) (th RS disj_forward)) @ ths end
| _ => nodups ctxt th :: ths
and cnf_nil th = cnf_aux (th, [])
val cls =
if has_too_many_clauses ctxt (Thm.concl_of th) then
(trace_msg ctxt (fn () =>
"cnf is ignoring: " ^ Thm.string_of_thm ctxt th); ths)
else
cnf_aux (th, ths)
in (cls, !ctxt_ref) end
fun make_cnf old_skolem_ths th ctxt =
cnf old_skolem_ths ctxt (th, [])
fun finish_cnf ths = filter (not o is_taut) (map refl_clause ths);
fun is_left \<^Const_>‹Trueprop for \<^Const_>‹disj for \<^Const_>‹disj for _ _› _›› = true
| is_left _ = false;
fun assoc_right th =
if is_left (Thm.prop_of th) then assoc_right (th RS disj_assoc)
else th;
val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule];
val resolution_clause_rules = [disj_assoc, make_neg_rule', make_pos_rule'];
fun make_goal th =
make_goal (tryres(th, clause_rules))
handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]);
fun rigid t = not (is_Var (head_of t));
fun ok4horn \<^Const_>‹Trueprop for \<^Const_>‹disj for t _›› = rigid t
| ok4horn \<^Const_>‹Trueprop for t› = rigid t
| ok4horn _ = false;
fun make_horn crules th =
if ok4horn (Thm.concl_of th)
then make_horn crules (tryres(th,crules)) handle THM _ => th
else th;
fun add_contras crules th hcs =
let fun rots (0,_) = hcs
| rots (k,th) = zero_var_indexes (make_horn crules th) ::
rots(k-1, assoc_right (th RS disj_comm))
in case nliterals(Thm.prop_of th) of
1 => th::hcs
| n => rots(n, assoc_right th)
end;
fun name_thms label =
let fun name1 th (k, ths) =
(k-1, Thm.put_name_hint (label ^ string_of_int k) th :: ths)
in fn ths => #2 (fold_rev name1 ths (length ths, [])) end;
fun is_negative th = forall (not o #1) (literals (Thm.prop_of th));
val neg_clauses = filter is_negative;
fun rhyps (\<^Const_>‹Pure.imp for \<^Const_>‹Trueprop for A› phi›, As) = rhyps(phi, A::As)
| rhyps (_, As) = As;
fun ins_term t net = Net.insert_term (op aconv) (t, t) net;
fun has_reps [] = false
| has_reps [_] = false
| has_reps [t,u] = (t aconv u)
| has_reps ts = (fold ins_term ts Net.empty; false) handle Net.INSERT => true;
fun TRYING_eq_assume_tac 0 st = Seq.single st
| TRYING_eq_assume_tac i st =
TRYING_eq_assume_tac (i-1) (Thm.eq_assumption i st)
handle THM _ => TRYING_eq_assume_tac (i-1) st;
fun TRYALL_eq_assume_tac st = TRYING_eq_assume_tac (Thm.nprems_of st) st;
fun check_tac st =
if exists (fn prem => has_reps (rhyps(prem,[]))) (Thm.prems_of st)
then Seq.empty else Seq.single st;
fun prolog_step_tac ctxt horns i =
(assume_tac ctxt i APPEND resolve_tac ctxt horns i) THEN check_tac THEN
TRYALL_eq_assume_tac;
fun addconcl prem sz = size_of_term (Logic.strip_assums_concl prem) + sz;
fun size_of_subgoals st = fold_rev addconcl (Thm.prems_of st) 0;
val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD,
not_impD, not_iffD, not_allD, not_exD, not_notD];
fun ok4nnf \<^Const_>‹Trueprop for \<^Const_>‹Not for t›› = rigid t
| ok4nnf \<^Const_>‹Trueprop for t› = rigid t
| ok4nnf _ = false;
fun make_nnf1 ctxt th =
if ok4nnf (Thm.concl_of th)
then make_nnf1 ctxt (tryres(th, nnf_rls))
handle THM ("tryres", _, _) =>
forward_res ctxt (make_nnf1 ctxt)
(tryres(th, [conj_forward,disj_forward,all_forward,ex_forward]))
handle THM ("tryres", _, _) => th
else th
val nnf_simps =
@{thms simp_implies_def Ex1_def Ball_def Bex_def if_True if_False if_cancel
if_eq_cancel cases_simp}
fun nnf_extra_simps ({if_simps, ...} : simp_options) =
(if if_simps then @{thms split_ifs} else []) @ @{thms ex_simps all_simps simp_thms}
fun nnf_ss simp_options =
simpset_of (put_simpset HOL_basic_ss \<^context>
addsimps (nnf_extra_simps simp_options)
addsimprocs [\<^simproc>‹defined_All›, \<^simproc>‹defined_Ex›, \<^simproc>‹neq›, \<^simproc>‹let_simp›])
val presimplified_consts =
[\<^const_name>‹simp_implies›, \<^const_name>‹False›, \<^const_name>‹True›,
\<^const_name>‹Ex1›, \<^const_name>‹Ball›, \<^const_name>‹Bex›, \<^const_name>‹If›,
\<^const_name>‹Let›]
fun presimplify (simp_options as {let_simps, ...} : simp_options) ctxt =
rewrite_rule ctxt (map safe_mk_meta_eq nnf_simps)
#> simplify (put_simpset (nnf_ss simp_options) ctxt)
#> let_simps ? rewrite_rule ctxt @{thms Let_def [abs_def]}
fun make_nnf simp_options ctxt th =
(case Thm.prems_of th of
[] => th |> presimplify simp_options ctxt |> make_nnf1 ctxt
| _ => raise THM ("make_nnf: premises in argument", 0, [th]));
fun choice_theorems thy =
try (Global_Theory.get_thm thy) "Hilbert_Choice.choice" |> the_list
fun skolemize_with_choice_theorems simp_options ctxt choice_ths =
let
fun aux th =
if not (has_conns [\<^const_name>‹Ex›] (Thm.prop_of th)) then
th
else
tryres (th, choice_ths @
[conj_exD1, conj_exD2, disj_exD, disj_exD1, disj_exD2])
|> aux
handle THM ("tryres", _, _) =>
tryres (th, [conj_forward, disj_forward, all_forward])
|> forward_res ctxt aux
|> aux
handle THM ("tryres", _, _) =>
rename_bound_vars_RS th ex_forward
|> forward_res ctxt aux
in aux o make_nnf simp_options ctxt end
fun skolemize simp_options ctxt =
let val thy = Proof_Context.theory_of ctxt in
skolemize_with_choice_theorems simp_options ctxt (choice_theorems thy)
end
exception NO_F_PATTERN of unit
fun get_F_pattern T t u =
let
fun pat t u =
let
val ((head1, args1), (head2, args2)) = (t, u) |> apply2 strip_comb
in
if head1 = head2 then
let val pats = map2 pat args1 args2 in
case filter (is_some o fst) pats of
[(SOME T, _)] => (SOME T, list_comb (head1, map snd pats))
| [] => (NONE, t)
| _ => raise NO_F_PATTERN ()
end
else
let val T = fastype_of t in
if can dest_funT T then (SOME T, Bound 0) else raise NO_F_PATTERN ()
end
end
in
if T = \<^Type>‹bool› then
NONE
else case pat t u of
(SOME T, p as _ $ _) => SOME (Abs (Name.uu, T, p))
| _ => NONE
end
handle NO_F_PATTERN () => NONE
val ext_cong_neq = @{thm ext_cong_neq}
fun cong_extensionalize_thm ctxt th =
(case Thm.concl_of th of
\<^Const_>‹Trueprop for \<^Const_>‹Not for \<^Const_>‹HOL.eq T for ‹t as _ $ _› ‹u as _ $ _›››› =>
(case get_F_pattern T t u of
SOME p => th RS infer_instantiate ctxt [(("F", 0), Thm.cterm_of ctxt p)] ext_cong_neq
| NONE => th)
| _ => th)
fun abs_extensionalize_conv ctxt ct =
(case Thm.term_of ct of
\<^Const_>‹HOL.eq _ for _ ‹Abs _›› =>
ct |> (Conv.rewr_conv @{thm fun_eq_iff [THEN eq_reflection]}
then_conv abs_extensionalize_conv ctxt)
| _ $ _ => Conv.comb_conv (abs_extensionalize_conv ctxt) ct
| Abs _ => Conv.abs_conv (abs_extensionalize_conv o snd) ctxt ct
| _ => Conv.all_conv ct)
val abs_extensionalize_thm = Conv.fconv_rule o abs_extensionalize_conv
fun try_skolemize_etc simp_options ctxt th =
let
val th = th |> cong_extensionalize_thm ctxt
in
[th]
|> insert Thm.eq_thm_prop (abs_extensionalize_thm ctxt th)
|> map_filter (fn th => th |> try (skolemize simp_options ctxt)
|> tap (fn NONE =>
trace_msg ctxt (fn () =>
"Failed to skolemize " ^
Thm.string_of_thm ctxt th)
| _ => ()))
end
fun add_clauses ctxt th cls =
let
val (cnfs, ctxt') = ctxt
|> Variable.declare_thm th
|> make_cnf [] th;
in Variable.export ctxt' ctxt cnfs @ cls end;
fun fewerlits (th1, th2) = nliterals (Thm.prop_of th1) < nliterals (Thm.prop_of th2)
fun make_clauses_unsorted ctxt ths = fold_rev (add_clauses ctxt) ths [];
val make_clauses = sort (make_ord fewerlits) oo make_clauses_unsorted;
fun make_horns ths =
name_thms "Horn#"
(distinct Thm.eq_thm_prop (fold_rev (add_contras clause_rules) ths []));
fun best_prolog_tac ctxt sizef horns =
BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac ctxt horns 1);
fun depth_prolog_tac ctxt horns =
DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac ctxt horns 1);
fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls));
fun skolemize_prems_tac simp_options ctxt prems =
cut_facts_tac (maps (try_skolemize_etc simp_options ctxt) prems) THEN'
REPEAT o eresolve_tac ctxt [exE]
fun MESON preskolem_tac mkcl cltac ctxt i st =
SELECT_GOAL
(EVERY [Object_Logic.atomize_prems_tac ctxt 1,
resolve_tac ctxt @{thms ccontr} 1,
preskolem_tac,
Subgoal.FOCUS (fn {context = ctxt', prems = negs, ...} =>
EVERY1 [skolemize_prems_tac simp_options_all_true ctxt' negs,
Subgoal.FOCUS (cltac o mkcl o #prems) ctxt']) ctxt 1]) i st
handle THM _ => no_tac st;
fun best_meson_tac sizef ctxt =
MESON all_tac (make_clauses ctxt)
(fn cls =>
THEN_BEST_FIRST (resolve_tac ctxt (gocls cls) 1)
(has_fewer_prems 1, sizef)
(prolog_step_tac ctxt (make_horns cls) 1))
ctxt
fun safe_best_meson_tac ctxt =
SELECT_GOAL (TRY (safe_tac ctxt) THEN TRYALL (best_meson_tac size_of_subgoals ctxt));
fun depth_meson_tac ctxt =
MESON all_tac (make_clauses ctxt)
(fn cls => EVERY [resolve_tac ctxt (gocls cls) 1, depth_prolog_tac ctxt (make_horns cls)])
ctxt
fun prolog_step_tac' ctxt horns =
let val horn0s =
take_prefix Thm.no_prems horns
val nrtac = resolve_from_net_tac ctxt (Tactic.build_net horns)
in fn i => eq_assume_tac i ORELSE
match_tac ctxt horn0s i ORELSE
((assume_tac ctxt i APPEND nrtac i) THEN check_tac)
end;
fun iter_deepen_prolog_tac ctxt horns =
ITER_DEEPEN iter_deepen_limit (has_fewer_prems 1) (prolog_step_tac' ctxt horns);
fun iter_deepen_meson_tac ctxt ths = ctxt |> MESON all_tac (make_clauses ctxt)
(fn cls =>
(case (gocls (cls @ ths)) of
[] => no_tac
| goes =>
let
val horns = make_horns (cls @ ths)
val _ = trace_msg ctxt (fn () =>
cat_lines ("meson method called:" ::
map (Thm.string_of_thm ctxt) (cls @ ths) @
["clauses:"] @ map (Thm.string_of_thm ctxt) horns))
in
THEN_ITER_DEEPEN iter_deepen_limit
(resolve_tac ctxt goes 1) (has_fewer_prems 1) (prolog_step_tac' ctxt horns)
end));
fun meson_tac ctxt ths =
SELECT_GOAL (TRY (safe_tac ctxt) THEN TRYALL (iter_deepen_meson_tac ctxt ths));
val notEfalse = @{lemma "¬ P ⟹ P ⟹ False" by (rule notE)};
val notEfalse' = @{lemma "P ⟹ ¬ P ⟹ False" by (rule notE)};
fun negated_asm_of_head th =
th RS notEfalse handle THM _ => th RS notEfalse';
fun make_meta_clause ctxt th =
let val (fth, thaw) = Misc_Legacy.freeze_thaw_robust ctxt th
in
(zero_var_indexes o Thm.varifyT_global o thaw 0 o
negated_asm_of_head o make_horn resolution_clause_rules) fth
end;
fun make_meta_clauses ctxt ths =
name_thms "MClause#"
(distinct Thm.eq_thm_prop (map (make_meta_clause ctxt) ths));
end;