src/Provers/classical.ML
author paulson
Tue Oct 08 10:18:18 1996 +0200 (1996-10-08)
changeset 2066 b9063086ef56
parent 1938 4e29ea45520d
child 2173 08c68550460b
permissions -rw-r--r--
Introduction of Slow_tac and Slow_best_tac
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*)
paulson@1800
    29
infix 4 addSIs addSEs addSDs addIs addEs addDs delrules
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
paulson@1800
    45
  val delrules		: claset * thm list -> claset
lcp@982
    46
  val setwrapper 	: claset * (tactic->tactic) -> claset
lcp@982
    47
  val compwrapper 	: claset * (tactic->tactic) -> claset
lcp@982
    48
  val addbefore 	: claset * tactic -> claset
lcp@982
    49
  val addafter 		: claset * tactic -> claset
lcp@982
    50
lcp@681
    51
  val print_cs		: claset -> unit
lcp@1073
    52
  val rep_claset	: 
lcp@1073
    53
      claset -> {safeIs: thm list, safeEs: thm list, 
lcp@1073
    54
		 hazIs: thm list, hazEs: thm list,
lcp@1073
    55
		 wrapper: tactic -> tactic,
lcp@1073
    56
		 safe0_netpair: netpair, safep_netpair: netpair,
lcp@1073
    57
		 haz_netpair: netpair, dup_netpair: netpair}
lcp@982
    58
  val getwrapper	: claset -> tactic -> tactic
lcp@982
    59
  val THEN_MAYBE	: tactic * tactic -> tactic
lcp@982
    60
paulson@1587
    61
  val fast_tac 		: claset -> int -> tactic
paulson@1587
    62
  val slow_tac 		: claset -> int -> tactic
paulson@1587
    63
  val weight_ASTAR	: int ref
paulson@1587
    64
  val astar_tac		: claset -> int -> tactic
paulson@1587
    65
  val slow_astar_tac 	: claset -> int -> tactic
lcp@681
    66
  val best_tac 		: claset -> int -> tactic
paulson@1587
    67
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
    68
  val depth_tac		: claset -> int -> int -> tactic
paulson@1938
    69
  val DEEPEN  	        : (int -> int -> tactic) -> int -> int -> tactic
lcp@681
    70
  val deepen_tac	: claset -> int -> int -> tactic
paulson@1587
    71
paulson@1587
    72
  val contr_tac 	: int -> tactic
lcp@681
    73
  val dup_elim		: thm -> thm
lcp@681
    74
  val dup_intr		: thm -> thm
lcp@681
    75
  val dup_step_tac	: claset -> int -> tactic
lcp@681
    76
  val eq_mp_tac		: int -> tactic
lcp@681
    77
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
    78
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
    79
  val mp_tac		: int -> tactic
lcp@681
    80
  val safe_tac 		: claset -> tactic
lcp@681
    81
  val safe_step_tac 	: claset -> int -> tactic
lcp@681
    82
  val step_tac 		: claset -> int -> tactic
lcp@681
    83
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
    84
  val swapify 		: thm list -> thm list
lcp@681
    85
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
    86
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
    87
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
    88
  val instp_step_tac 	: claset -> int -> tactic
berghofe@1724
    89
berghofe@1724
    90
  val claset : claset ref
berghofe@1724
    91
  val AddDs 		: thm list -> unit
berghofe@1724
    92
  val AddEs 		: thm list -> unit
berghofe@1724
    93
  val AddIs 		: thm list -> unit
berghofe@1724
    94
  val AddSDs		: thm list -> unit
berghofe@1724
    95
  val AddSEs		: thm list -> unit
berghofe@1724
    96
  val AddSIs		: thm list -> unit
paulson@1807
    97
  val Delrules		: thm list -> unit
paulson@1814
    98
  val Safe_step_tac	: int -> tactic
paulson@1800
    99
  val Step_tac 		: int -> tactic
berghofe@1724
   100
  val Fast_tac 		: int -> tactic
