52792
|
1 |
(* Title: Doc/Datatypes/Datatypes.thy
|
|
2 |
Author: Jasmin Blanchette, TU Muenchen
|
|
3 |
|
|
4 |
Tutorial for (co)datatype definitions with the new package.
|
|
5 |
*)
|
|
6 |
|
|
7 |
theory Datatypes
|
52824
|
8 |
imports Setup
|
52792
|
9 |
begin
|
|
10 |
|
|
11 |
section {* Introduction *}
|
|
12 |
|
|
13 |
text {*
|
52794
|
14 |
The 2013 edition of Isabelle introduced new definitional package for datatypes
|
|
15 |
and codatatypes. The datatype support is similar to that provided by the earlier
|
|
16 |
package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL};
|
52822
|
17 |
indeed, replacing @{command datatype} by @{command datatype_new} is usually sufficient
|
52794
|
18 |
to port existing specifications to the new package. What makes the new package
|
|
19 |
attractive is that it supports definitions with recursion through a large class
|
|
20 |
of non-datatypes, notably finite sets:
|
52792
|
21 |
*}
|
|
22 |
|
52805
|
23 |
datatype_new 'a treeFS = TreeFS 'a "'a treeFS fset"
|
52792
|
24 |
|
|
25 |
text {*
|
52794
|
26 |
\noindent
|
|
27 |
Another advantage of the new package is that it supports local definitions:
|
52792
|
28 |
*}
|
|
29 |
|
52805
|
30 |
context linorder
|
|
31 |
begin
|
|
32 |
datatype_new flag = Less | Eq | Greater
|
|
33 |
end
|
52792
|
34 |
|
|
35 |
text {*
|
52794
|
36 |
\noindent
|
|
37 |
Finally, the package also provides some convenience, notably automatically
|
52822
|
38 |
generated destructors.
|
52794
|
39 |
|
52822
|
40 |
The command @{command datatype_new} is expected to displace @{command datatype} in a future
|
|
41 |
release. Authors of new theories are encouraged to use @{command datatype_new}, and
|
52805
|
42 |
maintainers of older theories may want to consider upgrading in the coming months.
|
52794
|
43 |
|
|
44 |
The package also provides codatatypes (or ``coinductive datatypes''), which may
|
52805
|
45 |
have infinite values. The following command introduces a codatatype of infinite
|
52794
|
46 |
streams:
|
|
47 |
*}
|
|
48 |
|
52805
|
49 |
codatatype 'a stream = Stream 'a "'a stream"
|
52794
|
50 |
|
|
51 |
text {*
|
|
52 |
\noindent
|
|
53 |
Mixed inductive--coinductive recursion is possible via nesting.
|
|
54 |
Compare the following four examples:
|
|
55 |
*}
|
|
56 |
|
52805
|
57 |
datatype_new 'a treeFF = TreeFF 'a "'a treeFF list"
|
|
58 |
datatype_new 'a treeFI = TreeFI 'a "'a treeFF stream"
|
|
59 |
codatatype 'a treeIF = TreeIF 'a "'a treeFF list"
|
|
60 |
codatatype 'a treeII = TreeII 'a "'a treeFF stream"
|
52792
|
61 |
|
52794
|
62 |
text {*
|
|
63 |
To use the package, it is necessary to import the @{theory BNF} theory, which
|
52806
|
64 |
can be precompiled as the \textit{HOL-BNF} image. The following commands show
|
|
65 |
how to launch jEdit/PIDE with the image loaded and how to build the image
|
|
66 |
without launching jEdit:
|
52794
|
67 |
*}
|
|
68 |
|
|
69 |
text {*
|
|
70 |
\noindent
|
52806
|
71 |
\ \ \ \ \texttt{isabelle jedit -l HOL-BNF} \\
|
52827
|
72 |
\noindent
|
52805
|
73 |
\ \ \ \ \texttt{isabelle build -b HOL-BNF}
|
52794
|
74 |
*}
|
|
75 |
|
|
76 |
text {*
|
52805
|
77 |
The package, like its predecessor, fully adheres to the LCF philosophy
|
|
78 |
\cite{mgordon79}: The characteristic theorems associated with the specified
|
|
79 |
(co)datatypes are derived rather than introduced axiomatically.%
|
|
80 |
\footnote{Nonetheless, if the \textit{quick\_and\_dirty} option is enabled, some
|
|
81 |
of the internal constructions and most of the internal proof obligations are
|
|
82 |
skipped.}
|
|
83 |
The package's metatheory is described in a pair of papers
|
|
84 |
\cite{traytel-et-al-2012,blanchette-et-al-wit}.
|
52792
|
85 |
*}
|
|
86 |
|
52794
|
87 |
text {*
|
|
88 |
This tutorial is organized as follows:
|
|
89 |
|
52805
|
90 |
\begin{itemize}
|
52822
|
91 |
\setlength{\itemsep}{0pt}
|
|
92 |
|
52805
|
93 |
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,''
|
52822
|
94 |
describes how to specify datatypes using the @{command datatype_new} command.
|
52805
|
95 |
|
|
96 |
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive
|
|
97 |
Functions,'' describes how to specify recursive functions using
|
52824
|
98 |
\keyw{primrec\_new}, @{command fun}, and @{command function}.
|
52805
|
99 |
|
|
100 |
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,''
|
52822
|
101 |
describes how to specify codatatypes using the @{command codatatype} command.
|
52805
|
102 |
|
|
103 |
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive
|
|
104 |
Functions,'' describes how to specify corecursive functions using the
|
52824
|
105 |
\keyw{primcorec} command.
|
52794
|
106 |
|
52805
|
107 |
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering
|
|
108 |
Bounded Natural Functors,'' explains how to set up the (co)datatype package to
|
|
109 |
allow nested recursion through custom well-behaved type constructors.
|
|
110 |
|
|
111 |
\item Section \ref{sec:generating-free-constructor-theorems}, ``Generating Free
|
|
112 |
Constructor Theorems,'' explains how to derive convenience theorems for free
|
52822
|
113 |
constructors, as performed internally by @{command datatype_new} and
|
|
114 |
@{command codatatype}.
|
52794
|
115 |
|
52805
|
116 |
\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,''
|
|
117 |
describes the package's programmatic interface.
|
52794
|
118 |
|
52805
|
119 |
\item Section \ref{sec:interoperability}, ``Interoperability,''
|
|
120 |
is concerned with the packages' interaction with other Isabelle packages and
|
|
121 |
tools, such as the code generator and the counterexample generators.
|
|
122 |
|
|
123 |
\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and
|
52822
|
124 |
Limitations,'' concludes with known open issues at the time of writing.
|
52805
|
125 |
\end{itemize}
|
52822
|
126 |
|
|
127 |
|
|
128 |
\newbox\boxA
|
|
129 |
\setbox\boxA=\hbox{\texttt{nospam}}
|
|
130 |
|
|
131 |
\newcommand\authoremaili{\texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@\allowbreak
|
|
132 |
in.\allowbreak tum.\allowbreak de}}
|
|
133 |
\newcommand\authoremailii{\texttt{pope{\color{white}nospam}\kern-\wd\boxA{}scua@\allowbreak
|
|
134 |
in.\allowbreak tum.\allowbreak de}}
|
|
135 |
\newcommand\authoremailiii{\texttt{tray{\color{white}nospam}\kern-\wd\boxA{}tel@\allowbreak
|
|
136 |
in.\allowbreak tum.\allowbreak de}}
|
|
137 |
|
|
138 |
\noindent
|
|
139 |
Comments and bug reports concerning either
|
|
140 |
the tool or the manual should be directed to the authors at
|
|
141 |
\authoremaili, \authoremailii, and \authoremailiii.
|
|
142 |
|
|
143 |
\begin{framed}
|
|
144 |
\noindent
|
|
145 |
\textbf{Warning:} This document is under heavy construction. Please apologise
|
52827
|
146 |
for its appearance and come back in a few months. If you have ideas regarding
|
|
147 |
material that should be included, please let the authors know.
|
52822
|
148 |
\end{framed}
|
|
149 |
|
52794
|
150 |
*}
|
|
151 |
|
52827
|
152 |
section {* Defining Datatypes
|
52805
|
153 |
\label{sec:defining-datatypes} *}
|
|
154 |
|
|
155 |
text {*
|
52822
|
156 |
This section describes how to specify datatypes using the @{command datatype_new}
|
52805
|
157 |
command. The command is first illustrated through concrete examples featuring
|
52822
|
158 |
different flavors of recursion. More examples can be found in the directory
|
|
159 |
\verb|~~/src/HOL/BNF/Examples|.
|
52805
|
160 |
*}
|
52792
|
161 |
|
52824
|
162 |
|
52794
|
163 |
subsection {* Introductory Examples *}
|
|
164 |
|
|
165 |
subsubsection {* Nonrecursive Types *}
|
|
166 |
|
52805
|
167 |
text {*
|
|
168 |
enumeration type:
|
|
169 |
*}
|
|
170 |
|
|
171 |
datatype_new trool = Truue | Faalse | Maaybe
|
|
172 |
|
|
173 |
text {*
|
|
174 |
Haskell-style option type:
|
|
175 |
*}
|
|
176 |
|
|
177 |
datatype_new 'a maybe = Nothing | Just 'a
|
|
178 |
|
|
179 |
text {*
|
|
180 |
triple:
|
|
181 |
*}
|
|
182 |
|
|
183 |
datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c
|
|
184 |
|
52824
|
185 |
|
52794
|
186 |
subsubsection {* Simple Recursion *}
|
|
187 |
|
52805
|
188 |
text {*
|
|
189 |
simplest recursive type: natural numbers
|
|
190 |
*}
|
|
191 |
|
|
192 |
datatype_new nat = Zero | Suc nat
|
|
193 |
|
|
194 |
text {*
|
|
195 |
lists were shown in the introduction
|
|
196 |
|
|
197 |
terminated lists are a variant:
|
|
198 |
*}
|
|
199 |
|
|
200 |
datatype_new ('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
|
|
201 |
|
|
202 |
text {*
|
|
203 |
On the right-hand side of the equal sign, the usual Isabelle conventions apply:
|
|
204 |
Nonatomic types must be enclosed in double quotes.
|
|
205 |
*}
|
|
206 |
|
52824
|
207 |
|
52794
|
208 |
subsubsection {* Mutual Recursion *}
|
|
209 |
|
52805
|
210 |
text {*
|
|
211 |
Mutual recursion = Define several types simultaneously, referring to each other.
|
|
212 |
|
|
213 |
Simple example: distinction between even and odd natural numbers:
|
|
214 |
*}
|
|
215 |
|
|
216 |
datatype_new even_nat = Zero | Even_Suc odd_nat
|
|
217 |
and odd_nat = Odd_Suc even_nat
|
|
218 |
|
|
219 |
text {*
|
|
220 |
More complex, and more realistic, example:
|
|
221 |
*}
|
|
222 |
|
|
223 |
datatype_new ('a, 'b) expr =
|
52806
|
224 |
Term "('a, 'b) trm" | Sum "('a, 'b) trm" "('a, 'b) expr"
|
52805
|
225 |
and ('a, 'b) trm =
|
|
226 |
Factor "('a, 'b) factor" | Prod "('a, 'b) factor" "('a, 'b) trm"
|
|
227 |
and ('a, 'b) factor =
|
|
228 |
Const 'a | Var 'b | Sub_Expr "('a, 'b) expr"
|
|
229 |
|
52824
|
230 |
|
52794
|
231 |
subsubsection {* Nested Recursion *}
|
|
232 |
|
52805
|
233 |
text {*
|
|
234 |
Nested recursion = Have recursion through a type constructor.
|
|
235 |
|
|
236 |
The introduction showed some examples of trees with nesting through lists.
|
|
237 |
|
|
238 |
More complex example, which reuses our maybe and triple types:
|
|
239 |
*}
|
|
240 |
|
|
241 |
datatype_new 'a triple_tree =
|
|
242 |
Triple_Tree "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
|
|
243 |
|
|
244 |
text {*
|
|
245 |
Recursion may not be arbitrary; e.g. impossible to define
|
|
246 |
*}
|
|
247 |
|
|
248 |
(*
|
|
249 |
datatype_new 'a foo = Foo (*<*) datatype_new 'a bar = Bar "'a foo \<Rightarrow> 'a foo"
|
|
250 |
*)
|
|
251 |
|
52806
|
252 |
datatype_new 'a evil = Evil (*<*)'a
|
|
253 |
typ (*>*)"'a evil \<Rightarrow> 'a evil"
|
|
254 |
|
|
255 |
text {*
|
|
256 |
Issue: => allows recursion only on its right-hand side.
|
|
257 |
This issue is inherited by polymorphic datatypes (and codatatypes)
|
|
258 |
defined in terms of =>.
|
|
259 |
In general, type constructors "('a1, ..., 'an) k" allow recursion on a subset
|
|
260 |
of their type arguments.
|
|
261 |
*}
|
|
262 |
|
|
263 |
|
52805
|
264 |
subsubsection {* Auxiliary Constants and Syntaxes *}
|
|
265 |
|
|
266 |
text {*
|
52822
|
267 |
The @{command datatype_new} command introduces various constants in addition to the
|
|
268 |
constructors. Given a type @{text "('a1,\<dots>,'aM) t"} with constructors
|
52827
|
269 |
@{text "t.C\<^sub>1"}, \ldots, @{text "t.C\<^sub>m"}, the following auxiliary
|
|
270 |
constants are introduced (among others):
|
52822
|
271 |
|
|
272 |
\begin{itemize}
|
|
273 |
\setlength{\itemsep}{0pt}
|
|
274 |
|
52827
|
275 |
\item \emph{Set functions} (\emph{natural transformations}):
|
|
276 |
@{text t_set1}, \ldots, @{text t_setM}
|
52822
|
277 |
|
|
278 |
\item \emph{Map function} (\emph{functorial action}): @{text t_map}
|
|
279 |
|
|
280 |
\item \emph{Relator}: @{text t_rel}
|
|
281 |
|
|
282 |
\item \emph{Iterator}: @{text t_fold}
|
|
283 |
|
|
284 |
\item \emph{Recursor}: @{text t_rec}
|
|
285 |
|
52827
|
286 |
\item \emph{Discriminators}: @{text "t.is_C\<^sub>1"}, \ldots,
|
|
287 |
@{text "t.is_C\<^sub>m"}
|
52822
|
288 |
|
|
289 |
\item \emph{Selectors}:
|
52827
|
290 |
@{text t.un_C}$_{11}$, \ldots, @{text t.un_C}$_{1n_1}$, \ldots,
|
|
291 |
@{text t.un_C}$_{m1}$, \ldots, @{text t.un_C}$_{mn_m}$
|
52822
|
292 |
\end{itemize}
|
|
293 |
|
52827
|
294 |
The discriminators and selectors are collectively called \emph{destructors}. The
|
|
295 |
@{text "t."} prefix is an optional component of the name and is normally hidden.
|
52822
|
296 |
|
|
297 |
The set functions, map function, relator, discriminators, and selectors can be
|
|
298 |
given custom names, as in the example below:
|
|
299 |
*}
|
|
300 |
|
|
301 |
(*<*)hide_const Nil Cons hd tl(*>*)
|
|
302 |
datatype_new (set: 'a) list (map: map rel: list_all2) =
|
|
303 |
null: Nil (defaults tl: Nil)
|
|
304 |
| Cons (hd: 'a) (tl: "'a list")
|
|
305 |
|
|
306 |
text {*
|
|
307 |
\noindent
|
|
308 |
The command introduces a discriminator @{const null} and a pair of selectors
|
|
309 |
@{const hd} and @{const tl} characterized as follows:
|
|
310 |
%
|
|
311 |
\[@{thm list.collapse(1)[of xs, no_vars]}
|
|
312 |
\qquad @{thm list.collapse(2)[of xs, no_vars]}\]
|
|
313 |
%
|
|
314 |
For two-constructor datatypes, a single discriminator constant suffices. The
|
|
315 |
discriminator associated with @{const Cons} is simply @{text "\<not> null"}.
|
|
316 |
|
52824
|
317 |
The \keyw{defaults} keyword following the @{const Nil} constructor specifies a
|
52822
|
318 |
default value for selectors associated with other constructors. Here, it is
|
|
319 |
used to specify that the tail of the empty list is the empty list (instead of
|
|
320 |
being unspecified).
|
|
321 |
|
|
322 |
Because @{const Nil} is a nullary constructor, it is also possible to use @{text
|
|
323 |
"= Nil"} as a discriminator. This is specified by specifying @{text "="} instead
|
|
324 |
of the identifier @{const null} in the declaration above. Although this may look
|
|
325 |
appealing, the mixture of constructors and selectors in the resulting
|
|
326 |
characteristic theorems can lead Isabelle's automation to switch between the
|
|
327 |
constructor and the destructor view in surprising ways.
|
|
328 |
*}
|
|
329 |
|
|
330 |
text {*
|
|
331 |
The usual mixfix syntaxes are available for both types and constructors. For example:
|
|
332 |
|
|
333 |
%%% FIXME: remove trailing underscore and use locale trick instead once this is
|
|
334 |
%%% supported.
|
52805
|
335 |
*}
|
52794
|
336 |
|
52822
|
337 |
datatype_new ('a, 'b) prod (infixr "*" 20) =
|
|
338 |
Pair 'a 'b
|
|
339 |
|
|
340 |
datatype_new (set_: 'a) list_ =
|
|
341 |
null: Nil ("[]")
|
|
342 |
| Cons (hd: 'a) (tl: "'a list_") (infixr "#" 65)
|
|
343 |
|
52824
|
344 |
|
52827
|
345 |
subsection {* General Syntax
|
|
346 |
\label{datatype-general-syntax} *}
|
52794
|
347 |
|
52822
|
348 |
text {*
|
|
349 |
Datatype definitions have the following general syntax:
|
|
350 |
|
52824
|
351 |
@{rail "
|
52827
|
352 |
@@{command datatype_new} ('(' ((@'no_dests' | @'rep_compat') + ',') ')')? \\
|
52824
|
353 |
(@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
|
|
354 |
"}
|
|
355 |
|
52827
|
356 |
Two general options are supported:
|
52822
|
357 |
|
52824
|
358 |
\begin{itemize}
|
|
359 |
\setlength{\itemsep}{0pt}
|
|
360 |
|
|
361 |
\item
|
|
362 |
The \keyw{no\_dests} option indicates that no destructors (i.e.,
|
|
363 |
discriminators and selectors) should be generated.
|
52822
|
364 |
|
52824
|
365 |
\item
|
|
366 |
The \keyw{rep\_compat} option indicates that the names generated by the
|
|
367 |
package should contain optional (and normally not displayed) @{text "new."}
|
|
368 |
components to prevent clashes with a later call to @{command rep_datatype}. See
|
|
369 |
Section~\ref{ssec:datatype-compatibility-issues} for details.
|
|
370 |
\end{itemize}
|
52822
|
371 |
|
52827
|
372 |
The left-hand sides of the datatype equations specify the name of the type to
|
|
373 |
define, its type parameters, and optional additional information:
|
52822
|
374 |
|
52824
|
375 |
@{rail "
|
52827
|
376 |
@{syntax_def dt_name}: @{syntax tyargs}? @{syntax name}
|
52824
|
377 |
@{syntax map_rel}? @{syntax mixfix}?
|
|
378 |
;
|
52827
|
379 |
@{syntax_def tyargs}: @{syntax typefree} | '(' ((@{syntax name} ':')? @{syntax typefree} + ',') ')'
|
52824
|
380 |
;
|
|
381 |
@{syntax_def map_rel}: '(' ((('map' | 'rel') ':' @{syntax name}) +) ')'
|
|
382 |
"}
|
52822
|
383 |
|
52827
|
384 |
\noindent
|
|
385 |
The syntactic quantity @{syntax name} denotes an identifier, @{syntax typefree}
|
|
386 |
denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and @{syntax
|
|
387 |
mixfix} denotes the usual parenthesized mixfix notation. They are documented in
|
|
388 |
the Isar reference manual \cite{isabelle-isar-ref}.
|
52822
|
389 |
|
52827
|
390 |
The optional names preceding the type variables allow to override the default
|
|
391 |
names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
|
|
392 |
Inside a mutually recursive datatype specification, all defined datatypes must
|
|
393 |
specify exactly the same type variables in the same order.
|
52822
|
394 |
|
52824
|
395 |
@{rail "
|
|
396 |
@{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} *) \\
|
|
397 |
@{syntax sel_defaults}? @{syntax mixfix}?
|
|
398 |
"}
|
|
399 |
|
52827
|
400 |
\noindent
|
|
401 |
The main constituents of a constructor specification is the name of the
|
|
402 |
constructor and the list of its argument types. An optional discriminator name
|
|
403 |
can be supplied at the front to override the default name
|
|
404 |
(@{text t.un_C}$_{ij}$).
|
52822
|
405 |
|
52824
|
406 |
@{rail "
|
|
407 |
@{syntax_def ctor_arg}: @{syntax type} | '(' (@{syntax name} ':')? @{syntax type} ')'
|
52827
|
408 |
"}
|
|
409 |
|
|
410 |
\noindent
|
|
411 |
In addition to the type of a constructor argument, it is possible to specify a
|
|
412 |
name for the corresponding selector to override the default name
|
|
413 |
(@{text t.un_C}$_{ij}$). The same selector names can be reused for several
|
|
414 |
constructors as long as they have the same type.
|
|
415 |
|
|
416 |
@{rail "
|
52824
|
417 |
@{syntax_def sel_defaults}: '(' @'defaults' (@{syntax name} ':' @{syntax term} *) ')'
|
|
418 |
"}
|
52827
|
419 |
|
|
420 |
\noindent
|
|
421 |
Given a constructor
|
|
422 |
@{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
|
|
423 |
default values can be specified for any selector
|
|
424 |
@{text "un_D \<Colon> \<sigma> \<Rightarrow> \<tau>"}
|
|
425 |
associated with other constructors. The specified default value must have type
|
|
426 |
@{text "\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"}
|
|
427 |
(i.e., it may dependend on @{text C}'s arguments).
|
52822
|
428 |
*}
|
|
429 |
|
52794
|
430 |
subsection {* Characteristic Theorems *}
|
|
431 |
|
52827
|
432 |
subsection {* Compatibility Issues
|
52824
|
433 |
\label{ssec:datatype-compatibility-issues} *}
|
52794
|
434 |
|
52792
|
435 |
|
52827
|
436 |
section {* Defining Recursive Functions
|
52805
|
437 |
\label{sec:defining-recursive-functions} *}
|
|
438 |
|
|
439 |
text {*
|
|
440 |
This describes how to specify recursive functions over datatypes
|
52824
|
441 |
specified using @{command datatype_new}. The focus in on the \keyw{primrec\_new}
|
52805
|
442 |
command, which supports primitive recursion. A few examples feature the
|
52822
|
443 |
@{command fun} and @{command function} commands, described in a separate
|
|
444 |
tutorial \cite{isabelle-function}.
|
52805
|
445 |
%%% TODO: partial_function?
|
|
446 |
*}
|
52792
|
447 |
|
52824
|
448 |
|
52794
|
449 |
subsection {* Introductory Examples *}
|
|
450 |
|
52805
|
451 |
text {*
|
|
452 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
|
453 |
*}
|
|
454 |
|
52824
|
455 |
|
52827
|
456 |
subsection {* General Syntax
|
|
457 |
\label{primrec-general-syntax} *}
|
52794
|
458 |
|
52824
|
459 |
|
52794
|
460 |
subsection {* Characteristic Theorems *}
|
|
461 |
|
52824
|
462 |
|
52827
|
463 |
subsection {* Recursive Default Values
|
|
464 |
\label{ssec:recursive-default-values} *}
|
|
465 |
|
|
466 |
text {*
|
|
467 |
A datatype selector @{text un_D} can have a default value for each constructor
|
|
468 |
on which it is not otherwise specified. Occasionally, it is useful to have the
|
|
469 |
default value be defined recursively. This produces a chicken-and-egg situation
|
|
470 |
that appears unsolvable, because the datatype is not introduced yet at the
|
|
471 |
moment when the selectors are introduced. Of course, we can always define the
|
|
472 |
selectors manually afterward, but we then have to state and prove all the
|
|
473 |
characteristic theorems ourselves instead of letting the package do it.
|
|
474 |
|
|
475 |
Fortunately, there is a fairly elegant workaround that relies on overloading and
|
|
476 |
that avoids the tedium of manual derivations:
|
|
477 |
|
|
478 |
\begin{enumerate}
|
|
479 |
\setlength{\itemsep}{0pt}
|
|
480 |
|
|
481 |
\item
|
|
482 |
Introduce a fully unspecified constant @{text "un_D\<^sub>0 \<Colon> 'a"} using
|
|
483 |
@{keyword consts}.
|
|
484 |
|
|
485 |
\item
|
|
486 |
Define the datatype, specifying @{text "un_D\<^sub>0"} as the selector's default value.
|
|
487 |
|
|
488 |
\item
|
|
489 |
Define the behavior of @{text "un_D\<^sub>0"} on values of the newly introduced datatype
|
|
490 |
using the @{command overloading} command.
|
|
491 |
|
|
492 |
\item
|
|
493 |
Derive the desired equation on @{text un_D} from the characteristic equations
|
|
494 |
for @{text "un_D\<^sub>0"}.
|
|
495 |
\end{enumerate}
|
|
496 |
|
|
497 |
The following example illustrates this procedure:
|
|
498 |
*}
|
|
499 |
|
|
500 |
consts termi\<^sub>0 :: 'a
|
|
501 |
|
|
502 |
datatype_new (*<*)(rep_compat) (*>*)('a, 'b) tlist_ =
|
|
503 |
TNil (termi: 'b) (defaults ttl: TNil)
|
|
504 |
| TCons (thd: 'a) (ttl : "('a, 'b) tlist_") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs")
|
|
505 |
|
|
506 |
(*<*)
|
|
507 |
rep_datatype TNil TCons
|
|
508 |
by (erule tlist_.induct, assumption) auto
|
|
509 |
(*>*)
|
|
510 |
|
|
511 |
overloading
|
|
512 |
termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist_ \<Rightarrow> 'b"
|
|
513 |
begin
|
|
514 |
|
|
515 |
(*<*)(*FIXME: use primrec_new and avoid rep_datatype*)(*>*)
|
|
516 |
fun termi\<^sub>0 :: "('a, 'b) tlist_ \<Rightarrow> 'b" where
|
|
517 |
"termi\<^sub>0 (TNil y) = y" |
|
|
518 |
"termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs"
|
|
519 |
|
|
520 |
end
|
|
521 |
|
|
522 |
lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs"
|
|
523 |
by (cases xs) auto
|
|
524 |
|
|
525 |
|
52794
|
526 |
subsection {* Compatibility Issues *}
|
|
527 |
|
|
528 |
|
52827
|
529 |
section {* Defining Codatatypes
|
52805
|
530 |
\label{sec:defining-codatatypes} *}
|
|
531 |
|
|
532 |
text {*
|
52822
|
533 |
This section describes how to specify codatatypes using the @{command codatatype}
|
52805
|
534 |
command.
|
|
535 |
*}
|
52792
|
536 |
|
52824
|
537 |
|
52794
|
538 |
subsection {* Introductory Examples *}
|
|
539 |
|
52805
|
540 |
text {*
|
|
541 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
|
542 |
*}
|
|
543 |
|
52824
|
544 |
|
52827
|
545 |
subsection {* General Syntax
|
|
546 |
\label{codatatype-general-syntax} *}
|
52805
|
547 |
|
52824
|
548 |
text {*
|
52827
|
549 |
Definitions of codatatypes have almost exactly the same syntax as for datatypes
|
|
550 |
(Section \ref{ssec:datatype-general-syntax}), with two exceptions: The command
|
|
551 |
is called @{command codatatype}; the \keyw{no\_dests} option is not
|
|
552 |
available, because destructors are a central notion for codatatypes.
|
|
553 |
|
|
554 |
@{rail "
|
|
555 |
@@{command codatatype} ('(' (@'rep_compat' + ',') ')')? \\
|
|
556 |
(@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
|
|
557 |
"}
|
52824
|
558 |
*}
|
|
559 |
|
52805
|
560 |
subsection {* Characteristic Theorems *}
|
|
561 |
|
|
562 |
|
52827
|
563 |
section {* Defining Corecursive Functions
|
52805
|
564 |
\label{sec:defining-corecursive-functions} *}
|
|
565 |
|
|
566 |
text {*
|
|
567 |
This section describes how to specify corecursive functions using the
|
52824
|
568 |
\keyw{primcorec} command.
|
52805
|
569 |
*}
|
|
570 |
|
52824
|
571 |
|
52805
|
572 |
subsection {* Introductory Examples *}
|
|
573 |
|
|
574 |
text {*
|
|
575 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
52827
|
576 |
|
|
577 |
Also, for default values, the same trick as for datatypes is possible for
|
|
578 |
codatatypes (Section~\ref{ssec:recursive-default-values}).
|
52805
|
579 |
*}
|
|
580 |
|
52824
|
581 |
|
52827
|
582 |
subsection {* General Syntax
|
|
583 |
\label{primcorec-general-syntax} *}
|
52794
|
584 |
|
52824
|
585 |
|
52794
|
586 |
subsection {* Characteristic Theorems *}
|
|
587 |
|
|
588 |
|
52827
|
589 |
section {* Registering Bounded Natural Functors
|
52805
|
590 |
\label{sec:registering-bounded-natural-functors} *}
|
52792
|
591 |
|
52805
|
592 |
text {*
|
|
593 |
This section explains how to set up the (co)datatype package to allow nested
|
|
594 |
recursion through custom well-behaved type constructors. The key concept is that
|
|
595 |
of a bounded natural functor (BNF).
|
|
596 |
*}
|
|
597 |
|
52824
|
598 |
|
52805
|
599 |
subsection {* Introductory Example *}
|
|
600 |
|
|
601 |
text {*
|
|
602 |
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
|
|
603 |
\verb|~~/src/HOL/BNF/More_BNFs.thy|.
|
52806
|
604 |
|
|
605 |
Mention distinction between live and dead type arguments;
|
|
606 |
mention =>.
|
52805
|
607 |
*}
|
52794
|
608 |
|
52824
|
609 |
|
52827
|
610 |
subsection {* General Syntax
|
|
611 |
\label{bnf-general-syntax} *}
|
52794
|
612 |
|
52805
|
613 |
|
52827
|
614 |
section {* Generating Free Constructor Theorems
|
52805
|
615 |
\label{sec:generating-free-constructor-theorems} *}
|
52794
|
616 |
|
52805
|
617 |
text {*
|
|
618 |
This section explains how to derive convenience theorems for free constructors,
|
52822
|
619 |
as performed internally by @{command datatype_new} and @{command codatatype}.
|
52794
|
620 |
|
52805
|
621 |
* need for this is rare but may arise if you want e.g. to add destructors to
|
|
622 |
a type not introduced by ...
|
52794
|
623 |
|
52805
|
624 |
* also useful for compatibility with old package, e.g. add destructors to
|
52822
|
625 |
old @{command datatype}
|
52805
|
626 |
*}
|
52792
|
627 |
|
52824
|
628 |
|
52794
|
629 |
subsection {* Introductory Example *}
|
|
630 |
|
52824
|
631 |
|
52827
|
632 |
subsection {* General Syntax
|
|
633 |
\label{ctors-general-syntax} *}
|
52794
|
634 |
|
|
635 |
|
52827
|
636 |
section {* Standard ML Interface
|
52805
|
637 |
\label{sec:standard-ml-interface} *}
|
52792
|
638 |
|
52805
|
639 |
text {*
|
|
640 |
This section describes the package's programmatic interface.
|
|
641 |
*}
|
52794
|
642 |
|
|
643 |
|
52827
|
644 |
section {* Interoperability
|
52805
|
645 |
\label{sec:interoperability} *}
|
52794
|
646 |
|
52805
|
647 |
text {*
|
|
648 |
This section is concerned with the packages' interaction with other Isabelle
|
|
649 |
packages and tools, such as the code generator and the counterexample
|
|
650 |
generators.
|
|
651 |
*}
|
52794
|
652 |
|
52824
|
653 |
|
52794
|
654 |
subsection {* Transfer and Lifting *}
|
|
655 |
|
52824
|
656 |
|
52794
|
657 |
subsection {* Code Generator *}
|
|
658 |
|
52824
|
659 |
|
52794
|
660 |
subsection {* Quickcheck *}
|
|
661 |
|
52824
|
662 |
|
52794
|
663 |
subsection {* Nitpick *}
|
|
664 |
|
52824
|
665 |
|
52794
|
666 |
subsection {* Nominal Isabelle *}
|
|
667 |
|
52805
|
668 |
|
52827
|
669 |
section {* Known Bugs and Limitations
|
52805
|
670 |
\label{sec:known-bugs-and-limitations} *}
|
|
671 |
|
|
672 |
text {*
|
|
673 |
This section lists known open issues of the package.
|
|
674 |
*}
|
52794
|
675 |
|
|
676 |
text {*
|
52806
|
677 |
* primrec\_new and primcorec are vaporware
|
|
678 |
|
52794
|
679 |
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
|
52806
|
680 |
|
|
681 |
* issues with HOL-Proofs?
|
|
682 |
|
|
683 |
* partial documentation
|
|
684 |
|
|
685 |
* too much output by commands like "datatype_new" and "codatatype"
|
52822
|
686 |
|
|
687 |
* no direct way to define recursive functions for default values -- but show trick
|
|
688 |
based on overloading
|
|
689 |
*}
|
|
690 |
|
|
691 |
|
52827
|
692 |
section {* Acknowledgments
|
52822
|
693 |
\label{sec:acknowledgments} *}
|
|
694 |
|
|
695 |
text {*
|
52827
|
696 |
Tobias Nipkow and Makarius Wenzel have made this work possible. Andreas
|
|
697 |
Lochbihler has provided lots of comments on earlier versions of the package,
|
|
698 |
especially for the coinductive part. Brian Huffman suggested major
|
|
699 |
simplifications to the internal constructions, much of which has yet to be
|
|
700 |
implemented. Stefan Milius and Lutz Schr\"oder suggested an elegant prove to
|
|
701 |
eliminate one of the BNF assumptions. Florian Haftmann and Christian Urban gave
|
|
702 |
advice on Isabelle and package writing.
|
52794
|
703 |
*}
|
52792
|
704 |
|
|
705 |
end
|