src/HOL/Tools/meson.ML
author paulson
Tue Sep 05 10:15:23 2000 +0200 (2000-09-05)
changeset 9840 9dfcb0224f8c
child 9869 95dca9f991f2
permissions -rw-r--r--
meson.ML moved from HOL/ex to HOL/Tools: meson_tac installed by default
paulson@9840
     1
(*  Title:      HOL/ex/meson
paulson@9840
     2
    ID:         $Id$
paulson@9840
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@9840
     4
    Copyright   1992  University of Cambridge
paulson@9840
     5
paulson@9840
     6
The MESON resolution proof procedure for HOL
paulson@9840
     7
paulson@9840
     8
When making clauses, avoids using the rewriter -- instead uses RS recursively
paulson@9840
     9
paulson@9840
    10
NEED TO SORT LITERALS BY # OF VARS, USING ==>I/E.  ELIMINATES NEED FOR
paulson@9840
    11
FUNCTION nodups -- if done to goal clauses too!
paulson@9840
    12
*)
paulson@9840
    13
paulson@9840
    14
paulson@9840
    15
(**** LEMMAS : outside the "local" block ****)
paulson@9840
    16
paulson@9840
    17
(** "Axiom" of Choice, proved using the description operator **)
paulson@9840
    18
paulson@9840
    19
Goal "ALL x. EX y. Q x y ==> EX f. ALL x. Q x (f x)";
paulson@9840
    20
by (fast_tac (claset() addEs [selectI]) 1);
paulson@9840
    21
qed "choice";
paulson@9840
    22
paulson@9840
    23
(*** Generation of contrapositives ***)
paulson@9840
    24
paulson@9840
    25
(*Inserts negated disjunct after removing the negation; P is a literal*)
paulson@9840
    26
val [major,minor] = Goal "~P|Q ==> ((~P==>P) ==> Q)";
paulson@9840
    27
by (rtac (major RS disjE) 1);
paulson@9840
    28
by (rtac notE 1);
paulson@9840
    29
by (etac minor 2);
paulson@9840
    30
by (ALLGOALS assume_tac);
paulson@9840
    31
qed "make_neg_rule";
paulson@9840
    32
paulson@9840
    33
