wenzelm@9869: (* Title: HOL/Tools/meson.ML paulson@9840: ID: $Id$ paulson@9840: Author: Lawrence C Paulson, Cambridge University Computer Laboratory paulson@9840: Copyright 1992 University of Cambridge paulson@9840: wenzelm@9869: The MESON resolution proof procedure for HOL. paulson@9840: paulson@9840: When making clauses, avoids using the rewriter -- instead uses RS recursively paulson@9840: paulson@9840: NEED TO SORT LITERALS BY # OF VARS, USING ==>I/E. ELIMINATES NEED FOR paulson@9840: FUNCTION nodups -- if done to goal clauses too! paulson@9840: *) paulson@9840: paulson@9840: local paulson@9840: wenzelm@12299: val not_conjD = thm "meson_not_conjD"; wenzelm@12299: val not_disjD = thm "meson_not_disjD"; wenzelm@12299: val not_notD = thm "meson_not_notD"; wenzelm@12299: val not_allD = thm "meson_not_allD"; wenzelm@12299: val not_exD = thm "meson_not_exD"; wenzelm@12299: val imp_to_disjD = thm "meson_imp_to_disjD"; wenzelm@12299: val not_impD = thm "meson_not_impD"; wenzelm@12299: val iff_to_disjD = thm "meson_iff_to_disjD"; wenzelm@12299: val not_iffD = thm "meson_not_iffD"; wenzelm@12299: val conj_exD1 = thm "meson_conj_exD1"; wenzelm@12299: val conj_exD2 = thm "meson_conj_exD2"; wenzelm@12299: val disj_exD = thm "meson_disj_exD"; wenzelm@12299: val disj_exD1 = thm "meson_disj_exD1"; wenzelm@12299: val disj_exD2 = thm "meson_disj_exD2"; wenzelm@12299: val disj_assoc = thm "meson_disj_assoc"; wenzelm@12299: val disj_comm = thm "meson_disj_comm"; wenzelm@12299: val disj_FalseD1 = thm "meson_disj_FalseD1"; wenzelm@12299: val disj_FalseD2 = thm "meson_disj_FalseD2"; paulson@9840: paulson@9840: paulson@9840: (**** Operators for forward proof ****) paulson@9840: paulson@9840: (*raises exception if no rules apply -- unlike RL*) paulson@9840: fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls)) paulson@9840: | tryres (th, []) = raise THM("tryres", 0, [th]); paulson@9840: paulson@9840: val prop_of = #prop o rep_thm; paulson@9840: paulson@9840: (*Permits forward proof from rules that discharge assumptions*) paulson@9840: fun forward_res nf st = paulson@9840: case Seq.pull (ALLGOALS (METAHYPS (fn [prem] => rtac (nf prem) 1)) st) paulson@9840: of Some(th,_) => th paulson@9840: | None => raise THM("forward_res", 0, [st]); paulson@9840: paulson@9840: paulson@9840: (*Are any of the constants in "bs" present in the term?*) wenzelm@9869: fun has_consts bs = paulson@9840: let fun has (Const(a,_)) = a mem bs wenzelm@9869: | has (f$u) = has f orelse has u wenzelm@9869: | has (Abs(_,_,t)) = has t wenzelm@9869: | has _ = false paulson@9840: in has end; paulson@9840: paulson@9840: paulson@9840: (**** Clause handling ****) paulson@9840: paulson@9840: fun literals (Const("Trueprop",_) $ P) = literals P paulson@9840: | literals (Const("op |",_) $ P $ Q) = literals P @ literals Q paulson@9840: | literals (Const("Not",_) $ P) = [(false,P)] paulson@9840: | literals P = [(true,P)]; paulson@9840: paulson@9840: (*number of literals in a term*) paulson@9840: val nliterals = length o literals; paulson@9840: paulson@9840: (*to detect, and remove, tautologous clauses*) paulson@9840: fun taut_lits [] = false paulson@9840: | taut_lits ((flg,t)::ts) = (not flg,t) mem ts orelse taut_lits ts; paulson@9840: paulson@9840: (*Include False as a literal: an occurrence of ~False is a tautology*) wenzelm@9869: fun is_taut th = taut_lits ((true, HOLogic.false_const) :: wenzelm@9869: literals (prop_of th)); paulson@9840: paulson@9840: (*Generation of unique names -- maxidx cannot be relied upon to increase! paulson@9840: Cannot rely on "variant", since variables might coincide when literals wenzelm@9869: are joined to make a clause... paulson@9840: 19 chooses "U" as the first variable name*) paulson@9840: val name_ref = ref 19; paulson@9840: paulson@9840: (*Replaces universally quantified variables by FREE variables -- because paulson@9840: assumptions may not contain scheme variables. Later, call "generalize". *) paulson@9840: fun freeze_spec th = paulson@9840: let val sth = th RS spec paulson@9840: val newname = (name_ref := !name_ref + 1; wenzelm@9869: radixstring(26, "A", !name_ref)) paulson@9840: in read_instantiate [("x", newname)] sth end; paulson@9840: paulson@9840: fun resop nf [prem] = resolve_tac (nf prem) 1; paulson@9840: paulson@9840: (*Conjunctive normal form, detecting tautologies early. paulson@9840: Strips universal quantifiers and breaks up conjunctions. *) wenzelm@9869: fun cnf_aux seen (th,ths) = paulson@9840: if taut_lits (literals(prop_of th) @ seen) then ths paulson@9840: else if not (has_consts ["All","op &"] (prop_of th)) then th::ths paulson@9840: else (*conjunction?*) wenzelm@9869: cnf_aux seen (th RS conjunct1, wenzelm@9869: cnf_aux seen (th RS conjunct2, ths)) paulson@9840: handle THM _ => (*universal quant?*) wenzelm@9869: cnf_aux seen (freeze_spec th, ths) paulson@9840: handle THM _ => (*disjunction?*) wenzelm@9869: let val tac = wenzelm@9869: (METAHYPS (resop (cnf_nil seen)) 1) THEN wenzelm@9869: (fn st' => st' |> wenzelm@9869: METAHYPS (resop (cnf_nil (literals (concl_of st') @ seen))) 1) paulson@9840: in Seq.list_of (tac (th RS disj_forward)) @ ths end paulson@9840: and cnf_nil seen th = cnf_aux seen (th,[]); paulson@9840: paulson@9840: (*Top-level call to cnf -- it's safe to reset name_ref*) wenzelm@9869: fun cnf (th,ths) = paulson@9840: (name_ref := 19; cnf (th RS conjunct1, cnf (th RS conjunct2, ths)) paulson@9840: handle THM _ => (*not a conjunction*) cnf_aux [] (th, ths)); paulson@9840: paulson@9840: (**** Removal of duplicate literals ****) paulson@9840: paulson@9840: (*Forward proof, passing extra assumptions as theorems to the tactic*) paulson@9840: fun forward_res2 nf hyps st = paulson@9840: case Seq.pull wenzelm@9869: (REPEAT wenzelm@9869: (METAHYPS (fn major::minors => rtac (nf (minors@hyps) major) 1) 1) wenzelm@9869: st) paulson@9840: of Some(th,_) => th paulson@9840: | None => raise THM("forward_res2", 0, [st]); paulson@9840: paulson@9840: (*Remove duplicates in P|Q by assuming ~P in Q paulson@9840: rls (initially []) accumulates assumptions of the form P==>False*) paulson@9840: fun nodups_aux rls th = nodups_aux rls (th RS disj_assoc) paulson@9840: handle THM _ => tryres(th,rls) paulson@9840: handle THM _ => tryres(forward_res2 nodups_aux rls (th RS disj_forward2), wenzelm@9869: [disj_FalseD1, disj_FalseD2, asm_rl]) paulson@9840: handle THM _ => th; paulson@9840: paulson@9840: (*Remove duplicate literals, if there are any*) paulson@9840: fun nodups th = paulson@9840: if null(findrep(literals(prop_of th))) then th paulson@9840: else nodups_aux [] th; paulson@9840: paulson@9840: paulson@9840: (**** Generation of contrapositives ****) paulson@9840: paulson@9840: (*Associate disjuctions to right -- make leftmost disjunct a LITERAL*) paulson@9840: fun assoc_right th = assoc_right (th RS disj_assoc) wenzelm@9869: handle THM _ => th; paulson@9840: paulson@9840: (*Must check for negative literal first!*) paulson@9840: val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule]; paulson@9840: paulson@9840: (*For Plaisted's postive refinement. [currently unused] *) paulson@9840: val refined_clause_rules = [disj_assoc, make_refined_neg_rule, make_pos_rule]; paulson@9840: paulson@9840: (*Create a goal or support clause, conclusing False*) paulson@9840: fun make_goal th = (*Must check for negative literal first!*) wenzelm@9869: make_goal (tryres(th, clause_rules)) paulson@9840: handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]); paulson@9840: paulson@9840: (*Sort clauses by number of literals*) paulson@9840: fun fewerlits(th1,th2) = nliterals(prop_of th1) < nliterals(prop_of th2); paulson@9840: paulson@9840: (*TAUTOLOGY CHECK SHOULD NOT BE NECESSARY!*) paulson@9840: fun sort_clauses ths = sort (make_ord fewerlits) (filter (not o is_taut) ths); paulson@9840: paulson@9840: (*Convert all suitable free variables to schematic variables*) paulson@9840: fun generalize th = forall_elim_vars 0 (forall_intr_frees th); paulson@9840: paulson@9840: (*Create a meta-level Horn clause*) wenzelm@9869: fun make_horn crules th = make_horn crules (tryres(th,crules)) wenzelm@9869: handle THM _ => th; paulson@9840: paulson@9840: (*Generate Horn clauses for all contrapositives of a clause*) wenzelm@9869: fun add_contras crules (th,hcs) = paulson@9840: let fun rots (0,th) = hcs wenzelm@9869: | rots (k,th) = zero_var_indexes (make_horn crules th) :: wenzelm@9869: rots(k-1, assoc_right (th RS disj_comm)) paulson@9840: in case nliterals(prop_of th) of wenzelm@9869: 1 => th::hcs paulson@9840: | n => rots(n, assoc_right th) paulson@9840: end; paulson@9840: paulson@9840: (*Use "theorem naming" to label the clauses*) wenzelm@9869: fun name_thms label = paulson@9840: let fun name1 (th, (k,ths)) = wenzelm@9869: (k-1, Thm.name_thm (label ^ string_of_int k, th) :: ths) paulson@9840: paulson@9840: in fn ths => #2 (foldr name1 (ths, (length ths, []))) end; paulson@9840: paulson@9840: (*Find an all-negative support clause*) paulson@9840: fun is_negative th = forall (not o #1) (literals (prop_of th)); paulson@9840: paulson@9840: val neg_clauses = filter is_negative; paulson@9840: paulson@9840: paulson@9840: (***** MESON PROOF PROCEDURE *****) paulson@9840: paulson@9840: fun rhyps (Const("==>",_) $ (Const("Trueprop",_) $ A) $ phi, wenzelm@9869: As) = rhyps(phi, A::As) paulson@9840: | rhyps (_, As) = As; paulson@9840: paulson@9840: (** Detecting repeated assumptions in a subgoal **) paulson@9840: paulson@9840: (*The stringtree detects repeated assumptions.*) paulson@9840: fun ins_term (net,t) = Net.insert_term((t,t), net, op aconv); paulson@9840: paulson@9840: (*detects repetitions in a list of terms*) paulson@9840: fun has_reps [] = false paulson@9840: | has_reps [_] = false paulson@9840: | has_reps [t,u] = (t aconv u) paulson@9840: | has_reps ts = (foldl ins_term (Net.empty, ts); false) wenzelm@9869: handle INSERT => true; paulson@9840: paulson@9840: (*Like TRYALL eq_assume_tac, but avoids expensive THEN calls*) paulson@9840: fun TRYALL_eq_assume_tac 0 st = Seq.single st wenzelm@9869: | TRYALL_eq_assume_tac i st = wenzelm@9869: TRYALL_eq_assume_tac (i-1) (eq_assumption i st) wenzelm@9869: handle THM _ => TRYALL_eq_assume_tac (i-1) st; paulson@9840: paulson@9840: (*Loop checking: FAIL if trying to prove the same thing twice paulson@9840: -- if *ANY* subgoal has repeated literals*) wenzelm@9869: fun check_tac st = paulson@9840: if exists (fn prem => has_reps (rhyps(prem,[]))) (prems_of st) paulson@9840: then Seq.empty else Seq.single st; paulson@9840: paulson@9840: paulson@9840: (* net_resolve_tac actually made it slower... *) wenzelm@9869: fun prolog_step_tac horns i = paulson@9840: (assume_tac i APPEND resolve_tac horns i) THEN check_tac THEN paulson@9840: TRYALL eq_assume_tac; paulson@9840: paulson@9840: paulson@9840: in paulson@9840: paulson@9840: paulson@9840: (*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*) paulson@9840: local fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz paulson@9840: in paulson@9840: fun size_of_subgoals st = foldr addconcl (prems_of st, 0) paulson@9840: end; paulson@9840: paulson@9840: (*Negation Normal Form*) paulson@9840: val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD, wenzelm@9869: not_impD, not_iffD, not_allD, not_exD, not_notD]; paulson@9840: fun make_nnf th = make_nnf (tryres(th, nnf_rls)) wenzelm@9869: handle THM _ => wenzelm@9869: forward_res make_nnf wenzelm@9869: (tryres(th, [conj_forward,disj_forward,all_forward,ex_forward])) paulson@9840: handle THM _ => th; paulson@9840: paulson@9840: (*Pull existential quantifiers (Skolemization)*) wenzelm@9869: fun skolemize th = paulson@9840: if not (has_consts ["Ex"] (prop_of th)) then th paulson@9840: else skolemize (tryres(th, [choice, conj_exD1, conj_exD2, wenzelm@9869: disj_exD, disj_exD1, disj_exD2])) wenzelm@9869: handle THM _ => wenzelm@9869: skolemize (forward_res skolemize wenzelm@9869: (tryres (th, [conj_forward, disj_forward, all_forward]))) paulson@9840: handle THM _ => forward_res skolemize (th RS ex_forward); paulson@9840: paulson@9840: paulson@9840: (*Make clauses from a list of theorems, previously Skolemized and put into nnf. paulson@9840: The resulting clauses are HOL disjunctions.*) wenzelm@9869: fun make_clauses ths = paulson@9840: sort_clauses (map (generalize o nodups) (foldr cnf (ths,[]))); paulson@9840: paulson@9840: (*Convert a list of clauses to (contrapositive) Horn clauses*) wenzelm@9869: fun make_horns ths = paulson@9840: name_thms "Horn#" wenzelm@13105: (gen_distinct Drule.eq_thm_prop (foldr (add_contras clause_rules) (ths,[]))); paulson@9840: paulson@9840: (*Could simply use nprems_of, which would count remaining subgoals -- no paulson@9840: discrimination as to their size! With BEST_FIRST, fails for problem 41.*) paulson@9840: wenzelm@9869: fun best_prolog_tac sizef horns = paulson@9840: BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac horns 1); paulson@9840: wenzelm@9869: fun depth_prolog_tac horns = paulson@9840: DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac horns 1); paulson@9840: paulson@9840: (*Return all negative clauses, as possible goal clauses*) paulson@9840: fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls)); paulson@9840: paulson@9840: wenzelm@9869: fun skolemize_tac prems = paulson@9840: cut_facts_tac (map (skolemize o make_nnf) prems) THEN' paulson@9840: REPEAT o (etac exE); paulson@9840: paulson@9840: (*Shell of all meson-tactics. Supplies cltac with clauses: HOL disjunctions*) paulson@9840: fun MESON cltac = SELECT_GOAL paulson@9840: (EVERY1 [rtac ccontr, paulson@9840: METAHYPS (fn negs => paulson@9840: EVERY1 [skolemize_tac negs, paulson@9840: METAHYPS (cltac o make_clauses)])]); paulson@9840: paulson@9840: (** Best-first search versions **) paulson@9840: wenzelm@9869: fun best_meson_tac sizef = wenzelm@9869: MESON (fn cls => paulson@9840: THEN_BEST_FIRST (resolve_tac (gocls cls) 1) paulson@9840: (has_fewer_prems 1, sizef) paulson@9840: (prolog_step_tac (make_horns cls) 1)); paulson@9840: paulson@9840: (*First, breaks the goal into independent units*) paulson@9840: val safe_best_meson_tac = wenzelm@9869: SELECT_GOAL (TRY Safe_tac THEN paulson@9840: TRYALL (best_meson_tac size_of_subgoals)); paulson@9840: paulson@9840: (** Depth-first search version **) paulson@9840: paulson@9840: val depth_meson_tac = wenzelm@9869: MESON (fn cls => EVERY [resolve_tac (gocls cls) 1, paulson@9840: depth_prolog_tac (make_horns cls)]); paulson@9840: paulson@9840: paulson@9840: paulson@9840: (** Iterative deepening version **) paulson@9840: paulson@9840: (*This version does only one inference per call; paulson@9840: having only one eq_assume_tac speeds it up!*) wenzelm@9869: fun prolog_step_tac' horns = paulson@9840: let val (horn0s, hornps) = (*0 subgoals vs 1 or more*) paulson@9840: take_prefix Thm.no_prems horns paulson@9840: val nrtac = net_resolve_tac horns paulson@9840: in fn i => eq_assume_tac i ORELSE paulson@9840: match_tac horn0s i ORELSE (*no backtracking if unit MATCHES*) paulson@9840: ((assume_tac i APPEND nrtac i) THEN check_tac) paulson@9840: end; paulson@9840: wenzelm@9869: fun iter_deepen_prolog_tac horns = paulson@9840: ITER_DEEPEN (has_fewer_prems 1) (prolog_step_tac' horns); paulson@9840: wenzelm@9869: val iter_deepen_meson_tac = wenzelm@9869: MESON (fn cls => paulson@9840: (THEN_ITER_DEEPEN (resolve_tac (gocls cls) 1) paulson@9840: (has_fewer_prems 1) paulson@9840: (prolog_step_tac' (make_horns cls)))); paulson@9840: wenzelm@9869: fun meson_claset_tac cs = wenzelm@9869: SELECT_GOAL (TRY (safe_tac cs) THEN TRYALL iter_deepen_meson_tac); wenzelm@9869: wenzelm@9869: val meson_tac = CLASET' meson_claset_tac; wenzelm@9869: wenzelm@9869: wenzelm@9869: (* proof method setup *) wenzelm@9869: wenzelm@9869: local wenzelm@9869: wenzelm@9869: fun meson_meth ctxt = wenzelm@10821: Method.SIMPLE_METHOD' HEADGOAL wenzelm@10821: (CHANGED_PROP o meson_claset_tac (Classical.get_local_claset ctxt)); wenzelm@9869: wenzelm@9869: in wenzelm@9869: wenzelm@9869: val meson_setup = wenzelm@9869: [Method.add_methods wenzelm@9869: [("meson", Method.ctxt_args meson_meth, "The MESON resolution proof procedure")]]; paulson@9840: paulson@9840: end; wenzelm@9869: wenzelm@9869: end;