src/Provers/classical.ML
author oheimb
Mon Sep 21 23:14:33 1998 +0200 (1998-09-21 ago)
changeset 5523 dc8cdc192cd0
parent 5028 61c10aad3d71
child 5757 0ad476dabbc6
permissions -rw-r--r--
added addD2, addE2, addSD2, and addSE2
improved addbefore and addSbefore
improved mechanism for unsafe wrappers
wenzelm@4079
     1
(*  Title: 	Provers/classical.ML
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
wenzelm@4079
     9
Rules must be classified as intr, elim, safe, hazardous (unsafe).
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
wenzelm@4079
    17
(*higher precedence than := facilitates use of references*)
wenzelm@4079
    18
infix 4 addSIs addSEs addSDs addIs addEs addDs delrules
oheimb@4651
    19
  addSWrapper delSWrapper addWrapper delWrapper
oheimb@5523
    20
  addSbefore addSaltern addbefore addaltern
oheimb@5523
    21
  addD2 addE2 addSD2 addSE2;
wenzelm@4079
    22
wenzelm@4079
    23
wenzelm@4079
    24
(*should be a type abbreviation in signature CLASSICAL*)
wenzelm@4079
    25
type netpair = (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net;
oheimb@4651
    26
type wrapper = (int -> tactic) -> (int -> tactic);
wenzelm@4079
    27
wenzelm@4079
    28
signature CLASET_THY_DATA =
wenzelm@4079
    29
sig
wenzelm@5001
    30
  val clasetN: string
wenzelm@5001
    31
  val clasetK: Object.kind
wenzelm@5001
    32
  exception ClasetData of Object.T ref
wenzelm@4854
    33
  val setup: (theory -> theory) list
wenzelm@5001
    34
  val fix_methods: Object.T * (Object.T -> Object.T) *
wenzelm@5001
    35
    (Object.T * Object.T -> Object.T) * (Sign.sg -> Object.T -> unit) -> unit
wenzelm@4079
    36
end;
paulson@2868
    37
clasohm@0
    38
signature CLASSICAL_DATA =
wenzelm@4079
    39
sig
lcp@681
    40
  val mp	: thm    	(* [| P-->Q;  P |] ==> Q *)
lcp@681
    41
  val not_elim	: thm		(* [| ~P;  P |] ==> R *)
lcp@681
    42
  val classical	: thm		(* (~P ==> P) ==> P *)
lcp@681
    43
  val sizef 	: thm -> int	(* size function for BEST_FIRST *)
clasohm@0
    44
  val hyp_subst_tacs: (int -> tactic) list
wenzelm@4079
    45
end;
clasohm@0
    46
clasohm@0
    47
signature CLASSICAL =
wenzelm@4079
    48
sig
clasohm@0
    49
  type claset
wenzelm@4079
    50
  val empty_cs: claset
wenzelm@4079
    51
  val print_cs: claset -> unit
wenzelm@4380
    52
  val print_claset: theory -> unit
oheimb@4653
    53
  val rep_cs: (* BLAST_DATA in blast.ML dependent on this *)
wenzelm@4079
    54
    claset -> {safeIs: thm list, safeEs: thm list,
wenzelm@4079
    55
		 hazIs: thm list, hazEs: thm list,
oheimb@4651
    56
		 swrappers: (string * wrapper) list, 
oheimb@4651
    57
		 uwrappers: (string * wrapper) list,
wenzelm@4079
    58
		 safe0_netpair: netpair, safep_netpair: netpair,
wenzelm@4079
    59
		 haz_netpair: netpair, dup_netpair: netpair}
paulson@1711
    60
  val merge_cs		: claset * claset -> claset
lcp@681
    61
  val addDs 		: claset * thm list -> claset
lcp@681
    62
  val addEs 		: claset * thm list -> claset
lcp@681
    63
  val addIs 		: claset * thm list -> claset
lcp@681
    64
  val addSDs		: claset * thm list -> claset
lcp@681
    65
  val addSEs		: claset * thm list -> claset
lcp@681
    66
  val addSIs		: claset * thm list -> claset
paulson@1800
    67
  val delrules		: claset * thm list -> claset
oheimb@4651
    68
  val addSWrapper 	: claset * (string * wrapper) -> claset
oheimb@4651
    69
  val delSWrapper 	: claset *  string            -> claset
oheimb@4651
    70
  val addWrapper 	: claset * (string * wrapper) -> claset
oheimb@4651
    71
  val delWrapper 	: claset *  string            -> claset
oheimb@4651
    72
  val addSbefore 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    73
  val addSaltern 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    74
  val addbefore 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    75
  val addaltern	 	: claset * (string * (int -> tactic)) -> claset
oheimb@5523
    76
  val addD2             : claset * (string * thm) -> claset
oheimb@5523
    77
  val addE2             : claset * (string * thm) -> claset
oheimb@5523
    78
  val addSD2            : claset * (string * thm) -> claset
oheimb@5523
    79
  val addSE2            : claset * (string * thm) -> claset
oheimb@4765
    80
  val appSWrappers	: claset -> wrapper
oheimb@4651
    81
  val appWrappers	: claset -> wrapper
lcp@982
    82
wenzelm@4079
    83
  val claset_ref_of_sg: Sign.sg -> claset ref
wenzelm@4079
    84
  val claset_ref_of: theory -> claset ref
wenzelm@4079
    85
  val claset_of_sg: Sign.sg -> claset
wenzelm@4079
    86
  val claset_of: theory -> claset
wenzelm@4079
    87
  val CLASET: (claset -> tactic) -> tactic
wenzelm@4079
    88
  val CLASET': (claset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4079
    89
  val claset: unit -> claset
wenzelm@4079
    90
  val claset_ref: unit -> claset ref
wenzelm@4079
    91
paulson@1587
    92
  val fast_tac 		: claset -> int -> tactic
paulson@1587
    93
  val slow_tac 		: claset -> int -> tactic
paulson@1587
    94
  val weight_ASTAR	: int ref
paulson@1587
    95
  val astar_tac		: claset -> int -> tactic
paulson@1587
    96
  val slow_astar_tac 	: claset -> int -> tactic
lcp@681
    97
  val best_tac 		: claset -> int -> tactic
paulson@1587
    98
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
    99
  val depth_tac		: claset -> int -> int -> tactic
lcp@681
   100
  val deepen_tac	: claset -> int -> int -> tactic
paulson@1587
   101
paulson@1587
   102
  val contr_tac 	: int -> tactic
lcp@681
   103
  val dup_elim		: thm -> thm
lcp@681
   104
  val dup_intr		: thm -> thm
lcp@681
   105
  val dup_step_tac	: claset -> int -> tactic
lcp@681
   106
  val eq_mp_tac		: int -> tactic
lcp@681
   107
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
   108
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
   109
  val mp_tac		: int -> tactic
lcp@681
   110
  val safe_tac 		: claset -> tactic
lcp@681
   111
  val safe_step_tac 	: claset -> int -> tactic
paulson@3705
   112
  val clarify_tac 	: claset -> int -> tactic
paulson@3705
   113
  val clarify_step_tac 	: claset -> int -> tactic
lcp@681
   114
  val step_tac 		: claset -> int -> tactic
oheimb@2630
   115
  val slow_step_tac	: claset -> int -> tactic
lcp@681
   116
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
   117
  val swapify 		: thm list -> thm list
lcp@681
   118
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
   119
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
   120
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
   121
  val instp_step_tac 	: claset -> int -> tactic
berghofe@1724
   122
berghofe@1724
   123
  val AddDs 		: thm list -> unit
berghofe@1724
   124
  val AddEs 		: thm list -> unit
berghofe@1724
   125
  val AddIs 		: thm list -> unit
berghofe@1724
   126
  val AddSDs		: thm list -> unit
berghofe@1724
   127
  val AddSEs		: thm list -> unit
berghofe@1724
   128
  val AddSIs		: thm list -> unit
paulson@1807
   129
  val Delrules		: thm list -> unit
paulson@3727
   130
  val Safe_tac         	: tactic
paulson@1814
   131
  val Safe_step_tac	: int -> tactic
paulson@3705
   132
  val Clarify_tac 	: int -> tactic
paulson@3705
   133
  val Clarify_step_tac 	: int -> tactic
paulson@1800
   134
  val Step_tac 		: int -> tactic
berghofe@1724
   135
  val Fast_tac 		: int -> tactic
paulson@1800
   136
  val Best_tac 		: int -> tactic
paulson@2066
   137
  val Slow_tac 		: int -> tactic
paulson@2066
   138
  val Slow_best_tac     : int -> tactic
paulson@1800
   139
  val Deepen_tac	: int -> int -> tactic
wenzelm@4079
   140
end;
berghofe@1724
   141
wenzelm@4079
   142
wenzelm@4079
   143
structure ClasetThyData: CLASET_THY_DATA =
wenzelm@4079
   144
struct
wenzelm@4079
   145
wenzelm@4079
   146
(* data kind claset -- forward declaration *)
wenzelm@4079
   147
wenzelm@5001
   148
val clasetN = "Provers/claset";
wenzelm@5001
   149
val clasetK = Object.kind clasetN;
wenzelm@5001
   150
exception ClasetData of Object.T ref;
wenzelm@4079
   151
wenzelm@4079
   152
local
wenzelm@4079
   153
  fun undef _ = raise Match;
wenzelm@4079
   154
wenzelm@4079
   155
  val empty_ref = ref ERROR;
wenzelm@5001
   156
  val prep_ext_fn = ref (undef: Object.T -> Object.T);
wenzelm@5001
   157
  val merge_fn = ref (undef: Object.T * Object.T -> Object.T);
wenzelm@5001
   158
  val print_fn = ref (undef: Sign.sg -> Object.T -> unit);
wenzelm@4079
   159
wenzelm@4079
   160
  val empty = ClasetData empty_ref;
wenzelm@4079
   161
  fun prep_ext exn = ! prep_ext_fn exn;
wenzelm@4079
   162
  fun merge exn = ! merge_fn exn;
wenzelm@4259
   163
  fun print sg exn = ! print_fn sg exn;
wenzelm@4079
   164
in
wenzelm@5001
   165
  val setup = [Theory.init_data clasetK (empty, prep_ext, merge, print)];
wenzelm@4079
   166
  fun fix_methods (e, ext, mrg, prt) =
wenzelm@4079
   167
    (empty_ref := e; prep_ext_fn := ext; merge_fn := mrg; print_fn := prt);
wenzelm@4079
   168
end;
wenzelm@4079
   169
wenzelm@4079
   170
wenzelm@4079
   171
end;
clasohm@0
   172
clasohm@0
   173
clasohm@0
   174
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL = 
clasohm@0
   175
struct
clasohm@0
   176
wenzelm@4079
   177
local open ClasetThyData Data in
clasohm@0
   178
paulson@1800
   179
(*** Useful tactics for classical reasoning ***)
clasohm@0
   180
paulson@1524
   181
val imp_elim = (*cannot use bind_thm within a structure!*)
paulson@1524
   182
  store_thm ("imp_elim", make_elim mp);
clasohm@0
   183
paulson@4392
   184
(*Prove goal that assumes both P and ~P.  
paulson@4392
   185
  No backtracking if it finds an equal assumption.  Perhaps should call
paulson@4392
   186
  ematch_tac instead of eresolve_tac, but then cannot prove ZF/cantor.*)
paulson@4392
   187
val contr_tac = eresolve_tac [not_elim]  THEN'  
paulson@4392
   188
                (eq_assume_tac ORELSE' assume_tac);
clasohm@0
   189
lcp@681
   190
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   191
  Could do the same thing for P<->Q and P... *)
lcp@681
   192
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
   193
clasohm@0
   194
(*Like mp_tac but instantiates no variables*)
lcp@681
   195
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
   196
paulson@1524
   197
val swap =
paulson@1524
   198
  store_thm ("swap", rule_by_tactic (etac thin_rl 1) (not_elim RS classical));
clasohm@0
   199
clasohm@0
   200
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
   201
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
   202
clasohm@0
   203
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   204
  trying rules in order. *)
clasohm@0
   205
fun swap_res_tac rls = 
lcp@54
   206
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
   207
    in  assume_tac 	ORELSE' 
lcp@54
   208
	contr_tac 	ORELSE' 
lcp@54
   209
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   210
    end;
clasohm@0
   211
lcp@681
   212
(*Duplication of hazardous rules, for complete provers*)
paulson@2689
   213
fun dup_intr th = zero_var_indexes (th RS classical);
lcp@681
   214
paulson@4646
   215
fun dup_elim th = 
paulson@4646
   216
    th RSN (2, revcut_rl) |> assumption 2 |> Seq.hd |> 
paulson@4646
   217
    rule_by_tactic (TRYALL (etac revcut_rl))
paulson@4646
   218
    handle _ => error ("Bad format for elimination rule\n" ^ string_of_thm th);
clasohm@0
   219
lcp@1073
   220
paulson@1800
   221
(**** Classical rule sets ****)
clasohm@0
   222
clasohm@0
   223
datatype claset =
lcp@982
   224
  CS of {safeIs		: thm list,		(*safe introduction rules*)
lcp@982
   225
	 safeEs		: thm list,		(*safe elimination rules*)
lcp@982
   226
	 hazIs		: thm list,		(*unsafe introduction rules*)
lcp@982
   227
	 hazEs		: thm list,		(*unsafe elimination rules*)
oheimb@4651
   228
	 swrappers	: (string * wrapper) list, (*for transf. safe_step_tac*)
oheimb@4651
   229
	 uwrappers	: (string * wrapper) list, (*for transforming step_tac*)
lcp@982
   230
	 safe0_netpair	: netpair,		(*nets for trivial cases*)
lcp@982
   231
	 safep_netpair	: netpair,		(*nets for >0 subgoals*)
lcp@982
   232
	 haz_netpair  	: netpair,		(*nets for unsafe rules*)
lcp@982
   233
	 dup_netpair	: netpair};		(*nets for duplication*)
clasohm@0
   234
lcp@1073
   235
(*Desired invariants are
lcp@681
   236
	safe0_netpair = build safe0_brls,
lcp@681
   237
	safep_netpair = build safep_brls,
lcp@681
   238
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   239
	dup_netpair = build (joinrules(map dup_intr hazIs, 
lcp@681
   240
				       map dup_elim hazEs))}
lcp@1073
   241
lcp@1073
   242
where build = build_netpair(Net.empty,Net.empty), 
lcp@1073
   243
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   244
      safep_brls contains all brules that generate 1 or more new subgoals.
wenzelm@4079
   245
The theorem lists are largely comments, though they are used in merge_cs and print_cs.
lcp@1073
   246
Nets must be built incrementally, to save space and time.
lcp@1073
   247
*)
clasohm@0
   248
