src/Provers/classical.ML
author oheimb
Sat Feb 15 16:23:58 1997 +0100 (1997-02-15 ago)
changeset 2630 7a962f6829ca
parent 2173 08c68550460b
child 2689 6d3d893453de
permissions -rw-r--r--
moved THEN_MAYBE to Pure/tctical.ML
better addbefore, addafter (now: addaltern),
setwrapper (now: setWrapper) and addwrapper (now addWrapper)
added safe wrapper(and functions setSWrapper,compSWrapper,addSbefore,addSaltern)
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*)
lcp@681
   150
fun dup_intr th = standard (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@1927
   243
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   244
  is still allowed.*)
paulson@1927
   245
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) = 
paulson@1927
   246
       if gen_mem eq_thm (th, safeIs) then 
paulson@1927
   247
	 warning ("rule already in claset as Safe Intr\n" ^ string_of_thm th)
paulson@1927
   248
  else if gen_mem eq_thm (th, safeEs) then
paulson@1927
   249
         warning ("rule already in claset as Safe Elim\n" ^ string_of_thm th)
paulson@1927
   250
  else if gen_mem eq_thm (th, hazIs) then 
paulson@1927
   251
         warning ("rule already in claset as unsafe Intr\n" ^ string_of_thm th)
paulson@1927
   252
  else if gen_mem eq_thm (th, hazEs) then 
paulson@1927
   253
         warning ("rule already in claset as unsafe Elim\n" ^ string_of_thm th)
paulson@1927
   254
  else ();
paulson@1927
   255
paulson@1800
   256
(*** Safe rules ***)
lcp@982
   257
oheimb@2630
   258
fun addSI (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   259
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   260
	   th)  =
paulson@1927
   261
  if gen_mem eq_thm (th, safeIs) then 
paulson@1927
   262
	 (warning ("ignoring duplicate Safe Intr\n" ^ string_of_thm th);
paulson@1927
   263
	  cs)
paulson@1927
   264
  else
lcp@1073
   265
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   266
          partition (fn rl => nprems_of rl=0) [th]
paulson@1927
   267
      val nI = length safeIs + 1
lcp@1073
   268
      and nE = length safeEs
paulson@1927
   269
  in warn_dup th cs;
paulson@1927
   270
     CS{safeIs	= th::safeIs,
lcp@1073
   271
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   272
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   273
	safeEs	= safeEs,
lcp@1073
   274
	hazIs	= hazIs,
lcp@1073
   275
	hazEs	= hazEs,
oheimb@2630
   276
	uwrapper     = uwrapper,
oheimb@2630
   277
	swrapper     = swrapper,
oheimb@2630
   278
	haz_netpair  = haz_netpair,
oheimb@2630
   279
	dup_netpair  = dup_netpair}
lcp@1073
   280
  end;
lcp@1073
   281
oheimb@2630
   282
fun addSE (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   283
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   284
	   th)  =
paulson@1927
   285
  if gen_mem eq_thm (th, safeEs) then 
paulson@1927
   286
	 (warning ("ignoring duplicate Safe Elim\n" ^ string_of_thm th);
paulson@1927
   287
	  cs)
paulson@1927
   288
  else
lcp@1073
   289
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   290
          partition (fn rl => nprems_of rl=1) [th]
lcp@1073
   291
      val nI = length safeIs
paulson@1927
   292
      and nE = length safeEs + 1
paulson@1927
   293
  in warn_dup th cs;
paulson@1927
   294
     CS{safeEs	= th::safeEs,
lcp@1073
   295
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   296
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   297
	safeIs	= safeIs,
lcp@1073
   298
	hazIs	= hazIs,
lcp@1073
   299
	hazEs	= hazEs,
oheimb@2630
   300
	uwrapper     = uwrapper,
oheimb@2630
   301
	swrapper     = swrapper,
oheimb@2630
   302
	haz_netpair  = haz_netpair,
oheimb@2630
   303
	dup_netpair  = dup_netpair}