(*For Plaisted's "Postive refinement" of the MESON procedure*)
paulson@9840
    34
Goal "~P|Q ==> (P ==> Q)";
paulson@9840
    35
by (Blast_tac 1);
paulson@9840
    36
qed "make_refined_neg_rule";
paulson@9840
    37
paulson@9840
    38
(*P should be a literal*)
paulson@9840
    39
val [major,minor] = Goal "P|Q ==> ((P==>~P) ==> Q)";
paulson@9840
    40
by (rtac (major RS disjE) 1);
paulson@9840
    41
by (rtac notE 1);
paulson@9840
    42
by (etac minor 1);
paulson@9840
    43
by (ALLGOALS assume_tac);
paulson@9840
    44
qed "make_pos_rule";
paulson@9840
    45
paulson@9840
    46
(*** Generation of a goal clause -- put away the final literal ***)
paulson@9840
    47
paulson@9840
    48
val [major,minor] = Goal "~P ==> ((~P==>P) ==> False)";
paulson@9840
    49
by (rtac notE 1);
paulson@9840
    50
by (rtac minor 2);
paulson@9840
    51
by (ALLGOALS (rtac major));
paulson@9840
    52
qed "make_neg_goal";
paulson@9840
    53
paulson@9840
    54
val [major,minor] = Goal "P ==> ((P==>~P) ==> False)";
paulson@9840
    55
by (rtac notE 1);
paulson@9840
    56
by (rtac minor 1);
paulson@9840
    57
by (ALLGOALS (rtac major));
paulson@9840
    58
qed "make_pos_goal";
paulson@9840
    59
paulson@9840
    60
paulson@9840
    61
(**** Lemmas for forward proof (like congruence rules) ****)
paulson@9840
    62
paulson@9840
    63
(*NOTE: could handle conjunctions (faster?) by
paulson@9840
    64
    nf(th RS conjunct2) RS (nf(th RS conjunct1) RS conjI) *)
paulson@9840
    65
val major::prems = Goal
paulson@9840
    66
    "[| P'&Q';  P' ==> P;  Q' ==> Q |] ==> P&Q";
paulson@9840
    67
by (rtac (major RS conjE) 1);
paulson@9840
    68
by (rtac conjI 1);
paulson@9840
    69
by (ALLGOALS (eresolve_tac prems));
paulson@9840
    70
qed "conj_forward";
paulson@9840
    71
paulson@9840
    72
val major::prems = Goal
paulson@9840
    73
    "[| P'|Q';  P' ==> P;  Q' ==> Q |] ==> P|Q";
paulson@9840
    74
by (rtac (major RS disjE) 1);
paulson@9840
    75
by (ALLGOALS (dresolve_tac prems));
paulson@9840
    76
by (ALLGOALS (eresolve_tac [disjI1,disjI2]));
paulson@9840
    77
qed "disj_forward";
paulson@9840
    78
paulson@9840
    79
(*Version for removal of duplicate literals*)
paulson@9840
    80
val major::prems = Goal
paulson@9840
    81
    "[| P'|Q';  P' ==> P;  [| Q'; P==>False |] ==> Q |] ==> P|Q";
paulson@9840
    82
by (cut_facts_tac [major] 1);
paulson@9840
    83
by (blast_tac (claset() addIs prems) 1); 
paulson@9840
    84
qed "disj_forward2";
paulson@9840
    85
paulson@9840
    86
val major::prems = Goal
paulson@9840
    87
    "[| ALL x. P'(x);  !!x. P'(x) ==> P(x) |] ==> ALL x. P(x)";
paulson@9840
    88
by (rtac allI 1);
paulson@9840
    89
by (resolve_tac prems 1);
paulson@9840
    90
by (rtac (major RS spec) 1);
paulson@9840
    91
qed "all_forward";
paulson@9840
    92
paulson@9840
    93
val major::prems = Goal
paulson@9840
    94
    "[| EX x. P'(x);  !!x. P'(x) ==> P(x) |] ==> EX x. P(x)";
paulson@9840
    95
by (rtac (major RS exE) 1);
paulson@9840
    96
by (rtac exI 1);
paulson@9840
    97
by (eresolve_tac prems 1);
paulson@9840
    98
qed "ex_forward";
paulson@9840
    99
paulson@9840
   100
(**** END OF LEMMAS ****)
paulson@9840
   101
paulson@9840
   102
local
paulson@9840
   103
paulson@9840
   104
 (*Prove theorems using fast_tac*)
paulson@9840
   105
 fun prove_fun s = 
paulson@9840
   106
     prove_goal (the_context ()) s
paulson@9840
   107
	  (fn prems => [ cut_facts_tac prems 1, Fast_tac 1 ]);
paulson@9840
   108
paulson@9840
   109
 (**** Negation Normal Form ****)
paulson@9840
   110
paulson@9840
   111
 (*** de Morgan laws ***)
paulson@9840
   112
paulson@9840
   113
 val not_conjD = prove_fun "~(P&Q) ==> ~P | ~Q";
paulson@9840
   114
 val not_disjD = prove_fun "~(P|Q) ==> ~P & ~Q";
paulson@9840
   115
 val not_notD = prove_fun "~~P ==> P";
paulson@9840
   116
 val not_allD = prove_fun  "~(ALL x. P(x)) ==> EX x. ~P(x)";
paulson@9840
   117
 val not_exD = prove_fun   "~(EX x. P(x)) ==> ALL x. ~P(x)";
paulson@9840
   118
paulson@9840
   119
paulson@9840
   120
 (*** Removal of --> and <-> (positive and negative occurrences) ***)
paulson@9840
   121
paulson@9840
   122
 val imp_to_disjD = prove_fun "P-->Q ==> ~P | Q";
paulson@9840
   123
 val not_impD = prove_fun   "~(P-->Q) ==> P & ~Q";
paulson@9840
   124
paulson@9840
   125
 val iff_to_disjD = prove_fun "P=Q ==> (~P | Q) & (~Q | P)";
paulson@9840
   126
paulson@9840
   127
 (*Much more efficient than (P & ~Q) | (Q & ~P) for computing CNF*)
paulson@9840
   128
 val not_iffD = prove_fun "~(P=Q) ==> (P | Q) & (~P | ~Q)";
paulson@9840
   129
paulson@9840
   130
paulson@9840
   131
 (**** Pulling out the existential quantifiers ****)
paulson@9840
   132
paulson@9840
   133
 (*** Conjunction ***)
paulson@9840
   134
paulson@9840
   135
 val conj_exD1 = prove_fun "(EX x. P(x)) & Q ==> EX x. P(x) & Q";
paulson@9840
   136
 val conj_exD2 = prove_fun "P & (EX x. Q(x)) ==> EX x. P & Q(x)";
paulson@9840
   137
paulson@9840
   138
 (*** Disjunction ***)
paulson@9840
   139
paulson@9840
   140
 (*DO NOT USE with forall-Skolemization: makes fewer schematic variables!!
paulson@9840
   141
   With ex-Skolemization, makes fewer Skolem constants*)
paulson@9840
   142
 val disj_exD = prove_fun "(EX x. P(x)) | (EX x. Q(x)) ==> EX x. P(x) | Q(x)";
paulson@9840
   143
paulson@9840
   144
 val disj_exD1 = prove_fun "(EX x. P(x)) | Q ==> EX x. P(x) | Q";
paulson@9840
   145
 val disj_exD2 = prove_fun "P | (EX x. Q(x)) ==> EX x. P | Q(x)";
paulson@9840
   146
paulson@9840
   147
paulson@9840
   148
paulson@9840
   149
 (***** Generating clauses for the Meson Proof Procedure *****)
paulson@9840
   150
paulson@9840
   151
 (*** Disjunctions ***)
paulson@9840
   152
paulson@9840
   153
 val disj_assoc = prove_fun "(P|Q)|R ==> P|(Q|R)";
paulson@9840
   154
paulson@9840
   155
 val disj_comm = prove_fun "P|Q ==> Q|P";
paulson@9840
   156
paulson@9840
   157
 val disj_FalseD1 = prove_fun "False|P ==> P";
paulson@9840
   158
 val disj_FalseD2 = prove_fun "P|False ==> P";
paulson@9840
   159
paulson@9840
   160
paulson@9840
   161
 (**** Operators for forward proof ****)
paulson@9840
   162
paulson@9840
   163
 (*raises exception if no rules apply -- unlike RL*)
paulson@9840
   164
 fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
paulson@9840
   165
   | tryres (th, []) = raise THM("tryres", 0, [th]);
paulson@9840
   166
paulson@9840
   167
 val prop_of = #prop o rep_thm;
paulson@9840
   168
paulson@9840
   169
 (*Permits forward proof from rules that discharge assumptions*)
paulson@9840
   170
 fun forward_res nf st =
paulson@9840
   171
   case Seq.pull (ALLGOALS (METAHYPS (fn [prem] => rtac (nf prem) 1)) st)
paulson@9840
   172
   of Some(th,_) => th
paulson@9840
   173
    | None => raise THM("forward_res", 0, [st]);
paulson@9840
   174
paulson@9840
   175
paulson@9840
   176
 (*Are any of the constants in "bs" present in the term?*)
paulson@9840
   177
 fun has_consts bs = 
paulson@9840
   178
   let fun has (Const(a,_)) = a mem bs
paulson@9840
   179
	 | has (f$u) = has f orelse has u
paulson@9840
   180
	 | has (Abs(_,_,t)) = has t
paulson@9840
   181
	 | has _ = false
paulson@9840
   182
   in  has  end;
paulson@9840
   183
paulson@9840
   184
paulson@9840
   185
 (**** Clause handling ****)
paulson@9840
   186
paulson@9840
   187
 fun literals (Const("Trueprop",_) $ P) = literals P
paulson@9840
   188
   | literals (Const("op |",_) $ P $ Q) = literals P @ literals Q
paulson@9840
   189
   | literals (Const("Not",_) $ P) = [(false,P)]
paulson@9840
   190
   | literals P = [(true,P)];
paulson@9840
   191
paulson@9840
   192
 (*number of literals in a term*)
paulson@9840
   193
 val nliterals = length o literals;
paulson@9840
   194
paulson@9840
   195
 (*to detect, and remove, tautologous clauses*)
paulson@9840
   196
 fun taut_lits [] = false
paulson@9840
   197
   | taut_lits ((flg,t)::ts) = (not flg,t) mem ts orelse taut_lits ts;
paulson@9840
   198
paulson@9840
   199
 (*Include False as a literal: an occurrence of ~False is a tautology*)
paulson@9840
   200
 fun is_taut th = taut_lits ((true, HOLogic.false_const) :: 
paulson@9840
   201
			     literals (prop_of th));
paulson@9840
   202
paulson@9840
   203
 (*Generation of unique names -- maxidx cannot be relied upon to increase!
paulson@9840
   204
   Cannot rely on "variant", since variables might coincide when literals
paulson@9840
   205
   are joined to make a clause... 
paulson@9840
   206
   19 chooses "U" as the first variable name*)
paulson@9840
   207
 val name_ref = ref 19;
paulson@9840
   208
paulson@9840
   209
 (*Replaces universally quantified variables by FREE variables -- because
paulson@9840
   210
   assumptions may not contain scheme variables.  Later, call "generalize". *)
paulson@9840
   211
 fun freeze_spec th =
paulson@9840
   212
   let val sth = th RS spec
paulson@9840
   213
       val newname = (name_ref := !name_ref + 1;
paulson@9840
   214
		      radixstring(26, "A", !name_ref))
paulson@9840
   215
   in  read_instantiate [("x", newname)] sth  end;
paulson@9840
   216
paulson@9840
   217
 fun resop nf [prem] = resolve_tac (nf prem) 1;
paulson@9840
   218
paulson@9840
   219
 (*Conjunctive normal form, detecting tautologies early.
paulson@9840
   220
   Strips universal quantifiers and breaks up conjunctions. *)
paulson@9840
   221
 fun cnf_aux seen (th,ths) = 
paulson@9840
   222
   if taut_lits (literals(prop_of th) @ seen)  then ths
paulson@9840
   223
   else if not (has_consts ["All","op &"] (prop_of th))  then th::ths
paulson@9840
   224
   else (*conjunction?*)
paulson@9840
   225
	 cnf_aux seen (th RS conjunct1, 
paulson@9840
   226
		       cnf_aux seen (th RS conjunct2, ths))
paulson@9840
   227
   handle THM _ => (*universal quant?*)
paulson@9840
   228
	 cnf_aux  seen (freeze_spec th,  ths)
paulson@9840
   229
   handle THM _ => (*disjunction?*)
paulson@9840
   230
     let val tac = 
paulson@9840
   231
	 (METAHYPS (resop (cnf_nil seen)) 1) THEN
paulson@9840
   232
	 (fn st' => st' |>
paulson@9840
   233
		 METAHYPS (resop (cnf_nil (literals (concl_of st') @ seen))) 1)
paulson@9840
   234
     in  Seq.list_of (tac (th RS disj_forward)) @ ths  end
paulson@9840
   235
 and cnf_nil seen th = cnf_aux seen (th,[]);
paulson@9840
   236
paulson@9840
   237
 (*Top-level call to cnf -- it's safe to reset name_ref*)
paulson@9840
   238
 fun cnf (th,ths) = 
paulson@9840
   239
    (name_ref := 19;  cnf (th RS conjunct1, cnf (th RS conjunct2, ths))
paulson@9840
   240
     handle THM _ => (*not a conjunction*) cnf_aux [] (th, ths));
paulson@9840
   241
paulson@9840
   242
 (**** Removal of duplicate literals ****)
paulson@9840
   243
paulson@9840
   244
 (*Forward proof, passing extra assumptions as theorems to the tactic*)
paulson@9840
   245
 fun forward_res2 nf hyps st =
paulson@9840
   246
   case Seq.pull
paulson@9840
   247
	 (REPEAT 
paulson@9840
   248
	  (METAHYPS (fn major::minors => rtac (nf (minors@hyps) major) 1) 1) 
paulson@9840
   249
	  st)
paulson@9840
   250
   of Some(th,_) => th
paulson@9840
   251
    | None => raise THM("forward_res2", 0, [st]);
paulson@9840
   252
paulson@9840
   253
 (*Remove duplicates in P|Q by assuming ~P in Q
paulson@9840
   254
   rls (initially []) accumulates assumptions of the form P==>False*)
paulson@9840
   255
 fun nodups_aux rls th = nodups_aux rls (th RS disj_assoc)
paulson@9840
   256
     handle THM _ => tryres(th,rls)
paulson@9840
   257
     handle THM _ => tryres(forward_res2 nodups_aux rls (th RS disj_forward2),
paulson@9840
   258
			    [disj_FalseD1, disj_FalseD2, asm_rl])
paulson@9840
   259
     handle THM _ => th;
paulson@9840
   260
paulson@9840
   261
 (*Remove duplicate literals, if there are any*)
paulson@9840
   262
 fun nodups th =
paulson@9840
   263
     if null(findrep(literals(prop_of th))) then th
paulson@9840
   264
     else nodups_aux [] th;
paulson@9840
   265
paulson@9840
   266
paulson@9840
   267
 (**** Generation of contrapositives ****)
paulson@9840
   268
paulson@9840
   269
 (*Associate disjuctions to right -- make leftmost disjunct a LITERAL*)
paulson@9840
   270
 fun assoc_right th = assoc_right (th RS disj_assoc)
paulson@9840
   271
	 handle THM _ => th;
paulson@9840
   272
paulson@9840
   273
 (*Must check for negative literal first!*)
paulson@9840
   274
 val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule];
paulson@9840
   275
paulson@9840
   276
 (*For Plaisted's postive refinement.  [currently unused] *)
paulson@9840
   277
 val refined_clause_rules = [disj_assoc, make_refined_neg_rule, make_pos_rule];
paulson@9840
   278
paulson@9840
   279
 (*Create a goal or support clause, conclusing False*)
paulson@9840
   280
 fun make_goal th =   (*Must check for negative literal first!*)
paulson@9840
   281
     make_goal (tryres(th, clause_rules)) 
paulson@9840
   282
   handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]);
paulson@9840
   283
paulson@9840
   284
 (*Sort clauses by number of literals*)
paulson@9840
   285
 fun fewerlits(th1,th2) = nliterals(prop_of th1) < nliterals(prop_of th2);
paulson@9840
   286
paulson@9840
   287
 (*TAUTOLOGY CHECK SHOULD NOT BE NECESSARY!*)
paulson@9840
   288
 fun sort_clauses ths = sort (make_ord fewerlits) (filter (not o is_taut) ths);
paulson@9840
   289
paulson@9840
   290
 (*Convert all suitable free variables to schematic variables*)
paulson@9840
   291
 fun generalize th = forall_elim_vars 0 (forall_intr_frees th);
paulson@9840
   292
paulson@9840
   293
 (*Create a meta-level Horn clause*)
paulson@9840
   294
 fun make_horn crules th = make_horn crules (tryres(th,crules)) 
paulson@9840
   295
			   handle THM _ => th;
paulson@9840
   296
paulson@9840
   297
 (*Generate Horn clauses for all contrapositives of a clause*)
paulson@9840
   298
 fun add_contras crules (th,hcs) = 
paulson@9840
   299
   let fun rots (0,th) = hcs
paulson@9840
   300
	 | rots (k,th) = zero_var_indexes (make_horn crules th) ::
paulson@9840
   301
			 rots(k-1, assoc_right (th RS disj_comm))
paulson@9840
   302
   in case nliterals(prop_of th) of
paulson@9840
   303
	 1 => th::hcs
paulson@9840
   304
       | n => rots(n, assoc_right th)
paulson@9840
   305
   end;
paulson@9840
   306
paulson@9840
   307
 (*Use "theorem naming" to label the clauses*)
paulson@9840
   308
 fun name_thms label = 
paulson@9840
   309
     let fun name1 (th, (k,ths)) =
paulson@9840
   310
	   (k-1, Thm.name_thm (label ^ string_of_int k, th) :: ths)
paulson@9840
   311
paulson@9840
   312
     in  fn ths => #2 (foldr name1 (ths, (length ths, [])))  end;
paulson@9840
   313
paulson@9840
   314
 (*Find an all-negative support clause*)
paulson@9840
   315
 fun is_negative th = forall (not o #1) (literals (prop_of th));
paulson@9840
   316
paulson@9840
   317
 val neg_clauses = filter is_negative;
paulson@9840
   318
paulson@9840
   319
paulson@9840
   320
 (***** MESON PROOF PROCEDURE *****)
paulson@9840
   321
paulson@9840
   322
 fun rhyps (Const("==>",_) $ (Const("Trueprop",_) $ A) $ phi,
paulson@9840
   323
	    As) = rhyps(phi, A::As)
paulson@9840
   324
   | rhyps (_, As) = As;
paulson@9840
   325
paulson@9840
   326
 (** Detecting repeated assumptions in a subgoal **)
paulson@9840
   327
paulson@9840
   328
 (*The stringtree detects repeated assumptions.*)
paulson@9840
   329
 fun ins_term (net,t) = Net.insert_term((t,t), net, op aconv);
paulson@9840
   330
paulson@9840
   331
 (*detects repetitions in a list of terms*)
paulson@9840
   332
 fun has_reps [] = false
paulson@9840
   333
   | has_reps [_] = false
paulson@9840
   334
   | has_reps [t,u] = (t aconv u)
paulson@9840
   335
   | has_reps ts = (foldl ins_term (Net.empty, ts);  false)
paulson@9840
   336
		   handle INSERT => true; 
paulson@9840
   337
paulson@9840
   338
 (*Like TRYALL eq_assume_tac, but avoids expensive THEN calls*)
paulson@9840
   339
 fun TRYALL_eq_assume_tac 0 st = Seq.single st
paulson@9840
   340
   | TRYALL_eq_assume_tac i st = 
paulson@9840
   341
	TRYALL_eq_assume_tac (i-1) (eq_assumption i st)
paulson@9840
   342
	handle THM _ => TRYALL_eq_assume_tac (i-1) st;
paulson@9840
   343
paulson@9840
   344
 (*Loop checking: FAIL if trying to prove the same thing twice
paulson@9840
   345
   -- if *ANY* subgoal has repeated literals*)
paulson@9840
   346
 fun check_tac st = 
paulson@9840
   347
   if exists (fn prem => has_reps (rhyps(prem,[]))) (prems_of st)
paulson@9840
   348
   then  Seq.empty  else  Seq.single st;
paulson@9840
   349
paulson@9840
   350
paulson@9840
   351
 (* net_resolve_tac actually made it slower... *)
paulson@9840
   352
 fun prolog_step_tac horns i = 
paulson@9840
   353
     (assume_tac i APPEND resolve_tac horns i) THEN check_tac THEN
paulson@9840
   354
     TRYALL eq_assume_tac;
paulson@9840
   355
paulson@9840
   356
paulson@9840
   357
in
paulson@9840
   358
paulson@9840
   359
paulson@9840
   360
(*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*)
paulson@9840
   361
local fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz
paulson@9840
   362
in
paulson@9840
   363
fun size_of_subgoals st = foldr addconcl (prems_of st, 0)
paulson@9840
   364
end;
paulson@9840
   365
paulson@9840
   366
(*Negation Normal Form*)
paulson@9840
   367
val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD,
paulson@9840
   368
	       not_impD, not_iffD, not_allD, not_exD, not_notD];
paulson@9840
   369
fun make_nnf th = make_nnf (tryres(th, nnf_rls))
paulson@9840
   370
    handle THM _ => 
paulson@9840
   371
	forward_res make_nnf
paulson@9840
   372
	   (tryres(th, [conj_forward,disj_forward,all_forward,ex_forward]))
paulson@9840
   373
    handle THM _ => th;
paulson@9840
   374
paulson@9840
   375
(*Pull existential quantifiers (Skolemization)*)
paulson@9840
   376
fun skolemize th = 
paulson@9840
   377
  if not (has_consts ["Ex"] (prop_of th)) then th
paulson@9840
   378
  else skolemize (tryres(th, [choice, conj_exD1, conj_exD2,
paulson@9840
   379
			      disj_exD, disj_exD1, disj_exD2]))
paulson@9840
   380
    handle THM _ => 
paulson@9840
   381
	skolemize (forward_res skolemize
paulson@9840
   382
		   (tryres (th, [conj_forward, disj_forward, all_forward])))
paulson@9840
   383
    handle THM _ => forward_res skolemize (th RS ex_forward);
paulson@9840
   384
paulson@9840
   385
paulson@9840
   386
(*Make clauses from a list of theorems, previously Skolemized and put into nnf.
paulson@9840
   387
  The resulting clauses are HOL disjunctions.*)
paulson@9840
   388
fun make_clauses ths = 
paulson@9840
   389
    sort_clauses (map (generalize o nodups) (foldr cnf (ths,[])));
paulson@9840
   390
paulson@9840
   391
(*Convert a list of clauses to (contrapositive) Horn clauses*)
paulson@9840
   392
fun make_horns ths = 
paulson@9840
   393
    name_thms "Horn#"
paulson@9840
   394
      (gen_distinct eq_thm (foldr (add_contras clause_rules) (ths,[])));
paulson@9840
   395
paulson@9840
   396
(*Could simply use nprems_of, which would count remaining subgoals -- no
paulson@9840
   397
  discrimination as to their size!  With BEST_FIRST, fails for problem 41.*)
paulson@9840
   398
paulson@9840
   399
fun best_prolog_tac sizef horns = 
paulson@9840
   400
    BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac horns 1);
paulson@9840
   401
paulson@9840
   402
fun depth_prolog_tac horns = 
paulson@9840
   403
    DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac horns 1);
paulson@9840
   404
paulson@9840
   405
(*Return all negative clauses, as possible goal clauses*)
paulson@9840
   406
fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls));
paulson@9840
   407
paulson@9840
   408
paulson@9840
   409
fun skolemize_tac prems = 
paulson@9840
   410
    cut_facts_tac (map (skolemize o make_nnf) prems)  THEN'
paulson@9840
   411
    REPEAT o (etac exE);
paulson@9840
   412
paulson@9840
   413
(*Shell of all meson-tactics.  Supplies cltac with clauses: HOL disjunctions*)
paulson@9840
   414
fun MESON cltac = SELECT_GOAL
paulson@9840
   415
 (EVERY1 [rtac ccontr,
paulson@9840
   416
          METAHYPS (fn negs =>
paulson@9840
   417
                    EVERY1 [skolemize_tac negs,
paulson@9840
   418
                            METAHYPS (cltac o make_clauses)])]);
paulson@9840
   419
paulson@9840
   420
(** Best-first search versions **)
paulson@9840
   421
paulson@9840
   422
fun best_meson_tac sizef = 
paulson@9840
   423
  MESON (fn cls => 
paulson@9840
   424
         THEN_BEST_FIRST (resolve_tac (gocls cls) 1)
paulson@9840
   425
                         (has_fewer_prems 1, sizef)
paulson@9840
   426
                         (prolog_step_tac (make_horns cls) 1));
paulson@9840
   427
paulson@9840
   428
(*First, breaks the goal into independent units*)
paulson@9840
   429
val safe_best_meson_tac =
paulson@9840
   430
     SELECT_GOAL (TRY Safe_tac THEN 
paulson@9840
   431
                  TRYALL (best_meson_tac size_of_subgoals));
paulson@9840
   432
paulson@9840
   433
(** Depth-first search version **)
paulson@9840
   434
paulson@9840
   435
val depth_meson_tac =
paulson@9840
   436
     MESON (fn cls => EVERY [resolve_tac (gocls cls) 1, 
paulson@9840
   437
                             depth_prolog_tac (make_horns cls)]);
paulson@9840
   438
paulson@9840
   439
paulson@9840
   440
paulson@9840
   441
(** Iterative deepening version **)
paulson@9840
   442
paulson@9840
   443
(*This version does only one inference per call;
paulson@9840
   444
  having only one eq_assume_tac speeds it up!*)
paulson@9840
   445
fun prolog_step_tac' horns = 
paulson@9840
   446
    let val (horn0s, hornps) = (*0 subgoals vs 1 or more*)
paulson@9840
   447
            take_prefix Thm.no_prems horns
paulson@9840
   448
        val nrtac = net_resolve_tac horns
paulson@9840
   449
    in  fn i => eq_assume_tac i ORELSE
paulson@9840
   450
                match_tac horn0s i ORELSE  (*no backtracking if unit MATCHES*)
paulson@9840
   451
                ((assume_tac i APPEND nrtac i) THEN check_tac)
paulson@9840
   452
    end;
paulson@9840
   453
paulson@9840
   454
fun iter_deepen_prolog_tac horns = 
paulson@9840
   455
    ITER_DEEPEN (has_fewer_prems 1) (prolog_step_tac' horns);
paulson@9840
   456
paulson@9840
   457
val iter_deepen_meson_tac = 
paulson@9840
   458
  MESON (fn cls => 
paulson@9840
   459
         (THEN_ITER_DEEPEN (resolve_tac (gocls cls) 1)
paulson@9840
   460
                           (has_fewer_prems 1)
paulson@9840
   461
                           (prolog_step_tac' (make_horns cls))));
paulson@9840
   462
paulson@9840
   463
val meson_tac =
paulson@9840
   464
     SELECT_GOAL (TRY Safe_tac THEN 
paulson@9840
   465
                  TRYALL (iter_deepen_meson_tac));
paulson@9840
   466
paulson@9840
   467
end;