paulson@1800
   101
  val Best_tac 		: int -> tactic
paulson@2066
   102
  val Slow_tac 		: int -> tactic
paulson@2066
   103
  val Slow_best_tac     : int -> tactic
paulson@1800
   104
  val Deepen_tac	: int -> int -> tactic
berghofe@1724
   105
clasohm@0
   106
  end;
clasohm@0
   107
clasohm@0
   108
clasohm@0
   109
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
   110
struct
clasohm@0
   111
clasohm@0
   112
local open Data in
clasohm@0
   113
paulson@1800
   114
(*** Useful tactics for classical reasoning ***)
clasohm@0
   115
paulson@1524
   116
val imp_elim = (*cannot use bind_thm within a structure!*)
paulson@1524
   117
  store_thm ("imp_elim", make_elim mp);
clasohm@0
   118
clasohm@0
   119
(*Solve goal that assumes both P and ~P. *)
clasohm@0
   120
val contr_tac = eresolve_tac [not_elim]  THEN'  assume_tac;
clasohm@0
   121
lcp@681
   122
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   123
  Could do the same thing for P<->Q and P... *)
lcp@681
   124
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
   125
clasohm@0
   126
(*Like mp_tac but instantiates no variables*)
lcp@681
   127
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
   128
paulson@1524
   129
val swap =
paulson@1524
   130
  store_thm ("swap", rule_by_tactic (etac thin_rl 1) (not_elim RS classical));
clasohm@0
   131
clasohm@0
   132
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
   133
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
   134
clasohm@0
   135
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   136
  trying rules in order. *)
clasohm@0
   137
fun swap_res_tac rls = 
lcp@54
   138
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
   139
    in  assume_tac 	ORELSE' 
lcp@54
   140
	contr_tac 	ORELSE' 
lcp@54
   141
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   142
    end;
clasohm@0
   143
lcp@681
   144
(*Duplication of hazardous rules, for complete provers*)
lcp@681
   145
fun dup_intr th = standard (th RS classical);
lcp@681
   146
lcp@681
   147
fun dup_elim th = th RSN (2, revcut_rl) |> assumption 2 |> Sequence.hd |> 
lcp@681
   148
                  rule_by_tactic (TRYALL (etac revcut_rl));
clasohm@0
   149
lcp@1073
   150
paulson@1800
   151
(**** Classical rule sets ****)
clasohm@0
   152
clasohm@0
   153
type netpair = (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net;
clasohm@0
   154
clasohm@0
   155
datatype claset =
lcp@982
   156
  CS of {safeIs		: thm list,		(*safe introduction rules*)
lcp@982
   157
	 safeEs		: thm list,		(*safe elimination rules*)
lcp@982
   158
	 hazIs		: thm list,		(*unsafe introduction rules*)
lcp@982
   159
	 hazEs		: thm list,		(*unsafe elimination rules*)
lcp@982
   160
	 wrapper	: tactic->tactic,	(*for transforming step_tac*)
lcp@982
   161
	 safe0_netpair	: netpair,		(*nets for trivial cases*)
lcp@982
   162
	 safep_netpair	: netpair,		(*nets for >0 subgoals*)
lcp@982
   163
	 haz_netpair  	: netpair,		(*nets for unsafe rules*)
lcp@982
   164
	 dup_netpair	: netpair};		(*nets for duplication*)
clasohm@0
   165
lcp@1073
   166
(*Desired invariants are
lcp@681
   167
	safe0_netpair = build safe0_brls,
lcp@681
   168
	safep_netpair = build safep_brls,
lcp@681
   169
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   170
	dup_netpair = build (joinrules(map dup_intr hazIs, 
lcp@681
   171
				       map dup_elim hazEs))}
lcp@1073
   172
lcp@1073
   173
where build = build_netpair(Net.empty,Net.empty), 
lcp@1073
   174
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   175
      safep_brls contains all brules that generate 1 or more new subgoals.
paulson@1800
   176
The theorem lists are largely comments, though they are used in merge_cs.
lcp@1073
   177
Nets must be built incrementally, to save space and time.
lcp@1073
   178
*)
clasohm@0
   179
