src/Pure/tactic.ML
author wenzelm
Wed May 29 18:25:11 2013 +0200 (2013-05-29)
changeset 52223 5bb6ae8acb87
parent 52087 f3075fc4f5f6
child 58837 e84d900cd287
permissions -rw-r--r--
tuned signature -- more explicit flags for low-level Thm.bicompose;
     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 thy = Proof_Context.theory_of ctxt;
    89     val ctxt' = Variable.declare_thm rl ctxt;
    90     val ((_, [st]), ctxt'') = Variable.import true [Thm.transfer thy rl] ctxt';
    91   in
    92     (case Seq.pull (tac st) of
    93       NONE => raise THM ("rule_by_tactic", 0, [rl])
    94     | SOME (st', _) => zero_var_indexes (singleton (Variable.export ctxt'' ctxt') st'))
    95   end;
    96 
    97 
    98 (*** Basic tactics ***)
    99 
   100 (*** The following fail if the goal number is out of range:
   101      thus (REPEAT (resolve_tac rules i)) stops once subgoal i disappears. *)
   102 
   103 (*Solve subgoal i by assumption*)
   104 fun assume_tac i = PRIMSEQ (Thm.assumption 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 arg i =
   115   PRIMSEQ (Thm.bicompose {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 biresolve_tac brules i = PRIMSEQ (Thm.biresolution false brules i);
   123 
   124 (*Resolution: the simple case, works for introduction rules*)
   125 fun resolve_tac rules = biresolve_tac (map (pair false) rules);
   126 
   127 (*Resolution with elimination rules only*)
   128 fun eresolve_tac rules = biresolve_tac (map (pair true) rules);
   129 
   130 (*Forward reasoning using destruction rules.*)
   131 fun forward_tac rls = resolve_tac (map make_elim rls) THEN' assume_tac;
   132 
   133 (*Like forward_tac, but deletes the assumption after use.*)
   134 fun dresolve_tac rls = eresolve_tac (map make_elim rls);
   135 
   136 (*Shorthand versions: for resolution with a single theorem*)
   137 val atac    =   assume_tac;
   138 fun rtac rl =  resolve_tac [rl];
   139 fun dtac rl = dresolve_tac [rl];
   140 fun etac rl = eresolve_tac [rl];
   141 fun ftac rl =  forward_tac [rl];
   142 
   143 (*Use an assumption or some rules ... A popular combination!*)
   144 fun ares_tac rules = assume_tac  ORELSE'  resolve_tac rules;
   145 
   146 fun solve_tac rules = resolve_tac rules THEN_ALL_NEW assume_tac;
   147 
   148 (*Matching tactics -- as above, but forbid updating of state*)
   149 fun bimatch_tac brules i = PRIMSEQ (Thm.biresolution true brules i);
   150 fun match_tac rules  = bimatch_tac (map (pair false) rules);
   151 fun ematch_tac rules = bimatch_tac (map (pair true) rules);
   152 fun dmatch_tac rls   = ematch_tac (map make_elim rls);
   153 
   154 (*Smash all flex-flex disagreement pairs in the proof state.*)
   155 val flexflex_tac = PRIMSEQ Thm.flexflex_rule;
   156 
   157 (*Remove duplicate subgoals.*)
   158 val permute_tac = PRIMITIVE oo Thm.permute_prems;
   159 fun distinct_tac (i, k) =
   160   permute_tac 0 (i - 1) THEN
   161   permute_tac 1 (k - 1) THEN
   162   PRIMITIVE (fn st => Drule.comp_no_flatten (st, 0) 1 Drule.distinct_prems_rl) THEN
   163   permute_tac 1 (1 - k) THEN
   164   permute_tac 0 (1 - i);
   165 
   166 fun distinct_subgoal_tac i st =
   167   (case drop (i - 1) (Thm.prems_of st) of
   168     [] => no_tac st
   169   | A :: Bs =>
   170       st |> EVERY (fold (fn (B, k) =>
   171         if A aconv B then cons (distinct_tac (i, k)) else I) (Bs ~~ (1 upto length Bs)) []));
   172 
   173 fun distinct_subgoals_tac state =
   174   let
   175     val goals = Thm.prems_of state;
   176     val dups = distinct (eq_fst (op aconv)) (goals ~~ (1 upto length goals));
   177   in EVERY (rev (map (distinct_subgoal_tac o snd) dups)) state end;
   178 
   179 
   180 (*** Applications of cut_rl ***)
   181 
   182 (*The conclusion of the rule gets assumed in subgoal i,
   183   while subgoal i+1,... are the premises of the rule.*)
   184 fun cut_tac rule i = rtac cut_rl i THEN rtac rule (i + 1);
   185 
   186 (*"Cut" a list of rules into the goal.  Their premises will become new
   187   subgoals.*)
   188 fun cut_rules_tac ths i = EVERY (map (fn th => cut_tac th i) ths);
   189 
   190 (*As above, but inserts only facts (unconditional theorems);
   191   generates no additional subgoals. *)
   192 fun cut_facts_tac ths = cut_rules_tac (filter Thm.no_prems ths);
   193 
   194 
   195 (**** Indexing and filtering of theorems ****)
   196 
   197 (*Returns the list of potentially resolvable theorems for the goal "prem",
   198         using the predicate  could(subgoal,concl).
   199   Resulting list is no longer than "limit"*)
   200 fun filter_thms could (limit, prem, ths) =
   201   let val pb = Logic.strip_assums_concl prem;   (*delete assumptions*)
   202       fun filtr (limit, []) = []
   203         | filtr (limit, th::ths) =
   204             if limit=0 then  []
   205             else if could(pb, concl_of th)  then th :: filtr(limit-1, ths)
   206             else filtr(limit,ths)
   207   in  filtr(limit,ths)  end;
   208 
   209 
   210 (*** biresolution and resolution using nets ***)
   211 
   212 (** To preserve the order of the rules, tag them with increasing integers **)
   213 
   214 (*insert one tagged brl into the pair of nets*)
   215 fun insert_tagged_brl (kbrl as (k, (eres, th))) (inet, enet) =
   216   if eres then
   217     (case try Thm.major_prem_of th of
   218       SOME prem => (inet, Net.insert_term (K false) (prem, kbrl) enet)
   219     | NONE => error "insert_tagged_brl: elimination rule with no premises")
   220   else (Net.insert_term (K false) (concl_of th, kbrl) inet, enet);
   221 
   222 (*build a pair of nets for biresolution*)
   223 fun build_netpair netpair brls =
   224     fold_rev insert_tagged_brl (tag_list 1 brls) netpair;
   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 order match (inet,enet) =
   242   SUBGOAL
   243     (fn (prem,i) =>
   244       let val hyps = Logic.strip_assums_hyp prem
   245           and concl = Logic.strip_assums_concl prem
   246           val kbrls = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps
   247       in PRIMSEQ (Thm.biresolution match (order kbrls) i) end);
   248 
   249 (*versions taking pre-built nets.  No filtering of brls*)
   250 val biresolve_from_nets_tac = biresolution_from_nets_tac order_list false;
   251 val bimatch_from_nets_tac   = biresolution_from_nets_tac order_list true;
   252 
   253 (*fast versions using nets internally*)
   254 val net_biresolve_tac =
   255     biresolve_from_nets_tac o build_netpair(Net.empty,Net.empty);
   256 
   257 val net_bimatch_tac =
   258     bimatch_from_nets_tac o build_netpair(Net.empty,Net.empty);
   259 
   260 (*** Simpler version for resolve_tac -- only one net, and no hyps ***)
   261 
   262 (*insert one tagged rl into the net*)
   263 fun insert_krl (krl as (k,th)) =
   264   Net.insert_term (K false) (concl_of th, krl);
   265 
   266 (*build a net of rules for resolution*)
   267 fun build_net rls =
   268   fold_rev insert_krl (tag_list 1 rls) Net.empty;
   269 
   270 (*resolution using a net rather than rules; pred supports filt_resolve_tac*)
   271 fun filt_resolution_from_net_tac match pred net =
   272   SUBGOAL
   273     (fn (prem,i) =>
   274       let val krls = Net.unify_term net (Logic.strip_assums_concl prem)
   275       in
   276          if pred krls
   277          then PRIMSEQ
   278                 (Thm.biresolution match (map (pair false) (order_list krls)) i)
   279          else no_tac
   280       end);
   281 
   282 (*Resolve the subgoal using the rules (making a net) unless too flexible,
   283    which means more than maxr rules are unifiable.      *)
   284 fun filt_resolve_tac rules maxr =
   285     let fun pred krls = length krls <= maxr
   286     in  filt_resolution_from_net_tac false pred (build_net rules)  end;
   287 
   288 (*versions taking pre-built nets*)
   289 val resolve_from_net_tac = filt_resolution_from_net_tac false (K true);
   290 val match_from_net_tac = filt_resolution_from_net_tac true (K true);
   291 
   292 (*fast versions using nets internally*)
   293 val net_resolve_tac = resolve_from_net_tac o build_net;
   294 val net_match_tac = match_from_net_tac o build_net;
   295 
   296 
   297 (*** For Natural Deduction using (bires_flg, rule) pairs ***)
   298 
   299 (*The number of new subgoals produced by the brule*)
   300 fun subgoals_of_brl (true,rule)  = nprems_of rule - 1
   301   | subgoals_of_brl (false,rule) = nprems_of rule;
   302 
   303 (*Less-than test: for sorting to minimize number of new subgoals*)
   304 fun lessb (brl1,brl2) = subgoals_of_brl brl1 < subgoals_of_brl brl2;
   305 
   306 
   307 (*Renaming of parameters in a subgoal*)
   308 fun rename_tac xs i =
   309   case Library.find_first (not o Symbol_Pos.is_identifier) xs of
   310       SOME x => error ("Not an identifier: " ^ x)
   311     | NONE => PRIMITIVE (Thm.rename_params_rule (xs, i));
   312 
   313 (*rotate_tac n i: rotate the assumptions of subgoal i by n positions, from
   314   right to left if n is positive, and from left to right if n is negative.*)
   315 fun rotate_tac 0 i = all_tac
   316   | rotate_tac k i = PRIMITIVE (Thm.rotate_rule k i);
   317 
   318 (*Rotates the given subgoal to be the last.*)
   319 fun defer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1);
   320 
   321 (*Rotates the given subgoal to be the first.*)
   322 fun prefer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1 #> Thm.permute_prems 0 ~1);
   323 
   324 (* remove premises that do not satisfy p; fails if all prems satisfy p *)
   325 fun filter_prems_tac p =
   326   let fun Then NONE tac = SOME tac
   327         | Then (SOME tac) tac' = SOME(tac THEN' tac');
   328       fun thins H (tac,n) =
   329         if p H then (tac,n+1)
   330         else (Then tac (rotate_tac n THEN' etac thin_rl),0);
   331   in SUBGOAL(fn (subg,n) =>
   332        let val Hs = Logic.strip_assums_hyp subg
   333        in case fst(fold thins Hs (NONE,0)) of
   334             NONE => no_tac | SOME tac => tac n
   335        end)
   336   end;
   337 
   338 end;
   339 
   340 structure Basic_Tactic: BASIC_TACTIC = Tactic;
   341 open Basic_Tactic;