doc-src/Inductive/ind-defs.tex
author paulson
Thu, 20 Nov 1997 10:50:51 +0100
changeset 4239 8c98484ef66f
parent 3162 78fa85d44e68
child 4265 70fc6e05120c
permissions -rw-r--r--
No more makeatletter/other
wenzelm@3162
     1
\documentclass[12pt]{article}
paulson@4239
     2
\usepackage{a4,latexsym,../iman,../extra,../proof}
wenzelm@3162
     3
wenzelm@3162
     4
\newif\ifshort%''Short'' means a published version, not the documentation
wenzelm@3162
     5
\shortfalse%%%%%\shorttrue
wenzelm@3162
     6
wenzelm@3162
     7
\title{A Fixedpoint Approach to\\ 
wenzelm@3162
     8
  (Co)Inductive and (Co)Datatype Definitions%
wenzelm@3162
     9
  \thanks{J. Grundy and S. Thompson made detailed comments.  Mads Tofte and
wenzelm@3162
    10
    the referees were also helpful.  The research was funded by the SERC
wenzelm@3162
    11
    grants GR/G53279, GR/H40570 and by the ESPRIT Project 6453 ``Types''.}}
wenzelm@3162
    12
wenzelm@3162
    13
\author{Lawrence C. Paulson\\{\tt lcp@cl.cam.ac.uk}\\
wenzelm@3162
    14
        Computer Laboratory, University of Cambridge, England}
wenzelm@3162
    15
\date{\today} 
wenzelm@3162
    16
\setcounter{secnumdepth}{2} \setcounter{tocdepth}{2}
wenzelm@3162
    17
wenzelm@3162
    18
\newcommand\sbs{\subseteq}
wenzelm@3162
    19
\let\To=\Rightarrow
wenzelm@3162
    20
wenzelm@3162
    21