lcp@1073
   304
  end;
clasohm@0
   305
paulson@1927
   306
fun rev_foldl f (e, l) = foldl f (e, rev l);
paulson@1927
   307
paulson@1927
   308
val op addSIs = rev_foldl addSI;
paulson@1927
   309
val op addSEs = rev_foldl addSE;
paulson@1927
   310
clasohm@0
   311
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   312
lcp@1073
   313
paulson@1800
   314
(*** Hazardous (unsafe) rules ***)
clasohm@0
   315
oheimb@2630
   316
fun addI (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   317
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   318
	  th)=
paulson@1927
   319
  if gen_mem eq_thm (th, hazIs) then 
paulson@1927
   320
	 (warning ("ignoring duplicate unsafe Intr\n" ^ string_of_thm th);
paulson@1927
   321
	  cs)
paulson@1927
   322
  else
paulson@1927
   323
  let val nI = length hazIs + 1
lcp@1073
   324
      and nE = length hazEs
paulson@1927
   325
  in warn_dup th cs;
paulson@1927
   326
     CS{hazIs	= th::hazIs,
paulson@1927
   327
	haz_netpair = insert (nI,nE) ([th], []) haz_netpair,
paulson@1927
   328
	dup_netpair = insert (nI,nE) (map dup_intr [th], []) dup_netpair,
lcp@1073
   329
	safeIs 	= safeIs, 
lcp@1073
   330
	safeEs	= safeEs,
lcp@1073
   331
	hazEs	= hazEs,
oheimb@2630
   332
	uwrapper      = uwrapper,
oheimb@2630
   333
	swrapper      = swrapper,
lcp@1073
   334
	safe0_netpair = safe0_netpair,
lcp@1073
   335
	safep_netpair = safep_netpair}
lcp@1073
   336
  end;
lcp@1073
   337
oheimb@2630
   338
fun addE (cs as CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1927
   339
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   340
	  th) =
paulson@1927
   341
  if gen_mem eq_thm (th, hazEs) then 
paulson@1927
   342
	 (warning ("ignoring duplicate unsafe Elim\n" ^ string_of_thm th);
paulson@1927
   343
	  cs)
paulson@1927
   344
  else
lcp@1073
   345
  let val nI = length hazIs 
paulson@1927
   346
      and nE = length hazEs + 1
paulson@1927
   347
  in warn_dup th cs;
paulson@1927
   348
     CS{hazEs	= th::hazEs,
paulson@1927
   349
	haz_netpair = insert (nI,nE) ([], [th]) haz_netpair,
paulson@1927
   350
	dup_netpair = insert (nI,nE) ([], map dup_elim [th]) dup_netpair,
lcp@1073
   351
	safeIs	= safeIs, 
lcp@1073
   352
	safeEs	= safeEs,
lcp@1073
   353
	hazIs	= hazIs,
oheimb@2630
   354
	uwrapper      = uwrapper,
oheimb@2630
   355
	swrapper      = swrapper,
lcp@1073
   356
	safe0_netpair = safe0_netpair,
lcp@1073
   357
	safep_netpair = safep_netpair}
lcp@1073
   358
  end;
clasohm@0
   359
paulson@1927
   360
val op addIs = rev_foldl addI;
paulson@1927
   361
val op addEs = rev_foldl addE;
paulson@1927
   362
clasohm@0
   363
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   364
lcp@1073
   365
paulson@1800
   366
(*** Deletion of rules 
paulson@1800
   367
     Working out what to delete, requires repeating much of the code used
paulson@1800
   368
	to insert.
paulson@1927
   369
     Separate functions delSI, etc., are not exported; instead delrules
paulson@1800
   370
        searches in all the lists and chooses the relevant delXX function.
paulson@1800
   371
***)
paulson@1800
   372
oheimb@2630
   373
fun delSI (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1800
   374
               safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   375
            th) =
