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