src/Provers/classical.ML
author wenzelm
Sun Nov 01 15:44:26 2009 +0100 (2009-11-01)
changeset 33369 470a7b233ee5
parent 33339 d41f77196338
child 33519 e31a85f92ce9
permissions -rw-r--r--
modernized structure Context_Rules;
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@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@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@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@33369
   227
         xtra_netpair   : Context_Rules.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@32091
   259
  let val pretty_thms = map Display.pretty_thm_without_context 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@32091
   316
  if mem_thm safeIs th then
wenzelm@32091
   317
    warning ("Rule already declared as safe introduction (intro!)\n" ^
wenzelm@32091
   318
      Display.string_of_thm_without_context th)
wenzelm@18691
   319
  else if mem_thm safeEs th then
wenzelm@32091
   320
    warning ("Rule already declared as safe elimination (elim!)\n" ^
wenzelm@32091
   321
      Display.string_of_thm_without_context th)
wenzelm@18691
   322
  else if mem_thm hazIs th then
wenzelm@32091
   323
    warning ("Rule already declared as introduction (intro)\n" ^
wenzelm@32091
   324
      Display.string_of_thm_without_context th)
wenzelm@18691
   325
  else if mem_thm hazEs th then
wenzelm@32091
   326
    warning ("Rule already declared as elimination (elim)\n" ^
wenzelm@32091
   327
      Display.string_of_thm_without_context th)
paulson@1927
   328
  else ();
paulson@1927
   329
wenzelm@12376
   330
paulson@1800
   331
(*** Safe rules ***)
lcp@982
   332
wenzelm@18691
   333
fun addSI w th
wenzelm@18534
   334
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   335
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   336
  if mem_thm safeIs th then
wenzelm@32091
   337
    (warning ("Ignoring duplicate safe introduction (intro!)\n" ^
wenzelm@32091
   338
      Display.string_of_thm_without_context th); cs)
paulson@1927
   339
  else
wenzelm@23594
   340
  let val th' = flat_rule th
