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