src/HOL/Tools/meson.ML
author paulson
Fri Aug 20 12:21:03 2004 +0200 (2004-08-20)
changeset 15151 429666b09783
parent 15118 e2bd080c7975
child 15448 fb7b8313a20d
permissions -rw-r--r--
proof reconstruction for external ATPs
wenzelm@9869
     1
(*  Title:      HOL/Tools/meson.ML
paulson@9840
     2
    ID:         $Id$
paulson@9840
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@9840
     4
    Copyright   1992  University of Cambridge
paulson@9840
     5
wenzelm@9869
     6
The MESON resolution proof procedure for HOL.
paulson@9840
     7
paulson@9840
     8
When making clauses, avoids using the rewriter -- instead uses RS recursively
paulson@9840
     9
paulson@9840
    10
NEED TO SORT LITERALS BY # OF VARS, USING ==>I/E.  ELIMINATES NEED FOR
paulson@9840
    11
FUNCTION nodups -- if done to goal clauses too!
paulson@9840
    12
*)
paulson@9840
    13
paulson@9840
    14
local
paulson@9840
    15
wenzelm@12299
    16
 val not_conjD = thm "meson_not_conjD";
wenzelm@12299
    17
 val not_disjD = thm "meson_not_disjD";
wenzelm@12299
    18
 val not_notD = thm "meson_not_notD";
wenzelm@12299
    19
 val not_allD = thm "meson_not_allD";
wenzelm@12299
    20
 val not_exD = thm "meson_not_exD";
wenzelm@12299
    21
 val imp_to_disjD = thm "meson_imp_to_disjD";
wenzelm@12299
    22
 val not_impD = thm "meson_not_impD";
wenzelm@12299
    23
 val iff_to_disjD = thm "meson_iff_to_disjD";
wenzelm@12299
    24
 val not_iffD = thm "meson_not_iffD";
wenzelm@12299
    25
 val conj_exD1 = thm "meson_conj_exD1";
wenzelm@12299
    26
 val conj_exD2 = thm "meson_conj_exD2";
wenzelm@12299
    27
 val disj_exD = thm "meson_disj_exD";
wenzelm@12299
    28
 val disj_exD1 = thm "meson_disj_exD1";
wenzelm@12299
    29
 val disj_exD2 = thm "meson_disj_exD2";
wenzelm@12299
    30
 val disj_assoc = thm "meson_disj_assoc";
wenzelm@12299
    31
 val disj_comm = thm "meson_disj_comm";
wenzelm@12299
    32
 val disj_FalseD1 = thm "meson_disj_FalseD1";
wenzelm@12299
    33
 val disj_FalseD2 = thm "meson_disj_FalseD2";
paulson@9840
    34
paulson@9840
    35
paulson@9840
    36
 (**** Operators for forward proof ****)
paulson@9840
    37
paulson@9840
    38
 (*raises exception if no rules apply -- unlike RL*)
paulson@9840
    39
 fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
paulson@9840
    40
   | tryres (th, []) = raise THM("tryres", 0, [th]);
paulson@9840
    41
paulson@9840
    42
 val prop_of = #prop o rep_thm;
paulson@9840
    43
paulson@9840
    44
 (*Permits forward proof from rules that discharge assumptions*)
paulson@9840
    45
 fun forward_res nf st =
paulson@9840
    46
   case Seq.pull (ALLGOALS (METAHYPS (fn [prem] => rtac (nf prem) 1)) st)
paulson@9840
    47
   of Some(th,_) => th
paulson@9840
    48
    | None => raise THM("forward_res", 0, [st]);
paulson@9840
    49
paulson@9840
    50
paulson@9840
    51
 (*Are any of the constants in "bs" present in the term?*)
wenzelm@9869
    52
 fun has_consts bs =
paulson@9840
    53
   let fun has (Const(a,_)) = a mem bs
paulson@14763
    54
	 | has (Const ("Hilbert_Choice.Eps",_) $ _) = false
paulson@14763
    55
                      (*ignore constants within @-terms*)
wenzelm@9869
    56
         | has (f$u) = has f orelse has u
wenzelm@9869
    57
         | has (Abs(_,_,t)) = has t
wenzelm@9869
    58
         | has _ = false
paulson@9840
    59
   in  has  end;
