| author | nipkow | 
| Thu, 06 Jul 2000 15:38:42 +0200 | |
| changeset 9270 | 7eff23d0b380 | 
| parent 9258 | 2121ff73a37d | 
| child 9695 | ec7d7f877712 | 
| permissions | -rw-r--r-- | 
| 6580 | 1  | 
%% $Id$  | 
2  | 
\chapter{Higher-Order Logic}
 | 
|
3  | 
\index{higher-order logic|(}
 | 
|
4  | 
\index{HOL system@{\sc hol} system}
 | 
|
5  | 
||
6  | 
The theory~\thydx{HOL} implements higher-order logic.  It is based on
 | 
|
7  | 
Gordon's~{\sc hol} system~\cite{mgordon-hol}, which itself is based on
 | 
|
8  | 
Church's original paper~\cite{church40}.  Andrews's
 | 
|
9  | 
book~\cite{andrews86} is a full description of the original
 | 
|
10  | 
Church-style higher-order logic.  Experience with the {\sc hol} system
 | 
|
11  | 
has demonstrated that higher-order logic is widely applicable in many  | 
|
12  | 
areas of mathematics and computer science, not just hardware  | 
|
| 7490 | 13  | 
verification, {\sc hol}'s original \textit{raison d'{\^e}tre\/}.  It is
 | 
| 6580 | 14  | 
weaker than {\ZF} set theory but for most applications this does not
 | 
15  | 
matter.  If you prefer {\ML} to Lisp, you will probably prefer \HOL\ 
 | 
|
16  | 
to~{\ZF}.
 | 
|
17  | 
||
18  | 
The syntax of \HOL\footnote{Earlier versions of Isabelle's \HOL\ used a
 | 
|
19  | 
different syntax. Ancient releases of Isabelle included still another version  | 
|
20  | 
of~\HOL, with explicit type inference rules~\cite{paulson-COLOG}.  This
 | 
|
21  | 
version no longer exists, but \thydx{ZF} supports a similar style of
 | 
|
22  | 
reasoning.} follows $\lambda$-calculus and functional programming. Function  | 
|
23  | 
application is curried. To apply the function~$f$ of type  | 
|
24  | 
$\tau@1\To\tau@2\To\tau@3$ to the arguments~$a$ and~$b$ in \HOL, you simply  | 
|
25  | 
write $f\,a\,b$.  There is no `apply' operator as in \thydx{ZF}.  Note that
 | 
|
26  | 
$f(a,b)$ means ``$f$ applied to the pair $(a,b)$'' in \HOL. We write ordered  | 
|
27  | 
pairs as $(a,b)$, not $\langle a,b\rangle$ as in {\ZF}.
 | 
|
28  | 
||
29  | 
\HOL\ has a distinct feel, compared with {\ZF} and {\CTT}.  It
 | 
|
30  | 
identifies object-level types with meta-level types, taking advantage of  | 
|
31  | 
Isabelle's built-in type-checker. It identifies object-level functions  | 
|
32  | 
with meta-level functions, so it uses Isabelle's operations for abstraction  | 
|
33  | 
and application.  | 
|
34  | 
||
35  | 
These identifications allow Isabelle to support \HOL\ particularly  | 
|
36  | 
nicely, but they also mean that \HOL\ requires more sophistication  | 
|
37  | 
from the user --- in particular, an understanding of Isabelle's type  | 
|
38  | 
system.  Beginners should work with \texttt{show_types} (or even
 | 
|
39  | 
\texttt{show_sorts}) set to \texttt{true}.
 | 
|
40  | 
% Gain experience by  | 
|
41  | 
%working in first-order logic before attempting to use higher-order logic.  | 
|
42  | 
%This chapter assumes familiarity with~{\FOL{}}.
 | 
|
43  | 
||
44  | 
||
45  | 
\begin{figure}
 | 
|
46  | 
\begin{constants}
 | 
|
47  | 
\it name &\it meta-type & \it description \\  | 
|
48  | 
  \cdx{Trueprop}& $bool\To prop$                & coercion to $prop$\\
 | 
|
| 7490 | 49  | 
  \cdx{Not}     & $bool\To bool$                & negation ($\lnot$) \\
 | 
| 6580 | 50  | 
  \cdx{True}    & $bool$                        & tautology ($\top$) \\
 | 
51  | 
  \cdx{False}   & $bool$                        & absurdity ($\bot$) \\
 | 
|
52  | 
  \cdx{If}      & $[bool,\alpha,\alpha]\To\alpha$ & conditional \\
 | 
|
53  | 
  \cdx{Let}     & $[\alpha,\alpha\To\beta]\To\beta$ & let binder
 | 
|
54  | 
\end{constants}
 | 
|
55  | 
\subcaption{Constants}
 | 
|
56  | 
||
57  | 
\begin{constants}
 | 
|
58  | 
\index{"@@{\tt\at} symbol}
 | 
|
59  | 
\index{*"! symbol}\index{*"? symbol}
 | 
|
60  | 
\index{*"?"! symbol}\index{*"E"X"! symbol}
 | 
|
61  | 
\it symbol &\it name &\it meta-type & \it description \\  | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
62  | 
  \sdx{SOME} or \tt\at & \cdx{Eps}  & $(\alpha\To bool)\To\alpha$ & 
 | 
| 6580 | 63  | 
Hilbert description ($\varepsilon$) \\  | 
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
64  | 
  \sdx{ALL} or {\tt!~} & \cdx{All}  & $(\alpha\To bool)\To bool$ & 
 | 
| 6580 | 65  | 
universal quantifier ($\forall$) \\  | 
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
66  | 
  \sdx{EX} or {\tt?~}  & \cdx{Ex}   & $(\alpha\To bool)\To bool$ & 
 | 
| 6580 | 67  | 
existential quantifier ($\exists$) \\  | 
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
68  | 
  \texttt{EX!} or {\tt?!} & \cdx{Ex1}  & $(\alpha\To bool)\To bool$ & 
 | 
| 6580 | 69  | 
unique existence ($\exists!$)\\  | 
70  | 
  \texttt{LEAST}  & \cdx{Least}  & $(\alpha::ord \To bool)\To\alpha$ & 
 | 
|
71  | 
least element  | 
|
72  | 
\end{constants}
 | 
|
73  | 
\subcaption{Binders} 
 | 
|
74  | 
||
75  | 
\begin{constants}
 | 
|
76  | 
\index{*"= symbol}
 | 
|
77  | 
\index{&@{\tt\&} symbol}
 | 
|
78  | 
\index{*"| symbol}
 | 
|
79  | 
\index{*"-"-"> symbol}
 | 
|
80  | 
\it symbol & \it meta-type & \it priority & \it description \\  | 
|
81  | 
  \sdx{o}       & $[\beta\To\gamma,\alpha\To\beta]\To (\alpha\To\gamma)$ & 
 | 
|
82  | 
Left 55 & composition ($\circ$) \\  | 
|
83  | 
\tt = & $[\alpha,\alpha]\To bool$ & Left 50 & equality ($=$) \\  | 
|
84  | 
\tt < & $[\alpha::ord,\alpha]\To bool$ & Left 50 & less than ($<$) \\  | 
|
85  | 
\tt <= & $[\alpha::ord,\alpha]\To bool$ & Left 50 &  | 
|
86  | 
less than or equals ($\leq$)\\  | 
|
87  | 
\tt \& & $[bool,bool]\To bool$ & Right 35 & conjunction ($\conj$) \\  | 
|
88  | 
\tt | & $[bool,bool]\To bool$ & Right 30 & disjunction ($\disj$) \\  | 
|
89  | 
\tt --> & $[bool,bool]\To bool$ & Right 25 & implication ($\imp$)  | 
|
90  | 
\end{constants}
 | 
|
91  | 
\subcaption{Infixes}
 | 
|
92  | 
\caption{Syntax of \texttt{HOL}} \label{hol-constants}
 | 
|
93  | 
\end{figure}
 | 
|
94  | 
||
95  | 
||
96  | 
\begin{figure}
 | 
|
97  | 
\index{*let symbol}
 | 
|
98  | 
\index{*in symbol}
 | 
|
99  | 
\dquotes  | 
|
100  | 
\[\begin{array}{rclcl}
 | 
|
101  | 
    term & = & \hbox{expression of class~$term$} \\
 | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
102  | 
& | & "SOME~" id " . " formula  | 
| 6580 | 103  | 
& | & "\at~" id " . " formula \\  | 
104  | 
& | &  | 
|
105  | 
    \multicolumn{3}{l}{"let"~id~"="~term";"\dots";"~id~"="~term~"in"~term} \\
 | 
|
106  | 
& | &  | 
|
107  | 
    \multicolumn{3}{l}{"if"~formula~"then"~term~"else"~term} \\
 | 
|
108  | 
& | & "LEAST"~ id " . " formula \\[2ex]  | 
|
109  | 
 formula & = & \hbox{expression of type~$bool$} \\
 | 
|
110  | 
& | & term " = " term \\  | 
|
111  | 
& | & term " \ttilde= " term \\  | 
|
112  | 
& | & term " < " term \\  | 
|
113  | 
& | & term " <= " term \\  | 
|
114  | 
& | & "\ttilde\ " formula \\  | 
|
115  | 
& | & formula " \& " formula \\  | 
|
116  | 
& | & formula " | " formula \\  | 
|
117  | 
& | & formula " --> " formula \\  | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
118  | 
& | & "ALL~" id~id^* " . " formula  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
119  | 
& | & "!~~~" id~id^* " . " formula \\  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
120  | 
& | & "EX~~" id~id^* " . " formula  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
121  | 
& | & "?~~~" id~id^* " . " formula \\  | 
| 6580 | 122  | 
& | & "EX!~" id~id^* " . " formula  | 
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
123  | 
& | & "?!~~" id~id^* " . " formula \\  | 
| 6580 | 124  | 
  \end{array}
 | 
125  | 
\]  | 
|
126  | 
\caption{Full grammar for \HOL} \label{hol-grammar}
 | 
|
127  | 
\end{figure} 
 | 
|
128  | 
||
129  | 
||
130  | 
\section{Syntax}
 | 
|
131  | 
||
132  | 
Figure~\ref{hol-constants} lists the constants (including infixes and
 | 
|
133  | 
binders), while Fig.\ts\ref{hol-grammar} presents the grammar of
 | 
|
134  | 
higher-order logic. Note that $a$\verb|~=|$b$ is translated to  | 
|
| 7490 | 135  | 
$\lnot(a=b)$.  | 
| 6580 | 136  | 
|
137  | 
\begin{warn}
 | 
|
138  | 
\HOL\ has no if-and-only-if connective; logical equivalence is expressed  | 
|
139  | 
using equality. But equality has a high priority, as befitting a  | 
|
140  | 
relation, while if-and-only-if typically has the lowest priority. Thus,  | 
|
| 7490 | 141  | 
$\lnot\lnot P=P$ abbreviates $\lnot\lnot (P=P)$ and not $(\lnot\lnot P)=P$.  | 
| 6580 | 142  | 
When using $=$ to mean logical equivalence, enclose both operands in  | 
143  | 
parentheses.  | 
|
144  | 
\end{warn}
 | 
|
145  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
146  | 
\subsection{Types and overloading}
 | 
| 6580 | 147  | 
The universal type class of higher-order terms is called~\cldx{term}.
 | 
148  | 
By default, explicit type variables have class \cldx{term}.  In
 | 
|
149  | 
particular the equality symbol and quantifiers are polymorphic over  | 
|
150  | 
class \texttt{term}.
 | 
|
151  | 
||
152  | 
The type of formulae, \tydx{bool}, belongs to class \cldx{term}; thus,
 | 
|
153  | 
formulae are terms.  The built-in type~\tydx{fun}, which constructs
 | 
|
154  | 
function types, is overloaded with arity {\tt(term,\thinspace
 | 
|
155  | 
  term)\thinspace term}.  Thus, $\sigma\To\tau$ belongs to class~{\tt
 | 
|
156  | 
term} if $\sigma$ and~$\tau$ do, allowing quantification over  | 
|
157  | 
functions.  | 
|
158  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
159  | 
\HOL\ allows new types to be declared as subsets of existing types;  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
160  | 
see~{\S}\ref{sec:HOL:Types}.  ML-like datatypes can also be declared; see
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
161  | 
~{\S}\ref{sec:HOL:datatype}.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
162  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
163  | 
Several syntactic type classes --- \cldx{plus}, \cldx{minus},
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
164  | 
\cldx{times} and
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
165  | 
\cldx{power} --- permit overloading of the operators {\tt+},\index{*"+
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
166  | 
  symbol} {\tt-}\index{*"- symbol}, {\tt*}.\index{*"* symbol} 
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
167  | 
and \verb|^|.\index{^@\verb.^. symbol} 
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
168  | 
%  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
169  | 
They are overloaded to denote the obvious arithmetic operations on types  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
170  | 
\tdx{nat}, \tdx{int} and~\tdx{real}. (With the \verb|^| operator, the
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
171  | 
exponent always has type~\tdx{nat}.)  Non-arithmetic overloadings are also
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
172  | 
done: the operator {\tt-} can denote set difference, while \verb|^| can
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
173  | 
denote exponentiation of relations (iterated composition). Unary minus is  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
174  | 
also written as~{\tt-} and is overloaded like its 2-place counterpart; it even
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
175  | 
can stand for set complement.  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
176  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
177  | 
The constant \cdx{0} is also overloaded.  It serves as the zero element of
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
178  | 
several types, of which the most important is \tdx{nat} (the natural
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
179  | 
numbers).  The type class \cldx{plus_ac0} comprises all types for which 0
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
180  | 
and~+ satisfy the laws $x+y=y+x$, $(x+y)+z = x+(y+z)$ and $0+x = x$. These  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
181  | 
types include the numeric ones \tdx{nat}, \tdx{int} and~\tdx{real} and also
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
182  | 
multisets.  The summation operator \cdx{setsum} is available for all types in
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
183  | 
this class.  | 
| 6580 | 184  | 
|
185  | 
Theory \thydx{Ord} defines the syntactic class \cldx{ord} of order
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
186  | 
signatures. The relations $<$ and $\leq$ are polymorphic over this  | 
| 6580 | 187  | 
class, as are the functions \cdx{mono}, \cdx{min} and \cdx{max}, and
 | 
188  | 
the \cdx{LEAST} operator. \thydx{Ord} also defines a subclass
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
189  | 
\cldx{order} of \cldx{ord} which axiomatizes the types that are partially
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
190  | 
ordered with respect to~$\leq$.  A further subclass \cldx{linorder} of
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
191  | 
\cldx{order} axiomatizes linear orderings.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
192  | 
For details, see the file \texttt{Ord.thy}.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
193  | 
|
| 6580 | 194  | 
If you state a goal containing overloaded functions, you may need to include  | 
195  | 
type constraints. Type inference may otherwise make the goal more  | 
|
196  | 
polymorphic than you intended, with confusing results. For example, the  | 
|
| 7490 | 197  | 
variables $i$, $j$ and $k$ in the goal $i \leq j \Imp i \leq j+k$ have type  | 
| 6580 | 198  | 
$\alpha::\{ord,plus\}$, although you may have expected them to have some
 | 
199  | 
numeric type, e.g. $nat$. Instead you should have stated the goal as  | 
|
| 7490 | 200  | 
$(i::nat) \leq j \Imp i \leq j+k$, which causes all three variables to have  | 
| 6580 | 201  | 
type $nat$.  | 
202  | 
||
203  | 
\begin{warn}
 | 
|
204  | 
If resolution fails for no obvious reason, try setting  | 
|
205  | 
  \ttindex{show_types} to \texttt{true}, causing Isabelle to display
 | 
|
206  | 
  types of terms.  Possibly set \ttindex{show_sorts} to \texttt{true} as
 | 
|
207  | 
well, causing Isabelle to display type classes and sorts.  | 
|
208  | 
||
209  | 
  \index{unification!incompleteness of}
 | 
|
210  | 
Where function types are involved, Isabelle's unification code does not  | 
|
211  | 
guarantee to find instantiations for type variables automatically. Be  | 
|
212  | 
  prepared to use \ttindex{res_inst_tac} instead of \texttt{resolve_tac},
 | 
|
213  | 
possibly instantiating type variables. Setting  | 
|
214  | 
  \ttindex{Unify.trace_types} to \texttt{true} causes Isabelle to report
 | 
|
215  | 
  omitted search paths during unification.\index{tracing!of unification}
 | 
|
216  | 
\end{warn}
 | 
|
217  | 
||
218  | 
||
219  | 
\subsection{Binders}
 | 
|
220  | 
||
221  | 
Hilbert's {\bf description} operator~$\varepsilon x. P[x]$ stands for
 | 
|
222  | 
some~$x$ satisfying~$P$, if such exists. Since all terms in \HOL\  | 
|
223  | 
denote something, a description is always meaningful, but we do not  | 
|
224  | 
know its value unless $P$ defines it uniquely. We may write  | 
|
225  | 
descriptions as \cdx{Eps}($\lambda x. P[x]$) or use the syntax
 | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
226  | 
\hbox{\tt SOME~$x$.~$P[x]$}.
 | 
| 6580 | 227  | 
|
228  | 
Existential quantification is defined by  | 
|
229  | 
\[ \exists x. P~x \;\equiv\; P(\varepsilon x. P~x). \]  | 
|
230  | 
The unique existence quantifier, $\exists!x. P$, is defined in terms  | 
|
231  | 
of~$\exists$ and~$\forall$. An Isabelle binder, it admits nested  | 
|
232  | 
quantifications. For instance, $\exists!x\,y. P\,x\,y$ abbreviates  | 
|
233  | 
$\exists!x. \exists!y. P\,x\,y$; note that this does not mean that there  | 
|
234  | 
exists a unique pair $(x,y)$ satisfying~$P\,x\,y$.  | 
|
235  | 
||
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
236  | 
\medskip  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
237  | 
|
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
238  | 
\index{*"! symbol}\index{*"? symbol}\index{HOL system@{\sc hol} system} The
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
239  | 
basic Isabelle/HOL binders have two notations. Apart from the usual  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
240  | 
\texttt{ALL} and \texttt{EX} for $\forall$ and $\exists$, Isabelle/HOL also
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
241  | 
supports the original notation of Gordon's {\sc hol} system: \texttt{!}\ 
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
242  | 
and~\texttt{?}.  In the latter case, the existential quantifier \emph{must} be
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
243  | 
followed by a space; thus {\tt?x} is an unknown, while \verb'? x. f x=y' is a
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
244  | 
quantification. Both notations are accepted for input. The print mode  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
245  | 
``\ttindexbold{HOL}'' governs the output notation.  If enabled (e.g.\ by
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
246  | 
passing option \texttt{-m HOL} to the \texttt{isabelle} executable),
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
247  | 
then~{\tt!}\ and~{\tt?}\ are displayed.
 | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
248  | 
|
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
249  | 
\medskip  | 
| 6580 | 250  | 
|
251  | 
If $\tau$ is a type of class \cldx{ord}, $P$ a formula and $x$ a
 | 
|
252  | 
variable of type $\tau$, then the term \cdx{LEAST}~$x. P[x]$ is defined
 | 
|
| 7490 | 253  | 
to be the least (w.r.t.\ $\leq$) $x$ such that $P~x$ holds (see  | 
| 6580 | 254  | 
Fig.~\ref{hol-defs}).  The definition uses Hilbert's $\varepsilon$
 | 
255  | 
choice operator, so \texttt{Least} is always meaningful, but may yield
 | 
|
256  | 
nothing useful in case there is not a unique least element satisfying  | 
|
257  | 
$P$.\footnote{Class $ord$ does not require much of its instances, so
 | 
|
| 7490 | 258  | 
$\leq$ need not be a well-ordering, not even an order at all!}  | 
| 6580 | 259  | 
|
260  | 
\medskip All these binders have priority 10.  | 
|
261  | 
||
262  | 
\begin{warn}
 | 
|
263  | 
The low priority of binders means that they need to be enclosed in  | 
|
264  | 
parenthesis when they occur in the context of other operations. For example,  | 
|
265  | 
instead of $P \land \forall x. Q$ you need to write $P \land (\forall x. Q)$.  | 
|
266  | 
\end{warn}
 | 
|
267  | 
||
268  | 
||
| 6620 | 269  | 
\subsection{The let and case constructions}
 | 
| 6580 | 270  | 
Local abbreviations can be introduced by a \texttt{let} construct whose
 | 
271  | 
syntax appears in Fig.\ts\ref{hol-grammar}.  Internally it is translated into
 | 
|
272  | 
the constant~\cdx{Let}.  It can be expanded by rewriting with its
 | 
|
273  | 
definition, \tdx{Let_def}.
 | 
|
274  | 
||
275  | 
\HOL\ also defines the basic syntax  | 
|
276  | 
\[\dquotes"case"~e~"of"~c@1~"=>"~e@1~"|" \dots "|"~c@n~"=>"~e@n\]  | 
|
277  | 
as a uniform means of expressing \texttt{case} constructs.  Therefore \texttt{case}
 | 
|
278  | 
and \sdx{of} are reserved words.  Initially, this is mere syntax and has no
 | 
|
279  | 
logical meaning. By declaring translations, you can cause instances of the  | 
|
280  | 
\texttt{case} construct to denote applications of particular case operators.
 | 
|
281  | 
This is what happens automatically for each \texttt{datatype} definition
 | 
|
| 7490 | 282  | 
(see~{\S}\ref{sec:HOL:datatype}).
 | 
| 6580 | 283  | 
|
284  | 
\begin{warn}
 | 
|
285  | 
Both \texttt{if} and \texttt{case} constructs have as low a priority as
 | 
|
286  | 
quantifiers, which requires additional enclosing parentheses in the context  | 
|
287  | 
of most other operations.  For example, instead of $f~x = {\tt if\dots
 | 
|
288  | 
then\dots else}\dots$ you need to write $f~x = ({\tt if\dots then\dots
 | 
|
289  | 
else\dots})$.  | 
|
290  | 
\end{warn}
 | 
|
291  | 
||
292  | 
\section{Rules of inference}
 | 
|
293  | 
||
294  | 
\begin{figure}
 | 
|
295  | 
\begin{ttbox}\makeatother
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
296  | 
\tdx{refl}          t = (t::'a)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
297  | 
\tdx{subst}         [| s = t; P s |] ==> P (t::'a)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
298  | 
\tdx{ext}           (!!x::'a. (f x :: 'b) = g x) ==> (\%x. f x) = (\%x. g x)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
299  | 
\tdx{impI}          (P ==> Q) ==> P-->Q
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
300  | 
\tdx{mp}            [| P-->Q;  P |] ==> Q
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
301  | 
\tdx{iff}           (P-->Q) --> (Q-->P) --> (P=Q)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
302  | 
\tdx{selectI}       P(x::'a) ==> P(@x. P x)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
303  | 
\tdx{True_or_False} (P=True) | (P=False)
 | 
| 6580 | 304  | 
\end{ttbox}
 | 
305  | 
\caption{The \texttt{HOL} rules} \label{hol-rules}
 | 
|
306  | 
\end{figure}
 | 
|
307  | 
||
308  | 
Figure~\ref{hol-rules} shows the primitive inference rules of~\HOL{},
 | 
|
309  | 
with their~{\ML} names.  Some of the rules deserve additional
 | 
|
310  | 
comments:  | 
|
311  | 
\begin{ttdescription}
 | 
|
312  | 
\item[\tdx{ext}] expresses extensionality of functions.
 | 
|
313  | 
\item[\tdx{iff}] asserts that logically equivalent formulae are
 | 
|
314  | 
equal.  | 
|
315  | 
\item[\tdx{selectI}] gives the defining property of the Hilbert
 | 
|
316  | 
$\varepsilon$-operator. It is a form of the Axiom of Choice. The derived rule  | 
|
317  | 
  \tdx{select_equality} (see below) is often easier to use.
 | 
|
318  | 
\item[\tdx{True_or_False}] makes the logic classical.\footnote{In
 | 
|
319  | 
fact, the $\varepsilon$-operator already makes the logic classical, as  | 
|
320  | 
    shown by Diaconescu; see Paulson~\cite{paulson-COLOG} for details.}
 | 
|
321  | 
\end{ttdescription}
 | 
|
322  | 
||
323  | 
||
324  | 
\begin{figure}\hfuzz=4pt%suppress "Overfull \hbox" message
 | 
|
325  | 
\begin{ttbox}\makeatother
 | 
|
326  | 
\tdx{True_def}   True     == ((\%x::bool. x)=(\%x. x))
 | 
|
327  | 
\tdx{All_def}    All      == (\%P. P = (\%x. True))
 | 
|
328  | 
\tdx{Ex_def}     Ex       == (\%P. P(@x. P x))
 | 
|
329  | 
\tdx{False_def}  False    == (!P. P)
 | 
|
330  | 
\tdx{not_def}    not      == (\%P. P-->False)
 | 
|
331  | 
\tdx{and_def}    op &     == (\%P Q. !R. (P-->Q-->R) --> R)
 | 
|
332  | 
\tdx{or_def}     op |     == (\%P Q. !R. (P-->R) --> (Q-->R) --> R)
 | 
|
333  | 
\tdx{Ex1_def}    Ex1      == (\%P. ? x. P x & (! y. P y --> y=x))
 | 
|
334  | 
||
335  | 
\tdx{o_def}      op o     == (\%(f::'b=>'c) g x::'a. f(g x))
 | 
|
336  | 
\tdx{if_def}     If P x y ==
 | 
|
337  | 
(\%P x y. @z::'a.(P=True --> z=x) & (P=False --> z=y))  | 
|
338  | 
\tdx{Let_def}    Let s f  == f s
 | 
|
339  | 
\tdx{Least_def}  Least P  == @x. P(x) & (ALL y. P(y) --> x <= y)"
 | 
|
340  | 
\end{ttbox}
 | 
|
341  | 
\caption{The \texttt{HOL} definitions} \label{hol-defs}
 | 
|
342  | 
\end{figure}
 | 
|
343  | 
||
344  | 
||
345  | 
\HOL{} follows standard practice in higher-order logic: only a few
 | 
|
346  | 
connectives are taken as primitive, with the remainder defined obscurely  | 
|
347  | 
(Fig.\ts\ref{hol-defs}).  Gordon's {\sc hol} system expresses the
 | 
|
348  | 
corresponding definitions \cite[page~270]{mgordon-hol} using
 | 
|
349  | 
object-equality~({\tt=}), which is possible because equality in
 | 
|
350  | 
higher-order logic may equate formulae and even functions over formulae.  | 
|
351  | 
But theory~\HOL{}, like all other Isabelle theories, uses
 | 
|
352  | 
meta-equality~({\tt==}) for definitions.
 | 
|
353  | 
\begin{warn}
 | 
|
354  | 
The definitions above should never be expanded and are shown for completeness  | 
|
355  | 
only. Instead users should reason in terms of the derived rules shown below  | 
|
356  | 
or, better still, using high-level tactics  | 
|
| 7490 | 357  | 
(see~{\S}\ref{sec:HOL:generic-packages}).
 | 
| 6580 | 358  | 
\end{warn}
 | 
359  | 
||
360  | 
Some of the rules mention type variables; for example, \texttt{refl}
 | 
|
361  | 
mentions the type variable~{\tt'a}.  This allows you to instantiate
 | 
|
362  | 
type variables explicitly by calling \texttt{res_inst_tac}.
 | 
|
363  | 
||
364  | 
||
365  | 
\begin{figure}
 | 
|
366  | 
\begin{ttbox}
 | 
|
367  | 
\tdx{sym}         s=t ==> t=s
 | 
|
368  | 
\tdx{trans}       [| r=s; s=t |] ==> r=t
 | 
|
369  | 
\tdx{ssubst}      [| t=s; P s |] ==> P t
 | 
|
370  | 
\tdx{box_equals}  [| a=b;  a=c;  b=d |] ==> c=d  
 | 
|
371  | 
\tdx{arg_cong}    x = y ==> f x = f y
 | 
|
372  | 
\tdx{fun_cong}    f = g ==> f x = g x
 | 
|
373  | 
\tdx{cong}        [| f = g; x = y |] ==> f x = g y
 | 
|
374  | 
\tdx{not_sym}     t ~= s ==> s ~= t
 | 
|
375  | 
\subcaption{Equality}
 | 
|
376  | 
||
377  | 
\tdx{TrueI}       True 
 | 
|
378  | 
\tdx{FalseE}      False ==> P
 | 
|
379  | 
||
380  | 
\tdx{conjI}       [| P; Q |] ==> P&Q
 | 
|
381  | 
\tdx{conjunct1}   [| P&Q |] ==> P
 | 
|
382  | 
\tdx{conjunct2}   [| P&Q |] ==> Q 
 | 
|
383  | 
\tdx{conjE}       [| P&Q;  [| P; Q |] ==> R |] ==> R
 | 
|
384  | 
||
385  | 
\tdx{disjI1}      P ==> P|Q
 | 
|
386  | 
\tdx{disjI2}      Q ==> P|Q
 | 
|
387  | 
\tdx{disjE}       [| P | Q; P ==> R; Q ==> R |] ==> R
 | 
|
388  | 
||
389  | 
\tdx{notI}        (P ==> False) ==> ~ P
 | 
|
390  | 
\tdx{notE}        [| ~ P;  P |] ==> R
 | 
|
391  | 
\tdx{impE}        [| P-->Q;  P;  Q ==> R |] ==> R
 | 
|
392  | 
\subcaption{Propositional logic}
 | 
|
393  | 
||
394  | 
\tdx{iffI}        [| P ==> Q;  Q ==> P |] ==> P=Q
 | 
|
395  | 
\tdx{iffD1}       [| P=Q; P |] ==> Q
 | 
|
396  | 
\tdx{iffD2}       [| P=Q; Q |] ==> P
 | 
|
397  | 
\tdx{iffE}        [| P=Q; [| P --> Q; Q --> P |] ==> R |] ==> R
 | 
|
398  | 
%  | 
|
399  | 
%\tdx{eqTrueI}     P ==> P=True 
 | 
|
400  | 
%\tdx{eqTrueE}     P=True ==> P 
 | 
|
401  | 
\subcaption{Logical equivalence}
 | 
|
402  | 
||
403  | 
\end{ttbox}
 | 
|
404  | 
\caption{Derived rules for \HOL} \label{hol-lemmas1}
 | 
|
405  | 
\end{figure}
 | 
|
406  | 
||
407  | 
||
408  | 
\begin{figure}
 | 
|
409  | 
\begin{ttbox}\makeatother
 | 
|
410  | 
\tdx{allI}      (!!x. P x) ==> !x. P x
 | 
|
411  | 
\tdx{spec}      !x. P x ==> P x
 | 
|
412  | 
\tdx{allE}      [| !x. P x;  P x ==> R |] ==> R
 | 
|
413  | 
\tdx{all_dupE}  [| !x. P x;  [| P x; !x. P x |] ==> R |] ==> R
 | 
|
414  | 
||
415  | 
\tdx{exI}       P x ==> ? x. P x
 | 
|
416  | 
\tdx{exE}       [| ? x. P x; !!x. P x ==> Q |] ==> Q
 | 
|
417  | 
||
418  | 
\tdx{ex1I}      [| P a;  !!x. P x ==> x=a |] ==> ?! x. P x
 | 
|
419  | 
\tdx{ex1E}      [| ?! x. P x;  !!x. [| P x;  ! y. P y --> y=x |] ==> R 
 | 
|
420  | 
|] ==> R  | 
|
421  | 
||
422  | 
\tdx{select_equality} [| P a;  !!x. P x ==> x=a |] ==> (@x. P x) = a
 | 
|
423  | 
\subcaption{Quantifiers and descriptions}
 | 
|
424  | 
||
425  | 
\tdx{ccontr}          (~P ==> False) ==> P
 | 
|
426  | 
\tdx{classical}       (~P ==> P) ==> P
 | 
|
427  | 
\tdx{excluded_middle} ~P | P
 | 
|
428  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
429  | 
\tdx{disjCI}       (~Q ==> P) ==> P|Q
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
430  | 
\tdx{exCI}         (! x. ~ P x ==> P a) ==> ? x. P x
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
431  | 
\tdx{impCE}        [| P-->Q; ~ P ==> R; Q ==> R |] ==> R
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
432  | 
\tdx{iffCE}        [| P=Q;  [| P;Q |] ==> R;  [| ~P; ~Q |] ==> R |] ==> R
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
433  | 
\tdx{notnotD}      ~~P ==> P
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
434  | 
\tdx{swap}         ~P ==> (~Q ==> P) ==> Q
 | 
| 6580 | 435  | 
\subcaption{Classical logic}
 | 
436  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
437  | 
\tdx{if_P}         P ==> (if P then x else y) = x
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
438  | 
\tdx{if_not_P}     ~ P ==> (if P then x else y) = y
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
439  | 
\tdx{split_if}     P(if Q then x else y) = ((Q --> P x) & (~Q --> P y))
 | 
| 6580 | 440  | 
\subcaption{Conditionals}
 | 
441  | 
\end{ttbox}
 | 
|
442  | 
\caption{More derived rules} \label{hol-lemmas2}
 | 
|
443  | 
\end{figure}
 | 
|
444  | 
||
445  | 
Some derived rules are shown in Figures~\ref{hol-lemmas1}
 | 
|
446  | 
and~\ref{hol-lemmas2}, with their {\ML} names.  These include natural rules
 | 
|
447  | 
for the logical connectives, as well as sequent-style elimination rules for  | 
|
448  | 
conjunctions, implications, and universal quantifiers.  | 
|
449  | 
||
450  | 
Note the equality rules: \tdx{ssubst} performs substitution in
 | 
|
451  | 
backward proofs, while \tdx{box_equals} supports reasoning by
 | 
|
452  | 
simplifying both sides of an equation.  | 
|
453  | 
||
454  | 
The following simple tactics are occasionally useful:  | 
|
455  | 
\begin{ttdescription}
 | 
|
456  | 
\item[\ttindexbold{strip_tac} $i$] applies \texttt{allI} and \texttt{impI}
 | 
|
457  | 
repeatedly to remove all outermost universal quantifiers and implications  | 
|
458  | 
from subgoal $i$.  | 
|
| 8443 | 459  | 
\item[\ttindexbold{case_tac} {\tt"}$P${\tt"} $i$] performs case distinction on
 | 
460  | 
$P$ for subgoal $i$: the latter is replaced by two identical subgoals with  | 
|
461  | 
the added assumptions $P$ and $\lnot P$, respectively.  | 
|
| 7490 | 462  | 
\item[\ttindexbold{smp_tac} $j$ $i$] applies $j$ times \texttt{spec} and then
 | 
463  | 
  \texttt{mp} in subgoal $i$, which is typically useful when forward-chaining 
 | 
|
464  | 
  from an induction hypothesis. As a generalization of \texttt{mp_tac}, 
 | 
|
465  | 
  if there are assumptions $\forall \vec{x}. P \vec{x} \imp Q \vec{x}$ and 
 | 
|
466  | 
  $P \vec{a}$, ($\vec{x}$ being a vector of $j$ variables)
 | 
|
467  | 
  then it replaces the universally quantified implication by $Q \vec{a}$. 
 | 
|
468  | 
It may instantiate unknowns. It fails if it can do nothing.  | 
|
| 6580 | 469  | 
\end{ttdescription}
 | 
470  | 
||
471  | 
||
472  | 
\begin{figure} 
 | 
|
473  | 
\begin{center}
 | 
|
474  | 
\begin{tabular}{rrr}
 | 
|
475  | 
\it name &\it meta-type & \it description \\  | 
|
476  | 
\index{{}@\verb'{}' symbol}
 | 
|
477  | 
  \verb|{}|     & $\alpha\,set$         & the empty set \\
 | 
|
478  | 
  \cdx{insert}  & $[\alpha,\alpha\,set]\To \alpha\,set$
 | 
|
479  | 
& insertion of element \\  | 
|
480  | 
  \cdx{Collect} & $(\alpha\To bool)\To\alpha\,set$
 | 
|
481  | 
& comprehension \\  | 
|
482  | 
  \cdx{INTER} & $[\alpha\,set,\alpha\To\beta\,set]\To\beta\,set$
 | 
|
483  | 
& intersection over a set\\  | 
|
484  | 
  \cdx{UNION} & $[\alpha\,set,\alpha\To\beta\,set]\To\beta\,set$
 | 
|
485  | 
& union over a set\\  | 
|
486  | 
  \cdx{Inter} & $(\alpha\,set)set\To\alpha\,set$
 | 
|
487  | 
&set of sets intersection \\  | 
|
488  | 
  \cdx{Union} & $(\alpha\,set)set\To\alpha\,set$
 | 
|
489  | 
&set of sets union \\  | 
|
490  | 
  \cdx{Pow}   & $\alpha\,set \To (\alpha\,set)set$
 | 
|
491  | 
& powerset \\[1ex]  | 
|
492  | 
  \cdx{range}   & $(\alpha\To\beta )\To\beta\,set$
 | 
|
493  | 
& range of a function \\[1ex]  | 
|
494  | 
  \cdx{Ball}~~\cdx{Bex} & $[\alpha\,set,\alpha\To bool]\To bool$
 | 
|
495  | 
& bounded quantifiers  | 
|
496  | 
\end{tabular}
 | 
|
497  | 
\end{center}
 | 
|
498  | 
\subcaption{Constants}
 | 
|
499  | 
||
500  | 
\begin{center}
 | 
|
501  | 
\begin{tabular}{llrrr} 
 | 
|
502  | 
\it symbol &\it name &\it meta-type & \it priority & \it description \\  | 
|
503  | 
  \sdx{INT}  & \cdx{INTER1}  & $(\alpha\To\beta\,set)\To\beta\,set$ & 10 & 
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
504  | 
intersection\\  | 
| 6580 | 505  | 
  \sdx{UN}  & \cdx{UNION1}  & $(\alpha\To\beta\,set)\To\beta\,set$ & 10 & 
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
506  | 
union  | 
| 6580 | 507  | 
\end{tabular}
 | 
508  | 
\end{center}
 | 
|
509  | 
\subcaption{Binders} 
 | 
|
510  | 
||
511  | 
\begin{center}
 | 
|
512  | 
\index{*"`"` symbol}
 | 