lcp@1073
   180
val empty_cs = 
lcp@1073
   181
  CS{safeIs	= [],
lcp@1073
   182
     safeEs	= [],
lcp@1073
   183
     hazIs	= [],
lcp@1073
   184
     hazEs	= [],
lcp@1073
   185
     wrapper 	= I,
lcp@1073
   186
     safe0_netpair = (Net.empty,Net.empty),
lcp@1073
   187
     safep_netpair = (Net.empty,Net.empty),
lcp@1073
   188
     haz_netpair   = (Net.empty,Net.empty),
lcp@1073
   189
     dup_netpair   = (Net.empty,Net.empty)};
clasohm@0
   190
clasohm@0
   191
fun print_cs (CS{safeIs,safeEs,hazIs,hazEs,...}) =
lcp@1073
   192
 (writeln"Introduction rules";  	prths hazIs;
lcp@1073
   193
  writeln"Safe introduction rules";  	prths safeIs;
lcp@1073
   194
  writeln"Elimination rules";  		prths hazEs;
lcp@1073
   195
  writeln"Safe elimination rules";  	prths safeEs;
clasohm@0
   196
  ());
clasohm@0
   197
lcp@1073
   198
fun rep_claset (CS args) = args;
lcp@1073
   199
lcp@1073
   200
fun getwrapper (CS{wrapper,...}) = wrapper;
lcp@1073
   201
lcp@1073
   202
paulson@1800
   203
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   204
lcp@1073
   205
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   206
***)
clasohm@0
   207
lcp@1073
   208
(*For use with biresolve_tac.  Combines intr rules with swap to handle negated
lcp@1073
   209
  assumptions.  Pairs elim rules with true. *)
lcp@1073
   210
fun joinrules (intrs,elims) =  
lcp@1073
   211
    (map (pair true) (elims @ swapify intrs)  @
lcp@1073
   212
     map (pair false) intrs);
lcp@1073
   213
lcp@1073
   214
(*Priority: prefer rules with fewest subgoals, 
paulson@1231
   215
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   216
fun tag_brls k [] = []
lcp@1073
   217
  | tag_brls k (brl::brls) =
lcp@1073
   218
      (1000000*subgoals_of_brl brl + k, brl) :: 
lcp@1073
   219
      tag_brls (k+1) brls;
lcp@1073
   220
paulson@1800
   221
fun insert_tagged_list kbrls netpr = foldr insert_tagged_brl (kbrls, netpr);
lcp@1073
   222
lcp@1073
   223
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   224
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   225
  new insertions the lowest priority.*)
lcp@1073
   226
fun insert (nI,nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
lcp@1073
   227
paulson@1800
   228
fun delete_tagged_list brls netpr = foldr delete_tagged_brl (brls, netpr);
lcp@1073
   229
paulson@1800
   230
val delete = delete_tagged_list o joinrules;
paulson@1800
   231
paulson@1927
   232
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   233
  is still allowed.*)
paulson@1927
   234
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) = 
paulson@1927
   235
       if gen_mem eq_thm (th, safeIs) then 
paulson@1927
   236
	 warning ("rule already in claset as Safe Intr\n" ^ string_of_thm th)
paulson@1927
   237
  else if gen_mem eq_thm (th, safeEs) then
paulson@1927
   238
         warning ("rule already in claset as Safe Elim\n" ^ string_of_thm th)
paulson@1927
   239
  else if gen_mem eq_thm (th, hazIs) then 
paulson@1927
   240
         warning ("rule already in claset as unsafe Intr\n" ^ string_of_thm th)
paulson@1927
   241
  else if gen_mem eq_thm (th, hazEs) then 
paulson@1927
   242
         warning ("rule already in claset as unsafe Elim\n" ^ string_of_thm th)
paulson@1927
   243
  else ();
paulson@1927
   244
paulson@1800
   245