lcp@1073
   249
val empty_cs = 
lcp@1073
   250
  CS{safeIs	= [],
lcp@1073
   251
     safeEs	= [],
lcp@1073
   252
     hazIs	= [],
lcp@1073
   253
     hazEs	= [],
oheimb@4651
   254
     swrappers  = [],
oheimb@4651
   255
     uwrappers  = [],
lcp@1073
   256
     safe0_netpair = (Net.empty,Net.empty),
lcp@1073
   257
     safep_netpair = (Net.empty,Net.empty),
lcp@1073
   258
     haz_netpair   = (Net.empty,Net.empty),
lcp@1073
   259
     dup_netpair   = (Net.empty,Net.empty)};
clasohm@0
   260
wenzelm@3546
   261
fun print_cs (CS {safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@3546
   262
  let val pretty_thms = map Display.pretty_thm in
wenzelm@3546
   263
    Pretty.writeln (Pretty.big_list "safe introduction rules:" (pretty_thms safeIs));
wenzelm@4624
   264
    Pretty.writeln (Pretty.big_list "unsafe introduction rules:" (pretty_thms hazIs));
wenzelm@4625
   265
    Pretty.writeln (Pretty.big_list "safe elimination rules:" (pretty_thms safeEs));
wenzelm@4625
   266
    Pretty.writeln (Pretty.big_list "unsafe elimination rules:" (pretty_thms hazEs))
wenzelm@3546
   267
  end;
clasohm@0
   268
oheimb@4653
   269
fun rep_cs (CS args) = args;
lcp@1073
   270
oheimb@4651
   271
local 
oheimb@4651
   272
  fun calc_wrap l tac = foldr (fn ((name,tacf),w) => tacf w) (l, tac);
oheimb@4651
   273
in 
oheimb@4651
   274
  fun appSWrappers (CS{swrappers,...}) = calc_wrap swrappers;
oheimb@4651
   275
  fun appWrappers  (CS{uwrappers,...}) = calc_wrap uwrappers;
oheimb@4651
   276
end;
lcp@1073
   277
wenzelm@4079
   278
paulson@1800
   279
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   280
lcp@1073
   281
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   282
***)
clasohm@0
   283
lcp@1073
   284
(*For use with biresolve_tac.  Combines intr rules with swap to handle negated
lcp@1073
   285
  assumptions.  Pairs elim rules with true. *)
lcp@1073
   286
fun joinrules (intrs,elims) =  
lcp@1073
   287
    (map (pair true) (elims @ swapify intrs)  @
lcp@1073
   288
     map (pair false) intrs);
lcp@1073
   289
lcp@1073
   290
(*Priority: prefer rules with fewest subgoals, 
paulson@1231
   291
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   292
fun tag_brls k [] = []
lcp@1073
   293
  | tag_brls k (brl::brls) =
lcp@1073
   294
      (1000000*subgoals_of_brl brl + k, brl) :: 
lcp@1073
   295
      tag_brls (k+1) brls;
lcp@1073
   296
paulson@1800
   297
fun insert_tagged_list kbrls netpr = foldr insert_tagged_brl (kbrls, netpr);
lcp@1073
   298
lcp@1073
   299
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   300
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   301
  new insertions the lowest priority.*)
lcp@1073
   302
fun insert (nI,nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
lcp@1073
   303
paulson@1800
   304
fun delete_tagged_list brls netpr = foldr delete_tagged_brl (brls, netpr);
lcp@1073
   305
paulson@1800
   306
val delete = delete_tagged_list o joinrules;
paulson@1800
   307
paulson@2813
   308
val mem_thm = gen_mem eq_thm
paulson@2813
   309
and rem_thm = gen_rem eq_thm;
paulson@2813
   310
paulson@1927
   311
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   312
  is still allowed.*)
paulson@1927
   313
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) = 
paulson@2813
   314
       if mem_thm (th, safeIs) then 
