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