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