src/Provers/classical.ML
author wenzelm
Sat Apr 16 16:15:37 2011 +0200 (2011-04-16)
changeset 42361 23f352990944
parent 41581 72a02e3dec7e
child 42439 9efdd0af15ac
permissions -rw-r--r--
modernized structure Proof_Context;
wenzelm@9938
     1
(*  Title:      Provers/classical.ML
wenzelm@9938
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     3
clasohm@0
     4
Theorem prover for classical reasoning, including predicate calculus, set
clasohm@0
     5
theory, etc.
clasohm@0
     6
wenzelm@9563
     7
Rules must be classified as intro, elim, safe, hazardous (unsafe).
clasohm@0
     8
clasohm@0
     9
A rule is unsafe unless it can be applied blindly without harmful results.
clasohm@0
    10
For a rule to be safe, its premises and conclusion should be logically
clasohm@0
    11
equivalent.  There should be no variables in the premises that are not in
clasohm@0
    12
the conclusion.
clasohm@0
    13
*)
clasohm@0
    14
wenzelm@4079
    15
(*higher precedence than := facilitates use of references*)
wenzelm@12376
    16
infix 4 addSIs addSEs addSDs addIs addEs addDs delrules
oheimb@4651
    17
  addSWrapper delSWrapper addWrapper delWrapper
oheimb@11181
    18
  addSbefore addSafter addbefore addafter
oheimb@5523
    19
  addD2 addE2 addSD2 addSE2;
wenzelm@4079
    20
wenzelm@4079
    21
wenzelm@4079
    22
(*should be a type abbreviation in signature CLASSICAL*)
wenzelm@4079
    23
type netpair = (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net;
oheimb@4651
    24
type wrapper = (int -> tactic) -> (int -> tactic);
wenzelm@4079
    25
clasohm@0
    26
signature CLASSICAL_DATA =
wenzelm@4079
    27
sig
wenzelm@26412
    28
  val imp_elim  : thm           (* P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R *)
wenzelm@26412
    29
  val not_elim  : thm           (* ~P ==> P ==> R *)
wenzelm@26412
    30
  val swap      : thm           (* ~ P ==> (~ R ==> P) ==> R *)
wenzelm@26412
    31
  val classical : thm           (* (~ P ==> P) ==> P *)
wenzelm@9938
    32
  val sizef     : thm -> int    (* size function for BEST_FIRST *)
clasohm@0
    33
  val hyp_subst_tacs: (int -> tactic) list
wenzelm@4079
    34
end;
clasohm@0
    35
wenzelm@5841
    36
signature BASIC_CLASSICAL =
wenzelm@4079
    37
sig
clasohm@0
    38
  type claset
wenzelm@4079
    39
  val empty_cs: claset
wenzelm@4079
    40
  val print_cs: claset -> unit
wenzelm@18534
    41
  val rep_cs:
wenzelm@4079
    42
    claset -> {safeIs: thm list, safeEs: thm list,
wenzelm@9938
    43
                 hazIs: thm list, hazEs: thm list,
wenzelm@10736
    44
                 swrappers: (string * wrapper) list,
wenzelm@9938
    45
                 uwrappers: (string * wrapper) list,
wenzelm@9938
    46
                 safe0_netpair: netpair, safep_netpair: netpair,
wenzelm@12401
    47
                 haz_netpair: netpair, dup_netpair: netpair,
wenzelm@33369
    48
                 xtra_netpair: Context_Rules.netpair}
wenzelm@9938
    49
  val merge_cs          : claset * claset -> claset
wenzelm@9938
    50
  val addDs             : claset * thm list -> claset
wenzelm@9938
    51
  val addEs             : claset * thm list -> claset
wenzelm@9938
    52
  val addIs             : claset * thm list -> claset
wenzelm@9938
    53
  val addSDs            : claset * thm list -> claset
wenzelm@9938
    54
  val addSEs            : claset * thm list -> claset
wenzelm@9938
    55
  val addSIs            : claset * thm list -> claset
wenzelm@9938
    56
  val delrules          : claset * thm list -> claset
wenzelm@9938
    57
  val addSWrapper       : claset * (string * wrapper) -> claset
wenzelm@9938
    58
  val delSWrapper       : claset *  string            -> claset
wenzelm@9938
    59
  val addWrapper        : claset * (string * wrapper) -> claset
wenzelm@9938
    60
  val delWrapper        : claset *  string            -> claset
wenzelm@9938
    61
  val addSbefore        : claset * (string * (int -> tactic)) -> claset
oheimb@11181
    62
  val addSafter         : claset * (string * (int -> tactic)) -> claset
wenzelm@9938
    63
  val addbefore         : claset * (string * (int -> tactic)) -> claset
oheimb@11181
    64
  val addafter          : claset * (string * (int -> tactic)) -> claset
oheimb@5523
    65
  val addD2             : claset * (string * thm) -> claset
oheimb@5523
    66
  val addE2             : claset * (string * thm) -> claset
oheimb@5523
    67
  val addSD2            : claset * (string * thm) -> claset
oheimb@5523
    68
  val addSE2            : claset * (string * thm) -> claset
wenzelm@9938
    69
  val appSWrappers      : claset -> wrapper
wenzelm@9938
    70
  val appWrappers       : claset -> wrapper
lcp@982
    71
wenzelm@32148
    72
  val global_claset_of  : theory -> claset
wenzelm@32148
    73
  val claset_of         : Proof.context -> claset
wenzelm@4079
    74
wenzelm@9938
    75
  val fast_tac          : claset -> int -> tactic
wenzelm@9938
    76
  val slow_tac          : claset -> int -> tactic
wenzelm@32740
    77
  val weight_ASTAR      : int Unsynchronized.ref
wenzelm@9938
    78
  val astar_tac         : claset -> int -> tactic
wenzelm@9938
    79
  val slow_astar_tac    : claset -> int -> tactic
wenzelm@9938
    80
  val best_tac          : claset -> int -> tactic
wenzelm@9938
    81
  val first_best_tac    : claset -> int -> tactic
wenzelm@9938
    82
  val slow_best_tac     : claset -> int -> tactic
wenzelm@9938
    83
  val depth_tac         : claset -> int -> int -> tactic
wenzelm@9938
    84
  val deepen_tac        : claset -> int -> int -> tactic
paulson@1587
    85
wenzelm@9938
    86
  val contr_tac         : int -> tactic
wenzelm@9938
    87
  val dup_elim          : thm -> thm
wenzelm@9938
    88
  val dup_intr          : thm -> thm
wenzelm@9938
    89
  val dup_step_tac      : claset -> int -> tactic
wenzelm@9938
    90
  val eq_mp_tac         : int -> tactic
wenzelm@9938
    91
  val haz_step_tac      : claset -> int -> tactic
wenzelm@9938
    92
  val joinrules         : thm list * thm list -> (bool * thm) list
wenzelm@9938
    93
  val mp_tac            : int -> tactic
wenzelm@9938
    94
  val safe_tac          : claset -> tactic
wenzelm@9938
    95
  val safe_steps_tac    : claset -> int -> tactic
wenzelm@9938
    96
  val safe_step_tac     : claset -> int -> tactic
wenzelm@9938
    97
  val clarify_tac       : claset -> int -> tactic
wenzelm@9938
    98
  val clarify_step_tac  : claset -> int -> tactic
wenzelm@9938
    99
  val step_tac          : claset -> int -> tactic
wenzelm@9938
   100
  val slow_step_tac     : claset -> int -> tactic
wenzelm@9938
   101
  val swapify           : thm list -> thm list
wenzelm@9938
   102
  val swap_res_tac      : thm list -> int -> tactic
wenzelm@9938
   103
  val inst_step_tac     : claset -> int -> tactic
wenzelm@9938
   104
  val inst0_step_tac    : claset -> int -> tactic
wenzelm@9938
   105
  val instp_step_tac    : claset -> int -> tactic
wenzelm@4079
   106
end;
berghofe@1724
   107
wenzelm@5841
   108
signature CLASSICAL =
wenzelm@5841
   109
sig
wenzelm@5841
   110
  include BASIC_CLASSICAL
wenzelm@18534
   111
  val classical_rule: thm -> thm
wenzelm@15036
   112
  val add_context_safe_wrapper: string * (Proof.context -> wrapper) -> theory -> theory
wenzelm@15036
   113
  val del_context_safe_wrapper: string -> theory -> theory
wenzelm@15036
   114
  val add_context_unsafe_wrapper: string * (Proof.context -> wrapper) -> theory -> theory
wenzelm@15036
   115
  val del_context_unsafe_wrapper: string -> theory -> theory
wenzelm@32261
   116
  val get_claset: Proof.context -> claset
wenzelm@32261
   117
  val put_claset: claset -> Proof.context -> Proof.context
wenzelm@24021
   118
  val get_cs: Context.generic -> claset
wenzelm@24021
   119
  val map_cs: (claset -> claset) -> Context.generic -> Context.generic
wenzelm@18728
   120
  val safe_dest: int option -> attribute
wenzelm@18728
   121
  val safe_elim: int option -> attribute
wenzelm@18728
   122
  val safe_intro: int option -> attribute
wenzelm@18728
   123
  val haz_dest: int option -> attribute
wenzelm@18728
   124
  val haz_elim: int option -> attribute
wenzelm@18728
   125
  val haz_intro: int option -> attribute
wenzelm@18728
   126
  val rule_del: attribute
wenzelm@30513
   127
  val cla_modifiers: Method.modifier parser list
wenzelm@35613
   128
  val cla_meth: (claset -> tactic) -> Proof.context -> Proof.method
wenzelm@35613
   129
  val cla_meth': (claset -> int -> tactic) -> Proof.context -> Proof.method
wenzelm@30541
   130
  val cla_method: (claset -> tactic) -> (Proof.context -> Proof.method) context_parser
wenzelm@30541
   131
  val cla_method': (claset -> int -> tactic) -> (Proof.context -> Proof.method) context_parser
wenzelm@18708
   132
  val setup: theory -> theory
wenzelm@5841
   133
end;
wenzelm@5841
   134
clasohm@0
   135
wenzelm@5927
   136
functor ClassicalFun(Data: CLASSICAL_DATA): CLASSICAL =
clasohm@0
   137
struct
clasohm@0
   138
wenzelm@7354
   139
local open Data in
clasohm@0
   140
wenzelm@18534
   141
(** classical elimination rules **)
wenzelm@18534
   142
wenzelm@18534
   143
(*
wenzelm@18534
   144
Classical reasoning requires stronger elimination rules.  For
wenzelm@18534
   145
instance, make_elim of Pure transforms the HOL rule injD into
wenzelm@18534
   146
wenzelm@18534
   147
    [| inj f; f x = f y; x = y ==> PROP W |] ==> PROP W
wenzelm@18534
   148
wenzelm@26938
   149
Such rules can cause fast_tac to fail and blast_tac to report "PROOF
wenzelm@18534
   150
FAILED"; classical_rule will strenthen this to
wenzelm@18534
   151
wenzelm@18534
   152
    [| inj f; ~ W ==> f x = f y; x = y ==> W |] ==> W
wenzelm@18534
   153
*)
wenzelm@18534
   154
wenzelm@18534
   155
fun classical_rule rule =
wenzelm@41581
   156
  if is_some (Object_Logic.elim_concl rule) then
wenzelm@18534
   157
    let
wenzelm@18534
   158
      val rule' = rule RS classical;
wenzelm@18534
   159
      val concl' = Thm.concl_of rule';
wenzelm@18534
   160
      fun redundant_hyp goal =
wenzelm@19257
   161
        concl' aconv Logic.strip_assums_concl goal orelse
wenzelm@18534
   162
          (case Logic.strip_assums_hyp goal of
wenzelm@18534
   163
            hyp :: hyps => exists (fn t => t aconv hyp) hyps
wenzelm@18534
   164
          | _ => false);
wenzelm@18534
   165
      val rule'' =
wenzelm@18534
   166
        rule' |> ALLGOALS (SUBGOAL (fn (goal, i) =>
wenzelm@18534
   167
          if i = 1 orelse redundant_hyp goal
wenzelm@18534
   168
          then Tactic.etac thin_rl i
wenzelm@18534
   169
          else all_tac))
