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