src/Provers/classical.ML
author berghofe
Tue May 07 09:46:25 1996 +0200 (1996-05-07)
changeset 1724 bb02e6976258
parent 1711 c06d01f75764
child 1800 3d9d2ef0cd3b
permissions -rw-r--r--
Added functions for default claset.
clasohm@0
     1
(*  Title: 	Provers/classical
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1992  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Theorem prover for classical reasoning, including predicate calculus, set
clasohm@0
     7
theory, etc.
clasohm@0
     8
clasohm@0
     9
Rules must be classified as intr, elim, safe, hazardous.
clasohm@0
    10
clasohm@0
    11
A rule is unsafe unless it can be applied blindly without harmful results.
clasohm@0
    12
For a rule to be safe, its premises and conclusion should be logically
clasohm@0
    13
equivalent.  There should be no variables in the premises that are not in
clasohm@0
    14
the conclusion.
clasohm@0
    15
*)
clasohm@0
    16
lcp@982
    17
infix 1 THEN_MAYBE;
lcp@982
    18
clasohm@0
    19
signature CLASSICAL_DATA =
clasohm@0
    20
  sig
lcp@681
    21
  val mp	: thm    	(* [| P-->Q;  P |] ==> Q *)
lcp@681
    22
  val not_elim	: thm		(* [| ~P;  P |] ==> R *)
lcp@681
    23
  val classical	: thm		(* (~P ==> P) ==> P *)
lcp@681
    24
  val sizef 	: thm -> int	(* size function for BEST_FIRST *)
clasohm@0
    25
  val hyp_subst_tacs: (int -> tactic) list
clasohm@0
    26
  end;
clasohm@0
    27
clasohm@0
    28
(*Higher precedence than := facilitates use of references*)
lcp@982
    29
infix 4 addSIs addSEs addSDs addIs addEs addDs 
lcp@982
    30
        setwrapper compwrapper addbefore addafter;
clasohm@0
    31
clasohm@0
    32
clasohm@0
    33
signature CLASSICAL =
clasohm@0
    34
  sig
clasohm@0
    35
  type claset
lcp@1073
    36
  type netpair
lcp@681
    37
  val empty_cs		: claset
paulson@1711
    38
  val merge_cs		: claset * claset -> claset
lcp@681
    39
  val addDs 		: claset * thm list -> claset
lcp@681
    40
  val addEs 		: claset * thm list -> claset
lcp@681
    41
  val addIs 		: claset * thm list -> claset
lcp@681
    42
  val addSDs		: claset * thm list -> claset
lcp@681
    43
  val addSEs		: claset * thm list -> claset
lcp@681
    44
  val addSIs		: claset * thm list -> claset
lcp@982
    45
  val setwrapper 	: claset * (tactic->tactic) -> claset
lcp@982
    46
  val compwrapper 	: claset * (tactic->tactic) -> claset
lcp@982
    47
  val addbefore 	: claset * tactic -> claset
lcp@982
    48
  val addafter 		: claset * tactic -> claset
lcp@982
    49
lcp@681
    50
  val print_cs		: claset -> unit
lcp@1073
    51
  val rep_claset	: 
lcp@1073
    52
      claset -> {safeIs: thm list, safeEs: thm list, 
lcp@1073
    53
		 hazIs: thm list, hazEs: thm list,
lcp@1073
    54
		 wrapper: tactic -> tactic,
lcp@1073
    55
		 safe0_netpair: netpair, safep_netpair: netpair,
lcp@1073
    56
		 haz_netpair: netpair, dup_netpair: netpair}
lcp@982
    57
  val getwrapper	: claset -> tactic -> tactic
lcp@982
    58
  val THEN_MAYBE	: tactic * tactic -> tactic
lcp@982
    59
paulson@1587
    60
  val fast_tac 		: claset -> int -> tactic
paulson@1587
    61
  val slow_tac 		: claset -> int -> tactic
paulson@1587
    62
  val weight_ASTAR	: int ref
paulson@1587
    63
  val astar_tac		: claset -> int -> tactic
paulson@1587
    64
  val slow_astar_tac 	: claset -> int -> tactic
lcp@681
    65
  val best_tac 		: claset -> int -> tactic
