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