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