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