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