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