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