paulson@1587
    66
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
    67
  val depth_tac		: claset -> int -> int -> tactic
lcp@681
    68
  val deepen_tac	: claset -> int -> int -> tactic
paulson@1587
    69
paulson@1587
    70
  val contr_tac 	: int -> tactic
lcp@681
    71
  val dup_elim		: thm -> thm
lcp@681
    72
  val dup_intr		: thm -> thm
lcp@681
    73
  val dup_step_tac	: claset -> int -> tactic
lcp@681
    74
  val eq_mp_tac		: int -> tactic
lcp@681
    75
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
    76
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
    77
  val mp_tac		: int -> tactic
lcp@681
    78
  val safe_tac 		: claset -> tactic
lcp@681
    79
  val safe_step_tac 	: claset -> int -> tactic
lcp@681
    80
  val step_tac 		: claset -> int -> tactic
lcp@681
    81
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
    82
  val swapify 		: thm list -> thm list
lcp@681
    83
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
    84
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
    85
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
    86
  val instp_step_tac 	: claset -> int -> tactic
berghofe@1724
    87
berghofe@1724
    88
  val claset : claset ref
berghofe@1724
    89
  val AddDs 		: thm list -> unit
berghofe@1724
    90
  val AddEs 		: thm list -> unit
berghofe@1724
    91
  val AddIs 		: thm list -> unit
berghofe@1724
    92
  val AddSDs		: thm list -> unit
berghofe@1724
    93
  val AddSEs		: thm list -> unit
berghofe@1724
    94
  val AddSIs		: thm list -> unit
berghofe@1724
    95
  val Fast_tac 		: int -> tactic
berghofe@1724
    96
clasohm@0
    97
  end;
clasohm@0
    98
clasohm@0
    99
clasohm@0
   100
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
   101
struct
clasohm@0
   102
clasohm@0
   103
local open Data in
clasohm@0
   104
clasohm@0
   105
(** Useful tactics for classical reasoning **)
clasohm@0
   106
paulson@1524
   107
val imp_elim = (*cannot use bind_thm within a structure!*)
paulson@1524
   108
  store_thm ("imp_elim", make_elim mp);
clasohm@0
   109
clasohm@0
   110
(*Solve goal that assumes both P and ~P. *)
clasohm@0
   111
val contr_tac = eresolve_tac [not_elim]  THEN'  assume_tac;
clasohm@0
   112
lcp@681
   113
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   114
  Could do the same thing for P<->Q and P... *)
lcp@681
   115
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
   116
clasohm@0
   117
(*Like mp_tac but instantiates no variables*)
lcp@681
   118
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
   119
paulson@1524
   120
val swap =
paulson@1524
   121
  store_thm ("swap", rule_by_tactic (etac thin_rl 1) (not_elim RS classical));
clasohm@0
   122
clasohm@0
   123
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
   124
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
   125
clasohm@0
   126
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   127
  trying rules in order. *)
clasohm@0
   128
fun swap_res_tac rls = 
lcp@54
   129
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
   130
    in  assume_tac 	ORELSE' 
lcp@54
   131
	contr_tac 	ORELSE' 
lcp@54
   132
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   133
    end;
clasohm@0
   134
lcp@681
   135
(*Duplication of hazardous rules, for complete provers*)
lcp@681
   136
fun dup_intr th = standard (th RS classical);
lcp@681
   137
lcp@681
   138
fun dup_elim th = th RSN (2, revcut_rl) |> assumption 2 |> Sequence.hd |> 
lcp@681
   139
                  rule_by_tactic (TRYALL (etac revcut_rl));
clasohm@0
   140
lcp@1073
   141
clasohm@0
   142
(*** Classical rule sets ***)
clasohm@0
   143
clasohm@0
   144
