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