(*** Safe rules ***)
lcp@982
   246
paulson@1927
   247
fun addSI (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1927
   248
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   249
	   th)  =
paulson@1927
   250
  if gen_mem eq_thm (th, safeIs) then 
paulson@1927
   251
	 (warning ("ignoring duplicate Safe Intr\n" ^ string_of_thm th);
paulson@1927
   252
	  cs)
paulson@1927
   253
  else
lcp@1073
   254
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   255
          partition (fn rl => nprems_of rl=0) [th]
paulson@1927
   256
      val nI = length safeIs + 1
lcp@1073
   257
      and nE = length safeEs
paulson@1927
   258
  in warn_dup th cs;
paulson@1927
   259
     CS{safeIs	= th::safeIs,
lcp@1073
   260
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   261
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   262
	safeEs	= safeEs,
lcp@1073
   263
	hazIs	= hazIs,
lcp@1073
   264
	hazEs	= hazEs,
lcp@1073
   265
	wrapper = wrapper,
lcp@1073
   266
	haz_netpair = haz_netpair,
lcp@1073
   267
	dup_netpair = dup_netpair}
lcp@1073
   268
  end;
lcp@1073
   269
paulson@1927
   270
fun addSE (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1927
   271
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   272
	   th)  =
paulson@1927
   273
  if gen_mem eq_thm (th, safeEs) then 
paulson@1927
   274
	 (warning ("ignoring duplicate Safe Elim\n" ^ string_of_thm th);
paulson@1927
   275
	  cs)
paulson@1927
   276
  else
lcp@1073
   277
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   278
          partition (fn rl => nprems_of rl=1) [th]
lcp@1073
   279
      val nI = length safeIs
paulson@1927
   280
      and nE = length safeEs + 1
paulson@1927
   281
  in warn_dup th cs;
paulson@1927
   282
     CS{safeEs	= th::safeEs,
lcp@1073
   283
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   284
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   285
	safeIs	= safeIs,
lcp@1073
   286
	hazIs	= hazIs,
lcp@1073
   287
	hazEs	= hazEs,
lcp@1073
   288
	wrapper = wrapper,
lcp@1073
   289
	haz_netpair = haz_netpair,
lcp@1073
   290
	dup_netpair = dup_netpair}
lcp@1073
   291
  end;
clasohm@0
   292
paulson@1927
   293
fun rev_foldl f (e, l) = foldl f (e, rev l);
paulson@1927
   294
paulson@1927
   295
val op addSIs = rev_foldl addSI;
paulson@1927
   296
val op addSEs = rev_foldl addSE;
paulson@1927
   297
clasohm@0
   298
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   299
lcp@1073
   300
paulson@1800
   301
(*** Hazardous (unsafe) rules ***)
clasohm@0
   302
paulson@1927
   303
fun addI (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1927
   304
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   305
	  th)=
paulson@1927
   306
  if gen_mem eq_thm (th, hazIs) then 
paulson@1927
   307
	 (warning ("ignoring duplicate unsafe Intr\n" ^ string_of_thm th);
paulson@1927
   308
	  cs)
paulson@1927
   309
  else
paulson@1927
   310
  let val nI = length hazIs + 1
lcp@1073
   311
      and nE = length hazEs
paulson@1927
   312
  in warn_dup th cs;
paulson@1927
   313
     CS{hazIs	= th::hazIs,
paulson@1927
   314
	haz_netpair = insert (nI,nE) ([th], []) haz_netpair,
paulson@1927
   315
	dup_netpair = insert (nI,nE) (map dup_intr [th], []) dup_netpair,
lcp@1073
   316
	safeIs 	= safeIs, 
lcp@1073
   317
	safeEs	= safeEs,
lcp@1073
   318
	hazEs	= hazEs,
lcp@1073
   319
	wrapper 	= wrapper,
lcp@1073
   320
	safe0_netpair = safe0_netpair,
lcp@1073
   321
	safep_netpair = safep_netpair}
lcp@1073
   322
  end;