paulson@9840
    60
paulson@9840
    61
paulson@9840
    62
 (**** Clause handling ****)
paulson@9840
    63
paulson@9840
    64
 fun literals (Const("Trueprop",_) $ P) = literals P
paulson@9840
    65
   | literals (Const("op |",_) $ P $ Q) = literals P @ literals Q
paulson@9840
    66
   | literals (Const("Not",_) $ P) = [(false,P)]
paulson@9840
    67
   | literals P = [(true,P)];
paulson@9840
    68
paulson@9840
    69
 (*number of literals in a term*)
paulson@9840
    70
 val nliterals = length o literals;
paulson@9840
    71
paulson@9840
    72
 (*to detect, and remove, tautologous clauses*)
paulson@9840
    73
 fun taut_lits [] = false
paulson@9840
    74
   | taut_lits ((flg,t)::ts) = (not flg,t) mem ts orelse taut_lits ts;
paulson@9840
    75
paulson@9840
    76
 (*Include False as a literal: an occurrence of ~False is a tautology*)
wenzelm@9869
    77
 fun is_taut th = taut_lits ((true, HOLogic.false_const) ::
wenzelm@9869
    78
                             literals (prop_of th));
paulson@9840
    79
paulson@9840
    80
 (*Generation of unique names -- maxidx cannot be relied upon to increase!
paulson@9840
    81
   Cannot rely on "variant", since variables might coincide when literals
wenzelm@9869
    82
   are joined to make a clause...
paulson@9840
    83
   19 chooses "U" as the first variable name*)
paulson@9840
    84
 val name_ref = ref 19;
paulson@9840
    85
paulson@9840
    86
 (*Replaces universally quantified variables by FREE variables -- because
paulson@9840
    87
   assumptions may not contain scheme variables.  Later, call "generalize". *)
paulson@9840
    88
 fun freeze_spec th =
paulson@9840
    89
   let val sth = th RS spec
paulson@9840
    90
       val newname = (name_ref := !name_ref + 1;
wenzelm@9869
    91
                      radixstring(26, "A", !name_ref))
paulson@9840
    92
   in  read_instantiate [("x", newname)] sth  end;
paulson@9840
    93
paulson@9840
    94
 fun resop nf [prem] = resolve_tac (nf prem) 1;
paulson@9840
    95
paulson@9840
    96
 (*Conjunctive normal form, detecting tautologies early.
paulson@9840
    97
   Strips universal quantifiers and breaks up conjunctions. *)
wenzelm@9869
    98
 fun cnf_aux seen (th,ths) =
paulson@14763
    99
   if taut_lits (literals(prop_of th) @ seen)  
paulson@14763
   100
   then ths     (*tautology ignored*)
paulson@14763
   101
   else if not (has_consts ["All","op &"] (prop_of th))  
paulson@14763
   102
   then th::ths (*no work to do, terminate*)
paulson@9840
   103
   else (*conjunction?*)
wenzelm@9869
   104
         cnf_aux seen (th RS conjunct1,
wenzelm@9869
   105
                       cnf_aux seen (th RS conjunct2, ths))
paulson@9840
   106
   handle THM _ => (*universal quant?*)
wenzelm@9869
   107
         cnf_aux  seen (freeze_spec th,  ths)
paulson@9840
   108
   handle THM _ => (*disjunction?*)
wenzelm@9869
   109
     let val tac =
wenzelm@9869
   110
         (METAHYPS (resop (cnf_nil seen)) 1) THEN