type netpair = (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net;
clasohm@0
   145
clasohm@0
   146
datatype claset =
lcp@982
   147
  CS of {safeIs		: thm list,		(*safe introduction rules*)
lcp@982
   148
	 safeEs		: thm list,		(*safe elimination rules*)
lcp@982
   149
	 hazIs		: thm list,		(*unsafe introduction rules*)
lcp@982
   150
	 hazEs		: thm list,		(*unsafe elimination rules*)
lcp@982
   151
	 wrapper	: tactic->tactic,	(*for transforming step_tac*)
lcp@982
   152
	 safe0_netpair	: netpair,		(*nets for trivial cases*)
lcp@982
   153
	 safep_netpair	: netpair,		(*nets for >0 subgoals*)
lcp@982
   154
	 haz_netpair  	: netpair,		(*nets for unsafe rules*)
lcp@982
   155
	 dup_netpair	: netpair};		(*nets for duplication*)
clasohm@0
   156
lcp@1073
   157
(*Desired invariants are
lcp@681
   158
	safe0_netpair = build safe0_brls,
lcp@681
   159
	safep_netpair = build safep_brls,
lcp@681
   160
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   161
	dup_netpair = build (joinrules(map dup_intr hazIs, 
lcp@681
   162
				       map dup_elim hazEs))}
lcp@1073
   163
lcp@1073
   164
where build = build_netpair(Net.empty,Net.empty), 
lcp@1073
   165
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   166
      safep_brls contains all brules that generate 1 or more new subgoals.
lcp@1073
   167
Nets must be built incrementally, to save space and time.
lcp@1073
   168
*)
clasohm@0
   169
lcp@1073
   170
val empty_cs = 
lcp@1073
   171
  CS{safeIs	= [],
lcp@1073
   172
     safeEs	= [],
lcp@1073
   173
     hazIs	= [],
lcp@1073
   174
     hazEs	= [],
lcp@1073
   175
     wrapper 	= I,
lcp@1073
   176
     safe0_netpair = (Net.empty,Net.empty),
lcp@1073
   177
     safep_netpair = (Net.empty,Net.empty),
lcp@1073
   178
     haz_netpair   = (Net.empty,Net.empty),
lcp@1073
   179
     dup_netpair   = (Net.empty,Net.empty)};
clasohm@0
   180
clasohm@0
   181
fun print_cs (CS{safeIs,safeEs,hazIs,hazEs,...}) =
lcp@1073
   182
 (writeln"Introduction rules";  	prths hazIs;
lcp@1073
   183
  writeln"Safe introduction rules";  	prths safeIs;
lcp@1073
   184
  writeln"Elimination rules";  		prths hazEs;
lcp@1073
   185
  writeln"Safe elimination rules";  	prths safeEs;
clasohm@0
   186
  ());
clasohm@0
   187
lcp@1073
   188
fun rep_claset (CS args) = args;
lcp@1073
   189
lcp@1073
   190
fun getwrapper (CS{wrapper,...}) = wrapper;
lcp@1073
   191
lcp@1073
   192
lcp@1073
   193
(** Adding (un)safe introduction or elimination rules.
lcp@1073
   194
lcp@1073
   195
    In case of overlap, new rules are tried BEFORE old ones!!
lcp@1010
   196
**)
clasohm@0
   197
lcp@1073
   198
(*For use with biresolve_tac.  Combines intr rules with swap to handle negated
lcp@1073
   199
  assumptions.  Pairs elim rules with true. *)
lcp@1073
   200
fun joinrules (intrs,elims) =  
lcp@1073
   201
    (map (pair true) (elims @ swapify intrs)  @
lcp@1073
   202
     map (pair false) intrs);
lcp@1073
   203
lcp@1073
   204
(*Priority: prefer rules with fewest subgoals, 
paulson@1231
   205
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   206
fun tag_brls k [] = []
lcp@1073
   207
  | tag_brls k (brl::brls) =
lcp@1073
   208
      (1000000*subgoals_of_brl brl + k, brl) :: 
lcp@1073
   209
      tag_brls (k+1) brls;
lcp@1073
   210
lcp@1073
   211
fun insert_tagged_list kbrls np = foldr insert_tagged_brl (kbrls, np);
lcp@1073
   212
lcp@1073
   213
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   214
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   215
  new insertions the lowest priority.*)
lcp@1073
   216