paulson@1800
   376
  let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=0) [th]
paulson@1800
   377
  in CS{safeIs	= gen_rem eq_thm (safeIs,th),
paulson@1800
   378
        safe0_netpair = delete (safe0_rls, []) safe0_netpair,
paulson@1800
   379
	safep_netpair = delete (safep_rls, []) safep_netpair,
paulson@1800
   380
	safeEs	= safeEs,
paulson@1800
   381
	hazIs	= hazIs,
paulson@1800
   382
	hazEs	= hazEs,
oheimb@2630
   383
	uwrapper     = uwrapper,
oheimb@2630
   384
	swrapper     = swrapper,
oheimb@2630
   385
	haz_netpair  = haz_netpair,
oheimb@2630
   386
	dup_netpair  = dup_netpair}
paulson@1800
   387
  end;
paulson@1800
   388
oheimb@2630
   389
fun delSE (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1800
   390
	       safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   391
            th) =
paulson@1800
   392
  let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=1) [th]
paulson@1800
   393
  in CS{safeEs	= gen_rem eq_thm (safeEs,th),
paulson@1800
   394
        safe0_netpair = delete ([], safe0_rls) safe0_netpair,
paulson@1800
   395
	safep_netpair = delete ([], safep_rls) safep_netpair,
paulson@1800
   396
	safeIs	= safeIs,
paulson@1800
   397
	hazIs	= hazIs,
paulson@1800
   398
	hazEs	= hazEs,
oheimb@2630
   399
	uwrapper     = uwrapper,
oheimb@2630
   400
	swrapper     = swrapper,
oheimb@2630
   401
	haz_netpair  = haz_netpair,
oheimb@2630
   402
	dup_netpair  = dup_netpair}
paulson@1800
   403
  end;
paulson@1800
   404
paulson@1800
   405
oheimb@2630
   406
fun delI (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1800
   407
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   408
	   th) =
paulson@1800
   409
     CS{hazIs	= gen_rem eq_thm (hazIs,th),
paulson@1800
   410
	haz_netpair = delete ([th], []) haz_netpair,
paulson@1800
   411
	dup_netpair = delete ([dup_intr th], []) dup_netpair,
paulson@1800
   412
	safeIs 	= safeIs, 
paulson@1800
   413
	safeEs	= safeEs,
paulson@1800
   414
	hazEs	= hazEs,
oheimb@2630
   415
	uwrapper      = uwrapper,
oheimb@2630
   416
	swrapper      = swrapper,
paulson@1800
   417
	safe0_netpair = safe0_netpair,
paulson@1800
   418
	safep_netpair = safep_netpair};
paulson@1800
   419
oheimb@2630
   420
fun delE (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper, 
paulson@1800
   421
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1800
   422
	   th) =
paulson@1800
   423
     CS{hazEs	= gen_rem eq_thm (hazEs,th),
paulson@1800
   424
	haz_netpair = delete ([], [th]) haz_netpair,
paulson@1800
   425
	dup_netpair = delete ([], [dup_elim th]) dup_netpair,
paulson@1800
   426
	safeIs	= safeIs, 
paulson@1800
   427
	safeEs	= safeEs,
paulson@1800
   428
	hazIs	= hazIs,
oheimb@2630
   429
	uwrapper      = uwrapper,
oheimb@2630
   430
	swrapper      = swrapper,
paulson@1800
   431
	safe0_netpair = safe0_netpair,
paulson@1800
   432
	safep_netpair = safep_netpair};
paulson@1800
   433
paulson@1800
   434
fun delrule (cs as CS{safeIs, safeEs, hazIs, hazEs, ...}, th) =
paulson@1927
   435
       if gen_mem eq_thm (th, safeIs) then delSI(cs,th)
paulson@1927
   436
  else if gen_mem eq_thm (th, safeEs) then delSE(cs,th)
paulson@1927
   437
  else if gen_mem eq_thm (th, hazIs) then delI(cs,th)
