src/Pure/tactic.ML
author wenzelm
Thu Dec 07 23:16:55 2006 +0100 (2006-12-07)
changeset 21708 45e7491bea47
parent 21687 f689f729afab
child 22360 26ead7ed4f4b
permissions -rw-r--r--
reorganized structure Tactic vs. MetaSimplifier;
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@10805
     6
Tactics.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@11774
     9
signature BASIC_TACTIC =
wenzelm@11774
    10
sig
wenzelm@10805
    11
  val ares_tac          : thm list -> int -> tactic
wenzelm@10805
    12
  val assume_tac        : int -> tactic
wenzelm@10805
    13
  val atac      : int ->tactic
wenzelm@10817
    14
  val bimatch_from_nets_tac:
paulson@1501
    15
      (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net -> int -> tactic
wenzelm@10805
    16
  val bimatch_tac       : (bool*thm)list -> int -> tactic
wenzelm@10817
    17
  val biresolution_from_nets_tac:
wenzelm@10805
    18
        ('a list -> (bool * thm) list) ->
wenzelm@10805
    19
        bool -> 'a Net.net * 'a Net.net -> int -> tactic
wenzelm@10817
    20
  val biresolve_from_nets_tac:
paulson@1501
    21
      (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net -> int -> tactic
wenzelm@10805
    22
  val biresolve_tac     : (bool*thm)list -> int -> tactic
wenzelm@10805
    23
  val build_net : thm list -> (int*thm) Net.net
paulson@1501
    24
  val build_netpair:    (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net ->
paulson@1501
    25
      (bool*thm)list -> (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net
wenzelm@10817
    26
  val compose_inst_tac  : (string*string)list -> (bool*thm*int) ->
paulson@3409
    27
                          int -> tactic
wenzelm@10817
    28
  val compose_tac       : (bool * thm * int) -> int -> tactic
wenzelm@10805
    29
  val cut_facts_tac     : thm list -> int -> tactic
paulson@13650
    30
  val cut_rules_tac     : thm list -> int -> tactic
wenzelm@10817
    31
  val cut_inst_tac      : (string*string)list -> thm -> int -> tactic
oheimb@7491
    32
  val datac             : thm -> int -> int -> tactic
wenzelm@10805
    33
  val defer_tac         : int -> tactic
wenzelm@10805
    34
  val distinct_subgoals_tac     : tactic
wenzelm@10805
    35
  val dmatch_tac        : thm list -> int -> tactic
wenzelm@10805
    36
  val dresolve_tac      : thm list -> int -> tactic
wenzelm@10817
    37
  val dres_inst_tac     : (string*string)list -> thm -> int -> tactic
wenzelm@10805
    38
  val dtac              : thm -> int ->tactic
oheimb@7491
    39
  val eatac             : thm -> int -> int -> tactic
wenzelm@10805
    40
  val etac              : thm -> int ->tactic
wenzelm@10817
    41
  val eq_assume_tac     : int -> tactic
wenzelm@10805
    42
  val ematch_tac        : thm list -> int -> tactic
wenzelm@10805
    43
  val eresolve_tac      : thm list -> int -> tactic
wenzelm@10805
    44
  val eres_inst_tac     : (string*string)list -> thm -> int -> tactic
oheimb@7491
    45
  val fatac             : thm -> int -> int -> tactic
wenzelm@10817
    46
  val filter_prems_tac  : (term -> bool) -> int -> tactic
wenzelm@10805
    47
  val filter_thms       : (term*term->bool) -> int*term*thm list -> thm list
wenzelm@10805
    48
  val filt_resolve_tac  : thm list -> int -> int -> tactic
wenzelm@10805
    49
  val flexflex_tac      : tactic
wenzelm@10817
    50
  val forward_tac       : thm list -> int -> tactic
wenzelm@10805
    51
  val forw_inst_tac     : (string*string)list -> thm -> int -> tactic
wenzelm@10805
    52
  val ftac              : thm -> int ->tactic
wenzelm@12320
    53
  val insert_tagged_brl : ('a * (bool * thm)) *
wenzelm@12320
    54
    (('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net) ->
wenzelm@12320
    55
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@12320
    56
  val delete_tagged_brl : (bool * thm) *
wenzelm@12320
    57
    (('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net) ->
wenzelm@12320
    58
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@10805
    59
  val lessb             : (bool * thm) * (bool * thm) -> bool
wenzelm@10805
    60
  val lift_inst_rule    : thm * int * (string*string)list * thm -> thm
wenzelm@10805
    61
  val make_elim         : thm -> thm
wenzelm@10805
    62
  val match_from_net_tac        : (int*thm) Net.net -> int -> tactic
wenzelm@10805
    63
  val match_tac : thm list -> int -> tactic
wenzelm@10805
    64
  val metacut_tac       : thm -> int -> tactic
wenzelm@10805
    65
  val net_bimatch_tac   : (bool*thm) list -> int -> tactic
wenzelm@10805
    66
  val net_biresolve_tac : (bool*thm) list -> int -> tactic
wenzelm@10805
    67
  val net_match_tac     : thm list -> int -> tactic
wenzelm@10805
    68
  val net_resolve_tac   : thm list -> int -> tactic
wenzelm@10805
    69
  val rename_params_tac : string list -> int -> tactic
wenzelm@10805
    70
  val rename_tac        : string -> int -> tactic
wenzelm@10805
    71
  val rename_last_tac   : string -> string list -> int -> tactic
wenzelm@10805
    72
  val resolve_from_net_tac      : (int*thm) Net.net -> int -> tactic
wenzelm@10805
    73
  val resolve_tac       : thm list -> int -> tactic
wenzelm@10817
    74
  val res_inst_tac      : (string*string)list -> thm -> int -> tactic
wenzelm@10805
    75
  val rotate_tac        : int -> int -> tactic
wenzelm@10805
    76
  val rtac              : thm -> int -> tactic
wenzelm@10805
    77
  val rule_by_tactic    : tactic -> thm -> thm
wenzelm@10805
    78
  val solve_tac         : thm list -> int -> tactic
wenzelm@10805
    79
  val subgoal_tac       : string -> int -> tactic
wenzelm@10805
    80
  val subgoals_tac      : string list -> int -> tactic
wenzelm@10805
    81
  val subgoals_of_brl   : bool * thm -> int
wenzelm@10805
    82
  val term_lift_inst_rule       :
berghofe@15797
    83
      thm * int * ((indexname * sort) * typ) list * ((indexname * typ) * term) list * thm
nipkow@1975
    84
      -> thm
oheimb@10347
    85
  val instantiate_tac   : (string * string) list -> tactic
wenzelm@10805
    86
  val thin_tac          : string -> int -> tactic
wenzelm@10805
    87
  val trace_goalno_tac  : (int -> tactic) -> int -> tactic
wenzelm@11774
    88
end;
clasohm@0
    89
wenzelm@11774
    90
signature TACTIC =
wenzelm@11774
    91
sig
wenzelm@11774
    92
  include BASIC_TACTIC
wenzelm@11929
    93
  val innermost_params: int -> thm -> (string * typ) list
wenzelm@11774
    94
  val untaglist: (int * 'a) list -> 'a list
wenzelm@16809
    95
  val eq_kbrl: ('a * (bool * thm)) * ('a * (bool * thm)) -> bool
wenzelm@11774
    96
  val orderlist: (int * 'a) list -> 'a list
berghofe@15442
    97
  val compose_inst_tac' : (indexname * string) list -> (bool * thm * int) ->
berghofe@15442
    98
                          int -> tactic
berghofe@15442
    99
  val lift_inst_rule'   : thm * int * (indexname * string) list * thm -> thm
berghofe@15464
   100
  val eres_inst_tac'    : (indexname * string) list -> thm -> int -> tactic
berghofe@15442
   101
  val res_inst_tac'     : (indexname * string) list -> thm -> int -> tactic
berghofe@15797
   102
  val instantiate_tac'  : (indexname * string) list -> tactic
wenzelm@20115
   103
  val make_elim_preserve: thm -> thm
wenzelm@11774
   104
end;
clasohm@0
   105
wenzelm@11774
   106
structure Tactic: TACTIC =
clasohm@0
   107
struct
clasohm@0
   108
paulson@1501
   109
(*Discover which goal is chosen:  SOMEGOAL(trace_goalno_tac tac) *)
wenzelm@10817
   110
fun trace_goalno_tac tac i st =
wenzelm@4270
   111
    case Seq.pull(tac i st) of
skalberg@15531
   112
        NONE    => Seq.empty
wenzelm@12262
   113
      | seqcell => (tracing ("Subgoal " ^ string_of_int i ^ " selected");
wenzelm@10805
   114
                         Seq.make(fn()=> seqcell));
clasohm@0
   115
clasohm@0
   116
(*Makes a rule by applying a tactic to an existing rule*)
paulson@1501
   117
fun rule_by_tactic tac rl =
wenzelm@19925
   118
  let
wenzelm@19925
   119
    val ctxt = Variable.thm_context rl;
wenzelm@20218
   120
    val ((_, [st]), ctxt') = Variable.import true [rl] ctxt;
wenzelm@19925
   121
  in
wenzelm@19925
   122
    (case Seq.pull (tac st) of
wenzelm@19925
   123
      NONE => raise THM ("rule_by_tactic", 0, [rl])
wenzelm@19925
   124
    | SOME (st', _) => zero_var_indexes (singleton (Variable.export ctxt' ctxt) st'))
paulson@2688
   125
  end;
wenzelm@10817
   126
wenzelm@19925
   127
clasohm@0
   128
(*** Basic tactics ***)
clasohm@0
   129
clasohm@0
   130
(*** The following fail if the goal number is out of range:
clasohm@0
   131
     thus (REPEAT (resolve_tac rules i)) stops once subgoal i disappears. *)
clasohm@0
   132
clasohm@0
   133
(*Solve subgoal i by assumption*)
clasohm@0
   134
fun assume_tac i = PRIMSEQ (assumption i);
clasohm@0
   135
clasohm@0
   136
(*Solve subgoal i by assumption, using no unification*)
clasohm@0
   137
fun eq_assume_tac i = PRIMITIVE (eq_assumption i);
clasohm@0
   138
clasohm@0
   139
(** Resolution/matching tactics **)
clasohm@0
   140
clasohm@0
   141
(*The composition rule/state: no lifting or var renaming.
clasohm@0
   142
  The arg = (bires_flg, orule, m) ;  see bicompose for explanation.*)
clasohm@0
   143
fun compose_tac arg i = PRIMSEQ (bicompose false arg i);
clasohm@0
   144
clasohm@0
   145
(*Converts a "destruct" rule like P&Q==>P to an "elimination" rule
clasohm@0
   146
  like [| P&Q; P==>R |] ==> R *)
clasohm@0
   147
fun make_elim rl = zero_var_indexes (rl RS revcut_rl);
clasohm@0
   148
clasohm@0
   149
(*Attack subgoal i by resolution, using flags to indicate elimination rules*)
clasohm@0
   150
fun biresolve_tac brules i = PRIMSEQ (biresolution false brules i);
clasohm@0
   151
clasohm@0
   152
(*Resolution: the simple case, works for introduction rules*)
clasohm@0
   153
fun resolve_tac rules = biresolve_tac (map (pair false) rules);
clasohm@0
   154
clasohm@0
   155
(*Resolution with elimination rules only*)
clasohm@0
   156
fun eresolve_tac rules = biresolve_tac (map (pair true) rules);
clasohm@0
   157
clasohm@0
   158
(*Forward reasoning using destruction rules.*)
clasohm@0
   159
fun forward_tac rls = resolve_tac (map make_elim rls) THEN' assume_tac;
clasohm@0
   160
clasohm@0
   161
(*Like forward_tac, but deletes the assumption after use.*)
clasohm@0
   162
fun dresolve_tac rls = eresolve_tac (map make_elim rls);
clasohm@0
   163
clasohm@0
   164
(*Shorthand versions: for resolution with a single theorem*)
oheimb@7491
   165
val atac    =   assume_tac;
oheimb@7491
   166
fun rtac rl =  resolve_tac [rl];
oheimb@7491
   167
fun dtac rl = dresolve_tac [rl];
clasohm@1460
   168
fun etac rl = eresolve_tac [rl];
oheimb@7491
   169
fun ftac rl =  forward_tac [rl];
oheimb@7491
   170
fun datac thm j = EVERY' (dtac thm::replicate j atac);
oheimb@7491
   171
fun eatac thm j = EVERY' (etac thm::replicate j atac);
oheimb@7491
   172
fun fatac thm j = EVERY' (ftac thm::replicate j atac);
clasohm@0
   173
clasohm@0
   174
(*Use an assumption or some rules ... A popular combination!*)
clasohm@0
   175
fun ares_tac rules = assume_tac  ORELSE'  resolve_tac rules;
clasohm@0
   176
wenzelm@5263
   177
fun solve_tac rules = resolve_tac rules THEN_ALL_NEW assume_tac;
wenzelm@5263
   178
clasohm@0
   179
(*Matching tactics -- as above, but forbid updating of state*)
clasohm@0
   180
fun bimatch_tac brules i = PRIMSEQ (biresolution true brules i);
clasohm@0
   181
fun match_tac rules  = bimatch_tac (map (pair false) rules);
clasohm@0
   182
fun ematch_tac rules = bimatch_tac (map (pair true) rules);
clasohm@0
   183
fun dmatch_tac rls   = ematch_tac (map make_elim rls);
clasohm@0
   184
clasohm@0
   185
(*Smash all flex-flex disagreement pairs in the proof state.*)
clasohm@0
   186
val flexflex_tac = PRIMSEQ flexflex_rule;
clasohm@0
   187
wenzelm@19056
   188
(*Remove duplicate subgoals.*)
wenzelm@10817
   189
fun distinct_subgoals_tac state =
wenzelm@19056
   190
  let
wenzelm@19056
   191
    val perm_tac = PRIMITIVE oo Thm.permute_prems;
paulson@3409
   192
wenzelm@19056
   193
    fun distinct_tac (i, k) =
wenzelm@19056
   194
      perm_tac 0 (i - 1) THEN
wenzelm@19056
   195
      perm_tac 1 (k - 1) THEN
wenzelm@19056
   196
      DETERM (PRIMSEQ (fn st =>
wenzelm@19056
   197
        Thm.compose_no_flatten false (st, 0) 1
wenzelm@19056
   198
          (Drule.incr_indexes st Drule.distinct_prems_rl))) THEN
wenzelm@19056
   199
      perm_tac 1 (1 - k) THEN
wenzelm@19056
   200
      perm_tac 0 (1 - i);
wenzelm@19056
   201
wenzelm@19056
   202
    fun distinct_subgoal_tac i st =
wenzelm@19056
   203
      (case Library.drop (i - 1, Thm.prems_of st) of
wenzelm@19056
   204
        [] => no_tac st
wenzelm@19056
   205
      | A :: Bs =>
wenzelm@19056
   206
          st |> EVERY (fold (fn (B, k) =>
wenzelm@19056
   207
            if A aconv B then cons (distinct_tac (i, k)) else I) (Bs ~~ (1 upto length Bs)) []));
wenzelm@19056
   208
wenzelm@19056
   209
    val goals = Thm.prems_of state;
wenzelm@19056
   210
    val dups = distinct (eq_fst (op aconv)) (goals ~~ (1 upto length goals));
wenzelm@19056
   211
  in EVERY (rev (map (distinct_subgoal_tac o snd) dups)) state end;
paulson@3409
   212
wenzelm@11929
   213
(*Determine print names of goal parameters (reversed)*)
wenzelm@11929
   214
fun innermost_params i st =
wenzelm@11929
   215
  let val (_, _, Bi, _) = dest_state (st, i)
wenzelm@11929
   216
  in rename_wrt_term Bi (Logic.strip_params Bi) end;
wenzelm@11929
   217
paulson@15453
   218
(*params of subgoal i as they are printed*)
paulson@19532
   219
fun params_of_state i st = rev (innermost_params i st);
wenzelm@16425
   220
paulson@15453
   221
(*read instantiations with respect to subgoal i of proof state st*)
paulson@15453
   222
fun read_insts_in_state (st, i, sinsts, rule) =
wenzelm@16425
   223
  let val thy = Thm.theory_of_thm st
paulson@19532
   224
      and params = params_of_state i st
wenzelm@16425
   225
      and rts = types_sorts rule and (types,sorts) = types_sorts st
haftmann@17271
   226
      fun types'(a, ~1) = (case AList.lookup (op =) params a of NONE => types (a, ~1) | sm => sm)
wenzelm@16425
   227
        | types' ixn = types ixn;
wenzelm@16425
   228
      val used = Drule.add_used rule (Drule.add_used st []);
wenzelm@16425
   229
  in read_insts thy rts (types',sorts) used sinsts end;
paulson@15453
   230
clasohm@0
   231
(*Lift and instantiate a rule wrt the given state and subgoal number *)
berghofe@15442
   232
fun lift_inst_rule' (st, i, sinsts, rule) =
paulson@15453
   233
let val (Tinsts,insts) = read_insts_in_state (st, i, sinsts, rule)
paulson@15453
   234
    and {maxidx,...} = rep_thm st
paulson@19532
   235
    and params = params_of_state i st
clasohm@0
   236
    val paramTs = map #2 params
clasohm@0
   237
    and inc = maxidx+1
wenzelm@16876
   238
    fun liftvar (Var ((a,j), T)) = Var((a, j+inc), paramTs---> Logic.incr_tvar inc T)
clasohm@0
   239
      | liftvar t = raise TERM("Variable expected", [t]);
wenzelm@10817
   240
    fun liftterm t = list_abs_free (params,
wenzelm@10805
   241
                                    Logic.incr_indexes(paramTs,inc) t)
clasohm@0
   242
    (*Lifts instantiation pair over params*)
lcp@230
   243
    fun liftpair (cv,ct) = (cterm_fun liftvar cv, cterm_fun liftterm ct)
wenzelm@16876
   244
    val lifttvar = pairself (ctyp_fun (Logic.incr_tvar inc))
paulson@8129
   245
in Drule.instantiate (map lifttvar Tinsts, map liftpair insts)
wenzelm@18145
   246
                     (Thm.lift_rule (Thm.cprem_of st i) rule)
clasohm@0
   247
end;
clasohm@0
   248
berghofe@15442
   249
fun lift_inst_rule (st, i, sinsts, rule) = lift_inst_rule'
wenzelm@20302
   250
  (st, i, map (apfst Syntax.read_indexname) sinsts, rule);
berghofe@15442
   251
nipkow@3984
   252
(*
nipkow@3984
   253
Like lift_inst_rule but takes terms, not strings, where the terms may contain
nipkow@3984
   254
Bounds referring to parameters of the subgoal.
nipkow@3984
   255
nipkow@3984
   256
insts: [...,(vj,tj),...]
nipkow@3984
   257
nipkow@3984
   258
The tj may contain references to parameters of subgoal i of the state st
nipkow@3984
   259
in the form of Bound k, i.e. the tj may be subterms of the subgoal.
nipkow@3984
   260
To saturate the lose bound vars, the tj are enclosed in abstractions
nipkow@3984
   261
corresponding to the parameters of subgoal i, thus turning them into
nipkow@3984
   262
functions. At the same time, the types of the vj are lifted.
nipkow@3984
   263
nipkow@3984
   264
NB: the types in insts must be correctly instantiated already,
nipkow@3984
   265
    i.e. Tinsts is not applied to insts.
nipkow@3984
   266
*)
nipkow@1975
   267
fun term_lift_inst_rule (st, i, Tinsts, insts, rule) =
wenzelm@16425
   268
let val {maxidx,thy,...} = rep_thm st
paulson@19532
   269
    val paramTs = map #2 (params_of_state i st)
nipkow@1966
   270
    and inc = maxidx+1
wenzelm@16876
   271
    fun liftvar ((a,j), T) = Var((a, j+inc), paramTs---> Logic.incr_tvar inc T)
nipkow@1975
   272
    (*lift only Var, not term, which must be lifted already*)
wenzelm@16425
   273
    fun liftpair (v,t) = (cterm_of thy (liftvar v), cterm_of thy t)
berghofe@15797
   274
    fun liftTpair (((a, i), S), T) =
wenzelm@16425
   275
      (ctyp_of thy (TVar ((a, i + inc), S)),
wenzelm@16876
   276
       ctyp_of thy (Logic.incr_tvar inc T))
paulson@8129
   277
in Drule.instantiate (map liftTpair Tinsts, map liftpair insts)
wenzelm@18145
   278
                     (Thm.lift_rule (Thm.cprem_of st i) rule)
nipkow@1966
   279
end;
clasohm@0
   280
clasohm@0
   281
(*** Resolve after lifting and instantation; may refer to parameters of the
clasohm@0
   282
     subgoal.  Fails if "i" is out of range.  ***)
clasohm@0
   283
clasohm@0
   284
(*compose version: arguments are as for bicompose.*)
berghofe@15442
   285
fun gen_compose_inst_tac instf sinsts (bires_flg, rule, nsubgoal) i st =
paulson@8977
   286
  if i > nprems_of st then no_tac st
paulson@8977
   287
  else st |>
berghofe@15442
   288
    (compose_tac (bires_flg, instf (st, i, sinsts, rule), nsubgoal) i
wenzelm@12262
   289
     handle TERM (msg,_)   => (warning msg;  no_tac)
wenzelm@12262
   290
          | THM  (msg,_,_) => (warning msg;  no_tac));
clasohm@0
   291
berghofe@15442
   292
val compose_inst_tac = gen_compose_inst_tac lift_inst_rule;
berghofe@15442
   293
val compose_inst_tac' = gen_compose_inst_tac lift_inst_rule';
berghofe@15442
   294
lcp@761
   295
(*"Resolve" version.  Note: res_inst_tac cannot behave sensibly if the
lcp@761
   296
  terms that are substituted contain (term or type) unknowns from the
lcp@761
   297
  goal, because it is unable to instantiate goal unknowns at the same time.
lcp@761
   298
paulson@2029
   299
  The type checker is instructed not to freeze flexible type vars that
nipkow@952
   300
  were introduced during type inference and still remain in the term at the
nipkow@952
   301
  end.  This increases flexibility but can introduce schematic type vars in
nipkow@952
   302
  goals.
lcp@761
   303
*)
clasohm@0
   304
fun res_inst_tac sinsts rule i =
clasohm@0
   305
    compose_inst_tac sinsts (false, rule, nprems_of rule) i;
clasohm@0
   306
berghofe@15442
   307
fun res_inst_tac' sinsts rule i =
berghofe@15442
   308
    compose_inst_tac' sinsts (false, rule, nprems_of rule) i;
berghofe@15442
   309
paulson@1501
   310
(*eresolve elimination version*)
clasohm@0
   311
fun eres_inst_tac sinsts rule i =
clasohm@0
   312
    compose_inst_tac sinsts (true, rule, nprems_of rule) i;
clasohm@0
   313
berghofe@15464
   314
fun eres_inst_tac' sinsts rule i =
berghofe@15464
   315
    compose_inst_tac' sinsts (true, rule, nprems_of rule) i;
berghofe@15464
   316
lcp@270
   317
(*For forw_inst_tac and dres_inst_tac.  Preserve Var indexes of rl;
lcp@270
   318
  increment revcut_rl instead.*)
wenzelm@10817
   319
fun make_elim_preserve rl =
lcp@270
   320
  let val {maxidx,...} = rep_thm rl
wenzelm@16425
   321
      fun cvar ixn = cterm_of ProtoPure.thy (Var(ixn,propT));
wenzelm@10817
   322
      val revcut_rl' =
wenzelm@10805
   323
          instantiate ([],  [(cvar("V",0), cvar("V",maxidx+1)),
wenzelm@10805
   324
                             (cvar("W",0), cvar("W",maxidx+1))]) revcut_rl
clasohm@0
   325
      val arg = (false, rl, nprems_of rl)
wenzelm@4270
   326
      val [th] = Seq.list_of (bicompose false arg 1 revcut_rl')
clasohm@0
   327
  in  th  end
clasohm@0
   328
  handle Bind => raise THM("make_elim_preserve", 1, [rl]);
clasohm@0
   329
lcp@270
   330
(*instantiate and cut -- for a FACT, anyway...*)
lcp@270
   331
fun cut_inst_tac sinsts rule = res_inst_tac sinsts (make_elim_preserve rule);
clasohm@0
   332
lcp@270
   333
(*forward tactic applies a RULE to an assumption without deleting it*)
lcp@270
   334
fun forw_inst_tac sinsts rule = cut_inst_tac sinsts rule THEN' assume_tac;
lcp@270
   335
lcp@270
   336
(*dresolve tactic applies a RULE to replace an assumption*)
clasohm@0
   337
fun dres_inst_tac sinsts rule = eres_inst_tac sinsts (make_elim_preserve rule);
clasohm@0
   338
oheimb@10347
   339
(*instantiate variables in the whole state*)
oheimb@10347
   340
val instantiate_tac = PRIMITIVE o read_instantiate;
oheimb@10347
   341
berghofe@15797
   342
val instantiate_tac' = PRIMITIVE o Drule.read_instantiate';
berghofe@15797
   343
paulson@1951
   344
(*Deletion of an assumption*)
paulson@1951
   345
fun thin_tac s = eres_inst_tac [("V",s)] thin_rl;
paulson@1951
   346
lcp@270
   347
(*** Applications of cut_rl ***)
clasohm@0
   348
clasohm@0
   349
(*Used by metacut_tac*)
clasohm@0
   350
fun bires_cut_tac arg i =
clasohm@1460
   351
    resolve_tac [cut_rl] i  THEN  biresolve_tac arg (i+1) ;
clasohm@0
   352
clasohm@0
   353
(*The conclusion of the rule gets assumed in subgoal i,
clasohm@0
   354
  while subgoal i+1,... are the premises of the rule.*)
clasohm@0
   355
fun metacut_tac rule = bires_cut_tac [(false,rule)];
clasohm@0
   356
paulson@13650
   357
(*"Cut" a list of rules into the goal.  Their premises will become new
paulson@13650
   358
  subgoals.*)
paulson@13650
   359
fun cut_rules_tac ths i = EVERY (map (fn th => metacut_tac th i) ths);
paulson@13650
   360
paulson@13650
   361
(*As above, but inserts only facts (unconditional theorems);
paulson@13650
   362
  generates no additional subgoals. *)
wenzelm@20232
   363
fun cut_facts_tac ths = cut_rules_tac (filter Thm.no_prems ths);
clasohm@0
   364
clasohm@0
   365
(*Introduce the given proposition as a lemma and subgoal*)
wenzelm@12847
   366
fun subgoal_tac sprop =
wenzelm@12847
   367
  DETERM o res_inst_tac [("psi", sprop)] cut_rl THEN' SUBGOAL (fn (prop, _) =>
wenzelm@12847
   368
    let val concl' = Logic.strip_assums_concl prop in
paulson@4178
   369
      if null (term_tvars concl') then ()
paulson@4178
   370
      else warning"Type variables in new subgoal: add a type constraint?";
wenzelm@12847
   371
      all_tac
wenzelm@12847
   372
  end);
clasohm@0
   373
lcp@439
   374
(*Introduce a list of lemmas and subgoals*)
lcp@439
   375
fun subgoals_tac sprops = EVERY' (map subgoal_tac sprops);
lcp@439
   376
clasohm@0
   377
clasohm@0
   378
(**** Indexing and filtering of theorems ****)
clasohm@0
   379
clasohm@0
   380
(*Returns the list of potentially resolvable theorems for the goal "prem",
wenzelm@10805
   381
        using the predicate  could(subgoal,concl).
clasohm@0
   382
  Resulting list is no longer than "limit"*)
clasohm@0
   383
fun filter_thms could (limit, prem, ths) =
clasohm@0
   384
  let val pb = Logic.strip_assums_concl prem;   (*delete assumptions*)
clasohm@0
   385
      fun filtr (limit, []) = []
wenzelm@10805
   386
        | filtr (limit, th::ths) =
wenzelm@10805
   387
            if limit=0 then  []
wenzelm@10805
   388
            else if could(pb, concl_of th)  then th :: filtr(limit-1, ths)
wenzelm@10805
   389
            else filtr(limit,ths)
clasohm@0
   390
  in  filtr(limit,ths)  end;
clasohm@0
   391
clasohm@0
   392
clasohm@0
   393
(*** biresolution and resolution using nets ***)
clasohm@0
   394
clasohm@0
   395
(** To preserve the order of the rules, tag them with increasing integers **)
clasohm@0
   396
clasohm@0
   397
(*insert tags*)
clasohm@0
   398
fun taglist k [] = []
clasohm@0
   399
  | taglist k (x::xs) = (k,x) :: taglist (k+1) xs;
clasohm@0
   400
clasohm@0
   401
(*remove tags and suppress duplicates -- list is assumed sorted!*)
clasohm@0
   402
fun untaglist [] = []
clasohm@0
   403
  | untaglist [(k:int,x)] = [x]
clasohm@0
   404
  | untaglist ((k,x) :: (rest as (k',x')::_)) =
clasohm@0
   405
      if k=k' then untaglist rest
clasohm@0
   406
      else    x :: untaglist rest;
clasohm@0
   407
clasohm@0
   408
(*return list elements in original order*)
wenzelm@10817
   409
fun orderlist kbrls = untaglist (sort (int_ord o pairself fst) kbrls);
clasohm@0
   410
clasohm@0
   411
(*insert one tagged brl into the pair of nets*)
wenzelm@12320
   412
fun insert_tagged_brl (kbrl as (k, (eres, th)), (inet, enet)) =
wenzelm@12320
   413
  if eres then
wenzelm@12320
   414
    (case try Thm.major_prem_of th of
wenzelm@16809
   415
      SOME prem => (inet, Net.insert_term (K false) (prem, kbrl) enet)
skalberg@15531
   416
    | NONE => error "insert_tagged_brl: elimination rule with no premises")
wenzelm@16809
   417
  else (Net.insert_term (K false) (concl_of th, kbrl) inet, enet);
clasohm@0
   418
clasohm@0
   419
(*build a pair of nets for biresolution*)
wenzelm@10817
   420
fun build_netpair netpair brls =
skalberg@15574
   421
    foldr insert_tagged_brl netpair (taglist 1 brls);
clasohm@0
   422
wenzelm@12320
   423
(*delete one kbrl from the pair of nets*)
wenzelm@16809
   424
fun eq_kbrl ((_, (_, th)), (_, (_, th'))) = Drule.eq_thm_prop (th, th')
wenzelm@16809
   425
wenzelm@12320
   426
fun delete_tagged_brl (brl as (eres, th), (inet, enet)) =
paulson@13925
   427
  (if eres then
wenzelm@12320
   428
    (case try Thm.major_prem_of th of
wenzelm@16809
   429
      SOME prem => (inet, Net.delete_term eq_kbrl (prem, ((), brl)) enet)
skalberg@15531
   430
    | NONE => (inet, enet))  (*no major premise: ignore*)
wenzelm@16809
   431
  else (Net.delete_term eq_kbrl (Thm.concl_of th, ((), brl)) inet, enet))
paulson@13925
   432
  handle Net.DELETE => (inet,enet);
paulson@1801
   433
paulson@1801
   434
wenzelm@10817
   435
(*biresolution using a pair of nets rather than rules.
paulson@3706
   436
    function "order" must sort and possibly filter the list of brls.
paulson@3706
   437
    boolean "match" indicates matching or unification.*)
paulson@3706
   438
fun biresolution_from_nets_tac order match (inet,enet) =
clasohm@0
   439
  SUBGOAL
clasohm@0
   440
    (fn (prem,i) =>
clasohm@0
   441
      let val hyps = Logic.strip_assums_hyp prem
wenzelm@10817
   442
          and concl = Logic.strip_assums_concl prem
wenzelm@19482
   443
          val kbrls = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps
paulson@3706
   444
      in PRIMSEQ (biresolution match (order kbrls) i) end);
clasohm@0
   445
paulson@3706
   446
(*versions taking pre-built nets.  No filtering of brls*)
paulson@3706
   447
val biresolve_from_nets_tac = biresolution_from_nets_tac orderlist false;
paulson@3706
   448
val bimatch_from_nets_tac   = biresolution_from_nets_tac orderlist true;
clasohm@0
   449
clasohm@0
   450
(*fast versions using nets internally*)
lcp@670
   451
val net_biresolve_tac =
lcp@670
   452
    biresolve_from_nets_tac o build_netpair(Net.empty,Net.empty);
lcp@670
   453
lcp@670
   454
val net_bimatch_tac =
lcp@670
   455
    bimatch_from_nets_tac o build_netpair(Net.empty,Net.empty);
clasohm@0
   456
clasohm@0
   457
(*** Simpler version for resolve_tac -- only one net, and no hyps ***)
clasohm@0
   458
clasohm@0
   459
(*insert one tagged rl into the net*)
clasohm@0
   460
fun insert_krl (krl as (k,th), net) =
wenzelm@16809
   461
    Net.insert_term (K false) (concl_of th, krl) net;
clasohm@0
   462
clasohm@0
   463
(*build a net of rules for resolution*)
wenzelm@10817
   464
fun build_net rls =
skalberg@15574
   465
    foldr insert_krl Net.empty (taglist 1 rls);
clasohm@0
   466
clasohm@0
   467
(*resolution using a net rather than rules; pred supports filt_resolve_tac*)
clasohm@0
   468
fun filt_resolution_from_net_tac match pred net =
clasohm@0
   469
  SUBGOAL
clasohm@0
   470
    (fn (prem,i) =>
clasohm@0
   471
      let val krls = Net.unify_term net (Logic.strip_assums_concl prem)
wenzelm@10817
   472
      in
wenzelm@10817
   473
         if pred krls
clasohm@0
   474
         then PRIMSEQ
wenzelm@10805
   475
                (biresolution match (map (pair false) (orderlist krls)) i)
clasohm@0
   476
         else no_tac
clasohm@0
   477
      end);
clasohm@0
   478
clasohm@0
   479
(*Resolve the subgoal using the rules (making a net) unless too flexible,
clasohm@0
   480
   which means more than maxr rules are unifiable.      *)
wenzelm@10817
   481
fun filt_resolve_tac rules maxr =
clasohm@0
   482
    let fun pred krls = length krls <= maxr
clasohm@0
   483
    in  filt_resolution_from_net_tac false pred (build_net rules)  end;
clasohm@0
   484
clasohm@0
   485
(*versions taking pre-built nets*)
clasohm@0
   486
val resolve_from_net_tac = filt_resolution_from_net_tac false (K true);
clasohm@0
   487
val match_from_net_tac = filt_resolution_from_net_tac true (K true);
clasohm@0
   488
clasohm@0
   489
(*fast versions using nets internally*)
clasohm@0
   490
val net_resolve_tac = resolve_from_net_tac o build_net;
clasohm@0
   491
val net_match_tac = match_from_net_tac o build_net;
clasohm@0
   492
clasohm@0
   493
clasohm@0
   494
(*** For Natural Deduction using (bires_flg, rule) pairs ***)
clasohm@0
   495
clasohm@0
   496
(*The number of new subgoals produced by the brule*)
lcp@1077
   497
fun subgoals_of_brl (true,rule)  = nprems_of rule - 1
lcp@1077
   498
  | subgoals_of_brl (false,rule) = nprems_of rule;
clasohm@0
   499
clasohm@0
   500
(*Less-than test: for sorting to minimize number of new subgoals*)
clasohm@0
   501
fun lessb (brl1,brl2) = subgoals_of_brl brl1 < subgoals_of_brl brl2;
clasohm@0
   502
clasohm@0
   503
lcp@69
   504
(*** Renaming of parameters in a subgoal
lcp@69
   505
     Names may contain letters, digits or primes and must be
lcp@69
   506
     separated by blanks ***)
clasohm@0
   507
wenzelm@9535
   508
fun rename_params_tac xs i =
wenzelm@14673
   509
  case Library.find_first (not o Syntax.is_identifier) xs of
skalberg@15531
   510
      SOME x => error ("Not an identifier: " ^ x)
wenzelm@16425
   511
    | NONE =>
paulson@13559
   512
       (if !Logic.auto_rename
wenzelm@16425
   513
         then (warning "Resetting Logic.auto_rename";
wenzelm@16425
   514
             Logic.auto_rename := false)
wenzelm@16425
   515
        else (); PRIMITIVE (rename_params_rule (xs, i)));
wenzelm@9535
   516
wenzelm@10817
   517
fun rename_tac str i =
wenzelm@10817
   518
  let val cs = Symbol.explode str in
wenzelm@4693
   519
  case #2 (take_prefix (Symbol.is_letdig orf Symbol.is_blank) cs) of
wenzelm@9535
   520
      [] => rename_params_tac (scanwords Symbol.is_letdig cs) i
clasohm@0
   521
    | c::_ => error ("Illegal character: " ^ c)
clasohm@0
   522
  end;
clasohm@0
   523
paulson@1501
   524
(*Rename recent parameters using names generated from a and the suffixes,
paulson@1501
   525
  provided the string a, which represents a term, is an identifier. *)
wenzelm@10817
   526
fun rename_last_tac a sufs i =
clasohm@0
   527
  let val names = map (curry op^ a) sufs
clasohm@0
   528
  in  if Syntax.is_identifier a
clasohm@0
   529
      then PRIMITIVE (rename_params_rule (names,i))
clasohm@0
   530
      else all_tac
clasohm@0
   531
  end;
clasohm@0
   532
paulson@1501
   533
(*rotate_tac n i: rotate the assumptions of subgoal i by n positions, from
paulson@1501
   534
  right to left if n is positive, and from left to right if n is negative.*)
paulson@2672
   535
fun rotate_tac 0 i = all_tac
paulson@2672
   536
  | rotate_tac k i = PRIMITIVE (rotate_rule k i);
nipkow@1209
   537
paulson@7248
   538
(*Rotates the given subgoal to be the last.*)
paulson@7248
   539
fun defer_tac i = PRIMITIVE (permute_prems (i-1) 1);
paulson@7248
   540
nipkow@5974
   541
(* remove premises that do not satisfy p; fails if all prems satisfy p *)
nipkow@5974
   542
fun filter_prems_tac p =
skalberg@15531
   543
  let fun Then NONE tac = SOME tac
skalberg@15531
   544
        | Then (SOME tac) tac' = SOME(tac THEN' tac');
wenzelm@19473
   545
      fun thins H (tac,n) =
nipkow@5974
   546
        if p H then (tac,n+1)
nipkow@5974
   547
        else (Then tac (rotate_tac n THEN' etac thin_rl),0);
nipkow@5974
   548
  in SUBGOAL(fn (subg,n) =>
nipkow@5974
   549
       let val Hs = Logic.strip_assums_hyp subg
wenzelm@19473
   550
       in case fst(fold thins Hs (NONE,0)) of
skalberg@15531
   551
            NONE => no_tac | SOME tac => tac n
nipkow@5974
   552
       end)
nipkow@5974
   553
  end;
nipkow@5974
   554
clasohm@0
   555
end;
paulson@1501
   556
wenzelm@11774
   557
structure BasicTactic: BASIC_TACTIC = Tactic;
wenzelm@11774
   558
open BasicTactic;