src/Provers/classical.ML
author wenzelm
Fri Jul 30 13:43:26 1999 +0200 (1999-07-30 ago)
changeset 7132 5c31d06ead04
parent 7106 c47d94f61ced
child 7154 687657a3f7e6
permissions -rw-r--r--
eliminated METHOD0 in favour of same_tac;
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@6556
    34
  val fix_methods: Object.T * (Object.T -> 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
wenzelm@5841
    47
signature BASIC_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,
wenzelm@6955
    56
		 xtraIs: thm list, xtraEs: thm list,
oheimb@4651
    57
		 swrappers: (string * wrapper) list, 
oheimb@4651
    58
		 uwrappers: (string * wrapper) list,
wenzelm@4079
    59
		 safe0_netpair: netpair, safep_netpair: netpair,
wenzelm@6955
    60
		 haz_netpair: netpair, dup_netpair: netpair, xtra_netpair: netpair}
paulson@1711
    61
  val merge_cs		: claset * claset -> claset
lcp@681
    62
  val addDs 		: claset * thm list -> claset
lcp@681
    63
  val addEs 		: claset * thm list -> claset
lcp@681
    64
  val addIs 		: claset * thm list -> claset
lcp@681
    65
  val addSDs		: claset * thm list -> claset
lcp@681
    66
  val addSEs		: claset * thm list -> claset
lcp@681
    67
  val addSIs		: claset * thm list -> claset
paulson@1800
    68
  val delrules		: claset * thm list -> claset
oheimb@4651
    69
  val addSWrapper 	: claset * (string * wrapper) -> claset
oheimb@4651
    70
  val delSWrapper 	: claset *  string            -> claset
oheimb@4651
    71
  val addWrapper 	: claset * (string * wrapper) -> claset
oheimb@4651
    72
  val delWrapper 	: claset *  string            -> claset
oheimb@4651
    73
  val addSbefore 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    74
  val addSaltern 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    75
  val addbefore 	: claset * (string * (int -> tactic)) -> claset
oheimb@4651
    76
  val addaltern	 	: claset * (string * (int -> tactic)) -> claset
oheimb@5523
    77
  val addD2             : claset * (string * thm) -> claset
oheimb@5523
    78
  val addE2             : claset * (string * thm) -> claset
oheimb@5523
    79
  val addSD2            : claset * (string * thm) -> claset
oheimb@5523
    80
  val addSE2            : claset * (string * thm) -> claset
oheimb@4765
    81
  val appSWrappers	: claset -> wrapper
oheimb@4651
    82
  val appWrappers	: claset -> wrapper
wenzelm@5927
    83
  val trace_rules	: bool ref
lcp@982
    84
wenzelm@4079
    85
  val claset_ref_of_sg: Sign.sg -> claset ref
wenzelm@4079
    86
  val claset_ref_of: theory -> claset ref
wenzelm@4079
    87
  val claset_of_sg: Sign.sg -> claset
wenzelm@4079
    88
  val claset_of: theory -> claset
wenzelm@4079
    89
  val CLASET: (claset -> tactic) -> tactic
wenzelm@4079
    90
  val CLASET': (claset -> 'a -> tactic) -> 'a -> tactic
wenzelm@4079
    91
  val claset: unit -> claset
wenzelm@4079
    92
  val claset_ref: unit -> claset ref
wenzelm@4079
    93
paulson@1587
    94
  val fast_tac 		: claset -> int -> tactic
paulson@1587
    95
  val slow_tac 		: claset -> int -> tactic
paulson@1587
    96
  val weight_ASTAR	: int ref
paulson@1587
    97
  val astar_tac		: claset -> int -> tactic
paulson@1587
    98
  val slow_astar_tac 	: claset -> int -> tactic
lcp@681
    99
  val best_tac 		: claset -> int -> tactic
paulson@1587
   100
  val slow_best_tac 	: claset -> int -> tactic
lcp@681
   101
  val depth_tac		: claset -> int -> int -> tactic
lcp@681
   102
  val deepen_tac	: claset -> int -> int -> tactic
paulson@1587
   103
paulson@1587
   104
  val contr_tac 	: int -> tactic
lcp@681
   105
  val dup_elim		: thm -> thm
lcp@681
   106
  val dup_intr		: thm -> thm
lcp@681
   107
  val dup_step_tac	: claset -> int -> tactic
lcp@681
   108
  val eq_mp_tac		: int -> tactic
lcp@681
   109
  val haz_step_tac 	: claset -> int -> tactic
lcp@681
   110
  val joinrules 	: thm list * thm list -> (bool * thm) list
lcp@681
   111
  val mp_tac		: int -> tactic
lcp@681
   112
  val safe_tac 		: claset -> tactic
oheimb@5757
   113
  val safe_steps_tac 	: claset -> int -> tactic
lcp@681
   114
  val safe_step_tac 	: claset -> int -> tactic
paulson@3705
   115
  val clarify_tac 	: claset -> int -> tactic
paulson@3705
   116
  val clarify_step_tac 	: claset -> int -> tactic
lcp@681
   117
  val step_tac 		: claset -> int -> tactic
oheimb@2630
   118
  val slow_step_tac	: claset -> int -> tactic
lcp@681
   119
  val swap		: thm                 (* ~P ==> (~Q ==> P) ==> Q *)
lcp@681
   120
  val swapify 		: thm list -> thm list
lcp@681
   121
  val swap_res_tac 	: thm list -> int -> tactic
lcp@681
   122
  val inst_step_tac 	: claset -> int -> tactic
lcp@747
   123
  val inst0_step_tac 	: claset -> int -> tactic
lcp@747
   124
  val instp_step_tac 	: claset -> int -> tactic
berghofe@1724
   125
berghofe@1724
   126
  val AddDs 		: thm list -> unit
berghofe@1724
   127
  val AddEs 		: thm list -> unit
berghofe@1724
   128
  val AddIs 		: thm list -> unit
berghofe@1724
   129
  val AddSDs		: thm list -> unit
berghofe@1724
   130
  val AddSEs		: thm list -> unit
berghofe@1724
   131
  val AddSIs		: thm list -> unit
wenzelm@6955
   132
  val AddXDs		: thm list -> unit
wenzelm@6955
   133
  val AddXEs		: thm list -> unit
wenzelm@6955
   134
  val AddXIs		: thm list -> unit
paulson@1807
   135
  val Delrules		: thm list -> unit
paulson@3727
   136
  val Safe_tac         	: tactic
paulson@1814
   137
  val Safe_step_tac	: int -> tactic
paulson@3705
   138
  val Clarify_tac 	: int -> tactic
paulson@3705
   139
  val Clarify_step_tac 	: int -> tactic
paulson@1800
   140
  val Step_tac 		: int -> tactic
berghofe@1724
   141
  val Fast_tac 		: int -> tactic
paulson@1800
   142
  val Best_tac 		: int -> tactic
paulson@2066
   143
  val Slow_tac 		: int -> tactic
paulson@2066
   144
  val Slow_best_tac     : int -> tactic
paulson@1800
   145
  val Deepen_tac	: int -> int -> tactic
wenzelm@4079
   146
end;
berghofe@1724
   147
wenzelm@5841
   148
signature CLASSICAL =
wenzelm@5841
   149
sig
wenzelm@5841
   150
  include BASIC_CLASSICAL
wenzelm@5841
   151
  val print_local_claset: Proof.context -> unit
wenzelm@5841
   152
  val get_local_claset: Proof.context -> claset
wenzelm@5841
   153
  val put_local_claset: claset -> Proof.context -> Proof.context
wenzelm@5841
   154
  val safe_dest_global: theory attribute
wenzelm@5841
   155
  val safe_elim_global: theory attribute
wenzelm@5841
   156
  val safe_intro_global: theory attribute
wenzelm@6955
   157
  val haz_dest_global: theory attribute
wenzelm@6955
   158
  val haz_elim_global: theory attribute
wenzelm@6955
   159
  val haz_intro_global: theory attribute
wenzelm@6955
   160
  val xtra_dest_global: theory attribute
wenzelm@6955
   161
  val xtra_elim_global: theory attribute
wenzelm@6955
   162
  val xtra_intro_global: theory attribute
wenzelm@5885
   163
  val delrule_global: theory attribute
wenzelm@6955
   164
  val safe_dest_local: Proof.context attribute
wenzelm@6955
   165
  val safe_elim_local: Proof.context attribute
wenzelm@6955
   166
  val safe_intro_local: Proof.context attribute
wenzelm@5885
   167
  val haz_dest_local: Proof.context attribute
wenzelm@5885
   168
  val haz_elim_local: Proof.context attribute
wenzelm@5885
   169
  val haz_intro_local: Proof.context attribute
wenzelm@6955
   170
  val xtra_dest_local: Proof.context attribute
wenzelm@6955
   171
  val xtra_elim_local: Proof.context attribute
wenzelm@6955
   172
  val xtra_intro_local: Proof.context attribute
wenzelm@5885
   173
  val delrule_local: Proof.context attribute
wenzelm@5927
   174
  val cla_modifiers: (Args.T list -> (Proof.context attribute * Args.T list)) list
