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