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