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