wenzelm@5927
   175
  val cla_method: (claset -> tactic) -> Args.src -> Proof.context -> Proof.method
wenzelm@5927
   176
  val cla_method': (claset -> int -> tactic) -> Args.src -> Proof.context -> Proof.method
wenzelm@6096
   177
  val single_tac: claset -> thm list -> int -> tactic
wenzelm@5841
   178
  val setup: (theory -> theory) list
wenzelm@5841
   179
end;
wenzelm@5841
   180
wenzelm@4079
   181
structure ClasetThyData: CLASET_THY_DATA =
wenzelm@4079
   182
struct
wenzelm@4079
   183
wenzelm@4079
   184
(* data kind claset -- forward declaration *)
wenzelm@4079
   185
wenzelm@5001
   186
val clasetN = "Provers/claset";
wenzelm@5001
   187
val clasetK = Object.kind clasetN;
wenzelm@5001
   188
exception ClasetData of Object.T ref;
wenzelm@4079
   189
wenzelm@4079
   190
local
wenzelm@4079
   191
  fun undef _ = raise Match;
wenzelm@4079
   192
wenzelm@4079
   193
  val empty_ref = ref ERROR;
wenzelm@6556
   194
  val copy_fn = ref (undef: Object.T -> Object.T);
wenzelm@5001
   195
  val prep_ext_fn = ref (undef: Object.T -> Object.T);
wenzelm@5001
   196
  val merge_fn = ref (undef: Object.T * Object.T -> Object.T);
wenzelm@5001
   197
  val print_fn = ref (undef: Sign.sg -> Object.T -> unit);
wenzelm@4079
   198
wenzelm@4079
   199
  val empty = ClasetData empty_ref;
wenzelm@6556
   200
  fun copy exn = ! copy_fn exn;
wenzelm@4079
   201
  fun prep_ext exn = ! prep_ext_fn exn;
wenzelm@4079
   202
  fun merge exn = ! merge_fn exn;
wenzelm@4259
   203
  fun print sg exn = ! print_fn sg exn;
wenzelm@4079
   204
in
wenzelm@6556
   205
  val setup = [Theory.init_data clasetK (empty, copy, prep_ext, merge, print)];
wenzelm@6556
   206
  fun fix_methods (e, cp, ext, mrg, prt) =
wenzelm@6556
   207
    (empty_ref := e; copy_fn := cp; prep_ext_fn := ext; merge_fn := mrg; print_fn := prt);
wenzelm@4079
   208
end;
wenzelm@4079
   209
wenzelm@4079
   210
wenzelm@4079
   211
end;
clasohm@0
   212
clasohm@0
   213
wenzelm@5927
   214
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL =
clasohm@0
   215
struct
clasohm@0
   216
wenzelm@4079
   217
local open ClasetThyData Data in
clasohm@0
   218
paulson@1800
   219
(*** Useful tactics for classical reasoning ***)
clasohm@0
   220
paulson@1524
   221
val imp_elim = (*cannot use bind_thm within a structure!*)
paulson@1524
   222
  store_thm ("imp_elim", make_elim mp);
clasohm@0
   223
paulson@4392
   224
(*Prove goal that assumes both P and ~P.  
paulson@4392
   225
  No backtracking if it finds an equal assumption.  Perhaps should call
paulson@4392
   226
  ematch_tac instead of eresolve_tac, but then cannot prove ZF/cantor.*)
paulson@4392
   227
val contr_tac = eresolve_tac [not_elim]  THEN'  
paulson@4392
   228
                (eq_assume_tac ORELSE' assume_tac);
clasohm@0
   229
lcp@681
   230
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   231
  Could do the same thing for P<->Q and P... *)
lcp@681
   232
fun mp_tac i = eresolve_tac [not_elim, imp_elim] i  THEN  assume_tac i;
clasohm@0
   233
clasohm@0
   234
(*Like mp_tac but instantiates no variables*)
lcp@681
   235
fun eq_mp_tac i = ematch_tac [not_elim, imp_elim] i  THEN  eq_assume_tac i;
lcp@681
   236
paulson@1524
   237
val swap =
paulson@1524
   238
  store_thm ("swap", rule_by_tactic (etac thin_rl 1) (not_elim RS classical));
clasohm@0
   239
clasohm@0
   240
(*Creates rules to eliminate ~A, from rules to introduce A*)
clasohm@0
   241
fun swapify intrs = intrs RLN (2, [swap]);
clasohm@0
   242
clasohm@0
   243
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   244
  trying rules in order. *)
clasohm@0
   245
fun swap_res_tac rls = 
lcp@54
   246
    let fun addrl (rl,brls) = (false, rl) :: (true, rl RSN (2,swap)) :: brls
lcp@54
   247
    in  assume_tac 	ORELSE' 
lcp@54
   248
	contr_tac 	ORELSE' 
lcp@54
   249
        biresolve_tac (foldr addrl (rls,[]))
clasohm@0
   250
    end;
clasohm@0
   251
lcp@681
   252
(*Duplication of hazardous rules, for complete provers*)
paulson@2689
   253
fun dup_intr th = zero_var_indexes (th RS classical);
lcp@681
   254
wenzelm@6967
   255
fun dup_elim th =
wenzelm@6967
   256
  (case try
wenzelm@6967
   257
      (rule_by_tactic (TRYALL (etac revcut_rl)))
wenzelm@6967
   258
      (th RSN (2, revcut_rl) |> assumption 2 |> Seq.hd) of
wenzelm@6967
   259
    Some th' => th'
wenzelm@6967
   260
  | _ => error ("Bad format for elimination rule\n" ^ string_of_thm th));
clasohm@0
   261
lcp@1073
   262
paulson@1800
   263
(**** Classical rule sets ****)
clasohm@0
   264
clasohm@0
   265
datatype claset =
lcp@982
   266
  CS of {safeIs		: thm list,		(*safe introduction rules*)
lcp@982
   267
	 safeEs		: thm list,		(*safe elimination rules*)
lcp@982
   268
	 hazIs		: thm list,		(*unsafe introduction rules*)
lcp@982
   269
	 hazEs		: thm list,		(*unsafe elimination rules*)
wenzelm@6955
   270
	 xtraIs		: thm list,		(*extra introduction rules*)
wenzelm@6955
   271
	 xtraEs		: thm list,		(*extra elimination rules*)
oheimb@4651
   272
	 swrappers	: (string * wrapper) list, (*for transf. safe_step_tac*)
oheimb@4651
   273
	 uwrappers	: (string * wrapper) list, (*for transforming step_tac*)
lcp@982
   274
	 safe0_netpair	: netpair,		(*nets for trivial cases*)
lcp@982
   275
	 safep_netpair	: netpair,		(*nets for >0 subgoals*)
lcp@982
   276
	 haz_netpair  	: netpair,		(*nets for unsafe rules*)
wenzelm@6955
   277
	 dup_netpair	: netpair,		(*nets for duplication*)
wenzelm@6955
   278
	 xtra_netpair	: netpair};		(*nets for extra rules*)
clasohm@0
   279
lcp@1073
   280
(*Desired invariants are
lcp@681
   281
	safe0_netpair = build safe0_brls,
lcp@681
   282
	safep_netpair = build safep_brls,
lcp@681
   283
	haz_netpair = build (joinrules(hazIs, hazEs)),
lcp@681
   284
	dup_netpair = build (joinrules(map dup_intr hazIs, 
wenzelm@6955
   285
				       map dup_elim hazEs)),
wenzelm@6955
   286
	xtra_netpair = build (joinrules(xtraIs, xtraEs))}
lcp@1073
   287
lcp@1073
   288
where build = build_netpair(Net.empty,Net.empty), 
lcp@1073
   289
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   290
      safep_brls contains all brules that generate 1 or more new subgoals.
wenzelm@4079
   291
The theorem lists are largely comments, though they are used in merge_cs and print_cs.
lcp@1073
   292
Nets must be built incrementally, to save space and time.
lcp@1073
   293
*)
clasohm@0
   294
wenzelm@6502
   295
val empty_netpair = (Net.empty, Net.empty);
wenzelm@6502
   296
lcp@1073
   297
val empty_cs = 
lcp@1073
   298
  CS{safeIs	= [],
lcp@1073
   299
     safeEs	= [],
lcp@1073
   300
     hazIs	= [],
lcp@1073
   301
     hazEs	= [],
wenzelm@6955
   302
     xtraIs	= [],
wenzelm@6955
   303
     xtraEs	= [],
oheimb@4651
   304
     swrappers  = [],
oheimb@4651
   305
     uwrappers  = [],
wenzelm@6502
   306
     safe0_netpair = empty_netpair,
wenzelm@6502
   307
     safep_netpair = empty_netpair,
wenzelm@6502
   308
     haz_netpair   = empty_netpair,
wenzelm@6955
   309
     dup_netpair   = empty_netpair,
wenzelm@6955
   310
     xtra_netpair  = empty_netpair};
clasohm@0
   311
wenzelm@6955
   312
fun print_cs (CS {safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, ...}) =
wenzelm@3546
   313
  let val pretty_thms = map Display.pretty_thm in
