src/Pure/tactic.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29276 94b1ffec9201
child 30558 2ef9892114fd
permissions -rw-r--r--
use long names for old-style fold combinators;
wenzelm@10805
     1
(*  Title:      Pure/tactic.ML
wenzelm@10805
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     3
wenzelm@29276
     4
Fundamental tactics.
clasohm@0
     5
*)
clasohm@0
     6
wenzelm@11774
     7
signature BASIC_TACTIC =
wenzelm@11774
     8
sig
wenzelm@23223
     9
  val trace_goalno_tac: (int -> tactic) -> int -> tactic
wenzelm@23223
    10
  val rule_by_tactic: tactic -> thm -> thm
wenzelm@23223
    11
  val assume_tac: int -> tactic
wenzelm@23223
    12
  val eq_assume_tac: int -> tactic
wenzelm@23223
    13
  val compose_tac: (bool * thm * int) -> int -> tactic
wenzelm@23223
    14
  val make_elim: thm -> thm
wenzelm@23223
    15
  val biresolve_tac: (bool * thm) list -> int -> tactic
wenzelm@23223
    16
  val resolve_tac: thm list -> int -> tactic
wenzelm@23223
    17
  val eresolve_tac: thm list -> int -> tactic
wenzelm@23223
    18
  val forward_tac: thm list -> int -> tactic
wenzelm@23223
    19
  val dresolve_tac: thm list -> int -> tactic
wenzelm@23223
    20
  val atac: int -> tactic
wenzelm@23223
    21
  val rtac: thm -> int -> tactic
wenzelm@23223
    22
  val dtac: thm -> int ->tactic
wenzelm@23223
    23
  val etac: thm -> int ->tactic
wenzelm@23223
    24
  val ftac: thm -> int ->tactic
wenzelm@23223
    25
  val datac: thm -> int -> int -> tactic
wenzelm@23223
    26
  val eatac: thm -> int -> int -> tactic
wenzelm@23223
    27
  val fatac: thm -> int -> int -> tactic
wenzelm@23223
    28
  val ares_tac: thm list -> int -> tactic
wenzelm@23223
    29
  val solve_tac: thm list -> int -> tactic
wenzelm@23223
    30
  val bimatch_tac: (bool * thm) list -> int -> tactic
wenzelm@23223
    31
  val match_tac: thm list -> int -> tactic
wenzelm@23223
    32
  val ematch_tac: thm list -> int -> tactic
wenzelm@23223
    33
  val dmatch_tac: thm list -> int -> tactic
wenzelm@23223
    34
  val flexflex_tac: tactic
wenzelm@23223
    35
  val distinct_subgoal_tac: int -> tactic
wenzelm@23223
    36
  val distinct_subgoals_tac: tactic
wenzelm@23223
    37
  val metacut_tac: thm -> int -> tactic
wenzelm@23223
    38
  val cut_rules_tac: thm list -> int -> tactic
wenzelm@23223
    39
  val cut_facts_tac: thm list -> int -> tactic
wenzelm@23223
    40
  val filter_thms: (term * term -> bool) -> int * term * thm list -> thm list
