doc-src/Ref/tactic.tex
author wenzelm
Thu, 27 Nov 1997 19:39:02 +0100
changeset 4317 7264fa2ff2ec
parent 4276 a770eae2cdb0
child 4597 a0bdee64194c
permissions -rw-r--r--
several minor updates;
     1 %% $Id$
     2 \chapter{Tactics} \label{tactics}
     3 \index{tactics|(} Tactics have type \mltydx{tactic}.  This is just an
     4 abbreviation for functions from theorems to theorem sequences, where
     5 the theorems represent states of a backward proof.  Tactics seldom
     6 need to be coded from scratch, as functions; instead they are
     7 expressed using basic tactics and tacticals.
     8 
     9 This chapter only presents the primitive tactics.  Substantial proofs
    10 require the power of automatic tools like simplification
    11 (Chapter~\ref{chap:simplification}) and classical tableau reasoning
    12 (Chapter~\ref{chap:classical}).
    13 
    14 \section{Resolution and assumption tactics}
    15 {\bf Resolution} is Isabelle's basic mechanism for refining a subgoal using
    16 a rule.  {\bf Elim-resolution} is particularly suited for elimination
    17 rules, while {\bf destruct-resolution} is particularly suited for
    18 destruction rules.  The {\tt r}, {\tt e}, {\tt d} naming convention is
    19 maintained for several different kinds of resolution tactics, as well as
    20 the shortcuts in the subgoal module.
    21 
    22 All the tactics in this section act on a subgoal designated by a positive
    23 integer~$i$.  They fail (by returning the empty sequence) if~$i$ is out of
    24 range.
    25 
    26 \subsection{Resolution tactics}
    27 \index{resolution!tactics}
    28 \index{tactics!resolution|bold}
    29 \begin{ttbox} 
    30 resolve_tac  : thm list -> int -> tactic
    31 eresolve_tac : thm list -> int -> tactic
    32 dresolve_tac : thm list -> int -> tactic
    33 forward_tac  : thm list -> int -> tactic 
    34 \end{ttbox}
    35 These perform resolution on a list of theorems, $thms$, representing a list
    36 of object-rules.  When generating next states, they take each of the rules
    37 in the order given.  Each rule may yield several next states, or none:
    38 higher-order resolution may yield multiple resolvents.
    39 \begin{ttdescription}
    40 \item[\ttindexbold{resolve_tac} {\it thms} {\it i}] 
    41   refines the proof state using the rules, which should normally be
    42   introduction rules.  It resolves a rule's conclusion with
    43   subgoal~$i$ of the proof state.
    44 
    45 \item[\ttindexbold{eresolve_tac} {\it thms} {\it i}] 
    46   \index{elim-resolution}
    47   performs elim-resolution with the rules, which should normally be
    48   elimination rules.  It resolves with a rule, solves its first premise by
    49   assumption, and finally {\em deletes\/} that assumption from any new
    50   subgoals.
    51 
    52 \item[\ttindexbold{dresolve_tac} {\it thms} {\it i}] 
    53   \index{forward proof}\index{destruct-resolution}
    54   performs destruct-resolution with the rules, which normally should
    55   be destruction rules.  This replaces an assumption by the result of
    56   applying one of the rules.
    57 
    58 \item[\ttindexbold{forward_tac}]\index{forward proof}
    59   is like {\tt dresolve_tac} except that the selected assumption is not
    60   deleted.  It applies a rule to an assumption, adding the result as a new
    61   assumption.
    62 \end{ttdescription}
    63 
    64 \subsection{Assumption tactics}
    65 \index{tactics!assumption|bold}\index{assumptions!tactics for}
    66 \begin{ttbox} 
    67 assume_tac    : int -> tactic
    68 eq_assume_tac : int -> tactic
    69 \end{ttbox} 
    70 \begin{ttdescription}
    71 \item[\ttindexbold{assume_tac} {\it i}] 
    72 attempts to solve subgoal~$i$ by assumption.
    73 
    74 \item[\ttindexbold{eq_assume_tac}] 
    75 is like {\tt assume_tac} but does not use unification.  It succeeds (with a
    76 {\em unique\/} next state) if one of the assumptions is identical to the
    77 subgoal's conclusion.  Since it does not instantiate variables, it cannot
    78 make other subgoals unprovable.  It is intended to be called from proof
    79 strategies, not interactively.
    80 \end{ttdescription}
    81 
    82 \subsection{Matching tactics} \label{match_tac}
    83 \index{tactics!matching}
    84 \begin{ttbox} 
    85 match_tac  : thm list -> int -> tactic
    86 ematch_tac : thm list -> int -> tactic
    87 dmatch_tac : thm list -> int -> tactic
    88 \end{ttbox}
    89 These are just like the resolution tactics except that they never
    90 instantiate unknowns in the proof state.  Flexible subgoals are not updated
    91 willy-nilly, but are left alone.  Matching --- strictly speaking --- means
    92 treating the unknowns in the proof state as constants; these tactics merely
    93 discard unifiers that would update the proof state.
    94 \begin{ttdescription}
    95 \item[\ttindexbold{match_tac} {\it thms} {\it i}] 
    96 refines the proof state using the rules, matching a rule's
    97 conclusion with subgoal~$i$ of the proof state.
    98 
    99 \item[\ttindexbold{ematch_tac}] 
   100 is like {\tt match_tac}, but performs elim-resolution.
   101 
   102 \item[\ttindexbold{dmatch_tac}] 
   103 is like {\tt match_tac}, but performs destruct-resolution.
   104 \end{ttdescription}
   105 
   106 
   107 \subsection{Resolution with instantiation} \label{res_inst_tac}
   108 \index{tactics!instantiation}\index{instantiation}
   109 \begin{ttbox} 
   110 res_inst_tac  : (string*string)list -> thm -> int -> tactic
   111 eres_inst_tac : (string*string)list -> thm -> int -> tactic
   112 dres_inst_tac : (string*string)list -> thm -> int -> tactic
   113 forw_inst_tac : (string*string)list -> thm -> int -> tactic
   114 \end{ttbox}
   115 These tactics are designed for applying rules such as substitution and
   116 induction, which cause difficulties for higher-order unification.  The
   117 tactics accept explicit instantiations for unknowns in the rule ---
   118 typically, in the rule's conclusion.  Each instantiation is a pair
   119 {\tt($v$,$e$)}, where $v$ is an unknown {\em without\/} its leading
   120 question mark!
   121 \begin{itemize}
   122 \item If $v$ is the type unknown {\tt'a}, then
   123 the rule must contain a type unknown \verb$?'a$ of some
   124 sort~$s$, and $e$ should be a type of sort $s$.
   125 
   126 \item If $v$ is the unknown {\tt P}, then
   127 the rule must contain an unknown \verb$?P$ of some type~$\tau$,
   128 and $e$ should be a term of some type~$\sigma$ such that $\tau$ and
   129 $\sigma$ are unifiable.  If the unification of $\tau$ and $\sigma$
   130 instantiates any type unknowns in $\tau$, these instantiations
   131 are recorded for application to the rule.
   132 \end{itemize}
   133 Types are instantiated before terms.  Because type instantiations are
   134 inferred from term instantiations, explicit type instantiations are seldom
   135 necessary --- if \verb$?t$ has type \verb$?'a$, then the instantiation list
   136 \verb$[("'a","bool"),("t","True")]$ may be simplified to
   137 \verb$[("t","True")]$.  Type unknowns in the proof state may cause
   138 failure because the tactics cannot instantiate them.
   139 
   140 The instantiation tactics act on a given subgoal.  Terms in the
   141 instantiations are type-checked in the context of that subgoal --- in
   142 particular, they may refer to that subgoal's parameters.  Any unknowns in
   143 the terms receive subscripts and are lifted over the parameters; thus, you
   144 may not refer to unknowns in the subgoal.
   145 
   146 \begin{ttdescription}
   147 \item[\ttindexbold{res_inst_tac} {\it insts} {\it thm} {\it i}]
   148 instantiates the rule {\it thm} with the instantiations {\it insts}, as
   149 described above, and then performs resolution on subgoal~$i$.  Resolution
   150 typically causes further instantiations; you need not give explicit
   151 instantiations for every unknown in the rule.
   152 
   153 \item[\ttindexbold{eres_inst_tac}] 
   154 is like {\tt res_inst_tac}, but performs elim-resolution.
   155 
   156 \item[\ttindexbold{dres_inst_tac}] 
   157 is like {\tt res_inst_tac}, but performs destruct-resolution.
   158 
   159 \item[\ttindexbold{forw_inst_tac}] 
   160 is like {\tt dres_inst_tac} except that the selected assumption is not
   161 deleted.  It applies the instantiated rule to an assumption, adding the
   162 result as a new assumption.
   163 \end{ttdescription}
   164 
   165 
   166 \section{Other basic tactics}
   167 \subsection{Tactic shortcuts}
   168 \index{shortcuts!for tactics}
   169 \index{tactics!resolution}\index{tactics!assumption}
   170 \index{tactics!meta-rewriting}
   171 \begin{ttbox} 
   172 rtac     :      thm -> int -> tactic
   173 etac     :      thm -> int -> tactic
   174 dtac     :      thm -> int -> tactic
   175 atac     :             int -> tactic
   176 ares_tac : thm list -> int -> tactic
   177 rewtac   :      thm ->        tactic
   178 \end{ttbox}
   179 These abbreviate common uses of tactics.
   180 \begin{ttdescription}
   181 \item[\ttindexbold{rtac} {\it thm} {\it i}] 
   182 abbreviates \hbox{\tt resolve_tac [{\it thm}] {\it i}}, doing resolution.
   183 
   184 \item[\ttindexbold{etac} {\it thm} {\it i}] 
   185 abbreviates \hbox{\tt eresolve_tac [{\it thm}] {\it i}}, doing elim-resolution.
   186 
   187 \item[\ttindexbold{dtac} {\it thm} {\it i}] 
   188 abbreviates \hbox{\tt dresolve_tac [{\it thm}] {\it i}}, doing
   189 destruct-resolution.
   190 
   191 \item[\ttindexbold{atac} {\it i}] 
   192 abbreviates \hbox{\tt assume_tac {\it i}}, doing proof by assumption.
   193 
   194 \item[\ttindexbold{ares_tac} {\it thms} {\it i}] 
   195 tries proof by assumption and resolution; it abbreviates
   196 \begin{ttbox}
   197 assume_tac {\it i} ORELSE resolve_tac {\it thms} {\it i}
   198 \end{ttbox}
   199 
   200 \item[\ttindexbold{rewtac} {\it def}] 
   201 abbreviates \hbox{\tt rewrite_goals_tac [{\it def}]}, unfolding a definition.
   202 \end{ttdescription}
   203 
   204 
   205 \subsection{Inserting premises and facts}\label{cut_facts_tac}
   206 \index{tactics!for inserting facts}\index{assumptions!inserting}
   207 \begin{ttbox} 
   208 cut_facts_tac : thm list -> int -> tactic
   209 cut_inst_tac  : (string*string)list -> thm -> int -> tactic
   210 subgoal_tac   : string -> int -> tactic
   211 subgoal_tacs  : string list -> int -> tactic
   212 \end{ttbox}
   213 These tactics add assumptions to a subgoal.
   214 \begin{ttdescription}
   215 \item[\ttindexbold{cut_facts_tac} {\it thms} {\it i}] 
   216   adds the {\it thms} as new assumptions to subgoal~$i$.  Once they have
   217   been inserted as assumptions, they become subject to tactics such as {\tt
   218     eresolve_tac} and {\tt rewrite_goals_tac}.  Only rules with no premises
   219   are inserted: Isabelle cannot use assumptions that contain $\Imp$
   220   or~$\Forall$.  Sometimes the theorems are premises of a rule being
   221   derived, returned by~{\tt goal}; instead of calling this tactic, you
   222   could state the goal with an outermost meta-quantifier.
   223 
   224 \item[\ttindexbold{cut_inst_tac} {\it insts} {\it thm} {\it i}]
   225   instantiates the {\it thm} with the instantiations {\it insts}, as
   226   described in \S\ref{res_inst_tac}.  It adds the resulting theorem as a
   227   new assumption to subgoal~$i$. 
   228 
   229 \item[\ttindexbold{subgoal_tac} {\it formula} {\it i}] 
   230 adds the {\it formula} as a assumption to subgoal~$i$, and inserts the same
   231 {\it formula} as a new subgoal, $i+1$.
   232 
   233 \item[\ttindexbold{subgoals_tac} {\it formulae} {\it i}] 
   234   uses {\tt subgoal_tac} to add the members of the list of {\it
   235     formulae} as assumptions to subgoal~$i$. 
   236 \end{ttdescription}
   237 
   238 
   239 \subsection{``Putting off'' a subgoal}
   240 \begin{ttbox} 
   241 defer_tac : int -> tactic
   242 \end{ttbox}
   243 \begin{ttdescription}
   244 \item[\ttindexbold{defer_tac} {\it i}] 
   245   moves subgoal~$i$ to the last position in the proof state.  It can be
   246   useful when correcting a proof script: if the tactic given for subgoal~$i$
   247   fails, calling {\tt defer_tac} instead will let you continue with the rest
   248   of the script.
   249 
   250   The tactic fails if subgoal~$i$ does not exist or if the proof state
   251   contains type unknowns. 
   252 \end{ttdescription}
   253 
   254 
   255 \subsection{Definitions and meta-level rewriting} \label{sec:rewrite_goals}
   256 \index{tactics!meta-rewriting|bold}\index{meta-rewriting|bold}
   257 \index{definitions}
   258 
   259 Definitions in Isabelle have the form $t\equiv u$, where $t$ is typically a
   260 constant or a constant applied to a list of variables, for example $\it
   261 sqr(n)\equiv n\times n$.  Conditional definitions, $\phi\Imp t\equiv u$,
   262 are also supported.  {\bf Unfolding} the definition ${t\equiv u}$ means using
   263 it as a rewrite rule, replacing~$t$ by~$u$ throughout a theorem.  {\bf
   264 Folding} $t\equiv u$ means replacing~$u$ by~$t$.  Rewriting continues until
   265 no rewrites are applicable to any subterm.
   266 
   267 There are rules for unfolding and folding definitions; Isabelle does not do
   268 this automatically.  The corresponding tactics rewrite the proof state,
   269 yielding a single next state.  See also the {\tt goalw} command, which is the
   270 easiest way of handling definitions.
   271 \begin{ttbox} 
   272 rewrite_goals_tac : thm list -> tactic
   273 rewrite_tac       : thm list -> tactic
   274 fold_goals_tac    : thm list -> tactic
   275 fold_tac          : thm list -> tactic
   276 \end{ttbox}
   277 \begin{ttdescription}
   278 \item[\ttindexbold{rewrite_goals_tac} {\it defs}]  
   279 unfolds the {\it defs} throughout the subgoals of the proof state, while
   280 leaving the main goal unchanged.  Use \ttindex{SELECT_GOAL} to restrict it to a
   281 particular subgoal.
   282 
   283 \item[\ttindexbold{rewrite_tac} {\it defs}]  
   284 unfolds the {\it defs} throughout the proof state, including the main goal
   285 --- not normally desirable!
   286 
   287 \item[\ttindexbold{fold_goals_tac} {\it defs}]  
   288 folds the {\it defs} throughout the subgoals of the proof state, while
   289 leaving the main goal unchanged.
   290 
   291 \item[\ttindexbold{fold_tac} {\it defs}]  
   292 folds the {\it defs} throughout the proof state.
   293 \end{ttdescription}
   294 
   295 \begin{warn}
   296   These tactics only cope with definitions expressed as meta-level
   297   equalities ($\equiv$).  More general equivalences are handled by the
   298   simplifier, provided that it is set up appropriately for your logic
   299   (see Chapter~\ref{chap:simplification}).
   300 \end{warn}
   301 
   302 \subsection{Theorems useful with tactics}
   303 \index{theorems!of pure theory}
   304 \begin{ttbox} 
   305 asm_rl: thm 
   306 cut_rl: thm 
   307 \end{ttbox}
   308 \begin{ttdescription}
   309 \item[\tdx{asm_rl}] 
   310 is $\psi\Imp\psi$.  Under elim-resolution it does proof by assumption, and
   311 \hbox{\tt eresolve_tac (asm_rl::{\it thms}) {\it i}} is equivalent to
   312 \begin{ttbox} 
   313 assume_tac {\it i}  ORELSE  eresolve_tac {\it thms} {\it i}
   314 \end{ttbox}
   315 
   316 \item[\tdx{cut_rl}] 
   317 is $\List{\psi\Imp\theta,\psi}\Imp\theta$.  It is useful for inserting
   318 assumptions; it underlies {\tt forward_tac}, {\tt cut_facts_tac}
   319 and {\tt subgoal_tac}.
   320 \end{ttdescription}
   321 
   322 
   323 \section{Obscure tactics}
   324 
   325 \subsection{Renaming parameters in a goal} \index{parameters!renaming}
   326 \begin{ttbox} 
   327 rename_tac        : string -> int -> tactic
   328 rename_last_tac   : string -> string list -> int -> tactic
   329 Logic.set_rename_prefix : string -> unit
   330 Logic.auto_rename       : bool ref      \hfill{\bf initially false}
   331 \end{ttbox}
   332 When creating a parameter, Isabelle chooses its name by matching variable
   333 names via the object-rule.  Given the rule $(\forall I)$ formalized as
   334 $\left(\Forall x. P(x)\right) \Imp \forall x.P(x)$, Isabelle will note that
   335 the $\Forall$-bound variable in the premise has the same name as the
   336 $\forall$-bound variable in the conclusion.  
   337 
   338 Sometimes there is insufficient information and Isabelle chooses an
   339 arbitrary name.  The renaming tactics let you override Isabelle's choice.
   340 Because renaming parameters has no logical effect on the proof state, the
   341 {\tt by} command prints the message {\tt Warning:\ same as previous
   342 level}.
   343 
   344 Alternatively, you can suppress the naming mechanism described above and
   345 have Isabelle generate uniform names for parameters.  These names have the
   346 form $p${\tt a}, $p${\tt b}, $p${\tt c},~\ldots, where $p$ is any desired
   347 prefix.  They are ugly but predictable.
   348 
   349 \begin{ttdescription}
   350 \item[\ttindexbold{rename_tac} {\it str} {\it i}] 
   351 interprets the string {\it str} as a series of blank-separated variable
   352 names, and uses them to rename the parameters of subgoal~$i$.  The names
   353 must be distinct.  If there are fewer names than parameters, then the
   354 tactic renames the innermost parameters and may modify the remaining ones
   355 to ensure that all the parameters are distinct.
   356 
   357 \item[\ttindexbold{rename_last_tac} {\it prefix} {\it suffixes} {\it i}] 
   358 generates a list of names by attaching each of the {\it suffixes\/} to the 
   359 {\it prefix}.  It is intended for coding structural induction tactics,
   360 where several of the new parameters should have related names.
   361 
   362 \item[\ttindexbold{Logic.set_rename_prefix} {\it prefix};] 
   363 sets the prefix for uniform renaming to~{\it prefix}.  The default prefix
   364 is {\tt"k"}.
   365 
   366 \item[set \ttindexbold{Logic.auto_rename};] 
   367 makes Isabelle generate uniform names for parameters. 
   368 \end{ttdescription}
   369 
   370 
   371 \subsection{Manipulating assumptions}
   372 \index{assumptions!rotating}
   373 \begin{ttbox} 
   374 thin_tac   : string -> int -> tactic
   375 rotate_tac : int -> int -> tactic
   376 \end{ttbox}
   377 \begin{ttdescription}
   378 \item[\ttindexbold{thin_tac} {\it formula} $i$]  
   379 \index{assumptions!deleting}
   380 deletes the specified assumption from subgoal $i$.  Often the assumption
   381 can be abbreviated, replacing subformul{\ae} by unknowns; the first matching
   382 assumption will be deleted.  Removing useless assumptions from a subgoal
   383 increases its readability and can make search tactics run faster.
   384 
   385 \item[\ttindexbold{rotate_tac} $n$ $i$]  
   386 \index{assumptions!rotating}
   387 rotates the assumptions of subgoal $i$ by $n$ positions: from right to left
   388 if $n$ is positive, and from left to right if $n$ is negative.  This is 
   389 sometimes necessary in connection with \ttindex{asm_full_simp_tac}, which 
   390 processes assumptions from left to right.
   391 \end{ttdescription}
   392 
   393 
   394 \subsection{Tidying the proof state}
   395 \index{duplicate subgoals!removing}
   396 \index{parameters!removing unused}
   397 \index{flex-flex constraints}
   398 \begin{ttbox} 
   399 distinct_subgoals_tac : tactic
   400 prune_params_tac      : tactic
   401 flexflex_tac          : tactic
   402 \end{ttbox}
   403 \begin{ttdescription}
   404 \item[\ttindexbold{distinct_subgoals_tac}]  
   405   removes duplicate subgoals from a proof state.  (These arise especially
   406   in \ZF{}, where the subgoals are essentially type constraints.)
   407 
   408 \item[\ttindexbold{prune_params_tac}]  
   409   removes unused parameters from all subgoals of the proof state.  It works
   410   by rewriting with the theorem $(\Forall x. V)\equiv V$.  This tactic can
   411   make the proof state more readable.  It is used with
   412   \ttindex{rule_by_tactic} to simplify the resulting theorem.
   413 
   414 \item[\ttindexbold{flexflex_tac}]  
   415   removes all flex-flex pairs from the proof state by applying the trivial
   416   unifier.  This drastic step loses information, and should only be done as
   417   the last step of a proof.
   418 
   419   Flex-flex constraints arise from difficult cases of higher-order
   420   unification.  To prevent this, use \ttindex{res_inst_tac} to instantiate
   421   some variables in a rule~(\S\ref{res_inst_tac}).  Normally flex-flex
   422   constraints can be ignored; they often disappear as unknowns get
   423   instantiated.
   424 \end{ttdescription}
   425 
   426 
   427 \subsection{Composition: resolution without lifting}
   428 \index{tactics!for composition}
   429 \begin{ttbox}
   430 compose_tac: (bool * thm * int) -> int -> tactic
   431 \end{ttbox}
   432 {\bf Composing} two rules means resolving them without prior lifting or
   433 renaming of unknowns.  This low-level operation, which underlies the
   434 resolution tactics, may occasionally be useful for special effects.
   435 A typical application is \ttindex{res_inst_tac}, which lifts and instantiates a
   436 rule, then passes the result to {\tt compose_tac}.
   437 \begin{ttdescription}
   438 \item[\ttindexbold{compose_tac} ($flag$, $rule$, $m$) $i$] 
   439 refines subgoal~$i$ using $rule$, without lifting.  The $rule$ is taken to
   440 have the form $\List{\psi@1; \ldots; \psi@m} \Imp \psi$, where $\psi$ need
   441 not be atomic; thus $m$ determines the number of new subgoals.  If
   442 $flag$ is {\tt true} then it performs elim-resolution --- it solves the
   443 first premise of~$rule$ by assumption and deletes that assumption.
   444 \end{ttdescription}
   445 
   446 
   447 \section{*Managing lots of rules}
   448 These operations are not intended for interactive use.  They are concerned
   449 with the processing of large numbers of rules in automatic proof
   450 strategies.  Higher-order resolution involving a long list of rules is
   451 slow.  Filtering techniques can shorten the list of rules given to
   452 resolution, and can also detect whether a subgoal is too flexible,
   453 with too many rules applicable.
   454 
   455 \subsection{Combined resolution and elim-resolution} \label{biresolve_tac}
   456 \index{tactics!resolution}
   457 \begin{ttbox} 
   458 biresolve_tac   : (bool*thm)list -> int -> tactic
   459 bimatch_tac     : (bool*thm)list -> int -> tactic
   460 subgoals_of_brl : bool*thm -> int
   461 lessb           : (bool*thm) * (bool*thm) -> bool
   462 \end{ttbox}
   463 {\bf Bi-resolution} takes a list of $\it (flag,rule)$ pairs.  For each
   464 pair, it applies resolution if the flag is~{\tt false} and
   465 elim-resolution if the flag is~{\tt true}.  A single tactic call handles a
   466 mixture of introduction and elimination rules.
   467 
   468 \begin{ttdescription}
   469 \item[\ttindexbold{biresolve_tac} {\it brls} {\it i}] 
   470 refines the proof state by resolution or elim-resolution on each rule, as
   471 indicated by its flag.  It affects subgoal~$i$ of the proof state.
   472 
   473 \item[\ttindexbold{bimatch_tac}] 
   474 is like {\tt biresolve_tac}, but performs matching: unknowns in the
   475 proof state are never updated (see~\S\ref{match_tac}).
   476 
   477 \item[\ttindexbold{subgoals_of_brl}({\it flag},{\it rule})] 
   478 returns the number of new subgoals that bi-resolution would yield for the
   479 pair (if applied to a suitable subgoal).  This is $n$ if the flag is
   480 {\tt false} and $n-1$ if the flag is {\tt true}, where $n$ is the number
   481 of premises of the rule.  Elim-resolution yields one fewer subgoal than
   482 ordinary resolution because it solves the major premise by assumption.
   483 
   484 \item[\ttindexbold{lessb} ({\it brl1},{\it brl2})] 
   485 returns the result of 
   486 \begin{ttbox}
   487 subgoals_of_brl{\it brl1} < subgoals_of_brl{\it brl2}
   488 \end{ttbox}
   489 \end{ttdescription}
   490 Note that \hbox{\tt sort lessb {\it brls}} sorts a list of $\it
   491 (flag,rule)$ pairs by the number of new subgoals they will yield.  Thus,
   492 those that yield the fewest subgoals should be tried first.
   493 
   494 
   495 \subsection{Discrimination nets for fast resolution}\label{filt_resolve_tac}
   496 \index{discrimination nets|bold}
   497 \index{tactics!resolution}
   498 \begin{ttbox} 
   499 net_resolve_tac  : thm list -> int -> tactic
   500 net_match_tac    : thm list -> int -> tactic
   501 net_biresolve_tac: (bool*thm) list -> int -> tactic
   502 net_bimatch_tac  : (bool*thm) list -> int -> tactic
   503 filt_resolve_tac : thm list -> int -> int -> tactic
   504 could_unify      : term*term->bool
   505 filter_thms      : (term*term->bool) -> int*term*thm list -> thm list
   506 \end{ttbox}
   507 The module {\tt Net} implements a discrimination net data structure for
   508 fast selection of rules \cite[Chapter 14]{charniak80}.  A term is
   509 classified by the symbol list obtained by flattening it in preorder.
   510 The flattening takes account of function applications, constants, and free
   511 and bound variables; it identifies all unknowns and also regards
   512 \index{lambda abs@$\lambda$-abstractions}
   513 $\lambda$-abstractions as unknowns, since they could $\eta$-contract to
   514 anything.  
   515 
   516 A discrimination net serves as a polymorphic dictionary indexed by terms.
   517 The module provides various functions for inserting and removing items from
   518 nets.  It provides functions for returning all items whose term could match
   519 or unify with a target term.  The matching and unification tests are
   520 overly lax (due to the identifications mentioned above) but they serve as
   521 useful filters.
   522 
   523 A net can store introduction rules indexed by their conclusion, and
   524 elimination rules indexed by their major premise.  Isabelle provides
   525 several functions for `compiling' long lists of rules into fast
   526 resolution tactics.  When supplied with a list of theorems, these functions
   527 build a discrimination net; the net is used when the tactic is applied to a
   528 goal.  To avoid repeatedly constructing the nets, use currying: bind the
   529 resulting tactics to \ML{} identifiers.
   530 
   531 \begin{ttdescription}
   532 \item[\ttindexbold{net_resolve_tac} {\it thms}] 
   533 builds a discrimination net to obtain the effect of a similar call to {\tt
   534 resolve_tac}.
   535 
   536 \item[\ttindexbold{net_match_tac} {\it thms}] 
   537 builds a discrimination net to obtain the effect of a similar call to {\tt
   538 match_tac}.
   539 
   540 \item[\ttindexbold{net_biresolve_tac} {\it brls}] 
   541 builds a discrimination net to obtain the effect of a similar call to {\tt
   542 biresolve_tac}.
   543 
   544 \item[\ttindexbold{net_bimatch_tac} {\it brls}] 
   545 builds a discrimination net to obtain the effect of a similar call to {\tt
   546 bimatch_tac}.
   547 
   548 \item[\ttindexbold{filt_resolve_tac} {\it thms} {\it maxr} {\it i}] 
   549 uses discrimination nets to extract the {\it thms} that are applicable to
   550 subgoal~$i$.  If more than {\it maxr\/} theorems are applicable then the
   551 tactic fails.  Otherwise it calls {\tt resolve_tac}.  
   552 
   553 This tactic helps avoid runaway instantiation of unknowns, for example in
   554 type inference.
   555 
   556 \item[\ttindexbold{could_unify} ({\it t},{\it u})] 
   557 returns {\tt false} if~$t$ and~$u$ are `obviously' non-unifiable, and
   558 otherwise returns~{\tt true}.  It assumes all variables are distinct,
   559 reporting that {\tt ?a=?a} may unify with {\tt 0=1}.
   560 
   561 \item[\ttindexbold{filter_thms} $could\; (limit,prem,thms)$] 
   562 returns the list of potentially resolvable rules (in {\it thms\/}) for the
   563 subgoal {\it prem}, using the predicate {\it could\/} to compare the
   564 conclusion of the subgoal with the conclusion of each rule.  The resulting list
   565 is no longer than {\it limit}.
   566 \end{ttdescription}
   567 
   568 
   569 \section{Programming tools for proof strategies}
   570 Do not consider using the primitives discussed in this section unless you
   571 really need to code tactics from scratch.
   572 
   573 \subsection{Operations on type {\tt tactic}}
   574 \index{tactics!primitives for coding} A tactic maps theorems to sequences of
   575 theorems.  The type constructor for sequences (lazy lists) is called
   576 \mltydx{Seq.seq}.  To simplify the types of tactics and tacticals,
   577 Isabelle defines a type abbreviation:
   578 \begin{ttbox} 
   579 type tactic = thm -> thm Seq.seq
   580 \end{ttbox} 
   581 The following operations provide means for coding tactics in a clean style.
   582 \begin{ttbox} 
   583 PRIMITIVE :                  (thm -> thm) -> tactic  
   584 SUBGOAL   : ((term*int) -> tactic) -> int -> tactic
   585 \end{ttbox} 
   586 \begin{ttdescription}
   587 \item[\ttindexbold{PRIMITIVE} $f$] packages the meta-rule~$f$ as a tactic that
   588   applies $f$ to the proof state and returns the result as a one-element
   589   sequence.  If $f$ raises an exception, then the tactic's result is the empty
   590   sequence.
   591 
   592 \item[\ttindexbold{SUBGOAL} $f$ $i$] 
   593 extracts subgoal~$i$ from the proof state as a term~$t$, and computes a
   594 tactic by calling~$f(t,i)$.  It applies the resulting tactic to the same
   595 state.  The tactic body is expressed using tactics and tacticals, but may
   596 peek at a particular subgoal:
   597 \begin{ttbox} 
   598 SUBGOAL (fn (t,i) => {\it tactic-valued expression})
   599 \end{ttbox} 
   600 \end{ttdescription}
   601 
   602 
   603 \subsection{Tracing}
   604 \index{tactics!tracing}
   605 \index{tracing!of tactics}
   606 \begin{ttbox} 
   607 pause_tac: tactic
   608 print_tac: tactic
   609 \end{ttbox}
   610 These tactics print tracing information when they are applied to a proof
   611 state.  Their output may be difficult to interpret.  Note that certain of
   612 the searching tacticals, such as {\tt REPEAT}, have built-in tracing
   613 options.
   614 \begin{ttdescription}
   615 \item[\ttindexbold{pause_tac}] 
   616 prints {\footnotesize\tt** Press RETURN to continue:} and then reads a line
   617 from the terminal.  If this line is blank then it returns the proof state
   618 unchanged; otherwise it fails (which may terminate a repetition).
   619 
   620 \item[\ttindexbold{print_tac}] 
   621 returns the proof state unchanged, with the side effect of printing it at
   622 the terminal.
   623 \end{ttdescription}
   624 
   625 
   626 \section{*Sequences}
   627 \index{sequences (lazy lists)|bold}
   628 The module {\tt Seq} declares a type of lazy lists.  It uses
   629 Isabelle's type \mltydx{option} to represent the possible presence
   630 (\ttindexbold{Some}) or absence (\ttindexbold{None}) of
   631 a value:
   632 \begin{ttbox}
   633 datatype 'a option = None  |  Some of 'a;
   634 \end{ttbox}
   635 The {\tt Seq} structure is supposed to be accessed via fully qualified
   636 names and should not be \texttt{open}.
   637 
   638 \subsection{Basic operations on sequences}
   639 \begin{ttbox} 
   640 Seq.empty   : 'a seq
   641 Seq.make    : (unit -> ('a * 'a seq) option) -> 'a seq
   642 Seq.single  : 'a -> 'a seq
   643 Seq.pull    : 'a seq -> ('a * 'a seq) option
   644 \end{ttbox}
   645 \begin{ttdescription}
   646 \item[Seq.empty] is the empty sequence.
   647 
   648 \item[\tt Seq.make (fn () => Some ($x$, $xq$))] constructs the
   649   sequence with head~$x$ and tail~$xq$, neither of which is evaluated.
   650 
   651 \item[Seq.single $x$] 
   652 constructs the sequence containing the single element~$x$.
   653 
   654 \item[Seq.pull $xq$] returns {\tt None} if the sequence is empty and
   655   {\tt Some ($x$, $xq'$)} if the sequence has head~$x$ and tail~$xq'$.
   656   Warning: calling \hbox{Seq.pull $xq$} again will {\it recompute\/}
   657   the value of~$x$; it is not stored!
   658 \end{ttdescription}
   659 
   660 
   661 \subsection{Converting between sequences and lists}
   662 \begin{ttbox} 
   663 Seq.chop    : int * 'a seq -> 'a list * 'a seq
   664 Seq.list_of : 'a seq -> 'a list
   665 Seq.of_list : 'a list -> 'a seq
   666 \end{ttbox}
   667 \begin{ttdescription}
   668 \item[Seq.chop ($n$, $xq$)] returns the first~$n$ elements of~$xq$ as a
   669   list, paired with the remaining elements of~$xq$.  If $xq$ has fewer
   670   than~$n$ elements, then so will the list.
   671   
   672 \item[Seq.list_of $xq$] returns the elements of~$xq$, which must be
   673   finite, as a list.
   674   
   675 \item[Seq.of_list $xs$] creates a sequence containing the elements
   676   of~$xs$.
   677 \end{ttdescription}
   678 
   679 
   680 \subsection{Combining sequences}
   681 \begin{ttbox} 
   682 Seq.append      : 'a seq * 'a seq -> 'a seq
   683 Seq.interleave  : 'a seq * 'a seq -> 'a seq
   684 Seq.flat        : 'a seq seq -> 'a seq
   685 Seq.map         : ('a -> 'b) -> 'a seq -> 'b seq
   686 Seq.filter      : ('a -> bool) -> 'a seq -> 'a seq
   687 \end{ttbox} 
   688 \begin{ttdescription}
   689 \item[Seq.append ($xq$, $yq$)] concatenates $xq$ to $yq$.
   690   
   691 \item[Seq.interleave ($xq$, $yq$)] joins $xq$ with $yq$ by
   692   interleaving their elements.  The result contains all the elements
   693   of the sequences, even if both are infinite.
   694   
   695 \item[Seq.flat $xqq$] concatenates a sequence of sequences.
   696   
   697 \item[Seq.map $f$ $xq$] applies $f$ to every element
   698   of~$xq=x@1,x@2,\ldots$, yielding the sequence $f(x@1),f(x@2),\ldots$.
   699   
   700 \item[Seq.filter $p$ $xq$] returns the sequence consisting of all
   701   elements~$x$ of~$xq$ such that $p(x)$ is {\tt true}.
   702 \end{ttdescription}
   703 
   704 \index{tactics|)}