1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/doc-src/ind-defs.tex Tue Nov 09 16:47:38 1993 +0100
1.3 @@ -0,0 +1,1585 @@
1.4 +\documentstyle[11pt,a4,proof,lcp,alltt,amssymbols,draft]{article}
1.5 +\newif\ifCADE
1.6 +\CADEfalse
1.7 +
1.8 +\title{A Fixedpoint Approach to Implementing (Co-)Inductive Definitions\\
1.9 + DRAFT\thanks{Research funded by the SERC (grants GR/G53279,
1.10 + GR/H40570) and by the ESPRIT Basic Research Action 6453 `Types'.}}
1.11 +
1.12 +\author{{\em Lawrence C. Paulson}\\
1.13 + Computer Laboratory, University of Cambridge}
1.14 +\date{\today}
1.15 +\setcounter{secnumdepth}{2} \setcounter{tocdepth}{2}
1.16 +
1.17 +\def\picture #1 by #2 (#3){% pictures: width by height (name)
1.18 + \message{Picture #3}
1.19 + \vbox to #2{\hrule width #1 height 0pt depth 0pt
1.20 + \vfill \special{picture #3}}}
1.21 +
1.22 +
1.23 +\newcommand\sbs{\subseteq}
1.24 +\newcommand\List[1]{\lbrakk#1\rbrakk}
1.25 +\let\To=\Rightarrow
1.26 +\newcommand\Var[1]{{?\!#1}}
1.27 +
1.28 +
1.29 +%%%\newcommand\Pow{{\tt Pow}}
1.30 +\let\pow=\wp
1.31 +\newcommand\RepFun{{\tt RepFun}}
1.32 +\newcommand\pair[1]{\langle#1\rangle}
1.33 +\newcommand\cons{{\tt cons}}
1.34 +\def\succ{{\tt succ}}
1.35 +\newcommand\split{{\tt split}}
1.36 +\newcommand\fst{{\tt fst}}
1.37 +\newcommand\snd{{\tt snd}}
1.38 +\newcommand\converse{{\tt converse}}
1.39 +\newcommand\domain{{\tt domain}}
1.40 +\newcommand\range{{\tt range}}
1.41 +\newcommand\field{{\tt field}}
1.42 +\newcommand\bndmono{\hbox{\tt bnd\_mono}}
1.43 +\newcommand\lfp{{\tt lfp}}
1.44 +\newcommand\gfp{{\tt gfp}}
1.45 +\newcommand\id{{\tt id}}
1.46 +\newcommand\trans{{\tt trans}}
1.47 +\newcommand\wf{{\tt wf}}
1.48 +\newcommand\wfrec{\hbox{\tt wfrec}}
1.49 +\newcommand\nat{{\tt nat}}
1.50 +\newcommand\natcase{\hbox{\tt nat\_case}}
1.51 +\newcommand\transrec{{\tt transrec}}
1.52 +\newcommand\rank{{\tt rank}}
1.53 +\newcommand\univ{{\tt univ}}
1.54 +\newcommand\Vrec{{\tt Vrec}}
1.55 +\newcommand\Inl{{\tt Inl}}
1.56 +\newcommand\Inr{{\tt Inr}}
1.57 +\newcommand\case{{\tt case}}
1.58 +\newcommand\lst{{\tt list}}
1.59 +\newcommand\Nil{{\tt Nil}}
1.60 +\newcommand\Cons{{\tt Cons}}
1.61 +\newcommand\lstcase{\hbox{\tt list\_case}}
1.62 +\newcommand\lstrec{\hbox{\tt list\_rec}}
1.63 +\newcommand\length{{\tt length}}
1.64 +\newcommand\listn{{\tt listn}}
1.65 +\newcommand\acc{{\tt acc}}
1.66 +\newcommand\primrec{{\tt primrec}}
1.67 +\newcommand\SC{{\tt SC}}
1.68 +\newcommand\CONST{{\tt CONST}}
1.69 +\newcommand\PROJ{{\tt PROJ}}
1.70 +\newcommand\COMP{{\tt COMP}}
1.71 +\newcommand\PREC{{\tt PREC}}
1.72 +
1.73 +\newcommand\quniv{{\tt quniv}}
1.74 +\newcommand\llist{{\tt llist}}
1.75 +\newcommand\LNil{{\tt LNil}}
1.76 +\newcommand\LCons{{\tt LCons}}
1.77 +\newcommand\lconst{{\tt lconst}}
1.78 +\newcommand\lleq{{\tt lleq}}
1.79 +\newcommand\map{{\tt map}}
1.80 +\newcommand\term{{\tt term}}
1.81 +\newcommand\Apply{{\tt Apply}}
1.82 +\newcommand\termcase{{\tt term\_case}}
1.83 +\newcommand\rev{{\tt rev}}
1.84 +\newcommand\reflect{{\tt reflect}}
1.85 +\newcommand\tree{{\tt tree}}
1.86 +\newcommand\forest{{\tt forest}}
1.87 +\newcommand\Part{{\tt Part}}
1.88 +\newcommand\TF{{\tt tree\_forest}}
1.89 +\newcommand\Tcons{{\tt Tcons}}
1.90 +\newcommand\Fcons{{\tt Fcons}}
1.91 +\newcommand\Fnil{{\tt Fnil}}
1.92 +\newcommand\TFcase{\hbox{\tt TF\_case}}
1.93 +\newcommand\Fin{{\tt Fin}}
1.94 +\newcommand\QInl{{\tt QInl}}
1.95 +\newcommand\QInr{{\tt QInr}}
1.96 +\newcommand\qsplit{{\tt qsplit}}
1.97 +\newcommand\qcase{{\tt qcase}}
1.98 +\newcommand\Con{{\tt Con}}
1.99 +\newcommand\data{{\tt data}}
1.100 +
1.101 +\sloppy
1.102 +\binperiod %%%treat . like a binary operator
1.103 +
1.104 +\begin{document}
1.105 +\pagestyle{empty}
1.106 +\begin{titlepage}
1.107 +\maketitle
1.108 +\begin{abstract}
1.109 + Several theorem provers provide commands for formalizing recursive
1.110 + datatypes and/or inductively defined sets. This paper presents a new
1.111 + approach, based on fixedpoint definitions. It is unusually general:
1.112 + it admits all monotone inductive definitions. It is conceptually simple,
1.113 + which has allowed the easy implementation of mutual recursion and other
1.114 + conveniences. It also handles co-inductive definitions: simply replace
1.115 + the least fixedpoint by a greatest fixedpoint. This represents the first
1.116 + automated support for co-inductive definitions.
1.117 +
1.118 + Examples include lists of $n$ elements, the accessible part of a relation
1.119 + and the set of primitive recursive functions. One example of a
1.120 + co-inductive definition is bisimulations for lazy lists. \ifCADE\else
1.121 + Recursive datatypes are examined in detail, as well as one example of a
1.122 + ``co-datatype'': lazy lists. The appendices are simple user's manuals
1.123 + for this Isabelle/ZF package.\fi
1.124 +
1.125 + The method has been implemented in Isabelle's ZF set theory. It should
1.126 + be applicable to any logic in which the Knaster-Tarski Theorem can be
1.127 + proved. The paper briefly describes a method of formalizing
1.128 + non-well-founded data structures in standard ZF set theory.
1.129 +\end{abstract}
1.130 +%
1.131 +\begin{center} Copyright \copyright{} \number\year{} by Lawrence C. Paulson
1.132 +\end{center}
1.133 +\thispagestyle{empty}
1.134 +\end{titlepage}
1.135 +
1.136 +\tableofcontents
1.137 +\cleardoublepage
1.138 +\pagenumbering{arabic}\pagestyle{headings}\DRAFT
1.139 +
1.140 +\section{Introduction}
1.141 +Several theorem provers provide commands for formalizing recursive data
1.142 +structures, like lists and trees. Examples include Boyer and Moore's shell
1.143 +principle~\cite{bm79} and Melham's recursive type package for the HOL
1.144 +system~\cite{melham89}. Such data structures are called {\bf datatypes}
1.145 +below, by analogy with {\tt datatype} definitions in Standard~ML\@.
1.146 +
1.147 +A datatype is but one example of a {\bf inductive definition}. This
1.148 +specifies the least set closed under given rules~\cite{aczel77}. The
1.149 +collection of theorems in a logic is inductively defined. A structural
1.150 +operational semantics~\cite{hennessy90} is an inductive definition of a
1.151 +reduction or evaluation relation on programs. A few theorem provers
1.152 +provide commands for formalizing inductive definitions; these include
1.153 +Coq~\cite{paulin92} and again the HOL system~\cite{camilleri92}.
1.154 +
1.155 +The dual notion is that of a {\bf co-inductive definition}. This specifies
1.156 +the greatest set closed under given rules. Important examples include
1.157 +using bisimulation relations to formalize equivalence of
1.158 +processes~\cite{milner89} or lazy functional programs~\cite{abramsky90}.
1.159 +Other examples include lazy lists and other infinite data structures; these
1.160 +are called {\bf co-datatypes} below.
1.161 +
1.162 +Most existing implementations of datatype and inductive definitions accept
1.163 +an artifically narrow class of inputs, and are not easily extended. The
1.164 +shell principle and Coq's inductive definition rules are built into the
1.165 +underlying logic. Melham's packages derive datatypes and inductive
1.166 +definitions from specialized constructions in higher-order logic.
1.167 +
1.168 +This paper describes a package based on a fixedpoint approach. Least
1.169 +fixedpoints yield inductive definitions; greatest fixedpoints yield
1.170 +co-inductive definitions. The package is uniquely powerful:
1.171 +\begin{itemize}
1.172 +\item It accepts the largest natural class of inductive definitions, namely
1.173 + all monotone inductive definitions.
1.174 +\item It accepts a wide class of datatype definitions.
1.175 +\item It handles co-inductive and co-datatype definitions. Most of
1.176 + the discussion below applies equally to inductive and co-inductive
1.177 + definitions, and most of the code is shared. To my knowledge, this is
1.178 + the only package supporting co-inductive definitions.
1.179 +\item Definitions may be mutually recursive.
1.180 +\end{itemize}
1.181 +The package is implemented in Isabelle~\cite{isabelle-intro}, using ZF set
1.182 +theory \cite{paulson-set-I,paulson-set-II}. However, the fixedpoint
1.183 +approach is independent of Isabelle. The recursion equations are specified
1.184 +as introduction rules for the mutually recursive sets. The package
1.185 +transforms these rules into a mapping over sets, and attempts to prove that
1.186 +the mapping is monotonic and well-typed. If successful, the package
1.187 +makes fixedpoint definitions and proves the introduction, elimination and
1.188 +(co-)induction rules. The package consists of several Standard ML
1.189 +functors~\cite{paulson91}; it accepts its argument and returns its result
1.190 +as ML structures.
1.191 +
1.192 +Most datatype packages equip the new datatype with some means of expressing
1.193 +recursive functions. This is the main thing lacking from my package. Its
1.194 +fixedpoint operators define recursive sets, not recursive functions. But
1.195 +the Isabelle/ZF theory provides well-founded recursion and other logical
1.196 +tools to simplify this task~\cite{paulson-set-II}.
1.197 +
1.198 +\S2 briefly introduces the least and greatest fixedpoint operators. \S3
1.199 +discusses the form of introduction rules, mutual recursion and other points
1.200 +common to inductive and co-inductive definitions. \S4 discusses induction
1.201 +and co-induction rules separately. \S5 presents several examples,
1.202 +including a co-inductive definition. \S6 describes datatype definitions,
1.203 +while \S7 draws brief conclusions. \ifCADE\else The appendices are simple
1.204 +user's manuals for this Isabelle/ZF package.\fi
1.205 +
1.206 +Most of the definitions and theorems shown below have been generated by the
1.207 +package. I have renamed some variables to improve readability.
1.208 +
1.209 +\section{Fixedpoint operators}
1.210 +In set theory, the least and greatest fixedpoint operators are defined as
1.211 +follows:
1.212 +\begin{eqnarray*}
1.213 + \lfp(D,h) & \equiv & \inter\{X\sbs D. h(X)\sbs X\} \\
1.214 + \gfp(D,h) & \equiv & \union\{X\sbs D. X\sbs h(X)\}
1.215 +\end{eqnarray*}
1.216 +Say that $h$ is {\bf bounded by}~$D$ if $h(D)\sbs D$, and {\bf monotone} if
1.217 +$h(A)\sbs h(B)$ for all $A$ and $B$ such that $A\sbs B\sbs D$. If $h$ is
1.218 +bounded by~$D$ and monotone then both operators yield fixedpoints:
1.219 +\begin{eqnarray*}
1.220 + \lfp(D,h) & = & h(\lfp(D,h)) \\
1.221 + \gfp(D,h) & = & h(\gfp(D,h))
1.222 +\end{eqnarray*}
1.223 +These equations are instances of the Knaster-Tarski theorem, which states
1.224 +that every monotonic function over a complete lattice has a
1.225 +fixedpoint~\cite{davey&priestley}. It is obvious from their definitions
1.226 +that $\lfp$ must be the least fixedpoint, and $\gfp$ the greatest.
1.227 +
1.228 +This fixedpoint theory is simple. The Knaster-Tarski theorem is easy to
1.229 +prove. Showing monotonicity of~$h$ is trivial, in typical cases. We must
1.230 +also exhibit a bounding set~$D$ for~$h$. Sometimes this is trivial, as
1.231 +when a set of ``theorems'' is (co-)inductively defined over some previously
1.232 +existing set of ``formulae.'' But defining the bounding set for
1.233 +(co-)datatype definitions requires some effort; see~\S\ref{data-sec} below.
1.234 +
1.235 +
1.236 +\section{Elements of an inductive or co-inductive definition}\label{basic-sec}
1.237 +Consider a (co-)inductive definition of the sets $R_1$, \ldots,~$R_n$, in
1.238 +mutual recursion. They will be constructed from previously existing sets
1.239 +$D_1$, \ldots,~$D_n$, respectively, which are called their {\bf domains}.
1.240 +The construction yields not $R_i\sbs D_i$ but $R_i\sbs D_1+\cdots+D_n$, where
1.241 +$R_i$ is the image of~$D_i$ under an injection~\cite[\S4.5]{paulson-set-II}.
1.242 +
1.243 +The definition may involve arbitrary parameters $\vec{p}=p_1$,
1.244 +\ldots,~$p_k$. Each recursive set then has the form $R_i(\vec{p})$. The
1.245 +parameters must be identical every time they occur within a definition. This
1.246 +would appear to be a serious restriction compared with other systems such as
1.247 +Coq~\cite{paulin92}. For instance, we cannot define the lists of
1.248 +$n$ elements as the set $\listn(A,n)$ using rules where the parameter~$n$
1.249 +varies. \S\ref{listn-sec} describes how to express this definition using the
1.250 +package.
1.251 +
1.252 +To avoid clutter below, the recursive sets are shown as simply $R_i$
1.253 +instead of $R_i(\vec{p})$.
1.254 +
1.255 +\subsection{The form of the introduction rules}\label{intro-sec}
1.256 +The body of the definition consists of the desired introduction rules,
1.257 +specified as strings. The conclusion of each rule must have the form $t\in
1.258 +R_i$, where $t$ is any term. Premises typically have the same form, but
1.259 +they can have the more general form $t\in M(R_i)$ or express arbitrary
1.260 +side-conditions.
1.261 +
1.262 +The premise $t\in M(R_i)$ is permitted if $M$ is a monotonic operator on
1.263 +sets, satisfying the rule
1.264 +\[ \infer{M(A)\sbs M(B)}{A\sbs B} \]
1.265 +The inductive definition package must be supplied monotonicity rules for
1.266 +all such premises.
1.267 +
1.268 +Because any monotonic $M$ may appear in premises, the criteria for an
1.269 +acceptable definition is semantic rather than syntactic. A suitable choice
1.270 +of~$M$ and~$t$ can express a lot. The powerset operator $\pow$ is
1.271 +monotone, and the premise $t\in\pow(R)$ expresses $t\sbs R$; see
1.272 +\S\ref{acc-sec} for an example. The `list of' operator is monotone, and
1.273 +the premise $t\in\lst(R)$ avoids having to encode the effect of~$\lst(R)$
1.274 +using mutual recursion; see \S\ref{primrec-sec} and also my earlier
1.275 +paper~\cite[\S4.4]{paulson-set-II}.
1.276 +
1.277 +Introduction rules may also contain {\bf side-conditions}. These are
1.278 +premises consisting of arbitrary formulae not mentioning the recursive
1.279 +sets. Side-conditions typically involve type-checking. One example is the
1.280 +premise $a\in A$ in the following rule from the definition of lists:
1.281 +\[ \infer{\Cons(a,l)\in\lst(A)}{a\in A & l\in\lst(A)} \]
1.282 +
1.283 +\subsection{The fixedpoint definitions}
1.284 +The package translates the list of desired introduction rules into a fixedpoint
1.285 +definition. Consider, as a running example, the finite set operator
1.286 +$\Fin(A)$: the set of all finite subsets of~$A$. It can be
1.287 +defined as the least set closed under the rules
1.288 +\[ \emptyset\in\Fin(A) \qquad
1.289 + \infer{\{a\}\un b\in\Fin(A)}{a\in A & b\in\Fin(A)}
1.290 +\]
1.291 +
1.292 +The domain for a (co-)inductive definition must be some existing set closed
1.293 +under the rules. A suitable domain for $\Fin(A)$ is $\pow(A)$, the set of all
1.294 +subsets of~$A$. The package generates the definition
1.295 +\begin{eqnarray*}
1.296 + \Fin(A) & \equiv & \lfp(\pow(A), \;
1.297 + \begin{array}[t]{r@{\,}l}
1.298 + \lambda X. \{z\in\pow(A). & z=\emptyset \disj{} \\
1.299 + &(\exists a\,b. z=\{a\}\un b\conj a\in A\conj b\in X)\})
1.300 + \end{array}
1.301 +\end{eqnarray*}
1.302 +The contribution of each rule to the definition of $\Fin(A)$ should be
1.303 +obvious. A co-inductive definition is similar but uses $\gfp$ instead
1.304 +of~$\lfp$.
1.305 +
1.306 +The package must prove that the fixedpoint operator is applied to a
1.307 +monotonic function. If the introduction rules have the form described
1.308 +above, and if the package is supplied a monotonicity theorem for every
1.309 +$t\in M(R_i)$ premise, then this proof is trivial.\footnote{Due to the
1.310 + presence of logical connectives in the fixedpoint's body, the
1.311 + monotonicity proof requires some unusual rules. These state that the
1.312 + connectives $\conj$, $\disj$ and $\exists$ are monotonic with respect to
1.313 + the partial ordering on unary predicates given by $P\sqsubseteq Q$ if and
1.314 + only if $\forall x.P(x)\imp Q(x)$.}
1.315 +
1.316 +The result structure returns the definitions of the recursive sets as a theorem
1.317 +list called {\tt defs}. It also returns, as the theorem {\tt unfold}, a
1.318 +fixedpoint equation such as
1.319 +\begin{eqnarray*}
1.320 + \Fin(A) & = &
1.321 + \begin{array}[t]{r@{\,}l}
1.322 + \{z\in\pow(A). & z=\emptyset \disj{} \\
1.323 + &(\exists a\,b. z=\{a\}\un b\conj a\in A\conj b\in \Fin(A))\}
1.324 + \end{array}
1.325 +\end{eqnarray*}
1.326 +It also returns, as the theorem {\tt dom\_subset}, an inclusion such as
1.327 +$\Fin(A)\sbs\pow(A)$.
1.328 +
1.329 +
1.330 +\subsection{Mutual recursion} \label{mutual-sec}
1.331 +In a mutually recursive definition, the domain for the fixedoint construction
1.332 +is the disjoint sum of the domain~$D_i$ of each~$R_i$, for $i=1$,
1.333 +\ldots,~$n$. The package uses the injections of the
1.334 +binary disjoint sum, typically $\Inl$ and~$\Inr$, to express injections
1.335 +$h_{1,n}$, \ldots, $h_{n,n}$ for the $n$-ary disjoint sum $D_1+\cdots+D_n$.
1.336 +
1.337 +As discussed elsewhere \cite[\S4.5]{paulson-set-II}, Isabelle/ZF defines the
1.338 +operator $\Part$ to support mutual recursion. The set $\Part(A,h)$
1.339 +contains those elements of~$A$ having the form~$h(z)$:
1.340 +\begin{eqnarray*}
1.341 + \Part(A,h) & \equiv & \{x\in A. \exists z. x=h(z)\}.
1.342 +\end{eqnarray*}
1.343 +For mutually recursive sets $R_1$, \ldots,~$R_n$ with
1.344 +$n>1$, the package makes $n+1$ definitions. The first defines a set $R$ using
1.345 +a fixedpoint operator. The remaining $n$ definitions have the form
1.346 +\begin{eqnarray*}
1.347 + R_i & \equiv & \Part(R,h_{i,n}), \qquad i=1,\ldots, n.
1.348 +\end{eqnarray*}
1.349 +It follows that $R=R_1\un\cdots\un R_n$, where the $R_i$ are pairwise disjoint.
1.350 +
1.351 +
1.352 +\subsection{Proving the introduction rules}
1.353 +The uesr supplies the package with the desired form of the introduction
1.354 +rules. Once it has derived the theorem {\tt unfold}, it attempts
1.355 +to prove the introduction rules. From the user's point of view, this is the
1.356 +trickiest stage; the proofs often fail. The task is to show that the domain
1.357 +$D_1+\cdots+D_n$ of the combined set $R_1\un\cdots\un R_n$ is
1.358 +closed under all the introduction rules. This essentially involves replacing
1.359 +each~$R_i$ by $D_1+\cdots+D_n$ in each of the introduction rules and
1.360 +attempting to prove the result.
1.361 +
1.362 +Consider the $\Fin(A)$ example. After substituting $\pow(A)$ for $\Fin(A)$
1.363 +in the rules, the package must prove
1.364 +\[ \emptyset\in\pow(A) \qquad
1.365 + \infer{\{a\}\un b\in\pow(A)}{a\in A & b\in\pow(A)}
1.366 +\]
1.367 +Such proofs can be regarded as type-checking the definition. The user
1.368 +supplies the package with type-checking rules to apply. Usually these are
1.369 +general purpose rules from the ZF theory. They could however be rules
1.370 +specifically proved for a particular inductive definition; sometimes this is
1.371 +the easiest way to get the definition through!
1.372 +
1.373 +The package returns the introduction rules as the theorem list {\tt intrs}.
1.374 +
1.375 +\subsection{The elimination rule}
1.376 +The elimination rule, called {\tt elim}, is derived in a straightforward
1.377 +manner. Applying the rule performs a case analysis, with one case for each
1.378 +introduction rule. Continuing our example, the elimination rule for $\Fin(A)$
1.379 +is
1.380 +\[ \infer{Q}{x\in\Fin(A) & \infer*{Q}{[x=\emptyset]}
1.381 + & \infer*{Q}{[x=\{a\}\un b & a\in A &b\in\Fin(A)]_{a,b}} }
1.382 +\]
1.383 +The package also returns a function {\tt mk\_cases}, for generating simplified
1.384 +instances of the elimination rule. However, {\tt mk\_cases} only works for
1.385 +datatypes and for inductive definitions involving datatypes, such as an
1.386 +inductively defined relation between lists. It instantiates {\tt elim}
1.387 +with a user-supplied term, then simplifies the cases using the freeness of
1.388 +the underlying datatype.
1.389 +
1.390 +
1.391 +\section{Induction and co-induction rules}
1.392 +Here we must consider inductive and co-inductive definitions separately.
1.393 +For an inductive definition, the package returns an induction rule derived
1.394 +directly from the properties of least fixedpoints, as well as a modified
1.395 +rule for mutual recursion and inductively defined relations. For a
1.396 +co-inductive definition, the package returns a basic co-induction rule.
1.397 +
1.398 +\subsection{The basic induction rule}\label{basic-ind-sec}
1.399 +The basic rule, called simply {\tt induct}, is appropriate in most situations.
1.400 +For inductive definitions, it is strong rule induction~\cite{camilleri92}; for
1.401 +datatype definitions (see below), it is just structural induction.
1.402 +
1.403 +The induction rule for an inductively defined set~$R$ has the following form.
1.404 +The major premise is $x\in R$. There is a minor premise for each
1.405 +introduction rule:
1.406 +\begin{itemize}
1.407 +\item If the introduction rule concludes $t\in R_i$, then the minor premise
1.408 +is~$P(t)$.
1.409 +
1.410 +\item The minor premise's eigenvariables are precisely the introduction
1.411 +rule's free variables that are not parameters of~$R$ --- for instance, $A$
1.412 +is not an eigenvariable in the $\Fin(A)$ rule below.
1.413 +
1.414 +\item If the introduction rule has a premise $t\in R_i$, then the minor
1.415 +premise discharges the assumption $t\in R_i$ and the induction
1.416 +hypothesis~$P(t)$. If the introduction rule has a premise $t\in M(R_i)$
1.417 +then the minor premise discharges the single assumption
1.418 +\[ t\in M(\{z\in R_i. P(z)\}). \]
1.419 +Because $M$ is monotonic, this assumption implies $t\in M(R_i)$. The
1.420 +occurrence of $P$ gives the effect of an induction hypothesis, which may be
1.421 +exploited by appealing to properties of~$M$.
1.422 +\end{itemize}
1.423 +The rule for $\Fin(A)$ is
1.424 +\[ \infer{P(x)}{x\in\Fin(A) & P(\emptyset)
1.425 + & \infer*{P(\{a\}\un b)}{[a\in A & b\in\Fin(A) & P(b)]_{a,b}} }
1.426 +\]
1.427 +Stronger induction rules often suggest themselves. In the case of
1.428 +$\Fin(A)$, the Isabelle/ZF theory proceeds to derive a rule whose third
1.429 +premise discharges the extra assumption $a\not\in b$. Most special induction
1.430 +rules must be proved manually, but the package proves a rule for mutual
1.431 +induction and inductive relations.
1.432 +
1.433 +\subsection{Mutual induction}
1.434 +The mutual induction rule is called {\tt
1.435 +mutual\_induct}. It differs from the basic rule in several respects:
1.436 +\begin{itemize}
1.437 +\item Instead of a single predicate~$P$, it uses $n$ predicates $P_1$,
1.438 +\ldots,~$P_n$: one for each recursive set.
1.439 +
1.440 +\item There is no major premise such as $x\in R_i$. Instead, the conclusion
1.441 +refers to all the recursive sets:
1.442 +\[ (\forall z.z\in R_1\imp P_1(z))\conj\cdots\conj
1.443 + (\forall z.z\in R_n\imp P_n(z))
1.444 +\]
1.445 +Proving the premises simultaneously establishes $P_i(z)$ for $z\in
1.446 +R_i$ and $i=1$, \ldots,~$n$.
1.447 +
1.448 +\item If the domain of some $R_i$ is the Cartesian product
1.449 +$A_1\times\cdots\times A_m$, then the corresponding predicate $P_i$ takes $m$
1.450 +arguments and the corresponding conjunct of the conclusion is
1.451 +\[ (\forall z_1\ldots z_m.\pair{z_1,\ldots,z_m}\in R_i\imp P_i(z_1,\ldots,z_m))
1.452 +\]
1.453 +\end{itemize}
1.454 +The last point above simplifies reasoning about inductively defined
1.455 +relations. It eliminates the need to express properties of $z_1$,
1.456 +\ldots,~$z_m$ as properties of the tuple $\pair{z_1,\ldots,z_m}$.
1.457 +
1.458 +\subsection{Co-induction}\label{co-ind-sec}
1.459 +A co-inductive definition yields a primitive co-induction rule, with no
1.460 +refinements such as those for the induction rules. (Experience may suggest
1.461 +refinements later.) Consider the co-datatype of lazy lists as an example. For
1.462 +suitable definitions of $\LNil$ and $\LCons$, lazy lists may be defined as the
1.463 +greatest fixedpoint satisfying the rules
1.464 +\[ \LNil\in\llist(A) \qquad
1.465 + \infer[(-)]{\LCons(a,l)\in\llist(A)}{a\in A & l\in\llist(A)}
1.466 +\]
1.467 +The $(-)$ tag stresses that this is a co-inductive definition. A suitable
1.468 +domain for $\llist(A)$ is $\quniv(A)$, a set closed under variant forms of
1.469 +sum and product for representing infinite data structures
1.470 +(\S\ref{data-sec}). Co-inductive definitions use these variant sums and
1.471 +products.
1.472 +
1.473 +The package derives an {\tt unfold} theorem similar to that for $\Fin(A)$.
1.474 +Then it proves the theorem {\tt co\_induct}, which expresses that $\llist(A)$
1.475 +is the greatest solution to this equation contained in $\quniv(A)$:
1.476 +\[ \infer{a\in\llist(A)}{a\in X & X\sbs \quniv(A) &
1.477 + \infer*{z=\LNil\disj \bigl(\exists a\,l.\,
1.478 + \begin{array}[t]{@{}l}
1.479 + z=\LCons(a,l) \conj a\in A \conj{}\\
1.480 + l\in X\un\llist(A) \bigr)
1.481 + \end{array} }{[z\in X]_z}}
1.482 +\]
1.483 +Having $X\un\llist(A)$ instead of simply $X$ in the third premise above
1.484 +represents a slight strengthening of the greatest fixedpoint property. I
1.485 +discuss several forms of co-induction rules elsewhere~\cite{paulson-coind}.
1.486 +
1.487 +
1.488 +\section{Examples of inductive and co-inductive definitions}\label{ind-eg-sec}
1.489 +This section presents several examples: the finite set operator,
1.490 +lists of $n$ elements, bisimulations on lazy lists, the well-founded part
1.491 +of a relation, and the primitive recursive functions.
1.492 +
1.493 +\subsection{The finite set operator}
1.494 +The definition of finite sets has been discussed extensively above. Here
1.495 +is the corresponding ML invocation (note that $\cons(a,b)$ abbreviates
1.496 +$\{a\}\un b$ in Isabelle/ZF):
1.497 +\begin{ttbox}
1.498 +structure Fin = Inductive_Fun
1.499 + (val thy = Arith.thy addconsts [(["Fin"],"i=>i")];
1.500 + val rec_doms = [("Fin","Pow(A)")];
1.501 + val sintrs =
1.502 + ["0 : Fin(A)",
1.503 + "[| a: A; b: Fin(A) |] ==> cons(a,b) : Fin(A)"];
1.504 + val monos = [];
1.505 + val con_defs = [];
1.506 + val type_intrs = [empty_subsetI, cons_subsetI, PowI]
1.507 + val type_elims = [make_elim PowD]);
1.508 +\end{ttbox}
1.509 +The parent theory is obtained from {\tt Arith.thy} by adding the unary
1.510 +function symbol~$\Fin$. Its domain is specified as $\pow(A)$, where $A$ is
1.511 +the parameter appearing in the introduction rules. For type-checking, the
1.512 +package supplies the introduction rules:
1.513 +\[ \emptyset\sbs A \qquad
1.514 + \infer{\{a\}\un B\sbs C}{a\in C & B\sbs C}
1.515 +\]
1.516 +A further introduction rule and an elimination rule express the two
1.517 +directions of the equivalence $A\in\pow(B)\bimp A\sbs B$. Type-checking
1.518 +involves mostly introduction rules. When the package returns, we can refer
1.519 +to the $\Fin(A)$ introduction rules as {\tt Fin.intrs}, the induction rule
1.520 +as {\tt Fin.induct}, and so forth.
1.521 +
1.522 +\subsection{Lists of $n$ elements}\label{listn-sec}
1.523 +This has become a standard example in the
1.524 +literature. Following Paulin-Mohring~\cite{paulin92}, we could attempt to
1.525 +define a new datatype $\listn(A,n)$, for lists of length~$n$, as an $n$-indexed
1.526 +family of sets. But her introduction rules
1.527 +\[ {\tt Niln}\in\listn(A,0) \qquad
1.528 + \infer{{\tt Consn}(n,a,l)\in\listn(A,\succ(n))}
1.529 + {n\in\nat & a\in A & l\in\listn(A,n)}
1.530 +\]
1.531 +are not acceptable to the inductive definition package:
1.532 +$\listn$ occurs with three different parameter lists in the definition.
1.533 +
1.534 +\begin{figure}
1.535 +\begin{small}
1.536 +\begin{verbatim}
1.537 +structure ListN = Inductive_Fun
1.538 + (val thy = ListFn.thy addconsts [(["listn"],"i=>i")];
1.539 + val rec_doms = [("listn", "nat*list(A)")];
1.540 + val sintrs =
1.541 + ["<0,Nil> : listn(A)",
1.542 + "[| a: A; <n,l> : listn(A) |] ==> <succ(n), Cons(a,l)> : listn(A)"];
1.543 + val monos = [];
1.544 + val con_defs = [];
1.545 + val type_intrs = nat_typechecks@List.intrs@[SigmaI]
1.546 + val type_elims = [SigmaE2]);
1.547 +\end{verbatim}
1.548 +\end{small}
1.549 +\hrule
1.550 +\caption{Defining lists of $n$ elements} \label{listn-fig}
1.551 +\end{figure}
1.552 +
1.553 +There is an obvious way of handling this particular example, which may suggest
1.554 +a general approach to varying parameters. Here, we can take advantage of an
1.555 +existing datatype definition of $\lst(A)$, with constructors $\Nil$
1.556 +and~$\Cons$. Then incorporate the number~$n$ into the inductive set itself,
1.557 +defining $\listn(A)$ as a relation. It consists of pairs $\pair{n,l}$ such
1.558 +that $n\in\nat$ and~$l\in\lst(A)$ and $l$ has length~$n$. In fact,
1.559 +$\listn(A)$ turns out to be the converse of the length function on~$\lst(A)$.
1.560 +The Isabelle/ZF introduction rules are
1.561 +\[ \pair{0,\Nil}\in\listn(A) \qquad
1.562 + \infer{\pair{\succ(n),\Cons(a,l)}\in\listn(A)}
1.563 + {a\in A & \pair{n,l}\in\listn(A)}
1.564 +\]
1.565 +Figure~\ref{listn-fig} presents the ML invocation. A theory of lists,
1.566 +extended with a declaration of $\listn$, is the parent theory. The domain
1.567 +is specified as $\nat\times\lst(A)$. The type-checking rules include those
1.568 +for 0, $\succ$, $\Nil$ and $\Cons$. Because $\listn(A)$ is a set of pairs,
1.569 +type-checking also requires introduction and elimination rules to express
1.570 +both directions of the equivalence $\pair{a,b}\in A\times B \bimp a\in A
1.571 +\conj b\in B$.
1.572 +
1.573 +The package returns introduction, elimination and induction rules for
1.574 +$\listn$. The basic induction rule, {\tt ListN.induct}, is
1.575 +\[ \infer{P(x)}{x\in\listn(A) & P(\pair{0,\Nil}) &
1.576 + \infer*{P(\pair{\succ(n),\Cons(a,l)})}
1.577 + {[a\in A & \pair{n,l}\in\listn(A) & P(\pair{n,l})]_{a,l,n}}}
1.578 +\]
1.579 +This rule requires the induction formula to be a
1.580 +unary property of pairs,~$P(\pair{n,l})$. The alternative rule, {\tt
1.581 +ListN.mutual\_induct}, uses a binary property instead:
1.582 +\[ \infer{\forall n\,l. \pair{n,l}\in\listn(A) \imp P(\pair{n,l})}
1.583 + {P(0,\Nil) &
1.584 + \infer*{P(\succ(n),\Cons(a,l))}
1.585 + {[a\in A & \pair{n,l}\in\listn(A) & P(n,l)]_{a,l,n}}}
1.586 +\]
1.587 +It is now a simple matter to prove theorems about $\listn(A)$, such as
1.588 +\[ \forall l\in\lst(A). \pair{\length(l),\, l}\in\listn(A) \]
1.589 +\[ \listn(A)``\{n\} = \{l\in\lst(A). \length(l)=n\} \]
1.590 +This latter result --- here $r``A$ denotes the image of $A$ under $r$
1.591 +--- asserts that the inductive definition agrees with the obvious notion of
1.592 +$n$-element list.
1.593 +
1.594 +Unlike in Coq, the definition does not declare a new datatype. A `list of
1.595 +$n$ elements' really is a list, and is subject to list operators such
1.596 +as append. For example, a trivial induction yields
1.597 +\[ \infer{\pair{m\mathbin{+} m,\, l@l'}\in\listn(A)}
1.598 + {\pair{m,l}\in\listn(A) & \pair{m',l'}\in\listn(A)}
1.599 +\]
1.600 +where $+$ here denotes addition on the natural numbers and @ denotes append.
1.601 +
1.602 +\ifCADE\typeout{****Omitting mk_cases from CADE version!}
1.603 +\else
1.604 +\subsection{A demonstration of {\tt mk\_cases}}
1.605 +The elimination rule, {\tt ListN.elim}, is cumbersome:
1.606 +\[ \infer{Q}{x\in\listn(A) &
1.607 + \infer*{Q}{[x = \pair{0,\Nil}]} &
1.608 + \infer*{Q}
1.609 + {\left[\begin{array}{l}
1.610 + x = \pair{\succ(n),\Cons(a,l)} \\
1.611 + a\in A \\
1.612 + \pair{n,l}\in\listn(A)
1.613 + \end{array} \right]_{a,l,n}}}
1.614 +\]
1.615 +The function {\tt ListN.mk\_cases} generates simplified instances of this
1.616 +rule. It works by freeness reasoning on the list constructors.
1.617 +If $x$ is $\pair{i,\Nil}$ or $\pair{i,\Cons(a,l)}$ then {\tt ListN.mk\_cases}
1.618 +deduces the corresponding form of~$i$. For example,
1.619 +\begin{ttbox}
1.620 +ListN.mk_cases List.con_defs "<i,Cons(a,l)> : listn(A)"
1.621 +\end{ttbox}
1.622 +yields the rule
1.623 +\[ \infer{Q}{\pair{i, \Cons(a,l)}\in\listn(A) &
1.624 + \infer*{Q}
1.625 + {\left[\begin{array}{l}
1.626 + i = \succ(n) \\ a\in A \\ \pair{n,l}\in\listn(A)
1.627 + \end{array} \right]_{n}}}
1.628 +\]
1.629 +The package also has built-in rules for freeness reasoning about $0$
1.630 +and~$\succ$. So if $x$ is $\pair{0,l}$ or $\pair{\succ(i),l}$, then {\tt
1.631 +ListN.mk\_cases} can similarly deduce the corresponding form of~$l$.
1.632 +
1.633 +The function {\tt mk\_cases} is also useful with datatype definitions
1.634 +themselves. The version from the definition of lists, namely {\tt
1.635 +List.mk\_cases}, can prove the rule
1.636 +\[ \infer{Q}{\Cons(a,l)\in\lst(A) &
1.637 + & \infer*{Q}{[a\in A &l\in\lst(A)]} }
1.638 +\]
1.639 +The most important uses of {\tt mk\_cases} concern inductive definitions of
1.640 +evaluation relations. Then {\tt mk\_cases} supports the kind of backward
1.641 +inference typical of hand proofs, for example to prove that the evaluation
1.642 +relation is functional.
1.643 +\fi %CADE
1.644 +
1.645 +\subsection{A co-inductive definition: bisimulations on lazy lists}
1.646 +This example anticipates the definition of the co-datatype $\llist(A)$, which
1.647 +consists of lazy lists over~$A$. Its constructors are $\LNil$ and $\LCons$,
1.648 +satisfying the introduction rules shown in~\S\ref{co-ind-sec}.
1.649 +Because $\llist(A)$ is defined as a greatest fixedpoint and uses the variant
1.650 +pairing and injection operators, it contains non-well-founded elements such as
1.651 +solutions to $\LCons(a,l)=l$.
1.652 +
1.653 +The next step in the development of lazy lists is to define a co-induction
1.654 +principle for proving equalities. This is done by showing that the equality
1.655 +relation on lazy lists is the greatest fixedpoint of some monotonic
1.656 +operation. The usual approach~\cite{pitts94} is to define some notion of
1.657 +bisimulation for lazy lists, define equivalence to be the greatest
1.658 +bisimulation, and finally to prove that two lazy lists are equivalent if and
1.659 +only if they are equal. The co-induction rule for equivalence then yields a
1.660 +co-induction principle for equalities.
1.661 +
1.662 +A binary relation $R$ on lazy lists is a {\bf bisimulation} provided $R\sbs
1.663 +R^+$, where $R^+$ is the relation
1.664 +\[ \{\pair{\LNil;\LNil}\} \un
1.665 + \{\pair{\LCons(a,l);\LCons(a,l')} . a\in A \conj \pair{l;l'}\in R\}.
1.666 +\]
1.667 +Variant pairs are used, $\pair{l;l'}$ instead of $\pair{l,l'}$, because this
1.668 +is a co-inductive definition.
1.669 +
1.670 +A pair of lazy lists are {\bf equivalent} if they belong to some bisimulation.
1.671 +Equivalence can be co-inductively defined as the greatest fixedpoint for the
1.672 +introduction rules
1.673 +\[ \pair{\LNil;\LNil} \in\lleq(A) \qquad
1.674 + \infer[(-)]{\pair{\LCons(a,l);\LCons(a,l')} \in\lleq(A)}
1.675 + {a\in A & \pair{l;l'}\in \lleq(A)}
1.676 +\]
1.677 +To make this co-inductive definition, we invoke \verb|Co_Inductive_Fun|:
1.678 +\begin{ttbox}
1.679 +structure LList_Eq = Co_Inductive_Fun
1.680 +(val thy = LList.thy addconsts [(["lleq"],"i=>i")];
1.681 + val rec_doms = [("lleq", "llist(A) <*> llist(A)")];
1.682 + val sintrs =
1.683 + ["<LNil; LNil> : lleq(A)",
1.684 + "[| a:A; <l;l'>: lleq(A) |] ==> <LCons(a,l); LCons(a,l')>: lleq(A)"];
1.685 + val monos = [];
1.686 + val con_defs = [];
1.687 + val type_intrs = LList.intrs@[QSigmaI];
1.688 + val type_elims = [QSigmaE2]);
1.689 +\end{ttbox}
1.690 +Again, {\tt addconsts} declares a constant for $\lleq$ in the parent theory.
1.691 +The domain of $\lleq(A)$ is $\llist(A)\otimes\llist(A)$, where $\otimes$
1.692 +denotes the variant Cartesian product. The type-checking rules include the
1.693 +introduction rules for lazy lists as well as rules expressinve both
1.694 +definitions of the equivalence
1.695 +\[ \pair{a;b}\in A\otimes B \bimp a\in A \conj b\in B. \]
1.696 +
1.697 +The package returns the introduction rules and the elimination rule, as
1.698 +usual. But instead of induction rules, it returns a co-induction rule.
1.699 +The rule is too big to display in the usual notation; its conclusion is
1.700 +$a\in\lleq(A)$ and its premises are $a\in X$, $X\sbs \llist(A)\otimes\llist(A)$
1.701 +and
1.702 +\[ \infer*{z=\pair{\LNil;\LNil}\disj \bigl(\exists a\,l\,l'.\,
1.703 + \begin{array}[t]{@{}l}
1.704 + z=\pair{\LCons(a,l);\LCons(a,l')} \conj a\in A \conj{}\\
1.705 + \pair{l;l'}\in X\un\lleq(A) \bigr)
1.706 + \end{array} }{[z\in X]_z}
1.707 +\]
1.708 +Thus if $a\in X$, where $X$ is a bisimulation contained in the
1.709 +domain of $\lleq(A)$, then $a\in\lleq(A)$. It is easy to show that
1.710 +$\lleq(A)$ is reflexive: the equality relation is a bisimulation. And
1.711 +$\lleq(A)$ is symmetric: its converse is a bisimulation. But showing that
1.712 +$\lleq(A)$ coincides with the equality relation takes considerable work.
1.713 +
1.714 +\subsection{The accessible part of a relation}\label{acc-sec}
1.715 +Let $\prec$ be a binary relation on~$D$; in short, $(\prec)\sbs D\times D$.
1.716 +The {\bf accessible} or {\bf well-founded} part of~$\prec$, written
1.717 +$\acc(\prec)$, is essentially that subset of~$D$ for which $\prec$ admits
1.718 +no infinite decreasing chains~\cite{aczel77}. Formally, $\acc(\prec)$ is
1.719 +inductively defined to be the least set that contains $a$ if it contains
1.720 +all $\prec$-predecessors of~$a$, for $a\in D$. Thus we need an
1.721 +introduction rule of the form
1.722 +%%%%\[ \infer{a\in\acc(\prec)}{\infer*{y\in\acc(\prec)}{[y\prec a]_y}} \]
1.723 +\[ \infer{a\in\acc(\prec)}{\forall y.y\prec a\imp y\in\acc(\prec)} \]
1.724 +Paulin-Mohring treats this example in Coq~\cite{paulin92}, but it causes
1.725 +difficulties for other systems. Its premise does not conform to
1.726 +the structure of introduction rules for HOL's inductive definition
1.727 +package~\cite{camilleri92}. It is also unacceptable to Isabelle package
1.728 +(\S\ref{intro-sec}), but fortunately can be transformed into one of the
1.729 +form $t\in M(R)$.
1.730 +
1.731 +The powerset operator is monotonic, and $t\in\pow(R)$ is equivalent to
1.732 +$t\sbs R$. This in turn is equivalent to $\forall y\in t. y\in R$. To
1.733 +express $\forall y.y\prec a\imp y\in\acc(\prec)$ we need only find a
1.734 +term~$t$ such that $y\in t$ if and only if $y\prec a$. A suitable $t$ is
1.735 +the inverse image of~$\{a\}$ under~$\prec$.
1.736 +
1.737 +The ML invocation below follows this approach. Here $r$ is~$\prec$ and
1.738 +$\field(r)$ refers to~$D$, the domain of $\acc(r)$. Finally $r^{-}``\{a\}$
1.739 +denotes the inverse image of~$\{a\}$ under~$r$. The package is supplied
1.740 +the theorem {\tt Pow\_mono}, which asserts that $\pow$ is monotonic.
1.741 +\begin{ttbox}
1.742 +structure Acc = Inductive_Fun
1.743 + (val thy = WF.thy addconsts [(["acc"],"i=>i")];
1.744 + val rec_doms = [("acc", "field(r)")];
1.745 + val sintrs =
1.746 + ["[| r-``\{a\} : Pow(acc(r)); a : field(r) |] ==> a : acc(r)"];
1.747 + val monos = [Pow_mono];
1.748 + val con_defs = [];
1.749 + val type_intrs = [];
1.750 + val type_elims = []);
1.751 +\end{ttbox}
1.752 +The Isabelle theory proceeds to prove facts about $\acc(\prec)$. For
1.753 +instance, $\prec$ is well-founded if and only if its field is contained in
1.754 +$\acc(\prec)$.
1.755 +
1.756 +As mentioned in~\S\ref{basic-ind-sec}, a premise of the form $t\in M(R)$
1.757 +gives rise to an unusual induction hypothesis. Let us examine the
1.758 +induction rule, {\tt Acc.induct}:
1.759 +\[ \infer{P(x)}{x\in\acc(r) &
1.760 + \infer*{P(a)}{[r^{-}``\{a\}\in\pow(\{z\in\acc(r).P(z)\}) &
1.761 + a\in\field(r)]_a}}
1.762 +\]
1.763 +The strange induction hypothesis is equivalent to
1.764 +$\forall y. \pair{y,a}\in r\imp y\in\acc(r)\conj P(y)$.
1.765 +Therefore the rule expresses well-founded induction on the accessible part
1.766 +of~$\prec$.
1.767 +
1.768 +The use of inverse image is not essential. The Isabelle package can accept
1.769 +introduction rules with arbitrary premises of the form $\forall
1.770 +\vec{y}.P(\vec{y})\imp f(\vec{y})\in R$. The premise can be expressed
1.771 +equivalently as
1.772 +\[ \{z\in D. P(\vec{y}) \conj z=f(\vec{y})\} \]
1.773 +provided $f(\vec{y})\in D$ for all $\vec{y}$ such that~$P(\vec{y})$. The
1.774 +following section demonstrates another use of the premise $t\in M(R)$,
1.775 +where $M=\lst$.
1.776 +
1.777 +\subsection{The primitive recursive functions}\label{primrec-sec}
1.778 +The primitive recursive functions are traditionally defined inductively, as
1.779 +a subset of the functions over the natural numbers. One difficulty is that
1.780 +functions of all arities are taken together, but this is easily
1.781 +circumvented by regarding them as functions on lists. Another difficulty,
1.782 +the notion of composition, is less easily circumvented.
1.783 +
1.784 +Here is a more precise definition. Letting $\vec{x}$ abbreviate
1.785 +$x_0,\ldots,x_{n-1}$, we can write lists such as $[\vec{x}]$,
1.786 +$[y+1,\vec{x}]$, etc. A function is {\bf primitive recursive} if it
1.787 +belongs to the least set of functions in $\lst(\nat)\to\nat$ containing
1.788 +\begin{itemize}
1.789 +\item The {\bf successor} function $\SC$, such that $\SC[y,\vec{x}]=y+1$.
1.790 +\item All {\bf constant} functions $\CONST(k)$, such that
1.791 + $\CONST(k)[\vec{x}]=k$.
1.792 +\item All {\bf projection} functions $\PROJ(i)$, such that
1.793 + $\PROJ(i)[\vec{x}]=x_i$ if $0\leq i<n$.
1.794 +\item All {\bf compositions} $\COMP(g,[f_0,\ldots,f_{m-1}])$,
1.795 +where $g$ and $f_0$, \ldots, $f_{m-1}$ are primitive recursive,
1.796 +such that
1.797 +\begin{eqnarray*}
1.798 + \COMP(g,[f_0,\ldots,f_{m-1}])[\vec{x}] & = &
1.799 + g[f_0[\vec{x}],\ldots,f_{m-1}[\vec{x}]].
1.800 +\end{eqnarray*}
1.801 +
1.802 +\item All {\bf recursions} $\PREC(f,g)$, where $f$ and $g$ are primitive
1.803 + recursive, such that
1.804 +\begin{eqnarray*}
1.805 + \PREC(f,g)[0,\vec{x}] & = & f[\vec{x}] \\
1.806 + \PREC(f,g)[y+1,\vec{x}] & = & g[\PREC(f,g)[y,\vec{x}],\, y,\, \vec{x}].
1.807 +\end{eqnarray*}
1.808 +\end{itemize}
1.809 +Composition is awkward because it combines not two functions, as is usual,
1.810 +but $m+1$ functions. In her proof that Ackermann's function is not
1.811 +primitive recursive, Nora Szasz was unable to formalize this definition
1.812 +directly~\cite{szasz93}. So she generalized primitive recursion to
1.813 +tuple-valued functions. This modified the inductive definition such that
1.814 +each operation on primitive recursive functions combined just two functions.
1.815 +
1.816 +\begin{figure}
1.817 +\begin{ttbox}
1.818 +structure Primrec = Inductive_Fun
1.819 + (val thy = Primrec0.thy;
1.820 + val rec_doms = [("primrec", "list(nat)->nat")];
1.821 + val ext = None
1.822 + val sintrs =
1.823 + ["SC : primrec",
1.824 + "k: nat ==> CONST(k) : primrec",
1.825 + "i: nat ==> PROJ(i) : primrec",
1.826 + "[| g: primrec; fs: list(primrec) |] ==> COMP(g,fs): primrec",
1.827 + "[| f: primrec; g: primrec |] ==> PREC(f,g): primrec"];
1.828 + val monos = [list_mono];
1.829 + val con_defs = [SC_def,CONST_def,PROJ_def,COMP_def,PREC_def];
1.830 + val type_intrs = pr0_typechecks
1.831 + val type_elims = []);
1.832 +\end{ttbox}
1.833 +\hrule
1.834 +\caption{Inductive definition of the primitive recursive functions}
1.835 +\label{primrec-fig}
1.836 +\end{figure}
1.837 +\def\fs{{\it fs}}
1.838 +Szasz was using ALF, but Coq and HOL would also have problems accepting
1.839 +this definition. Isabelle's package accepts it easily since
1.840 +$[f_0,\ldots,f_{m-1}]$ is a list of primitive recursive functions and
1.841 +$\lst$ is monotonic. There are five introduction rules, one for each of
1.842 +the five forms of primitive recursive function. Note the one for $\COMP$:
1.843 +\[ \infer{\COMP(g,\fs)\in\primrec}{g\in\primrec & \fs\in\lst(\primrec)} \]
1.844 +The induction rule for $\primrec$ has one case for each introduction rule.
1.845 +Due to the use of $\lst$ as a monotone operator, the composition case has
1.846 +an unusual induction hypothesis:
1.847 + \[ \infer*{P(\COMP(g,\fs))}
1.848 + {[g\in\primrec & \fs\in\lst(\{z\in\primrec.P(x)\})]_{\fs,g}} \]
1.849 +The hypothesis states that $\fs$ is a list of primitive recursive functions
1.850 +satisfying the induction formula. Proving the $\COMP$ case typically requires
1.851 +structural induction on lists, yielding two subcases: either $\fs=\Nil$ or
1.852 +else $\fs=\Cons(f,\fs')$, where $f\in\primrec$, $P(f)$, and $\fs'$ is
1.853 +another list of primitive recursive functions satisfying~$P$.
1.854 +
1.855 +Figure~\ref{primrec-fig} presents the ML invocation. Theory {\tt
1.856 + Primrec0.thy} defines the constants $\SC$, etc.; their definitions
1.857 +consist of routine list programming and are omitted. The Isabelle theory
1.858 +goes on to formalize Ackermann's function and prove that it is not
1.859 +primitive recursive, using the induction rule {\tt Primrec.induct}. The
1.860 +proof follows Szasz's excellent account.
1.861 +
1.862 +ALF and Coq treat inductive definitions as datatypes, with a new
1.863 +constructor for each introduction rule. This forced Szasz to define a
1.864 +small programming language for the primitive recursive functions, and then
1.865 +define their semantics. But the Isabelle/ZF formulation defines the
1.866 +primitive recursive functions directly as a subset of the function set
1.867 +$\lst(\nat)\to\nat$. This saves a step and conforms better to mathematical
1.868 +tradition.
1.869 +
1.870 +
1.871 +\section{Datatypes and co-datatypes}\label{data-sec}
1.872 +A (co-)datatype definition is a (co-)inductive definition with automatically
1.873 +defined constructors and case analysis operator. The package proves that the
1.874 +case operator inverts the constructors, and can also prove freeness theorems
1.875 +involving any pair of constructors.
1.876 +
1.877 +
1.878 +\subsection{Constructors and their domain}
1.879 +Conceptually, our two forms of definition are distinct: a (co-)inductive
1.880 +definition selects a subset of an existing set, but a (co-)datatype
1.881 +definition creates a new set. But the package reduces the latter to the
1.882 +former. A set having strong closure properties must serve as the domain
1.883 +of the (co-)inductive definition. Constructing this set requires some
1.884 +theoretical effort. Consider first datatypes and then co-datatypes.
1.885 +
1.886 +Isabelle/ZF defines the standard notion of Cartesian product $A\times B$,
1.887 +containing ordered pairs $\pair{a,b}$. Now the $m$-tuple
1.888 +$\pair{x_1,\ldots\,x_m}$ is the empty set~$\emptyset$ if $m=0$, simply
1.889 +$x_1$ if $m=1$, and $\pair{x_1,\pair{x_2,\ldots\,x_m}}$ if $m\geq2$.
1.890 +Isabelle/ZF also defines the disjoint sum $A+B$, containing injections
1.891 +$\Inl(a)\equiv\pair{0,a}$ and $\Inr(b)\equiv\pair{1,b}$.
1.892 +
1.893 +A datatype constructor $\Con(x_1,\ldots\,x_m)$ is defined to be
1.894 +$h(\pair{x_1,\ldots\,x_m})$, where $h$ is composed of $\Inl$ and~$\Inr$.
1.895 +In a mutually recursive definition, all constructors for the set~$R_i$ have
1.896 +the outer form~$h_{i,n}$, where $h_{i,n}$ is the injection described
1.897 +in~\S\ref{mutual-sec}. Further nested injections ensure that the
1.898 +constructors for~$R_i$ are pairwise distinct.
1.899 +
1.900 +Isabelle/ZF defines the set $\univ(A)$, which contains~$A$ and
1.901 +furthermore contains $\pair{a,b}$, $\Inl(a)$ and $\Inr(b)$ for $a$,
1.902 +$b\in\univ(A)$. In a typical datatype definition with set parameters
1.903 +$A_1$, \ldots, $A_k$, a suitable domain for all the recursive sets is
1.904 +$\univ(A_1\un\cdots\un A_k)$. This solves the problem for
1.905 +datatypes~\cite[\S4.2]{paulson-set-II}.
1.906 +
1.907 +The standard pairs and injections can only yield well-founded
1.908 +constructions. This eases the (manual!) definition of recursive functions
1.909 +over datatypes. But they are unsuitable for co-datatypes, which typically
1.910 +contain non-well-founded objects.
1.911 +
1.912 +To support co-datatypes, Isabelle/ZF defines a variant notion of ordered
1.913 +pair, written~$\pair{a;b}$. It also defines the corresponding variant
1.914 +notion of Cartesian product $A\otimes B$, variant injections $\QInl(a)$
1.915 +and~$\QInr(b)$, and variant disjoint sum $A\oplus B$. Finally it defines
1.916 +the set $\quniv(A)$, which contains~$A$ and furthermore contains
1.917 +$\pair{a;b}$, $\QInl(a)$ and $\QInr(b)$ for $a$, $b\in\quniv(A)$. In a
1.918 +typical co-datatype definition with set parameters $A_1$, \ldots, $A_k$, a
1.919 +suitable domain is $\quniv(A_1\un\cdots\un A_k)$. This approach is an
1.920 +alternative to adopting an Anti-Foundation
1.921 +Axiom~\cite{aczel88}.\footnote{No reference is available. Variant pairs
1.922 + are defined by $\pair{a;b}\equiv a+b \equiv (\{0\}\times a) \un (\{1\}\times
1.923 + b)$, where $\times$ is the Cartesian product for standard ordered pairs. Now
1.924 + $\pair{a;b}$ is injective and monotonic in its two arguments.
1.925 + Non-well-founded constructions, such as infinite lists, are constructed
1.926 + as least fixedpoints; the bounding set typically has the form
1.927 + $\univ(a_1\un\cdots\un a_k)$, where $a_1$, \ldots, $a_k$ are specified
1.928 + elements of the construction.}
1.929 +
1.930 +
1.931 +\subsection{The case analysis operator}
1.932 +The (co-)datatype package automatically defines a case analysis operator,
1.933 +called {\tt$R$\_case}. A mutually recursive definition still has only
1.934 +one operator, called {\tt$R_1$\_\ldots\_$R_n$\_case}. The case operator is
1.935 +analogous to those for products and sums.
1.936 +
1.937 +Datatype definitions employ standard products and sums, whose operators are
1.938 +$\split$ and $\case$ and satisfy the equations
1.939 +\begin{eqnarray*}
1.940 + \split(f,\pair{x,y}) & = & f(x,y) \\
1.941 + \case(f,g,\Inl(x)) & = & f(x) \\
1.942 + \case(f,g,\Inr(y)) & = & g(y)
1.943 +\end{eqnarray*}
1.944 +Suppose the datatype has $k$ constructors $\Con_1$, \ldots,~$\Con_k$. Then
1.945 +its case operator takes $k+1$ arguments and satisfies an equation for each
1.946 +constructor:
1.947 +\begin{eqnarray*}
1.948 + R\hbox{\_case}(f_1,\ldots,f_k, {\tt Con}_i(\vec{x})) & = & f_i(\vec{x}),
1.949 + \qquad i = 1, \ldots, k
1.950 +\end{eqnarray*}
1.951 +Note that if $f$ and $g$ have meta-type $i\To i$ then so do $\split(f)$ and
1.952 +$\case(f,g)$. This works because $\split$ and $\case$ operate on their
1.953 +last argument. They are easily combined to make complex case analysis
1.954 +operators. Here are two examples:
1.955 +\begin{itemize}
1.956 +\item $\split(\lambda x.\split(f(x)))$ performs case analysis for
1.957 +$A\times (B\times C)$, as is easily verified:
1.958 +\begin{eqnarray*}
1.959 + \split(\lambda x.\split(f(x)), \pair{a,b,c})
1.960 + & = & (\lambda x.\split(f(x))(a,\pair{b,c}) \\
1.961 + & = & \split(f(a), \pair{b,c}) \\
1.962 + & = & f(a,b,c)
1.963 +\end{eqnarray*}
1.964 +
1.965 +\item $\case(f,\case(g,h))$ performs case analysis for $A+(B+C)$; let us
1.966 +verify one of the three equations:
1.967 +\begin{eqnarray*}
1.968 + \case(f,\case(g,h), \Inr(\Inl(b)))
1.969 + & = & \case(g,h,\Inl(b)) \\
1.970 + & = & g(b)
1.971 +\end{eqnarray*}
1.972 +\end{itemize}
1.973 +Co-datatype definitions are treated in precisely the same way. They express
1.974 +case operators using those for the variant products and sums, namely
1.975 +$\qsplit$ and~$\qcase$.
1.976 +
1.977 +
1.978 +\ifCADE The package has processed all the datatypes discussed in my earlier
1.979 +paper~\cite{paulson-set-II} and the co-datatype of lazy lists. Space
1.980 +limitations preclude discussing these examples here, but they are
1.981 +distributed with Isabelle.
1.982 +\typeout{****Omitting datatype examples from CADE version!} \else
1.983 +
1.984 +To see how constructors and the case analysis operator are defined, let us
1.985 +examine some examples. These include lists and trees/forests, which I have
1.986 +discussed extensively in another paper~\cite{paulson-set-II}.
1.987 +
1.988 +\begin{figure}
1.989 +\begin{ttbox}
1.990 +structure List = Datatype_Fun
1.991 + (val thy = Univ.thy;
1.992 + val rec_specs =
1.993 + [("list", "univ(A)",
1.994 + [(["Nil"], "i"),
1.995 + (["Cons"], "[i,i]=>i")])];
1.996 + val rec_styp = "i=>i";
1.997 + val ext = None
1.998 + val sintrs =
1.999 + ["Nil : list(A)",
1.1000 + "[| a: A; l: list(A) |] ==> Cons(a,l) : list(A)"];
1.1001 + val monos = [];
1.1002 + val type_intrs = datatype_intrs
1.1003 + val type_elims = datatype_elims);
1.1004 +\end{ttbox}
1.1005 +\hrule
1.1006 +\caption{Defining the datatype of lists} \label{list-fig}
1.1007 +
1.1008 +\medskip
1.1009 +\begin{ttbox}
1.1010 +structure LList = Co_Datatype_Fun
1.1011 + (val thy = QUniv.thy;
1.1012 + val rec_specs =
1.1013 + [("llist", "quniv(A)",
1.1014 + [(["LNil"], "i"),
1.1015 + (["LCons"], "[i,i]=>i")])];
1.1016 + val rec_styp = "i=>i";
1.1017 + val ext = None
1.1018 + val sintrs =
1.1019 + ["LNil : llist(A)",
1.1020 + "[| a: A; l: llist(A) |] ==> LCons(a,l) : llist(A)"];
1.1021 + val monos = [];
1.1022 + val type_intrs = co_datatype_intrs
1.1023 + val type_elims = co_datatype_elims);
1.1024 +\end{ttbox}
1.1025 +\hrule
1.1026 +\caption{Defining the co-datatype of lazy lists} \label{llist-fig}
1.1027 +\end{figure}
1.1028 +
1.1029 +\subsection{Example: lists and lazy lists}
1.1030 +Figures \ref{list-fig} and~\ref{llist-fig} present the ML definitions of
1.1031 +lists and lazy lists, respectively. They highlight the (many) similarities
1.1032 +and (few) differences between datatype and co-datatype definitions.
1.1033 +
1.1034 +Each form of list has two constructors, one for the empty list and one for
1.1035 +adding an element to a list. Each takes a parameter, defining the set of
1.1036 +lists over a given set~$A$. Each uses the appropriate domain from a
1.1037 +Isabelle/ZF theory:
1.1038 +\begin{itemize}
1.1039 +\item $\lst(A)$ specifies domain $\univ(A)$ and parent theory {\tt Univ.thy}.
1.1040 +
1.1041 +\item $\llist(A)$ specifies domain $\quniv(A)$ and parent theory {\tt
1.1042 +QUniv.thy}.
1.1043 +\end{itemize}
1.1044 +
1.1045 +Since $\lst(A)$ is a datatype, it enjoys a structural rule, {\tt List.induct}:
1.1046 +\[ \infer{P(x)}{x\in\lst(A) & P(\Nil)
1.1047 + & \infer*{P(\Cons(a,l))}{[a\in A & l\in\lst(A) & P(l)]_{a,l}} }
1.1048 +\]
1.1049 +Induction and freeness yield the law $l\not=\Cons(a,l)$. To strengthen this,
1.1050 +Isabelle/ZF defines the rank of a set and proves that the standard pairs and
1.1051 +injections have greater rank than their components. An immediate consequence,
1.1052 +which justifies structural recursion on lists \cite[\S4.3]{paulson-set-II},
1.1053 +is
1.1054 +\[ \rank(l) < \rank(\Cons(a,l)). \]
1.1055 +
1.1056 +Since $\llist(A)$ is a co-datatype, it has no induction rule. Instead it has
1.1057 +the co-induction rule shown in \S\ref{co-ind-sec}. Since variant pairs and
1.1058 +injections are monotonic and need not have greater rank than their
1.1059 +components, fixedpoint operators can create cyclic constructions. For
1.1060 +example, the definition
1.1061 +\begin{eqnarray*}
1.1062 + \lconst(a) & \equiv & \lfp(\univ(a), \lambda l. \LCons(a,l))
1.1063 +\end{eqnarray*}
1.1064 +yields $\lconst(a) = \LCons(a,\lconst(a))$.
1.1065 +
1.1066 +\medskip
1.1067 +It may be instructive to examine the definitions of the constructors and
1.1068 +case operator for $\lst(A)$. The definitions for $\llist(A)$ are similar.
1.1069 +The list constructors are defined as follows:
1.1070 +\begin{eqnarray*}
1.1071 + \Nil & = & \Inl(\emptyset) \\
1.1072 + \Cons(a,l) & = & \Inr(\pair{a,l})
1.1073 +\end{eqnarray*}
1.1074 +The operator $\lstcase$ performs case analysis on these two alternatives:
1.1075 +\begin{eqnarray*}
1.1076 + \lstcase(c,h) & \equiv & \case(\lambda u.c, \split(h))
1.1077 +\end{eqnarray*}
1.1078 +Let us verify the two equations:
1.1079 +\begin{eqnarray*}
1.1080 + \lstcase(c, h, \Nil) & = &
1.1081 + \case(\lambda u.c, \split(h), \Inl(\emptyset)) \\
1.1082 + & = & (\lambda u.c)(\emptyset) \\
1.1083 + & = & c.\\[1ex]
1.1084 + \lstcase(c, h, \Cons(x,y)) & = &
1.1085 + \case(\lambda u.c, \split(h), \Inr(\pair{x,y})) \\
1.1086 + & = & \split(h, \pair{x,y}) \\
1.1087 + & = & h(x,y).
1.1088 +\end{eqnarray*}
1.1089 +
1.1090 +\begin{figure}
1.1091 +\begin{small}
1.1092 +\begin{verbatim}
1.1093 +structure TF = Datatype_Fun
1.1094 + (val thy = Univ.thy;
1.1095 + val rec_specs =
1.1096 + [("tree", "univ(A)",
1.1097 + [(["Tcons"], "[i,i]=>i")]),
1.1098 + ("forest", "univ(A)",
1.1099 + [(["Fnil"], "i"),
1.1100 + (["Fcons"], "[i,i]=>i")])];
1.1101 + val rec_styp = "i=>i";
1.1102 + val ext = None
1.1103 + val sintrs =
1.1104 + ["[| a:A; f: forest(A) |] ==> Tcons(a,f) : tree(A)",
1.1105 + "Fnil : forest(A)",
1.1106 + "[| t: tree(A); f: forest(A) |] ==> Fcons(t,f) : forest(A)"];
1.1107 + val monos = [];
1.1108 + val type_intrs = datatype_intrs
1.1109 + val type_elims = datatype_elims);
1.1110 +\end{verbatim}
1.1111 +\end{small}
1.1112 +\hrule
1.1113 +\caption{Defining the datatype of trees and forests} \label{tf-fig}
1.1114 +\end{figure}
1.1115 +
1.1116 +
1.1117 +\subsection{Example: mutual recursion}
1.1118 +In the mutually recursive trees/forests~\cite[\S4.5]{paulson-set-II}, trees
1.1119 +have the one constructor $\Tcons$, while forests have the two constructors
1.1120 +$\Fnil$ and~$\Fcons$. Figure~\ref{tf-fig} presents the ML
1.1121 +definition. It has much in common with that of $\lst(A)$, including its
1.1122 +use of $\univ(A)$ for the domain and {\tt Univ.thy} for the parent theory.
1.1123 +The three introduction rules define the mutual recursion. The
1.1124 +distinguishing feature of this example is its two induction rules.
1.1125 +
1.1126 +The basic induction rule is called {\tt TF.induct}:
1.1127 +\[ \infer{P(x)}{x\in\TF(A) &
1.1128 + \infer*{P(\Tcons(a,f))}
1.1129 + {\left[\begin{array}{l} a\in A \\
1.1130 + f\in\forest(A) \\ P(f)
1.1131 + \end{array}
1.1132 + \right]_{a,f}}
1.1133 + & P(\Fnil)
1.1134 + & \infer*{P(\Fcons(a,l))}
1.1135 + {\left[\begin{array}{l} t\in\tree(A) \\ P(t) \\
1.1136 + f\in\forest(A) \\ P(f)
1.1137 + \end{array}
1.1138 + \right]_{t,f}} }
1.1139 +\]
1.1140 +This rule establishes a single predicate for $\TF(A)$, the union of the
1.1141 +recursive sets.
1.1142 +
1.1143 +Although such reasoning is sometimes useful
1.1144 +\cite[\S4.5]{paulson-set-II}, a proper mutual induction rule should establish
1.1145 +separate predicates for $\tree(A)$ and $\forest(A)$. The package calls this
1.1146 +rule {\tt TF.mutual\_induct}. Observe the usage of $P$ and $Q$ in the
1.1147 +induction hypotheses:
1.1148 +\[ \infer{(\forall z. z\in\tree(A)\imp P(z)) \conj
1.1149 + (\forall z. z\in\forest(A)\imp Q(z))}
1.1150 + {\infer*{P(\Tcons(a,f))}
1.1151 + {\left[\begin{array}{l} a\in A \\
1.1152 + f\in\forest(A) \\ Q(f)
1.1153 + \end{array}
1.1154 + \right]_{a,f}}
1.1155 + & Q(\Fnil)
1.1156 + & \infer*{Q(\Fcons(a,l))}
1.1157 + {\left[\begin{array}{l} t\in\tree(A) \\ P(t) \\
1.1158 + f\in\forest(A) \\ Q(f)
1.1159 + \end{array}
1.1160 + \right]_{t,f}} }
1.1161 +\]
1.1162 +As mentioned above, the package does not define a structural recursion
1.1163 +operator. I have described elsewhere how this is done
1.1164 +\cite[\S4.5]{paulson-set-II}.
1.1165 +
1.1166 +Both forest constructors have the form $\Inr(\cdots)$,
1.1167 +while the tree constructor has the form $\Inl(\cdots)$. This pattern would
1.1168 +hold regardless of how many tree or forest constructors there were.
1.1169 +\begin{eqnarray*}
1.1170 + \Tcons(a,l) & = & \Inl(\pair{a,l}) \\
1.1171 + \Fnil & = & \Inr(\Inl(\emptyset)) \\
1.1172 + \Fcons(a,l) & = & \Inr(\Inr(\pair{a,l}))
1.1173 +\end{eqnarray*}
1.1174 +There is only one case operator; it works on the union of the trees and
1.1175 +forests:
1.1176 +\begin{eqnarray*}
1.1177 + {\tt tree\_forest\_case}(f,c,g) & \equiv &
1.1178 + \case(\split(f),\, \case(\lambda u.c, \split(g)))
1.1179 +\end{eqnarray*}
1.1180 +
1.1181 +\begin{figure}
1.1182 +\begin{small}
1.1183 +\begin{verbatim}
1.1184 +structure Data = Datatype_Fun
1.1185 + (val thy = Univ.thy;
1.1186 + val rec_specs =
1.1187 + [("data", "univ(A Un B)",
1.1188 + [(["Con0"], "i"),
1.1189 + (["Con1"], "i=>i"),
1.1190 + (["Con2"], "[i,i]=>i"),
1.1191 + (["Con3"], "[i,i,i]=>i")])];
1.1192 + val rec_styp = "[i,i]=>i";
1.1193 + val ext = None
1.1194 + val sintrs =
1.1195 + ["Con0 : data(A,B)",
1.1196 + "[| a: A |] ==> Con1(a) : data(A,B)",
1.1197 + "[| a: A; b: B |] ==> Con2(a,b) : data(A,B)",
1.1198 + "[| a: A; b: B; d: data(A,B) |] ==> Con3(a,b,d) : data(A,B)"];
1.1199 + val monos = [];
1.1200 + val type_intrs = datatype_intrs
1.1201 + val type_elims = datatype_elims);
1.1202 +\end{verbatim}
1.1203 +\end{small}
1.1204 +\hrule
1.1205 +\caption{Defining the four-constructor sample datatype} \label{data-fig}
1.1206 +\end{figure}
1.1207 +
1.1208 +\subsection{A four-constructor datatype}
1.1209 +Finally let us consider a fairly general datatype. It has four
1.1210 +constructors $\Con_0$, $\Con_1$\ $\Con_2$ and $\Con_3$, with the
1.1211 +corresponding arities. Figure~\ref{data-fig} presents the ML definition.
1.1212 +Because this datatype has two set parameters, $A$ and~$B$, it specifies
1.1213 +$\univ(A\un B)$ as its domain. The structural induction rule has four
1.1214 +minor premises, one per constructor:
1.1215 +\[ \infer{P(x)}{x\in\data(A,B) &
1.1216 + P(\Con_0) &
1.1217 + \infer*{P(\Con_1(a))}{[a\in A]_a} &
1.1218 + \infer*{P(\Con_2(a,b))}
1.1219 + {\left[\begin{array}{l} a\in A \\ b\in B \end{array}
1.1220 + \right]_{a,b}} &
1.1221 + \infer*{P(\Con_3(a,b,d))}
1.1222 + {\left[\begin{array}{l} a\in A \\ b\in B \\
1.1223 + d\in\data(A,B) \\ P(d)
1.1224 + \end{array}
1.1225 + \right]_{a,b,d}} }
1.1226 +\]
1.1227 +
1.1228 +The constructor definitions are
1.1229 +\begin{eqnarray*}
1.1230 + \Con_0 & = & \Inl(\Inl(\emptyset)) \\
1.1231 + \Con_1(a) & = & \Inl(\Inr(a)) \\
1.1232 + \Con_2(a,b) & = & \Inr(\Inl(\pair{a,b})) \\
1.1233 + \Con_3(a,b,c) & = & \Inr(\Inr(\pair{a,b,c})).
1.1234 +\end{eqnarray*}
1.1235 +The case operator is
1.1236 +\begin{eqnarray*}
1.1237 + {\tt data\_case}(f_0,f_1,f_2,f_3) & \equiv &
1.1238 + \case(\begin{array}[t]{@{}l}
1.1239 + \case(\lambda u.f_0,\; f_1),\, \\
1.1240 + \case(\split(f_2),\; \split(\lambda v.\split(f_3(v)))) )
1.1241 + \end{array}
1.1242 +\end{eqnarray*}
1.1243 +This may look cryptic, but the case equations are trivial to verify.
1.1244 +
1.1245 +In the constructor definitions, the injections are balanced. A more naive
1.1246 +approach is to define $\Con_3(a,b,c)$ as
1.1247 +$\Inr(\Inr(\Inr(\pair{a,b,c})))$; instead, each constructor has two
1.1248 +injections. The difference here is small. But the ZF examples include a
1.1249 +60-element enumeration type, where each constructor has 5 or~6 injections.
1.1250 +The naive approach would require 1 to~59 injections; the definitions would be
1.1251 +quadratic in size. It is like the difference between the binary and unary
1.1252 +numeral systems.
1.1253 +
1.1254 +The package returns the constructor and case operator definitions as the
1.1255 +theorem list \verb|con_defs|. The head of this list defines the case
1.1256 +operator and the tail defines the constructors.
1.1257 +
1.1258 +The package returns the case equations, such as
1.1259 +\begin{eqnarray*}
1.1260 + {\tt data\_case}(f_0,f_1,f_2,f_3,\Con_3(a,b,c)) & = & f_3(a,b,c),
1.1261 +\end{eqnarray*}
1.1262 +as the theorem list \verb|case_eqns|. There is one equation per constructor.
1.1263 +
1.1264 +\subsection{Proving freeness theorems}
1.1265 +There are two kinds of freeness theorems:
1.1266 +\begin{itemize}
1.1267 +\item {\bf injectiveness} theorems, such as
1.1268 +\[ \Con_2(a,b) = \Con_2(a',b') \bimp a=a' \conj b=b' \]
1.1269 +
1.1270 +\item {\bf distinctness} theorems, such as
1.1271 +\[ \Con_1(a) \not= \Con_2(a',b') \]
1.1272 +\end{itemize}
1.1273 +Since the number of such theorems is quadratic in the number of constructors,
1.1274 +the package does not attempt to prove them all. Instead it returns tools for
1.1275 +proving desired theorems --- either explicitly or `on the fly' during
1.1276 +simplification or classical reasoning.
1.1277 +
1.1278 +The theorem list \verb|free_iffs| enables the simplifier to perform freeness
1.1279 +reasoning. This works by incremental unfolding of constructors that appear in
1.1280 +equations. The theorem list contains logical equivalences such as
1.1281 +\begin{eqnarray*}
1.1282 + \Con_0=c & \bimp & c=\Inl(\Inl(\emptyset)) \\
1.1283 + \Con_1(a)=c & \bimp & c=\Inl(\Inr(a)) \\
1.1284 + & \vdots & \\
1.1285 + \Inl(a)=\Inl(b) & \bimp & a=b \\
1.1286 + \Inl(a)=\Inr(b) & \bimp & \bot \\
1.1287 + \pair{a,b} = \pair{a',b'} & \bimp & a=a' \conj b=b'
1.1288 +\end{eqnarray*}
1.1289 +For example, these rewrite $\Con_1(a)=\Con_1(b)$ to $a=b$ in four steps.
1.1290 +
1.1291 +The theorem list \verb|free_SEs| enables the classical
1.1292 +reasoner to perform similar replacements. It consists of elimination rules
1.1293 +to replace $\Con_0=c$ by $c=\Inl(\Inl(\emptyset))$, and so forth, in the
1.1294 +assumptions.
1.1295 +
1.1296 +Such incremental unfolding combines freeness reasoning with other proof
1.1297 +steps. It has the unfortunate side-effect of unfolding definitions of
1.1298 +constructors in contexts such as $\exists x.\Con_1(a)=x$, where they should
1.1299 +be left alone. Calling the Isabelle tactic {\tt fold\_tac con\_defs}
1.1300 +restores the defined constants.
1.1301 +\fi %CADE
1.1302 +
1.1303 +\section{Conclusions and future work}
1.1304 +The fixedpoint approach makes it easy to implement a uniquely powerful
1.1305 +package for inductive and co-inductive definitions. It is efficient: it
1.1306 +processes most definitions in seconds and even a 60-constructor datatype
1.1307 +requires only two minutes. It is also simple: the package consists of
1.1308 +under 1100 lines (35K bytes) of Standard ML code. The first working
1.1309 +version took under a week to code.
1.1310 +
1.1311 +The approach is not restricted to set theory. It should be suitable for
1.1312 +any logic that has some notion of set and the Knaster-Tarski Theorem.
1.1313 +Indeed, Melham's inductive definition package for the HOL
1.1314 +system~\cite{camilleri92} implicitly proves this theorem.
1.1315 +
1.1316 +Datatype and co-datatype definitions furthermore require a particular set
1.1317 +closed under a suitable notion of ordered pair. I intend to use the
1.1318 +Isabelle/ZF package as the basis for a higher-order logic one, using
1.1319 +Isabelle/HOL\@. The necessary theory is already
1.1320 +mechanizeds~\cite{paulson-coind}. HOL represents sets by unary predicates;
1.1321 +defining the corresponding types may cause complication.
1.1322 +
1.1323 +
1.1324 +\bibliographystyle{plain}
1.1325 +\bibliography{atp,theory,funprog,isabelle}
1.1326 +%%%%%\doendnotes
1.1327 +
1.1328 +\ifCADE\typeout{****Omitting appendices from CADE version!}
1.1329 +\else
1.1330 +\newpage
1.1331 +\appendix
1.1332 +\section{Inductive and co-inductive definitions: users guide}
1.1333 +The ML functors \verb|Inductive_Fun| and \verb|Co_Inductive_Fun| build
1.1334 +inductive and co-inductive definitions, respectively. This section describes
1.1335 +how to invoke them.
1.1336 +
1.1337 +\subsection{The result structure}
1.1338 +Many of the result structure's components have been discussed
1.1339 +in~\S\ref{basic-sec}; others are self-explanatory.
1.1340 +\begin{description}
1.1341 +\item[\tt thy] is the new theory containing the recursive sets.
1.1342 +
1.1343 +\item[\tt defs] is the list of definitions of the recursive sets.
1.1344 +
1.1345 +\item[\tt bnd\_mono] is a monotonicity theorem for the fixedpoint operator.
1.1346 +
1.1347 +\item[\tt unfold] is a fixedpoint equation for the recursive set (the union of
1.1348 +the recursive sets, in the case of mutual recursion).
1.1349 +
1.1350 +\item[\tt dom\_subset] is a theorem stating inclusion in the domain.
1.1351 +
1.1352 +\item[\tt intrs] is the list of introduction rules, now proved as theorems, for
1.1353 +the recursive sets.
1.1354 +
1.1355 +\item[\tt elim] is the elimination rule.
1.1356 +
1.1357 +\item[\tt mk\_cases] is a function to create simplified instances of {\tt
1.1358 +elim}, using freeness reasoning on some underlying datatype.
1.1359 +\end{description}
1.1360 +
1.1361 +For an inductive definition, the result structure contains two induction rules,
1.1362 +{\tt induct} and \verb|mutual_induct|. For a co-inductive definition, it
1.1363 +contains the rule \verb|co_induct|.
1.1364 +
1.1365 +\begin{figure}
1.1366 +\begin{ttbox}
1.1367 +sig
1.1368 +val thy : theory
1.1369 +val defs : thm list
1.1370 +val bnd_mono : thm
1.1371 +val unfold : thm
1.1372 +val dom_subset : thm
1.1373 +val intrs : thm list
1.1374 +val elim : thm
1.1375 +val mk_cases : thm list -> string -> thm
1.1376 +{\it(Inductive definitions only)}
1.1377 +val induct : thm
1.1378 +val mutual_induct: thm
1.1379 +{\it(Co-inductive definitions only)}
1.1380 +val co_induct : thm
1.1381 +end
1.1382 +\end{ttbox}
1.1383 +\hrule
1.1384 +\caption{The result of a (co-)inductive definition} \label{def-result-fig}
1.1385 +\end{figure}
1.1386 +
1.1387 +Figure~\ref{def-result-fig} summarizes the two result signatures,
1.1388 +specifying the types of all these components.
1.1389 +
1.1390 +\begin{figure}
1.1391 +\begin{ttbox}
1.1392 +sig
1.1393 +val thy : theory
1.1394 +val rec_doms : (string*string) list
1.1395 +val sintrs : string list
1.1396 +val monos : thm list
1.1397 +val con_defs : thm list
1.1398 +val type_intrs : thm list
1.1399 +val type_elims : thm list
1.1400 +end
1.1401 +\end{ttbox}
1.1402 +\hrule
1.1403 +\caption{The argument of a (co-)inductive definition} \label{def-arg-fig}
1.1404 +\end{figure}
1.1405 +
1.1406 +\subsection{The argument structure}
1.1407 +Both \verb|Inductive_Fun| and \verb|Co_Inductive_Fun| take the same argument
1.1408 +structure (Figure~\ref{def-arg-fig}). Its components are as follows:
1.1409 +\begin{description}
1.1410 +\item[\tt thy] is the definition's parent theory, which {\it must\/}
1.1411 +declare constants for the recursive sets.
1.1412 +
1.1413 +\item[\tt rec\_doms] is a list of pairs, associating the name of each recursive
1.1414 +set with its domain.
1.1415 +
1.1416 +\item[\tt sintrs] specifies the desired introduction rules as strings.
1.1417 +
1.1418 +\item[\tt monos] consists of monotonicity theorems for each operator applied
1.1419 +to a recursive set in the introduction rules.
1.1420 +
1.1421 +\item[\tt con\_defs] contains definitions of constants appearing in the
1.1422 +introduction rules. The (co-)datatype package supplies the constructors'
1.1423 +definitions here. Most direct calls of \verb|Inductive_Fun| or
1.1424 +\verb|Co_Inductive_Fun| pass the empty list; one exception is the primitive
1.1425 +recursive functions example (\S\ref{primrec-sec}).
1.1426 +
1.1427 +\item[\tt type\_intrs] consists of introduction rules for type-checking the
1.1428 + definition, as discussed in~\S\ref{basic-sec}. They are applied using
1.1429 + depth-first search; you can trace the proof by setting
1.1430 + \verb|trace_DEPTH_FIRST := true|.
1.1431 +
1.1432 +\item[\tt type\_elims] consists of elimination rules for type-checking the
1.1433 +definition. They are presumed to be `safe' and are applied as much as
1.1434 +possible, prior to the {\tt type\_intrs} search.
1.1435 +\end{description}
1.1436 +The package has a few notable restrictions:
1.1437 +\begin{itemize}
1.1438 +\item The parent theory, {\tt thy}, must declare the recursive sets as
1.1439 + constants. You can extend a theory with new constants using {\tt
1.1440 + addconsts}, as illustrated in~\S\ref{ind-eg-sec}. If the inductive
1.1441 + definition also requires new concrete syntax, then it is simpler to
1.1442 + express the parent theory using a theory file. It is often convenient to
1.1443 + define an infix syntax for relations, say $a\prec b$ for $\pair{a,b}\in
1.1444 + R$.
1.1445 +
1.1446 +\item The names of the recursive sets must be identifiers, not infix
1.1447 +operators.
1.1448 +
1.1449 +\item Side-conditions must not be conjunctions. However, an introduction rule
1.1450 +may contain any number of side-conditions.
1.1451 +\end{itemize}
1.1452 +
1.1453 +
1.1454 +\section{Datatype and co-datatype definitions: users guide}
1.1455 +The ML functors \verb|Datatype_Fun| and \verb|Co_Datatype_Fun| define datatypes
1.1456 +and co-datatypes, invoking \verb|Datatype_Fun| and
1.1457 +\verb|Co_Datatype_Fun| to make the underlying (co-)inductive definitions.
1.1458 +
1.1459 +
1.1460 +\subsection{The result structure}
1.1461 +The result structure extends that of (co-)inductive definitions
1.1462 +(Figure~\ref{def-result-fig}) with several additional items:
1.1463 +\begin{ttbox}
1.1464 +val con_thy : theory
1.1465 +val con_defs : thm list
1.1466 +val case_eqns : thm list
1.1467 +val free_iffs : thm list
1.1468 +val free_SEs : thm list
1.1469 +val mk_free : string -> thm
1.1470 +\end{ttbox}
1.1471 +Most of these have been discussed in~\S\ref{data-sec}. Here is a summary:
1.1472 +\begin{description}
1.1473 +\item[\tt con\_thy] is a new theory containing definitions of the
1.1474 +(co-)datatype's constructors and case operator. It also declares the
1.1475 +recursive sets as constants, so that it may serve as the parent
1.1476 +theory for the (co-)inductive definition.
1.1477 +
1.1478 +\item[\tt con\_defs] is a list of definitions: the case operator followed by
1.1479 +the constructors. This theorem list can be supplied to \verb|mk_cases|, for
1.1480 +example.
1.1481 +
1.1482 +\item[\tt case\_eqns] is a list of equations, stating that the case operator
1.1483 +inverts each constructor.
1.1484 +
1.1485 +\item[\tt free\_iffs] is a list of logical equivalences to perform freeness
1.1486 +reasoning by rewriting. A typical application has the form
1.1487 +\begin{ttbox}
1.1488 +by (asm_simp_tac (ZF_ss addsimps free_iffs) 1);
1.1489 +\end{ttbox}
1.1490 +
1.1491 +\item[\tt free\_SEs] is a list of `safe' elimination rules to perform freeness
1.1492 +reasoning. It can be supplied to \verb|eresolve_tac| or to the classical
1.1493 +reasoner:
1.1494 +\begin{ttbox}
1.1495 +by (fast_tac (ZF_cs addSEs free_SEs) 1);
1.1496 +\end{ttbox}
1.1497 +
1.1498 +\item[\tt mk\_free] is a function to prove freeness properties, specified as
1.1499 +strings. The theorems can be expressed in various forms, such as logical
1.1500 +equivalences or elimination rules.
1.1501 +\end{description}
1.1502 +
1.1503 +The result structure also inherits everything from the underlying
1.1504 +(co-)inductive definition, such as the introduction rules, elimination rule,
1.1505 +and induction/co-induction rule.
1.1506 +
1.1507 +
1.1508 +\begin{figure}
1.1509 +\begin{ttbox}
1.1510 +sig
1.1511 +val thy : theory
1.1512 +val rec_specs : (string * string * (string list*string)list) list
1.1513 +val rec_styp : string
1.1514 +val ext : Syntax.sext option
1.1515 +val sintrs : string list
1.1516 +val monos : thm list
1.1517 +val type_intrs: thm list
1.1518 +val type_elims: thm list
1.1519 +end
1.1520 +\end{ttbox}
1.1521 +\hrule
1.1522 +\caption{The argument of a (co-)datatype definition} \label{data-arg-fig}
1.1523 +\end{figure}
1.1524 +
1.1525 +\subsection{The argument structure}
1.1526 +Both (co-)datatype functors take the same argument structure
1.1527 +(Figure~\ref{data-arg-fig}). It does not extend that for (co-)inductive
1.1528 +definitions, but shares several components and passes them uninterpreted to
1.1529 +\verb|Datatype_Fun| or
1.1530 +\verb|Co_Datatype_Fun|. The new components are as follows:
1.1531 +\begin{description}
1.1532 +\item[\tt thy] is the (co-)datatype's parent theory. It {\it must not\/}
1.1533 +declare constants for the recursive sets. Recall that (co-)inductive
1.1534 +definitions have the opposite restriction.
1.1535 +
1.1536 +\item[\tt rec\_specs] is a list of triples of the form ({\it recursive set\/},
1.1537 +{\it domain\/}, {\it constructors\/}) for each mutually recursive set. {\it
1.1538 +Constructors\/} is a list of the form (names, type). See the discussion and
1.1539 +examples in~\S\ref{data-sec}.
1.1540 +
1.1541 +\item[\tt rec\_styp] is the common meta-type of the mutually recursive sets,
1.1542 +specified as a string. They must all have the same type because all must
1.1543 +take the same parameters.
1.1544 +
1.1545 +\item[\tt ext] is an optional syntax extension, usually omitted by writing
1.1546 +{\tt None}. You can supply mixfix syntax for the constructors by supplying
1.1547 +\begin{ttbox}
1.1548 +Some (Syntax.simple_sext [{\it mixfix declarations\/}])
1.1549 +\end{ttbox}
1.1550 +\end{description}
1.1551 +The choice of domain is usually simple. Isabelle/ZF defines the set
1.1552 +$\univ(A)$, which contains~$A$ and is closed under the standard Cartesian
1.1553 +products and disjoint sums \cite[\S4.2]{paulson-set-II}. In a typical
1.1554 +datatype definition with set parameters $A_1$, \ldots, $A_k$, a suitable
1.1555 +domain for all the recursive sets is $\univ(A_1\un\cdots\un A_k)$. For a
1.1556 +co-datatype definition, the set
1.1557 +$\quniv(A)$ contains~$A$ and is closed under the variant Cartesian products
1.1558 +and disjoint sums; the appropropriate domain is
1.1559 +$\quniv(A_1\un\cdots\un A_k)$.
1.1560 +
1.1561 +The {\tt sintrs} specify the introduction rules, which govern the recursive
1.1562 +structure of the datatype. Introduction rules may involve monotone operators
1.1563 +and side-conditions to express things that go beyond the usual notion of
1.1564 +datatype. The theorem lists {\tt monos}, {\tt type\_intrs} and {\tt
1.1565 +type\_elims} should contain precisely what is needed for the underlying
1.1566 +(co-)inductive definition. Isabelle/ZF defines theorem lists that can be
1.1567 +defined for the latter two components:
1.1568 +\begin{itemize}
1.1569 +\item {\tt datatype\_intrs} and {\tt datatype\_elims} are type-checking rules
1.1570 +for $\univ(A)$.
1.1571 +\item {\tt co\_datatype\_intrs} and {\tt co\_datatype\_elims} are type-checking
1.1572 +rules for $\quniv(A)$.
1.1573 +\end{itemize}
1.1574 +In typical definitions, these theorem lists need not be supplemented with
1.1575 +other theorems.
1.1576 +
1.1577 +The constructor definitions' right-hand sides can overlap. A
1.1578 +simple example is the datatype for the combinators, whose constructors are
1.1579 +\begin{eqnarray*}
1.1580 + {\tt K} & \equiv & \Inl(\emptyset) \\
1.1581 + {\tt S} & \equiv & \Inr(\Inl(\emptyset)) \\
1.1582 + p{\tt\#}q & \equiv & \Inr(\Inl(\pair{p,q}))
1.1583 +\end{eqnarray*}
1.1584 +Unlike in previous versions of Isabelle, \verb|fold_tac| now ensures that the
1.1585 +longest right-hand sides are folded first.
1.1586 +
1.1587 +\fi
1.1588 +\end{document}