fun insert (nI,nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
lcp@1073
   217
lcp@1073
   218
lcp@1073
   219
(** Safe rules **)
lcp@982
   220
lcp@1073
   221
fun (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
lcp@1073
   222
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) 
lcp@1073
   223
    addSIs  ths  =
lcp@1073
   224
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
lcp@1073
   225
          take_prefix (fn rl => nprems_of rl=0) ths
lcp@1073
   226
      val nI = length safeIs + length ths
lcp@1073
   227
      and nE = length safeEs
lcp@1073
   228
  in CS{safeIs	= ths@safeIs,
lcp@1073
   229
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   230
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   231
	safeEs	= safeEs,
lcp@1073
   232
	hazIs	= hazIs,
lcp@1073
   233
	hazEs	= hazEs,
lcp@1073
   234
	wrapper = wrapper,
lcp@1073
   235
	haz_netpair = haz_netpair,
lcp@1073
   236
	dup_netpair = dup_netpair}
lcp@1073
   237
  end;
lcp@1073
   238
lcp@1073
   239
fun (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
lcp@1073
   240
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) 
lcp@1073
   241
    addSEs  ths  =
lcp@1073
   242
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
lcp@1073
   243
          take_prefix (fn rl => nprems_of rl=1) ths
lcp@1073
   244
      val nI = length safeIs
lcp@1073
   245
      and nE = length safeEs + length ths
lcp@1073
   246
  in 
lcp@1073
   247
     CS{safeEs	= ths@safeEs,
lcp@1073
   248
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   249
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   250
	safeIs	= safeIs,
lcp@1073
   251
	hazIs	= hazIs,
lcp@1073
   252
	hazEs	= hazEs,
lcp@1073
   253
	wrapper = wrapper,
lcp@1073
   254
	haz_netpair = haz_netpair,
lcp@1073
   255
	dup_netpair = dup_netpair}
lcp@1073
   256
  end;
clasohm@0
   257
clasohm@0
   258
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   259
lcp@1073
   260
lcp@1073
   261
(** Hazardous (unsafe) rules **)
clasohm@0
   262
lcp@1073
   263
fun (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
lcp@1073
   264
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) 
lcp@1073
   265
    addIs  ths  =
lcp@1073
   266
  let val nI = length hazIs + length ths
lcp@1073
   267
      and nE = length hazEs
lcp@1073
   268
  in 
lcp@1073
   269
     CS{hazIs	= ths@hazIs,
lcp@1073
   270
	haz_netpair = insert (nI,nE) (ths, []) haz_netpair,
lcp@1073
   271
	dup_netpair = insert (nI,nE) (map dup_intr ths, []) dup_netpair,
lcp@1073
   272
	safeIs 	= safeIs, 
lcp@1073
   273
	safeEs	= safeEs,
lcp@1073
   274
	hazEs	= hazEs,
lcp@1073
   275
	wrapper 	= wrapper,
lcp@1073
   276
	safe0_netpair = safe0_netpair,
lcp@1073
   277
	safep_netpair = safep_netpair}
lcp@1073
   278
  end;
lcp@1073
   279
lcp@1073
   280
fun (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
lcp@1073
   281
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) 
lcp@1073
   282
    addEs  ths  =
lcp@1073
   283
  let val nI = length hazIs 
lcp@1073
   284
      and nE = length hazEs + length ths
lcp@1073
   285
  in 
lcp@1073
   286
     CS{hazEs	= ths@hazEs,
lcp@1073
   287
	haz_netpair = insert (nI,nE) ([], ths) haz_netpair,
lcp@1073
   288
	dup_netpair = insert (nI,nE) ([], map dup_elim ths) dup_netpair,
lcp@1073
   289
	safeIs	= safeIs, 
lcp@1073
   290
	safeEs	= safeEs,
lcp@1073
   291
	hazIs	= hazIs,
lcp@1073
   292
	wrapper	= wrapper,
lcp@1073
   293
	safe0_netpair = safe0_netpair,
lcp@1073
   294
	safep_netpair = safep_netpair}
lcp@1073
   295
  end;
clasohm@0
   296
clasohm@0
   297
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   298
lcp@1073
   299
lcp@982
   300
(** Setting or modifying the wrapper tactical **)
lcp@982
   301
lcp@982
   302
(*Set a new wrapper*)
lcp@1073
   303
fun (CS{safeIs, safeEs, hazIs, hazEs, 
lcp@1073
   304
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
lcp@1073
   305
    setwrapper new_wrapper  =
lcp@1073
   306
  CS{wrapper 	= new_wrapper,
lcp@1073
   307
     safeIs	= safeIs,
lcp@1073
   308
     safeEs	= safeEs,
lcp@1073
   309
     hazIs	= hazIs,
lcp@1073
   310
     hazEs	= hazEs,
lcp@1073
   311
     safe0_netpair = safe0_netpair,
lcp@1073
   312
     safep_netpair = safep_netpair,
lcp@1073
   313
     haz_netpair = haz_netpair,
lcp@1073
   314
     dup_netpair = dup_netpair};
lcp@982
   315
lcp@982
   316
(*Compose a tactical with the existing wrapper*)
lcp@982
   317
fun cs compwrapper wrapper' = cs setwrapper (wrapper' o getwrapper cs);
lcp@982
   318
lcp@982
   319
(*Execute tac1, but only execute tac2 if there are at least as many subgoals
lcp@982
   320
  as before.  This ensures that tac2 is only applied to an outcome of tac1.*)
lcp@982
   321
fun tac1 THEN_MAYBE tac2 = 
lcp@982
   322
  STATE (fn state =>
lcp@982
   323
	 tac1  THEN  
lcp@982
   324
	 COND (has_fewer_prems (nprems_of state)) all_tac tac2);
lcp@982
   325
lcp@982
   326
(*Cause a tactic to be executed before/after the step tactic*)
lcp@982
   327
fun cs addbefore tac2 = cs compwrapper (fn tac1 => tac2 THEN_MAYBE tac1);
lcp@982
   328
fun cs addafter tac2  = cs compwrapper (fn tac1 => tac1 THEN_MAYBE tac2);
lcp@982
   329
lcp@982
   330
paulson@1711
   331
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   332
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   333
  treatment of priority might get muddled up.*)
paulson@1711
   334
fun merge_cs
paulson@1711
   335
    (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, ...},
paulson@1711
   336
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,...}) =
paulson@1711
   337
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   338
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
paulson@1711
   339
      val hazIs' = gen_rems eq_thm (hazIs2,hazIs)