%\newcommand\emph[1]{{\em#1\/}}
wenzelm@3162
    22
\newcommand\defn[1]{{\bf#1}}
wenzelm@3162
    23
%\newcommand\textsc[1]{{\sc#1}}
wenzelm@3162
    24
%\newcommand\texttt[1]{{\tt#1}}
wenzelm@3162
    25
wenzelm@3162
    26
\newcommand\pow{{\cal P}}
wenzelm@3162
    27
%%%\let\pow=\wp
wenzelm@3162
    28
\newcommand\RepFun{\hbox{\tt RepFun}}
wenzelm@3162
    29
\newcommand\cons{\hbox{\tt cons}}
wenzelm@3162
    30
\def\succ{\hbox{\tt succ}}
wenzelm@3162
    31
\newcommand\split{\hbox{\tt split}}
wenzelm@3162
    32
\newcommand\fst{\hbox{\tt fst}}
wenzelm@3162
    33
\newcommand\snd{\hbox{\tt snd}}
wenzelm@3162
    34
\newcommand\converse{\hbox{\tt converse}}
wenzelm@3162
    35
\newcommand\domain{\hbox{\tt domain}}
wenzelm@3162
    36
\newcommand\range{\hbox{\tt range}}
wenzelm@3162
    37
\newcommand\field{\hbox{\tt field}}
wenzelm@3162
    38
\newcommand\lfp{\hbox{\tt lfp}}
wenzelm@3162
    39
\newcommand\gfp{\hbox{\tt gfp}}
wenzelm@3162
    40
\newcommand\id{\hbox{\tt id}}
wenzelm@3162
    41
\newcommand\trans{\hbox{\tt trans}}
wenzelm@3162
    42
\newcommand\wf{\hbox{\tt wf}}
wenzelm@3162
    43
\newcommand\nat{\hbox{\tt nat}}
wenzelm@3162
    44
\newcommand\rank{\hbox{\tt rank}}
wenzelm@3162
    45
\newcommand\univ{\hbox{\tt univ}}
wenzelm@3162
    46
\newcommand\Vrec{\hbox{\tt Vrec}}
wenzelm@3162
    47
\newcommand\Inl{\hbox{\tt Inl}}
wenzelm@3162
    48
\newcommand\Inr{\hbox{\tt Inr}}
wenzelm@3162
    49
\newcommand\case{\hbox{\tt case}}
wenzelm@3162
    50
\newcommand\lst{\hbox{\tt list}}
wenzelm@3162
    51
\newcommand\Nil{\hbox{\tt Nil}}
wenzelm@3162
    52
\newcommand\Cons{\hbox{\tt Cons}}
wenzelm@3162
    53
\newcommand\lstcase{\hbox{\tt list\_case}}
wenzelm@3162
    54
\newcommand\lstrec{\hbox{\tt list\_rec}}
wenzelm@3162
    55
\newcommand\length{\hbox{\tt length}}
wenzelm@3162
    56
\newcommand\listn{\hbox{\tt listn}}
wenzelm@3162
    57
\newcommand\acc{\hbox{\tt acc}}
wenzelm@3162
    58
\newcommand\primrec{\hbox{\tt primrec}}
wenzelm@3162
    59
\newcommand\SC{\hbox{\tt SC}}
wenzelm@3162
    60
\newcommand\CONST{\hbox{\tt CONST}}
wenzelm@3162
    61
\newcommand\PROJ{\hbox{\tt PROJ}}
wenzelm@3162
    62
\newcommand\COMP{\hbox{\tt COMP}}
wenzelm@3162
    63
\newcommand\PREC{\hbox{\tt PREC}}
wenzelm@3162
    64
wenzelm@3162
    65
\newcommand\quniv{\hbox{\tt quniv}}
wenzelm@3162
    66
\newcommand\llist{\hbox{\tt llist}}
wenzelm@3162
    67
\newcommand\LNil{\hbox{\tt LNil}}
wenzelm@3162
    68
\newcommand\LCons{\hbox{\tt LCons}}
wenzelm@3162
    69
\newcommand\lconst{\hbox{\tt lconst}}
wenzelm@3162
    70
\newcommand\lleq{\hbox{\tt lleq}}
wenzelm@3162
    71
\newcommand\map{\hbox{\tt map}}
wenzelm@3162
    72
\newcommand\term{\hbox{\tt term}}
wenzelm@3162
    73
\newcommand\Apply{\hbox{\tt Apply}}
wenzelm@3162
    74
\newcommand\termcase{\hbox{\tt term\_case}}
wenzelm@3162
    75
\newcommand\rev{\hbox{\tt rev}}
wenzelm@3162
    76
\newcommand\reflect{\hbox{\tt reflect}}
wenzelm@3162
    77
\newcommand\tree{\hbox{\tt tree}}
wenzelm@3162
    78
\newcommand\forest{\hbox{\tt forest}}
wenzelm@3162
    79
\newcommand\Part{\hbox{\tt Part}}
wenzelm@3162
    80
\newcommand\TF{\hbox{\tt tree\_forest}}
wenzelm@3162
    81
\newcommand\Tcons{\hbox{\tt Tcons}}
wenzelm@3162
    82
\newcommand\Fcons{\hbox{\tt Fcons}}
wenzelm@3162
    83
\newcommand\Fnil{\hbox{\tt Fnil}}
wenzelm@3162
    84
\newcommand\TFcase{\hbox{\tt TF\_case}}
wenzelm@3162
    85
\newcommand\Fin{\hbox{\tt Fin}}
wenzelm@3162
    86
\newcommand\QInl{\hbox{\tt QInl}}
wenzelm@3162
    87
\newcommand\QInr{\hbox{\tt QInr}}
wenzelm@3162
    88
\newcommand\qsplit{\hbox{\tt qsplit}}
wenzelm@3162
    89
\newcommand\qcase{\hbox{\tt qcase}}
wenzelm@3162
    90
\newcommand\Con{\hbox{\tt Con}}
wenzelm@3162
    91
\newcommand\data{\hbox{\tt data}}
wenzelm@3162
    92
wenzelm@3162
    93
\binperiod     %%%treat . like a binary operator
wenzelm@3162
    94
wenzelm@3162
    95
\begin{document}
wenzelm@3162
    96
\pagestyle{empty}
wenzelm@3162
    97
\begin{titlepage}
wenzelm@3162
    98
\maketitle 
wenzelm@3162
    99
\begin{abstract}
wenzelm@3162
   100
  This paper presents a fixedpoint approach to inductive definitions.
wenzelm@3162
   101
  Instead of using a syntactic test such as ``strictly positive,'' the
wenzelm@3162
   102
  approach lets definitions involve any operators that have been proved
wenzelm@3162
   103
  monotone.  It is conceptually simple, which has allowed the easy
wenzelm@3162
   104
  implementation of mutual recursion and iterated definitions.  It also
wenzelm@3162
   105
  handles coinductive definitions: simply replace the least fixedpoint by a
wenzelm@3162
   106
  greatest fixedpoint.  
wenzelm@3162
   107
wenzelm@3162
   108
  The method has been implemented in two of Isabelle's logics, \textsc{zf} set
wenzelm@3162
   109
  theory and higher-order logic.  It should be applicable to any logic in
wenzelm@3162
   110
  which the Knaster-Tarski theorem can be proved.  Examples include lists of
wenzelm@3162
   111
  $n$ elements, the accessible part of a relation and the set of primitive
wenzelm@3162
   112
  recursive functions.  One example of a coinductive definition is
wenzelm@3162
   113
  bisimulations for lazy lists.  Recursive datatypes are examined in detail,
wenzelm@3162
   114
  as well as one example of a \defn{codatatype}: lazy lists.
wenzelm@3162
   115
wenzelm@3162
   116
  The Isabelle package has been applied in several large case studies,
wenzelm@3162
   117
  including two proofs of the Church-Rosser theorem and a coinductive proof of
wenzelm@3162
   118
  semantic consistency.  The package can be trusted because it proves theorems
wenzelm@3162
   119
  from definitions, instead of asserting desired properties as axioms.
wenzelm@3162
   120
\end{abstract}
wenzelm@3162
   121
%
wenzelm@3162
   122
\bigskip
wenzelm@3162
   123
\centerline{Copyright \copyright{} \number\year{} by Lawrence C. Paulson}
wenzelm@3162
   124
\thispagestyle{empty} 
wenzelm@3162
   125
\end{titlepage}
wenzelm@3162
   126
\tableofcontents\cleardoublepage\pagestyle{plain}
wenzelm@3162
   127
wenzelm@3162
   128
\setcounter{page}{1}
wenzelm@3162
   129
wenzelm@3162
   130
\section{Introduction}
wenzelm@3162
   131
Several theorem provers provide commands for formalizing recursive data
wenzelm@3162
   132
structures, like lists and trees.  Robin Milner implemented one of the first
wenzelm@3162
   133
of these, for Edinburgh \textsc{lcf}~\cite{milner-ind}.  Given a description
wenzelm@3162
   134
of the desired data structure, Milner's package formulated appropriate
wenzelm@3162
   135
definitions and proved the characteristic theorems.  Similar is Melham's
wenzelm@3162
   136
recursive type package for the Cambridge \textsc{hol} system~\cite{melham89}.
wenzelm@3162
   137
Such data structures are called \defn{datatypes}
wenzelm@3162
   138
below, by analogy with datatype declarations in Standard~\textsc{ml}\@.
wenzelm@3162
   139
Some logics take datatypes as primitive; consider Boyer and Moore's shell
wenzelm@3162
   140
principle~\cite{bm79} and the Coq type theory~\cite{paulin-tlca}.
wenzelm@3162
   141
wenzelm@3162
   142
A datatype is but one example of an \defn{inductive definition}.  Such a
wenzelm@3162
   143
definition~\cite{aczel77} specifies the least set~$R$ \defn{closed under}
wenzelm@3162
   144
given rules: applying a rule to elements of~$R$ yields a result within~$R$.
wenzelm@3162
   145
Inductive definitions have many applications.  The collection of theorems in a
wenzelm@3162
   146
logic is inductively defined.  A structural operational
wenzelm@3162
   147
semantics~\cite{hennessy90} is an inductive definition of a reduction or
wenzelm@3162
   148
evaluation relation on programs.  A few theorem provers provide commands for
wenzelm@3162
   149
formalizing inductive definitions; these include Coq~\cite{paulin-tlca} and
wenzelm@3162
   150
again the \textsc{hol} system~\cite{camilleri92}.
wenzelm@3162
   151
wenzelm@3162
   152
The dual notion is that of a \defn{coinductive definition}.  Such a definition
wenzelm@3162
   153
specifies the greatest set~$R$ \defn{consistent with} given rules: every
wenzelm@3162
   154
element of~$R$ can be seen as arising by applying a rule to elements of~$R$.
wenzelm@3162
   155
Important examples include using bisimulation relations to formalize
wenzelm@3162
   156
equivalence of processes~\cite{milner89} or lazy functional
wenzelm@3162
   157
programs~\cite{abramsky90}.  Other examples include lazy lists and other
wenzelm@3162
   158
infinite data structures; these are called \defn{codatatypes} below.
wenzelm@3162
   159
wenzelm@3162
   160
Not all inductive definitions are meaningful.  \defn{Monotone} inductive
wenzelm@3162
   161
definitions are a large, well-behaved class.  Monotonicity can be enforced
wenzelm@3162
   162
by syntactic conditions such as ``strictly positive,'' but this could lead to
wenzelm@3162
   163
monotone definitions being rejected on the grounds of their syntactic form.
wenzelm@3162
   164
More flexible is to formalize monotonicity within the logic and allow users
wenzelm@3162
   165
to prove it.
wenzelm@3162
   166
wenzelm@3162
   167
This paper describes a package based on a fixedpoint approach.  Least
wenzelm@3162
   168
fixedpoints yield inductive definitions; greatest fixedpoints yield
wenzelm@3162
   169
coinductive definitions.  Most of the discussion below applies equally to
wenzelm@3162
   170
inductive and coinductive definitions, and most of the code is shared.  
wenzelm@3162
   171
wenzelm@3162
   172
The package supports mutual recursion and infinitely-branching datatypes and
wenzelm@3162
   173
codatatypes.  It allows use of any operators that have been proved monotone,
wenzelm@3162
   174
thus accepting all provably monotone inductive definitions, including
wenzelm@3162
   175
iterated definitions.
wenzelm@3162
   176
wenzelm@3162
   177
The package has been implemented in
wenzelm@3162
   178
Isabelle~\cite{paulson-markt,paulson-isa-book} using 
wenzelm@3162
   179
\textsc{zf} set theory \cite{paulson-set-I,paulson-set-II}; part of it has
wenzelm@3162
   180
since been ported to Isabelle/\textsc{hol} (higher-order logic).  The
wenzelm@3162
   181
recursion equations are specified as introduction rules for the mutually
wenzelm@3162
   182
recursive sets.  The package transforms these rules into a mapping over sets,
wenzelm@3162
   183
and attempts to prove that the mapping is monotonic and well-typed.  If
wenzelm@3162
   184
successful, the package makes fixedpoint definitions and proves the
wenzelm@3162
   185
introduction, elimination and (co)induction rules.  Users invoke the package
wenzelm@3162
   186
by making simple declarations in Isabelle theory files.
wenzelm@3162
   187
wenzelm@3162
   188
Most datatype packages equip the new datatype with some means of expressing
wenzelm@3162
   189
recursive functions.  This is the main omission from my package.  Its
wenzelm@3162
   190
fixedpoint operators define only recursive sets.  The Isabelle/\textsc{zf}
wenzelm@3162
   191
theory provides well-founded recursion~\cite{paulson-set-II}, which is harder
wenzelm@3162
   192
to use than structural recursion but considerably more general.
wenzelm@3162
   193
Slind~\cite{slind-tfl} has written a package to automate the definition of
wenzelm@3162
   194
well-founded recursive functions in Isabelle/\textsc{hol}.
wenzelm@3162
   195
wenzelm@3162
   196
\paragraph*{Outline.} Section~2 introduces the least and greatest fixedpoint
wenzelm@3162
   197
operators.  Section~3 discusses the form of introduction rules, mutual
wenzelm@3162
   198
recursion and other points common to inductive and coinductive definitions.
wenzelm@3162
   199
Section~4 discusses induction and coinduction rules separately.  Section~5
wenzelm@3162
   200
presents several examples, including a coinductive definition.  Section~6
wenzelm@3162
   201
describes datatype definitions.  Section~7 presents related work.
wenzelm@3162
   202
Section~8 draws brief conclusions.  \ifshort\else The appendices are simple
wenzelm@3162
   203
user's manuals for this Isabelle package.\fi
wenzelm@3162
   204
wenzelm@3162
   205
Most of the definitions and theorems shown below have been generated by the
wenzelm@3162
   206
package.  I have renamed some variables to improve readability.
wenzelm@3162
   207
 
wenzelm@3162
   208
\section{Fixedpoint operators}
wenzelm@3162
   209
In set theory, the least and greatest fixedpoint operators are defined as
wenzelm@3162
   210
follows:
wenzelm@3162
   211
\begin{eqnarray*}
wenzelm@3162
   212
   \lfp(D,h)  & \equiv & \inter\{X\sbs D. h(X)\sbs X\} \\
wenzelm@3162
   213
   \gfp(D,h)  & \equiv & \union\{X\sbs D. X\sbs h(X)\}
wenzelm@3162
   214
\end{eqnarray*}   
wenzelm@3162
   215
Let $D$ be a set.  Say that $h$ is \defn{bounded by}~$D$ if $h(D)\sbs D$, and
wenzelm@3162
   216
\defn{monotone below~$D$} if
wenzelm@3162
   217
$h(A)\sbs h(B)$ for all $A$ and $B$ such that $A\sbs B\sbs D$.  If $h$ is
wenzelm@3162
   218
bounded by~$D$ and monotone then both operators yield fixedpoints:
wenzelm@3162
   219
\begin{eqnarray*}
wenzelm@3162
   220
   \lfp(D,h)  & = & h(\lfp(D,h)) \\
wenzelm@3162
   221
   \gfp(D,h)  & = & h(\gfp(D,h)) 
wenzelm@3162
   222
\end{eqnarray*}   
wenzelm@3162
   223
These equations are instances of the Knaster-Tarski theorem, which states
wenzelm@3162
   224
that every monotonic function over a complete lattice has a
wenzelm@3162
   225
fixedpoint~\cite{davey&priestley}.  It is obvious from their definitions
wenzelm@3162
   226
that $\lfp$ must be the least fixedpoint, and $\gfp$ the greatest.
wenzelm@3162
   227
wenzelm@3162
   228
This fixedpoint theory is simple.  The Knaster-Tarski theorem is easy to
wenzelm@3162
   229
prove.  Showing monotonicity of~$h$ is trivial, in typical cases.  We must
wenzelm@3162
   230
also exhibit a bounding set~$D$ for~$h$.  Frequently this is trivial, as when
wenzelm@3162
   231
a set of theorems is (co)inductively defined over some previously existing set
wenzelm@3162
   232
of formul{\ae}.  Isabelle/\textsc{zf} provides suitable bounding sets for
wenzelm@3162
   233
infinitely-branching (co)datatype definitions; see~\S\ref{univ-sec}.  Bounding
wenzelm@3162
   234
sets are also called \defn{domains}.
wenzelm@3162
   235
wenzelm@3162
   236
The powerset operator is monotone, but by Cantor's theorem there is no
wenzelm@3162
   237
set~$A$ such that $A=\pow(A)$.  We cannot put $A=\lfp(D,\pow)$ because
wenzelm@3162
   238
there is no suitable domain~$D$.  But \S\ref{acc-sec} demonstrates
wenzelm@3162
   239
that~$\pow$ is still useful in inductive definitions.
wenzelm@3162
   240
wenzelm@3162
   241
\section{Elements of an inductive or coinductive definition}\label{basic-sec}
wenzelm@3162
   242
Consider a (co)inductive definition of the sets $R_1$, \ldots,~$R_n$, in
wenzelm@3162
   243
mutual recursion.  They will be constructed from domains $D_1$,
wenzelm@3162
   244
\ldots,~$D_n$, respectively.  The construction yields not $R_i\sbs D_i$ but
wenzelm@3162
   245
$R_i\sbs D_1+\cdots+D_n$, where $R_i$ is contained in the image of~$D_i$
wenzelm@3162
   246
under an injection.  Reasons for this are discussed
wenzelm@3162
   247
elsewhere~\cite[\S4.5]{paulson-set-II}.
wenzelm@3162
   248
wenzelm@3162
   249
The definition may involve arbitrary parameters $\vec{p}=p_1$,
wenzelm@3162
   250
\ldots,~$p_k$.  Each recursive set then has the form $R_i(\vec{p})$.  The
wenzelm@3162
   251
parameters must be identical every time they occur within a definition.  This
wenzelm@3162
   252
would appear to be a serious restriction compared with other systems such as
wenzelm@3162
   253
Coq~\cite{paulin-tlca}.  For instance, we cannot define the lists of
wenzelm@3162
   254
$n$ elements as the set $\listn(A,n)$ using rules where the parameter~$n$
wenzelm@3162
   255
varies.  Section~\ref{listn-sec} describes how to express this set using the
wenzelm@3162
   256
inductive definition package.
wenzelm@3162
   257
wenzelm@3162
   258
To avoid clutter below, the recursive sets are shown as simply $R_i$
wenzelm@3162
   259
instead of~$R_i(\vec{p})$.
wenzelm@3162
   260
wenzelm@3162
   261
\subsection{The form of the introduction rules}\label{intro-sec}
wenzelm@3162
   262
The body of the definition consists of the desired introduction rules.  The
wenzelm@3162
   263
conclusion of each rule must have the form $t\in R_i$, where $t$ is any term.
wenzelm@3162
   264
Premises typically have the same form, but they can have the more general form
wenzelm@3162
   265
$t\in M(R_i)$ or express arbitrary side-conditions.
wenzelm@3162
   266
wenzelm@3162
   267
The premise $t\in M(R_i)$ is permitted if $M$ is a monotonic operator on
wenzelm@3162
   268
sets, satisfying the rule 
wenzelm@3162
   269
\[ \infer{M(A)\sbs M(B)}{A\sbs B} \]
wenzelm@3162
   270
The user must supply the package with monotonicity rules for all such premises.
wenzelm@3162
   271
wenzelm@3162
   272
The ability to introduce new monotone operators makes the approach
wenzelm@3162
   273
flexible.  A suitable choice of~$M$ and~$t$ can express a lot.  The
wenzelm@3162
   274
powerset operator $\pow$ is monotone, and the premise $t\in\pow(R)$
wenzelm@3162
   275
expresses $t\sbs R$; see \S\ref{acc-sec} for an example.  The \emph{list of}
wenzelm@3162
   276
operator is monotone, as is easily proved by induction.  The premise
wenzelm@3162
   277
$t\in\lst(R)$ avoids having to encode the effect of~$\lst(R)$ using mutual
wenzelm@3162
   278
recursion; see \S\ref{primrec-sec} and also my earlier
wenzelm@3162
   279
paper~\cite[\S4.4]{paulson-set-II}.
wenzelm@3162
   280
wenzelm@3162
   281
Introduction rules may also contain \defn{side-conditions}.  These are
wenzelm@3162
   282
premises consisting of arbitrary formul{\ae} not mentioning the recursive
wenzelm@3162
   283
sets. Side-conditions typically involve type-checking.  One example is the
wenzelm@3162
   284
premise $a\in A$ in the following rule from the definition of lists:
wenzelm@3162
   285
\[ \infer{\Cons(a,l)\in\lst(A)}{a\in A & l\in\lst(A)} \]
wenzelm@3162
   286
wenzelm@3162
   287
\subsection{The fixedpoint definitions}
wenzelm@3162
   288
The package translates the list of desired introduction rules into a fixedpoint
wenzelm@3162
   289
definition.  Consider, as a running example, the finite powerset operator
wenzelm@3162
   290
$\Fin(A)$: the set of all finite subsets of~$A$.  It can be
wenzelm@3162
   291
defined as the least set closed under the rules
wenzelm@3162
   292
\[  \emptyset\in\Fin(A)  \qquad 
wenzelm@3162
   293
    \infer{\{a\}\un b\in\Fin(A)}{a\in A & b\in\Fin(A)} 
wenzelm@3162
   294
\]
wenzelm@3162
   295
wenzelm@3162
   296
The domain in a (co)inductive definition must be some existing set closed
wenzelm@3162
   297
under the rules.  A suitable domain for $\Fin(A)$ is $\pow(A)$, the set of all
wenzelm@3162
   298
subsets of~$A$.  The package generates the definition
wenzelm@3162
   299
\[  \Fin(A) \equiv \lfp(\pow(A), \,
wenzelm@3162
   300
  \begin{array}[t]{r@{\,}l}
wenzelm@3162
   301
      \lambda X. \{z\in\pow(A). & z=\emptyset \disj{} \\
wenzelm@3162
   302
                  &(\exists a\,b. z=\{a\}\un b\conj a\in A\conj b\in X)\})
wenzelm@3162
   303
  \end{array}
wenzelm@3162
   304
\]
wenzelm@3162
   305
The contribution of each rule to the definition of $\Fin(A)$ should be
wenzelm@3162
   306
obvious.  A coinductive definition is similar but uses $\gfp$ instead
wenzelm@3162
   307
of~$\lfp$.
wenzelm@3162
   308
wenzelm@3162
   309
The package must prove that the fixedpoint operator is applied to a
wenzelm@3162
   310
monotonic function.  If the introduction rules have the form described
wenzelm@3162
   311
above, and if the package is supplied a monotonicity theorem for every
wenzelm@3162
   312
$t\in M(R_i)$ premise, then this proof is trivial.\footnote{Due to the
wenzelm@3162
   313
  presence of logical connectives in the fixedpoint's body, the
wenzelm@3162
   314
  monotonicity proof requires some unusual rules.  These state that the
wenzelm@3162
   315
  connectives $\conj$, $\disj$ and $\exists$ preserve monotonicity with respect
wenzelm@3162
   316
  to the partial ordering on unary predicates given by $P\sqsubseteq Q$ if and
wenzelm@3162
   317
  only if $\forall x.P(x)\imp Q(x)$.}
wenzelm@3162
   318
wenzelm@3162
   319
The package returns its result as an \textsc{ml} structure, which consists of named
wenzelm@3162
   320
components; we may regard it as a record.  The result structure contains
wenzelm@3162
   321
the definitions of the recursive sets as a theorem list called {\tt defs}.
wenzelm@3162
   322
It also contains some theorems; {\tt dom\_subset} is an inclusion such as 
wenzelm@3162
   323
$\Fin(A)\sbs\pow(A)$, while {\tt bnd\_mono} asserts that the fixedpoint
wenzelm@3162
   324
definition is monotonic.
wenzelm@3162
   325
wenzelm@3162
   326
Internally the package uses the theorem {\tt unfold}, a fixedpoint equation
wenzelm@3162
   327
such as
wenzelm@3162
   328
\[
wenzelm@3162
   329
  \begin{array}[t]{r@{\,}l}
wenzelm@3162
   330
     \Fin(A) = \{z\in\pow(A). & z=\emptyset \disj{} \\
wenzelm@3162
   331
             &(\exists a\,b. z=\{a\}\un b\conj a\in A\conj b\in \Fin(A))\}
wenzelm@3162
   332
  \end{array}
wenzelm@3162
   333
\]
wenzelm@3162
   334
In order to save space, this theorem is not exported.  
wenzelm@3162
   335
wenzelm@3162
   336
wenzelm@3162
   337
\subsection{Mutual recursion} \label{mutual-sec}
wenzelm@3162
   338
In a mutually recursive definition, the domain of the fixedpoint construction
wenzelm@3162
   339
is the disjoint sum of the domain~$D_i$ of each~$R_i$, for $i=1$,
wenzelm@3162
   340
\ldots,~$n$.  The package uses the injections of the
wenzelm@3162
   341
binary disjoint sum, typically $\Inl$ and~$\Inr$, to express injections
wenzelm@3162
   342
$h_{1n}$, \ldots, $h_{nn}$ for the $n$-ary disjoint sum $D_1+\cdots+D_n$.
wenzelm@3162
   343
wenzelm@3162
   344
As discussed elsewhere \cite[\S4.5]{paulson-set-II}, Isabelle/\textsc{zf} defines the
wenzelm@3162
   345
operator $\Part$ to support mutual recursion.  The set $\Part(A,h)$
wenzelm@3162
   346
contains those elements of~$A$ having the form~$h(z)$:
wenzelm@3162
   347
\[ \Part(A,h)  \equiv \{x\in A. \exists z. x=h(z)\}. \]   
wenzelm@3162
   348
For mutually recursive sets $R_1$, \ldots,~$R_n$ with
wenzelm@3162
   349
$n>1$, the package makes $n+1$ definitions.  The first defines a set $R$ using
wenzelm@3162
   350
a fixedpoint operator. The remaining $n$ definitions have the form
wenzelm@3162
   351
\[ R_i \equiv \Part(R,h_{in}), \qquad i=1,\ldots, n.  \] 
wenzelm@3162
   352
It follows that $R=R_1\un\cdots\un R_n$, where the $R_i$ are pairwise disjoint.
wenzelm@3162
   353
wenzelm@3162
   354
wenzelm@3162
   355
\subsection{Proving the introduction rules}
wenzelm@3162
   356
The user supplies the package with the desired form of the introduction
wenzelm@3162
   357
rules.  Once it has derived the theorem {\tt unfold}, it attempts
wenzelm@3162
   358
to prove those rules.  From the user's point of view, this is the
wenzelm@3162
   359
trickiest stage; the proofs often fail.  The task is to show that the domain 
wenzelm@3162
   360
$D_1+\cdots+D_n$ of the combined set $R_1\un\cdots\un R_n$ is
wenzelm@3162
   361
closed under all the introduction rules.  This essentially involves replacing
wenzelm@3162
   362
each~$R_i$ by $D_1+\cdots+D_n$ in each of the introduction rules and
wenzelm@3162
   363
attempting to prove the result.
wenzelm@3162
   364
wenzelm@3162
   365
Consider the $\Fin(A)$ example.  After substituting $\pow(A)$ for $\Fin(A)$
wenzelm@3162
   366
in the rules, the package must prove
wenzelm@3162
   367
\[  \emptyset\in\pow(A)  \qquad 
wenzelm@3162
   368
    \infer{\{a\}\un b\in\pow(A)}{a\in A & b\in\pow(A)} 
wenzelm@3162
   369
\]
wenzelm@3162
   370
Such proofs can be regarded as type-checking the definition.\footnote{The
wenzelm@3162
   371
  Isabelle/\textsc{hol} version does not require these proofs, as \textsc{hol}
wenzelm@3162
   372
  has implicit type-checking.} The user supplies the package with
wenzelm@3162
   373
type-checking rules to apply.  Usually these are general purpose rules from
wenzelm@3162
   374
the \textsc{zf} theory.  They could however be rules specifically proved for a
wenzelm@3162
   375
particular inductive definition; sometimes this is the easiest way to get the
wenzelm@3162
   376
definition through!
wenzelm@3162
   377
wenzelm@3162
   378
The result structure contains the introduction rules as the theorem list {\tt
wenzelm@3162
   379
intrs}.
wenzelm@3162
   380
wenzelm@3162
   381
\subsection{The case analysis rule}
wenzelm@3162
   382
The elimination rule, called {\tt elim}, performs case analysis.  It is a
wenzelm@3162
   383
simple consequence of {\tt unfold}.  There is one case for each introduction
wenzelm@3162
   384
rule.  If $x\in\Fin(A)$ then either $x=\emptyset$ or else $x=\{a\}\un b$ for
wenzelm@3162
   385
some $a\in A$ and $b\in\Fin(A)$.  Formally, the elimination rule for $\Fin(A)$
wenzelm@3162
   386
is written
wenzelm@3162
   387
\[ \infer{Q}{x\in\Fin(A) & \infer*{Q}{[x=\emptyset]}
wenzelm@3162
   388
                 & \infer*{Q}{[x=\{a\}\un b & a\in A &b\in\Fin(A)]_{a,b}} }
wenzelm@3162
   389
\]
wenzelm@3162
   390
The subscripted variables $a$ and~$b$ above the third premise are
wenzelm@3162
   391
eigenvariables, subject to the usual ``not free in \ldots'' proviso.
wenzelm@3162
   392
wenzelm@3162
   393
wenzelm@3162
   394
\section{Induction and coinduction rules}
wenzelm@3162
   395
Here we must consider inductive and coinductive definitions separately.  For
wenzelm@3162
   396
an inductive definition, the package returns an induction rule derived
wenzelm@3162
   397
directly from the properties of least fixedpoints, as well as a modified rule
wenzelm@3162
   398
for mutual recursion.  For a coinductive definition, the package returns a
wenzelm@3162
   399
basic coinduction rule.
wenzelm@3162
   400
wenzelm@3162
   401
\subsection{The basic induction rule}\label{basic-ind-sec}
wenzelm@3162
   402
The basic rule, called {\tt induct}, is appropriate in most situations.
wenzelm@3162
   403
For inductive definitions, it is strong rule induction~\cite{camilleri92}; for
wenzelm@3162
   404
datatype definitions (see below), it is just structural induction.  
wenzelm@3162
   405
wenzelm@3162
   406
The induction rule for an inductively defined set~$R$ has the form described
wenzelm@3162
   407
below.  For the time being, assume that $R$'s domain is not a Cartesian
wenzelm@3162
   408
product; inductively defined relations are treated slightly differently.
wenzelm@3162
   409
wenzelm@3162
   410
The major premise is $x\in R$.  There is a minor premise for each
wenzelm@3162
   411
introduction rule:
wenzelm@3162
   412
\begin{itemize}
wenzelm@3162
   413
\item If the introduction rule concludes $t\in R_i$, then the minor premise
wenzelm@3162
   414
is~$P(t)$.
wenzelm@3162
   415
wenzelm@3162
   416
\item The minor premise's eigenvariables are precisely the introduction
wenzelm@3162
   417
rule's free variables that are not parameters of~$R$.  For instance, the
wenzelm@3162
   418
eigenvariables in the $\Fin(A)$ rule below are $a$ and $b$, but not~$A$.
wenzelm@3162
   419
wenzelm@3162
   420
\item If the introduction rule has a premise $t\in R_i$, then the minor
wenzelm@3162
   421
premise discharges the assumption $t\in R_i$ and the induction
wenzelm@3162
   422
hypothesis~$P(t)$.  If the introduction rule has a premise $t\in M(R_i)$
wenzelm@3162
   423
then the minor premise discharges the single assumption
wenzelm@3162
   424
\[ t\in M(\{z\in R_i. P(z)\}). \] 
wenzelm@3162
   425
Because $M$ is monotonic, this assumption implies $t\in M(R_i)$.  The
wenzelm@3162
   426
occurrence of $P$ gives the effect of an induction hypothesis, which may be
wenzelm@3162
   427
exploited by appealing to properties of~$M$.
wenzelm@3162
   428
\end{itemize}
wenzelm@3162
   429
The induction rule for $\Fin(A)$ resembles the elimination rule shown above,
wenzelm@3162
   430
but includes an induction hypothesis:
wenzelm@3162
   431
\[ \infer{P(x)}{x\in\Fin(A) & P(\emptyset)
wenzelm@3162
   432
        & \infer*{P(\{a\}\un b)}{[a\in A & b\in\Fin(A) & P(b)]_{a,b}} }
wenzelm@3162
   433
\] 
wenzelm@3162
   434
Stronger induction rules often suggest themselves.  We can derive a rule for
wenzelm@3162
   435
$\Fin(A)$ whose third premise discharges the extra assumption $a\not\in b$.
wenzelm@3162
   436
The package provides rules for mutual induction and inductive relations.  The
wenzelm@3162
   437
Isabelle/\textsc{zf} theory also supports well-founded induction and recursion
wenzelm@3162
   438
over datatypes, by reasoning about the \defn{rank} of a
wenzelm@3162
   439
set~\cite[\S3.4]{paulson-set-II}.
wenzelm@3162
   440
wenzelm@3162
   441
wenzelm@3162
   442
\subsection{Modified induction rules}
wenzelm@3162
   443
wenzelm@3162
   444
If the domain of $R$ is a Cartesian product $A_1\times\cdots\times A_m$
wenzelm@3162
   445
(however nested), then the corresponding predicate $P_i$ takes $m$ arguments.
wenzelm@3162
   446
The major premise becomes $\pair{z_1,\ldots,z_m}\in R$ instead of $x\in R$;
wenzelm@3162
   447
the conclusion becomes $P(z_1,\ldots,z_m)$.  This simplifies reasoning about
wenzelm@3162
   448
inductively defined relations, eliminating the need to express properties of
wenzelm@3162
   449
$z_1$, \ldots,~$z_m$ as properties of the tuple $\pair{z_1,\ldots,z_m}$.
wenzelm@3162
   450
Occasionally it may require you to split up the induction variable
wenzelm@3162
   451
using {\tt SigmaE} and {\tt dom\_subset}, especially if the constant {\tt
wenzelm@3162
   452
  split} appears in the rule.
wenzelm@3162
   453
wenzelm@3162
   454
The mutual induction rule is called {\tt
wenzelm@3162
   455
mutual\_induct}.  It differs from the basic rule in two respects:
wenzelm@3162
   456
\begin{itemize}
wenzelm@3162
   457
\item Instead of a single predicate~$P$, it uses $n$ predicates $P_1$,
wenzelm@3162
   458
\ldots,~$P_n$: one for each recursive set.
wenzelm@3162
   459
wenzelm@3162
   460
\item There is no major premise such as $x\in R_i$.  Instead, the conclusion
wenzelm@3162
   461
refers to all the recursive sets:
wenzelm@3162
   462
\[ (\forall z.z\in R_1\imp P_1(z))\conj\cdots\conj
wenzelm@3162
   463
   (\forall z.z\in R_n\imp P_n(z))
wenzelm@3162
   464
\]
wenzelm@3162
   465
Proving the premises establishes $P_i(z)$ for $z\in R_i$ and $i=1$,
wenzelm@3162
   466
\ldots,~$n$.
wenzelm@3162
   467
\end{itemize}
wenzelm@3162
   468
%
wenzelm@3162
   469
If the domain of some $R_i$ is a Cartesian product, then the mutual induction
wenzelm@3162
   470
rule is modified accordingly.  The predicates are made to take $m$ separate
wenzelm@3162
   471
arguments instead of a tuple, and the quantification in the conclusion is over
wenzelm@3162
   472
the separate variables $z_1$, \ldots, $z_m$.
wenzelm@3162
   473
wenzelm@3162
   474
\subsection{Coinduction}\label{coind-sec}
wenzelm@3162
   475
A coinductive definition yields a primitive coinduction rule, with no
wenzelm@3162
   476
refinements such as those for the induction rules.  (Experience may suggest
wenzelm@3162
   477
refinements later.)  Consider the codatatype of lazy lists as an example.  For
wenzelm@3162
   478
suitable definitions of $\LNil$ and $\LCons$, lazy lists may be defined as the
wenzelm@3162
   479
greatest set consistent with the rules
wenzelm@3162
   480
\[  \LNil\in\llist(A)  \qquad 
wenzelm@3162
   481
    \infer[(-)]{\LCons(a,l)\in\llist(A)}{a\in A & l\in\llist(A)}
wenzelm@3162
   482
\]
wenzelm@3162
   483
The $(-)$ tag stresses that this is a coinductive definition.  A suitable
wenzelm@3162
   484
domain for $\llist(A)$ is $\quniv(A)$; this set is closed under the variant
wenzelm@3162
   485
forms of sum and product that are used to represent non-well-founded data
wenzelm@3162
   486
structures (see~\S\ref{univ-sec}).
wenzelm@3162
   487
wenzelm@3162
   488
The package derives an {\tt unfold} theorem similar to that for $\Fin(A)$. 
wenzelm@3162
   489
Then it proves the theorem {\tt coinduct}, which expresses that $\llist(A)$
wenzelm@3162
   490
is the greatest solution to this equation contained in $\quniv(A)$:
wenzelm@3162
   491
\[ \infer{x\in\llist(A)}{x\in X & X\sbs \quniv(A) &
wenzelm@3162
   492
    \infer*{
wenzelm@3162
   493
     \begin{array}[b]{r@{}l}
wenzelm@3162
   494
       z=\LNil\disj 
wenzelm@3162
   495
       \bigl(\exists a\,l.\, & z=\LCons(a,l) \conj a\in A \conj{}\\
wenzelm@3162
   496
                             & l\in X\un\llist(A) \bigr)
wenzelm@3162
   497
     \end{array}  }{[z\in X]_z}}
wenzelm@3162
   498
\]
wenzelm@3162
   499
This rule complements the introduction rules; it provides a means of showing
wenzelm@3162
   500
$x\in\llist(A)$ when $x$ is infinite.  For instance, if $x=\LCons(0,x)$ then
wenzelm@3162
   501
applying the rule with $X=\{x\}$ proves $x\in\llist(\nat)$.  (Here $\nat$
wenzelm@3162
   502
is the set of natural numbers.)
wenzelm@3162
   503
wenzelm@3162
   504
Having $X\un\llist(A)$ instead of simply $X$ in the third premise above
wenzelm@3162
   505
represents a slight strengthening of the greatest fixedpoint property.  I
wenzelm@3162
   506
discuss several forms of coinduction rules elsewhere~\cite{paulson-coind}.
wenzelm@3162
   507
wenzelm@3162
   508
The clumsy form of the third premise makes the rule hard to use, especially in
wenzelm@3162
   509
large definitions.  Probably a constant should be declared to abbreviate the
wenzelm@3162
   510
large disjunction, and rules derived to allow proving the separate disjuncts.
wenzelm@3162
   511
wenzelm@3162
   512
wenzelm@3162
   513
\section{Examples of inductive and coinductive definitions}\label{ind-eg-sec}
wenzelm@3162
   514
This section presents several examples from the literature: the finite
wenzelm@3162
   515
powerset operator, lists of $n$ elements, bisimulations on lazy lists, the
wenzelm@3162
   516
well-founded part of a relation, and the primitive recursive functions.
wenzelm@3162
   517
wenzelm@3162
   518
\subsection{The finite powerset operator}
wenzelm@3162
   519
This operator has been discussed extensively above.  Here is the
wenzelm@3162
   520
corresponding invocation in an Isabelle theory file.  Note that
wenzelm@3162
   521
$\cons(a,b)$ abbreviates $\{a\}\un b$ in Isabelle/\textsc{zf}.
wenzelm@3162
   522
\begin{ttbox}
wenzelm@3162
   523
Finite = Arith + 
wenzelm@3162
   524
consts      Fin :: i=>i
wenzelm@3162
   525
inductive
wenzelm@3162
   526
  domains   "Fin(A)" <= "Pow(A)"
wenzelm@3162
   527
  intrs
wenzelm@3162
   528
    emptyI  "0 : Fin(A)"
wenzelm@3162
   529
    consI   "[| a: A;  b: Fin(A) |] ==> cons(a,b) : Fin(A)"
wenzelm@3162
   530
  type_intrs "[empty_subsetI, cons_subsetI, PowI]"
wenzelm@3162
   531
  type_elims "[make_elim PowD]"
wenzelm@3162
   532
end
wenzelm@3162
   533
\end{ttbox}
wenzelm@3162
   534
Theory {\tt Finite} extends the parent theory {\tt Arith} by declaring the
wenzelm@3162
   535
unary function symbol~$\Fin$, which is defined inductively.  Its domain is
wenzelm@3162
   536
specified as $\pow(A)$, where $A$ is the parameter appearing in the
wenzelm@3162
   537
introduction rules.  For type-checking, we supply two introduction
wenzelm@3162
   538
rules:
wenzelm@3162
   539
\[ \emptyset\sbs A              \qquad
wenzelm@3162
   540
   \infer{\{a\}\un B\sbs C}{a\in C & B\sbs C}
wenzelm@3162
   541
\]
wenzelm@3162
   542
A further introduction rule and an elimination rule express both
wenzelm@3162
   543
directions of the equivalence $A\in\pow(B)\bimp A\sbs B$.  Type-checking
wenzelm@3162
   544
involves mostly introduction rules.  
wenzelm@3162
   545
wenzelm@3162
   546
Like all Isabelle theory files, this one yields a structure containing the
wenzelm@3162
   547
new theory as an \textsc{ml} value.  Structure {\tt Finite} also has a
wenzelm@3162
   548
substructure, called~{\tt Fin}.  After declaring \hbox{\tt open Finite;} we
wenzelm@3162
   549
can refer to the $\Fin(A)$ introduction rules as the list {\tt Fin.intrs}
wenzelm@3162
   550
or individually as {\tt Fin.emptyI} and {\tt Fin.consI}.  The induction
wenzelm@3162
   551
rule is {\tt Fin.induct}.
wenzelm@3162
   552
wenzelm@3162
   553
wenzelm@3162
   554
\subsection{Lists of $n$ elements}\label{listn-sec}
wenzelm@3162
   555
This has become a standard example of an inductive definition.  Following
wenzelm@3162
   556
Paulin-Mohring~\cite{paulin-tlca}, we could attempt to define a new datatype
wenzelm@3162
   557
$\listn(A,n)$, for lists of length~$n$, as an $n$-indexed family of sets.
wenzelm@3162
   558
But her introduction rules
wenzelm@3162
   559
\[ \hbox{\tt Niln}\in\listn(A,0)  \qquad
wenzelm@3162
   560
   \infer{\hbox{\tt Consn}(n,a,l)\in\listn(A,\succ(n))}
wenzelm@3162
   561
         {n\in\nat & a\in A & l\in\listn(A,n)}
wenzelm@3162
   562
\]
wenzelm@3162
   563
are not acceptable to the inductive definition package:
wenzelm@3162
   564
$\listn$ occurs with three different parameter lists in the definition.
wenzelm@3162
   565
wenzelm@3162
   566
The Isabelle version of this example suggests a general treatment of
wenzelm@3162
   567
varying parameters.  It uses the existing datatype definition of
wenzelm@3162
   568
$\lst(A)$, with constructors $\Nil$ and~$\Cons$, and incorporates the
wenzelm@3162
   569
parameter~$n$ into the inductive set itself.  It defines $\listn(A)$ as a
wenzelm@3162
   570
relation consisting of pairs $\pair{n,l}$ such that $n\in\nat$
wenzelm@3162
   571
and~$l\in\lst(A)$ and $l$ has length~$n$.  In fact, $\listn(A)$ is the
wenzelm@3162
   572
converse of the length function on~$\lst(A)$.  The Isabelle/\textsc{zf} introduction
wenzelm@3162
   573
rules are
wenzelm@3162
   574
\[ \pair{0,\Nil}\in\listn(A)  \qquad
wenzelm@3162
   575
   \infer{\pair{\succ(n),\Cons(a,l)}\in\listn(A)}
wenzelm@3162
   576
         {a\in A & \pair{n,l}\in\listn(A)}
wenzelm@3162
   577
\]
wenzelm@3162
   578
The Isabelle theory file takes, as parent, the theory~{\tt List} of lists.
wenzelm@3162
   579
We declare the constant~$\listn$ and supply an inductive definition,
wenzelm@3162
   580
specifying the domain as $\nat\times\lst(A)$:
wenzelm@3162
   581
\begin{ttbox}
wenzelm@3162
   582
ListN = List +
wenzelm@3162
   583
consts  listn :: i=>i
wenzelm@3162
   584
inductive
wenzelm@3162
   585
  domains   "listn(A)" <= "nat*list(A)"
wenzelm@3162
   586
  intrs
wenzelm@3162
   587
    NilI  "<0,Nil>: listn(A)"
wenzelm@3162
   588
    ConsI "[| a: A;  <n,l>: listn(A) |] ==> <succ(n), Cons(a,l)>: listn(A)"
wenzelm@3162
   589
  type_intrs "nat_typechecks @ list.intrs"
wenzelm@3162
   590
end
wenzelm@3162
   591
\end{ttbox}
wenzelm@3162
   592
The type-checking rules include those for 0, $\succ$, $\Nil$ and $\Cons$.
wenzelm@3162
   593
Because $\listn(A)$ is a set of pairs, type-checking requires the
wenzelm@3162
   594
equivalence $\pair{a,b}\in A\times B \bimp a\in A \conj b\in B$.  The
wenzelm@3162
   595
package always includes the rules for ordered pairs.
wenzelm@3162
   596
wenzelm@3162
   597
The package returns introduction, elimination and induction rules for
wenzelm@3162
   598
$\listn$.  The basic induction rule, {\tt listn.induct}, is
wenzelm@3162
   599
\[ \infer{P(z_1,z_2)}{\pair{z_1,z_2}\in\listn(A) & P(0,\Nil) &
wenzelm@3162
   600
             \infer*{P(\succ(n),\Cons(a,l))}
wenzelm@3162
   601
                {[a\in A & \pair{n,l}\in\listn(A) & P(n,l)]_{a,l,n}}}
wenzelm@3162
   602
\]
wenzelm@3162
   603
This rule lets the induction formula to be a 
wenzelm@3162
   604
binary property of pairs, $P(n,l)$.  
wenzelm@3162
   605
It is now a simple matter to prove theorems about $\listn(A)$, such as
wenzelm@3162
   606
\[ \forall l\in\lst(A). \pair{\length(l),\, l}\in\listn(A) \]
wenzelm@3162
   607
\[ \listn(A)``\{n\} = \{l\in\lst(A). \length(l)=n\} \]
wenzelm@3162
   608
This latter result --- here $r``X$ denotes the image of $X$ under $r$
wenzelm@3162
   609
--- asserts that the inductive definition agrees with the obvious notion of
wenzelm@3162
   610
$n$-element list.  
wenzelm@3162
   611
wenzelm@3162
   612
A ``list of $n$ elements'' really is a list, namely an element of ~$\lst(A)$.
wenzelm@3162
   613
It is subject to list operators such as append (concatenation).  For example,
wenzelm@3162
   614
a trivial induction on $\pair{m,l}\in\listn(A)$ yields
wenzelm@3162
   615
\[ \infer{\pair{m\mathbin{+} m',\, l@l'}\in\listn(A)}
wenzelm@3162
   616
         {\pair{m,l}\in\listn(A) & \pair{m',l'}\in\listn(A)} 
wenzelm@3162
   617
\]
wenzelm@3162
   618
where $+$ denotes addition on the natural numbers and @ denotes append.
wenzelm@3162
   619
wenzelm@3162
   620
\subsection{Rule inversion: the function {\tt mk\_cases}}
wenzelm@3162
   621
The elimination rule, {\tt listn.elim}, is cumbersome:
wenzelm@3162
   622
\[ \infer{Q}{x\in\listn(A) & 
wenzelm@3162
   623
          \infer*{Q}{[x = \pair{0,\Nil}]} &
wenzelm@3162
   624
          \infer*{Q}
wenzelm@3162
   625
             {\left[\begin{array}{l}
wenzelm@3162
   626
               x = \pair{\succ(n),\Cons(a,l)} \\
wenzelm@3162
   627
               a\in A \\
wenzelm@3162
   628
               \pair{n,l}\in\listn(A)
wenzelm@3162
   629
               \end{array} \right]_{a,l,n}}}
wenzelm@3162
   630
\]
wenzelm@3162
   631
The \textsc{ml} function {\tt listn.mk\_cases} generates simplified instances of
wenzelm@3162
   632
this rule.  It works by freeness reasoning on the list constructors:
wenzelm@3162
   633
$\Cons(a,l)$ is injective in its two arguments and differs from~$\Nil$.  If
wenzelm@3162
   634
$x$ is $\pair{i,\Nil}$ or $\pair{i,\Cons(a,l)}$ then {\tt listn.mk\_cases}
wenzelm@3162
   635
deduces the corresponding form of~$i$;  this is called rule inversion.  
wenzelm@3162
   636
Here is a sample session: 
wenzelm@3162
   637
\begin{ttbox}
wenzelm@3162
   638
listn.mk_cases list.con_defs "<i,Nil> : listn(A)";
wenzelm@3162
   639
{\out "[| <?i, []> : listn(?A); ?i = 0 ==> ?Q |] ==> ?Q" : thm}
wenzelm@3162
   640
listn.mk_cases list.con_defs "<i,Cons(a,l)> : listn(A)";
wenzelm@3162
   641
{\out "[| <?i, Cons(?a, ?l)> : listn(?A);}
wenzelm@3162
   642
{\out     !!n. [| ?a : ?A; <n, ?l> : listn(?A); ?i = succ(n) |] ==> ?Q }
wenzelm@3162
   643
{\out  |] ==> ?Q" : thm}
wenzelm@3162
   644
\end{ttbox}
wenzelm@3162
   645
Each of these rules has only two premises.  In conventional notation, the
wenzelm@3162
   646
second rule is
wenzelm@3162
   647
\[ \infer{Q}{\pair{i, \Cons(a,l)}\in\listn(A) & 
wenzelm@3162
   648
          \infer*{Q}
wenzelm@3162
   649
             {\left[\begin{array}{l}
wenzelm@3162
   650
               a\in A \\ \pair{n,l}\in\listn(A) \\ i = \succ(n)
wenzelm@3162
   651
               \end{array} \right]_{n}}}
wenzelm@3162
   652
\]
wenzelm@3162
   653
The package also has built-in rules for freeness reasoning about $0$
wenzelm@3162
   654
and~$\succ$.  So if $x$ is $\pair{0,l}$ or $\pair{\succ(i),l}$, then {\tt
wenzelm@3162
   655
listn.mk\_cases} can deduce the corresponding form of~$l$. 
wenzelm@3162
   656
wenzelm@3162
   657
The function {\tt mk\_cases} is also useful with datatype definitions.  The
wenzelm@3162
   658
instance from the definition of lists, namely {\tt list.mk\_cases}, can
wenzelm@3162
   659
prove that $\Cons(a,l)\in\lst(A)$ implies $a\in A $ and $l\in\lst(A)$:
wenzelm@3162
   660
\[ \infer{Q}{\Cons(a,l)\in\lst(A) & 
wenzelm@3162
   661
                 & \infer*{Q}{[a\in A &l\in\lst(A)]} }
wenzelm@3162
   662
\]
wenzelm@3162
   663
A typical use of {\tt mk\_cases} concerns inductive definitions of evaluation
wenzelm@3162
   664
relations.  Then rule inversion yields case analysis on possible evaluations.
wenzelm@3162
   665
For example, Isabelle/\textsc{zf} includes a short proof of the
wenzelm@3162
   666
diamond property for parallel contraction on combinators.  Ole Rasmussen used
wenzelm@3162
   667
{\tt mk\_cases} extensively in his development of the theory of
wenzelm@3162
   668
residuals~\cite{rasmussen95}.
wenzelm@3162
   669
wenzelm@3162
   670
wenzelm@3162
   671
\subsection{A coinductive definition: bisimulations on lazy lists}
wenzelm@3162
   672
This example anticipates the definition of the codatatype $\llist(A)$, which
wenzelm@3162
   673
consists of finite and infinite lists over~$A$.  Its constructors are $\LNil$
wenzelm@3162
   674
and~$\LCons$, satisfying the introduction rules shown in~\S\ref{coind-sec}.  
wenzelm@3162
   675
Because $\llist(A)$ is defined as a greatest fixedpoint and uses the variant
wenzelm@3162
   676
pairing and injection operators, it contains non-well-founded elements such as
wenzelm@3162
   677
solutions to $\LCons(a,l)=l$.
wenzelm@3162
   678
wenzelm@3162
   679
The next step in the development of lazy lists is to define a coinduction
wenzelm@3162
   680
principle for proving equalities.  This is done by showing that the equality
wenzelm@3162
   681
relation on lazy lists is the greatest fixedpoint of some monotonic
wenzelm@3162
   682
operation.  The usual approach~\cite{pitts94} is to define some notion of 
wenzelm@3162
   683
bisimulation for lazy lists, define equivalence to be the greatest
wenzelm@3162
   684
bisimulation, and finally to prove that two lazy lists are equivalent if and
wenzelm@3162
   685
only if they are equal.  The coinduction rule for equivalence then yields a
wenzelm@3162
   686
coinduction principle for equalities.
wenzelm@3162
   687
wenzelm@3162
   688
A binary relation $R$ on lazy lists is a \defn{bisimulation} provided $R\sbs
wenzelm@3162
   689
R^+$, where $R^+$ is the relation
wenzelm@3162
   690
\[ \{\pair{\LNil,\LNil}\} \un 
wenzelm@3162
   691
   \{\pair{\LCons(a,l),\LCons(a,l')} . a\in A \conj \pair{l,l'}\in R\}.
wenzelm@3162
   692
\]
wenzelm@3162
   693
A pair of lazy lists are \defn{equivalent} if they belong to some
wenzelm@3162
   694
bisimulation.  Equivalence can be coinductively defined as the greatest
wenzelm@3162
   695
fixedpoint for the introduction rules
wenzelm@3162
   696
\[  \pair{\LNil,\LNil} \in\lleq(A)  \qquad 
wenzelm@3162
   697
    \infer[(-)]{\pair{\LCons(a,l),\LCons(a,l')} \in\lleq(A)}
wenzelm@3162
   698
          {a\in A & \pair{l,l'}\in \lleq(A)}
wenzelm@3162
   699
\]
wenzelm@3162
   700
To make this coinductive definition, the theory file includes (after the
wenzelm@3162
   701
declaration of $\llist(A)$) the following lines:
wenzelm@3162
   702
\begin{ttbox}
wenzelm@3162
   703
consts    lleq :: i=>i
wenzelm@3162
   704
coinductive
wenzelm@3162
   705
  domains "lleq(A)" <= "llist(A) * llist(A)"
wenzelm@3162
   706
  intrs
wenzelm@3162
   707
    LNil  "<LNil,LNil> : lleq(A)"
wenzelm@3162
   708
    LCons "[| a:A; <l,l'>: lleq(A) |] ==> <LCons(a,l),LCons(a,l')>: lleq(A)"
wenzelm@3162
   709
  type_intrs  "llist.intrs"
wenzelm@3162
   710
\end{ttbox}
wenzelm@3162
   711
The domain of $\lleq(A)$ is $\llist(A)\times\llist(A)$.  The type-checking
wenzelm@3162
   712
rules include the introduction rules for $\llist(A)$, whose 
wenzelm@3162
   713
declaration is discussed below (\S\ref{lists-sec}).
wenzelm@3162
   714
wenzelm@3162
   715
The package returns the introduction rules and the elimination rule, as
wenzelm@3162
   716
usual.  But instead of induction rules, it returns a coinduction rule.
wenzelm@3162
   717
The rule is too big to display in the usual notation; its conclusion is
wenzelm@3162
   718
$x\in\lleq(A)$ and its premises are $x\in X$, 
wenzelm@3162
   719
${X\sbs\llist(A)\times\llist(A)}$ and
wenzelm@3162
   720
\[ \infer*{z=\pair{\LNil,\LNil}\disj \bigl(\exists a\,l\,l'.\,
wenzelm@3162
   721
     \begin{array}[t]{@{}l}
wenzelm@3162
   722
       z=\pair{\LCons(a,l),\LCons(a,l')} \conj a\in A \conj{}\\
wenzelm@3162
   723
       \pair{l,l'}\in X\un\lleq(A) \bigr)
wenzelm@3162
   724
     \end{array}  
wenzelm@3162
   725
    }{[z\in X]_z}
wenzelm@3162
   726
\]
wenzelm@3162
   727
Thus if $x\in X$, where $X$ is a bisimulation contained in the
wenzelm@3162
   728
domain of $\lleq(A)$, then $x\in\lleq(A)$.  It is easy to show that
wenzelm@3162
   729
$\lleq(A)$ is reflexive: the equality relation is a bisimulation.  And
wenzelm@3162
   730
$\lleq(A)$ is symmetric: its converse is a bisimulation.  But showing that
wenzelm@3162
   731
$\lleq(A)$ coincides with the equality relation takes some work.
wenzelm@3162
   732
wenzelm@3162
   733
\subsection{The accessible part of a relation}\label{acc-sec}
wenzelm@3162
   734
Let $\prec$ be a binary relation on~$D$; in short, $(\prec)\sbs D\times D$.
wenzelm@3162
   735
The \defn{accessible} or \defn{well-founded} part of~$\prec$, written
wenzelm@3162
   736
$\acc(\prec)$, is essentially that subset of~$D$ for which $\prec$ admits
wenzelm@3162
   737
no infinite decreasing chains~\cite{aczel77}.  Formally, $\acc(\prec)$ is
wenzelm@3162
   738
inductively defined to be the least set that contains $a$ if it contains
wenzelm@3162
   739
all $\prec$-predecessors of~$a$, for $a\in D$.  Thus we need an
wenzelm@3162
   740
introduction rule of the form 
wenzelm@3162
   741
\[ \infer{a\in\acc(\prec)}{\forall y.y\prec a\imp y\in\acc(\prec)} \]
wenzelm@3162
   742
Paulin-Mohring treats this example in Coq~\cite{paulin-tlca}, but it causes
wenzelm@3162
   743
difficulties for other systems.  Its premise is not acceptable to the
wenzelm@3162
   744
inductive definition package of the Cambridge \textsc{hol}
wenzelm@3162
   745
system~\cite{camilleri92}.  It is also unacceptable to the Isabelle package
wenzelm@3162
   746
(recall \S\ref{intro-sec}), but fortunately can be transformed into the
wenzelm@3162
   747
acceptable form $t\in M(R)$.
wenzelm@3162
   748
wenzelm@3162
   749
The powerset operator is monotonic, and $t\in\pow(R)$ is equivalent to
wenzelm@3162
   750
$t\sbs R$.  This in turn is equivalent to $\forall y\in t. y\in R$.  To
wenzelm@3162
   751
express $\forall y.y\prec a\imp y\in\acc(\prec)$ we need only find a
wenzelm@3162
   752
term~$t$ such that $y\in t$ if and only if $y\prec a$.  A suitable $t$ is
wenzelm@3162
   753
the inverse image of~$\{a\}$ under~$\prec$.
wenzelm@3162
   754
wenzelm@3162
   755
The definition below follows this approach.  Here $r$ is~$\prec$ and
wenzelm@3162
   756
$\field(r)$ refers to~$D$, the domain of $\acc(r)$.  (The field of a
wenzelm@3162
   757
relation is the union of its domain and range.)  Finally $r^{-}``\{a\}$
wenzelm@3162
   758
denotes the inverse image of~$\{a\}$ under~$r$.  We supply the theorem {\tt
wenzelm@3162
   759
  Pow\_mono}, which asserts that $\pow$ is monotonic.
wenzelm@3162
   760
\begin{ttbox}
wenzelm@3162
   761
consts    acc :: i=>i
wenzelm@3162
   762
inductive
wenzelm@3162
   763
  domains "acc(r)" <= "field(r)"
wenzelm@3162
   764
  intrs
wenzelm@3162
   765
    vimage  "[| r-``\{a\}: Pow(acc(r)); a: field(r) |] ==> a: acc(r)"
wenzelm@3162
   766
  monos     "[Pow_mono]"
wenzelm@3162
   767
\end{ttbox}
wenzelm@3162
   768
The Isabelle theory proceeds to prove facts about $\acc(\prec)$.  For
wenzelm@3162
   769
instance, $\prec$ is well-founded if and only if its field is contained in
wenzelm@3162
   770
$\acc(\prec)$.  
wenzelm@3162
   771
wenzelm@3162
   772
As mentioned in~\S\ref{basic-ind-sec}, a premise of the form $t\in M(R)$
wenzelm@3162
   773
gives rise to an unusual induction hypothesis.  Let us examine the
wenzelm@3162
   774
induction rule, {\tt acc.induct}:
wenzelm@3162
   775
\[ \infer{P(x)}{x\in\acc(r) &
wenzelm@3162
   776
     \infer*{P(a)}{\left[
wenzelm@3162
   777
                   \begin{array}{r@{}l}
wenzelm@3162
   778
                     r^{-}``\{a\} &\, \in\pow(\{z\in\acc(r).P(z)\}) \\
wenzelm@3162
   779
                                a &\, \in\field(r)
wenzelm@3162
   780
                   \end{array}
wenzelm@3162
   781
                   \right]_a}}
wenzelm@3162
   782
\]
wenzelm@3162
   783
The strange induction hypothesis is equivalent to
wenzelm@3162
   784
$\forall y. \pair{y,a}\in r\imp y\in\acc(r)\conj P(y)$.
wenzelm@3162
   785
Therefore the rule expresses well-founded induction on the accessible part
wenzelm@3162
   786
of~$\prec$.
wenzelm@3162
   787
wenzelm@3162
   788
The use of inverse image is not essential.  The Isabelle package can accept
wenzelm@3162
   789
introduction rules with arbitrary premises of the form $\forall
wenzelm@3162
   790
\vec{y}.P(\vec{y})\imp f(\vec{y})\in R$.  The premise can be expressed
wenzelm@3162
   791
equivalently as 
wenzelm@3162
   792
\[ \{z\in D. P(\vec{y}) \conj z=f(\vec{y})\} \in \pow(R) \] 
wenzelm@3162
   793
provided $f(\vec{y})\in D$ for all $\vec{y}$ such that~$P(\vec{y})$.  The
wenzelm@3162
   794
following section demonstrates another use of the premise $t\in M(R)$,
wenzelm@3162
   795
where $M=\lst$. 
wenzelm@3162
   796
wenzelm@3162
   797
\subsection{The primitive recursive functions}\label{primrec-sec}
wenzelm@3162
   798
The primitive recursive functions are traditionally defined inductively, as
wenzelm@3162
   799
a subset of the functions over the natural numbers.  One difficulty is that
wenzelm@3162
   800
functions of all arities are taken together, but this is easily
wenzelm@3162
   801
circumvented by regarding them as functions on lists.  Another difficulty,
wenzelm@3162
   802
the notion of composition, is less easily circumvented.
wenzelm@3162
   803
wenzelm@3162
   804
Here is a more precise definition.  Letting $\vec{x}$ abbreviate
wenzelm@3162
   805
$x_0,\ldots,x_{n-1}$, we can write lists such as $[\vec{x}]$,
wenzelm@3162
   806
$[y+1,\vec{x}]$, etc.  A function is \defn{primitive recursive} if it
wenzelm@3162
   807
belongs to the least set of functions in $\lst(\nat)\to\nat$ containing
wenzelm@3162
   808
\begin{itemize}
wenzelm@3162
   809
\item The \defn{successor} function $\SC$, such that $\SC[y,\vec{x}]=y+1$.
wenzelm@3162
   810
\item All \defn{constant} functions $\CONST(k)$, such that
wenzelm@3162
   811
  $\CONST(k)[\vec{x}]=k$. 
wenzelm@3162
   812
\item All \defn{projection} functions $\PROJ(i)$, such that
wenzelm@3162
   813
  $\PROJ(i)[\vec{x}]=x_i$ if $0\leq i<n$. 
wenzelm@3162
   814
\item All \defn{compositions} $\COMP(g,[f_0,\ldots,f_{m-1}])$, 
wenzelm@3162
   815
where $g$ and $f_0$, \ldots, $f_{m-1}$ are primitive recursive,
wenzelm@3162
   816
such that
wenzelm@3162
   817
\[ \COMP(g,[f_0,\ldots,f_{m-1}])[\vec{x}] = 
wenzelm@3162
   818
   g[f_0[\vec{x}],\ldots,f_{m-1}[\vec{x}]]. \] 
wenzelm@3162
   819
wenzelm@3162
   820
\item All \defn{recursions} $\PREC(f,g)$, where $f$ and $g$ are primitive
wenzelm@3162
   821
  recursive, such that
wenzelm@3162
   822
\begin{eqnarray*}
wenzelm@3162
   823
  \PREC(f,g)[0,\vec{x}] & = & f[\vec{x}] \\
wenzelm@3162
   824
  \PREC(f,g)[y+1,\vec{x}] & = & g[\PREC(f,g)[y,\vec{x}],\, y,\, \vec{x}].
wenzelm@3162
   825
\end{eqnarray*} 
wenzelm@3162
   826
\end{itemize}
wenzelm@3162
   827
Composition is awkward because it combines not two functions, as is usual,
wenzelm@3162
   828
but $m+1$ functions.  In her proof that Ackermann's function is not
wenzelm@3162
   829
primitive recursive, Nora Szasz was unable to formalize this definition
wenzelm@3162
   830
directly~\cite{szasz93}.  So she generalized primitive recursion to
wenzelm@3162
   831
tuple-valued functions.  This modified the inductive definition such that
wenzelm@3162
   832
each operation on primitive recursive functions combined just two functions.
wenzelm@3162
   833
wenzelm@3162
   834
\begin{figure}
wenzelm@3162
   835
\begin{ttbox}
wenzelm@3162
   836
Primrec = List +
wenzelm@3162
   837
consts
wenzelm@3162
   838
  primrec :: i
wenzelm@3162
   839
  SC      :: i
wenzelm@3162
   840
  \(\vdots\)
wenzelm@3162
   841
defs
wenzelm@3162
   842
  SC_def    "SC == lam l:list(nat).list_case(0, \%x xs.succ(x), l)"
wenzelm@3162
   843
  \(\vdots\)
wenzelm@3162
   844
inductive
wenzelm@3162
   845
  domains "primrec" <= "list(nat)->nat"
wenzelm@3162
   846
  intrs
wenzelm@3162
   847
    SC       "SC : primrec"
wenzelm@3162
   848
    CONST    "k: nat ==> CONST(k) : primrec"
wenzelm@3162
   849
    PROJ     "i: nat ==> PROJ(i) : primrec"
wenzelm@3162
   850
    COMP     "[| g: primrec; fs: list(primrec) |] ==> COMP(g,fs): primrec"
wenzelm@3162
   851
    PREC     "[| f: primrec; g: primrec |] ==> PREC(f,g): primrec"
wenzelm@3162
   852
  monos      "[list_mono]"
wenzelm@3162
   853
  con_defs   "[SC_def,CONST_def,PROJ_def,COMP_def,PREC_def]"
wenzelm@3162
   854
  type_intrs "nat_typechecks @ list.intrs @                      
wenzelm@3162
   855
              [lam_type, list_case_type, drop_type, map_type,    
wenzelm@3162
   856
               apply_type, rec_type]"
wenzelm@3162
   857
end
wenzelm@3162
   858
\end{ttbox}
wenzelm@3162
   859
\hrule
wenzelm@3162
   860
\caption{Inductive definition of the primitive recursive functions} 
wenzelm@3162
   861
\label{primrec-fig}
wenzelm@3162
   862
\end{figure}
wenzelm@3162
   863
\def\fs{{\it fs}} 
wenzelm@3162
   864
wenzelm@3162
   865
Szasz was using \textsc{alf}, but Coq and \textsc{hol} would also have
wenzelm@3162
   866
problems accepting this definition.  Isabelle's package accepts it easily
wenzelm@3162
   867
since $[f_0,\ldots,f_{m-1}]$ is a list of primitive recursive functions and
wenzelm@3162
   868
$\lst$ is monotonic.  There are five introduction rules, one for each of the
wenzelm@3162
   869
five forms of primitive recursive function.  Let us examine the one for
wenzelm@3162
   870
$\COMP$:
wenzelm@3162
   871
\[ \infer{\COMP(g,\fs)\in\primrec}{g\in\primrec & \fs\in\lst(\primrec)} \]
wenzelm@3162
   872
The induction rule for $\primrec$ has one case for each introduction rule.
wenzelm@3162
   873
Due to the use of $\lst$ as a monotone operator, the composition case has
wenzelm@3162
   874
an unusual induction hypothesis:
wenzelm@3162
   875
 \[ \infer*{P(\COMP(g,\fs))}
wenzelm@3162
   876
          {[g\in\primrec & \fs\in\lst(\{z\in\primrec.P(z)\})]_{\fs,g}} 
wenzelm@3162
   877
\]
wenzelm@3162
   878
The hypothesis states that $\fs$ is a list of primitive recursive functions,
wenzelm@3162
   879
each satisfying the induction formula.  Proving the $\COMP$ case typically
wenzelm@3162
   880
requires structural induction on lists, yielding two subcases: either
wenzelm@3162
   881
$\fs=\Nil$ or else $\fs=\Cons(f,\fs')$, where $f\in\primrec$, $P(f)$, and
wenzelm@3162
   882
$\fs'$ is another list of primitive recursive functions satisfying~$P$.
wenzelm@3162
   883
wenzelm@3162
   884
Figure~\ref{primrec-fig} presents the theory file.  Theory {\tt Primrec}
wenzelm@3162
   885
defines the constants $\SC$, $\CONST$, etc.  These are not constructors of
wenzelm@3162
   886
a new datatype, but functions over lists of numbers.  Their definitions,
wenzelm@3162
   887
most of which are omitted, consist of routine list programming.  In
wenzelm@3162
   888
Isabelle/\textsc{zf}, the primitive recursive functions are defined as a subset of
wenzelm@3162
   889
the function set $\lst(\nat)\to\nat$.
wenzelm@3162
   890
wenzelm@3162
   891
The Isabelle theory goes on to formalize Ackermann's function and prove
wenzelm@3162
   892
that it is not primitive recursive, using the induction rule {\tt
wenzelm@3162
   893
  primrec.induct}.  The proof follows Szasz's excellent account.
wenzelm@3162
   894
wenzelm@3162
   895
wenzelm@3162
   896
\section{Datatypes and codatatypes}\label{data-sec}
wenzelm@3162
   897
A (co)datatype definition is a (co)inductive definition with automatically
wenzelm@3162
   898
defined constructors and a case analysis operator.  The package proves that
wenzelm@3162
   899
the case operator inverts the constructors and can prove freeness theorems
wenzelm@3162
   900
involving any pair of constructors.
wenzelm@3162
   901
wenzelm@3162
   902
wenzelm@3162
   903
\subsection{Constructors and their domain}\label{univ-sec}
wenzelm@3162
   904
A (co)inductive definition selects a subset of an existing set; a (co)datatype
wenzelm@3162
   905
definition creates a new set.  The package reduces the latter to the former.
wenzelm@3162
   906
Isabelle/\textsc{zf} supplies sets having strong closure properties to serve
wenzelm@3162
   907
as domains for (co)inductive definitions.
wenzelm@3162
   908
wenzelm@3162
   909
Isabelle/\textsc{zf} defines the Cartesian product $A\times
wenzelm@3162
   910
B$, containing ordered pairs $\pair{a,b}$; it also defines the
wenzelm@3162
   911
disjoint sum $A+B$, containing injections $\Inl(a)\equiv\pair{0,a}$ and
wenzelm@3162
   912
$\Inr(b)\equiv\pair{1,b}$.  For use below, define the $m$-tuple
wenzelm@3162
   913
$\pair{x_1,\ldots,x_m}$ to be the empty set~$\emptyset$ if $m=0$, simply $x_1$
wenzelm@3162
   914
if $m=1$ and $\pair{x_1,\pair{x_2,\ldots,x_m}}$ if $m\geq2$.
wenzelm@3162
   915
wenzelm@3162
   916
A datatype constructor $\Con(x_1,\ldots,x_m)$ is defined to be
wenzelm@3162
   917
$h(\pair{x_1,\ldots,x_m})$, where $h$ is composed of $\Inl$ and~$\Inr$.
wenzelm@3162
   918
In a mutually recursive definition, all constructors for the set~$R_i$ have
wenzelm@3162
   919
the outer form~$h_{in}$, where $h_{in}$ is the injection described
wenzelm@3162
   920
in~\S\ref{mutual-sec}.  Further nested injections ensure that the
wenzelm@3162
   921
constructors for~$R_i$ are pairwise distinct.  
wenzelm@3162
   922
wenzelm@3162
   923
Isabelle/\textsc{zf} defines the set $\univ(A)$, which contains~$A$ and
wenzelm@3162
   924
furthermore contains $\pair{a,b}$, $\Inl(a)$ and $\Inr(b)$ for $a$,
wenzelm@3162
   925
$b\in\univ(A)$.  In a typical datatype definition with set parameters
wenzelm@3162
   926
$A_1$, \ldots, $A_k$, a suitable domain for all the recursive sets is
wenzelm@3162
   927
$\univ(A_1\un\cdots\un A_k)$.  This solves the problem for
wenzelm@3162
   928
datatypes~\cite[\S4.2]{paulson-set-II}.
wenzelm@3162
   929
wenzelm@3162
   930
The standard pairs and injections can only yield well-founded
wenzelm@3162
   931
constructions.  This eases the (manual!) definition of recursive functions
wenzelm@3162
   932
over datatypes.  But they are unsuitable for codatatypes, which typically
wenzelm@3162
   933
contain non-well-founded objects.
wenzelm@3162
   934
wenzelm@3162
   935
To support codatatypes, Isabelle/\textsc{zf} defines a variant notion of
wenzelm@3162
   936
ordered pair, written~$\pair{a;b}$.  It also defines the corresponding variant
wenzelm@3162
   937
notion of Cartesian product $A\otimes B$, variant injections $\QInl(a)$
wenzelm@3162
   938
and~$\QInr(b)$ and variant disjoint sum $A\oplus B$.  Finally it defines the
wenzelm@3162
   939
set $\quniv(A)$, which contains~$A$ and furthermore contains $\pair{a;b}$,
wenzelm@3162
   940
$\QInl(a)$ and $\QInr(b)$ for $a$, $b\in\quniv(A)$.  In a typical codatatype
wenzelm@3162
   941
definition with set parameters $A_1$, \ldots, $A_k$, a suitable domain is
wenzelm@3162
   942
$\quniv(A_1\un\cdots\un A_k)$.  Details are published
wenzelm@3162
   943
elsewhere~\cite{paulson-final}.
wenzelm@3162
   944
wenzelm@3162
   945
\subsection{The case analysis operator}
wenzelm@3162
   946
The (co)datatype package automatically defines a case analysis operator,
wenzelm@3162
   947
called {\tt$R$\_case}.  A mutually recursive definition still has only one
wenzelm@3162
   948
operator, whose name combines those of the recursive sets: it is called
wenzelm@3162
   949
{\tt$R_1$\_\ldots\_$R_n$\_case}.  The case operator is analogous to those
wenzelm@3162
   950
for products and sums.
wenzelm@3162
   951
wenzelm@3162
   952
Datatype definitions employ standard products and sums, whose operators are
wenzelm@3162
   953
$\split$ and $\case$ and satisfy the equations
wenzelm@3162
   954
\begin{eqnarray*}
wenzelm@3162
   955
  \split(f,\pair{x,y})  & = &  f(x,y) \\
wenzelm@3162
   956
  \case(f,g,\Inl(x))    & = &  f(x)   \\
wenzelm@3162
   957
  \case(f,g,\Inr(y))    & = &  g(y)
wenzelm@3162
   958
\end{eqnarray*}
wenzelm@3162
   959
Suppose the datatype has $k$ constructors $\Con_1$, \ldots,~$\Con_k$.  Then
wenzelm@3162
   960
its case operator takes $k+1$ arguments and satisfies an equation for each
wenzelm@3162
   961
constructor:
wenzelm@3162
   962
\[ R\hbox{\_case}(f_1,\ldots,f_k, {\tt Con}_i(\vec{x})) = f_i(\vec{x}),
wenzelm@3162
   963
    \qquad i = 1, \ldots, k
wenzelm@3162
   964
\]
wenzelm@3162
   965
The case operator's definition takes advantage of Isabelle's representation of
wenzelm@3162
   966
syntax in the typed $\lambda$-calculus; it could readily be adapted to a
wenzelm@3162
   967
theorem prover for higher-order logic.  If $f$ and~$g$ have meta-type $i\To i$
wenzelm@3162
   968
then so do $\split(f)$ and $\case(f,g)$.  This works because $\split$ and
wenzelm@3162
   969
$\case$ operate on their last argument.  They are easily combined to make
wenzelm@3162
   970
complex case analysis operators.  For example, $\case(f,\case(g,h))$ performs
wenzelm@3162
   971
case analysis for $A+(B+C)$; let us verify one of the three equations:
wenzelm@3162
   972
\[ \case(f,\case(g,h), \Inr(\Inl(b))) = \case(g,h,\Inl(b)) = g(b) \]
wenzelm@3162
   973
Codatatype definitions are treated in precisely the same way.  They express
wenzelm@3162
   974
case operators using those for the variant products and sums, namely
wenzelm@3162
   975
$\qsplit$ and~$\qcase$.
wenzelm@3162
   976
wenzelm@3162
   977
\medskip
wenzelm@3162
   978
wenzelm@3162
   979
To see how constructors and the case analysis operator are defined, let us
wenzelm@3162
   980
examine some examples.  Further details are available
wenzelm@3162
   981
elsewhere~\cite{paulson-set-II}.
wenzelm@3162
   982
wenzelm@3162
   983
wenzelm@3162
   984
\subsection{Example: lists and lazy lists}\label{lists-sec}
wenzelm@3162
   985
Here is a declaration of the datatype of lists, as it might appear in a theory
wenzelm@3162
   986
file:
wenzelm@3162
   987
\begin{ttbox} 
wenzelm@3162
   988
consts  list :: i=>i
wenzelm@3162
   989
datatype "list(A)" = Nil | Cons ("a:A", "l: list(A)")
wenzelm@3162
   990
\end{ttbox}
wenzelm@3162
   991
And here is a declaration of the codatatype of lazy lists:
wenzelm@3162
   992
\begin{ttbox}
wenzelm@3162
   993
consts  llist :: i=>i
wenzelm@3162
   994
codatatype "llist(A)" = LNil | LCons ("a: A", "l: llist(A)")
wenzelm@3162
   995
\end{ttbox}
wenzelm@3162
   996
wenzelm@3162
   997
Each form of list has two constructors, one for the empty list and one for
wenzelm@3162
   998
adding an element to a list.  Each takes a parameter, defining the set of
wenzelm@3162
   999
lists over a given set~$A$.  Each is automatically given the appropriate
wenzelm@3162
  1000
domain: $\univ(A)$ for $\lst(A)$ and $\quniv(A)$ for $\llist(A)$.  The default
wenzelm@3162
  1001
can be overridden.
wenzelm@3162
  1002
wenzelm@3162
  1003
\ifshort
wenzelm@3162
  1004
Now $\lst(A)$ is a datatype and enjoys the usual induction rule.
wenzelm@3162
  1005
\else
wenzelm@3162
  1006
Since $\lst(A)$ is a datatype, it enjoys a structural induction rule, {\tt
wenzelm@3162
  1007
  list.induct}:
wenzelm@3162
  1008
\[ \infer{P(x)}{x\in\lst(A) & P(\Nil)
wenzelm@3162
  1009
        & \infer*{P(\Cons(a,l))}{[a\in A & l\in\lst(A) & P(l)]_{a,l}} }
wenzelm@3162
  1010
\] 
wenzelm@3162
  1011
Induction and freeness yield the law $l\not=\Cons(a,l)$.  To strengthen this,
wenzelm@3162
  1012
Isabelle/\textsc{zf} defines the rank of a set and proves that the standard
wenzelm@3162
  1013
pairs and injections have greater rank than their components.  An immediate
wenzelm@3162
  1014
consequence, which justifies structural recursion on lists
wenzelm@3162
  1015
\cite[\S4.3]{paulson-set-II}, is
wenzelm@3162
  1016
\[ \rank(l) < \rank(\Cons(a,l)). \]
wenzelm@3162
  1017
\par
wenzelm@3162
  1018
\fi
wenzelm@3162
  1019
But $\llist(A)$ is a codatatype and has no induction rule.  Instead it has
wenzelm@3162
  1020
the coinduction rule shown in \S\ref{coind-sec}.  Since variant pairs and
wenzelm@3162
  1021
injections are monotonic and need not have greater rank than their
wenzelm@3162
  1022
components, fixedpoint operators can create cyclic constructions.  For
wenzelm@3162
  1023
example, the definition
wenzelm@3162
  1024
\[ \lconst(a) \equiv \lfp(\univ(a), \lambda l. \LCons(a,l)) \]
wenzelm@3162
  1025
yields $\lconst(a) = \LCons(a,\lconst(a))$.
wenzelm@3162
  1026
wenzelm@3162
  1027
\ifshort
wenzelm@3162
  1028
\typeout{****SHORT VERSION}
wenzelm@3162
  1029
\typeout{****Omitting discussion of constructors!}
wenzelm@3162
  1030
\else
wenzelm@3162
  1031
\medskip
wenzelm@3162
  1032
It may be instructive to examine the definitions of the constructors and
wenzelm@3162
  1033
case operator for $\lst(A)$.  The definitions for $\llist(A)$ are similar.
wenzelm@3162
  1034
The list constructors are defined as follows:
wenzelm@3162
  1035
\begin{eqnarray*}
wenzelm@3162
  1036
  \Nil       & \equiv & \Inl(\emptyset) \\
wenzelm@3162
  1037
  \Cons(a,l) & \equiv & \Inr(\pair{a,l})
wenzelm@3162
  1038
\end{eqnarray*}
wenzelm@3162
  1039
The operator $\lstcase$ performs case analysis on these two alternatives:
wenzelm@3162
  1040
\[ \lstcase(c,h) \equiv \case(\lambda u.c, \split(h)) \]
wenzelm@3162
  1041
Let us verify the two equations:
wenzelm@3162
  1042
\begin{eqnarray*}
wenzelm@3162
  1043
    \lstcase(c, h, \Nil) & = & 
wenzelm@3162
  1044
       \case(\lambda u.c, \split(h), \Inl(\emptyset)) \\
wenzelm@3162
  1045
     & = & (\lambda u.c)(\emptyset) \\
wenzelm@3162
  1046
     & = & c\\[1ex]
wenzelm@3162
  1047
    \lstcase(c, h, \Cons(x,y)) & = & 
wenzelm@3162
  1048
       \case(\lambda u.c, \split(h), \Inr(\pair{x,y})) \\
wenzelm@3162
  1049
     & = & \split(h, \pair{x,y}) \\
wenzelm@3162
  1050
     & = & h(x,y)
wenzelm@3162
  1051
\end{eqnarray*} 
wenzelm@3162
  1052
\fi
wenzelm@3162
  1053
wenzelm@3162
  1054
wenzelm@3162
  1055
\ifshort
wenzelm@3162
  1056
\typeout{****Omitting mutual recursion example!}
wenzelm@3162
  1057
\else
wenzelm@3162
  1058
\subsection{Example: mutual recursion}
wenzelm@3162
  1059
In mutually recursive trees and forests~\cite[\S4.5]{paulson-set-II}, trees
wenzelm@3162
  1060
have the one constructor $\Tcons$, while forests have the two constructors
wenzelm@3162
  1061
$\Fnil$ and~$\Fcons$:
wenzelm@3162
  1062
\begin{ttbox}
wenzelm@3162
  1063
consts  tree, forest, tree_forest    :: i=>i
wenzelm@3162
  1064
datatype "tree(A)"   = Tcons ("a: A",  "f: forest(A)")
wenzelm@3162
  1065
and      "forest(A)" = Fnil  |  Fcons ("t: tree(A)",  "f: forest(A)")
wenzelm@3162
  1066
\end{ttbox}
wenzelm@3162
  1067
The three introduction rules define the mutual recursion.  The
wenzelm@3162
  1068
distinguishing feature of this example is its two induction rules.
wenzelm@3162
  1069
wenzelm@3162
  1070
The basic induction rule is called {\tt tree\_forest.induct}:
wenzelm@3162
  1071
\[ \infer{P(x)}{x\in\TF(A) & 
wenzelm@3162
  1072
     \infer*{P(\Tcons(a,f))}
wenzelm@3162
  1073
        {\left[\begin{array}{l} a\in A \\ 
wenzelm@3162
  1074
                                f\in\forest(A) \\ P(f)
wenzelm@3162
  1075
               \end{array}
wenzelm@3162
  1076
         \right]_{a,f}}
wenzelm@3162
  1077
     & P(\Fnil)
wenzelm@3162
  1078
     & \infer*{P(\Fcons(t,f))}
wenzelm@3162
  1079
        {\left[\begin{array}{l} t\in\tree(A)   \\ P(t) \\
wenzelm@3162
  1080
                                f\in\forest(A) \\ P(f)
wenzelm@3162
  1081
                \end{array}
wenzelm@3162
  1082
         \right]_{t,f}} }
wenzelm@3162
  1083
\] 
wenzelm@3162
  1084
This rule establishes a single predicate for $\TF(A)$, the union of the
wenzelm@3162
  1085
recursive sets.  Although such reasoning is sometimes useful
wenzelm@3162
  1086
\cite[\S4.5]{paulson-set-II}, a proper mutual induction rule should establish
wenzelm@3162
  1087
separate predicates for $\tree(A)$ and $\forest(A)$.  The package calls this
wenzelm@3162
  1088
rule {\tt tree\_forest.mutual\_induct}.  Observe the usage of $P$ and $Q$ in
wenzelm@3162
  1089
the induction hypotheses:
wenzelm@3162
  1090
\[ \infer{(\forall z. z\in\tree(A)\imp P(z)) \conj
wenzelm@3162
  1091
          (\forall z. z\in\forest(A)\imp Q(z))}
wenzelm@3162
  1092
     {\infer*{P(\Tcons(a,f))}
wenzelm@3162
  1093
        {\left[\begin{array}{l} a\in A \\ 
wenzelm@3162
  1094
                                f\in\forest(A) \\ Q(f)
wenzelm@3162
  1095
               \end{array}
wenzelm@3162
  1096
         \right]_{a,f}}
wenzelm@3162
  1097
     & Q(\Fnil)
wenzelm@3162
  1098
     & \infer*{Q(\Fcons(t,f))}
wenzelm@3162
  1099
        {\left[\begin{array}{l} t\in\tree(A)   \\ P(t) \\
wenzelm@3162
  1100
                                f\in\forest(A) \\ Q(f)
wenzelm@3162
  1101
                \end{array}
wenzelm@3162
  1102
         \right]_{t,f}} }
wenzelm@3162
  1103
\] 
wenzelm@3162
  1104
Elsewhere I describe how to define mutually recursive functions over trees and
wenzelm@3162
  1105
forests \cite[\S4.5]{paulson-set-II}.
wenzelm@3162
  1106
wenzelm@3162
  1107
Both forest constructors have the form $\Inr(\cdots)$,
wenzelm@3162
  1108
while the tree constructor has the form $\Inl(\cdots)$.  This pattern would
wenzelm@3162
  1109
hold regardless of how many tree or forest constructors there were.
wenzelm@3162
  1110
\begin{eqnarray*}
wenzelm@3162
  1111
  \Tcons(a,l)  & \equiv & \Inl(\pair{a,l}) \\
wenzelm@3162
  1112
  \Fnil        & \equiv & \Inr(\Inl(\emptyset)) \\
wenzelm@3162
  1113
  \Fcons(a,l)  & \equiv & \Inr(\Inr(\pair{a,l}))
wenzelm@3162
  1114
\end{eqnarray*} 
wenzelm@3162
  1115
There is only one case operator; it works on the union of the trees and
wenzelm@3162
  1116
forests:
wenzelm@3162
  1117
\[ {\tt tree\_forest\_case}(f,c,g) \equiv 
wenzelm@3162
  1118
    \case(\split(f),\, \case(\lambda u.c, \split(g))) 
wenzelm@3162
  1119
\]
wenzelm@3162
  1120
\fi
wenzelm@3162
  1121
wenzelm@3162
  1122
wenzelm@3162
  1123
\subsection{Example: a four-constructor datatype}
wenzelm@3162
  1124
A bigger datatype will illustrate some efficiency 
wenzelm@3162
  1125
refinements.  It has four constructors $\Con_0$, \ldots, $\Con_3$, with the
wenzelm@3162
  1126
corresponding arities.
wenzelm@3162
  1127
\begin{ttbox}
wenzelm@3162
  1128
consts    data :: [i,i] => i
wenzelm@3162
  1129
datatype  "data(A,B)" = Con0
wenzelm@3162
  1130
                      | Con1 ("a: A")
wenzelm@3162
  1131
                      | Con2 ("a: A", "b: B")
wenzelm@3162
  1132
                      | Con3 ("a: A", "b: B", "d: data(A,B)")
wenzelm@3162
  1133
\end{ttbox}
wenzelm@3162
  1134
Because this datatype has two set parameters, $A$ and~$B$, the package
wenzelm@3162
  1135
automatically supplies $\univ(A\un B)$ as its domain.  The structural
wenzelm@3162
  1136
induction rule has four minor premises, one per constructor, and only the last
wenzelm@3162
  1137
has an induction hypothesis.  (Details are left to the reader.)
wenzelm@3162
  1138
wenzelm@3162
  1139
The constructors are defined by the equations
wenzelm@3162
  1140
\begin{eqnarray*}
wenzelm@3162
  1141
  \Con_0         & \equiv & \Inl(\Inl(\emptyset)) \\
wenzelm@3162
  1142
  \Con_1(a)      & \equiv & \Inl(\Inr(a)) \\
wenzelm@3162
  1143
  \Con_2(a,b)    & \equiv & \Inr(\Inl(\pair{a,b})) \\
wenzelm@3162
  1144
  \Con_3(a,b,c)  & \equiv & \Inr(\Inr(\pair{a,b,c})).
wenzelm@3162
  1145
\end{eqnarray*} 
wenzelm@3162
  1146
The case analysis operator is
wenzelm@3162
  1147
\[ {\tt data\_case}(f_0,f_1,f_2,f_3) \equiv 
wenzelm@3162
  1148
    \case(\begin{array}[t]{@{}l}
wenzelm@3162
  1149
          \case(\lambda u.f_0,\; f_1),\, \\
wenzelm@3162
  1150
          \case(\split(f_2),\; \split(\lambda v.\split(f_3(v)))) )
wenzelm@3162
  1151
   \end{array} 
wenzelm@3162
  1152
\]
wenzelm@3162
  1153
This may look cryptic, but the case equations are trivial to verify.
wenzelm@3162
  1154
wenzelm@3162
  1155
In the constructor definitions, the injections are balanced.  A more naive
wenzelm@3162
  1156
approach is to define $\Con_3(a,b,c)$ as $\Inr(\Inr(\Inr(\pair{a,b,c})))$;
wenzelm@3162
  1157
instead, each constructor has two injections.  The difference here is small.
wenzelm@3162
  1158
But the \textsc{zf} examples include a 60-element enumeration type, where each
wenzelm@3162
  1159
constructor has 5 or~6 injections.  The naive approach would require 1 to~59
wenzelm@3162
  1160
injections; the definitions would be quadratic in size.  It is like the
wenzelm@3162
  1161
advantage of binary notation over unary.
wenzelm@3162
  1162
wenzelm@3162
  1163
The result structure contains the case operator and constructor definitions as
wenzelm@3162
  1164
the theorem list \verb|con_defs|. It contains the case equations, such as 
wenzelm@3162
  1165
\[ {\tt data\_case}(f_0,f_1,f_2,f_3,\Con_3(a,b,c)) = f_3(a,b,c), \]
wenzelm@3162
  1166
as the theorem list \verb|case_eqns|.  There is one equation per constructor.
wenzelm@3162
  1167
wenzelm@3162
  1168
\subsection{Proving freeness theorems}
wenzelm@3162
  1169
There are two kinds of freeness theorems:
wenzelm@3162
  1170
\begin{itemize}
wenzelm@3162
  1171
\item \defn{injectiveness} theorems, such as
wenzelm@3162
  1172
\[ \Con_2(a,b) = \Con_2(a',b') \bimp a=a' \conj b=b' \]
wenzelm@3162
  1173
wenzelm@3162
  1174
\item \defn{distinctness} theorems, such as
wenzelm@3162
  1175
\[ \Con_1(a) \not= \Con_2(a',b')  \]
wenzelm@3162
  1176
\end{itemize}
wenzelm@3162
  1177
Since the number of such theorems is quadratic in the number of constructors,
wenzelm@3162
  1178
the package does not attempt to prove them all.  Instead it returns tools for
wenzelm@3162
  1179
proving desired theorems --- either manually or during
wenzelm@3162
  1180
simplification or classical reasoning.
wenzelm@3162
  1181
wenzelm@3162
  1182
The theorem list \verb|free_iffs| enables the simplifier to perform freeness
wenzelm@3162
  1183
reasoning.  This works by incremental unfolding of constructors that appear in
wenzelm@3162
  1184
equations.  The theorem list contains logical equivalences such as
wenzelm@3162
  1185
\begin{eqnarray*}
wenzelm@3162
  1186
  \Con_0=c      & \bimp &  c=\Inl(\Inl(\emptyset))     \\
wenzelm@3162
  1187
  \Con_1(a)=c   & \bimp &  c=\Inl(\Inr(a))             \\
wenzelm@3162
  1188
                & \vdots &                             \\
wenzelm@3162
  1189
  \Inl(a)=\Inl(b)   & \bimp &  a=b                     \\
wenzelm@3162
  1190
  \Inl(a)=\Inr(b)   & \bimp &  {\tt False}             \\
wenzelm@3162
  1191
  \pair{a,b} = \pair{a',b'} & \bimp & a=a' \conj b=b'
wenzelm@3162
  1192
\end{eqnarray*}
wenzelm@3162
  1193
For example, these rewrite $\Con_1(a)=\Con_1(b)$ to $a=b$ in four steps.
wenzelm@3162
  1194
wenzelm@3162
  1195
The theorem list \verb|free_SEs| enables the classical
wenzelm@3162
  1196
reasoner to perform similar replacements.  It consists of elimination rules
wenzelm@3162
  1197
to replace $\Con_0=c$ by $c=\Inl(\Inl(\emptyset))$ and so forth, in the
wenzelm@3162
  1198
assumptions.
wenzelm@3162
  1199
wenzelm@3162
  1200
Such incremental unfolding combines freeness reasoning with other proof
wenzelm@3162
  1201
steps.  It has the unfortunate side-effect of unfolding definitions of
wenzelm@3162
  1202
constructors in contexts such as $\exists x.\Con_1(a)=x$, where they should
wenzelm@3162
  1203
be left alone.  Calling the Isabelle tactic {\tt fold\_tac con\_defs}
wenzelm@3162
  1204
restores the defined constants.
wenzelm@3162
  1205
wenzelm@3162
  1206
wenzelm@3162
  1207
\section{Related work}\label{related}
wenzelm@3162
  1208
The use of least fixedpoints to express inductive definitions seems
wenzelm@3162
  1209
obvious.  Why, then, has this technique so seldom been implemented?
wenzelm@3162
  1210
wenzelm@3162
  1211
Most automated logics can only express inductive definitions by asserting
wenzelm@3162
  1212
axioms.  Little would be left of Boyer and Moore's logic~\cite{bm79} if their
wenzelm@3162
  1213
shell principle were removed.  With \textsc{alf} the situation is more
wenzelm@3162
  1214
complex; earlier versions of Martin-L\"of's type theory could (using
wenzelm@3162
  1215
wellordering types) express datatype definitions, but the version underlying
wenzelm@3162
  1216
\textsc{alf} requires new rules for each definition~\cite{dybjer91}.  With Coq
wenzelm@3162
  1217
the situation is subtler still; its underlying Calculus of Constructions can
wenzelm@3162
  1218
express inductive definitions~\cite{huet88}, but cannot quite handle datatype
wenzelm@3162
  1219
definitions~\cite{paulin-tlca}.  It seems that researchers tried hard to
wenzelm@3162
  1220
circumvent these problems before finally extending the Calculus with rule
wenzelm@3162
  1221
schemes for strictly positive operators.  Recently Gim{\'e}nez has extended
wenzelm@3162
  1222
the Calculus of Constructions with inductive and coinductive
wenzelm@3162
  1223
types~\cite{gimenez-codifying}, with mechanized support in Coq.
wenzelm@3162
  1224
wenzelm@3162
  1225
Higher-order logic can express inductive definitions through quantification
wenzelm@3162
  1226
over unary predicates.  The following formula expresses that~$i$ belongs to the
wenzelm@3162
  1227
least set containing~0 and closed under~$\succ$:
wenzelm@3162
  1228
\[ \forall P. P(0)\conj (\forall x.P(x)\imp P(\succ(x))) \imp P(i) \] 
wenzelm@3162
  1229
This technique can be used to prove the Knaster-Tarski theorem, which (in its
wenzelm@3162
  1230
general form) is little used in the Cambridge \textsc{hol} system.
wenzelm@3162
  1231
Melham~\cite{melham89} describes the development.  The natural numbers are
wenzelm@3162
  1232
defined as shown above, but lists are defined as functions over the natural
wenzelm@3162
  1233
numbers.  Unlabelled trees are defined using G\"odel numbering; a labelled
wenzelm@3162
  1234
tree consists of an unlabelled tree paired with a list of labels.  Melham's
wenzelm@3162
  1235
datatype package expresses the user's datatypes in terms of labelled trees.
wenzelm@3162
  1236
It has been highly successful, but a fixedpoint approach might have yielded
wenzelm@3162
  1237
greater power with less effort.
wenzelm@3162
  1238
wenzelm@3162
  1239
Elsa Gunter~\cite{gunter-trees} reports an ongoing project to generalize the
wenzelm@3162
  1240
Cambridge \textsc{hol} system with mutual recursion and infinitely-branching
wenzelm@3162
  1241
trees.  She retains many features of Melham's approach.
wenzelm@3162
  1242
wenzelm@3162
  1243
Melham's inductive definition package~\cite{camilleri92} also uses
wenzelm@3162
  1244
quantification over predicates.  But instead of formalizing the notion of
wenzelm@3162
  1245
monotone function, it requires definitions to consist of finitary rules, a
wenzelm@3162
  1246
syntactic form that excludes many monotone inductive definitions.
wenzelm@3162
  1247
wenzelm@3162
  1248
\textsc{pvs}~\cite{pvs-language} is another proof assistant based on
wenzelm@3162
  1249
higher-order logic.  It supports both inductive definitions and datatypes,
wenzelm@3162
  1250
apparently by asserting axioms.  Datatypes may not be iterated in general, but
wenzelm@3162
  1251
may use recursion over the built-in $\lst$ type.
wenzelm@3162
  1252
wenzelm@3162
  1253
The earliest use of least fixedpoints is probably Robin Milner's.  Brian
wenzelm@3162
  1254
Monahan extended this package considerably~\cite{monahan84}, as did I in
wenzelm@3162
  1255
unpublished work.\footnote{The datatype package described in my \textsc{lcf}
wenzelm@3162
  1256
  book~\cite{paulson87} does {\it not\/} make definitions, but merely asserts
wenzelm@3162
  1257
  axioms.} \textsc{lcf} is a first-order logic of domain theory; the relevant
wenzelm@3162
  1258
fixedpoint theorem is not Knaster-Tarski but concerns fixedpoints of
wenzelm@3162
  1259
continuous functions over domains.  \textsc{lcf} is too weak to express
wenzelm@3162
  1260
recursive predicates.  The Isabelle package might be the first to be based on
wenzelm@3162
  1261
the Knaster-Tarski theorem.
wenzelm@3162
  1262
wenzelm@3162
  1263
wenzelm@3162
  1264
\section{Conclusions and future work}
wenzelm@3162
  1265
Higher-order logic and set theory are both powerful enough to express
wenzelm@3162
  1266
inductive definitions.  A growing number of theorem provers implement one
wenzelm@3162
  1267
of these~\cite{IMPS,saaltink-fme}.  The easiest sort of inductive
wenzelm@3162
  1268
definition package to write is one that asserts new axioms, not one that
wenzelm@3162
  1269
makes definitions and proves theorems about them.  But asserting axioms
wenzelm@3162
  1270
could introduce unsoundness.
wenzelm@3162
  1271
wenzelm@3162
  1272
The fixedpoint approach makes it fairly easy to implement a package for
wenzelm@3162
  1273
(co)in\-duc\-tive definitions that does not assert axioms.  It is efficient:
wenzelm@3162
  1274
it processes most definitions in seconds and even a 60-constructor datatype
wenzelm@3162
  1275
requires only a few minutes.  It is also simple: The first working version took
wenzelm@3162
  1276
under a week to code, consisting of under 1100 lines (35K bytes) of Standard
wenzelm@3162
  1277
\textsc{ml}.
wenzelm@3162
  1278
wenzelm@3162
  1279
In set theory, care is needed to ensure that the inductive definition yields
wenzelm@3162
  1280
a set (rather than a proper class).  This problem is inherent to set theory,
wenzelm@3162
  1281
whether or not the Knaster-Tarski theorem is employed.  We must exhibit a
wenzelm@3162
  1282
bounding set (called a domain above).  For inductive definitions, this is
wenzelm@3162
  1283
often trivial.  For datatype definitions, I have had to formalize much set
wenzelm@3162
  1284
theory.  To justify infinitely-branching datatype definitions, I have had to
wenzelm@3162
  1285
develop a theory of cardinal arithmetic~\cite{paulson-gr}, such as the theorem
wenzelm@3162
  1286
that if $\kappa$ is an infinite cardinal and $|X(\alpha)| \le \kappa$ for all
wenzelm@3162
  1287
$\alpha<\kappa$ then $|\union\sb{\alpha<\kappa} X(\alpha)| \le \kappa$.  
wenzelm@3162
  1288
The need for such efforts is not a drawback of the fixedpoint approach, for
wenzelm@3162
  1289
the alternative is to take such definitions on faith.
wenzelm@3162
  1290
wenzelm@3162
  1291
Care is also needed to ensure that the greatest fixedpoint really yields a
wenzelm@3162
  1292
coinductive definition.  In set theory, standard pairs admit only well-founded
wenzelm@3162
  1293
constructions.  Aczel's anti-foundation axiom~\cite{aczel88} could be used to
wenzelm@3162
  1294
get non-well-founded objects, but it does not seem easy to mechanize.
wenzelm@3162
  1295
Isabelle/\textsc{zf} instead uses a variant notion of ordered pairing, which
wenzelm@3162
  1296
can be generalized to a variant notion of function.  Elsewhere I have
wenzelm@3162
  1297
proved that this simple approach works (yielding final coalgebras) for a broad
wenzelm@3162
  1298
class of definitions~\cite{paulson-final}.
wenzelm@3162
  1299
wenzelm@3162
  1300
Several large studies make heavy use of inductive definitions.  L\"otzbeyer
wenzelm@3162
  1301
and Sandner have formalized two chapters of a semantics book~\cite{winskel93},
wenzelm@3162
  1302
proving the equivalence between the operational and denotational semantics of
wenzelm@3162
  1303
a simple imperative language.  A single theory file contains three datatype
wenzelm@3162
  1304
definitions (of arithmetic expressions, boolean expressions and commands) and
wenzelm@3162
  1305
three inductive definitions (the corresponding operational rules).  Using
wenzelm@3162
  1306
different techniques, Nipkow~\cite{nipkow-CR} and Rasmussen~\cite{rasmussen95}
wenzelm@3162
  1307
have both proved the Church-Rosser theorem; inductive definitions specify
wenzelm@3162
  1308
several reduction relations on $\lambda$-terms.  Recently, I have applied
wenzelm@3162
  1309
inductive definitions to the analysis of cryptographic
wenzelm@3162
  1310
protocols~\cite{paulson-markt}. 
wenzelm@3162
  1311
wenzelm@3162
  1312
To demonstrate coinductive definitions, Frost~\cite{frost95} has proved the
wenzelm@3162
  1313
consistency of the dynamic and static semantics for a small functional
wenzelm@3162
  1314
language.  The example is due to Milner and Tofte~\cite{milner-coind}.  It
wenzelm@3162
  1315
concerns an extended correspondence relation, which is defined coinductively.
wenzelm@3162
  1316
A codatatype definition specifies values and value environments in mutual
wenzelm@3162
  1317
recursion.  Non-well-founded values represent recursive functions.  Value
wenzelm@3162
  1318
environments are variant functions from variables into values.  This one key
wenzelm@3162
  1319
definition uses most of the package's novel features.
wenzelm@3162
  1320
wenzelm@3162
  1321
The approach is not restricted to set theory.  It should be suitable for any
wenzelm@3162
  1322
logic that has some notion of set and the Knaster-Tarski theorem.  I have
wenzelm@3162
  1323
ported the (co)inductive definition package from Isabelle/\textsc{zf} to
wenzelm@3162
  1324
Isabelle/\textsc{hol} (higher-order logic).  V\"olker~\cite{voelker95}
wenzelm@3162
  1325
is investigating how to port the (co)datatype package.  \textsc{hol}
wenzelm@3162
  1326
represents sets by unary predicates; defining the corresponding types may
wenzelm@3162
  1327
cause complications.
wenzelm@3162
  1328
wenzelm@3162
  1329
wenzelm@3162
  1330
\begin{footnotesize}
wenzelm@3162
  1331
\bibliographystyle{springer}
wenzelm@3162
  1332
\bibliography{string-abbrv,atp,theory,funprog,isabelle,crossref}
wenzelm@3162
  1333
\end{footnotesize}
wenzelm@3162
  1334
%%%%%\doendnotes
wenzelm@3162
  1335
wenzelm@3162
  1336
\ifshort\typeout{****Omitting appendices}
wenzelm@3162
  1337
\else
wenzelm@3162
  1338
\newpage
wenzelm@3162
  1339
\appendix
wenzelm@3162
  1340
\section{Inductive and coinductive definitions: users guide}
wenzelm@3162
  1341
A theory file may contain any number of inductive and coinductive
wenzelm@3162
  1342
definitions.  They may be intermixed with other declarations; in
wenzelm@3162
  1343
particular, the (co)inductive sets \defn{must} be declared separately as
wenzelm@3162
  1344
constants, and may have mixfix syntax or be subject to syntax translations.
wenzelm@3162
  1345
wenzelm@3162
  1346
The syntax is rather complicated.  Please consult the examples above and the
wenzelm@3162
  1347
theory files on the \textsc{zf} source directory.  
wenzelm@3162
  1348
wenzelm@3162
  1349
Each (co)inductive definition adds definitions to the theory and also proves
wenzelm@3162
  1350
some theorems.  Each definition creates an \textsc{ml} structure, which is a
wenzelm@3162
  1351
substructure of the main theory structure.
wenzelm@3162
  1352
wenzelm@3162
  1353
Inductive and datatype definitions can take up considerable storage.  The
wenzelm@3162
  1354
introduction rules are replicated in slightly different forms as fixedpoint
wenzelm@3162
  1355
definitions, elimination rules and induction rules.  L\"otzbeyer and Sandner's
wenzelm@3162
  1356
six definitions occupy over 600K in total.  Defining the 60-constructor
wenzelm@3162
  1357
datatype requires nearly 560K\@.
wenzelm@3162
  1358
wenzelm@3162
  1359
\subsection{The result structure}
wenzelm@3162
  1360
Many of the result structure's components have been discussed
wenzelm@3162
  1361
in~\S\ref{basic-sec}; others are self-explanatory.
wenzelm@3162
  1362
\begin{description}
wenzelm@3162
  1363
\item[\tt thy] is the new theory containing the recursive sets.
wenzelm@3162
  1364
wenzelm@3162
  1365
\item[\tt defs] is the list of definitions of the recursive sets.
wenzelm@3162
  1366
wenzelm@3162
  1367
\item[\tt bnd\_mono] is a monotonicity theorem for the fixedpoint operator.
wenzelm@3162
  1368
wenzelm@3162
  1369
\item[\tt dom\_subset] is a theorem stating inclusion in the domain.
wenzelm@3162
  1370
wenzelm@3162
  1371
\item[\tt intrs] is the list of introduction rules, now proved as theorems, for
wenzelm@3162
  1372
the recursive sets.  The rules are also available individually, using the
wenzelm@3162
  1373
names given them in the theory file. 
wenzelm@3162
  1374
wenzelm@3162
  1375
\item[\tt elim] is the elimination rule.
wenzelm@3162
  1376
wenzelm@3162
  1377
\item[\tt mk\_cases] is a function to create simplified instances of {\tt
wenzelm@3162
  1378
elim}, using freeness reasoning on some underlying datatype.
wenzelm@3162
  1379
\end{description}
wenzelm@3162
  1380
wenzelm@3162
  1381
For an inductive definition, the result structure contains two induction
wenzelm@3162
  1382
rules, {\tt induct} and \verb|mutual_induct|.  (To save storage, the latter
wenzelm@3162
  1383
rule is just {\tt True} unless more than one set is being defined.)  For a
wenzelm@3162
  1384
coinductive definition, it contains the rule \verb|coinduct|.
wenzelm@3162
  1385
wenzelm@3162
  1386
Figure~\ref{def-result-fig} summarizes the two result signatures,
wenzelm@3162
  1387
specifying the types of all these components.
wenzelm@3162
  1388
wenzelm@3162
  1389
\begin{figure}
wenzelm@3162
  1390
\begin{ttbox}
wenzelm@3162
  1391
sig
wenzelm@3162
  1392
val thy          : theory
wenzelm@3162
  1393
val defs         : thm list
wenzelm@3162
  1394
val bnd_mono     : thm
wenzelm@3162
  1395
val dom_subset   : thm
wenzelm@3162
  1396
val intrs        : thm list
wenzelm@3162
  1397
val elim         : thm
wenzelm@3162
  1398
val mk_cases     : thm list -> string -> thm
wenzelm@3162
  1399
{\it(Inductive definitions only)} 
wenzelm@3162
  1400
val induct       : thm
wenzelm@3162
  1401
val mutual_induct: thm
wenzelm@3162
  1402
{\it(Coinductive definitions only)}
wenzelm@3162
  1403
val coinduct    : thm
wenzelm@3162
  1404
end
wenzelm@3162
  1405
\end{ttbox}
wenzelm@3162
  1406
\hrule
wenzelm@3162
  1407
\caption{The result of a (co)inductive definition} \label{def-result-fig}
wenzelm@3162
  1408
\end{figure}
wenzelm@3162
  1409
wenzelm@3162
  1410
\subsection{The syntax of a (co)inductive definition}
wenzelm@3162
  1411
An inductive definition has the form
wenzelm@3162
  1412
\begin{ttbox}
wenzelm@3162
  1413
inductive
wenzelm@3162
  1414
  domains    {\it domain declarations}
wenzelm@3162
  1415
  intrs      {\it introduction rules}
wenzelm@3162
  1416
  monos      {\it monotonicity theorems}
wenzelm@3162
  1417
  con_defs   {\it constructor definitions}
wenzelm@3162
  1418
  type_intrs {\it introduction rules for type-checking}
wenzelm@3162
  1419
  type_elims {\it elimination rules for type-checking}
wenzelm@3162
  1420
\end{ttbox}
wenzelm@3162
  1421
A coinductive definition is identical, but starts with the keyword
wenzelm@3162
  1422
{\tt coinductive}.  
wenzelm@3162
  1423
wenzelm@3162
  1424
The {\tt monos}, {\tt con\_defs}, {\tt type\_intrs} and {\tt type\_elims}
wenzelm@3162
  1425
sections are optional.  If present, each is specified as a string, which
wenzelm@3162
  1426
must be a valid \textsc{ml} expression of type {\tt thm list}.  It is simply
wenzelm@3162
  1427
inserted into the {\tt .thy.ML} file; if it is ill-formed, it will trigger
wenzelm@3162
  1428
\textsc{ml} error messages.  You can then inspect the file on your directory.
wenzelm@3162
  1429
wenzelm@3162
  1430
\begin{description}
wenzelm@3162
  1431
\item[\it domain declarations] consist of one or more items of the form
wenzelm@3162
  1432
  {\it string\/}~{\tt <=}~{\it string}, associating each recursive set with
wenzelm@3162
  1433
  its domain.
wenzelm@3162
  1434
wenzelm@3162
  1435
\item[\it introduction rules] specify one or more introduction rules in
wenzelm@3162
  1436
  the form {\it ident\/}~{\it string}, where the identifier gives the name of
wenzelm@3162
  1437
  the rule in the result structure.
wenzelm@3162
  1438
wenzelm@3162
  1439
\item[\it monotonicity theorems] are required for each operator applied to
wenzelm@3162
  1440
  a recursive set in the introduction rules.  There \defn{must} be a theorem
wenzelm@3162
  1441
  of the form $A\sbs B\Imp M(A)\sbs M(B)$, for each premise $t\in M(R_i)$
wenzelm@3162
  1442
  in an introduction rule!
wenzelm@3162
  1443
wenzelm@3162
  1444
\item[\it constructor definitions] contain definitions of constants
wenzelm@3162
  1445
  appearing in the introduction rules.  The (co)datatype package supplies
wenzelm@3162
  1446
  the constructors' definitions here.  Most (co)inductive definitions omit
wenzelm@3162
  1447
  this section; one exception is the primitive recursive functions example
wenzelm@3162
  1448
  (\S\ref{primrec-sec}).
wenzelm@3162
  1449
wenzelm@3162
  1450
\item[\it type\_intrs] consists of introduction rules for type-checking the
wenzelm@3162
  1451
  definition, as discussed in~\S\ref{basic-sec}.  They are applied using
wenzelm@3162
  1452
  depth-first search; you can trace the proof by setting
wenzelm@3162
  1453
wenzelm@3162
  1454
  \verb|trace_DEPTH_FIRST := true|.
wenzelm@3162
  1455
wenzelm@3162
  1456
\item[\it type\_elims] consists of elimination rules for type-checking the
wenzelm@3162
  1457
  definition.  They are presumed to be safe and are applied as much as
wenzelm@3162
  1458
  possible, prior to the {\tt type\_intrs} search.
wenzelm@3162
  1459
\end{description}
wenzelm@3162
  1460
wenzelm@3162
  1461
The package has a few notable restrictions:
wenzelm@3162
  1462
\begin{itemize}
wenzelm@3162
  1463
\item The theory must separately declare the recursive sets as
wenzelm@3162
  1464
  constants.
wenzelm@3162
  1465
wenzelm@3162
  1466
\item The names of the recursive sets must be identifiers, not infix
wenzelm@3162
  1467
operators.  
wenzelm@3162
  1468
wenzelm@3162
  1469
\item Side-conditions must not be conjunctions.  However, an introduction rule
wenzelm@3162
  1470
may contain any number of side-conditions.
wenzelm@3162
  1471
wenzelm@3162
  1472
\item Side-conditions of the form $x=t$, where the variable~$x$ does not
wenzelm@3162
  1473
  occur in~$t$, will be substituted through the rule \verb|mutual_induct|.
wenzelm@3162
  1474
\end{itemize}
wenzelm@3162
  1475
wenzelm@3162
  1476
Isabelle/\textsc{hol} uses a simplified syntax for inductive definitions,
wenzelm@3162
  1477
thanks to type-checking.  There are no \texttt{domains}, \texttt{type\_intrs}
wenzelm@3162
  1478
or \texttt{type\_elims} parts.
wenzelm@3162
  1479
wenzelm@3162
  1480
wenzelm@3162
  1481
\section{Datatype and codatatype definitions: users guide}
wenzelm@3162
  1482
This section explains how to include (co)datatype declarations in a theory
wenzelm@3162
  1483
file.  Please include {\tt Datatype} as a parent theory; this makes available
wenzelm@3162
  1484
the definitions of $\univ$ and $\quniv$.
wenzelm@3162
  1485
wenzelm@3162
  1486
wenzelm@3162
  1487
\subsection{The result structure}
wenzelm@3162
  1488
The result structure extends that of (co)inductive definitions
wenzelm@3162
  1489
(Figure~\ref{def-result-fig}) with several additional items:
wenzelm@3162
  1490
\begin{ttbox}
wenzelm@3162
  1491
val con_defs  : thm list
wenzelm@3162
  1492
val case_eqns : thm list
wenzelm@3162
  1493
val free_iffs : thm list
wenzelm@3162
  1494
val free_SEs  : thm list
wenzelm@3162
  1495
val mk_free   : string -> thm
wenzelm@3162
  1496
\end{ttbox}
wenzelm@3162
  1497
Most of these have been discussed in~\S\ref{data-sec}.  Here is a summary:
wenzelm@3162
  1498
\begin{description}
wenzelm@3162
  1499
\item[\tt con\_defs] is a list of definitions: the case operator followed by
wenzelm@3162
  1500
the constructors.  This theorem list can be supplied to \verb|mk_cases|, for
wenzelm@3162
  1501
example.
wenzelm@3162
  1502
wenzelm@3162
  1503
\item[\tt case\_eqns] is a list of equations, stating that the case operator
wenzelm@3162
  1504
inverts each constructor.
wenzelm@3162
  1505
wenzelm@3162
  1506
\item[\tt free\_iffs] is a list of logical equivalences to perform freeness
wenzelm@3162
  1507
reasoning by rewriting.  A typical application has the form
wenzelm@3162
  1508
\begin{ttbox}
wenzelm@3162
  1509
by (asm_simp_tac (ZF_ss addsimps free_iffs) 1);
wenzelm@3162
  1510
\end{ttbox}
wenzelm@3162
  1511
wenzelm@3162
  1512
\item[\tt free\_SEs] is a list of safe elimination rules to perform freeness
wenzelm@3162
  1513
reasoning.  It can be supplied to \verb|eresolve_tac| or to the classical
wenzelm@3162
  1514
reasoner:
wenzelm@3162
  1515
\begin{ttbox} 
wenzelm@3162
  1516
by (fast_tac (ZF_cs addSEs free_SEs) 1);
wenzelm@3162
  1517
\end{ttbox}
wenzelm@3162
  1518
wenzelm@3162
  1519
\item[\tt mk\_free] is a function to prove freeness properties, specified as
wenzelm@3162
  1520
strings.  The theorems can be expressed in various forms, such as logical
wenzelm@3162
  1521
equivalences or elimination rules.
wenzelm@3162
  1522
\end{description}
wenzelm@3162
  1523
wenzelm@3162
  1524
The result structure also inherits everything from the underlying
wenzelm@3162
  1525
(co)inductive definition, such as the introduction rules, elimination rule,
wenzelm@3162
  1526
and (co)induction rule.
wenzelm@3162
  1527
wenzelm@3162
  1528
wenzelm@3162
  1529
\subsection{The syntax of a (co)datatype definition}
wenzelm@3162
  1530
A datatype definition has the form
wenzelm@3162
  1531
\begin{ttbox}
wenzelm@3162
  1532
datatype <={\it domain}
wenzelm@3162
  1533
 {\it datatype declaration} and {\it datatype declaration} and \ldots
wenzelm@3162
  1534
  monos      {\it monotonicity theorems}
wenzelm@3162
  1535
  type_intrs {\it introduction rules for type-checking}
wenzelm@3162
  1536
  type_elims {\it elimination rules for type-checking}
wenzelm@3162
  1537
\end{ttbox}
wenzelm@3162
  1538
A codatatype definition is identical save that it starts with the keyword {\tt
wenzelm@3162
  1539
  codatatype}.
wenzelm@3162
  1540
wenzelm@3162
  1541
The {\tt monos}, {\tt type\_intrs} and {\tt type\_elims} sections are
wenzelm@3162
  1542
optional.  They are treated like their counterparts in a (co)inductive
wenzelm@3162
  1543
definition, as described above.  The package supplements your type-checking
wenzelm@3162
  1544
rules (if any) with additional ones that should cope with any
wenzelm@3162
  1545
finitely-branching (co)datatype definition.
wenzelm@3162
  1546
wenzelm@3162
  1547
\begin{description}
wenzelm@3162
  1548
\item[\it domain] specifies a single domain to use for all the mutually
wenzelm@3162
  1549
  recursive (co)datatypes.  If it (and the preceeding~{\tt <=}) are
wenzelm@3162
  1550
  omitted, the package supplies a domain automatically.  Suppose the
wenzelm@3162
  1551
  definition involves the set parameters $A_1$, \ldots, $A_k$.  Then
wenzelm@3162
  1552
  $\univ(A_1\un\cdots\un A_k)$ is used for a datatype definition and
wenzelm@3162
  1553
  $\quniv(A_1\un\cdots\un A_k)$ is used for a codatatype definition.
wenzelm@3162
  1554
wenzelm@3162
  1555
  These choices should work for all finitely-branching (co)datatype
wenzelm@3162
  1556
  definitions.  For examples of infinitely-branching datatypes, 
wenzelm@3162
  1557
  see file {\tt ZF/ex/Brouwer.thy}.
wenzelm@3162
  1558
wenzelm@3162
  1559
\item[\it datatype declaration] has the form
wenzelm@3162
  1560
\begin{quote}
wenzelm@3162
  1561
 {\it string\/} {\tt =} {\it constructor} {\tt|} {\it constructor} {\tt|}
wenzelm@3162
  1562
 \ldots 
wenzelm@3162
  1563
\end{quote}
wenzelm@3162
  1564
The {\it string\/} is the datatype, say {\tt"list(A)"}.  Each
wenzelm@3162
  1565
{\it constructor\/} has the form 
wenzelm@3162
  1566
\begin{quote}
wenzelm@3162
  1567
 {\it name\/} {\tt(} {\it premise} {\tt,} {\it premise} {\tt,} \ldots {\tt)}
wenzelm@3162
  1568
 {\it mixfix\/}
wenzelm@3162
  1569
\end{quote}
wenzelm@3162
  1570
The {\it name\/} specifies a new constructor while the {\it premises\/} its
wenzelm@3162
  1571
typing conditions.  The optional {\it mixfix\/} phrase may give
wenzelm@3162
  1572
the constructor infix, for example.
wenzelm@3162
  1573
wenzelm@3162
  1574
Mutually recursive {\it datatype declarations\/} are separated by the
wenzelm@3162
  1575
keyword~{\tt and}.
wenzelm@3162
  1576
\end{description}
wenzelm@3162
  1577
wenzelm@3162
  1578
Isabelle/\textsc{hol}'s datatype definition package is (as of this writing)
wenzelm@3162
  1579
entirely different from Isabelle/\textsc{zf}'s.  The syntax is different, and
wenzelm@3162
  1580
instead of making an inductive definition it asserts axioms.
wenzelm@3162
  1581
wenzelm@3162
  1582
\paragraph*{Note.}
wenzelm@3162
  1583
In the definitions of the constructors, the right-hand sides may overlap.
wenzelm@3162
  1584
For instance, the datatype of combinators has constructors defined by
wenzelm@3162
  1585
\begin{eqnarray*}
wenzelm@3162
  1586
  {\tt K} & \equiv & \Inl(\emptyset) \\
wenzelm@3162
  1587
  {\tt S} & \equiv & \Inr(\Inl(\emptyset)) \\
wenzelm@3162
  1588
  p{\tt\#}q & \equiv & \Inr(\Inl(\pair{p,q})) 
wenzelm@3162
  1589
\end{eqnarray*}
wenzelm@3162
  1590
Unlike in previous versions of Isabelle, \verb|fold_tac| now ensures that the
wenzelm@3162
  1591
longest right-hand sides are folded first.
wenzelm@3162
  1592
wenzelm@3162
  1593
\fi
wenzelm@3162
  1594
\end{document}