wenzelm@9869
   111
         (fn st' => st' |>
wenzelm@9869
   112
                 METAHYPS (resop (cnf_nil (literals (concl_of st') @ seen))) 1)
paulson@9840
   113
     in  Seq.list_of (tac (th RS disj_forward)) @ ths  end
paulson@9840
   114
 and cnf_nil seen th = cnf_aux seen (th,[]);
paulson@9840
   115
paulson@9840
   116
 (*Top-level call to cnf -- it's safe to reset name_ref*)
wenzelm@9869
   117
 fun cnf (th,ths) =
paulson@9840
   118
    (name_ref := 19;  cnf (th RS conjunct1, cnf (th RS conjunct2, ths))
paulson@9840
   119
     handle THM _ => (*not a conjunction*) cnf_aux [] (th, ths));
paulson@9840
   120
paulson@9840
   121
 (**** Removal of duplicate literals ****)
paulson@9840
   122
paulson@9840
   123
 (*Forward proof, passing extra assumptions as theorems to the tactic*)
paulson@9840
   124
 fun forward_res2 nf hyps st =
paulson@9840
   125
   case Seq.pull
wenzelm@9869
   126
         (REPEAT
wenzelm@9869
   127
          (METAHYPS (fn major::minors => rtac (nf (minors@hyps) major) 1) 1)
wenzelm@9869
   128
          st)
paulson@9840
   129
   of Some(th,_) => th
paulson@9840
   130
    | None => raise THM("forward_res2", 0, [st]);
paulson@9840
   131
paulson@9840
   132
 (*Remove duplicates in P|Q by assuming ~P in Q
paulson@9840
   133
   rls (initially []) accumulates assumptions of the form P==>False*)
paulson@9840
   134
 fun nodups_aux rls th = nodups_aux rls (th RS disj_assoc)
paulson@9840
   135
     handle THM _ => tryres(th,rls)
paulson@9840
   136
     handle THM _ => tryres(forward_res2 nodups_aux rls (th RS disj_forward2),
wenzelm@9869
   137
                            [disj_FalseD1, disj_FalseD2, asm_rl])
paulson@9840
   138
     handle THM _ => th;
paulson@9840
   139
paulson@9840
   140
 (*Remove duplicate literals, if there are any*)
paulson@9840
   141
 fun nodups th =
paulson@9840
   142
     if null(findrep(literals(prop_of th))) then th
paulson@9840
   143
     else nodups_aux [] th;
paulson@9840
   144
paulson@9840
   145
paulson@9840
   146
 (**** Generation of contrapositives ****)
paulson@9840
   147
paulson@9840
   148
 (*Associate disjuctions to right -- make leftmost disjunct a LITERAL*)
paulson@9840
   149
 fun assoc_right th = assoc_right (th RS disj_assoc)
wenzelm@9869
   150
         handle THM _ => th;
paulson@9840
   151
paulson@9840
   152
 (*Must check for negative literal first!*)
paulson@9840
   153
 val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule];
paulson@9840
   154
paulson@14733
   155
 (*For ordinary resolution. *)
paulson@14733
   156
 val resolution_clause_rules = [disj_assoc, make_neg_rule', make_pos_rule'];
paulson@9840
   157
paulson@9840
   158
 (*Create a goal or support clause, conclusing False*)
paulson@9840
   159
 fun make_goal th =   (*Must check for negative literal first!*)
wenzelm@9869
   160
     make_goal (tryres(th, clause_rules))
paulson@9840
   161
   handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]);
paulson@9840
   162
paulson@9840
   163
 (*Sort clauses by number of literals*)
paulson@9840
   164
 fun fewerlits(th1,th2) = nliterals(prop_of th1) < nliterals(prop_of th2);
paulson@9840
   165
paulson@9840
   166
 (*TAUTOLOGY CHECK SHOULD NOT BE NECESSARY!*)
paulson@9840
   167
 fun sort_clauses ths = sort (make_ord fewerlits) (filter (not o is_taut) ths);
paulson@9840
   168
paulson@9840
   169
 (*Convert all suitable free variables to schematic variables*)
paulson@9840
   170
 fun generalize th = forall_elim_vars 0 (forall_intr_frees th);
paulson@9840
   171
paulson@9840
   172
 (*Create a meta-level Horn clause*)
wenzelm@9869
   173
 fun make_horn crules th = make_horn crules (tryres(th,crules))
wenzelm@9869
   174
                           handle THM _ => th;
paulson@9840
   175
paulson@9840
   176
 (*Generate Horn clauses for all contrapositives of a clause*)
wenzelm@9869
   177
 fun add_contras crules (th,hcs) =
paulson@9840
   178
   let fun rots (0,th) = hcs
wenzelm@9869
   179
         | rots (k,th) = zero_var_indexes (make_horn crules th) ::
wenzelm@9869
   180
                         rots(k-1, assoc_right (th RS disj_comm))
paulson@9840
   181
   in case nliterals(prop_of th) of
wenzelm@9869
   182
         1 => th::hcs
paulson@9840
   183
       | n => rots(n, assoc_right th)
paulson@9840
   184
   end;
paulson@9840
   185
paulson@9840
   186
 (*Use "theorem naming" to label the clauses*)
wenzelm@9869
   187
 fun name_thms label =
paulson@9840
   188
     let fun name1 (th, (k,ths)) =
wenzelm@9869
   189
           (k-1, Thm.name_thm (label ^ string_of_int k, th) :: ths)
paulson@9840
   190
paulson@9840
   191
     in  fn ths => #2 (foldr name1 (ths, (length ths, [])))  end;
paulson@9840
   192
paulson@9840
   193
 (*Find an all-negative support clause*)
paulson@9840
   194
 fun is_negative th = forall (not o #1) (literals (prop_of th));
paulson@9840
   195
paulson@9840
   196
 val neg_clauses = filter is_negative;
paulson@9840
   197
paulson@9840
   198
paulson@9840
   199
 (***** MESON PROOF PROCEDURE *****)
paulson@9840
   200
paulson@9840
   201
 fun rhyps (Const("==>",_) $ (Const("Trueprop",_) $ A) $ phi,
wenzelm@9869
   202
            As) = rhyps(phi, A::As)
paulson@9840
   203
   | rhyps (_, As) = As;
paulson@9840
   204
paulson@9840
   205
 (** Detecting repeated assumptions in a subgoal **)
paulson@9840
   206
paulson@9840
   207
 (*The stringtree detects repeated assumptions.*)
paulson@9840
   208
 fun ins_term (net,t) = Net.insert_term((t,t), net, op aconv);
paulson@9840
   209
paulson@9840
   210
 (*detects repetitions in a list of terms*)
paulson@9840
   211
 fun has_reps [] = false
paulson@9840
   212
   | has_reps [_] = false
paulson@9840
   213
   | has_reps [t,u] = (t aconv u)
paulson@9840
   214
   | has_reps ts = (foldl ins_term (Net.empty, ts);  false)
wenzelm@9869
   215
                   handle INSERT => true;
paulson@9840
   216
paulson@9840
   217
 (*Like TRYALL eq_assume_tac, but avoids expensive THEN calls*)
paulson@9840
   218
 fun TRYALL_eq_assume_tac 0 st = Seq.single st
wenzelm@9869
   219
   | TRYALL_eq_assume_tac i st =
wenzelm@9869
   220
        TRYALL_eq_assume_tac (i-1) (eq_assumption i st)
wenzelm@9869
   221
        handle THM _ => TRYALL_eq_assume_tac (i-1) st;
paulson@9840
   222
paulson@9840
   223
 (*Loop checking: FAIL if trying to prove the same thing twice
paulson@9840
   224
   -- if *ANY* subgoal has repeated literals*)
wenzelm@9869
   225
 fun check_tac st =
paulson@9840
   226
   if exists (fn prem => has_reps (rhyps(prem,[]))) (prems_of st)
paulson@9840
   227
   then  Seq.empty  else  Seq.single st;
paulson@9840
   228
paulson@9840
   229
paulson@9840
   230
 (* net_resolve_tac actually made it slower... *)
wenzelm@9869
   231
 fun prolog_step_tac horns i =
paulson@9840
   232
     (assume_tac i APPEND resolve_tac horns i) THEN check_tac THEN
paulson@9840
   233
     TRYALL eq_assume_tac;
paulson@9840
   234
paulson@9840
   235
paulson@9840
   236
in
paulson@9840
   237
paulson@9840
   238
paulson@9840
   239
(*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*)
paulson@9840
   240
local fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz
paulson@9840
   241
in
paulson@9840
   242
fun size_of_subgoals st = foldr addconcl (prems_of st, 0)
paulson@9840
   243
end;
paulson@9840
   244
paulson@9840
   245
(*Negation Normal Form*)
paulson@9840
   246
val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD,
wenzelm@9869
   247
               not_impD, not_iffD, not_allD, not_exD, not_notD];
paulson@9840
   248
fun make_nnf th = make_nnf (tryres(th, nnf_rls))
wenzelm@9869
   249
    handle THM _ =>
wenzelm@9869
   250
        forward_res make_nnf
wenzelm@9869
   251
           (tryres(th, [conj_forward,disj_forward,all_forward,ex_forward]))
paulson@9840
   252
    handle THM _ => th;
paulson@9840
   253
paulson@9840
   254
(*Pull existential quantifiers (Skolemization)*)
wenzelm@9869
   255
fun skolemize th =
paulson@9840
   256
  if not (has_consts ["Ex"] (prop_of th)) then th
paulson@9840
   257
  else skolemize (tryres(th, [choice, conj_exD1, conj_exD2,
wenzelm@9869
   258
                              disj_exD, disj_exD1, disj_exD2]))
wenzelm@9869
   259
    handle THM _ =>
wenzelm@9869
   260
        skolemize (forward_res skolemize
wenzelm@9869
   261
                   (tryres (th, [conj_forward, disj_forward, all_forward])))
paulson@9840
   262
    handle THM _ => forward_res skolemize (th RS ex_forward);
paulson@9840
   263
paulson@9840
   264
paulson@9840
   265
(*Make clauses from a list of theorems, previously Skolemized and put into nnf.
paulson@9840
   266
  The resulting clauses are HOL disjunctions.*)
wenzelm@9869
   267
fun make_clauses ths =
paulson@9840
   268
    sort_clauses (map (generalize o nodups) (foldr cnf (ths,[])));
paulson@9840
   269
paulson@9840
   270
(*Convert a list of clauses to (contrapositive) Horn clauses*)
wenzelm@9869
   271
fun make_horns ths =
paulson@9840
   272
    name_thms "Horn#"
wenzelm@13105
   273
      (gen_distinct Drule.eq_thm_prop (foldr (add_contras clause_rules) (ths,[])));
paulson@9840
   274
paulson@9840
   275
(*Could simply use nprems_of, which would count remaining subgoals -- no
paulson@9840
   276
  discrimination as to their size!  With BEST_FIRST, fails for problem 41.*)
paulson@9840
   277
wenzelm@9869
   278
fun best_prolog_tac sizef horns =
paulson@9840
   279
    BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac horns 1);
paulson@9840
   280
wenzelm@9869
   281
fun depth_prolog_tac horns =
paulson@9840
   282
    DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac horns 1);
paulson@9840
   283
paulson@9840
   284
(*Return all negative clauses, as possible goal clauses*)
paulson@9840
   285
fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls));
paulson@9840
   286