paulson@1927
   438
  else if gen_mem eq_thm (th, hazEs) then delE(cs,th)
paulson@1800
   439
  else (warning ("rule not in claset\n" ^ (string_of_thm th)); 
paulson@1800
   440
	cs);
paulson@1800
   441
paulson@1800
   442
val op delrules = foldl delrule;
paulson@1800
   443
paulson@1800
   444
oheimb@2630
   445
(*** Setting or modifying the wrapper tacticals ***)
lcp@982
   446
oheimb@2630
   447
(*Set a new uwrapper*)
oheimb@2630
   448
fun (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper,
lcp@1073
   449
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
oheimb@2630
   450
    setWrapper new_uwrapper  =
oheimb@2630
   451
  CS{safeIs	= safeIs,
lcp@1073
   452
     safeEs	= safeEs,
lcp@1073
   453
     hazIs	= hazIs,
lcp@1073
   454
     hazEs	= hazEs,
oheimb@2630
   455
     uwrapper 	= new_uwrapper,
oheimb@2630
   456
     swrapper   = swrapper,
lcp@1073
   457
     safe0_netpair = safe0_netpair,
lcp@1073
   458
     safep_netpair = safep_netpair,
lcp@1073
   459
     haz_netpair = haz_netpair,
lcp@1073
   460
     dup_netpair = dup_netpair};
lcp@982
   461
oheimb@2630
   462
(*Set a new swrapper*)
oheimb@2630
   463
fun (CS{safeIs, safeEs, hazIs, hazEs, uwrapper, swrapper,
oheimb@2630
   464
	safe0_netpair, safep_netpair, haz_netpair, dup_netpair, ...}) 
oheimb@2630
   465
    setSWrapper new_swrapper  =
oheimb@2630
   466
  CS{safeIs	= safeIs,
oheimb@2630
   467
     safeEs	= safeEs,
oheimb@2630
   468
     hazIs	= hazIs,
oheimb@2630
   469
     hazEs	= hazEs,
oheimb@2630
   470
     uwrapper   = uwrapper,
oheimb@2630
   471
     swrapper   = new_swrapper,
oheimb@2630
   472
     safe0_netpair = safe0_netpair,
oheimb@2630
   473
     safep_netpair = safep_netpair,
oheimb@2630
   474
     haz_netpair = haz_netpair,
oheimb@2630
   475
     dup_netpair = dup_netpair};
lcp@982
   476
oheimb@2630
   477
(*Compose a tactical with the existing uwrapper*)
oheimb@2630
   478
fun cs compWrapper  uwrapper' = cs setWrapper  (uwrapper' o getWrapper cs);
oheimb@2630
   479
oheimb@2630
   480
(*Compose a tactical with the existing swrapper*)
oheimb@2630
   481
fun cs compSWrapper swrapper' = cs setSWrapper (swrapper' o getSWrapper cs);
lcp@982
   482
oheimb@2630
   483
(*compose a safe tactic sequentially before/alternatively after safe_step_tac*)
oheimb@2630
   484