wenzelm@4079
   315
	 warning ("Rule already in claset as Safe Intr\n" ^ string_of_thm th)
paulson@2813
   316
  else if mem_thm (th, safeEs) then
wenzelm@4079
   317
         warning ("Rule already in claset as Safe Elim\n" ^ string_of_thm th)
paulson@2813
   318
  else if mem_thm (th, hazIs) then 
wenzelm@4079
   319
         warning ("Rule already in claset as unsafe Intr\n" ^ string_of_thm th)
paulson@2813
   320
  else if mem_thm (th, hazEs) then 
wenzelm@4079
   321
         warning ("Rule already in claset as unsafe Elim\n" ^ string_of_thm th)
paulson@1927
   322
  else ();
paulson@1927
   323
paulson@1800
   324
(*** Safe rules ***)
lcp@982
   325
oheimb@4651
   326
fun addSI (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@1927
   327
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   328
	   th)  =
paulson@2813
   329
  if mem_thm (th, safeIs) then 
wenzelm@4079
   330
	 (warning ("Ignoring duplicate Safe Intr\n" ^ string_of_thm th);
paulson@1927
   331
	  cs)
paulson@1927
   332
  else
lcp@1073
   333
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   334
          partition (fn rl => nprems_of rl=0) [th]
paulson@1927
   335
      val nI = length safeIs + 1