paulson@9840
   287
paulson@15008
   288
fun skolemize_prems_tac prems =
paulson@9840
   289
    cut_facts_tac (map (skolemize o make_nnf) prems)  THEN'
paulson@9840
   290
    REPEAT o (etac exE);
paulson@9840
   291
paulson@9840
   292
(*Shell of all meson-tactics.  Supplies cltac with clauses: HOL disjunctions*)
paulson@9840
   293
fun MESON cltac = SELECT_GOAL
paulson@9840
   294
 (EVERY1 [rtac ccontr,
paulson@9840
   295
          METAHYPS (fn negs =>
paulson@15008
   296
                    EVERY1 [skolemize_prems_tac negs,
paulson@9840
   297
                            METAHYPS (cltac o make_clauses)])]);
paulson@9840
   298
paulson@9840
   299
(** Best-first search versions **)
paulson@9840
   300
wenzelm@9869
   301
fun best_meson_tac sizef =
wenzelm@9869
   302
  MESON (fn cls =>
paulson@9840
   303
         THEN_BEST_FIRST (resolve_tac (gocls cls) 1)
paulson@9840
   304
                         (has_fewer_prems 1, sizef)
paulson@9840
   305
                         (prolog_step_tac (make_horns cls) 1));
paulson@9840
   306