fun cs addSbefore tac1 = cs compSWrapper (fn tac2 => tac1 THEN_MAYBE' tac2);
oheimb@2630
   485
fun cs addSaltern tac2 = cs compSWrapper (fn tac1 => tac1 ORELSE'     tac2);
lcp@982
   486
oheimb@2630
   487
(*compose a tactic sequentially before/alternatively after the step tactic*)
oheimb@2630
   488
fun cs addbefore  tac1 = cs compWrapper  (fn tac2 => tac1 THEN_MAYBE' tac2);
oheimb@2630
   489
fun cs addaltern  tac2 = cs compWrapper  (fn tac1 => tac1 APPEND'     tac2);
lcp@982
   490
paulson@1711
   491
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   492
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   493
  treatment of priority might get muddled up.*)
paulson@1711
   494
fun merge_cs
oheimb@2630
   495
    (cs as CS{safeIs, safeEs, hazIs, hazEs, ...},
paulson@1711
   496
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,...}) =
paulson@1711
   497
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   498
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
oheimb@2630
   499
      val  hazIs' = gen_rems eq_thm ( hazIs2, hazIs)
oheimb@2630
   500
      val  hazEs' = gen_rems eq_thm ( hazEs2, hazEs)
paulson@1711
   501
  in cs addSIs safeIs'
paulson@1711
   502
        addSEs safeEs'
paulson@1711
   503
        addIs  hazIs'
paulson@1711
   504
        addEs  hazEs'
paulson@1711
   505
  end;
paulson@1711
   506
lcp@982
   507
paulson@1800
   508
(**** Simple tactics for theorem proving ****)
clasohm@0
   509
clasohm@0
   510
(*Attack subgoals using safe inferences -- matching, not resolution*)
oheimb@2630
   511
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@2630
   512
  getSWrapper cs (FIRST' [
oheimb@2630
   513
	eq_assume_tac,
oheimb@2630
   514
	eq_mp_tac,
oheimb@2630
   515
	bimatch_from_nets_tac safe0_netpair,
oheimb@2630
   516
	FIRST' hyp_subst_tacs,
oheimb@2630
   517
	bimatch_from_nets_tac safep_netpair]);
clasohm@0
   518
clasohm@0
   519
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@2630
   520
fun safe_tac cs = REPEAT_DETERM_FIRST 
oheimb@2630
   521
	(fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
lcp@747
   522
lcp@747
   523
(*But these unsafe steps at least solve a subgoal!*)
lcp@747
   524
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   525
  assume_tac 			  APPEND' 
lcp@747
   526
  contr_tac 			  APPEND' 
lcp@747
   527
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   528
lcp@747
   529
(*These are much worse since they could generate more and more subgoals*)
lcp@747
   530
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   531
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   532
clasohm@0
   533
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   534
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   535
lcp@982
   536
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   537
  biresolve_from_nets_tac haz_netpair;
lcp@681
   538
clasohm@0
   539
(*Single step for the prover.  FAILS unless it makes progress. *)
oheimb@2630
   540
fun step_tac cs i = getWrapper cs 
oheimb@2630
   541
	(K (safe_tac cs) ORELSE' (inst_step_tac cs ORELSE' haz_step_tac cs)) i;
clasohm@0
   542
clasohm@0
   543
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   544
  allows backtracking from "safe" rules to "unsafe" rules here.*)
oheimb@2630
   545
fun slow_step_tac cs i = getWrapper cs 
oheimb@2630
   546
	(K (safe_tac cs) ORELSE' (inst_step_tac cs APPEND' haz_step_tac cs)) i;
clasohm@0
   547
paulson@1800
   548
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   549
clasohm@0
   550
(*Dumb but fast*)
clasohm@0
   551
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   552
clasohm@0
   553
(*Slower but smarter than fast_tac*)
clasohm@0
   554
fun best_tac cs = 
clasohm@0
   555
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   556
clasohm@0
   557
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   558
clasohm@0
   559
fun slow_best_tac cs = 
clasohm@0
   560
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   561
lcp@681
   562
paulson@1800
   563
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   564
val weight_ASTAR = ref 5; 
paulson@1587
   565
paulson@1587
   566
fun astar_tac cs = 
paulson@1587
   567
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   568
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   569
	      (step_tac cs 1));
paulson@1587
   570
paulson@1587
   571
fun slow_astar_tac cs = 
paulson@1587
   572
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   573
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   574
	      (slow_step_tac cs 1));
paulson@1587
   575
paulson@1800
   576
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   577
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   578
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   579
  theorems such as 43. ****)
lcp@681
   580
lcp@747
   581
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   582
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   583
  greater search depth required.*)
lcp@681
   584
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   585
  biresolve_from_nets_tac dup_netpair;
lcp@681
   586
lcp@747
   587
(*Searching to depth m.*)
lcp@747
   588
