src/Provers/classical.ML
author wenzelm
Mon Jul 06 21:24:30 2009 +0200 (2009-07-06)
changeset 31945 d5f186aa0bed
parent 30609 983e8b6e4e69
child 32091 30e2ffbba718
permissions -rw-r--r--
structure Thm: less pervasive names;
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@12401
    48
                 xtra_netpair: ContextRules.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@4079
    72
  val claset_of: theory -> claset
wenzelm@15036
    73
  val local_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@9938
    77
  val weight_ASTAR      : int 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@17880
   116
  val get_claset: theory -> claset
wenzelm@26497
   117
  val map_claset: (claset -> claset) -> theory -> theory
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@7559
   128
  val cla_meth: (claset -> tactic) -> thm list -> Proof.context -> Proof.method
wenzelm@7559
   129
  val cla_meth': (claset -> int -> tactic) -> thm list -> 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@19257
   156
  if ObjectLogic.is_elim 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@23594
   176
val flat_rule = Conv.fconv_rule (Conv.prems_conv ~1 ObjectLogic.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@26412
   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@30190
   204
        biresolve_tac (List.foldr addrl [] rls)
clasohm@0
   205
    end;
clasohm@0
   206
lcp@681
   207
(*Duplication of hazardous rules, for complete provers*)
paulson@2689
   208
fun dup_intr th = zero_var_indexes (th RS classical);
lcp@681
   209
wenzelm@6967
   210
fun dup_elim th =
wenzelm@13525
   211
    rule_by_tactic (TRYALL (etac revcut_rl))
wenzelm@31945
   212
      ((th RSN (2, revcut_rl)) |> Thm.assumption 2 |> Seq.hd);
lcp@1073
   213
paulson@1800
   214
(**** Classical rule sets ****)
clasohm@0
   215
clasohm@0
   216
datatype claset =
wenzelm@12401
   217
  CS of {safeIs         : thm list,                (*safe introduction rules*)
wenzelm@12401
   218
         safeEs         : thm list,                (*safe elimination rules*)
wenzelm@12401
   219
         hazIs          : thm list,                (*unsafe introduction rules*)
wenzelm@12401
   220
         hazEs          : thm list,                (*unsafe elimination rules*)
wenzelm@12401
   221
         swrappers      : (string * wrapper) list, (*for transforming safe_step_tac*)
wenzelm@9938
   222
         uwrappers      : (string * wrapper) list, (*for transforming step_tac*)
wenzelm@12401
   223
         safe0_netpair  : netpair,                 (*nets for trivial cases*)
wenzelm@12401
   224
         safep_netpair  : netpair,                 (*nets for >0 subgoals*)
wenzelm@12401
   225
         haz_netpair    : netpair,                 (*nets for unsafe rules*)
wenzelm@12401
   226
         dup_netpair    : netpair,                 (*nets for duplication*)
wenzelm@12401
   227
         xtra_netpair   : ContextRules.netpair};   (*nets for extra rules*)
clasohm@0
   228
lcp@1073
   229
(*Desired invariants are
wenzelm@9938
   230
        safe0_netpair = build safe0_brls,
wenzelm@9938
   231
        safep_netpair = build safep_brls,
wenzelm@9938
   232
        haz_netpair = build (joinrules(hazIs, hazEs)),
wenzelm@10736
   233
        dup_netpair = build (joinrules(map dup_intr hazIs,
wenzelm@12376
   234
                                       map dup_elim hazEs))
lcp@1073
   235
wenzelm@10736
   236
where build = build_netpair(Net.empty,Net.empty),
lcp@1073
   237
      safe0_brls contains all brules that solve the subgoal, and
lcp@1073
   238
      safep_brls contains all brules that generate 1 or more new subgoals.
wenzelm@4079
   239
The theorem lists are largely comments, though they are used in merge_cs and print_cs.
lcp@1073
   240
Nets must be built incrementally, to save space and time.
lcp@1073
   241
*)
clasohm@0
   242
wenzelm@6502
   243
val empty_netpair = (Net.empty, Net.empty);
wenzelm@6502
   244
wenzelm@10736
   245
val empty_cs =
wenzelm@9938
   246
  CS{safeIs     = [],
wenzelm@9938
   247
     safeEs     = [],
wenzelm@9938
   248
     hazIs      = [],
wenzelm@9938
   249
     hazEs      = [],
oheimb@4651
   250
     swrappers  = [],
oheimb@4651
   251
     uwrappers  = [],
wenzelm@6502
   252
     safe0_netpair = empty_netpair,
wenzelm@6502
   253
     safep_netpair = empty_netpair,
wenzelm@6502
   254
     haz_netpair   = empty_netpair,
wenzelm@6955
   255
     dup_netpair   = empty_netpair,
wenzelm@6955
   256
     xtra_netpair  = empty_netpair};
clasohm@0
   257
wenzelm@15036
   258
fun print_cs (CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers, ...}) =
wenzelm@3546
   259
  let val pretty_thms = map Display.pretty_thm in