|
513  | 
\index{*": symbol}
 | 
|
514  | 
\index{*"<"= symbol}
 | 
|
515  | 
\begin{tabular}{rrrr} 
 | 
|
516  | 
\it symbol & \it meta-type & \it priority & \it description \\  | 
|
517  | 
\tt `` & $[\alpha\To\beta ,\alpha\,set]\To \beta\,set$  | 
|
518  | 
& Left 90 & image \\  | 
|
519  | 
  \sdx{Int}     & $[\alpha\,set,\alpha\,set]\To\alpha\,set$
 | 
|
520  | 
& Left 70 & intersection ($\int$) \\  | 
|
521  | 
  \sdx{Un}      & $[\alpha\,set,\alpha\,set]\To\alpha\,set$
 | 
|
522  | 
& Left 65 & union ($\un$) \\  | 
|
523  | 
\tt: & $[\alpha ,\alpha\,set]\To bool$  | 
|
524  | 
& Left 50 & membership ($\in$) \\  | 
|
525  | 
\tt <= & $[\alpha\,set,\alpha\,set]\To bool$  | 
|
526  | 
& Left 50 & subset ($\subseteq$)  | 
|
527  | 
\end{tabular}
 | 
|
528  | 
\end{center}
 | 
|
529  | 
\subcaption{Infixes}
 | 
|
530  | 
\caption{Syntax of the theory \texttt{Set}} \label{hol-set-syntax}
 | 
|
531  | 
\end{figure} 
 | 
|
532  | 
||
533  | 
||
534  | 
\begin{figure} 
 | 
|
535  | 
\begin{center} \tt\frenchspacing
 | 
|
536  | 
\index{*"! symbol}
 | 
|
537  | 
\begin{tabular}{rrr} 
 | 
|
538  | 
\it external & \it internal & \it description \\  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
539  | 
$a$ \ttilde: $b$ & \ttilde($a$ : $b$) & \rm not in\\  | 
| 6580 | 540  | 
  {\ttlbrace}$a@1$, $\ldots${\ttrbrace}  &  insert $a@1$ $\ldots$ {\ttlbrace}{\ttrbrace} & \rm finite set \\
 | 
541  | 
  {\ttlbrace}$x$. $P[x]${\ttrbrace}        &  Collect($\lambda x. P[x]$) &
 | 
|
542  | 
\rm comprehension \\  | 
|
543  | 
  \sdx{INT} $x$:$A$. $B[x]$      & INTER $A$ $\lambda x. B[x]$ &
 | 
|
544  | 
\rm intersection \\  | 
|
545  | 
  \sdx{UN}{\tt\ }  $x$:$A$. $B[x]$      & UNION $A$ $\lambda x. B[x]$ &
 | 
|
546  | 
\rm union \\  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
547  | 
  \sdx{ALL} $x$:$A$.\ $P[x]$ or \texttt{!} $x$:$A$.\ $P[x]$ &
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
548  | 
Ball $A$ $\lambda x.\ P[x]$ &  | 
| 6580 | 549  | 
\rm bounded $\forall$ \\  | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
550  | 
  \sdx{EX}{\tt\ } $x$:$A$.\ $P[x]$ or \texttt{?} $x$:$A$.\ $P[x]$ & 
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
551  | 
Bex $A$ $\lambda x.\ P[x]$ & \rm bounded $\exists$  | 
| 6580 | 552  | 
\end{tabular}
 | 
553  | 
\end{center}
 | 
|
554  | 
\subcaption{Translations}
 | 
|
555  | 
||
556  | 
\dquotes  | 
|
557  | 
\[\begin{array}{rclcl}
 | 
|
558  | 
    term & = & \hbox{other terms\ldots} \\
 | 
|
559  | 
         & | & "{\ttlbrace}{\ttrbrace}" \\
 | 
|
560  | 
         & | & "{\ttlbrace} " term\; ("," term)^* " {\ttrbrace}" \\
 | 
|
561  | 
         & | & "{\ttlbrace} " id " . " formula " {\ttrbrace}" \\
 | 
|
562  | 
& | & term " `` " term \\  | 
|
563  | 
& | & term " Int " term \\  | 
|
564  | 
& | & term " Un " term \\  | 
|
565  | 
& | & "INT~~" id ":" term " . " term \\  | 
|
566  | 
& | & "UN~~~" id ":" term " . " term \\  | 
|
567  | 
& | & "INT~~" id~id^* " . " term \\  | 
|
568  | 
& | & "UN~~~" id~id^* " . " term \\[2ex]  | 
|
569  | 
 formula & = & \hbox{other formulae\ldots} \\
 | 
|
570  | 
& | & term " : " term \\  | 
|
571  | 
& | & term " \ttilde: " term \\  | 
|
572  | 
& | & term " <= " term \\  | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
573  | 
& | & "ALL " id ":" term " . " formula  | 
| 
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
574  | 
& | & "!~" id ":" term " . " formula \\  | 
| 6580 | 575  | 
& | & "EX~~" id ":" term " . " formula  | 
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
576  | 
& | & "?~" id ":" term " . " formula \\  | 
| 6580 | 577  | 
  \end{array}
 | 
578  | 
\]  | 
|
579  | 
\subcaption{Full Grammar}
 | 
|
580  | 
\caption{Syntax of the theory \texttt{Set} (continued)} \label{hol-set-syntax2}
 | 
|
581  | 
\end{figure} 
 | 
|
582  | 
||
583  | 
||
584  | 
\section{A formulation of set theory}
 | 
|
585  | 
Historically, higher-order logic gives a foundation for Russell and  | 
|
586  | 
Whitehead's theory of classes. Let us use modern terminology and call them  | 
|
587  | 
{\bf sets}, but note that these sets are distinct from those of {\ZF} set
 | 
|
588  | 
theory, and behave more like {\ZF} classes.
 | 
|
589  | 
\begin{itemize}
 | 
|
590  | 
\item  | 
|
591  | 
Sets are given by predicates over some type~$\sigma$. Types serve to  | 
|
592  | 
define universes for sets, but type-checking is still significant.  | 
|
593  | 
\item  | 
|
594  | 
There is a universal set (for each type). Thus, sets have complements, and  | 
|
595  | 
may be defined by absolute comprehension.  | 
|
596  | 
\item  | 
|
597  | 
Although sets may contain other sets as elements, the containing set must  | 
|
598  | 
have a more complex type.  | 
|
599  | 
\end{itemize}
 | 
|
600  | 
Finite unions and intersections have the same behaviour in \HOL\ as they  | 
|
601  | 
do in~{\ZF}.  In \HOL\ the intersection of the empty set is well-defined,
 | 
|
602  | 
denoting the universal set for the given type.  | 
|
603  | 
||
604  | 
\subsection{Syntax of set theory}\index{*set type}
 | 
|
605  | 
\HOL's set theory is called \thydx{Set}.  The type $\alpha\,set$ is
 | 
|
606  | 
essentially the same as $\alpha\To bool$. The new type is defined for  | 
|
607  | 
clarity and to avoid complications involving function types in unification.  | 
|
608  | 
The isomorphisms between the two types are declared explicitly. They are  | 
|
609  | 
very natural: \texttt{Collect} maps $\alpha\To bool$ to $\alpha\,set$, while
 | 
|
610  | 
\hbox{\tt op :} maps in the other direction (ignoring argument order).
 | 
|
611  | 
||
612  | 
Figure~\ref{hol-set-syntax} lists the constants, infixes, and syntax
 | 
|
613  | 
translations.  Figure~\ref{hol-set-syntax2} presents the grammar of the new
 | 
|
614  | 
constructs. Infix operators include union and intersection ($A\un B$  | 
|
615  | 
and $A\int B$), the subset and membership relations, and the image  | 
|
616  | 
operator~{\tt``}\@.  Note that $a$\verb|~:|$b$ is translated to
 | 
|
| 7490 | 617  | 
$\lnot(a\in b)$.  | 
| 6580 | 618  | 
|
619  | 
The $\{a@1,\ldots\}$ notation abbreviates finite sets constructed in
 | 
|
620  | 
the obvious manner using~\texttt{insert} and~$\{\}$:
 | 
|
621  | 
\begin{eqnarray*}
 | 
|
622  | 
  \{a, b, c\} & \equiv &
 | 
|
623  | 
  \texttt{insert} \, a \, ({\tt insert} \, b \, ({\tt insert} \, c \, \{\}))
 | 
|
624  | 
\end{eqnarray*}
 | 
|
625  | 
||
626  | 
The set \hbox{\tt{\ttlbrace}$x$.\ $P[x]${\ttrbrace}} consists of all $x$ (of suitable type)
 | 
|
627  | 
that satisfy~$P[x]$, where $P[x]$ is a formula that may contain free  | 
|
628  | 
occurrences of~$x$.  This syntax expands to \cdx{Collect}$(\lambda
 | 
|
629  | 
x. P[x])$. It defines sets by absolute comprehension, which is impossible  | 
|
630  | 
in~{\ZF}; the type of~$x$ implicitly restricts the comprehension.
 | 
|
631  | 
||
632  | 
The set theory defines two {\bf bounded quantifiers}:
 | 
|
633  | 
\begin{eqnarray*}
 | 
|
634  | 
   \forall x\in A. P[x] &\hbox{abbreviates}& \forall x. x\in A\imp P[x] \\
 | 
|
635  | 
   \exists x\in A. P[x] &\hbox{abbreviates}& \exists x. x\in A\conj P[x]
 | 
|
636  | 
\end{eqnarray*}
 | 
|
637  | 
The constants~\cdx{Ball} and~\cdx{Bex} are defined
 | 
|
638  | 
accordingly.  Instead of \texttt{Ball $A$ $P$} and \texttt{Bex $A$ $P$} we may
 | 
|
639  | 
write\index{*"! symbol}\index{*"? symbol}
 | 
|
640  | 
\index{*ALL symbol}\index{*EX symbol} 
 | 
|
641  | 
%  | 
|
| 
7245
 
65ccac4e1f3f
eliminated HOL_quantifiers (replaced by "HOL" print mode);
 
wenzelm 
parents: 
7044 
diff
changeset
 | 
642  | 
\hbox{\tt ALL~$x$:$A$.\ $P[x]$} and \hbox{\tt EX~$x$:$A$.\ $P[x]$}.  The
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
643  | 
original notation of Gordon's {\sc hol} system is supported as well:
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
644  | 
\texttt{!}\ and \texttt{?}.
 | 
| 6580 | 645  | 
|
646  | 
Unions and intersections over sets, namely $\bigcup@{x\in A}B[x]$ and
 | 
|
647  | 
$\bigcap@{x\in A}B[x]$, are written 
 | 
|
648  | 
\sdx{UN}~\hbox{\tt$x$:$A$.\ $B[x]$} and
 | 
|
649  | 
\sdx{INT}~\hbox{\tt$x$:$A$.\ $B[x]$}.  
 | 
|
650  | 
||
651  | 
Unions and intersections over types, namely $\bigcup@x B[x]$ and $\bigcap@x  | 
|
652  | 
B[x]$, are written \sdx{UN}~\hbox{\tt$x$.\ $B[x]$} and
 | 
|
653  | 
\sdx{INT}~\hbox{\tt$x$.\ $B[x]$}.  They are equivalent to the previous
 | 
|
654  | 
union and intersection operators when $A$ is the universal set.  | 
|
655  | 
||
656  | 
The operators $\bigcup A$ and $\bigcap A$ act upon sets of sets. They are  | 
|
657  | 
not binders, but are equal to $\bigcup@{x\in A}x$ and $\bigcap@{x\in A}x$,
 | 
|
658  | 
respectively.  | 
|
659  | 
||
660  | 
||
661  | 
||
662  | 
\begin{figure} \underscoreon
 | 
|
663  | 
\begin{ttbox}
 | 
|
664  | 
\tdx{mem_Collect_eq}    (a : {\ttlbrace}x. P x{\ttrbrace}) = P a
 | 
|
665  | 
\tdx{Collect_mem_eq}    {\ttlbrace}x. x:A{\ttrbrace} = A
 | 
|
666  | 
||
667  | 
\tdx{empty_def}         {\ttlbrace}{\ttrbrace}          == {\ttlbrace}x. False{\ttrbrace}
 | 
|
668  | 
\tdx{insert_def}        insert a B  == {\ttlbrace}x. x=a{\ttrbrace} Un B
 | 
|
669  | 
\tdx{Ball_def}          Ball A P    == ! x. x:A --> P x
 | 
|
670  | 
\tdx{Bex_def}           Bex A P     == ? x. x:A & P x
 | 
|
671  | 
\tdx{subset_def}        A <= B      == ! x:A. x:B
 | 
|
672  | 
\tdx{Un_def}            A Un B      == {\ttlbrace}x. x:A | x:B{\ttrbrace}
 | 
|
673  | 
\tdx{Int_def}           A Int B     == {\ttlbrace}x. x:A & x:B{\ttrbrace}
 | 
|
674  | 
\tdx{set_diff_def}      A - B       == {\ttlbrace}x. x:A & x~:B{\ttrbrace}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
675  | 
\tdx{Compl_def}         -A          == {\ttlbrace}x. ~ x:A{\ttrbrace}
 | 
| 6580 | 676  | 
\tdx{INTER_def}         INTER A B   == {\ttlbrace}y. ! x:A. y: B x{\ttrbrace}
 | 
677  | 
\tdx{UNION_def}         UNION A B   == {\ttlbrace}y. ? x:A. y: B x{\ttrbrace}
 | 
|
678  | 
\tdx{INTER1_def}        INTER1 B    == INTER {\ttlbrace}x. True{\ttrbrace} B 
 | 
|
679  | 
\tdx{UNION1_def}        UNION1 B    == UNION {\ttlbrace}x. True{\ttrbrace} B 
 | 
|
680  | 
\tdx{Inter_def}         Inter S     == (INT x:S. x)
 | 
|
681  | 
\tdx{Union_def}         Union S     == (UN  x:S. x)
 | 
|
682  | 
\tdx{Pow_def}           Pow A       == {\ttlbrace}B. B <= A{\ttrbrace}
 | 
|
683  | 
\tdx{image_def}         f``A        == {\ttlbrace}y. ? x:A. y=f x{\ttrbrace}
 | 
|
684  | 
\tdx{range_def}         range f     == {\ttlbrace}y. ? x. y=f x{\ttrbrace}
 | 
|
685  | 
\end{ttbox}
 | 
|
686  | 
\caption{Rules of the theory \texttt{Set}} \label{hol-set-rules}
 | 
|
687  | 
\end{figure}
 | 
|
688  | 
||
689  | 
||
690  | 
\begin{figure} \underscoreon
 | 
|
691  | 
\begin{ttbox}
 | 
|
692  | 
\tdx{CollectI}        [| P a |] ==> a : {\ttlbrace}x. P x{\ttrbrace}
 | 
|
693  | 
\tdx{CollectD}        [| a : {\ttlbrace}x. P x{\ttrbrace} |] ==> P a
 | 
|
694  | 
\tdx{CollectE}        [| a : {\ttlbrace}x. P x{\ttrbrace};  P a ==> W |] ==> W
 | 
|
695  | 
||
696  | 
\tdx{ballI}           [| !!x. x:A ==> P x |] ==> ! x:A. P x
 | 
|
697  | 
\tdx{bspec}           [| ! x:A. P x;  x:A |] ==> P x
 | 
|
698  | 
\tdx{ballE}           [| ! x:A. P x;  P x ==> Q;  ~ x:A ==> Q |] ==> Q
 | 
|
699  | 
||
700  | 
\tdx{bexI}            [| P x;  x:A |] ==> ? x:A. P x
 | 
|
701  | 
\tdx{bexCI}           [| ! x:A. ~ P x ==> P a;  a:A |] ==> ? x:A. P x
 | 
|
702  | 
\tdx{bexE}            [| ? x:A. P x;  !!x. [| x:A; P x |] ==> Q  |] ==> Q
 | 
|
703  | 
\subcaption{Comprehension and Bounded quantifiers}
 | 
|
704  | 
||
705  | 
\tdx{subsetI}         (!!x. x:A ==> x:B) ==> A <= B
 | 
|
706  | 
\tdx{subsetD}         [| A <= B;  c:A |] ==> c:B
 | 
|
707  | 
\tdx{subsetCE}        [| A <= B;  ~ (c:A) ==> P;  c:B ==> P |] ==> P
 | 
|
708  | 
||
709  | 
\tdx{subset_refl}     A <= A
 | 
|
710  | 
\tdx{subset_trans}    [| A<=B;  B<=C |] ==> A<=C
 | 
|
711  | 
||
712  | 
\tdx{equalityI}       [| A <= B;  B <= A |] ==> A = B
 | 
|
713  | 
\tdx{equalityD1}      A = B ==> A<=B
 | 
|
714  | 
\tdx{equalityD2}      A = B ==> B<=A
 | 
|
715  | 
\tdx{equalityE}       [| A = B;  [| A<=B; B<=A |] ==> P |]  ==>  P
 | 
|
716  | 
||
717  | 
\tdx{equalityCE}      [| A = B;  [| c:A; c:B |] ==> P;  
 | 
|
718  | 
[| ~ c:A; ~ c:B |] ==> P  | 
|
719  | 
|] ==> P  | 
|
720  | 
\subcaption{The subset and equality relations}
 | 
|
721  | 
\end{ttbox}
 | 
|
722  | 
\caption{Derived rules for set theory} \label{hol-set1}
 | 
|
723  | 
\end{figure}
 | 
|
724  | 
||
725  | 
||
726  | 
\begin{figure} \underscoreon
 | 
|
727  | 
\begin{ttbox}
 | 
|
728  | 
\tdx{emptyE}   a : {\ttlbrace}{\ttrbrace} ==> P
 | 
|
729  | 
||
730  | 
\tdx{insertI1} a : insert a B
 | 
|
731  | 
\tdx{insertI2} a : B ==> a : insert b B
 | 
|
732  | 
\tdx{insertE}  [| a : insert b A;  a=b ==> P;  a:A ==> P |] ==> P
 | 
|
733  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
734  | 
\tdx{ComplI}   [| c:A ==> False |] ==> c : -A
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
735  | 
\tdx{ComplD}   [| c : -A |] ==> ~ c:A
 | 
| 6580 | 736  | 
|
737  | 
\tdx{UnI1}     c:A ==> c : A Un B
 | 
|
738  | 
\tdx{UnI2}     c:B ==> c : A Un B
 | 
|
739  | 
\tdx{UnCI}     (~c:B ==> c:A) ==> c : A Un B
 | 
|
740  | 
\tdx{UnE}      [| c : A Un B;  c:A ==> P;  c:B ==> P |] ==> P
 | 
|
741  | 
||
742  | 
\tdx{IntI}     [| c:A;  c:B |] ==> c : A Int B
 | 
|
743  | 
\tdx{IntD1}    c : A Int B ==> c:A
 | 
|
744  | 
\tdx{IntD2}    c : A Int B ==> c:B
 | 
|
745  | 
\tdx{IntE}     [| c : A Int B;  [| c:A; c:B |] ==> P |] ==> P
 | 
|
746  | 
||
747  | 
\tdx{UN_I}     [| a:A;  b: B a |] ==> b: (UN x:A. B x)
 | 
|
748  | 
\tdx{UN_E}     [| b: (UN x:A. B x);  !!x.[| x:A;  b:B x |] ==> R |] ==> R
 | 
|
749  | 
||
750  | 
\tdx{INT_I}    (!!x. x:A ==> b: B x) ==> b : (INT x:A. B x)
 | 
|
751  | 
\tdx{INT_D}    [| b: (INT x:A. B x);  a:A |] ==> b: B a
 | 
|
752  | 
\tdx{INT_E}    [| b: (INT x:A. B x);  b: B a ==> R;  ~ a:A ==> R |] ==> R
 | 
|
753  | 
||
754  | 
\tdx{UnionI}   [| X:C;  A:X |] ==> A : Union C
 | 
|
755  | 
\tdx{UnionE}   [| A : Union C;  !!X.[| A:X;  X:C |] ==> R |] ==> R
 | 
|
756  | 
||
757  | 
\tdx{InterI}   [| !!X. X:C ==> A:X |] ==> A : Inter C
 | 
|
758  | 
\tdx{InterD}   [| A : Inter C;  X:C |] ==> A:X
 | 
|
759  | 
\tdx{InterE}   [| A : Inter C;  A:X ==> R;  ~ X:C ==> R |] ==> R
 | 
|
760  | 
||
761  | 
\tdx{PowI}     A<=B ==> A: Pow B
 | 
|
762  | 
\tdx{PowD}     A: Pow B ==> A<=B
 | 
|
763  | 
||
764  | 
\tdx{imageI}   [| x:A |] ==> f x : f``A
 | 
|
765  | 
\tdx{imageE}   [| b : f``A;  !!x.[| b=f x;  x:A |] ==> P |] ==> P
 | 
|
766  | 
||
767  | 
\tdx{rangeI}   f x : range f
 | 
|
768  | 
\tdx{rangeE}   [| b : range f;  !!x.[| b=f x |] ==> P |] ==> P
 | 
|
769  | 
\end{ttbox}
 | 
|
770  | 
\caption{Further derived rules for set theory} \label{hol-set2}
 | 
|
771  | 
\end{figure}
 | 
|
772  | 
||
773  | 
||
774  | 
\subsection{Axioms and rules of set theory}
 | 
|
775  | 
Figure~\ref{hol-set-rules} presents the rules of theory \thydx{Set}.  The
 | 
|
776  | 
axioms \tdx{mem_Collect_eq} and \tdx{Collect_mem_eq} assert
 | 
|
777  | 
that the functions \texttt{Collect} and \hbox{\tt op :} are isomorphisms.  Of
 | 
|
778  | 
course, \hbox{\tt op :} also serves as the membership relation.
 | 
|
779  | 
||
780  | 
All the other axioms are definitions. They include the empty set, bounded  | 
|
781  | 
quantifiers, unions, intersections, complements and the subset relation.  | 
|
782  | 
They also include straightforward constructions on functions: image~({\tt``})
 | 
|
783  | 
and \texttt{range}.
 | 
|
784  | 
||
785  | 
%The predicate \cdx{inj_on} is used for simulating type definitions.
 | 
|
786  | 
%The statement ${\tt inj_on}~f~A$ asserts that $f$ is injective on the
 | 
|
787  | 
%set~$A$, which specifies a subset of its domain type. In a type  | 
|
788  | 
%definition, $f$ is the abstraction function and $A$ is the set of valid  | 
|
789  | 
%representations; we should not expect $f$ to be injective outside of~$A$.  | 
|
790  | 
||
791  | 
%\begin{figure} \underscoreon
 | 
|
792  | 
%\begin{ttbox}
 | 
|
793  | 
%\tdx{Inv_f_f}    inj f ==> Inv f (f x) = x
 | 
|
794  | 
%\tdx{f_Inv_f}    y : range f ==> f(Inv f y) = y
 | 
|
795  | 
%  | 
|
796  | 
%\tdx{Inv_injective}
 | 
|
797  | 
% [| Inv f x=Inv f y; x: range f; y: range f |] ==> x=y  | 
|
798  | 
%  | 
|
799  | 
%  | 
|
800  | 
%\tdx{monoI}      [| !!A B. A <= B ==> f A <= f B |] ==> mono f
 | 
|
801  | 
%\tdx{monoD}      [| mono f;  A <= B |] ==> f A <= f B
 | 
|
802  | 
%  | 
|
803  | 
%\tdx{injI}       [| !! x y. f x = f y ==> x=y |] ==> inj f
 | 
|
804  | 
%\tdx{inj_inverseI}              (!!x. g(f x) = x) ==> inj f
 | 
|
805  | 
%\tdx{injD}       [| inj f; f x = f y |] ==> x=y
 | 
|
806  | 
%  | 
|
807  | 
%\tdx{inj_onI}  (!!x y. [| f x=f y; x:A; y:A |] ==> x=y) ==> inj_on f A
 | 
|
808  | 
%\tdx{inj_onD}  [| inj_on f A;  f x=f y;  x:A;  y:A |] ==> x=y
 | 
|
809  | 
%  | 
|
810  | 
%\tdx{inj_on_inverseI}
 | 
|
811  | 
% (!!x. x:A ==> g(f x) = x) ==> inj_on f A  | 
|
812  | 
%\tdx{inj_on_contraD}
 | 
|
813  | 
% [| inj_on f A; x~=y; x:A; y:A |] ==> ~ f x=f y  | 
|
814  | 
%\end{ttbox}
 | 
|
815  | 
%\caption{Derived rules involving functions} \label{hol-fun}
 | 
|
816  | 
%\end{figure}
 | 
|
817  | 
||
818  | 
||
819  | 
\begin{figure} \underscoreon
 | 
|
820  | 
\begin{ttbox}
 | 
|
821  | 
\tdx{Union_upper}     B:A ==> B <= Union A
 | 
|
822  | 
\tdx{Union_least}     [| !!X. X:A ==> X<=C |] ==> Union A <= C
 | 
|
823  | 
||
824  | 
\tdx{Inter_lower}     B:A ==> Inter A <= B
 | 
|
825  | 
\tdx{Inter_greatest}  [| !!X. X:A ==> C<=X |] ==> C <= Inter A
 | 
|
826  | 
||
827  | 
\tdx{Un_upper1}       A <= A Un B
 | 
|
828  | 
\tdx{Un_upper2}       B <= A Un B
 | 
|
829  | 
\tdx{Un_least}        [| A<=C;  B<=C |] ==> A Un B <= C
 | 
|
830  | 
||
831  | 
\tdx{Int_lower1}      A Int B <= A
 | 
|
832  | 
\tdx{Int_lower2}      A Int B <= B
 | 
|
833  | 
\tdx{Int_greatest}    [| C<=A;  C<=B |] ==> C <= A Int B
 | 
|
834  | 
\end{ttbox}
 | 
|
835  | 
\caption{Derived rules involving subsets} \label{hol-subset}
 | 
|
836  | 
\end{figure}
 | 
|
837  | 
||
838  | 
||
839  | 
\begin{figure} \underscoreon   \hfuzz=4pt%suppress "Overfull \hbox" message
 | 
|
840  | 
\begin{ttbox}
 | 
|
841  | 
\tdx{Int_absorb}        A Int A = A
 | 
|
842  | 
\tdx{Int_commute}       A Int B = B Int A
 | 
|
843  | 
\tdx{Int_assoc}         (A Int B) Int C  =  A Int (B Int C)
 | 
|
844  | 
\tdx{Int_Un_distrib}    (A Un B)  Int C  =  (A Int C) Un (B Int C)
 | 
|
845  | 
||
846  | 
\tdx{Un_absorb}         A Un A = A
 | 
|
847  | 
\tdx{Un_commute}        A Un B = B Un A
 | 
|
848  | 
\tdx{Un_assoc}          (A Un B)  Un C  =  A Un (B Un C)
 | 
|
849  | 
\tdx{Un_Int_distrib}    (A Int B) Un C  =  (A Un C) Int (B Un C)
 | 
|
850  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
851  | 
\tdx{Compl_disjoint}    A Int (-A) = {\ttlbrace}x. False{\ttrbrace}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
852  | 
\tdx{Compl_partition}   A Un  (-A) = {\ttlbrace}x. True{\ttrbrace}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
853  | 
\tdx{double_complement} -(-A) = A
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
854  | 
\tdx{Compl_Un}          -(A Un B)  = (-A) Int (-B)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
855  | 
\tdx{Compl_Int}         -(A Int B) = (-A) Un (-B)
 | 
| 6580 | 856  | 
|
857  | 
\tdx{Union_Un_distrib}  Union(A Un B) = (Union A) Un (Union B)
 | 
|
858  | 
\tdx{Int_Union}         A Int (Union B) = (UN C:B. A Int C)
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
859  | 
%\tdx{Un_Union_image}    (UN x:C.(A x) Un (B x)) = Union(A``C) Un Union(B``C)
 | 
| 6580 | 860  | 
|
861  | 
\tdx{Inter_Un_distrib}  Inter(A Un B) = (Inter A) Int (Inter B)
 | 
|
862  | 
\tdx{Un_Inter}          A Un (Inter B) = (INT C:B. A Un C)
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
863  | 
%\tdx{Int_Inter_image}   (INT x:C.(A x) Int (B x)) = Inter(A``C) Int Inter(B``C)
 | 
| 6580 | 864  | 
\end{ttbox}
 | 
865  | 
\caption{Set equalities} \label{hol-equalities}
 | 
|
866  | 
\end{figure}
 | 
|
867  | 
||
868  | 
||
869  | 
Figures~\ref{hol-set1} and~\ref{hol-set2} present derived rules.  Most are
 | 
|
870  | 
obvious and resemble rules of Isabelle's {\ZF} set theory.  Certain rules,
 | 
|
871  | 
such as \tdx{subsetCE}, \tdx{bexCI} and \tdx{UnCI},
 | 
|
872  | 
are designed for classical reasoning; the rules \tdx{subsetD},
 | 
|
873  | 
\tdx{bexI}, \tdx{Un1} and~\tdx{Un2} are not
 | 
|
874  | 
strictly necessary but yield more natural proofs. Similarly,  | 
|
875  | 
\tdx{equalityCE} supports classical reasoning about extensionality,
 | 
|
876  | 
after the fashion of \tdx{iffCE}.  See the file \texttt{HOL/Set.ML} for
 | 
|
877  | 
proofs pertaining to set theory.  | 
|
878  | 
||
879  | 
Figure~\ref{hol-subset} presents lattice properties of the subset relation.
 | 
|
880  | 
Unions form least upper bounds; non-empty intersections form greatest lower  | 
|
881  | 
bounds. Reasoning directly about subsets often yields clearer proofs than  | 
|
882  | 
reasoning about the membership relation.  See the file \texttt{HOL/subset.ML}.
 | 
|
883  | 
||
884  | 
Figure~\ref{hol-equalities} presents many common set equalities.  They
 | 
|
885  | 
include commutative, associative and distributive laws involving unions,  | 
|
886  | 
intersections and complements.  For a complete listing see the file {\tt
 | 
|
887  | 
HOL/equalities.ML}.  | 
|
888  | 
||
889  | 
\begin{warn}
 | 
|
890  | 
\texttt{Blast_tac} proves many set-theoretic theorems automatically.
 | 
|
891  | 
Hence you seldom need to refer to the theorems above.  | 
|
892  | 
\end{warn}
 | 
|
893  | 
||
894  | 
\begin{figure}
 | 
|
895  | 
\begin{center}
 | 
|
896  | 
\begin{tabular}{rrr}
 | 
|
897  | 
\it name &\it meta-type & \it description \\  | 
|
898  | 
  \cdx{inj}~~\cdx{surj}& $(\alpha\To\beta )\To bool$
 | 
|
899  | 
& injective/surjective \\  | 
|
900  | 
  \cdx{inj_on}        & $[\alpha\To\beta ,\alpha\,set]\To bool$
 | 
|
901  | 
& injective over subset\\  | 
|
902  | 
  \cdx{inv} & $(\alpha\To\beta)\To(\beta\To\alpha)$ & inverse function
 | 
|
903  | 
\end{tabular}
 | 
|
904  | 
\end{center}
 | 
|
905  | 
||
906  | 
\underscoreon  | 
|
907  | 
\begin{ttbox}
 | 
|
908  | 
\tdx{inj_def}         inj f      == ! x y. f x=f y --> x=y
 | 
|
909  | 
\tdx{surj_def}        surj f     == ! y. ? x. y=f x
 | 
|
910  | 
\tdx{inj_on_def}      inj_on f A == !x:A. !y:A. f x=f y --> x=y
 | 
|
911  | 
\tdx{inv_def}         inv f      == (\%y. @x. f(x)=y)
 | 
|
912  | 
\end{ttbox}
 | 
|
913  | 
\caption{Theory \thydx{Fun}} \label{fig:HOL:Fun}
 | 
|
914  | 
\end{figure}
 | 
|
915  | 
||
916  | 
\subsection{Properties of functions}\nopagebreak
 | 
|
917  | 
Figure~\ref{fig:HOL:Fun} presents a theory of simple properties of functions.
 | 
|
918  | 
Note that ${\tt inv}~f$ uses Hilbert's $\varepsilon$ to yield an inverse
 | 
|
919  | 
of~$f$.  See the file \texttt{HOL/Fun.ML} for a complete listing of the derived
 | 
|
920  | 
rules.  Reasoning about function composition (the operator~\sdx{o}) and the
 | 
|
921  | 
predicate~\cdx{surj} is done simply by expanding the definitions.
 | 
|
922  | 
||
923  | 
There is also a large collection of monotonicity theorems for constructions  | 
|
924  | 
on sets in the file \texttt{HOL/mono.ML}.
 | 
|
925  | 
||
| 7283 | 926  | 
|
| 6580 | 927  | 
\section{Generic packages}
 | 
928  | 
\label{sec:HOL:generic-packages}
 | 
|
929  | 
||
930  | 
\HOL\ instantiates most of Isabelle's generic packages, making available the  | 
|
931  | 
simplifier and the classical reasoner.  | 
|
932  | 
||
933  | 
\subsection{Simplification and substitution}
 | 
|
934  | 
||
935  | 
Simplification tactics tactics such as \texttt{Asm_simp_tac} and \texttt{Full_simp_tac} use the default simpset
 | 
|
936  | 
(\texttt{simpset()}), which works for most purposes.  A quite minimal
 | 
|
937  | 
simplification set for higher-order logic is~\ttindexbold{HOL_ss};
 | 
|
938  | 
even more frugal is \ttindexbold{HOL_basic_ss}.  Equality~($=$), which
 | 
|
939  | 
also expresses logical equivalence, may be used for rewriting. See  | 
|
940  | 
the file \texttt{HOL/simpdata.ML} for a complete listing of the basic
 | 
|
941  | 
simplification rules.  | 
|
942  | 
||
943  | 
See \iflabelundefined{chap:classical}{the {\em Reference Manual\/}}%
 | 
|
944  | 
{Chaps.\ts\ref{substitution} and~\ref{simp-chap}} for details of substitution
 | 
|
945  | 
and simplification.  | 
|
946  | 
||
947  | 
\begin{warn}\index{simplification!of conjunctions}%
 | 
|
948  | 
Reducing $a=b\conj P(a)$ to $a=b\conj P(b)$ is sometimes advantageous. The  | 
|
949  | 
left part of a conjunction helps in simplifying the right part. This effect  | 
|
950  | 
is not available by default: it can be slow. It can be obtained by  | 
|
951  | 
  including \ttindex{conj_cong} in a simpset, \verb$addcongs [conj_cong]$.
 | 
|
952  | 
\end{warn}
 | 
|
953  | 
||
| 8604 | 954  | 
\begin{warn}\index{simplification!of \texttt{if}}\label{if-simp}%
 | 
955  | 
  By default only the condition of an \ttindex{if} is simplified but not the
 | 
|
956  | 
  \texttt{then} and \texttt{else} parts. Of course the latter are simplified
 | 
|
957  | 
  once the condition simplifies to \texttt{True} or \texttt{False}. To ensure
 | 
|
958  | 
full simplification of all parts of a conditional you must remove  | 
|
959  | 
  \ttindex{if_weak_cong} from the simpset, \verb$delcongs [if_weak_cong]$.
 | 
|
960  | 
\end{warn}
 | 
|
961  | 
||
| 6580 | 962  | 
If the simplifier cannot use a certain rewrite rule --- either because  | 
963  | 
of nontermination or because its left-hand side is too flexible ---  | 
|
964  | 
then you might try \texttt{stac}:
 | 
|
965  | 
\begin{ttdescription}
 | 
|
966  | 
\item[\ttindexbold{stac} $thm$ $i,$] where $thm$ is of the form $lhs = rhs$,
 | 
|
967  | 
replaces in subgoal $i$ instances of $lhs$ by corresponding instances of  | 
|
968  | 
$rhs$. In case of multiple instances of $lhs$ in subgoal $i$, backtracking  | 
|
969  | 
may be necessary to select the desired ones.  | 
|
970  | 
||
971  | 
If $thm$ is a conditional equality, the instantiated condition becomes an  | 
|
972  | 
additional (first) subgoal.  | 
|
973  | 
\end{ttdescription}
 | 
|
974  | 
||
975  | 
 \HOL{} provides the tactic \ttindex{hyp_subst_tac}, which substitutes
 | 
|
976  | 
for an equality throughout a subgoal and its hypotheses. This tactic uses  | 
|
977  | 
\HOL's general substitution rule.  | 
|
978  | 
||
979  | 
\subsubsection{Case splitting}
 | 
|
980  | 
\label{subsec:HOL:case:splitting}
 | 
|
981  | 
||
982  | 
\HOL{} also provides convenient means for case splitting during
 | 
|
983  | 
rewriting. Goals containing a subterm of the form \texttt{if}~$b$~{\tt
 | 
|
984  | 
then\dots else\dots} often require a case distinction on $b$. This is  | 
|
985  | 
expressed by the theorem \tdx{split_if}:
 | 
|
986  | 
$$  | 
|
987  | 
\Var{P}(\mbox{\tt if}~\Var{b}~{\tt then}~\Var{x}~\mbox{\tt else}~\Var{y})~=~
 | 
|
| 7490 | 988  | 
((\Var{b} \to \Var{P}(\Var{x})) \land (\lnot \Var{b} \to \Var{P}(\Var{y})))
 | 
| 6580 | 989  | 
\eqno{(*)}
 | 
990  | 
$$  | 
|
991  | 
For example, a simple instance of $(*)$ is  | 
|
992  | 
\[  | 
|
993  | 
x \in (\mbox{\tt if}~x \in A~{\tt then}~A~\mbox{\tt else}~\{x\})~=~
 | 
|
994  | 
((x \in A \to x \in A) \land (x \notin A \to x \in \{x\}))
 | 
|
995  | 
\]  | 
|
996  | 
Because $(*)$ is too general as a rewrite rule for the simplifier (the  | 
|
997  | 
left-hand side is not a higher-order pattern in the sense of  | 
|
998  | 
\iflabelundefined{chap:simplification}{the {\em Reference Manual\/}}%
 | 
|
999  | 
{Chap.\ts\ref{chap:simplification}}), there is a special infix function 
 | 
|
1000  | 
\ttindexbold{addsplits} of type \texttt{simpset * thm list -> simpset}
 | 
|
1001  | 
(analogous to \texttt{addsimps}) that adds rules such as $(*)$ to a
 | 
|
1002  | 
simpset, as in  | 
|
1003  | 
\begin{ttbox}
 | 
|
1004  | 
by(simp_tac (simpset() addsplits [split_if]) 1);  | 
|
1005  | 
\end{ttbox}
 | 
|
1006  | 
The effect is that after each round of simplification, one occurrence of  | 
|
1007  | 
\texttt{if} is split acording to \texttt{split_if}, until all occurences of
 | 
|
1008  | 
\texttt{if} have been eliminated.
 | 
|
1009  | 
||
1010  | 
It turns out that using \texttt{split_if} is almost always the right thing to
 | 
|
1011  | 
do. Hence \texttt{split_if} is already included in the default simpset. If
 | 
|
1012  | 
you want to delete it from a simpset, use \ttindexbold{delsplits}, which is
 | 
|
1013  | 
the inverse of \texttt{addsplits}:
 | 
|
1014  | 
\begin{ttbox}
 | 
|
1015  | 
by(simp_tac (simpset() delsplits [split_if]) 1);  | 
|
1016  | 
\end{ttbox}
 | 
|
1017  | 
||
1018  | 
In general, \texttt{addsplits} accepts rules of the form
 | 
|
1019  | 
\[  | 
|
1020  | 
\Var{P}(c~\Var{x@1}~\dots~\Var{x@n})~=~ rhs
 | 
|
1021  | 
\]  | 
|
1022  | 
where $c$ is a constant and $rhs$ is arbitrary. Note that $(*)$ is of the  | 
|
1023  | 
right form because internally the left-hand side is  | 
|
1024  | 
$\Var{P}(\mathtt{If}~\Var{b}~\Var{x}~~\Var{y})$. Important further examples
 | 
|
| 7490 | 1025  | 
are splitting rules for \texttt{case} expressions (see~{\S}\ref{subsec:list}
 | 
1026  | 
and~{\S}\ref{subsec:datatype:basics}).
 | 
|
| 6580 | 1027  | 
|
1028  | 
Analogous to \texttt{Addsimps} and \texttt{Delsimps}, there are also
 | 
|
1029  | 
imperative versions of \texttt{addsplits} and \texttt{delsplits}
 | 
|
1030  | 
\begin{ttbox}
 | 
|
1031  | 
\ttindexbold{Addsplits}: thm list -> unit
 | 
|
1032  | 
\ttindexbold{Delsplits}: thm list -> unit
 | 
|
1033  | 
\end{ttbox}
 | 
|
1034  | 
for adding splitting rules to, and deleting them from the current simpset.  | 
|
1035  | 
||
1036  | 
\subsection{Classical reasoning}
 | 
|
1037  | 
||
1038  | 
\HOL\ derives classical introduction rules for $\disj$ and~$\exists$, as  | 
|
1039  | 
well as classical elimination rules for~$\imp$ and~$\bimp$, and the swap  | 
|
1040  | 
rule; recall Fig.\ts\ref{hol-lemmas2} above.
 | 
|
1041  | 
||
| 7283 | 1042  | 
The classical reasoner is installed.  Tactics such as \texttt{Blast_tac} and
 | 
1043  | 
{\tt Best_tac} refer to the default claset (\texttt{claset()}), which works
 | 
|
1044  | 
for most purposes.  Named clasets include \ttindexbold{prop_cs}, which
 | 
|
1045  | 
includes the propositional rules, and \ttindexbold{HOL_cs}, which also
 | 
|
1046  | 
includes quantifier rules.  See the file \texttt{HOL/cladata.ML} for lists of
 | 
|
1047  | 
the classical rules,  | 
|
| 6580 | 1048  | 
and \iflabelundefined{chap:classical}{the {\em Reference Manual\/}}%
 | 
1049  | 
{Chap.\ts\ref{chap:classical}} for more discussion of classical proof methods.
 | 
|
1050  | 
||
1051  | 
||
| 7283 | 1052  | 
\section{Calling the decision procedure SVC}\label{sec:HOL:SVC}
 | 
1053  | 
||
1054  | 
\index{SVC decision procedure|(}
 | 
|
1055  | 
||
1056  | 
The Stanford Validity Checker (SVC) is a tool that can check the validity of  | 
|
1057  | 
certain types of formulae. If it is installed on your machine, then  | 
|
1058  | 
Isabelle/HOL can be configured to call it through the tactic  | 
|
1059  | 
\ttindex{svc_tac}.  It is ideal for large tautologies and complex problems in
 | 
|
1060  | 
linear arithmetic. Subexpressions that SVC cannot handle are automatically  | 
|
1061  | 
replaced by variables, so you can call the tactic on any subgoal. See the  | 
|
1062  | 
file \texttt{HOL/ex/svc_test.ML} for examples.
 | 
|
1063  | 
\begin{ttbox} 
 | 
|
1064  | 
svc_tac : int -> tactic  | 
|
1065  | 
Svc.trace : bool ref      \hfill{\bf initially false}
 | 
|
1066  | 
\end{ttbox}
 | 
|
1067  | 
||
1068  | 
\begin{ttdescription}
 | 
|
1069  | 
\item[\ttindexbold{svc_tac} $i$] attempts to prove subgoal~$i$ by translating
 | 
|
1070  | 
it into a formula recognized by~SVC\@. If it succeeds then the subgoal is  | 
|
1071  | 
removed. It fails if SVC is unable to prove the subgoal. It crashes with  | 
|
1072  | 
an error message if SVC appears not to be installed. Numeric variables may  | 
|
1073  | 
  have types \texttt{nat}, \texttt{int} or \texttt{real}.
 | 
|
1074  | 
||
1075  | 
\item[\ttindexbold{Svc.trace}] is a flag that, if set, causes \texttt{svc_tac}
 | 
|
1076  | 
to trace its operations: abstraction of the subgoal, translation to SVC  | 
|
1077  | 
syntax, SVC's response.  | 
|
1078  | 
\end{ttdescription}
 | 
|
1079  | 
||
1080  | 
Here is an example, with tracing turned on:  | 
|
1081  | 
\begin{ttbox}
 | 
|
1082  | 
set Svc.trace;  | 
|
1083  | 
{\out val it : bool = true}
 | 
|
1084  | 
Goal "(#3::nat)*a <= #2 + #4*b + #6*c & #11 <= #2*a + b + #2*c & \ttback  | 
|
1085  | 
\ttback a + #3*b <= #5 + #2*c --> #2 + #3*b <= #2*a + #6*c";  | 
|
1086  | 
||
1087  | 
by (svc_tac 1);  | 
|
1088  | 
{\out Subgoal abstracted to}
 | 
|
1089  | 
{\out #3 * a <= #2 + #4 * b + #6 * c &}
 | 
|
1090  | 
{\out #11 <= #2 * a + b + #2 * c & a + #3 * b <= #5 + #2 * c -->}
 | 
|
1091  | 
{\out #2 + #3 * b <= #2 * a + #6 * c}
 | 
|
1092  | 
{\out Calling SVC:}
 | 
|
1093  | 
{\out (=> (<= 0  (F_c) )  (=> (<= 0  (F_b) )  (=> (<= 0  (F_a) )}
 | 
|
1094  | 
{\out   (=> (AND (<= {* 3  (F_a) }  {+ {+ 2  {* 4  (F_b) } }  }
 | 
|
1095  | 
{\out {* 6  (F_c) } } )  (AND (<= 11  {+ {+ {* 2  (F_a) }  (F_b) }}
 | 
|
1096  | 
{\out   {* 2  (F_c) } } )  (<= {+ (F_a)  {* 3  (F_b) } }  {+ 5  }
 | 
|
1097  | 
{\out {* 2  (F_c) } } ) ) )  (< {+ 2  {* 3  (F_b) } }  {+ 1  {+ }
 | 
|
1098  | 
{\out {* 2  (F_a) }  {* 6  (F_c) } } } ) ) ) ) ) }
 | 
|
1099  | 
{\out SVC Returns:}
 | 
|
1100  | 
{\out VALID}
 | 
|
1101  | 
{\out Level 1}
 | 
|
1102  | 
{\out #3 * a <= #2 + #4 * b + #6 * c &}
 | 
|
1103  | 
{\out #11 <= #2 * a + b + #2 * c & a + #3 * b <= #5 + #2 * c -->}
 | 
|
1104  | 
{\out #2 + #3 * b <= #2 * a + #6 * c}
 | 
|
1105  | 
{\out No subgoals!}
 | 
|
1106  | 
\end{ttbox}
 | 
|
1107  | 
||
1108  | 
||
1109  | 
\begin{warn}
 | 
|
1110  | 
Calling \ttindex{svc_tac} entails an above-average risk of
 | 
|
1111  | 
unsoundness. Isabelle does not check SVC's result independently. Moreover,  | 
|
1112  | 
the tactic translates the submitted formula using code that lies outside  | 
|
1113  | 
Isabelle's inference core. Theorems that depend upon results proved using SVC  | 
|
1114  | 
(and other oracles) are displayed with the annotation \texttt{[!]} attached.
 | 
|
1115  | 
You can also use \texttt{\#der (rep_thm $th$)} to examine the proof object of
 | 
|
1116  | 
theorem~$th$, as described in the \emph{Reference Manual}.  
 | 
|
1117  | 
\end{warn}
 | 
|
1118  | 
||
1119  | 
To start, first download SVC from the Internet at URL  | 
|
1120  | 
\begin{ttbox}
 | 
|
1121  | 
http://agamemnon.stanford.edu/~levitt/vc/index.html  | 
|
1122  | 
\end{ttbox}
 | 
|
1123  | 
and install it using the instructions supplied. SVC requires two environment  | 
|
1124  | 
variables:  | 
|
1125  | 
\begin{ttdescription}
 | 
|
1126  | 
\item[\ttindexbold{SVC_HOME}] is an absolute pathname to the SVC
 | 
|
1127  | 
distribution directory.  | 
|
1128  | 
||
1129  | 
  \item[\ttindexbold{SVC_MACHINE}] identifies the type of computer and
 | 
|
1130  | 
operating system.  | 
|
1131  | 
\end{ttdescription}
 | 
|
1132  | 
You can set these environment variables either using the Unix shell or through  | 
|
1133  | 
an Isabelle \texttt{settings} file.  Isabelle assumes SVC to be installed if
 | 
|
1134  | 
\texttt{SVC_HOME} is defined.
 | 
|
1135  | 
||
1136  | 
\paragraph*{Acknowledgement.}  This interface uses code supplied by S{\o}ren
 | 
|
1137  | 
Heilmann.  | 
|
1138  | 
||
1139  | 
||
1140  | 
\index{SVC decision procedure|)}
 | 
|
1141  | 
||
1142  | 
||
1143  | 
||
1144  | 
||
| 6580 | 1145  | 
\section{Types}\label{sec:HOL:Types}
 | 
1146  | 
This section describes \HOL's basic predefined types ($\alpha \times  | 
|
1147  | 
\beta$, $\alpha + \beta$, $nat$ and $\alpha \; list$) and ways for  | 
|
1148  | 
introducing new types in general. The most important type  | 
|
1149  | 
construction, the \texttt{datatype}, is treated separately in
 | 
|
| 7490 | 1150  | 
{\S}\ref{sec:HOL:datatype}.
 | 
| 6580 | 1151  | 
|
1152  | 
||
1153  | 
\subsection{Product and sum types}\index{*"* type}\index{*"+ type}
 | 
|
1154  | 
\label{subsec:prod-sum}
 | 
|
1155  | 
||
1156  | 
\begin{figure}[htbp]
 | 
|
1157  | 
\begin{constants}
 | 
|
1158  | 
\it symbol & \it meta-type & & \it description \\  | 
|
1159  | 
  \cdx{Pair}    & $[\alpha,\beta]\To \alpha\times\beta$
 | 
|
1160  | 
& & ordered pairs $(a,b)$ \\  | 
|
1161  | 
  \cdx{fst}     & $\alpha\times\beta \To \alpha$        & & first projection\\
 | 
|
1162  | 
  \cdx{snd}     & $\alpha\times\beta \To \beta$         & & second projection\\
 | 
|
1163  | 
  \cdx{split}   & $[[\alpha,\beta]\To\gamma, \alpha\times\beta] \To \gamma$ 
 | 
|
1164  | 
& & generalized projection\\  | 
|
1165  | 
  \cdx{Sigma}  & 
 | 
|
1166  | 
$[\alpha\,set, \alpha\To\beta\,set]\To(\alpha\times\beta)set$ &  | 
|
1167  | 
& general sum of sets  | 
|
1168  | 
\end{constants}
 | 
|
1169  | 
\begin{ttbox}\makeatletter
 | 
|
1170  | 
%\tdx{fst_def}      fst p     == @a. ? b. p = (a,b)
 | 
|
1171  | 
%\tdx{snd_def}      snd p     == @b. ? a. p = (a,b)
 | 
|
1172  | 
%\tdx{split_def}    split c p == c (fst p) (snd p)
 | 
|
1173  | 
\tdx{Sigma_def}    Sigma A B == UN x:A. UN y:B x. {\ttlbrace}(x,y){\ttrbrace}
 | 
|
1174  | 
||
1175  | 
\tdx{Pair_eq}      ((a,b) = (a',b')) = (a=a' & b=b')
 | 
|
1176  | 
\tdx{Pair_inject}  [| (a, b) = (a',b');  [| a=a';  b=b' |] ==> R |] ==> R
 | 
|
1177  | 
\tdx{PairE}        [| !!x y. p = (x,y) ==> Q |] ==> Q
 | 
|
1178  | 
||
1179  | 
\tdx{fst_conv}     fst (a,b) = a
 | 
|
1180  | 
\tdx{snd_conv}     snd (a,b) = b
 | 
|
1181  | 
\tdx{surjective_pairing}  p = (fst p,snd p)
 | 
|
1182  | 
||
1183  | 
\tdx{split}        split c (a,b) = c a b
 | 
|
1184  | 
\tdx{split_split}  R(split c p) = (! x y. p = (x,y) --> R(c x y))
 | 
|
1185  | 
||
1186  | 
\tdx{SigmaI}    [| a:A;  b:B a |] ==> (a,b) : Sigma A B
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1187  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1188  | 
\tdx{SigmaE}    [| c:Sigma A B; !!x y.[| x:A; y:B x; c=(x,y) |] ==> P 
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1189  | 
|] ==> P  | 
| 6580 | 1190  | 
\end{ttbox}
 | 
1191  | 
\caption{Type $\alpha\times\beta$}\label{hol-prod}
 | 
|
1192  | 
\end{figure} 
 | 
|
1193  | 
||
1194  | 
Theory \thydx{Prod} (Fig.\ts\ref{hol-prod}) defines the product type
 | 
|
1195  | 
$\alpha\times\beta$, with the ordered pair syntax $(a, b)$. General  | 
|
1196  | 
tuples are simulated by pairs nested to the right:  | 
|
1197  | 
\begin{center}
 | 
|
1198  | 
\begin{tabular}{c|c}
 | 
|
1199  | 
external & internal \\  | 
|
1200  | 
\hline  | 
|
1201  | 
$\tau@1 \times \dots \times \tau@n$ & $\tau@1 \times (\dots (\tau@{n-1} \times \tau@n)\dots)$ \\
 | 
|
1202  | 
\hline  | 
|
1203  | 
$(t@1,\dots,t@n)$ & $(t@1,(\dots,(t@{n-1},t@n)\dots)$ \\
 | 
|
1204  | 
\end{tabular}
 | 
|
1205  | 
\end{center}
 | 
|
1206  | 
In addition, it is possible to use tuples  | 
|
1207  | 
as patterns in abstractions:  | 
|
1208  | 
\begin{center}
 | 
|
1209  | 
{\tt\%($x$,$y$). $t$} \quad stands for\quad \texttt{split(\%$x$\thinspace$y$.\ $t$)} 
 | 
|
1210  | 
\end{center}
 | 
|
1211  | 
Nested patterns are also supported. They are translated stepwise:  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1212  | 
\begin{eqnarray*}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1213  | 
\hbox{\tt\%($x$,$y$,$z$).\ $t$} 
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1214  | 
   & \leadsto & \hbox{\tt\%($x$,($y$,$z$)).\ $t$} \\
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1215  | 
   & \leadsto & \hbox{\tt split(\%$x$.\%($y$,$z$).\ $t$)}\\
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1216  | 
   & \leadsto & \hbox{\tt split(\%$x$.\ split(\%$y$ $z$.\ $t$))}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1217  | 
\end{eqnarray*}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1218  | 
The reverse translation is performed upon printing.  | 
| 6580 | 1219  | 
\begin{warn}
 | 
1220  | 
  The translation between patterns and \texttt{split} is performed automatically
 | 
|
1221  | 
by the parser and printer. Thus the internal and external form of a term  | 
|
1222  | 
  may differ, which can affects proofs.  For example the term {\tt
 | 
|
1223  | 
  (\%(x,y).(y,x))(a,b)} requires the theorem \texttt{split} (which is in the
 | 
|
1224  | 
  default simpset) to rewrite to {\tt(b,a)}.
 | 
|
1225  | 
\end{warn}
 | 
|
1226  | 
In addition to explicit $\lambda$-abstractions, patterns can be used in any  | 
|
1227  | 
variable binding construct which is internally described by a  | 
|
1228  | 
$\lambda$-abstraction. Some important examples are  | 
|
1229  | 
\begin{description}
 | 
|
1230  | 
\item[Let:] \texttt{let {\it pattern} = $t$ in $u$}
 | 
|
1231  | 
\item[Quantifiers:] \texttt{!~{\it pattern}:$A$.~$P$}
 | 
|
1232  | 
\item[Choice:] {\underscoreon \tt @~{\it pattern}~.~$P$}
 | 
|
1233  | 
\item[Set operations:] \texttt{UN~{\it pattern}:$A$.~$B$}
 | 
|
1234  | 
\item[Sets:] \texttt{{\ttlbrace}~{\it pattern}~.~$P$~{\ttrbrace}}
 | 
|
1235  | 
\end{description}
 | 
|
1236  | 
||
1237  | 
There is a simple tactic which supports reasoning about patterns:  | 
|
1238  | 
\begin{ttdescription}
 | 
|
1239  | 
\item[\ttindexbold{split_all_tac} $i$] replaces in subgoal $i$ all
 | 
|
1240  | 
  {\tt!!}-quantified variables of product type by individual variables for
 | 
|
1241  | 
each component. A simple example:  | 
|
1242  | 
\begin{ttbox}
 | 
|
1243  | 
{\out 1. !!p. (\%(x,y,z). (x, y, z)) p = p}
 | 
|
1244  | 
by(split_all_tac 1);  | 
|
1245  | 
{\out 1. !!x xa ya. (\%(x,y,z). (x, y, z)) (x, xa, ya) = (x, xa, ya)}
 | 
|
1246  | 
\end{ttbox}
 | 
|
1247  | 
\end{ttdescription}
 | 
|
1248  | 
||
1249  | 
Theory \texttt{Prod} also introduces the degenerate product type \texttt{unit}
 | 
|
1250  | 
which contains only a single element named {\tt()} with the property
 | 
|
1251  | 
\begin{ttbox}
 | 
|
1252  | 
\tdx{unit_eq}       u = ()
 | 
|
1253  | 
\end{ttbox}
 | 
|
1254  | 
\bigskip  | 
|
1255  | 
||
1256  | 
Theory \thydx{Sum} (Fig.~\ref{hol-sum}) defines the sum type $\alpha+\beta$
 | 
|
1257  | 
which associates to the right and has a lower priority than $*$: $\tau@1 +  | 
|
1258  | 
\tau@2 + \tau@3*\tau@4$ means $\tau@1 + (\tau@2 + (\tau@3*\tau@4))$.  | 
|
1259  | 
||
1260  | 
The definition of products and sums in terms of existing types is not  | 
|
1261  | 
shown. The constructions are fairly standard and can be found in the  | 
|
| 7325 | 1262  | 
respective theory files. Although the sum and product types are  | 
1263  | 
constructed manually for foundational reasons, they are represented as  | 
|
| 7490 | 1264  | 
actual datatypes later (see {\S}\ref{subsec:datatype:rep_datatype}).
 | 
| 7325 | 1265  | 
Therefore, the theory \texttt{Datatype} should be used instead of
 | 
1266  | 
\texttt{Sum} or \texttt{Prod}.
 | 
|
| 6580 | 1267  | 
|
1268  | 
\begin{figure}
 | 
|
1269  | 
\begin{constants}
 | 
|
1270  | 
\it symbol & \it meta-type & & \it description \\  | 
|
1271  | 
  \cdx{Inl}     & $\alpha \To \alpha+\beta$    & & first injection\\
 | 
|
1272  | 
  \cdx{Inr}     & $\beta \To \alpha+\beta$     & & second injection\\
 | 
|
1273  | 
  \cdx{sum_case} & $[\alpha\To\gamma, \beta\To\gamma, \alpha+\beta] \To\gamma$
 | 
|
1274  | 
& & conditional  | 
|
1275  | 
\end{constants}
 | 
|
1276  | 
\begin{ttbox}\makeatletter
 | 
|
1277  | 
\tdx{Inl_not_Inr}    Inl a ~= Inr b
 | 
|
1278  | 
||
1279  | 
\tdx{inj_Inl}        inj Inl
 | 
|
1280  | 
\tdx{inj_Inr}        inj Inr
 | 
|
1281  | 
||
1282  | 
\tdx{sumE}           [| !!x. P(Inl x);  !!y. P(Inr y) |] ==> P s
 | 
|
1283  | 
||
1284  | 
\tdx{sum_case_Inl}   sum_case f g (Inl x) = f x
 | 
|
1285  | 
\tdx{sum_case_Inr}   sum_case f g (Inr x) = g x
 | 
|
1286  | 
||
1287  | 
\tdx{surjective_sum} sum_case (\%x. f(Inl x)) (\%y. f(Inr y)) s = f s
 | 
|
| 7325 | 1288  | 
\tdx{sum.split_case} R(sum_case f g s) = ((! x. s = Inl(x) --> R(f(x))) &
 | 
| 6580 | 1289  | 
(! y. s = Inr(y) --> R(g(y))))  | 
1290  | 
\end{ttbox}
 | 
|
1291  | 
\caption{Type $\alpha+\beta$}\label{hol-sum}
 | 
|
1292  | 
\end{figure}
 | 
|
1293  | 
||
1294  | 
\begin{figure}
 | 
|
1295  | 
\index{*"< symbol}
 | 
|
1296  | 
\index{*"* symbol}
 | 
|
1297  | 
\index{*div symbol}
 | 
|
1298  | 
\index{*mod symbol}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1299  | 
\index{*dvd symbol}
 | 
| 6580 | 1300  | 
\index{*"+ symbol}
 | 
1301  | 
\index{*"- symbol}
 | 
|
1302  | 
\begin{constants}
 | 
|
1303  | 
\it symbol & \it meta-type & \it priority & \it description \\  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1304  | 
  \cdx{0}       & $\alpha$  & & zero \\
 | 
| 6580 | 1305  | 
  \cdx{Suc}     & $nat \To nat$ & & successor function\\
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1306  | 
\tt * & $[\alpha,\alpha]\To \alpha$ & Left 70 & multiplication \\  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1307  | 
\tt div & $[\alpha,\alpha]\To \alpha$ & Left 70 & division\\  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1308  | 
\tt mod & $[\alpha,\alpha]\To \alpha$ & Left 70 & modulus\\  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1309  | 
\tt dvd & $[\alpha,\alpha]\To bool$ & Left 70 & ``divides'' relation\\  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1310  | 
\tt + & $[\alpha,\alpha]\To \alpha$ & Left 65 & addition\\  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1311  | 
\tt - & $[\alpha,\alpha]\To \alpha$ & Left 65 & subtraction  | 
| 6580 | 1312  | 
\end{constants}
 | 
1313  | 
\subcaption{Constants and infixes}
 | 
|
1314  | 
||
1315  | 
\begin{ttbox}\makeatother
 | 
|
1316  | 
\tdx{nat_induct}     [| P 0; !!n. P n ==> P(Suc n) |]  ==> P n
 | 
|
1317  | 
||
1318  | 
\tdx{Suc_not_Zero}   Suc m ~= 0
 | 
|
1319  | 
\tdx{inj_Suc}        inj Suc
 | 
|
1320  | 
\tdx{n_not_Suc_n}    n~=Suc n
 | 
|
1321  | 
\subcaption{Basic properties}
 | 
|
1322  | 
\end{ttbox}
 | 
|
1323  | 
\caption{The type of natural numbers, \tydx{nat}} \label{hol-nat1}
 | 
|
1324  | 
\end{figure}
 | 
|
1325  | 
||
1326  | 
||
1327  | 
\begin{figure}
 | 
|
1328  | 
\begin{ttbox}\makeatother
 | 
|
1329  | 
0+n = n  | 
|
1330  | 
(Suc m)+n = Suc(m+n)  | 
|
1331  | 
||
1332  | 
m-0 = m  | 
|
1333  | 
0-n = n  | 
|
1334  | 
Suc(m)-Suc(n) = m-n  | 
|
1335  | 
||
1336  | 
0*n = 0  | 
|
1337  | 
Suc(m)*n = n + m*n  | 
|
1338  | 
||
1339  | 
\tdx{mod_less}      m<n ==> m mod n = m
 | 
|
1340  | 
\tdx{mod_geq}       [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n
 | 
|
1341  | 
||
1342  | 
\tdx{div_less}      m<n ==> m div n = 0
 | 
|
1343  | 
\tdx{div_geq}       [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)
 | 
|
1344  | 
\end{ttbox}
 | 
|
1345  | 
\caption{Recursion equations for the arithmetic operators} \label{hol-nat2}
 | 
|
1346  | 
\end{figure}
 | 
|
1347  | 
||
1348  | 
\subsection{The type of natural numbers, \textit{nat}}
 | 
|
1349  | 
\index{nat@{\textit{nat}} type|(}
 | 
|
1350  | 
||
1351  | 
The theory \thydx{NatDef} defines the natural numbers in a roundabout but
 | 
|
1352  | 
traditional way.  The axiom of infinity postulates a type~\tydx{ind} of
 | 
|
1353  | 
individuals, which is non-empty and closed under an injective operation. The  | 
|
1354  | 
natural numbers are inductively generated by choosing an arbitrary individual  | 
|
1355  | 
for~0 and using the injective operation to take successors. This is a least  | 
|
1356  | 
fixedpoint construction.  For details see the file \texttt{NatDef.thy}.
 | 
|
1357  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1358  | 
Type~\tydx{nat} is an instance of class~\cldx{ord}, which makes the overloaded
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1359  | 
functions of this class (especially \cdx{<} and \cdx{<=}, but also \cdx{min},
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1360  | 
\cdx{max} and \cdx{LEAST}) available on \tydx{nat}.  Theory \thydx{Nat} builds
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1361  | 
on \texttt{NatDef} and shows that {\tt<=} is a linear order, so \tydx{nat} is
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1362  | 
also an instance of class \cldx{linorder}.
 | 
| 6580 | 1363  | 
|
1364  | 
Theory \thydx{Arith} develops arithmetic on the natural numbers.  It defines
 | 
|
1365  | 
addition, multiplication and subtraction.  Theory \thydx{Divides} defines
 | 
|
1366  | 
division, remainder and the ``divides'' relation. The numerous theorems  | 
|
1367  | 
proved include commutative, associative, distributive, identity and  | 
|
1368  | 
cancellation laws.  See Figs.\ts\ref{hol-nat1} and~\ref{hol-nat2}.  The
 | 
|
1369  | 
recursion equations for the operators \texttt{+}, \texttt{-} and \texttt{*} on
 | 
|
1370  | 
\texttt{nat} are part of the default simpset.
 | 
|
1371  | 
||
1372  | 
Functions on \tydx{nat} can be defined by primitive or well-founded recursion;
 | 
|
| 7490 | 1373  | 
see {\S}\ref{sec:HOL:recursive}.  A simple example is addition.
 | 
| 6580 | 1374  | 
Here, \texttt{op +} is the name of the infix operator~\texttt{+}, following
 | 
1375  | 
the standard convention.  | 
|
1376  | 
\begin{ttbox}
 | 
|
1377  | 
\sdx{primrec}
 | 
|
1378  | 
"0 + n = n"  | 
|
1379  | 
"Suc m + n = Suc (m + n)"  | 
|
1380  | 
\end{ttbox}
 | 
|
1381  | 
There is also a \sdx{case}-construct
 | 
|
1382  | 
of the form  | 
|
1383  | 
\begin{ttbox}
 | 
|
1384  | 
case \(e\) of 0 => \(a\) | Suc \(m\) => \(b\)  | 
|
1385  | 
\end{ttbox}
 | 
|
1386  | 
Note that Isabelle insists on precisely this format; you may not even change  | 
|
1387  | 
the order of the two cases.  | 
|
1388  | 
Both \texttt{primrec} and \texttt{case} are realized by a recursion operator
 | 
|
| 7325 | 1389  | 
\cdx{nat_rec}, which is available because \textit{nat} is represented as
 | 
| 7490 | 1390  | 
a datatype (see {\S}\ref{subsec:datatype:rep_datatype}).
 | 
| 6580 | 1391  | 
|
1392  | 
%The predecessor relation, \cdx{pred_nat}, is shown to be well-founded.
 | 
|
1393  | 
%Recursion along this relation resembles primitive recursion, but is  | 
|
1394  | 
%stronger because we are in higher-order logic; using primitive recursion to  | 
|
1395  | 
%define a higher-order function, we can easily Ackermann's function, which  | 
|
1396  | 
%is not primitive recursive \cite[page~104]{thompson91}.
 | 
|
1397  | 
%The transitive closure of \cdx{pred_nat} is~$<$.  Many functions on the
 | 
|
1398  | 
%natural numbers are most easily expressed using recursion along~$<$.  | 
|
1399  | 
||
1400  | 
Tactic {\tt\ttindex{induct_tac} "$n$" $i$} performs induction on variable~$n$
 | 
|
1401  | 
in subgoal~$i$ using theorem \texttt{nat_induct}.  There is also the derived
 | 
|
1402  | 
theorem \tdx{less_induct}:
 | 
|
1403  | 
\begin{ttbox}
 | 
|
1404  | 
[| !!n. [| ! m. m<n --> P m |] ==> P n |] ==> P n  | 
|
1405  | 
\end{ttbox}
 | 
|
1406  | 
||
1407  | 
||
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1408  | 
\subsection{Numerical types and numerical reasoning}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1409  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1410  | 
The integers (type \tdx{int}) are also available in \HOL, and the reals (type
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1411  | 
\tdx{real}) are available in the logic image \texttt{HOL-Real}.  They support
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1412  | 
the expected operations of addition (\texttt{+}), subtraction (\texttt{-}) and
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1413  | 
multiplication (\texttt{*}), and much else.  Type \tdx{int} provides the
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1414  | 
\texttt{div} and \texttt{mod} operators, while type \tdx{real} provides real
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1415  | 
division and other operations.  Both types belong to class \cldx{linorder}, so
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1416  | 
they inherit the relational operators and all the usual properties of linear  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1417  | 
orderings. For full details, please survey the theories in subdirectories  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1418  | 
\texttt{Integ} and \texttt{Real}.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1419  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1420  | 
All three numeric types admit numerals of the form \texttt{\#$sd\ldots d$},
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1421  | 
where $s$ is an optional minus sign and $d\ldots d$ is a string of digits.  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1422  | 
Numerals are represented internally by a datatype for binary notation, which  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1423  | 
allows numerical calculations to be performed by rewriting. For example, the  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1424  | 
integer division of \texttt{\#54342339} by \texttt{\#3452} takes about five
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1425  | 
seconds. By default, the simplifier cancels like terms on the opposite sites  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1426  | 
of relational operators (reducing \texttt{z+x<x+y} to \texttt{z<y}, for
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1427  | 
instance. The simplifier also collects like terms, replacing  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1428  | 
\texttt{x+y+x*\#3} by \texttt{\#4*x+y}.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1429  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1430  | 
Sometimes numerals are not wanted, because for example \texttt{n+\#3} does not
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1431  | 
match a pattern of the form \texttt{Suc $k$}.  You can re-arrange the form of
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1432  | 
an arithmetic expression by proving (via \texttt{subgoal_tac}) a lemma such
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1433  | 
as \texttt{n+\#3 = Suc (Suc (Suc n))}.  As an alternative, you can disable the
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1434  | 
fancier simplifications by using a basic simpset such as \texttt{HOL_ss}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1435  | 
rather than the default one, \texttt{simpset()}.
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1436  | 
|
| 6580 | 1437  | 
Reasoning about arithmetic inequalities can be tedious. Fortunately HOL  | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1438  | 
provides a decision procedure for quantifier-free linear arithmetic (that is,  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1439  | 
addition and subtraction). The simplifier invokes a weak version of this  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1440  | 
decision procedure automatically. If this is not sufficent, you can invoke the  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1441  | 
full procedure \ttindex{arith_tac} explicitly.  It copes with arbitrary
 | 
| 6580 | 1442  | 
formulae involving {\tt=}, {\tt<}, {\tt<=}, {\tt+}, {\tt-}, {\tt Suc}, {\tt
 | 
1443  | 
  min}, {\tt max} and numerical constants; other subterms are treated as
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1444  | 
atomic; subformulae not involving numerical types are ignored; quantified  | 
| 6580 | 1445  | 
subformulae are ignored unless they are positive universal or negative  | 
1446  | 
existential. Note that the running time is exponential in the number of  | 
|
1447  | 
occurrences of {\tt min}, {\tt max}, and {\tt-} because they require case
 | 
|
1448  | 
distinctions. Note also that \texttt{arith_tac} is not complete: if
 | 
|
1449  | 
divisibility plays a role, it may fail to prove a valid formula, for example  | 
|
1450  | 
$m+m \neq n+n+1$. Fortunately such examples are rare in practice.  | 
|
1451  | 
||
1452  | 
If \texttt{arith_tac} fails you, try to find relevant arithmetic results in
 | 
|
1453  | 
the library.  The theory \texttt{NatDef} contains theorems about {\tt<} and
 | 
|
1454  | 
{\tt<=}, the theory \texttt{Arith} contains theorems about \texttt{+},
 | 
|
1455  | 
\texttt{-} and \texttt{*}, and theory \texttt{Divides} contains theorems about
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1456  | 
\texttt{div} and \texttt{mod}.  Use \texttt{thms_containing} or the
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1457  | 
\texttt{find}-functions to locate them (see the {\em Reference Manual\/}).
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1458  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1459  | 
\index{nat@{\textit{nat}} type|)}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1460  | 
|
| 6580 | 1461  | 
|
1462  | 
\begin{figure}
 | 
|
1463  | 
\index{#@{\tt[]} symbol}
 | 
|
1464  | 
\index{#@{\tt\#} symbol}
 | 
|
1465  | 
\index{"@@{\tt\at} symbol}
 | 
|
1466  | 
\index{*"! symbol}
 | 
|
1467  | 
\begin{constants}
 | 
|
1468  | 
\it symbol & \it meta-type & \it priority & \it description \\  | 
|
1469  | 
\tt[] & $\alpha\,list$ & & empty list\\  | 
|
1470  | 
\tt \# & $[\alpha,\alpha\,list]\To \alpha\,list$ & Right 65 &  | 
|
1471  | 
list constructor \\  | 
|
1472  | 
  \cdx{null}    & $\alpha\,list \To bool$ & & emptiness test\\
 | 
|
1473  | 
  \cdx{hd}      & $\alpha\,list \To \alpha$ & & head \\
 | 
|
1474  | 
  \cdx{tl}      & $\alpha\,list \To \alpha\,list$ & & tail \\
 | 
|
1475  | 
  \cdx{last}    & $\alpha\,list \To \alpha$ & & last element \\
 | 
|
1476  | 
  \cdx{butlast} & $\alpha\,list \To \alpha\,list$ & & drop last element \\
 | 
|
1477  | 
\tt\at & $[\alpha\,list,\alpha\,list]\To \alpha\,list$ & Left 65 & append \\  | 
|
1478  | 
  \cdx{map}     & $(\alpha\To\beta) \To (\alpha\,list \To \beta\,list)$
 | 
|
1479  | 
& & apply to all\\  | 
|
1480  | 
  \cdx{filter}  & $(\alpha \To bool) \To (\alpha\,list \To \alpha\,list)$
 | 
|
1481  | 
& & filter functional\\  | 
|
1482  | 
  \cdx{set}& $\alpha\,list \To \alpha\,set$ & & elements\\
 | 
|
1483  | 
  \sdx{mem}  & $\alpha \To \alpha\,list \To bool$  &  Left 55   & membership\\
 | 
|
1484  | 
  \cdx{foldl}   & $(\beta\To\alpha\To\beta) \To \beta \To \alpha\,list \To \beta$ &
 | 
|
1485  | 
& iteration \\  | 
|
1486  | 
  \cdx{concat}   & $(\alpha\,list)list\To \alpha\,list$ & & concatenation \\
 | 
|
1487  | 
  \cdx{rev}     & $\alpha\,list \To \alpha\,list$ & & reverse \\
 | 
|
1488  | 
  \cdx{length}  & $\alpha\,list \To nat$ & & length \\
 | 
|
1489  | 
\tt! & $\alpha\,list \To nat \To \alpha$ & Left 100 & indexing \\  | 
|
1490  | 
  \cdx{take}, \cdx{drop} & $nat \To \alpha\,list \To \alpha\,list$ &&
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1491  | 
take/drop a prefix \\  | 
| 6580 | 1492  | 
  \cdx{takeWhile},\\
 | 
1493  | 
  \cdx{dropWhile} &
 | 
|
1494  | 
$(\alpha \To bool) \To \alpha\,list \To \alpha\,list$ &&  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1495  | 
take/drop a prefix  | 
| 6580 | 1496  | 
\end{constants}
 | 
1497  | 
\subcaption{Constants and infixes}
 | 
|
1498  | 
||
1499  | 
\begin{center} \tt\frenchspacing
 | 
|
1500  | 
\begin{tabular}{rrr} 
 | 
|
1501  | 
  \it external        & \it internal  & \it description \\{}
 | 
|
1502  | 
[$x@1$, $\dots$, $x@n$] & $x@1$ \# $\cdots$ \# $x@n$ \# [] &  | 
|
1503  | 
        \rm finite list \\{}
 | 
|
1504  | 
  [$x$:$l$. $P$]  & filter ($\lambda x{.}P$) $l$ & 
 | 
|
1505  | 
\rm list comprehension  | 
|
1506  | 
\end{tabular}
 | 
|
1507  | 
\end{center}
 | 
|
1508  | 
\subcaption{Translations}
 | 
|
1509  | 
\caption{The theory \thydx{List}} \label{hol-list}
 | 
|
1510  | 
\end{figure}
 | 
|
1511  | 
||
1512  | 
||
1513  | 
\begin{figure}
 | 
|
1514  | 
\begin{ttbox}\makeatother
 | 
|
1515  | 
null [] = True  | 
|
1516  | 
null (x#xs) = False  | 
|
1517  | 
||
1518  | 
hd (x#xs) = x  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1519  | 
|
| 6580 | 1520  | 
tl (x#xs) = xs  | 
1521  | 
tl [] = []  | 
|
1522  | 
||
1523  | 
[] @ ys = ys  | 
|
1524  | 
(x#xs) @ ys = x # xs @ ys  | 
|
1525  | 
||
1526  | 
set [] = \ttlbrace\ttrbrace  | 
|
1527  | 
set (x#xs) = insert x (set xs)  | 
|
1528  | 
||
1529  | 
x mem [] = False  | 
|
1530  | 
x mem (y#ys) = (if y=x then True else x mem ys)  | 
|
1531  | 
||
1532  | 
concat([]) = []  | 
|
1533  | 
concat(x#xs) = x @ concat(xs)  | 
|
1534  | 
||
1535  | 
rev([]) = []  | 
|
1536  | 
rev(x#xs) = rev(xs) @ [x]  | 
|
1537  | 
||
1538  | 
length([]) = 0  | 
|
1539  | 
length(x#xs) = Suc(length(xs))  | 
|
1540  | 
||
1541  | 
xs!0 = hd xs  | 
|
1542  | 
xs!(Suc n) = (tl xs)!n  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1543  | 
\end{ttbox}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1544  | 
\caption{Simple list processing functions}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1545  | 
\label{fig:HOL:list-simps}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1546  | 
\end{figure}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1547  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1548  | 
\begin{figure}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1549  | 
\begin{ttbox}\makeatother
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1550  | 
map f [] = []  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1551  | 
map f (x#xs) = f x # map f xs  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1552  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1553  | 
filter P [] = []  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1554  | 
filter P (x#xs) = (if P x then x#filter P xs else filter P xs)  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1555  | 
|
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1556  | 
foldl f a [] = a  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1557  | 
foldl f a (x#xs) = foldl f (f a x) xs  | 
| 6580 | 1558  | 
|
1559  | 
take n [] = []  | 
|
1560  | 
take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)  | 
|
1561  | 
||
1562  | 
drop n [] = []  | 
|
1563  | 
drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)  | 
|
1564  | 
||
1565  | 
takeWhile P [] = []  | 
|
1566  | 
takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])  | 
|
1567  | 
||
1568  | 
dropWhile P [] = []  | 
|
1569  | 
dropWhile P (x#xs) = (if P x then dropWhile P xs else xs)  | 
|
1570  | 
\end{ttbox}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1571  | 
\caption{Further list processing functions}
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1572  | 
\label{fig:HOL:list-simps2}
 | 
| 6580 | 1573  | 
\end{figure}
 | 
1574  | 
||
1575  | 
||
1576  | 
\subsection{The type constructor for lists, \textit{list}}
 | 
|
1577  | 
\label{subsec:list}
 | 
|
1578  | 
\index{list@{\textit{list}} type|(}
 | 
|
1579  | 
||
1580  | 
Figure~\ref{hol-list} presents the theory \thydx{List}: the basic list
 | 
|
1581  | 
operations with their types and syntax. Type $\alpha \; list$ is  | 
|
1582  | 
defined as a \texttt{datatype} with the constructors {\tt[]} and {\tt\#}.
 | 
|
1583  | 
As a result the generic structural induction and case analysis tactics  | 
|
| 8424 | 1584  | 
\texttt{induct\_tac} and \texttt{cases\_tac} also become available for
 | 
| 6580 | 1585  | 
lists.  A \sdx{case} construct of the form
 | 
1586  | 
\begin{center}\tt
 | 
|
1587  | 
case $e$ of [] => $a$ | \(x\)\#\(xs\) => b  | 
|
1588  | 
\end{center}
 | 
|
| 7490 | 1589  | 
is defined by translation.  For details see~{\S}\ref{sec:HOL:datatype}. There
 | 
| 6580 | 1590  | 
is also a case splitting rule \tdx{split_list_case}
 | 
1591  | 
\[  | 
|
1592  | 
\begin{array}{l}
 | 
|
1593  | 
P(\mathtt{case}~e~\mathtt{of}~\texttt{[] =>}~a ~\texttt{|}~
 | 
|
1594  | 
               x\texttt{\#}xs~\texttt{=>}~f~x~xs) ~= \\
 | 
|
1595  | 
((e = \texttt{[]} \to P(a)) \land
 | 
|
1596  | 
 (\forall x~ xs. e = x\texttt{\#}xs \to P(f~x~xs)))
 | 
|
1597  | 
\end{array}
 | 
|
1598  | 
\]  | 
|
1599  | 
which can be fed to \ttindex{addsplits} just like
 | 
|
| 7490 | 1600  | 
\texttt{split_if} (see~{\S}\ref{subsec:HOL:case:splitting}).
 | 
| 6580 | 1601  | 
|
1602  | 
\texttt{List} provides a basic library of list processing functions defined by
 | 
|
| 7490 | 1603  | 
primitive recursion (see~{\S}\ref{sec:HOL:primrec}).  The recursion equations
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1604  | 
are shown in Figs.\ts\ref{fig:HOL:list-simps} and~\ref{fig:HOL:list-simps2}.
 | 
| 6580 | 1605  | 
|
1606  | 
\index{list@{\textit{list}} type|)}
 | 
|
1607  | 
||
1608  | 
||
1609  | 
\subsection{Introducing new types} \label{sec:typedef}
 | 
|
1610  | 
||
1611  | 
The \HOL-methodology dictates that all extensions to a theory should  | 
|
1612  | 
be \textbf{definitional}.  The type definition mechanism that
 | 
|
1613  | 
meets this criterion is \ttindex{typedef}.  Note that \emph{type synonyms},
 | 
|
1614  | 
which are inherited from {\Pure} and described elsewhere, are just
 | 
|
1615  | 
syntactic abbreviations that have no logical meaning.  | 
|
1616  | 
||
1617  | 
\begin{warn}
 | 
|
1618  | 
Types in \HOL\ must be non-empty; otherwise the quantifier rules would be  | 
|
| 7490 | 1619  | 
  unsound, because $\exists x. x=x$ is a theorem \cite[{\S}7]{paulson-COLOG}.
 | 
| 6580 | 1620  | 
\end{warn}
 | 
1621  | 
A \bfindex{type definition} identifies the new type with a subset of
 | 
|
1622  | 
an existing type. More precisely, the new type is defined by  | 
|
1623  | 
exhibiting an existing type~$\tau$, a set~$A::\tau\,set$, and a  | 
|
1624  | 
theorem of the form $x:A$. Thus~$A$ is a non-empty subset of~$\tau$,  | 
|
1625  | 
and the new type denotes this subset. New functions are defined that  | 
|
1626  | 
establish an isomorphism between the new type and the subset. If  | 
|
1627  | 
type~$\tau$ involves type variables $\alpha@1$, \ldots, $\alpha@n$,  | 
|
1628  | 
then the type definition creates a type constructor  | 
|
1629  | 
$(\alpha@1,\ldots,\alpha@n)ty$ rather than a particular type.  | 
|
1630  | 
||
1631  | 
\begin{figure}[htbp]
 | 
|
1632  | 
\begin{rail}
 | 
|
1633  | 
typedef  : 'typedef' ( () | '(' name ')') type '=' set witness;
 | 
|
1634  | 
||
1635  | 
type    : typevarlist name ( () | '(' infix ')' );
 | 
|
1636  | 
set : string;  | 
|
1637  | 
witness : () | '(' id ')';
 | 
|
1638  | 
\end{rail}
 | 
|
1639  | 
\caption{Syntax of type definitions}
 | 
|
1640  | 
\label{fig:HOL:typedef}
 | 
|
1641  | 
\end{figure}
 | 
|
1642  | 
||
1643  | 
The syntax for type definitions is shown in Fig.~\ref{fig:HOL:typedef}.  For
 | 
|
1644  | 
the definition of `typevarlist' and `infix' see  | 
|
1645  | 
\iflabelundefined{chap:classical}
 | 
|
1646  | 
{the appendix of the {\em Reference Manual\/}}%
 | 
|
1647  | 
{Appendix~\ref{app:TheorySyntax}}.  The remaining nonterminals have the
 | 
|
1648  | 
following meaning:  | 
|
1649  | 
\begin{description}
 | 
|
1650  | 
\item[\it type:] the new type constructor $(\alpha@1,\dots,\alpha@n)ty$ with  | 
|
1651  | 
optional infix annotation.  | 
|
1652  | 
\item[\it name:] an alphanumeric name $T$ for the type constructor  | 
|
1653  | 
$ty$, in case $ty$ is a symbolic name. Defaults to $ty$.  | 
|
1654  | 
\item[\it set:] the representing subset $A$.  | 
|
1655  | 
\item[\it witness:] name of a theorem of the form $a:A$ proving  | 
|
1656  | 
non-emptiness. It can be omitted in case Isabelle manages to prove  | 
|
1657  | 
non-emptiness automatically.  | 
|
1658  | 
\end{description}
 | 
|
1659  | 
If all context conditions are met (no duplicate type variables in  | 
|
1660  | 
`typevarlist', no extra type variables in `set', and no free term variables  | 
|
1661  | 
in `set'), the following components are added to the theory:  | 
|
1662  | 
\begin{itemize}
 | 
|
1663  | 
\item a type $ty :: (term,\dots,term)term$  | 
|
1664  | 
\item constants  | 
|
1665  | 
\begin{eqnarray*}
 | 
|
1666  | 
T &::& \tau\;set \\  | 
|
1667  | 
Rep_T &::& (\alpha@1,\dots,\alpha@n)ty \To \tau \\  | 
|
1668  | 
Abs_T &::& \tau \To (\alpha@1,\dots,\alpha@n)ty  | 
|
1669  | 
\end{eqnarray*}
 | 
|
1670  | 
\item a definition and three axioms  | 
|
1671  | 
\[  | 
|
1672  | 
\begin{array}{ll}
 | 
|
1673  | 
T{\tt_def} & T \equiv A \\
 | 
|
1674  | 
{\tt Rep_}T & Rep_T\,x \in T \\
 | 
|
1675  | 
{\tt Rep_}T{\tt_inverse} & Abs_T\,(Rep_T\,x) = x \\
 | 
|
1676  | 
{\tt Abs_}T{\tt_inverse} & y \in T \Imp Rep_T\,(Abs_T\,y) = y
 | 
|
1677  | 
\end{array}
 | 
|
1678  | 
\]  | 
|
1679  | 
stating that $(\alpha@1,\dots,\alpha@n)ty$ is isomorphic to $A$ by $Rep_T$  | 
|
1680  | 
and its inverse $Abs_T$.  | 
|
1681  | 
\end{itemize}
 | 
|
1682  | 
Below are two simple examples of \HOL\ type definitions. Non-emptiness  | 
|
1683  | 
is proved automatically here.  | 
|
1684  | 
\begin{ttbox}
 | 
|
1685  | 
typedef unit = "{\ttlbrace}True{\ttrbrace}"
 | 
|
1686  | 
||
1687  | 
typedef (prod)  | 
|
1688  | 
  ('a, 'b) "*"    (infixr 20)
 | 
|
1689  | 
      = "{\ttlbrace}f . EX (a::'a) (b::'b). f = (\%x y. x = a & y = b){\ttrbrace}"
 | 
|
1690  | 
\end{ttbox}
 | 
|
1691  | 
||
1692  | 
Type definitions permit the introduction of abstract data types in a safe  | 
|
1693  | 
way, namely by providing models based on already existing types. Given some  | 
|
1694  | 
abstract axiomatic description $P$ of a type, this involves two steps:  | 
|
1695  | 
\begin{enumerate}
 | 
|
1696  | 
\item Find an appropriate type $\tau$ and subset $A$ which has the desired  | 
|
1697  | 
properties $P$, and make a type definition based on this representation.  | 
|
1698  | 
\item Prove that $P$ holds for $ty$ by lifting $P$ from the representation.  | 
|
1699  | 
\end{enumerate}
 | 
|
1700  | 
You can now forget about the representation and work solely in terms of the  | 
|
1701  | 
abstract properties $P$.  | 
|
1702  | 
||
1703  | 
\begin{warn}
 | 
|
1704  | 
If you introduce a new type (constructor) $ty$ axiomatically, i.e.\ by  | 
|
1705  | 
declaring the type and its operations and by stating the desired axioms, you  | 
|
1706  | 
should make sure the type has a non-empty model. You must also have a clause  | 
|
1707  | 
\par  | 
|
1708  | 
\begin{ttbox}
 | 
|
1709  | 
arities \(ty\) :: (term,\thinspace\(\dots\),{\thinspace}term){\thinspace}term
 | 
|
1710  | 
\end{ttbox}
 | 
|
1711  | 
in your theory file to tell Isabelle that $ty$ is in class \texttt{term}, the
 | 
|
1712  | 
class of all \HOL\ types.  | 
|
1713  | 
\end{warn}
 | 
|
1714  | 
||
1715  | 
||
1716  | 
\section{Records}
 | 
|
1717  | 
||
1718  | 
At a first approximation, records are just a minor generalisation of tuples,  | 
|
1719  | 
where components may be addressed by labels instead of just position (think of  | 
|
1720  | 
{\ML}, for example).  The version of records offered by Isabelle/HOL is
 | 
|
1721  | 
slightly more advanced, though, supporting \emph{extensible record schemes}.
 | 
|
1722  | 
This admits operations that are polymorphic with respect to record extension,  | 
|
1723  | 
yielding ``object-oriented'' effects like (single) inheritance. See also  | 
|
| 6592 | 1724  | 
\cite{NaraschewskiW-TPHOLs98} for more details on object-oriented
 | 
| 6580 | 1725  | 
verification and record subtyping in HOL.  | 
1726  | 
||
1727  | 
||
1728  | 
\subsection{Basics}
 | 
|
1729  | 
||
1730  | 
Isabelle/HOL supports fixed and schematic records both at the level of terms  | 
|
1731  | 
and types. The concrete syntax is as follows:  | 
|
1732  | 
||
1733  | 
\begin{center}
 | 
|
1734  | 
\begin{tabular}{l|l|l}
 | 
|
1735  | 
& record terms & record types \\ \hline  | 
|
1736  | 
  fixed & $\record{x = a\fs y = b}$ & $\record{x \ty A\fs y \ty B}$ \\
 | 
|
1737  | 
  schematic & $\record{x = a\fs y = b\fs \more = m}$ &
 | 
|
1738  | 
    $\record{x \ty A\fs y \ty B\fs \more \ty M}$ \\
 | 
|
1739  | 
\end{tabular}
 | 
|
1740  | 
\end{center}
 | 
|
1741  | 
||
1742  | 
\noindent The \textsc{ascii} representation of $\record{x = a}$ is \texttt{(| x = a |)}.
 | 
|
1743  | 
||
1744  | 
A fixed record $\record{x = a\fs y = b}$ has field $x$ of value $a$ and field
 | 
|
1745  | 
$y$ of value $b$.  The corresponding type is $\record{x \ty A\fs y \ty B}$,
 | 
|
1746  | 
assuming that $a \ty A$ and $b \ty B$.  | 
|
1747  | 
||
1748  | 
A record scheme like $\record{x = a\fs y = b\fs \more = m}$ contains fields
 | 
|
1749  | 
$x$ and $y$ as before, but also possibly further fields as indicated by the  | 
|
1750  | 
``$\more$'' notation (which is actually part of the syntax). The improper  | 
|
1751  | 
field ``$\more$'' of a record scheme is called the \emph{more part}.
 | 
|
1752  | 
Logically it is just a free variable, which is occasionally referred to as  | 
|
1753  | 
\emph{row variable} in the literature.  The more part of a record scheme may
 | 
|
1754  | 
be instantiated by zero or more further components. For example, above scheme  | 
|
1755  | 
might get instantiated to $\record{x = a\fs y = b\fs z = c\fs \more = m'}$,
 | 
|
1756  | 
where $m'$ refers to a different more part. Fixed records are special  | 
|
1757  | 
instances of record schemes, where ``$\more$'' is properly terminated by the  | 
|
1758  | 
$() :: unit$ element.  Actually, $\record{x = a\fs y = b}$ is just an
 | 
|
1759  | 
abbreviation for $\record{x = a\fs y = b\fs \more = ()}$.
 | 
|
1760  | 
||
1761  | 
\medskip  | 
|
1762  | 
||
1763  | 
There are two key features that make extensible records in a simply typed  | 
|
1764  | 
language like HOL feasible:  | 
|
1765  | 
\begin{enumerate}
 | 
|
1766  | 
\item the more part is internalised, as a free term or type variable,  | 
|
1767  | 
\item field names are externalised, they cannot be accessed within the logic  | 
|
1768  | 
as first-class values.  | 
|
1769  | 
\end{enumerate}
 | 
|
1770  | 
||
1771  | 
\medskip  | 
|
1772  | 
||
1773  | 
In Isabelle/HOL record types have to be defined explicitly, fixing their field  | 
|
1774  | 
names and types, and their (optional) parent record (see  | 
|
| 7490 | 1775  | 
{\S}\ref{sec:HOL:record-def}).  Afterwards, records may be formed using above
 | 
| 6580 | 1776  | 
syntax, while obeying the canonical order of fields as given by their  | 
1777  | 
declaration. The record package also provides several operations like  | 
|
| 7490 | 1778  | 
selectors and updates (see {\S}\ref{sec:HOL:record-ops}), together with
 | 
1779  | 
characteristic properties (see {\S}\ref{sec:HOL:record-thms}).
 | 
|
| 6580 | 1780  | 
|
1781  | 
There is an example theory demonstrating most basic aspects of extensible  | 
|
1782  | 
records (see theory \texttt{HOL/ex/Points} in the Isabelle sources).
 | 
|
1783  | 
||
1784  | 
||
1785  | 
\subsection{Defining records}\label{sec:HOL:record-def}
 | 
|
1786  | 
||
1787  | 
The theory syntax for record type definitions is shown in  | 
|
1788  | 
Fig.~\ref{fig:HOL:record}.  For the definition of `typevarlist' and `type' see
 | 
|
1789  | 
\iflabelundefined{chap:classical}
 | 
|
1790  | 
{the appendix of the {\em Reference Manual\/}}%
 | 
|
1791  | 
{Appendix~\ref{app:TheorySyntax}}.
 | 
|
1792  | 
||
1793  | 
\begin{figure}[htbp]
 | 
|
1794  | 
\begin{rail}
 | 
|
1795  | 
record : 'record' typevarlist name '=' parent (field +);  | 
|
1796  | 
||
1797  | 
parent : ( () | type '+');  | 
|
1798  | 
field : name '::' type;  | 
|
1799  | 
\end{rail}
 | 
|
1800  | 
\caption{Syntax of record type definitions}
 | 
|
1801  | 
\label{fig:HOL:record}
 | 
|
1802  | 
\end{figure}
 | 
|
1803  | 
||
1804  | 
A general \ttindex{record} specification is of the following form:
 | 
|
1805  | 
\[  | 
|
1806  | 
\mathtt{record}~(\alpha@1, \dots, \alpha@n) \, t ~=~
 | 
|
1807  | 
(\tau@1, \dots, \tau@m) \, s ~+~ c@1 :: \sigma@1 ~ \dots ~ c@l :: \sigma@l  | 
|
1808  | 
\]  | 
|
1809  | 
where $\vec\alpha@n$ are distinct type variables, and $\vec\tau@m$,  | 
|
1810  | 
$\vec\sigma@l$ are types containing at most variables from $\vec\alpha@n$.  | 
|
1811  | 
Type constructor $t$ has to be new, while $s$ has to specify an existing  | 
|
1812  | 
record type. Furthermore, the $\vec c@l$ have to be distinct field names.  | 
|
1813  | 
There has to be at least one field.  | 
|
1814  | 
||
1815  | 
In principle, field names may never be shared with other records. This is no  | 
|
1816  | 
actual restriction in practice, since $\vec c@l$ are internally declared  | 
|
1817  | 
within a separate name space qualified by the name $t$ of the record.  | 
|
1818  | 
||
1819  | 
\medskip  | 
|
1820  | 
||
1821  | 
Above definition introduces a new record type $(\vec\alpha@n) \, t$ by  | 
|
1822  | 
extending an existing one $(\vec\tau@m) \, s$ by new fields $\vec c@l \ty  | 
|
1823  | 
\vec\sigma@l$. The parent record specification is optional, by omitting it  | 
|
1824  | 
$t$ becomes a \emph{root record}.  The hierarchy of all records declared
 | 
|
1825  | 
within a theory forms a forest structure, i.e.\ a set of trees, where any of  | 
|
1826  | 
these is rooted by some root record.  | 
|
1827  | 
||
1828  | 
For convenience, $(\vec\alpha@n) \, t$ is made a type abbreviation for the  | 
|
1829  | 
fixed record type $\record{\vec c@l \ty \vec\sigma@l}$, and $(\vec\alpha@n,
 | 
|
1830  | 
\zeta) \, t_scheme$ is made an abbreviation for $\record{\vec c@l \ty
 | 
|
1831  | 
\vec\sigma@l\fs \more \ty \zeta}$.  | 
|
1832  | 
||
1833  | 
\medskip  | 
|
1834  | 
||
1835  | 
The following simple example defines a root record type $point$ with fields $x  | 
|
1836  | 
\ty nat$ and $y \ty nat$, and record type $cpoint$ by extending $point$ with  | 
|
1837  | 
an additional $colour$ component.  | 
|
1838  | 
||
1839  | 
\begin{ttbox}
 | 
|
1840  | 
record point =  | 
|
1841  | 
x :: nat  | 
|
1842  | 
y :: nat  | 
|
1843  | 
||
1844  | 
record cpoint = point +  | 
|
1845  | 
colour :: string  | 
|
1846  | 
\end{ttbox}
 | 
|
1847  | 
||
1848  | 
||
1849  | 
\subsection{Record operations}\label{sec:HOL:record-ops}
 | 
|
1850  | 
||
1851  | 
Any record definition of the form presented above produces certain standard  | 
|
1852  | 
operations. Selectors and updates are provided for any field, including the  | 
|
1853  | 
improper one ``$more$''. There are also cumulative record constructor  | 
|
1854  | 
functions.  | 
|
1855  | 
||
1856  | 
To simplify the presentation below, we first assume that $(\vec\alpha@n) \, t$  | 
|
1857  | 
is a root record with fields $\vec c@l \ty \vec\sigma@l$.  | 
|
1858  | 
||
1859  | 
\medskip  | 
|
1860  | 
||
1861  | 
\textbf{Selectors} and \textbf{updates} are available for any field (including
 | 
|
1862  | 
``$more$'') as follows:  | 
|
1863  | 
\begin{matharray}{lll}
 | 
|
1864  | 
  c@i & \ty & \record{\vec c@l \ty \vec \sigma@l, \more \ty \zeta} \To \sigma@i \\
 | 
|
1865  | 
  c@i_update & \ty & \sigma@i \To \record{\vec c@l \ty \vec \sigma@l, \more \ty \zeta} \To
 | 
|
1866  | 
    \record{\vec c@l \ty \vec \sigma@l, \more \ty \zeta}
 | 
|
1867  | 
\end{matharray}
 | 
|
1868  | 
||
1869  | 
There is some special syntax for updates: $r \, \record{x \asn a}$ abbreviates
 | 
|
1870  | 
term $x_update \, a \, r$. Repeated updates are also supported: $r \,  | 
|
1871  | 
\record{x \asn a} \, \record{y \asn b} \, \record{z \asn c}$ may be written as
 | 
|
1872  | 
$r \, \record{x \asn a\fs y \asn b\fs z \asn c}$.  Note that because of
 | 
|
1873  | 
postfix notation the order of fields shown here is reverse than in the actual  | 
|
1874  | 
term. This might lead to confusion in conjunction with proof tools like  | 
|
1875  | 
ordered rewriting.  | 
|
1876  | 
||
1877  | 
Since repeated updates are just function applications, fields may be freely  | 
|
1878  | 
permuted in $\record{x \asn a\fs y \asn b\fs z \asn c}$, as far as the logic
 | 
|
1879  | 
is concerned. Thus commutativity of updates can be proven within the logic  | 
|
1880  | 
for any two fields, but not as a general theorem: fields are not first-class  | 
|
1881  | 
values.  | 
|
1882  | 
||
1883  | 
\medskip  | 
|
1884  | 
||
1885  | 
\textbf{Make} operations provide cumulative record constructor functions:
 | 
|
1886  | 
\begin{matharray}{lll}
 | 
|
1887  | 
  make & \ty & \vec\sigma@l \To \record{\vec c@l \ty \vec \sigma@l} \\
 | 
|
1888  | 
make_scheme & \ty & \vec\sigma@l \To  | 
|
1889  | 
    \zeta \To \record{\vec c@l \ty \vec \sigma@l, \more \ty \zeta} \\
 | 
|
1890  | 
\end{matharray}
 | 
|
1891  | 
\noindent  | 
|
1892  | 
These functions are curried. The corresponding definitions in terms of actual  | 
|
1893  | 
record terms are part of the standard simpset. Thus $point\dtt make\,a\,b$  | 
|
1894  | 
rewrites to $\record{x = a\fs y = b}$.
 | 
|
1895  | 
||
1896  | 
\medskip  | 
|
1897  | 
||
1898  | 
Any of above selector, update and make operations are declared within a local  | 
|
1899  | 
name space prefixed by the name $t$ of the record. In case that different  | 
|
1900  | 
records share base names of fields, one has to qualify names explicitly (e.g.\  | 
|
1901  | 
$t\dtt c@i_update$). This is recommended especially for operations like  | 
|
1902  | 
$make$ or $update_more$ that always have the same base name. Just use $t\dtt  | 
|
1903  | 
make$ etc.\ to avoid confusion.  | 
|
1904  | 
||
1905  | 
\bigskip  | 
|
1906  | 
||
1907  | 
We reconsider the case of non-root records, which are derived of some parent  | 
|
1908  | 
record. In general, the latter may depend on another parent as well,  | 
|
1909  | 
resulting in a list of \emph{ancestor records}.  Appending the lists of fields
 | 
|
1910  | 
of all ancestors results in a certain field prefix. The record package  | 
|
1911  | 
automatically takes care of this by lifting operations over this context of  | 
|
1912  | 
ancestor fields. Assuming that $(\vec\alpha@n) \, t$ has ancestor fields  | 
|
1913  | 
$\vec d@k \ty \vec\rho@k$, selectors will get the following types:  | 
|
1914  | 
\begin{matharray}{lll}
 | 
|
1915  | 
  c@i & \ty & \record{\vec d@k \ty \vec\rho@k, \vec c@l \ty \vec \sigma@l, \more \ty \zeta}
 | 
|
1916  | 
\To \sigma@i  | 
|
1917  | 
\end{matharray}
 | 
|
1918  | 
\noindent  | 
|
1919  | 
Update and make operations are analogous.  | 
|
1920  | 
||
1921  | 
||
1922  | 
\subsection{Proof tools}\label{sec:HOL:record-thms}
 | 
|
1923  | 
||
1924  | 
The record package provides the following proof rules for any record type $t$.  | 
|
1925  | 
\begin{enumerate}
 | 
|
1926  | 
||
1927  | 
\item Standard conversions (selectors or updates applied to record constructor  | 
|
1928  | 
terms, make function definitions) are part of the standard simpset (via  | 
|
1929  | 
  \texttt{addsimps}).
 | 
|
1930  | 
||
| 7328 | 1931  | 
\item Selectors applied to updated records are automatically reduced by  | 
1932  | 
  simplification procedure \ttindex{record_simproc}, which is part of the
 | 
|
1933  | 
default simpset.  | 
|
1934  | 
||
| 6580 | 1935  | 
\item Inject equations of a form analogous to $((x, y) = (x', y')) \equiv x=x'  | 
1936  | 
\conj y=y'$ are made part of the standard simpset and claset (via  | 
|
1937  | 
  \texttt{addIffs}).
 | 
|
1938  | 
||
| 7328 | 1939  | 
\item A tactic for record field splitting (\ttindex{record_split_tac}) may be
 | 
1940  | 
  made part of the claset (via \texttt{addSWrapper}).  This tactic is based on
 | 
|
1941  | 
  rules analogous to $(\All x PROP~P~x) \equiv (\All{a~b} PROP~P(a, b))$ for
 | 
|
1942  | 
any field.  | 
|
| 6580 | 1943  | 
\end{enumerate}
 | 
1944  | 
||
1945  | 
The first two kinds of rules are stored within the theory as $t\dtt simps$ and  | 
|
1946  | 
$t\dtt iffs$, respectively. In some situations it might be appropriate to  | 
|
| 7328 | 1947  | 
expand the definitions of updates: $t\dtt update_defs$. Note that these names  | 
1948  | 
are \emph{not} bound at the {\ML} level.
 | 
|
| 6580 | 1949  | 
|
1950  | 
\medskip  | 
|
1951  | 
||
1952  | 
The example theory \texttt{HOL/ex/Points} demonstrates typical proofs
 | 
|
1953  | 
concerning records.  The basic idea is to make \ttindex{record_split_tac}
 | 
|
1954  | 
expand quantified record variables and then simplify by the conversion rules.  | 
|
1955  | 
By using a combination of the simplifier and classical prover together with  | 
|
1956  | 
the default simpset and claset, record problems should be solved with a single  | 
|
| 7328 | 1957  | 
stroke of \texttt{Auto_tac} or \texttt{Force_tac}.  Most of the time, plain
 | 
1958  | 
\texttt{Simp_tac} should be sufficient, though.
 | 
|
| 6580 | 1959  | 
|
1960  | 
||
1961  | 
\section{Datatype definitions}
 | 
|
1962  | 
\label{sec:HOL:datatype}
 | 
|
1963  | 
\index{*datatype|(}
 | 
|
1964  | 
||
| 6626 | 1965  | 
Inductive datatypes, similar to those of \ML, frequently appear in  | 
| 6580 | 1966  | 
applications of Isabelle/HOL. In principle, such types could be defined by  | 
| 7490 | 1967  | 
hand via \texttt{typedef} (see {\S}\ref{sec:typedef}), but this would be far too
 | 
| 6626 | 1968  | 
tedious.  The \ttindex{datatype} definition package of Isabelle/HOL (cf.\ 
 | 
1969  | 
\cite{Berghofer-Wenzel:1999:TPHOL}) automates such chores.  It generates an
 | 
|
1970  | 
appropriate \texttt{typedef} based on a least fixed-point construction, and
 | 
|
1971  | 
proves freeness theorems and induction rules, as well as theorems for  | 
|
1972  | 
recursion and case combinators. The user just has to give a simple  | 
|
1973  | 
specification of new inductive types using a notation similar to {\ML} or
 | 
|
1974  | 
Haskell.  | 
|
| 6580 | 1975  | 
|
1976  | 
The current datatype package can handle both mutual and indirect recursion.  | 
|
1977  | 
It also offers to represent existing types as datatypes giving the advantage  | 
|
1978  | 
of a more uniform view on standard theories.  | 
|
1979  | 
||
1980  | 
||
1981  | 
\subsection{Basics}
 | 
|
1982  | 
\label{subsec:datatype:basics}
 | 
|
1983  | 
||
1984  | 
A general \texttt{datatype} definition is of the following form:
 | 
|
1985  | 
\[  | 
|
1986  | 
\begin{array}{llcl}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1987  | 
\mathtt{datatype} & (\vec{\alpha})t@1 & = &
 | 
| 6580 | 1988  | 
  C^1@1~\tau^1@{1,1}~\ldots~\tau^1@{1,m^1@1} ~\mid~ \ldots ~\mid~
 | 
1989  | 
    C^1@{k@1}~\tau^1@{k@1,1}~\ldots~\tau^1@{k@1,m^1@{k@1}} \\
 | 
|
1990  | 
& & \vdots \\  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1991  | 
\mathtt{and} & (\vec{\alpha})t@n & = &
 | 
| 6580 | 1992  | 
  C^n@1~\tau^n@{1,1}~\ldots~\tau^n@{1,m^n@1} ~\mid~ \ldots ~\mid~
 | 
1993  | 
    C^n@{k@n}~\tau^n@{k@n,1}~\ldots~\tau^n@{k@n,m^n@{k@n}}
 | 
|
1994  | 
\end{array}
 | 
|
1995  | 
\]  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1996  | 
where $\vec{\alpha} = (\alpha@1,\ldots,\alpha@h)$ is a list of type variables,
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1997  | 
$C^j@i$ are distinct constructor names and $\tau^j@{i,i'}$ are {\em
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1998  | 
admissible} types containing at most the type variables $\alpha@1, \ldots,  | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
1999  | 
\alpha@h$. A type $\tau$ occurring in a \texttt{datatype} definition is {\em
 | 
| 9258 | 2000  | 
admissible} if and only if  | 
| 6580 | 2001  | 
\begin{itemize}
 | 
2002  | 
\item $\tau$ is non-recursive, i.e.\ $\tau$ does not contain any of the  | 
|
2003  | 
newly defined type constructors $t@1,\ldots,t@n$, or  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2004  | 
\item $\tau = (\vec{\alpha})t@{j'}$ where $1 \leq j' \leq n$, or
 | 
| 6580 | 2005  | 
\item $\tau = (\tau'@1,\ldots,\tau'@{h'})t'$, where $t'$ is
 | 
2006  | 
the type constructor of an already existing datatype and $\tau'@1,\ldots,\tau'@{h'}$
 | 
|
2007  | 
are admissible types.  | 
|
| 7490 | 2008  | 
\item $\tau = \sigma \to \tau'$, where $\tau'$ is an admissible  | 
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2009  | 
type and $\sigma$ is non-recursive (i.e. the occurrences of the newly defined  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2010  | 
types are {\em strictly positive})
 | 
| 6580 | 2011  | 
\end{itemize}
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2012  | 
If some $(\vec{\alpha})t@{j'}$ occurs in a type $\tau^j@{i,i'}$
 | 
| 6580 | 2013  | 
of the form  | 
2014  | 
\[  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2015  | 
(\ldots,\ldots ~ (\vec{\alpha})t@{j'} ~ \ldots,\ldots)t'
 | 
| 6580 | 2016  | 
\]  | 
2017  | 
this is called a {\em nested} (or \emph{indirect}) occurrence. A very simple
 | 
|
2018  | 
example of a datatype is the type \texttt{list}, which can be defined by
 | 
|
2019  | 
\begin{ttbox}
 | 
|
2020  | 
datatype 'a list = Nil  | 
|
2021  | 
                 | Cons 'a ('a list)
 | 
|
2022  | 
\end{ttbox}
 | 
|
2023  | 
Arithmetic expressions \texttt{aexp} and boolean expressions \texttt{bexp} can be modelled
 | 
|
2024  | 
by the mutually recursive datatype definition  | 
|
2025  | 
\begin{ttbox}
 | 
|
2026  | 
datatype 'a aexp = If_then_else ('a bexp) ('a aexp) ('a aexp)
 | 
|
2027  | 
                 | Sum ('a aexp) ('a aexp)
 | 
|
2028  | 
                 | Diff ('a aexp) ('a aexp)
 | 
|
2029  | 
| Var 'a  | 
|
2030  | 
| Num nat  | 
|
2031  | 
and      'a bexp = Less ('a aexp) ('a aexp)
 | 
|
2032  | 
                 | And ('a bexp) ('a bexp)
 | 
|
2033  | 
                 | Or ('a bexp) ('a bexp)
 | 
|
2034  | 
\end{ttbox}
 | 
|
2035  | 
The datatype \texttt{term}, which is defined by
 | 
|
2036  | 
\begin{ttbox}
 | 
|
2037  | 
datatype ('a, 'b) term = Var 'a
 | 
|
2038  | 
                       | App 'b ((('a, 'b) term) list)
 | 
|
2039  | 
\end{ttbox}
 | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2040  | 
is an example for a datatype with nested recursion. Using nested recursion  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2041  | 
involving function spaces, we may also define infinitely branching datatypes, e.g.  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2042  | 
\begin{ttbox}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2043  | 
datatype 'a tree = Atom 'a | Branch "nat => 'a tree"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2044  | 
\end{ttbox}
 | 
| 6580 | 2045  | 
|
2046  | 
\medskip  | 
|
2047  | 
||
2048  | 
Types in HOL must be non-empty. Each of the new datatypes  | 
|
| 9258 | 2049  | 
$(\vec{\alpha})t@j$ with $1 \leq j \leq n$ is non-empty if and only if it has a
 | 
| 6580 | 2050  | 
constructor $C^j@i$ with the following property: for all argument types  | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2051  | 
$\tau^j@{i,i'}$ of the form $(\vec{\alpha})t@{j'}$ the datatype
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2052  | 
$(\vec{\alpha})t@{j'}$ is non-empty.
 | 
| 6580 | 2053  | 
|
2054  | 
If there are no nested occurrences of the newly defined datatypes, obviously  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2055  | 
at least one of the newly defined datatypes $(\vec{\alpha})t@j$
 | 
| 6580 | 2056  | 
must have a constructor $C^j@i$ without recursive arguments, a \emph{base
 | 
2057  | 
case}, to ensure that the new types are non-empty. If there are nested  | 
|
2058  | 
occurrences, a datatype can even be non-empty without having a base case  | 
|
2059  | 
itself. Since \texttt{list} is a non-empty datatype, \texttt{datatype t = C (t
 | 
|
2060  | 
list)} is non-empty as well.  | 
|
2061  | 
||
2062  | 
||
2063  | 
\subsubsection{Freeness of the constructors}
 | 
|
2064  | 
||
2065  | 
The datatype constructors are automatically defined as functions of their  | 
|
2066  | 
respective type:  | 
|
2067  | 
\[ C^j@i :: [\tau^j@{i,1},\dots,\tau^j@{i,m^j@i}] \To (\alpha@1,\dots,\alpha@h)t@j \]
 | 
|
2068  | 
These functions have certain {\em freeness} properties.  They construct
 | 
|
2069  | 
distinct values:  | 
|
2070  | 
\[  | 
|
2071  | 
C^j@i~x@1~\dots~x@{m^j@i} \neq C^j@{i'}~y@1~\dots~y@{m^j@{i'}} \qquad
 | 
|
2072  | 
\mbox{for all}~ i \neq i'.
 | 
|
2073  | 
\]  | 
|
2074  | 
The constructor functions are injective:  | 
|
2075  | 
\[  | 
|
2076  | 
(C^j@i~x@1~\dots~x@{m^j@i} = C^j@i~y@1~\dots~y@{m^j@i}) =
 | 
|
2077  | 
(x@1 = y@1 \land \dots \land x@{m^j@i} = y@{m^j@i})
 | 
|
2078  | 
\]  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2079  | 
Since the number of distinctness inequalities is quadratic in the number of  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2080  | 
constructors, the datatype package avoids proving them separately if there are  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2081  | 
too many constructors. Instead, specific inequalities are proved by a suitable  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2082  | 
simplification procedure on demand.\footnote{This procedure, which is already part
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2083  | 
of the default simpset, may be referred to by the ML identifier  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2084  | 
\texttt{DatatypePackage.distinct_simproc}.}
 | 
| 6580 | 2085  | 
|
2086  | 
\subsubsection{Structural induction}
 | 
|
2087  | 
||
2088  | 
The datatype package also provides structural induction rules. For  | 
|
2089  | 
datatypes without nested recursion, this is of the following form:  | 
|
2090  | 
\[  | 
|
| 7490 | 2091  | 
\infer{P@1~x@1 \land \dots \land P@n~x@n}
 | 
| 6580 | 2092  | 
  {\begin{array}{lcl}
 | 
2093  | 
     \Forall x@1 \dots x@{m^1@1}.
 | 
|
2094  | 
       \List{P@{s^1@{1,1}}~x@{r^1@{1,1}}; \dots;
 | 
|
2095  | 
         P@{s^1@{1,l^1@1}}~x@{r^1@{1,l^1@1}}} & \Imp &
 | 
|
2096  | 
           P@1~\left(C^1@1~x@1~\dots~x@{m^1@1}\right) \\
 | 
|
2097  | 
& \vdots \\  | 
|
2098  | 
     \Forall x@1 \dots x@{m^1@{k@1}}.
 | 
|
2099  | 
       \List{P@{s^1@{k@1,1}}~x@{r^1@{k@1,1}}; \dots;
 | 
|
2100  | 
         P@{s^1@{k@1,l^1@{k@1}}}~x@{r^1@{k@1,l^1@{k@1}}}} & \Imp &
 | 
|
2101  | 
           P@1~\left(C^1@{k@1}~x@1~\ldots~x@{m^1@{k@1}}\right) \\
 | 
|
2102  | 
& \vdots \\  | 
|
2103  | 
     \Forall x@1 \dots x@{m^n@1}.
 | 
|
2104  | 
       \List{P@{s^n@{1,1}}~x@{r^n@{1,1}}; \dots;
 | 
|
2105  | 
         P@{s^n@{1,l^n@1}}~x@{r^n@{1,l^n@1}}} & \Imp &
 | 
|
2106  | 
           P@n~\left(C^n@1~x@1~\ldots~x@{m^n@1}\right) \\
 | 
|
2107  | 
& \vdots \\  | 
|
2108  | 
     \Forall x@1 \dots x@{m^n@{k@n}}.
 | 
|
2109  | 
       \List{P@{s^n@{k@n,1}}~x@{r^n@{k@n,1}}; \dots
 | 
|
2110  | 
         P@{s^n@{k@n,l^n@{k@n}}}~x@{r^n@{k@n,l^n@{k@n}}}} & \Imp &
 | 
|
2111  | 
           P@n~\left(C^n@{k@n}~x@1~\ldots~x@{m^n@{k@n}}\right)
 | 
|
2112  | 
   \end{array}}
 | 
|
2113  | 
\]  | 
|
2114  | 
where  | 
|
2115  | 
\[  | 
|
2116  | 
\begin{array}{rcl}
 | 
|
2117  | 
Rec^j@i & := &  | 
|
2118  | 
   \left\{\left(r^j@{i,1},s^j@{i,1}\right),\ldots,
 | 
|
2119  | 
     \left(r^j@{i,l^j@i},s^j@{i,l^j@i}\right)\right\} = \\[2ex]
 | 
|
2120  | 
&& \left\{(i',i'')~\left|~
 | 
|
| 7490 | 2121  | 
1\leq i' \leq m^j@i \land 1 \leq i'' \leq n \land  | 
| 6580 | 2122  | 
       \tau^j@{i,i'} = (\alpha@1,\ldots,\alpha@h)t@{i''}\right.\right\}
 | 
2123  | 
\end{array}
 | 
|
2124  | 
\]  | 
|
2125  | 
i.e.\ the properties $P@j$ can be assumed for all recursive arguments.  | 
|
2126  | 
||
2127  | 
For datatypes with nested recursion, such as the \texttt{term} example from
 | 
|
2128  | 
above, things are a bit more complicated. Conceptually, Isabelle/HOL unfolds  | 
|
2129  | 
a definition like  | 
|
2130  | 
\begin{ttbox}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2131  | 
datatype ('a,'b) term = Var 'a
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2132  | 
                      | App 'b ((('a, 'b) term) list)
 | 
| 6580 | 2133  | 
\end{ttbox}
 | 
2134  | 
to an equivalent definition without nesting:  | 
|
2135  | 
\begin{ttbox}
 | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2136  | 
datatype ('a,'b) term      = Var
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2137  | 
                           | App 'b (('a, 'b) term_list)
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2138  | 
and      ('a,'b) term_list = Nil'
 | 
| 
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2139  | 
                           | Cons' (('a,'b) term) (('a,'b) term_list)
 | 
| 6580 | 2140  | 
\end{ttbox}
 | 
2141  | 
Note however, that the type \texttt{('a,'b) term_list} and the constructors {\tt
 | 
|
2142  | 
  Nil'} and \texttt{Cons'} are not really introduced.  One can directly work with
 | 
|
2143  | 
the original (isomorphic) type \texttt{(('a, 'b) term) list} and its existing
 | 
|
2144  | 
constructors \texttt{Nil} and \texttt{Cons}. Thus, the structural induction rule for
 | 
|
2145  | 
\texttt{term} gets the form
 | 
|
2146  | 
\[  | 
|
| 7490 | 2147  | 
\infer{P@1~x@1 \land P@2~x@2}
 | 
| 6580 | 2148  | 
  {\begin{array}{l}
 | 
2149  | 
     \Forall x.~P@1~(\mathtt{Var}~x) \\
 | 
|
2150  | 
     \Forall x@1~x@2.~P@2~x@2 \Imp P@1~(\mathtt{App}~x@1~x@2) \\
 | 
|
2151  | 
     P@2~\mathtt{Nil} \\
 | 
|
2152  | 
     \Forall x@1~x@2. \List{P@1~x@1; P@2~x@2} \Imp P@2~(\mathtt{Cons}~x@1~x@2)
 | 
|
2153  | 
   \end{array}}
 | 
|
2154  | 
\]  | 
|
2155  | 
Note that there are two predicates $P@1$ and $P@2$, one for the type \texttt{('a,'b) term}
 | 
|
2156  | 
and one for the type \texttt{(('a, 'b) term) list}.
 | 
|
2157  | 
||
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2158  | 
For a datatype with function types such as \texttt{'a tree}, the induction rule
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2159  | 
is of the form  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2160  | 
\[  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2161  | 
\infer{P~t}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2162  | 
  {\Forall a.~P~(\mathtt{Atom}~a) &
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2163  | 
   \Forall ts.~(\forall x.~P~(ts~x)) \Imp P~(\mathtt{Branch}~ts)}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2164  | 
\]  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2165  | 
|
| 6580 | 2166  | 
\medskip In principle, inductive types are already fully determined by  | 
2167  | 
freeness and structural induction. For convenience in applications,  | 
|
2168  | 
the following derived constructions are automatically provided for any  | 
|
2169  | 
datatype.  | 
|
2170  | 
||
2171  | 
\subsubsection{The \sdx{case} construct}
 | 
|
2172  | 
||
2173  | 
The type comes with an \ML-like \texttt{case}-construct:
 | 
|
2174  | 
\[  | 
|
2175  | 
\begin{array}{rrcl}
 | 
|
2176  | 
\mbox{\tt case}~e~\mbox{\tt of} & C^j@1~x@{1,1}~\dots~x@{1,m^j@1} & \To & e@1 \\
 | 
|
2177  | 
\vdots \\  | 
|
2178  | 
                           \mid & C^j@{k@j}~x@{k@j,1}~\dots~x@{k@j,m^j@{k@j}} & \To & e@{k@j}
 | 
|
2179  | 
\end{array}
 | 
|
2180  | 
\]  | 
|
2181  | 
where the $x@{i,j}$ are either identifiers or nested tuple patterns as in
 | 
|
| 7490 | 2182  | 
{\S}\ref{subsec:prod-sum}.
 | 
| 6580 | 2183  | 
\begin{warn}
 | 
2184  | 
All constructors must be present, their order is fixed, and nested patterns  | 
|
2185  | 
are not supported (with the exception of tuples). Violating this  | 
|
2186  | 
restriction results in strange error messages.  | 
|
2187  | 
\end{warn}
 | 
|
2188  | 
||
2189  | 
To perform case distinction on a goal containing a \texttt{case}-construct,
 | 
|
2190  | 
the theorem $t@j.$\texttt{split} is provided:
 | 
|
2191  | 
\[  | 
|
2192  | 
\begin{array}{@{}rcl@{}}
 | 
|
2193  | 
P(t@j_\mathtt{case}~f@1~\dots~f@{k@j}~e) &\!\!\!=&
 | 
|
2194  | 
\!\!\! ((\forall x@1 \dots x@{m^j@1}. e = C^j@1~x@1\dots x@{m^j@1} \to
 | 
|
2195  | 
                             P(f@1~x@1\dots x@{m^j@1})) \\
 | 
|
2196  | 
&&\!\!\! ~\land~ \dots ~\land \\  | 
|
2197  | 
&&~\!\!\! (\forall x@1 \dots x@{m^j@{k@j}}. e = C^j@{k@j}~x@1\dots x@{m^j@{k@j}} \to
 | 
|
2198  | 
                             P(f@{k@j}~x@1\dots x@{m^j@{k@j}})))
 | 
|
2199  | 
\end{array}
 | 
|
2200  | 
\]  | 
|
2201  | 
where $t@j$\texttt{_case} is the internal name of the \texttt{case}-construct.
 | 
|
2202  | 
This theorem can be added to a simpset via \ttindex{addsplits}
 | 
|
| 7490 | 2203  | 
(see~{\S}\ref{subsec:HOL:case:splitting}).
 | 
| 6580 | 2204  | 
|
| 8604 | 2205  | 
\begin{warn}\index{simplification!of \texttt{case}}%
 | 
2206  | 
By default only the selector expression ($e$ above) in a  | 
|
2207  | 
  \texttt{case}-construct is simplified, in analogy with \texttt{if} (see
 | 
|
2208  | 
  page~\pageref{if-simp}). Only if that reduces to a constructor is one of
 | 
|
2209  | 
  the arms of the \texttt{case}-construct exposed and simplified. To ensure
 | 
|
2210  | 
  full simplification of all parts of a \texttt{case}-construct for datatype
 | 
|
2211  | 
  $t$, remove $t$\texttt{.}\ttindexbold{case_weak_cong} from the simpset, for
 | 
|
2212  | 
  example by \texttt{delcongs [thm "$t$.weak_case_cong"]}.
 | 
|
2213  | 
\end{warn}
 | 
|
2214  | 
||
| 6580 | 2215  | 
\subsubsection{The function \cdx{size}}\label{sec:HOL:size}
 | 
2216  | 
||
2217  | 
Theory \texttt{Arith} declares a generic function \texttt{size} of type
 | 
|
2218  | 
$\alpha\To nat$.  Each datatype defines a particular instance of \texttt{size}
 | 
|
2219  | 
by overloading according to the following scheme:  | 
|
2220  | 
%%% FIXME: This formula is too big and is completely unreadable  | 
|
2221  | 
\[  | 
|
2222  | 
size(C^j@i~x@1~\dots~x@{m^j@i}) = \!
 | 
|
2223  | 
\left\{
 | 
|
2224  | 
\begin{array}{ll}
 | 
|
2225  | 
0 & \!\mbox{if $Rec^j@i = \emptyset$} \\
 | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2226  | 
1+\sum\limits@{h=1}^{l^j@i}size~x@{r^j@{i,h}} &
 | 
| 6580 | 2227  | 
 \!\mbox{if $Rec^j@i = \left\{\left(r^j@{i,1},s^j@{i,1}\right),\ldots,
 | 
2228  | 
  \left(r^j@{i,l^j@i},s^j@{i,l^j@i}\right)\right\}$}
 | 
|
2229  | 
\end{array}
 | 
|
2230  | 
\right.  | 
|
2231  | 
\]  | 
|
2232  | 
where $Rec^j@i$ is defined above. Viewing datatypes as generalised trees, the  | 
|
2233  | 
size of a leaf is 0 and the size of a node is the sum of the sizes of its  | 
|
2234  | 
subtrees ${}+1$.
 | 
|
2235  | 
||
2236  | 
\subsection{Defining datatypes}
 | 
|
2237  | 
||
2238  | 
The theory syntax for datatype definitions is shown in  | 
|
2239  | 
Fig.~\ref{datatype-grammar}.  In order to be well-formed, a datatype
 | 
|
2240  | 
definition has to obey the rules stated in the previous section. As a result  | 
|
2241  | 
the theory is extended with the new types, the constructors, and the theorems  | 
|
2242  | 
listed in the previous section.  | 
|
2243  | 
||
2244  | 
\begin{figure}
 | 
|
2245  | 
\begin{rail}
 | 
|
2246  | 
datatype : 'datatype' typedecls;  | 
|
2247  | 
||
2248  | 
typedecls: ( newtype '=' (cons + '|') ) + 'and'  | 
|
2249  | 
;  | 
|
2250  | 
newtype  : typevarlist id ( () | '(' infix ')' )
 | 
|
2251  | 
;  | 
|
2252  | 
cons     : name (argtype *) ( () | ( '(' mixfix ')' ) )
 | 
|
2253  | 
;  | 
|
2254  | 
argtype  : id | tid | ('(' typevarlist id ')')
 | 
|
2255  | 
;  | 
|
2256  | 
\end{rail}
 | 
|
2257  | 
\caption{Syntax of datatype declarations}
 | 
|
2258  | 
\label{datatype-grammar}
 | 
|
2259  | 
\end{figure}
 | 
|
2260  | 
||
2261  | 
Most of the theorems about datatypes become part of the default simpset and  | 
|
2262  | 
you never need to see them again because the simplifier applies them  | 
|
| 8424 | 2263  | 
automatically. Only induction or case distinction are usually invoked by hand.  | 
| 6580 | 2264  | 
\begin{ttdescription}
 | 
2265  | 
\item[\ttindexbold{induct_tac} {\tt"}$x${\tt"} $i$]
 | 
|
2266  | 
applies structural induction on variable $x$ to subgoal $i$, provided the  | 
|
2267  | 
type of $x$ is a datatype.  | 
|
| 7846 | 2268  | 
\item[\texttt{induct_tac}
 | 
2269  | 
  {\tt"}$x@1$ $\ldots$ $x@n${\tt"} $i$] applies simultaneous
 | 
|
| 6580 | 2270  | 
structural induction on the variables $x@1,\ldots,x@n$ to subgoal $i$. This  | 
2271  | 
is the canonical way to prove properties of mutually recursive datatypes  | 
|
2272  | 
  such as \texttt{aexp} and \texttt{bexp}, or datatypes with nested recursion such as
 | 
|
2273  | 
  \texttt{term}.
 | 
|
2274  | 
\end{ttdescription}
 | 
|
2275  | 
In some cases, induction is overkill and a case distinction over all  | 
|
2276  | 
constructors of the datatype suffices.  | 
|
2277  | 
\begin{ttdescription}
 | 
|
| 8443 | 2278  | 
\item[\ttindexbold{case_tac} {\tt"}$u${\tt"} $i$]
 | 
| 8424 | 2279  | 
performs a case analysis for the term $u$ whose type must be a datatype.  | 
2280  | 
 If the datatype has $k@j$ constructors  $C^j@1$, \dots $C^j@{k@j}$, subgoal
 | 
|
2281  | 
$i$ is replaced by $k@j$ new subgoals which contain the additional  | 
|
2282  | 
 assumption $u = C^j@{i'}~x@1~\dots~x@{m^j@{i'}}$ for  $i'=1$, $\dots$,~$k@j$.
 | 
|
| 6580 | 2283  | 
\end{ttdescription}
 | 
2284  | 
||
2285  | 
Note that induction is only allowed on free variables that should not occur  | 
|
| 8424 | 2286  | 
among the premises of the subgoal. Case distinction applies to arbitrary terms.  | 
| 6580 | 2287  | 
|
2288  | 
\bigskip  | 
|
2289  | 
||
2290  | 
||
2291  | 
For the technically minded, we exhibit some more details. Processing the  | 
|
2292  | 
theory file produces an \ML\ structure which, in addition to the usual  | 
|
2293  | 
components, contains a structure named $t$ for each datatype $t$ defined in  | 
|
2294  | 
the file. Each structure $t$ contains the following elements:  | 
|
2295  | 
\begin{ttbox}
 | 
|
2296  | 
val distinct : thm list  | 
|
2297  | 
val inject : thm list  | 
|
2298  | 
val induct : thm  | 
|
2299  | 
val exhaust : thm  | 
|
2300  | 
val cases : thm list  | 
|
2301  | 
val split : thm  | 
|
2302  | 
val split_asm : thm  | 
|
2303  | 
val recs : thm list  | 
|
2304  | 
val size : thm list  | 
|
2305  | 
val simps : thm list  | 
|
2306  | 
\end{ttbox}
 | 
|
2307  | 
\texttt{distinct}, \texttt{inject}, \texttt{induct}, \texttt{size}
 | 
|
2308  | 
and \texttt{split} contain the theorems
 | 
|
2309  | 
described above.  For user convenience, \texttt{distinct} contains
 | 
|
2310  | 
inequalities in both directions.  The reduction rules of the {\tt
 | 
|
2311  | 
  case}-construct are in \texttt{cases}.  All theorems from {\tt
 | 
|
2312  | 
  distinct}, \texttt{inject} and \texttt{cases} are combined in \texttt{simps}.
 | 
|
2313  | 
In case of mutually recursive datatypes, \texttt{recs}, \texttt{size}, \texttt{induct}
 | 
|
2314  | 
and \texttt{simps} are contained in a separate structure named $t@1_\ldots_t@n$.
 | 
|
2315  | 
||
2316  | 
||
| 7325 | 2317  | 
\subsection{Representing existing types as datatypes}\label{subsec:datatype:rep_datatype}
 | 
| 6580 | 2318  | 
|
2319  | 
For foundational reasons, some basic types such as \texttt{nat}, \texttt{*}, {\tt
 | 
|
2320  | 
  +}, \texttt{bool} and \texttt{unit} are not defined in a \texttt{datatype} section,
 | 
|
2321  | 
but by more primitive means using \texttt{typedef}. To be able to use the
 | 
|
| 8443 | 2322  | 
tactics \texttt{induct_tac} and \texttt{case_tac} and to define functions by
 | 
| 6580 | 2323  | 
primitive recursion on these types, such types may be represented as actual  | 
2324  | 
datatypes. This is done by specifying an induction rule, as well as theorems  | 
|
2325  | 
stating the distinctness and injectivity of constructors in a {\tt
 | 
|
2326  | 
  rep_datatype} section.  For type \texttt{nat} this works as follows:
 | 
|
2327  | 
\begin{ttbox}
 | 
|
2328  | 
rep_datatype nat  | 
|
2329  | 
distinct Suc_not_Zero, Zero_not_Suc  | 
|
2330  | 
inject Suc_Suc_eq  | 
|
2331  | 
induct nat_induct  | 
|
2332  | 
\end{ttbox}
 | 
|
2333  | 
The datatype package automatically derives additional theorems for recursion  | 
|
2334  | 
and case combinators from these rules. Any of the basic HOL types mentioned  | 
|
2335  | 
above are represented as datatypes.  Try an induction on \texttt{bool}
 | 
|
2336  | 
today.  | 
|
2337  | 
||
2338  | 
||
2339  | 
\subsection{Examples}
 | 
|
2340  | 
||
2341  | 
\subsubsection{The datatype $\alpha~mylist$}
 | 
|
2342  | 
||
2343  | 
We want to define a type $\alpha~mylist$. To do this we have to build a new  | 
|
2344  | 
theory that contains the type definition. We start from the theory  | 
|
2345  | 
\texttt{Datatype} instead of \texttt{Main} in order to avoid clashes with the
 | 
|
2346  | 
\texttt{List} theory of Isabelle/HOL.
 | 
|
2347  | 
\begin{ttbox}
 | 
|
2348  | 
MyList = Datatype +  | 
|
2349  | 
  datatype 'a mylist = Nil | Cons 'a ('a mylist)
 | 
|
2350  | 
end  | 
|
2351  | 
\end{ttbox}
 | 
|
2352  | 
After loading the theory, we can prove $Cons~x~xs\neq xs$, for example. To  | 
|
2353  | 
ease the induction applied below, we state the goal with $x$ quantified at the  | 
|
2354  | 
object-level.  This will be stripped later using \ttindex{qed_spec_mp}.
 | 
|
2355  | 
\begin{ttbox}
 | 
|
2356  | 
Goal "!x. Cons x xs ~= xs";  | 
|
2357  | 
{\out Level 0}
 | 
|
2358  | 
{\out ! x. Cons x xs ~= xs}
 | 
|
2359  | 
{\out  1. ! x. Cons x xs ~= xs}
 | 
|
2360  | 
\end{ttbox}
 | 
|
2361  | 
This can be proved by the structural induction tactic:  | 
|
2362  | 
\begin{ttbox}
 | 
|
2363  | 
by (induct_tac "xs" 1);  | 
|
2364  | 
{\out Level 1}
 | 
|
2365  | 
{\out ! x. Cons x xs ~= xs}
 | 
|
2366  | 
{\out  1. ! x. Cons x Nil ~= Nil}
 | 
|
2367  | 
{\out  2. !!a mylist.}
 | 
|
2368  | 
{\out        ! x. Cons x mylist ~= mylist ==>}
 | 
|
2369  | 
{\out        ! x. Cons x (Cons a mylist) ~= Cons a mylist}
 | 
|
2370  | 
\end{ttbox}
 | 
|
2371  | 
The first subgoal can be proved using the simplifier. Isabelle/HOL has  | 
|
2372  | 
already added the freeness properties of lists to the default simplification  | 
|
2373  | 
set.  | 
|
2374  | 
\begin{ttbox}
 | 
|
2375  | 
by (Simp_tac 1);  | 
|
2376  | 
{\out Level 2}
 | 
|
2377  | 
{\out ! x. Cons x xs ~= xs}
 | 
|
2378  | 
{\out  1. !!a mylist.}
 | 
|
2379  | 
{\out        ! x. Cons x mylist ~= mylist ==>}
 | 
|
2380  | 
{\out        ! x. Cons x (Cons a mylist) ~= Cons a mylist}
 | 
|
2381  | 
\end{ttbox}
 | 
|
2382  | 
Similarly, we prove the remaining goal.  | 
|
2383  | 
\begin{ttbox}
 | 
|
2384  | 
by (Asm_simp_tac 1);  | 
|
2385  | 
{\out Level 3}
 | 
|
2386  | 
{\out ! x. Cons x xs ~= xs}
 | 
|
2387  | 
{\out No subgoals!}
 | 
|
2388  | 
\ttbreak  | 
|
2389  | 
qed_spec_mp "not_Cons_self";  | 
|
2390  | 
{\out val not_Cons_self = "Cons x xs ~= xs" : thm}
 | 
|
2391  | 
\end{ttbox}
 | 
|
2392  | 
Because both subgoals could have been proved by \texttt{Asm_simp_tac}
 | 
|
2393  | 
we could have done that in one step:  | 
|
2394  | 
\begin{ttbox}
 | 
|
2395  | 
by (ALLGOALS Asm_simp_tac);  | 
|
2396  | 
\end{ttbox}
 | 
|
2397  | 
||
2398  | 
||
2399  | 
\subsubsection{The datatype $\alpha~mylist$ with mixfix syntax}
 | 
|
2400  | 
||
2401  | 
In this example we define the type $\alpha~mylist$ again but this time  | 
|
2402  | 
we want to write \texttt{[]} for \texttt{Nil} and we want to use infix
 | 
|
2403  | 
notation \verb|#| for \texttt{Cons}.  To do this we simply add mixfix
 | 
|
2404  | 
annotations after the constructor declarations as follows:  | 
|
2405  | 
\begin{ttbox}
 | 
|
2406  | 
MyList = Datatype +  | 
|
2407  | 
datatype 'a mylist =  | 
|
2408  | 
    Nil ("[]")  |
 | 
|
2409  | 
    Cons 'a ('a mylist)  (infixr "#" 70)
 | 
|
2410  | 
end  | 
|
2411  | 
\end{ttbox}
 | 
|
2412  | 
Now the theorem in the previous example can be written \verb|x#xs ~= xs|.  | 
|
2413  | 
||
2414  | 
||
2415  | 
\subsubsection{A datatype for weekdays}
 | 
|
2416  | 
||
2417  | 
This example shows a datatype that consists of 7 constructors:  | 
|
2418  | 
\begin{ttbox}
 | 
|
2419  | 
Days = Main +  | 
|
2420  | 
datatype days = Mon | Tue | Wed | Thu | Fri | Sat | Sun  | 
|
2421  | 
end  | 
|
2422  | 
\end{ttbox}
 | 
|
2423  | 
Because there are more than 6 constructors, inequality is expressed via a function  | 
|
2424  | 
\verb|days_ord|. The theorem \verb|Mon ~= Tue| is not directly  | 
|
2425  | 
contained among the distinctness theorems, but the simplifier can  | 
|
2426  | 
prove it thanks to rewrite rules inherited from theory \texttt{Arith}:
 | 
|
2427  | 
\begin{ttbox}
 | 
|
2428  | 
Goal "Mon ~= Tue";  | 
|
2429  | 
by (Simp_tac 1);  | 
|
2430  | 
\end{ttbox}
 | 
|
2431  | 
You need not derive such inequalities explicitly: the simplifier will dispose  | 
|
2432  | 
of them automatically.  | 
|
2433  | 
\index{*datatype|)}
 | 
|
2434  | 
||
2435  | 
||
2436  | 
\section{Recursive function definitions}\label{sec:HOL:recursive}
 | 
|
2437  | 
\index{recursive functions|see{recursion}}
 | 
|
2438  | 
||
2439  | 
Isabelle/HOL provides two main mechanisms of defining recursive functions.  | 
|
2440  | 
\begin{enumerate}
 | 
|
2441  | 
\item \textbf{Primitive recursion} is available only for datatypes, and it is
 | 
|
2442  | 
somewhat restrictive. Recursive calls are only allowed on the argument's  | 
|
2443  | 
immediate constituents. On the other hand, it is the form of recursion most  | 
|
2444  | 
often wanted, and it is easy to use.  | 
|
2445  | 
||
2446  | 
\item \textbf{Well-founded recursion} requires that you supply a well-founded
 | 
|
2447  | 
relation that governs the recursion. Recursive calls are only allowed if  | 
|
2448  | 
they make the argument decrease under the relation. Complicated recursion  | 
|
2449  | 
forms, such as nested recursion, can be dealt with. Termination can even be  | 
|
2450  | 
proved at a later time, though having unsolved termination conditions around  | 
|
2451  | 
can make work difficult.%  | 
|
2452  | 
  \footnote{This facility is based on Konrad Slind's TFL
 | 
|
2453  | 
    package~\cite{slind-tfl}.  Thanks are due to Konrad for implementing TFL
 | 
|
2454  | 
and assisting with its installation.}  | 
|
2455  | 
\end{enumerate}
 | 
|
2456  | 
||
2457  | 
Following good HOL tradition, these declarations do not assert arbitrary  | 
|
2458  | 
axioms. Instead, they define the function using a recursion operator. Both  | 
|
2459  | 
HOL and ZF derive the theory of well-founded recursion from first  | 
|
2460  | 
principles~\cite{paulson-set-II}.  Primitive recursion over some datatype
 | 
|
2461  | 
relies on the recursion operator provided by the datatype package. With  | 
|
2462  | 
either form of function definition, Isabelle proves the desired recursion  | 
|
2463  | 
equations as theorems.  | 
|
2464  | 
||
2465  | 
||
2466  | 
\subsection{Primitive recursive functions}
 | 
|
2467  | 
\label{sec:HOL:primrec}
 | 
|
2468  | 
\index{recursion!primitive|(}
 | 
|
2469  | 
\index{*primrec|(}
 | 
|
2470  | 
||
2471  | 
Datatypes come with a uniform way of defining functions, {\bf primitive
 | 
|
2472  | 
recursion}. In principle, one could introduce primitive recursive functions  | 
|
2473  | 
by asserting their reduction rules as new axioms, but this is not recommended:  | 
|
2474  | 
\begin{ttbox}\slshape
 | 
|
2475  | 
Append = Main +  | 
|
2476  | 
consts app :: ['a list, 'a list] => 'a list  | 
|
2477  | 
rules  | 
|
2478  | 
app_Nil "app [] ys = ys"  | 
|
2479  | 
app_Cons "app (x#xs) ys = x#app xs ys"  | 
|
2480  | 
end  | 
|
2481  | 
\end{ttbox}
 | 
|
2482  | 
Asserting axioms brings the danger of accidentally asserting nonsense, as  | 
|
2483  | 
in \verb$app [] ys = us$.  | 
|
2484  | 
||
2485  | 
The \ttindex{primrec} declaration is a safe means of defining primitive
 | 
|
2486  | 
recursive functions on datatypes:  | 
|
2487  | 
\begin{ttbox}
 | 
|
2488  | 
Append = Main +  | 
|
2489  | 
consts app :: ['a list, 'a list] => 'a list  | 
|
2490  | 
primrec  | 
|
2491  | 
"app [] ys = ys"  | 
|
2492  | 
"app (x#xs) ys = x#app xs ys"  | 
|
2493  | 
end  | 
|
2494  | 
\end{ttbox}
 | 
|
2495  | 
Isabelle will now check that the two rules do indeed form a primitive  | 
|
2496  | 
recursive definition. For example  | 
|
2497  | 
\begin{ttbox}
 | 
|
2498  | 
primrec  | 
|
2499  | 
"app [] ys = us"  | 
|
2500  | 
\end{ttbox}
 | 
|
2501  | 
is rejected with an error message ``\texttt{Extra variables on rhs}''.
 | 
|
2502  | 
||
2503  | 
\bigskip  | 
|
2504  | 
||
2505  | 
The general form of a primitive recursive definition is  | 
|
2506  | 
\begin{ttbox}
 | 
|
2507  | 
primrec  | 
|
2508  | 
    {\it reduction rules}
 | 
|
2509  | 
\end{ttbox}
 | 
|
2510  | 
where \textit{reduction rules} specify one or more equations of the form
 | 
|
2511  | 
\[ f \, x@1 \, \dots \, x@m \, (C \, y@1 \, \dots \, y@k) \, z@1 \,  | 
|
2512  | 
\dots \, z@n = r \] such that $C$ is a constructor of the datatype, $r$  | 
|
2513  | 
contains only the free variables on the left-hand side, and all recursive  | 
|
2514  | 
calls in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$. There  | 
|
2515  | 
must be at most one reduction rule for each constructor. The order is  | 
|
2516  | 
immaterial. For missing constructors, the function is defined to return a  | 
|
2517  | 
default value.  | 
|
2518  | 
||
2519  | 
If you would like to refer to some rule by name, then you must prefix  | 
|
2520  | 
the rule with an identifier. These identifiers, like those in the  | 
|
2521  | 
\texttt{rules} section of a theory, will be visible at the \ML\ level.
 | 
|
2522  | 
||
2523  | 
The primitive recursive function can have infix or mixfix syntax:  | 
|
2524  | 
\begin{ttbox}\underscoreon
 | 
|
2525  | 
consts "@" :: ['a list, 'a list] => 'a list (infixr 60)  | 
|
2526  | 
primrec  | 
|
2527  | 
"[] @ ys = ys"  | 
|
2528  | 
"(x#xs) @ ys = x#(xs @ ys)"  | 
|
2529  | 
\end{ttbox}
 | 
|
2530  | 
||
2531  | 
The reduction rules become part of the default simpset, which  | 
|
2532  | 
leads to short proof scripts:  | 
|
2533  | 
\begin{ttbox}\underscoreon
 | 
|
2534  | 
Goal "(xs @ ys) @ zs = xs @ (ys @ zs)";  | 
|
2535  | 
by (induct\_tac "xs" 1);  | 
|
2536  | 
by (ALLGOALS Asm\_simp\_tac);  | 
|
2537  | 
\end{ttbox}
 | 
|
2538  | 
||
2539  | 
\subsubsection{Example: Evaluation of expressions}
 | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2540  | 
Using mutual primitive recursion, we can define evaluation functions \texttt{evala}
 | 
| 6580 | 2541  | 
and \texttt{eval_bexp} for the datatypes of arithmetic and boolean expressions mentioned in
 | 
| 7490 | 2542  | 
{\S}\ref{subsec:datatype:basics}:
 | 
| 6580 | 2543  | 
\begin{ttbox}
 | 
2544  | 
consts  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2545  | 
evala :: "['a => nat, 'a aexp] => nat"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2546  | 
evalb :: "['a => nat, 'a bexp] => bool"  | 
| 6580 | 2547  | 
|
2548  | 
primrec  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2549  | 
"evala env (If_then_else b a1 a2) =  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2550  | 
(if evalb env b then evala env a1 else evala env a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2551  | 
"evala env (Sum a1 a2) = evala env a1 + evala env a2"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2552  | 
"evala env (Diff a1 a2) = evala env a1 - evala env a2"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2553  | 
"evala env (Var v) = env v"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2554  | 
"evala env (Num n) = n"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2555  | 
|
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2556  | 
"evalb env (Less a1 a2) = (evala env a1 < evala env a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2557  | 
"evalb env (And b1 b2) = (evalb env b1 & evalb env b2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2558  | 
"evalb env (Or b1 b2) = (evalb env b1 & evalb env b2)"  | 
| 6580 | 2559  | 
\end{ttbox}
 | 
2560  | 
Since the value of an expression depends on the value of its variables,  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2561  | 
the functions \texttt{evala} and \texttt{evalb} take an additional
 | 
| 6580 | 2562  | 
parameter, an {\em environment} of type \texttt{'a => nat}, which maps
 | 
2563  | 
variables to their values.  | 
|
2564  | 
||
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2565  | 
Similarly, we may define substitution functions \texttt{substa}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2566  | 
and \texttt{substb} for expressions: The mapping \texttt{f} of type
 | 
| 6580 | 2567  | 
\texttt{'a => 'a aexp} given as a parameter is lifted canonically
 | 
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2568  | 
on the types \texttt{'a aexp} and \texttt{'a bexp}:
 | 
| 6580 | 2569  | 
\begin{ttbox}
 | 
2570  | 
consts  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2571  | 
substa :: "['a => 'b aexp, 'a aexp] => 'b aexp"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2572  | 
substb :: "['a => 'b aexp, 'a bexp] => 'b bexp"  | 
| 6580 | 2573  | 
|
2574  | 
primrec  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2575  | 
"substa f (If_then_else b a1 a2) =  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2576  | 
If_then_else (substb f b) (substa f a1) (substa f a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2577  | 
"substa f (Sum a1 a2) = Sum (substa f a1) (substa f a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2578  | 
"substa f (Diff a1 a2) = Diff (substa f a1) (substa f a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2579  | 
"substa f (Var v) = f v"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2580  | 
"substa f (Num n) = Num n"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2581  | 
|
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2582  | 
"substb f (Less a1 a2) = Less (substa f a1) (substa f a2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2583  | 
"substb f (And b1 b2) = And (substb f b1) (substb f b2)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2584  | 
"substb f (Or b1 b2) = Or (substb f b1) (substb f b2)"  | 
| 6580 | 2585  | 
\end{ttbox}
 | 
2586  | 
In textbooks about semantics one often finds {\em substitution theorems},
 | 
|
2587  | 
which express the relationship between substitution and evaluation. For  | 
|
2588  | 
\texttt{'a aexp} and \texttt{'a bexp}, we can prove such a theorem by mutual
 | 
|
2589  | 
induction, followed by simplification:  | 
|
2590  | 
\begin{ttbox}
 | 
|
2591  | 
Goal  | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2592  | 
"evala env (substa (Var(v := a')) a) =  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2593  | 
evala (env(v := evala env a')) a &  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2594  | 
evalb env (substb (Var(v := a')) b) =  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2595  | 
evalb (env(v := evala env a')) b";  | 
| 7846 | 2596  | 
by (induct_tac "a b" 1);  | 
| 6580 | 2597  | 
by (ALLGOALS Asm_full_simp_tac);  | 
2598  | 
\end{ttbox}
 | 
|
2599  | 
||
2600  | 
\subsubsection{Example: A substitution function for terms}
 | 
|
2601  | 
Functions on datatypes with nested recursion, such as the type  | 
|
| 7490 | 2602  | 
\texttt{term} mentioned in {\S}\ref{subsec:datatype:basics}, are
 | 
| 6580 | 2603  | 
also defined by mutual primitive recursion. A substitution  | 
2604  | 
function \texttt{subst_term} on type \texttt{term}, similar to the functions
 | 
|
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2605  | 
\texttt{substa} and \texttt{substb} described above, can
 | 
| 6580 | 2606  | 
be defined as follows:  | 
2607  | 
\begin{ttbox}
 | 
|
2608  | 
consts  | 
|
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2609  | 
  subst_term :: "['a => ('a,'b) term, ('a,'b) term] => ('a,'b) term"
 | 
| 6580 | 2610  | 
subst_term_list ::  | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2611  | 
    "['a => ('a,'b) term, ('a,'b) term list] => ('a,'b) term list"
 | 
| 6580 | 2612  | 
|
2613  | 
primrec  | 
|
2614  | 
"subst_term f (Var a) = f a"  | 
|
2615  | 
"subst_term f (App b ts) = App b (subst_term_list f ts)"  | 
|
2616  | 
||
2617  | 
"subst_term_list f [] = []"  | 
|
2618  | 
"subst_term_list f (t # ts) =  | 
|
2619  | 
subst_term f t # subst_term_list f ts"  | 
|
2620  | 
\end{ttbox}
 | 
|
2621  | 
The recursion scheme follows the structure of the unfolded definition of type  | 
|
| 7490 | 2622  | 
\texttt{term} shown in {\S}\ref{subsec:datatype:basics}. To prove properties of
 | 
| 6580 | 2623  | 
this substitution function, mutual induction is needed:  | 
2624  | 
\begin{ttbox}
 | 
|
2625  | 
Goal  | 
|
2626  | 
"(subst_term ((subst_term f1) o f2) t) =  | 
|
2627  | 
(subst_term f1 (subst_term f2 t)) &  | 
|
2628  | 
(subst_term_list ((subst_term f1) o f2) ts) =  | 
|
2629  | 
(subst_term_list f1 (subst_term_list f2 ts))";  | 
|
| 7846 | 2630  | 
by (induct_tac "t ts" 1);  | 
| 6580 | 2631  | 
by (ALLGOALS Asm_full_simp_tac);  | 
2632  | 
\end{ttbox}
 | 
|
2633  | 
||
| 
7044
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2634  | 
\subsubsection{Example: A map function for infinitely branching trees}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2635  | 
Defining functions on infinitely branching datatypes by primitive  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2636  | 
recursion is just as easy. For example, we can define a function  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2637  | 
\texttt{map_tree} on \texttt{'a tree} as follows:
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2638  | 
\begin{ttbox}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2639  | 
consts  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2640  | 
  map_tree :: "('a => 'b) => 'a tree => 'b tree"
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2641  | 
|
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2642  | 
primrec  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2643  | 
"map_tree f (Atom a) = Atom (f a)"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2644  | 
"map_tree f (Branch ts) = Branch (\%x. map_tree f (ts x))"  | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2645  | 
\end{ttbox}
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2646  | 
Note that all occurrences of functions such as \texttt{ts} in the
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2647  | 
\texttt{primrec} clauses must be applied to an argument. In particular,
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2648  | 
\texttt{map_tree f o ts} is not allowed.
 | 
| 
 
193a8601fabd
Documented usage of function types in datatype specifications.
 
berghofe 
parents: 
6626 
diff
changeset
 | 
2649  | 
|
| 6580 | 2650  | 
\index{recursion!primitive|)}
 | 
2651  | 
\index{*primrec|)}
 | 
|
2652  | 
||
2653  | 
||
2654  | 
\subsection{General recursive functions}
 | 
|
2655  | 
\label{sec:HOL:recdef}
 | 
|
2656  | 
\index{recursion!general|(}
 | 
|
2657  | 
\index{*recdef|(}
 | 
|
2658  | 
||
2659  | 
Using \texttt{recdef}, you can declare functions involving nested recursion
 | 
|
2660  | 
and pattern-matching. Recursion need not involve datatypes and there are few  | 
|
2661  | 
syntactic restrictions. Termination is proved by showing that each recursive  | 
|
2662  | 
call makes the argument smaller in a suitable sense, which you specify by  | 
|
2663  | 
supplying a well-founded relation.  | 
|
2664  | 
||
2665  | 
Here is a simple example, the Fibonacci function. The first line declares  | 
|
2666  | 
\texttt{fib} to be a constant.  The well-founded relation is simply~$<$ (on
 | 
|
2667  | 
the natural numbers).  Pattern-matching is used here: \texttt{1} is a
 | 
|
2668  | 
macro for \texttt{Suc~0}.
 | 
|
2669  | 
\begin{ttbox}
 | 
|
2670  | 
consts fib :: "nat => nat"  | 
|
2671  | 
recdef fib "less_than"  | 
|
2672  | 
"fib 0 = 0"  | 
|
2673  | 
"fib 1 = 1"  | 
|
2674  | 
"fib (Suc(Suc x)) = (fib x + fib (Suc x))"  | 
|
2675  | 
\end{ttbox}
 | 
|
2676  | 
||
2677  | 
With \texttt{recdef}, function definitions may be incomplete, and patterns may
 | 
|
2678  | 
overlap, as in functional programming.  The \texttt{recdef} package
 | 
|
2679  | 
disambiguates overlapping patterns by taking the order of rules into account.  | 
|
2680  | 
For missing patterns, the function is defined to return a default value.  | 
|
2681  | 
||
2682  | 
%For example, here is a declaration of the list function \cdx{hd}:
 | 
|
2683  | 
%\begin{ttbox}
 | 
|
2684  | 
%consts hd :: 'a list => 'a  | 
|
2685  | 
%recdef hd "\{\}"
 | 
|
2686  | 
% "hd (x#l) = x"  | 
|
2687  | 
%\end{ttbox}
 | 
|
2688  | 
%Because this function is not recursive, we may supply the empty well-founded  | 
|
2689  | 
%relation, $\{\}$.
 | 
|
2690  | 
||
2691  | 
The well-founded relation defines a notion of ``smaller'' for the function's  | 
|
2692  | 
argument type.  The relation $\prec$ is \textbf{well-founded} provided it
 | 
|
2693  | 
admits no infinitely decreasing chains  | 
|
2694  | 
\[ \cdots\prec x@n\prec\cdots\prec x@1. \]  | 
|
2695  | 
If the function's argument has type~$\tau$, then $\prec$ has to be a relation  | 
|
2696  | 
over~$\tau$: it must have type $(\tau\times\tau)set$.  | 
|
2697  | 
||
2698  | 
Proving well-foundedness can be tricky, so Isabelle/HOL provides a collection  | 
|
2699  | 
of operators for building well-founded relations. The package recognises  | 
|
2700  | 
these operators and automatically proves that the constructed relation is  | 
|
2701  | 
well-founded. Here are those operators, in order of importance:  | 
|
2702  | 
\begin{itemize}
 | 
|
2703  | 
\item \texttt{less_than} is ``less than'' on the natural numbers.
 | 
|
2704  | 
(It has type $(nat\times nat)set$, while $<$ has type $[nat,nat]\To bool$.  | 
|
2705  | 
||
2706  | 
\item $\mathop{\mathtt{measure}} f$, where $f$ has type $\tau\To nat$, is the
 | 
|
| 9258 | 2707  | 
relation~$\prec$ on type~$\tau$ such that $x\prec y$ if and only if  | 
2708  | 
$f(x)<f(y)$.  | 
|
| 6580 | 2709  | 
Typically, $f$ takes the recursive function's arguments (as a tuple) and  | 
2710  | 
  returns a result expressed in terms of the function \texttt{size}.  It is
 | 
|
2711  | 
  called a \textbf{measure function}.  Recall that \texttt{size} is overloaded
 | 
|
| 7490 | 2712  | 
  and is defined on all datatypes (see {\S}\ref{sec:HOL:size}).
 | 
| 6580 | 2713  | 
|
| 9258 | 2714  | 
\item $\mathop{\mathtt{inv_image}} R\;f$ is a generalisation of
 | 
2715  | 
  \texttt{measure}.  It specifies a relation such that $x\prec y$ if and only
 | 
|
2716  | 
if $f(x)$  | 
|
| 6580 | 2717  | 
is less than $f(y)$ according to~$R$, which must itself be a well-founded  | 
2718  | 
relation.  | 
|
2719  | 
||
2720  | 
\item $R@1\texttt{**}R@2$ is the lexicographic product of two relations.  It
 | 
|
| 9258 | 2721  | 
is a relation on pairs and satisfies $(x@1,x@2)\prec(y@1,y@2)$ if and only  | 
2722  | 
if $x@1$  | 
|
| 6580 | 2723  | 
is less than $y@1$ according to~$R@1$ or $x@1=y@1$ and $x@2$  | 
2724  | 
is less than $y@2$ according to~$R@2$.  | 
|
2725  | 
||
2726  | 
\item \texttt{finite_psubset} is the proper subset relation on finite sets.
 | 
|
2727  | 
\end{itemize}
 | 
|
2728  | 
||
2729  | 
We can use \texttt{measure} to declare Euclid's algorithm for the greatest
 | 
|
2730  | 
common divisor. The measure function, $\lambda(m,n). n$, specifies that the  | 
|
2731  | 
recursion terminates because argument~$n$ decreases.  | 
|
2732  | 
\begin{ttbox}
 | 
|
2733  | 
recdef gcd "measure ((\%(m,n). n) ::nat*nat=>nat)"  | 
|
2734  | 
"gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"  | 
|
2735  | 
\end{ttbox}
 | 
|
2736  | 
||
2737  | 
The general form of a well-founded recursive definition is  | 
|
2738  | 
\begin{ttbox}
 | 
|
2739  | 
recdef {\it function} {\it rel}
 | 
|
2740  | 
    congs   {\it congruence rules}      {\bf(optional)}
 | 
|
2741  | 
    simpset {\it simplification set}      {\bf(optional)}
 | 
|
2742  | 
   {\it reduction rules}
 | 
|
2743  | 
\end{ttbox}
 | 
|
2744  | 
where  | 
|
2745  | 
\begin{itemize}
 | 
|
2746  | 
\item \textit{function} is the name of the function, either as an \textit{id}
 | 
|
2747  | 
  or a \textit{string}.  
 | 
|
2748  | 
||
2749  | 
\item \textit{rel} is a {\HOL} expression for the well-founded termination
 | 
|
2750  | 
relation.  | 
|
2751  | 
||
2752  | 
\item \textit{congruence rules} are required only in highly exceptional
 | 
|
2753  | 
circumstances.  | 
|
2754  | 
||
2755  | 
\item The \textit{simplification set} is used to prove that the supplied
 | 
|
2756  | 
  relation is well-founded.  It is also used to prove the \textbf{termination
 | 
|
2757  | 
conditions}: assertions that arguments of recursive calls decrease under  | 
|
2758  | 
  \textit{rel}.  By default, simplification uses \texttt{simpset()}, which
 | 
|
2759  | 
is sufficient to prove well-foundedness for the built-in relations listed  | 
|
2760  | 
above.  | 
|
2761  | 
||
2762  | 
\item \textit{reduction rules} specify one or more recursion equations.  Each
 | 
|
2763  | 
left-hand side must have the form $f\,t$, where $f$ is the function and $t$  | 
|
2764  | 
is a tuple of distinct variables. If more than one equation is present then  | 
|
2765  | 
$f$ is defined by pattern-matching on components of its argument whose type  | 
|
2766  | 
  is a \texttt{datatype}.  
 | 
|
2767  | 
||
| 8628 | 2768  | 
  The \ML\ identifier $f$\texttt{.simps} contains the reduction rules as
 | 
2769  | 
a list of theorems.  | 
|
| 6580 | 2770  | 
\end{itemize}
 | 
2771  | 
||
2772  | 
With the definition of \texttt{gcd} shown above, Isabelle/HOL is unable to
 | 
|
2773  | 
prove one termination condition. It remains as a precondition of the  | 
|
| 8628 | 2774  | 
recursion theorems:  | 
| 6580 | 2775  | 
\begin{ttbox}
 | 
| 8628 | 2776  | 
gcd.simps;  | 
| 6580 | 2777  | 
{\out ["! m n. n ~= 0 --> m mod n < n}
 | 
| 
9212
 
4afe62073b41
overloading, axclasses, numerals and general tidying
 
paulson 
parents: 
8628 
diff
changeset
 | 
2778  | 
{\out   ==> gcd (?m,?n) = (if ?n=0 then ?m else gcd (?n, ?m mod ?n))"] }
 | 
| 6580 | 2779  | 
{\out : thm list}
 | 
2780  | 
\end{ttbox}
 | 
|
2781  | 
The theory \texttt{HOL/ex/Primes} illustrates how to prove termination
 | 
|
2782  | 
conditions afterwards.  The function \texttt{Tfl.tgoalw} is like the standard
 | 
|
2783  | 
function \texttt{goalw}, which sets up a goal to prove, but its argument
 | 
|
| 8628 | 2784  | 
should be the identifier $f$\texttt{.simps} and its effect is to set up a
 | 
| 6580 | 2785  | 
proof of the termination conditions:  | 
2786  | 
\begin{ttbox}
 | 
|
| 8628 | 2787  | 
Tfl.tgoalw thy [] gcd.simps;  | 
| 6580 | 2788  | 
{\out Level 0}
 | 
2789  | 
{\out ! m n. n ~= 0 --> m mod n < n}
 | 
|
2790  | 
{\out  1. ! m n. n ~= 0 --> m mod n < n}
 | 
|
2791  | 
\end{ttbox}
 | 
|
2792  | 
This subgoal has a one-step proof using \texttt{simp_tac}.  Once the theorem
 | 
|
2793  | 
is proved, it can be used to eliminate the termination conditions from  | 
|
| 8628 | 2794  | 
elements of \texttt{gcd.simps}.  Theory \texttt{HOL/Subst/Unify} is a much
 | 
| 6580 | 2795  | 
more complicated example of this process, where the termination conditions can  | 
2796  | 
only be proved by complicated reasoning involving the recursive function  | 
|
2797  | 
itself.  | 
|
2798  | 
||
2799  | 
Isabelle/HOL can prove the \texttt{gcd} function's termination condition
 | 
|
2800  | 
automatically if supplied with the right simpset.  | 
|
2801  | 
\begin{ttbox}
 | 
|
2802  | 
recdef gcd "measure ((\%(m,n). n) ::nat*nat=>nat)"  | 
|
2803  | 
simpset "simpset() addsimps [mod_less_divisor, zero_less_eq]"  | 
|
2804  | 
"gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"  | 
|
2805  | 
\end{ttbox}
 | 
|
2806  | 
||
| 8628 | 2807  | 
If all termination conditions were proved automatically, $f$\texttt{.simps}
 | 
2808  | 
is added to the simpset automatically, just as in \texttt{primrec}. 
 | 
|
2809  | 
The simplification rules corresponding to clause $i$ (where counting starts  | 
|
2810  | 
at 0) are called $f$\texttt{.}$i$ and can be accessed as \texttt{thms
 | 
|
2811  | 
"$f$.$i$"},  | 
|
2812  | 
which returns a list of theorems. Thus you can, for example, remove specific  | 
|
2813  | 
clauses from the simpset. Note that a single clause may give rise to a set of  | 
|
2814  | 
simplification rules in order to capture the fact that if clauses overlap,  | 
|
2815  | 
their order disambiguates them.  | 
|
2816  | 
||
| 6580 | 2817  | 
A \texttt{recdef} definition also returns an induction rule specialised for
 | 
2818  | 
the recursive function.  For the \texttt{gcd} function above, the induction
 | 
|
2819  | 
rule is  | 
|
2820  | 
\begin{ttbox}
 | 
|
2821  | 
gcd.induct;  | 
|
2822  | 
{\out "(!!m n. n ~= 0 --> ?P n (m mod n) ==> ?P m n) ==> ?P ?u ?v" : thm}
 | 
|
2823  | 
\end{ttbox}
 | 
|
2824  | 
This rule should be used to reason inductively about the \texttt{gcd}
 | 
|
2825  | 
function. It usually makes the induction hypothesis available at all  | 
|
2826  | 
recursive calls, leading to very direct proofs. If any termination conditions  | 
|
2827  | 
remain unproved, they will become additional premises of this rule.  | 
|
2828  | 
||
2829  | 
\index{recursion!general|)}
 | 
|
2830  | 
\index{*recdef|)}
 | 
|
2831  | 
||
2832  | 
||
2833  | 
\section{Inductive and coinductive definitions}
 | 
|
2834  | 
\index{*inductive|(}
 | 
|
2835  | 
\index{*coinductive|(}
 | 
|
2836  | 
||
2837  | 
An {\bf inductive definition} specifies the least set~$R$ closed under given
 | 
|
2838  | 
rules. (Applying a rule to elements of~$R$ yields a result within~$R$.) For  | 
|
2839  | 
example, a structural operational semantics is an inductive definition of an  | 
|
2840  | 
evaluation relation.  Dually, a {\bf coinductive definition} specifies the
 | 
|
2841  | 
greatest set~$R$ consistent with given rules. (Every element of~$R$ can be  | 
|
2842  | 
seen as arising by applying a rule to elements of~$R$.) An important example  | 
|
2843  | 
is using bisimulation relations to formalise equivalence of processes and  | 
|
2844  | 
infinite data structures.  | 
|
2845  | 
||
2846  | 
A theory file may contain any number of inductive and coinductive  | 
|
2847  | 
definitions. They may be intermixed with other declarations; in  | 
|
2848  | 
particular, the (co)inductive sets {\bf must} be declared separately as
 | 
|
2849  | 
constants, and may have mixfix syntax or be subject to syntax translations.  | 
|
2850  | 
||
2851  | 
Each (co)inductive definition adds definitions to the theory and also  | 
|
2852  | 
proves some theorems. Each definition creates an \ML\ structure, which is a  | 
|
2853  | 
substructure of the main theory structure.  | 
|
2854  | 
||
2855  | 
This package is related to the \ZF\ one, described in a separate  | 
|
2856  | 
paper,%  | 
|
2857  | 
\footnote{It appeared in CADE~\cite{paulson-CADE}; a longer version is
 | 
|
2858  | 
distributed with Isabelle.} %  | 
|
2859  | 
which you should refer to in case of difficulties. The package is simpler  | 
|
2860  | 
than \ZF's thanks to \HOL's extra-logical automatic type-checking. The types  | 
|
2861  | 
of the (co)inductive sets determine the domain of the fixedpoint definition,  | 
|
2862  | 
and the package does not have to use inference rules for type-checking.  | 
|
2863  | 
||
2864  | 
||
2865  | 
\subsection{The result structure}
 | 
|
2866  | 
Many of the result structure's components have been discussed in the paper;  | 
|
2867  | 
others are self-explanatory.  | 
|
2868  | 
\begin{description}
 | 
|
2869  | 
\item[\tt defs] is the list of definitions of the recursive sets.  | 
|
2870  | 
||
2871  | 
\item[\tt mono] is a monotonicity theorem for the fixedpoint operator.  | 
|
2872  | 
||
2873  | 
\item[\tt unfold] is a fixedpoint equation for the recursive set (the union of  | 
|
2874  | 
the recursive sets, in the case of mutual recursion).  | 
|
2875  | 
||
2876  | 
\item[\tt intrs] is the list of introduction rules, now proved as theorems, for  | 
|
2877  | 
the recursive sets. The rules are also available individually, using the  | 
|
2878  | 
names given them in the theory file.  | 
|
2879  | 
||
2880  | 
\item[\tt elims] is the list of elimination rule.  | 
|
2881  | 
||
2882  | 
\item[\tt elim] is the head of the list \texttt{elims}.
 | 
|
2883  | 
||
2884  | 
\item[\tt mk_cases] is a function to create simplified instances of {\tt
 | 
|
2885  | 
elim} using freeness reasoning on underlying datatypes.  | 
|
2886  | 
\end{description}
 | 
|
2887  | 
||
2888  | 
For an inductive definition, the result structure contains the  | 
|
2889  | 
rule \texttt{induct}.  For a
 | 
|
2890  | 
coinductive definition, it contains the rule \verb|coinduct|.  | 
|
2891  | 
||
2892  | 
Figure~\ref{def-result-fig} summarises the two result signatures,
 | 
|
2893  | 
specifying the types of all these components.  | 
|
2894  | 
||
2895  | 
\begin{figure}
 | 
|
2896  | 
\begin{ttbox}
 | 
|
2897  | 
sig  | 
|
2898  | 
val defs : thm list  | 
|
2899  | 
val mono : thm  | 
|
2900  | 
val unfold : thm  | 
|
2901  | 
val intrs : thm list  | 
|
2902  | 
val elims : thm list  | 
|
2903  | 
val elim : thm  | 
|
2904  | 
val mk_cases : string -> thm  | 
|
2905  | 
{\it(Inductive definitions only)} 
 | 
|
2906  | 
val induct : thm  | 
|
2907  | 
{\it(coinductive definitions only)}
 | 
|
2908  | 
val coinduct : thm  | 
|
2909  | 
end  | 
|
2910  | 
\end{ttbox}
 | 
|
2911  | 
\hrule  | 
|
2912  | 
\caption{The {\ML} result of a (co)inductive definition} \label{def-result-fig}
 | 
|
2913  | 
\end{figure}
 | 
|
2914  | 
||
2915  | 
\subsection{The syntax of a (co)inductive definition}
 | 
|
2916  | 
An inductive definition has the form  | 
|
2917  | 
\begin{ttbox}
 | 
|
2918  | 
inductive    {\it inductive sets}
 | 
|
2919  | 
  intrs      {\it introduction rules}
 | 
|
2920  | 
  monos      {\it monotonicity theorems}
 | 
|
2921  | 
  con_defs   {\it constructor definitions}
 | 
|
2922  | 
\end{ttbox}
 | 
|
2923  | 
A coinductive definition is identical, except that it starts with the keyword  | 
|
2924  | 
\texttt{coinductive}.  
 | 
|
2925  | 
||
2926  | 
The \texttt{monos} and \texttt{con_defs} sections are optional.  If present,
 | 
|
2927  | 
each is specified by a list of identifiers.  | 
|
2928  | 
||
2929  | 
\begin{itemize}
 | 
|
2930  | 
\item The \textit{inductive sets} are specified by one or more strings.
 | 
|
2931  | 
||
2932  | 
\item The \textit{introduction rules} specify one or more introduction rules in
 | 
|
2933  | 
  the form \textit{ident\/}~\textit{string}, where the identifier gives the name of
 | 
|
2934  | 
the rule in the result structure.  | 
|
2935  | 
||
2936  | 
\item The \textit{monotonicity theorems} are required for each operator
 | 
|
2937  | 
  applied to a recursive set in the introduction rules.  There {\bf must}
 | 
|
2938  | 
be a theorem of the form $A\subseteq B\Imp M(A)\subseteq M(B)$, for each  | 
|
2939  | 
premise $t\in M(R@i)$ in an introduction rule!  | 
|
2940  | 
||
2941  | 
\item The \textit{constructor definitions} contain definitions of constants
 | 
|
2942  | 
appearing in the introduction rules. In most cases it can be omitted.  | 
|
2943  | 
\end{itemize}
 | 
|
2944  | 
||
2945  | 
||
| 7830 | 2946  | 
\subsection{*Monotonicity theorems}
 | 
2947  | 
||
2948  | 
Each theory contains a default set of theorems that are used in monotonicity  | 
|
2949  | 
proofs. New rules can be added to this set via the \texttt{mono} attribute.
 | 
|
2950  | 
Theory \texttt{Inductive} shows how this is done. In general, the following
 | 
|
2951  | 
monotonicity theorems may be added:  | 
|
2952  | 
\begin{itemize}
 | 
|
2953  | 
\item Theorems of the form $A\subseteq B\Imp M(A)\subseteq M(B)$, for proving  | 
|
2954  | 
monotonicity of inductive definitions whose introduction rules have premises  | 
|
2955  | 
involving terms such as $t\in M(R@i)$.  | 
|
2956  | 
\item Monotonicity theorems for logical operators, which are of the general form  | 
|
2957  | 
  $\List{\cdots \rightarrow \cdots;~\ldots;~\cdots \rightarrow \cdots} \Imp
 | 
|
2958  | 
\cdots \rightarrow \cdots$.  | 
|
2959  | 
For example, in the case of the operator $\vee$, the corresponding theorem is  | 
|
2960  | 
\[  | 
|
2961  | 
  \infer{P@1 \vee P@2 \rightarrow Q@1 \vee Q@2}
 | 
|
2962  | 
    {P@1 \rightarrow Q@1 & P@2 \rightarrow Q@2}
 | 
|
2963  | 
\]  | 
|
2964  | 
\item De Morgan style equations for reasoning about the ``polarity'' of expressions, e.g.  | 
|
2965  | 
\[  | 
|
2966  | 
(\neg \neg P) ~=~ P \qquad\qquad  | 
|
2967  | 
(\neg (P \wedge Q)) ~=~ (\neg P \vee \neg Q)  | 
|
2968  | 
\]  | 
|
2969  | 
\item Equations for reducing complex operators to more primitive ones whose  | 
|
2970  | 
monotonicity can easily be proved, e.g.  | 
|
2971  | 
\[  | 
|
2972  | 
(P \rightarrow Q) ~=~ (\neg P \vee Q) \qquad\qquad  | 
|
2973  | 
  \mathtt{Ball}~A~P ~\equiv~ \forall x.~x \in A \rightarrow P~x
 | 
|
2974  | 
\]  | 
|
2975  | 
\end{itemize}
 | 
|
2976  | 
||
| 6580 | 2977  | 
\subsection{Example of an inductive definition}
 | 
2978  | 
Two declarations, included in a theory file, define the finite powerset  | 
|
2979  | 
operator.  First we declare the constant~\texttt{Fin}.  Then we declare it
 | 
|
2980  | 
inductively, with two introduction rules:  | 
|
2981  | 
\begin{ttbox}
 | 
|
2982  | 
consts Fin :: 'a set => 'a set set  | 
|
2983  | 
inductive "Fin A"  | 
|
2984  | 
intrs  | 
|
2985  | 
    emptyI  "{\ttlbrace}{\ttrbrace} : Fin A"
 | 
|
2986  | 
insertI "[| a: A; b: Fin A |] ==> insert a b : Fin A"  | 
|
2987  | 
\end{ttbox}
 | 
|
2988  | 
The resulting theory structure contains a substructure, called~\texttt{Fin}.
 | 
|
2989  | 
It contains the \texttt{Fin}$~A$ introduction rules as the list \texttt{Fin.intrs},
 | 
|
2990  | 
and also individually as \texttt{Fin.emptyI} and \texttt{Fin.consI}.  The induction
 | 
|
2991  | 
rule is \texttt{Fin.induct}.
 | 
|
2992  | 
||
2993  | 
For another example, here is a theory file defining the accessible  | 
|
| 7830 | 2994  | 
part of a relation. The paper  | 
| 6580 | 2995  | 
\cite{paulson-CADE} discusses a \ZF\ version of this example in more
 | 
2996  | 
detail.  | 
|
2997  | 
\begin{ttbox}
 | 
|
| 7830 | 2998  | 
Acc = WF + Inductive +  | 
2999  | 
||
3000  | 
consts acc :: "('a * 'a)set => 'a set"   (* accessible part *)
 | 
|
3001  | 
||
| 6580 | 3002  | 
inductive "acc r"  | 
3003  | 
intrs  | 
|
| 7830 | 3004  | 
accI "ALL y. (y, x) : r --> y : acc r ==> x : acc r"  | 
3005  | 
||
| 6580 | 3006  | 
end  | 
3007  | 
\end{ttbox}
 | 
|
3008  | 
The Isabelle distribution contains many other inductive definitions. Simple  | 
|
3009  | 
examples are collected on subdirectory \texttt{HOL/Induct}.  The theory
 | 
|
3010  | 
\texttt{HOL/Induct/LList} contains coinductive definitions.  Larger examples
 | 
|
3011  | 
may be found on other subdirectories of \texttt{HOL}, such as \texttt{IMP},
 | 
|
3012  | 
\texttt{Lambda} and \texttt{Auth}.
 | 
|
3013  | 
||
3014  | 
\index{*coinductive|)} \index{*inductive|)}
 | 
|
3015  | 
||
3016  | 
||
3017  | 
\section{The examples directories}
 | 
|
3018  | 
||
| 6592 | 3019  | 
Directory \texttt{HOL/Auth} contains theories for proving the correctness of
 | 
3020  | 
cryptographic protocols~\cite{paulson-jcs}.  The approach is based upon
 | 
|
3021  | 
operational semantics rather than the more usual belief logics. On the same  | 
|
3022  | 
directory are proofs for some standard examples, such as the Needham-Schroeder  | 
|
3023  | 
public-key authentication protocol and the Otway-Rees  | 
|
3024  | 
protocol.  | 
|
| 6580 | 3025  | 
|
3026  | 
Directory \texttt{HOL/IMP} contains a formalization of various denotational,
 | 
|
3027  | 
operational and axiomatic semantics of a simple while-language, the necessary  | 
|
| 6588 | 3028  | 
equivalence proofs, soundness and completeness of the Hoare rules with  | 
3029  | 
respect to the denotational semantics, and soundness and completeness of a  | 
|
3030  | 
verification condition generator. Much of development is taken from  | 
|
| 6580 | 3031  | 
Winskel~\cite{winskel93}.  For details see~\cite{nipkow-IMP}.
 | 
3032  | 
||
3033  | 
Directory \texttt{HOL/Hoare} contains a user friendly surface syntax for Hoare
 | 
|
3034  | 
logic, including a tactic for generating verification-conditions.  | 
|
3035  | 
||
| 6588 | 3036  | 
Directory \texttt{HOL/MiniML} contains a formalization of the type system of
 | 
3037  | 
the core functional language Mini-ML and a correctness proof for its type  | 
|
3038  | 
inference algorithm $\cal W$~\cite{milner78,nipkow-W}.
 | 
|
| 6580 | 3039  | 
|
3040  | 
Directory \texttt{HOL/Lambda} contains a formalization of untyped
 | 
|
3041  | 
$\lambda$-calculus in de~Bruijn notation and Church-Rosser proofs for $\beta$  | 
|
3042  | 
and $\eta$ reduction~\cite{Nipkow-CR}.
 | 
|
3043  | 
||
3044  | 
Directory \texttt{HOL/Subst} contains Martin Coen's mechanization of a theory of
 | 
|
3045  | 
substitutions and unifiers. It is based on Paulson's previous  | 
|
3046  | 
mechanisation in {\LCF}~\cite{paulson85} of Manna and Waldinger's
 | 
|
3047  | 
theory~\cite{mw81}.  It demonstrates a complicated use of \texttt{recdef},
 | 
|
3048  | 
with nested recursion.  | 
|
3049  | 
||
3050  | 
Directory \texttt{HOL/Induct} presents simple examples of (co)inductive
 | 
|
3051  | 
definitions and datatypes.  | 
|
3052  | 
\begin{itemize}
 | 
|
3053  | 
\item Theory \texttt{PropLog} proves the soundness and completeness of
 | 
|
3054  | 
classical propositional logic, given a truth table semantics. The only  | 
|
3055  | 
connective is $\imp$. A Hilbert-style axiom system is specified, and its  | 
|
3056  | 
  set of theorems defined inductively.  A similar proof in \ZF{} is
 | 
|
3057  | 
  described elsewhere~\cite{paulson-set-II}.
 | 
|
3058  | 
||
3059  | 
\item Theory \texttt{Term} defines the datatype \texttt{term}.
 | 
|
3060  | 
||
3061  | 
\item Theory \texttt{ABexp} defines arithmetic and boolean expressions
 | 
|
3062  | 
as mutually recursive datatypes.  | 
|
3063  | 
||
3064  | 
\item The definition of lazy lists demonstrates methods for handling  | 
|
3065  | 
infinite data structures and coinduction in higher-order  | 
|
3066  | 
  logic~\cite{paulson-coind}.%
 | 
|
3067  | 
\footnote{To be precise, these lists are \emph{potentially infinite} rather
 | 
|
3068  | 
than lazy. Lazy implies a particular operational semantics.}  | 
|
3069  | 
  Theory \thydx{LList} defines an operator for
 | 
|
3070  | 
corecursion on lazy lists, which is used to define a few simple functions  | 
|
3071  | 
such as map and append. A coinduction principle is defined  | 
|
3072  | 
for proving equations on lazy lists.  | 
|
3073  | 
||
3074  | 
\item Theory \thydx{LFilter} defines the filter functional for lazy lists.
 | 
|
3075  | 
This functional is notoriously difficult to define because finding the next  | 
|
3076  | 
element meeting the predicate requires possibly unlimited search. It is not  | 
|
3077  | 
computable, but can be expressed using a combination of induction and  | 
|
3078  | 
corecursion.  | 
|
3079  | 
||
3080  | 
\item Theory \thydx{Exp} illustrates the use of iterated inductive definitions
 | 
|
3081  | 
to express a programming language semantics that appears to require mutual  | 
|
3082  | 
induction. Iterated induction allows greater modularity.  | 
|
3083  | 
\end{itemize}
 | 
|
3084  | 
||
3085  | 
Directory \texttt{HOL/ex} contains other examples and experimental proofs in
 | 
|
3086  | 
{\HOL}.  
 | 
|
3087  | 
\begin{itemize}
 | 
|
3088  | 
\item Theory \texttt{Recdef} presents many examples of using \texttt{recdef}
 | 
|
3089  | 
  to define recursive functions.  Another example is \texttt{Fib}, which
 | 
|
3090  | 
defines the Fibonacci function.  | 
|
3091  | 
||
3092  | 
\item Theory \texttt{Primes} defines the Greatest Common Divisor of two
 | 
|
3093  | 
natural numbers and proves a key lemma of the Fundamental Theorem of  | 
|
3094  | 
Arithmetic: if $p$ is prime and $p$ divides $m\times n$ then $p$ divides~$m$  | 
|
3095  | 
or $p$ divides~$n$.  | 
|
3096  | 
||
3097  | 
\item Theory \texttt{Primrec} develops some computation theory.  It
 | 
|
3098  | 
inductively defines the set of primitive recursive functions and presents a  | 
|
3099  | 
proof that Ackermann's function is not primitive recursive.  | 
|
3100  | 
||
3101  | 
\item File \texttt{cla.ML} demonstrates the classical reasoner on over sixty
 | 
|
3102  | 
predicate calculus theorems, ranging from simple tautologies to  | 
|
3103  | 
moderately difficult problems involving equality and quantifiers.  | 
|
3104  | 
||
3105  | 
\item File \texttt{meson.ML} contains an experimental implementation of the {\sc
 | 
|
3106  | 
    meson} proof procedure, inspired by Plaisted~\cite{plaisted90}.  It is
 | 
|
3107  | 
much more powerful than Isabelle's classical reasoner. But it is less  | 
|
3108  | 
useful in practice because it works only for pure logic; it does not  | 
|
3109  | 
accept derived rules for the set theory primitives, for example.  | 
|
3110  | 
||
3111  | 
\item File \texttt{mesontest.ML} contains test data for the {\sc meson} proof
 | 
|
3112  | 
  procedure.  These are mostly taken from Pelletier \cite{pelletier86}.
 | 
|
3113  | 
||
3114  | 
\item File \texttt{set.ML} proves Cantor's Theorem, which is presented in
 | 
|
| 7490 | 3115  | 
  {\S}\ref{sec:hol-cantor} below, and the Schr{\"o}der-Bernstein Theorem.
 | 
| 6580 | 3116  | 
|
3117  | 
\item Theory \texttt{MT} contains Jacob Frost's formalization~\cite{frost93} of
 | 
|
3118  | 
  Milner and Tofte's coinduction example~\cite{milner-coind}.  This
 | 
|
3119  | 
substantial proof concerns the soundness of a type system for a simple  | 
|
3120  | 
functional language. The semantics of recursion is given by a cyclic  | 
|
3121  | 
environment, which makes a coinductive argument appropriate.  | 
|
3122  | 
\end{itemize}
 | 
|
3123  | 
||
3124  | 
||
3125  | 
\goodbreak  | 
|
3126  | 
\section{Example: Cantor's Theorem}\label{sec:hol-cantor}
 | 
|
3127  | 
Cantor's Theorem states that every set has more subsets than it has  | 
|
3128  | 
elements. It has become a favourite example in higher-order logic since  | 
|
3129  | 
it is so easily expressed:  | 
|
3130  | 
\[ \forall f::\alpha \To \alpha \To bool. \exists S::\alpha\To bool.  | 
|
3131  | 
\forall x::\alpha. f~x \not= S  | 
|
3132  | 
\]  | 
|
3133  | 
%  | 
|
3134  | 
Viewing types as sets, $\alpha\To bool$ represents the powerset  | 
|
3135  | 
of~$\alpha$. This version states that for every function from $\alpha$ to  | 
|
3136  | 
its powerset, some subset is outside its range.  | 
|
3137  | 
||
3138  | 
The Isabelle proof uses \HOL's set theory, with the type $\alpha\,set$ and  | 
|
3139  | 
the operator \cdx{range}.
 | 
|
3140  | 
\begin{ttbox}
 | 
|
3141  | 
context Set.thy;  | 
|
3142  | 
\end{ttbox}
 | 
|
3143  | 
The set~$S$ is given as an unknown instead of a  | 
|
3144  | 
quantified variable so that we may inspect the subset found by the proof.  | 
|
3145  | 
\begin{ttbox}
 | 
|
3146  | 
Goal "?S ~: range\thinspace(f :: 'a=>'a set)";  | 
|
3147  | 
{\out Level 0}
 | 
|
3148  | 
{\out ?S ~: range f}
 | 
|
3149  | 
{\out  1. ?S ~: range f}
 | 
|
3150  | 
\end{ttbox}
 | 
|
3151  | 
The first two steps are routine.  The rule \tdx{rangeE} replaces
 | 
|
3152  | 
$\Var{S}\in \texttt{range} \, f$ by $\Var{S}=f~x$ for some~$x$.
 | 
|
3153  | 
\begin{ttbox}
 | 
|
3154  | 
by (resolve_tac [notI] 1);  | 
|
3155  | 
{\out Level 1}
 | 
|
3156  | 
{\out ?S ~: range f}
 | 
|
3157  | 
{\out  1. ?S : range f ==> False}
 | 
|
3158  | 
\ttbreak  | 
|
3159  | 
by (eresolve_tac [rangeE] 1);  | 
|
3160  | 
{\out Level 2}
 | 
|
3161  | 
{\out ?S ~: range f}
 | 
|
3162  | 
{\out  1. !!x. ?S = f x ==> False}
 | 
|
3163  | 
\end{ttbox}
 | 
|
3164  | 
Next, we apply \tdx{equalityCE}, reasoning that since $\Var{S}=f~x$,
 | 
|
3165  | 
we have $\Var{c}\in \Var{S}$ if and only if $\Var{c}\in f~x$ for
 | 
|
3166  | 
any~$\Var{c}$.
 | 
|
3167  | 
\begin{ttbox}
 | 
|
3168  | 
by (eresolve_tac [equalityCE] 1);  | 
|
3169  | 
{\out Level 3}
 | 
|
3170  | 
{\out ?S ~: range f}
 | 
|
3171  | 
{\out  1. !!x. [| ?c3 x : ?S; ?c3 x : f x |] ==> False}
 | 
|
3172  | 
{\out  2. !!x. [| ?c3 x ~: ?S; ?c3 x ~: f x |] ==> False}
 | 
|
3173  | 
\end{ttbox}
 | 
|
3174  | 
Now we use a bit of creativity.  Suppose that~$\Var{S}$ has the form of a
 | 
|
3175  | 
comprehension.  Then $\Var{c}\in\{x.\Var{P}~x\}$ implies
 | 
|
3176  | 
$\Var{P}~\Var{c}$.   Destruct-resolution using \tdx{CollectD}
 | 
|
3177  | 
instantiates~$\Var{S}$ and creates the new assumption.
 | 
|
3178  | 
\begin{ttbox}
 | 
|
3179  | 
by (dresolve_tac [CollectD] 1);  | 
|
3180  | 
{\out Level 4}
 | 
|
3181  | 
{\out {\ttlbrace}x. ?P7 x{\ttrbrace} ~: range f}
 | 
|
3182  | 
{\out  1. !!x. [| ?c3 x : f x; ?P7(?c3 x) |] ==> False}
 | 
|
3183  | 
{\out  2. !!x. [| ?c3 x ~: {\ttlbrace}x. ?P7 x{\ttrbrace}; ?c3 x ~: f x |] ==> False}
 | 
|
3184  | 
\end{ttbox}
 | 
|
3185  | 
Forcing a contradiction between the two assumptions of subgoal~1  | 
|
3186  | 
completes the instantiation of~$S$.  It is now the set $\{x. x\not\in
 | 
|
3187  | 
f~x\}$, which is the standard diagonal construction.  | 
|
3188  | 
\begin{ttbox}
 | 
|
3189  | 
by (contr_tac 1);  | 
|
3190  | 
{\out Level 5}
 | 
|
3191  | 
{\out {\ttlbrace}x. x ~: f x{\ttrbrace} ~: range f}
 | 
|
3192  | 
{\out  1. !!x. [| x ~: {\ttlbrace}x. x ~: f x{\ttrbrace}; x ~: f x |] ==> False}
 | 
|
3193  | 
\end{ttbox}
 | 
|
3194  | 
The rest should be easy.  To apply \tdx{CollectI} to the negated
 | 
|
3195  | 
assumption, we employ \ttindex{swap_res_tac}:
 | 
|
3196  | 
\begin{ttbox}
 | 
|
3197  | 
by (swap_res_tac [CollectI] 1);  | 
|
3198  | 
{\out Level 6}
 | 
|
3199  | 
{\out {\ttlbrace}x. x ~: f x{\ttrbrace} ~: range f}
 | 
|
3200  | 
{\out  1. !!x. [| x ~: f x; ~ False |] ==> x ~: f x}
 | 
|
3201  | 
\ttbreak  | 
|
3202  | 
by (assume_tac 1);  | 
|
3203  | 
{\out Level 7}
 | 
|
3204  | 
{\out {\ttlbrace}x. x ~: f x{\ttrbrace} ~: range f}
 | 
|
3205  | 
{\out No subgoals!}
 | 
|
3206  | 
\end{ttbox}
 | 
|
3207  | 
How much creativity is required? As it happens, Isabelle can prove this  | 
|
3208  | 
theorem automatically.  The default classical set \texttt{claset()} contains rules
 | 
|
3209  | 
for most of the constructs of \HOL's set theory. We must augment it with  | 
|
3210  | 
\tdx{equalityCE} to break up set equalities, and then apply best-first
 | 
|
3211  | 
search. Depth-first search would diverge, but best-first search  | 
|
3212  | 
successfully navigates through the large search space.  | 
|
3213  | 
\index{search!best-first}
 | 
|
3214  | 
\begin{ttbox}
 | 
|
3215  | 
choplev 0;  | 
|
3216  | 
{\out Level 0}
 | 
|
3217  | 
{\out ?S ~: range f}
 | 
|
3218  | 
{\out  1. ?S ~: range f}
 | 
|
3219  | 
\ttbreak  | 
|
3220  | 
by (best_tac (claset() addSEs [equalityCE]) 1);  | 
|
3221  | 
{\out Level 1}
 | 
|
3222  | 
{\out {\ttlbrace}x. x ~: f x{\ttrbrace} ~: range f}
 | 
|
3223  | 
{\out No subgoals!}
 | 
|
3224  | 
\end{ttbox}
 | 
|
3225  | 
If you run this example interactively, make sure your current theory contains  | 
|
3226  | 
theory \texttt{Set}, for example by executing \ttindex{context}~{\tt Set.thy}.
 | 
|
3227  | 
Otherwise the default claset may not contain the rules for set theory.  | 
|
3228  | 
\index{higher-order logic|)}
 | 
|
3229  | 
||
3230  | 
%%% Local Variables:  | 
|
3231  | 
%%% mode: latex  | 
|
3232  | 
%%% TeX-master: "logics"  | 
|
3233  | 
%%% End:  |