lcp@1073
   323
paulson@1927
   324
fun addE (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1927
   325
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   326
	  th) =
paulson@1927
   327
  if gen_mem eq_thm (th, hazEs) then 
paulson@1927
   328
	 (warning ("ignoring duplicate unsafe Elim\n" ^ string_of_thm th);
paulson@1927
   329
	  cs)
paulson@1927
   330
  else
lcp@1073
   331
  let val nI = length hazIs 
paulson@1927
   332
      and nE = length hazEs + 1
paulson@1927
   333
  in warn_dup th cs;
paulson@1927
   334
     CS{hazEs	= th::hazEs,
paulson@1927
   335
	haz_netpair = insert (nI,nE) ([], [th]) haz_netpair,
paulson@1927
   336
	dup_netpair = insert (nI,nE) ([], map dup_elim [th]) dup_netpair,
lcp@1073
   337
	safeIs	= safeIs, 
lcp@1073
   338
	safeEs	= safeEs,
lcp@1073
   339
	hazIs	= hazIs,
lcp@1073
   340
	wrapper	= wrapper,
lcp@1073
   341
	safe0_netpair = safe0_netpair,
lcp@1073
   342
	safep_netpair = safep_netpair}
lcp@1073
   343
  end;
clasohm@0
   344
paulson@1927
   345
val op addIs = rev_foldl addI;
paulson@1927
   346
val op addEs = rev_foldl addE;
paulson@1927
   347
clasohm@0
   348
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   349
lcp@1073
   350
paulson@1800
   351
(*** Deletion of rules 
paulson@1800
   352
     Working out what to delete, requires repeating much of the code used
paulson@1800
   353
	to insert.
paulson@1927
   354
     Separate functions delSI, etc., are not exported; instead delrules
paulson@1800
   355
        searches in all the lists and chooses the relevant delXX function.
paulson@1800
   356
***)
paulson@1800
   357
paulson@1927
   358
fun delSI (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1800
   359
               safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   360
            th) =
paulson@1800
   361
  let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=0) [th]
paulson@1800
   362
  in CS{safeIs	= gen_rem eq_thm (safeIs,th),
paulson@1800
   363
        safe0_netpair = delete (safe0_rls, []) safe0_netpair,
paulson@1800
   364
	safep_netpair = delete (safep_rls, []) safep_netpair,
paulson@1800
   365
	safeEs	= safeEs,
paulson@1800
   366
	hazIs	= hazIs,
paulson@1800
   367
	hazEs	= hazEs,
paulson@1800
   368
	wrapper = wrapper,
paulson@1800
   369
	haz_netpair = haz_netpair,
paulson@1800
   370
	dup_netpair = dup_netpair}
paulson@1800
   371
  end;
paulson@1800
   372
paulson@1927
   373
fun delSE (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1800
   374
	       safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   375
            th) =
paulson@1800
   376
  let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=1) [th]
paulson@1800
   377
  in CS{safeEs	= gen_rem eq_thm (safeEs,th),
paulson@1800
   378
        safe0_netpair = delete ([], safe0_rls) safe0_netpair,
paulson@1800
   379
	safep_netpair = delete ([], safep_rls) safep_netpair,
paulson@1800
   380
	safeIs	= safeIs,
paulson@1800
   381
	hazIs	= hazIs,
paulson@1800
   382
	hazEs	= hazEs,
paulson@1800
   383
	wrapper = wrapper,
paulson@1800
   384
	haz_netpair = haz_netpair,
paulson@1800
   385
	dup_netpair = dup_netpair}
paulson@1800
   386
  end;
paulson@1800
   387
paulson@1800
   388
paulson@1927
   389
fun delI (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1800
   390
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   391
	   th) =
