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