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