wenzelm@23594
   341
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@23594
   342
          List.partition Thm.no_prems [th']
paulson@1927
   343
      val nI = length safeIs + 1
lcp@1073
   344
      and nE = length safeEs
paulson@1927
   345
  in warn_dup th cs;
wenzelm@9938
   346
     CS{safeIs  = th::safeIs,
lcp@1073
   347
        safe0_netpair = insert (nI,nE) (safe0_rls, []) safe0_netpair,
wenzelm@9938
   348
        safep_netpair = insert (nI,nE) (safep_rls, []) safep_netpair,
wenzelm@9938
   349
        safeEs  = safeEs,
wenzelm@9938
   350
        hazIs   = hazIs,
wenzelm@9938
   351
        hazEs   = hazEs,
wenzelm@9938
   352
        swrappers    = swrappers,
wenzelm@9938
   353
        uwrappers    = uwrappers,
wenzelm@9938
   354
        haz_netpair  = haz_netpair,
wenzelm@9938
   355
        dup_netpair  = dup_netpair,
wenzelm@18691
   356
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([th], []) xtra_netpair}
lcp@1073
   357
  end;
lcp@1073
   358
wenzelm@18691
   359
fun addSE w th
wenzelm@18534
   360
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   361
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   362
  if mem_thm safeEs th then
wenzelm@32091
   363
    (warning ("Ignoring duplicate safe elimination (elim!)\n" ^
wenzelm@32091
   364
        Display.string_of_thm_without_context th); cs)
paulson@18557
   365
  else if has_fewer_prems 1 th then
wenzelm@32960
   366
        error ("Ill-formed elimination rule\n" ^ Display.string_of_thm_without_context th)
paulson@1927
   367
  else
wenzelm@18534
   368
  let
wenzelm@23594
   369
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   370
      val (safe0_rls, safep_rls) = (*0 subgoals vs 1 or more*)
wenzelm@18534
   371
          List.partition (fn rl => nprems_of rl=1) [th']
lcp@1073
   372
      val nI = length safeIs
paulson@1927
   373
      and nE = length safeEs + 1
paulson@1927
   374
  in warn_dup th cs;
wenzelm@9938
   375
     CS{safeEs  = th::safeEs,
lcp@1073
   376
        safe0_netpair = insert (nI,nE) ([], safe0_rls) safe0_netpair,
wenzelm@9938
   377
        safep_netpair = insert (nI,nE) ([], safep_rls) safep_netpair,
wenzelm@9938
   378
        safeIs  = safeIs,
wenzelm@9938
   379
        hazIs   = hazIs,
wenzelm@9938
   380
        hazEs   = hazEs,
wenzelm@9938
   381
        swrappers    = swrappers,
wenzelm@9938
   382
        uwrappers    = uwrappers,
wenzelm@9938
   383
        haz_netpair  = haz_netpair,
wenzelm@9938
   384
        dup_netpair  = dup_netpair,
wenzelm@18691
   385
        xtra_netpair = insert' (the_default 0 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   386
  end;
clasohm@0
   387
wenzelm@18691
   388
fun cs addSIs ths = fold_rev (addSI NONE) ths cs;
wenzelm@18691
   389
fun cs addSEs ths = fold_rev (addSE NONE) ths cs;
paulson@1927
   390
paulson@21689
   391
fun make_elim th =
paulson@18557
   392
    if has_fewer_prems 1 th then
wenzelm@32960
   393
          error ("Ill-formed destruction rule\n" ^ Display.string_of_thm_without_context th)
paulson@21689
   394
    else Tactic.make_elim th;
paulson@17084
   395
paulson@21689
   396
fun cs addSDs ths = cs addSEs (map make_elim ths);
clasohm@0
   397
lcp@1073
   398
paulson@1800
   399
(*** Hazardous (unsafe) rules ***)
clasohm@0
   400
wenzelm@18691
   401
fun addI w th
wenzelm@18534
   402
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   403
             safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   404
  if mem_thm hazIs th then
wenzelm@32091
   405
    (warning ("Ignoring duplicate introduction (intro)\n" ^
wenzelm@32091
   406
        Display.string_of_thm_without_context th); cs)
paulson@1927
   407
  else
wenzelm@23594
   408
  let val th' = flat_rule th
wenzelm@23594
   409
      val nI = length hazIs + 1
lcp@1073
   410
      and nE = length hazEs
paulson@1927
   411
  in warn_dup th cs;
wenzelm@9938
   412
     CS{hazIs   = th::hazIs,
wenzelm@23594
   413
        haz_netpair = insert (nI,nE) ([th'], []) haz_netpair,
wenzelm@23594
   414
        dup_netpair = insert (nI,nE) (map dup_intr [th'], []) dup_netpair,
wenzelm@10736
   415
        safeIs  = safeIs,
wenzelm@9938
   416
        safeEs  = safeEs,
wenzelm@9938
   417
        hazEs   = hazEs,
wenzelm@9938
   418
        swrappers     = swrappers,
wenzelm@9938
   419
        uwrappers     = uwrappers,
wenzelm@9938
   420
        safe0_netpair = safe0_netpair,
wenzelm@9938
   421
        safep_netpair = safep_netpair,
wenzelm@18691
   422
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([th], []) xtra_netpair}
paulson@18557
   423
  end
paulson@18557
   424
  handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@32091
   425
    error ("Ill-formed introduction rule\n" ^ Display.string_of_thm_without_context th);
lcp@1073
   426
wenzelm@18691
   427
fun addE w th
wenzelm@18534
   428
  (cs as CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@18534
   429
            safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   430
  if mem_thm hazEs th then
wenzelm@32091
   431
    (warning ("Ignoring duplicate elimination (elim)\n" ^
wenzelm@32091
   432
        Display.string_of_thm_without_context th); cs)
paulson@18557
   433
  else if has_fewer_prems 1 th then
wenzelm@32960
   434
        error("Ill-formed elimination rule\n" ^ Display.string_of_thm_without_context th)
paulson@1927
   435
  else
wenzelm@18534
   436
  let
wenzelm@23594
   437
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   438
      val nI = length hazIs
paulson@1927
   439
      and nE = length hazEs + 1
paulson@1927
   440
  in warn_dup th cs;
wenzelm@9938
   441
     CS{hazEs   = th::hazEs,
wenzelm@18534
   442
        haz_netpair = insert (nI,nE) ([], [th']) haz_netpair,
wenzelm@18534
   443
        dup_netpair = insert (nI,nE) ([], map dup_elim [th']) dup_netpair,
wenzelm@10736
   444
        safeIs  = safeIs,
wenzelm@9938
   445
        safeEs  = safeEs,
wenzelm@9938
   446
        hazIs   = hazIs,
wenzelm@9938
   447
        swrappers     = swrappers,
wenzelm@9938
   448
        uwrappers     = uwrappers,
wenzelm@9938
   449
        safe0_netpair = safe0_netpair,
wenzelm@9938
   450
        safep_netpair = safep_netpair,
wenzelm@18691
   451
        xtra_netpair = insert' (the_default 1 w) (nI,nE) ([], [th]) xtra_netpair}
lcp@1073
   452
  end;
clasohm@0
   453
wenzelm@18691
   454
fun cs addIs ths = fold_rev (addI NONE) ths cs;
wenzelm@18691
   455
fun cs addEs ths = fold_rev (addE NONE) ths cs;
paulson@1927
   456
paulson@21689
   457
fun cs addDs ths = cs addEs (map make_elim ths);
clasohm@0
   458
lcp@1073
   459
wenzelm@10736
   460
(*** Deletion of rules
paulson@1800
   461
     Working out what to delete, requires repeating much of the code used
wenzelm@9938
   462
        to insert.
paulson@1800
   463
***)
paulson@1800
   464
wenzelm@10736
   465
fun delSI th
wenzelm@12376
   466
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   467
                    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   468
 if mem_thm safeIs th then
wenzelm@23594
   469
   let val th' = flat_rule th
wenzelm@23594
   470
       val (safe0_rls, safep_rls) = List.partition Thm.no_prems [th']
paulson@2813
   471
   in CS{safe0_netpair = delete (safe0_rls, []) safe0_netpair,
wenzelm@9938
   472
         safep_netpair = delete (safep_rls, []) safep_netpair,
wenzelm@18691
   473
         safeIs = rem_thm th safeIs,
wenzelm@9938
   474
         safeEs = safeEs,
wenzelm@9938
   475
         hazIs  = hazIs,
wenzelm@9938
   476
         hazEs  = hazEs,
wenzelm@9938
   477
         swrappers    = swrappers,
wenzelm@9938
   478
         uwrappers    = uwrappers,
wenzelm@9938
   479
         haz_netpair  = haz_netpair,
wenzelm@9938
   480
         dup_netpair  = dup_netpair,
wenzelm@12401
   481
         xtra_netpair = delete' ([th], []) xtra_netpair}
paulson@2813
   482
   end
paulson@2813
   483
 else cs;
paulson@1800
   484
paulson@2813
   485
fun delSE th
wenzelm@12376
   486
          (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   487
                    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   488
  if mem_thm safeEs th then
wenzelm@18534
   489
    let
wenzelm@23594
   490
      val th' = classical_rule (flat_rule th)
wenzelm@18534
   491
      val (safe0_rls, safep_rls) = List.partition (fn rl => nprems_of rl=1) [th']
wenzelm@18534
   492
    in CS{safe0_netpair = delete ([], safe0_rls) safe0_netpair,
wenzelm@9938
   493
         safep_netpair = delete ([], safep_rls) safep_netpair,
wenzelm@9938
   494
         safeIs = safeIs,
wenzelm@18691
   495
         safeEs = rem_thm th safeEs,
wenzelm@9938
   496
         hazIs  = hazIs,
wenzelm@9938
   497
         hazEs  = hazEs,
wenzelm@9938
   498
         swrappers    = swrappers,
wenzelm@9938
   499
         uwrappers    = uwrappers,
wenzelm@9938
   500
         haz_netpair  = haz_netpair,
wenzelm@9938
   501
         dup_netpair  = dup_netpair,
wenzelm@12401
   502
         xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@18534
   503
    end
wenzelm@18534
   504
  else cs;
paulson@1800
   505
paulson@1800
   506
paulson@2813
   507
fun delI th
wenzelm@12376
   508
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   509
                   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@18691
   510
 if mem_thm hazIs th then
wenzelm@23594
   511
    let val th' = flat_rule th
wenzelm@23594
   512
    in CS{haz_netpair = delete ([th'], []) haz_netpair,
wenzelm@23594
   513
        dup_netpair = delete ([dup_intr th'], []) dup_netpair,
wenzelm@10736
   514
        safeIs  = safeIs,
wenzelm@9938
   515
        safeEs  = safeEs,
wenzelm@18691
   516
        hazIs   = rem_thm th hazIs,
wenzelm@9938
   517
        hazEs   = hazEs,
wenzelm@9938
   518
        swrappers     = swrappers,
wenzelm@9938
   519
        uwrappers     = uwrappers,
wenzelm@9938
   520
        safe0_netpair = safe0_netpair,
wenzelm@9938
   521
        safep_netpair = safep_netpair,
wenzelm@12401
   522
        xtra_netpair = delete' ([th], []) xtra_netpair}
wenzelm@23594
   523
    end
paulson@18557
   524
 else cs
paulson@18557
   525
 handle THM("RSN: no unifiers",_,_) => (*from dup_intr*)
wenzelm@32091
   526
   error ("Ill-formed introduction rule\n" ^ Display.string_of_thm_without_context th);
paulson@18557
   527
paulson@1800
   528
paulson@2813
   529
fun delE th
wenzelm@12376
   530
         (cs as CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
wenzelm@9938
   531
                   safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
wenzelm@23594
   532
 if mem_thm hazEs th then
wenzelm@23594
   533
   let val th' = classical_rule (flat_rule th)
wenzelm@23594
   534
   in CS{haz_netpair = delete ([], [th']) haz_netpair,
wenzelm@18534
   535
        dup_netpair = delete ([], [dup_elim th']) dup_netpair,
wenzelm@10736
   536
        safeIs  = safeIs,
wenzelm@9938
   537
        safeEs  = safeEs,
wenzelm@9938
   538
        hazIs   = hazIs,
wenzelm@18691
   539
        hazEs   = rem_thm th hazEs,
wenzelm@9938
   540
        swrappers     = swrappers,
wenzelm@9938
   541
        uwrappers     = uwrappers,
wenzelm@9938
   542
        safe0_netpair = safe0_netpair,
wenzelm@9938
   543
        safep_netpair = safep_netpair,
wenzelm@12401
   544
        xtra_netpair = delete' ([], [th]) xtra_netpair}
wenzelm@23594
   545
   end
wenzelm@23594
   546
 else cs;
paulson@1800
   547
paulson@2813
   548
(*Delete ALL occurrences of "th" in the claset (perhaps from several lists)*)
wenzelm@18534
   549
fun delrule th (cs as CS {safeIs, safeEs, hazIs, hazEs, ...}) =
wenzelm@18534
   550
  let val th' = Tactic.make_elim th in
wenzelm@18691
   551
    if mem_thm safeIs th orelse mem_thm safeEs th orelse
wenzelm@18691
   552
      mem_thm hazIs th orelse mem_thm hazEs th orelse
wenzelm@18691
   553
      mem_thm safeEs th' orelse mem_thm hazEs th'
wenzelm@12376
   554
    then delSI th (delSE th (delI th (delE th (delSE th' (delE th' cs)))))
wenzelm@32091
   555
    else (warning ("Undeclared classical rule\n" ^ Display.string_of_thm_without_context th); cs)
wenzelm@9938
   556
  end;
paulson@1800
   557
wenzelm@18534
   558
fun cs delrules ths = fold delrule ths cs;
paulson@1800
   559
paulson@1800
   560
oheimb@4767
   561
(*** Modifying the wrapper tacticals ***)
haftmann@22674
   562
fun map_swrappers f
haftmann@22674
   563
  (CS {safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   564
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   565
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   566
    swrappers = f swrappers, uwrappers = uwrappers,
oheimb@4767
   567
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   568
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   569
haftmann@22674
   570
fun map_uwrappers f
haftmann@22674
   571
  (CS{safeIs, safeEs, hazIs, hazEs, swrappers, uwrappers,
haftmann@22674
   572
    safe0_netpair, safep_netpair, haz_netpair, dup_netpair, xtra_netpair}) =
haftmann@22674
   573
  CS {safeIs = safeIs, safeEs = safeEs, hazIs = hazIs, hazEs = hazEs,
oheimb@4767
   574
    swrappers = swrappers, uwrappers = f uwrappers,
oheimb@4767
   575
    safe0_netpair = safe0_netpair, safep_netpair = safep_netpair,
wenzelm@6955
   576
    haz_netpair = haz_netpair, dup_netpair = dup_netpair, xtra_netpair = xtra_netpair};
oheimb@4767
   577
haftmann@22674
   578
fun update_warn msg (p as (key : string, _)) xs =
haftmann@22674
   579
  (if AList.defined (op =) xs key then warning msg else ();
haftmann@22674
   580
    AList.update (op =) p xs);
haftmann@22674
   581
haftmann@22674
   582
fun delete_warn msg (key : string) xs =
haftmann@22674
   583
  if AList.defined (op =) xs key then AList.delete (op =) key xs
haftmann@22674
   584
    else (warning msg; xs);
lcp@982
   585
oheimb@4651
   586
(*Add/replace a safe wrapper*)
haftmann@22674
   587
fun cs addSWrapper new_swrapper = map_swrappers
haftmann@22674
   588
  (update_warn ("Overwriting safe wrapper " ^ fst new_swrapper) new_swrapper) cs;
oheimb@4651
   589
oheimb@4651
   590
(*Add/replace an unsafe wrapper*)
haftmann@22674
   591
fun cs addWrapper new_uwrapper = map_uwrappers
haftmann@22674
   592
  (update_warn ("Overwriting unsafe wrapper " ^ fst new_uwrapper) new_uwrapper) cs;
lcp@982
   593
oheimb@4651
   594
(*Remove a safe wrapper*)
haftmann@22674
   595
fun cs delSWrapper name = map_swrappers
haftmann@22674
   596
  (delete_warn ("No such safe wrapper in claset: " ^ name) name) cs;
lcp@982
   597
oheimb@4651
   598
(*Remove an unsafe wrapper*)
haftmann@22674
   599
fun cs delWrapper name = map_uwrappers
haftmann@22674
   600
  (delete_warn ("No such unsafe wrapper in claset: " ^ name) name) cs;
lcp@982
   601
oheimb@11168
   602
(* compose a safe tactic alternatively before/after safe_step_tac *)
wenzelm@10736
   603
fun cs addSbefore  (name,    tac1) =
oheimb@5523
   604
    cs addSWrapper (name, fn tac2 => tac1 ORELSE' tac2);
oheimb@11181
   605
fun cs addSafter   (name,    tac2) =
oheimb@5523
   606
    cs addSWrapper (name, fn tac1 => tac1 ORELSE' tac2);
lcp@982
   607
oheimb@11168
   608
(*compose a tactic alternatively before/after the step tactic *)
wenzelm@10736
   609
fun cs addbefore   (name,    tac1) =
oheimb@5523
   610
    cs addWrapper  (name, fn tac2 => tac1 APPEND' tac2);
oheimb@11181
   611
fun cs addafter    (name,    tac2) =
oheimb@5523
   612
    cs addWrapper  (name, fn tac1 => tac1 APPEND' tac2);
oheimb@4767
   613
wenzelm@10736
   614
fun cs addD2     (name, thm) =
oheimb@11181
   615
    cs addafter  (name, datac thm 1);
wenzelm@10736
   616
fun cs addE2     (name, thm) =
oheimb@11181
   617
    cs addafter  (name, eatac thm 1);
oheimb@11181
   618
fun cs addSD2    (name, thm) =
oheimb@11181
   619
    cs addSafter (name, dmatch_tac [thm] THEN' eq_assume_tac);
oheimb@11181
   620
fun cs addSE2    (name, thm) =
oheimb@11181
   621
    cs addSafter (name, ematch_tac [thm] THEN' eq_assume_tac);
lcp@982
   622
paulson@1711
   623
(*Merge works by adding all new rules of the 2nd claset into the 1st claset.
paulson@1711
   624
  Merging the term nets may look more efficient, but the rather delicate
paulson@1711
   625
  treatment of priority might get muddled up.*)
haftmann@22674
   626
fun merge_cs (cs as CS {safeIs, safeEs, hazIs, hazEs, ...},
wenzelm@24358
   627
    cs' as CS {safeIs = safeIs2, safeEs = safeEs2, hazIs = hazIs2, hazEs = hazEs2,
haftmann@22674
   628
      swrappers, uwrappers, ...}) =
wenzelm@24358
   629
  if pointer_eq (cs, cs') then cs
wenzelm@24358
   630
  else
wenzelm@24358
   631
    let
wenzelm@24358
   632
      val safeIs' = fold rem_thm safeIs safeIs2;
wenzelm@24358
   633
      val safeEs' = fold rem_thm safeEs safeEs2;
wenzelm@24358
   634
      val hazIs' = fold rem_thm hazIs hazIs2;
wenzelm@24358
   635
      val hazEs' = fold rem_thm hazEs hazEs2;
wenzelm@24358
   636
      val cs1   = cs addSIs safeIs'
wenzelm@24358
   637
                     addSEs safeEs'
wenzelm@24358
   638
                     addIs  hazIs'
wenzelm@24358
   639
                     addEs  hazEs';
wenzelm@24358
   640
      val cs2 = map_swrappers
wenzelm@24358
   641
        (fn ws => AList.merge (op =) (K true) (ws, swrappers)) cs1;
wenzelm@24358
   642
      val cs3 = map_uwrappers
wenzelm@24358
   643
        (fn ws => AList.merge (op =) (K true) (ws, uwrappers)) cs2;
wenzelm@24358
   644
    in cs3 end;
paulson@1711
   645
lcp@982
   646
paulson@1800
   647
(**** Simple tactics for theorem proving ****)
clasohm@0
   648
clasohm@0
   649
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   650
fun safe_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   651
  appSWrappers cs (FIRST' [
wenzelm@9938
   652
        eq_assume_tac,
wenzelm@9938
   653
        eq_mp_tac,
wenzelm@9938
   654
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   655
        FIRST' hyp_subst_tacs,
wenzelm@9938
   656
        bimatch_from_nets_tac safep_netpair]);
clasohm@0
   657
oheimb@5757
   658
(*Repeatedly attack a subgoal using safe inferences -- it's deterministic!*)
wenzelm@10736
   659
fun safe_steps_tac cs = REPEAT_DETERM1 o
wenzelm@9938
   660
        (fn i => COND (has_fewer_prems i) no_tac (safe_step_tac cs i));
oheimb@5757
   661
clasohm@0
   662
(*Repeatedly attack subgoals using safe inferences -- it's deterministic!*)
oheimb@5757
   663
fun safe_tac cs = REPEAT_DETERM1 (FIRSTGOAL (safe_steps_tac cs));
lcp@747
   664
paulson@3705
   665
paulson@3705
   666
(*** Clarify_tac: do safe steps without causing branching ***)
paulson@3705
   667
paulson@3705
   668
fun nsubgoalsP n (k,brl) = (subgoals_of_brl brl = n);
paulson@3705
   669
paulson@3705
   670
(*version of bimatch_from_nets_tac that only applies rules that
paulson@3705
   671
  create precisely n subgoals.*)
wenzelm@10736
   672
fun n_bimatch_from_nets_tac n =
wenzelm@33317
   673
    biresolution_from_nets_tac (order_list o filter (nsubgoalsP n)) true;
paulson@3705
   674
paulson@3705
   675
fun eq_contr_tac i = ematch_tac [not_elim] i  THEN  eq_assume_tac i;
paulson@3705
   676
val eq_assume_contr_tac = eq_assume_tac ORELSE' eq_contr_tac;
paulson@3705
   677
paulson@3705
   678
(*Two-way branching is allowed only if one of the branches immediately closes*)
paulson@3705
   679
fun bimatch2_tac netpair i =
paulson@3705
   680
    n_bimatch_from_nets_tac 2 netpair i THEN
paulson@3705
   681
    (eq_assume_contr_tac i ORELSE eq_assume_contr_tac (i+1));
paulson@3705
   682
paulson@3705
   683
(*Attack subgoals using safe inferences -- matching, not resolution*)
wenzelm@10736
   684
fun clarify_step_tac (cs as CS{safe0_netpair,safep_netpair,...}) =
oheimb@4651
   685
  appSWrappers cs (FIRST' [
wenzelm@9938
   686
        eq_assume_contr_tac,
wenzelm@9938
   687
        bimatch_from_nets_tac safe0_netpair,
wenzelm@9938
   688
        FIRST' hyp_subst_tacs,
wenzelm@9938
   689
        n_bimatch_from_nets_tac 1 safep_netpair,
paulson@3705
   690
        bimatch2_tac safep_netpair]);
paulson@3705
   691
paulson@3705
   692
fun clarify_tac cs = SELECT_GOAL (REPEAT_DETERM (clarify_step_tac cs 1));
paulson@3705
   693
paulson@3705
   694
paulson@3705
   695
(*** Unsafe steps instantiate variables or lose information ***)
paulson@3705
   696
paulson@4066
   697
(*Backtracking is allowed among the various these unsafe ways of
paulson@4066
   698
  proving a subgoal.  *)
wenzelm@32862
   699
fun inst0_step_tac (CS {safe0_netpair, ...}) =
wenzelm@32862
   700
  assume_tac APPEND'
wenzelm@32862
   701
  contr_tac APPEND'
lcp@747
   702
  biresolve_from_nets_tac safe0_netpair;
lcp@747
   703
paulson@4066
   704
(*These unsafe steps could generate more subgoals.*)
wenzelm@32862
   705
fun instp_step_tac (CS {safep_netpair, ...}) =
lcp@747
   706
  biresolve_from_nets_tac safep_netpair;
clasohm@0
   707
clasohm@0
   708
(*These steps could instantiate variables and are therefore unsafe.*)
lcp@747
   709
fun inst_step_tac cs = inst0_step_tac cs APPEND' instp_step_tac cs;
clasohm@0
   710
wenzelm@10736
   711
fun haz_step_tac (CS{haz_netpair,...}) =
lcp@681
   712
  biresolve_from_nets_tac haz_netpair;
lcp@681
   713
clasohm@0
   714
(*Single step for the prover.  FAILS unless it makes progress. *)
wenzelm@10736
   715
fun step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   716
        (inst_step_tac cs ORELSE' haz_step_tac cs) i;
clasohm@0
   717
clasohm@0
   718
(*Using a "safe" rule to instantiate variables is unsafe.  This tactic
clasohm@0
   719
  allows backtracking from "safe" rules to "unsafe" rules here.*)
wenzelm@10736
   720
fun slow_step_tac cs i = safe_tac cs ORELSE appWrappers cs
wenzelm@9938
   721
        (inst_step_tac cs APPEND' haz_step_tac cs) i;
clasohm@0
   722
paulson@1800
   723
(**** The following tactics all fail unless they solve one goal ****)
clasohm@0
   724
clasohm@0
   725
(*Dumb but fast*)
wenzelm@10382
   726
fun fast_tac cs =
wenzelm@23594
   727
  ObjectLogic.atomize_prems_tac THEN' SELECT_GOAL (DEPTH_SOLVE (step_tac cs 1));
clasohm@0
   728
clasohm@0
   729
(*Slower but smarter than fast_tac*)
wenzelm@10382
   730
fun best_tac cs =
wenzelm@23594
   731
  ObjectLogic.atomize_prems_tac THEN'
clasohm@0
   732
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (step_tac cs 1));
clasohm@0
   733
oheimb@9402
   734
(*even a bit smarter than best_tac*)
wenzelm@10382
   735
fun first_best_tac cs =
wenzelm@23594
   736
  ObjectLogic.atomize_prems_tac THEN'
oheimb@9402
   737
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (FIRSTGOAL (step_tac cs)));
oheimb@9402
   738
wenzelm@10382
   739
fun slow_tac cs =
wenzelm@23594
   740
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   741
  SELECT_GOAL (DEPTH_SOLVE (slow_step_tac cs 1));
clasohm@0
   742
wenzelm@10382
   743
fun slow_best_tac cs =
wenzelm@23594
   744
  ObjectLogic.atomize_prems_tac THEN'
clasohm@0
   745
  SELECT_GOAL (BEST_FIRST (has_fewer_prems 1, sizef) (slow_step_tac cs 1));
clasohm@0
   746
lcp@681
   747
wenzelm@10736
   748
(***ASTAR with weight weight_ASTAR, by Norbert Voelker*)
wenzelm@32740
   749
val weight_ASTAR = Unsynchronized.ref 5;
paulson@1587
   750
wenzelm@10382
   751
fun astar_tac cs =
wenzelm@23594
   752
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   753
  SELECT_GOAL
wenzelm@10382
   754
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   755
      (step_tac cs 1));
paulson@1587
   756
wenzelm@10736
   757
fun slow_astar_tac cs =
wenzelm@23594
   758
  ObjectLogic.atomize_prems_tac THEN'
wenzelm@10382
   759
  SELECT_GOAL
wenzelm@10382
   760
    (ASTAR (has_fewer_prems 1, fn lev => fn thm => size_of_thm thm + !weight_ASTAR * lev)
wenzelm@10382
   761
      (slow_step_tac cs 1));
paulson@1587
   762
paulson@1800
   763
(**** Complete tactic, loosely based upon LeanTaP.  This tactic is the outcome
lcp@747
   764
  of much experimentation!  Changing APPEND to ORELSE below would prove
lcp@747
   765
  easy theorems faster, but loses completeness -- and many of the harder
paulson@1800
   766
  theorems such as 43. ****)
lcp@681
   767
lcp@747
   768
(*Non-deterministic!  Could always expand the first unsafe connective.
lcp@747
   769
  That's hard to implement and did not perform better in experiments, due to
lcp@747
   770
  greater search depth required.*)
wenzelm@32863
   771
fun dup_step_tac (CS {dup_netpair, ...}) =
lcp@681
   772
  biresolve_from_nets_tac dup_netpair;
lcp@681
   773
oheimb@5523
   774
(*Searching to depth m. A variant called nodup_depth_tac appears in clasimp.ML*)
oheimb@5757
   775
local
wenzelm@10736
   776
fun slow_step_tac' cs = appWrappers cs
wenzelm@9938
   777
        (instp_step_tac cs APPEND' dup_step_tac cs);
wenzelm@10736
   778
in fun depth_tac cs m i state = SELECT_GOAL
wenzelm@10736
   779
   (safe_steps_tac cs 1 THEN_ELSE
wenzelm@9938
   780
        (DEPTH_SOLVE (depth_tac cs m 1),
wenzelm@9938
   781
         inst0_step_tac cs 1 APPEND COND (K (m=0)) no_tac
wenzelm@9938
   782
                (slow_step_tac' cs 1 THEN DEPTH_SOLVE (depth_tac cs (m-1) 1))
oheimb@5757
   783
        )) i state;
oheimb@5757
   784
end;
lcp@747
   785
wenzelm@10736
   786
(*Search, with depth bound m.
paulson@2173
   787
  This is the "entry point", which does safe inferences first.*)
wenzelm@10736
   788
fun safe_depth_tac cs m =
wenzelm@10736
   789
  SUBGOAL
lcp@681
   790
    (fn (prem,i) =>
lcp@681
   791
      let val deti =
wenzelm@9938
   792
          (*No Vars in the goal?  No need to backtrack between goals.*)
wenzelm@29267
   793
          if exists_subterm (fn Var _ => true | _ => false) prem then DETERM else I
wenzelm@10736
   794
      in  SELECT_GOAL (TRY (safe_tac cs) THEN
wenzelm@9938
   795
                       DEPTH_SOLVE (deti (depth_tac cs m 1))) i
lcp@747
   796
      end);
lcp@681
   797
paulson@2868
   798
fun deepen_tac cs = DEEPEN (2,10) (safe_depth_tac cs);
lcp@681
   799
wenzelm@4079
   800
berghofe@1724
   801
wenzelm@15036
   802
(** context dependent claset components **)
wenzelm@15036
   803
wenzelm@15036
   804
datatype context_cs = ContextCS of
wenzelm@15036
   805
 {swrappers: (string * (Proof.context -> wrapper)) list,
wenzelm@15036
   806
  uwrappers: (string * (Proof.context -> wrapper)) list};
wenzelm@15036
   807
wenzelm@15036
   808
fun context_cs ctxt cs (ContextCS {swrappers, uwrappers}) =
wenzelm@15036
   809
  let
wenzelm@15036
   810
    fun add_wrapper add (name, f) claset = add (claset, (name, f ctxt));
wenzelm@15036
   811
  in
haftmann@22674
   812
    cs
haftmann@22674
   813
    |> fold_rev (add_wrapper (op addSWrapper)) swrappers
wenzelm@15036
   814
    |> fold_rev (add_wrapper (op addWrapper)) uwrappers
wenzelm@15036
   815
  end;
wenzelm@15036
   816
wenzelm@15036
   817
fun make_context_cs (swrappers, uwrappers) =
wenzelm@15036
   818
  ContextCS {swrappers = swrappers, uwrappers = uwrappers};
wenzelm@15036
   819
wenzelm@15036
   820
val empty_context_cs = make_context_cs ([], []);
wenzelm@15036
   821
wenzelm@15036
   822
fun merge_context_cs (ctxt_cs1, ctxt_cs2) =
wenzelm@24358
   823
  if pointer_eq (ctxt_cs1, ctxt_cs2) then ctxt_cs1
wenzelm@24358
   824
  else
wenzelm@24358
   825
    let
wenzelm@24358
   826
      val ContextCS {swrappers = swrappers1, uwrappers = uwrappers1} = ctxt_cs1;
wenzelm@24358
   827
      val ContextCS {swrappers = swrappers2, uwrappers = uwrappers2} = ctxt_cs2;
wenzelm@24358
   828
      val swrappers' = AList.merge (op =) (K true) (swrappers1, swrappers2);
wenzelm@24358
   829
      val uwrappers' = AList.merge (op =) (K true) (uwrappers1, uwrappers2);
wenzelm@24358
   830
    in make_context_cs (swrappers', uwrappers') end;
wenzelm@15036
   831
wenzelm@15036
   832
wenzelm@15036
   833
wenzelm@17880
   834
(** claset data **)
wenzelm@4079
   835
wenzelm@24021
   836
(* global clasets *)
berghofe@1724
   837
wenzelm@16424
   838
structure GlobalClaset = TheoryDataFun
wenzelm@22846
   839
(
wenzelm@26497
   840
  type T = claset * context_cs;
wenzelm@26497
   841
  val empty = (empty_cs, empty_context_cs);
wenzelm@26497
   842
  val copy = I;
wenzelm@26497
   843
  val extend = I;
wenzelm@26497
   844
  fun merge _ ((cs1, ctxt_cs1), (cs2, ctxt_cs2)) =
wenzelm@26497
   845
    (merge_cs (cs1, cs2), merge_context_cs (ctxt_cs1, ctxt_cs2));
wenzelm@22846
   846
);
berghofe@1724
   847
wenzelm@32261
   848
val get_global_claset = #1 o GlobalClaset.get;
wenzelm@32261
   849
val map_global_claset = GlobalClaset.map o apfst;
wenzelm@17880
   850
wenzelm@15036
   851
val get_context_cs = #2 o GlobalClaset.get o ProofContext.theory_of;
wenzelm@15036
   852
fun map_context_cs f = GlobalClaset.map (apsnd
wenzelm@15036
   853
  (fn ContextCS {swrappers, uwrappers} => make_context_cs (f (swrappers, uwrappers))));
wenzelm@4079
   854
wenzelm@32148
   855
fun global_claset_of thy =
wenzelm@26497
   856
  let val (cs, ctxt_cs) = GlobalClaset.get thy
wenzelm@26497
   857
  in context_cs (ProofContext.init thy) cs (ctxt_cs) end;
paulson@3727
   858
wenzelm@4079
   859
wenzelm@15036
   860
(* context dependent components *)
wenzelm@15036
   861
wenzelm@26497
   862
fun add_context_safe_wrapper wrapper = map_context_cs (apfst ((AList.update (op =) wrapper)));
wenzelm@26497
   863
fun del_context_safe_wrapper name = map_context_cs (apfst ((AList.delete (op =) name)));
wenzelm@15036
   864
wenzelm@26497
   865
fun add_context_unsafe_wrapper wrapper = map_context_cs (apsnd ((AList.update (op =) wrapper)));
wenzelm@26497
   866
fun del_context_unsafe_wrapper name = map_context_cs (apsnd ((AList.delete (op =) name)));
wenzelm@15036
   867
wenzelm@15036
   868
wenzelm@24021
   869
(* local clasets *)
wenzelm@5841
   870
wenzelm@16424
   871
structure LocalClaset = ProofDataFun
wenzelm@22846
   872
(
wenzelm@5841
   873
  type T = claset;
wenzelm@32261
   874
  val init = get_global_claset;
wenzelm@22846
   875
);
wenzelm@5841
   876
wenzelm@32261
   877
val get_claset = LocalClaset.get;
wenzelm@32261
   878
val put_claset = LocalClaset.put;
wenzelm@32261
   879
wenzelm@32148
   880
fun claset_of ctxt =
wenzelm@26497
   881
  context_cs ctxt (LocalClaset.get ctxt) (get_context_cs ctxt);
wenzelm@22846
   882
wenzelm@5841
   883
wenzelm@24021
   884
(* generic clasets *)
wenzelm@24021
   885
wenzelm@32148
   886
val get_cs = Context.cases global_claset_of claset_of;
wenzelm@32261
   887
fun map_cs f = Context.mapping (map_global_claset f) (LocalClaset.map f);
wenzelm@24021
   888
wenzelm@24021
   889
wenzelm@5885
   890
(* attributes *)
wenzelm@5885
   891
wenzelm@18728
   892
fun attrib f = Thm.declaration_attribute (fn th =>
wenzelm@32261
   893
  Context.mapping (map_global_claset (f th)) (LocalClaset.map (f th)));
wenzelm@5885
   894
paulson@21689
   895
fun safe_dest w = attrib (addSE w o make_elim);
wenzelm@18691
   896
val safe_elim = attrib o addSE;
wenzelm@18691
   897
val safe_intro = attrib o addSI;
paulson@21689
   898
fun haz_dest w = attrib (addE w o make_elim);
wenzelm@18691
   899
val haz_elim = attrib o addE;
wenzelm@18691
   900
val haz_intro = attrib o addI;
wenzelm@33369
   901
val rule_del = attrib delrule o Context_Rules.rule_del;
wenzelm@5885
   902
wenzelm@5885
   903
wenzelm@10736
   904
end;
wenzelm@5841
   905
wenzelm@5841
   906
wenzelm@5841
   907
wenzelm@5885
   908
(** concrete syntax of attributes **)
wenzelm@5841
   909
wenzelm@5841
   910
val introN = "intro";
wenzelm@5841
   911
val elimN = "elim";
wenzelm@5841
   912
val destN = "dest";
wenzelm@5841
   913
wenzelm@30528
   914
val setup_attrs =
wenzelm@30528
   915
  Attrib.setup @{binding swapped} (Scan.succeed swapped)
wenzelm@30528
   916
    "classical swap of introduction rule" #>
wenzelm@33369
   917
  Attrib.setup @{binding dest} (Context_Rules.add safe_dest haz_dest Context_Rules.dest_query)
wenzelm@30528
   918
    "declaration of Classical destruction rule" #>
wenzelm@33369
   919
  Attrib.setup @{binding elim} (Context_Rules.add safe_elim haz_elim Context_Rules.elim_query)
wenzelm@30528
   920
    "declaration of Classical elimination rule" #>
wenzelm@33369
   921
  Attrib.setup @{binding intro} (Context_Rules.add safe_intro haz_intro Context_Rules.intro_query)
wenzelm@30528
   922
    "declaration of Classical introduction rule" #>
wenzelm@30528
   923
  Attrib.setup @{binding rule} (Scan.lift Args.del >> K rule_del)
wenzelm@30528
   924
    "remove declaration of intro/elim/dest rule";
wenzelm@5841
   925
wenzelm@5841
   926
wenzelm@5841
   927
wenzelm@7230
   928
(** proof methods **)
wenzelm@7230
   929
wenzelm@7230
   930
local
wenzelm@7230
   931
wenzelm@30609
   932
fun some_rule_tac ctxt facts = SUBGOAL (fn (goal, i) =>
wenzelm@5841
   933
  let
wenzelm@33369
   934
    val [rules1, rules2, rules4] = Context_Rules.find_rules false facts goal ctxt;
wenzelm@32148
   935
    val CS {xtra_netpair, ...} = claset_of ctxt;
wenzelm@33369
   936
    val rules3 = Context_Rules.find_rules_netpair true facts goal xtra_netpair;
wenzelm@12376
   937
    val rules = rules1 @ rules2 @ rules3 @ rules4;
wenzelm@18223
   938
    val ruleq = Drule.multi_resolves facts rules;
wenzelm@12376
   939
  in
wenzelm@12376
   940
    Method.trace ctxt rules;
wenzelm@32952
   941
    fn st => Seq.maps (fn rule => Tactic.rtac rule i st) ruleq
wenzelm@18834
   942
  end)
wenzelm@21687
   943
  THEN_ALL_NEW Goal.norm_hhf_tac;
wenzelm@5841
   944
wenzelm@30609
   945
in
wenzelm@7281
   946
wenzelm@30609
   947
fun rule_tac ctxt [] facts = some_rule_tac ctxt facts
wenzelm@30609
   948
  | rule_tac _ rules facts = Method.rule_tac rules facts;
wenzelm@30609
   949
wenzelm@30609
   950
fun default_tac ctxt rules facts =
wenzelm@30609
   951
  HEADGOAL (rule_tac ctxt rules facts) ORELSE
haftmann@26470
   952
  Class.default_intro_tac ctxt facts;
wenzelm@10309
   953
wenzelm@7230
   954
end;
wenzelm@5841
   955
wenzelm@5841
   956
wenzelm@7230
   957
(* contradiction method *)
wenzelm@6502
   958
wenzelm@7425
   959
val contradiction = Method.rule [Data.not_elim, Data.not_elim COMP Drule.swap_prems_rl];
wenzelm@6502
   960
wenzelm@6502
   961
wenzelm@6502
   962
(* automatic methods *)
wenzelm@5841
   963
wenzelm@5927
   964
val cla_modifiers =
wenzelm@18728
   965
 [Args.$$$ destN -- Args.bang_colon >> K ((I, safe_dest NONE): Method.modifier),
wenzelm@18728
   966
  Args.$$$ destN -- Args.colon >> K (I, haz_dest NONE),
wenzelm@18728
   967
  Args.$$$ elimN -- Args.bang_colon >> K (I, safe_elim NONE),
wenzelm@18728
   968
  Args.$$$ elimN -- Args.colon >> K (I, haz_elim NONE),
wenzelm@18728
   969
  Args.$$$ introN -- Args.bang_colon >> K (I, safe_intro NONE),
wenzelm@18728
   970
  Args.$$$ introN -- Args.colon >> K (I, haz_intro NONE),
wenzelm@18728
   971
  Args.del -- Args.colon >> K (I, rule_del)];
wenzelm@5927
   972
wenzelm@30510
   973
fun cla_meth tac prems ctxt = METHOD (fn facts =>
wenzelm@32148
   974
  ALLGOALS (Method.insert_tac (prems @ facts)) THEN tac (claset_of ctxt));
wenzelm@7132
   975
wenzelm@30510
   976
fun cla_meth' tac prems ctxt = METHOD (fn facts =>
wenzelm@32148
   977
  HEADGOAL (Method.insert_tac (prems @ facts) THEN' tac (claset_of ctxt)));
wenzelm@5841
   978
wenzelm@30541
   979
fun cla_method tac = Args.bang_facts --| Method.sections cla_modifiers >> (cla_meth tac);
wenzelm@30541
   980
fun cla_method' tac = Args.bang_facts --| Method.sections cla_modifiers >> (cla_meth' tac);
wenzelm@5841
   981
wenzelm@5841
   982
wenzelm@5841
   983
wenzelm@5841
   984
(** setup_methods **)
wenzelm@5841
   985
wenzelm@30541
   986
val setup_methods =
wenzelm@30609
   987
  Method.setup @{binding default}
wenzelm@30609
   988
   (Attrib.thms >> (fn rules => fn ctxt => METHOD (default_tac ctxt rules)))
wenzelm@30541
   989
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30609
   990
  Method.setup @{binding rule}
wenzelm@30609
   991
    (Attrib.thms >> (fn rules => fn ctxt => METHOD (HEADGOAL o rule_tac ctxt rules)))
wenzelm@30541
   992
    "apply some intro/elim rule (potentially classical)" #>
wenzelm@30541
   993
  Method.setup @{binding contradiction} (Scan.succeed (K contradiction))
wenzelm@30541
   994
    "proof by contradiction" #>
wenzelm@30541
   995
  Method.setup @{binding clarify} (cla_method' (CHANGED_PROP oo clarify_tac))
wenzelm@30541
   996
    "repeatedly apply safe steps" #>
wenzelm@30541
   997
  Method.setup @{binding fast} (cla_method' fast_tac) "classical prover (depth-first)" #>
wenzelm@30541
   998
  Method.setup @{binding slow} (cla_method' slow_tac) "classical prover (slow depth-first)" #>
wenzelm@30541
   999
  Method.setup @{binding best} (cla_method' best_tac) "classical prover (best-first)" #>
wenzelm@30541
  1000
  Method.setup @{binding deepen} (cla_method' (fn cs => deepen_tac cs 4))
wenzelm@30541
  1001
    "classical prover (iterative deepening)" #>
wenzelm@30541
  1002
  Method.setup @{binding safe} (cla_method (CHANGED_PROP o safe_tac))
wenzelm@30541
  1003
    "classical prover (apply safe rules)";
wenzelm@5841
  1004
wenzelm@5841
  1005
wenzelm@5841
  1006
wenzelm@5841
  1007
(** theory setup **)
wenzelm@5841
  1008
wenzelm@26497
  1009
val setup = setup_attrs #> setup_methods;
wenzelm@5841
  1010
wenzelm@5841
  1011
wenzelm@8667
  1012
wenzelm@8667
  1013
(** outer syntax **)
wenzelm@8667
  1014
wenzelm@24867
  1015
val _ =
wenzelm@8667
  1016
  OuterSyntax.improper_command "print_claset" "print context of Classical Reasoner"
wenzelm@17057
  1017
    OuterKeyword.diag
wenzelm@26497
  1018
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@32148
  1019
      Toplevel.keep (print_cs o claset_of o Toplevel.context_of)));
wenzelm@8667
  1020
wenzelm@5841
  1021
end;