src/Pure/tactic.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 51602 4c7fdc00bd59
child 52087 f3075fc4f5f6
permissions -rw-r--r--
tuned signature;
tuned comments;
     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 permute_tac = PRIMITIVE oo Thm.permute_prems;
   157 fun distinct_tac (i, k) =
   158   permute_tac 0 (i - 1) THEN
   159   permute_tac 1 (k - 1) THEN
   160   PRIMITIVE (fn st => Drule.comp_no_flatten (st, 0) 1 Drule.distinct_prems_rl) THEN
   161   permute_tac 1 (1 - k) THEN
   162   permute_tac 0 (1 - i);
   163 
   164 fun distinct_subgoal_tac i st =
   165   (case drop (i - 1) (Thm.prems_of st) of
   166     [] => no_tac st
   167   | A :: Bs =>
   168       st |> EVERY (fold (fn (B, k) =>
   169         if A aconv B then cons (distinct_tac (i, k)) else I) (Bs ~~ (1 upto length Bs)) []));
   170 
   171 fun distinct_subgoals_tac state =
   172   let
   173     val goals = Thm.prems_of state;
   174     val dups = distinct (eq_fst (op aconv)) (goals ~~ (1 upto length goals));
   175   in EVERY (rev (map (distinct_subgoal_tac o snd) dups)) state end;
   176 
   177 
   178 (*** Applications of cut_rl ***)
   179 
   180 (*The conclusion of the rule gets assumed in subgoal i,
   181   while subgoal i+1,... are the premises of the rule.*)
   182 fun cut_tac rule i = rtac cut_rl i THEN rtac rule (i + 1);
   183 
   184 (*"Cut" a list of rules into the goal.  Their premises will become new
   185   subgoals.*)
   186 fun cut_rules_tac ths i = EVERY (map (fn th => cut_tac th i) ths);
   187 
   188 (*As above, but inserts only facts (unconditional theorems);
   189   generates no additional subgoals. *)
   190 fun cut_facts_tac ths = cut_rules_tac (filter Thm.no_prems ths);
   191 
   192 
   193 (**** Indexing and filtering of theorems ****)
   194 
   195 (*Returns the list of potentially resolvable theorems for the goal "prem",
   196         using the predicate  could(subgoal,concl).
   197   Resulting list is no longer than "limit"*)
   198 fun filter_thms could (limit, prem, ths) =
   199   let val pb = Logic.strip_assums_concl prem;   (*delete assumptions*)
   200       fun filtr (limit, []) = []
   201         | filtr (limit, th::ths) =
   202             if limit=0 then  []
   203             else if could(pb, concl_of th)  then th :: filtr(limit-1, ths)
   204             else filtr(limit,ths)
   205   in  filtr(limit,ths)  end;
   206 
   207 
   208 (*** biresolution and resolution using nets ***)
   209 
   210 (** To preserve the order of the rules, tag them with increasing integers **)
   211 
   212 (*insert one tagged brl into the pair of nets*)
   213 fun insert_tagged_brl (kbrl as (k, (eres, th))) (inet, enet) =
   214   if eres then
   215     (case try Thm.major_prem_of th of
   216       SOME prem => (inet, Net.insert_term (K false) (prem, kbrl) enet)
   217     | NONE => error "insert_tagged_brl: elimination rule with no premises")
   218   else (Net.insert_term (K false) (concl_of th, kbrl) inet, enet);
   219 
   220 (*build a pair of nets for biresolution*)
   221 fun build_netpair netpair brls =
   222     fold_rev insert_tagged_brl (tag_list 1 brls) netpair;
   223 
   224 (*delete one kbrl from the pair of nets*)
   225 fun eq_kbrl ((_, (_, th)), (_, (_, th'))) = Thm.eq_thm_prop (th, th')
   226 
   227 fun delete_tagged_brl (brl as (eres, th)) (inet, enet) =
   228   (if eres then
   229     (case try Thm.major_prem_of th of
   230       SOME prem => (inet, Net.delete_term eq_kbrl (prem, ((), brl)) enet)
   231     | NONE => (inet, enet))  (*no major premise: ignore*)
   232   else (Net.delete_term eq_kbrl (Thm.concl_of th, ((), brl)) inet, enet))
   233   handle Net.DELETE => (inet,enet);
   234 
   235 
   236 (*biresolution using a pair of nets rather than rules.
   237     function "order" must sort and possibly filter the list of brls.
   238     boolean "match" indicates matching or unification.*)
   239 fun biresolution_from_nets_tac order match (inet,enet) =
   240   SUBGOAL
   241     (fn (prem,i) =>
   242       let val hyps = Logic.strip_assums_hyp prem
   243           and concl = Logic.strip_assums_concl prem
   244           val kbrls = Net.unify_term inet concl @ maps (Net.unify_term enet) hyps
   245       in PRIMSEQ (Thm.biresolution match (order kbrls) i) end);
   246 
   247 (*versions taking pre-built nets.  No filtering of brls*)
   248 val biresolve_from_nets_tac = biresolution_from_nets_tac order_list false;
   249 val bimatch_from_nets_tac   = biresolution_from_nets_tac order_list true;
   250 
   251 (*fast versions using nets internally*)
   252 val net_biresolve_tac =
   253     biresolve_from_nets_tac o build_netpair(Net.empty,Net.empty);
   254 
   255 val net_bimatch_tac =
   256     bimatch_from_nets_tac o build_netpair(Net.empty,Net.empty);
   257 
   258 (*** Simpler version for resolve_tac -- only one net, and no hyps ***)
   259 
   260 (*insert one tagged rl into the net*)
   261 fun insert_krl (krl as (k,th)) =
   262   Net.insert_term (K false) (concl_of th, krl);
   263 
   264 (*build a net of rules for resolution*)
   265 fun build_net rls =
   266   fold_rev insert_krl (tag_list 1 rls) Net.empty;
   267 
   268 (*resolution using a net rather than rules; pred supports filt_resolve_tac*)
   269 fun filt_resolution_from_net_tac match pred net =
   270   SUBGOAL
   271     (fn (prem,i) =>
   272       let val krls = Net.unify_term net (Logic.strip_assums_concl prem)
   273       in
   274          if pred krls
   275          then PRIMSEQ
   276                 (Thm.biresolution match (map (pair false) (order_list krls)) i)
   277          else no_tac
   278       end);
   279 
   280 (*Resolve the subgoal using the rules (making a net) unless too flexible,
   281    which means more than maxr rules are unifiable.      *)
   282 fun filt_resolve_tac rules maxr =
   283     let fun pred krls = length krls <= maxr
   284     in  filt_resolution_from_net_tac false pred (build_net rules)  end;
   285 
   286 (*versions taking pre-built nets*)
   287 val resolve_from_net_tac = filt_resolution_from_net_tac false (K true);
   288 val match_from_net_tac = filt_resolution_from_net_tac true (K true);
   289 
   290 (*fast versions using nets internally*)
   291 val net_resolve_tac = resolve_from_net_tac o build_net;
   292 val net_match_tac = match_from_net_tac o build_net;
   293 
   294 
   295 (*** For Natural Deduction using (bires_flg, rule) pairs ***)
   296 
   297 (*The number of new subgoals produced by the brule*)
   298 fun subgoals_of_brl (true,rule)  = nprems_of rule - 1
   299   | subgoals_of_brl (false,rule) = nprems_of rule;
   300 
   301 (*Less-than test: for sorting to minimize number of new subgoals*)
   302 fun lessb (brl1,brl2) = subgoals_of_brl brl1 < subgoals_of_brl brl2;
   303 
   304 
   305 (*Renaming of parameters in a subgoal*)
   306 fun rename_tac xs i =
   307   case Library.find_first (not o Symbol_Pos.is_identifier) xs of
   308       SOME x => error ("Not an identifier: " ^ x)
   309     | NONE => PRIMITIVE (Thm.rename_params_rule (xs, i));
   310 
   311 (*rotate_tac n i: rotate the assumptions of subgoal i by n positions, from
   312   right to left if n is positive, and from left to right if n is negative.*)
   313 fun rotate_tac 0 i = all_tac
   314   | rotate_tac k i = PRIMITIVE (Thm.rotate_rule k i);
   315 
   316 (*Rotates the given subgoal to be the last.*)
   317 fun defer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1);
   318 
   319 (*Rotates the given subgoal to be the first.*)
   320 fun prefer_tac i = PRIMITIVE (Thm.permute_prems (i - 1) 1 #> Thm.permute_prems 0 ~1);
   321 
   322 (* remove premises that do not satisfy p; fails if all prems satisfy p *)
   323 fun filter_prems_tac p =
   324   let fun Then NONE tac = SOME tac
   325         | Then (SOME tac) tac' = SOME(tac THEN' tac');
   326       fun thins H (tac,n) =
   327         if p H then (tac,n+1)
   328         else (Then tac (rotate_tac n THEN' etac thin_rl),0);
   329   in SUBGOAL(fn (subg,n) =>
   330        let val Hs = Logic.strip_assums_hyp subg
   331        in case fst(fold thins Hs (NONE,0)) of
   332             NONE => no_tac | SOME tac => tac n
   333        end)
   334   end;
   335 
   336 end;
   337 
   338 structure Basic_Tactic: BASIC_TACTIC = Tactic;
   339 open Basic_Tactic;