paulson@9840
   307
(*First, breaks the goal into independent units*)
paulson@9840
   308
val safe_best_meson_tac =
wenzelm@9869
   309
     SELECT_GOAL (TRY Safe_tac THEN
paulson@9840
   310
                  TRYALL (best_meson_tac size_of_subgoals));
paulson@9840
   311
paulson@9840
   312
(** Depth-first search version **)
paulson@9840
   313
paulson@9840
   314
val depth_meson_tac =
wenzelm@9869
   315
     MESON (fn cls => EVERY [resolve_tac (gocls cls) 1,
paulson@9840
   316
                             depth_prolog_tac (make_horns cls)]);
paulson@9840
   317
paulson@9840
   318
paulson@9840
   319
paulson@9840
   320
(** Iterative deepening version **)
paulson@9840
   321
paulson@9840
   322
(*This version does only one inference per call;
paulson@9840
   323
  having only one eq_assume_tac speeds it up!*)
wenzelm@9869
   324
fun prolog_step_tac' horns =
paulson@9840
   325
    let val (horn0s, hornps) = (*0 subgoals vs 1 or more*)
paulson@9840
   326
            take_prefix Thm.no_prems horns
paulson@9840
   327
        val nrtac = net_resolve_tac horns
paulson@9840
   328
    in  fn i => eq_assume_tac i ORELSE
