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