paulson@1800
   392
     CS{hazIs	= gen_rem eq_thm (hazIs,th),
paulson@1800
   393
	haz_netpair = delete ([th], []) haz_netpair,
paulson@1800
   394
	dup_netpair = delete ([dup_intr th], []) dup_netpair,
paulson@1800
   395
	safeIs 	= safeIs, 
paulson@1800
   396
	safeEs	= safeEs,
paulson@1800
   397
	hazEs	= hazEs,
paulson@1800
   398
	wrapper 	= wrapper,
paulson@1800
   399
	safe0_netpair = safe0_netpair,
paulson@1800
   400
	safep_netpair = safep_netpair};
paulson@1800
   401
paulson@1927
   402
fun delE (CS{safeIs, safeEs, hazIs, hazEs, wrapper, 
paulson@1800
   403
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   404
	   th) =
paulson@1800
   405
     CS{hazEs	= gen_rem eq_thm (hazEs,th),
paulson@1800
   406
	haz_netpair = delete ([], [th]) haz_netpair,
paulson@1800
   407
	dup_netpair = delete ([], [dup_elim th]) dup_netpair,
paulson@1800
   408
	safeIs	= safeIs, 
paulson@1800
   409
	safeEs	= safeEs,
paulson@1800
   410
	hazIs	= hazIs,
paulson@1800
   411
	wrapper	= wrapper,
paulson@1800
   412
	safe0_netpair = safe0_netpair,
paulson@1800
   413
	safep_netpair = safep_netpair};
paulson@1800
   414
paulson@1800
   415
fun delrule (cs as CS{safeIs, safeEs, hazIs, hazEs, ...}, th) =
paulson@1927
   416
       if gen_mem eq_thm (th, safeIs) then delSI(cs,th)
paulson@1927
   417
  else if gen_mem eq_thm (th, safeEs) then delSE(cs,th)
paulson@1927
   418
  else if gen_mem eq_thm (th, hazIs) then delI(cs,th)
paulson@1927
   419
  else if gen_mem eq_thm (th, hazEs) then delE(cs,th)
paulson@1800
   420
  else (warning ("rule not in claset\n" ^ (string_of_thm th)); 
paulson@1800
   421
	cs);
paulson@1800
   422
paulson@1800
   423
val op delrules = foldl delrule;
paulson@1800
   424
paulson@1800
   425
paulson@1800
   426
(*** Setting or modifying the wrapper tactical ***)
lcp@982
   427
lcp@982
   428
(*Set a new wrapper*)
lcp@1073
   429
fun (CS{safeIs, safeEs, hazIs, hazEs, 
lcp@1073
   430
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
lcp@1073
   431
    setwrapper new_wrapper  =
lcp@1073
   432
  CS{wrapper 	= new_wrapper,
lcp@1073
   433
     safeIs	= safeIs,
lcp@1073
   434
     safeEs	= safeEs,
lcp@1073
   435
     hazIs	= hazIs,
lcp@1073
   436
     hazEs	= hazEs,
lcp@1073
   437
     safe0_netpair = safe0_netpair,
lcp@1073
   438
     safep_netpair = safep_netpair,
lcp@1073
   439
     haz_netpair = haz_netpair,
lcp@1073
   440
     dup_netpair = dup_netpair};
lcp@982
   441
lcp@982
   442
(*Compose a tactical with the existing wrapper*)
lcp@982
   443
fun cs compwrapper wrapper' = cs setwrapper (wrapper' o getwrapper cs);
lcp@982
   444
lcp@982
   445
(*Execute tac1, but only execute tac2 if there are at least as many subgoals
lcp@982
   446
  as before.  This ensures that tac2 is only applied to an outcome of tac1.*)
lcp@982
   447
fun tac1 THEN_MAYBE tac2 = 
lcp@982
   448
  STATE (fn state =>
lcp@982
   449
	 tac1  THEN  
lcp@982
   450
	 COND (has_fewer_prems (nprems_of state)) all_tac tac2);
lcp@982
   451
lcp@982
   452
(*Cause a tactic to be executed before/after the step tactic*)
lcp@982
   453
fun cs addbefore tac2 = cs compwrapper (fn tac1 => tac2 THEN_MAYBE tac1);
lcp@982
   454
fun cs addafter tac2  = cs compwrapper (fn tac1 => tac1 THEN_MAYBE tac2);
lcp@982
   455
lcp@982
   456
paulson@1711
   457
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   458
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   459
  treatment of priority might get muddled up.*)
paulson@1711
   460
fun merge_cs
paulson@1711
   461
    (cs as CS{safeIs, safeEs, hazIs, hazEs, wrapper, ...},
paulson@1711
   462
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,...}) =
paulson@1711
   463
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   464
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
paulson@1711
   465
      val hazIs' = gen_rems eq_thm (hazIs2,hazIs)
