doc-src/ZF/ZF.tex
author paulson
Wed, 03 Feb 1999 14:02:49 +0100
changeset 6173 2c0579e8e6fa
parent 6143 1eb364a68c54
child 6592 c120262044b6
permissions -rw-r--r--
documented typecheck_tac, etc
paulson@6121
     1
%% $Id$
paulson@6121
     2
\chapter{Zermelo-Fraenkel Set Theory}
paulson@6121
     3
\index{set theory|(}
paulson@6121
     4
paulson@6121
     5
The theory~\thydx{ZF} implements Zermelo-Fraenkel set
paulson@6121
     6
theory~\cite{halmos60,suppes72} as an extension of~\texttt{FOL}, classical
paulson@6121
     7
first-order logic.  The theory includes a collection of derived natural
paulson@6121
     8
deduction rules, for use with Isabelle's classical reasoner.  Much
paulson@6121
     9
of it is based on the work of No\"el~\cite{noel}.
paulson@6121
    10
paulson@6121
    11
A tremendous amount of set theory has been formally developed, including the
paulson@6121
    12
basic properties of relations, functions, ordinals and cardinals.  Significant
paulson@6121
    13
results have been proved, such as the Schr\"oder-Bernstein Theorem, the
paulson@6121
    14
Wellordering Theorem and a version of Ramsey's Theorem.  \texttt{ZF} provides
paulson@6121
    15
both the integers and the natural numbers.  General methods have been
paulson@6121
    16
developed for solving recursion equations over monotonic functors; these have
paulson@6121
    17
been applied to yield constructions of lists, trees, infinite lists, etc.
paulson@6121
    18
paulson@6121
    19
\texttt{ZF} has a flexible package for handling inductive definitions,
paulson@6121
    20
such as inference systems, and datatype definitions, such as lists and
paulson@6121
    21
trees.  Moreover it handles coinductive definitions, such as
paulson@6121
    22
bisimulation relations, and codatatype definitions, such as streams.  It
paulson@6121
    23
provides a streamlined syntax for defining primitive recursive functions over
paulson@6121
    24
datatypes. 
paulson@6121
    25
paulson@6121
    26
Because {\ZF} is an extension of {\FOL}, it provides the same
paulson@6121
    27
packages, namely \texttt{hyp_subst_tac}, the simplifier, and the
paulson@6121
    28
classical reasoner.  The default simpset and claset are usually
paulson@6121
    29
satisfactory.
paulson@6121
    30
paulson@6121
    31
Published articles~\cite{paulson-set-I,paulson-set-II} describe \texttt{ZF}
paulson@6121
    32
less formally than this chapter.  Isabelle employs a novel treatment of
paulson@6121
    33
non-well-founded data structures within the standard {\sc zf} axioms including
paulson@6121
    34
the Axiom of Foundation~\cite{paulson-final}.
paulson@6121
    35
paulson@6121
    36
paulson@6121
    37
\section{Which version of axiomatic set theory?}
paulson@6121
    38
The two main axiom systems for set theory are Bernays-G\"odel~({\sc bg})
paulson@6121
    39
and Zermelo-Fraenkel~({\sc zf}).  Resolution theorem provers can use {\sc
paulson@6121
    40
  bg} because it is finite~\cite{boyer86,quaife92}.  {\sc zf} does not
paulson@6121
    41
have a finite axiom system because of its Axiom Scheme of Replacement.
paulson@6121
    42
This makes it awkward to use with many theorem provers, since instances
paulson@6121
    43
of the axiom scheme have to be invoked explicitly.  Since Isabelle has no
paulson@6121
    44
difficulty with axiom schemes, we may adopt either axiom system.
paulson@6121
    45
paulson@6121
    46
These two theories differ in their treatment of {\bf classes}, which are
paulson@6121
    47
collections that are `too big' to be sets.  The class of all sets,~$V$,
paulson@6121
    48
cannot be a set without admitting Russell's Paradox.  In {\sc bg}, both
paulson@6121
    49
classes and sets are individuals; $x\in V$ expresses that $x$ is a set.  In
paulson@6121
    50
{\sc zf}, all variables denote sets; classes are identified with unary
paulson@6121
    51
predicates.  The two systems define essentially the same sets and classes,
paulson@6121
    52
with similar properties.  In particular, a class cannot belong to another
paulson@6121
    53
class (let alone a set).
paulson@6121
    54
paulson@6121
    55
Modern set theorists tend to prefer {\sc zf} because they are mainly concerned
paulson@6121
    56
with sets, rather than classes.  {\sc bg} requires tiresome proofs that various
paulson@6121
    57
collections are sets; for instance, showing $x\in\{x\}$ requires showing that
paulson@6121
    58
$x$ is a set.
paulson@6121
    59
paulson@6121
    60
paulson@6121
    61
\begin{figure} \small
paulson@6121
    62
\begin{center}
paulson@6121
    63
\begin{tabular}{rrr} 
paulson@6121
    64
  \it name      &\it meta-type  & \it description \\ 
paulson@6121
    65
  \cdx{Let}     & $[\alpha,\alpha\To\beta]\To\beta$ & let binder\\
paulson@6121
    66
  \cdx{0}       & $i$           & empty set\\
paulson@6121
    67
  \cdx{cons}    & $[i,i]\To i$  & finite set constructor\\
paulson@6121
    68
  \cdx{Upair}   & $[i,i]\To i$  & unordered pairing\\
paulson@6121
    69
  \cdx{Pair}    & $[i,i]\To i$  & ordered pairing\\
paulson@6121
    70
  \cdx{Inf}     & $i$   & infinite set\\
paulson@6121
    71
  \cdx{Pow}     & $i\To i$      & powerset\\
paulson@6121
    72
  \cdx{Union} \cdx{Inter} & $i\To i$    & set union/intersection \\
paulson@6121
    73
  \cdx{split}   & $[[i,i]\To i, i] \To i$ & generalized projection\\
paulson@6121
    74
  \cdx{fst} \cdx{snd}   & $i\To i$      & projections\\
paulson@6121
    75
  \cdx{converse}& $i\To i$      & converse of a relation\\
paulson@6121
    76
  \cdx{succ}    & $i\To i$      & successor\\
paulson@6121
    77
  \cdx{Collect} & $[i,i\To o]\To i$     & separation\\
paulson@6121
    78
  \cdx{Replace} & $[i, [i,i]\To o] \To i$       & replacement\\
paulson@6121
    79
  \cdx{PrimReplace} & $[i, [i,i]\To o] \To i$   & primitive replacement\\
paulson@6121
    80
  \cdx{RepFun}  & $[i, i\To i] \To i$   & functional replacement\\
paulson@6121
    81
  \cdx{Pi} \cdx{Sigma}  & $[i,i\To i]\To i$     & general product/sum\\
paulson@6121
    82
  \cdx{domain}  & $i\To i$      & domain of a relation\\
paulson@6121
    83
  \cdx{range}   & $i\To i$      & range of a relation\\
paulson@6121
    84
  \cdx{field}   & $i\To i$      & field of a relation\\
paulson@6121
    85
  \cdx{Lambda}  & $[i, i\To i]\To i$    & $\lambda$-abstraction\\
paulson@6121
    86
  \cdx{restrict}& $[i, i] \To i$        & restriction of a function\\
paulson@6121
    87
  \cdx{The}     & $[i\To o]\To i$       & definite description\\
paulson@6121
    88
  \cdx{if}      & $[o,i,i]\To i$        & conditional\\
paulson@6121
    89
  \cdx{Ball} \cdx{Bex}  & $[i, i\To o]\To o$    & bounded quantifiers
paulson@6121
    90
\end{tabular}
paulson@6121
    91
\end{center}
paulson@6121
    92
\subcaption{Constants}
paulson@6121
    93
paulson@6121
    94
\begin{center}
paulson@6121
    95
\index{*"`"` symbol}
paulson@6121
    96
\index{*"-"`"` symbol}
paulson@6121
    97
\index{*"` symbol}\index{function applications!in \ZF}
paulson@6121
    98
\index{*"- symbol}
paulson@6121
    99
\index{*": symbol}
paulson@6121
   100
\index{*"<"= symbol}
paulson@6121
   101
\begin{tabular}{rrrr} 
paulson@6121
   102
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
   103
  \tt ``        & $[i,i]\To i$  &  Left 90      & image \\
paulson@6121
   104
  \tt -``       & $[i,i]\To i$  &  Left 90      & inverse image \\
paulson@6121
   105
  \tt `         & $[i,i]\To i$  &  Left 90      & application \\
paulson@6121
   106
  \sdx{Int}     & $[i,i]\To i$  &  Left 70      & intersection ($\int$) \\
paulson@6121
   107
  \sdx{Un}      & $[i,i]\To i$  &  Left 65      & union ($\un$) \\
paulson@6121
   108
  \tt -         & $[i,i]\To i$  &  Left 65      & set difference ($-$) \\[1ex]
paulson@6121
   109
  \tt:          & $[i,i]\To o$  &  Left 50      & membership ($\in$) \\
paulson@6121
   110
  \tt <=        & $[i,i]\To o$  &  Left 50      & subset ($\subseteq$) 
paulson@6121
   111
\end{tabular}
paulson@6121
   112
\end{center}
paulson@6121
   113
\subcaption{Infixes}
paulson@6121
   114
\caption{Constants of {\ZF}} \label{zf-constants}
paulson@6121
   115
\end{figure} 
paulson@6121
   116
paulson@6121
   117
paulson@6121
   118
\section{The syntax of set theory}
paulson@6121
   119
The language of set theory, as studied by logicians, has no constants.  The
paulson@6121
   120
traditional axioms merely assert the existence of empty sets, unions,
paulson@6121
   121
powersets, etc.; this would be intolerable for practical reasoning.  The
paulson@6121
   122
Isabelle theory declares constants for primitive sets.  It also extends
paulson@6121
   123
\texttt{FOL} with additional syntax for finite sets, ordered pairs,
paulson@6121
   124
comprehension, general union/intersection, general sums/products, and
paulson@6121
   125
bounded quantifiers.  In most other respects, Isabelle implements precisely
paulson@6121
   126
Zermelo-Fraenkel set theory.
paulson@6121
   127
paulson@6121
   128
Figure~\ref{zf-constants} lists the constants and infixes of~\ZF, while
paulson@6121
   129
Figure~\ref{zf-trans} presents the syntax translations.  Finally,
paulson@6121
   130
Figure~\ref{zf-syntax} presents the full grammar for set theory, including
paulson@6121
   131
the constructs of \FOL.
paulson@6121
   132
paulson@6121
   133
Local abbreviations can be introduced by a \texttt{let} construct whose
paulson@6121
   134
syntax appears in Fig.\ts\ref{zf-syntax}.  Internally it is translated into
paulson@6121
   135
the constant~\cdx{Let}.  It can be expanded by rewriting with its
paulson@6121
   136
definition, \tdx{Let_def}.
paulson@6121
   137
paulson@6121
   138
Apart from \texttt{let}, set theory does not use polymorphism.  All terms in
paulson@6121
   139
{\ZF} have type~\tydx{i}, which is the type of individuals and has class~{\tt
paulson@6121
   140
  term}.  The type of first-order formulae, remember, is~\textit{o}.
paulson@6121
   141
paulson@6121
   142
Infix operators include binary union and intersection ($A\un B$ and
paulson@6121
   143
$A\int B$), set difference ($A-B$), and the subset and membership
paulson@6121
   144
relations.  Note that $a$\verb|~:|$b$ is translated to $\neg(a\in b)$.  The
paulson@6121
   145
union and intersection operators ($\bigcup A$ and $\bigcap A$) form the
paulson@6121
   146
union or intersection of a set of sets; $\bigcup A$ means the same as
paulson@6121
   147
$\bigcup@{x\in A}x$.  Of these operators, only $\bigcup A$ is primitive.
paulson@6121
   148
paulson@6121
   149
The constant \cdx{Upair} constructs unordered pairs; thus {\tt
paulson@6121
   150
  Upair($A$,$B$)} denotes the set~$\{A,B\}$ and \texttt{Upair($A$,$A$)}
paulson@6121
   151
denotes the singleton~$\{A\}$.  General union is used to define binary
paulson@6121
   152
union.  The Isabelle version goes on to define the constant
paulson@6121
   153
\cdx{cons}:
paulson@6121
   154
\begin{eqnarray*}
paulson@6121
   155
   A\cup B              & \equiv &       \bigcup(\texttt{Upair}(A,B)) \\
paulson@6121
   156
   \texttt{cons}(a,B)      & \equiv &        \texttt{Upair}(a,a) \un B
paulson@6121
   157
\end{eqnarray*}
paulson@6121
   158
The $\{a@1, \ldots\}$ notation abbreviates finite sets constructed in the
paulson@6121
   159
obvious manner using~\texttt{cons} and~$\emptyset$ (the empty set):
paulson@6121
   160
\begin{eqnarray*}
paulson@6121
   161
 \{a,b,c\} & \equiv & \texttt{cons}(a,\texttt{cons}(b,\texttt{cons}(c,\emptyset)))
paulson@6121
   162
\end{eqnarray*}
paulson@6121
   163
paulson@6121
   164
The constant \cdx{Pair} constructs ordered pairs, as in {\tt
paulson@6121
   165
Pair($a$,$b$)}.  Ordered pairs may also be written within angle brackets,
paulson@6121
   166
as {\tt<$a$,$b$>}.  The $n$-tuple {\tt<$a@1$,\ldots,$a@{n-1}$,$a@n$>}
paulson@6121
   167
abbreviates the nest of pairs\par\nobreak
paulson@6121
   168
\centerline{\texttt{Pair($a@1$,\ldots,Pair($a@{n-1}$,$a@n$)\ldots).}}
paulson@6121
   169
paulson@6121
   170
In {\ZF}, a function is a set of pairs.  A {\ZF} function~$f$ is simply an
paulson@6121
   171
individual as far as Isabelle is concerned: its Isabelle type is~$i$, not
paulson@6121
   172
say $i\To i$.  The infix operator~{\tt`} denotes the application of a
paulson@6121
   173
function set to its argument; we must write~$f{\tt`}x$, not~$f(x)$.  The
paulson@6121
   174
syntax for image is~$f{\tt``}A$ and that for inverse image is~$f{\tt-``}A$.
paulson@6121
   175
paulson@6121
   176
paulson@6121
   177
\begin{figure} 
paulson@6121
   178
\index{lambda abs@$\lambda$-abstractions!in \ZF}
paulson@6121
   179
\index{*"-"> symbol}
paulson@6121
   180
\index{*"* symbol}
paulson@6121
   181
\begin{center} \footnotesize\tt\frenchspacing
paulson@6121
   182
\begin{tabular}{rrr} 
paulson@6121
   183
  \it external          & \it internal  & \it description \\ 
paulson@6121
   184
  $a$ \ttilde: $b$      & \ttilde($a$ : $b$)    & \rm negated membership\\
paulson@6121
   185
  \ttlbrace$a@1$, $\ldots$, $a@n$\ttrbrace  &  cons($a@1$,$\ldots$,cons($a@n$,0)) &
paulson@6121
   186
        \rm finite set \\
paulson@6121
   187
  <$a@1$, $\ldots$, $a@{n-1}$, $a@n$> & 
paulson@6121
   188
        Pair($a@1$,\ldots,Pair($a@{n-1}$,$a@n$)\ldots) &
paulson@6121
   189
        \rm ordered $n$-tuple \\
paulson@6121
   190
  \ttlbrace$x$:$A . P[x]$\ttrbrace    &  Collect($A$,$\lambda x. P[x]$) &
paulson@6121
   191
        \rm separation \\
paulson@6121
   192
  \ttlbrace$y . x$:$A$, $Q[x,y]$\ttrbrace  &  Replace($A$,$\lambda x\,y. Q[x,y]$) &
paulson@6121
   193
        \rm replacement \\
paulson@6121
   194
  \ttlbrace$b[x] . x$:$A$\ttrbrace  &  RepFun($A$,$\lambda x. b[x]$) &
paulson@6121
   195
        \rm functional replacement \\
paulson@6121
   196
  \sdx{INT} $x$:$A . B[x]$      & Inter(\ttlbrace$B[x] . x$:$A$\ttrbrace) &
paulson@6121
   197
        \rm general intersection \\
paulson@6121
   198
  \sdx{UN}  $x$:$A . B[x]$      & Union(\ttlbrace$B[x] . x$:$A$\ttrbrace) &
paulson@6121
   199
        \rm general union \\
paulson@6121
   200
  \sdx{PROD} $x$:$A . B[x]$     & Pi($A$,$\lambda x. B[x]$) & 
paulson@6121
   201
        \rm general product \\
paulson@6121
   202
  \sdx{SUM}  $x$:$A . B[x]$     & Sigma($A$,$\lambda x. B[x]$) & 
paulson@6121
   203
        \rm general sum \\
paulson@6121
   204
  $A$ -> $B$            & Pi($A$,$\lambda x. B$) & 
paulson@6121
   205
        \rm function space \\
paulson@6121
   206
  $A$ * $B$             & Sigma($A$,$\lambda x. B$) & 
paulson@6121
   207
        \rm binary product \\
paulson@6121
   208
  \sdx{THE}  $x . P[x]$ & The($\lambda x. P[x]$) & 
paulson@6121
   209
        \rm definite description \\
paulson@6121
   210
  \sdx{lam}  $x$:$A . b[x]$     & Lambda($A$,$\lambda x. b[x]$) & 
paulson@6121
   211
        \rm $\lambda$-abstraction\\[1ex]
paulson@6121
   212
  \sdx{ALL} $x$:$A . P[x]$      & Ball($A$,$\lambda x. P[x]$) & 
paulson@6121
   213
        \rm bounded $\forall$ \\
paulson@6121
   214
  \sdx{EX}  $x$:$A . P[x]$      & Bex($A$,$\lambda x. P[x]$) & 
paulson@6121
   215
        \rm bounded $\exists$
paulson@6121
   216
\end{tabular}
paulson@6121
   217
\end{center}
paulson@6121
   218
\caption{Translations for {\ZF}} \label{zf-trans}
paulson@6121
   219
\end{figure} 
paulson@6121
   220
paulson@6121
   221
paulson@6121
   222
\begin{figure} 
paulson@6121
   223
\index{*let symbol}
paulson@6121
   224
\index{*in symbol}
paulson@6121
   225
\dquotes
paulson@6121
   226
\[\begin{array}{rcl}
paulson@6121
   227
    term & = & \hbox{expression of type~$i$} \\
paulson@6121
   228
         & | & "let"~id~"="~term";"\dots";"~id~"="~term~"in"~term \\
paulson@6121
   229
         & | & "if"~term~"then"~term~"else"~term \\
paulson@6121
   230
         & | & "{\ttlbrace} " term\; ("," term)^* " {\ttrbrace}" \\
paulson@6121
   231
         & | & "< "  term\; ("," term)^* " >"  \\
paulson@6121
   232
         & | & "{\ttlbrace} " id ":" term " . " formula " {\ttrbrace}" \\
paulson@6121
   233
         & | & "{\ttlbrace} " id " . " id ":" term ", " formula " {\ttrbrace}" \\
paulson@6121
   234
         & | & "{\ttlbrace} " term " . " id ":" term " {\ttrbrace}" \\
paulson@6121
   235
         & | & term " `` " term \\
paulson@6121
   236
         & | & term " -`` " term \\
paulson@6121
   237
         & | & term " ` " term \\
paulson@6121
   238
         & | & term " * " term \\
paulson@6121
   239
         & | & term " Int " term \\
paulson@6121
   240
         & | & term " Un " term \\
paulson@6121
   241
         & | & term " - " term \\
paulson@6121
   242
         & | & term " -> " term \\
paulson@6121
   243
         & | & "THE~~"  id  " . " formula\\
paulson@6121
   244
         & | & "lam~~"  id ":" term " . " term \\
paulson@6121
   245
         & | & "INT~~"  id ":" term " . " term \\
paulson@6121
   246
         & | & "UN~~~"  id ":" term " . " term \\
paulson@6121
   247
         & | & "PROD~"  id ":" term " . " term \\
paulson@6121
   248
         & | & "SUM~~"  id ":" term " . " term \\[2ex]
paulson@6121
   249
 formula & = & \hbox{expression of type~$o$} \\
paulson@6121
   250
         & | & term " : " term \\
paulson@6121
   251
         & | & term " \ttilde: " term \\
paulson@6121
   252
         & | & term " <= " term \\
paulson@6121
   253
         & | & term " = " term \\
paulson@6121
   254
         & | & term " \ttilde= " term \\
paulson@6121
   255
         & | & "\ttilde\ " formula \\
paulson@6121
   256
         & | & formula " \& " formula \\
paulson@6121
   257
         & | & formula " | " formula \\
paulson@6121
   258
         & | & formula " --> " formula \\
paulson@6121
   259
         & | & formula " <-> " formula \\
paulson@6121
   260
         & | & "ALL " id ":" term " . " formula \\
paulson@6121
   261
         & | & "EX~~" id ":" term " . " formula \\
paulson@6121
   262
         & | & "ALL~" id~id^* " . " formula \\
paulson@6121
   263
         & | & "EX~~" id~id^* " . " formula \\
paulson@6121
   264
         & | & "EX!~" id~id^* " . " formula
paulson@6121
   265
  \end{array}
paulson@6121
   266
\]
paulson@6121
   267
\caption{Full grammar for {\ZF}} \label{zf-syntax}
paulson@6121
   268
\end{figure} 
paulson@6121
   269
paulson@6121
   270
paulson@6121
   271
\section{Binding operators}
paulson@6121
   272
The constant \cdx{Collect} constructs sets by the principle of {\bf
paulson@6121
   273
  separation}.  The syntax for separation is
paulson@6121
   274
\hbox{\tt\ttlbrace$x$:$A$.\ $P[x]$\ttrbrace}, where $P[x]$ is a formula
paulson@6121
   275
that may contain free occurrences of~$x$.  It abbreviates the set {\tt
paulson@6121
   276
  Collect($A$,$\lambda x. P[x]$)}, which consists of all $x\in A$ that
paulson@6121
   277
satisfy~$P[x]$.  Note that \texttt{Collect} is an unfortunate choice of
paulson@6121
   278
name: some set theories adopt a set-formation principle, related to
paulson@6121
   279
replacement, called collection.
paulson@6121
   280
paulson@6121
   281
The constant \cdx{Replace} constructs sets by the principle of {\bf
paulson@6121
   282
  replacement}.  The syntax
paulson@6121
   283
\hbox{\tt\ttlbrace$y$.\ $x$:$A$,$Q[x,y]$\ttrbrace} denotes the set {\tt
paulson@6121
   284
  Replace($A$,$\lambda x\,y. Q[x,y]$)}, which consists of all~$y$ such
paulson@6121
   285
that there exists $x\in A$ satisfying~$Q[x,y]$.  The Replacement Axiom
paulson@6121
   286
has the condition that $Q$ must be single-valued over~$A$: for
paulson@6121
   287
all~$x\in A$ there exists at most one $y$ satisfying~$Q[x,y]$.  A
paulson@6121
   288
single-valued binary predicate is also called a {\bf class function}.
paulson@6121
   289
paulson@6121
   290
The constant \cdx{RepFun} expresses a special case of replacement,
paulson@6121
   291
where $Q[x,y]$ has the form $y=b[x]$.  Such a $Q$ is trivially
paulson@6121
   292
single-valued, since it is just the graph of the meta-level
paulson@6121
   293
function~$\lambda x. b[x]$.  The resulting set consists of all $b[x]$
paulson@6121
   294
for~$x\in A$.  This is analogous to the \ML{} functional \texttt{map},
paulson@6121
   295
since it applies a function to every element of a set.  The syntax is
paulson@6121
   296
\hbox{\tt\ttlbrace$b[x]$.\ $x$:$A$\ttrbrace}, which expands to {\tt
paulson@6121
   297
  RepFun($A$,$\lambda x. b[x]$)}.
paulson@6121
   298
paulson@6121
   299
\index{*INT symbol}\index{*UN symbol} 
paulson@6121
   300
General unions and intersections of indexed
paulson@6121
   301
families of sets, namely $\bigcup@{x\in A}B[x]$ and $\bigcap@{x\in A}B[x]$,
paulson@6121
   302
are written \hbox{\tt UN $x$:$A$.\ $B[x]$} and \hbox{\tt INT $x$:$A$.\ $B[x]$}.
paulson@6121
   303
Their meaning is expressed using \texttt{RepFun} as
paulson@6121
   304
\[
paulson@6121
   305
\bigcup(\{B[x]. x\in A\}) \qquad\hbox{and}\qquad 
paulson@6121
   306
\bigcap(\{B[x]. x\in A\}). 
paulson@6121
   307
\]
paulson@6121
   308
General sums $\sum@{x\in A}B[x]$ and products $\prod@{x\in A}B[x]$ can be
paulson@6121
   309
constructed in set theory, where $B[x]$ is a family of sets over~$A$.  They
paulson@6121
   310
have as special cases $A\times B$ and $A\to B$, where $B$ is simply a set.
paulson@6121
   311
This is similar to the situation in Constructive Type Theory (set theory
paulson@6121
   312
has `dependent sets') and calls for similar syntactic conventions.  The
paulson@6121
   313
constants~\cdx{Sigma} and~\cdx{Pi} construct general sums and
paulson@6121
   314
products.  Instead of \texttt{Sigma($A$,$B$)} and \texttt{Pi($A$,$B$)} we may
paulson@6121
   315
write 
paulson@6121
   316
\hbox{\tt SUM $x$:$A$.\ $B[x]$} and \hbox{\tt PROD $x$:$A$.\ $B[x]$}.  
paulson@6121
   317
\index{*SUM symbol}\index{*PROD symbol}%
paulson@6121
   318
The special cases as \hbox{\tt$A$*$B$} and \hbox{\tt$A$->$B$} abbreviate
paulson@6121
   319
general sums and products over a constant family.\footnote{Unlike normal
paulson@6121
   320
infix operators, {\tt*} and {\tt->} merely define abbreviations; there are
paulson@6121
   321
no constants~\texttt{op~*} and~\hbox{\tt op~->}.} Isabelle accepts these
paulson@6121
   322
abbreviations in parsing and uses them whenever possible for printing.
paulson@6121
   323
paulson@6121
   324
\index{*THE symbol} 
paulson@6121
   325
As mentioned above, whenever the axioms assert the existence and uniqueness
paulson@6121
   326
of a set, Isabelle's set theory declares a constant for that set.  These
paulson@6121
   327
constants can express the {\bf definite description} operator~$\iota
paulson@6121
   328
x. P[x]$, which stands for the unique~$a$ satisfying~$P[a]$, if such exists.
paulson@6121
   329
Since all terms in {\ZF} denote something, a description is always
paulson@6121
   330
meaningful, but we do not know its value unless $P[x]$ defines it uniquely.
paulson@6121
   331
Using the constant~\cdx{The}, we may write descriptions as {\tt
paulson@6121
   332
  The($\lambda x. P[x]$)} or use the syntax \hbox{\tt THE $x$.\ $P[x]$}.
paulson@6121
   333
paulson@6121
   334
\index{*lam symbol}
paulson@6121
   335
Function sets may be written in $\lambda$-notation; $\lambda x\in A. b[x]$
paulson@6121
   336
stands for the set of all pairs $\pair{x,b[x]}$ for $x\in A$.  In order for
paulson@6121
   337
this to be a set, the function's domain~$A$ must be given.  Using the
paulson@6121
   338
constant~\cdx{Lambda}, we may express function sets as {\tt
paulson@6121
   339
Lambda($A$,$\lambda x. b[x]$)} or use the syntax \hbox{\tt lam $x$:$A$.\ $b[x]$}.
paulson@6121
   340
paulson@6121
   341
Isabelle's set theory defines two {\bf bounded quantifiers}:
paulson@6121
   342
\begin{eqnarray*}
paulson@6121
   343
   \forall x\in A. P[x] &\hbox{abbreviates}& \forall x. x\in A\imp P[x] \\
paulson@6121
   344
   \exists x\in A. P[x] &\hbox{abbreviates}& \exists x. x\in A\conj P[x]
paulson@6121
   345
\end{eqnarray*}
paulson@6121
   346
The constants~\cdx{Ball} and~\cdx{Bex} are defined
paulson@6121
   347
accordingly.  Instead of \texttt{Ball($A$,$P$)} and \texttt{Bex($A$,$P$)} we may
paulson@6121
   348
write
paulson@6121
   349
\hbox{\tt ALL $x$:$A$.\ $P[x]$} and \hbox{\tt EX $x$:$A$.\ $P[x]$}.
paulson@6121
   350
paulson@6121
   351
paulson@6121
   352
%%%% ZF.thy
paulson@6121
   353
paulson@6121
   354
\begin{figure}
paulson@6121
   355
\begin{ttbox}
paulson@6121
   356
\tdx{Let_def}            Let(s, f) == f(s)
paulson@6121
   357
paulson@6121
   358
\tdx{Ball_def}           Ball(A,P) == ALL x. x:A --> P(x)
paulson@6121
   359
\tdx{Bex_def}            Bex(A,P)  == EX x. x:A & P(x)
paulson@6121
   360
paulson@6121
   361
\tdx{subset_def}         A <= B  == ALL x:A. x:B
paulson@6121
   362
\tdx{extension}          A = B  <->  A <= B & B <= A
paulson@6121
   363
paulson@6121
   364
\tdx{Union_iff}          A : Union(C) <-> (EX B:C. A:B)
paulson@6121
   365
\tdx{Pow_iff}            A : Pow(B) <-> A <= B
paulson@6121
   366
\tdx{foundation}         A=0 | (EX x:A. ALL y:x. ~ y:A)
paulson@6121
   367
paulson@6121
   368
\tdx{replacement}        (ALL x:A. ALL y z. P(x,y) & P(x,z) --> y=z) ==>
paulson@6121
   369
                   b : PrimReplace(A,P) <-> (EX x:A. P(x,b))
paulson@6121
   370
\subcaption{The Zermelo-Fraenkel Axioms}
paulson@6121
   371
paulson@6121
   372
\tdx{Replace_def}  Replace(A,P) == 
paulson@6121
   373
                   PrimReplace(A, \%x y. (EX!z. P(x,z)) & P(x,y))
paulson@6121
   374
\tdx{RepFun_def}   RepFun(A,f)  == {\ttlbrace}y . x:A, y=f(x)\ttrbrace
paulson@6121
   375
\tdx{the_def}      The(P)       == Union({\ttlbrace}y . x:{\ttlbrace}0{\ttrbrace}, P(y){\ttrbrace})
paulson@6121
   376
\tdx{if_def}       if(P,a,b)    == THE z. P & z=a | ~P & z=b
paulson@6121
   377
\tdx{Collect_def}  Collect(A,P) == {\ttlbrace}y . x:A, x=y & P(x){\ttrbrace}
paulson@6121
   378
\tdx{Upair_def}    Upair(a,b)   == 
paulson@6121
   379
                 {\ttlbrace}y. x:Pow(Pow(0)), (x=0 & y=a) | (x=Pow(0) & y=b){\ttrbrace}
paulson@6121
   380
\subcaption{Consequences of replacement}
paulson@6121
   381
paulson@6121
   382
\tdx{Inter_def}    Inter(A) == {\ttlbrace}x:Union(A) . ALL y:A. x:y{\ttrbrace}
paulson@6121
   383
\tdx{Un_def}       A Un  B  == Union(Upair(A,B))
paulson@6121
   384
\tdx{Int_def}      A Int B  == Inter(Upair(A,B))
paulson@6121
   385
\tdx{Diff_def}     A - B    == {\ttlbrace}x:A . x~:B{\ttrbrace}
paulson@6121
   386
\subcaption{Union, intersection, difference}
paulson@6121
   387
\end{ttbox}
paulson@6121
   388
\caption{Rules and axioms of {\ZF}} \label{zf-rules}
paulson@6121
   389
\end{figure}
paulson@6121
   390
paulson@6121
   391
paulson@6121
   392
\begin{figure}
paulson@6121
   393
\begin{ttbox}
paulson@6121
   394
\tdx{cons_def}     cons(a,A) == Upair(a,a) Un A
paulson@6121
   395
\tdx{succ_def}     succ(i) == cons(i,i)
paulson@6121
   396
\tdx{infinity}     0:Inf & (ALL y:Inf. succ(y): Inf)
paulson@6121
   397
\subcaption{Finite and infinite sets}
paulson@6121
   398
paulson@6121
   399
\tdx{Pair_def}       <a,b>      == {\ttlbrace}{\ttlbrace}a,a{\ttrbrace}, {\ttlbrace}a,b{\ttrbrace}{\ttrbrace}
paulson@6121
   400
\tdx{split_def}      split(c,p) == THE y. EX a b. p=<a,b> & y=c(a,b)
paulson@6121
   401
\tdx{fst_def}        fst(A)     == split(\%x y. x, p)
paulson@6121
   402
\tdx{snd_def}        snd(A)     == split(\%x y. y, p)
paulson@6121
   403
\tdx{Sigma_def}      Sigma(A,B) == UN x:A. UN y:B(x). {\ttlbrace}<x,y>{\ttrbrace}
paulson@6121
   404
\subcaption{Ordered pairs and Cartesian products}
paulson@6121
   405
paulson@6121
   406
\tdx{converse_def}   converse(r) == {\ttlbrace}z. w:r, EX x y. w=<x,y> & z=<y,x>{\ttrbrace}
paulson@6121
   407
\tdx{domain_def}     domain(r)   == {\ttlbrace}x. w:r, EX y. w=<x,y>{\ttrbrace}
paulson@6121
   408
\tdx{range_def}      range(r)    == domain(converse(r))
paulson@6121
   409
\tdx{field_def}      field(r)    == domain(r) Un range(r)
paulson@6121
   410
\tdx{image_def}      r `` A      == {\ttlbrace}y : range(r) . EX x:A. <x,y> : r{\ttrbrace}
paulson@6121
   411
\tdx{vimage_def}     r -`` A     == converse(r)``A
paulson@6121
   412
\subcaption{Operations on relations}
paulson@6121
   413
paulson@6121
   414
\tdx{lam_def}    Lambda(A,b) == {\ttlbrace}<x,b(x)> . x:A{\ttrbrace}
paulson@6121
   415
\tdx{apply_def}  f`a         == THE y. <a,y> : f
paulson@6121
   416
\tdx{Pi_def}     Pi(A,B) == {\ttlbrace}f: Pow(Sigma(A,B)). ALL x:A. EX! y. <x,y>: f{\ttrbrace}
paulson@6121
   417
\tdx{restrict_def}   restrict(f,A) == lam x:A. f`x
paulson@6121
   418
\subcaption{Functions and general product}
paulson@6121
   419
\end{ttbox}
paulson@6121
   420
\caption{Further definitions of {\ZF}} \label{zf-defs}
paulson@6121
   421
\end{figure}
paulson@6121
   422
paulson@6121
   423
paulson@6121
   424
paulson@6121
   425
\section{The Zermelo-Fraenkel axioms}
paulson@6121
   426
The axioms appear in Fig.\ts \ref{zf-rules}.  They resemble those
paulson@6121
   427
presented by Suppes~\cite{suppes72}.  Most of the theory consists of
paulson@6121
   428
definitions.  In particular, bounded quantifiers and the subset relation
paulson@6121
   429
appear in other axioms.  Object-level quantifiers and implications have
paulson@6121
   430
been replaced by meta-level ones wherever possible, to simplify use of the
paulson@6121
   431
axioms.  See the file \texttt{ZF/ZF.thy} for details.
paulson@6121
   432
paulson@6121
   433
The traditional replacement axiom asserts
paulson@6121
   434
\[ y \in \texttt{PrimReplace}(A,P) \bimp (\exists x\in A. P(x,y)) \]
paulson@6121
   435
subject to the condition that $P(x,y)$ is single-valued for all~$x\in A$.
paulson@6121
   436
The Isabelle theory defines \cdx{Replace} to apply
paulson@6121
   437
\cdx{PrimReplace} to the single-valued part of~$P$, namely
paulson@6121
   438
\[ (\exists!z. P(x,z)) \conj P(x,y). \]
paulson@6121
   439
Thus $y\in \texttt{Replace}(A,P)$ if and only if there is some~$x$ such that
paulson@6121
   440
$P(x,-)$ holds uniquely for~$y$.  Because the equivalence is unconditional,
paulson@6121
   441
\texttt{Replace} is much easier to use than \texttt{PrimReplace}; it defines the
paulson@6121
   442
same set, if $P(x,y)$ is single-valued.  The nice syntax for replacement
paulson@6121
   443
expands to \texttt{Replace}.
paulson@6121
   444
paulson@6121
   445
Other consequences of replacement include functional replacement
paulson@6121
   446
(\cdx{RepFun}) and definite descriptions (\cdx{The}).
paulson@6121
   447
Axioms for separation (\cdx{Collect}) and unordered pairs
paulson@6121
   448
(\cdx{Upair}) are traditionally assumed, but they actually follow
paulson@6121
   449
from replacement~\cite[pages 237--8]{suppes72}.
paulson@6121
   450
paulson@6121
   451
The definitions of general intersection, etc., are straightforward.  Note
paulson@6121
   452
the definition of \texttt{cons}, which underlies the finite set notation.
paulson@6121
   453
The axiom of infinity gives us a set that contains~0 and is closed under
paulson@6121
   454
successor (\cdx{succ}).  Although this set is not uniquely defined,
paulson@6121
   455
the theory names it (\cdx{Inf}) in order to simplify the
paulson@6121
   456
construction of the natural numbers.
paulson@6121
   457
                                             
paulson@6121
   458
Further definitions appear in Fig.\ts\ref{zf-defs}.  Ordered pairs are
paulson@6121
   459
defined in the standard way, $\pair{a,b}\equiv\{\{a\},\{a,b\}\}$.  Recall
paulson@6121
   460
that \cdx{Sigma}$(A,B)$ generalizes the Cartesian product of two
paulson@6121
   461
sets.  It is defined to be the union of all singleton sets
paulson@6121
   462
$\{\pair{x,y}\}$, for $x\in A$ and $y\in B(x)$.  This is a typical usage of
paulson@6121
   463
general union.
paulson@6121
   464
paulson@6121
   465
The projections \cdx{fst} and~\cdx{snd} are defined in terms of the
paulson@6121
   466
generalized projection \cdx{split}.  The latter has been borrowed from
paulson@6121
   467
Martin-L\"of's Type Theory, and is often easier to use than \cdx{fst}
paulson@6121
   468
and~\cdx{snd}.
paulson@6121
   469
paulson@6121
   470
Operations on relations include converse, domain, range, and image.  The
paulson@6121
   471
set ${\tt Pi}(A,B)$ generalizes the space of functions between two sets.
paulson@6121
   472
Note the simple definitions of $\lambda$-abstraction (using
paulson@6121
   473
\cdx{RepFun}) and application (using a definite description).  The
paulson@6121
   474
function \cdx{restrict}$(f,A)$ has the same values as~$f$, but only
paulson@6121
   475
over the domain~$A$.
paulson@6121
   476
paulson@6121
   477
paulson@6121
   478
%%%% zf.ML
paulson@6121
   479
paulson@6121
   480
\begin{figure}
paulson@6121
   481
\begin{ttbox}
paulson@6121
   482
\tdx{ballI}       [| !!x. x:A ==> P(x) |] ==> ALL x:A. P(x)
paulson@6121
   483
\tdx{bspec}       [| ALL x:A. P(x);  x: A |] ==> P(x)
paulson@6121
   484
\tdx{ballE}       [| ALL x:A. P(x);  P(x) ==> Q;  ~ x:A ==> Q |] ==> Q
paulson@6121
   485
paulson@6121
   486
\tdx{ball_cong}   [| A=A';  !!x. x:A' ==> P(x) <-> P'(x) |] ==> 
paulson@6121
   487
            (ALL x:A. P(x)) <-> (ALL x:A'. P'(x))
paulson@6121
   488
paulson@6121
   489
\tdx{bexI}        [| P(x);  x: A |] ==> EX x:A. P(x)
paulson@6121
   490
\tdx{bexCI}       [| ALL x:A. ~P(x) ==> P(a);  a: A |] ==> EX x:A. P(x)
paulson@6121
   491
\tdx{bexE}        [| EX x:A. P(x);  !!x. [| x:A; P(x) |] ==> Q |] ==> Q
paulson@6121
   492
paulson@6121
   493
\tdx{bex_cong}    [| A=A';  !!x. x:A' ==> P(x) <-> P'(x) |] ==> 
paulson@6121
   494
            (EX x:A. P(x)) <-> (EX x:A'. P'(x))
paulson@6121
   495
\subcaption{Bounded quantifiers}
paulson@6121
   496
paulson@6121
   497
\tdx{subsetI}       (!!x. x:A ==> x:B) ==> A <= B
paulson@6121
   498
\tdx{subsetD}       [| A <= B;  c:A |] ==> c:B
paulson@6121
   499
\tdx{subsetCE}      [| A <= B;  ~(c:A) ==> P;  c:B ==> P |] ==> P
paulson@6121
   500
\tdx{subset_refl}   A <= A
paulson@6121
   501
\tdx{subset_trans}  [| A<=B;  B<=C |] ==> A<=C
paulson@6121
   502
paulson@6121
   503
\tdx{equalityI}     [| A <= B;  B <= A |] ==> A = B
paulson@6121
   504
\tdx{equalityD1}    A = B ==> A<=B
paulson@6121
   505
\tdx{equalityD2}    A = B ==> B<=A
paulson@6121
   506
\tdx{equalityE}     [| A = B;  [| A<=B; B<=A |] ==> P |]  ==>  P
paulson@6121
   507
\subcaption{Subsets and extensionality}
paulson@6121
   508
paulson@6121
   509
\tdx{emptyE}          a:0 ==> P
paulson@6121
   510
\tdx{empty_subsetI}   0 <= A
paulson@6121
   511
\tdx{equals0I}        [| !!y. y:A ==> False |] ==> A=0
paulson@6121
   512
\tdx{equals0D}        [| A=0;  a:A |] ==> P
paulson@6121
   513
paulson@6121
   514
\tdx{PowI}            A <= B ==> A : Pow(B)
paulson@6121
   515
\tdx{PowD}            A : Pow(B)  ==>  A<=B
paulson@6121
   516
\subcaption{The empty set; power sets}
paulson@6121
   517
\end{ttbox}
paulson@6121
   518
\caption{Basic derived rules for {\ZF}} \label{zf-lemmas1}
paulson@6121
   519
\end{figure}
paulson@6121
   520
paulson@6121
   521
paulson@6121
   522
\section{From basic lemmas to function spaces}
paulson@6121
   523
Faced with so many definitions, it is essential to prove lemmas.  Even
paulson@6121
   524
trivial theorems like $A \int B = B \int A$ would be difficult to
paulson@6121
   525
prove from the definitions alone.  Isabelle's set theory derives many
paulson@6121
   526
rules using a natural deduction style.  Ideally, a natural deduction
paulson@6121
   527
rule should introduce or eliminate just one operator, but this is not
paulson@6121
   528
always practical.  For most operators, we may forget its definition
paulson@6121
   529
and use its derived rules instead.
paulson@6121
   530
paulson@6121
   531
\subsection{Fundamental lemmas}
paulson@6121
   532
Figure~\ref{zf-lemmas1} presents the derived rules for the most basic
paulson@6121
   533
operators.  The rules for the bounded quantifiers resemble those for the
paulson@6121
   534
ordinary quantifiers, but note that \tdx{ballE} uses a negated assumption
paulson@6121
   535
in the style of Isabelle's classical reasoner.  The \rmindex{congruence
paulson@6121
   536
  rules} \tdx{ball_cong} and \tdx{bex_cong} are required by Isabelle's
paulson@6121
   537
simplifier, but have few other uses.  Congruence rules must be specially
paulson@6121
   538
derived for all binding operators, and henceforth will not be shown.
paulson@6121
   539
paulson@6121
   540
Figure~\ref{zf-lemmas1} also shows rules for the subset and equality
paulson@6121
   541
relations (proof by extensionality), and rules about the empty set and the
paulson@6121
   542
power set operator.
paulson@6121
   543
paulson@6121
   544
Figure~\ref{zf-lemmas2} presents rules for replacement and separation.
paulson@6121
   545
The rules for \cdx{Replace} and \cdx{RepFun} are much simpler than
paulson@6121
   546
comparable rules for \texttt{PrimReplace} would be.  The principle of
paulson@6121
   547
separation is proved explicitly, although most proofs should use the
paulson@6121
   548
natural deduction rules for \texttt{Collect}.  The elimination rule
paulson@6121
   549
\tdx{CollectE} is equivalent to the two destruction rules
paulson@6121
   550
\tdx{CollectD1} and \tdx{CollectD2}, but each rule is suited to
paulson@6121
   551
particular circumstances.  Although too many rules can be confusing, there
paulson@6121
   552
is no reason to aim for a minimal set of rules.  See the file
paulson@6121
   553
\texttt{ZF/ZF.ML} for a complete listing.
paulson@6121
   554
paulson@6121
   555
Figure~\ref{zf-lemmas3} presents rules for general union and intersection.
paulson@6121
   556
The empty intersection should be undefined.  We cannot have
paulson@6121
   557
$\bigcap(\emptyset)=V$ because $V$, the universal class, is not a set.  All
paulson@6121
   558
expressions denote something in {\ZF} set theory; the definition of
paulson@6121
   559
intersection implies $\bigcap(\emptyset)=\emptyset$, but this value is
paulson@6121
   560
arbitrary.  The rule \tdx{InterI} must have a premise to exclude
paulson@6121
   561
the empty intersection.  Some of the laws governing intersections require
paulson@6121
   562
similar premises.
paulson@6121
   563
paulson@6121
   564
paulson@6121
   565
%the [p] gives better page breaking for the book
paulson@6121
   566
\begin{figure}[p]
paulson@6121
   567
\begin{ttbox}
paulson@6121
   568
\tdx{ReplaceI}      [| x: A;  P(x,b);  !!y. P(x,y) ==> y=b |] ==> 
paulson@6121
   569
              b : {\ttlbrace}y. x:A, P(x,y){\ttrbrace}
paulson@6121
   570
paulson@6121
   571
\tdx{ReplaceE}      [| b : {\ttlbrace}y. x:A, P(x,y){\ttrbrace};  
paulson@6121
   572
                 !!x. [| x: A;  P(x,b);  ALL y. P(x,y)-->y=b |] ==> R 
paulson@6121
   573
              |] ==> R
paulson@6121
   574
paulson@6121
   575
\tdx{RepFunI}       [| a : A |] ==> f(a) : {\ttlbrace}f(x). x:A{\ttrbrace}
paulson@6121
   576
\tdx{RepFunE}       [| b : {\ttlbrace}f(x). x:A{\ttrbrace};  
paulson@6121
   577
                 !!x.[| x:A;  b=f(x) |] ==> P |] ==> P
paulson@6121
   578
paulson@6121
   579
\tdx{separation}     a : {\ttlbrace}x:A. P(x){\ttrbrace} <-> a:A & P(a)
paulson@6121
   580
\tdx{CollectI}       [| a:A;  P(a) |] ==> a : {\ttlbrace}x:A. P(x){\ttrbrace}
paulson@6121
   581
\tdx{CollectE}       [| a : {\ttlbrace}x:A. P(x){\ttrbrace};  [| a:A; P(a) |] ==> R |] ==> R
paulson@6121
   582
\tdx{CollectD1}      a : {\ttlbrace}x:A. P(x){\ttrbrace} ==> a:A
paulson@6121
   583
\tdx{CollectD2}      a : {\ttlbrace}x:A. P(x){\ttrbrace} ==> P(a)
paulson@6121
   584
\end{ttbox}
paulson@6121
   585
\caption{Replacement and separation} \label{zf-lemmas2}
paulson@6121
   586
\end{figure}
paulson@6121
   587
paulson@6121
   588
paulson@6121
   589
\begin{figure}
paulson@6121
   590
\begin{ttbox}
paulson@6121
   591
\tdx{UnionI}    [| B: C;  A: B |] ==> A: Union(C)
paulson@6121
   592
\tdx{UnionE}    [| A : Union(C);  !!B.[| A: B;  B: C |] ==> R |] ==> R
paulson@6121
   593
paulson@6121
   594
\tdx{InterI}    [| !!x. x: C ==> A: x;  c:C |] ==> A : Inter(C)
paulson@6121
   595
\tdx{InterD}    [| A : Inter(C);  B : C |] ==> A : B
paulson@6121
   596
\tdx{InterE}    [| A : Inter(C);  A:B ==> R;  ~ B:C ==> R |] ==> R
paulson@6121
   597
paulson@6121
   598
\tdx{UN_I}      [| a: A;  b: B(a) |] ==> b: (UN x:A. B(x))
paulson@6121
   599
\tdx{UN_E}      [| b : (UN x:A. B(x));  !!x.[| x: A;  b: B(x) |] ==> R 
paulson@6121
   600
          |] ==> R
paulson@6121
   601
paulson@6121
   602
\tdx{INT_I}     [| !!x. x: A ==> b: B(x);  a: A |] ==> b: (INT x:A. B(x))
paulson@6121
   603
\tdx{INT_E}     [| b : (INT x:A. B(x));  a: A |] ==> b : B(a)
paulson@6121
   604
\end{ttbox}
paulson@6121
   605
\caption{General union and intersection} \label{zf-lemmas3}
paulson@6121
   606
\end{figure}
paulson@6121
   607
paulson@6121
   608
paulson@6121
   609
%%% upair.ML
paulson@6121
   610
paulson@6121
   611
\begin{figure}
paulson@6121
   612
\begin{ttbox}
paulson@6121
   613
\tdx{pairing}      a:Upair(b,c) <-> (a=b | a=c)
paulson@6121
   614
\tdx{UpairI1}      a : Upair(a,b)
paulson@6121
   615
\tdx{UpairI2}      b : Upair(a,b)
paulson@6121
   616
\tdx{UpairE}       [| a : Upair(b,c);  a = b ==> P;  a = c ==> P |] ==> P
paulson@6121
   617
\end{ttbox}
paulson@6121
   618
\caption{Unordered pairs} \label{zf-upair1}
paulson@6121
   619
\end{figure}
paulson@6121
   620
paulson@6121
   621
paulson@6121
   622
\begin{figure}
paulson@6121
   623
\begin{ttbox}
paulson@6121
   624
\tdx{UnI1}         c : A ==> c : A Un B
paulson@6121
   625
\tdx{UnI2}         c : B ==> c : A Un B
paulson@6121
   626
\tdx{UnCI}         (~c : B ==> c : A) ==> c : A Un B
paulson@6121
   627
\tdx{UnE}          [| c : A Un B;  c:A ==> P;  c:B ==> P |] ==> P
paulson@6121
   628
paulson@6121
   629
\tdx{IntI}         [| c : A;  c : B |] ==> c : A Int B
paulson@6121
   630
\tdx{IntD1}        c : A Int B ==> c : A
paulson@6121
   631
\tdx{IntD2}        c : A Int B ==> c : B
paulson@6121
   632
\tdx{IntE}         [| c : A Int B;  [| c:A; c:B |] ==> P |] ==> P
paulson@6121
   633
paulson@6121
   634
\tdx{DiffI}        [| c : A;  ~ c : B |] ==> c : A - B
paulson@6121
   635
\tdx{DiffD1}       c : A - B ==> c : A
paulson@6121
   636
\tdx{DiffD2}       c : A - B ==> c ~: B
paulson@6121
   637
\tdx{DiffE}        [| c : A - B;  [| c:A; ~ c:B |] ==> P |] ==> P
paulson@6121
   638
\end{ttbox}
paulson@6121
   639
\caption{Union, intersection, difference} \label{zf-Un}
paulson@6121
   640
\end{figure}
paulson@6121
   641
paulson@6121
   642
paulson@6121
   643
\begin{figure}
paulson@6121
   644
\begin{ttbox}
paulson@6121
   645
\tdx{consI1}       a : cons(a,B)
paulson@6121
   646
\tdx{consI2}       a : B ==> a : cons(b,B)
paulson@6121
   647
\tdx{consCI}       (~ a:B ==> a=b) ==> a: cons(b,B)
paulson@6121
   648
\tdx{consE}        [| a : cons(b,A);  a=b ==> P;  a:A ==> P |] ==> P
paulson@6121
   649
paulson@6121
   650
\tdx{singletonI}   a : {\ttlbrace}a{\ttrbrace}
paulson@6121
   651
\tdx{singletonE}   [| a : {\ttlbrace}b{\ttrbrace}; a=b ==> P |] ==> P
paulson@6121
   652
\end{ttbox}
paulson@6121
   653
\caption{Finite and singleton sets} \label{zf-upair2}
paulson@6121
   654
\end{figure}
paulson@6121
   655
paulson@6121
   656
paulson@6121
   657
\begin{figure}
paulson@6121
   658
\begin{ttbox}
paulson@6121
   659
\tdx{succI1}       i : succ(i)
paulson@6121
   660
\tdx{succI2}       i : j ==> i : succ(j)
paulson@6121
   661
\tdx{succCI}       (~ i:j ==> i=j) ==> i: succ(j)
paulson@6121
   662
\tdx{succE}        [| i : succ(j);  i=j ==> P;  i:j ==> P |] ==> P
paulson@6121
   663
\tdx{succ_neq_0}   [| succ(n)=0 |] ==> P
paulson@6121
   664
\tdx{succ_inject}  succ(m) = succ(n) ==> m=n
paulson@6121
   665
\end{ttbox}
paulson@6121
   666
\caption{The successor function} \label{zf-succ}
paulson@6121
   667
\end{figure}
paulson@6121
   668
paulson@6121
   669
paulson@6121
   670
\begin{figure}
paulson@6121
   671
\begin{ttbox}
paulson@6121
   672
\tdx{the_equality}     [| P(a);  !!x. P(x) ==> x=a |] ==> (THE x. P(x)) = a
paulson@6121
   673
\tdx{theI}             EX! x. P(x) ==> P(THE x. P(x))
paulson@6121
   674
paulson@6121
   675
\tdx{if_P}              P ==> (if P then a else b) = a
paulson@6121
   676
\tdx{if_not_P}         ~P ==> (if P then a else b) = b
paulson@6121
   677
paulson@6121
   678
\tdx{mem_asym}         [| a:b;  b:a |] ==> P
paulson@6121
   679
\tdx{mem_irrefl}       a:a ==> P
paulson@6121
   680
\end{ttbox}
paulson@6121
   681
\caption{Descriptions; non-circularity} \label{zf-the}
paulson@6121
   682
\end{figure}
paulson@6121
   683
paulson@6121
   684
paulson@6121
   685
\subsection{Unordered pairs and finite sets}
paulson@6121
   686
Figure~\ref{zf-upair1} presents the principle of unordered pairing, along
paulson@6121
   687
with its derived rules.  Binary union and intersection are defined in terms
paulson@6121
   688
of ordered pairs (Fig.\ts\ref{zf-Un}).  Set difference is also included.  The
paulson@6121
   689
rule \tdx{UnCI} is useful for classical reasoning about unions,
paulson@6121
   690
like \texttt{disjCI}\@; it supersedes \tdx{UnI1} and
paulson@6121
   691
\tdx{UnI2}, but these rules are often easier to work with.  For
paulson@6121
   692
intersection and difference we have both elimination and destruction rules.
paulson@6121
   693
Again, there is no reason to provide a minimal rule set.
paulson@6121
   694
paulson@6121
   695
Figure~\ref{zf-upair2} is concerned with finite sets: it presents rules
paulson@6121
   696
for~\texttt{cons}, the finite set constructor, and rules for singleton
paulson@6121
   697
sets.  Figure~\ref{zf-succ} presents derived rules for the successor
paulson@6121
   698
function, which is defined in terms of~\texttt{cons}.  The proof that {\tt
paulson@6121
   699
  succ} is injective appears to require the Axiom of Foundation.
paulson@6121
   700
paulson@6121
   701
Definite descriptions (\sdx{THE}) are defined in terms of the singleton
paulson@6121
   702
set~$\{0\}$, but their derived rules fortunately hide this
paulson@6121
   703
(Fig.\ts\ref{zf-the}).  The rule~\tdx{theI} is difficult to apply
paulson@6121
   704
because of the two occurrences of~$\Var{P}$.  However,
paulson@6121
   705
\tdx{the_equality} does not have this problem and the files contain
paulson@6121
   706
many examples of its use.
paulson@6121
   707
paulson@6121
   708
Finally, the impossibility of having both $a\in b$ and $b\in a$
paulson@6121
   709
(\tdx{mem_asym}) is proved by applying the Axiom of Foundation to
paulson@6121
   710
the set $\{a,b\}$.  The impossibility of $a\in a$ is a trivial consequence.
paulson@6121
   711
paulson@6121
   712
See the file \texttt{ZF/upair.ML} for full proofs of the rules discussed in
paulson@6121
   713
this section.
paulson@6121
   714
paulson@6121
   715
paulson@6121
   716
%%% subset.ML
paulson@6121
   717
paulson@6121
   718
\begin{figure}
paulson@6121
   719
\begin{ttbox}
paulson@6121
   720
\tdx{Union_upper}       B:A ==> B <= Union(A)
paulson@6121
   721
\tdx{Union_least}       [| !!x. x:A ==> x<=C |] ==> Union(A) <= C
paulson@6121
   722
paulson@6121
   723
\tdx{Inter_lower}       B:A ==> Inter(A) <= B
paulson@6121
   724
\tdx{Inter_greatest}    [| a:A;  !!x. x:A ==> C<=x |] ==> C <= Inter(A)
paulson@6121
   725
paulson@6121
   726
\tdx{Un_upper1}         A <= A Un B
paulson@6121
   727
\tdx{Un_upper2}         B <= A Un B
paulson@6121
   728
\tdx{Un_least}          [| A<=C;  B<=C |] ==> A Un B <= C
paulson@6121
   729
paulson@6121
   730
\tdx{Int_lower1}        A Int B <= A
paulson@6121
   731
\tdx{Int_lower2}        A Int B <= B
paulson@6121
   732
\tdx{Int_greatest}      [| C<=A;  C<=B |] ==> C <= A Int B
paulson@6121
   733
paulson@6121
   734
\tdx{Diff_subset}       A-B <= A
paulson@6121
   735
\tdx{Diff_contains}     [| C<=A;  C Int B = 0 |] ==> C <= A-B
paulson@6121
   736
paulson@6121
   737
\tdx{Collect_subset}    Collect(A,P) <= A
paulson@6121
   738
\end{ttbox}
paulson@6121
   739
\caption{Subset and lattice properties} \label{zf-subset}
paulson@6121
   740
\end{figure}
paulson@6121
   741
paulson@6121
   742
paulson@6121
   743
\subsection{Subset and lattice properties}
paulson@6121
   744
The subset relation is a complete lattice.  Unions form least upper bounds;
paulson@6121
   745
non-empty intersections form greatest lower bounds.  Figure~\ref{zf-subset}
paulson@6121
   746
shows the corresponding rules.  A few other laws involving subsets are
paulson@6121
   747
included.  Proofs are in the file \texttt{ZF/subset.ML}.
paulson@6121
   748
paulson@6121
   749
Reasoning directly about subsets often yields clearer proofs than
paulson@6121
   750
reasoning about the membership relation.  Section~\ref{sec:ZF-pow-example}
paulson@6121
   751
below presents an example of this, proving the equation ${{\tt Pow}(A)\cap
paulson@6121
   752
  {\tt Pow}(B)}= {\tt Pow}(A\cap B)$.
paulson@6121
   753
paulson@6121
   754
%%% pair.ML
paulson@6121
   755
paulson@6121
   756
\begin{figure}
paulson@6121
   757
\begin{ttbox}
paulson@6121
   758
\tdx{Pair_inject1}    <a,b> = <c,d> ==> a=c
paulson@6121
   759
\tdx{Pair_inject2}    <a,b> = <c,d> ==> b=d
paulson@6121
   760
\tdx{Pair_inject}     [| <a,b> = <c,d>;  [| a=c; b=d |] ==> P |] ==> P
paulson@6121
   761
\tdx{Pair_neq_0}      <a,b>=0 ==> P
paulson@6121
   762
paulson@6121
   763
\tdx{fst_conv}        fst(<a,b>) = a
paulson@6121
   764
\tdx{snd_conv}        snd(<a,b>) = b
paulson@6121
   765
\tdx{split}           split(\%x y. c(x,y), <a,b>) = c(a,b)
paulson@6121
   766
paulson@6121
   767
\tdx{SigmaI}          [| a:A;  b:B(a) |] ==> <a,b> : Sigma(A,B)
paulson@6121
   768
paulson@6121
   769
\tdx{SigmaE}          [| c: Sigma(A,B);  
paulson@6121
   770
                   !!x y.[| x:A; y:B(x); c=<x,y> |] ==> P |] ==> P
paulson@6121
   771
paulson@6121
   772
\tdx{SigmaE2}         [| <a,b> : Sigma(A,B);    
paulson@6121
   773
                   [| a:A;  b:B(a) |] ==> P   |] ==> P
paulson@6121
   774
\end{ttbox}
paulson@6121
   775
\caption{Ordered pairs; projections; general sums} \label{zf-pair}
paulson@6121
   776
\end{figure}
paulson@6121
   777
paulson@6121
   778
paulson@6121
   779
\subsection{Ordered pairs} \label{sec:pairs}
paulson@6121
   780
paulson@6121
   781
Figure~\ref{zf-pair} presents the rules governing ordered pairs,
paulson@6121
   782
projections and general sums.  File \texttt{ZF/pair.ML} contains the
paulson@6121
   783
full (and tedious) proof that $\{\{a\},\{a,b\}\}$ functions as an ordered
paulson@6121
   784
pair.  This property is expressed as two destruction rules,
paulson@6121
   785
\tdx{Pair_inject1} and \tdx{Pair_inject2}, and equivalently
paulson@6121
   786
as the elimination rule \tdx{Pair_inject}.
paulson@6121
   787
paulson@6121
   788
The rule \tdx{Pair_neq_0} asserts $\pair{a,b}\neq\emptyset$.  This
paulson@6121
   789
is a property of $\{\{a\},\{a,b\}\}$, and need not hold for other 
paulson@6121
   790
encodings of ordered pairs.  The non-standard ordered pairs mentioned below
paulson@6121
   791
satisfy $\pair{\emptyset;\emptyset}=\emptyset$.
paulson@6121
   792
paulson@6121
   793
The natural deduction rules \tdx{SigmaI} and \tdx{SigmaE}
paulson@6121
   794
assert that \cdx{Sigma}$(A,B)$ consists of all pairs of the form
paulson@6121
   795
$\pair{x,y}$, for $x\in A$ and $y\in B(x)$.  The rule \tdx{SigmaE2}
paulson@6121
   796
merely states that $\pair{a,b}\in \texttt{Sigma}(A,B)$ implies $a\in A$ and
paulson@6121
   797
$b\in B(a)$.
paulson@6121
   798
paulson@6121
   799
In addition, it is possible to use tuples as patterns in abstractions:
paulson@6121
   800
\begin{center}
paulson@6121
   801
{\tt\%<$x$,$y$>. $t$} \quad stands for\quad \texttt{split(\%$x$ $y$.\ $t$)}
paulson@6121
   802
\end{center}
paulson@6121
   803
Nested patterns are translated recursively:
paulson@6121
   804
{\tt\%<$x$,$y$,$z$>. $t$} $\leadsto$ {\tt\%<$x$,<$y$,$z$>>. $t$} $\leadsto$
paulson@6121
   805
\texttt{split(\%$x$.\%<$y$,$z$>. $t$)} $\leadsto$ \texttt{split(\%$x$. split(\%$y$
paulson@6121
   806
  $z$.\ $t$))}.  The reverse translation is performed upon printing.
paulson@6121
   807
\begin{warn}
paulson@6121
   808
  The translation between patterns and \texttt{split} is performed automatically
paulson@6121
   809
  by the parser and printer.  Thus the internal and external form of a term
paulson@6121
   810
  may differ, which affects proofs.  For example the term {\tt
paulson@6121
   811
    (\%<x,y>.<y,x>)<a,b>} requires the theorem \texttt{split} to rewrite to
paulson@6121
   812
  {\tt<b,a>}.
paulson@6121
   813
\end{warn}
paulson@6121
   814
In addition to explicit $\lambda$-abstractions, patterns can be used in any
paulson@6121
   815
variable binding construct which is internally described by a
paulson@6121
   816
$\lambda$-abstraction.  Here are some important examples:
paulson@6121
   817
\begin{description}
paulson@6121
   818
\item[Let:] \texttt{let {\it pattern} = $t$ in $u$}
paulson@6121
   819
\item[Choice:] \texttt{THE~{\it pattern}~.~$P$}
paulson@6121
   820
\item[Set operations:] \texttt{UN~{\it pattern}:$A$.~$B$}
paulson@6121
   821
\item[Comprehension:] \texttt{{\ttlbrace}~{\it pattern}:$A$~.~$P$~{\ttrbrace}}
paulson@6121
   822
\end{description}
paulson@6121
   823
paulson@6121
   824
paulson@6121
   825
%%% domrange.ML
paulson@6121
   826
paulson@6121
   827
\begin{figure}
paulson@6121
   828
\begin{ttbox}
paulson@6121
   829
\tdx{domainI}        <a,b>: r ==> a : domain(r)
paulson@6121
   830
\tdx{domainE}        [| a : domain(r);  !!y. <a,y>: r ==> P |] ==> P
paulson@6121
   831
\tdx{domain_subset}  domain(Sigma(A,B)) <= A
paulson@6121
   832
paulson@6121
   833
\tdx{rangeI}         <a,b>: r ==> b : range(r)
paulson@6121
   834
\tdx{rangeE}         [| b : range(r);  !!x. <x,b>: r ==> P |] ==> P
paulson@6121
   835
\tdx{range_subset}   range(A*B) <= B
paulson@6121
   836
paulson@6121
   837
\tdx{fieldI1}        <a,b>: r ==> a : field(r)
paulson@6121
   838
\tdx{fieldI2}        <a,b>: r ==> b : field(r)
paulson@6121
   839
\tdx{fieldCI}        (~ <c,a>:r ==> <a,b>: r) ==> a : field(r)
paulson@6121
   840
paulson@6121
   841
\tdx{fieldE}         [| a : field(r);  
paulson@6121
   842
                  !!x. <a,x>: r ==> P;  
paulson@6121
   843
                  !!x. <x,a>: r ==> P      
paulson@6121
   844
               |] ==> P
paulson@6121
   845
paulson@6121
   846
\tdx{field_subset}   field(A*A) <= A
paulson@6121
   847
\end{ttbox}
paulson@6121
   848
\caption{Domain, range and field of a relation} \label{zf-domrange}
paulson@6121
   849
\end{figure}
paulson@6121
   850
paulson@6121
   851
\begin{figure}
paulson@6121
   852
\begin{ttbox}
paulson@6121
   853
\tdx{imageI}         [| <a,b>: r;  a:A |] ==> b : r``A
paulson@6121
   854
\tdx{imageE}         [| b: r``A;  !!x.[| <x,b>: r;  x:A |] ==> P |] ==> P
paulson@6121
   855
paulson@6121
   856
\tdx{vimageI}        [| <a,b>: r;  b:B |] ==> a : r-``B
paulson@6121
   857
\tdx{vimageE}        [| a: r-``B;  !!x.[| <a,x>: r;  x:B |] ==> P |] ==> P
paulson@6121
   858
\end{ttbox}
paulson@6121
   859
\caption{Image and inverse image} \label{zf-domrange2}
paulson@6121
   860
\end{figure}
paulson@6121
   861
paulson@6121
   862
paulson@6121
   863
\subsection{Relations}
paulson@6121
   864
Figure~\ref{zf-domrange} presents rules involving relations, which are sets
paulson@6121
   865
of ordered pairs.  The converse of a relation~$r$ is the set of all pairs
paulson@6121
   866
$\pair{y,x}$ such that $\pair{x,y}\in r$; if $r$ is a function, then
paulson@6121
   867
{\cdx{converse}$(r)$} is its inverse.  The rules for the domain
paulson@6121
   868
operation, namely \tdx{domainI} and~\tdx{domainE}, assert that
paulson@6121
   869
\cdx{domain}$(r)$ consists of all~$x$ such that $r$ contains
paulson@6121
   870
some pair of the form~$\pair{x,y}$.  The range operation is similar, and
paulson@6121
   871
the field of a relation is merely the union of its domain and range.  
paulson@6121
   872
paulson@6121
   873
Figure~\ref{zf-domrange2} presents rules for images and inverse images.
paulson@6121
   874
Note that these operations are generalisations of range and domain,
paulson@6121
   875
respectively.  See the file \texttt{ZF/domrange.ML} for derivations of the
paulson@6121
   876
rules.
paulson@6121
   877
paulson@6121
   878
paulson@6121
   879
%%% func.ML
paulson@6121
   880
paulson@6121
   881
\begin{figure}
paulson@6121
   882
\begin{ttbox}
paulson@6121
   883
\tdx{fun_is_rel}      f: Pi(A,B) ==> f <= Sigma(A,B)
paulson@6121
   884
paulson@6121
   885
\tdx{apply_equality}  [| <a,b>: f;  f: Pi(A,B) |] ==> f`a = b
paulson@6121
   886
\tdx{apply_equality2} [| <a,b>: f;  <a,c>: f;  f: Pi(A,B) |] ==> b=c
paulson@6121
   887
paulson@6121
   888
\tdx{apply_type}      [| f: Pi(A,B);  a:A |] ==> f`a : B(a)
paulson@6121
   889
\tdx{apply_Pair}      [| f: Pi(A,B);  a:A |] ==> <a,f`a>: f
paulson@6121
   890
\tdx{apply_iff}       f: Pi(A,B) ==> <a,b>: f <-> a:A & f`a = b
paulson@6121
   891
paulson@6121
   892
\tdx{fun_extension}   [| f : Pi(A,B);  g: Pi(A,D);
paulson@6121
   893
                   !!x. x:A ==> f`x = g`x     |] ==> f=g
paulson@6121
   894
paulson@6121
   895
\tdx{domain_type}     [| <a,b> : f;  f: Pi(A,B) |] ==> a : A
paulson@6121
   896
\tdx{range_type}      [| <a,b> : f;  f: Pi(A,B) |] ==> b : B(a)
paulson@6121
   897
paulson@6121
   898
\tdx{Pi_type}         [| f: A->C;  !!x. x:A ==> f`x: B(x) |] ==> f: Pi(A,B)
paulson@6121
   899
\tdx{domain_of_fun}   f: Pi(A,B) ==> domain(f)=A
paulson@6121
   900
\tdx{range_of_fun}    f: Pi(A,B) ==> f: A->range(f)
paulson@6121
   901
paulson@6121
   902
\tdx{restrict}        a : A ==> restrict(f,A) ` a = f`a
paulson@6121
   903
\tdx{restrict_type}   [| !!x. x:A ==> f`x: B(x) |] ==> 
paulson@6121
   904
                restrict(f,A) : Pi(A,B)
paulson@6121
   905
\end{ttbox}
paulson@6121
   906
\caption{Functions} \label{zf-func1}
paulson@6121
   907
\end{figure}
paulson@6121
   908
paulson@6121
   909
paulson@6121
   910
\begin{figure}
paulson@6121
   911
\begin{ttbox}
paulson@6121
   912
\tdx{lamI}         a:A ==> <a,b(a)> : (lam x:A. b(x))
paulson@6121
   913
\tdx{lamE}         [| p: (lam x:A. b(x));  !!x.[| x:A; p=<x,b(x)> |] ==> P 
paulson@6121
   914
             |] ==>  P
paulson@6121
   915
paulson@6121
   916
\tdx{lam_type}     [| !!x. x:A ==> b(x): B(x) |] ==> (lam x:A. b(x)) : Pi(A,B)
paulson@6121
   917
paulson@6121
   918
\tdx{beta}         a : A ==> (lam x:A. b(x)) ` a = b(a)
paulson@6121
   919
\tdx{eta}          f : Pi(A,B) ==> (lam x:A. f`x) = f
paulson@6121
   920
\end{ttbox}
paulson@6121
   921
\caption{$\lambda$-abstraction} \label{zf-lam}
paulson@6121
   922
\end{figure}
paulson@6121
   923
paulson@6121
   924
paulson@6121
   925
\begin{figure}
paulson@6121
   926
\begin{ttbox}
paulson@6121
   927
\tdx{fun_empty}            0: 0->0
paulson@6121
   928
\tdx{fun_single}           {\ttlbrace}<a,b>{\ttrbrace} : {\ttlbrace}a{\ttrbrace} -> {\ttlbrace}b{\ttrbrace}
paulson@6121
   929
paulson@6121
   930
\tdx{fun_disjoint_Un}      [| f: A->B;  g: C->D;  A Int C = 0  |] ==>  
paulson@6121
   931
                     (f Un g) : (A Un C) -> (B Un D)
paulson@6121
   932
paulson@6121
   933
\tdx{fun_disjoint_apply1}  [| a:A;  f: A->B;  g: C->D;  A Int C = 0 |] ==>  
paulson@6121
   934
                     (f Un g)`a = f`a
paulson@6121
   935
paulson@6121
   936
\tdx{fun_disjoint_apply2}  [| c:C;  f: A->B;  g: C->D;  A Int C = 0 |] ==>  
paulson@6121
   937
                     (f Un g)`c = g`c
paulson@6121
   938
\end{ttbox}
paulson@6121
   939
\caption{Constructing functions from smaller sets} \label{zf-func2}
paulson@6121
   940
\end{figure}
paulson@6121
   941
paulson@6121
   942
paulson@6121
   943
\subsection{Functions}
paulson@6121
   944
Functions, represented by graphs, are notoriously difficult to reason
paulson@6121
   945
about.  The file \texttt{ZF/func.ML} derives many rules, which overlap more
paulson@6121
   946
than they ought.  This section presents the more important rules.
paulson@6121
   947
paulson@6121
   948
Figure~\ref{zf-func1} presents the basic properties of \cdx{Pi}$(A,B)$,
paulson@6121
   949
the generalized function space.  For example, if $f$ is a function and
paulson@6121
   950
$\pair{a,b}\in f$, then $f`a=b$ (\tdx{apply_equality}).  Two functions
paulson@6121
   951
are equal provided they have equal domains and deliver equals results
paulson@6121
   952
(\tdx{fun_extension}).
paulson@6121
   953
paulson@6121
   954
By \tdx{Pi_type}, a function typing of the form $f\in A\to C$ can be
paulson@6121
   955
refined to the dependent typing $f\in\prod@{x\in A}B(x)$, given a suitable
paulson@6121
   956
family of sets $\{B(x)\}@{x\in A}$.  Conversely, by \tdx{range_of_fun},
paulson@6121
   957
any dependent typing can be flattened to yield a function type of the form
paulson@6121
   958
$A\to C$; here, $C={\tt range}(f)$.
paulson@6121
   959
paulson@6121
   960
Among the laws for $\lambda$-abstraction, \tdx{lamI} and \tdx{lamE}
paulson@6121
   961
describe the graph of the generated function, while \tdx{beta} and
paulson@6121
   962
\tdx{eta} are the standard conversions.  We essentially have a
paulson@6121
   963
dependently-typed $\lambda$-calculus (Fig.\ts\ref{zf-lam}).
paulson@6121
   964
paulson@6121
   965
Figure~\ref{zf-func2} presents some rules that can be used to construct
paulson@6121
   966
functions explicitly.  We start with functions consisting of at most one
paulson@6121
   967
pair, and may form the union of two functions provided their domains are
paulson@6121
   968
disjoint.  
paulson@6121
   969
paulson@6121
   970
paulson@6121
   971
\begin{figure}
paulson@6121
   972
\begin{ttbox}
paulson@6121
   973
\tdx{Int_absorb}         A Int A = A
paulson@6121
   974
\tdx{Int_commute}        A Int B = B Int A
paulson@6121
   975
\tdx{Int_assoc}          (A Int B) Int C  =  A Int (B Int C)
paulson@6121
   976
\tdx{Int_Un_distrib}     (A Un B) Int C  =  (A Int C) Un (B Int C)
paulson@6121
   977
paulson@6121
   978
\tdx{Un_absorb}          A Un A = A
paulson@6121
   979
\tdx{Un_commute}         A Un B = B Un A
paulson@6121
   980
\tdx{Un_assoc}           (A Un B) Un C  =  A Un (B Un C)
paulson@6121
   981
\tdx{Un_Int_distrib}     (A Int B) Un C  =  (A Un C) Int (B Un C)
paulson@6121
   982
paulson@6121
   983
\tdx{Diff_cancel}        A-A = 0
paulson@6121
   984
\tdx{Diff_disjoint}      A Int (B-A) = 0
paulson@6121
   985
\tdx{Diff_partition}     A<=B ==> A Un (B-A) = B
paulson@6121
   986
\tdx{double_complement}  [| A<=B; B<= C |] ==> (B - (C-A)) = A
paulson@6121
   987
\tdx{Diff_Un}            A - (B Un C) = (A-B) Int (A-C)
paulson@6121
   988
\tdx{Diff_Int}           A - (B Int C) = (A-B) Un (A-C)
paulson@6121
   989
paulson@6121
   990
\tdx{Union_Un_distrib}   Union(A Un B) = Union(A) Un Union(B)
paulson@6121
   991
\tdx{Inter_Un_distrib}   [| a:A;  b:B |] ==> 
paulson@6121
   992
                   Inter(A Un B) = Inter(A) Int Inter(B)
paulson@6121
   993
paulson@6121
   994
\tdx{Int_Union_RepFun}   A Int Union(B) = (UN C:B. A Int C)
paulson@6121
   995
paulson@6121
   996
\tdx{Un_Inter_RepFun}    b:B ==> 
paulson@6121
   997
                   A Un Inter(B) = (INT C:B. A Un C)
paulson@6121
   998
paulson@6121
   999
\tdx{SUM_Un_distrib1}    (SUM x:A Un B. C(x)) = 
paulson@6121
  1000
                   (SUM x:A. C(x)) Un (SUM x:B. C(x))
paulson@6121
  1001
paulson@6121
  1002
\tdx{SUM_Un_distrib2}    (SUM x:C. A(x) Un B(x)) =
paulson@6121
  1003
                   (SUM x:C. A(x))  Un  (SUM x:C. B(x))
paulson@6121
  1004
paulson@6121
  1005
\tdx{SUM_Int_distrib1}   (SUM x:A Int B. C(x)) =
paulson@6121
  1006
                   (SUM x:A. C(x)) Int (SUM x:B. C(x))
paulson@6121
  1007
paulson@6121
  1008
\tdx{SUM_Int_distrib2}   (SUM x:C. A(x) Int B(x)) =
paulson@6121
  1009
                   (SUM x:C. A(x)) Int (SUM x:C. B(x))
paulson@6121
  1010
\end{ttbox}
paulson@6121
  1011
\caption{Equalities} \label{zf-equalities}
paulson@6121
  1012
\end{figure}
paulson@6121
  1013
paulson@6121
  1014
paulson@6121
  1015
\begin{figure}
paulson@6121
  1016
%\begin{constants} 
paulson@6121
  1017
%  \cdx{1}       & $i$           &       & $\{\emptyset\}$       \\
paulson@6121
  1018
%  \cdx{bool}    & $i$           &       & the set $\{\emptyset,1\}$     \\
paulson@6121
  1019
%  \cdx{cond}   & $[i,i,i]\To i$ &       & conditional for \texttt{bool}    \\
paulson@6121
  1020
%  \cdx{not}    & $i\To i$       &       & negation for \texttt{bool}       \\
paulson@6121
  1021
%  \sdx{and}    & $[i,i]\To i$   & Left 70 & conjunction for \texttt{bool}  \\
paulson@6121
  1022
%  \sdx{or}     & $[i,i]\To i$   & Left 65 & disjunction for \texttt{bool}  \\
paulson@6121
  1023
%  \sdx{xor}    & $[i,i]\To i$   & Left 65 & exclusive-or for \texttt{bool}
paulson@6121
  1024
%\end{constants}
paulson@6121
  1025
%
paulson@6121
  1026
\begin{ttbox}
paulson@6121
  1027
\tdx{bool_def}       bool == {\ttlbrace}0,1{\ttrbrace}
paulson@6121
  1028
\tdx{cond_def}       cond(b,c,d) == if b=1 then c else d
paulson@6121
  1029
\tdx{not_def}        not(b)  == cond(b,0,1)
paulson@6121
  1030
\tdx{and_def}        a and b == cond(a,b,0)
paulson@6121
  1031
\tdx{or_def}         a or b  == cond(a,1,b)
paulson@6121
  1032
\tdx{xor_def}        a xor b == cond(a,not(b),b)
paulson@6121
  1033
paulson@6121
  1034
\tdx{bool_1I}        1 : bool
paulson@6121
  1035
\tdx{bool_0I}        0 : bool
paulson@6121
  1036
\tdx{boolE}          [| c: bool;  c=1 ==> P;  c=0 ==> P |] ==> P
paulson@6121
  1037
\tdx{cond_1}         cond(1,c,d) = c
paulson@6121
  1038
\tdx{cond_0}         cond(0,c,d) = d
paulson@6121
  1039
\end{ttbox}
paulson@6121
  1040
\caption{The booleans} \label{zf-bool}
paulson@6121
  1041
\end{figure}
paulson@6121
  1042
paulson@6121
  1043
paulson@6121
  1044
\section{Further developments}
paulson@6121
  1045
The next group of developments is complex and extensive, and only
paulson@6121
  1046
highlights can be covered here.  It involves many theories and ML files of
paulson@6121
  1047
proofs. 
paulson@6121
  1048
paulson@6121
  1049
Figure~\ref{zf-equalities} presents commutative, associative, distributive,
paulson@6121
  1050
and idempotency laws of union and intersection, along with other equations.
paulson@6121
  1051
See file \texttt{ZF/equalities.ML}.
paulson@6121
  1052
paulson@6121
  1053
Theory \thydx{Bool} defines $\{0,1\}$ as a set of booleans, with the usual
paulson@6121
  1054
operators including a conditional (Fig.\ts\ref{zf-bool}).  Although {\ZF} is a
paulson@6121
  1055
first-order theory, you can obtain the effect of higher-order logic using
paulson@6121
  1056
\texttt{bool}-valued functions, for example.  The constant~\texttt{1} is
paulson@6121
  1057
translated to \texttt{succ(0)}.
paulson@6121
  1058
paulson@6121
  1059
\begin{figure}
paulson@6121
  1060
\index{*"+ symbol}
paulson@6121
  1061
\begin{constants}
paulson@6121
  1062
  \it symbol    & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1063
  \tt +         & $[i,i]\To i$  &  Right 65     & disjoint union operator\\
paulson@6121
  1064
  \cdx{Inl}~~\cdx{Inr}  & $i\To i$      &       & injections\\
paulson@6121
  1065
  \cdx{case}    & $[i\To i,i\To i, i]\To i$ &   & conditional for $A+B$
paulson@6121
  1066
\end{constants}
paulson@6121
  1067
\begin{ttbox}
paulson@6121
  1068
\tdx{sum_def}        A+B == {\ttlbrace}0{\ttrbrace}*A Un {\ttlbrace}1{\ttrbrace}*B
paulson@6121
  1069
\tdx{Inl_def}        Inl(a) == <0,a>
paulson@6121
  1070
\tdx{Inr_def}        Inr(b) == <1,b>
paulson@6121
  1071
\tdx{case_def}       case(c,d,u) == split(\%y z. cond(y, d(z), c(z)), u)
paulson@6121
  1072
paulson@6121
  1073
\tdx{sum_InlI}       a : A ==> Inl(a) : A+B
paulson@6121
  1074
\tdx{sum_InrI}       b : B ==> Inr(b) : A+B
paulson@6121
  1075
paulson@6121
  1076
\tdx{Inl_inject}     Inl(a)=Inl(b) ==> a=b
paulson@6121
  1077
\tdx{Inr_inject}     Inr(a)=Inr(b) ==> a=b
paulson@6121
  1078
\tdx{Inl_neq_Inr}    Inl(a)=Inr(b) ==> P
paulson@6121
  1079
paulson@6121
  1080
\tdx{sumE2}   u: A+B ==> (EX x. x:A & u=Inl(x)) | (EX y. y:B & u=Inr(y))
paulson@6121
  1081
paulson@6121
  1082
\tdx{case_Inl}       case(c,d,Inl(a)) = c(a)
paulson@6121
  1083
\tdx{case_Inr}       case(c,d,Inr(b)) = d(b)
paulson@6121
  1084
\end{ttbox}
paulson@6121
  1085
\caption{Disjoint unions} \label{zf-sum}
paulson@6121
  1086
\end{figure}
paulson@6121
  1087
paulson@6121
  1088
paulson@6121
  1089
Theory \thydx{Sum} defines the disjoint union of two sets, with
paulson@6121
  1090
injections and a case analysis operator (Fig.\ts\ref{zf-sum}).  Disjoint
paulson@6121
  1091
unions play a role in datatype definitions, particularly when there is
paulson@6121
  1092
mutual recursion~\cite{paulson-set-II}.
paulson@6121
  1093
paulson@6121
  1094
\begin{figure}
paulson@6121
  1095
\begin{ttbox}
paulson@6121
  1096
\tdx{QPair_def}       <a;b> == a+b
paulson@6121
  1097
\tdx{qsplit_def}      qsplit(c,p)  == THE y. EX a b. p=<a;b> & y=c(a,b)
paulson@6121
  1098
\tdx{qfsplit_def}     qfsplit(R,z) == EX x y. z=<x;y> & R(x,y)
paulson@6121
  1099
\tdx{qconverse_def}   qconverse(r) == {\ttlbrace}z. w:r, EX x y. w=<x;y> & z=<y;x>{\ttrbrace}
paulson@6121
  1100
\tdx{QSigma_def}      QSigma(A,B)  == UN x:A. UN y:B(x). {\ttlbrace}<x;y>{\ttrbrace}
paulson@6121
  1101
paulson@6121
  1102
\tdx{qsum_def}        A <+> B      == ({\ttlbrace}0{\ttrbrace} <*> A) Un ({\ttlbrace}1{\ttrbrace} <*> B)
paulson@6121
  1103
\tdx{QInl_def}        QInl(a)      == <0;a>
paulson@6121
  1104
\tdx{QInr_def}        QInr(b)      == <1;b>
paulson@6121
  1105
\tdx{qcase_def}       qcase(c,d)   == qsplit(\%y z. cond(y, d(z), c(z)))
paulson@6121
  1106
\end{ttbox}
paulson@6121
  1107
\caption{Non-standard pairs, products and sums} \label{zf-qpair}
paulson@6121
  1108
\end{figure}
paulson@6121
  1109
paulson@6121
  1110
Theory \thydx{QPair} defines a notion of ordered pair that admits
paulson@6121
  1111
non-well-founded tupling (Fig.\ts\ref{zf-qpair}).  Such pairs are written
paulson@6121
  1112
{\tt<$a$;$b$>}.  It also defines the eliminator \cdx{qsplit}, the
paulson@6121
  1113
converse operator \cdx{qconverse}, and the summation operator
paulson@6121
  1114
\cdx{QSigma}.  These are completely analogous to the corresponding
paulson@6121
  1115
versions for standard ordered pairs.  The theory goes on to define a
paulson@6121
  1116
non-standard notion of disjoint sum using non-standard pairs.  All of these
paulson@6121
  1117
concepts satisfy the same properties as their standard counterparts; in
paulson@6121
  1118
addition, {\tt<$a$;$b$>} is continuous.  The theory supports coinductive
paulson@6121
  1119
definitions, for example of infinite lists~\cite{paulson-final}.
paulson@6121
  1120
paulson@6121
  1121
\begin{figure}
paulson@6121
  1122
\begin{ttbox}
paulson@6121
  1123
\tdx{bnd_mono_def}   bnd_mono(D,h) == 
paulson@6121
  1124
                 h(D)<=D & (ALL W X. W<=X --> X<=D --> h(W) <= h(X))
paulson@6121
  1125
paulson@6121
  1126
\tdx{lfp_def}        lfp(D,h) == Inter({\ttlbrace}X: Pow(D). h(X) <= X{\ttrbrace})
paulson@6121
  1127
\tdx{gfp_def}        gfp(D,h) == Union({\ttlbrace}X: Pow(D). X <= h(X){\ttrbrace})
paulson@6121
  1128
paulson@6121
  1129
paulson@6121
  1130
\tdx{lfp_lowerbound} [| h(A) <= A;  A<=D |] ==> lfp(D,h) <= A
paulson@6121
  1131
paulson@6121
  1132
\tdx{lfp_subset}     lfp(D,h) <= D
paulson@6121
  1133
paulson@6121
  1134
\tdx{lfp_greatest}   [| bnd_mono(D,h);  
paulson@6121
  1135
                  !!X. [| h(X) <= X;  X<=D |] ==> A<=X 
paulson@6121
  1136
               |] ==> A <= lfp(D,h)
paulson@6121
  1137
paulson@6121
  1138
\tdx{lfp_Tarski}     bnd_mono(D,h) ==> lfp(D,h) = h(lfp(D,h))
paulson@6121
  1139
paulson@6121
  1140
\tdx{induct}         [| a : lfp(D,h);  bnd_mono(D,h);
paulson@6121
  1141
                  !!x. x : h(Collect(lfp(D,h),P)) ==> P(x)
paulson@6121
  1142
               |] ==> P(a)
paulson@6121
  1143
paulson@6121
  1144
\tdx{lfp_mono}       [| bnd_mono(D,h);  bnd_mono(E,i);
paulson@6121
  1145
                  !!X. X<=D ==> h(X) <= i(X)  
paulson@6121
  1146
               |] ==> lfp(D,h) <= lfp(E,i)
paulson@6121
  1147
paulson@6121
  1148
\tdx{gfp_upperbound} [| A <= h(A);  A<=D |] ==> A <= gfp(D,h)
paulson@6121
  1149
paulson@6121
  1150
\tdx{gfp_subset}     gfp(D,h) <= D
paulson@6121
  1151
paulson@6121
  1152
\tdx{gfp_least}      [| bnd_mono(D,h);  
paulson@6121
  1153
                  !!X. [| X <= h(X);  X<=D |] ==> X<=A
paulson@6121
  1154
               |] ==> gfp(D,h) <= A
paulson@6121
  1155
paulson@6121
  1156
\tdx{gfp_Tarski}     bnd_mono(D,h) ==> gfp(D,h) = h(gfp(D,h))
paulson@6121
  1157
paulson@6121
  1158
\tdx{coinduct}       [| bnd_mono(D,h); a: X; X <= h(X Un gfp(D,h)); X <= D 
paulson@6121
  1159
               |] ==> a : gfp(D,h)
paulson@6121
  1160
paulson@6121
  1161
\tdx{gfp_mono}       [| bnd_mono(D,h);  D <= E;
paulson@6121
  1162
                  !!X. X<=D ==> h(X) <= i(X)  
paulson@6121
  1163
               |] ==> gfp(D,h) <= gfp(E,i)
paulson@6121
  1164
\end{ttbox}
paulson@6121
  1165
\caption{Least and greatest fixedpoints} \label{zf-fixedpt}
paulson@6121
  1166
\end{figure}
paulson@6121
  1167
paulson@6121
  1168
The Knaster-Tarski Theorem states that every monotone function over a
paulson@6121
  1169
complete lattice has a fixedpoint.  Theory \thydx{Fixedpt} proves the
paulson@6121
  1170
Theorem only for a particular lattice, namely the lattice of subsets of a
paulson@6121
  1171
set (Fig.\ts\ref{zf-fixedpt}).  The theory defines least and greatest
paulson@6121
  1172
fixedpoint operators with corresponding induction and coinduction rules.
paulson@6121
  1173
These are essential to many definitions that follow, including the natural
paulson@6121
  1174
numbers and the transitive closure operator.  The (co)inductive definition
paulson@6121
  1175
package also uses the fixedpoint operators~\cite{paulson-CADE}.  See
paulson@6121
  1176
Davey and Priestley~\cite{davey&priestley} for more on the Knaster-Tarski
paulson@6121
  1177
Theorem and my paper~\cite{paulson-set-II} for discussion of the Isabelle
paulson@6121
  1178
proofs.
paulson@6121
  1179
paulson@6121
  1180
Monotonicity properties are proved for most of the set-forming operations:
paulson@6121
  1181
union, intersection, Cartesian product, image, domain, range, etc.  These
paulson@6121
  1182
are useful for applying the Knaster-Tarski Fixedpoint Theorem.  The proofs
paulson@6121
  1183
themselves are trivial applications of Isabelle's classical reasoner.  See
paulson@6121
  1184
file \texttt{ZF/mono.ML}.
paulson@6121
  1185
paulson@6121
  1186
paulson@6121
  1187
\begin{figure}
paulson@6121
  1188
\begin{constants} 
paulson@6121
  1189
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1190
  \sdx{O}       & $[i,i]\To i$  &  Right 60     & composition ($\circ$) \\
paulson@6121
  1191
  \cdx{id}      & $i\To i$      &       & identity function \\
paulson@6121
  1192
  \cdx{inj}     & $[i,i]\To i$  &       & injective function space\\
paulson@6121
  1193
  \cdx{surj}    & $[i,i]\To i$  &       & surjective function space\\
paulson@6121
  1194
  \cdx{bij}     & $[i,i]\To i$  &       & bijective function space
paulson@6121
  1195
\end{constants}
paulson@6121
  1196
paulson@6121
  1197
\begin{ttbox}
paulson@6121
  1198
\tdx{comp_def}  r O s     == {\ttlbrace}xz : domain(s)*range(r) . 
paulson@6121
  1199
                        EX x y z. xz=<x,z> & <x,y>:s & <y,z>:r{\ttrbrace}
paulson@6121
  1200
\tdx{id_def}    id(A)     == (lam x:A. x)
paulson@6121
  1201
\tdx{inj_def}   inj(A,B)  == {\ttlbrace} f: A->B. ALL w:A. ALL x:A. f`w=f`x --> w=x {\ttrbrace}
paulson@6121
  1202
\tdx{surj_def}  surj(A,B) == {\ttlbrace} f: A->B . ALL y:B. EX x:A. f`x=y {\ttrbrace}
paulson@6121
  1203
\tdx{bij_def}   bij(A,B)  == inj(A,B) Int surj(A,B)
paulson@6121
  1204
paulson@6121
  1205
paulson@6121
  1206
\tdx{left_inverse}     [| f: inj(A,B);  a: A |] ==> converse(f)`(f`a) = a
paulson@6121
  1207
\tdx{right_inverse}    [| f: inj(A,B);  b: range(f) |] ==> 
paulson@6121
  1208
                 f`(converse(f)`b) = b
paulson@6121
  1209
paulson@6121
  1210
\tdx{inj_converse_inj} f: inj(A,B) ==> converse(f): inj(range(f), A)
paulson@6121
  1211
\tdx{bij_converse_bij} f: bij(A,B) ==> converse(f): bij(B,A)
paulson@6121
  1212
paulson@6121
  1213
\tdx{comp_type}        [| s<=A*B;  r<=B*C |] ==> (r O s) <= A*C
paulson@6121
  1214
\tdx{comp_assoc}       (r O s) O t = r O (s O t)
paulson@6121
  1215
paulson@6121
  1216
\tdx{left_comp_id}     r<=A*B ==> id(B) O r = r
paulson@6121
  1217
\tdx{right_comp_id}    r<=A*B ==> r O id(A) = r
paulson@6121
  1218
paulson@6121
  1219
\tdx{comp_func}        [| g:A->B; f:B->C |] ==> (f O g):A->C
paulson@6121
  1220
\tdx{comp_func_apply}  [| g:A->B; f:B->C; a:A |] ==> (f O g)`a = f`(g`a)
paulson@6121
  1221
paulson@6121
  1222
\tdx{comp_inj}         [| g:inj(A,B);  f:inj(B,C)  |] ==> (f O g):inj(A,C)
paulson@6121
  1223
\tdx{comp_surj}        [| g:surj(A,B); f:surj(B,C) |] ==> (f O g):surj(A,C)
paulson@6121
  1224
\tdx{comp_bij}         [| g:bij(A,B); f:bij(B,C) |] ==> (f O g):bij(A,C)
paulson@6121
  1225
paulson@6121
  1226
\tdx{left_comp_inverse}     f: inj(A,B) ==> converse(f) O f = id(A)
paulson@6121
  1227
\tdx{right_comp_inverse}    f: surj(A,B) ==> f O converse(f) = id(B)
paulson@6121
  1228
paulson@6121
  1229
\tdx{bij_disjoint_Un}   
paulson@6121
  1230
    [| f: bij(A,B);  g: bij(C,D);  A Int C = 0;  B Int D = 0 |] ==> 
paulson@6121
  1231
    (f Un g) : bij(A Un C, B Un D)
paulson@6121
  1232
paulson@6121
  1233
\tdx{restrict_bij}  [| f:inj(A,B);  C<=A |] ==> restrict(f,C): bij(C, f``C)
paulson@6121
  1234
\end{ttbox}
paulson@6121
  1235
\caption{Permutations} \label{zf-perm}
paulson@6121
  1236
\end{figure}
paulson@6121
  1237
paulson@6121
  1238
The theory \thydx{Perm} is concerned with permutations (bijections) and
paulson@6121
  1239
related concepts.  These include composition of relations, the identity
paulson@6121
  1240
relation, and three specialized function spaces: injective, surjective and
paulson@6121
  1241
bijective.  Figure~\ref{zf-perm} displays many of their properties that
paulson@6121
  1242
have been proved.  These results are fundamental to a treatment of
paulson@6121
  1243
equipollence and cardinality.
paulson@6121
  1244
paulson@6121
  1245
\begin{figure}\small
paulson@6121
  1246
\index{#*@{\tt\#*} symbol}
paulson@6121
  1247
\index{*div symbol}
paulson@6121
  1248
\index{*mod symbol}
paulson@6121
  1249
\index{#+@{\tt\#+} symbol}
paulson@6121
  1250
\index{#-@{\tt\#-} symbol}
paulson@6121
  1251
\begin{constants}
paulson@6121
  1252
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1253
  \cdx{nat}     & $i$                   &       & set of natural numbers \\
paulson@6121
  1254
  \cdx{nat_case}& $[i,i\To i,i]\To i$     &     & conditional for $nat$\\
paulson@6121
  1255
  \tt \#*       & $[i,i]\To i$  &  Left 70      & multiplication \\
paulson@6121
  1256
  \tt div       & $[i,i]\To i$  &  Left 70      & division\\
paulson@6121
  1257
  \tt mod       & $[i,i]\To i$  &  Left 70      & modulus\\
paulson@6121
  1258
  \tt \#+       & $[i,i]\To i$  &  Left 65      & addition\\
paulson@6121
  1259
  \tt \#-       & $[i,i]\To i$  &  Left 65      & subtraction
paulson@6121
  1260
\end{constants}
paulson@6121
  1261
paulson@6121
  1262
\begin{ttbox}
paulson@6121
  1263
\tdx{nat_def}  nat == lfp(lam r: Pow(Inf). {\ttlbrace}0{\ttrbrace} Un {\ttlbrace}succ(x). x:r{\ttrbrace}
paulson@6121
  1264
paulson@6121
  1265
\tdx{mod_def}  m mod n == transrec(m, \%j f. if j:n then j else f`(j#-n))
paulson@6121
  1266
\tdx{div_def}  m div n == transrec(m, \%j f. if j:n then 0 else succ(f`(j#-n)))
paulson@6121
  1267
paulson@6121
  1268
\tdx{nat_case_def}  nat_case(a,b,k) == 
paulson@6121
  1269
              THE y. k=0 & y=a | (EX x. k=succ(x) & y=b(x))
paulson@6121
  1270
paulson@6121
  1271
\tdx{nat_0I}        0 : nat
paulson@6121
  1272
\tdx{nat_succI}     n : nat ==> succ(n) : nat
paulson@6121
  1273
paulson@6121
  1274
\tdx{nat_induct}        
paulson@6121
  1275
    [| n: nat;  P(0);  !!x. [| x: nat;  P(x) |] ==> P(succ(x)) 
paulson@6121
  1276
    |] ==> P(n)
paulson@6121
  1277
paulson@6121
  1278
\tdx{nat_case_0}    nat_case(a,b,0) = a
paulson@6121
  1279
\tdx{nat_case_succ} nat_case(a,b,succ(m)) = b(m)
paulson@6121
  1280
paulson@6121
  1281
\tdx{add_0}        0 #+ n = n
paulson@6121
  1282
\tdx{add_succ}     succ(m) #+ n = succ(m #+ n)
paulson@6121
  1283
paulson@6121
  1284
\tdx{mult_type}     [| m:nat;  n:nat |] ==> m #* n : nat
paulson@6121
  1285
\tdx{mult_0}        0 #* n = 0
paulson@6121
  1286
\tdx{mult_succ}     succ(m) #* n = n #+ (m #* n)
paulson@6121
  1287
\tdx{mult_commute}  [| m:nat; n:nat |] ==> m #* n = n #* m
paulson@6121
  1288
\tdx{add_mult_dist} [| m:nat; k:nat |] ==> (m #+ n) #* k = (m #* k){\thinspace}#+{\thinspace}(n #* k)
paulson@6121
  1289
\tdx{mult_assoc}
paulson@6121
  1290
    [| m:nat;  n:nat;  k:nat |] ==> (m #* n) #* k = m #* (n #* k)
paulson@6121
  1291
\tdx{mod_quo_equality}
paulson@6121
  1292
    [| 0:n;  m:nat;  n:nat |] ==> (m div n)#*n #+ m mod n = m
paulson@6121
  1293
\end{ttbox}
paulson@6121
  1294
\caption{The natural numbers} \label{zf-nat}
paulson@6121
  1295
\end{figure}
paulson@6121
  1296
paulson@6121
  1297
Theory \thydx{Nat} defines the natural numbers and mathematical
paulson@6121
  1298
induction, along with a case analysis operator.  The set of natural
paulson@6121
  1299
numbers, here called \texttt{nat}, is known in set theory as the ordinal~$\omega$.
paulson@6121
  1300
paulson@6121
  1301
Theory \thydx{Arith} develops arithmetic on the natural numbers
paulson@6121
  1302
(Fig.\ts\ref{zf-nat}).  Addition, multiplication and subtraction are defined
paulson@6121
  1303
by primitive recursion.  Division and remainder are defined by repeated
paulson@6121
  1304
subtraction, which requires well-founded recursion; the termination argument
paulson@6121
  1305
relies on the divisor's being non-zero.  Many properties are proved:
paulson@6121
  1306
commutative, associative and distributive laws, identity and cancellation
paulson@6121
  1307
laws, etc.  The most interesting result is perhaps the theorem $a \bmod b +
paulson@6121
  1308
(a/b)\times b = a$.
paulson@6121
  1309
paulson@6121
  1310
Theory \thydx{Univ} defines a `universe' $\texttt{univ}(A)$, which is used by
paulson@6121
  1311
the datatype package.  This set contains $A$ and the
paulson@6121
  1312
natural numbers.  Vitally, it is closed under finite products: ${\tt
paulson@6121
  1313
  univ}(A)\times{\tt univ}(A)\subseteq{\tt univ}(A)$.  This theory also
paulson@6121
  1314
defines the cumulative hierarchy of axiomatic set theory, which
paulson@6121
  1315
traditionally is written $V@\alpha$ for an ordinal~$\alpha$.  The
paulson@6121
  1316
`universe' is a simple generalization of~$V@\omega$.
paulson@6121
  1317
paulson@6121
  1318
Theory \thydx{QUniv} defines a `universe' ${\tt quniv}(A)$, which is used by
paulson@6121
  1319
the datatype package to construct codatatypes such as streams.  It is
paulson@6121
  1320
analogous to ${\tt univ}(A)$ (and is defined in terms of it) but is closed
paulson@6121
  1321
under the non-standard product and sum.
paulson@6121
  1322
paulson@6121
  1323
Theory \texttt{Finite} (Figure~\ref{zf-fin}) defines the finite set operator;
paulson@6121
  1324
${\tt Fin}(A)$ is the set of all finite sets over~$A$.  The theory employs
paulson@6121
  1325
Isabelle's inductive definition package, which proves various rules
paulson@6121
  1326
automatically.  The induction rule shown is stronger than the one proved by
paulson@6121
  1327
the package.  The theory also defines the set of all finite functions
paulson@6121
  1328
between two given sets.
paulson@6121
  1329
paulson@6121
  1330
\begin{figure}
paulson@6121
  1331
\begin{ttbox}
paulson@6121
  1332
\tdx{Fin.emptyI}      0 : Fin(A)
paulson@6121
  1333
\tdx{Fin.consI}       [| a: A;  b: Fin(A) |] ==> cons(a,b) : Fin(A)
paulson@6121
  1334
paulson@6121
  1335
\tdx{Fin_induct}
paulson@6121
  1336
    [| b: Fin(A);
paulson@6121
  1337
       P(0);
paulson@6121
  1338
       !!x y. [| x: A;  y: Fin(A);  x~:y;  P(y) |] ==> P(cons(x,y))
paulson@6121
  1339
    |] ==> P(b)
paulson@6121
  1340
paulson@6121
  1341
\tdx{Fin_mono}        A<=B ==> Fin(A) <= Fin(B)
paulson@6121
  1342
\tdx{Fin_UnI}         [| b: Fin(A);  c: Fin(A) |] ==> b Un c : Fin(A)
paulson@6121
  1343
\tdx{Fin_UnionI}      C : Fin(Fin(A)) ==> Union(C) : Fin(A)
paulson@6121
  1344
\tdx{Fin_subset}      [| c<=b;  b: Fin(A) |] ==> c: Fin(A)
paulson@6121
  1345
\end{ttbox}
paulson@6121
  1346
\caption{The finite set operator} \label{zf-fin}
paulson@6121
  1347
\end{figure}
paulson@6121
  1348
paulson@6121
  1349
\begin{figure}
paulson@6121
  1350
\begin{constants}
paulson@6121
  1351
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1352
  \cdx{list}    & $i\To i$      && lists over some set\\
paulson@6121
  1353
  \cdx{list_case} & $[i, [i,i]\To i, i] \To i$  && conditional for $list(A)$ \\
paulson@6121
  1354
  \cdx{map}     & $[i\To i, i] \To i$   &       & mapping functional\\
paulson@6121
  1355
  \cdx{length}  & $i\To i$              &       & length of a list\\
paulson@6121
  1356
  \cdx{rev}     & $i\To i$              &       & reverse of a list\\
paulson@6121
  1357
  \tt \at       & $[i,i]\To i$  &  Right 60     & append for lists\\
paulson@6121
  1358
  \cdx{flat}    & $i\To i$   &                  & append of list of lists
paulson@6121
  1359
\end{constants}
paulson@6121
  1360
paulson@6121
  1361
\underscoreon %%because @ is used here
paulson@6121
  1362
\begin{ttbox}
paulson@6121
  1363
\tdx{NilI}            Nil : list(A)
paulson@6121
  1364
\tdx{ConsI}           [| a: A;  l: list(A) |] ==> Cons(a,l) : list(A)
paulson@6121
  1365
paulson@6121
  1366
\tdx{List.induct}
paulson@6121
  1367
    [| l: list(A);
paulson@6121
  1368
       P(Nil);
paulson@6121
  1369
       !!x y. [| x: A;  y: list(A);  P(y) |] ==> P(Cons(x,y))
paulson@6121
  1370
    |] ==> P(l)
paulson@6121
  1371
paulson@6121
  1372
\tdx{Cons_iff}        Cons(a,l)=Cons(a',l') <-> a=a' & l=l'
paulson@6121
  1373
\tdx{Nil_Cons_iff}    ~ Nil=Cons(a,l)
paulson@6121
  1374
paulson@6121
  1375
\tdx{list_mono}       A<=B ==> list(A) <= list(B)
paulson@6121
  1376
paulson@6121
  1377
\tdx{map_ident}       l: list(A) ==> map(\%u. u, l) = l
paulson@6121
  1378
\tdx{map_compose}     l: list(A) ==> map(h, map(j,l)) = map(\%u. h(j(u)), l)
paulson@6121
  1379
\tdx{map_app_distrib} xs: list(A) ==> map(h, xs@ys) = map(h,xs) @ map(h,ys)
paulson@6121
  1380
\tdx{map_type}
paulson@6121
  1381
    [| l: list(A);  !!x. x: A ==> h(x): B |] ==> map(h,l) : list(B)
paulson@6121
  1382
\tdx{map_flat}
paulson@6121
  1383
    ls: list(list(A)) ==> map(h, flat(ls)) = flat(map(map(h),ls))
paulson@6121
  1384
\end{ttbox}
paulson@6121
  1385
\caption{Lists} \label{zf-list}
paulson@6121
  1386
\end{figure}
paulson@6121
  1387
paulson@6121
  1388
paulson@6121
  1389
Figure~\ref{zf-list} presents the set of lists over~$A$, ${\tt list}(A)$.  The
paulson@6121
  1390
definition employs Isabelle's datatype package, which defines the introduction
paulson@6121
  1391
and induction rules automatically, as well as the constructors, case operator
paulson@6121
  1392
(\verb|list_case|) and recursion operator.  The theory then defines the usual
paulson@6121
  1393
list functions by primitive recursion.  See theory \texttt{List}.
paulson@6121
  1394
paulson@6121
  1395
paulson@6173
  1396
\section{Automatic Tools}
paulson@6173
  1397
paulson@6173
  1398
{\ZF} provides the simplifier and the classical reasoner.   Moreover it
paulson@6173
  1399
supplies a specialized tool to infer `types' of terms.
paulson@6173
  1400
paulson@6173
  1401
\subsection{Simplification}
paulson@6121
  1402
paulson@6121
  1403
{\ZF} inherits simplification from {\FOL} but adopts it for set theory.  The
paulson@6121
  1404
extraction of rewrite rules takes the {\ZF} primitives into account.  It can
paulson@6121
  1405
strip bounded universal quantifiers from a formula; for example, ${\forall
paulson@6121
  1406
  x\in A. f(x)=g(x)}$ yields the conditional rewrite rule $x\in A \Imp
paulson@6121
  1407
f(x)=g(x)$.  Given $a\in\{x\in A. P(x)\}$ it extracts rewrite rules from $a\in
paulson@6121
  1408
A$ and~$P(a)$.  It can also break down $a\in A\int B$ and $a\in A-B$.
paulson@6121
  1409
paulson@6121
  1410
Simplification tactics tactics such as \texttt{Asm_simp_tac} and
paulson@6121
  1411
\texttt{Full_simp_tac} use the default simpset (\texttt{simpset()}), which
paulson@6121
  1412
works for most purposes.  A small simplification set for set theory is
paulson@6121
  1413
called~\ttindexbold{ZF_ss}, and you can even use \ttindex{FOL_ss} as a minimal
paulson@6121
  1414
starting point.  \texttt{ZF_ss} contains congruence rules for all the binding
paulson@6121
  1415
operators of {\ZF}\@.  It contains all the conversion rules, such as
paulson@6121
  1416
\texttt{fst} and \texttt{snd}, as well as the rewrites shown in
paulson@6121
  1417
Fig.\ts\ref{zf-simpdata}.  See the file \texttt{ZF/simpdata.ML} for a fuller
paulson@6121
  1418
list.
paulson@6121
  1419
paulson@6173
  1420
paulson@6173
  1421
\subsection{Classical Reasoning}
paulson@6173
  1422
paulson@6121
  1423
As for the classical reasoner, tactics such as \texttt{Blast_tac} and {\tt
paulson@6121
  1424
  Best_tac} refer to the default claset (\texttt{claset()}).  This works for
paulson@6121
  1425
most purposes.  Named clasets include \ttindexbold{ZF_cs} (basic set theory)
paulson@6121
  1426
and \ttindexbold{le_cs} (useful for reasoning about the relations $<$ and
paulson@6121
  1427
$\le$).  You can use \ttindex{FOL_cs} as a minimal basis for building your own
paulson@6121
  1428
clasets.  See \iflabelundefined{chap:classical}{the {\em Reference Manual\/}}%
paulson@6121
  1429
{Chap.\ts\ref{chap:classical}} for more discussion of classical proof methods.
paulson@6121
  1430
paulson@6121
  1431
paulson@6121
  1432
\begin{figure}
paulson@6121
  1433
\begin{eqnarray*}
paulson@6121
  1434
  a\in \emptyset        & \bimp &  \bot\\
paulson@6121
  1435
  a \in A \un B      & \bimp &  a\in A \disj a\in B\\
paulson@6121
  1436
  a \in A \int B      & \bimp &  a\in A \conj a\in B\\
paulson@6121
  1437
  a \in A-B             & \bimp &  a\in A \conj \neg (a\in B)\\
paulson@6121
  1438
  \pair{a,b}\in {\tt Sigma}(A,B)
paulson@6121
  1439
                        & \bimp &  a\in A \conj b\in B(a)\\
paulson@6121
  1440
  a \in {\tt Collect}(A,P)      & \bimp &  a\in A \conj P(a)\\
paulson@6121
  1441
  (\forall x \in \emptyset. P(x)) & \bimp &  \top\\
paulson@6121
  1442
  (\forall x \in A. \top)       & \bimp &  \top
paulson@6121
  1443
\end{eqnarray*}
paulson@6121
  1444
\caption{Some rewrite rules for set theory} \label{zf-simpdata}
paulson@6121
  1445
\end{figure}
paulson@6121
  1446
paulson@6121
  1447
paulson@6173
  1448
\subsection{Type-Checking Tactics}
paulson@6173
  1449
\index{type-checking tactics}
paulson@6173
  1450
paulson@6173
  1451
Isabelle/{\ZF} provides simple tactics to help automate those proofs that are
paulson@6173
  1452
essentially type-checking.  Such proofs are built by applying rules such as
paulson@6173
  1453
these:
paulson@6173
  1454
\begin{ttbox}
paulson@6173
  1455
[| ?P ==> ?a : ?A; ~ ?P ==> ?b : ?A |] ==> (if ?P then ?a else ?b) : ?A
paulson@6173
  1456
paulson@6173
  1457
[| ?m : nat; ?n : nat |] ==> ?m #+ ?n : nat
paulson@6173
  1458
paulson@6173
  1459
?a : ?A ==> Inl(?a) : ?A + ?B  
paulson@6173
  1460
\end{ttbox}
paulson@6173
  1461
In typical applications, the goal has the form $t\in\Var{A}$: in other words,
paulson@6173
  1462
we have a specific term~$t$ and need to infer its `type' by instantiating the
paulson@6173
  1463
set variable~$\Var{A}$.  Neither the simplifier nor the classical reasoner
paulson@6173
  1464
does this job well.  The if-then-else rule, and many similar ones, can make
paulson@6173
  1465
the classical reasoner loop.  The simplifier refuses (on principle) to
paulson@6173
  1466
instantiate variables during rewriting, so goals such as \texttt{i\#+j :\ ?A}
paulson@6173
  1467
are left unsolved.
paulson@6173
  1468
paulson@6173
  1469
The simplifier calls the type-checker to solve rewritten subgoals: this stage
paulson@6173
  1470
can indeed instantiate variables.  If you have defined new constants and
paulson@6173
  1471
proved type-checking rules for them, then insert the rules using
paulson@6173
  1472
\texttt{AddTCs} and the rest should be automatic.  In particular, the
paulson@6173
  1473
simplifier will use type-checking to help satisfy conditional rewrite rules.
paulson@6173
  1474
Call the tactic \ttindex{Typecheck_tac} to break down all subgoals using
paulson@6173
  1475
type-checking rules.
paulson@6173
  1476
paulson@6173
  1477
Though the easiest way to invoke the type-checker is via the simplifier,
paulson@6173
  1478
specialized applications may require more detailed knowledge of
paulson@6173
  1479
the type-checking primitives.  They are modelled on the simplifier's:
paulson@6173
  1480
\begin{ttdescription}
paulson@6173
  1481
\item[\ttindexbold{tcset}] is the type of tcsets: sets of type-checking rules.
paulson@6173
  1482
paulson@6173
  1483
\item[\ttindexbold{addTCs}] is an infix operator to add type-checking rules to
paulson@6173
  1484
  a tcset.
paulson@6173
  1485
  
paulson@6173
  1486
\item[\ttindexbold{delTCs}] is an infix operator to remove type-checking rules
paulson@6173
  1487
  from a tcset.
paulson@6173
  1488
paulson@6173
  1489
\item[\ttindexbold{typecheck_tac}] is a tactic for attempting to prove all
paulson@6173
  1490
  subgoals using the rules given in its argument, a tcset.
paulson@6173
  1491
\end{ttdescription}
paulson@6173
  1492
paulson@6173
  1493
Tcsets, like simpsets, are associated with theories and are merged when
paulson@6173
  1494
theories are merged.  There are further primitives that use the default tcset.
paulson@6173
  1495
\begin{ttdescription}
paulson@6173
  1496
\item[\ttindexbold{tcset}] is a function to return the default tcset; use the
paulson@6173
  1497
  expression \texttt{tcset()}.
paulson@6173
  1498
paulson@6173
  1499
\item[\ttindexbold{AddTCs}] adds type-checking rules to the default tcset.
paulson@6173
  1500
  
paulson@6173
  1501
\item[\ttindexbold{DelTCs}] removes type-checking rules from the default
paulson@6173
  1502
  tcset.
paulson@6173
  1503
paulson@6173
  1504
\item[\ttindexbold{Typecheck_tac}] calls \texttt{typecheck_tac} using the
paulson@6173
  1505
  default tcset.
paulson@6173
  1506
\end{ttdescription}
paulson@6173
  1507
paulson@6173
  1508
To supply some type-checking rules temporarily, using \texttt{Addrules} and
paulson@6173
  1509
later \texttt{Delrules} is the simplest way.  There is also a high-tech
paulson@6173
  1510
approach.  Call the simplifier with a new solver expressed using
paulson@6173
  1511
\ttindexbold{type_solver_tac} and your temporary type-checking rules.
paulson@6173
  1512
\begin{ttbox}
paulson@6173
  1513
by (asm_simp_tac 
paulson@6173
  1514
     (simpset() setSolver type_solver_tac (tcset() addTCs prems)) 2);
paulson@6173
  1515
\end{ttbox}
paulson@6173
  1516
paulson@6173
  1517
paulson@6173
  1518
paulson@6121
  1519
\section{Datatype definitions}
paulson@6121
  1520
\label{sec:ZF:datatype}
paulson@6121
  1521
\index{*datatype|(}
paulson@6121
  1522
paulson@6121
  1523
The \ttindex{datatype} definition package of \ZF\ constructs inductive
paulson@6121
  1524
datatypes similar to those of \ML.  It can also construct coinductive
paulson@6121
  1525
datatypes (codatatypes), which are non-well-founded structures such as
paulson@6121
  1526
streams.  It defines the set using a fixed-point construction and proves
paulson@6121
  1527
induction rules, as well as theorems for recursion and case combinators.  It
paulson@6121
  1528
supplies mechanisms for reasoning about freeness.  The datatype package can
paulson@6121
  1529
handle both mutual and indirect recursion.
paulson@6121
  1530
paulson@6121
  1531
paulson@6121
  1532
\subsection{Basics}
paulson@6121
  1533
\label{subsec:datatype:basics}
paulson@6121
  1534
paulson@6121
  1535
A \texttt{datatype} definition has the following form:
paulson@6121
  1536
\[
paulson@6121
  1537
\begin{array}{llcl}
paulson@6121
  1538
\mathtt{datatype} & t@1(A@1,\ldots,A@h) & = &
paulson@6121
  1539
  constructor^1@1 ~\mid~ \ldots ~\mid~ constructor^1@{k@1} \\
paulson@6121
  1540
 & & \vdots \\
paulson@6121
  1541
\mathtt{and} & t@n(A@1,\ldots,A@h) & = &
paulson@6121
  1542
  constructor^n@1~ ~\mid~ \ldots ~\mid~ constructor^n@{k@n}
paulson@6121
  1543
\end{array}
paulson@6121
  1544
\]
paulson@6121
  1545
Here $t@1$, \ldots,~$t@n$ are identifiers and $A@1$, \ldots,~$A@h$ are
paulson@6121
  1546
variables: the datatype's parameters.  Each constructor specification has the
paulson@6121
  1547
form \dquotesoff
paulson@6121
  1548
\[ C \hbox{\tt~( } \hbox{\tt"} x@1 \hbox{\tt:} T@1 \hbox{\tt"},\;
paulson@6121
  1549
                   \ldots,\;
paulson@6121
  1550
                   \hbox{\tt"} x@m \hbox{\tt:} T@m \hbox{\tt"}
paulson@6121
  1551
     \hbox{\tt~)}
paulson@6121
  1552
\]
paulson@6121
  1553
Here $C$ is the constructor name, and variables $x@1$, \ldots,~$x@m$ are the
paulson@6121
  1554
constructor arguments, belonging to the sets $T@1$, \ldots, $T@m$,
paulson@6121
  1555
respectively.  Typically each $T@j$ is either a constant set, a datatype
paulson@6121
  1556
parameter (one of $A@1$, \ldots, $A@h$) or a recursive occurrence of one of
paulson@6121
  1557
the datatypes, say $t@i(A@1,\ldots,A@h)$.  More complex possibilities exist,
paulson@6121
  1558
but they are much harder to realize.  Often, additional information must be
paulson@6121
  1559
supplied in the form of theorems.
paulson@6121
  1560
paulson@6121
  1561
A datatype can occur recursively as the argument of some function~$F$.  This
paulson@6121
  1562
is called a {\em nested} (or \emph{indirect}) occurrence.  It is only allowed
paulson@6121
  1563
if the datatype package is given a theorem asserting that $F$ is monotonic.
paulson@6121
  1564
If the datatype has indirect occurrences, then Isabelle/ZF does not support
paulson@6121
  1565
recursive function definitions.
paulson@6121
  1566
paulson@6121
  1567
A simple example of a datatype is \texttt{list}, which is built-in, and is
paulson@6121
  1568
defined by
paulson@6121
  1569
\begin{ttbox}
paulson@6121
  1570
consts     list :: i=>i
paulson@6121
  1571
datatype  "list(A)" = Nil | Cons ("a:A", "l: list(A)")
paulson@6121
  1572
\end{ttbox}
paulson@6121
  1573
Note that the datatype operator must be declared as a constant first.
paulson@6121
  1574
However, the package declares the constructors.  Here, \texttt{Nil} gets type
paulson@6121
  1575
$i$ and \texttt{Cons} gets type $[i,i]\To i$.
paulson@6121
  1576
paulson@6121
  1577
Trees and forests can be modelled by the mutually recursive datatype
paulson@6121
  1578
definition
paulson@6121
  1579
\begin{ttbox}
paulson@6121
  1580
consts     tree, forest, tree_forest :: i=>i
paulson@6121
  1581
datatype  "tree(A)"   = Tcons ("a: A",  "f: forest(A)")
paulson@6121
  1582
and       "forest(A)" = Fnil  |  Fcons ("t: tree(A)",  "f: forest(A)")
paulson@6121
  1583
\end{ttbox}
paulson@6121
  1584
Here $\texttt{tree}(A)$ is the set of trees over $A$, $\texttt{forest}(A)$ is
paulson@6121
  1585
the set of forests over $A$, and  $\texttt{tree_forest}(A)$ is the union of
paulson@6121
  1586
the previous two sets.  All three operators must be declared first.
paulson@6121
  1587
paulson@6121
  1588
The datatype \texttt{term}, which is defined by
paulson@6121
  1589
\begin{ttbox}
paulson@6121
  1590
consts     term :: i=>i
paulson@6121
  1591
datatype  "term(A)" = Apply ("a: A", "l: list(term(A))")
paulson@6121
  1592
  monos "[list_mono]"
paulson@6121
  1593
\end{ttbox}
paulson@6121
  1594
is an example of nested recursion.  (The theorem \texttt{list_mono} is proved
paulson@6121
  1595
in file \texttt{List.ML}, and the \texttt{term} example is devaloped in theory
paulson@6121
  1596
\thydx{ex/Term}.)
paulson@6121
  1597
paulson@6121
  1598
\subsubsection{Freeness of the constructors}
paulson@6121
  1599
paulson@6121
  1600
Constructors satisfy {\em freeness} properties.  Constructions are distinct,
paulson@6121
  1601
for example $\texttt{Nil}\not=\texttt{Cons}(a,l)$, and they are injective, for
paulson@6121
  1602
example $\texttt{Cons}(a,l)=\texttt{Cons}(a',l') \bimp a=a' \conj l=l'$.
paulson@6121
  1603
Because the number of freeness is quadratic in the number of constructors, the
paulson@6143
  1604
datatype package does not prove them.  Instead, it ensures that simplification
paulson@6143
  1605
will prove them dynamically: when the simplifier encounters a formula
paulson@6143
  1606
asserting the equality of two datatype constructors, it performs freeness
paulson@6143
  1607
reasoning.  
paulson@6143
  1608
paulson@6143
  1609
Freeness reasoning can also be done using the classical reasoner, but it is
paulson@6143
  1610
more complicated.  You have to add some safe elimination rules rules to the
paulson@6143
  1611
claset.  For the \texttt{list} datatype, they are called
paulson@6143
  1612
\texttt{list.free_SEs}.  Occasionally this exposes the underlying
paulson@6143
  1613
representation of some constructor, which can be rectified using the command
paulson@6143
  1614
\hbox{\tt fold_tac list.con_defs}.
paulson@6143
  1615
paulson@6121
  1616
paulson@6121
  1617
\subsubsection{Structural induction}
paulson@6121
  1618
paulson@6121
  1619
The datatype package also provides structural induction rules.  For datatypes
paulson@6121
  1620
without mutual or nested recursion, the rule has the form exemplified by
paulson@6121
  1621
\texttt{list.induct} in Fig.\ts\ref{zf-list}.  For mutually recursive
paulson@6121
  1622
datatypes, the induction rule is supplied in two forms.  Consider datatype
paulson@6121
  1623
\texttt{TF}.  The rule \texttt{tree_forest.induct} performs induction over a
paulson@6121
  1624
single predicate~\texttt{P}, which is presumed to be defined for both trees
paulson@6121
  1625
and forests:
paulson@6121
  1626
\begin{ttbox}
paulson@6121
  1627
[| x : tree_forest(A);
paulson@6121
  1628
   !!a f. [| a : A; f : forest(A); P(f) |] ==> P(Tcons(a, f)); P(Fnil);
paulson@6121
  1629
   !!f t. [| t : tree(A); P(t); f : forest(A); P(f) |]
paulson@6121
  1630
          ==> P(Fcons(t, f)) 
paulson@6121
  1631
|] ==> P(x)
paulson@6121
  1632
\end{ttbox}
paulson@6121
  1633
The rule \texttt{tree_forest.mutual_induct} performs induction over two
paulson@6121
  1634
distinct predicates, \texttt{P_tree} and \texttt{P_forest}.
paulson@6121
  1635
\begin{ttbox}
paulson@6121
  1636
[| !!a f.
paulson@6121
  1637
      [| a : A; f : forest(A); P_forest(f) |] ==> P_tree(Tcons(a, f));
paulson@6121
  1638
   P_forest(Fnil);
paulson@6121
  1639
   !!f t. [| t : tree(A); P_tree(t); f : forest(A); P_forest(f) |]
paulson@6121
  1640
          ==> P_forest(Fcons(t, f)) 
paulson@6121
  1641
|] ==> (ALL za. za : tree(A) --> P_tree(za)) &
paulson@6121
  1642
    (ALL za. za : forest(A) --> P_forest(za))
paulson@6121
  1643
\end{ttbox}
paulson@6121
  1644
paulson@6121
  1645
For datatypes with nested recursion, such as the \texttt{term} example from
paulson@6121
  1646
above, things are a bit more complicated.  The rule \texttt{term.induct}
paulson@6121
  1647
refers to the monotonic operator, \texttt{list}:
paulson@6121
  1648
\begin{ttbox}
paulson@6121
  1649
[| x : term(A);
paulson@6121
  1650
   !!a l. [| a : A; l : list(Collect(term(A), P)) |] ==> P(Apply(a, l)) 
paulson@6121
  1651
|] ==> P(x)
paulson@6121
  1652
\end{ttbox}
paulson@6121
  1653
The file \texttt{ex/Term.ML} derives two higher-level induction rules, one of
paulson@6121
  1654
which is particularly useful for proving equations:
paulson@6121
  1655
\begin{ttbox}
paulson@6121
  1656
[| t : term(A);
paulson@6121
  1657
   !!x zs. [| x : A; zs : list(term(A)); map(f, zs) = map(g, zs) |]
paulson@6121
  1658
           ==> f(Apply(x, zs)) = g(Apply(x, zs)) 
paulson@6121
  1659
|] ==> f(t) = g(t)  
paulson@6121
  1660
\end{ttbox}
paulson@6121
  1661
How this can be generalized to other nested datatypes is a matter for future
paulson@6121
  1662
research.
paulson@6121
  1663
paulson@6121
  1664
paulson@6121
  1665
\subsubsection{The \texttt{case} operator}
paulson@6121
  1666
paulson@6121
  1667
The package defines an operator for performing case analysis over the
paulson@6121
  1668
datatype.  For \texttt{list}, it is called \texttt{list_case} and satisfies
paulson@6121
  1669
the equations
paulson@6121
  1670
\begin{ttbox}
paulson@6121
  1671
list_case(f_Nil, f_Cons, []) = f_Nil
paulson@6121
  1672
list_case(f_Nil, f_Cons, Cons(a, l)) = f_Cons(a, l)
paulson@6121
  1673
\end{ttbox}
paulson@6121
  1674
Here \texttt{f_Nil} is the value to return if the argument is \texttt{Nil} and
paulson@6121
  1675
\texttt{f_Cons} is a function that computes the value to return if the
paulson@6121
  1676
argument has the form $\texttt{Cons}(a,l)$.  The function can be expressed as
paulson@6121
  1677
an abstraction, over patterns if desired (\S\ref{sec:pairs}).
paulson@6121
  1678
paulson@6121
  1679
For mutually recursive datatypes, there is a single \texttt{case} operator.
paulson@6121
  1680
In the tree/forest example, the constant \texttt{tree_forest_case} handles all
paulson@6121
  1681
of the constructors of the two datatypes.
paulson@6121
  1682
paulson@6121
  1683
paulson@6121
  1684
paulson@6121
  1685
paulson@6121
  1686
\subsection{Defining datatypes}
paulson@6121
  1687
paulson@6121
  1688
The theory syntax for datatype definitions is shown in
paulson@6121
  1689
Fig.~\ref{datatype-grammar}.  In order to be well-formed, a datatype
paulson@6121
  1690
definition has to obey the rules stated in the previous section.  As a result
paulson@6121
  1691
the theory is extended with the new types, the constructors, and the theorems
paulson@6121
  1692
listed in the previous section.  The quotation marks are necessary because
paulson@6121
  1693
they enclose general Isabelle formul\ae.
paulson@6121
  1694
paulson@6121
  1695
\begin{figure}
paulson@6121
  1696
\begin{rail}
paulson@6121
  1697
datatype : ( 'datatype' | 'codatatype' ) datadecls;
paulson@6121
  1698
paulson@6121
  1699
datadecls: ( '"' id arglist '"' '=' (constructor + '|') ) + 'and'
paulson@6121
  1700
         ;
paulson@6121
  1701
constructor : name ( () | consargs )  ( () | ( '(' mixfix ')' ) )
paulson@6121
  1702
         ;
paulson@6121
  1703
consargs : '(' ('"' var ':' term '"' + ',') ')'
paulson@6121
  1704
         ;
paulson@6121
  1705
\end{rail}
paulson@6121
  1706
\caption{Syntax of datatype declarations}
paulson@6121
  1707
\label{datatype-grammar}
paulson@6121
  1708
\end{figure}
paulson@6121
  1709
paulson@6121
  1710
Codatatypes are declared like datatypes and are identical to them in every
paulson@6121
  1711
respect except that they have a coinduction rule instead of an induction rule.
paulson@6121
  1712
Note that while an induction rule has the effect of limiting the values
paulson@6121
  1713
contained in the set, a coinduction rule gives a way of constructing new
paulson@6121
  1714
values of the set.
paulson@6121
  1715
paulson@6121
  1716
Most of the theorems about datatypes become part of the default simpset.  You
paulson@6121
  1717
never need to see them again because the simplifier applies them
paulson@6143
  1718
automatically.  Induction or exhaustion are usually invoked by hand,
paulson@6121
  1719
usually via these special-purpose tactics:
paulson@6121
  1720
\begin{ttdescription}
paulson@6121
  1721
\item[\ttindexbold{induct_tac} {\tt"}$x${\tt"} $i$] applies structural
paulson@6121
  1722
  induction on variable $x$ to subgoal $i$, provided the type of $x$ is a
paulson@6121
  1723
  datatype.  The induction variable should not occur among other assumptions
paulson@6121
  1724
  of the subgoal.
paulson@6121
  1725
\end{ttdescription}
paulson@6121
  1726
In some cases, induction is overkill and a case distinction over all
paulson@6121
  1727
constructors of the datatype suffices.
paulson@6121
  1728
\begin{ttdescription}
paulson@6121
  1729
\item[\ttindexbold{exhaust_tac} {\tt"}$x${\tt"} $i$]
paulson@6121
  1730
 performs an exhaustive case analysis for the variable~$x$.
paulson@6121
  1731
\end{ttdescription}
paulson@6121
  1732
paulson@6121
  1733
Both tactics can only be applied to a variable, whose typing must be given in
paulson@6121
  1734
some assumption, for example the assumption \texttt{x:\ list(A)}.  The tactics
paulson@6121
  1735
also work for the natural numbers (\texttt{nat}) and disjoint sums, although
paulson@6121
  1736
these sets were not defined using the datatype package.  (Disjoint sums are
paulson@6121
  1737
not recursive, so only \texttt{exhaust_tac} is available.)
paulson@6121
  1738
paulson@6121
  1739
\bigskip
paulson@6121
  1740
Here are some more details for the technically minded.  Processing the
paulson@6121
  1741
theory file produces an \ML\ structure which, in addition to the usual
paulson@6121
  1742
components, contains a structure named $t$ for each datatype $t$ defined in
paulson@6121
  1743
the file.  Each structure $t$ contains the following elements:
paulson@6121
  1744
\begin{ttbox}
paulson@6121
  1745
val intrs         : thm list  \textrm{the introduction rules}
paulson@6121
  1746
val elim          : thm       \textrm{the elimination (case analysis) rule}
paulson@6121
  1747
val induct        : thm       \textrm{the standard induction rule}
paulson@6121
  1748
val mutual_induct : thm       \textrm{the mutual induction rule, or \texttt{True}}
paulson@6121
  1749
val case_eqns     : thm list  \textrm{equations for the case operator}
paulson@6121
  1750
val recursor_eqns : thm list  \textrm{equations for the recursor}
paulson@6121
  1751
val con_defs      : thm list  \textrm{definitions of the case operator and constructors}
paulson@6121
  1752
val free_iffs     : thm list  \textrm{logical equivalences for proving freeness}
paulson@6121
  1753
val free_SEs      : thm list  \textrm{elimination rules for proving freeness}
paulson@6121
  1754
val mk_free       : string -> thm  \textrm{A function for proving freeness theorems}
paulson@6141
  1755
val mk_cases      : string -> thm  \textrm{case analysis, see below}
paulson@6121
  1756
val defs          : thm list  \textrm{definitions of operators}
paulson@6121
  1757
val bnd_mono      : thm list  \textrm{monotonicity property}
paulson@6121
  1758
val dom_subset    : thm list  \textrm{inclusion in `bounding set'}
paulson@6121
  1759
\end{ttbox}
paulson@6121
  1760
Furthermore there is the theorem $C$\texttt{_I} for every constructor~$C$; for
paulson@6121
  1761
example, the \texttt{list} datatype's introduction rules are bound to the
paulson@6121
  1762
identifiers \texttt{Nil_I} and \texttt{Cons_I}.
paulson@6121
  1763
paulson@6121
  1764
For a codatatype, the component \texttt{coinduct} is the coinduction rule,
paulson@6121
  1765
replacing the \texttt{induct} component.
paulson@6121
  1766
paulson@6121
  1767
See the theories \texttt{ex/Ntree} and \texttt{ex/Brouwer} for examples of
paulson@6121
  1768
infinitely branching datatypes.  See theory \texttt{ex/LList} for an example
paulson@6121
  1769
of a codatatype.  Some of these theories illustrate the use of additional,
paulson@6121
  1770
undocumented features of the datatype package.  Datatype definitions are
paulson@6121
  1771
reduced to inductive definitions, and the advanced features should be
paulson@6121
  1772
understood in that light.
paulson@6121
  1773
paulson@6121
  1774
paulson@6121
  1775
\subsection{Examples}
paulson@6121
  1776
paulson@6121
  1777
\subsubsection{The datatype of binary trees}
paulson@6121
  1778
paulson@6121
  1779
Let us define the set $\texttt{bt}(A)$ of binary trees over~$A$.  The theory
paulson@6121
  1780
must contain these lines:
paulson@6121
  1781
\begin{ttbox}
paulson@6121
  1782
consts   bt :: i=>i
paulson@6121
  1783
datatype "bt(A)"  =  Lf  |  Br ("a: A",  "t1: bt(A)",  "t2: bt(A)")
paulson@6121
  1784
\end{ttbox}
paulson@6121
  1785
After loading the theory, we can prove, for example, that no tree equals its
paulson@6121
  1786
left branch.  To ease the induction, we state the goal using quantifiers.
paulson@6121
  1787
\begin{ttbox}
paulson@6121
  1788
Goal "l : bt(A) ==> ALL x r. Br(x,l,r) ~= l";
paulson@6121
  1789
{\out Level 0}
paulson@6121
  1790
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1791
{\out  1. l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1792
\end{ttbox}
paulson@6121
  1793
This can be proved by the structural induction tactic:
paulson@6121
  1794
\begin{ttbox}
paulson@6121
  1795
by (induct_tac "l" 1);
paulson@6121
  1796
{\out Level 1}
paulson@6121
  1797
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1798
{\out  1. ALL x r. Br(x, Lf, r) ~= Lf}
paulson@6121
  1799
{\out  2. !!a t1 t2.}
paulson@6121
  1800
{\out        [| a : A; t1 : bt(A); ALL x r. Br(x, t1, r) ~= t1; t2 : bt(A);}
paulson@6121
  1801
{\out           ALL x r. Br(x, t2, r) ~= t2 |]}
paulson@6121
  1802
{\out        ==> ALL x r. Br(x, Br(a, t1, t2), r) ~= Br(a, t1, t2)}
paulson@6121
  1803
\end{ttbox}
paulson@6143
  1804
Both subgoals are proved using \texttt{Auto_tac}, which performs the necessary
paulson@6143
  1805
freeness reasoning. 
paulson@6121
  1806
\begin{ttbox}
paulson@6143
  1807
by Auto_tac;
paulson@6121
  1808
{\out Level 2}
paulson@6121
  1809
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1810
{\out No subgoals!}
paulson@6121
  1811
\end{ttbox}
paulson@6121
  1812
To remove the quantifiers from the induction formula, we save the theorem using
paulson@6121
  1813
\ttindex{qed_spec_mp}.
paulson@6121
  1814
\begin{ttbox}
paulson@6121
  1815
qed_spec_mp "Br_neq_left";
paulson@6121
  1816
{\out val Br_neq_left = "?l : bt(?A) ==> Br(?x, ?l, ?r) ~= ?l" : thm}
paulson@6121
  1817
\end{ttbox}
paulson@6121
  1818
paulson@6121
  1819
When there are only a few constructors, we might prefer to prove the freenness
paulson@6121
  1820
theorems for each constructor.  This is trivial, using the function given us
paulson@6121
  1821
for that purpose:
paulson@6121
  1822
\begin{ttbox}
paulson@6121
  1823
val Br_iff = bt.mk_free "Br(a,l,r)=Br(a',l',r') <-> a=a' & l=l' & r=r'";
paulson@6121
  1824
{\out val Br_iff =}
paulson@6121
  1825
{\out   "Br(?a, ?l, ?r) = Br(?a', ?l', ?r') <->}
paulson@6121
  1826
{\out                     ?a = ?a' & ?l = ?l' & ?r = ?r'" : thm}
paulson@6121
  1827
\end{ttbox}
paulson@6121
  1828
paulson@6141
  1829
The purpose of \ttindex{mk_cases} is to generate instances of the elimination
paulson@6141
  1830
(case analysis) rule that have been simplified using freeness reasoning.  For
paulson@6141
  1831
example, this instance of the elimination rule propagates type-checking
paulson@6141
  1832
information from the premise $\texttt{Br}(a,l,r)\in\texttt{bt}(A)$:
paulson@6121
  1833
\begin{ttbox}
paulson@6141
  1834
val BrE = bt.mk_cases "Br(a,l,r) : bt(A)";
paulson@6121
  1835
{\out val BrE =}
paulson@6121
  1836
{\out   "[| Br(?a, ?l, ?r) : bt(?A);}
paulson@6121
  1837
{\out       [| ?a : ?A; ?l : bt(?A); ?r : bt(?A) |] ==> ?Q |] ==> ?Q" : thm}
paulson@6121
  1838
\end{ttbox}
paulson@6121
  1839
paulson@6121
  1840
paulson@6121
  1841
\subsubsection{Mixfix syntax in datatypes}
paulson@6121
  1842
paulson@6121
  1843
Mixfix syntax is sometimes convenient.  The theory \texttt{ex/PropLog} makes a
paulson@6121
  1844
deep embedding of propositional logic:
paulson@6121
  1845
\begin{ttbox}
paulson@6121
  1846
consts     prop :: i
paulson@6121
  1847
datatype  "prop" = Fls
paulson@6121
  1848
                 | Var ("n: nat")                ("#_" [100] 100)
paulson@6121
  1849
                 | "=>" ("p: prop", "q: prop")   (infixr 90)
paulson@6121
  1850
\end{ttbox}
paulson@6121
  1851
The second constructor has a special $\#n$ syntax, while the third constructor
paulson@6121
  1852
is an infixed arrow.
paulson@6121
  1853
paulson@6121
  1854
paulson@6121
  1855
\subsubsection{A giant enumeration type}
paulson@6121
  1856
paulson@6121
  1857
This example shows a datatype that consists of 60 constructors:
paulson@6121
  1858
\begin{ttbox}
paulson@6121
  1859
consts  enum :: i
paulson@6121
  1860
datatype
paulson@6121
  1861
  "enum" = C00 | C01 | C02 | C03 | C04 | C05 | C06 | C07 | C08 | C09
paulson@6121
  1862
         | C10 | C11 | C12 | C13 | C14 | C15 | C16 | C17 | C18 | C19
paulson@6121
  1863
         | C20 | C21 | C22 | C23 | C24 | C25 | C26 | C27 | C28 | C29
paulson@6121
  1864
         | C30 | C31 | C32 | C33 | C34 | C35 | C36 | C37 | C38 | C39
paulson@6121
  1865
         | C40 | C41 | C42 | C43 | C44 | C45 | C46 | C47 | C48 | C49
paulson@6121
  1866
         | C50 | C51 | C52 | C53 | C54 | C55 | C56 | C57 | C58 | C59
paulson@6121
  1867
end
paulson@6121
  1868
\end{ttbox}
paulson@6121
  1869
The datatype package scales well.  Even though all properties are proved
paulson@6121
  1870
rather than assumed, full processing of this definition takes under 15 seconds
paulson@6121
  1871
(on a 300 MHz Pentium).  The constructors have a balanced representation,
paulson@6121
  1872
essentially binary notation, so freeness properties can be proved fast.
paulson@6121
  1873
\begin{ttbox}
paulson@6121
  1874
Goal "C00 ~= C01";
paulson@6143
  1875
by (Simp_tac 1);
paulson@6121
  1876
\end{ttbox}
paulson@6121
  1877
You need not derive such inequalities explicitly.  The simplifier will dispose
paulson@6143
  1878
of them automatically.
paulson@6121
  1879
paulson@6121
  1880
\index{*datatype|)}
paulson@6121
  1881
paulson@6121
  1882
paulson@6121
  1883
\subsection{Recursive function definitions}\label{sec:ZF:recursive}
paulson@6121
  1884
\index{recursive functions|see{recursion}}
paulson@6121
  1885
\index{*primrec|(}
paulson@6173
  1886
\index{recursion!primitive|(}
paulson@6121
  1887
paulson@6121
  1888
Datatypes come with a uniform way of defining functions, {\bf primitive
paulson@6121
  1889
  recursion}.  Such definitions rely on the recursion operator defined by the
paulson@6121
  1890
datatype package.  Isabelle proves the desired recursion equations as
paulson@6121
  1891
theorems.
paulson@6121
  1892
paulson@6121
  1893
In principle, one could introduce primitive recursive functions by asserting
paulson@6121
  1894
their reduction rules as new axioms.  Here is a dangerous way of defining the
paulson@6121
  1895
append function for lists:
paulson@6121
  1896
\begin{ttbox}\slshape
paulson@6121
  1897
consts  "\at" :: [i,i]=>i                        (infixr 60)
paulson@6121
  1898
rules 
paulson@6121
  1899
   app_Nil   "[] \at ys = ys"
paulson@6121
  1900
   app_Cons  "(Cons(a,l)) \at ys = Cons(a, l \at ys)"
paulson@6121
  1901
\end{ttbox}
paulson@6121
  1902
Asserting axioms brings the danger of accidentally asserting nonsense.  It
paulson@6121
  1903
should be avoided at all costs!
paulson@6121
  1904
paulson@6121
  1905
The \ttindex{primrec} declaration is a safe means of defining primitive
paulson@6121
  1906
recursive functions on datatypes:
paulson@6121
  1907
\begin{ttbox}
paulson@6121
  1908
consts  "\at" :: [i,i]=>i                        (infixr 60)
paulson@6121
  1909
primrec 
paulson@6121
  1910
   "[] \at ys = ys"
paulson@6121
  1911
   "(Cons(a,l)) \at ys = Cons(a, l \at ys)"
paulson@6121
  1912
\end{ttbox}
paulson@6121
  1913
Isabelle will now check that the two rules do indeed form a primitive
paulson@6121
  1914
recursive definition.  For example, the declaration
paulson@6121
  1915
\begin{ttbox}
paulson@6121
  1916
primrec
paulson@6121
  1917
   "[] \at ys = us"
paulson@6121
  1918
\end{ttbox}
paulson@6121
  1919
is rejected with an error message ``\texttt{Extra variables on rhs}''.
paulson@6121
  1920
paulson@6121
  1921
paulson@6121
  1922
\subsubsection{Syntax of recursive definitions}
paulson@6121
  1923
paulson@6121
  1924
The general form of a primitive recursive definition is
paulson@6121
  1925
\begin{ttbox}
paulson@6121
  1926
primrec
paulson@6121
  1927
    {\it reduction rules}
paulson@6121
  1928
\end{ttbox}
paulson@6121
  1929
where \textit{reduction rules} specify one or more equations of the form
paulson@6121
  1930
\[ f \, x@1 \, \dots \, x@m \, (C \, y@1 \, \dots \, y@k) \, z@1 \,
paulson@6121
  1931
\dots \, z@n = r \] such that $C$ is a constructor of the datatype, $r$
paulson@6121
  1932
contains only the free variables on the left-hand side, and all recursive
paulson@6121
  1933
calls in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$.  
paulson@6121
  1934
There must be at most one reduction rule for each constructor.  The order is
paulson@6121
  1935
immaterial.  For missing constructors, the function is defined to return zero.
paulson@6121
  1936
paulson@6121
  1937
All reduction rules are added to the default simpset.
paulson@6121
  1938
If you would like to refer to some rule by name, then you must prefix
paulson@6121
  1939
the rule with an identifier.  These identifiers, like those in the
paulson@6121
  1940
\texttt{rules} section of a theory, will be visible at the \ML\ level.
paulson@6121
  1941
paulson@6121
  1942
The reduction rules for {\tt\at} become part of the default simpset, which
paulson@6121
  1943
leads to short proof scripts:
paulson@6121
  1944
\begin{ttbox}\underscoreon
paulson@6121
  1945
Goal "xs: list(A) ==> (xs @ ys) @ zs = xs @ (ys @ zs)";
paulson@6121
  1946
by (induct\_tac "xs" 1);
paulson@6121
  1947
by (ALLGOALS Asm\_simp\_tac);
paulson@6121
  1948
\end{ttbox}
paulson@6121
  1949
paulson@6121
  1950
You can even use the \texttt{primrec} form with non-recursive datatypes and
paulson@6121
  1951
with codatatypes.  Recursion is not allowed, but it provides a convenient
paulson@6121
  1952
syntax for defining functions by cases.
paulson@6121
  1953
paulson@6121
  1954
paulson@6121
  1955
\subsubsection{Example: varying arguments}
paulson@6121
  1956
paulson@6121
  1957
All arguments, other than the recursive one, must be the same in each equation
paulson@6121
  1958
and in each recursive call.  To get around this restriction, use explict
paulson@6121
  1959
$\lambda$-abstraction and function application.  Here is an example, drawn
paulson@6121
  1960
from the theory \texttt{Resid/Substitution}.  The type of redexes is declared
paulson@6121
  1961
as follows:
paulson@6121
  1962
\begin{ttbox}
paulson@6121
  1963
consts  redexes :: i
paulson@6121
  1964
datatype
paulson@6121
  1965
  "redexes" = Var ("n: nat")            
paulson@6121
  1966
            | Fun ("t: redexes")
paulson@6121
  1967
            | App ("b:bool" ,"f:redexes" , "a:redexes")
paulson@6121
  1968
\end{ttbox}
paulson@6121
  1969
paulson@6121
  1970
The function \texttt{lift} takes a second argument, $k$, which varies in
paulson@6121
  1971
recursive calls.
paulson@6121
  1972
\begin{ttbox}
paulson@6121
  1973
primrec
paulson@6121
  1974
  "lift(Var(i)) = (lam k:nat. if i<k then Var(i) else Var(succ(i)))"
paulson@6121
  1975
  "lift(Fun(t)) = (lam k:nat. Fun(lift(t) ` succ(k)))"
paulson@6121
  1976
  "lift(App(b,f,a)) = (lam k:nat. App(b, lift(f)`k, lift(a)`k))"
paulson@6121
  1977
\end{ttbox}
paulson@6121
  1978
Now \texttt{lift(r)`k} satisfies the required recursion equations.
paulson@6121
  1979
paulson@6121
  1980
\index{recursion!primitive|)}
paulson@6121
  1981
\index{*primrec|)}
paulson@6121
  1982
paulson@6121
  1983
paulson@6121
  1984
\section{Inductive and coinductive definitions}
paulson@6121
  1985
\index{*inductive|(}
paulson@6121
  1986
\index{*coinductive|(}
paulson@6121
  1987
paulson@6121
  1988
An {\bf inductive definition} specifies the least set~$R$ closed under given
paulson@6121
  1989
rules.  (Applying a rule to elements of~$R$ yields a result within~$R$.)  For
paulson@6121
  1990
example, a structural operational semantics is an inductive definition of an
paulson@6121
  1991
evaluation relation.  Dually, a {\bf coinductive definition} specifies the
paulson@6121
  1992
greatest set~$R$ consistent with given rules.  (Every element of~$R$ can be
paulson@6121
  1993
seen as arising by applying a rule to elements of~$R$.)  An important example
paulson@6121
  1994
is using bisimulation relations to formalise equivalence of processes and
paulson@6121
  1995
infinite data structures.
paulson@6121
  1996
paulson@6121
  1997
A theory file may contain any number of inductive and coinductive
paulson@6121
  1998
definitions.  They may be intermixed with other declarations; in
paulson@6121
  1999
particular, the (co)inductive sets {\bf must} be declared separately as
paulson@6121
  2000
constants, and may have mixfix syntax or be subject to syntax translations.
paulson@6121
  2001
paulson@6121
  2002
Each (co)inductive definition adds definitions to the theory and also
paulson@6121
  2003
proves some theorems.  Each definition creates an \ML\ structure, which is a
paulson@6121
  2004
substructure of the main theory structure.
paulson@6121
  2005
This package is described in detail in a separate paper,%
paulson@6121
  2006
\footnote{It appeared in CADE~\cite{paulson-CADE}; a longer version is
paulson@6121
  2007
  distributed with Isabelle as \emph{A Fixedpoint Approach to 
paulson@6121
  2008
 (Co)Inductive and (Co)Datatype Definitions}.}  %
paulson@6121
  2009
which you might refer to for background information.
paulson@6121
  2010
paulson@6121
  2011
paulson@6121
  2012
\subsection{The syntax of a (co)inductive definition}
paulson@6121
  2013
An inductive definition has the form
paulson@6121
  2014
\begin{ttbox}
paulson@6121
  2015
inductive
paulson@6121
  2016
  domains    {\it domain declarations}
paulson@6121
  2017
  intrs      {\it introduction rules}
paulson@6121
  2018
  monos      {\it monotonicity theorems}
paulson@6121
  2019
  con_defs   {\it constructor definitions}
paulson@6121
  2020
  type_intrs {\it introduction rules for type-checking}
paulson@6121
  2021
  type_elims {\it elimination rules for type-checking}
paulson@6121
  2022
\end{ttbox}
paulson@6121
  2023
A coinductive definition is identical, but starts with the keyword
paulson@6121
  2024
{\tt coinductive}.  
paulson@6121
  2025
paulson@6121
  2026
The {\tt monos}, {\tt con\_defs}, {\tt type\_intrs} and {\tt type\_elims}
paulson@6121
  2027
sections are optional.  If present, each is specified either as a list of
paulson@6121
  2028
identifiers or as a string.  If the latter, then the string must be a valid
paulson@6121
  2029
\textsc{ml} expression of type {\tt thm list}.  The string is simply inserted
paulson@6121
  2030
into the {\tt _thy.ML} file; if it is ill-formed, it will trigger \textsc{ml}
paulson@6121
  2031
error messages.  You can then inspect the file on the temporary directory.
paulson@6121
  2032
paulson@6121
  2033
\begin{description}
paulson@6121
  2034
\item[\it domain declarations] consist of one or more items of the form
paulson@6121
  2035
  {\it string\/}~{\tt <=}~{\it string}, associating each recursive set with
paulson@6121
  2036
  its domain.  (The domain is some existing set that is large enough to
paulson@6121
  2037
  hold the new set being defined.)
paulson@6121
  2038
paulson@6121
  2039
\item[\it introduction rules] specify one or more introduction rules in
paulson@6121
  2040
  the form {\it ident\/}~{\it string}, where the identifier gives the name of
paulson@6121
  2041
  the rule in the result structure.
paulson@6121
  2042
paulson@6121
  2043
\item[\it monotonicity theorems] are required for each operator applied to
paulson@6121
  2044
  a recursive set in the introduction rules.  There \textbf{must} be a theorem
paulson@6121
  2045
  of the form $A\subseteq B\Imp M(A)\subseteq M(B)$, for each premise $t\in M(R_i)$
paulson@6121
  2046
  in an introduction rule!
paulson@6121
  2047
paulson@6121
  2048
\item[\it constructor definitions] contain definitions of constants
paulson@6121
  2049
  appearing in the introduction rules.  The (co)datatype package supplies
paulson@6121
  2050
  the constructors' definitions here.  Most (co)inductive definitions omit
paulson@6121
  2051
  this section; one exception is the primitive recursive functions example;
paulson@6121
  2052
  see theory \texttt{ex/Primrec}.
paulson@6121
  2053
  
paulson@6121
  2054
\item[\it type\_intrs] consists of introduction rules for type-checking the
paulson@6121
  2055
  definition: for demonstrating that the new set is included in its domain.
paulson@6121
  2056
  (The proof uses depth-first search.)
paulson@6121
  2057
paulson@6121
  2058
\item[\it type\_elims] consists of elimination rules for type-checking the
paulson@6121
  2059
  definition.  They are presumed to be safe and are applied as often as
paulson@6121
  2060
  possible prior to the {\tt type\_intrs} search.
paulson@6121
  2061
\end{description}
paulson@6121
  2062
paulson@6121
  2063
The package has a few restrictions:
paulson@6121
  2064
\begin{itemize}
paulson@6121
  2065
\item The theory must separately declare the recursive sets as
paulson@6121
  2066
  constants.
paulson@6121
  2067
paulson@6121
  2068
\item The names of the recursive sets must be identifiers, not infix
paulson@6121
  2069
operators.  
paulson@6121
  2070
paulson@6121
  2071
\item Side-conditions must not be conjunctions.  However, an introduction rule
paulson@6121
  2072
may contain any number of side-conditions.
paulson@6121
  2073
paulson@6121
  2074
\item Side-conditions of the form $x=t$, where the variable~$x$ does not
paulson@6121
  2075
  occur in~$t$, will be substituted through the rule \verb|mutual_induct|.
paulson@6121
  2076
\end{itemize}
paulson@6121
  2077
paulson@6121
  2078
paulson@6121
  2079
\subsection{Example of an inductive definition}
paulson@6121
  2080
paulson@6121
  2081
Two declarations, included in a theory file, define the finite powerset
paulson@6121
  2082
operator.  First we declare the constant~\texttt{Fin}.  Then we declare it
paulson@6121
  2083
inductively, with two introduction rules:
paulson@6121
  2084
\begin{ttbox}
paulson@6121
  2085
consts  Fin :: i=>i
paulson@6121
  2086
paulson@6121
  2087
inductive
paulson@6121
  2088
  domains   "Fin(A)" <= "Pow(A)"
paulson@6121
  2089
  intrs
paulson@6121
  2090
    emptyI  "0 : Fin(A)"
paulson@6121
  2091
    consI   "[| a: A;  b: Fin(A) |] ==> cons(a,b) : Fin(A)"
paulson@6121
  2092
  type_intrs empty_subsetI, cons_subsetI, PowI
paulson@6121
  2093
  type_elims "[make_elim PowD]"
paulson@6121
  2094
\end{ttbox}
paulson@6121
  2095
The resulting theory structure contains a substructure, called~\texttt{Fin}.
paulson@6121
  2096
It contains the \texttt{Fin}$~A$ introduction rules as the list
paulson@6121
  2097
\texttt{Fin.intrs}, and also individually as \texttt{Fin.emptyI} and
paulson@6121
  2098
\texttt{Fin.consI}.  The induction rule is \texttt{Fin.induct}.
paulson@6121
  2099
paulson@6121
  2100
The chief problem with making (co)inductive definitions involves type-checking
paulson@6121
  2101
the rules.  Sometimes, additional theorems need to be supplied under
paulson@6121
  2102
\texttt{type_intrs} or \texttt{type_elims}.  If the package fails when trying
paulson@6121
  2103
to prove your introduction rules, then set the flag \ttindexbold{trace_induct}
paulson@6121
  2104
to \texttt{true} and try again.  (See the manual \emph{A Fixedpoint Approach
paulson@6121
  2105
  \ldots} for more discussion of type-checking.)
paulson@6121
  2106
paulson@6121
  2107
In the example above, $\texttt{Pow}(A)$ is given as the domain of
paulson@6121
  2108
$\texttt{Fin}(A)$, for obviously every finite subset of~$A$ is a subset
paulson@6121
  2109
of~$A$.  However, the inductive definition package can only prove that given a
paulson@6121
  2110
few hints.
paulson@6121
  2111
Here is the output that results (with the flag set) when the
paulson@6121
  2112
\texttt{type_intrs} and \texttt{type_elims} are omitted from the inductive
paulson@6121
  2113
definition above:
paulson@6121
  2114
\begin{ttbox}
paulson@6121
  2115
Inductive definition Finite.Fin
paulson@6121
  2116
Fin(A) ==
paulson@6121
  2117
lfp(Pow(A),
paulson@6121
  2118
    \%X. {z: Pow(A) . z = 0 | (EX a b. z = cons(a, b) & a : A & b : X)})
paulson@6121
  2119
  Proving monotonicity...
paulson@6121
  2120
\ttbreak
paulson@6121
  2121
  Proving the introduction rules...
paulson@6173
  2122
The type-checking subgoal:
paulson@6121
  2123
0 : Fin(A)
paulson@6121
  2124
 1. 0 : Pow(A)
paulson@6121
  2125
\ttbreak
paulson@6121
  2126
The subgoal after monos, type_elims:
paulson@6121
  2127
0 : Fin(A)
paulson@6121
  2128
 1. 0 : Pow(A)
paulson@6121
  2129
*** prove_goal: tactic failed
paulson@6121
  2130
\end{ttbox}
paulson@6121
  2131
We see the need to supply theorems to let the package prove
paulson@6121
  2132
$\emptyset\in\texttt{Pow}(A)$.  Restoring the \texttt{type_intrs} but not the
paulson@6121
  2133
\texttt{type_elims}, we again get an error message:
paulson@6121
  2134
\begin{ttbox}
paulson@6173
  2135
The type-checking subgoal:
paulson@6121
  2136
0 : Fin(A)
paulson@6121
  2137
 1. 0 : Pow(A)
paulson@6121
  2138
\ttbreak
paulson@6121
  2139
The subgoal after monos, type_elims:
paulson@6121
  2140
0 : Fin(A)
paulson@6121
  2141
 1. 0 : Pow(A)
paulson@6121
  2142
\ttbreak
paulson@6173
  2143
The type-checking subgoal:
paulson@6121
  2144
cons(a, b) : Fin(A)
paulson@6121
  2145
 1. [| a : A; b : Fin(A) |] ==> cons(a, b) : Pow(A)
paulson@6121
  2146
\ttbreak
paulson@6121
  2147
The subgoal after monos, type_elims:
paulson@6121
  2148
cons(a, b) : Fin(A)
paulson@6121
  2149
 1. [| a : A; b : Pow(A) |] ==> cons(a, b) : Pow(A)
paulson@6121
  2150
*** prove_goal: tactic failed
paulson@6121
  2151
\end{ttbox}
paulson@6121
  2152
The first rule has been type-checked, but the second one has failed.  The
paulson@6121
  2153
simplest solution to such problems is to prove the failed subgoal separately
paulson@6121
  2154
and to supply it under \texttt{type_intrs}.  The solution actually used is
paulson@6121
  2155
to supply, under \texttt{type_elims}, a rule that changes
paulson@6121
  2156
$b\in\texttt{Pow}(A)$ to $b\subseteq A$; together with \texttt{cons_subsetI}
paulson@6121
  2157
and \texttt{PowI}, it is enough to complete the type-checking.
paulson@6121
  2158
paulson@6121
  2159
paulson@6121
  2160
paulson@6121
  2161
\subsection{Further examples}
paulson@6121
  2162
paulson@6121
  2163
An inductive definition may involve arbitrary monotonic operators.  Here is a
paulson@6121
  2164
standard example: the accessible part of a relation.  Note the use
paulson@6121
  2165
of~\texttt{Pow} in the introduction rule and the corresponding mention of the
paulson@6121
  2166
rule \verb|Pow_mono| in the \texttt{monos} list.  If the desired rule has a
paulson@6121
  2167
universally quantified premise, usually the effect can be obtained using
paulson@6121
  2168
\texttt{Pow}.
paulson@6121
  2169
\begin{ttbox}
paulson@6121
  2170
consts  acc :: i=>i
paulson@6121
  2171
inductive
paulson@6121
  2172
  domains "acc(r)" <= "field(r)"
paulson@6121
  2173
  intrs
paulson@6121
  2174
    vimage  "[| r-``{a}: Pow(acc(r)); a: field(r) |] ==> a: acc(r)"
paulson@6121
  2175
  monos      Pow_mono
paulson@6121
  2176
\end{ttbox}
paulson@6121
  2177
paulson@6121
  2178
Finally, here is a coinductive definition.  It captures (as a bisimulation)
paulson@6121
  2179
the notion of equality on lazy lists, which are first defined as a codatatype:
paulson@6121
  2180
\begin{ttbox}
paulson@6121
  2181
consts  llist :: i=>i
paulson@6121
  2182
codatatype  "llist(A)" = LNil | LCons ("a: A", "l: llist(A)")
paulson@6121
  2183
\ttbreak
paulson@6121
  2184
paulson@6121
  2185
consts  lleq :: i=>i
paulson@6121
  2186
coinductive
paulson@6121
  2187
  domains "lleq(A)" <= "llist(A) * llist(A)"
paulson@6121
  2188
  intrs
paulson@6121
  2189
    LNil  "<LNil, LNil> : lleq(A)"
paulson@6121
  2190
    LCons "[| a:A; <l,l'>: lleq(A) |] 
paulson@6121
  2191
           ==> <LCons(a,l), LCons(a,l')>: lleq(A)"
paulson@6121
  2192
  type_intrs  "llist.intrs"
paulson@6121
  2193
\end{ttbox}
paulson@6121
  2194
This use of \texttt{type_intrs} is typical: the relation concerns the
paulson@6121
  2195
codatatype \texttt{llist}, so naturally the introduction rules for that
paulson@6121
  2196
codatatype will be required for type-checking the rules.
paulson@6121
  2197
paulson@6121
  2198
The Isabelle distribution contains many other inductive definitions.  Simple
paulson@6121
  2199
examples are collected on subdirectory \texttt{ZF/ex}.  The directory
paulson@6121
  2200
\texttt{Coind} and the theory \texttt{ZF/ex/LList} contain coinductive
paulson@6121
  2201
definitions.  Larger examples may be found on other subdirectories of
paulson@6121
  2202
\texttt{ZF}, such as \texttt{IMP}, and \texttt{Resid}.
paulson@6121
  2203
paulson@6121
  2204
paulson@6121
  2205
\subsection{The result structure}
paulson@6121
  2206
paulson@6121
  2207
Each (co)inductive set defined in a theory file generates an \ML\ substructure
paulson@6121
  2208
having the same name.  The the substructure contains the following elements:
paulson@6121
  2209
paulson@6121
  2210
\begin{ttbox}
paulson@6121
  2211
val intrs         : thm list  \textrm{the introduction rules}
paulson@6121
  2212
val elim          : thm       \textrm{the elimination (case analysis) rule}
paulson@6141
  2213
val mk_cases      : string -> thm  \textrm{case analysis, see below}
paulson@6121
  2214
val induct        : thm       \textrm{the standard induction rule}
paulson@6121
  2215
val mutual_induct : thm       \textrm{the mutual induction rule, or \texttt{True}}
paulson@6121
  2216
val defs          : thm list  \textrm{definitions of operators}
paulson@6121
  2217
val bnd_mono      : thm list  \textrm{monotonicity property}
paulson@6121
  2218
val dom_subset    : thm list  \textrm{inclusion in `bounding set'}
paulson@6121
  2219
\end{ttbox}
paulson@6121
  2220
Furthermore there is the theorem $C$\texttt{_I} for every constructor~$C$; for
paulson@6121
  2221
example, the \texttt{list} datatype's introduction rules are bound to the
paulson@6121
  2222
identifiers \texttt{Nil_I} and \texttt{Cons_I}.
paulson@6121
  2223
paulson@6121
  2224
For a codatatype, the component \texttt{coinduct} is the coinduction rule,
paulson@6121
  2225
replacing the \texttt{induct} component.
paulson@6121
  2226
paulson@6121
  2227
Recall that \ttindex{mk_cases} generates simplified instances of the
paulson@6121
  2228
elimination (case analysis) rule.  It is as useful for inductive definitions
paulson@6121
  2229
as it is for datatypes.  There are many examples in the theory
paulson@6121
  2230
\texttt{ex/Comb}, which is discussed at length
paulson@6121
  2231
elsewhere~\cite{paulson-generic}.  The theory first defines the datatype
paulson@6121
  2232
\texttt{comb} of combinators:
paulson@6121
  2233
\begin{ttbox}
paulson@6121
  2234
consts comb :: i
paulson@6121
  2235
datatype  "comb" = K
paulson@6121
  2236
                 | S
paulson@6121
  2237
                 | "#" ("p: comb", "q: comb")   (infixl 90)
paulson@6121
  2238
\end{ttbox}
paulson@6121
  2239
The theory goes on to define contraction and parallel contraction
paulson@6121
  2240
inductively.  Then the file \texttt{ex/Comb.ML} defines special cases of
paulson@6121
  2241
contraction using \texttt{mk_cases}:
paulson@6121
  2242
\begin{ttbox}
paulson@6141
  2243
val K_contractE = contract.mk_cases "K -1-> r";
paulson@6121
  2244
{\out val K_contractE = "K -1-> ?r ==> ?Q" : thm}
paulson@6121
  2245
\end{ttbox}
paulson@6121
  2246
We can read this as saying that the combinator \texttt{K} cannot reduce to
paulson@6121
  2247
anything.  Similar elimination rules for \texttt{S} and application are also
paulson@6121
  2248
generated and are supplied to the classical reasoner.  Note that
paulson@6121
  2249
\texttt{comb.con_defs} is given to \texttt{mk_cases} to allow freeness
paulson@6121
  2250
reasoning on datatype \texttt{comb}.
paulson@6121
  2251
paulson@6121
  2252
\index{*coinductive|)} \index{*inductive|)}
paulson@6121
  2253
paulson@6121
  2254
paulson@6121
  2255
paulson@6121
  2256
paulson@6121
  2257
\section{The outer reaches of set theory}
paulson@6121
  2258
paulson@6121
  2259
The constructions of the natural numbers and lists use a suite of
paulson@6121
  2260
operators for handling recursive function definitions.  I have described
paulson@6121
  2261
the developments in detail elsewhere~\cite{paulson-set-II}.  Here is a brief
paulson@6121
  2262
summary:
paulson@6121
  2263
\begin{itemize}
paulson@6121
  2264
  \item Theory \texttt{Trancl} defines the transitive closure of a relation
paulson@6121
  2265
    (as a least fixedpoint).
paulson@6121
  2266
paulson@6121
  2267
  \item Theory \texttt{WF} proves the Well-Founded Recursion Theorem, using an
paulson@6121
  2268
    elegant approach of Tobias Nipkow.  This theorem permits general
paulson@6121
  2269
    recursive definitions within set theory.
paulson@6121
  2270
paulson@6121
  2271
  \item Theory \texttt{Ord} defines the notions of transitive set and ordinal
paulson@6121
  2272
    number.  It derives transfinite induction.  A key definition is {\bf
paulson@6121
  2273
      less than}: $i<j$ if and only if $i$ and $j$ are both ordinals and
paulson@6121
  2274
    $i\in j$.  As a special case, it includes less than on the natural
paulson@6121
  2275
    numbers.
paulson@6121
  2276
    
paulson@6121
  2277
  \item Theory \texttt{Epsilon} derives $\varepsilon$-induction and
paulson@6121
  2278
    $\varepsilon$-recursion, which are generalisations of transfinite
paulson@6121
  2279
    induction and recursion.  It also defines \cdx{rank}$(x)$, which
paulson@6121
  2280
    is the least ordinal $\alpha$ such that $x$ is constructed at
paulson@6121
  2281
    stage $\alpha$ of the cumulative hierarchy (thus $x\in
paulson@6121
  2282
    V@{\alpha+1}$).
paulson@6121
  2283
\end{itemize}
paulson@6121
  2284
paulson@6121
  2285
Other important theories lead to a theory of cardinal numbers.  They have
paulson@6121
  2286
not yet been written up anywhere.  Here is a summary:
paulson@6121
  2287
\begin{itemize}
paulson@6121
  2288
\item Theory \texttt{Rel} defines the basic properties of relations, such as
paulson@6121
  2289
  (ir)reflexivity, (a)symmetry, and transitivity.
paulson@6121
  2290
paulson@6121
  2291
\item Theory \texttt{EquivClass} develops a theory of equivalence
paulson@6121
  2292
  classes, not using the Axiom of Choice.
paulson@6121
  2293
paulson@6121
  2294
\item Theory \texttt{Order} defines partial orderings, total orderings and
paulson@6121
  2295
  wellorderings.
paulson@6121
  2296
paulson@6121
  2297
\item Theory \texttt{OrderArith} defines orderings on sum and product sets.
paulson@6121
  2298
  These can be used to define ordinal arithmetic and have applications to
paulson@6121
  2299
  cardinal arithmetic.
paulson@6121
  2300
paulson@6121
  2301
\item Theory \texttt{OrderType} defines order types.  Every wellordering is
paulson@6121
  2302
  equivalent to a unique ordinal, which is its order type.
paulson@6121
  2303
paulson@6121
  2304
\item Theory \texttt{Cardinal} defines equipollence and cardinal numbers.
paulson@6121
  2305
 
paulson@6121
  2306
\item Theory \texttt{CardinalArith} defines cardinal addition and
paulson@6121
  2307
  multiplication, and proves their elementary laws.  It proves that there
paulson@6121
  2308
  is no greatest cardinal.  It also proves a deep result, namely
paulson@6121
  2309
  $\kappa\otimes\kappa=\kappa$ for every infinite cardinal~$\kappa$; see
paulson@6121
  2310
  Kunen~\cite[page 29]{kunen80}.  None of these results assume the Axiom of
paulson@6121
  2311
  Choice, which complicates their proofs considerably.  
paulson@6121
  2312
\end{itemize}
paulson@6121
  2313
paulson@6121
  2314
The following developments involve the Axiom of Choice (AC):
paulson@6121
  2315
\begin{itemize}
paulson@6121
  2316
\item Theory \texttt{AC} asserts the Axiom of Choice and proves some simple
paulson@6121
  2317
  equivalent forms.
paulson@6121
  2318
paulson@6121
  2319
\item Theory \texttt{Zorn} proves Hausdorff's Maximal Principle, Zorn's Lemma
paulson@6121
  2320
  and the Wellordering Theorem, following Abrial and
paulson@6121
  2321
  Laffitte~\cite{abrial93}.
paulson@6121
  2322
paulson@6121
  2323
\item Theory \verb|Cardinal_AC| uses AC to prove simplified theorems about
paulson@6121
  2324
  the cardinals.  It also proves a theorem needed to justify
paulson@6121
  2325
  infinitely branching datatype declarations: if $\kappa$ is an infinite
paulson@6121
  2326
  cardinal and $|X(\alpha)| \le \kappa$ for all $\alpha<\kappa$ then
paulson@6121
  2327
  $|\union\sb{\alpha<\kappa} X(\alpha)| \le \kappa$.
paulson@6121
  2328
paulson@6121
  2329
\item Theory \texttt{InfDatatype} proves theorems to justify infinitely
paulson@6121
  2330
  branching datatypes.  Arbitrary index sets are allowed, provided their
paulson@6121
  2331
  cardinalities have an upper bound.  The theory also justifies some
paulson@6121
  2332
  unusual cases of finite branching, involving the finite powerset operator
paulson@6121
  2333
  and the finite function space operator.
paulson@6121
  2334
\end{itemize}
paulson@6121
  2335
paulson@6121
  2336
paulson@6121
  2337
paulson@6121
  2338
\section{The examples directories}
paulson@6121
  2339
Directory \texttt{HOL/IMP} contains a mechanised version of a semantic
paulson@6121
  2340
equivalence proof taken from Winskel~\cite{winskel93}.  It formalises the
paulson@6121
  2341
denotational and operational semantics of a simple while-language, then
paulson@6121
  2342
proves the two equivalent.  It contains several datatype and inductive
paulson@6121
  2343
definitions, and demonstrates their use.
paulson@6121
  2344
paulson@6121
  2345
The directory \texttt{ZF/ex} contains further developments in {\ZF} set
paulson@6121
  2346
theory.  Here is an overview; see the files themselves for more details.  I
paulson@6121
  2347
describe much of this material in other
paulson@6121
  2348
publications~\cite{paulson-set-I,paulson-set-II,paulson-CADE}. 
paulson@6121
  2349
\begin{itemize}
paulson@6121
  2350
\item File \texttt{misc.ML} contains miscellaneous examples such as
paulson@6121
  2351
  Cantor's Theorem, the Schr\"oder-Bernstein Theorem and the `Composition
paulson@6121
  2352
  of homomorphisms' challenge~\cite{boyer86}.
paulson@6121
  2353
paulson@6121
  2354
\item Theory \texttt{Ramsey} proves the finite exponent 2 version of
paulson@6121
  2355
  Ramsey's Theorem, following Basin and Kaufmann's
paulson@6121
  2356
  presentation~\cite{basin91}.
paulson@6121
  2357
paulson@6121
  2358
\item Theory \texttt{Integ} develops a theory of the integers as
paulson@6121
  2359
  equivalence classes of pairs of natural numbers.
paulson@6121
  2360
paulson@6121
  2361
\item Theory \texttt{Primrec} develops some computation theory.  It
paulson@6121
  2362
  inductively defines the set of primitive recursive functions and presents a
paulson@6121
  2363
  proof that Ackermann's function is not primitive recursive.
paulson@6121
  2364
paulson@6121
  2365
\item Theory \texttt{Primes} defines the Greatest Common Divisor of two
paulson@6121
  2366
  natural numbers and and the ``divides'' relation.
paulson@6121
  2367
paulson@6121
  2368
\item Theory \texttt{Bin} defines a datatype for two's complement binary
paulson@6121
  2369
  integers, then proves rewrite rules to perform binary arithmetic.  For
paulson@6121
  2370
  instance, $1359\times {-}2468 = {-}3354012$ takes under 14 seconds.
paulson@6121
  2371
paulson@6121
  2372
\item Theory \texttt{BT} defines the recursive data structure ${\tt
paulson@6121
  2373
    bt}(A)$, labelled binary trees.
paulson@6121
  2374
paulson@6121
  2375
\item Theory \texttt{Term} defines a recursive data structure for terms
paulson@6121
  2376
  and term lists.  These are simply finite branching trees.
paulson@6121
  2377
paulson@6121
  2378
\item Theory \texttt{TF} defines primitives for solving mutually
paulson@6121
  2379
  recursive equations over sets.  It constructs sets of trees and forests
paulson@6121
  2380
  as an example, including induction and recursion rules that handle the
paulson@6121
  2381
  mutual recursion.
paulson@6121
  2382
paulson@6121
  2383
\item Theory \texttt{Prop} proves soundness and completeness of
paulson@6121
  2384
  propositional logic~\cite{paulson-set-II}.  This illustrates datatype
paulson@6121
  2385
  definitions, inductive definitions, structural induction and rule
paulson@6121
  2386
  induction.
paulson@6121
  2387
paulson@6121
  2388
\item Theory \texttt{ListN} inductively defines the lists of $n$
paulson@6121
  2389
  elements~\cite{paulin92}.
paulson@6121
  2390
paulson@6121
  2391
\item Theory \texttt{Acc} inductively defines the accessible part of a
paulson@6121
  2392
  relation~\cite{paulin92}.
paulson@6121
  2393
paulson@6121
  2394
\item Theory \texttt{Comb} defines the datatype of combinators and
paulson@6121
  2395
  inductively defines contraction and parallel contraction.  It goes on to
paulson@6121
  2396
  prove the Church-Rosser Theorem.  This case study follows Camilleri and
paulson@6121
  2397
  Melham~\cite{camilleri92}.
paulson@6121
  2398
paulson@6121
  2399
\item Theory \texttt{LList} defines lazy lists and a coinduction
paulson@6121
  2400
  principle for proving equations between them.
paulson@6121
  2401
\end{itemize}
paulson@6121
  2402
paulson@6121
  2403
paulson@6121
  2404
\section{A proof about powersets}\label{sec:ZF-pow-example}
paulson@6121
  2405
To demonstrate high-level reasoning about subsets, let us prove the
paulson@6121
  2406
equation ${{\tt Pow}(A)\cap {\tt Pow}(B)}= {\tt Pow}(A\cap B)$.  Compared
paulson@6121
  2407
with first-order logic, set theory involves a maze of rules, and theorems
paulson@6121
  2408
have many different proofs.  Attempting other proofs of the theorem might
paulson@6121
  2409
be instructive.  This proof exploits the lattice properties of
paulson@6121
  2410
intersection.  It also uses the monotonicity of the powerset operation,
paulson@6121
  2411
from \texttt{ZF/mono.ML}:
paulson@6121
  2412
\begin{ttbox}
paulson@6121
  2413
\tdx{Pow_mono}      A<=B ==> Pow(A) <= Pow(B)
paulson@6121
  2414
\end{ttbox}
paulson@6121
  2415
We enter the goal and make the first step, which breaks the equation into
paulson@6121
  2416
two inclusions by extensionality:\index{*equalityI theorem}
paulson@6121
  2417
\begin{ttbox}
paulson@6121
  2418
Goal "Pow(A Int B) = Pow(A) Int Pow(B)";
paulson@6121
  2419
{\out Level 0}
paulson@6121
  2420
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2421
{\out  1. Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2422
\ttbreak
paulson@6121
  2423
by (resolve_tac [equalityI] 1);
paulson@6121
  2424
{\out Level 1}
paulson@6121
  2425
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2426
{\out  1. Pow(A Int B) <= Pow(A) Int Pow(B)}
paulson@6121
  2427
{\out  2. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2428
\end{ttbox}
paulson@6121
  2429
Both inclusions could be tackled straightforwardly using \texttt{subsetI}.
paulson@6121
  2430
A shorter proof results from noting that intersection forms the greatest
paulson@6121
  2431
lower bound:\index{*Int_greatest theorem}
paulson@6121
  2432
\begin{ttbox}
paulson@6121
  2433
by (resolve_tac [Int_greatest] 1);
paulson@6121
  2434
{\out Level 2}
paulson@6121
  2435
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2436
{\out  1. Pow(A Int B) <= Pow(A)}
paulson@6121
  2437
{\out  2. Pow(A Int B) <= Pow(B)}
paulson@6121
  2438
{\out  3. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2439
\end{ttbox}
paulson@6121
  2440
Subgoal~1 follows by applying the monotonicity of \texttt{Pow} to $A\int
paulson@6121
  2441
B\subseteq A$; subgoal~2 follows similarly:
paulson@6121
  2442
\index{*Int_lower1 theorem}\index{*Int_lower2 theorem}
paulson@6121
  2443
\begin{ttbox}
paulson@6121
  2444
by (resolve_tac [Int_lower1 RS Pow_mono] 1);
paulson@6121
  2445
{\out Level 3}
paulson@6121
  2446
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2447
{\out  1. Pow(A Int B) <= Pow(B)}
paulson@6121
  2448
{\out  2. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2449
\ttbreak
paulson@6121
  2450
by (resolve_tac [Int_lower2 RS Pow_mono] 1);
paulson@6121
  2451
{\out Level 4}
paulson@6121
  2452
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2453
{\out  1. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2454
\end{ttbox}
paulson@6121
  2455
We are left with the opposite inclusion, which we tackle in the
paulson@6121
  2456
straightforward way:\index{*subsetI theorem}
paulson@6121
  2457
\begin{ttbox}
paulson@6121
  2458
by (resolve_tac [subsetI] 1);
paulson@6121
  2459
{\out Level 5}
paulson@6121
  2460
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2461
{\out  1. !!x. x : Pow(A) Int Pow(B) ==> x : Pow(A Int B)}
paulson@6121
  2462
\end{ttbox}
paulson@6121
  2463
The subgoal is to show $x\in {\tt Pow}(A\cap B)$ assuming $x\in{\tt
paulson@6121
  2464
Pow}(A)\cap {\tt Pow}(B)$; eliminating this assumption produces two
paulson@6121
  2465
subgoals.  The rule \tdx{IntE} treats the intersection like a conjunction
paulson@6121
  2466
instead of unfolding its definition.
paulson@6121
  2467
\begin{ttbox}
paulson@6121
  2468
by (eresolve_tac [IntE] 1);
paulson@6121
  2469
{\out Level 6}
paulson@6121
  2470
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2471
{\out  1. !!x. [| x : Pow(A); x : Pow(B) |] ==> x : Pow(A Int B)}
paulson@6121
  2472
\end{ttbox}
paulson@6121
  2473
The next step replaces the \texttt{Pow} by the subset
paulson@6121
  2474
relation~($\subseteq$).\index{*PowI theorem}
paulson@6121
  2475
\begin{ttbox}
paulson@6121
  2476
by (resolve_tac [PowI] 1);
paulson@6121
  2477
{\out Level 7}
paulson@6121
  2478
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2479
{\out  1. !!x. [| x : Pow(A); x : Pow(B) |] ==> x <= A Int B}
paulson@6121
  2480
\end{ttbox}
paulson@6121
  2481
We perform the same replacement in the assumptions.  This is a good
paulson@6121
  2482
demonstration of the tactic \ttindex{dresolve_tac}:\index{*PowD theorem}
paulson@6121
  2483
\begin{ttbox}
paulson@6121
  2484
by (REPEAT (dresolve_tac [PowD] 1));
paulson@6121
  2485
{\out Level 8}
paulson@6121
  2486
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2487
{\out  1. !!x. [| x <= A; x <= B |] ==> x <= A Int B}
paulson@6121
  2488
\end{ttbox}
paulson@6121
  2489
The assumptions are that $x$ is a lower bound of both $A$ and~$B$, but
paulson@6121
  2490
$A\int B$ is the greatest lower bound:\index{*Int_greatest theorem}
paulson@6121
  2491
\begin{ttbox}
paulson@6121
  2492
by (resolve_tac [Int_greatest] 1);
paulson@6121
  2493
{\out Level 9}
paulson@6121
  2494
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2495
{\out  1. !!x. [| x <= A; x <= B |] ==> x <= A}
paulson@6121
  2496
{\out  2. !!x. [| x <= A; x <= B |] ==> x <= B}
paulson@6121
  2497
\end{ttbox}
paulson@6121
  2498
To conclude the proof, we clear up the trivial subgoals:
paulson@6121
  2499
\begin{ttbox}
paulson@6121
  2500
by (REPEAT (assume_tac 1));
paulson@6121
  2501
{\out Level 10}
paulson@6121
  2502
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2503
{\out No subgoals!}
paulson@6121
  2504
\end{ttbox}
paulson@6121
  2505
\medskip
paulson@6121
  2506
We could have performed this proof in one step by applying
paulson@6121
  2507
\ttindex{Blast_tac}.  Let us
paulson@6121
  2508
go back to the start:
paulson@6121
  2509
\begin{ttbox}
paulson@6121
  2510
choplev 0;
paulson@6121
  2511
{\out Level 0}
paulson@6121
  2512
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2513
{\out  1. Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2514
by (Blast_tac 1);
paulson@6121
  2515
{\out Depth = 0}
paulson@6121
  2516
{\out Depth = 1}
paulson@6121
  2517
{\out Depth = 2}
paulson@6121
  2518
{\out Depth = 3}
paulson@6121
  2519
{\out Level 1}
paulson@6121
  2520
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2521
{\out No subgoals!}
paulson@6121
  2522
\end{ttbox}
paulson@6121
  2523
Past researchers regarded this as a difficult proof, as indeed it is if all
paulson@6121
  2524
the symbols are replaced by their definitions.
paulson@6121
  2525
\goodbreak
paulson@6121
  2526
paulson@6121
  2527
\section{Monotonicity of the union operator}
paulson@6121
  2528
For another example, we prove that general union is monotonic:
paulson@6121
  2529
${C\subseteq D}$ implies $\bigcup(C)\subseteq \bigcup(D)$.  To begin, we
paulson@6121
  2530
tackle the inclusion using \tdx{subsetI}:
paulson@6121
  2531
\begin{ttbox}
paulson@6121
  2532
Goal "C<=D ==> Union(C) <= Union(D)";
paulson@6121
  2533
{\out Level 0}
paulson@6121
  2534
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2535
{\out  1. C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2536
\ttbreak
paulson@6121
  2537
by (resolve_tac [subsetI] 1);
paulson@6121
  2538
{\out Level 1}
paulson@6121
  2539
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2540
{\out  1. !!x. [| C <= D; x : Union(C) |] ==> x : Union(D)}
paulson@6121
  2541
\end{ttbox}
paulson@6121
  2542
Big union is like an existential quantifier --- the occurrence in the
paulson@6121
  2543
assumptions must be eliminated early, since it creates parameters.
paulson@6121
  2544
\index{*UnionE theorem}
paulson@6121
  2545
\begin{ttbox}
paulson@6121
  2546
by (eresolve_tac [UnionE] 1);
paulson@6121
  2547
{\out Level 2}
paulson@6121
  2548
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2549
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> x : Union(D)}
paulson@6121
  2550
\end{ttbox}
paulson@6121
  2551
Now we may apply \tdx{UnionI}, which creates an unknown involving the
paulson@6121
  2552
parameters.  To show $x\in \bigcup(D)$ it suffices to show that $x$ belongs
paulson@6121
  2553
to some element, say~$\Var{B2}(x,B)$, of~$D$.
paulson@6121
  2554
\begin{ttbox}
paulson@6121
  2555
by (resolve_tac [UnionI] 1);
paulson@6121
  2556
{\out Level 3}
paulson@6121
  2557
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2558
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> ?B2(x,B) : D}
paulson@6121
  2559
{\out  2. !!x B. [| C <= D; x : B; B : C |] ==> x : ?B2(x,B)}
paulson@6121
  2560
\end{ttbox}
paulson@6121
  2561
Combining \tdx{subsetD} with the assumption $C\subseteq D$ yields 
paulson@6121
  2562
$\Var{a}\in C \Imp \Var{a}\in D$, which reduces subgoal~1.  Note that
paulson@6121
  2563
\texttt{eresolve_tac} has removed that assumption.
paulson@6121
  2564
\begin{ttbox}
paulson@6121
  2565
by (eresolve_tac [subsetD] 1);
paulson@6121
  2566
{\out Level 4}
paulson@6121
  2567
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2568
{\out  1. !!x B. [| x : B; B : C |] ==> ?B2(x,B) : C}
paulson@6121
  2569
{\out  2. !!x B. [| C <= D; x : B; B : C |] ==> x : ?B2(x,B)}
paulson@6121
  2570
\end{ttbox}
paulson@6121
  2571
The rest is routine.  Observe how~$\Var{B2}(x,B)$ is instantiated.
paulson@6121
  2572
\begin{ttbox}
paulson@6121
  2573
by (assume_tac 1);
paulson@6121
  2574
{\out Level 5}
paulson@6121
  2575
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2576
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> x : B}
paulson@6121
  2577
by (assume_tac 1);
paulson@6121
  2578
{\out Level 6}
paulson@6121
  2579
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2580
{\out No subgoals!}
paulson@6121
  2581
\end{ttbox}
paulson@6121
  2582
Again, \ttindex{Blast_tac} can prove the theorem in one step.
paulson@6121
  2583
\begin{ttbox}
paulson@6121
  2584
by (Blast_tac 1);
paulson@6121
  2585
{\out Depth = 0}
paulson@6121
  2586
{\out Depth = 1}
paulson@6121
  2587
{\out Depth = 2}
paulson@6121
  2588
{\out Level 1}
paulson@6121
  2589
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2590
{\out No subgoals!}
paulson@6121
  2591
\end{ttbox}
paulson@6121
  2592
paulson@6121
  2593
The file \texttt{ZF/equalities.ML} has many similar proofs.  Reasoning about
paulson@6121
  2594
general intersection can be difficult because of its anomalous behaviour on
paulson@6121
  2595
the empty set.  However, \ttindex{Blast_tac} copes well with these.  Here is
paulson@6121
  2596
a typical example, borrowed from Devlin~\cite[page 12]{devlin79}:
paulson@6121
  2597
\begin{ttbox}
paulson@6121
  2598
a:C ==> (INT x:C. A(x) Int B(x)) = (INT x:C. A(x)) Int (INT x:C. B(x))
paulson@6121
  2599
\end{ttbox}
paulson@6121
  2600
In traditional notation this is
paulson@6121
  2601
\[ a\in C \,\Imp\, \inter@{x\in C} \Bigl(A(x) \int B(x)\Bigr) =        
paulson@6121
  2602
       \Bigl(\inter@{x\in C} A(x)\Bigr)  \int  
paulson@6121
  2603
       \Bigl(\inter@{x\in C} B(x)\Bigr)  \]
paulson@6121
  2604
paulson@6121
  2605
\section{Low-level reasoning about functions}
paulson@6121
  2606
The derived rules \texttt{lamI}, \texttt{lamE}, \texttt{lam_type}, \texttt{beta}
paulson@6121
  2607
and \texttt{eta} support reasoning about functions in a
paulson@6121
  2608
$\lambda$-calculus style.  This is generally easier than regarding
paulson@6121
  2609
functions as sets of ordered pairs.  But sometimes we must look at the
paulson@6121
  2610
underlying representation, as in the following proof
paulson@6121
  2611
of~\tdx{fun_disjoint_apply1}.  This states that if $f$ and~$g$ are
paulson@6121
  2612
functions with disjoint domains~$A$ and~$C$, and if $a\in A$, then
paulson@6121
  2613
$(f\un g)`a = f`a$:
paulson@6121
  2614
\begin{ttbox}
paulson@6121
  2615
Goal "[| a:A;  f: A->B;  g: C->D;  A Int C = 0 |] ==>  \ttback
paulson@6121
  2616
\ttback    (f Un g)`a = f`a";
paulson@6121
  2617
{\out Level 0}
paulson@6121
  2618
{\out [| a : A; f : A -> B; g : C -> D; A Int C = 0 |]}
paulson@6121
  2619
{\out ==> (f Un g) ` a = f ` a}
paulson@6121
  2620
{\out  1. [| a : A; f : A -> B; g : C -> D; A Int C = 0 |]}
paulson@6121
  2621
{\out     ==> (f Un g) ` a = f ` a}
paulson@6121
  2622
\end{ttbox}
paulson@6121
  2623
Using \tdx{apply_equality}, we reduce the equality to reasoning about
paulson@6121
  2624
ordered pairs.  The second subgoal is to verify that $f\un g$ is a function.
paulson@6121
  2625
To save space, the assumptions will be abbreviated below.
paulson@6121
  2626
\begin{ttbox}
paulson@6121
  2627
by (resolve_tac [apply_equality] 1);
paulson@6121
  2628
{\out Level 1}
paulson@6121
  2629
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2630
{\out  1. [| \ldots |] ==> <a,f ` a> : f Un g}
paulson@6121
  2631
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2632
\end{ttbox}
paulson@6121
  2633
We must show that the pair belongs to~$f$ or~$g$; by~\tdx{UnI1} we
paulson@6121
  2634
choose~$f$:
paulson@6121
  2635
\begin{ttbox}
paulson@6121
  2636
by (resolve_tac [UnI1] 1);
paulson@6121
  2637
{\out Level 2}
paulson@6121
  2638
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2639
{\out  1. [| \ldots |] ==> <a,f ` a> : f}
paulson@6121
  2640
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2641
\end{ttbox}
paulson@6121
  2642
To show $\pair{a,f`a}\in f$ we use \tdx{apply_Pair}, which is
paulson@6121
  2643
essentially the converse of \tdx{apply_equality}:
paulson@6121
  2644
\begin{ttbox}
paulson@6121
  2645
by (resolve_tac [apply_Pair] 1);
paulson@6121
  2646
{\out Level 3}
paulson@6121
  2647
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2648
{\out  1. [| \ldots |] ==> f : (PROD x:?A2. ?B2(x))}
paulson@6121
  2649
{\out  2. [| \ldots |] ==> a : ?A2}
paulson@6121
  2650
{\out  3. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2651
\end{ttbox}
paulson@6121
  2652
Using the assumptions $f\in A\to B$ and $a\in A$, we solve the two subgoals
paulson@6121
  2653
from \tdx{apply_Pair}.  Recall that a $\Pi$-set is merely a generalized
paulson@6121
  2654
function space, and observe that~{\tt?A2} is instantiated to~\texttt{A}.
paulson@6121
  2655
\begin{ttbox}
paulson@6121
  2656
by (assume_tac 1);
paulson@6121
  2657
{\out Level 4}
paulson@6121
  2658
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2659
{\out  1. [| \ldots |] ==> a : A}
paulson@6121
  2660
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2661
by (assume_tac 1);
paulson@6121
  2662
{\out Level 5}
paulson@6121
  2663
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2664
{\out  1. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2665
\end{ttbox}
paulson@6121
  2666
To construct functions of the form $f\un g$, we apply
paulson@6121
  2667
\tdx{fun_disjoint_Un}:
paulson@6121
  2668
\begin{ttbox}
paulson@6121
  2669
by (resolve_tac [fun_disjoint_Un] 1);
paulson@6121
  2670
{\out Level 6}
paulson@6121
  2671
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2672
{\out  1. [| \ldots |] ==> f : ?A3 -> ?B3}
paulson@6121
  2673
{\out  2. [| \ldots |] ==> g : ?C3 -> ?D3}
paulson@6121
  2674
{\out  3. [| \ldots |] ==> ?A3 Int ?C3 = 0}
paulson@6121
  2675
\end{ttbox}
paulson@6121
  2676
The remaining subgoals are instances of the assumptions.  Again, observe how
paulson@6121
  2677
unknowns are instantiated:
paulson@6121
  2678
\begin{ttbox}
paulson@6121
  2679
by (assume_tac 1);
paulson@6121
  2680
{\out Level 7}
paulson@6121
  2681
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2682
{\out  1. [| \ldots |] ==> g : ?C3 -> ?D3}
paulson@6121
  2683
{\out  2. [| \ldots |] ==> A Int ?C3 = 0}
paulson@6121
  2684
by (assume_tac 1);
paulson@6121
  2685
{\out Level 8}
paulson@6121
  2686
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2687
{\out  1. [| \ldots |] ==> A Int C = 0}
paulson@6121
  2688
by (assume_tac 1);
paulson@6121
  2689
{\out Level 9}
paulson@6121
  2690
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2691
{\out No subgoals!}
paulson@6121
  2692
\end{ttbox}
paulson@6121
  2693
See the files \texttt{ZF/func.ML} and \texttt{ZF/WF.ML} for more
paulson@6121
  2694
examples of reasoning about functions.
paulson@6121
  2695
paulson@6121
  2696
\index{set theory|)}