doc-src/ZF/ZF.tex
author paulson
Sat, 12 Aug 2000 21:42:51 +0200
changeset 9584 af21f4364c05
parent 8249 3fc32155372c
child 9695 ec7d7f877712
permissions -rw-r--r--
documented the integers and updated section on nat arithmetic
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@6592
    34
the Axiom of Foundation~\cite{paulson-mscs}.
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@8249
   912
\tdx{lamI}      a:A ==> <a,b(a)> : (lam x:A. b(x))
paulson@8249
   913
\tdx{lamE}      [| p: (lam x:A. b(x));  !!x.[| x:A; p=<x,b(x)> |] ==> P 
paulson@8249
   914
          |] ==>  P
paulson@8249
   915
paulson@8249
   916
\tdx{lam_type}  [| !!x. x:A ==> b(x): B(x) |] ==> (lam x:A. b(x)) : Pi(A,B)
paulson@8249
   917
paulson@8249
   918
\tdx{beta}      a : A ==> (lam x:A. b(x)) ` a = b(a)
paulson@8249
   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@9584
  1089
\subsection{Disjoint unions}
paulson@9584
  1090
paulson@6121
  1091
Theory \thydx{Sum} defines the disjoint union of two sets, with
paulson@6121
  1092
injections and a case analysis operator (Fig.\ts\ref{zf-sum}).  Disjoint
paulson@6121
  1093
unions play a role in datatype definitions, particularly when there is
paulson@6121
  1094
mutual recursion~\cite{paulson-set-II}.
paulson@6121
  1095
paulson@6121
  1096
\begin{figure}
paulson@6121
  1097
\begin{ttbox}
paulson@6121
  1098
\tdx{QPair_def}       <a;b> == a+b
paulson@6121
  1099
\tdx{qsplit_def}      qsplit(c,p)  == THE y. EX a b. p=<a;b> & y=c(a,b)
paulson@6121
  1100
\tdx{qfsplit_def}     qfsplit(R,z) == EX x y. z=<x;y> & R(x,y)
paulson@6121
  1101
\tdx{qconverse_def}   qconverse(r) == {\ttlbrace}z. w:r, EX x y. w=<x;y> & z=<y;x>{\ttrbrace}
paulson@6121
  1102
\tdx{QSigma_def}      QSigma(A,B)  == UN x:A. UN y:B(x). {\ttlbrace}<x;y>{\ttrbrace}
paulson@6121
  1103
paulson@6121
  1104
\tdx{qsum_def}        A <+> B      == ({\ttlbrace}0{\ttrbrace} <*> A) Un ({\ttlbrace}1{\ttrbrace} <*> B)
paulson@6121
  1105
\tdx{QInl_def}        QInl(a)      == <0;a>
paulson@6121
  1106
\tdx{QInr_def}        QInr(b)      == <1;b>
paulson@6121
  1107
\tdx{qcase_def}       qcase(c,d)   == qsplit(\%y z. cond(y, d(z), c(z)))
paulson@6121
  1108
\end{ttbox}
paulson@6121
  1109
\caption{Non-standard pairs, products and sums} \label{zf-qpair}
paulson@6121
  1110
\end{figure}
paulson@6121
  1111
paulson@9584
  1112
paulson@9584
  1113
\subsection{Non-standard ordered pairs}
paulson@9584
  1114
paulson@6121
  1115
Theory \thydx{QPair} defines a notion of ordered pair that admits
paulson@6121
  1116
non-well-founded tupling (Fig.\ts\ref{zf-qpair}).  Such pairs are written
paulson@6121
  1117
{\tt<$a$;$b$>}.  It also defines the eliminator \cdx{qsplit}, the
paulson@6121
  1118
converse operator \cdx{qconverse}, and the summation operator
paulson@6121
  1119
\cdx{QSigma}.  These are completely analogous to the corresponding
paulson@6121
  1120
versions for standard ordered pairs.  The theory goes on to define a
paulson@6121
  1121
non-standard notion of disjoint sum using non-standard pairs.  All of these
paulson@6121
  1122
concepts satisfy the same properties as their standard counterparts; in
paulson@6121
  1123
addition, {\tt<$a$;$b$>} is continuous.  The theory supports coinductive
paulson@6592
  1124
definitions, for example of infinite lists~\cite{paulson-mscs}.
paulson@6121
  1125
paulson@6121
  1126
\begin{figure}
paulson@6121
  1127
\begin{ttbox}
paulson@6121
  1128
\tdx{bnd_mono_def}   bnd_mono(D,h) == 
paulson@6121
  1129
                 h(D)<=D & (ALL W X. W<=X --> X<=D --> h(W) <= h(X))
paulson@6121
  1130
paulson@6121
  1131
\tdx{lfp_def}        lfp(D,h) == Inter({\ttlbrace}X: Pow(D). h(X) <= X{\ttrbrace})
paulson@6121
  1132
\tdx{gfp_def}        gfp(D,h) == Union({\ttlbrace}X: Pow(D). X <= h(X){\ttrbrace})
paulson@6121
  1133
paulson@6121
  1134
paulson@6121
  1135
\tdx{lfp_lowerbound} [| h(A) <= A;  A<=D |] ==> lfp(D,h) <= A
paulson@6121
  1136
paulson@6121
  1137
\tdx{lfp_subset}     lfp(D,h) <= D
paulson@6121
  1138
paulson@6121
  1139
\tdx{lfp_greatest}   [| bnd_mono(D,h);  
paulson@6121
  1140
                  !!X. [| h(X) <= X;  X<=D |] ==> A<=X 
paulson@6121
  1141
               |] ==> A <= lfp(D,h)
paulson@6121
  1142
paulson@6121
  1143
\tdx{lfp_Tarski}     bnd_mono(D,h) ==> lfp(D,h) = h(lfp(D,h))
paulson@6121
  1144
paulson@6121
  1145
\tdx{induct}         [| a : lfp(D,h);  bnd_mono(D,h);
paulson@6121
  1146
                  !!x. x : h(Collect(lfp(D,h),P)) ==> P(x)
paulson@6121
  1147
               |] ==> P(a)
paulson@6121
  1148
paulson@6121
  1149
\tdx{lfp_mono}       [| bnd_mono(D,h);  bnd_mono(E,i);
paulson@6121
  1150
                  !!X. X<=D ==> h(X) <= i(X)  
paulson@6121
  1151
               |] ==> lfp(D,h) <= lfp(E,i)
paulson@6121
  1152
paulson@6121
  1153
\tdx{gfp_upperbound} [| A <= h(A);  A<=D |] ==> A <= gfp(D,h)
paulson@6121
  1154
paulson@6121
  1155
\tdx{gfp_subset}     gfp(D,h) <= D
paulson@6121
  1156
paulson@6121
  1157
\tdx{gfp_least}      [| bnd_mono(D,h);  
paulson@6121
  1158
                  !!X. [| X <= h(X);  X<=D |] ==> X<=A
paulson@6121
  1159
               |] ==> gfp(D,h) <= A
paulson@6121
  1160
paulson@6121
  1161
\tdx{gfp_Tarski}     bnd_mono(D,h) ==> gfp(D,h) = h(gfp(D,h))
paulson@6121
  1162
paulson@6121
  1163
\tdx{coinduct}       [| bnd_mono(D,h); a: X; X <= h(X Un gfp(D,h)); X <= D 
paulson@6121
  1164
               |] ==> a : gfp(D,h)
paulson@6121
  1165
paulson@6121
  1166
\tdx{gfp_mono}       [| bnd_mono(D,h);  D <= E;
paulson@6121
  1167
                  !!X. X<=D ==> h(X) <= i(X)  
paulson@6121
  1168
               |] ==> gfp(D,h) <= gfp(E,i)
paulson@6121
  1169
\end{ttbox}
paulson@6121
  1170
\caption{Least and greatest fixedpoints} \label{zf-fixedpt}
paulson@6121
  1171
\end{figure}
paulson@6121
  1172
paulson@9584
  1173
paulson@9584
  1174
\subsection{Least and greatest fixedpoints}
paulson@9584
  1175
paulson@6121
  1176
The Knaster-Tarski Theorem states that every monotone function over a
paulson@6121
  1177
complete lattice has a fixedpoint.  Theory \thydx{Fixedpt} proves the
paulson@6121
  1178
Theorem only for a particular lattice, namely the lattice of subsets of a
paulson@6121
  1179
set (Fig.\ts\ref{zf-fixedpt}).  The theory defines least and greatest
paulson@6121
  1180
fixedpoint operators with corresponding induction and coinduction rules.
paulson@6121
  1181
These are essential to many definitions that follow, including the natural
paulson@6121
  1182
numbers and the transitive closure operator.  The (co)inductive definition
paulson@6121
  1183
package also uses the fixedpoint operators~\cite{paulson-CADE}.  See
wenzelm@6745
  1184
Davey and Priestley~\cite{davey-priestley} for more on the Knaster-Tarski
paulson@6121
  1185
Theorem and my paper~\cite{paulson-set-II} for discussion of the Isabelle
paulson@6121
  1186
proofs.
paulson@6121
  1187
paulson@6121
  1188
Monotonicity properties are proved for most of the set-forming operations:
paulson@6121
  1189
union, intersection, Cartesian product, image, domain, range, etc.  These
paulson@6121
  1190
are useful for applying the Knaster-Tarski Fixedpoint Theorem.  The proofs
paulson@6121
  1191
themselves are trivial applications of Isabelle's classical reasoner.  See
paulson@6121
  1192
file \texttt{ZF/mono.ML}.
paulson@6121
  1193
paulson@6121
  1194
paulson@9584
  1195
\subsection{Finite sets and lists}
paulson@9584
  1196
paulson@9584
  1197
Theory \texttt{Finite} (Figure~\ref{zf-fin}) defines the finite set operator;
paulson@9584
  1198
${\tt Fin}(A)$ is the set of all finite sets over~$A$.  The theory employs
paulson@9584
  1199
Isabelle's inductive definition package, which proves various rules
paulson@9584
  1200
automatically.  The induction rule shown is stronger than the one proved by
paulson@9584
  1201
the package.  The theory also defines the set of all finite functions
paulson@9584
  1202
between two given sets.
paulson@9584
  1203
paulson@9584
  1204
\begin{figure}
paulson@9584
  1205
\begin{ttbox}
paulson@9584
  1206
\tdx{Fin.emptyI}      0 : Fin(A)
paulson@9584
  1207
\tdx{Fin.consI}       [| a: A;  b: Fin(A) |] ==> cons(a,b) : Fin(A)
paulson@9584
  1208
paulson@9584
  1209
\tdx{Fin_induct}
paulson@9584
  1210
    [| b: Fin(A);
paulson@9584
  1211
       P(0);
paulson@9584
  1212
       !!x y. [| x: A;  y: Fin(A);  x~:y;  P(y) |] ==> P(cons(x,y))
paulson@9584
  1213
    |] ==> P(b)
paulson@9584
  1214
paulson@9584
  1215
\tdx{Fin_mono}        A<=B ==> Fin(A) <= Fin(B)
paulson@9584
  1216
\tdx{Fin_UnI}         [| b: Fin(A);  c: Fin(A) |] ==> b Un c : Fin(A)
paulson@9584
  1217
\tdx{Fin_UnionI}      C : Fin(Fin(A)) ==> Union(C) : Fin(A)
paulson@9584
  1218
\tdx{Fin_subset}      [| c<=b;  b: Fin(A) |] ==> c: Fin(A)
paulson@9584
  1219
\end{ttbox}
paulson@9584
  1220
\caption{The finite set operator} \label{zf-fin}
paulson@9584
  1221
\end{figure}
paulson@9584
  1222
paulson@9584
  1223
\begin{figure}
paulson@9584
  1224
\begin{constants}
paulson@9584
  1225
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@9584
  1226
  \cdx{list}    & $i\To i$      && lists over some set\\
paulson@9584
  1227
  \cdx{list_case} & $[i, [i,i]\To i, i] \To i$  && conditional for $list(A)$ \\
paulson@9584
  1228
  \cdx{map}     & $[i\To i, i] \To i$   &       & mapping functional\\
paulson@9584
  1229
  \cdx{length}  & $i\To i$              &       & length of a list\\
paulson@9584
  1230
  \cdx{rev}     & $i\To i$              &       & reverse of a list\\
paulson@9584
  1231
  \tt \at       & $[i,i]\To i$  &  Right 60     & append for lists\\
paulson@9584
  1232
  \cdx{flat}    & $i\To i$   &                  & append of list of lists
paulson@9584
  1233
\end{constants}
paulson@9584
  1234
paulson@9584
  1235
\underscoreon %%because @ is used here
paulson@9584
  1236
\begin{ttbox}
paulson@9584
  1237
\tdx{NilI}            Nil : list(A)
paulson@9584
  1238
\tdx{ConsI}           [| a: A;  l: list(A) |] ==> Cons(a,l) : list(A)
paulson@9584
  1239
paulson@9584
  1240
\tdx{List.induct}
paulson@9584
  1241
    [| l: list(A);
paulson@9584
  1242
       P(Nil);
paulson@9584
  1243
       !!x y. [| x: A;  y: list(A);  P(y) |] ==> P(Cons(x,y))
paulson@9584
  1244
    |] ==> P(l)
paulson@9584
  1245
paulson@9584
  1246
\tdx{Cons_iff}        Cons(a,l)=Cons(a',l') <-> a=a' & l=l'
paulson@9584
  1247
\tdx{Nil_Cons_iff}    ~ Nil=Cons(a,l)
paulson@9584
  1248
paulson@9584
  1249
\tdx{list_mono}       A<=B ==> list(A) <= list(B)
paulson@9584
  1250
paulson@9584
  1251
\tdx{map_ident}       l: list(A) ==> map(\%u. u, l) = l
paulson@9584
  1252
\tdx{map_compose}     l: list(A) ==> map(h, map(j,l)) = map(\%u. h(j(u)), l)
paulson@9584
  1253
\tdx{map_app_distrib} xs: list(A) ==> map(h, xs@ys) = map(h,xs) @ map(h,ys)
paulson@9584
  1254
\tdx{map_type}
paulson@9584
  1255
    [| l: list(A);  !!x. x: A ==> h(x): B |] ==> map(h,l) : list(B)
paulson@9584
  1256
\tdx{map_flat}
paulson@9584
  1257
    ls: list(list(A)) ==> map(h, flat(ls)) = flat(map(map(h),ls))
paulson@9584
  1258
\end{ttbox}
paulson@9584
  1259
\caption{Lists} \label{zf-list}
paulson@9584
  1260
\end{figure}
paulson@9584
  1261
paulson@9584
  1262
paulson@9584
  1263
Figure~\ref{zf-list} presents the set of lists over~$A$, ${\tt list}(A)$.  The
paulson@9584
  1264
definition employs Isabelle's datatype package, which defines the introduction
paulson@9584
  1265
and induction rules automatically, as well as the constructors, case operator
paulson@9584
  1266
(\verb|list_case|) and recursion operator.  The theory then defines the usual
paulson@9584
  1267
list functions by primitive recursion.  See theory \texttt{List}.
paulson@9584
  1268
paulson@9584
  1269
paulson@9584
  1270
\subsection{Miscellaneous}
paulson@9584
  1271
paulson@6121
  1272
\begin{figure}
paulson@6121
  1273
\begin{constants} 
paulson@6121
  1274
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1275
  \sdx{O}       & $[i,i]\To i$  &  Right 60     & composition ($\circ$) \\
paulson@6121
  1276
  \cdx{id}      & $i\To i$      &       & identity function \\
paulson@6121
  1277
  \cdx{inj}     & $[i,i]\To i$  &       & injective function space\\
paulson@6121
  1278
  \cdx{surj}    & $[i,i]\To i$  &       & surjective function space\\
paulson@6121
  1279
  \cdx{bij}     & $[i,i]\To i$  &       & bijective function space
paulson@6121
  1280
\end{constants}
paulson@6121
  1281
paulson@6121
  1282
\begin{ttbox}
paulson@6121
  1283
\tdx{comp_def}  r O s     == {\ttlbrace}xz : domain(s)*range(r) . 
paulson@6121
  1284
                        EX x y z. xz=<x,z> & <x,y>:s & <y,z>:r{\ttrbrace}
paulson@6121
  1285
\tdx{id_def}    id(A)     == (lam x:A. x)
paulson@6121
  1286
\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
  1287
\tdx{surj_def}  surj(A,B) == {\ttlbrace} f: A->B . ALL y:B. EX x:A. f`x=y {\ttrbrace}
paulson@6121
  1288
