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