lcp@1073
   336
      and nE = length safeEs
paulson@1927
   337
  in warn_dup th cs;
paulson@1927
   338
     CS{safeIs	= th::safeIs,
lcp@1073
   339
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   340
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   341
	safeEs	= safeEs,
lcp@1073
   342
	hazIs	= hazIs,
lcp@1073
   343
	hazEs	= hazEs,
oheimb@4651
   344
	swrappers    = swrappers,
oheimb@4651
   345
	uwrappers    = uwrappers,
oheimb@2630
   346
	haz_netpair  = haz_netpair,
oheimb@2630
   347
	dup_netpair  = dup_netpair}
lcp@1073
   348
  end;
lcp@1073
   349
oheimb@4651
   350
fun addSE (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@1927
   351
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   352
	   th)  =
paulson@2813
   353
  if mem_thm (th, safeEs) then 
wenzelm@4079
   354
	 (warning ("Ignoring duplicate Safe Elim\n" ^ string_of_thm th);
paulson@1927
   355
	  cs)
paulson@1927
   356
  else
lcp@1073
   357
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   358
          partition (fn rl => nprems_of rl=1) [th]
lcp@1073
   359
      val nI = length safeIs
paulson@1927
   360
      and nE = length safeEs + 1
paulson@1927
   361
  in warn_dup th cs;
paulson@1927
   362
     CS{safeEs	= th::safeEs,
lcp@1073
   363
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   364
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   365
	safeIs	= safeIs,
lcp@1073
   366
	hazIs	= hazIs,
lcp@1073
   367
	hazEs	= hazEs,
oheimb@4651
   368
	swrappers    = swrappers,
oheimb@4651
   369
	uwrappers    = uwrappers,
oheimb@2630
   370
	haz_netpair  = haz_netpair,
oheimb@2630
   371
	dup_netpair  = dup_netpair}
lcp@1073
   372
  end;
clasohm@0
   373
paulson@1927
   374
fun rev_foldl f (e, l) = foldl f (e, rev l);
paulson@1927
   375
paulson@1927
   376
val op addSIs = rev_foldl addSI;
paulson@1927
   377
val op addSEs = rev_foldl addSE;
paulson@1927
   378
clasohm@0
   379
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   380
lcp@1073
   381
paulson@1800
   382
(*** Hazardous (unsafe) rules ***)
clasohm@0
   383
oheimb@4651
   384
fun addI (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@1927
   385
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   386
	  th)=
paulson@2813
   387
  if mem_thm (th, hazIs) then 
wenzelm@4079
   388
	 (warning ("Ignoring duplicate unsafe Intr\n" ^ string_of_thm th);
paulson@1927
   389
	  cs)
paulson@1927
   390
  else
paulson@1927
   391
  let val nI = length hazIs + 1
lcp@1073
   392
      and nE = length hazEs
paulson@1927
   393
  in warn_dup th cs;
