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