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