wenzelm@3546
   314
    Pretty.writeln (Pretty.big_list "safe introduction rules:" (pretty_thms safeIs));
wenzelm@4624
   315
    Pretty.writeln (Pretty.big_list "unsafe introduction rules:" (pretty_thms hazIs));
wenzelm@6955
   316
    Pretty.writeln (Pretty.big_list "extra introduction rules:" (pretty_thms xtraIs));
wenzelm@4625
   317
    Pretty.writeln (Pretty.big_list "safe elimination rules:" (pretty_thms safeEs));
wenzelm@6955
   318
    Pretty.writeln (Pretty.big_list "unsafe elimination rules:" (pretty_thms hazEs));
wenzelm@6955
   319
    Pretty.writeln (Pretty.big_list "extra elimination rules:" (pretty_thms xtraEs))
wenzelm@3546
   320
  end;
clasohm@0
   321
oheimb@4653
   322
fun rep_cs (CS args) = args;
lcp@1073
   323
oheimb@4651
   324
local 
oheimb@4651
   325
  fun calc_wrap l tac = foldr (fn ((name,tacf),w) => tacf w) (l, tac);
oheimb@4651
   326
in 
oheimb@4651
   327
  fun appSWrappers (CS{swrappers,...}) = calc_wrap swrappers;
oheimb@4651
   328
  fun appWrappers  (CS{uwrappers,...}) = calc_wrap uwrappers;
oheimb@4651
   329
end;
lcp@1073
   330
wenzelm@4079
   331
paulson@1800
   332
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   333
lcp@1073
   334
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   335
***)
clasohm@0
   336
lcp@1073
   337
(*For use with biresolve_tac.  Combines intr rules with swap to handle negated
lcp@1073
   338
  assumptions.  Pairs elim rules with true. *)
lcp@1073
   339
fun joinrules (intrs,elims) =  
lcp@1073
   340
    (map (pair true) (elims @ swapify intrs)  @
lcp@1073
   341
     map (pair false) intrs);
lcp@1073
   342
lcp@1073
   343
(*Priority: prefer rules with fewest subgoals, 
paulson@1231
   344
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   345
fun tag_brls k [] = []
lcp@1073
   346
  | tag_brls k (brl::brls) =
lcp@1073
   347
      (1000000*subgoals_of_brl brl + k, brl) :: 
lcp@1073
   348
      tag_brls (k+1) brls;
lcp@1073
   349
paulson@1800
   350
fun insert_tagged_list kbrls netpr = foldr insert_tagged_brl (kbrls, netpr);
lcp@1073
   351
lcp@1073
   352
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   353
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   354
  new insertions the lowest priority.*)
lcp@1073
   355
fun insert (nI,nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
lcp@1073
   356
paulson@1800
   357
fun delete_tagged_list brls netpr = foldr delete_tagged_brl (brls, netpr);
lcp@1073
   358
paulson@1800
   359
val delete = delete_tagged_list o joinrules;
paulson@1800
   360
paulson@2813
   361
val mem_thm = gen_mem eq_thm
paulson@2813
   362
and rem_thm = gen_rem eq_thm;
paulson@2813
   363
paulson@1927
   364
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   365
  is still allowed.*)
wenzelm@6955
   366
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, ...}) = 
paulson@2813
   367
       if mem_thm (th, safeIs) then 
wenzelm@4079
   368
	 warning ("Rule already in claset as Safe Intr\n" ^ string_of_thm th)
paulson@2813
   369
  else if mem_thm (th, safeEs) then
wenzelm@4079
   370
         warning ("Rule already in claset as Safe Elim\n" ^ string_of_thm th)
paulson@2813
   371
  else if mem_thm (th, hazIs) then 
wenzelm@4079
   372
         warning ("Rule already in claset as unsafe Intr\n" ^ string_of_thm th)
paulson@2813
   373
  else if mem_thm (th, hazEs) then 
wenzelm@4079
   374
         warning ("Rule already in claset as unsafe Elim\n" ^ string_of_thm th)
wenzelm@6955
   375
  else if mem_thm (th, xtraIs) then 
wenzelm@6955
   376
         warning ("Rule already in claset as extra Intr\n" ^ string_of_thm th)
wenzelm@6955
   377
  else if mem_thm (th, xtraEs) then 
wenzelm@6955
   378
         warning ("Rule already in claset as extra Elim\n" ^ string_of_thm th)
paulson@1927
   379
  else ();
paulson@1927
   380
paulson@1800
   381
(*** Safe rules ***)
lcp@982
   382
wenzelm@6955
   383
fun addSI (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   384
	      safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
paulson@1927
   385
	   th)  =
paulson@2813
   386
  if mem_thm (th, safeIs) then 
wenzelm@4079
   387
	 (warning ("Ignoring duplicate Safe Intr\n" ^ string_of_thm th);
paulson@1927
   388
	  cs)
paulson@1927
   389
  else
lcp@1073
   390
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   391
          partition (fn rl => nprems_of rl=0) [th]
paulson@1927
   392
      val nI = length safeIs + 1
lcp@1073
   393
      and nE = length safeEs
paulson@1927
   394
  in warn_dup th cs;
paulson@1927
   395
     CS{safeIs	= th::safeIs,
lcp@1073
   396
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
lcp@1073
   397
	safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
lcp@1073
   398
	safeEs	= safeEs,
lcp@1073
   399
	hazIs	= hazIs,
lcp@1073
   400
	hazEs	= hazEs,
wenzelm@6955
   401
	xtraIs	= xtraIs,
wenzelm@6955
   402
	xtraEs	= xtraEs,
oheimb@4651
   403
	swrappers    = swrappers,
oheimb@4651
   404
	uwrappers    = uwrappers,
oheimb@2630
   405
	haz_netpair  = haz_netpair,
wenzelm@6955
   406
	dup_netpair  = dup_netpair,
wenzelm@6955
   407
	xtra_netpair = xtra_netpair}
lcp@1073
   408
  end;
lcp@1073
   409
wenzelm@6955
   410
fun addSE (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   411
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
paulson@1927
   412
	   th)  =
paulson@2813
   413
  if mem_thm (th, safeEs) then 
wenzelm@4079
   414
	 (warning ("Ignoring duplicate Safe Elim\n" ^ string_of_thm th);
paulson@1927
   415
	  cs)
paulson@1927
   416
  else
lcp@1073
   417
  let val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
paulson@1927
   418
          partition (fn rl => nprems_of rl=1) [th]
lcp@1073
   419
      val nI = length safeIs
paulson@1927
   420
      and nE = length safeEs + 1
paulson@1927
   421
  in warn_dup th cs;
paulson@1927
   422
     CS{safeEs	= th::safeEs,
lcp@1073
   423
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
lcp@1073
   424
	safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
lcp@1073
   425
	safeIs	= safeIs,
lcp@1073
   426
	hazIs	= hazIs,
lcp@1073
   427
	hazEs	= hazEs,
wenzelm@6955
   428
	xtraIs	= xtraIs,
wenzelm@6955
   429
	xtraEs	= xtraEs,
oheimb@4651
   430
	swrappers    = swrappers,
oheimb@4651
   431
	uwrappers    = uwrappers,
oheimb@2630
   432
	haz_netpair  = haz_netpair,
wenzelm@6955
   433
	dup_netpair  = dup_netpair,
wenzelm@6955
   434
	xtra_netpair = xtra_netpair}
lcp@1073
   435
  end;
clasohm@0
   436
paulson@1927
   437
fun rev_foldl f (e, l) = foldl f (e, rev l);
paulson@1927
   438
paulson@1927
   439
val op addSIs = rev_foldl addSI;
paulson@1927
   440
val op addSEs = rev_foldl addSE;
paulson@1927
   441
clasohm@0
   442
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   443
lcp@1073
   444
paulson@1800
   445
(*** Hazardous (unsafe) rules ***)
clasohm@0
   446
wenzelm@6955
   447
fun addI (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   448
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
paulson@1927
   449
	  th)=
paulson@2813
   450
  if mem_thm (th, hazIs) then 
wenzelm@4079
   451
	 (warning ("Ignoring duplicate unsafe Intr\n" ^ string_of_thm th);
paulson@1927
   452
	  cs)
paulson@1927
   453
  else
paulson@1927
   454
  let val nI = length hazIs + 1
lcp@1073
   455
      and nE = length hazEs
paulson@1927
   456
  in warn_dup th cs;
paulson@1927
   457
     CS{hazIs	= th::hazIs,
paulson@1927
   458
	haz_netpair = insert (nI,nE) ([th], []) haz_netpair,
paulson@1927
   459
	dup_netpair = insert (nI,nE) (map dup_intr [th], []) dup_netpair,
lcp@1073
   460
	safeIs 	= safeIs, 
lcp@1073
   461
	safeEs	= safeEs,
lcp@1073
   462
	hazEs	= hazEs,
wenzelm@6955
   463
	xtraIs	= xtraIs,
wenzelm@6955
   464
	xtraEs	= xtraEs,
oheimb@4651
   465
	swrappers     = swrappers,
oheimb@4651
   466
	uwrappers     = uwrappers,
lcp@1073
   467
	safe0_netpair = safe0_netpair,
wenzelm@6955
   468
	safep_netpair = safep_netpair,
wenzelm@6955
   469
	xtra_netpair = xtra_netpair}
