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