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