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
|
|
8 |
imports BNF
|
|
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};
|
|
17 |
indeed, replacing \cmd{datatype} by \cmd{datatype\_new} is usually sufficient
|
|
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
|
|
38 |
generated destructors. For example, the command
|
|
39 |
*}
|
52792
|
40 |
|
52794
|
41 |
(*<*)hide_const Nil Cons hd tl(*>*)
|
52805
|
42 |
datatype_new 'a list = null: Nil | Cons (hd: 'a) (tl: "'a list")
|
52794
|
43 |
|
|
44 |
text {*
|
|
45 |
\noindent
|
|
46 |
introduces a discriminator @{const null} and a pair of selectors @{const hd} and
|
|
47 |
@{const tl} characterized as follows:
|
52805
|
48 |
%
|
|
49 |
\[@{thm list.collapse(1)[no_vars]}\qquad @{thm list.collapse(2)[no_vars]}\]
|
52794
|
50 |
|
|
51 |
The command \cmd{datatype\_new} is expected to displace \cmd{datatype} in a future
|
|
52 |
release. Authors of new theories are encouraged to use \cmd{datatype\_new}, and
|
52805
|
53 |
maintainers of older theories may want to consider upgrading in the coming months.
|
52794
|
54 |
|
|
55 |
The package also provides codatatypes (or ``coinductive datatypes''), which may
|
52805
|
56 |
have infinite values. The following command introduces a codatatype of infinite
|
52794
|
57 |
streams:
|
|
58 |
*}
|
|
59 |
|
52805
|
60 |
codatatype 'a stream = Stream 'a "'a stream"
|
52794
|
61 |
|
|
62 |
text {*
|
|
63 |
\noindent
|
|
64 |
Mixed inductive--coinductive recursion is possible via nesting.
|
|
65 |
Compare the following four examples:
|
|
66 |
*}
|
|
67 |
|
52805
|
68 |
datatype_new 'a treeFF = TreeFF 'a "'a treeFF list"
|
|
69 |
datatype_new 'a treeFI = TreeFI 'a "'a treeFF stream"
|
|
70 |
codatatype 'a treeIF = TreeIF 'a "'a treeFF list"
|
|
71 |
codatatype 'a treeII = TreeII 'a "'a treeFF stream"
|
52792
|
72 |
|
52794
|
73 |
text {*
|
|
74 |
To use the package, it is necessary to import the @{theory BNF} theory, which
|
52805
|
75 |
can be precompiled as the \textit{HOL-BNF} image:
|
52794
|
76 |
*}
|
|
77 |
|
|
78 |
text {*
|
|
79 |
\noindent
|
52805
|
80 |
\ \ \ \ \texttt{isabelle build -b HOL-BNF}
|
52794
|
81 |
*}
|
|
82 |
|
|
83 |
text {*
|
52805
|
84 |
The package, like its predecessor, fully adheres to the LCF philosophy
|
|
85 |
\cite{mgordon79}: The characteristic theorems associated with the specified
|
|
86 |
(co)datatypes are derived rather than introduced axiomatically.%
|
|
87 |
\footnote{Nonetheless, if the \textit{quick\_and\_dirty} option is enabled, some
|
|
88 |
of the internal constructions and most of the internal proof obligations are
|
|
89 |
skipped.}
|
|
90 |
The package's metatheory is described in a pair of papers
|
|
91 |
\cite{traytel-et-al-2012,blanchette-et-al-wit}.
|
52792
|
92 |
*}
|
|
93 |
|
52794
|
94 |
text {*
|
|
95 |
This tutorial is organized as follows:
|
|
96 |
|
52805
|
97 |
\begin{itemize}
|
|
98 |
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,''
|
|
99 |
describes how to specify datatypes using the \cmd{datatype\_new} command.
|
|
100 |
|
|
101 |
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive
|
|
102 |
Functions,'' describes how to specify recursive functions using
|
|
103 |
\cmd{primrec\_new}, \cmd{fun}, and \cmd{function}.
|
|
104 |
|
|
105 |
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,''
|
|
106 |
describes how to specify codatatypes using the \cmd{codatatype} command.
|
|
107 |
|
|
108 |
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive
|
|
109 |
Functions,'' describes how to specify corecursive functions using the
|
|
110 |
\cmd{primcorec} command.
|
52794
|
111 |
|
52805
|
112 |
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering
|
|
113 |
Bounded Natural Functors,'' explains how to set up the (co)datatype package to
|
|
114 |
allow nested recursion through custom well-behaved type constructors.
|
|
115 |
|
|
116 |
\item Section \ref{sec:generating-free-constructor-theorems}, ``Generating Free
|
|
117 |
Constructor Theorems,'' explains how to derive convenience theorems for free
|
|
118 |
constructors, as performed internally by \cmd{datatype\_new} and
|
|
119 |
\cmd{codatatype}.
|
52794
|
120 |
|
52805
|
121 |
\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,''
|
|
122 |
describes the package's programmatic interface.
|
52794
|
123 |
|
52805
|
124 |
\item Section \ref{sec:interoperability}, ``Interoperability,''
|
|
125 |
is concerned with the packages' interaction with other Isabelle packages and
|
|
126 |
tools, such as the code generator and the counterexample generators.
|
|
127 |
|
|
128 |
\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and
|
|
129 |
Limitations,'' concludes with open issues.
|
|
130 |
\end{itemize}
|
52794
|
131 |
*}
|
|
132 |
|
52805
|
133 |
section {* Defining Datatypes%
|
|
134 |
\label{sec:defining-datatypes} *}
|
|
135 |
|
|
136 |
text {*
|
|
137 |
This section describes how to specify datatypes using the \cmd{datatype\_new}
|
|
138 |
command. The command is first illustrated through concrete examples featuring
|
|
139 |
different flavors of recursion. More examples are available in
|
|
140 |
\verb|~~/src/HOL/BNF/Examples|. The syntax is largely modeled after that of the
|
|
141 |
existing \cmd{datatype} command.
|
|
142 |
*}
|
52792
|
143 |
|
52794
|
144 |
subsection {* Introductory Examples *}
|
|
145 |
|
|
146 |
subsubsection {* Nonrecursive Types *}
|
|
147 |
|
52805
|
148 |
text {*
|
|
149 |
enumeration type:
|
|
150 |
*}
|
|
151 |
|
|
152 |
datatype_new trool = Truue | Faalse | Maaybe
|
|
153 |
|
|
154 |
text {*
|
|
155 |
Haskell-style option type:
|
|
156 |
*}
|
|
157 |
|
|
158 |
datatype_new 'a maybe = Nothing | Just 'a
|
|
159 |
|
|
160 |
text {*
|
|
161 |
triple:
|
|
162 |
*}
|
|
163 |
|
|
164 |
datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c
|
|
165 |
|
52794
|
166 |
subsubsection {* Simple Recursion *}
|
|
167 |
|
52805
|
168 |
text {*
|
|
169 |
simplest recursive type: natural numbers
|
|
170 |
*}
|
|
171 |
|
|
172 |
datatype_new nat = Zero | Suc nat
|
|
173 |
|
|
174 |
text {*
|
|
175 |
lists were shown in the introduction
|
|
176 |
|
|
177 |
terminated lists are a variant:
|
|
178 |
*}
|
|
179 |
|
|
180 |
datatype_new ('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
|
|
181 |
|
|
182 |
text {*
|
|
183 |
On the right-hand side of the equal sign, the usual Isabelle conventions apply:
|
|
184 |
Nonatomic types must be enclosed in double quotes.
|
|
185 |
*}
|
|
186 |
|
52794
|
187 |
subsubsection {* Mutual Recursion *}
|
|
188 |
|
52805
|
189 |
text {*
|
|
190 |
Mutual recursion = Define several types simultaneously, referring to each other.
|
|
191 |
|
|
192 |
Simple example: distinction between even and odd natural numbers:
|
|
193 |
*}
|
|
194 |
|
|
195 |
datatype_new even_nat = Zero | Even_Suc odd_nat
|
|
196 |
and odd_nat = Odd_Suc even_nat
|
|
197 |
|
|
198 |
text {*
|
|
199 |
More complex, and more realistic, example:
|
|
200 |
*}
|
|
201 |
|
|
202 |
datatype_new ('a, 'b) expr =
|
|
203 |
Term "('a, 'b) term" | Sum "('a, 'b) term" "('a, 'b) expr"
|
|
204 |
and ('a, 'b) trm =
|
|
205 |
Factor "('a, 'b) factor" | Prod "('a, 'b) factor" "('a, 'b) trm"
|
|
206 |
and ('a, 'b) factor =
|
|
207 |
Const 'a | Var 'b | Sub_Expr "('a, 'b) expr"
|
|
208 |
|
52794
|
209 |
subsubsection {* Nested Recursion *}
|
|
210 |
|
52805
|
211 |
text {*
|
|
212 |
Nested recursion = Have recursion through a type constructor.
|
|
213 |
|
|
214 |
The introduction showed some examples of trees with nesting through lists.
|
|
215 |
|
|
216 |
More complex example, which reuses our maybe and triple types:
|
|
217 |
*}
|
|
218 |
|
|
219 |
datatype_new 'a triple_tree =
|
|
220 |
Triple_Tree "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
|
|
221 |
|
|
222 |
text {*
|
|
223 |
Recursion may not be arbitrary; e.g. impossible to define
|
|
224 |
*}
|
|
225 |
|
|
226 |
(*
|
|
227 |
datatype_new 'a foo = Foo (*<*) datatype_new 'a bar = Bar "'a foo \<Rightarrow> 'a foo"
|
|
228 |
*)
|
|
229 |
|
|
230 |
subsubsection {* Auxiliary Constants and Syntaxes *}
|
|
231 |
|
|
232 |
text {*
|
|
233 |
* destructors
|
|
234 |
* also mention special syntaxes
|
|
235 |
*}
|
52794
|
236 |
|
|
237 |
subsection {* General Syntax *}
|
|
238 |
|
|
239 |
subsection {* Characteristic Theorems *}
|
|
240 |
|
|
241 |
subsection {* Compatibility Issues *}
|
|
242 |
|
52792
|
243 |
|
52805
|
244 |
section {* Defining Recursive Functions%
|
|
245 |
\label{sec:defining-recursive-functions} *}
|
|
246 |
|
|
247 |
text {*
|
|
248 |
This describes how to specify recursive functions over datatypes
|
|
249 |
specified using \cmd{datatype\_new}. The focus in on the \cmd{primrec\_new}
|
|
250 |
command, which supports primitive recursion. A few examples feature the
|
|
251 |
\cmd{fun} and \cmd{function} commands, described in a separate tutorial
|
|
252 |
\cite{isabelle-function}.
|
|
253 |
%%% TODO: partial_function?
|
|
254 |
*}
|
52792
|
255 |
|
52794
|
256 |
subsection {* Introductory Examples *}
|
|
257 |
|
52805
|
258 |
text {*
|
|
259 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
|
260 |
*}
|
|
261 |
|
52794
|
262 |
subsection {* General Syntax *}
|
|
263 |
|
|
264 |
subsection {* Characteristic Theorems *}
|
|
265 |
|
|
266 |
subsection {* Compatibility Issues *}
|
|
267 |
|
|
268 |
|
52805
|
269 |
section {* Defining Codatatypes%
|
|
270 |
\label{sec:defining-codatatypes} *}
|
|
271 |
|
|
272 |
text {*
|
|
273 |
This section describes how to specify codatatypes using the \cmd{codatatype}
|
|
274 |
command.
|
|
275 |
*}
|
52792
|
276 |
|
52794
|
277 |
subsection {* Introductory Examples *}
|
|
278 |
|
52805
|
279 |
text {*
|
|
280 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
|
281 |
*}
|
|
282 |
|
|
283 |
subsection {* General Syntax *}
|
|
284 |
|
|
285 |
subsection {* Characteristic Theorems *}
|
|
286 |
|
|
287 |
|
|
288 |
section {* Defining Corecursive Functions%
|
|
289 |
\label{sec:defining-corecursive-functions} *}
|
|
290 |
|
|
291 |
text {*
|
|
292 |
This section describes how to specify corecursive functions using the
|
|
293 |
\cmd{primcorec} command.
|
|
294 |
*}
|
|
295 |
|
|
296 |
subsection {* Introductory Examples *}
|
|
297 |
|
|
298 |
text {*
|
|
299 |
More examples in \verb|~~/src/HOL/BNF/Examples|.
|
|
300 |
*}
|
|
301 |
|
52794
|
302 |
subsection {* General Syntax *}
|
|
303 |
|
|
304 |
subsection {* Characteristic Theorems *}
|
|
305 |
|
|
306 |
|
52805
|
307 |
section {* Registering Bounded Natural Functors%
|
|
308 |
\label{sec:registering-bounded-natural-functors} *}
|
52792
|
309 |
|
52805
|
310 |
text {*
|
|
311 |
This section explains how to set up the (co)datatype package to allow nested
|
|
312 |
recursion through custom well-behaved type constructors. The key concept is that
|
|
313 |
of a bounded natural functor (BNF).
|
|
314 |
*}
|
|
315 |
|
|
316 |
subsection {* Introductory Example *}
|
|
317 |
|
|
318 |
text {*
|
|
319 |
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
|
|
320 |
\verb|~~/src/HOL/BNF/More_BNFs.thy|.
|
|
321 |
*}
|
52794
|
322 |
|
|
323 |
subsection {* General Syntax *}
|
|
324 |
|
52805
|
325 |
|
|
326 |
section {* Generating Free Constructor Theorems%
|
|
327 |
\label{sec:generating-free-constructor-theorems} *}
|
52794
|
328 |
|
52805
|
329 |
text {*
|
|
330 |
This section explains how to derive convenience theorems for free constructors,
|
|
331 |
as performed internally by \cmd{datatype\_new} and \cmd{codatatype}.
|
52794
|
332 |
|
52805
|
333 |
* need for this is rare but may arise if you want e.g. to add destructors to
|
|
334 |
a type not introduced by ...
|
52794
|
335 |
|
52805
|
336 |
* also useful for compatibility with old package, e.g. add destructors to
|
|
337 |
old \cmd{datatype}
|
|
338 |
*}
|
52792
|
339 |
|
52794
|
340 |
subsection {* Introductory Example *}
|
|
341 |
|
|
342 |
subsection {* General Syntax *}
|
|
343 |
|
|
344 |
|
52805
|
345 |
section {* Standard ML Interface%
|
|
346 |
\label{sec:standard-ml-interface} *}
|
52792
|
347 |
|
52805
|
348 |
text {*
|
|
349 |
This section describes the package's programmatic interface.
|
|
350 |
*}
|
52794
|
351 |
|
|
352 |
|
52805
|
353 |
section {* Interoperability%
|
|
354 |
\label{sec:interoperability} *}
|
52794
|
355 |
|
52805
|
356 |
text {*
|
|
357 |
This section is concerned with the packages' interaction with other Isabelle
|
|
358 |
packages and tools, such as the code generator and the counterexample
|
|
359 |
generators.
|
|
360 |
*}
|
52794
|
361 |
|
|
362 |
subsection {* Transfer and Lifting *}
|
|
363 |
|
|
364 |
subsection {* Code Generator *}
|
|
365 |
|
|
366 |
subsection {* Quickcheck *}
|
|
367 |
|
|
368 |
subsection {* Nitpick *}
|
|
369 |
|
|
370 |
subsection {* Nominal Isabelle *}
|
|
371 |
|
52805
|
372 |
|
|
373 |
section {* Known Bugs and Limitations%
|
|
374 |
\label{sec:known-bugs-and-limitations} *}
|
|
375 |
|
|
376 |
text {*
|
|
377 |
This section lists known open issues of the package.
|
|
378 |
*}
|
52794
|
379 |
|
|
380 |
text {*
|
|
381 |
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
|
|
382 |
*}
|
52792
|
383 |
|
|
384 |
end
|