wenzelm@18534
   170
        |> Seq.hd
wenzelm@21963
   171
        |> Drule.zero_var_indexes;
wenzelm@22360
   172
    in if Thm.equiv_thm (rule, rule'') then rule else rule'' end
wenzelm@18534
   173
  else rule;
wenzelm@18534
   174
wenzelm@23594
   175
(*flatten nested meta connectives in prems*)
wenzelm@35625
   176
val flat_rule = Conv.fconv_rule (Conv.prems_conv ~1 Object_Logic.atomize_prems);
wenzelm@18534
   177
wenzelm@18534
   178
paulson@1800
   179
(*** Useful tactics for classical reasoning ***)
clasohm@0
   180
wenzelm@10736
   181
(*Prove goal that assumes both P and ~P.
paulson@4392
   182
  No backtracking if it finds an equal assumption.  Perhaps should call
paulson@4392
   183
  ematch_tac instead of eresolve_tac, but then cannot prove ZF/cantor.*)
wenzelm@10736
   184
val contr_tac = eresolve_tac [not_elim]  THEN'
paulson@4392
   185
                (eq_assume_tac ORELSE' assume_tac);
clasohm@0
   186
lcp@681
   187
(*Finds P-->Q and P in the assumptions, replaces implication by Q.
lcp@681
   188
  Could do the same thing for P<->Q and P... *)
wenzelm@26412
   189
fun mp_tac i = eresolve_tac [not_elim, Data.imp_elim] i  THEN  assume_tac i;
clasohm@0
   190
clasohm@0
   191
(*Like mp_tac but instantiates no variables*)
wenzelm@26412
   192
fun eq_mp_tac i = ematch_tac [not_elim, Data.imp_elim] i  THEN  eq_assume_tac i;
clasohm@0
   193
clasohm@0
   194
(*Creates rules to eliminate ~A, from rules to introduce A*)
wenzelm@26412
   195
fun swapify intrs = intrs RLN (2, [Data.swap]);
wenzelm@30528
   196
val swapped = Thm.rule_attribute (fn _ => fn th => th RSN (2, Data.swap));
clasohm@0
   197
clasohm@0
   198
(*Uses introduction rules in the normal way, or on negated assumptions,
clasohm@0
   199
  trying rules in order. *)
wenzelm@10736
   200
fun swap_res_tac rls =
wenzelm@33339
   201
    let fun addrl rl brls = (false, rl) :: (true, rl RSN (2, Data.swap)) :: brls
wenzelm@10736
   202
    in  assume_tac      ORELSE'
wenzelm@10736
   203
        contr_tac       ORELSE'
wenzelm@33339
   204
        biresolve_tac (fold_rev addrl rls [])
clasohm@0
   205
    end;
clasohm@0
   206
lcp@681
   207
(*Duplication of hazardous rules, for complete provers*)
paulson@2689
   208
fun dup_intr th = zero_var_indexes (th RS classical);
lcp@681
   209
wenzelm@6967
   210
fun dup_elim th =
wenzelm@36546
   211
  let
wenzelm@36546
   212
    val rl = (th RSN (2, revcut_rl)) |> Thm.assumption 2 |> Seq.hd;
wenzelm@42361
   213
    val ctxt = Proof_Context.init_global (Thm.theory_of_thm rl);
wenzelm@36546
   214
  in rule_by_tactic ctxt (TRYALL (etac revcut_rl)) rl end;
wenzelm@36546
   215
lcp@1073
   216
paulson@1800
   217
(**** Classical rule sets ****)
clasohm@0
   218
clasohm@0
   219
datatype claset =
wenzelm@12401
   220
  CS of {safeIs         : thm list,                (*safe introduction rules*)
wenzelm@12401
   221
         safeEs         : thm list,                (*safe elimination rules*)
wenzelm@12401
   222
         hazIs          : thm list,                (*unsafe introduction rules*)
wenzelm@12401
   223
         hazEs          : thm list,                (*unsafe elimination rules*)
wenzelm@12401
   224
         swrappers      : (string * wrapper) list, (*for transforming safe_step_tac*)
wenzelm@9938
   225
         uwrappers      : (string * wrapper) list, (*for transforming step_tac*)
wenzelm@12401
   226
         safe0_netpair  : netpair,                 (*nets for trivial cases*)
wenzelm@12401
   227
         safep_netpair  : netpair,                 (*nets for >0 subgoals*)
wenzelm@12401
   228
         haz_netpair    : netpair,                 (*nets for unsafe rules*)
wenzelm@12401
   229
         dup_netpair    : netpair,                 (*nets for duplication*)
wenzelm@33369
   230
         xtra_netpair   : Context_Rules.netpair};  (*nets for extra rules*)
clasohm@0
   231
lcp@1073
   232
(*Desired invariants are
wenzelm@9938
   233
        safe0_netpair = build safe0_brls,
wenzelm@9938
   234
        safep_netpair = build safep_brls,
wenzelm@9938
   235
        haz_netpair = build (joinrules(hazIs, hazEs)),
wenzelm@10736
   236
        dup_netpair = build (joinrules(map dup_intr hazIs,
wenzelm@12376
   237
                                       map dup_elim hazEs))
lcp@1073
   238
wenzelm@10736
   239
where build = build_netpair(Net.empty,Net.empty),
lcp@1073
   240
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   241
      safep_brls contains all brules that generate 1 or more new subgoals.
wenzelm@4079
   242
The theorem lists are largely comments, though they are used in merge_cs and print_cs.
lcp@1073
   243
Nets must be built incrementally, to save space and time.
lcp@1073
   244
*)
clasohm@0
   245
wenzelm@6502
   246
val empty_netpair = (Net.empty, Net.empty);
wenzelm@6502
   247
wenzelm@10736
   248
val empty_cs =
wenzelm@9938
   249
  CS{safeIs     = [],
wenzelm@9938
   250
     safeEs     = [],
wenzelm@9938
   251
     hazIs      = [],
wenzelm@9938
   252
     hazEs      = [],
oheimb@4651
   253
     swrappers  = [],
oheimb@4651
   254
     uwrappers  = [],
wenzelm@6502
   255
     safe0_netpair = empty_netpair,
wenzelm@6502
   256
     safep_netpair = empty_netpair,
wenzelm@6502
   257
     haz_netpair   = empty_netpair,
wenzelm@6955
   258
     dup_netpair   = empty_netpair,
wenzelm@6955
   259
     xtra_netpair  = empty_netpair};
clasohm@0
   260
wenzelm@15036
   261
fun print_cs (CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, ...}) =
wenzelm@32091
   262
  let val pretty_thms = map Display.pretty_thm_without_context in