paulson@9840
   329
                match_tac horn0s i ORELSE  (*no backtracking if unit MATCHES*)
paulson@9840
   330
                ((assume_tac i APPEND nrtac i) THEN check_tac)
paulson@9840
   331
    end;
paulson@9840
   332
wenzelm@9869
   333
fun iter_deepen_prolog_tac horns =
paulson@9840
   334
    ITER_DEEPEN (has_fewer_prems 1) (prolog_step_tac' horns);
paulson@9840
   335
wenzelm@9869
   336
val iter_deepen_meson_tac =
wenzelm@9869
   337
  MESON (fn cls =>
paulson@9840
   338
         (THEN_ITER_DEEPEN (resolve_tac (gocls cls) 1)
paulson@9840
   339
                           (has_fewer_prems 1)
paulson@9840
   340
                           (prolog_step_tac' (make_horns cls))));
paulson@9840
   341
wenzelm@9869
   342
fun meson_claset_tac cs =
wenzelm@9869
   343
  SELECT_GOAL (TRY (safe_tac cs) THEN TRYALL iter_deepen_meson_tac);
wenzelm@9869
   344
wenzelm@9869
   345
val meson_tac = CLASET' meson_claset_tac;
wenzelm@9869
   346
wenzelm@9869
   347
paulson@14813
   348
(**** Code to support ordinary resolution, rather than Model Elimination ****)
paulson@14744
   349
paulson@15008
   350
(*Convert a list of clauses (disjunctions) to meta-level clauses (==>), 
paulson@15008
   351
  with no contrapositives, for ordinary resolution.*)
paulson@14744
   352
paulson@14744
   353
(*Rules to convert the head literal into a negated assumption. If the head
paulson@14744
   354
  literal is already negated, then using notEfalse instead of notEfalse'
paulson@14744
   355
  prevents a double negation.*)
paulson@14744
   356
val notEfalse = read_instantiate [("R","False")] notE;
paulson@14744
   357
val notEfalse' = rotate_prems 1 notEfalse;
paulson@14744
   358
paulson@14744
   359
fun negate_nead th = 
paulson@14744
   360
    th RS notEfalse handle THM _ => th RS notEfalse';
paulson@14744
   361
paulson@14744
   362
(*Converting one clause*)
paulson@14744
   363
fun make_meta_clause th = negate_nead (make_horn resolution_clause_rules th);
paulson@14744
   364
paulson@14744
   365
fun make_meta_clauses ths =
paulson@14744
   366
    name_thms "MClause#"
paulson@14744
   367
      (gen_distinct Drule.eq_thm_prop (map make_meta_clause ths));
paulson@14744
   368
paulson@14744
   369
(*Permute a rule's premises to move the i-th premise to the last position.*)
paulson@14744
   370
fun make_last i th =
paulson@14744
   371
  let val n = nprems_of th 
paulson@14744
   372
  in  if 1 <= i andalso i <= n 
paulson@14744
   373
      then Thm.permute_prems (i-1) 1 th
paulson@15118
   374
      else raise THM("select_literal", i, [th])
paulson@14744
   375
  end;
paulson@14744
   376
paulson@14744
   377
(*Maps a rule that ends "... ==> P ==> False" to "... ==> ~P" while suppressing
paulson@14744
   378
  double-negations.*)
paulson@14744
   379
val negate_head = rewrite_rule [atomize_not, not_not RS eq_reflection];
paulson@14744
   380
paulson@14744
   381
(*Maps the clause  [P1,...Pn]==>False to [P1,...,P(i-1),P(i+1),...Pn] ==> ~P*)
paulson@14744
   382
fun select_literal i cl = negate_head (make_last i cl);
paulson@14744
   383
paulson@14813
   384
(*Top-level Skolemization. Allows part of the conversion to clauses to be
paulson@14813
   385
  expressed as a tactic (or Isar method).  Each assumption of the selected 
paulson@14813
   386
  goal is converted to NNF and then its existential quantifiers are pulled
paulson@14813
   387
  to the front. Finally, all existential quantifiers are eliminated, 
paulson@14813
   388
  leaving !!-quantified variables. Perhaps Safe_tac should follow, but it
paulson@14813
   389
  might generate many subgoals.*)
paulson@14813
   390
val skolemize_tac = 
paulson@14813
   391
  SUBGOAL
paulson@14813
   392
    (fn (prop,_) =>
paulson@14813
   393
     let val ts = Logic.strip_assums_hyp prop
paulson@14813
   394
     in EVERY1 
paulson@14813
   395
	 [METAHYPS
paulson@14813
   396
	    (fn hyps => (cut_facts_tac (map (skolemize o make_nnf) hyps) 1
paulson@14813
   397
                         THEN REPEAT (etac exE 1))),
paulson@14813
   398
	  REPEAT_DETERM_N (length ts) o (etac thin_rl)]
paulson@14813
   399
     end);
paulson@14813
   400
paulson@15118
   401
(*Top-level conversion to meta-level clauses. Each clause has  
paulson@15118
   402
  leading !!-bound universal variables, to express generality. To get 
paulson@15118
   403
  disjunctions instead of meta-clauses, remove "make_meta_clauses" below.*)
paulson@15008
   404
val make_clauses_tac = 
paulson@15008
   405
  SUBGOAL
paulson@15008
   406
    (fn (prop,_) =>
paulson@15008
   407
     let val ts = Logic.strip_assums_hyp prop
paulson@15008
   408
     in EVERY1 
paulson@15008
   409
	 [METAHYPS
paulson@15008
   410
	    (fn hyps => 
paulson@15151
   411
              (Method.insert_tac
paulson@15118
   412
                (map forall_intr_vars 
paulson@15118
   413
                  (make_meta_clauses (make_clauses hyps))) 1)),
paulson@15008
   414
	  REPEAT_DETERM_N (length ts) o (etac thin_rl)]
paulson@15008
   415
     end);
paulson@15008
   416
paulson@14744
   417
paulson@14744
   418
(** proof method setup **)
wenzelm@9869
   419
wenzelm@9869
   420
local
wenzelm@9869
   421
wenzelm@9869
   422
fun meson_meth ctxt =
wenzelm@10821
   423
  Method.SIMPLE_METHOD' HEADGOAL
wenzelm@15032
   424
    (CHANGED_PROP o meson_claset_tac (local_claset_of ctxt));
wenzelm@9869
   425
paulson@14890
   426
val skolemize_meth =
paulson@14890
   427
  Method.SIMPLE_METHOD' HEADGOAL
paulson@14890
   428
    (CHANGED_PROP o skolemize_tac);
paulson@14890
   429
paulson@15008
   430
val make_clauses_meth =
paulson@15008
   431
  Method.SIMPLE_METHOD' HEADGOAL
paulson@15008
   432
    (CHANGED_PROP o make_clauses_tac);
paulson@15008
   433
wenzelm@9869
   434
in
wenzelm@9869
   435
wenzelm@9869
   436
val meson_setup =
wenzelm@9869
   437
 [Method.add_methods
paulson@14813
   438
  [("meson", Method.ctxt_args meson_meth, 
paulson@14813
   439
    "The MESON resolution proof procedure"),
paulson@14890
   440
   ("skolemize", Method.no_args skolemize_meth, 
paulson@15008
   441
    "Skolemization into existential quantifiers"),
paulson@15008
   442
   ("make_clauses", Method.no_args make_clauses_meth, 
paulson@15118
   443
    "Conversion to !!-quantified meta-level clauses")]];
paulson@9840
   444
paulson@9840
   445
end;
wenzelm@9869
   446
wenzelm@9869
   447
end;