paulson@1927
   394
     CS{hazIs	= th::hazIs,
paulson@1927
   395
	haz_netpair = insert (nI,nE) ([th], []) haz_netpair,
paulson@1927
   396
	dup_netpair = insert (nI,nE) (map dup_intr [th], []) dup_netpair,
lcp@1073
   397
	safeIs 	= safeIs, 
lcp@1073
   398
	safeEs	= safeEs,
lcp@1073
   399
	hazEs	= hazEs,
oheimb@4651
   400
	swrappers     = swrappers,
oheimb@4651
   401
	uwrappers     = uwrappers,
lcp@1073
   402
	safe0_netpair = safe0_netpair,
lcp@1073
   403
	safep_netpair = safep_netpair}
lcp@1073
   404
  end;
lcp@1073
   405
oheimb@4651
   406
fun addE (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@1927
   407
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair},
paulson@1927
   408
	  th) =
paulson@2813
   409
  if mem_thm (th, hazEs) then 
wenzelm@4079
   410
	 (warning ("Ignoring duplicate unsafe Elim\n" ^ string_of_thm th);
paulson@1927
   411
	  cs)
paulson@1927
   412
  else
lcp@1073
   413
  let val nI = length hazIs 
paulson@1927
   414
      and nE = length hazEs + 1
paulson@1927
   415
  in warn_dup th cs;
paulson@1927
   416
     CS{hazEs	= th::hazEs,
paulson@1927
   417
	haz_netpair = insert (nI,nE) ([], [th]) haz_netpair,
paulson@1927
   418
	dup_netpair = insert (nI,nE) ([], map dup_elim [th]) dup_netpair,
lcp@1073
   419
	safeIs	= safeIs, 
lcp@1073
   420
	safeEs	= safeEs,
lcp@1073
   421
	hazIs	= hazIs,
oheimb@4651
   422
	swrappers     = swrappers,
oheimb@4651
   423
	uwrappers     = uwrappers,
lcp@1073
   424
	safe0_netpair = safe0_netpair,
lcp@1073
   425
	safep_netpair = safep_netpair}
lcp@1073
   426
  end;
clasohm@0
   427
paulson@1927
   428
val op addIs = rev_foldl addI;
paulson@1927
   429
val op addEs = rev_foldl addE;
paulson@1927
   430
clasohm@0
   431
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   432
lcp@1073
   433
paulson@1800
   434
(*** Deletion of rules 
paulson@1800
   435
     Working out what to delete, requires repeating much of the code used
paulson@1800
   436
	to insert.
paulson@1927
   437
     Separate functions delSI, etc., are not exported; instead delrules
paulson@2813
   438
        searches in all the lists and chooses the relevant delXX functions.
paulson@1800
   439
***)
paulson@1800
   440
paulson@2813
   441
fun delSI th 
oheimb@4651
   442
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@2813
   443
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   444
 if mem_thm (th, safeIs) then
paulson@2813
   445
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=0) [th]
paulson@2813
   446
   in CS{safe0_netpair = delete (safe0_rls, []) safe0_netpair,
paulson@2813
   447
	 safep_netpair = delete (safep_rls, []) safep_netpair,
paulson@2813
   448
	 safeIs	= rem_thm (safeIs,th),
paulson@2813
   449
	 safeEs	= safeEs,
paulson@2813
   450
	 hazIs	= hazIs,
paulson@2813
   451
	 hazEs	= hazEs,
oheimb@4651
   452
	 swrappers    = swrappers,
oheimb@4651
   453
	 uwrappers    = uwrappers,
paulson@2813
   454
	 haz_netpair  = haz_netpair,
paulson@2813
   455
	 dup_netpair  = dup_netpair}
paulson@2813
   456
   end
paulson@2813
   457
 else cs;
paulson@1800
   458
paulson@2813
   459
fun delSE th
oheimb@4651
   460
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@2813
   461
	            safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   462
 if mem_thm (th, safeEs) then
paulson@2813
   463
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=1) [th]
paulson@2813
   464
   in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
paulson@2813
   465
	 safep_netpair = delete ([], safep_rls) safep_netpair,
paulson@2813
   466
	 safeIs	= safeIs,
paulson@2813
   467
	 safeEs	= rem_thm (safeEs,th),
paulson@2813
   468
	 hazIs	= hazIs,
paulson@2813
   469
	 hazEs	= hazEs,
oheimb@4651
   470
	 swrappers    = swrappers,
oheimb@4651
   471
	 uwrappers    = uwrappers,
paulson@2813
   472
	 haz_netpair  = haz_netpair,
paulson@2813
   473
	 dup_netpair  = dup_netpair}
paulson@2813
   474
   end
paulson@2813
   475
 else cs;
paulson@1800
   476
paulson@1800
   477
paulson@2813
   478
fun delI th
oheimb@4651
   479
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@2813
   480
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   481
 if mem_thm (th, hazIs) then
paulson@2813
   482
     CS{haz_netpair = delete ([th], []) haz_netpair,
paulson@1800
   483
	dup_netpair = delete ([dup_intr th], []) dup_netpair,
paulson@1800
   484
	safeIs 	= safeIs, 
paulson@1800
   485
	safeEs	= safeEs,
paulson@2813
   486
	hazIs	= rem_thm (hazIs,th),
paulson@1800
   487
	hazEs	= hazEs,
oheimb@4651
   488
	swrappers     = swrappers,
oheimb@4651
   489
	uwrappers     = uwrappers,
paulson@1800
   490
	safe0_netpair = safe0_netpair,
paulson@2813
   491
	safep_netpair = safep_netpair}
paulson@2813
   492
 else cs;
paulson@1800
   493
paulson@2813
   494
fun delE th
oheimb@4651
   495
	 (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
paulson@2813
   496
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) =
paulson@2813
   497
 if mem_thm (th, hazEs) then
