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