1\chapter{Conversions} 2\label{avra_conv} 3 4A {\it conversion\/} in \HOL\ is a rule that maps a term to a theorem 5expressing the equality of that term to some other term. An example is the 6rule for $\beta$-conversion: 7 8\[\ml{(}\verb%\%x\ml{.}t_1\ml{)}t_2\quad\mapsto\quad 9\ml{|- (}\verb%\%x\ml{.}t_1\ml{)}t_2\;\; \ml{=} \;\;t_1[t_2/x] \] 10 11\noindent Theorems\index{equational theorems, in HOL logic@equational theorems, in \HOL\ logic!produced by conversions}\index{conversions!purpose of} of this sort are used in 12\HOL\ in a variety of contexts, to justify the replacement of particular terms 13by semantically equivalent terms. 14 15The \ML\ type of conversions is \ml{conv}: 16 17\begin{hol}\begin{verbatim} 18type conv = term -> thm 19\end{verbatim}\end{hol} 20 21\noindent For example, \ml{BETA\_CONV}\index{BETA_CONV@\ml{BETA\_CONV}} 22is an \ML\ function of type \ml{conv} (\ie\ a conversion) that 23expresses $\beta$-conversion in \HOL. It produces the appropriate 24equational theorem on $\beta$-redexes and fails elsewhere. 25 26\setcounter{sessioncount}{1} 27\begin{session} 28\begin{verbatim} 29- BETA_CONV; 30> val it = fn : term -> thm 31 32- BETA_CONV ``(\x. (\y. (\z. x + y + z)3)2) 1``; 33> val it = |- (\x. (\y. (\z. x + (y + z))3)2)1 = (\y. (\z. 1 + (y + z))3)2 : 34 thm 35 36- BETA_CONV ``(\y. (\z. 1 + (y + z))3) 2``; 37> val it = |- (\y. (\z. 1 + (y + z))3)2 = (\z. 1 + (2 + z))3 : thm 38 39- BETA_CONV ``(\z. 1 + (2 + z)) 3``; 40> val it = |- (\z. 1 + (2 + z))3 = 1 + (2 + 3) : thm 41 42- BETA_CONV ``1 + (2 + 3)``; 43! Uncaught exception: 44! HOL_ERR 45\end{verbatim} 46\end{session} 47 48The basic conversions, as well as a number of those commonly used, are 49provided in \HOL. There are also groups of 50application-specific\index{conversions!application specific} 51conversions to be found in some of the libraries. (Of those provided, 52some are derived and some, like \ml{BETA\_CONV} are taken as 53axiomatic\footnote{A list of the axiomatic rules was supplied in 54 Section~\ref{avra_standard}.}.) In addition, \HOL\ provides a 55collection of \ML\ functions enabling users to define new conversions 56(as well as new rules and tactics) as functions of existing ones. 57Some of these are described in Sections~\ref{avra1} and~\ref{avra2}. 58The notion of conversions is inherited from Cambridge 59\LCF;\index{LCF@\LCF!Cambridge} the underlying principles are 60described in \cite{lcp-rewrite,new-LCF-man}. 61 62Conversions such as \ml{BETA\_CONV}\index{beta-conversion, in HOL 63 logic@beta-conversion, in \HOL\ logic!not expressible as a 64 theorem}\index{theorems, in HOL logic@theorems, in \HOL\ logic!rules 65 inexpressible as}\index{conversions!as families of equations} 66represent infinite families of equations\footnote{This was also 67 mentioned in Section~\ref{avra_standard}.}. They are particularly 68useful in cases in which it is impossible to state, within the logic, 69a single axiom or theorem instantiable to every equation in a 70family\index{families of inferences, in HOL logic@families of 71 inferences, in \HOL\ logic}.\footnote{In the case of 72 $\beta$-conversion specifically, it is the substitution of one term 73 for another in a context that is inexpressible; but in general, 74 there is a variety of reasons that arise.} Instead, an \ML\ 75procedure returns the instance of the desired theorem for any given 76term. This is also the reason that quite a few of the other rules in 77\HOL\ are not stated instead as axioms or theorems. As rules, 78conversions are distinguished with an \ML\ type abbreviation simply 79because there are relatively many of them with the same type, and 80because they return equational\index{theorems, in HOL logic@theorems, 81 in \HOL\ logic!equational}\index{equational theorems, in HOL 82 logic@equational theorems, in \HOL\ logic!produced by conversions} 83theorems that lend themselves directly to term rewriting.\footnote{In 84 fact, some ML functions have names with the suffix `{\tt \_CONV}' 85 but do not have the type {\tt conv}; {\tt SUBST\_CONV}, for example, 86 has type {\tt (thm \# term) list -> term -> conv}. Those that 87 eventually produce conversion are thought of as `conversion 88 schemas'.} In many \HOL\ applications, the main use of conversions 89is to produce these equational theorems. A few examples of 90conversions are illustrated below. 91 92\setcounter{sessioncount}{1} 93\begin{session} 94\begin{verbatim} 95- NOT_FORALL_CONV ``~!x. (f:'a->'a) x = g x``; 96> val it = |- ~(!x. f x = g x) = (?x. ~(f x = g x)) : thm 97 98- CONTRAPOS_CONV ``(!x. f x = g x) ==> ((f:'a->'a) = g)``; 99> val it = |- (!x. f x = g x) ==> (f = g) = ~(f = g) ==> ~!x. f x = g x : thm 100 101- SELECT_CONV ``(@f:'a->'a. f x = g x)x = g x``; 102> val it = |- ((@f. f x = g x)x = g x) = ?f. f x = g x 103 104- EXISTS_UNIQUE_CONV ``?!z. (f:'a->'a) z = g z``; 105> val it = 106 |- (?!z. f z = g z) = 107 (?z. f z = g z) /\ !z z'. (f z = g z) /\ (f z' = g z') ==> (z = z') : 108 thm 109\end{verbatim} 110\end{session} 111 112\noindent An example of an application specific conversion is 113\ml{numLib}'s \ml{num\_CONV}: 114 115\setcounter{sessioncount}{1} 116\begin{session} 117\begin{verbatim} 118- numLib.num_CONV ``2``; 119> val it = |- 2 = SUC 1 : thm 120 121- numLib.num_CONV ``1``; 122> val it = |- 1 = SUC 0 : thm 123 124- numLib.num_CONV ``0`` handle e => Raise e; 125 126Exception raised at Num_conv.num_CONV: 127Term either not a numeral or zero 128! Uncaught exception: 129! HOL_ERR 130\end{verbatim} 131\end{session} 132 133Another application of conversions, related to the first, is in the 134implementation of the existing rewriting tools, \ml{REWRITE\_CONV} 135(Section~\ref{avra3}), \ml{REWRITE\_RULE} (Section~\ref{avra_rewrite}) 136and \ml{REWRITE\_TAC} (Chapter~\ref{tactics-and-tacticals}), which are 137central to theorem proving in \HOL. This use is explained in 138Section~\ref{avra3}, both as an example and because users may have 139occasion to construct rewriting tools of their own design, by similar 140methods. The next section introduces the conversion-building tools in 141general. 142 143\section{Indicating unchangedness} 144 145All conversions may raise the special exception \ml{Conv.UNCHANGED} on 146an input term \ml{t}, as a ``short-hand'' instead of returning the 147reflexive theorem \ml{|-~t~=~t}. This is done for efficiency reasons. 148All of the connectives described below in Section~\ref{avra1} handle 149this exception appropriately. The standard function \ml{QCONV} is 150provided to automatically handle this exception in contexts where it 151would be inappropriate (typically where a conversion is being called 152to provide a theorem directly). \ml{QCONV}'s implementation is 153\begin{alltt} 154 fun QCONV c t = c t handle UNCHANGED => REFL t 155\end{alltt} 156 157 158\section{Conversion combining operators} 159\label{avra1} 160 161A term $u$ is said to {\it reduce\/}\index{reduction, by conversions}\index{conversions!reduction by} to a term $v$ by a conversion $c$ if 162there exists a 163finite sequence\index{sequencing!of conversions} of terms $t_1$, $t_2$, $\ldots$, $t_n$ such that: 164\begin{myenumerate} 165\item $u = t_1$ and $v = t_n$; 166\item $c\ t_i$ evaluates to the theorem 167$\ml{|- }t_i\;\ml{=}\;t_{i+1}$ for $1\leq i < n$; 168\item The evaluation of $c\ t_n$ fails. 169\end{myenumerate} 170 171\noindent The first session of this chapter illustrates the reduction of 172the term 173 174\begin{hol} 175\begin{verbatim} 176 (\x. (\y. (\z. x + y + z)3)2)1 177\end{verbatim} 178\end{hol} 179 180\noindent to \ml{1 + (2 + 3)} by the 181conversion \ml{BETA\_CONV}, in a reduction sequence of length four: 182 183\begin{hol} 184\begin{verbatim} 185 (\x. (\y. (\z. x + (y + z))3)2)1 186 (\y. (\z. 1 + (y + z))3)2 187 (\z. 1 + (2 + z))3 188 1 + (2 + 3) 189\end{verbatim} 190\end{hol} 191 192\noindent That is, \ml{BETA\_CONV} applies to each term of the sequence, 193except the fourth and last, to give a theorem equating that term to 194the next term. Therefore, each term of the sequence, from the second 195on, can be extracted from the theorem for the previous term; namely, 196it is the right hand side of the conclusion. The whole reduction can 197therefore be accomplished by repeated application of \ml{BETA\_CONV} 198to the terms of the sequence as they are generated. 199 200\index{alternation!of conversions|(} To transform \ml{BETA\_CONV} to 201achieve this effect, two operators on 202conversions\index{conversions!operators on|(} are introduced. The 203first one, infixed, is \ml{THENC}, which sequences conversions. 204 205\begin{holboxed} 206\index{THENC@\ml{THENC}|pin} 207\begin{verbatim} 208 op THENC : conv -> conv -> conv 209\end{verbatim} 210\end{holboxed} 211 212\noindent If $c_1\ t_1$ evaluates to $\Gamma_1\ml{ |- }t_1\ml{=}t_2$ and 213$c_2\ t_2$ evaluates to $\Gamma_2\ml{ |- }t_2\ml{=}t_3$, then 214$\ml{(}c_1\ \ml{THENC}\ c_2\ml{)}\ t_1$ evaluates to $\Gamma_1\cup 215\Gamma_2\ml{\ |-\ }t_1\ml{=}t_3$. If the evaluation of $c_1\ t_1$ or 216the evaluation of $c_2\ t_2$ fails, then so does the evaluation of 217$c_1\ \ml{THENC}\ c_2$. \ml{THENC} is justified by the transitivity of 218equality. 219 220The second, also infixed, is \ml{ORELSEC}; this applies a second 221conversion if the application of the first one fails. 222 223\begin{holboxed} 224\index{ORELSEC@\ml{ORELSEC}|pin} 225\begin{verbatim} 226 op ORELSEC : conv -> conv -> conv 227\end{verbatim} 228\end{holboxed} 229 230\noindent $\ml{(}c_1\ \ml{ORELSEC}\ c_2\ml{)}\ t$ evaluates to $c_1\ t$ 231if that evaluation succeeds, and to $c_2\ t$ otherwise. (The failure 232to evaluate is detected via the \ML\ failure construct.) 233 234The functions \ml{THENC} and \ml{ORELSEC} are used to define the 235desired operator, \ml{REPEATC}\index{repetition!of conversions}, which 236successively\index{successive application!conversion operator for} 237applies a conversion until it fails: 238 239\begin{holboxed} 240\index{REPEATC@\ml{REPEATC}|pin} 241\begin{verbatim} 242 REPEATC : conv -> conv 243\end{verbatim} 244\end{holboxed} 245 246\noindent \ml{REPEATC}\ $c$ is intuitively equivalent to: 247 248\begin{hol} 249\begin{alltt} 250 (\m{c} THENC \m{c} THENC ... THENC \m{c} THENC ...) ORELSEC ALL_CONV 251\end{alltt} 252\end{hol} 253 254\noindent It is defined recursively:\footnote{Note that because ML is a 255 call-by-value language, the extra argument $t$ is needed in the 256 definition of {\tt REPEATC}; without it the definition would loop. 257 There is a similar problem with the tactical {\tt REPEAT}; see 258 Chapter~\ref{tactics-and-tacticals}.} 259 260\begin{hol} 261\begin{verbatim} 262 fun REPEATC c t = ((c THENC (REPEATC c)) ORELSEC ALL_CONV) t 263\end{verbatim} 264\end{hol} 265 266The current example term can thus be completely reduced by use of 267\ml{BETA\_CONV} transformed by the \ml{REPEATC} operator: 268 269\setcounter{sessioncount}{1} 270\begin{session} 271\begin{verbatim} 272- REPEATC BETA_CONV; 273> val it = fn : term -> thm 274 275- REPEATC BETA_CONV ``(\x. (\y. (\z. x + y + z)3)2)1``; 276> val it = |- (\x. (\y. (\z. x + (y + z))3)2)1 = 1 + (2 + 3) : thm 277\end{verbatim} 278\end{session} 279\index{alternation!of conversions|)} 280 281\ml{BETA\_CONV} applies to terms of a certain top level form only, 282namely to $\beta$-redexes, and fails on terms of any other form. In 283addition, no number of repetitions of \ml{BETA\_CONV} will 284$\beta$-reduce {\it arbitrary\/} $\beta$-redexes embedded in terms. 285For example, the term shown below fails even at the top level because 286it is not a $\beta$-redex: 287 288\setcounter{sessioncount}{1} 289\begin{session} 290\begin{verbatim} 291- BETA_CONV ``(((\x.(\y.(\z. x + y + z))) 1) 2) 3``; 292! Uncaught exception: 293! HOL_ERR 294 295- is_abs ``(((\x.(\y.(\z. x + y + z))) 1) 2)``; 296> val it = false : bool 297\end{verbatim} 298\end{session} 299 300\noindent 301The $\beta$-redex {\small\verb|(\w.w)3|} is not affected in the third 302input of the session shown below, because of its position in the 303structure of the whole term. This is so even though the whole term is 304reduced, and the subterm at top level could be reduced: 305 306\setcounter{sessioncount}{1} 307\begin{session}\begin{verbatim} 308#BETA_CONV "(\z. x + y + z)3";; 309|- (\z. x + (y + z))3 = x + (y + 3) 310 311#BETA_CONV "(\w.w)3";; 312|- (\w. w)3 = 3 313 314#REPEATC BETA_CONV "(\z. x + y + z)((\w.w)3)";; 315|- (\z. x + (y + z))((\w. w)3) = x + (y + ((\w. w)3)) 316\end{verbatim}\end{session} 317 318To produce, from a conversion $c$, a conversion that applies $c$ to every 319subterm of a term, the function \ml{DEPTH\_CONV} can be applied to $c$: 320 321\begin{holboxed} 322\index{DEPTH_CONV@\ml{DEPTH\_CONV}|pin} 323\begin{verbatim} 324 DEPTH_CONV : conv -> conv 325\end{verbatim}\end{holboxed} 326 327\noindent \ml{DEPTH\_CONV}$\ c$ is a conversion 328 329\[t\quad\mapsto\quad \ml{|- }t\ml{ = }t'\] 330 331\noindent where $t'$ is obtained from $t$ by replacing every subterm 332$u$ by $v$ if $u$ reduces to $v$ by $c$. (Subterms for which 333$c\ u$ fails are left unchanged.) The definition leaves open the search strategy; 334in fact, 335\ml{DEPTH\_CONV}$\ c$\index{DEPTH_CONV@\ml{DEPTH\_CONV}!search strategy of} 336 traverses a term\footnote{That is, it traverses 337the abstract parse tree of the term.} `bottom up', once, and left-to-right, 338repeatedly applying $c$ to each subterm until no longer applicable. 339This helps with the two problems thus far: 340 341\setcounter{sessioncount}{1} 342\begin{session}\begin{verbatim} 343#DEPTH_CONV BETA_CONV "(((\x.(\y.(\z. x + y + z))) 1) 2) 3";; 344|- (\x y z. x + (y + z))1 2 3 = 1 + (2 + 3) 345 346#DEPTH_CONV BETA_CONV "(\z. x + y + z)((\w.w)3)";; 347|- (\z. x + (y + z))((\w. w)3) = x + (y + 3) 348\end{verbatim}\end{session} 349 350It may happen, however, 351that the result of such a conversion still contains subterms 352that could themselves be reduced at top level. For example: 353 354\setcounter{sessioncount}{1} 355\begin{session}\begin{verbatim} 356#let t = "(\f.\x.f x)(\n.n+1)";; 357t = "(\f x. f x)(\n. n + 1)" : term 358 359#DEPTH_CONV BETA_CONV t;; 360|- (\f x. f x)(\n. n + 1) = (\x. (\n. n + 1)x) 361\end{verbatim}\end{session} 362 363\noindent The function \ml{TOP\_DEPTH\_CONV} 364 does more 365searching and reduction than 366\ml{DEPTH\_CONV}: it replaces 367every subterm 368$u$ by $v'$ if $u$ reduces to $v$ by $c$ and $v$ {\it recursively\/} reduces 369to $v'$ by {\tt TOP\_DEPTH\_CONV}$\ c$.\footnote{Readers interested 370in characterizing the search strategy of {\tt TOP\_DEPTH\_CONV} should 371study the \ML\ definitions near the end of this section.} 372 373\begin{holboxed} 374\index{TOP_DEPTH_CONV@\ml{TOP\_DEPTH\_CONV}|pin} 375\begin{verbatim} 376 TOP_DEPTH_CONV : conv -> conv 377\end{verbatim}\end{holboxed} 378 379\noindent Thus: 380 381\begin{session}\begin{verbatim} 382#TOP_DEPTH_CONV BETA_CONV t;; 383|- (\f x. f x)(\n. n + 1) = (\x. x + 1) 384\end{verbatim}\end{session} 385 386Finally, the simpler function \ml{ONCE\_DEPTH\_CONV} is provided: 387 388\begin{holboxed} 389\index{ONCE_DEPTH_CONV@\ml{ONCE\_DEPTH\_CONV}|pin} 390\begin{verbatim} 391 ONCE_DEPTH_CONV : conv -> conv 392\end{verbatim}\end{holboxed} 393 394\noindent $\ml{ONCE\_DEPTH\_CONV}\ c\ t$ applies $c$ once to the first 395term (and only the first term) 396on which it succeeds in a top-down traversal: 397 398\begin{session}\begin{verbatim} 399#ONCE_DEPTH_CONV BETA_CONV t;; 400|- (\f x. f x)(\n. n + 1) = (\x. (\n. n + 1)x) 401 402#ONCE_DEPTH_CONV BETA_CONV "(\x. (\n. n + 1)x)";; 403|- (\x. (\n. n + 1)x) = (\x. x + 1) 404\end{verbatim}\end{session} 405 406The equational theorems returned by conversions are not always 407useful in equational form. To make the results more useful for theorem 408proving, 409a conversion can be converted to a rule or a tactic, using the functions 410\ml{CONV\_RULE} or \ml{CONV\_TAC}, respectively. 411 412 413\begin{holboxed} 414\index{CONV_RULE@\ml{CONV\_RULE}|pin} 415\index{CONV_TAC@\ml{CONV\_TAC}|pin} 416\begin{verbatim} 417 CONV_RULE : conv -> thm -> thm 418 CONV_TAC : conv -> tactic 419\end{verbatim}\end{holboxed} 420 421\noindent $\ml{CONV\_RULE}\ c\ \ml{(|- }t\ml{)}$ returns $\ml{|- }t'$, where 422$c\ t$ evaluates to the equation 423$\ml{|-}\ t\ml{=}t'$. 424$\ml{CONV\_TAC}\ c$ is a tactic that 425converts the conclusion of a goal using $c$. \ml{CONV\_RULE} is defined by: 426 427\begin{hol}\begin{verbatim} 428 let CONV_RULE c th = EQ_MP (c(concl th)) th 429\end{verbatim}\end{hol} 430 431\noindent (The validation of \ml{CONV\_TAC} also 432uses \ml{EQ\_MP}\footnote{For \ml{EQ\_MP}, see ~\ref{avra_eq_mp}.}.) 433For example, the built-in rule \ml{BETA\_RULE} reduces some 434of the $\beta$-redex subterms of a term. 435 436\begin{holboxed} 437\index{BETA_RULE@\ml{BETA\_RULE}|pin} 438\begin{verbatim} 439 BETA_RULE : thm -> thm 440\end{verbatim}\end{holboxed} 441 442\noindent It is defined by: 443 444\begin{hol}\begin{verbatim} 445 let BETA_RULE = CONV_RULE(DEPTH_CONV BETA_CONV) 446\end{verbatim}\end{hol} 447 448\noindent The search invoked by \ml{BETA\_RULE} 449is adequate for some purposes but not others; for example, 450the first use shown below but not the second: 451 452\begin{session}\begin{verbatim} 453#BETA_RULE(ASSUME "(((\x.(\y.(\z. x + y + z))) 1) 2) 3 < 10");; 454. |- (1 + (2 + 3)) < 10 455 456#let th = ASSUME "NEXT = ^t";; 457th = . |- NEXT = (\f x. f x)(\n. n + 1) 458 459#BETA_RULE th;; 460. |- NEXT = (\x. (\n. n + 1)x) 461 462#BETA_RULE(BETA_RULE th);; 463. |- NEXT = (\x. x + 1) 464\end{verbatim}\end{session} 465 466\noindent A more powerful 467$\beta$-reduction rule 468that used the second search strategy could be defined as shown below 469(this is not built into \HOL). 470 471\begin{session}\begin{verbatim} 472#let TOP_BETA_RULE = CONV_RULE(TOP_DEPTH_CONV BETA_CONV);; 473TOP_BETA_RULE = - : (thm -> thm) 474 475#TOP_BETA_RULE th;; 476. |- NEXT = (\x. x + 1) 477\end{verbatim}\end{session} 478 479\ml{TOP\_DEPTH\_CONV} is the traversal 480strategy used by the \HOL\ rewriting tools described in Section~\ref{avra3}. 481\index{conversions!operators on|)} 482 483\section{Writing compound conversions} 484\label{avra2} 485 486\index{conversions!functions for building|(} 487There are several other conversion operators in \HOL, which, 488together with \ml{THENC}, \ml{ORELSEC} and \ml{REPEATC} are available 489for building more complex conversions, as well as rules, tactics, and so on. 490These are described below; several are good illustrations themselves 491of how functions are built using conversions. The section culminates 492with the explanation of how \ml{DEPTH\_CONV}, \ml{TOP\_DEPTH\_CONV}, and 493\ml{ONCE\_DEPTH\_CONV} are built. 494 495The conversion \ml{NO\_CONV} is an identity for 496\ml{ORELSEC}\index{ORELSEC@\ml{ORELSEC}}, useful 497in building functions. 498 499\begin{holboxed} 500\index{NO_CONV@\ml{NO\_CONV}|pin} 501\begin{verbatim} 502 NO_CONV : conv 503\end{verbatim}\end{holboxed} 504 505\noindent $\ml{NO\_CONV}\ t$ always fails. 506 507The function \ml{FIRST\_CONV} 508returns $c\ t$ for the first conversion $c$, in a list of conversions, 509for which the evaluation of $c\ t$ succeeds. 510 511\begin{holboxed} 512\index{FIRST_CONV@\ml{FIRST\_CONV}|pin} 513\begin{verbatim} 514 FIRST_CONV : conv list -> conv 515\end{verbatim}\end{holboxed} 516 517\noindent $\ml{FIRST\_CONV [}c_1\ml{;}\ldots\ml{;}c_n\ml{]}$ is equivalent, 518intuitively, to: 519 520\begin{hol} 521\index{ORELSEC@\ml{ORELSEC}} 522\begin{alltt} 523 \m{c\sb{1}} ORELSEC \m{c\sb{2}} ORELSEC \m{\ldots} ORELSEC \m{c\sb{n}} 524\end{alltt}\end{hol} 525 526\noindent It is defined by: 527 528\begin{hol}\begin{verbatim} 529 let FIRST_CONV cl t = 530 itlist $ORELSEC cl NO_CONV t ? failwith `FIRST_CONV`;; 531\end{verbatim}\end{hol} 532 533The conversion \ml{ALL\_CONV} is an identity for \ml{THENC},\index{THENC@\ml{THENC}} useful 534in building functions. 535 536\begin{holboxed} 537\index{ALL_CONV@\ml{ALL\_CONV}|pin} 538\begin{verbatim} 539 ALL_CONV : conv 540\end{verbatim}\end{holboxed} 541 542\noindent \ml{ALL\_CONV\ $t$} evaluates to \ml{|-\ $t$=$t$}. It is 543defined as being identical to \ml{REFL}.\index{REFL@\ml{REFL}} 544 545The function \ml{EVERY\_CONV} applies a list of conversions in sequence. 546 547\begin{holboxed} 548\index{EVERY_CONV@\ml{EVERY\_CONV}|pin} 549\begin{verbatim} 550 EVERY_CONV : conv list -> conv 551\end{verbatim}\end{holboxed} 552 553\noindent $\ml{EVERY\_CONV [}c_1\ml{;}\ldots\ml{;}c_n\ml{]}$ is equivalent, 554intuitively, to: 555 556\begin{hol} 557\index{THENC@\ml{THENC}} 558\begin{alltt} 559 \m{c\sb{1}} THENC \m{c\sb{2}} THENC \m{\ldots} THENC \m{c\sb{n}} 560\end{alltt}\end{hol} 561 562\noindent It is defined by: 563 564\begin{hol}\begin{verbatim} 565 let EVERY_CONV cl t = 566 itlist $THENC cl ALL_CONV t ? failwith `EVERY_CONV` 567\end{verbatim}\end{hol} 568 569The operator \ml{CHANGED\_CONV} converts one conversion to another that 570fails on arguments that it cannot change. 571 572\begin{holboxed} 573\index{CHANGED_CONV@\ml{CHANGED\_CONV}|pin} 574\begin{verbatim} 575 CHANGED_CONV : conv -> conv 576\end{verbatim}\end{holboxed} 577 578\noindent If $c\ t$ evaluates to $\ml{|-}\ t\ml{=}t'$, then 579$\ml{CHANGED\_CONV}\ c\ t$ also evaluates to $\ml{|-}\ t\ml{=}t'$, 580unless $t$ and $t'$ are the same (up to $\alpha$-conversion), 581in which case it fails. 582 583The operator \ml{TRY\_CONV} maps one conversion to another that 584always succeeds, by replacing failures with the identity conversion. 585 586\begin{holboxed} 587\index{TRY_CONV@\ml{TRY\_CONV}|pin} 588\begin{verbatim} 589 TRY_CONV : conv 590\end{verbatim}\end{holboxed} 591 592\noindent If $c\ t$ evaluates to $\ml{|-}\ t\ml{=}t'$, then 593$\ml{TRY\_CONV}\ c\ t$ also evaluates to $\ml{|-}\ t\ml{=}t'$. If 594$c\ t$ fails, then $\ml{TRY\_CONV}\ c\ t$ evaluates 595to $\ml{|-}\ t\ml{=}t$. \ml{TRY\_CONV} is implemented by: 596 597\begin{hol}\begin{verbatim} 598 let TRY_CONV c = c ORELSEC ALL_CONV 599\end{verbatim}\end{hol} 600 601\noindent It is used in the implementation of 602\ml{TOP\_DEPTH\_CONV} (given later). 603 604There are a number of operators for applying conversions to the 605immediate subterms of a term. These use the \ML\ functions: 606 607 608\begin{holboxed} 609\index{MK_COMB@\ml{MK\_COMB}|pin} 610\index{MK_ABS@\ml{MK\_ABS}|pin} 611\begin{verbatim} 612 MK_COMB : thm # thm -> thm 613 MK_ABS : thm -> thm 614\end{verbatim}\end{holboxed} 615 616\noindent \ml{MK\_COMB} and \ml{MK\_ABS} implement the following derived rules: 617 618$${\Gamma_1 \ml{ |- } u_1\ml{=}v_1\qquad 619 \Gamma_2\ml{ |- } u_2\ml{=}v_2 \over 620\Gamma_1\cup\Gamma_2\ml{ |- } u_1\ u_2 \ml{=} v_1\ v_2}\quad\ml{MK\_COMB}$$ 621 622$${\Gamma\ml{ |- !}x\ml{.}u\ml{=}v \over 623\Gamma\ml{ |- (}\verb%\%x\ml{.}u\ml)=(\verb%\%x\ml{.}v\ml{)}}\quad\ml{MK\_ABS}$$ 624 625\noindent The function \ml{SUB\_CONV} 626applies a conversion to the immediate subterms 627of a term. 628 629\begin{holboxed} 630\index{SUB_CONV@\ml{SUB\_CONV}|pin} 631\begin{verbatim} 632 SUB_CONV : conv 633\end{verbatim}\end{holboxed} 634 635\noindent In particular: 636 637\begin{itemize} 638\item \ml{SUB\_CONV}$\;\;c\;\;$\ml{"}$x$\ml{"} $\;=\;$\ \ml{|- }$x$\ml{=}$x$; 639\item \ml{SUB\_CONV}$\;\;c\;\;$\ml{"}$u\;v$\ml{"} $\;=\;$ \ml{|- }$u\;v$\ml{=}$u'\;v'$,\ \ if 640$c\;\;u$\ $=$\ \ml{|- }$u$\ml{=}$u'$ and 641$c\;\;v$\ $=$\ \ml{|- }$v$\ml{=}$v'$; 642\item\ml{SUB\_CONV}$\;\;c\;\;$\ml{"}{\small\verb%\%}$x$\ml{.}$u$\ml{"} $\;=\;$ 643\ml{|- (}{\small\verb%\%}$x$\ml{.}$u$\ml{) = (}{\small\verb%\%}$x$\ml{.}$u'$\ml{)}, \ \ if 644$c\;\;u$\ $=$\ \ml{|- }$u$\ml{=}$u'$. 645\end{itemize} 646 647\noindent \ml{SUB\_CONV} is implemented\index{conversions!implementation of, in ML@implementation of, in \ML|(} in terms of 648\ml{MK\_COMB} and \ml{MK\_ABS}: 649 650\begin{hol}\begin{verbatim} 651 let SUB_CONV c t = 652 if is_comb t then 653 (let rator,rand = dest_comb t in 654 MK_COMB (c rator, c rand)) 655 if is_abs t then 656 (let bv,body = dest_abs t in 657 let bodyth = c body in 658 MK_ABS (GEN bv bodyth)) 659 else (ALL_CONV t) 660\end{verbatim}\end{hol} 661 662\noindent \ml{SUB\_CONV}, too, is used in the definitions of 663\ml{DEPTH\_CONV} and \ml{TOP\_DEPTH\_CONV}. 664 665Three other useful conversion operators, also for applying conversions 666to the immediate subterms of a term, are as follows: 667 668 669\begin{holboxed} 670\index{RATOR_CONV@\ml{RATOR\_CONV}|pin} 671\index{RAND_CONV@\ml{RAND\_CONV}|pin} 672\index{ABS_CONV@\ml{ABS\_CONV}|pin} 673\begin{verbatim} 674 RATOR_CONV : conv -> conv 675 RAND_CONV : conv -> conv 676 ABS_CONV : conv -> conv 677\end{verbatim}\end{holboxed} 678 679\noindent \ml{RATOR\_CONV}$\ c$ converts the operator of an application using 680$c$; \ml{RAND\_CONV}$\ c$ converts the operand of an application; and 681\ml{ABS\_CONV}$\ c$ converts the body of an abstraction. Combinations 682of these are useful for applying conversions to particular subterms. 683These are implemented by: 684 685\begin{hol}\begin{verbatim} 686 let RATOR_CONV c t = 687 (let rator,rand = dest_comb t in 688 MK_COMB (c rator, REFL rand)) ? failwith `RATOR_CONV` 689 690 let ABS_CONV c t = 691 (let bv,body = dest_abs t in 692 let bodyth = c body in 693 MK_ABS (GEN bv bodyth)) ? failwith `ABS_CONV` 694\end{verbatim}\end{hol} 695 696\noindent The following 697is an example session illustrating these immediate subterm conversions 698(recalling that the expression $t_1\ml{+}t_2$ 699actually parses as $\ml{+}\ t_1\ t_2$). 700 701\setcounter{sessioncount}{1} 702\begin{session}\begin{verbatim} 703#let t = "(\x.x+1)m + (\x.x+2)n";; 704t = "((\x. x + 1)m) + ((\x. x + 2)n)" : term 705 706#RAND_CONV BETA_CONV t;; 707|- ((\x. x + 1)m) + ((\x. x + 2)n) = ((\x. x + 1)m) + (n + 2) 708 709#RATOR_CONV (RAND_CONV BETA_CONV) t;; 710|- ((\x. x + 1)m) + ((\x. x + 2)n) = (m + 1) + ((\x. x + 2)n) 711\end{verbatim}\end{session} 712 713\noindent Finally, the definitions of \ml{DEPTH\_CONV} and 714\ml{TOP\_DEPTH\_CONV} are given below. 715 716 717\begin{hol} 718\index{DEPTH_CONV@\ml{DEPTH\_CONV}} 719\index{TOP_DEPTH_CONV@\ml{TOP\_DEPTH\_CONV}} 720\begin{verbatim} 721 letrec DEPTH_CONV c t = 722 (SUB_CONV (DEPTH_CONV c) THENC (REPEATC c)) t 723 724 letrec TOP_DEPTH_CONV c t = 725 (REPEATC c THENC 726 (TRY_CONV 727 (CHANGED_CONV (SUB_CONV (TOP_DEPTH_CONV c)) THENC 728 TRY_CONV(c THENC TOP_DEPTH_CONV c)))) t 729 730 letrec ONCE_DEPTH_CONV c t = 731 (c ORELSEC (SUB_CONV (ONCE_DEPTH_CONV c))) t 732\end{verbatim}\end{hol} 733\index{conversions!functions for building|)} 734\index{conversions!implementation of, in ML@implementation of, in \ML|)} 735 736\noindent Note that the extra argument {\small\verb%t%} is needed to stop 737these definitions looping (because \ML\ is a call-by-value language). 738Note also that the actual definition of {\small\verb%ONCE_DEPTH_CONV%} 739used in the system has been optimised to use failure to avoid 740rebuilding unchanged subterms. 741 742 743\section{Built in conversions}\label{built-in-conv} 744 745 746Many conversions are predefined in \HOL; only those likely to be of 747general interest are listed here. 748 749\subsection{Generalized beta-reduction}\label{genbeta} 750 751The conversion: 752 753 754\begin{holboxed}\index{PAIRED_BETA_CONV@\ml{PAIRED\_BETA\_CONV}|pin} 755\begin{verbatim} 756 PAIRED_BETA_CONV : conv 757\end{verbatim}\end{holboxed} 758 759\noindent does 760generalized beta-conversion of tupled lambda abstractions applied to 761tuples. 762 763Given the term: 764 765\begin{hol}\begin{verbatim} 766 "(\(x1, ... ,xn).t) (t1, ... ,tn)" 767\end{verbatim}\end{hol} 768 769\noindent \ml{PAIRED\_BETA\_CONV} proves that: 770 771\begin{hol}\begin{verbatim} 772 |- (\(x1, ... ,xn). t[x1,...,xn]) (t1, ... ,tn) = t[t1, ... ,tn] 773\end{verbatim}\end{hol} 774 775\noindent The conversion works for arbitrarily nested tuples. For example: 776 777\setcounter{sessioncount}{1} 778\begin{session}\begin{verbatim} 779#PAIRED_BETA_CONV "(\((a,b),(c,d)). [a;b;c;d]) ((1,2),(3,4))";; 780|- (\((a,b),c,d). [a;b;c;d])((1,2),3,4) = [1;2;3;4] 781\end{verbatim}\end{session} 782 783\subsection{Arithmetical conversions} 784 785The conversion: 786 787\begin{holboxed}\index{ADD_CONV@\ml{ADD\_CONV}|pin} 788\begin{verbatim} 789 ADD_CONV : conv 790\end{verbatim}\end{holboxed} 791 792\noindent does addition by formal proof. 793If $n$ and $m$ are numerals then 794{\small\verb%ADD_CONV "%}$n\ $\ml{+}$\ m$\ml{"} 795returns the theorem {\small\verb%|- %}$n\ $\ml{+}$\ m\ $\ml{=}$\ s$, 796where $s$ is the numeral denoting the sum of $n$ and $m$. For example: 797 798\setcounter{sessioncount}{1} 799\begin{session}\begin{verbatim} 800#ADD_CONV "1 + 2";; 801|- 1 + 2 = 3 802 803#ADD_CONV "0 + 1000";; 804|- 0 + 1000 = 1000 805 806#ADD_CONV "101 + 102";; 807|- 101 + 102 = 203 808\end{verbatim}\end{session} 809 810 811 812The next conversion decides the equality of natural number 813constants. 814 815\begin{holboxed}\index{num_EQ_CONV@\ml{num\_EQ\_CONV}|pin} 816\begin{verbatim} 817 num_EQ_CONV : conv 818\end{verbatim}\end{holboxed} 819 820\noindent If $n$ and $m$ are terms constructed from numeral constants 821and the successor function \ml{SUC}, then: 822{\small\verb%num_EQ_CONV "%}$n$\ml{=}$m$\ml{"} 823returns: 824 825\begin{hol}\begin{alltt} 826 |- (\(n\)=\(m\)) = T \(\mbox{if \(n\) and \(m\) represent the same number}\) 827 |- (\(n\)=\(m\)) = F \(\mbox{if \(n\) and \(m\) represent different numbers}\) 828\end{alltt}\end{hol} 829 830\noindent In addition, {\small\verb%num_EQ_CONV "%}$t\ $\ml{=}$\ t$\ml{"} 831returns: {\small\verb%|- (%}$t$\ml{=}$t${\small\verb%) = T%} 832 833\subsection{List processing conversions} 834 835There are two useful built-in conversions for lists: 836 837\begin{holboxed} 838\index{LENGTH_CONV@\ml{LENGTH\_CONV}|pin} 839\index{list_EQ_CONV@\ml{list\_EQ\_CONV}|pin} 840\begin{verbatim} 841 LENGTH_CONV : conv 842 list_EQ_CONV: conv 843\end{verbatim}\end{holboxed} 844 845\ml{LENGTH\_CONV}: computes the length of a list. 846A call to: 847 848\begin{hol}\begin{alltt} 849 LENGTH_CONV "LENGTH[\(t\sb{1}\);\(\ldots\);\(t\sb{n}\)]" 850\end{alltt}\end{hol} 851 852\noindent generates the theorem: 853 854\begin{hol}\begin{alltt} 855 |- LENGTH [\(t\sb{1}\);\(\ldots\);\(t\sb{n}\)] = n 856\end{alltt}\end{hol} 857 858The other conversion, \ml{list\_EQ\_CONV}, proves or disproves the 859equality of two lists, given 860a conversion for deciding the equality of elements. 861A call to: 862 863\begin{hol}\begin{alltt} 864 list_EQ_CONV \(conv\) "[\(u\sb{1}\);\(\ldots\);\(u\sb{n}\)] = [\(v\sb{1}\);\(\ldots\);\(v\sb{m}\)]" 865\end{alltt}\end{hol} 866 867\noindent returns: {\small\verb%|- ([%}$u_1$\m{;}$\ldots$\ml{;}$u_n$\ml{]\ =\ [}$v_1$\ml{;}$\ldots$\ml{;}$v_m$\ml{])\ =\ F} if: 868 869\begin{myenumerate} 870\item {\small\verb%~%}\ml{($n$=$m$)} or 871\item \ml{$conv$} proves {\small\verb%|- (%}$u_i\ $\ml{=}$\ v_i$\ml{)\ =\ F} 872for any $1\leq i \leq m$. 873\end{myenumerate} 874 875\noindent {\small\verb%|- ([%}$u_1$\m{;}$\ldots$\ml{;}$u_n$\ml{]\ =\ [}$v_1$\ml{;}$\ldots$\ml{;}$v_m$\ml{])\ =\ T} is returned if: 876 877\begin{myenumerate} 878 879\item \ml{($n$=$m$)} and \ml{$u_i$} is syntactically identical to 880\ml{$v_i$} for $1\leq i \leq m$, or 881\item \ml{($n$=$m$)} and \ml{$conv$} proves 882{\small\verb%|- (%}$u_i$\ml{=}$v_i$\ml{)=T} for $1\leq i \leq n$. 883\end{myenumerate} 884 885\subsection{Simplifying {\tt let}-terms}\label{let-terms} 886\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!simplification of} 887 888A conversion for reducing {\tt let}-terms is now provided. 889 890\begin{holboxed}\index{let_CONV@\ml{let\_CONV}|pin} 891\begin{verbatim} 892 let_CONV : conv 893\end{verbatim}\end{holboxed} 894 895\noindent Given a term: 896 897\begin{hol}\begin{alltt} 898 "let \(v\sb{1}\) = \(t\sb{1}\) and \(\cdots\) and \(v\sb{n}\) = \(t\sb{n}\) in \(t[v\sb{1},\ldots,v\sb{n}]\)" 899\end{alltt}\end{hol} 900 901\noindent \ml{let\_CONV} proves that: 902 903\begin{hol}\begin{alltt} 904 |- let \(v\sb{1}\) = \(t\sb{1}\) and \(\cdots\) and \(v\sb{n}\) = \(t\sb{n}\) in \(t[v\sb{1},\ldots,v\sb{n}]\) = \(t[t\sb{1},\ldots,t\sb{n}]\) 905\end{alltt}\end{hol} 906 907\noindent The \ml{$v_i$}'s can take any one of the following forms: 908 909\begin{myenumerate} 910\item Variables: \ml{x} etc. 911\item Tuples: \ml{(x,y)}, \ml{(a,b,c)}, \ml{((a,b),(c,d))} etc. 912\item Applications: \ml{f (x,y) z}, \ml{f x} etc. 913\end{myenumerate} 914 915\noindent Variables are just substituted for. With tuples, the substitution is 916done component-wise, and function applications are effectively 917rewritten in the body of the {\tt let}-term. 918 919\setcounter{sessioncount}{1} 920\begin{session}\begin{verbatim} 921#let_CONV "let x = 1 in x+y";; 922|- (let x = 1 in x + y) = 1 + y 923 924#let_CONV "let (x,y) = (1,2) in x+y";; 925|- (let (x,y) = 1,2 in x + y) = 1 + 2 926 927#let_CONV "let f x = 1 and f y = 2 in (f 10) + (f 20)";; 928|- (let f x = 1 and f y = 2 in (f 10) + (f 20)) = 2 + 2 929 930#let_CONV "let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))";; 931|- (let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))) = 932(((0 + 2) + 1) + 2) + 1 933 934#CONV_RULE(DEPTH_CONV ADD_CONV)it;; 935|- (let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))) = 6 936 937#let_CONV "let f x y = x+y in f 1";; % NB: partial application % 938|- (let f x y = x + y in f 1) = (\y. 1 + y) 939\end{verbatim}\end{session} 940 941\subsection{Skolemization}\index{Skolemization} 942 943Two conversions are provided for a higher-order version of 944Skolemization (using existentially quantified function variables 945rather than first-order Skolem constants). 946 947The conversion 948 949\begin{holboxed}\index{X_SKOLEM_CONV@\ml{X\_SKOLEM\_CONV}|pin} 950\begin{verbatim} 951 X_SKOLEM_CONV : term -> conv 952\end{verbatim}\end{holboxed} 953 954\noindent takes a variable parameter, \ml{$f$} say, and 955proves: 956 957\begin{hol}\begin{alltt} 958 |- (!\(x\sb{1}\) \(\ldots\) \(x\sb{n}\). ?\(y\). \(t[x\sb{1},\ldots,x\sb{n},y]\) = (?\(f\). !\(x\sb{1}\) \(\ldots\) \(x\sb{n}\). \(t[x\sb{1},\ldots,x\sb{n},f x\sb{1}\ldots x\sb{n}]\) 959\end{alltt}\end{hol} 960 961\noindent for any input term 962\ml{!$x_1\ \ldots\ x_n$.\ ?$y$. $t[x_1,\ldots,x_n,y]$}. 963Note that when $n=\ml{0}$, this 964is equivalent to alpha-conversion: 965 966\begin{hol}\begin{alltt} 967 |- (?\(y\). \(t[y]\)) = (?\(f\). \(t[f]\)) 968\end{alltt}\end{hol} 969 970\noindent and that the conversion fails if there is already a free 971variable \ml{$f$} of the appropriate type in the input term. For example: 972 973\begin{hol}\begin{alltt} 974 X_SKOLEM_CONV "f:num->*" "!n:num. ?x:*. x = (f n)" 975\end{alltt}\end{hol} 976 977\noindent will fail. The conversion \ml{SKOLEM\_CONV} is 978like \ml{X\_SKOLEM\_CONV}, except that it 979uses a primed variant of the name of the existentially quantified variable 980as the name of the skolem function it introduces. For example: 981 982\begin{hol}\begin{alltt} 983 SKOLEM_CONV "!x. ?y. P x y" 984\end{alltt}\end{hol} 985 986\noindent proves that: 987 988\begin{hol}\begin{alltt} 989 |- ?y. !x. P x (y x) 990\end{alltt}\end{hol} 991 992 993\subsection{Quantifier movement conversions} 994\index{quantifiers!conversions for} 995\index{conversions!quantifier movement} 996 997A complete and systematically-named set of conversions for moving quantifiers 998inwards and outwards through the logical connectives {\small\verb%~%}, 999{\small\verb%/\%}, {\small\verb%\/%}, and {\small\verb%==>%} is provided. 1000The naming scheme is based on the following 1001atoms: 1002 1003\begin{hol}\begin{alltt} 1004 <\(quant\)> := FORALL | EXISTS 1005 <\(conn\)> := NOT | AND | OR | IMP 1006 [\(dir\)] := LEFT | RIGHT \({\it (optional)}\) 1007\end{alltt}\end{hol} 1008 1009 1010 1011The conversions for moving quantifiers inwards are called: 1012 1013\begin{hol}\begin{alltt} 1014 <\(quant\)>_<\(conn\)>_CONV 1015\end{alltt}\end{hol} 1016 1017\noindent where the quantifier \ml{<$quant$>} is to be moved inwards 1018through \ml{<$conn$>}. 1019 1020The conversions for moving quantifiers outwards are called: 1021 1022\begin{hol}\begin{alltt} 1023 [\(dir\)]_<\(conn\)>_<\(quant\)>_CONV 1024\end{alltt}\end{hol} 1025 1026\noindent where \ml{<$quant$>} is to be moved outwards 1027through \ml{<$conn$>}, and the optional 1028\ml{[$dir$]} identifies which operand (left or right) contains the quantifier. 1029The complete set is: 1030 1031\begin{hol}\begin{verbatim} 1032 NOT_FORALL_CONV |- ~(!x.P) = ?x.~P 1033 NOT_EXISTS_CONV |- ~(?x.P) = !x.~P 1034 EXISTS_NOT_CONV |- (?x.~P) = ~!x.P 1035 FORALL_NOT_CONV |- (!x.~P) = ~?x.P 1036\end{verbatim}\end{hol} 1037 1038\begin{hol}\begin{verbatim} 1039 FORALL_AND_CONV |- (!x. P /\ Q) = (!x.P) /\ (!x.Q) 1040 AND_FORALL_CONV |- (!x.P) /\ (!x.Q) = (!x. P /\ Q) 1041 LEFT_AND_FORALL_CONV |- (!x.P) /\ Q = (!x'. P[x'/x] /\ Q) 1042 RIGHT_AND_FORALL_CONV |- P /\ (!x.Q) = (!x'. P /\ Q[x'/x]) 1043\end{verbatim}\end{hol} 1044 1045\begin{hol}\begin{verbatim} 1046 EXISTS_OR_CONV |- (?x. P \/ Q) = (?x.P) \/ (?x.Q) 1047 OR_EXISTS_CONV |- (?x.P) \/ (?x.Q) = (?x. P \/ Q) 1048 LEFT_OR_EXISTS_CONV |- (?x.P) \/ Q = (?x'. P[x'/x] \/ Q) 1049 RIGHT_OR_EXISTS_CONV |- P \/ (?x.Q) = (?x'. P \/ Q[x'/x]) 1050\end{verbatim}\end{hol} 1051 1052\begin{hol}\begin{verbatim} 1053 FORALL_OR_CONV 1054 |- (!x.P \/ Q) = P \/ !x.Q [x not free in P] 1055 |- (!x.P \/ Q) = (!x.P) \/ Q [x not free in Q] 1056 |- (!x.P \/ Q) = (!x.P) \/ (!x.Q) [x not free in P or Q] 1057\end{verbatim}\end{hol} 1058 1059\begin{hol}\begin{verbatim} 1060 OR_FORALL_CONV 1061 |- (!x.P) \/ (!x.Q) = (!x.P \/ Q) [x not free in P or Q] 1062\end{verbatim}\end{hol} 1063 1064\begin{hol}\begin{verbatim} 1065 LEFT_OR_FORALL_CONV |- (!x.P) \/ Q = !x'. P[x'/x] \/ Q 1066 RIGHT_OR_FORALL_CONV |- P \/ (!x.Q) = !x'. P \/ Q[x'/x] 1067\end{verbatim}\end{hol} 1068 1069\begin{hol}\begin{verbatim} 1070 EXISTS_AND_CONV 1071 |- (?x.P /\ Q) = P /\ ?x.Q [x not free in P] 1072 |- (?x.P /\ Q) = (?x.P) /\ Q [x not free in Q] 1073 |- (?x.P /\ Q) = (?x.P) /\ (?x.Q) [x not free in P or Q] 1074\end{verbatim}\end{hol} 1075 1076\begin{hol}\begin{verbatim} 1077 AND_EXISTS_CONV 1078 |- (?x.P) /\ (?x.Q) = (?x.P /\ Q) [x not free in P or Q] 1079\end{verbatim}\end{hol} 1080 1081\begin{hol}\begin{verbatim} 1082 LEFT_AND_EXISTS_CONV |- (?x.P) /\ Q = ?x'. P[x'/x] /\ Q 1083 RIGHT_AND_EXISTS_CONV |- P /\ (?x.Q) = ?x'. P /\ Q[x'/x] 1084\end{verbatim}\end{hol} 1085 1086\begin{hol}\begin{verbatim} 1087 FORALL_IMP_CONV 1088 |- (!x.P ==> Q) = P ==> !x.Q [x not free in P] 1089 |- (!x.P ==> Q) = (?x.P) ==> Q [x not free in Q] 1090 |- (!x.P ==> Q) = (?x.P) ==> (!x.Q) [x not free in P or Q] 1091\end{verbatim}\end{hol} 1092 1093\begin{hol}\begin{verbatim} 1094 LEFT_IMP_FORALL_CONV |- (!x.P) ==> Q = !x'. P[x/'x] ==> Q 1095 RIGHT_IMP_FORALL_CONV |- P ==> (!x.Q) = !x'. P ==> Q[x'/x] 1096\end{verbatim}\end{hol} 1097 1098\begin{hol}\begin{verbatim} 1099 EXISTS_IMP_CONV 1100 |- (?x.P ==> Q) = P ==> ?x.Q [x not free in P] 1101 |- (?x.P ==> Q) = (!x.P) ==> Q [x not free in Q] 1102 |- (?x.P ==> Q) = (!x.P) ==> (?x.Q) [x not free in P or Q] 1103\end{verbatim}\end{hol} 1104 1105\begin{hol}\begin{verbatim} 1106 LEFT_IMP_EXISTS_CONV |- (?x.P) ==> Q = !x'. P[x/'x] ==> Q 1107 RIGHT_IMP_EXISTS_CONV |- P ==> (?x.Q) = ?x'. P ==> Q[x'/x] 1108\end{verbatim}\end{hol} 1109 1110 1111\section{Rewriting tools} 1112\label{avra3} 1113 1114The rewriting tool\index{rewriting!as based on conversions|(} 1115\ml{REWRITE\_RULE} was introduced 1116in Chapter~\ref{derived-rules}. 1117There are also rewriting conversion like \ml{REWRITE\_CONV}. 1118All of the various rewriting tools provided in \HOL\ 1119are implemented by use of conversions. 1120Certain new tools could also be built in a similar way. 1121 1122The rewriting primitive in \HOL\ is \ml{REWR\_CONV}: 1123 1124\begin{holboxed} 1125\index{REWR_CONV@\ml{REWR\_CONV}|pin} 1126\begin{verbatim} 1127 REWR_CONV : thm -> conv 1128\end{verbatim}\end{holboxed} 1129 1130\noindent $\ml{REWR\_CONV (}\Gamma\ml{ |- }u \ml{=}v\ml{) }t$ evaluates to a 1131theorem $\Gamma\ml{ |- }t\ml{=}t'$ 1132if $t$ is an instance (by type and/or variable instantiation) 1133of $u$ and $t'$ is the corresponding instance of $v$. 1134The first argument to \ml{REWR\_CONV} can be quantified. 1135Below is an illustration. 1136 1137\setcounter{sessioncount}{1} 1138\begin{session}\begin{verbatim} 1139#REWR_CONV ADD1 "SUC 0";; 1140Theorem ADD1 autoloaded from theory `arithmetic`. 1141ADD1 = |- !m. SUC m = m + 1 1142 1143|- SUC 0 = 0 + 1 1144\end{verbatim}\end{session} 1145 1146\noindent All subterms of $t$ can be rewritten according to 1147an equation $th$ using\index{rewriting!use of DEPTH_CONV in@use of \ml{DEPTH\_CONV} in}\index{DEPTH_CONV@\ml{DEPTH\_CONV}!use of, in rewriting tools} 1148 1149\[ \ml{DEPTH\_CONV(REWR\_CONV}\;\;th\ml{)} \] 1150 1151\noindent as shown below. The function \ml{"PRE"} is the usual 1152predecessor function. 1153 1154\begin{session}\begin{verbatim} 1155#DEPTH_CONV (REWR_CONV ADD1) "SUC(SUC 0) = PRE(SUC 2)";; 1156|- (SUC(SUC 0) = PRE(SUC 2)) = ((0 + 1) + 1 = PRE(2 + 1)) 1157\end{verbatim}\end{session} 1158 1159In itself, this is not a very useful rewriting tool, but a collection 1160of others have been developed for use in \HOL. 1161All of the rewriting tools are, in fact, logically derived, and are 1162based on conversions similar to \ml{DEPTH\_CONV}. 1163They have been optimized in various ways, so their 1164implementation is in some cases rather complex and is not given here. 1165The conversions, rules and tactics for rewriting all take a list of 1166theorems\index{theorems, in HOL logic@theorems, in \HOL\ logic!as rewrite rules} 1167to be used as rewrites. 1168The theorems in the list need not be in simple equational form 1169(\eg\ a conjunction of equations is permissible); but are 1170converted to equational form 1171automatically (and internally). 1172(For example, a conjunction of equations is split into 1173its constituent conjuncts.) There are also a number 1174of standard equations (representing common tautologies) held in 1175the \ML\ variable 1176\ml{basic\_rewrites},\index{basic-rewrites@\ml{basic-rewrites}} and these 1177are used by some of the rewriting tools. All the 1178built-in rewriting tools are listed below, for 1179reference, beginning with the rules. 1180(All are fully 1181described in \REFERENCE.) 1182 1183The prefix `\ml{PURE\_}' indicates 1184that the built-in equations in \ml{basic\_rewrites} are not used, 1185(\ie\ only those 1186given explicitly are used). The prefix `\ml{ONCE\_}' indicates that the 1187tool 1188makes only one rewriting pass through the expression 1189(this is useful to avoid divergence). It is based on \ml{ONCE\_DEPTH\_CONV}, 1190while the other tools traverse using \ml{TOP\_DEPTH\_CONV}. 1191 1192The rewriting converions are: 1193 1194\begin{hol}\begin{verbatim} 1195 REWRITE_CONV : thm list -> conv 1196 PURE_REWRITE_CONV : thm list -> conv 1197 ONCE_REWRITE_CONV : thm list -> conv 1198 PURE_ONCE_REWRITE_CONV : thm list -> conv 1199\end{verbatim}\end{hol} 1200 1201The basic rewriting rules are: 1202 1203\begin{hol}\begin{verbatim} 1204 REWRITE_RULE : thm list -> thm -> thm 1205 PURE_REWRITE_RULE : thm list -> thm -> thm 1206 ONCE_REWRITE_RULE : thm list -> thm -> thm 1207 PURE_ONCE_REWRITE_RULE : thm list -> thm -> thm 1208\end{verbatim}\end{hol} 1209 1210\noindent The prefix `\ml{ASM\_}' 1211indicates that the rule rewrites using the assumptions 1212of the theorem as rewrites. 1213 1214\begin{hol}\begin{verbatim} 1215 ASM_REWRITE_RULE : thm list -> thm -> thm 1216 PURE_ASM_REWRITE_RULE : thm list -> thm -> thm 1217 ONCE_ASM_REWRITE_RULE : thm list -> thm -> thm 1218 PURE_ONCE_ASM_REWRITE_RULE : thm list -> thm -> thm 1219\end{verbatim}\end{hol} 1220 1221\noindent The prefix `\ml{FILTER\_}' 1222indicates that the rule only rewrites with 1223those assumptions of the theorem satisfying the predicate supplied. 1224 1225\begin{hol}\begin{verbatim} 1226 FILTER_ASM_REWRITE_RULE : (term -> bool) -> thm list -> thm -> thm 1227 FILTER_PURE_ASM_REWRITE_RULE : (term -> bool) -> thm list -> thm -> thm 1228 FILTER_ONCE_ASM_REWRITE_RULE : (term -> bool) -> thm list -> thm -> thm 1229 FILTER_PURE_ONCE_ASM_REWRITE_RULE : (term -> bool) -> thm list -> thm -> thm 1230\end{verbatim}\end{hol} 1231 1232\noindent Tactics\index{rewriting!list of tactics for|(} are introduced in Chapter~\ref{tactics-and-tacticals}, 1233but are listed here for reference. 1234The tactics corresponding to the above rules are the following: 1235 1236\begin{hol}\begin{verbatim} 1237 REWRITE_TAC : thm list -> tactic 1238 PURE_REWRITE_TAC : thm list -> tactic 1239 ONCE_REWRITE_TAC : thm list -> tactic 1240 PURE_ONCE_REWRITE_TAC : thm list -> tactic 1241\end{verbatim}\end{hol} 1242 1243\noindent The prefix `\ml{ASM\_}' 1244indicates that the tactic rewrites using the assumptions 1245of the goal as rewrites. 1246 1247\begin{hol}\index{ASM_REWRITE_TAC@\ml{ASM\_REWRITE\_TAC}} 1248\begin{verbatim} 1249 ASM_REWRITE_TAC : thm list -> tactic 1250 PURE_ASM_REWRITE_TAC : thm list -> tactic 1251 ONCE_ASM_REWRITE_TAC : thm list -> tactic 1252 PURE_ONCE_ASM_REWRITE_TAC : thm list -> tactic 1253\end{verbatim}\end{hol} 1254 1255\noindent The prefix `\ml{FILTER\_}' 1256indicates that the tactic only rewrites with 1257those assumptions of the goal satisfying the predicate supplied. 1258 1259\begin{hol}\begin{verbatim} 1260 FILTER_ASM_REWRITE_TAC : (term -> bool) -> thm list -> tactic 1261 FILTER_PURE_ASM_REWRITE_TAC : (term -> bool) -> thm list -> tactic 1262 FILTER_ONCE_ASM_REWRITE_TAC : (term -> bool) -> thm list -> tactic 1263 FILTER_PURE_ONCE_ASM_REWRITE_TAC : (term -> bool) -> thm list -> tactic 1264\end{verbatim}\end{hol} 1265\index{rewriting!as based on conversions|)} 1266\index{rewriting!list of tactics for|)} 1267 1268%%% Local Variables: 1269%%% mode: latex 1270%%% TeX-master: "description" 1271%%% End: 1272