paulson@1711
   466
      val hazEs' = gen_rems eq_thm (hazEs2,hazEs)
paulson@1711
   467
  in cs addSIs safeIs'
paulson@1711
   468
        addSEs safeEs'
paulson@1711
   469
        addIs  hazIs'
paulson@1711
   470
        addEs  hazEs'
paulson@1711
   471
  end;
paulson@1711
   472
lcp@982
   473
paulson@1800
   474
(**** Simple tactics for theorem proving ****)
clasohm@0
   475
clasohm@0
   476
(*Attack subgoals using safe inferences -- matching, not resolution*)
clasohm@0
   477
fun safe_step_tac (CS{safe0_netpair,safep_netpair,...}) = 
clasohm@0
   478
  FIRST' [eq_assume_tac,
clasohm@0
   479
	  eq_mp_tac,
clasohm@0
   480
	  bimatch_from_nets_tac safe0_netpair,
clasohm@0
   481
	  FIRST' hyp_subst_tacs,
clasohm@0
   482
	  bimatch_from_nets_tac safep_netpair] ;
clasohm@0
   483
clasohm@0
   484
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
lcp@747
   485
fun safe_tac cs = REPEAT_DETERM_FIRST (safe_step_tac cs);
lcp@747
   486
lcp@747
   487
(*But these unsafe steps at least solve a subgoal!*)
lcp@747
   488
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   489
  assume_tac 			  APPEND' 
lcp@747
   490
  contr_tac 			  APPEND' 
lcp@747
   491
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   492
lcp@747
   493
(*These are much worse since they could generate more and more subgoals*)
lcp@747
   494
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   495
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   496
clasohm@0
   497
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   498
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   499
lcp@982
   500
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   501
  biresolve_from_nets_tac haz_netpair;
lcp@681
   502
clasohm@0
   503
(*Single step for the prover.  FAILS unless it makes progress. *)
lcp@681
   504
fun step_tac cs i = 
lcp@982
   505
  getwrapper cs 
lcp@982
   506
    (FIRST [safe_tac cs, inst_step_tac cs i, haz_step_tac cs i]);
clasohm@0
   507
clasohm@0
   508
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   509
  allows backtracking from "safe" rules to "unsafe" rules here.*)
lcp@681
   510
fun slow_step_tac cs i = 
lcp@982
   511
  getwrapper cs 
lcp@982
   512
    (safe_tac cs ORELSE (inst_step_tac cs i APPEND haz_step_tac cs i));
clasohm@0
   513
paulson@1800
   514
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   515
clasohm@0
   516
(*Dumb but fast*)
clasohm@0
   517
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   518
clasohm@0
   519
(*Slower but smarter than fast_tac*)
clasohm@0
   520
fun best_tac cs = 
clasohm@0
   521
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   522
clasohm@0
   523
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   524
clasohm@0
   525
fun slow_best_tac cs = 
clasohm@0
   526
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   527
lcp@681
   528
paulson@1800
   529
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   530
val weight_ASTAR = ref 5; 
paulson@1587
   531
paulson@1587
   532
fun astar_tac cs = 
paulson@1587
   533
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   534
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   535
	      (step_tac cs 1));
paulson@1587
   536
paulson@1587
   537
fun slow_astar_tac cs = 
paulson@1587
   538
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   539
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   540
	      (slow_step_tac cs 1));
paulson@1587
   541