wenzelm@9760
   263
    [Pretty.big_list "safe introduction rules (intro!):" (pretty_thms safeIs),
wenzelm@9760
   264
      Pretty.big_list "introduction rules (intro):" (pretty_thms hazIs),
wenzelm@9760
   265
      Pretty.big_list "safe elimination rules (elim!):" (pretty_thms safeEs),
wenzelm@15036
   266
      Pretty.big_list "elimination rules (elim):" (pretty_thms hazEs),
wenzelm@15036
   267
      Pretty.strs ("safe wrappers:" :: map #1 swrappers),
wenzelm@15036
   268
      Pretty.strs ("unsafe wrappers:" :: map #1 uwrappers)]
wenzelm@8727
   269
    |> Pretty.chunks |> Pretty.writeln
wenzelm@3546
   270
  end;
clasohm@0
   271
oheimb@4653
   272
fun rep_cs (CS args) = args;
lcp@1073
   273
haftmann@22674
   274
fun appSWrappers (CS {swrappers, ...}) = fold snd swrappers;
haftmann@22674
   275
fun appWrappers  (CS {uwrappers, ...}) = fold snd uwrappers;
lcp@1073
   276
wenzelm@4079
   277
paulson@1800
   278
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   279
lcp@1073
   280
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   281
***)
clasohm@0
   282
wenzelm@12376
   283
(*For use with biresolve_tac.  Combines intro rules with swap to handle negated
lcp@1073
   284
  assumptions.  Pairs elim rules with true. *)
wenzelm@12376
   285
fun joinrules (intrs, elims) =
paulson@18557
   286
  (map (pair true) (elims @ swapify intrs)) @ map (pair false) intrs;
wenzelm@12376
   287
wenzelm@12401
   288
fun joinrules' (intrs, elims) =
paulson@18557
   289
  map (pair true) elims @ map (pair false) intrs;
lcp@1073
   290
wenzelm@10736
   291
(*Priority: prefer rules with fewest subgoals,
paulson@1231
   292
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   293
fun tag_brls k [] = []
lcp@1073
   294
  | tag_brls k (brl::brls) =
wenzelm@10736
   295
      (1000000*subgoals_of_brl brl + k, brl) ::
lcp@1073
   296
      tag_brls (k+1) brls;
lcp@1073
   297
wenzelm@12401
   298
fun tag_brls' _ _ [] = []
wenzelm@12401
   299
  | tag_brls' w k (brl::brls) = ((w, k), brl) :: tag_brls' w (k + 1) brls;
wenzelm@10736
   300
wenzelm@23178
   301
fun insert_tagged_list rls = fold_rev Tactic.insert_tagged_brl rls;
lcp@1073
   302
lcp@1073
   303
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   304
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   305
  new insertions the lowest priority.*)
wenzelm@12376
   306
fun insert (nI, nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
wenzelm@12401
   307
fun insert' w (nI, nE) = insert_tagged_list o tag_brls' w (~(nI + nE)) o joinrules';
lcp@1073
   308
wenzelm@23178
   309
fun delete_tagged_list rls = fold_rev Tactic.delete_tagged_brl rls;
wenzelm@12362
   310
fun delete x = delete_tagged_list (joinrules x);
wenzelm@12401
   311
fun delete' x = delete_tagged_list (joinrules' x);
paulson@1800
   312
wenzelm@22360
   313
val mem_thm = member Thm.eq_thm_prop
wenzelm@22360
   314
and rem_thm = remove Thm.eq_thm_prop;
paulson@2813
   315
paulson@1927
   316
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   317
  is still allowed.*)
wenzelm@12376
   318
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@32091
   319
  if mem_thm safeIs th then
wenzelm@32091
   320
    warning ("Rule already declared as safe introduction (intro!)\n" ^
wenzelm@32091
   321
      Display.string_of_thm_without_context th)
wenzelm@18691
   322
  else if mem_thm safeEs th then
wenzelm@32091
   323
    warning ("Rule already declared as safe elimination (elim!)\n" ^
wenzelm@32091
   324
      Display.string_of_thm_without_context th)
wenzelm@18691
   325
  else if mem_thm hazIs th then
wenzelm@32091
   326
    warning ("Rule already declared as introduction (intro)\n" ^
wenzelm@32091
   327
      Display.string_of_thm_without_context th)
wenzelm@18691
   328
  else if mem_thm hazEs th then
wenzelm@32091
   329
    warning ("Rule already declared as elimination (elim)\n" ^
wenzelm@32091
   330
      Display.string_of_thm_without_context th)
paulson@1927
   331
  else ();
paulson@1927
   332
wenzelm@12376
   333
paulson@1800
   334
(*** Safe rules ***)
lcp@982
   335
wenzelm@18691
   336
fun addSI w th
wenzelm@18534
   337
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   338
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   339
  if mem_thm safeIs th then
wenzelm@32091
   340
    (warning ("Ignoring duplicate safe introduction (intro!)\n" ^
wenzelm@32091
   341
      Display.string_of_thm_without_context th); cs)
paulson@1927
   342
  else
wenzelm@23594
   343
  let val th' = flat_rule th
wenzelm@23594
   344
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@23594
   345
          List.partition Thm.no_prems [th']
paulson@1927
   346
      val nI = length safeIs + 1
lcp@1073
   347
      and nE = length safeEs
paulson@1927
   348
  in warn_dup th cs;
wenzelm@9938
   349
     CS{safeIs  = th::safeIs,
lcp@1073
   350
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
wenzelm@9938
   351
        safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
wenzelm@9938
   352
        safeEs  = safeEs,
wenzelm@9938
   353
        hazIs   = hazIs,
wenzelm@9938
   354
        hazEs   = hazEs,
wenzelm@9938
   355
        swrappers    = swrappers,
wenzelm@9938
   356
        uwrappers    = uwrappers,
wenzelm@9938
   357
        haz_netpair  = haz_netpair,
wenzelm@9938
   358
        dup_netpair  = dup_netpair,
wenzelm@18691
   359
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([th], []) xtra_netpair}
lcp@1073
   360
  end;
lcp@1073
   361
wenzelm@18691
   362
fun addSE w th
wenzelm@18534
   363
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   364
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   365
  if mem_thm safeEs th then
wenzelm@32091
   366
    (warning ("Ignoring duplicate safe elimination (elim!)\n" ^
wenzelm@32091
   367
        Display.string_of_thm_without_context th); cs)
paulson@18557
   368
  else if has_fewer_prems 1 th then
wenzelm@32960
   369
        error ("Ill-formed elimination rule\n" ^ Display.string_of_thm_without_context th)
paulson@1927
   370
  else
wenzelm@18534
   371
  let
wenzelm@23594
   372
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   373
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@18534
   374
          List.partition (fn rl => nprems_of rl=1) [th']
lcp@1073
   375
      val nI = length safeIs
paulson@1927
   376
      and nE = length safeEs + 1
paulson@1927
   377
  in warn_dup th cs;
wenzelm@9938
   378
     CS{safeEs  = th::safeEs,
lcp@1073
   379
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
wenzelm@9938
   380
        safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
wenzelm@9938
   381
        safeIs  = safeIs,
wenzelm@9938
   382
        hazIs   = hazIs,
wenzelm@9938
   383
        hazEs   = hazEs,
wenzelm@9938
   384
        swrappers    = swrappers,
wenzelm@9938
   385
        uwrappers    = uwrappers,
wenzelm@9938
   386
        haz_netpair  = haz_netpair,
wenzelm@9938
   387
        dup_netpair  = dup_netpair,
wenzelm@18691
   388
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   389
  end;
clasohm@0
   390
wenzelm@18691
   391
fun cs addSIs ths = fold_rev (addSI NONE) ths cs;
wenzelm@18691
   392
fun cs addSEs ths = fold_rev (addSE NONE) ths cs;
paulson@1927
   393
paulson@21689
   394
fun make_elim th =
paulson@18557
   395
    if has_fewer_prems 1 th then
wenzelm@32960
   396
          error ("Ill-formed destruction rule\n" ^ Display.string_of_thm_without_context th)
paulson@21689
   397
    else Tactic.make_elim th;
paulson@17084
   398
paulson@21689
   399
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   400
lcp@1073
   401
paulson@1800
   402
(*** Hazardous (unsafe) rules ***)
clasohm@0
   403
wenzelm@18691
   404
fun addI w th
wenzelm@18534
   405
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   406
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   407
  if mem_thm hazIs th then
wenzelm@32091
   408
    (warning ("Ignoring duplicate introduction (intro)\n" ^
wenzelm@32091
   409
        Display.string_of_thm_without_context th); cs)
paulson@1927
   410
  else
wenzelm@23594
   411
  let val th' = flat_rule th
wenzelm@23594
   412
      val nI = length hazIs + 1
lcp@1073
   413
      and nE = length hazEs
paulson@1927
   414
  in warn_dup th cs;
wenzelm@9938
   415
     CS{hazIs   = th::hazIs,
wenzelm@23594
   416
        haz_netpair = insert (nI,nE) ([th'], []) haz_netpair,
wenzelm@23594
   417
        dup_netpair = insert (nI,nE) (map dup_intr [th'], []) dup_netpair,
wenzelm@10736
   418
        safeIs  = safeIs,
wenzelm@9938
   419
        safeEs  = safeEs,
wenzelm@9938
   420
        hazEs   = hazEs,
wenzelm@9938
   421
        swrappers     = swrappers,
wenzelm@9938
   422
        uwrappers     = uwrappers,
wenzelm@9938
   423
        safe0_netpair = safe0_netpair,
wenzelm@9938
   424
        safep_netpair = safep_netpair,
wenzelm@18691
   425
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([th], []) xtra_netpair}
paulson@18557
   426
  end
paulson@18557
   427
  handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@32091
   428
    error ("Ill-formed introduction rule\n" ^ Display.string_of_thm_without_context th);
lcp@1073
   429
wenzelm@18691
   430
fun addE w th
wenzelm@18534
   431
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   432
            safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   433
  if mem_thm hazEs th then
wenzelm@32091
   434
    (warning ("Ignoring duplicate elimination (elim)\n" ^
wenzelm@32091
   435
        Display.string_of_thm_without_context th); cs)
paulson@18557
   436
  else if has_fewer_prems 1 th then
wenzelm@32960
   437
        error("Ill-formed elimination rule\n" ^ Display.string_of_thm_without_context th)
paulson@1927
   438
  else
wenzelm@18534
   439
  let
wenzelm@23594
   440
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   441
      val nI = length hazIs
paulson@1927
   442
      and nE = length hazEs + 1
paulson@1927
   443
  in warn_dup th cs;
wenzelm@9938
   444
     CS{hazEs   = th::hazEs,
wenzelm@18534
   445
        haz_netpair = insert (nI,nE) ([], [th']) haz_netpair,
wenzelm@18534
   446
        dup_netpair = insert (nI,nE) ([], map dup_elim [th']) dup_netpair,
wenzelm@10736
   447
        safeIs  = safeIs,
wenzelm@9938
   448
        safeEs  = safeEs,
wenzelm@9938
   449
        hazIs   = hazIs,
wenzelm@9938
   450
        swrappers     = swrappers,
wenzelm@9938
   451
        uwrappers     = uwrappers,
wenzelm@9938
   452
        safe0_netpair = safe0_netpair,
wenzelm@9938
   453
        safep_netpair = safep_netpair,
wenzelm@18691
   454
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   455
  end;
clasohm@0
   456
wenzelm@18691
   457
fun cs addIs ths = fold_rev (addI NONE) ths cs;
wenzelm@18691
   458
fun cs addEs ths = fold_rev (addE NONE) ths cs;
paulson@1927
   459
paulson@21689
   460
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   461
lcp@1073
   462
wenzelm@10736
   463
(*** Deletion of rules
paulson@1800
   464
     Working out what to delete, requires repeating much of the code used
wenzelm@9938
   465
        to insert.
paulson@1800
   466
***)
paulson@1800
   467
wenzelm@10736
   468
fun delSI th
wenzelm@12376
   469
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   470
                    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   471
 if mem_thm safeIs th then
wenzelm@23594
   472
   let val th' = flat_rule th
wenzelm@23594
   473
       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@18691
   476
         safeIs = rem_thm th safeIs,
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}) =
wenzelm@18691
   491
  if mem_thm safeEs th then
wenzelm@18534
   492
    let
wenzelm@23594
   493
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   494
      val (safe0_rls, safep_rls) = List.partition (fn rl => nprems_of rl=1) [th']
wenzelm@18534
   495
    in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
wenzelm@9938
   496
         safep_netpair = delete ([], safep_rls) safep_netpair,
wenzelm@9938
   497
         safeIs = safeIs,
wenzelm@18691
   498
         safeEs = rem_thm th safeEs,
wenzelm@9938
   499
         hazIs  = hazIs,
wenzelm@9938
   500
         hazEs  = hazEs,
wenzelm@9938
   501
         swrappers    = swrappers,
wenzelm@9938
   502
         uwrappers    = uwrappers,
wenzelm@9938
   503
         haz_netpair  = haz_netpair,
wenzelm@9938
   504
         dup_netpair  = dup_netpair,
wenzelm@12401
   505
         xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@18534
   506
    end
wenzelm@18534
   507
  else cs;
paulson@1800
   508
paulson@1800
   509
paulson@2813
   510
fun delI th
wenzelm@12376
   511
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   512
                   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   513
 if mem_thm hazIs th then
wenzelm@23594
   514
    let val th' = flat_rule th
wenzelm@23594
   515
    in CS{haz_netpair = delete ([th'], []) haz_netpair,
wenzelm@23594
   516
        dup_netpair = delete ([dup_intr th'], []) dup_netpair,
wenzelm@10736
   517
        safeIs  = safeIs,
wenzelm@9938
   518
        safeEs  = safeEs,
wenzelm@18691
   519
        hazIs   = rem_thm th hazIs,
wenzelm@9938
   520
        hazEs   = hazEs,
wenzelm@9938
   521
        swrappers     = swrappers,
wenzelm@9938
   522
        uwrappers     = uwrappers,
wenzelm@9938
   523
        safe0_netpair = safe0_netpair,
wenzelm@9938
   524
        safep_netpair = safep_netpair,
wenzelm@12401
   525
        xtra_netpair = delete' ([th], []) xtra_netpair}
wenzelm@23594
   526
    end
paulson@18557
   527
 else cs
paulson@18557
   528
 handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@32091
   529
   error ("Ill-formed introduction rule\n" ^ Display.string_of_thm_without_context th);
paulson@18557
   530
paulson@1800
   531
paulson@2813
   532
fun delE th
wenzelm@12376
   533
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   534
                   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@23594
   535
 if mem_thm hazEs th then
wenzelm@23594
   536
   let val th' = classical_rule (flat_rule th)
wenzelm@23594
   537
   in CS{haz_netpair = delete ([], [th']) haz_netpair,
wenzelm@18534
   538
        dup_netpair = delete ([], [dup_elim th']) dup_netpair,
wenzelm@10736
   539
        safeIs  = safeIs,
wenzelm@9938
   540
        safeEs  = safeEs,
wenzelm@9938
   541
        hazIs   = hazIs,
wenzelm@18691
   542
        hazEs   = rem_thm th hazEs,
wenzelm@9938
   543
        swrappers     = swrappers,
wenzelm@9938
   544
        uwrappers     = uwrappers,
wenzelm@9938
   545
        safe0_netpair = safe0_netpair,
wenzelm@9938
   546
        safep_netpair = safep_netpair,
wenzelm@12401
   547
        xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@23594
   548
   end
wenzelm@23594
   549
 else cs;
paulson@1800
   550
paulson@2813
   551
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
wenzelm@18534
   552
fun delrule th (cs as CS {safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@18534
   553
  let val th' = Tactic.make_elim th in
wenzelm@18691
   554
    if mem_thm safeIs th orelse mem_thm safeEs th orelse
wenzelm@18691
   555
      mem_thm hazIs th orelse mem_thm hazEs th orelse
wenzelm@18691
   556
      mem_thm safeEs th' orelse mem_thm hazEs th'
wenzelm@12376
   557
    then delSI th (delSE th (delI th (delE th (delSE th' (delE th' cs)))))
wenzelm@32091
   558
    else (warning ("Undeclared classical rule\n" ^ Display.string_of_thm_without_context th); cs)
wenzelm@9938
   559
  end;
paulson@1800
   560
wenzelm@18534
   561
fun cs delrules ths = fold delrule ths cs;
paulson@1800
   562
paulson@1800
   563
oheimb@4767
   564
(*** Modifying the wrapper tacticals ***)
haftmann@22674
   565
fun map_swrappers f
haftmann@22674
   566
  (CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   567
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   568
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   569
    swrappers = f swrappers, uwrappers = 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
haftmann@22674
   573
fun map_uwrappers f
haftmann@22674
   574
  (CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   575
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   576
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   577
    swrappers = swrappers, uwrappers = f uwrappers,
oheimb@4767
   578
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   579
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   580
haftmann@22674
   581
fun update_warn msg (p as (key : string, _)) xs =
haftmann@22674
   582
  (if AList.defined (op =) xs key then warning msg else ();
haftmann@22674
   583
    AList.update (op =) p xs);
haftmann@22674
   584
haftmann@22674
   585
fun delete_warn msg (key : string) xs =
haftmann@22674
   586
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@22674
   587
    else (warning msg; xs);
lcp@982
   588
oheimb@4651
   589
(*Add/replace a safe wrapper*)
haftmann@22674
   590
fun cs addSWrapper new_swrapper = map_swrappers
haftmann@22674
   591
  (update_warn ("Overwriting safe wrapper " ^ fst new_swrapper) new_swrapper) cs;
oheimb@4651
   592
oheimb@4651
   593
(*Add/replace an unsafe wrapper*)
haftmann@22674
   594
fun cs addWrapper new_uwrapper = map_uwrappers
haftmann@22674
   595
  (update_warn ("Overwriting unsafe wrapper " ^ fst new_uwrapper) new_uwrapper) cs;
lcp@982
   596
oheimb@4651
   597
(*Remove a safe wrapper*)
haftmann@22674
   598
fun cs delSWrapper name = map_swrappers
haftmann@22674
   599
  (delete_warn ("No such safe wrapper in claset: " ^ name) name) cs;
lcp@982
   600
oheimb@4651
   601
(*Remove an unsafe wrapper*)
haftmann@22674
   602
fun cs delWrapper name = map_uwrappers
haftmann@22674
   603
  (delete_warn ("No such unsafe wrapper in claset: " ^ name) name) cs;
lcp@982
   604
oheimb@11168
   605
(* compose a safe tactic alternatively before/after safe_step_tac *)
wenzelm@10736
   606
fun cs addSbefore  (name,    tac1) =
oheimb@5523
   607
    cs addSWrapper (name, fn tac2 => tac1 ORELSE' tac2);
oheimb@11181
   608
fun cs addSafter   (name,    tac2) =
oheimb@5523
   609
    cs addSWrapper (name, fn tac1 => tac1 ORELSE' tac2);
lcp@982
   610
oheimb@11168
   611
(*compose a tactic alternatively before/after the step tactic *)
wenzelm@10736
   612
fun cs addbefore   (name,    tac1) =
oheimb@5523
   613
    cs addWrapper  (name, fn tac2 => tac1 APPEND' tac2);
oheimb@11181
   614
fun cs addafter    (name,    tac2) =
oheimb@5523
   615
    cs addWrapper  (name, fn tac1 => tac1 APPEND' tac2);
oheimb@4767
   616
wenzelm@10736
   617
fun cs addD2     (name, thm) =
oheimb@11181
   618
    cs addafter  (name, datac thm 1);
wenzelm@10736
   619
fun cs addE2     (name, thm) =
oheimb@11181
   620
    cs addafter  (name, eatac thm 1);
oheimb@11181
   621
fun cs addSD2    (name, thm) =
oheimb@11181
   622
    cs addSafter (name, dmatch_tac [thm] THEN' eq_assume_tac);
oheimb@11181
   623
fun cs addSE2    (name, thm) =
oheimb@11181
   624
    cs addSafter (name, ematch_tac [thm] THEN' eq_assume_tac);
lcp@982
   625
paulson@1711
   626
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   627
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   628
  treatment of priority might get muddled up.*)
haftmann@22674
   629
fun merge_cs (cs as CS {safeIs, safeEs, hazIs, hazEs, ...},
wenzelm@24358
   630
    cs' as CS {safeIs = safeIs2, safeEs = safeEs2, hazIs = hazIs2, hazEs = hazEs2,
haftmann@22674
   631
      swrappers, uwrappers, ...}) =
wenzelm@24358
   632
  if pointer_eq (cs, cs') then cs
wenzelm@24358
   633
  else
wenzelm@24358
   634
    let
wenzelm@24358
   635
      val safeIs' = fold rem_thm safeIs safeIs2;
wenzelm@24358
   636
      val safeEs' = fold rem_thm safeEs safeEs2;
wenzelm@24358
   637
      val hazIs' = fold rem_thm hazIs hazIs2;
wenzelm@24358
   638
      val hazEs' = fold rem_thm hazEs hazEs2;
wenzelm@24358
   639
      val cs1   = cs addSIs safeIs'
wenzelm@24358
   640
                     addSEs safeEs'
wenzelm@24358
   641
                     addIs  hazIs'
wenzelm@24358
   642
                     addEs  hazEs';
wenzelm@24358
   643
      val cs2 = map_swrappers
wenzelm@24358
   644
        (fn ws => AList.merge (op =) (K true) (ws, swrappers)) cs1;
wenzelm@24358
   645
      val cs3 = map_uwrappers
wenzelm@24358
   646
        (fn ws => AList.merge (op =) (K true) (ws, uwrappers)) cs2;
wenzelm@24358
   647
    in cs3 end;
paulson@1711
   648
lcp@982
   649
paulson@1800
   650
(**** Simple tactics for theorem proving ****)
clasohm@0
   651
clasohm@0
   652
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   653
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   654
  appSWrappers cs (FIRST' [
wenzelm@9938
   655
        eq_assume_tac,
wenzelm@9938
   656
        eq_mp_tac,
wenzelm@9938
   657
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   658
        FIRST' hyp_subst_tacs,
wenzelm@9938
   659
        bimatch_from_nets_tac safep_netpair]);
clasohm@0
   660
oheimb@5757
   661
(*Repeatedly attack a subgoal using safe inferences -- it's deterministic!*)
wenzelm@10736
   662
fun safe_steps_tac cs = REPEAT_DETERM1 o
wenzelm@9938
   663
        (fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
oheimb@5757
   664
clasohm@0
   665
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@5757
   666
fun safe_tac cs = REPEAT_DETERM1 (FIRSTGOAL (safe_steps_tac cs));
lcp@747
   667
paulson@3705
   668
paulson@3705
   669
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   670
paulson@3705
   671
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   672
paulson@3705
   673
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   674
  create precisely n subgoals.*)
wenzelm@10736
   675
fun n_bimatch_from_nets_tac n =
wenzelm@33317
   676
    biresolution_from_nets_tac (order_list o filter (nsubgoalsP n)) true;
paulson@3705
   677
paulson@3705
   678
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   679
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   680
paulson@3705
   681
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   682
fun bimatch2_tac netpair i =
paulson@3705
   683
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   684
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   685
paulson@3705
   686
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   687
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   688
  appSWrappers cs (FIRST' [
wenzelm@9938
   689
        eq_assume_contr_tac,
wenzelm@9938
   690
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   691
        FIRST' hyp_subst_tacs,
wenzelm@9938
   692
        n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   693
        bimatch2_tac safep_netpair]);
paulson@3705
   694
paulson@3705
   695
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   696
paulson@3705
   697
paulson@3705
   698
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   699
paulson@4066
   700
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   701
  proving a subgoal.  *)
wenzelm@32862
   702
fun inst0_step_tac (CS {safe0_netpair, ...}) =
wenzelm@32862
   703
  assume_tac APPEND'
wenzelm@32862
   704
  contr_tac APPEND'
lcp@747
   705
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   706
paulson@4066
   707
(*These unsafe steps could generate more subgoals.*)
wenzelm@32862
   708
fun instp_step_tac (CS {safep_netpair, ...}) =
lcp@747
   709
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   710
clasohm@0
   711
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   712
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   713
wenzelm@10736
   714
fun haz_step_tac (CS{haz_netpair,...}) =
lcp@681
   715
  biresolve_from_nets_tac haz_netpair;
lcp@681
   716
clasohm@0
   717
(*Single step for the prover.  FAILS unless it makes progress. *)
wenzelm@10736
   718
fun step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   719
        (inst_step_tac cs ORELSE' haz_step_tac cs) i;
clasohm@0
   720
clasohm@0
   721
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   722
  allows backtracking from "safe" rules to "unsafe" rules here.*)
wenzelm@10736
   723
fun slow_step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   724
        (inst_step_tac cs APPEND' haz_step_tac cs) i;
clasohm@0
   725
paulson@1800
   726
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   727
clasohm@0
   728
(*Dumb but fast*)
wenzelm@10382
   729
fun fast_tac cs =
wenzelm@35625
   730
  Object_Logic.atomize_prems_tac THEN' SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   731
clasohm@0
   732
(*Slower but smarter than fast_tac*)
wenzelm@10382
   733
fun best_tac cs =
wenzelm@35625
   734
  Object_Logic.atomize_prems_tac THEN'
clasohm@0
   735
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   736
oheimb@9402
   737
(*even a bit smarter than best_tac*)
wenzelm@10382
   738
fun first_best_tac cs =
wenzelm@35625
   739
  Object_Logic.atomize_prems_tac THEN'
oheimb@9402
   740
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (FIRSTGOAL (step_tac cs)));
oheimb@9402
   741
wenzelm@10382
   742
fun slow_tac cs =
wenzelm@35625
   743
  Object_Logic.atomize_prems_tac THEN'
wenzelm@10382
   744
  SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   745
wenzelm@10382
   746
fun slow_best_tac cs =
wenzelm@35625
   747
  Object_Logic.atomize_prems_tac THEN'
clasohm@0
   748
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   749
lcp@681
   750
wenzelm@10736
   751
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*)
wenzelm@32740
   752
val weight_ASTAR = Unsynchronized.ref 5;
paulson@1587
   753
wenzelm@10382
   754
fun astar_tac cs =
wenzelm@35625
   755
  Object_Logic.atomize_prems_tac THEN'
wenzelm@10382
   756
  SELECT_GOAL
wenzelm@10382
   757
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   758
      (step_tac cs 1));
paulson@1587
   759
wenzelm@10736
   760
fun slow_astar_tac cs =
wenzelm@35625
   761
  Object_Logic.atomize_prems_tac THEN'
wenzelm@10382
   762
  SELECT_GOAL
wenzelm@10382
   763
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   764
      (slow_step_tac cs 1));
paulson@1587
   765
paulson@1800
   766
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   767
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   768
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   769
  theorems such as 43. ****)
lcp@681
   770
lcp@747
   771
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   772
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   773
  greater search depth required.*)
wenzelm@32863
   774
fun dup_step_tac (CS {dup_netpair, ...}) =
lcp@681
   775
  biresolve_from_nets_tac dup_netpair;
lcp@681
   776
oheimb@5523
   777
(*Searching to depth m. A variant called nodup_depth_tac appears in clasimp.ML*)
oheimb@5757
   778
local
wenzelm@10736
   779
fun slow_step_tac' cs = appWrappers cs
wenzelm@9938
   780
        (instp_step_tac cs APPEND' dup_step_tac cs);
wenzelm@10736
   781
in fun depth_tac cs m i state = SELECT_GOAL
wenzelm@10736
   782
   (safe_steps_tac cs 1 THEN_ELSE
wenzelm@9938
   783
        (DEPTH_SOLVE (depth_tac cs m 1),
wenzelm@9938
   784
         inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9938
   785
                (slow_step_tac' cs 1 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))
oheimb@5757
   786
        )) i state;
oheimb@5757
   787
end;
lcp@747
   788
wenzelm@10736
   789
(*Search, with depth bound m.
paulson@2173
   790
  This is the "entry point", which does safe inferences first.*)
wenzelm@10736
   791
fun safe_depth_tac cs m =
wenzelm@10736
   792
  SUBGOAL
lcp@681
   793
    (fn (prem,i) =>
lcp@681
   794
      let val deti =
wenzelm@9938
   795
          (*No Vars in the goal?  No need to backtrack between goals.*)
wenzelm@29267
   796
          if exists_subterm (fn Var _ => true | _ => false) prem then DETERM else I
wenzelm@10736
   797
      in  SELECT_GOAL (TRY (safe_tac cs) THEN
wenzelm@9938
   798
                       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   799
      end);
lcp@681
   800
paulson@2868
   801
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   802
wenzelm@4079
   803
berghofe@1724
   804
wenzelm@15036
   805
(** context dependent claset components **)
wenzelm@15036
   806
wenzelm@15036
   807
datatype context_cs = ContextCS of
wenzelm@15036
   808
 {swrappers: (string * (Proof.context -> wrapper)) list,
wenzelm@15036
   809
  uwrappers: (string * (Proof.context -> wrapper)) list};
wenzelm@15036
   810
wenzelm@15036
   811
fun context_cs ctxt cs (ContextCS {swrappers, uwrappers}) =
wenzelm@15036
   812
  let
wenzelm@15036
   813
    fun add_wrapper add (name, f) claset = add (claset, (name, f ctxt));
wenzelm@15036
   814
  in
haftmann@22674
   815
    cs
haftmann@22674
   816
    |> fold_rev (add_wrapper (op addSWrapper)) swrappers
wenzelm@15036
   817
    |> fold_rev (add_wrapper (op addWrapper)) uwrappers
wenzelm@15036
   818
  end;
wenzelm@15036
   819
wenzelm@15036
   820
fun make_context_cs (swrappers, uwrappers) =
wenzelm@15036
   821
  ContextCS {swrappers = swrappers, uwrappers = uwrappers};
wenzelm@15036
   822
wenzelm@15036
   823
val empty_context_cs = make_context_cs ([], []);
wenzelm@15036
   824
wenzelm@15036
   825
fun merge_context_cs (ctxt_cs1, ctxt_cs2) =
wenzelm@24358
   826
  if pointer_eq (ctxt_cs1, ctxt_cs2) then ctxt_cs1
wenzelm@24358
   827
  else
wenzelm@24358
   828
    let
wenzelm@24358
   829
      val ContextCS {swrappers = swrappers1, uwrappers = uwrappers1} = ctxt_cs1;
wenzelm@24358
   830
      val ContextCS {swrappers = swrappers2, uwrappers = uwrappers2} = ctxt_cs2;
wenzelm@24358
   831
      val swrappers' = AList.merge (op =) (K true) (swrappers1, swrappers2);
wenzelm@24358
   832
      val uwrappers' = AList.merge (op =) (K true) (uwrappers1, uwrappers2);
wenzelm@24358
   833
    in make_context_cs (swrappers', uwrappers') end;
wenzelm@15036
   834
wenzelm@15036
   835
wenzelm@15036
   836
wenzelm@17880
   837
(** claset data **)
wenzelm@4079
   838
wenzelm@24021
   839
(* global clasets *)
berghofe@1724
   840
wenzelm@33522
   841
structure GlobalClaset = Theory_Data
wenzelm@22846
   842
(
wenzelm@26497
   843
  type T = claset * context_cs;
wenzelm@26497
   844
  val empty = (empty_cs, empty_context_cs);
wenzelm@26497
   845
  val extend = I;
wenzelm@33522
   846
  fun merge ((cs1, ctxt_cs1), (cs2, ctxt_cs2)) =
wenzelm@26497
   847
    (merge_cs (cs1, cs2), merge_context_cs (ctxt_cs1, ctxt_cs2));
wenzelm@22846
   848
);
berghofe@1724
   849
wenzelm@32261
   850
val get_global_claset = #1 o GlobalClaset.get;
wenzelm@32261
   851
val map_global_claset = GlobalClaset.map o apfst;
wenzelm@17880
   852
wenzelm@42361
   853
val get_context_cs = #2 o GlobalClaset.get o Proof_Context.theory_of;
wenzelm@15036
   854
fun map_context_cs f = GlobalClaset.map (apsnd
wenzelm@15036
   855
  (fn ContextCS {swrappers, uwrappers} => make_context_cs (f (swrappers, uwrappers))));
wenzelm@4079
   856
wenzelm@32148
   857
fun global_claset_of thy =
wenzelm@26497
   858
  let val (cs, ctxt_cs) = GlobalClaset.get thy
wenzelm@42361
   859
  in context_cs (Proof_Context.init_global thy) cs (ctxt_cs) end;
paulson@3727
   860
wenzelm@4079
   861
wenzelm@15036
   862
(* context dependent components *)
wenzelm@15036
   863
wenzelm@26497
   864
fun add_context_safe_wrapper wrapper = map_context_cs (apfst ((AList.update (op =) wrapper)));
wenzelm@26497
   865
fun del_context_safe_wrapper name = map_context_cs (apfst ((AList.delete (op =) name)));
wenzelm@15036
   866
wenzelm@26497
   867
fun add_context_unsafe_wrapper wrapper = map_context_cs (apsnd ((AList.update (op =) wrapper)));
wenzelm@26497
   868
fun del_context_unsafe_wrapper name = map_context_cs (apsnd ((AList.delete (op =) name)));
wenzelm@15036
   869
wenzelm@15036
   870
wenzelm@24021
   871
(* local clasets *)
wenzelm@5841
   872
wenzelm@33519
   873
structure LocalClaset = Proof_Data
wenzelm@22846
   874
(
wenzelm@5841
   875
  type T = claset;
wenzelm@32261
   876
  val init = get_global_claset;
wenzelm@22846
   877
);
wenzelm@5841
   878
wenzelm@32261
   879
val get_claset = LocalClaset.get;
wenzelm@32261
   880
val put_claset = LocalClaset.put;
wenzelm@32261
   881
wenzelm@32148
   882
fun claset_of ctxt =
wenzelm@26497
   883
  context_cs ctxt (LocalClaset.get ctxt) (get_context_cs ctxt);
wenzelm@22846
   884
wenzelm@5841
   885
wenzelm@24021
   886
(* generic clasets *)
wenzelm@24021
   887
wenzelm@32148
   888
val get_cs = Context.cases global_claset_of claset_of;
wenzelm@32261
   889
fun map_cs f = Context.mapping (map_global_claset f) (LocalClaset.map f);
wenzelm@24021
   890
wenzelm@24021
   891
wenzelm@5885
   892
(* attributes *)
wenzelm@5885
   893
wenzelm@18728
   894
fun attrib f = Thm.declaration_attribute (fn th =>
wenzelm@32261
   895
  Context.mapping (map_global_claset (f th)) (LocalClaset.map (f th)));
wenzelm@5885
   896
paulson@21689
   897
fun safe_dest w = attrib (addSE w o make_elim);
wenzelm@18691
   898
val safe_elim = attrib o addSE;
wenzelm@18691
   899
val safe_intro = attrib o addSI;
paulson@21689
   900
fun haz_dest w = attrib (addE w o make_elim);
wenzelm@18691
   901
val haz_elim = attrib o addE;
wenzelm@18691
   902
val haz_intro = attrib o addI;
wenzelm@33369
   903
val rule_del = attrib delrule o Context_Rules.rule_del;
wenzelm@5885
   904
wenzelm@5885
   905
wenzelm@10736
   906
end;
wenzelm@5841
   907
wenzelm@5841
   908
wenzelm@5841
   909
wenzelm@5885
   910
(** concrete syntax of attributes **)
wenzelm@5841
   911
wenzelm@5841
   912
val introN = "intro";
wenzelm@5841
   913
val elimN = "elim";
wenzelm@5841
   914
val destN = "dest";
wenzelm@5841
   915
wenzelm@30528
   916
val setup_attrs =
wenzelm@30528
   917
  Attrib.setup @{binding swapped} (Scan.succeed swapped)
wenzelm@30528
   918
    "classical swap of introduction rule" #>
wenzelm@33369
   919
  Attrib.setup @{binding dest} (Context_Rules.add safe_dest haz_dest Context_Rules.dest_query)
wenzelm@30528
   920
    "declaration of Classical destruction rule" #>
wenzelm@33369
   921
  Attrib.setup @{binding elim} (Context_Rules.add safe_elim haz_elim Context_Rules.elim_query)
wenzelm@30528
   922
    "declaration of Classical elimination rule" #>
wenzelm@33369
   923
  Attrib.setup @{binding intro} (Context_Rules.add safe_intro haz_intro Context_Rules.intro_query)
wenzelm@30528
   924
    "declaration of Classical introduction rule" #>
wenzelm@30528
   925
  Attrib.setup @{binding rule} (Scan.lift Args.del >> K rule_del)
wenzelm@30528
   926
    "remove declaration of intro/elim/dest rule";
wenzelm@5841
   927
wenzelm@5841
   928
wenzelm@5841
   929
wenzelm@7230
   930
(** proof methods **)
wenzelm@7230
   931
wenzelm@7230
   932
local
wenzelm@7230
   933
wenzelm@30609
   934
fun some_rule_tac ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@5841
   935
  let
wenzelm@33369
   936
    val [rules1, rules2, rules4] = Context_Rules.find_rules false facts goal ctxt;
wenzelm@32148
   937
    val CS {xtra_netpair, ...} = claset_of ctxt;
wenzelm@33369
   938
    val rules3 = Context_Rules.find_rules_netpair true facts goal xtra_netpair;
wenzelm@12376
   939
    val rules = rules1 @ rules2 @ rules3 @ rules4;
wenzelm@18223
   940
    val ruleq = Drule.multi_resolves facts rules;
wenzelm@12376
   941
  in
wenzelm@12376
   942
    Method.trace ctxt rules;
wenzelm@32952
   943
    fn st => Seq.maps (fn rule => Tactic.rtac rule i st) ruleq
wenzelm@18834
   944
  end)
wenzelm@21687
   945
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@5841
   946
wenzelm@30609
   947
in
wenzelm@7281
   948
wenzelm@30609
   949
fun rule_tac ctxt [] facts = some_rule_tac ctxt facts
wenzelm@30609
   950
  | rule_tac _ rules facts = Method.rule_tac rules facts;
wenzelm@30609
   951
wenzelm@30609
   952
fun default_tac ctxt rules facts =
wenzelm@30609
   953
  HEADGOAL (rule_tac ctxt rules facts) ORELSE
haftmann@26470
   954
  Class.default_intro_tac ctxt facts;
wenzelm@10309
   955
wenzelm@7230
   956
end;
wenzelm@5841
   957
wenzelm@5841
   958
wenzelm@7230
   959
(* contradiction method *)
wenzelm@6502
   960
wenzelm@7425
   961
val contradiction = Method.rule [Data.not_elim, Data.not_elim COMP Drule.swap_prems_rl];
wenzelm@6502
   962
wenzelm@6502
   963
wenzelm@6502
   964
(* automatic methods *)
wenzelm@5841
   965
wenzelm@5927
   966
val cla_modifiers =
wenzelm@18728
   967
 [Args.$$$ destN -- Args.bang_colon >> K ((I, safe_dest NONE): Method.modifier),
wenzelm@18728
   968
  Args.$$$ destN -- Args.colon >> K (I, haz_dest NONE),
wenzelm@18728
   969
  Args.$$$ elimN -- Args.bang_colon >> K (I, safe_elim NONE),
wenzelm@18728
   970
  Args.$$$ elimN -- Args.colon >> K (I, haz_elim NONE),
wenzelm@18728
   971
  Args.$$$ introN -- Args.bang_colon >> K (I, safe_intro NONE),
wenzelm@18728
   972
  Args.$$$ introN -- Args.colon >> K (I, haz_intro NONE),
wenzelm@18728
   973
  Args.del -- Args.colon >> K (I, rule_del)];
wenzelm@5927
   974
wenzelm@35613
   975
fun cla_meth tac ctxt = METHOD (fn facts =>
wenzelm@35613
   976
  ALLGOALS (Method.insert_tac facts) THEN tac (claset_of ctxt));
wenzelm@7132
   977
wenzelm@35613
   978
fun cla_meth' tac ctxt = METHOD (fn facts =>
wenzelm@35613
   979
  HEADGOAL (Method.insert_tac facts THEN' tac (claset_of ctxt)));
wenzelm@5841
   980
wenzelm@35613
   981
fun cla_method tac = Method.sections cla_modifiers >> K (cla_meth tac);
wenzelm@35613
   982
fun cla_method' tac = Method.sections cla_modifiers >> K (cla_meth' tac);
wenzelm@5841
   983
wenzelm@5841
   984
wenzelm@5841
   985
wenzelm@5841
   986
(** setup_methods **)
wenzelm@5841
   987
wenzelm@30541
   988
val setup_methods =
wenzelm@30609
   989
  Method.setup @{binding default}
wenzelm@30609
   990
   (Attrib.thms >> (fn rules => fn ctxt => METHOD (default_tac ctxt rules)))
wenzelm@30541
   991
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30609
   992
  Method.setup @{binding rule}
wenzelm@30609
   993
    (Attrib.thms >> (fn rules => fn ctxt => METHOD (HEADGOAL o rule_tac ctxt rules)))
wenzelm@30541
   994
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30541
   995
  Method.setup @{binding contradiction} (Scan.succeed (K contradiction))
wenzelm@30541
   996
    "proof by contradiction" #>
wenzelm@30541
   997
  Method.setup @{binding clarify} (cla_method' (CHANGED_PROP oo clarify_tac))
wenzelm@30541
   998
    "repeatedly apply safe steps" #>
wenzelm@30541
   999
  Method.setup @{binding fast} (cla_method' fast_tac) "classical prover (depth-first)" #>
wenzelm@30541
  1000
  Method.setup @{binding slow} (cla_method' slow_tac) "classical prover (slow depth-first)" #>
wenzelm@30541
  1001
  Method.setup @{binding best} (cla_method' best_tac) "classical prover (best-first)" #>
wenzelm@30541
  1002
  Method.setup @{binding deepen} (cla_method' (fn cs => deepen_tac cs 4))
wenzelm@30541
  1003
    "classical prover (iterative deepening)" #>
wenzelm@30541
  1004
  Method.setup @{binding safe} (cla_method (CHANGED_PROP o safe_tac))
wenzelm@30541
  1005
    "classical prover (apply safe rules)";
wenzelm@5841
  1006
wenzelm@5841
  1007
wenzelm@5841
  1008
wenzelm@5841
  1009
(** theory setup **)
wenzelm@5841
  1010
wenzelm@26497
  1011
val setup = setup_attrs #> setup_methods;
wenzelm@5841
  1012
wenzelm@5841
  1013
wenzelm@8667
  1014
wenzelm@8667
  1015
(** outer syntax **)
wenzelm@8667
  1016
wenzelm@24867
  1017
val _ =
wenzelm@36960
  1018
  Outer_Syntax.improper_command "print_claset" "print context of Classical Reasoner"
wenzelm@36960
  1019
    Keyword.diag
wenzelm@26497
  1020
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@32148
  1021
      Toplevel.keep (print_cs o claset_of o Toplevel.context_of)));
wenzelm@8667
  1022
wenzelm@5841
  1023
end;