src/Pure/tactic.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 60793 bbcd4ab6d26e
child 67721 5348bea4accd
permissions -rw-r--r--
more robust sorted_entries;
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@36546
    10
  val rule_by_tactic: Proof.context -> tactic -> thm -> thm
wenzelm@58963
    11
  val assume_tac: Proof.context -> int -> tactic
wenzelm@23223
    12
  val eq_assume_tac: int -> tactic
wenzelm@58956
    13
  val compose_tac: Proof.context -> (bool * thm * int) -> int -> tactic
wenzelm@23223
    14
  val make_elim: thm -> thm
wenzelm@59498
    15
  val biresolve0_tac: (bool * thm) list -> int -> tactic
wenzelm@59498
    16
  val biresolve_tac: Proof.context -> (bool * thm) list -> int -> tactic
wenzelm@59498
    17
  val resolve0_tac: thm list -> int -> tactic
wenzelm@59498
    18
  val resolve_tac: Proof.context -> thm list -> int -> tactic
wenzelm@59498
    19
  val eresolve0_tac: thm list -> int -> tactic
wenzelm@59498
    20
  val eresolve_tac: Proof.context -> thm list -> int -> tactic
wenzelm@59498
    21
  val forward_tac: Proof.context -> thm list -> int -> tactic
wenzelm@59498
    22
  val dresolve0_tac: thm list -> int -> tactic
wenzelm@59498
    23
  val dresolve_tac: Proof.context -> thm list -> int -> tactic
wenzelm@60774
    24
  val ares_tac: Proof.context -> thm list -> int -> tactic
wenzelm@59498
    25
  val solve_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58957
    26
  val bimatch_tac: Proof.context -> (bool * thm) list -> int -> tactic
wenzelm@58957
    27
  val match_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58957
    28
  val ematch_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58957
    29
  val dmatch_tac: Proof.context -> thm list -> int -> tactic
wenzelm@58950
    30
  val flexflex_tac: Proof.context -> tactic
wenzelm@23223
    31
  val distinct_subgoal_tac: int -> tactic
wenzelm@23223
    32
  val distinct_subgoals_tac: tactic
wenzelm@46704
    33
  val cut_tac: thm -> int -> tactic
wenzelm@23223
    34
  val cut_rules_tac: thm list -> int -> tactic
wenzelm@23223
    35
  val cut_facts_tac: thm list -> int -> tactic
wenzelm@23223
    36
  val filter_thms: (term * term -> bool) -> int * term * thm list -> thm list
wenzelm@59164
    37
  val biresolution_from_nets_tac: Proof.context ->