\tdx{bij_def}   bij(A,B)  == inj(A,B) Int surj(A,B)
paulson@6121
  1289
paulson@6121
  1290
paulson@6121
  1291
\tdx{left_inverse}     [| f: inj(A,B);  a: A |] ==> converse(f)`(f`a) = a
paulson@6121
  1292
\tdx{right_inverse}    [| f: inj(A,B);  b: range(f) |] ==> 
paulson@6121
  1293
                 f`(converse(f)`b) = b
paulson@6121
  1294
paulson@6121
  1295
\tdx{inj_converse_inj} f: inj(A,B) ==> converse(f): inj(range(f), A)
paulson@6121
  1296
\tdx{bij_converse_bij} f: bij(A,B) ==> converse(f): bij(B,A)
paulson@6121
  1297
paulson@6121
  1298
\tdx{comp_type}        [| s<=A*B;  r<=B*C |] ==> (r O s) <= A*C
paulson@6121
  1299
\tdx{comp_assoc}       (r O s) O t = r O (s O t)
paulson@6121
  1300
paulson@6121
  1301
\tdx{left_comp_id}     r<=A*B ==> id(B) O r = r
paulson@6121
  1302
\tdx{right_comp_id}    r<=A*B ==> r O id(A) = r
paulson@6121
  1303
paulson@6121
  1304
\tdx{comp_func}        [| g:A->B; f:B->C |] ==> (f O g):A->C
paulson@6121
  1305
