src/HOL/Tools/meson.ML
author wenzelm
Tue May 07 14:26:32 2002 +0200 (2002-05-07)
changeset 13105 3d1e7a199bdc
parent 12299 2c76042c3b06
child 14733 3eda95792083
permissions -rw-r--r--
use eq_thm_prop instead of slightly inadequate eq_thm;
     1 (*  Title:      HOL/Tools/meson.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1992  University of Cambridge
     5 
     6 The MESON resolution proof procedure for HOL.
     7 
     8 When making clauses, avoids using the rewriter -- instead uses RS recursively
     9 
    10 NEED TO SORT LITERALS BY # OF VARS, USING ==>I/E.  ELIMINATES NEED FOR
    11 FUNCTION nodups -- if done to goal clauses too!
    12 *)
    13 
    14 local
    15 
    16  val not_conjD = thm "meson_not_conjD";
    17  val not_disjD = thm "meson_not_disjD";
    18  val not_notD = thm "meson_not_notD";
    19  val not_allD = thm "meson_not_allD";
    20  val not_exD = thm "meson_not_exD";
    21  val imp_to_disjD = thm "meson_imp_to_disjD";
    22  val not_impD = thm "meson_not_impD";
    23  val iff_to_disjD = thm "meson_iff_to_disjD";
    24  val not_iffD = thm "meson_not_iffD";
    25  val conj_exD1 = thm "meson_conj_exD1";
    26  val conj_exD2 = thm "meson_conj_exD2";
    27  val disj_exD = thm "meson_disj_exD";
    28  val disj_exD1 = thm "meson_disj_exD1";
    29  val disj_exD2 = thm "meson_disj_exD2";
    30  val disj_assoc = thm "meson_disj_assoc";
    31  val disj_comm = thm "meson_disj_comm";
    32  val disj_FalseD1 = thm "meson_disj_FalseD1";
    33  val disj_FalseD2 = thm "meson_disj_FalseD2";
    34 
    35 
    36  (**** Operators for forward proof ****)
    37 
    38  (*raises exception if no rules apply -- unlike RL*)
    39  fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
    40    | tryres (th, []) = raise THM("tryres", 0, [th]);
    41 
    42  val prop_of = #prop o rep_thm;
    43 
    44  (*Permits forward proof from rules that discharge assumptions*)
    45  fun forward_res nf st =
    46    case Seq.pull (ALLGOALS (METAHYPS (fn [prem] => rtac (nf prem) 1)) st)
    47    of Some(th,_) => th
    48     | None => raise THM("forward_res", 0, [st]);
    49 
    50 
    51  (*Are any of the constants in "bs" present in the term?*)
    52  fun has_consts bs =
    53    let fun has (Const(a,_)) = a mem bs
    54          | has (f$u) = has f orelse has u
    55          | has (Abs(_,_,t)) = has t
    56          | has _ = false
    57    in  has  end;
    58 
    59 
    60  (**** Clause handling ****)
    61 
    62  fun literals (Const("Trueprop",_) $ P) = literals P
    63    | literals (Const("op |",_) $ P $ Q) = literals P @ literals Q
    64    | literals (Const("Not",_) $ P) = [(false,P)]
    65    | literals P = [(true,P)];
    66 
    67  (*number of literals in a term*)
    68  val nliterals = length o literals;
    69 
    70  (*to detect, and remove, tautologous clauses*)
    71  fun taut_lits [] = false
    72    | taut_lits ((flg,t)::ts) = (not flg,t) mem ts orelse taut_lits ts;
    73 
    74  (*Include False as a literal: an occurrence of ~False is a tautology*)
    75  fun is_taut th = taut_lits ((true, HOLogic.false_const) ::
    76                              literals (prop_of th));
    77 
    78  (*Generation of unique names -- maxidx cannot be relied upon to increase!
    79    Cannot rely on "variant", since variables might coincide when literals
    80    are joined to make a clause...
    81    19 chooses "U" as the first variable name*)
    82  val name_ref = ref 19;
    83 
    84  (*Replaces universally quantified variables by FREE variables -- because
    85    assumptions may not contain scheme variables.  Later, call "generalize". *)
    86  fun freeze_spec th =
    87    let val sth = th RS spec
    88        val newname = (name_ref := !name_ref + 1;
    89                       radixstring(26, "A", !name_ref))
    90    in  read_instantiate [("x", newname)] sth  end;
    91 
    92  fun resop nf [prem] = resolve_tac (nf prem) 1;
    93 
    94  (*Conjunctive normal form, detecting tautologies early.
    95    Strips universal quantifiers and breaks up conjunctions. *)
    96  fun cnf_aux seen (th,ths) =
    97    if taut_lits (literals(prop_of th) @ seen)  then ths
    98    else if not (has_consts ["All","op &"] (prop_of th))  then th::ths
    99    else (*conjunction?*)
   100          cnf_aux seen (th RS conjunct1,
   101                        cnf_aux seen (th RS conjunct2, ths))
   102    handle THM _ => (*universal quant?*)
   103          cnf_aux  seen (freeze_spec th,  ths)
   104    handle THM _ => (*disjunction?*)
   105      let val tac =
   106          (METAHYPS (resop (cnf_nil seen)) 1) THEN
   107          (fn st' => st' |>
   108                  METAHYPS (resop (cnf_nil (literals (concl_of st') @ seen))) 1)
   109      in  Seq.list_of (tac (th RS disj_forward)) @ ths  end
   110  and cnf_nil seen th = cnf_aux seen (th,[]);
   111 
   112  (*Top-level call to cnf -- it's safe to reset name_ref*)
   113  fun cnf (th,ths) =
   114     (name_ref := 19;  cnf (th RS conjunct1, cnf (th RS conjunct2, ths))
   115      handle THM _ => (*not a conjunction*) cnf_aux [] (th, ths));
   116 
   117  (**** Removal of duplicate literals ****)
   118 
   119  (*Forward proof, passing extra assumptions as theorems to the tactic*)
   120  fun forward_res2 nf hyps st =
   121    case Seq.pull
   122          (REPEAT
   123           (METAHYPS (fn major::minors => rtac (nf (minors@hyps) major) 1) 1)
   124           st)
   125    of Some(th,_) => th
   126     | None => raise THM("forward_res2", 0, [st]);
   127 
   128  (*Remove duplicates in P|Q by assuming ~P in Q
   129    rls (initially []) accumulates assumptions of the form P==>False*)
   130  fun nodups_aux rls th = nodups_aux rls (th RS disj_assoc)
   131      handle THM _ => tryres(th,rls)
   132      handle THM _ => tryres(forward_res2 nodups_aux rls (th RS disj_forward2),
   133                             [disj_FalseD1, disj_FalseD2, asm_rl])
   134      handle THM _ => th;
   135 
   136  (*Remove duplicate literals, if there are any*)
   137  fun nodups th =
   138      if null(findrep(literals(prop_of th))) then th
   139      else nodups_aux [] th;
   140 
   141 
   142  (**** Generation of contrapositives ****)
   143 
   144  (*Associate disjuctions to right -- make leftmost disjunct a LITERAL*)
   145  fun assoc_right th = assoc_right (th RS disj_assoc)
   146          handle THM _ => th;
   147 
   148  (*Must check for negative literal first!*)
   149  val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule];
   150 
   151  (*For Plaisted's postive refinement.  [currently unused] *)
   152  val refined_clause_rules = [disj_assoc, make_refined_neg_rule, make_pos_rule];
   153 
   154  (*Create a goal or support clause, conclusing False*)
   155  fun make_goal th =   (*Must check for negative literal first!*)
   156      make_goal (tryres(th, clause_rules))
   157    handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]);
   158 
   159  (*Sort clauses by number of literals*)
   160  fun fewerlits(th1,th2) = nliterals(prop_of th1) < nliterals(prop_of th2);
   161 
   162  (*TAUTOLOGY CHECK SHOULD NOT BE NECESSARY!*)
   163  fun sort_clauses ths = sort (make_ord fewerlits) (filter (not o is_taut) ths);
   164 
   165  (*Convert all suitable free variables to schematic variables*)
   166  fun generalize th = forall_elim_vars 0 (forall_intr_frees th);
   167 
   168  (*Create a meta-level Horn clause*)
   169  fun make_horn crules th = make_horn crules (tryres(th,crules))
   170                            handle THM _ => th;
   171 
   172  (*Generate Horn clauses for all contrapositives of a clause*)
   173  fun add_contras crules (th,hcs) =
   174    let fun rots (0,th) = hcs
   175          | rots (k,th) = zero_var_indexes (make_horn crules th) ::
   176                          rots(k-1, assoc_right (th RS disj_comm))
   177    in case nliterals(prop_of th) of
   178          1 => th::hcs
   179        | n => rots(n, assoc_right th)
   180    end;
   181 
   182  (*Use "theorem naming" to label the clauses*)
   183  fun name_thms label =
   184      let fun name1 (th, (k,ths)) =
   185            (k-1, Thm.name_thm (label ^ string_of_int k, th) :: ths)
   186 
   187      in  fn ths => #2 (foldr name1 (ths, (length ths, [])))  end;
   188 
   189  (*Find an all-negative support clause*)
   190  fun is_negative th = forall (not o #1) (literals (prop_of th));
   191 
   192  val neg_clauses = filter is_negative;
   193 
   194 
   195  (***** MESON PROOF PROCEDURE *****)
   196 
   197  fun rhyps (Const("==>",_) $ (Const("Trueprop",_) $ A) $ phi,
   198             As) = rhyps(phi, A::As)
   199    | rhyps (_, As) = As;
   200 
   201  (** Detecting repeated assumptions in a subgoal **)
   202 
   203  (*The stringtree detects repeated assumptions.*)
   204  fun ins_term (net,t) = Net.insert_term((t,t), net, op aconv);
   205 
   206  (*detects repetitions in a list of terms*)
   207  fun has_reps [] = false
   208    | has_reps [_] = false
   209    | has_reps [t,u] = (t aconv u)
   210    | has_reps ts = (foldl ins_term (Net.empty, ts);  false)
   211                    handle INSERT => true;
   212 
   213  (*Like TRYALL eq_assume_tac, but avoids expensive THEN calls*)
   214  fun TRYALL_eq_assume_tac 0 st = Seq.single st
   215    | TRYALL_eq_assume_tac i st =
   216         TRYALL_eq_assume_tac (i-1) (eq_assumption i st)
   217         handle THM _ => TRYALL_eq_assume_tac (i-1) st;
   218 
   219  (*Loop checking: FAIL if trying to prove the same thing twice
   220    -- if *ANY* subgoal has repeated literals*)
   221  fun check_tac st =
   222    if exists (fn prem => has_reps (rhyps(prem,[]))) (prems_of st)
   223    then  Seq.empty  else  Seq.single st;
   224 
   225 
   226  (* net_resolve_tac actually made it slower... *)
   227  fun prolog_step_tac horns i =
   228      (assume_tac i APPEND resolve_tac horns i) THEN check_tac THEN
   229      TRYALL eq_assume_tac;
   230 
   231 
   232 in
   233 
   234 
   235 (*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*)
   236 local fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz
   237 in
   238 fun size_of_subgoals st = foldr addconcl (prems_of st, 0)
   239 end;
   240 
   241 (*Negation Normal Form*)
   242 val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD,
   243                not_impD, not_iffD, not_allD, not_exD, not_notD];
   244 fun make_nnf th = make_nnf (tryres(th, nnf_rls))
   245     handle THM _ =>
   246         forward_res make_nnf
   247            (tryres(th, [conj_forward,disj_forward,all_forward,ex_forward]))
   248     handle THM _ => th;
   249 
   250 (*Pull existential quantifiers (Skolemization)*)
   251 fun skolemize th =
   252   if not (has_consts ["Ex"] (prop_of th)) then th
   253   else skolemize (tryres(th, [choice, conj_exD1, conj_exD2,
   254                               disj_exD, disj_exD1, disj_exD2]))
   255     handle THM _ =>
   256         skolemize (forward_res skolemize
   257                    (tryres (th, [conj_forward, disj_forward, all_forward])))
   258     handle THM _ => forward_res skolemize (th RS ex_forward);
   259 
   260 
   261 (*Make clauses from a list of theorems, previously Skolemized and put into nnf.
   262   The resulting clauses are HOL disjunctions.*)
   263 fun make_clauses ths =
   264     sort_clauses (map (generalize o nodups) (foldr cnf (ths,[])));
   265 
   266 (*Convert a list of clauses to (contrapositive) Horn clauses*)
   267 fun make_horns ths =
   268     name_thms "Horn#"
   269       (gen_distinct Drule.eq_thm_prop (foldr (add_contras clause_rules) (ths,[])));
   270 
   271 (*Could simply use nprems_of, which would count remaining subgoals -- no
   272   discrimination as to their size!  With BEST_FIRST, fails for problem 41.*)
   273 
   274 fun best_prolog_tac sizef horns =
   275     BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac horns 1);
   276 
   277 fun depth_prolog_tac horns =
   278     DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac horns 1);
   279 
   280 (*Return all negative clauses, as possible goal clauses*)
   281 fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls));
   282 
   283 
   284 fun skolemize_tac prems =
   285     cut_facts_tac (map (skolemize o make_nnf) prems)  THEN'
   286     REPEAT o (etac exE);
   287 
   288 (*Shell of all meson-tactics.  Supplies cltac with clauses: HOL disjunctions*)
   289 fun MESON cltac = SELECT_GOAL
   290  (EVERY1 [rtac ccontr,
   291           METAHYPS (fn negs =>
   292                     EVERY1 [skolemize_tac negs,
   293                             METAHYPS (cltac o make_clauses)])]);
   294 
   295 (** Best-first search versions **)
   296 
   297 fun best_meson_tac sizef =
   298   MESON (fn cls =>
   299          THEN_BEST_FIRST (resolve_tac (gocls cls) 1)
   300                          (has_fewer_prems 1, sizef)
   301                          (prolog_step_tac (make_horns cls) 1));
   302 
   303 (*First, breaks the goal into independent units*)
   304 val safe_best_meson_tac =
   305      SELECT_GOAL (TRY Safe_tac THEN
   306                   TRYALL (best_meson_tac size_of_subgoals));
   307 
   308 (** Depth-first search version **)
   309 
   310 val depth_meson_tac =
   311      MESON (fn cls => EVERY [resolve_tac (gocls cls) 1,
   312                              depth_prolog_tac (make_horns cls)]);
   313 
   314 
   315 
   316 (** Iterative deepening version **)
   317 
   318 (*This version does only one inference per call;
   319   having only one eq_assume_tac speeds it up!*)
   320 fun prolog_step_tac' horns =
   321     let val (horn0s, hornps) = (*0 subgoals vs 1 or more*)
   322             take_prefix Thm.no_prems horns
   323         val nrtac = net_resolve_tac horns
   324     in  fn i => eq_assume_tac i ORELSE
   325                 match_tac horn0s i ORELSE  (*no backtracking if unit MATCHES*)
   326                 ((assume_tac i APPEND nrtac i) THEN check_tac)
   327     end;
   328 
   329 fun iter_deepen_prolog_tac horns =
   330     ITER_DEEPEN (has_fewer_prems 1) (prolog_step_tac' horns);
   331 
   332 val iter_deepen_meson_tac =
   333   MESON (fn cls =>
   334          (THEN_ITER_DEEPEN (resolve_tac (gocls cls) 1)
   335                            (has_fewer_prems 1)
   336                            (prolog_step_tac' (make_horns cls))));
   337 
   338 fun meson_claset_tac cs =
   339   SELECT_GOAL (TRY (safe_tac cs) THEN TRYALL iter_deepen_meson_tac);
   340 
   341 val meson_tac = CLASET' meson_claset_tac;
   342 
   343 
   344 (* proof method setup *)
   345 
   346 local
   347 
   348 fun meson_meth ctxt =
   349   Method.SIMPLE_METHOD' HEADGOAL
   350     (CHANGED_PROP o meson_claset_tac (Classical.get_local_claset ctxt));
   351 
   352 in
   353 
   354 val meson_setup =
   355  [Method.add_methods
   356   [("meson", Method.ctxt_args meson_meth, "The MESON resolution proof procedure")]];
   357 
   358 end;
   359 
   360 end;