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