\tdx{comp_func_apply}  [| g:A->B; f:B->C; a:A |] ==> (f O g)`a = f`(g`a)
paulson@6121
  1306
paulson@6121
  1307
\tdx{comp_inj}         [| g:inj(A,B);  f:inj(B,C)  |] ==> (f O g):inj(A,C)
paulson@6121
  1308
\tdx{comp_surj}        [| g:surj(A,B); f:surj(B,C) |] ==> (f O g):surj(A,C)
paulson@6121
  1309
\tdx{comp_bij}         [| g:bij(A,B); f:bij(B,C) |] ==> (f O g):bij(A,C)
paulson@6121
  1310
paulson@6121
  1311
\tdx{left_comp_inverse}     f: inj(A,B) ==> converse(f) O f = id(A)
paulson@6121
  1312
\tdx{right_comp_inverse}    f: surj(A,B) ==> f O converse(f) = id(B)
paulson@6121
  1313
paulson@6121
  1314
\tdx{bij_disjoint_Un}   
paulson@6121
  1315
    [| f: bij(A,B);  g: bij(C,D);  A Int C = 0;  B Int D = 0 |] ==> 
paulson@6121
  1316
    (f Un g) : bij(A Un C, B Un D)
paulson@6121
  1317
paulson@6121
  1318
\tdx{restrict_bij}  [| f:inj(A,B);  C<=A |] ==> restrict(f,C): bij(C, f``C)
paulson@6121
  1319
\end{ttbox}
paulson@6121
  1320
\caption{Permutations} \label{zf-perm}
paulson@6121
  1321
\end{figure}
paulson@6121
  1322
paulson@6121
  1323
The theory \thydx{Perm} is concerned with permutations (bijections) and
paulson@6121
  1324
related concepts.  These include composition of relations, the identity
paulson@6121
  1325
relation, and three specialized function spaces: injective, surjective and
paulson@6121
  1326
bijective.  Figure~\ref{zf-perm} displays many of their properties that
paulson@6121
  1327
have been proved.  These results are fundamental to a treatment of
paulson@6121
  1328
equipollence and cardinality.
paulson@6121
  1329
paulson@9584
  1330
Theory \thydx{Univ} defines a `universe' $\texttt{univ}(A)$, which is used by
paulson@9584
  1331
the datatype package.  This set contains $A$ and the
paulson@9584
  1332
natural numbers.  Vitally, it is closed under finite products: ${\tt
paulson@9584
  1333
  univ}(A)\times{\tt univ}(A)\subseteq{\tt univ}(A)$.  This theory also
paulson@9584
  1334
defines the cumulative hierarchy of axiomatic set theory, which
paulson@9584
  1335
traditionally is written $V@\alpha$ for an ordinal~$\alpha$.  The
paulson@9584
  1336
`universe' is a simple generalization of~$V@\omega$.
paulson@9584
  1337
paulson@9584
  1338
Theory \thydx{QUniv} defines a `universe' ${\tt quniv}(A)$, which is used by
paulson@9584
  1339
the datatype package to construct codatatypes such as streams.  It is
paulson@9584
  1340
analogous to ${\tt univ}(A)$ (and is defined in terms of it) but is closed
paulson@9584
  1341
under the non-standard product and sum.
paulson@9584
  1342
paulson@9584
  1343
paulson@9584
  1344
\section{Automatic Tools}
paulson@9584
  1345
paulson@9584
  1346
{\ZF} provides the simplifier and the classical reasoner.   Moreover it
paulson@9584
  1347
supplies a specialized tool to infer `types' of terms.
paulson@9584
  1348
paulson@9584
  1349
\subsection{Simplification}
paulson@9584
  1350
paulson@9584
  1351
{\ZF} inherits simplification from {\FOL} but adopts it for set theory.  The
paulson@9584
  1352
extraction of rewrite rules takes the {\ZF} primitives into account.  It can
paulson@9584
  1353
strip bounded universal quantifiers from a formula; for example, ${\forall
paulson@9584
  1354
  x\in A. f(x)=g(x)}$ yields the conditional rewrite rule $x\in A \Imp
paulson@9584
  1355
f(x)=g(x)$.  Given $a\in\{x\in A. P(x)\}$ it extracts rewrite rules from $a\in
paulson@9584
  1356
A$ and~$P(a)$.  It can also break down $a\in A\int B$ and $a\in A-B$.
paulson@9584
  1357
paulson@9584
  1358
Simplification tactics tactics such as \texttt{Asm_simp_tac} and
paulson@9584
  1359
\texttt{Full_simp_tac} use the default simpset (\texttt{simpset()}), which
paulson@9584
  1360
works for most purposes.  A small simplification set for set theory is
paulson@9584
  1361
called~\ttindexbold{ZF_ss}, and you can even use \ttindex{FOL_ss} as a minimal
paulson@9584
  1362
starting point.  \texttt{ZF_ss} contains congruence rules for all the binding
paulson@9584
  1363
operators of {\ZF}\@.  It contains all the conversion rules, such as
paulson@9584
  1364
\texttt{fst} and \texttt{snd}, as well as the rewrites shown in
paulson@9584
  1365
Fig.\ts\ref{zf-simpdata}.  See the file \texttt{ZF/simpdata.ML} for a fuller
paulson@9584
  1366
list.
paulson@9584
  1367
paulson@9584
  1368
paulson@9584
  1369
\subsection{Classical Reasoning}
paulson@9584
  1370
paulson@9584
  1371
As for the classical reasoner, tactics such as \texttt{Blast_tac} and {\tt
paulson@9584
  1372
  Best_tac} refer to the default claset (\texttt{claset()}).  This works for
paulson@9584
  1373
most purposes.  Named clasets include \ttindexbold{ZF_cs} (basic set theory)
paulson@9584
  1374
and \ttindexbold{le_cs} (useful for reasoning about the relations $<$ and
paulson@9584
  1375
$\le$).  You can use \ttindex{FOL_cs} as a minimal basis for building your own
paulson@9584
  1376
clasets.  See \iflabelundefined{chap:classical}{the {\em Reference Manual\/}}%
paulson@9584
  1377
{Chap.\ts\ref{chap:classical}} for more discussion of classical proof methods.
paulson@9584
  1378
paulson@9584
  1379
paulson@9584
  1380
\begin{figure}
paulson@9584
  1381
\begin{eqnarray*}
paulson@9584
  1382
  a\in \emptyset        & \bimp &  \bot\\
paulson@9584
  1383
  a \in A \un B      & \bimp &  a\in A \disj a\in B\\
paulson@9584
  1384
  a \in A \int B      & \bimp &  a\in A \conj a\in B\\
paulson@9584
  1385
  a \in A-B             & \bimp &  a\in A \conj \neg (a\in B)\\
paulson@9584
  1386
  \pair{a,b}\in {\tt Sigma}(A,B)
paulson@9584
  1387
                        & \bimp &  a\in A \conj b\in B(a)\\
paulson@9584
  1388
  a \in {\tt Collect}(A,P)      & \bimp &  a\in A \conj P(a)\\
paulson@9584
  1389
  (\forall x \in \emptyset. P(x)) & \bimp &  \top\\
paulson@9584
  1390
  (\forall x \in A. \top)       & \bimp &  \top
paulson@9584
  1391
\end{eqnarray*}
paulson@9584
  1392
\caption{Some rewrite rules for set theory} \label{zf-simpdata}
paulson@9584
  1393
\end{figure}
paulson@9584
  1394
paulson@9584
  1395
paulson@9584
  1396
\subsection{Type-Checking Tactics}
paulson@9584
  1397
\index{type-checking tactics}
paulson@9584
  1398
paulson@9584
  1399
Isabelle/{\ZF} provides simple tactics to help automate those proofs that are
paulson@9584
  1400
essentially type-checking.  Such proofs are built by applying rules such as
paulson@9584
  1401
these:
paulson@9584
  1402
\begin{ttbox}
paulson@9584
  1403
[| ?P ==> ?a: ?A; ~?P ==> ?b: ?A |] ==> (if ?P then ?a else ?b): ?A
paulson@9584
  1404
paulson@9584
  1405
[| ?m : nat; ?n : nat |] ==> ?m #+ ?n : nat
paulson@9584
  1406
paulson@9584
  1407
?a : ?A ==> Inl(?a) : ?A + ?B  
paulson@9584
  1408
\end{ttbox}
paulson@9584
  1409
In typical applications, the goal has the form $t\in\Var{A}$: in other words,
paulson@9584
  1410
we have a specific term~$t$ and need to infer its `type' by instantiating the
paulson@9584
  1411
set variable~$\Var{A}$.  Neither the simplifier nor the classical reasoner
paulson@9584
  1412
does this job well.  The if-then-else rule, and many similar ones, can make
paulson@9584
  1413
the classical reasoner loop.  The simplifier refuses (on principle) to
paulson@9584
  1414
instantiate variables during rewriting, so goals such as \texttt{i\#+j :\ ?A}
paulson@9584
  1415
are left unsolved.
paulson@9584
  1416
paulson@9584
  1417
The simplifier calls the type-checker to solve rewritten subgoals: this stage
paulson@9584
  1418
can indeed instantiate variables.  If you have defined new constants and
paulson@9584
  1419
proved type-checking rules for them, then insert the rules using
paulson@9584
  1420
\texttt{AddTCs} and the rest should be automatic.  In particular, the
paulson@9584
  1421
simplifier will use type-checking to help satisfy conditional rewrite rules.
paulson@9584
  1422
Call the tactic \ttindex{Typecheck_tac} to break down all subgoals using
paulson@9584
  1423
type-checking rules.
paulson@9584
  1424
paulson@9584
  1425
Though the easiest way to invoke the type-checker is via the simplifier,
paulson@9584
  1426
specialized applications may require more detailed knowledge of
paulson@9584
  1427
the type-checking primitives.  They are modelled on the simplifier's:
paulson@9584
  1428
\begin{ttdescription}
paulson@9584
  1429
\item[\ttindexbold{tcset}] is the type of tcsets: sets of type-checking rules.
paulson@9584
  1430
paulson@9584
  1431
\item[\ttindexbold{addTCs}] is an infix operator to add type-checking rules to
paulson@9584
  1432
  a tcset.
paulson@9584
  1433
  
paulson@9584
  1434
\item[\ttindexbold{delTCs}] is an infix operator to remove type-checking rules
paulson@9584
  1435
  from a tcset.
paulson@9584
  1436
paulson@9584
  1437
\item[\ttindexbold{typecheck_tac}] is a tactic for attempting to prove all
paulson@9584
  1438
  subgoals using the rules given in its argument, a tcset.
paulson@9584
  1439
\end{ttdescription}
paulson@9584
  1440
paulson@9584
  1441
Tcsets, like simpsets, are associated with theories and are merged when
paulson@9584
  1442
theories are merged.  There are further primitives that use the default tcset.
paulson@9584
  1443
\begin{ttdescription}
paulson@9584
  1444
\item[\ttindexbold{tcset}] is a function to return the default tcset; use the
paulson@9584
  1445
  expression \texttt{tcset()}.
paulson@9584
  1446
paulson@9584
  1447
\item[\ttindexbold{AddTCs}] adds type-checking rules to the default tcset.
paulson@9584
  1448
  
paulson@9584
  1449
\item[\ttindexbold{DelTCs}] removes type-checking rules from the default
paulson@9584
  1450
  tcset.
paulson@9584
  1451
paulson@9584
  1452
\item[\ttindexbold{Typecheck_tac}] calls \texttt{typecheck_tac} using the
paulson@9584
  1453
  default tcset.
paulson@9584
  1454
\end{ttdescription}
paulson@9584
  1455
paulson@9584
  1456
To supply some type-checking rules temporarily, using \texttt{Addrules} and
paulson@9584
  1457
later \texttt{Delrules} is the simplest way.  There is also a high-tech
paulson@9584
  1458
approach.  Call the simplifier with a new solver expressed using
paulson@9584
  1459
\ttindexbold{type_solver_tac} and your temporary type-checking rules.
paulson@9584
  1460
\begin{ttbox}
paulson@9584
  1461
by (asm_simp_tac 
paulson@9584
  1462
     (simpset() setSolver type_solver_tac (tcset() addTCs prems)) 2);
paulson@9584
  1463
\end{ttbox}
paulson@9584
  1464
paulson@9584
  1465
paulson@9584
  1466
\section{Natural number and integer arithmetic}
paulson@9584
  1467
paulson@9584
  1468
\index{arithmetic|(}
paulson@9584
  1469
paulson@6121
  1470
\begin{figure}\small
paulson@6121
  1471
\index{#*@{\tt\#*} symbol}
paulson@6121
  1472
\index{*div symbol}
paulson@6121
  1473
\index{*mod symbol}
paulson@6121
  1474
\index{#+@{\tt\#+} symbol}
paulson@6121
  1475
\index{#-@{\tt\#-} symbol}
paulson@6121
  1476
\begin{constants}
paulson@6121
  1477
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@6121
  1478
  \cdx{nat}     & $i$                   &       & set of natural numbers \\
paulson@6121
  1479
  \cdx{nat_case}& $[i,i\To i,i]\To i$     &     & conditional for $nat$\\
paulson@6121
  1480
  \tt \#*       & $[i,i]\To i$  &  Left 70      & multiplication \\
paulson@6121
  1481
  \tt div       & $[i,i]\To i$  &  Left 70      & division\\
paulson@6121
  1482
  \tt mod       & $[i,i]\To i$  &  Left 70      & modulus\\
paulson@6121
  1483
  \tt \#+       & $[i,i]\To i$  &  Left 65      & addition\\
paulson@6121
  1484
  \tt \#-       & $[i,i]\To i$  &  Left 65      & subtraction
paulson@6121
  1485
\end{constants}
paulson@6121
  1486
paulson@6121
  1487
\begin{ttbox}
paulson@6121
  1488
\tdx{nat_def}  nat == lfp(lam r: Pow(Inf). {\ttlbrace}0{\ttrbrace} Un {\ttlbrace}succ(x). x:r{\ttrbrace}
paulson@6121
  1489
paulson@6121
  1490
\tdx{nat_case_def}  nat_case(a,b,k) == 
paulson@6121
  1491
              THE y. k=0 & y=a | (EX x. k=succ(x) & y=b(x))
paulson@6121
  1492
paulson@9584
  1493
\tdx{nat_0I}           0 : nat
paulson@9584
  1494
\tdx{nat_succI}        n : nat ==> succ(n) : nat
paulson@6121
  1495
paulson@6121
  1496
\tdx{nat_induct}        
paulson@6121
  1497
    [| n: nat;  P(0);  !!x. [| x: nat;  P(x) |] ==> P(succ(x)) 
paulson@6121
  1498
    |] ==> P(n)
paulson@6121
  1499
paulson@9584
  1500
\tdx{nat_case_0}      nat_case(a,b,0) = a
paulson@9584
  1501
\tdx{nat_case_succ}   nat_case(a,b,succ(m)) = b(m)
paulson@9584
  1502
paulson@9584
  1503
\tdx{add_0_natify}     0 #+ n = natify(n)
paulson@9584
  1504
\tdx{add_succ}         succ(m) #+ n = succ(m #+ n)
paulson@9584
  1505
paulson@9584
  1506
\tdx{mult_type}        m #* n : nat
paulson@9584
  1507
\tdx{mult_0}           0 #* n = 0
paulson@9584
  1508
\tdx{mult_succ}        succ(m) #* n = n #+ (m #* n)
paulson@9584
  1509
\tdx{mult_commute}     m #* n = n #* m
paulson@9584
  1510
\tdx{add_mult_dist}    (m #+ n) #* k = (m #* k) #+ (n #* k)
paulson@9584
  1511
\tdx{mult_assoc}       (m #* n) #* k = m #* (n #* k)
paulson@9584
  1512
\tdx{mod_div_equality} m: nat ==> (m div n)#*n #+ m mod n = m
paulson@6121
  1513
\end{ttbox}
paulson@6121
  1514
\caption{The natural numbers} \label{zf-nat}
paulson@6121
  1515
\end{figure}
paulson@6121
  1516
paulson@9584
  1517
\index{natural numbers}
paulson@9584
  1518
paulson@6121
  1519
Theory \thydx{Nat} defines the natural numbers and mathematical
paulson@6121
  1520
induction, along with a case analysis operator.  The set of natural
paulson@6121
  1521
numbers, here called \texttt{nat}, is known in set theory as the ordinal~$\omega$.
paulson@6121
  1522
paulson@6121
  1523
Theory \thydx{Arith} develops arithmetic on the natural numbers
paulson@6121
  1524
(Fig.\ts\ref{zf-nat}).  Addition, multiplication and subtraction are defined
paulson@6121
  1525
by primitive recursion.  Division and remainder are defined by repeated
paulson@6121
  1526
subtraction, which requires well-founded recursion; the termination argument
paulson@6121
  1527
relies on the divisor's being non-zero.  Many properties are proved:
paulson@6121
  1528
commutative, associative and distributive laws, identity and cancellation
paulson@6121
  1529
laws, etc.  The most interesting result is perhaps the theorem $a \bmod b +
paulson@6121
  1530
(a/b)\times b = a$.
paulson@6121
  1531
paulson@9584
  1532
To minimize the need for tedious proofs of $t\in\texttt{nat}$, the arithmetic
paulson@9584
  1533
operators coerce their arguments to be natural numbers.  The function
paulson@9584
  1534
\cdx{natify} is defined such that $\texttt{natify}(n) = n$ if $n$ is a natural
paulson@9584
  1535
number, $\texttt{natify}(\texttt{succ}(x)) =
paulson@9584
  1536
\texttt{succ}(\texttt{natify}(x))$ for all $x$, and finally
paulson@9584
  1537
$\texttt{natify}(x)=0$ in all other cases.  The benefit is that the addition,
paulson@9584
  1538
subtraction, multiplication, division and remainder operators always return
paulson@9584
  1539
natural numbers, regardless of their arguments.  Algebraic laws (commutative,
paulson@9584
  1540
associative, distributive) are unconditional.  Occurrences of \texttt{natify}
paulson@9584
  1541
as operands of those operators are simplified away.  Any remaining occurrences
paulson@9584
  1542
can either be tolerated or else eliminated by proving that the argument is a
paulson@9584
  1543
natural number.
paulson@9584
  1544
paulson@9584
  1545
The simplifier automatically cancels common terms on the opposite sides of
paulson@9584
  1546
subtraction and of relations ($=$, $<$ and $\le$).  Here is an example:
paulson@6121
  1547
\begin{ttbox}
paulson@9584
  1548
 1. i #+ j #+ k #- j < k #+ l
paulson@9584
  1549
> by (Simp_tac 1);
paulson@9584
  1550
 1. natify(i) < natify(l)
paulson@6121
  1551
\end{ttbox}
paulson@9584
  1552
Given the assumptions \texttt{i:nat} and \texttt{l:nat}, both occurrences of
paulson@9584
  1553
\cdx{natify} would be simplified away.
paulson@9584
  1554
paulson@9584
  1555
paulson@9584
  1556
\begin{figure}\small
paulson@9584
  1557
\index{$*@{\tt\$*} symbol}
paulson@9584
  1558
\index{$+@{\tt\$+} symbol}
paulson@9584
  1559
\index{$-@{\tt\$-} symbol}
paulson@6121
  1560
\begin{constants}
paulson@6121
  1561
  \it symbol  & \it meta-type & \it priority & \it description \\ 
paulson@9584
  1562
  \cdx{int}     & $i$                   &       & set of integers \\
paulson@9584
  1563
  \tt \$*       & $[i,i]\To i$  &  Left 70      & multiplication \\
paulson@9584
  1564
  \tt \$+       & $[i,i]\To i$  &  Left 65      & addition\\
paulson@9584
  1565
  \tt \$-       & $[i,i]\To i$  &  Left 65      & subtraction\\
paulson@9584
  1566
  \tt \$<       & $[i,i]\To o$  &  Left 50      & $<$ on integers\\
paulson@9584
  1567
  \tt \$<=      & $[i,i]\To o$  &  Left 50      & $\le$ on integers
paulson@6121
  1568
\end{constants}
paulson@6121
  1569
paulson@6121
  1570
\begin{ttbox}
paulson@9584
  1571
\tdx{zadd_0_intify}     0 $+ n = intify(n)
paulson@9584
  1572
paulson@9584
  1573
\tdx{zmult_type}        m $* n : int
paulson@9584
  1574
\tdx{zmult_0}           0 $* n = 0
paulson@9584
  1575
\tdx{zmult_commute}     m $* n = n $* m
paulson@9584
  1576
\tdx{zadd_zmult_dist}    (m $+ n) $* k = (m $* k) $+ (n $* k)
paulson@9584
  1577
\tdx{zmult_assoc}       (m $* n) $* k = m $* (n $* k)
paulson@6121
  1578
\end{ttbox}
paulson@9584
  1579
\caption{The integers} \label{zf-int}
paulson@6121
  1580
\end{figure}
paulson@6121
  1581
paulson@6121
  1582
paulson@9584
  1583
\index{integers}
paulson@9584
  1584
paulson@9584
  1585
Theory \thydx{Int} defines the integers, as equivalence classes of natural
paulson@9584
  1586
numbers.   Figure~\ref{zf-int} presents a tidy collection of laws.  In
paulson@9584
  1587
fact, a large library of facts is proved, including monotonicity laws for
paulson@9584
  1588
addition and multiplication, covering both positive and negative operands.  
paulson@9584
  1589
paulson@9584
  1590
As with the natural numbers, the need for typing proofs is minimized.  All the
paulson@9584
  1591
operators defined in Fig.\ts\ref{zf-int} coerce their operands to integers by
paulson@9584
  1592
applying the function \cdx{intify}.  This function is the identity on integers
paulson@9584
  1593
and maps other operands to zero.
paulson@9584
  1594
paulson@9584
  1595
Decimal notation is provided for the integers.  Numbers, written as
paulson@9584
  1596
\texttt{\#$nnn$} or \texttt{\#-$nnn$}, are represented internally in
paulson@9584
  1597
two's-complement binary.  Expressions involving addition, subtraction and
paulson@9584
  1598
multiplication of numeral constants are evaluated (with acceptable efficiency)
paulson@9584
  1599
by simplification.  The simplifier also collects similar terms, multiplying
paulson@9584
  1600
them by a numerical coefficient.  It also cancels occurrences of the same
paulson@9584
  1601
terms on the other side of the relational operators.  Example:
paulson@6173
  1602
\begin{ttbox}
paulson@9584
  1603
 1. y $+ z $+ #-3 $* x $+ y $<= x $* #2 $+ z
paulson@9584
  1604
> by (Simp_tac 1);
paulson@9584
  1605
 1. #2 $* y $<= #5 $* x
paulson@6173
  1606
\end{ttbox}
paulson@9584
  1607
For more information on the integers, please see the theories on directory
paulson@9584
  1608
\texttt{ZF/Integ}. 
paulson@9584
  1609
paulson@9584
  1610
\index{arithmetic|)}
paulson@6173
  1611
paulson@6173
  1612
paulson@6121
  1613
\section{Datatype definitions}
paulson@6121
  1614
\label{sec:ZF:datatype}
paulson@6121
  1615
\index{*datatype|(}
paulson@6121
  1616
paulson@6121
  1617
The \ttindex{datatype} definition package of \ZF\ constructs inductive
paulson@6121
  1618
datatypes similar to those of \ML.  It can also construct coinductive
paulson@6121
  1619
datatypes (codatatypes), which are non-well-founded structures such as
paulson@6121
  1620
streams.  It defines the set using a fixed-point construction and proves
paulson@6121
  1621
induction rules, as well as theorems for recursion and case combinators.  It
paulson@6121
  1622
supplies mechanisms for reasoning about freeness.  The datatype package can
paulson@6121
  1623
handle both mutual and indirect recursion.
paulson@6121
  1624
paulson@6121
  1625
paulson@6121
  1626
\subsection{Basics}
paulson@6121
  1627
\label{subsec:datatype:basics}
paulson@6121
  1628
paulson@6121
  1629
A \texttt{datatype} definition has the following form:
paulson@6121
  1630
\[
paulson@6121
  1631
\begin{array}{llcl}
paulson@6121
  1632
\mathtt{datatype} & t@1(A@1,\ldots,A@h) & = &
paulson@6121
  1633
  constructor^1@1 ~\mid~ \ldots ~\mid~ constructor^1@{k@1} \\
paulson@6121
  1634
 & & \vdots \\
paulson@6121
  1635
\mathtt{and} & t@n(A@1,\ldots,A@h) & = &
paulson@6121
  1636
  constructor^n@1~ ~\mid~ \ldots ~\mid~ constructor^n@{k@n}
paulson@6121
  1637
\end{array}
paulson@6121
  1638
\]
paulson@6121
  1639
Here $t@1$, \ldots,~$t@n$ are identifiers and $A@1$, \ldots,~$A@h$ are
paulson@6121
  1640
variables: the datatype's parameters.  Each constructor specification has the
paulson@6121
  1641
form \dquotesoff
paulson@6121
  1642
\[ C \hbox{\tt~( } \hbox{\tt"} x@1 \hbox{\tt:} T@1 \hbox{\tt"},\;
paulson@6121
  1643
                   \ldots,\;
paulson@6121
  1644
                   \hbox{\tt"} x@m \hbox{\tt:} T@m \hbox{\tt"}
paulson@6121
  1645
     \hbox{\tt~)}
paulson@6121
  1646
\]
paulson@6121
  1647
Here $C$ is the constructor name, and variables $x@1$, \ldots,~$x@m$ are the
paulson@6121
  1648
constructor arguments, belonging to the sets $T@1$, \ldots, $T@m$,
paulson@6121
  1649
respectively.  Typically each $T@j$ is either a constant set, a datatype
paulson@6121
  1650
parameter (one of $A@1$, \ldots, $A@h$) or a recursive occurrence of one of
paulson@6121
  1651
the datatypes, say $t@i(A@1,\ldots,A@h)$.  More complex possibilities exist,
paulson@6121
  1652
but they are much harder to realize.  Often, additional information must be
paulson@6121
  1653
supplied in the form of theorems.
paulson@6121
  1654
paulson@6121
  1655
A datatype can occur recursively as the argument of some function~$F$.  This
paulson@6121
  1656
is called a {\em nested} (or \emph{indirect}) occurrence.  It is only allowed
paulson@6121
  1657
if the datatype package is given a theorem asserting that $F$ is monotonic.
paulson@6121
  1658
If the datatype has indirect occurrences, then Isabelle/ZF does not support
paulson@6121
  1659
recursive function definitions.
paulson@6121
  1660
paulson@6121
  1661
A simple example of a datatype is \texttt{list}, which is built-in, and is
paulson@6121
  1662
defined by
paulson@6121
  1663
\begin{ttbox}
paulson@6121
  1664
consts     list :: i=>i
paulson@6121
  1665
datatype  "list(A)" = Nil | Cons ("a:A", "l: list(A)")
paulson@6121
  1666
\end{ttbox}
paulson@6121
  1667
Note that the datatype operator must be declared as a constant first.
paulson@6121
  1668
However, the package declares the constructors.  Here, \texttt{Nil} gets type
paulson@6121
  1669
$i$ and \texttt{Cons} gets type $[i,i]\To i$.
paulson@6121
  1670
paulson@6121
  1671
Trees and forests can be modelled by the mutually recursive datatype
paulson@6121
  1672
definition
paulson@6121
  1673
\begin{ttbox}
paulson@6121
  1674
consts     tree, forest, tree_forest :: i=>i
paulson@6121
  1675
datatype  "tree(A)"   = Tcons ("a: A",  "f: forest(A)")
paulson@6121
  1676
and       "forest(A)" = Fnil  |  Fcons ("t: tree(A)",  "f: forest(A)")
paulson@6121
  1677
\end{ttbox}
paulson@6121
  1678
Here $\texttt{tree}(A)$ is the set of trees over $A$, $\texttt{forest}(A)$ is
paulson@6121
  1679
the set of forests over $A$, and  $\texttt{tree_forest}(A)$ is the union of
paulson@6121
  1680
the previous two sets.  All three operators must be declared first.
paulson@6121
  1681
paulson@6121
  1682
The datatype \texttt{term}, which is defined by
paulson@6121
  1683
\begin{ttbox}
paulson@6121
  1684
consts     term :: i=>i
paulson@6121
  1685
datatype  "term(A)" = Apply ("a: A", "l: list(term(A))")
paulson@6121
  1686
  monos "[list_mono]"
paulson@6121
  1687
\end{ttbox}
paulson@6121
  1688
is an example of nested recursion.  (The theorem \texttt{list_mono} is proved
paulson@6121
  1689
in file \texttt{List.ML}, and the \texttt{term} example is devaloped in theory
paulson@6121
  1690
\thydx{ex/Term}.)
paulson@6121
  1691
paulson@6121
  1692
\subsubsection{Freeness of the constructors}
paulson@6121
  1693
paulson@6121
  1694
Constructors satisfy {\em freeness} properties.  Constructions are distinct,
paulson@6121
  1695
for example $\texttt{Nil}\not=\texttt{Cons}(a,l)$, and they are injective, for
paulson@6121
  1696
example $\texttt{Cons}(a,l)=\texttt{Cons}(a',l') \bimp a=a' \conj l=l'$.
paulson@6121
  1697
Because the number of freeness is quadratic in the number of constructors, the
paulson@6143
  1698
datatype package does not prove them.  Instead, it ensures that simplification
paulson@6143
  1699
will prove them dynamically: when the simplifier encounters a formula
paulson@6143
  1700
asserting the equality of two datatype constructors, it performs freeness
paulson@6143
  1701
reasoning.  
paulson@6143
  1702
paulson@6143
  1703
Freeness reasoning can also be done using the classical reasoner, but it is
paulson@6143
  1704
more complicated.  You have to add some safe elimination rules rules to the
paulson@6143
  1705
claset.  For the \texttt{list} datatype, they are called
paulson@6143
  1706
\texttt{list.free_SEs}.  Occasionally this exposes the underlying
paulson@6143
  1707
representation of some constructor, which can be rectified using the command
paulson@6143
  1708
\hbox{\tt fold_tac list.con_defs}.
paulson@6143
  1709
paulson@6121
  1710
paulson@6121
  1711
\subsubsection{Structural induction}
paulson@6121
  1712
paulson@6121
  1713
The datatype package also provides structural induction rules.  For datatypes
paulson@6121
  1714
without mutual or nested recursion, the rule has the form exemplified by
paulson@6121
  1715
\texttt{list.induct} in Fig.\ts\ref{zf-list}.  For mutually recursive
paulson@6121
  1716
datatypes, the induction rule is supplied in two forms.  Consider datatype
paulson@6121
  1717
\texttt{TF}.  The rule \texttt{tree_forest.induct} performs induction over a
paulson@6121
  1718
single predicate~\texttt{P}, which is presumed to be defined for both trees
paulson@6121
  1719
and forests:
paulson@6121
  1720
\begin{ttbox}
paulson@6121
  1721
[| x : tree_forest(A);
paulson@8249
  1722
   !!a f. [| a : A; f : forest(A); P(f) |] ==> P(Tcons(a, f)); 
paulson@8249
  1723
   P(Fnil);
paulson@6121
  1724
   !!f t. [| t : tree(A); P(t); f : forest(A); P(f) |]
paulson@6121
  1725
          ==> P(Fcons(t, f)) 
paulson@6121
  1726
|] ==> P(x)
paulson@6121
  1727
\end{ttbox}
paulson@6121
  1728
The rule \texttt{tree_forest.mutual_induct} performs induction over two
paulson@6121
  1729
distinct predicates, \texttt{P_tree} and \texttt{P_forest}.
paulson@6121
  1730
\begin{ttbox}
paulson@6121
  1731
[| !!a f.
paulson@6121
  1732
      [| a : A; f : forest(A); P_forest(f) |] ==> P_tree(Tcons(a, f));
paulson@6121
  1733
   P_forest(Fnil);
paulson@6121
  1734
   !!f t. [| t : tree(A); P_tree(t); f : forest(A); P_forest(f) |]
paulson@6121
  1735
          ==> P_forest(Fcons(t, f)) 
paulson@6121
  1736
|] ==> (ALL za. za : tree(A) --> P_tree(za)) &
paulson@6121
  1737
    (ALL za. za : forest(A) --> P_forest(za))
paulson@6121
  1738
\end{ttbox}
paulson@6121
  1739
paulson@6121
  1740
For datatypes with nested recursion, such as the \texttt{term} example from
paulson@6121
  1741
above, things are a bit more complicated.  The rule \texttt{term.induct}
paulson@6121
  1742
refers to the monotonic operator, \texttt{list}:
paulson@6121
  1743
\begin{ttbox}
paulson@6121
  1744
[| x : term(A);
paulson@8249
  1745
   !!a l. [| a: A; l: list(Collect(term(A), P)) |] ==> P(Apply(a, l)) 
paulson@6121
  1746
|] ==> P(x)
paulson@6121
  1747
\end{ttbox}
paulson@6121
  1748
The file \texttt{ex/Term.ML} derives two higher-level induction rules, one of
paulson@6121
  1749
which is particularly useful for proving equations:
paulson@6121
  1750
\begin{ttbox}
paulson@6121
  1751
[| t : term(A);
paulson@6121
  1752
   !!x zs. [| x : A; zs : list(term(A)); map(f, zs) = map(g, zs) |]
paulson@6121
  1753
           ==> f(Apply(x, zs)) = g(Apply(x, zs)) 
paulson@6121
  1754
|] ==> f(t) = g(t)  
paulson@6121
  1755
\end{ttbox}
paulson@6121
  1756
How this can be generalized to other nested datatypes is a matter for future
paulson@6121
  1757
research.
paulson@6121
  1758
paulson@6121
  1759
paulson@6121
  1760
\subsubsection{The \texttt{case} operator}
paulson@6121
  1761
paulson@6121
  1762
The package defines an operator for performing case analysis over the
paulson@6121
  1763
datatype.  For \texttt{list}, it is called \texttt{list_case} and satisfies
paulson@6121
  1764
the equations
paulson@6121
  1765
\begin{ttbox}
paulson@6121
  1766
list_case(f_Nil, f_Cons, []) = f_Nil
paulson@6121
  1767
list_case(f_Nil, f_Cons, Cons(a, l)) = f_Cons(a, l)
paulson@6121
  1768
\end{ttbox}
paulson@6121
  1769
Here \texttt{f_Nil} is the value to return if the argument is \texttt{Nil} and
paulson@6121
  1770
\texttt{f_Cons} is a function that computes the value to return if the
paulson@6121
  1771
argument has the form $\texttt{Cons}(a,l)$.  The function can be expressed as
paulson@6121
  1772
an abstraction, over patterns if desired (\S\ref{sec:pairs}).
paulson@6121
  1773
paulson@6121
  1774
For mutually recursive datatypes, there is a single \texttt{case} operator.
paulson@6121
  1775
In the tree/forest example, the constant \texttt{tree_forest_case} handles all
paulson@6121
  1776
of the constructors of the two datatypes.
paulson@6121
  1777
paulson@6121
  1778
paulson@6121
  1779
paulson@6121
  1780
paulson@6121
  1781
\subsection{Defining datatypes}
paulson@6121
  1782
paulson@6121
  1783
The theory syntax for datatype definitions is shown in
paulson@6121
  1784
Fig.~\ref{datatype-grammar}.  In order to be well-formed, a datatype
paulson@6121
  1785
definition has to obey the rules stated in the previous section.  As a result
paulson@6121
  1786
the theory is extended with the new types, the constructors, and the theorems
paulson@6121
  1787
listed in the previous section.  The quotation marks are necessary because
paulson@6121
  1788
they enclose general Isabelle formul\ae.
paulson@6121
  1789
paulson@6121
  1790
\begin{figure}
paulson@6121
  1791
\begin{rail}
paulson@6121
  1792
datatype : ( 'datatype' | 'codatatype' ) datadecls;
paulson@6121
  1793
paulson@6121
  1794
datadecls: ( '"' id arglist '"' '=' (constructor + '|') ) + 'and'
paulson@6121
  1795
         ;
paulson@6121
  1796
constructor : name ( () | consargs )  ( () | ( '(' mixfix ')' ) )
paulson@6121
  1797
         ;
paulson@6121
  1798
consargs : '(' ('"' var ':' term '"' + ',') ')'
paulson@6121
  1799
         ;
paulson@6121
  1800
\end{rail}
paulson@6121
  1801
\caption{Syntax of datatype declarations}
paulson@6121
  1802
\label{datatype-grammar}
paulson@6121
  1803
\end{figure}
paulson@6121
  1804
paulson@6121
  1805
Codatatypes are declared like datatypes and are identical to them in every
paulson@6121
  1806
respect except that they have a coinduction rule instead of an induction rule.
paulson@6121
  1807
Note that while an induction rule has the effect of limiting the values
paulson@6121
  1808
contained in the set, a coinduction rule gives a way of constructing new
paulson@6121
  1809
values of the set.
paulson@6121
  1810
paulson@6121
  1811
Most of the theorems about datatypes become part of the default simpset.  You
paulson@6121
  1812
never need to see them again because the simplifier applies them
paulson@6143
  1813
automatically.  Induction or exhaustion are usually invoked by hand,
paulson@6121
  1814
usually via these special-purpose tactics:
paulson@6121
  1815
\begin{ttdescription}
paulson@6121
  1816
\item[\ttindexbold{induct_tac} {\tt"}$x${\tt"} $i$] applies structural
paulson@6121
  1817
  induction on variable $x$ to subgoal $i$, provided the type of $x$ is a
paulson@6121
  1818
  datatype.  The induction variable should not occur among other assumptions
paulson@6121
  1819
  of the subgoal.
paulson@6121
  1820
\end{ttdescription}
paulson@6121
  1821
In some cases, induction is overkill and a case distinction over all
paulson@6121
  1822
constructors of the datatype suffices.
paulson@6121
  1823
\begin{ttdescription}
paulson@6121
  1824
\item[\ttindexbold{exhaust_tac} {\tt"}$x${\tt"} $i$]
paulson@6121
  1825
 performs an exhaustive case analysis for the variable~$x$.
paulson@6121
  1826
\end{ttdescription}
paulson@6121
  1827
paulson@6121
  1828
Both tactics can only be applied to a variable, whose typing must be given in
paulson@6121
  1829
some assumption, for example the assumption \texttt{x:\ list(A)}.  The tactics
paulson@6121
  1830
also work for the natural numbers (\texttt{nat}) and disjoint sums, although
paulson@6121
  1831
these sets were not defined using the datatype package.  (Disjoint sums are
paulson@6121
  1832
not recursive, so only \texttt{exhaust_tac} is available.)
paulson@6121
  1833
paulson@6121
  1834
\bigskip
paulson@6121
  1835
Here are some more details for the technically minded.  Processing the
paulson@6121
  1836
theory file produces an \ML\ structure which, in addition to the usual
paulson@6121
  1837
components, contains a structure named $t$ for each datatype $t$ defined in
paulson@6121
  1838
the file.  Each structure $t$ contains the following elements:
paulson@6121
  1839
\begin{ttbox}
paulson@6121
  1840
val intrs         : thm list  \textrm{the introduction rules}
paulson@6121
  1841
val elim          : thm       \textrm{the elimination (case analysis) rule}
paulson@6121
  1842
val induct        : thm       \textrm{the standard induction rule}
paulson@6121
  1843
val mutual_induct : thm       \textrm{the mutual induction rule, or \texttt{True}}
paulson@6121
  1844
val case_eqns     : thm list  \textrm{equations for the case operator}
paulson@6121
  1845
val recursor_eqns : thm list  \textrm{equations for the recursor}
paulson@6121
  1846
val con_defs      : thm list  \textrm{definitions of the case operator and constructors}
paulson@6121
  1847
val free_iffs     : thm list  \textrm{logical equivalences for proving freeness}
paulson@6121
  1848
val free_SEs      : thm list  \textrm{elimination rules for proving freeness}
paulson@6121
  1849
val mk_free       : string -> thm  \textrm{A function for proving freeness theorems}
paulson@6141
  1850
val mk_cases      : string -> thm  \textrm{case analysis, see below}
paulson@6121
  1851
val defs          : thm list  \textrm{definitions of operators}
paulson@6121
  1852
val bnd_mono      : thm list  \textrm{monotonicity property}
paulson@6121
  1853
val dom_subset    : thm list  \textrm{inclusion in `bounding set'}
paulson@6121
  1854
\end{ttbox}
paulson@6121
  1855
Furthermore there is the theorem $C$\texttt{_I} for every constructor~$C$; for
paulson@6121
  1856
example, the \texttt{list} datatype's introduction rules are bound to the
paulson@6121
  1857
identifiers \texttt{Nil_I} and \texttt{Cons_I}.
paulson@6121
  1858
paulson@6121
  1859
For a codatatype, the component \texttt{coinduct} is the coinduction rule,
paulson@6121
  1860
replacing the \texttt{induct} component.
paulson@6121
  1861
paulson@6121
  1862
See the theories \texttt{ex/Ntree} and \texttt{ex/Brouwer} for examples of
paulson@6121
  1863
infinitely branching datatypes.  See theory \texttt{ex/LList} for an example
paulson@6121
  1864
of a codatatype.  Some of these theories illustrate the use of additional,
paulson@6121
  1865
undocumented features of the datatype package.  Datatype definitions are
paulson@6121
  1866
reduced to inductive definitions, and the advanced features should be
paulson@6121
  1867
understood in that light.
paulson@6121
  1868
paulson@6121
  1869
paulson@6121
  1870
\subsection{Examples}
paulson@6121
  1871
paulson@6121
  1872
\subsubsection{The datatype of binary trees}
paulson@6121
  1873
paulson@6121
  1874
Let us define the set $\texttt{bt}(A)$ of binary trees over~$A$.  The theory
paulson@6121
  1875
must contain these lines:
paulson@6121
  1876
\begin{ttbox}
paulson@6121
  1877
consts   bt :: i=>i
paulson@6121
  1878
datatype "bt(A)"  =  Lf  |  Br ("a: A",  "t1: bt(A)",  "t2: bt(A)")
paulson@6121
  1879
\end{ttbox}
paulson@6121
  1880
After loading the theory, we can prove, for example, that no tree equals its
paulson@6121
  1881
left branch.  To ease the induction, we state the goal using quantifiers.
paulson@6121
  1882
\begin{ttbox}
paulson@6121
  1883
Goal "l : bt(A) ==> ALL x r. Br(x,l,r) ~= l";
paulson@6121
  1884
{\out Level 0}
paulson@6121
  1885
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1886
{\out  1. l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1887
\end{ttbox}
paulson@6121
  1888
This can be proved by the structural induction tactic:
paulson@6121
  1889
\begin{ttbox}
paulson@6121
  1890
by (induct_tac "l" 1);
paulson@6121
  1891
{\out Level 1}
paulson@6121
  1892
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1893
{\out  1. ALL x r. Br(x, Lf, r) ~= Lf}
paulson@6121
  1894
{\out  2. !!a t1 t2.}
paulson@8249
  1895
{\out        [| a : A; t1 : bt(A);}
paulson@8249
  1896
{\out           ALL x r. Br(x, t1, r) ~= t1; t2 : bt(A);}
paulson@6121
  1897
{\out           ALL x r. Br(x, t2, r) ~= t2 |]}
paulson@6121
  1898
{\out        ==> ALL x r. Br(x, Br(a, t1, t2), r) ~= Br(a, t1, t2)}
paulson@6121
  1899
\end{ttbox}
paulson@6143
  1900
Both subgoals are proved using \texttt{Auto_tac}, which performs the necessary
paulson@6143
  1901
freeness reasoning. 
paulson@6121
  1902
\begin{ttbox}
paulson@6143
  1903
by Auto_tac;
paulson@6121
  1904
{\out Level 2}
paulson@6121
  1905
{\out l : bt(A) ==> ALL x r. Br(x, l, r) ~= l}
paulson@6121
  1906
{\out No subgoals!}
paulson@6121
  1907
\end{ttbox}
paulson@6121
  1908
To remove the quantifiers from the induction formula, we save the theorem using
paulson@6121
  1909
\ttindex{qed_spec_mp}.
paulson@6121
  1910
\begin{ttbox}
paulson@6121
  1911
qed_spec_mp "Br_neq_left";
paulson@6121
  1912
{\out val Br_neq_left = "?l : bt(?A) ==> Br(?x, ?l, ?r) ~= ?l" : thm}
paulson@6121
  1913
\end{ttbox}
paulson@6121
  1914
paulson@6121
  1915
When there are only a few constructors, we might prefer to prove the freenness
paulson@6121
  1916
theorems for each constructor.  This is trivial, using the function given us
paulson@6121
  1917
for that purpose:
paulson@6121
  1918
\begin{ttbox}
paulson@8249
  1919
val Br_iff = 
paulson@8249
  1920
    bt.mk_free "Br(a,l,r)=Br(a',l',r') <-> a=a' & l=l' & r=r'";
paulson@6121
  1921
{\out val Br_iff =}
paulson@6121
  1922
{\out   "Br(?a, ?l, ?r) = Br(?a', ?l', ?r') <->}
paulson@6121
  1923
{\out                     ?a = ?a' & ?l = ?l' & ?r = ?r'" : thm}
paulson@6121
  1924
\end{ttbox}
paulson@6121
  1925
paulson@6141
  1926
The purpose of \ttindex{mk_cases} is to generate instances of the elimination
paulson@6141
  1927
(case analysis) rule that have been simplified using freeness reasoning.  For
paulson@6141
  1928
example, this instance of the elimination rule propagates type-checking
paulson@6141
  1929
information from the premise $\texttt{Br}(a,l,r)\in\texttt{bt}(A)$:
paulson@6121
  1930
\begin{ttbox}
paulson@6141
  1931
val BrE = bt.mk_cases "Br(a,l,r) : bt(A)";
paulson@6121
  1932
{\out val BrE =}
paulson@6121
  1933
{\out   "[| Br(?a, ?l, ?r) : bt(?A);}
paulson@8249
  1934
{\out       [| ?a : ?A; ?l : bt(?A); ?r : bt(?A) |] ==> ?Q |]}
paulson@8249
  1935
{\out    ==> ?Q" : thm}
paulson@6121
  1936
\end{ttbox}
paulson@6121
  1937
paulson@6121
  1938
paulson@6121
  1939
\subsubsection{Mixfix syntax in datatypes}
paulson@6121
  1940
paulson@6121
  1941
Mixfix syntax is sometimes convenient.  The theory \texttt{ex/PropLog} makes a
paulson@6121
  1942
deep embedding of propositional logic:
paulson@6121
  1943
\begin{ttbox}
paulson@6121
  1944
consts     prop :: i
paulson@6121
  1945
datatype  "prop" = Fls
paulson@6121
  1946
                 | Var ("n: nat")                ("#_" [100] 100)
paulson@6121
  1947
                 | "=>" ("p: prop", "q: prop")   (infixr 90)
paulson@6121
  1948
\end{ttbox}
paulson@6121
  1949
The second constructor has a special $\#n$ syntax, while the third constructor
paulson@6121
  1950
is an infixed arrow.
paulson@6121
  1951
paulson@6121
  1952
paulson@6121
  1953
\subsubsection{A giant enumeration type}
paulson@6121
  1954
paulson@6121
  1955
This example shows a datatype that consists of 60 constructors:
paulson@6121
  1956
\begin{ttbox}
paulson@6121
  1957
consts  enum :: i
paulson@6121
  1958
datatype
paulson@6121
  1959
  "enum" = C00 | C01 | C02 | C03 | C04 | C05 | C06 | C07 | C08 | C09
paulson@6121
  1960
         | C10 | C11 | C12 | C13 | C14 | C15 | C16 | C17 | C18 | C19
paulson@6121
  1961
         | C20 | C21 | C22 | C23 | C24 | C25 | C26 | C27 | C28 | C29
paulson@6121
  1962
         | C30 | C31 | C32 | C33 | C34 | C35 | C36 | C37 | C38 | C39
paulson@6121
  1963
         | C40 | C41 | C42 | C43 | C44 | C45 | C46 | C47 | C48 | C49
paulson@6121
  1964
         | C50 | C51 | C52 | C53 | C54 | C55 | C56 | C57 | C58 | C59
paulson@6121
  1965
end
paulson@6121
  1966
\end{ttbox}
paulson@6121
  1967
The datatype package scales well.  Even though all properties are proved
paulson@6121
  1968
rather than assumed, full processing of this definition takes under 15 seconds
paulson@6121
  1969
(on a 300 MHz Pentium).  The constructors have a balanced representation,
paulson@6121
  1970
essentially binary notation, so freeness properties can be proved fast.
paulson@6121
  1971
\begin{ttbox}
paulson@6121
  1972
Goal "C00 ~= C01";
paulson@6143
  1973
by (Simp_tac 1);
paulson@6121
  1974
\end{ttbox}
paulson@6121
  1975
You need not derive such inequalities explicitly.  The simplifier will dispose
paulson@6143
  1976
of them automatically.
paulson@6121
  1977
paulson@6121
  1978
\index{*datatype|)}
paulson@6121
  1979
paulson@6121
  1980
paulson@6121
  1981
\subsection{Recursive function definitions}\label{sec:ZF:recursive}
paulson@6121
  1982
\index{recursive functions|see{recursion}}
paulson@6121
  1983
\index{*primrec|(}
paulson@6173
  1984
\index{recursion!primitive|(}
paulson@6121
  1985
paulson@6121
  1986
Datatypes come with a uniform way of defining functions, {\bf primitive
paulson@6121
  1987
  recursion}.  Such definitions rely on the recursion operator defined by the
paulson@6121
  1988
datatype package.  Isabelle proves the desired recursion equations as
paulson@6121
  1989
theorems.
paulson@6121
  1990
paulson@6121
  1991
In principle, one could introduce primitive recursive functions by asserting
paulson@6121
  1992
their reduction rules as new axioms.  Here is a dangerous way of defining the
paulson@6121
  1993
append function for lists:
paulson@6121
  1994
\begin{ttbox}\slshape
paulson@6121
  1995
consts  "\at" :: [i,i]=>i                        (infixr 60)
paulson@6121
  1996
rules 
paulson@6121
  1997
   app_Nil   "[] \at ys = ys"
paulson@6121
  1998
   app_Cons  "(Cons(a,l)) \at ys = Cons(a, l \at ys)"
paulson@6121
  1999
\end{ttbox}
paulson@6121
  2000
Asserting axioms brings the danger of accidentally asserting nonsense.  It
paulson@6121
  2001
should be avoided at all costs!
paulson@6121
  2002
paulson@6121
  2003
The \ttindex{primrec} declaration is a safe means of defining primitive
paulson@6121
  2004
recursive functions on datatypes:
paulson@6121
  2005
\begin{ttbox}
paulson@6121
  2006
consts  "\at" :: [i,i]=>i                        (infixr 60)
paulson@6121
  2007
primrec 
paulson@6121
  2008
   "[] \at ys = ys"
paulson@6121
  2009
   "(Cons(a,l)) \at ys = Cons(a, l \at ys)"
paulson@6121
  2010
\end{ttbox}
paulson@6121
  2011
Isabelle will now check that the two rules do indeed form a primitive
paulson@6121
  2012
recursive definition.  For example, the declaration
paulson@6121
  2013
\begin{ttbox}
paulson@6121
  2014
primrec
paulson@6121
  2015
   "[] \at ys = us"
paulson@6121
  2016
\end{ttbox}
paulson@6121
  2017
is rejected with an error message ``\texttt{Extra variables on rhs}''.
paulson@6121
  2018
paulson@6121
  2019
paulson@6121
  2020
\subsubsection{Syntax of recursive definitions}
paulson@6121
  2021
paulson@6121
  2022
The general form of a primitive recursive definition is
paulson@6121
  2023
\begin{ttbox}
paulson@6121
  2024
primrec
paulson@6121
  2025
    {\it reduction rules}
paulson@6121
  2026
\end{ttbox}
paulson@6121
  2027
where \textit{reduction rules} specify one or more equations of the form
paulson@6121
  2028
\[ f \, x@1 \, \dots \, x@m \, (C \, y@1 \, \dots \, y@k) \, z@1 \,
paulson@6121
  2029
\dots \, z@n = r \] such that $C$ is a constructor of the datatype, $r$
paulson@6121
  2030
contains only the free variables on the left-hand side, and all recursive
paulson@6121
  2031
calls in $r$ are of the form $f \, \dots \, y@i \, \dots$ for some $i$.  
paulson@6121
  2032
There must be at most one reduction rule for each constructor.  The order is
paulson@6121
  2033
immaterial.  For missing constructors, the function is defined to return zero.
paulson@6121
  2034
paulson@6121
  2035
All reduction rules are added to the default simpset.
paulson@6121
  2036
If you would like to refer to some rule by name, then you must prefix
paulson@6121
  2037
the rule with an identifier.  These identifiers, like those in the
paulson@6121
  2038
\texttt{rules} section of a theory, will be visible at the \ML\ level.
paulson@6121
  2039
paulson@6121
  2040
The reduction rules for {\tt\at} become part of the default simpset, which
paulson@6121
  2041
leads to short proof scripts:
paulson@6121
  2042
\begin{ttbox}\underscoreon
paulson@6121
  2043
Goal "xs: list(A) ==> (xs @ ys) @ zs = xs @ (ys @ zs)";
paulson@6121
  2044
by (induct\_tac "xs" 1);
paulson@6121
  2045
by (ALLGOALS Asm\_simp\_tac);
paulson@6121
  2046
\end{ttbox}
paulson@6121
  2047
paulson@6121
  2048
You can even use the \texttt{primrec} form with non-recursive datatypes and
paulson@6121
  2049
with codatatypes.  Recursion is not allowed, but it provides a convenient
paulson@6121
  2050
syntax for defining functions by cases.
paulson@6121
  2051
paulson@6121
  2052
paulson@6121
  2053
\subsubsection{Example: varying arguments}
paulson@6121
  2054
paulson@6121
  2055
All arguments, other than the recursive one, must be the same in each equation
paulson@6121
  2056
and in each recursive call.  To get around this restriction, use explict
paulson@6121
  2057
$\lambda$-abstraction and function application.  Here is an example, drawn
paulson@6121
  2058
from the theory \texttt{Resid/Substitution}.  The type of redexes is declared
paulson@6121
  2059
as follows:
paulson@6121
  2060
\begin{ttbox}
paulson@6121
  2061
consts  redexes :: i
paulson@6121
  2062
datatype
paulson@6121
  2063
  "redexes" = Var ("n: nat")            
paulson@6121
  2064
            | Fun ("t: redexes")
paulson@6121
  2065
            | App ("b:bool" ,"f:redexes" , "a:redexes")
paulson@6121
  2066
\end{ttbox}
paulson@6121
  2067
paulson@6121
  2068
The function \texttt{lift} takes a second argument, $k$, which varies in
paulson@6121
  2069
recursive calls.
paulson@6121
  2070
\begin{ttbox}
paulson@6121
  2071
primrec
paulson@6121
  2072
  "lift(Var(i)) = (lam k:nat. if i<k then Var(i) else Var(succ(i)))"
paulson@6121
  2073
  "lift(Fun(t)) = (lam k:nat. Fun(lift(t) ` succ(k)))"
paulson@6121
  2074
  "lift(App(b,f,a)) = (lam k:nat. App(b, lift(f)`k, lift(a)`k))"
paulson@6121
  2075
\end{ttbox}
paulson@6121
  2076
Now \texttt{lift(r)`k} satisfies the required recursion equations.
paulson@6121
  2077
paulson@6121
  2078
\index{recursion!primitive|)}
paulson@6121
  2079
\index{*primrec|)}
paulson@6121
  2080
paulson@6121
  2081
paulson@6121
  2082
\section{Inductive and coinductive definitions}
paulson@6121
  2083
\index{*inductive|(}
paulson@6121
  2084
\index{*coinductive|(}
paulson@6121
  2085
paulson@6121
  2086
An {\bf inductive definition} specifies the least set~$R$ closed under given
paulson@6121
  2087
rules.  (Applying a rule to elements of~$R$ yields a result within~$R$.)  For
paulson@6121
  2088
example, a structural operational semantics is an inductive definition of an
paulson@6121
  2089
evaluation relation.  Dually, a {\bf coinductive definition} specifies the
paulson@6121
  2090
greatest set~$R$ consistent with given rules.  (Every element of~$R$ can be
paulson@6121
  2091
seen as arising by applying a rule to elements of~$R$.)  An important example
paulson@6121
  2092
is using bisimulation relations to formalise equivalence of processes and
paulson@6121
  2093
infinite data structures.
paulson@6121
  2094
paulson@6121
  2095
A theory file may contain any number of inductive and coinductive
paulson@6121
  2096
definitions.  They may be intermixed with other declarations; in
paulson@6121
  2097
particular, the (co)inductive sets {\bf must} be declared separately as
paulson@6121
  2098
constants, and may have mixfix syntax or be subject to syntax translations.
paulson@6121
  2099
paulson@6121
  2100
Each (co)inductive definition adds definitions to the theory and also
paulson@6121
  2101
proves some theorems.  Each definition creates an \ML\ structure, which is a
paulson@6121
  2102
substructure of the main theory structure.
paulson@6121
  2103
This package is described in detail in a separate paper,%
paulson@6121
  2104
\footnote{It appeared in CADE~\cite{paulson-CADE}; a longer version is
paulson@6121
  2105
  distributed with Isabelle as \emph{A Fixedpoint Approach to 
paulson@6121
  2106
 (Co)Inductive and (Co)Datatype Definitions}.}  %
paulson@6121
  2107
which you might refer to for background information.
paulson@6121
  2108
paulson@6121
  2109
paulson@6121
  2110
\subsection{The syntax of a (co)inductive definition}
paulson@6121
  2111
An inductive definition has the form
paulson@6121
  2112
\begin{ttbox}
paulson@6121
  2113
inductive
paulson@6121
  2114
  domains    {\it domain declarations}
paulson@6121
  2115
  intrs      {\it introduction rules}
paulson@6121
  2116
  monos      {\it monotonicity theorems}
paulson@6121
  2117
  con_defs   {\it constructor definitions}
paulson@6121
  2118
  type_intrs {\it introduction rules for type-checking}
paulson@6121
  2119
  type_elims {\it elimination rules for type-checking}
paulson@6121
  2120
\end{ttbox}
paulson@6121
  2121
A coinductive definition is identical, but starts with the keyword
paulson@8249
  2122
{\tt co\-inductive}.  
paulson@6121
  2123
paulson@6121
  2124
The {\tt monos}, {\tt con\_defs}, {\tt type\_intrs} and {\tt type\_elims}
paulson@6121
  2125
sections are optional.  If present, each is specified either as a list of
paulson@6121
  2126
identifiers or as a string.  If the latter, then the string must be a valid
paulson@6121
  2127
\textsc{ml} expression of type {\tt thm list}.  The string is simply inserted
paulson@6121
  2128
into the {\tt _thy.ML} file; if it is ill-formed, it will trigger \textsc{ml}
paulson@6121
  2129
error messages.  You can then inspect the file on the temporary directory.
paulson@6121
  2130
paulson@6121
  2131
\begin{description}
paulson@8249
  2132
\item[\it domain declarations] are items of the form
paulson@6121
  2133
  {\it string\/}~{\tt <=}~{\it string}, associating each recursive set with
paulson@6121
  2134
  its domain.  (The domain is some existing set that is large enough to
paulson@6121
  2135
  hold the new set being defined.)
paulson@6121
  2136
paulson@6121
  2137
\item[\it introduction rules] specify one or more introduction rules in
paulson@6121
  2138
  the form {\it ident\/}~{\it string}, where the identifier gives the name of
paulson@6121
  2139
  the rule in the result structure.
paulson@6121
  2140
paulson@6121
  2141
\item[\it monotonicity theorems] are required for each operator applied to
paulson@6121
  2142
  a recursive set in the introduction rules.  There \textbf{must} be a theorem
paulson@6121
  2143
  of the form $A\subseteq B\Imp M(A)\subseteq M(B)$, for each premise $t\in M(R_i)$
paulson@6121
  2144
  in an introduction rule!
paulson@6121
  2145
paulson@6121
  2146
\item[\it constructor definitions] contain definitions of constants
paulson@6121
  2147
  appearing in the introduction rules.  The (co)datatype package supplies
paulson@6121
  2148
  the constructors' definitions here.  Most (co)inductive definitions omit
paulson@6121
  2149
  this section; one exception is the primitive recursive functions example;
paulson@6121
  2150
  see theory \texttt{ex/Primrec}.
paulson@6121
  2151
  
paulson@6121
  2152
\item[\it type\_intrs] consists of introduction rules for type-checking the
paulson@6121
  2153
  definition: for demonstrating that the new set is included in its domain.
paulson@6121
  2154
  (The proof uses depth-first search.)
paulson@6121
  2155
paulson@6121
  2156
\item[\it type\_elims] consists of elimination rules for type-checking the
paulson@6121
  2157
  definition.  They are presumed to be safe and are applied as often as
paulson@6121
  2158
  possible prior to the {\tt type\_intrs} search.
paulson@6121
  2159
\end{description}
paulson@6121
  2160
paulson@6121
  2161
The package has a few restrictions:
paulson@6121
  2162
\begin{itemize}
paulson@6121
  2163
\item The theory must separately declare the recursive sets as
paulson@6121
  2164
  constants.
paulson@6121
  2165
paulson@6121
  2166
\item The names of the recursive sets must be identifiers, not infix
paulson@6121
  2167
operators.  
paulson@6121
  2168
paulson@6121
  2169
\item Side-conditions must not be conjunctions.  However, an introduction rule
paulson@6121
  2170
may contain any number of side-conditions.
paulson@6121
  2171
paulson@6121
  2172
\item Side-conditions of the form $x=t$, where the variable~$x$ does not
paulson@6121
  2173
  occur in~$t$, will be substituted through the rule \verb|mutual_induct|.
paulson@6121
  2174
\end{itemize}
paulson@6121
  2175
paulson@6121
  2176
paulson@6121
  2177
\subsection{Example of an inductive definition}
paulson@6121
  2178
paulson@6121
  2179
Two declarations, included in a theory file, define the finite powerset
paulson@6121
  2180
operator.  First we declare the constant~\texttt{Fin}.  Then we declare it
paulson@6121
  2181
inductively, with two introduction rules:
paulson@6121
  2182
\begin{ttbox}
paulson@6121
  2183
consts  Fin :: i=>i
paulson@6121
  2184
paulson@6121
  2185
inductive
paulson@6121
  2186
  domains   "Fin(A)" <= "Pow(A)"
paulson@6121
  2187
  intrs
paulson@6121
  2188
    emptyI  "0 : Fin(A)"
paulson@6121
  2189
    consI   "[| a: A;  b: Fin(A) |] ==> cons(a,b) : Fin(A)"
paulson@6121
  2190
  type_intrs empty_subsetI, cons_subsetI, PowI
paulson@6121
  2191
  type_elims "[make_elim PowD]"
paulson@6121
  2192
\end{ttbox}
paulson@6121
  2193
The resulting theory structure contains a substructure, called~\texttt{Fin}.
paulson@6121
  2194
It contains the \texttt{Fin}$~A$ introduction rules as the list
paulson@6121
  2195
\texttt{Fin.intrs}, and also individually as \texttt{Fin.emptyI} and
paulson@6121
  2196
\texttt{Fin.consI}.  The induction rule is \texttt{Fin.induct}.
paulson@6121
  2197
paulson@6121
  2198
The chief problem with making (co)inductive definitions involves type-checking
paulson@6121
  2199
the rules.  Sometimes, additional theorems need to be supplied under
paulson@6121
  2200
\texttt{type_intrs} or \texttt{type_elims}.  If the package fails when trying
paulson@6121
  2201
to prove your introduction rules, then set the flag \ttindexbold{trace_induct}
paulson@6121
  2202
to \texttt{true} and try again.  (See the manual \emph{A Fixedpoint Approach
paulson@6121
  2203
  \ldots} for more discussion of type-checking.)
paulson@6121
  2204
paulson@6121
  2205
In the example above, $\texttt{Pow}(A)$ is given as the domain of
paulson@6121
  2206
$\texttt{Fin}(A)$, for obviously every finite subset of~$A$ is a subset
paulson@6121
  2207
of~$A$.  However, the inductive definition package can only prove that given a
paulson@6121
  2208
few hints.
paulson@6121
  2209
Here is the output that results (with the flag set) when the
paulson@6121
  2210
\texttt{type_intrs} and \texttt{type_elims} are omitted from the inductive
paulson@6121
  2211
definition above:
paulson@6121
  2212
\begin{ttbox}
paulson@6121
  2213
Inductive definition Finite.Fin
paulson@6121
  2214
Fin(A) ==
paulson@6121
  2215
lfp(Pow(A),
paulson@6121
  2216
    \%X. {z: Pow(A) . z = 0 | (EX a b. z = cons(a, b) & a : A & b : X)})
paulson@6121
  2217
  Proving monotonicity...
paulson@6121
  2218
\ttbreak
paulson@6121
  2219
  Proving the introduction rules...
paulson@6173
  2220
The type-checking subgoal:
paulson@6121
  2221
0 : Fin(A)
paulson@6121
  2222
 1. 0 : Pow(A)
paulson@6121
  2223
\ttbreak
paulson@6121
  2224
The subgoal after monos, type_elims:
paulson@6121
  2225
0 : Fin(A)
paulson@6121
  2226
 1. 0 : Pow(A)
paulson@6121
  2227
*** prove_goal: tactic failed
paulson@6121
  2228
\end{ttbox}
paulson@6121
  2229
We see the need to supply theorems to let the package prove
paulson@6121
  2230
$\emptyset\in\texttt{Pow}(A)$.  Restoring the \texttt{type_intrs} but not the
paulson@6121
  2231
\texttt{type_elims}, we again get an error message:
paulson@6121
  2232
\begin{ttbox}
paulson@6173
  2233
The type-checking subgoal:
paulson@6121
  2234
0 : Fin(A)
paulson@6121
  2235
 1. 0 : Pow(A)
paulson@6121
  2236
\ttbreak
paulson@6121
  2237
The subgoal after monos, type_elims:
paulson@6121
  2238
0 : Fin(A)
paulson@6121
  2239
 1. 0 : Pow(A)
paulson@6121
  2240
\ttbreak
paulson@6173
  2241
The type-checking subgoal:
paulson@6121
  2242
cons(a, b) : Fin(A)
paulson@6121
  2243
 1. [| a : A; b : Fin(A) |] ==> cons(a, b) : Pow(A)
paulson@6121
  2244
\ttbreak
paulson@6121
  2245
The subgoal after monos, type_elims:
paulson@6121
  2246
cons(a, b) : Fin(A)
paulson@6121
  2247
 1. [| a : A; b : Pow(A) |] ==> cons(a, b) : Pow(A)
paulson@6121
  2248
*** prove_goal: tactic failed
paulson@6121
  2249
\end{ttbox}
paulson@6121
  2250
The first rule has been type-checked, but the second one has failed.  The
paulson@6121
  2251
simplest solution to such problems is to prove the failed subgoal separately
paulson@6121
  2252
and to supply it under \texttt{type_intrs}.  The solution actually used is
paulson@6121
  2253
to supply, under \texttt{type_elims}, a rule that changes
paulson@6121
  2254
$b\in\texttt{Pow}(A)$ to $b\subseteq A$; together with \texttt{cons_subsetI}
paulson@6121
  2255
and \texttt{PowI}, it is enough to complete the type-checking.
paulson@6121
  2256
paulson@6121
  2257
paulson@6121
  2258
paulson@6121
  2259
\subsection{Further examples}
paulson@6121
  2260
paulson@6121
  2261
An inductive definition may involve arbitrary monotonic operators.  Here is a
paulson@6121
  2262
standard example: the accessible part of a relation.  Note the use
paulson@6121
  2263
of~\texttt{Pow} in the introduction rule and the corresponding mention of the
paulson@6121
  2264
rule \verb|Pow_mono| in the \texttt{monos} list.  If the desired rule has a
paulson@6121
  2265
universally quantified premise, usually the effect can be obtained using
paulson@6121
  2266
\texttt{Pow}.
paulson@6121
  2267
\begin{ttbox}
paulson@6121
  2268
consts  acc :: i=>i
paulson@6121
  2269
inductive
paulson@6121
  2270
  domains "acc(r)" <= "field(r)"
paulson@6121
  2271
  intrs
paulson@6121
  2272
    vimage  "[| r-``{a}: Pow(acc(r)); a: field(r) |] ==> a: acc(r)"
paulson@6121
  2273
  monos      Pow_mono
paulson@6121
  2274
\end{ttbox}
paulson@6121
  2275
paulson@6121
  2276
Finally, here is a coinductive definition.  It captures (as a bisimulation)
paulson@6121
  2277
the notion of equality on lazy lists, which are first defined as a codatatype:
paulson@6121
  2278
\begin{ttbox}
paulson@6121
  2279
consts  llist :: i=>i
paulson@6121
  2280
codatatype  "llist(A)" = LNil | LCons ("a: A", "l: llist(A)")
paulson@6121
  2281
\ttbreak
paulson@6121
  2282
paulson@6121
  2283
consts  lleq :: i=>i
paulson@6121
  2284
coinductive
paulson@6121
  2285
  domains "lleq(A)" <= "llist(A) * llist(A)"
paulson@6121
  2286
  intrs
paulson@6121
  2287
    LNil  "<LNil, LNil> : lleq(A)"
paulson@6121
  2288
    LCons "[| a:A; <l,l'>: lleq(A) |] 
paulson@6121
  2289
           ==> <LCons(a,l), LCons(a,l')>: lleq(A)"
paulson@6121
  2290
  type_intrs  "llist.intrs"
paulson@6121
  2291
\end{ttbox}
paulson@6121
  2292
This use of \texttt{type_intrs} is typical: the relation concerns the
paulson@6121
  2293
codatatype \texttt{llist}, so naturally the introduction rules for that
paulson@6121
  2294
codatatype will be required for type-checking the rules.
paulson@6121
  2295
paulson@6121
  2296
The Isabelle distribution contains many other inductive definitions.  Simple
paulson@6121
  2297
examples are collected on subdirectory \texttt{ZF/ex}.  The directory
paulson@6121
  2298
\texttt{Coind} and the theory \texttt{ZF/ex/LList} contain coinductive
paulson@6121
  2299
definitions.  Larger examples may be found on other subdirectories of
paulson@6121
  2300
\texttt{ZF}, such as \texttt{IMP}, and \texttt{Resid}.
paulson@6121
  2301
paulson@6121
  2302
paulson@6121
  2303
\subsection{The result structure}
paulson@6121
  2304
paulson@6121
  2305
Each (co)inductive set defined in a theory file generates an \ML\ substructure
paulson@6121
  2306
having the same name.  The the substructure contains the following elements:
paulson@6121
  2307
paulson@6121
  2308
\begin{ttbox}
paulson@6121
  2309
val intrs         : thm list  \textrm{the introduction rules}
paulson@6121
  2310
val elim          : thm       \textrm{the elimination (case analysis) rule}
paulson@6141
  2311
val mk_cases      : string -> thm  \textrm{case analysis, see below}
paulson@6121
  2312
val induct        : thm       \textrm{the standard induction rule}
paulson@6121
  2313
val mutual_induct : thm       \textrm{the mutual induction rule, or \texttt{True}}
paulson@6121
  2314
val defs          : thm list  \textrm{definitions of operators}
paulson@6121
  2315
val bnd_mono      : thm list  \textrm{monotonicity property}
paulson@6121
  2316
val dom_subset    : thm list  \textrm{inclusion in `bounding set'}
paulson@6121
  2317
\end{ttbox}
paulson@6121
  2318
Furthermore there is the theorem $C$\texttt{_I} for every constructor~$C$; for
paulson@6121
  2319
example, the \texttt{list} datatype's introduction rules are bound to the
paulson@6121
  2320
identifiers \texttt{Nil_I} and \texttt{Cons_I}.
paulson@6121
  2321
paulson@6121
  2322
For a codatatype, the component \texttt{coinduct} is the coinduction rule,
paulson@6121
  2323
replacing the \texttt{induct} component.
paulson@6121
  2324
paulson@6121
  2325
Recall that \ttindex{mk_cases} generates simplified instances of the
paulson@6121
  2326
elimination (case analysis) rule.  It is as useful for inductive definitions
paulson@6121
  2327
as it is for datatypes.  There are many examples in the theory
paulson@6121
  2328
\texttt{ex/Comb}, which is discussed at length
paulson@6121
  2329
elsewhere~\cite{paulson-generic}.  The theory first defines the datatype
paulson@6121
  2330
\texttt{comb} of combinators:
paulson@6121
  2331
\begin{ttbox}
paulson@6121
  2332
consts comb :: i
paulson@6121
  2333
datatype  "comb" = K
paulson@6121
  2334
                 | S
paulson@6121
  2335
                 | "#" ("p: comb", "q: comb")   (infixl 90)
paulson@6121
  2336
\end{ttbox}
paulson@6121
  2337
The theory goes on to define contraction and parallel contraction
paulson@6121
  2338
inductively.  Then the file \texttt{ex/Comb.ML} defines special cases of
paulson@6121
  2339
contraction using \texttt{mk_cases}:
paulson@6121
  2340
\begin{ttbox}
paulson@6141
  2341
val K_contractE = contract.mk_cases "K -1-> r";
paulson@6121
  2342
{\out val K_contractE = "K -1-> ?r ==> ?Q" : thm}
paulson@6121
  2343
\end{ttbox}
paulson@6121
  2344
We can read this as saying that the combinator \texttt{K} cannot reduce to
paulson@6121
  2345
anything.  Similar elimination rules for \texttt{S} and application are also
paulson@6121
  2346
generated and are supplied to the classical reasoner.  Note that
paulson@6121
  2347
\texttt{comb.con_defs} is given to \texttt{mk_cases} to allow freeness
paulson@6121
  2348
reasoning on datatype \texttt{comb}.
paulson@6121
  2349
paulson@6121
  2350
\index{*coinductive|)} \index{*inductive|)}
paulson@6121
  2351
paulson@6121
  2352
paulson@6121
  2353
paulson@6121
  2354
paulson@6121
  2355
\section{The outer reaches of set theory}
paulson@6121
  2356
paulson@6121
  2357
The constructions of the natural numbers and lists use a suite of
paulson@6121
  2358
operators for handling recursive function definitions.  I have described
paulson@6121
  2359
the developments in detail elsewhere~\cite{paulson-set-II}.  Here is a brief
paulson@6121
  2360
summary:
paulson@6121
  2361
\begin{itemize}
paulson@6121
  2362
  \item Theory \texttt{Trancl} defines the transitive closure of a relation
paulson@6121
  2363
    (as a least fixedpoint).
paulson@6121
  2364
paulson@6121
  2365
  \item Theory \texttt{WF} proves the Well-Founded Recursion Theorem, using an
paulson@6121
  2366
    elegant approach of Tobias Nipkow.  This theorem permits general
paulson@6121
  2367
    recursive definitions within set theory.
paulson@6121
  2368
paulson@6121
  2369
  \item Theory \texttt{Ord} defines the notions of transitive set and ordinal
paulson@6121
  2370
    number.  It derives transfinite induction.  A key definition is {\bf
paulson@6121
  2371
      less than}: $i<j$ if and only if $i$ and $j$ are both ordinals and
paulson@6121
  2372
    $i\in j$.  As a special case, it includes less than on the natural
paulson@6121
  2373
    numbers.
paulson@6121
  2374
    
paulson@6121
  2375
  \item Theory \texttt{Epsilon} derives $\varepsilon$-induction and
paulson@6121
  2376
    $\varepsilon$-recursion, which are generalisations of transfinite
paulson@6121
  2377
    induction and recursion.  It also defines \cdx{rank}$(x)$, which
paulson@6121
  2378
    is the least ordinal $\alpha$ such that $x$ is constructed at
paulson@6121
  2379
    stage $\alpha$ of the cumulative hierarchy (thus $x\in
paulson@6121
  2380
    V@{\alpha+1}$).
paulson@6121
  2381
\end{itemize}
paulson@6121
  2382
paulson@6121
  2383
Other important theories lead to a theory of cardinal numbers.  They have
paulson@6121
  2384
not yet been written up anywhere.  Here is a summary:
paulson@6121
  2385
\begin{itemize}
paulson@6121
  2386
\item Theory \texttt{Rel} defines the basic properties of relations, such as
paulson@6121
  2387
  (ir)reflexivity, (a)symmetry, and transitivity.
paulson@6121
  2388
paulson@6121
  2389
\item Theory \texttt{EquivClass} develops a theory of equivalence
paulson@6121
  2390
  classes, not using the Axiom of Choice.
paulson@6121
  2391
paulson@6121
  2392
\item Theory \texttt{Order} defines partial orderings, total orderings and
paulson@6121
  2393
  wellorderings.
paulson@6121
  2394
paulson@6121
  2395
\item Theory \texttt{OrderArith} defines orderings on sum and product sets.
paulson@6121
  2396
  These can be used to define ordinal arithmetic and have applications to
paulson@6121
  2397
  cardinal arithmetic.
paulson@6121
  2398
paulson@6121
  2399
\item Theory \texttt{OrderType} defines order types.  Every wellordering is
paulson@6121
  2400
  equivalent to a unique ordinal, which is its order type.
paulson@6121
  2401
paulson@6121
  2402
\item Theory \texttt{Cardinal} defines equipollence and cardinal numbers.
paulson@6121
  2403
 
paulson@6121
  2404
\item Theory \texttt{CardinalArith} defines cardinal addition and
paulson@6121
  2405
  multiplication, and proves their elementary laws.  It proves that there
paulson@6121
  2406
  is no greatest cardinal.  It also proves a deep result, namely
paulson@6121
  2407
  $\kappa\otimes\kappa=\kappa$ for every infinite cardinal~$\kappa$; see
paulson@6121
  2408
  Kunen~\cite[page 29]{kunen80}.  None of these results assume the Axiom of
paulson@6121
  2409
  Choice, which complicates their proofs considerably.  
paulson@6121
  2410
\end{itemize}
paulson@6121
  2411
paulson@6121
  2412
The following developments involve the Axiom of Choice (AC):
paulson@6121
  2413
\begin{itemize}
paulson@6121
  2414
\item Theory \texttt{AC} asserts the Axiom of Choice and proves some simple
paulson@6121
  2415
  equivalent forms.
paulson@6121
  2416
paulson@6121
  2417
\item Theory \texttt{Zorn} proves Hausdorff's Maximal Principle, Zorn's Lemma
paulson@6121
  2418
  and the Wellordering Theorem, following Abrial and
paulson@6121
  2419
  Laffitte~\cite{abrial93}.
paulson@6121
  2420
paulson@6121
  2421
\item Theory \verb|Cardinal_AC| uses AC to prove simplified theorems about
paulson@6121
  2422
  the cardinals.  It also proves a theorem needed to justify
paulson@6121
  2423
  infinitely branching datatype declarations: if $\kappa$ is an infinite
paulson@6121
  2424
  cardinal and $|X(\alpha)| \le \kappa$ for all $\alpha<\kappa$ then
paulson@6121
  2425
  $|\union\sb{\alpha<\kappa} X(\alpha)| \le \kappa$.
paulson@6121
  2426
paulson@6121
  2427
\item Theory \texttt{InfDatatype} proves theorems to justify infinitely
paulson@6121
  2428
  branching datatypes.  Arbitrary index sets are allowed, provided their
paulson@6121
  2429
  cardinalities have an upper bound.  The theory also justifies some
paulson@6121
  2430
  unusual cases of finite branching, involving the finite powerset operator
paulson@6121
  2431
  and the finite function space operator.
paulson@6121
  2432
\end{itemize}
paulson@6121
  2433
paulson@6121
  2434
paulson@6121
  2435
paulson@6121
  2436
\section{The examples directories}
paulson@6121
  2437
Directory \texttt{HOL/IMP} contains a mechanised version of a semantic
paulson@6121
  2438
equivalence proof taken from Winskel~\cite{winskel93}.  It formalises the
paulson@6121
  2439
denotational and operational semantics of a simple while-language, then
paulson@6121
  2440
proves the two equivalent.  It contains several datatype and inductive
paulson@6121
  2441
definitions, and demonstrates their use.
paulson@6121
  2442
paulson@6121
  2443
The directory \texttt{ZF/ex} contains further developments in {\ZF} set
paulson@6121
  2444
theory.  Here is an overview; see the files themselves for more details.  I
paulson@6121
  2445
describe much of this material in other
paulson@6121
  2446
publications~\cite{paulson-set-I,paulson-set-II,paulson-CADE}. 
paulson@6121
  2447
\begin{itemize}
paulson@6121
  2448
\item File \texttt{misc.ML} contains miscellaneous examples such as
paulson@6121
  2449
  Cantor's Theorem, the Schr\"oder-Bernstein Theorem and the `Composition
paulson@6121
  2450
  of homomorphisms' challenge~\cite{boyer86}.
paulson@6121
  2451
paulson@6121
  2452
\item Theory \texttt{Ramsey} proves the finite exponent 2 version of
paulson@6121
  2453
  Ramsey's Theorem, following Basin and Kaufmann's
paulson@6121
  2454
  presentation~\cite{basin91}.
paulson@6121
  2455
paulson@6121
  2456
\item Theory \texttt{Integ} develops a theory of the integers as
paulson@6121
  2457
  equivalence classes of pairs of natural numbers.
paulson@6121
  2458
paulson@6121
  2459
\item Theory \texttt{Primrec} develops some computation theory.  It
paulson@6121
  2460
  inductively defines the set of primitive recursive functions and presents a
paulson@6121
  2461
  proof that Ackermann's function is not primitive recursive.
paulson@6121
  2462
paulson@6121
  2463
\item Theory \texttt{Primes} defines the Greatest Common Divisor of two
paulson@6121
  2464
  natural numbers and and the ``divides'' relation.
paulson@6121
  2465
paulson@6121
  2466
\item Theory \texttt{Bin} defines a datatype for two's complement binary
paulson@6121
  2467
  integers, then proves rewrite rules to perform binary arithmetic.  For
paulson@6121
  2468
  instance, $1359\times {-}2468 = {-}3354012$ takes under 14 seconds.
paulson@6121
  2469
paulson@6121
  2470
\item Theory \texttt{BT} defines the recursive data structure ${\tt
paulson@6121
  2471
    bt}(A)$, labelled binary trees.
paulson@6121
  2472
paulson@6121
  2473
\item Theory \texttt{Term} defines a recursive data structure for terms
paulson@6121
  2474
  and term lists.  These are simply finite branching trees.
paulson@6121
  2475
paulson@6121
  2476
\item Theory \texttt{TF} defines primitives for solving mutually
paulson@6121
  2477
  recursive equations over sets.  It constructs sets of trees and forests
paulson@6121
  2478
  as an example, including induction and recursion rules that handle the
paulson@6121
  2479
  mutual recursion.
paulson@6121
  2480
paulson@6121
  2481
\item Theory \texttt{Prop} proves soundness and completeness of
paulson@6121
  2482
  propositional logic~\cite{paulson-set-II}.  This illustrates datatype
paulson@6121
  2483
  definitions, inductive definitions, structural induction and rule
paulson@6121
  2484
  induction.
paulson@6121
  2485
paulson@6121
  2486
\item Theory \texttt{ListN} inductively defines the lists of $n$
paulson@6592
  2487
  elements~\cite{paulin-tlca}.
paulson@6121
  2488
paulson@6121
  2489
\item Theory \texttt{Acc} inductively defines the accessible part of a
paulson@6592
  2490
  relation~\cite{paulin-tlca}.
paulson@6121
  2491
paulson@6121
  2492
\item Theory \texttt{Comb} defines the datatype of combinators and
paulson@6121
  2493
  inductively defines contraction and parallel contraction.  It goes on to
paulson@6121
  2494
  prove the Church-Rosser Theorem.  This case study follows Camilleri and
paulson@6121
  2495
  Melham~\cite{camilleri92}.
paulson@6121
  2496
paulson@6121
  2497
\item Theory \texttt{LList} defines lazy lists and a coinduction
paulson@6121
  2498
  principle for proving equations between them.
paulson@6121
  2499
\end{itemize}
paulson@6121
  2500
paulson@6121
  2501
paulson@6121
  2502
\section{A proof about powersets}\label{sec:ZF-pow-example}
paulson@6121
  2503
To demonstrate high-level reasoning about subsets, let us prove the
paulson@6121
  2504
equation ${{\tt Pow}(A)\cap {\tt Pow}(B)}= {\tt Pow}(A\cap B)$.  Compared
paulson@6121
  2505
with first-order logic, set theory involves a maze of rules, and theorems
paulson@6121
  2506
have many different proofs.  Attempting other proofs of the theorem might
paulson@6121
  2507
be instructive.  This proof exploits the lattice properties of
paulson@6121
  2508
intersection.  It also uses the monotonicity of the powerset operation,
paulson@6121
  2509
from \texttt{ZF/mono.ML}:
paulson@6121
  2510
\begin{ttbox}
paulson@6121
  2511
\tdx{Pow_mono}      A<=B ==> Pow(A) <= Pow(B)
paulson@6121
  2512
\end{ttbox}
paulson@6121
  2513
We enter the goal and make the first step, which breaks the equation into
paulson@6121
  2514
two inclusions by extensionality:\index{*equalityI theorem}
paulson@6121
  2515
\begin{ttbox}
paulson@6121
  2516
Goal "Pow(A Int B) = Pow(A) Int Pow(B)";
paulson@6121
  2517
{\out Level 0}
paulson@6121
  2518
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2519
{\out  1. Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2520
\ttbreak
paulson@6121
  2521
by (resolve_tac [equalityI] 1);
paulson@6121
  2522
{\out Level 1}
paulson@6121
  2523
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2524
{\out  1. Pow(A Int B) <= Pow(A) Int Pow(B)}
paulson@6121
  2525
{\out  2. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2526
\end{ttbox}
paulson@6121
  2527
Both inclusions could be tackled straightforwardly using \texttt{subsetI}.
paulson@6121
  2528
A shorter proof results from noting that intersection forms the greatest
paulson@6121
  2529
lower bound:\index{*Int_greatest theorem}
paulson@6121
  2530
\begin{ttbox}
paulson@6121
  2531
by (resolve_tac [Int_greatest] 1);
paulson@6121
  2532
{\out Level 2}
paulson@6121
  2533
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2534
{\out  1. Pow(A Int B) <= Pow(A)}
paulson@6121
  2535
{\out  2. Pow(A Int B) <= Pow(B)}
paulson@6121
  2536
{\out  3. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2537
\end{ttbox}
paulson@6121
  2538
Subgoal~1 follows by applying the monotonicity of \texttt{Pow} to $A\int
paulson@6121
  2539
B\subseteq A$; subgoal~2 follows similarly:
paulson@6121
  2540
\index{*Int_lower1 theorem}\index{*Int_lower2 theorem}
paulson@6121
  2541
\begin{ttbox}
paulson@6121
  2542
by (resolve_tac [Int_lower1 RS Pow_mono] 1);
paulson@6121
  2543
{\out Level 3}
paulson@6121
  2544
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2545
{\out  1. Pow(A Int B) <= Pow(B)}
paulson@6121
  2546
{\out  2. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2547
\ttbreak
paulson@6121
  2548
by (resolve_tac [Int_lower2 RS Pow_mono] 1);
paulson@6121
  2549
{\out Level 4}
paulson@6121
  2550
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2551
{\out  1. Pow(A) Int Pow(B) <= Pow(A Int B)}
paulson@6121
  2552
\end{ttbox}
paulson@6121
  2553
We are left with the opposite inclusion, which we tackle in the
paulson@6121
  2554
straightforward way:\index{*subsetI theorem}
paulson@6121
  2555
\begin{ttbox}
paulson@6121
  2556
by (resolve_tac [subsetI] 1);
paulson@6121
  2557
{\out Level 5}
paulson@6121
  2558
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2559
{\out  1. !!x. x : Pow(A) Int Pow(B) ==> x : Pow(A Int B)}
paulson@6121
  2560
\end{ttbox}
paulson@6121
  2561
The subgoal is to show $x\in {\tt Pow}(A\cap B)$ assuming $x\in{\tt
paulson@6121
  2562
Pow}(A)\cap {\tt Pow}(B)$; eliminating this assumption produces two
paulson@6121
  2563
subgoals.  The rule \tdx{IntE} treats the intersection like a conjunction
paulson@6121
  2564
instead of unfolding its definition.
paulson@6121
  2565
\begin{ttbox}
paulson@6121
  2566
by (eresolve_tac [IntE] 1);
paulson@6121
  2567
{\out Level 6}
paulson@6121
  2568
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2569
{\out  1. !!x. [| x : Pow(A); x : Pow(B) |] ==> x : Pow(A Int B)}
paulson@6121
  2570
\end{ttbox}
paulson@6121
  2571
The next step replaces the \texttt{Pow} by the subset
paulson@6121
  2572
relation~($\subseteq$).\index{*PowI theorem}
paulson@6121
  2573
\begin{ttbox}
paulson@6121
  2574
by (resolve_tac [PowI] 1);
paulson@6121
  2575
{\out Level 7}
paulson@6121
  2576
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2577
{\out  1. !!x. [| x : Pow(A); x : Pow(B) |] ==> x <= A Int B}
paulson@6121
  2578
\end{ttbox}
paulson@6121
  2579
We perform the same replacement in the assumptions.  This is a good
paulson@6121
  2580
demonstration of the tactic \ttindex{dresolve_tac}:\index{*PowD theorem}
paulson@6121
  2581
\begin{ttbox}
paulson@6121
  2582
by (REPEAT (dresolve_tac [PowD] 1));
paulson@6121
  2583
{\out Level 8}
paulson@6121
  2584
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2585
{\out  1. !!x. [| x <= A; x <= B |] ==> x <= A Int B}
paulson@6121
  2586
\end{ttbox}
paulson@6121
  2587
The assumptions are that $x$ is a lower bound of both $A$ and~$B$, but
paulson@6121
  2588
$A\int B$ is the greatest lower bound:\index{*Int_greatest theorem}
paulson@6121
  2589
\begin{ttbox}
paulson@6121
  2590
by (resolve_tac [Int_greatest] 1);
paulson@6121
  2591
{\out Level 9}
paulson@6121
  2592
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2593
{\out  1. !!x. [| x <= A; x <= B |] ==> x <= A}
paulson@6121
  2594
{\out  2. !!x. [| x <= A; x <= B |] ==> x <= B}
paulson@6121
  2595
\end{ttbox}
paulson@6121
  2596
To conclude the proof, we clear up the trivial subgoals:
paulson@6121
  2597
\begin{ttbox}
paulson@6121
  2598
by (REPEAT (assume_tac 1));
paulson@6121
  2599
{\out Level 10}
paulson@6121
  2600
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2601
{\out No subgoals!}
paulson@6121
  2602
\end{ttbox}
paulson@6121
  2603
\medskip
paulson@6121
  2604
We could have performed this proof in one step by applying
paulson@6121
  2605
\ttindex{Blast_tac}.  Let us
paulson@6121
  2606
go back to the start:
paulson@6121
  2607
\begin{ttbox}
paulson@6121
  2608
choplev 0;
paulson@6121
  2609
{\out Level 0}
paulson@6121
  2610
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2611
{\out  1. Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2612
by (Blast_tac 1);
paulson@6121
  2613
{\out Depth = 0}
paulson@6121
  2614
{\out Depth = 1}
paulson@6121
  2615
{\out Depth = 2}
paulson@6121
  2616
{\out Depth = 3}
paulson@6121
  2617
{\out Level 1}
paulson@6121
  2618
{\out Pow(A Int B) = Pow(A) Int Pow(B)}
paulson@6121
  2619
{\out No subgoals!}
paulson@6121
  2620
\end{ttbox}
paulson@6121
  2621
Past researchers regarded this as a difficult proof, as indeed it is if all
paulson@6121
  2622
the symbols are replaced by their definitions.
paulson@6121
  2623
\goodbreak
paulson@6121
  2624
paulson@6121
  2625
\section{Monotonicity of the union operator}
paulson@6121
  2626
For another example, we prove that general union is monotonic:
paulson@6121
  2627
${C\subseteq D}$ implies $\bigcup(C)\subseteq \bigcup(D)$.  To begin, we
paulson@6121
  2628
tackle the inclusion using \tdx{subsetI}:
paulson@6121
  2629
\begin{ttbox}
paulson@6121
  2630
Goal "C<=D ==> Union(C) <= Union(D)";
paulson@6121
  2631
{\out Level 0}
paulson@6121
  2632
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2633
{\out  1. C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2634
\ttbreak
paulson@6121
  2635
by (resolve_tac [subsetI] 1);
paulson@6121
  2636
{\out Level 1}
paulson@6121
  2637
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2638
{\out  1. !!x. [| C <= D; x : Union(C) |] ==> x : Union(D)}
paulson@6121
  2639
\end{ttbox}
paulson@6121
  2640
Big union is like an existential quantifier --- the occurrence in the
paulson@6121
  2641
assumptions must be eliminated early, since it creates parameters.
paulson@6121
  2642
\index{*UnionE theorem}
paulson@6121
  2643
\begin{ttbox}
paulson@6121
  2644
by (eresolve_tac [UnionE] 1);
paulson@6121
  2645
{\out Level 2}
paulson@6121
  2646
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2647
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> x : Union(D)}
paulson@6121
  2648
\end{ttbox}
paulson@6121
  2649
Now we may apply \tdx{UnionI}, which creates an unknown involving the
paulson@6121
  2650
parameters.  To show $x\in \bigcup(D)$ it suffices to show that $x$ belongs
paulson@6121
  2651
to some element, say~$\Var{B2}(x,B)$, of~$D$.
paulson@6121
  2652
\begin{ttbox}
paulson@6121
  2653
by (resolve_tac [UnionI] 1);
paulson@6121
  2654
{\out Level 3}
paulson@6121
  2655
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2656
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> ?B2(x,B) : D}
paulson@6121
  2657
{\out  2. !!x B. [| C <= D; x : B; B : C |] ==> x : ?B2(x,B)}
paulson@6121
  2658
\end{ttbox}
paulson@6121
  2659
Combining \tdx{subsetD} with the assumption $C\subseteq D$ yields 
paulson@6121
  2660
$\Var{a}\in C \Imp \Var{a}\in D$, which reduces subgoal~1.  Note that
paulson@6121
  2661
\texttt{eresolve_tac} has removed that assumption.
paulson@6121
  2662
\begin{ttbox}
paulson@6121
  2663
by (eresolve_tac [subsetD] 1);
paulson@6121
  2664
{\out Level 4}
paulson@6121
  2665
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2666
{\out  1. !!x B. [| x : B; B : C |] ==> ?B2(x,B) : C}
paulson@6121
  2667
{\out  2. !!x B. [| C <= D; x : B; B : C |] ==> x : ?B2(x,B)}
paulson@6121
  2668
\end{ttbox}
paulson@6121
  2669
The rest is routine.  Observe how~$\Var{B2}(x,B)$ is instantiated.
paulson@6121
  2670
\begin{ttbox}
paulson@6121
  2671
by (assume_tac 1);
paulson@6121
  2672
{\out Level 5}
paulson@6121
  2673
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2674
{\out  1. !!x B. [| C <= D; x : B; B : C |] ==> x : B}
paulson@6121
  2675
by (assume_tac 1);
paulson@6121
  2676
{\out Level 6}
paulson@6121
  2677
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2678
{\out No subgoals!}
paulson@6121
  2679
\end{ttbox}
paulson@6121
  2680
Again, \ttindex{Blast_tac} can prove the theorem in one step.
paulson@6121
  2681
\begin{ttbox}
paulson@6121
  2682
by (Blast_tac 1);
paulson@6121
  2683
{\out Depth = 0}
paulson@6121
  2684
{\out Depth = 1}
paulson@6121
  2685
{\out Depth = 2}
paulson@6121
  2686
{\out Level 1}
paulson@6121
  2687
{\out C <= D ==> Union(C) <= Union(D)}
paulson@6121
  2688
{\out No subgoals!}
paulson@6121
  2689
\end{ttbox}
paulson@6121
  2690
paulson@6121
  2691
The file \texttt{ZF/equalities.ML} has many similar proofs.  Reasoning about
paulson@6121
  2692
general intersection can be difficult because of its anomalous behaviour on
paulson@6121
  2693
the empty set.  However, \ttindex{Blast_tac} copes well with these.  Here is
paulson@6121
  2694
a typical example, borrowed from Devlin~\cite[page 12]{devlin79}:
paulson@6121
  2695
\begin{ttbox}
paulson@6121
  2696
a:C ==> (INT x:C. A(x) Int B(x)) = (INT x:C. A(x)) Int (INT x:C. B(x))
paulson@6121
  2697
\end{ttbox}
paulson@6121
  2698
In traditional notation this is
paulson@6121
  2699
\[ a\in C \,\Imp\, \inter@{x\in C} \Bigl(A(x) \int B(x)\Bigr) =        
paulson@6121
  2700
       \Bigl(\inter@{x\in C} A(x)\Bigr)  \int  
paulson@6121
  2701
       \Bigl(\inter@{x\in C} B(x)\Bigr)  \]
paulson@6121
  2702
paulson@6121
  2703
\section{Low-level reasoning about functions}
paulson@6121
  2704
The derived rules \texttt{lamI}, \texttt{lamE}, \texttt{lam_type}, \texttt{beta}
paulson@6121
  2705
and \texttt{eta} support reasoning about functions in a
paulson@6121
  2706
$\lambda$-calculus style.  This is generally easier than regarding
paulson@6121
  2707
functions as sets of ordered pairs.  But sometimes we must look at the
paulson@6121
  2708
underlying representation, as in the following proof
paulson@6121
  2709
of~\tdx{fun_disjoint_apply1}.  This states that if $f$ and~$g$ are
paulson@6121
  2710
functions with disjoint domains~$A$ and~$C$, and if $a\in A$, then
paulson@6121
  2711
$(f\un g)`a = f`a$:
paulson@6121
  2712
\begin{ttbox}
paulson@6121
  2713
Goal "[| a:A;  f: A->B;  g: C->D;  A Int C = 0 |] ==>  \ttback
paulson@6121
  2714
\ttback    (f Un g)`a = f`a";
paulson@6121
  2715
{\out Level 0}
paulson@6121
  2716
{\out [| a : A; f : A -> B; g : C -> D; A Int C = 0 |]}
paulson@6121
  2717
{\out ==> (f Un g) ` a = f ` a}
paulson@6121
  2718
{\out  1. [| a : A; f : A -> B; g : C -> D; A Int C = 0 |]}
paulson@6121
  2719
{\out     ==> (f Un g) ` a = f ` a}
paulson@6121
  2720
\end{ttbox}
paulson@6121
  2721
Using \tdx{apply_equality}, we reduce the equality to reasoning about
paulson@6121
  2722
ordered pairs.  The second subgoal is to verify that $f\un g$ is a function.
paulson@6121
  2723
To save space, the assumptions will be abbreviated below.
paulson@6121
  2724
\begin{ttbox}
paulson@6121
  2725
by (resolve_tac [apply_equality] 1);
paulson@6121
  2726
{\out Level 1}
paulson@6121
  2727
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2728
{\out  1. [| \ldots |] ==> <a,f ` a> : f Un g}
paulson@6121
  2729
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2730
\end{ttbox}
paulson@6121
  2731
We must show that the pair belongs to~$f$ or~$g$; by~\tdx{UnI1} we
paulson@6121
  2732
choose~$f$:
paulson@6121
  2733
\begin{ttbox}
paulson@6121
  2734
by (resolve_tac [UnI1] 1);
paulson@6121
  2735
{\out Level 2}
paulson@6121
  2736
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2737
{\out  1. [| \ldots |] ==> <a,f ` a> : f}
paulson@6121
  2738
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2739
\end{ttbox}
paulson@6121
  2740
To show $\pair{a,f`a}\in f$ we use \tdx{apply_Pair}, which is
paulson@6121
  2741
essentially the converse of \tdx{apply_equality}:
paulson@6121
  2742
\begin{ttbox}
paulson@6121
  2743
by (resolve_tac [apply_Pair] 1);
paulson@6121
  2744
{\out Level 3}
paulson@6121
  2745
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2746
{\out  1. [| \ldots |] ==> f : (PROD x:?A2. ?B2(x))}
paulson@6121
  2747
{\out  2. [| \ldots |] ==> a : ?A2}
paulson@6121
  2748
{\out  3. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2749
\end{ttbox}
paulson@6121
  2750
Using the assumptions $f\in A\to B$ and $a\in A$, we solve the two subgoals
paulson@6121
  2751
from \tdx{apply_Pair}.  Recall that a $\Pi$-set is merely a generalized
paulson@6121
  2752
function space, and observe that~{\tt?A2} is instantiated to~\texttt{A}.
paulson@6121
  2753
\begin{ttbox}
paulson@6121
  2754
by (assume_tac 1);
paulson@6121
  2755
{\out Level 4}
paulson@6121
  2756
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2757
{\out  1. [| \ldots |] ==> a : A}
paulson@6121
  2758
{\out  2. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2759
by (assume_tac 1);
paulson@6121
  2760
{\out Level 5}
paulson@6121
  2761
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2762
{\out  1. [| \ldots |] ==> f Un g : (PROD x:?A. ?B(x))}
paulson@6121
  2763
\end{ttbox}
paulson@6121
  2764
To construct functions of the form $f\un g$, we apply
paulson@6121
  2765
\tdx{fun_disjoint_Un}:
paulson@6121
  2766
\begin{ttbox}
paulson@6121
  2767
by (resolve_tac [fun_disjoint_Un] 1);
paulson@6121
  2768
{\out Level 6}
paulson@6121
  2769
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2770
{\out  1. [| \ldots |] ==> f : ?A3 -> ?B3}
paulson@6121
  2771
{\out  2. [| \ldots |] ==> g : ?C3 -> ?D3}
paulson@6121
  2772
{\out  3. [| \ldots |] ==> ?A3 Int ?C3 = 0}
paulson@6121
  2773
\end{ttbox}
paulson@6121
  2774
The remaining subgoals are instances of the assumptions.  Again, observe how
paulson@6121
  2775
unknowns are instantiated:
paulson@6121
  2776
\begin{ttbox}
paulson@6121
  2777
by (assume_tac 1);
paulson@6121
  2778
{\out Level 7}
paulson@6121
  2779
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2780
{\out  1. [| \ldots |] ==> g : ?C3 -> ?D3}
paulson@6121
  2781
{\out  2. [| \ldots |] ==> A Int ?C3 = 0}
paulson@6121
  2782
by (assume_tac 1);
paulson@6121
  2783
{\out Level 8}
paulson@6121
  2784
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2785
{\out  1. [| \ldots |] ==> A Int C = 0}
paulson@6121
  2786
by (assume_tac 1);
paulson@6121
  2787
{\out Level 9}
paulson@6121
  2788
{\out [| \ldots |] ==> (f Un g) ` a = f ` a}
paulson@6121
  2789
{\out No subgoals!}
paulson@6121
  2790
\end{ttbox}
paulson@6121
  2791
See the files \texttt{ZF/func.ML} and \texttt{ZF/WF.ML} for more
paulson@6121
  2792
examples of reasoning about functions.
paulson@6121
  2793
paulson@6121
  2794
\index{set theory|)}