lcp@1073
   470
  end;
lcp@1073
   471
wenzelm@6955
   472
fun addE (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   473
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
paulson@1927
   474
	  th) =
paulson@2813
   475
  if mem_thm (th, hazEs) then 
wenzelm@4079
   476
	 (warning ("Ignoring duplicate unsafe Elim\n" ^ string_of_thm th);
paulson@1927
   477
	  cs)
paulson@1927
   478
  else
lcp@1073
   479
  let val nI = length hazIs 
paulson@1927
   480
      and nE = length hazEs + 1
paulson@1927
   481
  in warn_dup th cs;
paulson@1927
   482
     CS{hazEs	= th::hazEs,
paulson@1927
   483
	haz_netpair = insert (nI,nE) ([], [th]) haz_netpair,
paulson@1927
   484
	dup_netpair = insert (nI,nE) ([], map dup_elim [th]) dup_netpair,
lcp@1073
   485
	safeIs	= safeIs, 
lcp@1073
   486
	safeEs	= safeEs,
lcp@1073
   487
	hazIs	= hazIs,
wenzelm@6955
   488
	xtraIs	= xtraIs,
wenzelm@6955
   489
	xtraEs	= xtraEs,
oheimb@4651
   490
	swrappers     = swrappers,
oheimb@4651
   491
	uwrappers     = uwrappers,
lcp@1073
   492
	safe0_netpair = safe0_netpair,
wenzelm@6955
   493
	safep_netpair = safep_netpair,
wenzelm@6955
   494
	xtra_netpair = xtra_netpair}
lcp@1073
   495
  end;
clasohm@0
   496
paulson@1927
   497
val op addIs = rev_foldl addI;
paulson@1927
   498
val op addEs = rev_foldl addE;
paulson@1927
   499
clasohm@0
   500
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   501
lcp@1073
   502
wenzelm@6955
   503
(*** Extra (single step) rules ***)
wenzelm@6955
   504
wenzelm@6955
   505
fun addXI (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers,
wenzelm@6955
   506
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
wenzelm@6955
   507
	  th)=
wenzelm@6955
   508
  if mem_thm (th, xtraIs) then 
wenzelm@6955
   509
	 (warning ("Ignoring duplicate extra Intr\n" ^ string_of_thm th);
wenzelm@6955
   510
	  cs)
wenzelm@6955
   511
  else
wenzelm@6955
   512
  let val nI = length xtraIs + 1
wenzelm@6955
   513
      and nE = length xtraEs
wenzelm@6955
   514
  in warn_dup th cs;
wenzelm@6955
   515
     CS{xtraIs	= th::xtraIs,
wenzelm@6955
   516
	xtra_netpair = insert (nI,nE) ([th], []) xtra_netpair,
wenzelm@6955
   517
	safeIs 	= safeIs, 
wenzelm@6955
   518
	safeEs	= safeEs,
wenzelm@6955
   519
	hazIs	= hazIs,
wenzelm@6955
   520
	hazEs	= hazEs,
wenzelm@6955
   521
	xtraEs	= xtraEs,
wenzelm@6955
   522
	swrappers     = swrappers,
wenzelm@6955
   523
	uwrappers     = uwrappers,
wenzelm@6955
   524
	safe0_netpair = safe0_netpair,
wenzelm@6955
   525
	safep_netpair = safep_netpair,
wenzelm@6955
   526
	haz_netpair  = haz_netpair,
wenzelm@6955
   527
	dup_netpair  = dup_netpair}
wenzelm@6955
   528
  end;
wenzelm@6955
   529
wenzelm@6955
   530
fun addXE (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   531
		   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair},
wenzelm@6955
   532
	  th) =
wenzelm@6955
   533
  if mem_thm (th, xtraEs) then
wenzelm@6955
   534
	 (warning ("Ignoring duplicate extra Elim\n" ^ string_of_thm th);
wenzelm@6955
   535
	  cs)
wenzelm@6955
   536
  else
wenzelm@6955
   537
  let val nI = length xtraIs 
wenzelm@6955
   538
      and nE = length xtraEs + 1
wenzelm@6955
   539
  in warn_dup th cs;
wenzelm@6955
   540
     CS{xtraEs	= th::xtraEs,
wenzelm@6955
   541
	xtra_netpair = insert (nI,nE) ([], [th]) xtra_netpair,
wenzelm@6955
   542
	safeIs	= safeIs, 
wenzelm@6955
   543
	safeEs	= safeEs,
wenzelm@6955
   544
	hazIs	= hazIs,
wenzelm@6955
   545
	hazEs	= hazEs,
wenzelm@6955
   546
	xtraIs	= xtraIs,
wenzelm@6955
   547
	swrappers     = swrappers,
wenzelm@6955
   548
	uwrappers     = uwrappers,
wenzelm@6955
   549
	safe0_netpair = safe0_netpair,
wenzelm@6955
   550
	safep_netpair = safep_netpair,
wenzelm@6955
   551
	haz_netpair  = haz_netpair,
wenzelm@6955
   552
	dup_netpair  = dup_netpair}
wenzelm@6955
   553
  end;
wenzelm@6955
   554
wenzelm@6955
   555
infix 4 addXIs addXEs addXDs;
wenzelm@6955
   556
wenzelm@6955
   557
val op addXIs = rev_foldl addXI;
wenzelm@6955
   558
val op addXEs = rev_foldl addXE;
wenzelm@6955
   559
wenzelm@6955
   560
fun cs addXDs ths = cs addXEs (map make_elim ths);
wenzelm@6955
   561
wenzelm@6955
   562
paulson@1800
   563
(*** Deletion of rules 
paulson@1800
   564
     Working out what to delete, requires repeating much of the code used
paulson@1800
   565
	to insert.
paulson@1927
   566
     Separate functions delSI, etc., are not exported; instead delrules
paulson@2813
   567
        searches in all the lists and chooses the relevant delXX functions.
paulson@1800
   568
***)
paulson@1800
   569
paulson@2813
   570
fun delSI th 
wenzelm@6955
   571
          (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers,
wenzelm@6955
   572
		    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
paulson@2813
   573
 if mem_thm (th, safeIs) then
paulson@2813
   574
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=0) [th]
paulson@2813
   575
   in CS{safe0_netpair = delete (safe0_rls, []) safe0_netpair,
paulson@2813
   576
	 safep_netpair = delete (safep_rls, []) safep_netpair,
paulson@2813
   577
	 safeIs	= rem_thm (safeIs,th),
paulson@2813
   578
	 safeEs	= safeEs,
paulson@2813
   579
	 hazIs	= hazIs,
paulson@2813
   580
	 hazEs	= hazEs,
wenzelm@6955
   581
	 xtraIs	= xtraIs,
wenzelm@6955
   582
	 xtraEs	= xtraEs,
oheimb@4651
   583
	 swrappers    = swrappers,
oheimb@4651
   584
	 uwrappers    = uwrappers,
paulson@2813
   585
	 haz_netpair  = haz_netpair,
wenzelm@6955
   586
	 dup_netpair  = dup_netpair,
wenzelm@6955
   587
	 xtra_netpair = xtra_netpair}
paulson@2813
   588
   end
paulson@2813
   589
 else cs;
paulson@1800
   590
paulson@2813
   591
fun delSE th
wenzelm@6955
   592
          (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   593
	            safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
paulson@2813
   594
 if mem_thm (th, safeEs) then
paulson@2813
   595
   let val (safe0_rls, safep_rls) = partition (fn rl => nprems_of rl=1) [th]
paulson@2813
   596
   in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
paulson@2813
   597
	 safep_netpair = delete ([], safep_rls) safep_netpair,
paulson@2813
   598
	 safeIs	= safeIs,
paulson@2813
   599
	 safeEs	= rem_thm (safeEs,th),
paulson@2813
   600
	 hazIs	= hazIs,
paulson@2813
   601
	 hazEs	= hazEs,
wenzelm@6955
   602
	 xtraIs	= xtraIs,
wenzelm@6955
   603
	 xtraEs	= xtraEs,
oheimb@4651
   604
	 swrappers    = swrappers,
oheimb@4651
   605
	 uwrappers    = uwrappers,
paulson@2813
   606
	 haz_netpair  = haz_netpair,
wenzelm@6955
   607
	 dup_netpair  = dup_netpair,
wenzelm@6955
   608
	 xtra_netpair = xtra_netpair}
paulson@2813
   609
   end
paulson@2813
   610
 else cs;
paulson@1800
   611
paulson@1800
   612
paulson@2813
   613