wenzelm@59164
    38
    ('a list -> (bool * thm) list) -> bool -> 'a Net.net * 'a Net.net -> int -> tactic
wenzelm@59164
    39
  val biresolve_from_nets_tac: Proof.context ->
wenzelm@59164
    40
    (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net -> int -> tactic
wenzelm@59164
    41
  val bimatch_from_nets_tac: Proof.context ->
wenzelm@59164
    42
    (int * (bool * thm)) Net.net * (int * (bool * thm)) Net.net -> int -> tactic
wenzelm@59164
    43
  val filt_resolve_from_net_tac: Proof.context -> int -> (int * thm) Net.net -> int -> tactic
wenzelm@59164
    44
  val resolve_from_net_tac: Proof.context -> (int * thm) Net.net -> int -> tactic
wenzelm@59164
    45
  val match_from_net_tac: Proof.context -> (int * thm) Net.net -> int -> tactic
wenzelm@23223
    46
  val subgoals_of_brl: bool * thm -> int
wenzelm@23223
    47
  val lessb: (bool * thm) * (bool * thm) -> bool
wenzelm@27243
    48
  val rename_tac: string list -> int -> tactic
wenzelm@23223
    49
  val rotate_tac: int -> int -> tactic
wenzelm@23223
    50
  val defer_tac: int -> tactic
wenzelm@49865
    51
  val prefer_tac: int -> tactic
wenzelm@59498
    52
  val filter_prems_tac: Proof.context -> (term -> bool) -> int -> tactic
wenzelm@11774
    53
end;
clasohm@0
    54
wenzelm@11774
    55
signature TACTIC =
wenzelm@11774
    56
sig
wenzelm@11774
    57
  include BASIC_TACTIC
wenzelm@23223
    58
  val insert_tagged_brl: 'a * (bool * thm) ->
wenzelm@23223
    59
    ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net ->
wenzelm@23223
    60
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@23223
    61
  val delete_tagged_brl: bool * thm ->
wenzelm@23223
    62
    ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net ->
wenzelm@23223
    63
      ('a * (bool * thm)) Net.net * ('a * (bool * thm)) Net.net
wenzelm@23223
    64
  val eq_kbrl: ('a * (bool * thm)) * ('a * (bool * thm)) -> bool
wenzelm@32971
    65
  val build_net: thm list -> (int * thm) Net.net
wenzelm@11774
    66
end;
clasohm@0
    67
wenzelm@11774
    68
structure Tactic: TACTIC =
clasohm@0
    69
struct
clasohm@0
    70
paulson@1501
    71
(*Discover which goal is chosen:  SOMEGOAL(trace_goalno_tac tac) *)
wenzelm@10817
    72
fun trace_goalno_tac tac i st =
wenzelm@4270
    73
    case Seq.pull(tac i st) of
skalberg@15531
    74
        NONE    => Seq.empty
wenzelm@12262
    75
      | seqcell => (tracing ("Subgoal " ^ string_of_int i ^ " selected");
wenzelm@10805
    76
                         Seq.make(fn()=> seqcell));
clasohm@0
    77
clasohm@0
    78
(*Makes a rule by applying a tactic to an existing rule*)
wenzelm@36546
    79
fun rule_by_tactic ctxt tac rl =
wenzelm@19925
    80
  let
wenzelm@52087
    81
    val thy = Proof_Context.theory_of ctxt;
wenzelm@36546
    82
    val ctxt' = Variable.declare_thm rl ctxt;
wenzelm@52087
    83
    val ((_, [st]), ctxt'') = Variable.import true [Thm.transfer thy rl] ctxt';
wenzelm@19925
    84
  in
wenzelm@19925
    85
    (case Seq.pull (tac st) of
wenzelm@19925
    86
      NONE => raise THM ("rule_by_tactic", 0, [rl])
wenzelm@36546
    87
    | SOME (st', _) => zero_var_indexes (singleton (Variable.export ctxt'' ctxt') st'))
paulson@2688
    88
  end;
wenzelm@10817
    89
wenzelm@19925
    90
clasohm@0
    91
(*** Basic tactics ***)
clasohm@0
    92
clasohm@0
    93
(*** The following fail if the goal number is out of range:
clasohm@0
    94
     thus (REPEAT (resolve_tac rules i)) stops once subgoal i disappears. *)
clasohm@0
    95
clasohm@0
    96
(*Solve subgoal i by assumption*)
wenzelm@58963
    97
fun assume_tac ctxt i = PRIMSEQ (Thm.assumption (SOME ctxt) i);
clasohm@0
    98
clasohm@0
    99
(*Solve subgoal i by assumption, using no unification*)
wenzelm@31945
   100
fun eq_assume_tac i = PRIMITIVE (Thm.eq_assumption i);
clasohm@0
   101
wenzelm@23223
   102
clasohm@0
   103
(** Resolution/matching tactics **)
clasohm@0
   104
clasohm@0
   105
(*The composition rule/state: no lifting or var renaming.
wenzelm@31945
   106
  The arg = (bires_flg, orule, m);  see Thm.bicompose for explanation.*)
wenzelm@58956
   107
fun compose_tac ctxt arg i =
wenzelm@58956
   108
  PRIMSEQ (Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = false} arg i);
clasohm@0
   109
clasohm@0
   110
(*Converts a "destruct" rule like P&Q==>P to an "elimination" rule
clasohm@0
   111
  like [| P&Q; P==>R |] ==> R *)
clasohm@0
   112
fun make_elim rl = zero_var_indexes (rl RS revcut_rl);
clasohm@0
   113
clasohm@0
   114
(*Attack subgoal i by resolution, using flags to indicate elimination rules*)
wenzelm@59498
   115
fun biresolve0_tac brules i = PRIMSEQ (Thm.biresolution NONE false brules i);
wenzelm@59498
   116
fun biresolve_tac ctxt brules i = PRIMSEQ (Thm.biresolution (SOME ctxt) false brules i);
clasohm@0
   117
clasohm@0
   118
(*Resolution: the simple case, works for introduction rules*)
wenzelm@59498
   119
fun resolve0_tac rules = biresolve0_tac (map (pair false) rules);
wenzelm@59498
   120
fun resolve_tac ctxt rules = biresolve_tac ctxt (map (pair false) rules);
clasohm@0
   121
clasohm@0
   122
(*Resolution with elimination rules only*)
wenzelm@59498
   123
fun eresolve0_tac rules = biresolve0_tac (map (pair true) rules);
wenzelm@59498
   124
fun eresolve_tac ctxt rules = biresolve_tac ctxt (map (pair true) rules);
clasohm@0
   125
clasohm@0
   126
(*Forward reasoning using destruction rules.*)
wenzelm@60776
   127
fun forward_tac ctxt rls = resolve_tac ctxt (map make_elim rls) THEN' assume_tac ctxt;
clasohm@0
   128
clasohm@0
   129
(*Like forward_tac, but deletes the assumption after use.*)
wenzelm@59498
   130
fun dresolve0_tac rls = eresolve0_tac (map make_elim rls);
wenzelm@59498
   131
fun dresolve_tac ctxt rls = eresolve_tac ctxt (map make_elim rls);
clasohm@0
   132
wenzelm@60774
   133
(*Use an assumption or some rules*)
wenzelm@60774
   134
fun ares_tac ctxt rules = assume_tac ctxt ORELSE' resolve_tac ctxt rules;
clasohm@0
   135
wenzelm@59498
   136
fun solve_tac ctxt rules = resolve_tac ctxt rules THEN_ALL_NEW assume_tac ctxt;
wenzelm@5263
   137
clasohm@0
   138
(*Matching tactics -- as above, but forbid updating of state*)
wenzelm@58957
   139
fun bimatch_tac ctxt brules i = PRIMSEQ (Thm.biresolution (SOME ctxt) true brules i);
wenzelm@58957
   140
fun match_tac ctxt rules = bimatch_tac ctxt (map (pair false) rules);
wenzelm@58957
   141
fun ematch_tac ctxt rules = bimatch_tac ctxt (map (pair true) rules);
wenzelm@58957
   142
fun dmatch_tac ctxt rls = ematch_tac ctxt (map make_elim rls);
clasohm@0
   143
clasohm@0
   144
(*Smash all flex-flex disagreement pairs in the proof state.*)
wenzelm@58950
   145
fun flexflex_tac ctxt = PRIMSEQ (Thm.flexflex_rule (SOME ctxt));
clasohm@0
   146
wenzelm@19056
   147
(*Remove duplicate subgoals.*)
wenzelm@50081
   148
val permute_tac = PRIMITIVE oo Thm.permute_prems;
paulson@22560
   149
fun distinct_tac (i, k) =
wenzelm@50081
   150
  permute_tac 0 (i - 1) THEN
wenzelm@50081
   151
  permute_tac 1 (k - 1) THEN
wenzelm@51602
   152
  PRIMITIVE (fn st => Drule.comp_no_flatten (st, 0) 1 Drule.distinct_prems_rl) THEN
wenzelm@50081
   153
  permute_tac 1 (1 - k) THEN
wenzelm@50081
   154
  permute_tac 0 (1 - i);
paulson@22560
   155
paulson@22560
   156
fun distinct_subgoal_tac i st =
haftmann@33957
   157
  (case drop (i - 1) (Thm.prems_of st) of
paulson@22560
   158
    [] => no_tac st
paulson@22560
   159
  | A :: Bs =>
paulson@22560
   160
      st |> EVERY (fold (fn (B, k) =>
wenzelm@23223
   161
        if A aconv B then cons (distinct_tac (i, k)) else I) (Bs ~~ (1 upto length Bs)) []));
paulson@22560
   162
wenzelm@10817
   163
fun distinct_subgoals_tac state =
wenzelm@19056
   164
  let
wenzelm@19056
   165
    val goals = Thm.prems_of state;
wenzelm@19056
   166
    val dups = distinct (eq_fst (op aconv)) (goals ~~ (1 upto length goals));
wenzelm@19056
   167
  in EVERY (rev (map (distinct_subgoal_tac o snd) dups)) state end;
paulson@3409
   168
paulson@1951
   169
lcp@270
   170
(*** Applications of cut_rl ***)
clasohm@0
   171
clasohm@0
   172
(*The conclusion of the rule gets assumed in subgoal i,
clasohm@0
   173
  while subgoal i+1,... are the premises of the rule.*)
wenzelm@59498
   174
fun cut_tac rule i = resolve0_tac [cut_rl] i THEN resolve0_tac [rule] (i + 1);
clasohm@0
   175
paulson@13650
   176
(*"Cut" a list of rules into the goal.  Their premises will become new
paulson@13650
   177
  subgoals.*)
wenzelm@46704
   178
fun cut_rules_tac ths i = EVERY (map (fn th => cut_tac th i) ths);
paulson@13650
   179
paulson@13650
   180
(*As above, but inserts only facts (unconditional theorems);
paulson@13650
   181
  generates no additional subgoals. *)
wenzelm@20232
   182
fun cut_facts_tac ths = cut_rules_tac (filter Thm.no_prems ths);
clasohm@0
   183
clasohm@0
   184
clasohm@0
   185
(**** Indexing and filtering of theorems ****)
clasohm@0
   186
clasohm@0
   187
(*Returns the list of potentially resolvable theorems for the goal "prem",
wenzelm@10805
   188
        using the predicate  could(subgoal,concl).
clasohm@0
   189
  Resulting list is no longer than "limit"*)
clasohm@0
   190
fun filter_thms could (limit, prem, ths) =
clasohm@0
   191
  let val pb = Logic.strip_assums_concl prem;   (*delete assumptions*)
clasohm@0
   192
      fun filtr (limit, []) = []
wenzelm@10805
   193
        | filtr (limit, th::ths) =
wenzelm@10805
   194
            if limit=0 then  []
wenzelm@59582
   195
            else if could(pb, Thm.concl_of th)  then th :: filtr(limit-1, ths)
wenzelm@10805
   196
            else filtr(limit,ths)
clasohm@0
   197
  in  filtr(limit,ths)  end;
clasohm@0
   198
clasohm@0
   199
clasohm@0
   200
(*** biresolution and resolution using nets ***)
clasohm@0
   201
clasohm@0
   202
(** To preserve the order of the rules, tag them with increasing integers **)
clasohm@0
   203
clasohm@0
   204
(*insert one tagged brl into the pair of nets*)
wenzelm@23178
   205
fun insert_tagged_brl (kbrl as (k, (eres, th))) (inet, enet) =
wenzelm@12320
   206
  if eres then
wenzelm@12320
   207
    (case try Thm.major_prem_of th of
wenzelm@16809
   208
      SOME prem => (inet, Net.insert_term (K false) (prem, kbrl) enet)
skalberg@15531
   209
    | NONE => error "insert_tagged_brl: elimination rule with no premises")
wenzelm@59582
   210
  else (Net.insert_term (K false) (Thm.concl_of th, kbrl) inet, enet);
clasohm@0
   211
wenzelm@12320
   212
(*delete one kbrl from the pair of nets*)
wenzelm@22360
   213
fun eq_kbrl ((_, (_, th)), (_, (_, th'))) = Thm.eq_thm_prop (th, th')
wenzelm@16809
   214
wenzelm@23178
   215
fun delete_tagged_brl (brl as (eres, th)) (inet, enet) =
paulson@13925
   216
  (if eres then
wenzelm@12320
   217
    (case try Thm.major_prem_of th of
wenzelm@16809
   218
      SOME prem => (inet, Net.delete_term eq_kbrl (prem, ((), brl)) enet)
skalberg@15531
   219
    | NONE => (inet, enet))  (*no major premise: ignore*)
wenzelm@16809
   220
  else (Net.delete_term eq_kbrl (Thm.concl_of th, ((), brl)) inet, enet))
paulson@13925
   221
  handle Net.DELETE => (inet,enet);
paulson@1801
   222
paulson@1801
   223
wenzelm@10817
   224
(*biresolution using a pair of nets rather than rules.
paulson@3706
   225
    function "order" must sort and possibly filter the list of brls.
paulson@3706
   226
    boolean "match" indicates matching or unification.*)
wenzelm@59164
   227
fun biresolution_from_nets_tac ctxt order match (inet, enet) =
clasohm@0
   228
  SUBGOAL
wenzelm@59164
   229
    (fn (prem, i) =>
wenzelm@59164
   230
      let
wenzelm@59164
   231
        val hyps = Logic.strip_assums_hyp prem;
wenzelm@59164
   232
        val concl = Logic.strip_assums_concl prem;
wenzelm@59164
   233
        val kbrls = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps;
wenzelm@59164
   234
      in PRIMSEQ (Thm.biresolution (SOME ctxt) match (order kbrls) i) end);
clasohm@0
   235
paulson@3706
   236
(*versions taking pre-built nets.  No filtering of brls*)
wenzelm@59164
   237
fun biresolve_from_nets_tac ctxt = biresolution_from_nets_tac ctxt order_list false;
wenzelm@59164
   238
fun bimatch_from_nets_tac ctxt = biresolution_from_nets_tac ctxt order_list true;
clasohm@0
   239
clasohm@0
   240
clasohm@0
   241
(*** Simpler version for resolve_tac -- only one net, and no hyps ***)
clasohm@0
   242
clasohm@0
   243
(*insert one tagged rl into the net*)
wenzelm@23178
   244
fun insert_krl (krl as (k,th)) =
wenzelm@59582
   245
  Net.insert_term (K false) (Thm.concl_of th, krl);
clasohm@0
   246
clasohm@0
   247
(*build a net of rules for resolution*)
wenzelm@10817
   248
fun build_net rls =
wenzelm@30558
   249
  fold_rev insert_krl (tag_list 1 rls) Net.empty;
clasohm@0
   250
clasohm@0
   251
(*resolution using a net rather than rules; pred supports filt_resolve_tac*)
wenzelm@59164
   252
fun filt_resolution_from_net_tac ctxt match pred net =
wenzelm@59164
   253
  SUBGOAL (fn (prem, i) =>
wenzelm@58950
   254
    let val krls = Net.unify_term net (Logic.strip_assums_concl prem) in
wenzelm@58950
   255
      if pred krls then
wenzelm@59164
   256
        PRIMSEQ (Thm.biresolution (SOME ctxt) match (map (pair false) (order_list krls)) i)
wenzelm@58950
   257
      else no_tac
wenzelm@58950
   258
    end);
clasohm@0
   259
clasohm@0
   260
(*Resolve the subgoal using the rules (making a net) unless too flexible,
clasohm@0
   261
   which means more than maxr rules are unifiable.      *)
wenzelm@59164
   262
fun filt_resolve_from_net_tac ctxt maxr net =
wenzelm@59164
   263
  let fun pred krls = length krls <= maxr
wenzelm@59164
   264
  in filt_resolution_from_net_tac ctxt false pred net end;
clasohm@0
   265
clasohm@0
   266
(*versions taking pre-built nets*)
wenzelm@59164
   267
fun resolve_from_net_tac ctxt = filt_resolution_from_net_tac ctxt false (K true);
wenzelm@59164
   268
fun match_from_net_tac ctxt = filt_resolution_from_net_tac ctxt true (K true);
clasohm@0
   269
clasohm@0
   270
clasohm@0
   271
(*** For Natural Deduction using (bires_flg, rule) pairs ***)
clasohm@0
   272
clasohm@0
   273
(*The number of new subgoals produced by the brule*)
wenzelm@59582
   274
fun subgoals_of_brl (true, rule) = Thm.nprems_of rule - 1
wenzelm@59582
   275
  | subgoals_of_brl (false, rule) = Thm.nprems_of rule;
clasohm@0
   276
clasohm@0
   277
(*Less-than test: for sorting to minimize number of new subgoals*)
clasohm@0
   278
fun lessb (brl1,brl2) = subgoals_of_brl brl1 < subgoals_of_brl brl2;
clasohm@0
   279
clasohm@0
   280
wenzelm@27243
   281
(*Renaming of parameters in a subgoal*)
wenzelm@27243
   282
fun rename_tac xs i =
wenzelm@59584
   283
  case find_first (not o Symbol_Pos.is_identifier) xs of
skalberg@15531
   284
      SOME x => error ("Not an identifier: " ^ x)
wenzelm@31945
   285
    | NONE => PRIMITIVE (Thm.rename_params_rule (xs, i));
wenzelm@9535
   286
paulson@1501
   287
(*rotate_tac n i: rotate the assumptions of subgoal i by n positions, from
paulson@1501
   288
  right to left if n is positive, and from left to right if n is negative.*)
paulson@2672
   289
fun rotate_tac 0 i = all_tac
wenzelm@31945
   290
  | rotate_tac k i = PRIMITIVE (Thm.rotate_rule k i);
nipkow@1209
   291
wenzelm@59749
   292
(*Rotate the given subgoal to be the last.*)
wenzelm@31945
   293
fun defer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1);
paulson@7248
   294
wenzelm@59749
   295
(*Rotate the given subgoal to be the first.*)
wenzelm@49865
   296
fun prefer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1 #> Thm.permute_prems 0 ~1);
wenzelm@49865
   297
wenzelm@59749
   298
(*Remove premises that do not satisfy pred; fails if all prems satisfy pred.*)
wenzelm@59749
   299
fun filter_prems_tac ctxt pred =
wenzelm@59749
   300
  let
wenzelm@59749
   301
    fun Then NONE tac = SOME tac
wenzelm@59749
   302
      | Then (SOME tac) tac' = SOME (tac THEN' tac');
wenzelm@59749
   303
    fun thins H (tac, n) =
wenzelm@59749
   304
      if pred H then (tac, n + 1)
wenzelm@59749
   305
      else (Then tac (rotate_tac n THEN' eresolve_tac ctxt [thin_rl]), 0);
wenzelm@59749
   306
  in
wenzelm@59749
   307
    SUBGOAL (fn (goal, i) =>
wenzelm@59749
   308
      let val Hs = Logic.strip_assums_hyp goal in
wenzelm@59749
   309
        (case fst (fold thins Hs (NONE, 0)) of
wenzelm@59749
   310
          NONE => no_tac
wenzelm@59749
   311
        | SOME tac => tac i)
wenzelm@59749
   312
      end)
nipkow@5974
   313
  end;
nipkow@5974
   314
clasohm@0
   315
end;
paulson@1501
   316
wenzelm@32971
   317
structure Basic_Tactic: BASIC_TACTIC = Tactic;
wenzelm@32971
   318
open Basic_Tactic;