fun depth_tac cs m i = STATE(fn state => 
lcp@747
   589
  SELECT_GOAL 
oheimb@2630
   590
   (getWrapper cs
oheimb@2630
   591
    (fn i => REPEAT_DETERM1 (safe_step_tac cs i) THEN_ELSE
oheimb@2630
   592
     (DEPTH_SOLVE (depth_tac cs m i),
oheimb@2630
   593
      inst0_step_tac cs i  APPEND
lcp@747
   594
      COND (K(m=0)) no_tac
oheimb@2630
   595
        ((instp_step_tac cs i APPEND dup_step_tac cs i)
oheimb@2630
   596
	 THEN DEPTH_SOLVE (depth_tac cs (m-1) i)))) 1)
lcp@747
   597
  i);
lcp@747
   598
lcp@747
   599
(*Iterative deepening tactical.  Allows us to "deepen" any search tactic*)
lcp@747
   600
fun DEEPEN tacf m i = STATE(fn state => 
lcp@747
   601
   if has_fewer_prems i state then no_tac
lcp@747
   602
   else (writeln ("Depth = " ^ string_of_int m);
lcp@747
   603
	 tacf m i  ORELSE  DEEPEN tacf (m+2) i));
lcp@747
   604
paulson@2173
   605
(*Search, with depth bound m.  
paulson@2173
   606
  This is the "entry point", which does safe inferences first.*)
lcp@747
   607
fun safe_depth_tac cs m = 
lcp@681
   608
  SUBGOAL 
lcp@681
   609
    (fn (prem,i) =>
lcp@681
   610
      let val deti =
lcp@681
   611
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   612
	  case term_vars prem of
lcp@681
   613
	      []	=> DETERM 
lcp@681
   614
	    | _::_	=> I
lcp@681
   615
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   616
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   617
      end);
lcp@681
   618
lcp@747
   619
fun deepen_tac cs = DEEPEN (safe_depth_tac cs);
lcp@681
   620
berghofe@1724
   621
val claset = ref empty_cs;
berghofe@1724
   622
berghofe@1724
   623
fun AddDs ts = (claset := !claset addDs ts);
berghofe@1724
   624
berghofe@1724
   625
fun AddEs ts = (claset := !claset addEs ts);
berghofe@1724
   626
berghofe@1724
   627
fun AddIs ts = (claset := !claset addIs ts);
berghofe@1724
   628
berghofe@1724
   629
fun AddSDs ts = (claset := !claset addSDs ts);
berghofe@1724
   630
berghofe@1724
   631
fun AddSEs ts = (claset := !claset addSEs ts);
berghofe@1724
   632
berghofe@1724
   633
fun AddSIs ts = (claset := !claset addSIs ts);
berghofe@1724
   634
paulson@1807
   635
fun Delrules ts = (claset := !claset delrules ts);
paulson@1807
   636
paulson@1800
   637
(*Cannot have Safe_tac, as it takes no arguments; must delay dereferencing!*)
paulson@1800
   638
paulson@1814
   639
fun Safe_step_tac i = safe_step_tac (!claset) i; 
paulson@1814
   640
paulson@1800
   641
fun Step_tac i = step_tac (!claset) i; 
paulson@1800
   642
berghofe@1724
   643
fun Fast_tac i = fast_tac (!claset) i; 
berghofe@1724
   644
paulson@1800
   645
fun Best_tac i = best_tac (!claset) i; 
paulson@1800
   646
paulson@2066
   647
fun Slow_tac i = slow_tac (!claset) i; 
paulson@2066
   648
paulson@2066
   649
fun Slow_best_tac i = slow_best_tac (!claset) i; 
paulson@2066
   650
paulson@1800
   651
fun Deepen_tac m = deepen_tac (!claset) m; 
paulson@1800
   652
clasohm@0
   653
end; 
clasohm@0
   654
end;
oheimb@2630
   655
oheimb@2630
   656