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