wenzelm@9760
   260
    [Pretty.big_list "safe introduction rules (intro!):" (pretty_thms safeIs),
wenzelm@9760
   261
      Pretty.big_list "introduction rules (intro):" (pretty_thms hazIs),
wenzelm@9760
   262
      Pretty.big_list "safe elimination rules (elim!):" (pretty_thms safeEs),
wenzelm@15036
   263
      Pretty.big_list "elimination rules (elim):" (pretty_thms hazEs),
wenzelm@15036
   264
      Pretty.strs ("safe wrappers:" :: map #1 swrappers),
wenzelm@15036
   265
      Pretty.strs ("unsafe wrappers:" :: map #1 uwrappers)]
wenzelm@8727
   266
    |> Pretty.chunks |> Pretty.writeln
wenzelm@3546
   267
  end;
clasohm@0
   268
oheimb@4653
   269
fun rep_cs (CS args) = args;
lcp@1073
   270
haftmann@22674
   271
fun appSWrappers (CS {swrappers, ...}) = fold snd swrappers;
haftmann@22674
   272
fun appWrappers  (CS {uwrappers, ...}) = fold snd uwrappers;
lcp@1073
   273
wenzelm@4079
   274
paulson@1800
   275
(*** Adding (un)safe introduction or elimination rules.
lcp@1073
   276
lcp@1073
   277
    In case of overlap, new rules are tried BEFORE old ones!!
paulson@1800
   278
***)
clasohm@0
   279
wenzelm@12376
   280
(*For use with biresolve_tac.  Combines intro rules with swap to handle negated
lcp@1073
   281
  assumptions.  Pairs elim rules with true. *)
wenzelm@12376
   282
fun joinrules (intrs, elims) =
paulson@18557
   283
  (map (pair true) (elims @ swapify intrs)) @ map (pair false) intrs;
wenzelm@12376
   284
wenzelm@12401
   285
fun joinrules' (intrs, elims) =
paulson@18557
   286
  map (pair true) elims @ map (pair false) intrs;
lcp@1073
   287
wenzelm@10736
   288
(*Priority: prefer rules with fewest subgoals,
paulson@1231
   289
  then rules added most recently (preferring the head of the list).*)
lcp@1073
   290
fun tag_brls k [] = []
lcp@1073
   291
  | tag_brls k (brl::brls) =
wenzelm@10736
   292
      (1000000*subgoals_of_brl brl + k, brl) ::
lcp@1073
   293
      tag_brls (k+1) brls;
lcp@1073
   294
wenzelm@12401
   295
fun tag_brls' _ _ [] = []
wenzelm@12401
   296
  | tag_brls' w k (brl::brls) = ((w, k), brl) :: tag_brls' w (k + 1) brls;
wenzelm@10736
   297
wenzelm@23178
   298
fun insert_tagged_list rls = fold_rev Tactic.insert_tagged_brl rls;
lcp@1073
   299
lcp@1073
   300
(*Insert into netpair that already has nI intr rules and nE elim rules.
lcp@1073
   301
  Count the intr rules double (to account for swapify).  Negate to give the
lcp@1073
   302
  new insertions the lowest priority.*)
wenzelm@12376
   303
fun insert (nI, nE) = insert_tagged_list o (tag_brls (~(2*nI+nE))) o joinrules;
wenzelm@12401
   304
fun insert' w (nI, nE) = insert_tagged_list o tag_brls' w (~(nI + nE)) o joinrules';
lcp@1073
   305
wenzelm@23178
   306
fun delete_tagged_list rls = fold_rev Tactic.delete_tagged_brl rls;
wenzelm@12362
   307
fun delete x = delete_tagged_list (joinrules x);
wenzelm@12401
   308
fun delete' x = delete_tagged_list (joinrules' x);
paulson@1800
   309
wenzelm@22360
   310
val mem_thm = member Thm.eq_thm_prop
wenzelm@22360
   311
and rem_thm = remove Thm.eq_thm_prop;
paulson@2813
   312
paulson@1927
   313
(*Warn if the rule is already present ELSEWHERE in the claset.  The addition
paulson@1927
   314
  is still allowed.*)
wenzelm@12376
   315
fun warn_dup th (CS{safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@18691
   316
       if mem_thm safeIs th then
wenzelm@26928
   317
         warning ("Rule already declared as safe introduction (intro!)\n" ^ Display.string_of_thm th)
wenzelm@18691
   318
  else if mem_thm safeEs th then
wenzelm@26928
   319
         warning ("Rule already declared as safe elimination (elim!)\n" ^ Display.string_of_thm th)
wenzelm@18691
   320
  else if mem_thm hazIs th then
wenzelm@26928
   321
         warning ("Rule already declared as introduction (intro)\n" ^ Display.string_of_thm th)
wenzelm@18691
   322
  else if mem_thm hazEs th then
wenzelm@26928
   323
         warning ("Rule already declared as elimination (elim)\n" ^ Display.string_of_thm th)
paulson@1927
   324
  else ();
paulson@1927
   325
wenzelm@12376
   326
paulson@1800
   327
(*** Safe rules ***)
lcp@982
   328
wenzelm@18691
   329
fun addSI w th
wenzelm@18534
   330
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   331
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   332
  if mem_thm safeIs th then
wenzelm@26928
   333
         (warning ("Ignoring duplicate safe introduction (intro!)\n" ^ Display.string_of_thm th);
wenzelm@9938
   334
          cs)
paulson@1927
   335
  else
wenzelm@23594
   336
  let val th' = flat_rule th
wenzelm@23594
   337
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@23594
   338
          List.partition Thm.no_prems [th']
paulson@1927
   339
      val nI = length safeIs + 1
lcp@1073
   340
      and nE = length safeEs
paulson@1927
   341
  in warn_dup th cs;
wenzelm@9938
   342
     CS{safeIs  = th::safeIs,
lcp@1073
   343
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
wenzelm@9938
   344
        safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
wenzelm@9938
   345
        safeEs  = safeEs,
wenzelm@9938
   346
        hazIs   = hazIs,
wenzelm@9938
   347
        hazEs   = hazEs,
wenzelm@9938
   348
        swrappers    = swrappers,
wenzelm@9938
   349
        uwrappers    = uwrappers,
wenzelm@9938
   350
        haz_netpair  = haz_netpair,
wenzelm@9938
   351
        dup_netpair  = dup_netpair,
wenzelm@18691
   352
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([th], []) xtra_netpair}
lcp@1073
   353
  end;
lcp@1073
   354
wenzelm@18691
   355
fun addSE w th
wenzelm@18534
   356
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   357
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   358
  if mem_thm safeEs th then
wenzelm@26928
   359
         (warning ("Ignoring duplicate safe elimination (elim!)\n" ^ Display.string_of_thm th);
wenzelm@9938
   360
          cs)
paulson@18557
   361
  else if has_fewer_prems 1 th then
wenzelm@26928
   362
    	error("Ill-formed elimination rule\n" ^ Display.string_of_thm th)
paulson@1927
   363
  else
wenzelm@18534
   364
  let
wenzelm@23594
   365
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   366
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@18534
   367
          List.partition (fn rl => nprems_of rl=1) [th']
lcp@1073
   368
      val nI = length safeIs
paulson@1927
   369
      and nE = length safeEs + 1
paulson@1927
   370
  in warn_dup th cs;
wenzelm@9938
   371
     CS{safeEs  = th::safeEs,
lcp@1073
   372
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
wenzelm@9938
   373
        safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
wenzelm@9938
   374
        safeIs  = safeIs,
wenzelm@9938
   375
        hazIs   = hazIs,
wenzelm@9938
   376
        hazEs   = hazEs,
wenzelm@9938
   377
        swrappers    = swrappers,
wenzelm@9938
   378
        uwrappers    = uwrappers,
wenzelm@9938
   379
        haz_netpair  = haz_netpair,
wenzelm@9938
   380
        dup_netpair  = dup_netpair,
wenzelm@18691
   381
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   382
  end;
clasohm@0
   383
wenzelm@18691
   384
fun cs addSIs ths = fold_rev (addSI NONE) ths cs;
wenzelm@18691
   385
fun cs addSEs ths = fold_rev (addSE NONE) ths cs;
paulson@1927
   386
paulson@21689
   387
fun make_elim th =
paulson@18557
   388
    if has_fewer_prems 1 th then
wenzelm@26928
   389
    	error("Ill-formed destruction rule\n" ^ Display.string_of_thm th)
paulson@21689
   390
    else Tactic.make_elim th;
paulson@17084
   391
paulson@21689
   392
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   393
lcp@1073
   394
paulson@1800
   395
(*** Hazardous (unsafe) rules ***)
clasohm@0
   396
wenzelm@18691
   397
fun addI w th
wenzelm@18534
   398
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   399
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   400
  if mem_thm hazIs th then
wenzelm@26928
   401
         (warning ("Ignoring duplicate introduction (intro)\n" ^ Display.string_of_thm th);
wenzelm@9938
   402
          cs)
paulson@1927
   403
  else
wenzelm@23594
   404
  let val th' = flat_rule th
wenzelm@23594
   405
      val nI = length hazIs + 1
lcp@1073
   406
      and nE = length hazEs
paulson@1927
   407
  in warn_dup th cs;
wenzelm@9938
   408
     CS{hazIs   = th::hazIs,
wenzelm@23594
   409
        haz_netpair = insert (nI,nE) ([th'], []) haz_netpair,
wenzelm@23594
   410
        dup_netpair = insert (nI,nE) (map dup_intr [th'], []) dup_netpair,
wenzelm@10736
   411
        safeIs  = safeIs,
wenzelm@9938
   412
        safeEs  = safeEs,
wenzelm@9938
   413
        hazEs   = hazEs,
wenzelm@9938
   414
        swrappers     = swrappers,
wenzelm@9938
   415
        uwrappers     = uwrappers,
wenzelm@9938
   416
        safe0_netpair = safe0_netpair,
wenzelm@9938
   417
        safep_netpair = safep_netpair,
wenzelm@18691
   418
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([th], []) xtra_netpair}
paulson@18557
   419
  end
paulson@18557
   420
  handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@26928
   421
         error ("Ill-formed introduction rule\n" ^ Display.string_of_thm th);
lcp@1073
   422
wenzelm@18691
   423
fun addE w th
wenzelm@18534
   424
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   425
            safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   426
  if mem_thm hazEs th then
wenzelm@26928
   427
         (warning ("Ignoring duplicate elimination (elim)\n" ^ Display.string_of_thm th);
wenzelm@9938
   428
          cs)
paulson@18557
   429
  else if has_fewer_prems 1 th then
wenzelm@26928
   430
    	error("Ill-formed elimination rule\n" ^ Display.string_of_thm th)
paulson@1927
   431
  else
wenzelm@18534
   432
  let
wenzelm@23594
   433
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   434
      val nI = length hazIs
paulson@1927
   435
      and nE = length hazEs + 1
paulson@1927
   436
  in warn_dup th cs;
wenzelm@9938
   437
     CS{hazEs   = th::hazEs,
wenzelm@18534
   438
        haz_netpair = insert (nI,nE) ([], [th']) haz_netpair,
wenzelm@18534
   439
        dup_netpair = insert (nI,nE) ([], map dup_elim [th']) dup_netpair,
wenzelm@10736
   440
        safeIs  = safeIs,
wenzelm@9938
   441
        safeEs  = safeEs,
wenzelm@9938
   442
        hazIs   = hazIs,
wenzelm@9938
   443
        swrappers     = swrappers,
wenzelm@9938
   444
        uwrappers     = uwrappers,
wenzelm@9938
   445
        safe0_netpair = safe0_netpair,
wenzelm@9938
   446
        safep_netpair = safep_netpair,
wenzelm@18691
   447
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   448
  end;
clasohm@0
   449
wenzelm@18691
   450
fun cs addIs ths = fold_rev (addI NONE) ths cs;
wenzelm@18691
   451
fun cs addEs ths = fold_rev (addE NONE) ths cs;
paulson@1927
   452
paulson@21689
   453
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   454
lcp@1073
   455
wenzelm@10736
   456
(*** Deletion of rules
paulson@1800
   457
     Working out what to delete, requires repeating much of the code used
wenzelm@9938
   458
        to insert.
paulson@1800
   459
***)
paulson@1800
   460
wenzelm@10736
   461
fun delSI th
wenzelm@12376
   462
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   463
                    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   464
 if mem_thm safeIs th then
wenzelm@23594
   465
   let val th' = flat_rule th
wenzelm@23594
   466
       val (safe0_rls, safep_rls) = List.partition Thm.no_prems [th']
paulson@2813
   467
   in CS{safe0_netpair = delete (safe0_rls, []) safe0_netpair,
wenzelm@9938
   468
         safep_netpair = delete (safep_rls, []) safep_netpair,
wenzelm@18691
   469
         safeIs = rem_thm th safeIs,
wenzelm@9938
   470
         safeEs = safeEs,
wenzelm@9938
   471
         hazIs  = hazIs,
wenzelm@9938
   472
         hazEs  = hazEs,
wenzelm@9938
   473
         swrappers    = swrappers,
wenzelm@9938
   474
         uwrappers    = uwrappers,
wenzelm@9938
   475
         haz_netpair  = haz_netpair,
wenzelm@9938
   476
         dup_netpair  = dup_netpair,
wenzelm@12401
   477
         xtra_netpair = delete' ([th], []) xtra_netpair}
paulson@2813
   478
   end
paulson@2813
   479
 else cs;
paulson@1800
   480
paulson@2813
   481
fun delSE th
wenzelm@12376
   482
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   483
                    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   484
  if mem_thm safeEs th then
wenzelm@18534
   485
    let
wenzelm@23594
   486
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   487
      val (safe0_rls, safep_rls) = List.partition (fn rl => nprems_of rl=1) [th']
wenzelm@18534
   488
    in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
wenzelm@9938
   489
         safep_netpair = delete ([], safep_rls) safep_netpair,
wenzelm@9938
   490
         safeIs = safeIs,
wenzelm@18691
   491
         safeEs = rem_thm th safeEs,
wenzelm@9938
   492
         hazIs  = hazIs,
wenzelm@9938
   493
         hazEs  = hazEs,
wenzelm@9938
   494
         swrappers    = swrappers,
wenzelm@9938
   495
         uwrappers    = uwrappers,
wenzelm@9938
   496
         haz_netpair  = haz_netpair,
wenzelm@9938
   497
         dup_netpair  = dup_netpair,
wenzelm@12401
   498
         xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@18534
   499
    end
wenzelm@18534
   500
  else cs;
paulson@1800
   501
paulson@1800
   502
paulson@2813
   503
fun delI th
wenzelm@12376
   504
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   505
                   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   506
 if mem_thm hazIs th then
wenzelm@23594
   507
    let val th' = flat_rule th
wenzelm@23594
   508
    in CS{haz_netpair = delete ([th'], []) haz_netpair,
wenzelm@23594
   509
        dup_netpair = delete ([dup_intr th'], []) dup_netpair,
wenzelm@10736
   510
        safeIs  = safeIs,
wenzelm@9938
   511
        safeEs  = safeEs,
wenzelm@18691
   512
        hazIs   = rem_thm th hazIs,
wenzelm@9938
   513
        hazEs   = hazEs,
wenzelm@9938
   514
        swrappers     = swrappers,
wenzelm@9938
   515
        uwrappers     = uwrappers,
wenzelm@9938
   516
        safe0_netpair = safe0_netpair,
wenzelm@9938
   517
        safep_netpair = safep_netpair,
wenzelm@12401
   518
        xtra_netpair = delete' ([th], []) xtra_netpair}
wenzelm@23594
   519
    end
paulson@18557
   520
 else cs
paulson@18557
   521
 handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@26928
   522
        error ("Ill-formed introduction rule\n" ^ Display.string_of_thm th);
paulson@18557
   523
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}) =
wenzelm@23594
   528
 if mem_thm hazEs th then
wenzelm@23594
   529
   let val th' = classical_rule (flat_rule th)
wenzelm@23594
   530
   in CS{haz_netpair = delete ([], [th']) haz_netpair,
wenzelm@18534
   531
        dup_netpair = delete ([], [dup_elim th']) dup_netpair,
wenzelm@10736
   532
        safeIs  = safeIs,
wenzelm@9938
   533
        safeEs  = safeEs,
wenzelm@9938
   534
        hazIs   = hazIs,
wenzelm@18691
   535
        hazEs   = rem_thm th hazEs,
wenzelm@9938
   536
        swrappers     = swrappers,
wenzelm@9938
   537
        uwrappers     = uwrappers,
wenzelm@9938
   538
        safe0_netpair = safe0_netpair,
wenzelm@9938
   539
        safep_netpair = safep_netpair,
wenzelm@12401
   540
        xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@23594
   541
   end
wenzelm@23594
   542
 else cs;
paulson@1800
   543
paulson@2813
   544
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
wenzelm@18534
   545
fun delrule th (cs as CS {safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@18534
   546
  let val th' = Tactic.make_elim th in
wenzelm@18691
   547
    if mem_thm safeIs th orelse mem_thm safeEs th orelse
wenzelm@18691
   548
      mem_thm hazIs th orelse mem_thm hazEs th orelse
wenzelm@18691
   549
      mem_thm safeEs th' orelse mem_thm hazEs th'
wenzelm@12376
   550
    then delSI th (delSE th (delI th (delE th (delSE th' (delE th' cs)))))
wenzelm@26928
   551
    else (warning ("Undeclared classical rule\n" ^ Display.string_of_thm th); cs)
wenzelm@9938
   552
  end;
paulson@1800
   553
wenzelm@18534
   554
fun cs delrules ths = fold delrule ths cs;
paulson@1800
   555
paulson@1800
   556
oheimb@4767
   557
(*** Modifying the wrapper tacticals ***)
haftmann@22674
   558
fun map_swrappers f
haftmann@22674
   559
  (CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   560
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   561
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   562
    swrappers = f swrappers, uwrappers = uwrappers,
oheimb@4767
   563
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   564
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   565
haftmann@22674
   566
fun map_uwrappers f
haftmann@22674
   567
  (CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   568
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   569
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   570
    swrappers = swrappers, uwrappers = f uwrappers,
oheimb@4767
   571
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   572
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   573
haftmann@22674
   574
fun update_warn msg (p as (key : string, _)) xs =
haftmann@22674
   575
  (if AList.defined (op =) xs key then warning msg else ();
haftmann@22674
   576
    AList.update (op =) p xs);
haftmann@22674
   577
haftmann@22674
   578
fun delete_warn msg (key : string) xs =
haftmann@22674
   579
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@22674
   580
    else (warning msg; xs);
lcp@982
   581
oheimb@4651
   582
(*Add/replace a safe wrapper*)
haftmann@22674
   583
fun cs addSWrapper new_swrapper = map_swrappers
haftmann@22674
   584
  (update_warn ("Overwriting safe wrapper " ^ fst new_swrapper) new_swrapper) cs;
oheimb@4651
   585
oheimb@4651
   586
(*Add/replace an unsafe wrapper*)
haftmann@22674
   587
fun cs addWrapper new_uwrapper = map_uwrappers
haftmann@22674
   588
  (update_warn ("Overwriting unsafe wrapper " ^ fst new_uwrapper) new_uwrapper) cs;
lcp@982
   589
oheimb@4651
   590
(*Remove a safe wrapper*)
haftmann@22674
   591
fun cs delSWrapper name = map_swrappers
haftmann@22674
   592
  (delete_warn ("No such safe wrapper in claset: " ^ name) name) cs;
lcp@982
   593
oheimb@4651
   594
(*Remove an unsafe wrapper*)
haftmann@22674
   595
fun cs delWrapper name = map_uwrappers
haftmann@22674
   596
  (delete_warn ("No such unsafe wrapper in claset: " ^ name) name) cs;
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.*)
haftmann@22674
   622
fun merge_cs (cs as CS {safeIs, safeEs, hazIs, hazEs, ...},
wenzelm@24358
   623
    cs' as CS {safeIs = safeIs2, safeEs = safeEs2, hazIs = hazIs2, hazEs = hazEs2,
haftmann@22674
   624
      swrappers, uwrappers, ...}) =
wenzelm@24358
   625
  if pointer_eq (cs, cs') then cs
wenzelm@24358
   626
  else
wenzelm@24358
   627
    let
wenzelm@24358
   628
      val safeIs' = fold rem_thm safeIs safeIs2;
wenzelm@24358
   629
      val safeEs' = fold rem_thm safeEs safeEs2;
wenzelm@24358
   630
      val hazIs' = fold rem_thm hazIs hazIs2;
wenzelm@24358
   631
      val hazEs' = fold rem_thm hazEs hazEs2;
wenzelm@24358
   632
      val cs1   = cs addSIs safeIs'
wenzelm@24358
   633
                     addSEs safeEs'
wenzelm@24358
   634
                     addIs  hazIs'
wenzelm@24358
   635
                     addEs  hazEs';
wenzelm@24358
   636
      val cs2 = map_swrappers
wenzelm@24358
   637
        (fn ws => AList.merge (op =) (K true) (ws, swrappers)) cs1;
wenzelm@24358
   638
      val cs3 = map_uwrappers
wenzelm@24358
   639
        (fn ws => AList.merge (op =) (K true) (ws, uwrappers)) cs2;
wenzelm@24358
   640
    in cs3 end;
paulson@1711
   641
lcp@982
   642
paulson@1800
   643
(**** Simple tactics for theorem proving ****)
clasohm@0
   644
clasohm@0
   645
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   646
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   647
  appSWrappers cs (FIRST' [
wenzelm@9938
   648
        eq_assume_tac,
wenzelm@9938
   649
        eq_mp_tac,
wenzelm@9938
   650
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   651
        FIRST' hyp_subst_tacs,
wenzelm@9938
   652
        bimatch_from_nets_tac safep_netpair]);
clasohm@0
   653
oheimb@5757
   654
(*Repeatedly attack a subgoal using safe inferences -- it's deterministic!*)
wenzelm@10736
   655
fun safe_steps_tac cs = REPEAT_DETERM1 o
wenzelm@9938
   656
        (fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
oheimb@5757
   657
clasohm@0
   658
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@5757
   659
fun safe_tac cs = REPEAT_DETERM1 (FIRSTGOAL (safe_steps_tac cs));
lcp@747
   660
paulson@3705
   661
paulson@3705
   662
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   663
paulson@3705
   664
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   665
paulson@3705
   666
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   667
  create precisely n subgoals.*)
wenzelm@10736
   668
fun n_bimatch_from_nets_tac n =
wenzelm@30558
   669
    biresolution_from_nets_tac (order_list o List.filter (nsubgoalsP n)) true;
paulson@3705
   670
paulson@3705
   671
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   672
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   673
paulson@3705
   674
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   675
fun bimatch2_tac netpair i =
paulson@3705
   676
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   677
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   678
paulson@3705
   679
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   680
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   681
  appSWrappers cs (FIRST' [
wenzelm@9938
   682
        eq_assume_contr_tac,
wenzelm@9938
   683
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   684
        FIRST' hyp_subst_tacs,
wenzelm@9938
   685
        n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   686
        bimatch2_tac safep_netpair]);
paulson@3705
   687
paulson@3705
   688
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   689
paulson@3705
   690
paulson@3705
   691
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   692
paulson@4066
   693
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   694
  proving a subgoal.  *)
lcp@747
   695
fun inst0_step_tac (CS{safe0_netpair,safep_netpair,...}) =
wenzelm@10736
   696
  assume_tac                      APPEND'
wenzelm@10736
   697
  contr_tac                       APPEND'
lcp@747
   698
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   699
paulson@4066
   700
(*These unsafe steps could generate more subgoals.*)
lcp@747
   701
fun instp_step_tac (CS{safep_netpair,...}) =
lcp@747
   702
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   703
clasohm@0
   704
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   705
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   706
wenzelm@10736
   707
fun haz_step_tac (CS{haz_netpair,...}) =
lcp@681
   708
  biresolve_from_nets_tac haz_netpair;
lcp@681
   709
clasohm@0
   710
(*Single step for the prover.  FAILS unless it makes progress. *)
wenzelm@10736
   711
fun step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   712
        (inst_step_tac cs ORELSE' haz_step_tac cs) i;
clasohm@0
   713
clasohm@0
   714
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   715
  allows backtracking from "safe" rules to "unsafe" rules here.*)
wenzelm@10736
   716
fun slow_step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   717
        (inst_step_tac cs APPEND' haz_step_tac cs) i;
clasohm@0
   718
paulson@1800
   719
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   720
clasohm@0
   721
(*Dumb but fast*)
wenzelm@10382
   722
fun fast_tac cs =
wenzelm@23594
   723
  ObjectLogic.atomize_prems_tac THEN' SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   724
clasohm@0
   725
(*Slower but smarter than fast_tac*)
wenzelm@10382
   726
fun best_tac cs =
wenzelm@23594
   727
  ObjectLogic.atomize_prems_tac THEN'
clasohm@0
   728
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   729
oheimb@9402
   730
(*even a bit smarter than best_tac*)
wenzelm@10382
   731
fun first_best_tac cs =
wenzelm@23594
   732
  ObjectLogic.atomize_prems_tac THEN'
oheimb@9402
   733
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (FIRSTGOAL (step_tac cs)));
oheimb@9402
   734
wenzelm@10382
   735
fun slow_tac cs =
wenzelm@23594
   736
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   737
  SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   738
wenzelm@10382
   739
fun slow_best_tac cs =
wenzelm@23594
   740
  ObjectLogic.atomize_prems_tac THEN'
clasohm@0
   741
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   742
lcp@681
   743
wenzelm@10736
   744
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*)
wenzelm@10736
   745
val weight_ASTAR = ref 5;
paulson@1587
   746
wenzelm@10382
   747
fun astar_tac cs =
wenzelm@23594
   748
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   749
  SELECT_GOAL
wenzelm@10382
   750
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   751
      (step_tac cs 1));
paulson@1587
   752
wenzelm@10736
   753
fun slow_astar_tac cs =
wenzelm@23594
   754
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   755
  SELECT_GOAL
wenzelm@10382
   756
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   757
      (slow_step_tac cs 1));
paulson@1587
   758
paulson@1800
   759
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   760
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   761
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   762
  theorems such as 43. ****)
lcp@681
   763
lcp@747
   764
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   765
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   766
  greater search depth required.*)
wenzelm@10736
   767
fun dup_step_tac (cs as (CS{dup_netpair,...})) =
lcp@681
   768
  biresolve_from_nets_tac dup_netpair;
lcp@681
   769
oheimb@5523
   770
(*Searching to depth m. A variant called nodup_depth_tac appears in clasimp.ML*)
oheimb@5757
   771
local
wenzelm@10736
   772
fun slow_step_tac' cs = appWrappers cs
wenzelm@9938
   773
        (instp_step_tac cs APPEND' dup_step_tac cs);
wenzelm@10736
   774
in fun depth_tac cs m i state = SELECT_GOAL
wenzelm@10736
   775
   (safe_steps_tac cs 1 THEN_ELSE
wenzelm@9938
   776
        (DEPTH_SOLVE (depth_tac cs m 1),
wenzelm@9938
   777
         inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9938
   778
                (slow_step_tac' cs 1 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))
oheimb@5757
   779
        )) i state;
oheimb@5757
   780
end;
lcp@747
   781
wenzelm@10736
   782
(*Search, with depth bound m.
paulson@2173
   783
  This is the "entry point", which does safe inferences first.*)
wenzelm@10736
   784
fun safe_depth_tac cs m =
wenzelm@10736
   785
  SUBGOAL
lcp@681
   786
    (fn (prem,i) =>
lcp@681
   787
      let val deti =
wenzelm@9938
   788
          (*No Vars in the goal?  No need to backtrack between goals.*)
wenzelm@29267
   789
          if exists_subterm (fn Var _ => true | _ => false) prem then DETERM else I
wenzelm@10736
   790
      in  SELECT_GOAL (TRY (safe_tac cs) THEN
wenzelm@9938
   791
                       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   792
      end);
lcp@681
   793
paulson@2868
   794
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   795
wenzelm@4079
   796
berghofe@1724
   797
wenzelm@15036
   798
(** context dependent claset components **)
wenzelm@15036
   799
wenzelm@15036
   800
datatype context_cs = ContextCS of
wenzelm@15036
   801
 {swrappers: (string * (Proof.context -> wrapper)) list,
wenzelm@15036
   802
  uwrappers: (string * (Proof.context -> wrapper)) list};
wenzelm@15036
   803
wenzelm@15036
   804
fun context_cs ctxt cs (ContextCS {swrappers, uwrappers}) =
wenzelm@15036
   805
  let
wenzelm@15036
   806
    fun add_wrapper add (name, f) claset = add (claset, (name, f ctxt));
wenzelm@15036
   807
  in
haftmann@22674
   808
    cs
haftmann@22674
   809
    |> fold_rev (add_wrapper (op addSWrapper)) swrappers
wenzelm@15036
   810
    |> fold_rev (add_wrapper (op addWrapper)) uwrappers
wenzelm@15036
   811
  end;
wenzelm@15036
   812
wenzelm@15036
   813
fun make_context_cs (swrappers, uwrappers) =
wenzelm@15036
   814
  ContextCS {swrappers = swrappers, uwrappers = uwrappers};
wenzelm@15036
   815
wenzelm@15036
   816
val empty_context_cs = make_context_cs ([], []);
wenzelm@15036
   817
wenzelm@15036
   818
fun merge_context_cs (ctxt_cs1, ctxt_cs2) =
wenzelm@24358
   819
  if pointer_eq (ctxt_cs1, ctxt_cs2) then ctxt_cs1
wenzelm@24358
   820
  else
wenzelm@24358
   821
    let
wenzelm@24358
   822
      val ContextCS {swrappers = swrappers1, uwrappers = uwrappers1} = ctxt_cs1;
wenzelm@24358
   823
      val ContextCS {swrappers = swrappers2, uwrappers = uwrappers2} = ctxt_cs2;
wenzelm@24358
   824
      val swrappers' = AList.merge (op =) (K true) (swrappers1, swrappers2);
wenzelm@24358
   825
      val uwrappers' = AList.merge (op =) (K true) (uwrappers1, uwrappers2);
wenzelm@24358
   826
    in make_context_cs (swrappers', uwrappers') end;
wenzelm@15036
   827
wenzelm@15036
   828
wenzelm@15036
   829
wenzelm@17880
   830
(** claset data **)
wenzelm@4079
   831
wenzelm@24021
   832
(* global clasets *)
berghofe@1724
   833
wenzelm@16424
   834
structure GlobalClaset = TheoryDataFun
wenzelm@22846
   835
(
wenzelm@26497
   836
  type T = claset * context_cs;
wenzelm@26497
   837
  val empty = (empty_cs, empty_context_cs);
wenzelm@26497
   838
  val copy = I;
wenzelm@26497
   839
  val extend = I;
wenzelm@26497
   840
  fun merge _ ((cs1, ctxt_cs1), (cs2, ctxt_cs2)) =
wenzelm@26497
   841
    (merge_cs (cs1, cs2), merge_context_cs (ctxt_cs1, ctxt_cs2));
wenzelm@22846
   842
);
berghofe@1724
   843
wenzelm@26497
   844
val get_claset = #1 o GlobalClaset.get;
wenzelm@26497
   845
val map_claset = GlobalClaset.map o apfst;
wenzelm@17880
   846
wenzelm@15036
   847
val get_context_cs = #2 o GlobalClaset.get o ProofContext.theory_of;
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
wenzelm@18534
   851
fun claset_of thy =
wenzelm@26497
   852
  let val (cs, ctxt_cs) = GlobalClaset.get thy
wenzelm@26497
   853
  in context_cs (ProofContext.init thy) cs (ctxt_cs) end;
paulson@3727
   854
wenzelm@4079
   855
wenzelm@15036
   856
(* context dependent components *)
wenzelm@15036
   857
wenzelm@26497
   858
fun add_context_safe_wrapper wrapper = map_context_cs (apfst ((AList.update (op =) wrapper)));
wenzelm@26497
   859
fun del_context_safe_wrapper name = map_context_cs (apfst ((AList.delete (op =) name)));
wenzelm@15036
   860
wenzelm@26497
   861
fun add_context_unsafe_wrapper wrapper = map_context_cs (apsnd ((AList.update (op =) wrapper)));
wenzelm@26497
   862
fun del_context_unsafe_wrapper name = map_context_cs (apsnd ((AList.delete (op =) name)));
wenzelm@15036
   863
wenzelm@15036
   864
wenzelm@24021
   865
(* local clasets *)
wenzelm@5841
   866
wenzelm@16424
   867
structure LocalClaset = ProofDataFun
wenzelm@22846
   868
(
wenzelm@5841
   869
  type T = claset;
wenzelm@17880
   870
  val init = get_claset;
wenzelm@22846
   871
);
wenzelm@5841
   872
wenzelm@15036
   873
fun local_claset_of ctxt =
wenzelm@26497
   874
  context_cs ctxt (LocalClaset.get ctxt) (get_context_cs ctxt);
wenzelm@22846
   875
wenzelm@5841
   876
wenzelm@24021
   877
(* generic clasets *)
wenzelm@24021
   878
wenzelm@26497
   879
val get_cs = Context.cases claset_of local_claset_of;
wenzelm@26497
   880
fun map_cs f = Context.mapping (map_claset f) (LocalClaset.map f);
wenzelm@24021
   881
wenzelm@24021
   882
wenzelm@5885
   883
(* attributes *)
wenzelm@5885
   884
wenzelm@18728
   885
fun attrib f = Thm.declaration_attribute (fn th =>
wenzelm@26497
   886
  Context.mapping (map_claset (f th)) (LocalClaset.map (f th)));
wenzelm@5885
   887
paulson@21689
   888
fun safe_dest w = attrib (addSE w o make_elim);
wenzelm@18691
   889
val safe_elim = attrib o addSE;
wenzelm@18691
   890
val safe_intro = attrib o addSI;
paulson@21689
   891
fun haz_dest w = attrib (addE w o make_elim);
wenzelm@18691
   892
val haz_elim = attrib o addE;
wenzelm@18691
   893
val haz_intro = attrib o addI;
wenzelm@18691
   894
val rule_del = attrib delrule o ContextRules.rule_del;
wenzelm@5885
   895
wenzelm@5885
   896
wenzelm@10736
   897
end;
wenzelm@5841
   898
wenzelm@5841
   899
wenzelm@5841
   900
wenzelm@5885
   901
(** concrete syntax of attributes **)
wenzelm@5841
   902
wenzelm@5841
   903
val introN = "intro";
wenzelm@5841
   904
val elimN = "elim";
wenzelm@5841
   905
val destN = "dest";
wenzelm@9938
   906
val ruleN = "rule";
wenzelm@5841
   907
wenzelm@30528
   908
val setup_attrs =
wenzelm@30528
   909
  Attrib.setup @{binding swapped} (Scan.succeed swapped)
wenzelm@30528
   910
    "classical swap of introduction rule" #>
wenzelm@30528
   911
  Attrib.setup @{binding dest} (ContextRules.add safe_dest haz_dest ContextRules.dest_query)
wenzelm@30528
   912
    "declaration of Classical destruction rule" #>
wenzelm@30528
   913
  Attrib.setup @{binding elim} (ContextRules.add safe_elim haz_elim ContextRules.elim_query)
wenzelm@30528
   914
    "declaration of Classical elimination rule" #>
wenzelm@30528
   915
  Attrib.setup @{binding intro} (ContextRules.add safe_intro haz_intro ContextRules.intro_query)
wenzelm@30528
   916
    "declaration of Classical introduction rule" #>
wenzelm@30528
   917
  Attrib.setup @{binding rule} (Scan.lift Args.del >> K rule_del)
wenzelm@30528
   918
    "remove declaration of intro/elim/dest rule";
wenzelm@5841
   919
wenzelm@5841
   920
wenzelm@5841
   921
wenzelm@7230
   922
(** proof methods **)
wenzelm@7230
   923
wenzelm@7230
   924
local
wenzelm@7230
   925
wenzelm@30609
   926
fun some_rule_tac ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@5841
   927
  let
wenzelm@12401
   928
    val [rules1, rules2, rules4] = ContextRules.find_rules false facts goal ctxt;
wenzelm@30609
   929
    val CS {xtra_netpair, ...} = local_claset_of ctxt;
wenzelm@12401
   930
    val rules3 = ContextRules.find_rules_netpair true facts goal xtra_netpair;
wenzelm@12376
   931
    val rules = rules1 @ rules2 @ rules3 @ rules4;
wenzelm@18223
   932
    val ruleq = Drule.multi_resolves facts rules;
wenzelm@12376
   933
  in
wenzelm@12376
   934
    Method.trace ctxt rules;
wenzelm@12376
   935
    fn st => Seq.flat (Seq.map (fn rule => Tactic.rtac rule i st) ruleq)
wenzelm@18834
   936
  end)
wenzelm@21687
   937
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@5841
   938
wenzelm@30609
   939
in
wenzelm@7281
   940
wenzelm@30609
   941
fun rule_tac ctxt [] facts = some_rule_tac ctxt facts
wenzelm@30609
   942
  | rule_tac _ rules facts = Method.rule_tac rules facts;
wenzelm@30609
   943
wenzelm@30609
   944
fun default_tac ctxt rules facts =
wenzelm@30609
   945
  HEADGOAL (rule_tac ctxt rules facts) ORELSE
haftmann@26470
   946
  Class.default_intro_tac ctxt facts;
wenzelm@10309
   947
wenzelm@7230
   948
end;
wenzelm@5841
   949
wenzelm@5841
   950
wenzelm@7230
   951
(* contradiction method *)
wenzelm@6502
   952
wenzelm@7425
   953
val contradiction = Method.rule [Data.not_elim, Data.not_elim COMP Drule.swap_prems_rl];
wenzelm@6502
   954
wenzelm@6502
   955
wenzelm@6502
   956
(* automatic methods *)
wenzelm@5841
   957
wenzelm@5927
   958
val cla_modifiers =
wenzelm@18728
   959
 [Args.$$$ destN -- Args.bang_colon >> K ((I, safe_dest NONE): Method.modifier),
wenzelm@18728
   960
  Args.$$$ destN -- Args.colon >> K (I, haz_dest NONE),
wenzelm@18728
   961
  Args.$$$ elimN -- Args.bang_colon >> K (I, safe_elim NONE),
wenzelm@18728
   962
  Args.$$$ elimN -- Args.colon >> K (I, haz_elim NONE),
wenzelm@18728
   963
  Args.$$$ introN -- Args.bang_colon >> K (I, safe_intro NONE),
wenzelm@18728
   964
  Args.$$$ introN -- Args.colon >> K (I, haz_intro NONE),
wenzelm@18728
   965
  Args.del -- Args.colon >> K (I, rule_del)];
wenzelm@5927
   966
wenzelm@30510
   967
fun cla_meth tac prems ctxt = METHOD (fn facts =>
wenzelm@15036
   968
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (local_claset_of ctxt));
wenzelm@7132
   969
wenzelm@30510
   970
fun cla_meth' tac prems ctxt = METHOD (fn facts =>
wenzelm@15036
   971
  HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (local_claset_of ctxt)));
wenzelm@5841
   972
wenzelm@30541
   973
fun cla_method tac = Args.bang_facts --| Method.sections cla_modifiers >> (cla_meth tac);
wenzelm@30541
   974
fun cla_method' tac = Args.bang_facts --| Method.sections cla_modifiers >> (cla_meth' tac);
wenzelm@5841
   975
wenzelm@5841
   976
wenzelm@5841
   977
wenzelm@5841
   978
(** setup_methods **)
wenzelm@5841
   979
wenzelm@30541
   980
val setup_methods =
wenzelm@30609
   981
  Method.setup @{binding default}
wenzelm@30609
   982
   (Attrib.thms >> (fn rules => fn ctxt => METHOD (default_tac ctxt rules)))
wenzelm@30541
   983
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30609
   984
  Method.setup @{binding rule}
wenzelm@30609
   985
    (Attrib.thms >> (fn rules => fn ctxt => METHOD (HEADGOAL o rule_tac ctxt rules)))
wenzelm@30541
   986
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30541
   987
  Method.setup @{binding contradiction} (Scan.succeed (K contradiction))
wenzelm@30541
   988
    "proof by contradiction" #>
wenzelm@30541
   989
  Method.setup @{binding clarify} (cla_method' (CHANGED_PROP oo clarify_tac))
wenzelm@30541
   990
    "repeatedly apply safe steps" #>
wenzelm@30541
   991
  Method.setup @{binding fast} (cla_method' fast_tac) "classical prover (depth-first)" #>
wenzelm@30541
   992
  Method.setup @{binding slow} (cla_method' slow_tac) "classical prover (slow depth-first)" #>
wenzelm@30541
   993
  Method.setup @{binding best} (cla_method' best_tac) "classical prover (best-first)" #>
wenzelm@30541
   994
  Method.setup @{binding deepen} (cla_method' (fn cs => deepen_tac cs 4))
wenzelm@30541
   995
    "classical prover (iterative deepening)" #>
wenzelm@30541
   996
  Method.setup @{binding safe} (cla_method (CHANGED_PROP o safe_tac))
wenzelm@30541
   997
    "classical prover (apply safe rules)";
wenzelm@5841
   998
wenzelm@5841
   999
wenzelm@5841
  1000
wenzelm@5841
  1001
(** theory setup **)
wenzelm@5841
  1002
wenzelm@26497
  1003
val setup = setup_attrs #> setup_methods;
wenzelm@5841
  1004
wenzelm@5841
  1005
wenzelm@8667
  1006
wenzelm@8667
  1007
(** outer syntax **)
wenzelm@8667
  1008
wenzelm@24867
  1009
val _ =
wenzelm@8667
  1010
  OuterSyntax.improper_command "print_claset" "print context of Classical Reasoner"
wenzelm@17057
  1011
    OuterKeyword.diag
wenzelm@26497
  1012
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@26497
  1013
      Toplevel.keep (print_cs o local_claset_of o Toplevel.context_of)));
wenzelm@8667
  1014
wenzelm@5841
  1015
end;