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