wenzelm@23223
    41
  val biresolution_from_nets_tac: ('a list -> (bool * thm) list) ->
wenzelm@23223
    42
    bool -> 'a Net.net * 'a Net.net -> int -> tactic
wenzelm@23223
    43
  val biresolve_from_nets_tac: (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net ->
wenzelm@23223
    44
    int -> tactic
wenzelm@23223
    45
  val bimatch_from_nets_tac: (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net ->
wenzelm@23223
    46
    int -> tactic
wenzelm@23223
    47
  val net_biresolve_tac: (bool * thm) list -> int -> tactic
wenzelm@23223
    48
  val net_bimatch_tac: (bool * thm) list -> int -> tactic
wenzelm@23223
    49
  val build_net: thm list -> (int * thm) Net.net
wenzelm@23223
    50
  val filt_resolve_tac: thm list -> int -> int -> tactic
wenzelm@23223
    51
  val resolve_from_net_tac: (int * thm) Net.net -> int -> tactic
wenzelm@23223
    52
  val match_from_net_tac: (int * thm) Net.net -> int -> tactic
wenzelm@23223
    53
  val net_resolve_tac: thm list -> int -> tactic
wenzelm@23223
    54
  val net_match_tac: thm list -> int -> tactic
wenzelm@23223
    55
  val subgoals_of_brl: bool * thm -> int
wenzelm@23223
    56
  val lessb: (bool * thm) * (bool * thm) -> bool
wenzelm@27243
    57
  val rename_tac: string list -> int -> tactic
wenzelm@23223
    58
  val rotate_tac: int -> int -> tactic
wenzelm@23223
    59
  val defer_tac: int -> tactic
wenzelm@23223
    60
  val filter_prems_tac: (term -> bool) -> int -> tactic
wenzelm@11774
    61
end;
clasohm@0
    62
wenzelm@11774
    63
signature TACTIC =
wenzelm@11774
    64
sig
wenzelm@11774
    65
  include BASIC_TACTIC
wenzelm@11929
    66
  val innermost_params: int -> thm -> (string * typ) list
wenzelm@27243
    67
  val term_lift_inst_rule:
wenzelm@27243
    68
    thm * int * ((indexname * sort) * typ) list * ((indexname * typ) * term) list * thm -> thm
wenzelm@11774
    69
  val untaglist: (int * 'a) list -> 'a list
wenzelm@11774
    70
  val orderlist: (int * 'a) list -> 'a list
wenzelm@23223
    71
  val insert_tagged_brl: 'a * (bool * thm) ->
wenzelm@23223
    72
    ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net ->
wenzelm@23223
    73
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@23223
    74
  val build_netpair: (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net ->
wenzelm@23223
    75
    (bool * thm) list -> (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net
wenzelm@23223
    76
  val delete_tagged_brl: bool * thm ->
wenzelm@23223
    77
    ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net ->
wenzelm@23223
    78
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@23223
    79
  val eq_kbrl: ('a * (bool * thm)) * ('a * (bool * thm)) -> bool
wenzelm@11774
    80
end;
clasohm@0
    81
wenzelm@11774
    82
structure Tactic: TACTIC =
clasohm@0
    83
struct
clasohm@0
    84
paulson@1501
    85
(*Discover which goal is chosen:  SOMEGOAL(trace_goalno_tac tac) *)
wenzelm@10817
    86
fun trace_goalno_tac tac i st =
wenzelm@4270
    87
    case Seq.pull(tac i st) of
skalberg@15531
    88
        NONE    => Seq.empty
wenzelm@12262
    89
      | seqcell => (tracing ("Subgoal " ^ string_of_int i ^ " selected");
wenzelm@10805
    90
                         Seq.make(fn()=> seqcell));
clasohm@0
    91
clasohm@0
    92
(*Makes a rule by applying a tactic to an existing rule*)
paulson@1501
    93
fun rule_by_tactic tac rl =
wenzelm@19925
    94
  let
wenzelm@19925
    95
    val ctxt = Variable.thm_context rl;
wenzelm@22568
    96
    val ((_, [st]), ctxt') = Variable.import_thms true [rl] ctxt;
wenzelm@19925
    97
  in
wenzelm@19925
    98
    (case Seq.pull (tac st) of
wenzelm@19925
    99
      NONE => raise THM ("rule_by_tactic", 0, [rl])
wenzelm@19925
   100
    | SOME (st', _) => zero_var_indexes (singleton (Variable.export ctxt' ctxt) st'))
paulson@2688
   101
  end;
wenzelm@10817
   102
wenzelm@19925
   103
clasohm@0
   104
(*** Basic tactics ***)
clasohm@0
   105
clasohm@0
   106
(*** The following fail if the goal number is out of range:
clasohm@0
   107
     thus (REPEAT (resolve_tac rules i)) stops once subgoal i disappears. *)
clasohm@0
   108
clasohm@0
   109
(*Solve subgoal i by assumption*)
clasohm@0
   110
fun assume_tac i = PRIMSEQ (assumption i);
clasohm@0
   111
clasohm@0
   112
(*Solve subgoal i by assumption, using no unification*)
clasohm@0
   113
fun eq_assume_tac i = PRIMITIVE (eq_assumption i);
clasohm@0
   114
wenzelm@23223
   115
clasohm@0
   116
(** Resolution/matching tactics **)
clasohm@0
   117
clasohm@0
   118
(*The composition rule/state: no lifting or var renaming.
clasohm@0
   119
  The arg = (bires_flg, orule, m) ;  see bicompose for explanation.*)
clasohm@0
   120
fun compose_tac arg i = PRIMSEQ (bicompose false arg i);
clasohm@0
   121
clasohm@0
   122
(*Converts a "destruct" rule like P&Q==>P to an "elimination" rule
clasohm@0
   123
  like [| P&Q; P==>R |] ==> R *)
clasohm@0
   124
fun make_elim rl = zero_var_indexes (rl RS revcut_rl);
clasohm@0
   125
clasohm@0
   126
(*Attack subgoal i by resolution, using flags to indicate elimination rules*)
clasohm@0
   127
fun biresolve_tac brules i = PRIMSEQ (biresolution false brules i);
clasohm@0
   128
clasohm@0
   129
(*Resolution: the simple case, works for introduction rules*)
clasohm@0
   130
fun resolve_tac rules = biresolve_tac (map (pair false) rules);
clasohm@0
   131
clasohm@0
   132
(*Resolution with elimination rules only*)
clasohm@0
   133
fun eresolve_tac rules = biresolve_tac (map (pair true) rules);
clasohm@0
   134
clasohm@0
   135
(*Forward reasoning using destruction rules.*)
clasohm@0
   136
fun forward_tac rls = resolve_tac (map make_elim rls) THEN' assume_tac;
clasohm@0
   137
clasohm@0
   138
(*Like forward_tac, but deletes the assumption after use.*)
clasohm@0
   139
fun dresolve_tac rls = eresolve_tac (map make_elim rls);
clasohm@0
   140
clasohm@0
   141
(*Shorthand versions: for resolution with a single theorem*)
oheimb@7491
   142
val atac    =   assume_tac;
oheimb@7491
   143
fun rtac rl =  resolve_tac [rl];
oheimb@7491
   144
fun dtac rl = dresolve_tac [rl];
clasohm@1460
   145
fun etac rl = eresolve_tac [rl];
oheimb@7491
   146
fun ftac rl =  forward_tac [rl];
oheimb@7491
   147
fun datac thm j = EVERY' (dtac thm::replicate j atac);
oheimb@7491
   148
fun eatac thm j = EVERY' (etac thm::replicate j atac);
oheimb@7491
   149
fun fatac thm j = EVERY' (ftac thm::replicate j atac);
clasohm@0
   150
clasohm@0
   151
(*Use an assumption or some rules ... A popular combination!*)
clasohm@0
   152
fun ares_tac rules = assume_tac  ORELSE'  resolve_tac rules;
clasohm@0
   153
wenzelm@5263
   154
fun solve_tac rules = resolve_tac rules THEN_ALL_NEW assume_tac;
wenzelm@5263
   155
clasohm@0
   156
(*Matching tactics -- as above, but forbid updating of state*)
clasohm@0
   157
fun bimatch_tac brules i = PRIMSEQ (biresolution true brules i);
clasohm@0
   158
fun match_tac rules  = bimatch_tac (map (pair false) rules);
clasohm@0
   159
fun ematch_tac rules = bimatch_tac (map (pair true) rules);
clasohm@0
   160
fun dmatch_tac rls   = ematch_tac (map make_elim rls);
clasohm@0
   161
clasohm@0
   162
(*Smash all flex-flex disagreement pairs in the proof state.*)
clasohm@0
   163
val flexflex_tac = PRIMSEQ flexflex_rule;
clasohm@0
   164
wenzelm@19056
   165
(*Remove duplicate subgoals.*)
paulson@22560
   166
val perm_tac = PRIMITIVE oo Thm.permute_prems;
paulson@22560
   167
paulson@22560
   168
fun distinct_tac (i, k) =
paulson@22560
   169
  perm_tac 0 (i - 1) THEN
paulson@22560
   170
  perm_tac 1 (k - 1) THEN
paulson@22560
   171
  DETERM (PRIMSEQ (fn st =>
paulson@22560
   172
    Thm.compose_no_flatten false (st, 0) 1
paulson@22560
   173
      (Drule.incr_indexes st Drule.distinct_prems_rl))) THEN
paulson@22560
   174
  perm_tac 1 (1 - k) THEN
paulson@22560
   175
  perm_tac 0 (1 - i);
paulson@22560
   176
paulson@22560
   177
fun distinct_subgoal_tac i st =
paulson@22560
   178
  (case Library.drop (i - 1, Thm.prems_of st) of
paulson@22560
   179
    [] => no_tac st
paulson@22560
   180
  | A :: Bs =>
paulson@22560
   181
      st |> EVERY (fold (fn (B, k) =>
wenzelm@23223
   182
        if A aconv B then cons (distinct_tac (i, k)) else I) (Bs ~~ (1 upto length Bs)) []));
paulson@22560
   183
wenzelm@10817
   184
fun distinct_subgoals_tac state =
wenzelm@19056
   185
  let
wenzelm@19056
   186
    val goals = Thm.prems_of state;
wenzelm@19056
   187
    val dups = distinct (eq_fst (op aconv)) (goals ~~ (1 upto length goals));
wenzelm@19056
   188
  in EVERY (rev (map (distinct_subgoal_tac o snd) dups)) state end;
paulson@3409
   189
wenzelm@11929
   190
(*Determine print names of goal parameters (reversed)*)
wenzelm@11929
   191
fun innermost_params i st =
wenzelm@11929
   192
  let val (_, _, Bi, _) = dest_state (st, i)
wenzelm@29276
   193
  in Term.rename_wrt_term Bi (Logic.strip_params Bi) end;
wenzelm@11929
   194
paulson@15453
   195
(*params of subgoal i as they are printed*)
paulson@19532
   196
fun params_of_state i st = rev (innermost_params i st);
wenzelm@16425
   197
nipkow@3984
   198
(*
nipkow@3984
   199
Like lift_inst_rule but takes terms, not strings, where the terms may contain
nipkow@3984
   200
Bounds referring to parameters of the subgoal.
nipkow@3984
   201
nipkow@3984
   202
insts: [...,(vj,tj),...]
nipkow@3984
   203
nipkow@3984
   204
The tj may contain references to parameters of subgoal i of the state st
nipkow@3984
   205
in the form of Bound k, i.e. the tj may be subterms of the subgoal.
nipkow@3984
   206
To saturate the lose bound vars, the tj are enclosed in abstractions
nipkow@3984
   207
corresponding to the parameters of subgoal i, thus turning them into
nipkow@3984
   208
functions. At the same time, the types of the vj are lifted.
nipkow@3984
   209
nipkow@3984
   210
NB: the types in insts must be correctly instantiated already,
nipkow@3984
   211
    i.e. Tinsts is not applied to insts.
nipkow@3984
   212
*)
nipkow@1975
   213
fun term_lift_inst_rule (st, i, Tinsts, insts, rule) =
wenzelm@26626
   214
let
wenzelm@26626
   215
    val thy = Thm.theory_of_thm st
wenzelm@26626
   216
    val cert = Thm.cterm_of thy
wenzelm@26626
   217
    val certT = Thm.ctyp_of thy
wenzelm@26626
   218
    val maxidx = Thm.maxidx_of st
paulson@19532
   219
    val paramTs = map #2 (params_of_state i st)
wenzelm@26626
   220
    val inc = maxidx+1
wenzelm@16876
   221
    fun liftvar ((a,j), T) = Var((a, j+inc), paramTs---> Logic.incr_tvar inc T)
nipkow@1975
   222
    (*lift only Var, not term, which must be lifted already*)
wenzelm@26626
   223
    fun liftpair (v,t) = (cert (liftvar v), cert t)
berghofe@15797
   224
    fun liftTpair (((a, i), S), T) =
wenzelm@26626
   225
      (certT (TVar ((a, i + inc), S)),
wenzelm@26626
   226
       certT (Logic.incr_tvar inc T))
paulson@8129
   227
in Drule.instantiate (map liftTpair Tinsts, map liftpair insts)
wenzelm@18145
   228
                     (Thm.lift_rule (Thm.cprem_of st i) rule)
nipkow@1966
   229
end;
clasohm@0
   230
clasohm@0
   231
paulson@1951
   232
lcp@270
   233
(*** Applications of cut_rl ***)
clasohm@0
   234
clasohm@0
   235
(*The conclusion of the rule gets assumed in subgoal i,
clasohm@0
   236
  while subgoal i+1,... are the premises of the rule.*)
wenzelm@27243
   237
fun metacut_tac rule i = resolve_tac [cut_rl] i  THEN  biresolve_tac [(false, rule)] (i+1);
clasohm@0
   238
paulson@13650
   239
(*"Cut" a list of rules into the goal.  Their premises will become new
paulson@13650
   240
  subgoals.*)
paulson@13650
   241
fun cut_rules_tac ths i = EVERY (map (fn th => metacut_tac th i) ths);
paulson@13650
   242
paulson@13650
   243
(*As above, but inserts only facts (unconditional theorems);
paulson@13650
   244
  generates no additional subgoals. *)
wenzelm@20232
   245
fun cut_facts_tac ths = cut_rules_tac (filter Thm.no_prems ths);
clasohm@0
   246
clasohm@0
   247
clasohm@0
   248
(**** Indexing and filtering of theorems ****)
clasohm@0
   249
clasohm@0
   250
(*Returns the list of potentially resolvable theorems for the goal "prem",
wenzelm@10805
   251
        using the predicate  could(subgoal,concl).
clasohm@0
   252
  Resulting list is no longer than "limit"*)
clasohm@0
   253
fun filter_thms could (limit, prem, ths) =
clasohm@0
   254
  let val pb = Logic.strip_assums_concl prem;   (*delete assumptions*)
clasohm@0
   255
      fun filtr (limit, []) = []
wenzelm@10805
   256
        | filtr (limit, th::ths) =
wenzelm@10805
   257
            if limit=0 then  []
wenzelm@10805
   258
            else if could(pb, concl_of th)  then th :: filtr(limit-1, ths)
wenzelm@10805
   259
            else filtr(limit,ths)
clasohm@0
   260
  in  filtr(limit,ths)  end;
clasohm@0
   261
clasohm@0
   262
clasohm@0
   263
(*** biresolution and resolution using nets ***)
clasohm@0
   264
clasohm@0
   265
(** To preserve the order of the rules, tag them with increasing integers **)
clasohm@0
   266
clasohm@0
   267
(*insert tags*)
clasohm@0
   268
fun taglist k [] = []
clasohm@0
   269
  | taglist k (x::xs) = (k,x) :: taglist (k+1) xs;
clasohm@0
   270
clasohm@0
   271
(*remove tags and suppress duplicates -- list is assumed sorted!*)
clasohm@0
   272
fun untaglist [] = []
clasohm@0
   273
  | untaglist [(k:int,x)] = [x]
clasohm@0
   274
  | untaglist ((k,x) :: (rest as (k',x')::_)) =
clasohm@0
   275
      if k=k' then untaglist rest
clasohm@0
   276
      else    x :: untaglist rest;
clasohm@0
   277
clasohm@0
   278
(*return list elements in original order*)
wenzelm@10817
   279
fun orderlist kbrls = untaglist (sort (int_ord o pairself fst) kbrls);
clasohm@0
   280
clasohm@0
   281
(*insert one tagged brl into the pair of nets*)
wenzelm@23178
   282
fun insert_tagged_brl (kbrl as (k, (eres, th))) (inet, enet) =
wenzelm@12320
   283
  if eres then
wenzelm@12320
   284
    (case try Thm.major_prem_of th of
wenzelm@16809
   285
      SOME prem => (inet, Net.insert_term (K false) (prem, kbrl) enet)
skalberg@15531
   286
    | NONE => error "insert_tagged_brl: elimination rule with no premises")
wenzelm@16809
   287
  else (Net.insert_term (K false) (concl_of th, kbrl) inet, enet);
clasohm@0
   288
clasohm@0
   289
(*build a pair of nets for biresolution*)
wenzelm@10817
   290
fun build_netpair netpair brls =
wenzelm@23178
   291
    fold_rev insert_tagged_brl (taglist 1 brls) netpair;
clasohm@0
   292
wenzelm@12320
   293
(*delete one kbrl from the pair of nets*)
wenzelm@22360
   294
fun eq_kbrl ((_, (_, th)), (_, (_, th'))) = Thm.eq_thm_prop (th, th')
wenzelm@16809
   295
wenzelm@23178
   296
fun delete_tagged_brl (brl as (eres, th)) (inet, enet) =
paulson@13925
   297
  (if eres then
wenzelm@12320
   298
    (case try Thm.major_prem_of th of
wenzelm@16809
   299
      SOME prem => (inet, Net.delete_term eq_kbrl (prem, ((), brl)) enet)
skalberg@15531
   300
    | NONE => (inet, enet))  (*no major premise: ignore*)
wenzelm@16809
   301
  else (Net.delete_term eq_kbrl (Thm.concl_of th, ((), brl)) inet, enet))
paulson@13925
   302
  handle Net.DELETE => (inet,enet);
paulson@1801
   303
paulson@1801
   304
wenzelm@10817
   305
(*biresolution using a pair of nets rather than rules.
paulson@3706
   306
    function "order" must sort and possibly filter the list of brls.
paulson@3706
   307
    boolean "match" indicates matching or unification.*)
paulson@3706
   308
fun biresolution_from_nets_tac order match (inet,enet) =
clasohm@0
   309
  SUBGOAL
clasohm@0
   310
    (fn (prem,i) =>
clasohm@0
   311
      let val hyps = Logic.strip_assums_hyp prem
wenzelm@10817
   312
          and concl = Logic.strip_assums_concl prem
wenzelm@19482
   313
          val kbrls = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps
paulson@3706
   314
      in PRIMSEQ (biresolution match (order kbrls) i) end);
clasohm@0
   315
paulson@3706
   316
(*versions taking pre-built nets.  No filtering of brls*)
paulson@3706
   317
val biresolve_from_nets_tac = biresolution_from_nets_tac orderlist false;
paulson@3706
   318
val bimatch_from_nets_tac   = biresolution_from_nets_tac orderlist true;
clasohm@0
   319
clasohm@0
   320
(*fast versions using nets internally*)
lcp@670
   321
val net_biresolve_tac =
lcp@670
   322
    biresolve_from_nets_tac o build_netpair(Net.empty,Net.empty);
lcp@670
   323
lcp@670
   324
val net_bimatch_tac =
lcp@670
   325
    bimatch_from_nets_tac o build_netpair(Net.empty,Net.empty);
clasohm@0
   326
clasohm@0
   327
(*** Simpler version for resolve_tac -- only one net, and no hyps ***)
clasohm@0
   328
clasohm@0
   329
(*insert one tagged rl into the net*)
wenzelm@23178
   330
fun insert_krl (krl as (k,th)) =
wenzelm@23178
   331
  Net.insert_term (K false) (concl_of th, krl);
clasohm@0
   332
clasohm@0
   333
(*build a net of rules for resolution*)
wenzelm@10817
   334
fun build_net rls =
wenzelm@23178
   335
  fold_rev insert_krl (taglist 1 rls) Net.empty;
clasohm@0
   336
clasohm@0
   337
(*resolution using a net rather than rules; pred supports filt_resolve_tac*)
clasohm@0
   338
fun filt_resolution_from_net_tac match pred net =
clasohm@0
   339
  SUBGOAL
clasohm@0
   340
    (fn (prem,i) =>
clasohm@0
   341
      let val krls = Net.unify_term net (Logic.strip_assums_concl prem)
wenzelm@10817
   342
      in
wenzelm@10817
   343
         if pred krls
clasohm@0
   344
         then PRIMSEQ
wenzelm@10805
   345
                (biresolution match (map (pair false) (orderlist krls)) i)
clasohm@0
   346
         else no_tac
clasohm@0
   347
      end);
clasohm@0
   348
clasohm@0
   349
(*Resolve the subgoal using the rules (making a net) unless too flexible,
clasohm@0
   350
   which means more than maxr rules are unifiable.      *)
wenzelm@10817
   351
fun filt_resolve_tac rules maxr =
clasohm@0
   352
    let fun pred krls = length krls <= maxr
clasohm@0
   353
    in  filt_resolution_from_net_tac false pred (build_net rules)  end;
clasohm@0
   354
clasohm@0
   355
(*versions taking pre-built nets*)
clasohm@0
   356
val resolve_from_net_tac = filt_resolution_from_net_tac false (K true);
clasohm@0
   357
val match_from_net_tac = filt_resolution_from_net_tac true (K true);
clasohm@0
   358
clasohm@0
   359
(*fast versions using nets internally*)
clasohm@0
   360
val net_resolve_tac = resolve_from_net_tac o build_net;
clasohm@0
   361
val net_match_tac = match_from_net_tac o build_net;
clasohm@0
   362
clasohm@0
   363
clasohm@0
   364
(*** For Natural Deduction using (bires_flg, rule) pairs ***)
clasohm@0
   365
clasohm@0
   366
(*The number of new subgoals produced by the brule*)
lcp@1077
   367
fun subgoals_of_brl (true,rule)  = nprems_of rule - 1
lcp@1077
   368
  | subgoals_of_brl (false,rule) = nprems_of rule;
clasohm@0
   369
clasohm@0
   370
(*Less-than test: for sorting to minimize number of new subgoals*)
clasohm@0
   371
fun lessb (brl1,brl2) = subgoals_of_brl brl1 < subgoals_of_brl brl2;
clasohm@0
   372
clasohm@0
   373
wenzelm@27243
   374
(*Renaming of parameters in a subgoal*)
wenzelm@27243
   375
fun rename_tac xs i =
wenzelm@14673
   376
  case Library.find_first (not o Syntax.is_identifier) xs of
skalberg@15531
   377
      SOME x => error ("Not an identifier: " ^ x)
berghofe@25939
   378
    | NONE => PRIMITIVE (rename_params_rule (xs, i));
wenzelm@9535
   379
paulson@1501
   380
(*rotate_tac n i: rotate the assumptions of subgoal i by n positions, from
paulson@1501
   381
  right to left if n is positive, and from left to right if n is negative.*)
paulson@2672
   382
fun rotate_tac 0 i = all_tac
paulson@2672
   383
  | rotate_tac k i = PRIMITIVE (rotate_rule k i);
nipkow@1209
   384
paulson@7248
   385
(*Rotates the given subgoal to be the last.*)
paulson@7248
   386
fun defer_tac i = PRIMITIVE (permute_prems (i-1) 1);
paulson@7248
   387
nipkow@5974
   388
(* remove premises that do not satisfy p; fails if all prems satisfy p *)
nipkow@5974
   389
fun filter_prems_tac p =
skalberg@15531
   390
  let fun Then NONE tac = SOME tac
skalberg@15531
   391
        | Then (SOME tac) tac' = SOME(tac THEN' tac');
wenzelm@19473
   392
      fun thins H (tac,n) =
nipkow@5974
   393
        if p H then (tac,n+1)
nipkow@5974
   394
        else (Then tac (rotate_tac n THEN' etac thin_rl),0);
nipkow@5974
   395
  in SUBGOAL(fn (subg,n) =>
nipkow@5974
   396
       let val Hs = Logic.strip_assums_hyp subg
wenzelm@19473
   397
       in case fst(fold thins Hs (NONE,0)) of
skalberg@15531
   398
            NONE => no_tac | SOME tac => tac n
nipkow@5974
   399
       end)
nipkow@5974
   400
  end;
nipkow@5974
   401
clasohm@0
   402
end;
paulson@1501
   403
wenzelm@11774
   404
structure BasicTactic: BASIC_TACTIC = Tactic;
wenzelm@11774
   405
open BasicTactic;