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