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