paulson@1711
   340
      val hazEs' = gen_rems eq_thm (hazEs2,hazEs)
paulson@1711
   341
  in cs addSIs safeIs'
paulson@1711
   342
        addSEs safeEs'
paulson@1711
   343
        addIs  hazIs'
paulson@1711
   344
        addEs  hazEs'
paulson@1711
   345
  end;
paulson@1711
   346
lcp@982
   347
clasohm@0
   348
(*** Simple tactics for theorem proving ***)
clasohm@0
   349
clasohm@0
   350
(*Attack subgoals using safe inferences -- matching, not resolution*)
clasohm@0
   351
fun safe_step_tac (CS{safe0_netpair,safep_netpair,...}) = 
clasohm@0
   352
  FIRST' [eq_assume_tac,
clasohm@0
   353
	  eq_mp_tac,
clasohm@0
   354
	  bimatch_from_nets_tac safe0_netpair,
clasohm@0
   355
	  FIRST' hyp_subst_tacs,
clasohm@0
   356
	  bimatch_from_nets_tac safep_netpair] ;
clasohm@0
   357
clasohm@0
   358
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
lcp@747
   359
fun safe_tac cs = REPEAT_DETERM_FIRST (safe_step_tac cs);
lcp@747
   360
lcp@747
   361
(*But these unsafe steps at least solve a subgoal!*)
lcp@747
   362
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   363
  assume_tac 			  APPEND' 
lcp@747
   364
  contr_tac 			  APPEND' 
lcp@747
   365
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   366
lcp@747
   367
(*These are much worse since they could generate more and more subgoals*)
lcp@747
   368
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   369
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   370
clasohm@0
   371
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   372
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   373
lcp@982
   374
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   375
  biresolve_from_nets_tac haz_netpair;
lcp@681
   376
clasohm@0
   377
(*Single step for the prover.  FAILS unless it makes progress. *)
lcp@681
   378
fun step_tac cs i = 
lcp@982
   379
  getwrapper cs 
lcp@982
   380
    (FIRST [safe_tac cs, inst_step_tac cs i, haz_step_tac cs i]);
clasohm@0
   381
clasohm@0
   382
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   383
  allows backtracking from "safe" rules to "unsafe" rules here.*)
lcp@681
   384