paulson@1800
   542
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   543
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   544
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   545
  theorems such as 43. ****)
lcp@681
   546
lcp@747
   547
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   548
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   549
  greater search depth required.*)
lcp@681
   550
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   551
  biresolve_from_nets_tac dup_netpair;
lcp@681
   552
lcp@747
   553
(*Searching to depth m.*)
lcp@747
   554
fun depth_tac cs m i = STATE(fn state => 
lcp@747
   555
  SELECT_GOAL 
paulson@1938
   556
   (getwrapper cs
lcp@747
   557
    (REPEAT_DETERM1 (safe_step_tac cs 1) THEN_ELSE
lcp@747
   558
     (DEPTH_SOLVE (depth_tac cs m 1),
lcp@747
   559
      inst0_step_tac cs 1  APPEND
lcp@747
   560
      COND (K(m=0)) no_tac
lcp@747
   561
        ((instp_step_tac cs 1 APPEND dup_step_tac cs 1)
paulson@1938
   562
	 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1)))))
lcp@747
   563
  i);
lcp@747
   564
lcp@747
   565
(*Iterative deepening tactical.  Allows us to "deepen" any search tactic*)
lcp@747
   566
fun DEEPEN tacf m i = STATE(fn state => 
lcp@747
   567
   if has_fewer_prems i state then no_tac
lcp@747
   568
   else (writeln ("Depth = " ^ string_of_int m);
lcp@747
   569
	 tacf m i  ORELSE  DEEPEN tacf (m+2) i));
lcp@747
   570
lcp@747
   571
fun safe_depth_tac cs m = 
lcp@681
   572
  SUBGOAL 
lcp@681
   573
    (fn (prem,i) =>
lcp@681
   574
      let val deti =
lcp@681
   575
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   576
	  case term_vars prem of
lcp@681
   577
	      []	=> DETERM 
lcp@681
   578
	    | _::_	=> I
lcp@681
   579
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   580
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   581
      end);
lcp@681
   582
lcp@747
   583
fun deepen_tac cs = DEEPEN (safe_depth_tac cs);
lcp@681
   584
berghofe@1724
   585
val claset = ref empty_cs;
berghofe@1724
   586
berghofe@1724
   587
fun AddDs ts = (claset := !claset addDs ts);
berghofe@1724
   588
berghofe@1724
   589
fun AddEs ts = (claset := !claset addEs ts);
berghofe@1724
   590
berghofe@1724
   591
fun AddIs ts = (claset := !claset addIs ts);
berghofe@1724
   592
berghofe@1724
   593
fun AddSDs ts = (claset := !claset addSDs ts);
berghofe@1724
   594
berghofe@1724
   595
fun AddSEs ts = (claset := !claset addSEs ts);
berghofe@1724
   596
berghofe@1724
   597
fun AddSIs ts = (claset := !claset addSIs ts);
berghofe@1724
   598
paulson@1807
   599
fun Delrules ts = (claset := !claset delrules ts);
paulson@1807
   600
paulson@1800
   601
(*Cannot have Safe_tac, as it takes no arguments; must delay dereferencing!*)
paulson@1800
   602
paulson@1814
   603
fun Safe_step_tac i = safe_step_tac (!claset) i; 
paulson@1814
   604
paulson@1800
   605
fun Step_tac i = step_tac (!claset) i; 
paulson@1800
   606
berghofe@1724
   607
fun Fast_tac i = fast_tac (!claset) i; 
berghofe@1724
   608
paulson@1800
   609
fun Best_tac i = best_tac (!claset) i; 
paulson@1800
   610
paulson@2066
   611
fun Slow_tac i = slow_tac (!claset) i; 
paulson@2066
   612
paulson@2066
   613
fun Slow_best_tac i = slow_best_tac (!claset) i; 
paulson@2066
   614
paulson@1800
   615
fun Deepen_tac m = deepen_tac (!claset) m; 
paulson@1800
   616
clasohm@0
   617
end; 
clasohm@0
   618
end;