paulson@2813
   498
     CS{haz_netpair = delete ([], [th]) haz_netpair,
paulson@1800
   499
	dup_netpair = delete ([], [dup_elim th]) dup_netpair,
paulson@1800
   500
	safeIs	= safeIs, 
paulson@1800
   501
	safeEs	= safeEs,
paulson@1800
   502
	hazIs	= hazIs,
paulson@2813
   503
	hazEs	= rem_thm (hazEs,th),
oheimb@4651
   504
	swrappers     = swrappers,
oheimb@4651
   505
	uwrappers     = uwrappers,
paulson@1800
   506
	safe0_netpair = safe0_netpair,
paulson@2813
   507
	safep_netpair = safep_netpair}
paulson@2813
   508
 else cs;
paulson@1800
   509
paulson@2813
   510
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
paulson@1800
   511
fun delrule (cs as CS{safeIs, safeEs, hazIs, hazEs, ...}, th) =
paulson@2813
   512
       if mem_thm (th, safeIs) orelse mem_thm (th, safeEs) orelse
paulson@2813
   513
	  mem_thm (th, hazIs)  orelse mem_thm (th, hazEs) 
paulson@2813
   514
       then delSI th (delSE th (delI th (delE th cs)))
wenzelm@4079
   515
       else (warning ("Rule not in claset\n" ^ (string_of_thm th)); 
paulson@2813
   516
	     cs);
paulson@1800
   517
paulson@1800
   518
val op delrules = foldl delrule;
paulson@1800
   519
paulson@1800
   520
oheimb@4767
   521
(*** Modifying the wrapper tacticals ***)
oheimb@4767
   522
fun update_swrappers 
oheimb@4767
   523
(CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
oheimb@4767
   524
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) f =
oheimb@4767
   525
 CS{safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   526
    swrappers = f swrappers, uwrappers = uwrappers,
oheimb@4767
   527
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
oheimb@4767
   528
    haz_netpair = haz_netpair, dup_netpair = dup_netpair};
oheimb@4767
   529
oheimb@4767
   530
fun update_uwrappers 
oheimb@4767
   531
(CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, 
oheimb@4767
   532
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair}) f =
oheimb@4767
   533
 CS{safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   534
    swrappers = swrappers, uwrappers = f uwrappers,
oheimb@4767
   535
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
oheimb@4767
   536
    haz_netpair = haz_netpair, dup_netpair = dup_netpair};
oheimb@4767
   537
lcp@982
   538
oheimb@4651
   539
(*Add/replace a safe wrapper*)
oheimb@4767
   540
fun cs addSWrapper new_swrapper = update_swrappers cs (fn swrappers =>
oheimb@4767
   541
    (case assoc_string (swrappers,(fst new_swrapper)) of None =>()
oheimb@4651
   542
	   | Some x => warning("overwriting safe wrapper "^fst new_swrapper); 
oheimb@4767
   543
		   overwrite (swrappers, new_swrapper)));
oheimb@4651
   544
oheimb@4651
   545
(*Add/replace an unsafe wrapper*)
oheimb@4767
   546
fun cs addWrapper new_uwrapper = update_uwrappers cs (fn uwrappers =>
oheimb@4767
   547
    (case assoc_string (uwrappers,(fst new_uwrapper)) of None =>()
oheimb@4651
   548
	   | Some x => warning ("overwriting unsafe wrapper "^fst new_uwrapper);
oheimb@4767
   549
		   overwrite (uwrappers, new_uwrapper)));
lcp@982
   550
oheimb@4651
   551
(*Remove a safe wrapper*)
oheimb@4767
   552
fun cs delSWrapper name = update_swrappers cs (fn swrappers =>
oheimb@4767
   553
    let val (del,rest) = partition (fn (n,_) => n=name) swrappers
oheimb@4767
   554
    in if null del then (warning ("No such safe wrapper in claset: "^ name); 
oheimb@4767
   555
			 swrappers) else rest end);
lcp@982
   556
oheimb@4651
   557
(*Remove an unsafe wrapper*)
oheimb@4767
   558
fun cs delWrapper name = update_uwrappers cs (fn uwrappers =>
oheimb@4767
   559
    let val (del,rest) = partition (fn (n,_) => n=name) uwrappers
oheimb@4767
   560
    in if null del then (warning ("No such unsafe wrapper in claset: " ^ name);
oheimb@4767
   561
                         uwrappers) else rest end);
lcp@982
   562
oheimb@2630
   563
(*compose a safe tactic sequentially before/alternatively after safe_step_tac*)
oheimb@4767
   564