fun slow_step_tac cs i = 
lcp@982
   385
  getwrapper cs 
lcp@982
   386
    (safe_tac cs ORELSE (inst_step_tac cs i APPEND haz_step_tac cs i));
clasohm@0
   387
clasohm@0
   388
(*** The following tactics all fail unless they solve one goal ***)
clasohm@0
   389
clasohm@0
   390
(*Dumb but fast*)
clasohm@0
   391
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   392
clasohm@0
   393
(*Slower but smarter than fast_tac*)
clasohm@0
   394
fun best_tac cs = 
clasohm@0
   395
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   396
clasohm@0
   397
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   398
clasohm@0
   399
fun slow_best_tac cs = 
clasohm@0
   400
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   401
lcp@681
   402
paulson@1587
   403
(**ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   404
val weight_ASTAR = ref 5; 
paulson@1587
   405
paulson@1587
   406
fun astar_tac cs = 
paulson@1587
   407
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   408
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   409
	      (step_tac cs 1));
paulson@1587
   410
paulson@1587
   411
fun slow_astar_tac cs = 
paulson@1587
   412
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   413
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   414
	      (slow_step_tac cs 1));
paulson@1587
   415
lcp@982
   416
(*** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   417
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   418
  easy theorems faster, but loses completeness -- and many of the harder
lcp@747
   419
  theorems such as 43. ***)
lcp@681
   420
lcp@747
   421
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   422
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   423
  greater search depth required.*)
lcp@681
   424
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   425
  biresolve_from_nets_tac dup_netpair;
lcp@681
   426
lcp@747
   427
(*Searching to depth m.*)
lcp@747
   428
fun depth_tac cs m i = STATE(fn state => 
lcp@747
   429
  SELECT_GOAL 
lcp@747
   430
    (REPEAT_DETERM1 (safe_step_tac cs 1) THEN_ELSE
lcp@747
   431
     (DEPTH_SOLVE (depth_tac cs m 1),
lcp@747
   432
      inst0_step_tac cs 1  APPEND
lcp@747
   433
      COND (K(m=0)) no_tac
lcp@747
   434
        ((instp_step_tac cs 1 APPEND dup_step_tac cs 1)
lcp@747
   435
	 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))))
lcp@747
   436
  i);
lcp@747
   437
lcp@747
   438
(*Iterative deepening tactical.  Allows us to "deepen" any search tactic*)
lcp@747
   439
fun DEEPEN tacf m i = STATE(fn state => 
lcp@747
   440
   if has_fewer_prems i state then no_tac
lcp@747
   441
   else (writeln ("Depth = " ^ string_of_int m);
lcp@747
   442
	 tacf m i  ORELSE  DEEPEN tacf (m+2) i));
lcp@747
   443
lcp@747
   444
fun safe_depth_tac cs m = 
lcp@681
   445
  SUBGOAL 
lcp@681
   446
    (fn (prem,i) =>
lcp@681
   447
      let val deti =
lcp@681
   448
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   449
	  case term_vars prem of
lcp@681
   450
	      []	=> DETERM 
lcp@681
   451
	    | _::_	=> I
lcp@681
   452
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   453
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   454
      end);
lcp@681
   455
lcp@747
   456
fun deepen_tac cs = DEEPEN (safe_depth_tac cs);
lcp@681
   457
berghofe@1724
   458
val claset = ref empty_cs;
berghofe@1724
   459
berghofe@1724
   460
fun AddDs ts = (claset := !claset addDs ts);
berghofe@1724
   461
berghofe@1724
   462
fun AddEs ts = (claset := !claset addEs ts);
berghofe@1724
   463
berghofe@1724
   464
fun AddIs ts = (claset := !claset addIs ts);
berghofe@1724
   465
berghofe@1724
   466
fun AddSDs ts = (claset := !claset addSDs ts);
berghofe@1724
   467
berghofe@1724
   468
fun AddSEs ts = (claset := !claset addSEs ts);
berghofe@1724
   469
berghofe@1724
   470
fun AddSIs ts = (claset := !claset addSIs ts);
berghofe@1724
   471
berghofe@1724
   472
fun Fast_tac i = fast_tac (!claset) i; 
berghofe@1724
   473
clasohm@0
   474
end; 
clasohm@0
   475
end;