fun delI th
wenzelm@6955
   614
         (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   615
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
paulson@2813
   616
 if mem_thm (th, hazIs) then
paulson@2813
   617
     CS{haz_netpair = delete ([th], []) haz_netpair,
paulson@1800
   618
	dup_netpair = delete ([dup_intr th], []) dup_netpair,
paulson@1800
   619
	safeIs 	= safeIs, 
paulson@1800
   620
	safeEs	= safeEs,
paulson@2813
   621
	hazIs	= rem_thm (hazIs,th),
paulson@1800
   622
	hazEs	= hazEs,
wenzelm@6955
   623
	xtraIs	= xtraIs,
wenzelm@6955
   624
	xtraEs	= xtraEs,
oheimb@4651
   625
	swrappers     = swrappers,
oheimb@4651
   626
	uwrappers     = uwrappers,
paulson@1800
   627
	safe0_netpair = safe0_netpair,
wenzelm@6955
   628
	safep_netpair = safep_netpair,
wenzelm@6955
   629
	xtra_netpair = xtra_netpair}
paulson@2813
   630
 else cs;
paulson@1800
   631
paulson@2813
   632
fun delE th
wenzelm@6955
   633
	 (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   634
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
paulson@2813
   635
 if mem_thm (th, hazEs) then
paulson@2813
   636
     CS{haz_netpair = delete ([], [th]) haz_netpair,
paulson@1800
   637
	dup_netpair = delete ([], [dup_elim th]) dup_netpair,
paulson@1800
   638
	safeIs	= safeIs, 
paulson@1800
   639
	safeEs	= safeEs,
paulson@1800
   640
	hazIs	= hazIs,
paulson@2813
   641
	hazEs	= rem_thm (hazEs,th),
wenzelm@6955
   642
	xtraIs	= xtraIs,
wenzelm@6955
   643
	xtraEs	= xtraEs,
wenzelm@6955
   644
	swrappers     = swrappers,
wenzelm@6955
   645
	uwrappers     = uwrappers,
wenzelm@6955
   646
	safe0_netpair = safe0_netpair,
wenzelm@6955
   647
	safep_netpair = safep_netpair,
wenzelm@6955
   648
	xtra_netpair = xtra_netpair}
wenzelm@6955
   649
 else cs;
wenzelm@6955
   650
wenzelm@6955
   651
wenzelm@6955
   652
fun delXI th
wenzelm@6955
   653
         (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   654
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@6955
   655
 if mem_thm (th, xtraIs) then
wenzelm@6955
   656
     CS{xtra_netpair = delete ([th], []) xtra_netpair,
wenzelm@6955
   657
	safeIs 	= safeIs, 
wenzelm@6955
   658
	safeEs	= safeEs,
wenzelm@6955
   659
	hazIs	= hazIs,
wenzelm@6955
   660
	hazEs	= hazEs,
wenzelm@6955
   661
	xtraIs	= rem_thm (xtraIs,th),
wenzelm@6955
   662
	xtraEs	= xtraEs,
oheimb@4651
   663
	swrappers     = swrappers,
oheimb@4651
   664
	uwrappers     = uwrappers,
paulson@1800
   665
	safe0_netpair = safe0_netpair,
wenzelm@6955
   666
	safep_netpair = safep_netpair,
wenzelm@6955
   667
	haz_netpair  = haz_netpair,
wenzelm@6955
   668
	dup_netpair  = dup_netpair}
wenzelm@6955
   669
 else cs;
wenzelm@6955
   670
wenzelm@6955
   671
fun delXE th
wenzelm@6955
   672
	 (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   673
	           safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@6955
   674
 if mem_thm (th, xtraEs) then
wenzelm@6955
   675
     CS{xtra_netpair = delete ([], [th]) xtra_netpair,
wenzelm@6955
   676
	safeIs	= safeIs, 
wenzelm@6955
   677
	safeEs	= safeEs,
wenzelm@6955
   678
	hazIs	= hazIs,
wenzelm@6955
   679
	hazEs	= hazEs,
wenzelm@6955
   680
	xtraIs	= xtraIs,
wenzelm@6955
   681
	xtraEs	= rem_thm (xtraEs,th),
wenzelm@6955
   682
	swrappers     = swrappers,
wenzelm@6955
   683
	uwrappers     = uwrappers,
wenzelm@6955
   684
	safe0_netpair = safe0_netpair,
wenzelm@6955
   685
	safep_netpair = safep_netpair,
wenzelm@6955
   686
	haz_netpair  = haz_netpair,
wenzelm@6955
   687
	dup_netpair  = dup_netpair}
paulson@2813
   688
 else cs;
paulson@1800
   689
paulson@2813
   690
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
wenzelm@6955
   691
fun delrule (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, ...}, th) =
paulson@2813
   692
       if mem_thm (th, safeIs) orelse mem_thm (th, safeEs) orelse
wenzelm@6955
   693
	  mem_thm (th, hazIs)  orelse mem_thm (th, hazEs) orelse
wenzelm@6955
   694
          mem_thm (th, xtraIs)  orelse mem_thm (th, xtraEs) 
wenzelm@6955
   695
       then delSI th (delSE th (delI th (delE th (delXI th (delXE th cs)))))
wenzelm@4079
   696
       else (warning ("Rule not in claset\n" ^ (string_of_thm th)); 
paulson@2813
   697
	     cs);
paulson@1800
   698
paulson@1800
   699
val op delrules = foldl delrule;
paulson@1800
   700
paulson@1800
   701
oheimb@4767
   702
(*** Modifying the wrapper tacticals ***)
oheimb@4767
   703
fun update_swrappers 
wenzelm@6955
   704
(CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   705
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) f =
oheimb@4767
   706
 CS{safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
wenzelm@6955
   707
    xtraIs = xtraIs, xtraEs = xtraEs,
oheimb@4767
   708
    swrappers = f swrappers, uwrappers = uwrappers,
oheimb@4767
   709
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   710
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   711
oheimb@4767
   712
fun update_uwrappers 
wenzelm@6955
   713
(CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, swrappers, uwrappers, 
wenzelm@6955
   714
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) f =
oheimb@4767
   715
 CS{safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
wenzelm@6955
   716
    xtraIs = xtraIs, xtraEs = xtraEs,
oheimb@4767
   717
    swrappers = swrappers, uwrappers = f uwrappers,
oheimb@4767
   718
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   719
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   720
lcp@982
   721
oheimb@4651
   722
(*Add/replace a safe wrapper*)
oheimb@4767
   723
fun cs addSWrapper new_swrapper = update_swrappers cs (fn swrappers =>
oheimb@4767
   724
    (case assoc_string (swrappers,(fst new_swrapper)) of None =>()
oheimb@4651
   725
	   | Some x => warning("overwriting safe wrapper "^fst new_swrapper); 
oheimb@4767
   726
		   overwrite (swrappers, new_swrapper)));
oheimb@4651
   727
oheimb@4651
   728
(*Add/replace an unsafe wrapper*)
oheimb@4767
   729
fun cs addWrapper new_uwrapper = update_uwrappers cs (fn uwrappers =>
oheimb@4767
   730
    (case assoc_string (uwrappers,(fst new_uwrapper)) of None =>()
oheimb@4651
   731
	   | Some x => warning ("overwriting unsafe wrapper "^fst new_uwrapper);
oheimb@4767
   732
		   overwrite (uwrappers, new_uwrapper)));
lcp@982
   733
oheimb@4651
   734
(*Remove a safe wrapper*)
oheimb@4767
   735
fun cs delSWrapper name = update_swrappers cs (fn swrappers =>
oheimb@4767
   736
    let val (del,rest) = partition (fn (n,_) => n=name) swrappers
oheimb@4767
   737
    in if null del then (warning ("No such safe wrapper in claset: "^ name); 
oheimb@4767
   738
			 swrappers) else rest end);
lcp@982
   739
oheimb@4651
   740
(*Remove an unsafe wrapper*)
oheimb@4767
   741
fun cs delWrapper name = update_uwrappers cs (fn uwrappers =>
oheimb@4767
   742
    let val (del,rest) = partition (fn (n,_) => n=name) uwrappers
oheimb@4767
   743
    in if null del then (warning ("No such unsafe wrapper in claset: " ^ name);
oheimb@4767
   744
                         uwrappers) else rest end);
lcp@982
   745
oheimb@2630
   746
(*compose a safe tactic sequentially before/alternatively after safe_step_tac*)
oheimb@4767
   747
fun cs addSbefore  (name,    tac1) = 
oheimb@5523
   748
    cs addSWrapper (name, fn tac2 => tac1 ORELSE' tac2);
oheimb@4767
   749
fun cs addSaltern  (name,    tac2) = 
oheimb@5523
   750
    cs addSWrapper (name, fn tac1 => tac1 ORELSE' tac2);
lcp@982
   751
oheimb@2630
   752
(*compose a tactic sequentially before/alternatively after the step tactic*)
oheimb@4767
   753
fun cs addbefore   (name,    tac1) = 
oheimb@5523
   754
    cs addWrapper  (name, fn tac2 => tac1 APPEND' tac2);
oheimb@4767
   755
fun cs addaltern   (name,    tac2) =
oheimb@5523
   756
    cs addWrapper  (name, fn tac1 => tac1 APPEND' tac2);
oheimb@4767
   757
oheimb@5523
   758
fun cs addD2     (name, thm) = 
oheimb@5523
   759
    cs addaltern (name, dtac thm THEN' atac);
oheimb@5523
   760
fun cs addE2     (name, thm) = 
oheimb@5523
   761
    cs addaltern (name, etac thm THEN' atac);
oheimb@5523
   762
fun cs addSD2     (name, thm) = 
oheimb@5523
   763
    cs addSaltern (name, dmatch_tac [thm] THEN' eq_assume_tac);
oheimb@5523
   764
fun cs addSE2     (name, thm) = 
oheimb@5523
   765
    cs addSaltern (name, ematch_tac [thm] THEN' eq_assume_tac);
lcp@982
   766
paulson@1711
   767
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   768
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   769
  treatment of priority might get muddled up.*)
paulson@1711
   770
fun merge_cs
wenzelm@6955
   771
    (cs as CS{safeIs, safeEs, hazIs, hazEs, xtraIs, xtraEs, ...},
oheimb@4765
   772
     CS{safeIs=safeIs2, safeEs=safeEs2, hazIs=hazIs2, hazEs=hazEs2,
wenzelm@6955
   773
        xtraIs=xtraIs2, xtraEs=xtraEs2,	swrappers, uwrappers, ...}) =
paulson@1711
   774
  let val safeIs' = gen_rems eq_thm (safeIs2,safeIs)
paulson@1711
   775
      val safeEs' = gen_rems eq_thm (safeEs2,safeEs)
oheimb@2630
   776
      val  hazIs' = gen_rems eq_thm ( hazIs2, hazIs)
oheimb@2630
   777
      val  hazEs' = gen_rems eq_thm ( hazEs2, hazEs)
wenzelm@6955
   778
      val xtraIs' = gen_rems eq_thm (xtraIs2, xtraIs)
wenzelm@6955
   779
      val xtraEs' = gen_rems eq_thm (xtraEs2, xtraEs)
oheimb@4767
   780
      val cs1   = cs addSIs safeIs'
oheimb@4765
   781
		     addSEs safeEs'
oheimb@4765
   782
		     addIs  hazIs'
oheimb@4765
   783
		     addEs  hazEs'
wenzelm@6955
   784
		     addXIs xtraIs'
wenzelm@6955
   785
		     addXEs xtraEs'
oheimb@4767
   786
      val cs2 = update_swrappers cs1 (fn ws => merge_alists ws swrappers);
oheimb@4767
   787
      val cs3 = update_uwrappers cs2 (fn ws => merge_alists ws uwrappers);
oheimb@4767
   788
  in cs3 
paulson@1711
   789
  end;
paulson@1711
   790
lcp@982
   791
paulson@1800
   792
(**** Simple tactics for theorem proving ****)
clasohm@0
   793
clasohm@0
   794
(*Attack subgoals using safe inferences -- matching, not resolution*)
oheimb@2630
   795
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@4651
   796
  appSWrappers cs (FIRST' [
oheimb@2630
   797
	eq_assume_tac,
oheimb@2630
   798
	eq_mp_tac,
oheimb@2630
   799
	bimatch_from_nets_tac safe0_netpair,
oheimb@2630
   800
	FIRST' hyp_subst_tacs,
oheimb@2630
   801
	bimatch_from_nets_tac safep_netpair]);
clasohm@0
   802
oheimb@5757
   803
(*Repeatedly attack a subgoal using safe inferences -- it's deterministic!*)
oheimb@5757
   804
fun safe_steps_tac cs = REPEAT_DETERM1 o 
oheimb@5757
   805
	(fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
oheimb@5757
   806
clasohm@0
   807
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@5757
   808
fun safe_tac cs = REPEAT_DETERM1 (FIRSTGOAL (safe_steps_tac cs));
lcp@747
   809
paulson@3705
   810
paulson@3705
   811
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   812
paulson@3705
   813
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   814
paulson@3705
   815
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   816
  create precisely n subgoals.*)
paulson@3705
   817
fun n_bimatch_from_nets_tac n = 
paulson@3705
   818
    biresolution_from_nets_tac (orderlist o filter (nsubgoalsP n)) true;
paulson@3705
   819
paulson@3705
   820
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   821
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   822
paulson@3705
   823
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   824
fun bimatch2_tac netpair i =
paulson@3705
   825
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   826
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   827
paulson@3705
   828
(*Attack subgoals using safe inferences -- matching, not resolution*)
paulson@3705
   829
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) = 
oheimb@4651
   830
  appSWrappers cs (FIRST' [
paulson@3705
   831
	eq_assume_contr_tac,
paulson@3705
   832
	bimatch_from_nets_tac safe0_netpair,
paulson@3705
   833
	FIRST' hyp_subst_tacs,
paulson@3705
   834
	n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   835
        bimatch2_tac safep_netpair]);
paulson@3705
   836
paulson@3705
   837
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   838
paulson@3705
   839
paulson@3705
   840
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   841
paulson@4066
   842
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   843
  proving a subgoal.  *)
lcp@747
   844
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
lcp@747
   845
  assume_tac 			  APPEND' 
lcp@747
   846
  contr_tac 			  APPEND' 
lcp@747
   847
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   848
paulson@4066
   849
(*These unsafe steps could generate more subgoals.*)
lcp@747
   850
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   851
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   852
clasohm@0
   853
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   854
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   855
lcp@982
   856
fun haz_step_tac (CS{haz_netpair,...}) = 
lcp@681
   857
  biresolve_from_nets_tac haz_netpair;
lcp@681
   858
clasohm@0
   859
(*Single step for the prover.  FAILS unless it makes progress. *)
oheimb@5523
   860
fun step_tac cs i = safe_tac cs ORELSE appWrappers cs 
oheimb@5523
   861
	(inst_step_tac cs ORELSE' haz_step_tac cs) i;
clasohm@0
   862
clasohm@0
   863
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   864
  allows backtracking from "safe" rules to "unsafe" rules here.*)
oheimb@5523
   865
fun slow_step_tac cs i = safe_tac cs ORELSE appWrappers cs 
oheimb@5523
   866
	(inst_step_tac cs APPEND' haz_step_tac cs) i;
clasohm@0
   867
paulson@1800
   868
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   869
clasohm@0
   870
(*Dumb but fast*)
clasohm@0
   871
fun fast_tac cs = SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   872
clasohm@0
   873
(*Slower but smarter than fast_tac*)
clasohm@0
   874
fun best_tac cs = 
clasohm@0
   875
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   876
clasohm@0
   877
fun slow_tac cs = SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   878
clasohm@0
   879
fun slow_best_tac cs = 
clasohm@0
   880
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   881
lcp@681
   882
paulson@1800
   883
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*) 
paulson@1587
   884
val weight_ASTAR = ref 5; 
paulson@1587
   885
paulson@1587
   886
fun astar_tac cs = 
paulson@1587
   887
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   888
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   889
	      (step_tac cs 1));
paulson@1587
   890
paulson@1587
   891
fun slow_astar_tac cs = 
paulson@1587
   892
  SELECT_GOAL ( ASTAR (has_fewer_prems 1
paulson@1587
   893
	      , fn level =>(fn thm =>size_of_thm thm + !weight_ASTAR *level)) 
paulson@1587
   894
	      (slow_step_tac cs 1));
paulson@1587
   895
paulson@1800
   896
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   897
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   898
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   899
  theorems such as 43. ****)
lcp@681
   900
lcp@747
   901
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   902
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   903
  greater search depth required.*)
lcp@681
   904
fun dup_step_tac (cs as (CS{dup_netpair,...})) = 
lcp@681
   905
  biresolve_from_nets_tac dup_netpair;
lcp@681
   906
oheimb@5523
   907
(*Searching to depth m. A variant called nodup_depth_tac appears in clasimp.ML*)
oheimb@5757
   908
local
oheimb@5757
   909
fun slow_step_tac' cs = appWrappers cs 
oheimb@5757
   910
	(instp_step_tac cs APPEND' dup_step_tac cs);
oheimb@5757
   911
in fun depth_tac cs m i state = SELECT_GOAL 
oheimb@5757
   912
   (safe_steps_tac cs 1 THEN_ELSE 
oheimb@5757
   913
	(DEPTH_SOLVE (depth_tac cs m 1),
oheimb@5757
   914
	 inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
oheimb@5757
   915
		(slow_step_tac' cs 1 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))
oheimb@5757
   916
        )) i state;
oheimb@5757
   917
end;
lcp@747
   918
paulson@2173
   919
(*Search, with depth bound m.  
paulson@2173
   920
  This is the "entry point", which does safe inferences first.*)
lcp@747
   921
fun safe_depth_tac cs m = 
lcp@681
   922
  SUBGOAL 
lcp@681
   923
    (fn (prem,i) =>
lcp@681
   924
      let val deti =
lcp@681
   925
	  (*No Vars in the goal?  No need to backtrack between goals.*)
lcp@681
   926
	  case term_vars prem of
lcp@681
   927
	      []	=> DETERM 
lcp@681
   928
	    | _::_	=> I
lcp@681
   929
      in  SELECT_GOAL (TRY (safe_tac cs) THEN 
lcp@747
   930
		       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   931
      end);
lcp@681
   932
paulson@2868
   933
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   934
wenzelm@4079
   935
berghofe@1724
   936
wenzelm@4079
   937
(** claset theory data **)
wenzelm@4079
   938
wenzelm@4079
   939
(* init data kind claset *)
berghofe@1724
   940
wenzelm@4079
   941
exception CSData of claset ref;
berghofe@1724
   942
wenzelm@4079
   943
local
wenzelm@4079
   944
  val empty = CSData (ref empty_cs);
wenzelm@4079
   945
wenzelm@4079
   946
  (*create new references*)
wenzelm@6556
   947
  fun copy (ClasetData (ref (CSData (ref cs)))) =
wenzelm@4079
   948
    ClasetData (ref (CSData (ref cs)));
wenzelm@6556
   949
  val prep_ext = copy;
berghofe@1724
   950
wenzelm@4079
   951
  fun merge (ClasetData (ref (CSData (ref cs1))), ClasetData (ref (CSData (ref cs2)))) =
wenzelm@4079
   952
    ClasetData (ref (CSData (ref (merge_cs (cs1, cs2)))));
berghofe@1724
   953
wenzelm@4259
   954
  fun print (_: Sign.sg) (ClasetData (ref (CSData (ref cs)))) = print_cs cs;
wenzelm@4079
   955
in
wenzelm@6556
   956
  val _ = fix_methods (empty, copy, prep_ext, merge, print);
wenzelm@4079
   957
end;
wenzelm@4079
   958
berghofe@1724
   959
wenzelm@4079
   960
(* access claset *)
berghofe@1724
   961
wenzelm@5001
   962
val print_claset = Theory.print_data clasetK;
wenzelm@4380
   963
wenzelm@5001
   964
val claset_ref_of_sg = Sign.get_data clasetK (fn ClasetData (ref (CSData r)) => r);
paulson@1807
   965
wenzelm@6391
   966
val claset_ref_of = claset_ref_of_sg o Theory.sign_of;
wenzelm@4079
   967
val claset_of_sg = ! o claset_ref_of_sg;
wenzelm@6391
   968
val claset_of = claset_of_sg o Theory.sign_of;
paulson@1800
   969
wenzelm@6391
   970
fun CLASET tacf state = tacf (claset_of_sg (Thm.sign_of_thm state)) state;
wenzelm@6391
   971
fun CLASET' tacf i state = tacf (claset_of_sg (Thm.sign_of_thm state)) i state;
paulson@3705
   972
wenzelm@5028
   973
val claset = claset_of o Context.the_context;
wenzelm@6391
   974
val claset_ref = claset_ref_of_sg o Theory.sign_of o Context.the_context;
wenzelm@4079
   975
paulson@3705
   976
wenzelm@4079
   977
(* change claset *)
paulson@1800
   978
wenzelm@4079
   979
fun change_claset f x = claset_ref () := (f (claset (), x));
berghofe@1724
   980
wenzelm@4079
   981
val AddDs = change_claset (op addDs);
wenzelm@4079
   982
val AddEs = change_claset (op addEs);
wenzelm@4079
   983
val AddIs = change_claset (op addIs);
wenzelm@4079
   984
val AddSDs = change_claset (op addSDs);
wenzelm@4079
   985
val AddSEs = change_claset (op addSEs);
wenzelm@4079
   986
val AddSIs = change_claset (op addSIs);
wenzelm@6955
   987
val AddXDs = change_claset (op addXDs);
wenzelm@6955
   988
val AddXEs = change_claset (op addXEs);
wenzelm@6955
   989
val AddXIs = change_claset (op addXIs);
wenzelm@4079
   990
val Delrules = change_claset (op delrules);
paulson@3727
   991
wenzelm@4079
   992
wenzelm@5841
   993
(* proof data kind 'Provers/claset' *)
wenzelm@5841
   994
wenzelm@5841
   995
structure LocalClasetArgs =
wenzelm@5841
   996
struct
wenzelm@5841
   997
  val name = "Provers/claset";
wenzelm@5841
   998
  type T = claset;
wenzelm@5841
   999
  val init = claset_of;
wenzelm@5841
  1000
  fun print _ cs = print_cs cs;
wenzelm@5841
  1001
end;
wenzelm@5841
  1002
wenzelm@5841
  1003
structure LocalClaset = ProofDataFun(LocalClasetArgs);
wenzelm@5841
  1004
val print_local_claset = LocalClaset.print;
wenzelm@5841
  1005
val get_local_claset = LocalClaset.get;
wenzelm@5841
  1006
val put_local_claset = LocalClaset.put;
wenzelm@5841
  1007
wenzelm@5841
  1008
wenzelm@5885
  1009
(* attributes *)
wenzelm@5885
  1010
wenzelm@5885
  1011
fun change_global_cs f (thy, th) =
wenzelm@5885
  1012
  let val r = claset_ref_of thy
wenzelm@6096
  1013
  in r := f (! r, [th]); (thy, th) end;
wenzelm@5885
  1014
wenzelm@5885
  1015
fun change_local_cs f (ctxt, th) =
wenzelm@6096
  1016
  let val cs = f (get_local_claset ctxt, [th])
wenzelm@5885
  1017
  in (put_local_claset cs ctxt, th) end;
wenzelm@5885
  1018
wenzelm@5885
  1019
val safe_dest_global = change_global_cs (op addSDs);
wenzelm@5885
  1020
val safe_elim_global = change_global_cs (op addSEs);
wenzelm@5885
  1021
val safe_intro_global = change_global_cs (op addSIs);
wenzelm@6955
  1022
val haz_dest_global = change_global_cs (op addDs);
wenzelm@6955
  1023
val haz_elim_global = change_global_cs (op addEs);
wenzelm@6955
  1024
val haz_intro_global = change_global_cs (op addIs);
wenzelm@6955
  1025
val xtra_dest_global = change_global_cs (op addXDs);
wenzelm@6955
  1026
val xtra_elim_global = change_global_cs (op addXEs);
wenzelm@6955
  1027
val xtra_intro_global = change_global_cs (op addXIs);
wenzelm@5885
  1028
val delrule_global = change_global_cs (op delrules);
wenzelm@5885
  1029
wenzelm@6955
  1030
val safe_dest_local = change_local_cs (op addSDs);
wenzelm@6955
  1031
val safe_elim_local = change_local_cs (op addSEs);
wenzelm@6955
  1032
val safe_intro_local = change_local_cs (op addSIs);
wenzelm@5885
  1033
val haz_dest_local = change_local_cs (op addDs);
wenzelm@5885
  1034
val haz_elim_local = change_local_cs (op addEs);
wenzelm@5885
  1035
val haz_intro_local = change_local_cs (op addIs);
wenzelm@6955
  1036
val xtra_dest_local = change_local_cs (op addXDs);
wenzelm@6955
  1037
val xtra_elim_local = change_local_cs (op addXEs);
wenzelm@6955
  1038
val xtra_intro_local = change_local_cs (op addXIs);
wenzelm@5885
  1039
val delrule_local = change_local_cs (op delrules);
wenzelm@5885
  1040
wenzelm@5885
  1041
wenzelm@4079
  1042
(* tactics referring to the implicit claset *)
paulson@1800
  1043
wenzelm@4079
  1044
(*the abstraction over the proof state delays the dereferencing*)
wenzelm@4079
  1045
fun Safe_tac st		  = safe_tac (claset()) st;
wenzelm@4079
  1046
fun Safe_step_tac i st	  = safe_step_tac (claset()) i st;
wenzelm@4079
  1047
fun Clarify_step_tac i st = clarify_step_tac (claset()) i st;
wenzelm@4079
  1048
fun Clarify_tac i st	  = clarify_tac (claset()) i st;
wenzelm@4079
  1049
fun Step_tac i st	  = step_tac (claset()) i st;
wenzelm@4079
  1050
fun Fast_tac i st	  = fast_tac (claset()) i st;
wenzelm@4079
  1051
fun Best_tac i st	  = best_tac (claset()) i st;
wenzelm@4079
  1052
fun Slow_tac i st	  = slow_tac (claset()) i st;
wenzelm@4079
  1053
fun Slow_best_tac i st	  = slow_best_tac (claset()) i st;
wenzelm@4079
  1054
fun Deepen_tac m	  = deepen_tac (claset()) m;
paulson@2066
  1055
paulson@1800
  1056
clasohm@0
  1057
end; 
wenzelm@5841
  1058
wenzelm@5841
  1059
wenzelm@5841
  1060
wenzelm@5885
  1061
(** concrete syntax of attributes **)
wenzelm@5841
  1062
wenzelm@5841
  1063
(* add / del rules *)
wenzelm@5841
  1064
wenzelm@5841
  1065
val introN = "intro";
wenzelm@5841
  1066
val elimN = "elim";
wenzelm@5841
  1067
val destN = "dest";
wenzelm@5841
  1068
val delN = "del";
wenzelm@5885
  1069
val delruleN = "delrule";
wenzelm@5841
  1070
wenzelm@5885
  1071
val bang = Args.$$$ "!";
wenzelm@6955
  1072
val bbang = Args.$$$ "!!";
wenzelm@5841
  1073
wenzelm@6955
  1074
fun cla_att change xtra haz safe = Attrib.syntax
wenzelm@6955
  1075
  (Scan.lift ((bbang >> K xtra || bang >> K haz || Scan.succeed safe) >> change));
wenzelm@5841
  1076
wenzelm@6955
  1077
fun cla_attr f g h = (cla_att change_global_cs f g h, cla_att change_local_cs f g h);
wenzelm@5885
  1078
val del_attr = (Attrib.no_args delrule_global, Attrib.no_args delrule_local);
wenzelm@5841
  1079
wenzelm@5841
  1080
wenzelm@5841
  1081
(* setup_attrs *)
wenzelm@5841
  1082
wenzelm@5841
  1083
val setup_attrs = Attrib.add_attributes
wenzelm@6955
  1084
 [(destN, cla_attr (op addXDs) (op addDs) (op addSDs), "destruction rule"),
wenzelm@6955
  1085
  (elimN, cla_attr (op addXEs) (op addEs) (op addSEs), "elimination rule"),
wenzelm@6955
  1086
  (introN, cla_attr (op addXIs) (op addIs) (op addSIs), "introduction rule"),
wenzelm@5885
  1087
  (delruleN, del_attr, "delete rule")];
wenzelm@5841
  1088
wenzelm@5841
  1089
wenzelm@5841
  1090
wenzelm@5885
  1091
(** single rule proof method **)
wenzelm@5841
  1092
wenzelm@5841
  1093
(* utils *)
wenzelm@5841
  1094
wenzelm@5841
  1095
fun resolve_from_seq_tac rq i st = Seq.flat (Seq.map (fn r => rtac r i st) rq);
wenzelm@5841
  1096
fun order_rules xs = map snd (Tactic.orderlist xs);
wenzelm@5841
  1097
wenzelm@5841
  1098
fun find_rules concl nets =
wenzelm@5841
  1099
  let fun rules_of (inet, _) = order_rules (Net.unify_term inet concl)
wenzelm@5841
  1100
  in flat (map rules_of nets) end;
wenzelm@5841
  1101
wenzelm@5841
  1102
fun find_erules [] _ = []
wenzelm@6955
  1103
  | find_erules (fact :: _) nets =
wenzelm@5841
  1104
      let
wenzelm@6502
  1105
        fun may_unify net = Net.unify_term net o Logic.strip_assums_concl o #prop o Thm.rep_thm;
wenzelm@6955
  1106
        fun erules_of (_, enet) = order_rules (may_unify enet fact);
wenzelm@6502
  1107
      in flat (map erules_of nets) end;
wenzelm@5841
  1108
wenzelm@5841
  1109
wenzelm@5841
  1110
(* trace rules *)
wenzelm@5841
  1111
wenzelm@5841
  1112
val trace_rules = ref false;
wenzelm@5841
  1113
wenzelm@5841
  1114
fun print_rules rules i =
wenzelm@5841
  1115
  if not (! trace_rules) then ()
wenzelm@5841
  1116
  else
wenzelm@5841
  1117
    Pretty.writeln (Pretty.big_list ("trying standard rule(s) on goal #" ^ string_of_int i ^ ":")
wenzelm@5841
  1118
      (map Display.pretty_thm rules));
wenzelm@5841
  1119
wenzelm@5841
  1120
wenzelm@5841
  1121
(* single_tac *)
wenzelm@5841
  1122
wenzelm@6502
  1123
val imp_elim_netpair = insert (0, 0) ([], [imp_elim]) empty_netpair;
wenzelm@6502
  1124
val not_elim_netpair = insert (0, 0) ([], [Data.not_elim]) empty_netpair;
wenzelm@6502
  1125
wenzelm@6096
  1126
fun single_tac cs facts =
wenzelm@5841
  1127
  let
wenzelm@6955
  1128
    val CS {safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair, ...} = cs;
wenzelm@6502
  1129
    val nets = [imp_elim_netpair, safe0_netpair, safep_netpair,
wenzelm@6955
  1130
      not_elim_netpair, haz_netpair, dup_netpair, xtra_netpair];
wenzelm@6492
  1131
    val erules = find_erules facts nets;
wenzelm@5841
  1132
wenzelm@5841
  1133
    val tac = SUBGOAL (fn (goal, i) =>
wenzelm@5841
  1134
      let
wenzelm@5841
  1135
        val irules = find_rules (Logic.strip_assums_concl goal) nets;
wenzelm@5841
  1136
        val rules = erules @ irules;
wenzelm@5841
  1137
        val ruleq = Method.forward_chain facts rules;
wenzelm@5841
  1138
      in
wenzelm@5841
  1139
        print_rules rules i;
wenzelm@5841
  1140
        fn st => Seq.flat (Seq.map (fn rule => Tactic.rtac rule i st) ruleq) end);
wenzelm@5841
  1141
  in tac end;
wenzelm@5841
  1142
wenzelm@5841
  1143
val single = Method.METHOD (FIRSTGOAL o (fn facts => CLASET' (fn cs => single_tac cs facts)));
wenzelm@5841
  1144
wenzelm@5841
  1145
wenzelm@5841
  1146
wenzelm@6502
  1147
(** more proof methods **)
wenzelm@6502
  1148
wenzelm@6502
  1149
(* contradiction *)
wenzelm@6502
  1150
wenzelm@7132
  1151
val contradiction = Method.METHOD (fn facts =>
wenzelm@7132
  1152
  FIRSTGOAL (Method.same_tac facts THEN' contr_tac));
wenzelm@6502
  1153
wenzelm@6502
  1154
wenzelm@6502
  1155
(* automatic methods *)
wenzelm@5841
  1156
wenzelm@5927
  1157
val cla_modifiers =
wenzelm@6955
  1158
 [Args.$$$ destN -- bbang >> K xtra_dest_local,
wenzelm@6955
  1159
  Args.$$$ destN -- bang >> K haz_dest_local,
wenzelm@5927
  1160
  Args.$$$ destN >> K safe_dest_local,
wenzelm@6955
  1161
  Args.$$$ elimN -- bbang >> K xtra_elim_local,
wenzelm@5927
  1162
  Args.$$$ elimN -- bang >> K haz_elim_local,
wenzelm@5927
  1163
  Args.$$$ elimN >> K safe_elim_local,
wenzelm@6955
  1164
  Args.$$$ introN -- bbang >> K xtra_intro_local,
wenzelm@5927
  1165
  Args.$$$ introN -- bang >> K haz_intro_local,
wenzelm@5927
  1166
  Args.$$$ introN >> K safe_intro_local,
wenzelm@5927
  1167
  Args.$$$ delN >> K delrule_local];
wenzelm@5927
  1168
wenzelm@5927
  1169
val cla_args = Method.only_sectioned_args cla_modifiers;
wenzelm@5841
  1170
wenzelm@7132
  1171
fun cla_meth tac ctxt = Method.METHOD (fn facts =>
wenzelm@7132
  1172
  ALLGOALS (Method.same_tac facts) THEN tac (get_local_claset ctxt));
wenzelm@7132
  1173
wenzelm@7132
  1174
fun cla_meth' tac ctxt = Method.METHOD (fn facts =>
wenzelm@7132
  1175
  FIRSTGOAL (Method.same_tac facts THEN' tac (get_local_claset ctxt)));
wenzelm@5841
  1176
wenzelm@5885
  1177
val cla_method = cla_args o cla_meth;
wenzelm@5885
  1178
val cla_method' = cla_args o cla_meth';
wenzelm@5841
  1179
wenzelm@5841
  1180
wenzelm@5841
  1181
wenzelm@5841
  1182
(** setup_methods **)
wenzelm@5841
  1183
wenzelm@5841
  1184
val setup_methods = Method.add_methods
wenzelm@5841
  1185
 [("single", Method.no_args single, "apply standard rule (single step)"),
wenzelm@5841
  1186
  ("default", Method.no_args single, "apply standard rule (single step)"),
wenzelm@6502
  1187
  ("contradiction", Method.no_args contradiction, "proof by contradiction"),
wenzelm@7132
  1188
  ("safe_tac", cla_method safe_tac, "safe_tac (improper!)"),
wenzelm@7132
  1189
  ("safe_step_tac", cla_method' safe_step_tac, "safe_step_tac (improper!)"),
wenzelm@7132
  1190
  ("step_tac", cla_method' step_tac, "step_tac (improper!)"),
wenzelm@7004
  1191
  ("fast", cla_method' fast_tac, "classical prover (depth-first)"),
wenzelm@7004
  1192
  ("best", cla_method' best_tac, "classical prover (best-first)"),
wenzelm@7004
  1193
  ("slow", cla_method' slow_tac, "classical prover (depth-first, more backtracking)"),
wenzelm@7004
  1194
  ("slow_best", cla_method' slow_best_tac, "classical prover (best-first, more backtracking)")];
wenzelm@5841
  1195
wenzelm@5841
  1196
wenzelm@5841
  1197
wenzelm@5841
  1198
(** theory setup **)
wenzelm@5841
  1199
wenzelm@5841
  1200
(* FIXME claset theory data *)
wenzelm@5841
  1201
wenzelm@5841
  1202
val setup = [LocalClaset.init, setup_attrs, setup_methods];
wenzelm@5841
  1203
wenzelm@5841
  1204
wenzelm@5841
  1205
end;