fun cs addSbefore  (name,    tac1) = 
oheimb@5523
   565
    cs addSWrapper (name, fn tac2 => tac1 ORELSE' tac2);
oheimb@4767
   566
fun cs addSaltern  (name,    tac2) = 
oheimb@5523
   567
    cs addSWrapper (name, fn tac1 => tac1 ORELSE' tac2);
lcp@982
   568
oheimb@2630
   569
(*compose a tactic sequentially before/alternatively after the step tactic*)
oheimb@4767
   570
fun cs addbefore   (name,    tac1) = 
oheimb@5523
   571
    cs addWrapper  (name, fn tac2 => tac1 APPEND' tac2);
oheimb@4767
   572
fun cs addaltern   (name,    tac2) =
oheimb@5523
   573
    cs addWrapper  (name, fn tac1 => tac1 APPEND' tac2);
oheimb@4767
   574
oheimb@5523
   575
(*#####*)
oheimb@5523
   576
fun cs addD2     (name, thm) = 
oheimb@5523
   577
    cs addaltern (name, dtac thm THEN' atac);
oheimb@5523
   578
fun cs addE2     (name, thm) = 
oheimb@5523
   579
    cs addaltern (name, etac thm THEN' atac);
oheimb@5523
   580
fun cs addSD2     (name, thm) = 
oheimb@5523
   581
    cs addSaltern (name, dmatch_tac [thm] THEN' eq_assume_tac);
oheimb@5523
   582
fun cs addSE2     (name, thm) = 
oheimb@5523
   583
    cs addSaltern (name, ematch_tac [thm] THEN' eq_assume_tac);
lcp@982
   584
paulson@1711
   585
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   586
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   587
  treatment of priority might get muddled up.*)
paulson@1711
   588
fun merge_cs
oheimb@2630
   589
    (cs as CS{safeIs, safeEs, hazIs, hazEs, ...},
oheimb@4765
   590
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,
oheimb@4765
   591
	swrappers, uwrappers, ...}) =
paulson@1711
   592
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   593
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
oheimb@2630
   594
      val  hazIs' = gen_rems eq_thm ( hazIs2, hazIs)
oheimb@2630
   595
      val  hazEs' = gen_rems eq_thm ( hazEs2, hazEs)
oheimb@4767
   596
      val cs1   = cs addSIs safeIs'
oheimb@4765
   597
		     addSEs safeEs'
oheimb@4765
   598
		     addIs  hazIs'
oheimb@4765
   599
		     addEs  hazEs'
oheimb@4767
   600
      val cs2 = update_swrappers cs1 (fn ws => merge_alists ws swrappers);
oheimb@4767
   601
      val cs3 = update_uwrappers cs2 (fn ws => merge_alists ws uwrappers);
oheimb@4767
   602
  in cs3 
paulson@1711
   603
  end;
paulson@1711
   604
lcp@982
   605
paulson@1800
   606
(**** Simple tactics for theorem proving ****)
clasohm@0
   607
clasohm@0
   608
(*Attack subgoals using safe inferences -- matching, not resolution*)
oheimb@2630
   609
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@4651
   610
  appSWrappers cs (FIRST' [
oheimb@2630
   611
	eq_assume_tac,
oheimb@2630
   612
	eq_mp_tac,
oheimb@2630
   613
	bimatch_from_nets_tac safe0_netpair,
oheimb@2630
   614
	FIRST' hyp_subst_tacs,
oheimb@2630
   615
	bimatch_from_nets_tac safep_netpair]);
clasohm@0
   616
clasohm@0
   617
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@2630
   618
fun safe_tac cs = REPEAT_DETERM_FIRST 
oheimb@2630
   619
	(fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
lcp@747
   620
paulson@3705
   621
paulson@3705
   622
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   623
paulson@3705
   624
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   625
paulson@3705
   626
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   627
  create precisely n subgoals.*)
paulson@3705
   628
fun n_bimatch_from_nets_tac n = 
paulson@3705
   629
    biresolution_from_nets_tac (orderlist o filter (nsubgoalsP n)) true;
paulson@3705
   630
paulson@3705
   631
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   632
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   633
paulson@3705
   634
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   635
fun bimatch2_tac netpair i =
paulson@3705
   636
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   637
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   638
paulson@3705
   639
(*Attack subgoals using safe inferences -- matching, not resolution*)
paulson@3705
   640
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@4651
   641
  appSWrappers cs (FIRST' [
paulson@3705
   642
	eq_assume_contr_tac,
paulson@3705
   643
	bimatch_from_nets_tac safe0_netpair,
paulson@3705
   644
	FIRST' hyp_subst_tacs,
paulson@3705
   645
	n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   646
        bimatch2_tac safep_netpair]);
paulson@3705
   647
paulson@3705
   648
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   649
paulson@3705
   650
paulson@3705
   651
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   652
paulson@4066
   653
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   654
  proving a subgoal.  *)
lcp@747
   655
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   656
  assume_tac 			  APPEND' 
lcp@747
   657
  contr_tac 			  APPEND' 
lcp@747
   658
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   659
paulson@4066
   660
(*These unsafe steps could generate more subgoals.*)
lcp@747
   661
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   662
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   663
clasohm@0
   664
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   665
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   666
lcp@982
   667
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   668
  biresolve_from_nets_tac haz_netpair;
lcp@681
   669
clasohm@0
   670
(*Single step for the prover.  FAILS unless it makes progress. *)
oheimb@5523
   671
fun step_tac cs i = safe_tac cs ORELSE appWrappers cs 
oheimb@5523
   672
	(inst_step_tac cs ORELSE' haz_step_tac cs) i;
clasohm@0
   673
clasohm@0
   674
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   675
  allows backtracking from "safe" rules to "unsafe" rules here.*)
oheimb@5523
   676
fun slow_step_tac cs i = safe_tac cs ORELSE appWrappers cs 
oheimb@5523
   677
	(inst_step_tac cs APPEND' haz_step_tac cs) i;
clasohm@0
   678
paulson@1800
   679
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   680
clasohm@0
   681
(*Dumb but fast*)
clasohm@0
   682
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   683
clasohm@0
   684
(*Slower but smarter than fast_tac*)
clasohm@0
   685
fun best_tac cs = 
clasohm@0
   686
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   687
clasohm@0
   688
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   689
clasohm@0
   690
fun slow_best_tac cs = 
clasohm@0
   691
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   692
lcp@681
   693
paulson@1800
   694
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   695
val weight_ASTAR = ref 5; 
paulson@1587
   696
paulson@1587
   697
fun astar_tac cs = 
paulson@1587
   698
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   699
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   700
	      (step_tac cs 1));
paulson@1587
   701
paulson@1587
   702
fun slow_astar_tac cs = 
paulson@1587
   703
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   704
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   705
	      (slow_step_tac cs 1));
paulson@1587
   706
paulson@1800
   707
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   708
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   709
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   710
  theorems such as 43. ****)
lcp@681
   711
lcp@747
   712
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   713
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   714
  greater search depth required.*)
lcp@681
   715
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   716
  biresolve_from_nets_tac dup_netpair;
lcp@681
   717
oheimb@5523
   718
(*Searching to depth m. A variant called nodup_depth_tac appears in clasimp.ML*)
oheimb@5523
   719
fun depth_tac cs m i state = SELECT_GOAL 
oheimb@5523
   720
   (REPEAT_DETERM1 (COND (has_fewer_prems 1) no_tac (safe_step_tac cs 1)) 
oheimb@5523
   721
    THEN_ELSE (DEPTH_SOLVE (depth_tac cs m 1),
oheimb@5523
   722
               appWrappers cs (fn i => inst0_step_tac cs i APPEND
oheimb@5523
   723
	       COND (K (m=0)) no_tac
oheimb@5523
   724
		        ((instp_step_tac cs i APPEND dup_step_tac cs i)
oheimb@5523
   725
			 THEN DEPTH_SOLVE (depth_tac cs (m-1) i))) 1))
paulson@3537
   726
  i state;
lcp@747
   727
paulson@2173
   728
(*Search, with depth bound m.  
paulson@2173
   729
  This is the "entry point", which does safe inferences first.*)
lcp@747
   730
fun safe_depth_tac cs m = 
lcp@681
   731
  SUBGOAL 
lcp@681
   732
    (fn (prem,i) =>
lcp@681
   733
      let val deti =
lcp@681
   734
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   735
	  case term_vars prem of
lcp@681
   736
	      []	=> DETERM 
lcp@681
   737
	    | _::_	=> I
lcp@681
   738
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   739
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   740
      end);
lcp@681
   741
paulson@2868
   742
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   743
wenzelm@4079
   744
berghofe@1724
   745
wenzelm@4079
   746
(** claset theory data **)
wenzelm@4079
   747
wenzelm@4079
   748
(* init data kind claset *)
berghofe@1724
   749
wenzelm@4079
   750
exception CSData of claset ref;
berghofe@1724
   751
wenzelm@4079
   752
local
wenzelm@4079
   753
  val empty = CSData (ref empty_cs);
wenzelm@4079
   754
wenzelm@4079
   755
  (*create new references*)
wenzelm@4079
   756
  fun prep_ext (ClasetData (ref (CSData (ref cs)))) =
wenzelm@4079
   757
    ClasetData (ref (CSData (ref cs)));
berghofe@1724
   758
wenzelm@4079
   759
  fun merge (ClasetData (ref (CSData (ref cs1))), ClasetData (ref (CSData (ref cs2)))) =
wenzelm@4079
   760
    ClasetData (ref (CSData (ref (merge_cs (cs1, cs2)))));
berghofe@1724
   761
wenzelm@4259
   762
  fun print (_: Sign.sg) (ClasetData (ref (CSData (ref cs)))) = print_cs cs;
wenzelm@4079
   763
in
wenzelm@4079
   764
  val _ = fix_methods (empty, prep_ext, merge, print);
wenzelm@4079
   765
end;
wenzelm@4079
   766
berghofe@1724
   767
wenzelm@4079
   768
(* access claset *)
berghofe@1724
   769
wenzelm@5001
   770
val print_claset = Theory.print_data clasetK;
wenzelm@4380
   771
wenzelm@5001
   772
val claset_ref_of_sg = Sign.get_data clasetK (fn ClasetData (ref (CSData r)) => r);
paulson@1807
   773
wenzelm@4079
   774
val claset_ref_of = claset_ref_of_sg o sign_of;
wenzelm@4079
   775
val claset_of_sg = ! o claset_ref_of_sg;
wenzelm@4079
   776
val claset_of = claset_of_sg o sign_of;
paulson@1800
   777
wenzelm@4079
   778
fun CLASET tacf state = tacf (claset_of_sg (sign_of_thm state)) state;
wenzelm@4079
   779
fun CLASET' tacf i state = tacf (claset_of_sg (sign_of_thm state)) i state;
paulson@3705
   780
wenzelm@5028
   781
val claset = claset_of o Context.the_context;
wenzelm@5028
   782
val claset_ref = claset_ref_of_sg o sign_of o Context.the_context;
wenzelm@4079
   783
paulson@3705
   784
wenzelm@4079
   785
(* change claset *)
paulson@1800
   786
wenzelm@4079
   787
fun change_claset f x = claset_ref () := (f (claset (), x));
berghofe@1724
   788
wenzelm@4079
   789
val AddDs = change_claset (op addDs);
wenzelm@4079
   790
val AddEs = change_claset (op addEs);
wenzelm@4079
   791
val AddIs = change_claset (op addIs);
wenzelm@4079
   792
val AddSDs = change_claset (op addSDs);
wenzelm@4079
   793
val AddSEs = change_claset (op addSEs);
wenzelm@4079
   794
val AddSIs = change_claset (op addSIs);
wenzelm@4079
   795
val Delrules = change_claset (op delrules);
paulson@3727
   796
wenzelm@4079
   797
wenzelm@4079
   798
(* tactics referring to the implicit claset *)
paulson@1800
   799
wenzelm@4079
   800
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4079
   801
fun Safe_tac st		  = safe_tac (claset()) st;
wenzelm@4079
   802
fun Safe_step_tac i st	  = safe_step_tac (claset()) i st;
wenzelm@4079
   803
fun Clarify_step_tac i st = clarify_step_tac (claset()) i st;
wenzelm@4079
   804
fun Clarify_tac i st	  = clarify_tac (claset()) i st;
wenzelm@4079
   805
fun Step_tac i st	  = step_tac (claset()) i st;
wenzelm@4079
   806
fun Fast_tac i st	  = fast_tac (claset()) i st;
wenzelm@4079
   807
fun Best_tac i st	  = best_tac (claset()) i st;
wenzelm@4079
   808
fun Slow_tac i st	  = slow_tac (claset()) i st;
wenzelm@4079
   809
fun Slow_best_tac i st	  = slow_best_tac (claset()) i st;
wenzelm@4079
   810
fun Deepen_tac m	  = deepen_tac (claset()) m;
paulson@2066
   811
paulson@1800
   812
clasohm@0
   813
end; 
clasohm@0
   814
end;