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