author | blanchet |
Sun, 15 Sep 2013 23:02:23 +0200 | |
changeset 53646 | ac6e0a28489f |
parent 53644 | eb8362530715 |
child 53647 | e78ebb290dd6 |
permissions | -rw-r--r-- |
52792 | 1 |
(* Title: Doc/Datatypes/Datatypes.thy |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
53617 | 3 |
Author: Lorenz Panny, TU Muenchen |
4 |
Author: Andrei Popescu, TU Muenchen |
|
5 |
Author: Dmitriy Traytel, TU Muenchen |
|
52792 | 6 |
|
7 |
Tutorial for (co)datatype definitions with the new package. |
|
8 |
*) |
|
9 |
||
10 |
theory Datatypes |
|
52824 | 11 |
imports Setup |
53644 | 12 |
keywords |
13 |
"primcorec_notyet" :: thy_decl |
|
52792 | 14 |
begin |
15 |
||
52828 | 16 |
|
53644 | 17 |
(*<*) |
18 |
(* FIXME: Temporary setup until "primcorec" is fully implemented. *) |
|
19 |
ML_command {* |
|
20 |
fun add_dummy_cmd _ _ lthy = lthy; |
|
21 |
||
22 |
val _ = Outer_Syntax.local_theory @{command_spec "primcorec_notyet"} "" |
|
23 |
(Parse.fixes -- Parse_Spec.where_alt_specs >> uncurry add_dummy_cmd); |
|
24 |
*} |
|
25 |
(*>*) |
|
26 |
||
27 |
||
52828 | 28 |
section {* Introduction |
29 |
\label{sec:introduction} *} |
|
52792 | 30 |
|
31 |
text {* |
|
53028 | 32 |
The 2013 edition of Isabelle introduced a new definitional package for freely |
33 |
generated datatypes and codatatypes. The datatype support is similar to that |
|
34 |
provided by the earlier package due to Berghofer and Wenzel |
|
35 |
\cite{Berghofer-Wenzel:1999:TPHOL}, documented in the Isar reference manual |
|
53535 | 36 |
\cite{isabelle-isar-ref}; indeed, replacing the keyword \keyw{datatype} by |
53028 | 37 |
@{command datatype_new} is usually all that is needed to port existing theories |
38 |
to use the new package. |
|
52840 | 39 |
|
52841 | 40 |
Perhaps the main advantage of the new package is that it supports recursion |
53619 | 41 |
through a large class of non-datatypes, such as finite sets: |
52792 | 42 |
*} |
43 |
||
53644 | 44 |
datatype_new 'a tree\<^sub>f\<^sub>s = Node\<^sub>f\<^sub>s (lbl\<^sub>f\<^sub>s: 'a) (sub\<^sub>f\<^sub>s: "'a tree\<^sub>f\<^sub>s fset") |
52792 | 45 |
|
46 |
text {* |
|
52794 | 47 |
\noindent |
53025 | 48 |
Another strong point is the support for local definitions: |
52792 | 49 |
*} |
50 |
||
52805 | 51 |
context linorder |
52 |
begin |
|
52841 | 53 |
datatype_new flag = Less | Eq | Greater |
52805 | 54 |
end |
52792 | 55 |
|
56 |
text {* |
|
52794 | 57 |
\noindent |
52840 | 58 |
The package also provides some convenience, notably automatically generated |
53543 | 59 |
discriminators and selectors. |
52794 | 60 |
|
53025 | 61 |
In addition to plain inductive datatypes, the new package supports coinductive |
52840 | 62 |
datatypes, or \emph{codatatypes}, which may have infinite values. For example, |
53028 | 63 |
the following command introduces the type of lazy lists, which comprises both |
64 |
finite and infinite values: |
|
52794 | 65 |
*} |
66 |
||
53623 | 67 |
(*<*) |
68 |
locale dummy_llist |
|
69 |
begin |
|
70 |
(*>*) |
|
71 |
codatatype 'a llist = LNil | LCons 'a "'a llist" |
|
52794 | 72 |
|
73 |
text {* |
|
74 |
\noindent |
|
52840 | 75 |
Mixed inductive--coinductive recursion is possible via nesting. Compare the |
53028 | 76 |
following four Rose tree examples: |
52794 | 77 |
*} |
78 |
||
53644 | 79 |
datatype_new 'a tree\<^sub>f\<^sub>f = Node\<^sub>f\<^sub>f (lbl\<^sub>f\<^sub>f: 'a) (sub\<^sub>f\<^sub>f: "'a tree\<^sub>f\<^sub>f list") |
80 |
datatype_new 'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i (lbl\<^sub>f\<^sub>i: 'a) (sub\<^sub>f\<^sub>i: "'a tree\<^sub>f\<^sub>i llist") |
|
81 |
codatatype 'a tree\<^sub>i\<^sub>f = Node\<^sub>i\<^sub>f (lbl\<^sub>i\<^sub>f: 'a) (sub\<^sub>i\<^sub>f: "'a tree\<^sub>i\<^sub>f list") |
|
82 |
codatatype 'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i (lbl\<^sub>i\<^sub>i: 'a) (sub\<^sub>i\<^sub>i: "'a tree\<^sub>i\<^sub>i llist") |
|
53025 | 83 |
(*<*) |
84 |
end |
|
85 |
(*>*) |
|
52792 | 86 |
|
52794 | 87 |
text {* |
52840 | 88 |
The first two tree types allow only finite branches, whereas the last two allow |
53028 | 89 |
branches of infinite length. Orthogonally, the nodes in the first and third |
90 |
types have finite branching, whereas those of the second and fourth may have |
|
91 |
infinitely many direct subtrees. |
|
52840 | 92 |
|
52794 | 93 |
To use the package, it is necessary to import the @{theory BNF} theory, which |
53552 | 94 |
can be precompiled into the \texttt{HOL-BNF} image. The following commands show |
52806 | 95 |
how to launch jEdit/PIDE with the image loaded and how to build the image |
96 |
without launching jEdit: |
|
52794 | 97 |
*} |
98 |
||
99 |
text {* |
|
100 |
\noindent |
|
52806 | 101 |
\ \ \ \ \texttt{isabelle jedit -l HOL-BNF} \\ |
52827 | 102 |
\noindent |
52828 | 103 |
\hbox{}\ \ \ \ \texttt{isabelle build -b HOL-BNF} |
52794 | 104 |
*} |
105 |
||
106 |
text {* |
|
52805 | 107 |
The package, like its predecessor, fully adheres to the LCF philosophy |
108 |
\cite{mgordon79}: The characteristic theorems associated with the specified |
|
109 |
(co)datatypes are derived rather than introduced axiomatically.% |
|
53543 | 110 |
\footnote{If the @{text quick_and_dirty} option is enabled, some of the |
52841 | 111 |
internal constructions and most of the internal proof obligations are skipped.} |
52805 | 112 |
The package's metatheory is described in a pair of papers |
53552 | 113 |
\cite{traytel-et-al-2012,blanchette-et-al-wit}. The central notion is that of a |
114 |
\emph{bounded natural functor} (BNF)---a well-behaved type constructor for which |
|
115 |
nested (co)recursion is supported. |
|
52792 | 116 |
|
52794 | 117 |
This tutorial is organized as follows: |
118 |
||
52805 | 119 |
\begin{itemize} |
52822 | 120 |
\setlength{\itemsep}{0pt} |
121 |
||
52805 | 122 |
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,'' |
52822 | 123 |
describes how to specify datatypes using the @{command datatype_new} command. |
52805 | 124 |
|
53018 | 125 |
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive |
52805 | 126 |
Functions,'' describes how to specify recursive functions using |
53535 | 127 |
@{command primrec_new}, \keyw{fun}, and \keyw{function}. |
52805 | 128 |
|
129 |
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,'' |
|
52822 | 130 |
describes how to specify codatatypes using the @{command codatatype} command. |
52805 | 131 |
|
53018 | 132 |
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive |
52805 | 133 |
Functions,'' describes how to specify corecursive functions using the |
53535 | 134 |
@{command primcorec} command. |
52794 | 135 |
|
53018 | 136 |
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering |
53552 | 137 |
Bounded Natural Functors,'' explains how to use the @{command bnf} command |
138 |
to register arbitrary type constructors as BNFs. |
|
52805 | 139 |
|
53552 | 140 |
\item Section |
53617 | 141 |
\ref{sec:deriving-destructors-and-theorems-for-free-constructors}, |
142 |
``Deriving Destructors and Theorems for Free Constructors,'' explains how to |
|
53552 | 143 |
use the command @{command wrap_free_constructors} to derive destructor constants |
144 |
and theorems for freely generated types, as performed internally by @{command |
|
145 |
datatype_new} and @{command codatatype}. |
|
52794 | 146 |
|
53617 | 147 |
%\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,'' |
148 |
%describes the package's programmatic interface. |
|
52794 | 149 |
|
53617 | 150 |
%\item Section \ref{sec:interoperability}, ``Interoperability,'' |
151 |
%is concerned with the packages' interaction with other Isabelle packages and |
|
152 |
%tools, such as the code generator and the counterexample generators. |
|
52805 | 153 |
|
53617 | 154 |
%\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and |
155 |
%Limitations,'' concludes with known open issues at the time of writing. |
|
52805 | 156 |
\end{itemize} |
52822 | 157 |
|
158 |
||
159 |
\newbox\boxA |
|
160 |
\setbox\boxA=\hbox{\texttt{nospam}} |
|
161 |
||
162 |
\newcommand\authoremaili{\texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@\allowbreak |
|
163 |
in.\allowbreak tum.\allowbreak de}} |
|
53028 | 164 |
\newcommand\authoremailii{\texttt{lore{\color{white}nospam}\kern-\wd\boxA{}nz.panny@\allowbreak |
165 |
\allowbreak tum.\allowbreak de}} |
|
166 |
\newcommand\authoremailiii{\texttt{pope{\color{white}nospam}\kern-\wd\boxA{}scua@\allowbreak |
|
52822 | 167 |
in.\allowbreak tum.\allowbreak de}} |
53028 | 168 |
\newcommand\authoremailiv{\texttt{tray{\color{white}nospam}\kern-\wd\boxA{}tel@\allowbreak |
52822 | 169 |
in.\allowbreak tum.\allowbreak de}} |
170 |
||
53028 | 171 |
The commands @{command datatype_new} and @{command primrec_new} are expected to |
53535 | 172 |
displace \keyw{datatype} and \keyw{primrec} in a future release. Authors of new |
173 |
theories are encouraged to use the new commands, and maintainers of older |
|
53028 | 174 |
theories may want to consider upgrading. |
52841 | 175 |
|
176 |
Comments and bug reports concerning either the tool or this tutorial should be |
|
53028 | 177 |
directed to the authors at \authoremaili, \authoremailii, \authoremailiii, |
178 |
and \authoremailiv. |
|
52822 | 179 |
|
180 |
\begin{framed} |
|
181 |
\noindent |
|
53646 | 182 |
\textbf{Warning:}\enskip This tutorial and the package it describes are under |
183 |
construction. Please apologise for their appearance. Should you have suggestions |
|
184 |
or comments regarding either, please let the authors know. |
|
52822 | 185 |
\end{framed} |
52794 | 186 |
*} |
187 |
||
53491 | 188 |
|
52827 | 189 |
section {* Defining Datatypes |
52805 | 190 |
\label{sec:defining-datatypes} *} |
191 |
||
192 |
text {* |
|
53646 | 193 |
Datatypes can be specified using the @{command datatype_new} command. |
52805 | 194 |
*} |
52792 | 195 |
|
52824 | 196 |
|
53617 | 197 |
subsection {* Introductory Examples |
198 |
\label{ssec:datatype-introductory-examples} *} |
|
52794 | 199 |
|
53646 | 200 |
text {* |
201 |
Datatypes are illustrated through concrete examples featuring different flavors |
|
202 |
of recursion. More examples can be found in the directory |
|
203 |
\verb|~~/src/HOL/BNF/Examples|. |
|
204 |
*} |
|
53621 | 205 |
|
206 |
subsubsection {* Nonrecursive Types |
|
207 |
\label{sssec:datatype-nonrecursive-types} *} |
|
52794 | 208 |
|
52805 | 209 |
text {* |
53028 | 210 |
Datatypes are introduced by specifying the desired names and argument types for |
53491 | 211 |
their constructors. \emph{Enumeration} types are the simplest form of datatype. |
53028 | 212 |
All their constructors are nullary: |
52805 | 213 |
*} |
214 |
||
52828 | 215 |
datatype_new trool = Truue | Faalse | Perhaaps |
52805 | 216 |
|
217 |
text {* |
|
53028 | 218 |
\noindent |
53491 | 219 |
Here, @{const Truue}, @{const Faalse}, and @{const Perhaaps} have the type @{typ trool}. |
53028 | 220 |
|
221 |
Polymorphic types are possible, such as the following option type, modeled after |
|
222 |
its homologue from the @{theory Option} theory: |
|
52805 | 223 |
*} |
224 |
||
53025 | 225 |
(*<*) |
226 |
hide_const None Some |
|
227 |
(*>*) |
|
228 |
datatype_new 'a option = None | Some 'a |
|
52805 | 229 |
|
230 |
text {* |
|
53028 | 231 |
\noindent |
53491 | 232 |
The constructors are @{text "None :: 'a option"} and |
233 |
@{text "Some :: 'a \<Rightarrow> 'a option"}. |
|
53028 | 234 |
|
235 |
The next example has three type parameters: |
|
52805 | 236 |
*} |
237 |
||
238 |
datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c |
|
239 |
||
53028 | 240 |
text {* |
241 |
\noindent |
|
242 |
The constructor is |
|
53491 | 243 |
@{text "Triple :: 'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> ('a, 'b, 'c) triple"}. |
53028 | 244 |
Unlike in Standard ML, curried constructors are supported. The uncurried variant |
245 |
is also possible: |
|
246 |
*} |
|
247 |
||
248 |
datatype_new ('a, 'b, 'c) triple\<^sub>u = Triple\<^sub>u "'a * 'b * 'c" |
|
249 |
||
52824 | 250 |
|
53621 | 251 |
subsubsection {* Simple Recursion |
252 |
\label{sssec:datatype-simple-recursion} *} |
|
52794 | 253 |
|
52805 | 254 |
text {* |
53491 | 255 |
Natural numbers are the simplest example of a recursive type: |
52805 | 256 |
*} |
257 |
||
53332 | 258 |
datatype_new nat = Zero | Suc nat |
52805 | 259 |
|
260 |
text {* |
|
53491 | 261 |
\noindent |
262 |
Lists were shown in the introduction. Terminated lists are a variant: |
|
52805 | 263 |
*} |
264 |
||
53491 | 265 |
(*<*) |
266 |
locale dummy_tlist |
|
267 |
begin |
|
268 |
(*>*) |
|
52805 | 269 |
datatype_new ('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist" |
53491 | 270 |
(*<*) |
271 |
end |
|
272 |
(*>*) |
|
52805 | 273 |
|
274 |
text {* |
|
53491 | 275 |
\noindent |
53552 | 276 |
Occurrences of nonatomic types on the right-hand side of the equal sign must be |
277 |
enclosed in double quotes, as is customary in Isabelle. |
|
52805 | 278 |
*} |
279 |
||
52824 | 280 |
|
53621 | 281 |
subsubsection {* Mutual Recursion |
282 |
\label{sssec:datatype-mutual-recursion} *} |
|
52794 | 283 |
|
52805 | 284 |
text {* |
53552 | 285 |
\emph{Mutually recursive} types are introduced simultaneously and may refer to |
286 |
each other. The example below introduces a pair of types for even and odd |
|
287 |
natural numbers: |
|
52805 | 288 |
*} |
289 |
||
53623 | 290 |
datatype_new even_nat = Even_Zero | Even_Suc odd_nat |
291 |
and odd_nat = Odd_Suc even_nat |
|
52805 | 292 |
|
293 |
text {* |
|
53491 | 294 |
\noindent |
295 |
Arithmetic expressions are defined via terms, terms via factors, and factors via |
|
296 |
expressions: |
|
52805 | 297 |
*} |
298 |
||
52841 | 299 |
datatype_new ('a, 'b) exp = |
300 |
Term "('a, 'b) trm" | Sum "('a, 'b) trm" "('a, 'b) exp" |
|
52805 | 301 |
and ('a, 'b) trm = |
52841 | 302 |
Factor "('a, 'b) fct" | Prod "('a, 'b) fct" "('a, 'b) trm" |
303 |
and ('a, 'b) fct = |
|
304 |
Const 'a | Var 'b | Expr "('a, 'b) exp" |
|
52805 | 305 |
|
52824 | 306 |
|
53621 | 307 |
subsubsection {* Nested Recursion |
308 |
\label{sssec:datatype-nested-recursion} *} |
|
52794 | 309 |
|
52805 | 310 |
text {* |
53491 | 311 |
\emph{Nested recursion} occurs when recursive occurrences of a type appear under |
312 |
a type constructor. The introduction showed some examples of trees with nesting |
|
313 |
through lists. A more complex example, that reuses our @{text option} type, |
|
314 |
follows: |
|
52805 | 315 |
*} |
316 |
||
52843 | 317 |
datatype_new 'a btree = |
53025 | 318 |
BNode 'a "'a btree option" "'a btree option" |
52805 | 319 |
|
320 |
text {* |
|
53491 | 321 |
\noindent |
322 |
Not all nestings are admissible. For example, this command will fail: |
|
52805 | 323 |
*} |
324 |
||
53491 | 325 |
datatype_new 'a wrong = Wrong (*<*)'a |
53542 | 326 |
typ (*>*)"'a wrong \<Rightarrow> 'a" |
52806 | 327 |
|
328 |
text {* |
|
53491 | 329 |
\noindent |
330 |
The issue is that the function arrow @{text "\<Rightarrow>"} allows recursion |
|
331 |
only through its right-hand side. This issue is inherited by polymorphic |
|
332 |
datatypes defined in terms of~@{text "\<Rightarrow>"}: |
|
333 |
*} |
|
334 |
||
335 |
datatype_new ('a, 'b) fn = Fn "'a \<Rightarrow> 'b" |
|
336 |
datatype_new 'a also_wrong = Also_Wrong (*<*)'a |
|
53542 | 337 |
typ (*>*)"('a also_wrong, 'a) fn" |
53491 | 338 |
|
339 |
text {* |
|
340 |
\noindent |
|
53621 | 341 |
This is legal: |
342 |
*} |
|
343 |
||
344 |
datatype_new 'a ftree = FTLeaf 'a | FTNode "'a \<Rightarrow> 'a ftree" |
|
345 |
||
346 |
text {* |
|
347 |
\noindent |
|
53491 | 348 |
In general, type constructors @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
349 |
allow recursion on a subset of their type arguments @{text 'a\<^sub>1}, \ldots, |
|
350 |
@{text 'a\<^sub>m}. These type arguments are called \emph{live}; the remaining |
|
351 |
type arguments are called \emph{dead}. In @{typ "'a \<Rightarrow> 'b"} and |
|
352 |
@{typ "('a, 'b) fn"}, the type variable @{typ 'a} is dead and @{typ 'b} is live. |
|
53552 | 353 |
|
354 |
Type constructors must be registered as bounded natural functors (BNFs) to have |
|
355 |
live arguments. This is done automatically for datatypes and codatatypes |
|
356 |
introduced by the @{command datatype_new} and @{command codatatype} commands. |
|
357 |
Section~\ref{sec:registering-bounded-natural-functors} explains how to register |
|
358 |
arbitrary type constructors as BNFs. |
|
52806 | 359 |
*} |
360 |
||
361 |
||
53621 | 362 |
subsubsection {* Custom Names and Syntaxes |
363 |
\label{sssec:datatype-custom-names-and-syntaxes} *} |
|
52805 | 364 |
|
365 |
text {* |
|
53491 | 366 |
The @{command datatype_new} command introduces various constants in addition to |
53617 | 367 |
the constructors. With each datatype are associated set functions, a map |
368 |
function, a relator, discriminators, and selectors, all of which can be given |
|
369 |
custom names. In the example below, the traditional names |
|
370 |
@{text set}, @{text map}, @{text list_all2}, @{text null}, @{text hd}, and |
|
371 |
@{text tl} override the default names @{text list_set}, @{text list_map}, @{text |
|
372 |
list_rel}, @{text is_Nil}, @{text un_Cons1}, and @{text un_Cons2}: |
|
52822 | 373 |
*} |
374 |
||
52841 | 375 |
(*<*) |
376 |
no_translations |
|
377 |
"[x, xs]" == "x # [xs]" |
|
378 |
"[x]" == "x # []" |
|
379 |
||
380 |
no_notation |
|
381 |
Nil ("[]") and |
|
382 |
Cons (infixr "#" 65) |
|
383 |
||
53543 | 384 |
hide_type list |
53544 | 385 |
hide_const Nil Cons hd tl set map list_all2 list_case list_rec |
53025 | 386 |
|
387 |
locale dummy_list |
|
388 |
begin |
|
52841 | 389 |
(*>*) |
53025 | 390 |
datatype_new (set: 'a) list (map: map rel: list_all2) = |
52822 | 391 |
null: Nil (defaults tl: Nil) |
53025 | 392 |
| Cons (hd: 'a) (tl: "'a list") |
52822 | 393 |
|
394 |
text {* |
|
395 |
\noindent |
|
396 |
The command introduces a discriminator @{const null} and a pair of selectors |
|
397 |
@{const hd} and @{const tl} characterized as follows: |
|
398 |
% |
|
53025 | 399 |
\[@{thm list.collapse(1)[of xs, no_vars]} |
400 |
\qquad @{thm list.collapse(2)[of xs, no_vars]}\] |
|
52822 | 401 |
% |
402 |
For two-constructor datatypes, a single discriminator constant suffices. The |
|
53491 | 403 |
discriminator associated with @{const Cons} is simply |
404 |
@{term "\<lambda>xs. \<not> null xs"}. |
|
52822 | 405 |
|
53553 | 406 |
The @{text defaults} clause following the @{const Nil} constructor specifies a |
407 |
default value for selectors associated with other constructors. Here, it is used |
|
408 |
to ensure that the tail of the empty list is itself (instead of being left |
|
53535 | 409 |
unspecified). |
52822 | 410 |
|
53617 | 411 |
Because @{const Nil} is nullary, it is also possible to use |
53491 | 412 |
@{term "\<lambda>xs. xs = Nil"} as a discriminator. This is specified by |
53534 | 413 |
entering ``@{text "="}'' instead of the identifier @{const null}. Although this |
53535 | 414 |
may look appealing, the mixture of constructors and selectors in the |
53534 | 415 |
characteristic theorems can lead Isabelle's automation to switch between the |
416 |
constructor and the destructor view in surprising ways. |
|
52822 | 417 |
|
53491 | 418 |
The usual mixfix syntaxes are available for both types and constructors. For |
419 |
example: |
|
52805 | 420 |
*} |
52794 | 421 |
|
53025 | 422 |
(*<*) |
423 |
end |
|
424 |
(*>*) |
|
53552 | 425 |
datatype_new ('a, 'b) prod (infixr "*" 20) = Pair 'a 'b |
426 |
||
427 |
text {* \blankline *} |
|
52822 | 428 |
|
52841 | 429 |
datatype_new (set: 'a) list (map: map rel: list_all2) = |
52822 | 430 |
null: Nil ("[]") |
52841 | 431 |
| Cons (hd: 'a) (tl: "'a list") (infixr "#" 65) |
432 |
||
433 |
text {* |
|
53535 | 434 |
\noindent |
53025 | 435 |
Incidentally, this is how the traditional syntaxes can be set up: |
52841 | 436 |
*} |
437 |
||
438 |
syntax "_list" :: "args \<Rightarrow> 'a list" ("[(_)]") |
|
439 |
||
53552 | 440 |
text {* \blankline *} |
441 |
||
52841 | 442 |
translations |
443 |
"[x, xs]" == "x # [xs]" |
|
444 |
"[x]" == "x # []" |
|
52822 | 445 |
|
52824 | 446 |
|
53617 | 447 |
subsection {* Command Syntax |
448 |
\label{ssec:datatype-command-syntax} *} |
|
449 |
||
450 |
||
53621 | 451 |
subsubsection {* \keyw{datatype\_new} |
452 |
\label{sssec:datatype-new} *} |
|
52794 | 453 |
|
52822 | 454 |
text {* |
455 |
Datatype definitions have the following general syntax: |
|
456 |
||
52824 | 457 |
@{rail " |
53534 | 458 |
@@{command_def datatype_new} target? @{syntax dt_options}? \\ |
52824 | 459 |
(@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and') |
52828 | 460 |
; |
53623 | 461 |
@{syntax_def dt_options}: '(' (('no_discs_sels' | 'rep_compat') + ',') ')' |
52824 | 462 |
"} |
463 |
||
53534 | 464 |
The syntactic quantity \synt{target} can be used to specify a local |
465 |
context---e.g., @{text "(in linorder)"}. It is documented in the Isar reference |
|
466 |
manual \cite{isabelle-isar-ref}. |
|
467 |
% |
|
468 |
The optional target is optionally followed by datatype-specific options: |
|
52822 | 469 |
|
52824 | 470 |
\begin{itemize} |
471 |
\setlength{\itemsep}{0pt} |
|
472 |
||
473 |
\item |
|
53623 | 474 |
The @{text "no_discs_sels"} option indicates that no discriminators or selectors |
53543 | 475 |
should be generated. |
52822 | 476 |
|
52824 | 477 |
\item |
53644 | 478 |
The @{text "rep_compat"} option indicates that the generated names should |
479 |
contain optional (and normally not displayed) ``@{text "new."}'' components to |
|
480 |
prevent clashes with a later call to \keyw{rep\_datatype}. See |
|
52824 | 481 |
Section~\ref{ssec:datatype-compatibility-issues} for details. |
482 |
\end{itemize} |
|
52822 | 483 |
|
52827 | 484 |
The left-hand sides of the datatype equations specify the name of the type to |
53534 | 485 |
define, its type parameters, and additional information: |
52822 | 486 |
|
52824 | 487 |
@{rail " |
53534 | 488 |
@{syntax_def dt_name}: @{syntax tyargs}? name @{syntax map_rel}? mixfix? |
52824 | 489 |
; |
53534 | 490 |
@{syntax_def tyargs}: typefree | '(' ((name ':')? typefree + ',') ')' |
52824 | 491 |
; |
53534 | 492 |
@{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')' |
52824 | 493 |
"} |
52822 | 494 |
|
52827 | 495 |
\noindent |
53534 | 496 |
The syntactic quantity \synt{name} denotes an identifier, \synt{typefree} |
497 |
denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and \synt{mixfix} |
|
498 |
denotes the usual parenthesized mixfix notation. They are documented in the Isar |
|
499 |
reference manual \cite{isabelle-isar-ref}. |
|
52822 | 500 |
|
52827 | 501 |
The optional names preceding the type variables allow to override the default |
502 |
names of the set functions (@{text t_set1}, \ldots, @{text t_setM}). |
|
503 |
Inside a mutually recursive datatype specification, all defined datatypes must |
|
504 |
specify exactly the same type variables in the same order. |
|
52822 | 505 |
|
52824 | 506 |
@{rail " |
53534 | 507 |
@{syntax_def ctor}: (name ':')? name (@{syntax ctor_arg} * ) \\ |
508 |
@{syntax dt_sel_defaults}? mixfix? |
|
52824 | 509 |
"} |
510 |
||
53535 | 511 |
\medskip |
512 |
||
52827 | 513 |
\noindent |
514 |
The main constituents of a constructor specification is the name of the |
|
515 |
constructor and the list of its argument types. An optional discriminator name |
|
53554 | 516 |
can be supplied at the front to override the default name |
517 |
(@{text t.is_C\<^sub>j}). |
|
52822 | 518 |
|
52824 | 519 |
@{rail " |
53534 | 520 |
@{syntax_def ctor_arg}: type | '(' name ':' type ')' |
52827 | 521 |
"} |
522 |
||
53535 | 523 |
\medskip |
524 |
||
52827 | 525 |
\noindent |
526 |
In addition to the type of a constructor argument, it is possible to specify a |
|
527 |
name for the corresponding selector to override the default name |
|
53554 | 528 |
(@{text un_C\<^sub>ji}). The same selector names can be reused for several |
529 |
constructors as long as they share the same type. |
|
52827 | 530 |
|
531 |
@{rail " |
|
53621 | 532 |
@{syntax_def dt_sel_defaults}: '(' 'defaults' (name ':' term +) ')' |
52824 | 533 |
"} |
52827 | 534 |
|
535 |
\noindent |
|
536 |
Given a constructor |
|
537 |
@{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"}, |
|
538 |
default values can be specified for any selector |
|
539 |
@{text "un_D \<Colon> \<sigma> \<Rightarrow> \<tau>"} |
|
53535 | 540 |
associated with other constructors. The specified default value must be of type |
52828 | 541 |
@{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"} |
53535 | 542 |
(i.e., it may depends on @{text C}'s arguments). |
52822 | 543 |
*} |
544 |
||
53617 | 545 |
|
53621 | 546 |
subsubsection {* \keyw{datatype\_new\_compat} |
547 |
\label{sssec:datatype-new-compat} *} |
|
53617 | 548 |
|
549 |
text {* |
|
53621 | 550 |
The old datatype package provides some functionality that is not yet replicated |
551 |
in the new package: |
|
552 |
||
553 |
\begin{itemize} |
|
554 |
\item It is integrated with \keyw{fun} and \keyw{function} |
|
555 |
\cite{isabelle-function}, Nitpick \cite{isabelle-nitpick}, Quickcheck, |
|
556 |
and other packages. |
|
557 |
||
558 |
\item It is extended by various add-ons, notably to produce instances of the |
|
559 |
@{const size} function. |
|
560 |
\end{itemize} |
|
561 |
||
562 |
\noindent |
|
563 |
New-style datatypes can in most case be registered as old-style datatypes using |
|
564 |
the command |
|
565 |
||
53617 | 566 |
@{rail " |
53621 | 567 |
@@{command_def datatype_new_compat} names |
53617 | 568 |
"} |
53621 | 569 |
|
570 |
\noindent |
|
571 |
where the \textit{names} argument is simply a space-separated list of type names |
|
572 |
that are mutually recursive. For example: |
|
573 |
*} |
|
574 |
||
53623 | 575 |
datatype_new_compat even_nat odd_nat |
53621 | 576 |
|
577 |
text {* \blankline *} |
|
578 |
||
53623 | 579 |
thm even_nat_odd_nat.size |
53621 | 580 |
|
581 |
text {* \blankline *} |
|
582 |
||
53623 | 583 |
ML {* Datatype_Data.get_info @{theory} @{type_name even_nat} *} |
53621 | 584 |
|
585 |
text {* |
|
586 |
For nested recursive datatypes, all types through which recursion takes place |
|
587 |
must be new-style datatypes. In principle, it should be possible to support |
|
588 |
old-style datatypes as well, but the command does not support this yet. |
|
53617 | 589 |
*} |
590 |
||
591 |
||
592 |
subsection {* Generated Constants |
|
593 |
\label{ssec:datatype-generated-constants} *} |
|
594 |
||
595 |
text {* |
|
53623 | 596 |
Given a datatype @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
53617 | 597 |
with $m > 0$ live type variables and $n$ constructors |
598 |
@{text "t.C\<^sub>1"}, \ldots, @{text "t.C\<^sub>n"}, the |
|
599 |
following auxiliary constants are introduced: |
|
600 |
||
601 |
\begin{itemize} |
|
602 |
\setlength{\itemsep}{0pt} |
|
603 |
||
604 |
\item \relax{Case combinator}: @{text t_case} (rendered using the familiar |
|
605 |
@{text case}--@{text of} syntax) |
|
606 |
||
607 |
\item \relax{Discriminators}: @{text "t.is_C\<^sub>1"}, \ldots, |
|
608 |
@{text "t.is_C\<^sub>n"} |
|
609 |
||
610 |
\item \relax{Selectors}: |
|
611 |
@{text t.un_C\<^sub>11}$, \ldots, @{text t.un_C\<^sub>1k\<^sub>1}, \\ |
|
612 |
\phantom{\relax{Selectors:}} \quad\vdots \\ |
|
613 |
\phantom{\relax{Selectors:}} @{text t.un_C\<^sub>n1}$, \ldots, @{text t.un_C\<^sub>nk\<^sub>n}. |
|
614 |
||
615 |
\item \relax{Set functions} (or \relax{natural transformations}): |
|
616 |
@{text t_set1}, \ldots, @{text t_setm} |
|
617 |
||
618 |
\item \relax{Map function} (or \relax{functorial action}): @{text t_map} |
|
619 |
||
620 |
\item \relax{Relator}: @{text t_rel} |
|
621 |
||
622 |
\item \relax{Iterator}: @{text t_fold} |
|
623 |
||
624 |
\item \relax{Recursor}: @{text t_rec} |
|
625 |
||
626 |
\end{itemize} |
|
627 |
||
628 |
\noindent |
|
629 |
The case combinator, discriminators, and selectors are collectively called |
|
630 |
\emph{destructors}. The prefix ``@{text "t."}'' is an optional component of the |
|
631 |
name and is normally hidden. |
|
632 |
*} |
|
633 |
||
634 |
||
52840 | 635 |
subsection {* Generated Theorems |
636 |
\label{ssec:datatype-generated-theorems} *} |
|
52828 | 637 |
|
638 |
text {* |
|
53544 | 639 |
The characteristic theorems generated by @{command datatype_new} are grouped in |
53623 | 640 |
three broad categories: |
53535 | 641 |
|
53543 | 642 |
\begin{itemize} |
643 |
\item The \emph{free constructor theorems} are properties about the constructors |
|
644 |
and destructors that can be derived for any freely generated type. Internally, |
|
53542 | 645 |
the derivation is performed by @{command wrap_free_constructors}. |
53535 | 646 |
|
53552 | 647 |
\item The \emph{functorial theorems} are properties of datatypes related to |
648 |
their BNF nature. |
|
649 |
||
650 |
\item The \emph{inductive theorems} are properties of datatypes related to |
|
53544 | 651 |
their inductive nature. |
53552 | 652 |
|
53543 | 653 |
\end{itemize} |
53535 | 654 |
|
655 |
\noindent |
|
53542 | 656 |
The full list of named theorems can be obtained as usual by entering the |
53543 | 657 |
command \keyw{print\_theorems} immediately after the datatype definition. |
53542 | 658 |
This list normally excludes low-level theorems that reveal internal |
53552 | 659 |
constructions. To make these accessible, add the line |
53542 | 660 |
*} |
53535 | 661 |
|
53542 | 662 |
declare [[bnf_note_all]] |
663 |
(*<*) |
|
664 |
declare [[bnf_note_all = false]] |
|
665 |
(*>*) |
|
53535 | 666 |
|
53552 | 667 |
text {* |
668 |
\noindent |
|
669 |
to the top of the theory file. |
|
670 |
*} |
|
53535 | 671 |
|
53621 | 672 |
subsubsection {* Free Constructor Theorems |
673 |
\label{sssec:free-constructor-theorems} *} |
|
53535 | 674 |
|
53543 | 675 |
(*<*) |
676 |
consts is_Cons :: 'a |
|
677 |
(*>*) |
|
678 |
||
53535 | 679 |
text {* |
53543 | 680 |
The first subgroup of properties are concerned with the constructors. |
681 |
They are listed below for @{typ "'a list"}: |
|
682 |
||
53552 | 683 |
\begin{indentblock} |
53543 | 684 |
\begin{description} |
53544 | 685 |
|
53642 | 686 |
\item[@{text "t."}\hthm{inject} @{text "[iff, induct_simp]"}\rm:] ~ \\ |
53544 | 687 |
@{thm list.inject[no_vars]} |
688 |
||
53642 | 689 |
\item[@{text "t."}\hthm{distinct} @{text "[simp, induct_simp]"}\rm:] ~ \\ |
53543 | 690 |
@{thm list.distinct(1)[no_vars]} \\ |
691 |
@{thm list.distinct(2)[no_vars]} |
|
692 |
||
53642 | 693 |
\item[@{text "t."}\hthm{exhaust} @{text "[cases t, case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53543 | 694 |
@{thm list.exhaust[no_vars]} |
695 |
||
53642 | 696 |
\item[@{text "t."}\hthm{nchotomy}\rm:] ~ \\ |
53543 | 697 |
@{thm list.nchotomy[no_vars]} |
698 |
||
699 |
\end{description} |
|
53552 | 700 |
\end{indentblock} |
53543 | 701 |
|
702 |
\noindent |
|
53621 | 703 |
In addition, these nameless theorems are registered as safe elimination rules: |
704 |
||
705 |
\begin{indentblock} |
|
706 |
\begin{description} |
|
707 |
||
53642 | 708 |
\item[@{text "t."}\hthm{list.distinct {\upshape[}THEN notE}@{text ", elim!"}\hthm{\upshape]}\rm:] ~ \\ |
53621 | 709 |
@{thm list.distinct(1)[THEN notE, elim!, no_vars]} \\ |
710 |
@{thm list.distinct(2)[THEN notE, elim!, no_vars]} |
|
711 |
||
712 |
\end{description} |
|
713 |
\end{indentblock} |
|
714 |
||
715 |
\noindent |
|
53543 | 716 |
The next subgroup is concerned with the case combinator: |
717 |
||
53552 | 718 |
\begin{indentblock} |
53543 | 719 |
\begin{description} |
53544 | 720 |
|
53642 | 721 |
\item[@{text "t."}\hthm{case} @{text "[simp]"}\rm:] ~ \\ |
53543 | 722 |
@{thm list.case(1)[no_vars]} \\ |
723 |
@{thm list.case(2)[no_vars]} |
|
724 |
||
53642 | 725 |
\item[@{text "t."}\hthm{case\_cong}\rm:] ~ \\ |
53543 | 726 |
@{thm list.case_cong[no_vars]} |
727 |
||
53642 | 728 |
\item[@{text "t."}\hthm{weak\_case\_cong} @{text "[cong]"}\rm:] ~ \\ |
53543 | 729 |
@{thm list.weak_case_cong[no_vars]} |
730 |
||
53642 | 731 |
\item[@{text "t."}\hthm{split}\rm:] ~ \\ |
53543 | 732 |
@{thm list.split[no_vars]} |
733 |
||
53642 | 734 |
\item[@{text "t."}\hthm{split\_asm}\rm:] ~ \\ |
53543 | 735 |
@{thm list.split_asm[no_vars]} |
736 |
||
53544 | 737 |
\item[@{text "t."}\hthm{splits} = @{text "split split_asm"}] |
53543 | 738 |
|
739 |
\end{description} |
|
53552 | 740 |
\end{indentblock} |
53543 | 741 |
|
742 |
\noindent |
|
743 |
The third and last subgroup revolves around discriminators and selectors: |
|
744 |
||
53552 | 745 |
\begin{indentblock} |
53543 | 746 |
\begin{description} |
53544 | 747 |
|
53642 | 748 |
\item[@{text "t."}\hthm{discs} @{text "[simp]"}\rm:] ~ \\ |
53543 | 749 |
@{thm list.discs(1)[no_vars]} \\ |
750 |
@{thm list.discs(2)[no_vars]} |
|
751 |
||
53642 | 752 |
\item[@{text "t."}\hthm{sels} @{text "[simp]"}\rm:] ~ \\ |
53543 | 753 |
@{thm list.sels(1)[no_vars]} \\ |
754 |
@{thm list.sels(2)[no_vars]} |
|
755 |
||
53642 | 756 |
\item[@{text "t."}\hthm{collapse} @{text "[simp]"}\rm:] ~ \\ |
53543 | 757 |
@{thm list.collapse(1)[no_vars]} \\ |
758 |
@{thm list.collapse(2)[no_vars]} |
|
759 |
||
53642 | 760 |
\item[@{text "t."}\hthm{disc\_exclude}\rm:] ~ \\ |
53543 | 761 |
These properties are missing for @{typ "'a list"} because there is only one |
762 |
proper discriminator. Had the datatype been introduced with a second |
|
53544 | 763 |
discriminator called @{const is_Cons}, they would have read thusly: \\[\jot] |
53543 | 764 |
@{prop "null list \<Longrightarrow> \<not> is_Cons list"} \\ |
765 |
@{prop "is_Cons list \<Longrightarrow> \<not> null list"} |
|
766 |
||
53642 | 767 |
\item[@{text "t."}\hthm{disc\_exhaust} @{text "[case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53543 | 768 |
@{thm list.disc_exhaust[no_vars]} |
769 |
||
53642 | 770 |
\item[@{text "t."}\hthm{expand}\rm:] ~ \\ |
53543 | 771 |
@{thm list.expand[no_vars]} |
772 |
||
53642 | 773 |
\item[@{text "t."}\hthm{case\_conv}\rm:] ~ \\ |
53543 | 774 |
@{thm list.case_conv[no_vars]} |
775 |
||
776 |
\end{description} |
|
53552 | 777 |
\end{indentblock} |
778 |
*} |
|
779 |
||
780 |
||
53621 | 781 |
subsubsection {* Functorial Theorems |
782 |
\label{sssec:functorial-theorems} *} |
|
53552 | 783 |
|
784 |
text {* |
|
53623 | 785 |
The BNF-related theorem are as follows: |
53552 | 786 |
|
787 |
\begin{indentblock} |
|
788 |
\begin{description} |
|
789 |
||
53642 | 790 |
\item[@{text "t."}\hthm{sets} @{text "[code]"}\rm:] ~ \\ |
53552 | 791 |
@{thm list.sets(1)[no_vars]} \\ |
792 |
@{thm list.sets(2)[no_vars]} |
|
793 |
||
53642 | 794 |
\item[@{text "t."}\hthm{map} @{text "[code]"}\rm:] ~ \\ |
53552 | 795 |
@{thm list.map(1)[no_vars]} \\ |
796 |
@{thm list.map(2)[no_vars]} |
|
797 |
||
53642 | 798 |
\item[@{text "t."}\hthm{rel\_inject} @{text "[code]"}\rm:] ~ \\ |
53552 | 799 |
@{thm list.rel_inject(1)[no_vars]} \\ |
800 |
@{thm list.rel_inject(2)[no_vars]} |
|
801 |
||
53642 | 802 |
\item[@{text "t."}\hthm{rel\_distinct} @{text "[code]"}\rm:] ~ \\ |
53552 | 803 |
@{thm list.rel_distinct(1)[no_vars]} \\ |
804 |
@{thm list.rel_distinct(2)[no_vars]} |
|
805 |
||
806 |
\end{description} |
|
807 |
\end{indentblock} |
|
53535 | 808 |
*} |
809 |
||
810 |
||
53621 | 811 |
subsubsection {* Inductive Theorems |
812 |
\label{sssec:inductive-theorems} *} |
|
53535 | 813 |
|
814 |
text {* |
|
53623 | 815 |
The inductive theorems are as follows: |
53544 | 816 |
|
53552 | 817 |
\begin{indentblock} |
53544 | 818 |
\begin{description} |
819 |
||
53642 | 820 |
\item[@{text "t."}\hthm{induct} @{text "[induct t, case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53544 | 821 |
@{thm list.induct[no_vars]} |
822 |
||
53642 | 823 |
\item[@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{induct} @{text "[case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53544 | 824 |
Given $m > 1$ mutually recursive datatypes, this induction rule can be used to |
825 |
prove $m$ properties simultaneously. |
|
52828 | 826 |
|
53642 | 827 |
\item[@{text "t."}\hthm{fold} @{text "[code]"}\rm:] ~ \\ |
53544 | 828 |
@{thm list.fold(1)[no_vars]} \\ |
829 |
@{thm list.fold(2)[no_vars]} |
|
830 |
||
53642 | 831 |
\item[@{text "t."}\hthm{rec} @{text "[code]"}\rm:] ~ \\ |
53544 | 832 |
@{thm list.rec(1)[no_vars]} \\ |
833 |
@{thm list.rec(2)[no_vars]} |
|
834 |
||
835 |
\end{description} |
|
53552 | 836 |
\end{indentblock} |
53544 | 837 |
|
838 |
\noindent |
|
839 |
For convenience, @{command datatype_new} also provides the following collection: |
|
840 |
||
53552 | 841 |
\begin{indentblock} |
53544 | 842 |
\begin{description} |
843 |
||
844 |
\item[@{text "t."}\hthm{simps} = @{text t.inject} @{text t.distinct} @{text t.case} @{text t.rec} @{text t.fold} @{text t.map} @{text t.rel_inject}] ~ \\ |
|
845 |
@{text t.rel_distinct} @{text t.sets} |
|
846 |
||
847 |
\end{description} |
|
53552 | 848 |
\end{indentblock} |
52828 | 849 |
*} |
850 |
||
52794 | 851 |
|
52827 | 852 |
subsection {* Compatibility Issues |
52824 | 853 |
\label{ssec:datatype-compatibility-issues} *} |
52794 | 854 |
|
52828 | 855 |
text {* |
53617 | 856 |
% * main incompabilities between two packages |
857 |
% * differences in theorem names (e.g. singular vs. plural for some props?) |
|
858 |
% * differences in "simps"? |
|
859 |
% * different recursor/induction in nested case |
|
860 |
% * discussed in Section~\ref{sec:defining-recursive-functions} |
|
861 |
% * different ML interfaces / extension mechanisms |
|
862 |
% |
|
863 |
% * register new datatype as old datatype |
|
864 |
% * motivation: |
|
865 |
% * do recursion through new datatype in old datatype |
|
866 |
% (e.g. useful when porting to the new package one type at a time) |
|
867 |
% * use old primrec |
|
868 |
% * use fun |
|
869 |
% * use extensions like size (needed for fun), the AFP order, Quickcheck, |
|
870 |
% Nitpick |
|
871 |
% * provide exactly the same theorems with the same names (compatibility) |
|
872 |
% * option 1 |
|
53623 | 873 |
% * @{text "rep_compat"} |
53617 | 874 |
% * \keyw{rep\_datatype} |
875 |
% * has some limitations |
|
876 |
% * mutually recursive datatypes? (fails with rep_datatype?) |
|
877 |
% * nested datatypes? (fails with datatype_new?) |
|
878 |
% * option 2 |
|
879 |
% * @{command datatype_new_compat} |
|
880 |
% * not fully implemented yet? |
|
52828 | 881 |
|
53617 | 882 |
% * register old datatype as new datatype |
883 |
% * no clean way yet |
|
884 |
% * if the goal is to do recursion through old datatypes, can register it as |
|
885 |
% a BNF (Section XXX) |
|
886 |
% * can also derive destructors etc. using @{command wrap_free_constructors} |
|
887 |
% (Section XXX) |
|
52828 | 888 |
*} |
889 |
||
52792 | 890 |
|
52827 | 891 |
section {* Defining Recursive Functions |
52805 | 892 |
\label{sec:defining-recursive-functions} *} |
893 |
||
894 |
text {* |
|
53644 | 895 |
Recursive functions over datatypes can be specified using @{command |
896 |
primrec_new}, which supports primitive recursion, or using the more general |
|
897 |
\keyw{fun} and \keyw{function} commands. Here, the focus is on @{command |
|
898 |
primrec_new}; the other two commands are described in a separate tutorial |
|
53646 | 899 |
\cite{isabelle-function}. |
52828 | 900 |
|
53621 | 901 |
%%% TODO: partial_function |
52805 | 902 |
*} |
52792 | 903 |
|
52805 | 904 |
|
53617 | 905 |
subsection {* Introductory Examples |
906 |
\label{ssec:primrec-introductory-examples} *} |
|
52828 | 907 |
|
53646 | 908 |
text {* |
909 |
Primitive recursion is illustrated through concrete examples based on the |
|
910 |
datatypes defined in Section~\ref{ssec:datatype-introductory-examples}. More |
|
911 |
examples can be found in the directory \verb|~~/src/HOL/BNF/Examples|. |
|
912 |
*} |
|
913 |
||
53621 | 914 |
|
915 |
subsubsection {* Nonrecursive Types |
|
916 |
\label{sssec:primrec-nonrecursive-types} *} |
|
52828 | 917 |
|
52841 | 918 |
text {* |
53621 | 919 |
Primitive recursion removes one layer of constructors on the left-hand side in |
920 |
each equation. For example: |
|
52841 | 921 |
*} |
922 |
||
923 |
primrec_new bool_of_trool :: "trool \<Rightarrow> bool" where |
|
53621 | 924 |
"bool_of_trool Faalse \<longleftrightarrow> False" | |
925 |
"bool_of_trool Truue \<longleftrightarrow> True" |
|
52841 | 926 |
|
53621 | 927 |
text {* \blankline *} |
52841 | 928 |
|
53025 | 929 |
primrec_new the_list :: "'a option \<Rightarrow> 'a list" where |
930 |
"the_list None = []" | |
|
931 |
"the_list (Some a) = [a]" |
|
52841 | 932 |
|
53621 | 933 |
text {* \blankline *} |
934 |
||
53025 | 935 |
primrec_new the_default :: "'a \<Rightarrow> 'a option \<Rightarrow> 'a" where |
936 |
"the_default d None = d" | |
|
937 |
"the_default _ (Some a) = a" |
|
52843 | 938 |
|
53621 | 939 |
text {* \blankline *} |
940 |
||
52841 | 941 |
primrec_new mirrror :: "('a, 'b, 'c) triple \<Rightarrow> ('c, 'b, 'a) triple" where |
942 |
"mirrror (Triple a b c) = Triple c b a" |
|
943 |
||
53621 | 944 |
text {* |
945 |
\noindent |
|
946 |
The equations can be specified in any order, and it is acceptable to leave out |
|
947 |
some cases, which are then unspecified. Pattern matching on the left-hand side |
|
948 |
is restricted to a single datatype, which must correspond to the same argument |
|
949 |
in all equations. |
|
950 |
*} |
|
52828 | 951 |
|
53621 | 952 |
|
953 |
subsubsection {* Simple Recursion |
|
954 |
\label{sssec:primrec-simple-recursion} *} |
|
52828 | 955 |
|
52841 | 956 |
text {* |
53621 | 957 |
For simple recursive types, recursive calls on a constructor argument are |
958 |
allowed on the right-hand side: |
|
52841 | 959 |
*} |
960 |
||
53621 | 961 |
(*<*) |
962 |
context dummy_tlist |
|
963 |
begin |
|
964 |
(*>*) |
|
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
965 |
primrec_new replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where |
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
966 |
"replicate Zero _ = []" | |
53644 | 967 |
"replicate (Suc n) x = x # replicate n x" |
52841 | 968 |
|
53621 | 969 |
text {* \blankline *} |
52843 | 970 |
|
53332 | 971 |
primrec_new at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where |
53644 | 972 |
"at (x # xs) j = |
52843 | 973 |
(case j of |
53644 | 974 |
Zero \<Rightarrow> x |
975 |
| Suc j' \<Rightarrow> at xs j')" |
|
52843 | 976 |
|
53621 | 977 |
text {* \blankline *} |
978 |
||
52841 | 979 |
primrec_new tfold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) tlist \<Rightarrow> 'b" where |
53644 | 980 |
"tfold _ (TNil y) = y" | |
981 |
"tfold f (TCons x xs) = f x (tfold f xs)" |
|
53491 | 982 |
(*<*) |
983 |
end |
|
984 |
(*>*) |
|
52841 | 985 |
|
53025 | 986 |
text {* |
53621 | 987 |
\noindent |
988 |
The next example is not primitive recursive, but it can be defined easily using |
|
53644 | 989 |
\keyw{fun}. The @{command datatype_new_compat} command is needed to register |
990 |
new-style datatypes for use with \keyw{fun} and \keyw{function} |
|
53621 | 991 |
(Section~\ref{sssec:datatype-new-compat}): |
53025 | 992 |
*} |
52828 | 993 |
|
53621 | 994 |
datatype_new_compat nat |
995 |
||
996 |
text {* \blankline *} |
|
997 |
||
998 |
fun at_least_two :: "nat \<Rightarrow> bool" where |
|
999 |
"at_least_two (Suc (Suc _)) \<longleftrightarrow> True" | |
|
1000 |
"at_least_two _ \<longleftrightarrow> False" |
|
1001 |
||
1002 |
||
1003 |
subsubsection {* Mutual Recursion |
|
1004 |
\label{sssec:primrec-mutual-recursion} *} |
|
52828 | 1005 |
|
52841 | 1006 |
text {* |
53621 | 1007 |
The syntax for mutually recursive functions over mutually recursive datatypes |
1008 |
is straightforward: |
|
52841 | 1009 |
*} |
1010 |
||
1011 |
primrec_new |
|
53623 | 1012 |
nat_of_even_nat :: "even_nat \<Rightarrow> nat" and |
1013 |
nat_of_odd_nat :: "odd_nat \<Rightarrow> nat" |
|
52841 | 1014 |
where |
53623 | 1015 |
"nat_of_even_nat Even_Zero = Zero" | |
1016 |
"nat_of_even_nat (Even_Suc n) = Suc (nat_of_odd_nat n)" | |
|
1017 |
"nat_of_odd_nat (Odd_Suc n) = Suc (nat_of_even_nat n)" |
|
52841 | 1018 |
|
1019 |
primrec_new |
|
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1020 |
eval\<^sub>e :: "('a \<Rightarrow> int) \<Rightarrow> ('b \<Rightarrow> int) \<Rightarrow> ('a, 'b) exp \<Rightarrow> int" and |
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1021 |
eval\<^sub>t :: "('a \<Rightarrow> int) \<Rightarrow> ('b \<Rightarrow> int) \<Rightarrow> ('a, 'b) trm \<Rightarrow> int" and |
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1022 |
eval\<^sub>f :: "('a \<Rightarrow> int) \<Rightarrow> ('b \<Rightarrow> int) \<Rightarrow> ('a, 'b) fct \<Rightarrow> int" |
52841 | 1023 |
where |
1024 |
"eval\<^sub>e \<gamma> \<xi> (Term t) = eval\<^sub>t \<gamma> \<xi> t" | |
|
1025 |
"eval\<^sub>e \<gamma> \<xi> (Sum t e) = eval\<^sub>t \<gamma> \<xi> t + eval\<^sub>e \<gamma> \<xi> e" | |
|
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1026 |
"eval\<^sub>t \<gamma> \<xi> (Factor f) = eval\<^sub>f \<gamma> \<xi> f" | |
52841 | 1027 |
"eval\<^sub>t \<gamma> \<xi> (Prod f t) = eval\<^sub>f \<gamma> \<xi> f + eval\<^sub>t \<gamma> \<xi> t" | |
1028 |
"eval\<^sub>f \<gamma> _ (Const a) = \<gamma> a" | |
|
1029 |
"eval\<^sub>f _ \<xi> (Var b) = \<xi> b" | |
|
1030 |
"eval\<^sub>f \<gamma> \<xi> (Expr e) = eval\<^sub>e \<gamma> \<xi> e" |
|
1031 |
||
53621 | 1032 |
text {* |
1033 |
\noindent |
|
53644 | 1034 |
Mutual recursion is be possible within a single type, using \keyw{fun}: |
53621 | 1035 |
*} |
52828 | 1036 |
|
53621 | 1037 |
fun |
1038 |
even :: "nat \<Rightarrow> bool" and |
|
1039 |
odd :: "nat \<Rightarrow> bool" |
|
1040 |
where |
|
1041 |
"even Zero = True" | |
|
1042 |
"even (Suc n) = odd n" | |
|
1043 |
"odd Zero = False" | |
|
1044 |
"odd (Suc n) = even n" |
|
1045 |
||
1046 |
||
1047 |
subsubsection {* Nested Recursion |
|
1048 |
\label{sssec:primrec-nested-recursion} *} |
|
1049 |
||
1050 |
text {* |
|
1051 |
In a departure from the old datatype package, nested recursion is normally |
|
1052 |
handled via the map functions of the nesting type constructors. For example, |
|
1053 |
recursive calls are lifted to lists using @{const map}: |
|
1054 |
*} |
|
52828 | 1055 |
|
52843 | 1056 |
(*<*) |
53644 | 1057 |
datatype_new 'a tree\<^sub>f\<^sub>f = Node\<^sub>f\<^sub>f (lbl\<^sub>f\<^sub>f: 'a) (sub\<^sub>f\<^sub>f: "'a tree\<^sub>f\<^sub>f list") |
53621 | 1058 |
|
1059 |
context dummy_tlist |
|
1060 |
begin |
|
52843 | 1061 |
(*>*) |
53028 | 1062 |
primrec_new at\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" where |
1063 |
"at\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js = |
|
52843 | 1064 |
(case js of |
1065 |
[] \<Rightarrow> a |
|
53028 | 1066 |
| j # js' \<Rightarrow> at (map (\<lambda>t. at\<^sub>f\<^sub>f t js') ts) j)" |
53621 | 1067 |
(*<*) |
1068 |
end |
|
1069 |
(*>*) |
|
52843 | 1070 |
|
53025 | 1071 |
text {* |
53621 | 1072 |
The next example features recursion through the @{text option} type. Although |
53623 | 1073 |
@{text option} is not a new-style datatype, it is registered as a BNF with the |
53621 | 1074 |
map function @{const option_map}: |
53025 | 1075 |
*} |
52843 | 1076 |
|
53335
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1077 |
(*<*) |
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1078 |
locale sum_btree_nested |
53621 | 1079 |
begin |
53335
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1080 |
(*>*) |
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1081 |
primrec_new sum_btree :: "('a\<Colon>{zero,plus}) btree \<Rightarrow> 'a" where |
52843 | 1082 |
"sum_btree (BNode a lt rt) = |
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1083 |
a + the_default 0 (option_map sum_btree lt) + |
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1084 |
the_default 0 (option_map sum_btree rt)" |
53335
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1085 |
(*<*) |
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1086 |
end |
585b2fee55e5
fixed bug in primrec_new (allow indirect recursion through constructor arguments other than the first)
panny
parents:
53332
diff
changeset
|
1087 |
(*>*) |
52843 | 1088 |
|
53136 | 1089 |
text {* |
53621 | 1090 |
\noindent |
1091 |
The same principle applies for arbitrary type constructors through which |
|
1092 |
recursion is possible. Notably, the map function for the function type |
|
1093 |
(@{text \<Rightarrow>}) is simply composition (@{text "op \<circ>"}): |
|
53136 | 1094 |
*} |
52828 | 1095 |
|
53621 | 1096 |
primrec_new ftree_map :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree \<Rightarrow> 'a ftree" where |
1097 |
"ftree_map f (FTLeaf x) = FTLeaf (f x)" | |
|
1098 |
"ftree_map f (FTNode g) = FTNode (ftree_map f \<circ> g)" |
|
52828 | 1099 |
|
52843 | 1100 |
text {* |
53621 | 1101 |
\noindent |
1102 |
(No such function is defined by the package because @{typ 'a} is dead in |
|
1103 |
@{typ "'a ftree"}, but we can easily do it ourselves.) |
|
1104 |
*} |
|
1105 |
||
1106 |
||
1107 |
subsubsection {* Nested-as-Mutual Recursion |
|
53644 | 1108 |
\label{sssec:primrec-nested-as-mutual-recursion} *} |
53621 | 1109 |
|
1110 |
text {* |
|
1111 |
For compatibility with the old package, but also because it is sometimes |
|
1112 |
convenient in its own right, it is possible to treat nested recursive datatypes |
|
1113 |
as mutually recursive ones if the recursion takes place though new-style |
|
1114 |
datatypes. For example: |
|
52843 | 1115 |
*} |
1116 |
||
53331
20440c789759
prove theorem in the right context (that knows about local variables)
traytel
parents:
53330
diff
changeset
|
1117 |
primrec_new |
53028 | 1118 |
at_tree\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" and |
1119 |
at_trees\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f list \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> 'a" |
|
52843 | 1120 |
where |
53028 | 1121 |
"at_tree\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js = |
52843 | 1122 |
(case js of |
1123 |
[] \<Rightarrow> a |
|
53028 | 1124 |
| j # js' \<Rightarrow> at_trees\<^sub>f\<^sub>f ts j js')" | |
1125 |
"at_trees\<^sub>f\<^sub>f (t # ts) j = |
|
52843 | 1126 |
(case j of |
53028 | 1127 |
Zero \<Rightarrow> at_tree\<^sub>f\<^sub>f t |
1128 |
| Suc j' \<Rightarrow> at_trees\<^sub>f\<^sub>f ts j')" |
|
52843 | 1129 |
|
53621 | 1130 |
text {* \blankline *} |
1131 |
||
53331
20440c789759
prove theorem in the right context (that knows about local variables)
traytel
parents:
53330
diff
changeset
|
1132 |
primrec_new |
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1133 |
sum_btree :: "('a\<Colon>{zero,plus}) btree \<Rightarrow> 'a" and |
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1134 |
sum_btree_option :: "'a btree option \<Rightarrow> 'a" |
52843 | 1135 |
where |
1136 |
"sum_btree (BNode a lt rt) = |
|
53025 | 1137 |
a + sum_btree_option lt + sum_btree_option rt" | |
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1138 |
"sum_btree_option None = 0" | |
53025 | 1139 |
"sum_btree_option (Some t) = sum_btree t" |
52843 | 1140 |
|
1141 |
text {* |
|
53621 | 1142 |
|
1143 |
% * can pretend a nested type is mutually recursive (if purely inductive) |
|
1144 |
% * avoids the higher-order map |
|
1145 |
% * e.g. |
|
1146 |
||
53617 | 1147 |
% * this can always be avoided; |
1148 |
% * e.g. in our previous example, we first mapped the recursive |
|
1149 |
% calls, then we used a generic at function to retrieve the result |
|
1150 |
% |
|
1151 |
% * there's no hard-and-fast rule of when to use one or the other, |
|
1152 |
% just like there's no rule when to use fold and when to use |
|
1153 |
% primrec_new |
|
1154 |
% |
|
1155 |
% * higher-order approach, considering nesting as nesting, is more |
|
1156 |
% compositional -- e.g. we saw how we could reuse an existing polymorphic |
|
1157 |
% at or the_default, whereas @{const at_trees\<^sub>f\<^sub>f} is much more specific |
|
1158 |
% |
|
1159 |
% * but: |
|
1160 |
% * is perhaps less intuitive, because it requires higher-order thinking |
|
1161 |
% * may seem inefficient, and indeed with the code generator the |
|
1162 |
% mutually recursive version might be nicer |
|
1163 |
% * is somewhat indirect -- must apply a map first, then compute a result |
|
1164 |
% (cannot mix) |
|
1165 |
% * the auxiliary functions like @{const at_trees\<^sub>f\<^sub>f} are sometimes useful in own right |
|
1166 |
% |
|
1167 |
% * impact on automation unclear |
|
1168 |
% |
|
52843 | 1169 |
*} |
1170 |
||
52824 | 1171 |
|
53617 | 1172 |
subsection {* Command Syntax |
1173 |
\label{ssec:primrec-command-syntax} *} |
|
1174 |
||
1175 |
||
53621 | 1176 |
subsubsection {* \keyw{primrec\_new} |
1177 |
\label{sssec:primrec-new} *} |
|
52828 | 1178 |
|
1179 |
text {* |
|
52840 | 1180 |
Primitive recursive functions have the following general syntax: |
52794 | 1181 |
|
52840 | 1182 |
@{rail " |
53535 | 1183 |
@@{command_def primrec_new} target? fixes \\ @'where' |
52840 | 1184 |
(@{syntax primrec_equation} + '|') |
1185 |
; |
|
53534 | 1186 |
@{syntax_def primrec_equation}: thmdecl? prop |
52840 | 1187 |
"} |
52828 | 1188 |
*} |
1189 |
||
52840 | 1190 |
|
53619 | 1191 |
(* |
52840 | 1192 |
subsection {* Generated Theorems |
1193 |
\label{ssec:primrec-generated-theorems} *} |
|
52824 | 1194 |
|
52828 | 1195 |
text {* |
53617 | 1196 |
% * synthesized nonrecursive definition |
1197 |
% * user specification is rederived from it, exactly as entered |
|
1198 |
% |
|
1199 |
% * induct |
|
1200 |
% * mutualized |
|
1201 |
% * without some needless induction hypotheses if not used |
|
1202 |
% * fold, rec |
|
1203 |
% * mutualized |
|
52828 | 1204 |
*} |
53619 | 1205 |
*) |
1206 |
||
52824 | 1207 |
|
52840 | 1208 |
subsection {* Recursive Default Values for Selectors |
53623 | 1209 |
\label{ssec:primrec-recursive-default-values-for-selectors} *} |
52827 | 1210 |
|
1211 |
text {* |
|
1212 |
A datatype selector @{text un_D} can have a default value for each constructor |
|
1213 |
on which it is not otherwise specified. Occasionally, it is useful to have the |
|
1214 |
default value be defined recursively. This produces a chicken-and-egg situation |
|
53621 | 1215 |
that may seem unsolvable, because the datatype is not introduced yet at the |
52827 | 1216 |
moment when the selectors are introduced. Of course, we can always define the |
1217 |
selectors manually afterward, but we then have to state and prove all the |
|
1218 |
characteristic theorems ourselves instead of letting the package do it. |
|
1219 |
||
1220 |
Fortunately, there is a fairly elegant workaround that relies on overloading and |
|
1221 |
that avoids the tedium of manual derivations: |
|
1222 |
||
1223 |
\begin{enumerate} |
|
1224 |
\setlength{\itemsep}{0pt} |
|
1225 |
||
1226 |
\item |
|
1227 |
Introduce a fully unspecified constant @{text "un_D\<^sub>0 \<Colon> 'a"} using |
|
1228 |
@{keyword consts}. |
|
1229 |
||
1230 |
\item |
|
53535 | 1231 |
Define the datatype, specifying @{text "un_D\<^sub>0"} as the selector's default |
1232 |
value. |
|
52827 | 1233 |
|
1234 |
\item |
|
53535 | 1235 |
Define the behavior of @{text "un_D\<^sub>0"} on values of the newly introduced |
1236 |
datatype using the \keyw{overloading} command. |
|
52827 | 1237 |
|
1238 |
\item |
|
1239 |
Derive the desired equation on @{text un_D} from the characteristic equations |
|
1240 |
for @{text "un_D\<^sub>0"}. |
|
1241 |
\end{enumerate} |
|
1242 |
||
53619 | 1243 |
\noindent |
52827 | 1244 |
The following example illustrates this procedure: |
1245 |
*} |
|
1246 |
||
1247 |
consts termi\<^sub>0 :: 'a |
|
1248 |
||
53619 | 1249 |
text {* \blankline *} |
1250 |
||
53491 | 1251 |
datatype_new ('a, 'b) tlist = |
52827 | 1252 |
TNil (termi: 'b) (defaults ttl: TNil) |
53491 | 1253 |
| TCons (thd: 'a) (ttl : "('a, 'b) tlist") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs") |
52827 | 1254 |
|
53619 | 1255 |
text {* \blankline *} |
1256 |
||
52827 | 1257 |
overloading |
53491 | 1258 |
termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist \<Rightarrow> 'b" |
52827 | 1259 |
begin |
53491 | 1260 |
primrec_new termi\<^sub>0 :: "('a, 'b) tlist \<Rightarrow> 'b" where |
53621 | 1261 |
"termi\<^sub>0 (TNil y) = y" | |
1262 |
"termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs" |
|
52827 | 1263 |
end |
1264 |
||
53619 | 1265 |
text {* \blankline *} |
1266 |
||
52827 | 1267 |
lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs" |
1268 |
by (cases xs) auto |
|
1269 |
||
1270 |
||
53617 | 1271 |
(* |
52828 | 1272 |
subsection {* Compatibility Issues |
53617 | 1273 |
\label{ssec:primrec-compatibility-issues} *} |
52828 | 1274 |
|
1275 |
text {* |
|
53617 | 1276 |
% * different induction in nested case |
1277 |
% * solution: define nested-as-mutual functions with primrec, |
|
1278 |
% and look at .induct |
|
1279 |
% |
|
1280 |
% * different induction and recursor in nested case |
|
1281 |
% * only matters to low-level users; they can define a dummy function to force |
|
1282 |
% generation of mutualized recursor |
|
52828 | 1283 |
*} |
53617 | 1284 |
*) |
52794 | 1285 |
|
1286 |
||
52827 | 1287 |
section {* Defining Codatatypes |
52805 | 1288 |
\label{sec:defining-codatatypes} *} |
1289 |
||
1290 |
text {* |
|
53623 | 1291 |
Codatatypes can be specified using the @{command codatatype} command. The |
1292 |
command is first illustrated through concrete examples featuring different |
|
1293 |
flavors of corecursion. More examples can be found in the directory |
|
1294 |
\verb|~~/src/HOL/BNF/Examples|. The \emph{Archive of Formal Proofs} also |
|
1295 |
includes some useful codatatypes, notably for lazy lists \cite{lochbihler-2010}. |
|
52805 | 1296 |
*} |
52792 | 1297 |
|
52824 | 1298 |
|
53617 | 1299 |
subsection {* Introductory Examples |
1300 |
\label{ssec:codatatype-introductory-examples} *} |
|
52794 | 1301 |
|
53623 | 1302 |
|
1303 |
subsubsection {* Simple Corecursion |
|
1304 |
\label{sssec:codatatype-simple-corecursion} *} |
|
1305 |
||
52805 | 1306 |
text {* |
53623 | 1307 |
Noncorecursive codatatypes coincide with the corresponding datatypes, so |
1308 |
they have no practical uses. \emph{Corecursive codatatypes} have the same syntax |
|
1309 |
as recursive datatypes, except for the command name. For example, here is the |
|
1310 |
definition of lazy lists: |
|
1311 |
*} |
|
1312 |
||
1313 |
codatatype (lset: 'a) llist (map: lmap rel: llist_all2) = |
|
1314 |
lnull: LNil (defaults ltl: LNil) |
|
1315 |
| LCons (lhd: 'a) (ltl: "'a llist") |
|
1316 |
||
1317 |
text {* |
|
1318 |
\noindent |
|
1319 |
Lazy lists can be infinite, such as @{text "LCons 0 (LCons 0 (\<dots>))"} and |
|
1320 |
@{text "LCons 0 (LCons 1 (LCons 2 (\<dots>)))"}. Another interesting type that can |
|
1321 |
be defined as a codatatype is that of the extended natural numbers: |
|
1322 |
*} |
|
1323 |
||
53644 | 1324 |
codatatype enat = EZero | ESuc enat |
53623 | 1325 |
|
1326 |
text {* |
|
1327 |
\noindent |
|
1328 |
This type has exactly one infinite element, @{text "ESuc (ESuc (ESuc (\<dots>)))"}, |
|
1329 |
that represents $\infty$. In addition, it has finite values of the form |
|
1330 |
@{text "ESuc (\<dots> (ESuc EZero)\<dots>)"}. |
|
52805 | 1331 |
*} |
53623 | 1332 |
|
1333 |
||
1334 |
subsubsection {* Mutual Corecursion |
|
1335 |
\label{sssec:codatatype-mutual-corecursion} *} |
|
1336 |
||
1337 |
text {* |
|
1338 |
\noindent |
|
1339 |
The example below introduces a pair of \emph{mutually corecursive} types: |
|
1340 |
*} |
|
1341 |
||
1342 |
codatatype even_enat = Even_EZero | Even_ESuc odd_enat |
|
1343 |
and odd_enat = Odd_ESuc even_enat |
|
1344 |
||
1345 |
||
1346 |
subsubsection {* Nested Corecursion |
|
1347 |
\label{sssec:codatatype-nested-corecursion} *} |
|
1348 |
||
1349 |
text {* |
|
1350 |
\noindent |
|
1351 |
The next two examples feature \emph{nested corecursion}: |
|
1352 |
*} |
|
1353 |
||
53644 | 1354 |
codatatype 'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i (lbl\<^sub>i\<^sub>i: 'a) (sub\<^sub>i\<^sub>i: "'a tree\<^sub>i\<^sub>i llist") |
1355 |
codatatype 'a tree\<^sub>i\<^sub>s = Node\<^sub>i\<^sub>s (lbl\<^sub>i\<^sub>s: 'a) (sub\<^sub>i\<^sub>s: "'a tree\<^sub>i\<^sub>s fset") |
|
52805 | 1356 |
|
52824 | 1357 |
|
53617 | 1358 |
subsection {* Command Syntax |
1359 |
\label{ssec:codatatype-command-syntax} *} |
|
52805 | 1360 |
|
53619 | 1361 |
|
53621 | 1362 |
subsubsection {* \keyw{codatatype} |
1363 |
\label{sssec:codatatype} *} |
|
53619 | 1364 |
|
52824 | 1365 |
text {* |
52827 | 1366 |
Definitions of codatatypes have almost exactly the same syntax as for datatypes |
53617 | 1367 |
(Section~\ref{ssec:datatype-command-syntax}), with two exceptions: The command |
53623 | 1368 |
is called @{command codatatype}. The @{text "no_discs_sels"} option is not |
1369 |
available, because destructors are a crucial notion for codatatypes. |
|
1370 |
*} |
|
1371 |
||
1372 |
||
1373 |
subsection {* Generated Constants |
|
1374 |
\label{ssec:codatatype-generated-constants} *} |
|
1375 |
||
1376 |
text {* |
|
1377 |
Given a codatatype @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
|
1378 |
with $m > 0$ live type variables and $n$ constructors @{text "t.C\<^sub>1"}, |
|
1379 |
\ldots, @{text "t.C\<^sub>n"}, the same auxiliary constants are generated as for |
|
1380 |
datatypes (Section~\ref{ssec:datatype-generated-constants}), except that the |
|
1381 |
iterator and the recursor are replaced by dual concepts: |
|
1382 |
||
1383 |
\begin{itemize} |
|
1384 |
\setlength{\itemsep}{0pt} |
|
1385 |
||
1386 |
\item \relax{Coiterator}: @{text t_unfold} |
|
1387 |
||
1388 |
\item \relax{Corecursor}: @{text t_corec} |
|
1389 |
||
1390 |
\end{itemize} |
|
1391 |
*} |
|
1392 |
||
1393 |
||
1394 |
subsection {* Generated Theorems |
|
1395 |
\label{ssec:codatatype-generated-theorems} *} |
|
1396 |
||
1397 |
text {* |
|
1398 |
The characteristic theorems generated by @{command codatatype} are grouped in |
|
1399 |
three broad categories: |
|
1400 |
||
1401 |
\begin{itemize} |
|
1402 |
\item The \emph{free constructor theorems} are properties about the constructors |
|
1403 |
and destructors that can be derived for any freely generated type. |
|
1404 |
||
1405 |
\item The \emph{functorial theorems} are properties of datatypes related to |
|
1406 |
their BNF nature. |
|
1407 |
||
1408 |
\item The \emph{coinductive theorems} are properties of datatypes related to |
|
1409 |
their coinductive nature. |
|
1410 |
\end{itemize} |
|
1411 |
||
1412 |
\noindent |
|
1413 |
The first two categories are exactly as for datatypes and are described in |
|
53642 | 1414 |
Sections |
1415 |
\ref{sssec:free-constructor-theorems}~and~\ref{sssec:functorial-theorems}. |
|
52824 | 1416 |
*} |
1417 |
||
53617 | 1418 |
|
53623 | 1419 |
subsubsection {* Coinductive Theorems |
1420 |
\label{sssec:coinductive-theorems} *} |
|
1421 |
||
1422 |
text {* |
|
1423 |
The coinductive theorems are as follows: |
|
1424 |
||
1425 |
\begin{indentblock} |
|
1426 |
\begin{description} |
|
1427 |
||
53643 | 1428 |
\item[\begin{tabular}{@ {}l@ {}} |
1429 |
@{text "t."}\hthm{coinduct} @{text "[coinduct t, consumes m, case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1430 |
\phantom{@{text "t."}\hthm{coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1431 |
\end{tabular}] ~ \\ |
|
53623 | 1432 |
@{thm llist.coinduct[no_vars]} |
53617 | 1433 |
|
53643 | 1434 |
\item[\begin{tabular}{@ {}l@ {}} |
1435 |
@{text "t."}\hthm{strong\_coinduct} @{text "[consumes m, case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1436 |
\phantom{@{text "t."}\hthm{strong\_coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1437 |
\end{tabular}] ~ \\ |
|
1438 |
@{thm llist.strong_coinduct[no_vars]} |
|
53617 | 1439 |
|
53643 | 1440 |
\item[\begin{tabular}{@ {}l@ {}} |
1441 |
@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{coinduct} @{text "[case_names t\<^sub>1 \<dots> t\<^sub>m, case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"} \\ |
|
1442 |
@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{strong\_coinduct} @{text "[case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1443 |
\phantom{@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{strong\_coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1444 |
\end{tabular}] ~ \\ |
|
1445 |
Given $m > 1$ mutually corecursive codatatypes, these coinduction rules can be |
|
1446 |
used to prove $m$ properties simultaneously. |
|
1447 |
||
1448 |
\item[@{text "t."}\hthm{unfold} \rm(@{text "[simp]"})\rm:] ~ \\ |
|
53623 | 1449 |
@{thm llist.unfold(1)[no_vars]} \\ |
1450 |
@{thm llist.unfold(2)[no_vars]} |
|
1451 |
||
53643 | 1452 |
\item[@{text "t."}\hthm{corec} (@{text "[simp]"})\rm:] ~ \\ |
53623 | 1453 |
@{thm llist.corec(1)[no_vars]} \\ |
1454 |
@{thm llist.corec(2)[no_vars]} |
|
1455 |
||
53643 | 1456 |
\item[@{text "t."}\hthm{disc\_unfold} @{text "[simp]"}\rm:] ~ \\ |
1457 |
@{thm llist.disc_unfold(1)[no_vars]} \\ |
|
1458 |
@{thm llist.disc_unfold(2)[no_vars]} |
|
1459 |
||
1460 |
\item[@{text "t."}\hthm{disc\_corec} @{text "[simp]"}\rm:] ~ \\ |
|
1461 |
@{thm llist.disc_corec(1)[no_vars]} \\ |
|
1462 |
@{thm llist.disc_corec(2)[no_vars]} |
|
1463 |
||
1464 |
\item[@{text "t."}\hthm{disc\_unfold\_iff} @{text "[simp]"}\rm:] ~ \\ |
|
1465 |
@{thm llist.disc_unfold_iff(1)[no_vars]} \\ |
|
1466 |
@{thm llist.disc_unfold_iff(2)[no_vars]} |
|
1467 |
||
1468 |
\item[@{text "t."}\hthm{disc\_corec\_iff} @{text "[simp]"}\rm:] ~ \\ |
|
1469 |
@{thm llist.disc_corec_iff(1)[no_vars]} \\ |
|
1470 |
@{thm llist.disc_corec_iff(2)[no_vars]} |
|
1471 |
||
1472 |
\item[@{text "t."}\hthm{sel\_unfold} @{text "[simp]"}\rm:] ~ \\ |
|
1473 |
@{thm llist.sel_unfold(1)[no_vars]} \\ |
|
1474 |
@{thm llist.sel_unfold(2)[no_vars]} |
|
1475 |
||
1476 |
\item[@{text "t."}\hthm{sel\_corec} @{text "[simp]"}\rm:] ~ \\ |
|
1477 |
@{thm llist.sel_corec(1)[no_vars]} \\ |
|
1478 |
@{thm llist.sel_corec(2)[no_vars]} |
|
1479 |
||
53623 | 1480 |
\end{description} |
1481 |
\end{indentblock} |
|
1482 |
||
1483 |
\noindent |
|
1484 |
For convenience, @{command codatatype} also provides the following collection: |
|
1485 |
||
1486 |
\begin{indentblock} |
|
1487 |
\begin{description} |
|
1488 |
||
53643 | 1489 |
\item[@{text "t."}\hthm{simps} = @{text t.inject} @{text t.distinct} @{text t.case} @{text t.corec}$^*$ @{text t.disc_corec}] ~ \\ |
1490 |
@{text t.disc_corec_iff} @{text t.sel_corec} @{text t.unfold}$^*$ @{text t.disc_unfold} @{text t.disc_unfold_iff} ~ \\ |
|
1491 |
@{text t.sel_unfold} @{text t.map} @{text t.rel_inject} @{text t.rel_distinct} @{text t.sets} |
|
53623 | 1492 |
|
1493 |
\end{description} |
|
1494 |
\end{indentblock} |
|
1495 |
*} |
|
52805 | 1496 |
|
1497 |
||
52827 | 1498 |
section {* Defining Corecursive Functions |
52805 | 1499 |
\label{sec:defining-corecursive-functions} *} |
1500 |
||
1501 |
text {* |
|
53623 | 1502 |
Corecursive functions can be specified using the @{command primcorec} command. |
53644 | 1503 |
Corecursive functions over codatatypes can be specified using @{command |
1504 |
primcorec}, which supports primitive corecursion, or using the more general |
|
1505 |
\keyw{partial\_function} command. Here, the focus is on @{command primcorec}. |
|
1506 |
More examples can be found in the directory \verb|~~/src/HOL/BNF/Examples|. |
|
1507 |
||
1508 |
\begin{framed} |
|
1509 |
\noindent |
|
53646 | 1510 |
\textbf{Warning:}\enskip The @{command primcorec} command is under heavy |
1511 |
development. Much of the functionality described here is vaporware. Until the |
|
1512 |
command is fully in place, it is recommended to define corecursive functions |
|
1513 |
directly using the generated @{text t_unfold} or @{text t_corec} combinators. |
|
53644 | 1514 |
\end{framed} |
52828 | 1515 |
|
1516 |
%%% TODO: partial_function? E.g. for defining tail recursive function on lazy |
|
1517 |
%%% lists (cf. terminal0 in TLList.thy) |
|
52805 | 1518 |
*} |
1519 |
||
52824 | 1520 |
|
53617 | 1521 |
subsection {* Introductory Examples |
1522 |
\label{ssec:primcorec-introductory-examples} *} |
|
52805 | 1523 |
|
53646 | 1524 |
text {* |
1525 |
Primitive corecursion is illustrated through concrete examples based on the |
|
1526 |
codatatypes defined in Section~\ref{ssec:codatatype-introductory-examples}. More |
|
1527 |
examples can be found in the directory \verb|~~/src/HOL/BNF/Examples|. |
|
1528 |
*} |
|
1529 |
||
53644 | 1530 |
|
1531 |
subsubsection {* Simple Corecursion |
|
1532 |
\label{sssec:primcorec-simple-corecursion} *} |
|
1533 |
||
53646 | 1534 |
text {* |
1535 |
Whereas recursive functions consume datatypes one constructor at a time, |
|
1536 |
corecursive functions construct codatatypes one constructor at a time. |
|
1537 |
Reflecting a lack of agreement among proponents of coalgebraic methods, Isabelle |
|
1538 |
supports two competing syntaxes for specifying a function $f$: |
|
1539 |
||
1540 |
\begin{itemize} |
|
1541 |
\abovedisplayskip=.5\abovedisplayskip |
|
1542 |
\belowdisplayskip=.5\belowdisplayskip |
|
1543 |
||
1544 |
\item The \emph{constructor view} specifies $f$ by equations of the form |
|
1545 |
\[@{text "f x\<^sub>1 \<dots> x\<^sub>n = \<dots>"}\] |
|
1546 |
Haskell and other lazy functional programming languages support this style. |
|
1547 |
||
1548 |
\item The \emph{destructor view} specifies $f$ by implications of the form |
|
1549 |
\[@{text "\<dots> \<Longrightarrow> is_C\<^sub>j (f x\<^sub>1 \<dots> x\<^sub>n)"}\] and |
|
1550 |
equations of the form |
|
1551 |
\[@{text "un_C\<^sub>ji (f x\<^sub>1 \<dots> x\<^sub>n) = \<dots>"}\]. |
|
1552 |
This style is favored in the coalgebraic literature. |
|
1553 |
\end{itemize} |
|
1554 |
||
1555 |
\noindent |
|
1556 |
Both styles are available as input syntax to @{command primcorec}. Whichever |
|
1557 |
syntax is chosen, characteristic theorems following both styles are generated. |
|
1558 |
||
1559 |
In the constructor view, corecursive calls are allowed on the right-hand side |
|
1560 |
as long as they occur under a constructor. The constructor itself normally |
|
1561 |
appears directly to the right of the equal sign: |
|
1562 |
*} |
|
1563 |
||
53644 | 1564 |
primcorec iterate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist" where |
1565 |
"iterate f x = LCons x (iterate f (f x))" |
|
1566 |
. |
|
1567 |
||
53646 | 1568 |
text {* |
1569 |
\noindent |
|
1570 |
The constructor ensures that progress is made---i.e., the function is |
|
1571 |
\emph{productive}. The above function computes the infinite list |
|
1572 |
@{text "[x, f x, f (f x), \<dots>]"}. Productivity guarantees that prefixes |
|
1573 |
@{text "[x, f x, f (f x), \<dots>, (f ^^ k) x]"} of arbitrary finite length |
|
1574 |
@{text k} can be computed by unfolding the equation a finite number of times. |
|
1575 |
The period (@{text "."}) at the end of the command discharges the zero proof |
|
1576 |
obligations associated with this definition. |
|
1577 |
||
1578 |
The @{const iterate} function can be specified as follows in the destructor |
|
1579 |
view: |
|
1580 |
*} |
|
1581 |
||
53644 | 1582 |
(*<*) |
1583 |
locale dummy_dest_view |
|
1584 |
begin |
|
1585 |
(*>*) |
|
1586 |
primcorec iterate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist" where |
|
1587 |
"\<not> lnull (iterate _ x)" | |
|
1588 |
"lhd (iterate _ x) = x" | |
|
1589 |
"ltl (iterate f x) = iterate f (f x)" |
|
1590 |
. |
|
1591 |
(*<*) |
|
1592 |
end |
|
1593 |
(*>*) |
|
1594 |
||
53646 | 1595 |
text {* |
1596 |
Corecursive calls may only appear directly to the right of the equal sign. |
|
1597 |
||
1598 |
In the constructor view, constructs such as @{text "let"}---@{text "in"}, @{text |
|
1599 |
"if"}---@{text "then"}---@{text "else"}, and @{text "case"}---@{text "of"} may |
|
1600 |
appear around constructors that guard corecursive calls: |
|
1601 |
*} |
|
1602 |
||
53644 | 1603 |
primcorec_notyet lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
1604 |
"lappend xs ys = |
|
1605 |
(case xs of |
|
1606 |
LNil \<Rightarrow> ys |
|
1607 |
| LCons x xs' \<Rightarrow> LCons x (lappend xs' ys))" |
|
1608 |
||
53646 | 1609 |
text {* |
1610 |
\noindent |
|
1611 |
For this example, the destructor view is more cumbersome, because it requires us |
|
1612 |
to destroy the second argument @{term ys} (cf.\ @{term "lnull ys"}, @{term "lhd |
|
1613 |
ys"}, and @{term "ltl ys"}): |
|
1614 |
*} |
|
1615 |
||
53644 | 1616 |
(*<*) |
1617 |
context dummy_dest_view |
|
1618 |
begin |
|
1619 |
(*>*) |
|
1620 |
primcorec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
|
1621 |
"lnull xs \<Longrightarrow> lnull ys \<Longrightarrow> lnull (lappend xs ys)" | |
|
1622 |
"lhd (lappend xs ys) = |
|
1623 |
(case xs of |
|
1624 |
LNil \<Rightarrow> lhd ys |
|
1625 |
| LCons x _ \<Rightarrow> x)" | |
|
1626 |
"ltl (lappend xs ys) = |
|
1627 |
(case xs of |
|
1628 |
LNil \<Rightarrow> ltl ys |
|
1629 |
| LCons _ xs \<Rightarrow> xs)" |
|
1630 |
. |
|
1631 |
(*<*) |
|
1632 |
end |
|
1633 |
(*>*) |
|
1634 |
||
53646 | 1635 |
text {* |
1636 |
Corecursion is useful to specify not only functions but also infinite objects: |
|
1637 |
*} |
|
1638 |
||
53644 | 1639 |
primcorec infty :: enat where |
1640 |
"infty = ESuc infty" |
|
1641 |
. |
|
1642 |
||
53646 | 1643 |
text {* |
1644 |
\noindent |
|
1645 |
The same example in the destructor view: |
|
1646 |
*} |
|
1647 |
||
53644 | 1648 |
(*<*) |
1649 |
context dummy_dest_view |
|
1650 |
begin |
|
1651 |
(*>*) |
|
1652 |
primcorec infty :: enat where |
|
1653 |
"\<not> is_EZero infty" | |
|
1654 |
"un_ESuc infty = infty" |
|
1655 |
. |
|
1656 |
(*<*) |
|
1657 |
end |
|
1658 |
(*>*) |
|
1659 |
||
1660 |
||
1661 |
subsubsection {* Mutual Corecursion |
|
1662 |
\label{sssec:primcorec-mutual-corecursion} *} |
|
1663 |
||
1664 |
primcorec |
|
1665 |
even_infty :: even_enat and |
|
1666 |
odd_infty :: odd_enat |
|
1667 |
where |
|
1668 |
"even_infty = Even_ESuc odd_infty" | |
|
1669 |
"odd_infty = Odd_ESuc even_infty" |
|
1670 |
. |
|
1671 |
||
1672 |
(*<*) |
|
1673 |
context dummy_dest_view |
|
1674 |
begin |
|
1675 |
(*>*) |
|
1676 |
primcorec |
|
1677 |
even_infty :: even_enat and |
|
1678 |
odd_infty :: odd_enat |
|
1679 |
where |
|
1680 |
"\<not> is_Even_EZero even_infty" | |
|
1681 |
"un_Even_ESuc even_infty = odd_infty" | |
|
1682 |
"un_Odd_ESuc odd_infty = even_infty" |
|
1683 |
. |
|
1684 |
(*<*) |
|
1685 |
end |
|
1686 |
(*>*) |
|
1687 |
||
1688 |
||
1689 |
subsubsection {* Nested Corecursion |
|
1690 |
\label{sssec:primcorec-nested-corecursion} *} |
|
1691 |
||
1692 |
primcorec iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" where |
|
1693 |
"iterate\<^sub>i\<^sub>i f x = Node\<^sub>i\<^sub>i x (lmap (iterate\<^sub>i\<^sub>i f) (f x))" |
|
1694 |
. |
|
1695 |
||
1696 |
primcorec iterate\<^sub>i\<^sub>s :: "('a \<Rightarrow> 'a fset) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>s" where |
|
1697 |
"iterate\<^sub>i\<^sub>s f x = Node\<^sub>i\<^sub>s x (fmap (iterate\<^sub>i\<^sub>s f) (f x))" |
|
1698 |
. |
|
1699 |
||
52805 | 1700 |
text {* |
53644 | 1701 |
Again, let us not forget our destructor-oriented passengers: |
52805 | 1702 |
*} |
53644 | 1703 |
|
1704 |
(*<*) |
|
1705 |
context dummy_dest_view |
|
1706 |
begin |
|
1707 |
(*>*) |
|
1708 |
primcorec iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" where |
|
1709 |
"lbl\<^sub>i\<^sub>i (iterate\<^sub>i\<^sub>i f x) = x" | |
|
1710 |
"sub\<^sub>i\<^sub>i (iterate\<^sub>i\<^sub>i f x) = lmap (iterate\<^sub>i\<^sub>i f) (f x)" |
|
1711 |
. |
|
1712 |
(*<*) |
|
1713 |
end |
|
1714 |
(*>*) |
|
1715 |
||
1716 |
||
1717 |
subsubsection {* Nested-as-Mutual Corecursion |
|
1718 |
\label{sssec:primcorec-nested-as-mutual-corecursion} *} |
|
1719 |
||
1720 |
(*<*) |
|
1721 |
locale dummy_iterate |
|
1722 |
begin |
|
1723 |
(*>*) |
|
1724 |
primcorec_notyet |
|
1725 |
iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" and |
|
1726 |
iterates\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a llist \<Rightarrow> 'a tree\<^sub>i\<^sub>i llist" |
|
1727 |
where |
|
1728 |
"iterate\<^sub>i\<^sub>i f x = Node\<^sub>i\<^sub>i x (iterates\<^sub>i\<^sub>i f (f x))" | |
|
1729 |
"iterates\<^sub>i\<^sub>i f xs = |
|
1730 |
(case xs of |
|
1731 |
LNil \<Rightarrow> LNil |
|
1732 |
| LCons x xs' \<Rightarrow> LCons (iterate\<^sub>i\<^sub>i f x) (iterates\<^sub>i\<^sub>i f xs'))" |
|
1733 |
(*<*) |
|
1734 |
end |
|
1735 |
(*>*) |
|
52805 | 1736 |
|
52824 | 1737 |
|
53617 | 1738 |
subsection {* Command Syntax |
1739 |
\label{ssec:primcorec-command-syntax} *} |
|
1740 |
||
1741 |
||
53621 | 1742 |
subsubsection {* \keyw{primcorec} |
1743 |
\label{sssec:primcorec} *} |
|
52840 | 1744 |
|
1745 |
text {* |
|
53018 | 1746 |
Primitive corecursive definitions have the following general syntax: |
52840 | 1747 |
|
1748 |
@{rail " |
|
53535 | 1749 |
@@{command_def primcorec} target? fixes \\ @'where' |
52840 | 1750 |
(@{syntax primcorec_formula} + '|') |
1751 |
; |
|
53534 | 1752 |
@{syntax_def primcorec_formula}: thmdecl? prop (@'of' (term * ))? |
52840 | 1753 |
"} |
1754 |
*} |
|
52794 | 1755 |
|
52824 | 1756 |
|
53619 | 1757 |
(* |
52840 | 1758 |
subsection {* Generated Theorems |
1759 |
\label{ssec:primcorec-generated-theorems} *} |
|
53619 | 1760 |
*) |
52794 | 1761 |
|
1762 |
||
53623 | 1763 |
(* |
1764 |
subsection {* Recursive Default Values for Selectors |
|
1765 |
\label{ssec:primcorec-recursive-default-values-for-selectors} *} |
|
1766 |
||
1767 |
text {* |
|
1768 |
partial_function to the rescue |
|
1769 |
*} |
|
1770 |
*) |
|
1771 |
||
1772 |
||
52827 | 1773 |
section {* Registering Bounded Natural Functors |
52805 | 1774 |
\label{sec:registering-bounded-natural-functors} *} |
52792 | 1775 |
|
52805 | 1776 |
text {* |
53623 | 1777 |
The (co)datatype package can be set up to allow nested recursion through custom |
1778 |
well-behaved type constructors. The key concept is that of a bounded natural |
|
1779 |
functor (BNF). |
|
52829 | 1780 |
|
52805 | 1781 |
*} |
1782 |
||
52824 | 1783 |
|
53619 | 1784 |
(* |
53617 | 1785 |
subsection {* Introductory Example |
1786 |
\label{ssec:bnf-introductory-example} *} |
|
52805 | 1787 |
|
1788 |
text {* |
|
1789 |
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and |
|
1790 |
\verb|~~/src/HOL/BNF/More_BNFs.thy|. |
|
52806 | 1791 |
|
53617 | 1792 |
%Mention distinction between live and dead type arguments; |
1793 |
% * and existence of map, set for those |
|
1794 |
%mention =>. |
|
52805 | 1795 |
*} |
53619 | 1796 |
*) |
52794 | 1797 |
|
52824 | 1798 |
|
53617 | 1799 |
subsection {* Command Syntax |
1800 |
\label{ssec:bnf-command-syntax} *} |
|
1801 |
||
1802 |
||
53621 | 1803 |
subsubsection {* \keyw{bnf} |
1804 |
\label{sssec:bnf} *} |
|
52794 | 1805 |
|
53028 | 1806 |
text {* |
1807 |
@{rail " |
|
53535 | 1808 |
@@{command_def bnf} target? (name ':')? term \\ |
53534 | 1809 |
term_list term term_list term? |
53028 | 1810 |
; |
53534 | 1811 |
X_list: '[' (X + ',') ']' |
53028 | 1812 |
"} |
1813 |
*} |
|
52805 | 1814 |
|
53617 | 1815 |
|
53621 | 1816 |
subsubsection {* \keyw{print\_bnfs} |
1817 |
\label{sssec:print-bnfs} *} |
|
53617 | 1818 |
|
1819 |
text {* |
|
1820 |
@{command print_bnfs} |
|
1821 |
*} |
|
1822 |
||
1823 |
||
1824 |
section {* Deriving Destructors and Theorems for Free Constructors |
|
1825 |
\label{sec:deriving-destructors-and-theorems-for-free-constructors} *} |
|
52794 | 1826 |
|
52805 | 1827 |
text {* |
53623 | 1828 |
The derivation of convenience theorems for types equipped with free constructors, |
1829 |
as performed internally by @{command datatype_new} and @{command codatatype}, |
|
1830 |
is available as a stand-alone command called @{command wrap_free_constructors}. |
|
52794 | 1831 |
|
53617 | 1832 |
% * need for this is rare but may arise if you want e.g. to add destructors to |
1833 |
% a type not introduced by ... |
|
1834 |
% |
|
1835 |
% * also useful for compatibility with old package, e.g. add destructors to |
|
1836 |
% old \keyw{datatype} |
|
1837 |
% |
|
1838 |
% * @{command wrap_free_constructors} |
|
53623 | 1839 |
% * @{text "no_discs_sels"}, @{text "rep_compat"} |
53617 | 1840 |
% * hack to have both co and nonco view via locale (cf. ext nats) |
52805 | 1841 |
*} |
52792 | 1842 |
|
52824 | 1843 |
|
53619 | 1844 |
(* |
53617 | 1845 |
subsection {* Introductory Example |
1846 |
\label{ssec:ctors-introductory-example} *} |
|
53619 | 1847 |
*) |
52794 | 1848 |
|
52824 | 1849 |
|
53617 | 1850 |
subsection {* Command Syntax |
1851 |
\label{ssec:ctors-command-syntax} *} |
|
1852 |
||
1853 |
||
53621 | 1854 |
subsubsection {* \keyw{wrap\_free\_constructors} |
1855 |
\label{sssec:wrap-free-constructors} *} |
|
52828 | 1856 |
|
53018 | 1857 |
text {* |
1858 |
Free constructor wrapping has the following general syntax: |
|
1859 |
||
1860 |
@{rail " |
|
53535 | 1861 |
@@{command_def wrap_free_constructors} target? @{syntax dt_options} \\ |
53534 | 1862 |
term_list name @{syntax fc_discs_sels}? |
53018 | 1863 |
; |
53534 | 1864 |
@{syntax_def fc_discs_sels}: name_list (name_list_list name_term_list_list? )? |
53018 | 1865 |
; |
53534 | 1866 |
@{syntax_def name_term}: (name ':' term) |
53018 | 1867 |
"} |
1868 |
||
53617 | 1869 |
% options: no_discs_sels rep_compat |
53028 | 1870 |
|
53617 | 1871 |
% X_list is as for BNF |
53028 | 1872 |
|
53542 | 1873 |
Section~\ref{ssec:datatype-generated-theorems} lists the generated theorems. |
53018 | 1874 |
*} |
52828 | 1875 |
|
52794 | 1876 |
|
53617 | 1877 |
(* |
52827 | 1878 |
section {* Standard ML Interface |
52805 | 1879 |
\label{sec:standard-ml-interface} *} |
52792 | 1880 |
|
52805 | 1881 |
text {* |
53623 | 1882 |
The package's programmatic interface. |
52805 | 1883 |
*} |
53617 | 1884 |
*) |
52794 | 1885 |
|
1886 |
||
53617 | 1887 |
(* |
52827 | 1888 |
section {* Interoperability |
52805 | 1889 |
\label{sec:interoperability} *} |
52794 | 1890 |
|
52805 | 1891 |
text {* |
53623 | 1892 |
The package's interaction with other Isabelle packages and tools, such as the |
1893 |
code generator and the counterexample generators. |
|
52805 | 1894 |
*} |
52794 | 1895 |
|
52824 | 1896 |
|
52828 | 1897 |
subsection {* Transfer and Lifting |
1898 |
\label{ssec:transfer-and-lifting} *} |
|
52794 | 1899 |
|
52824 | 1900 |
|
52828 | 1901 |
subsection {* Code Generator |
1902 |
\label{ssec:code-generator} *} |
|
52794 | 1903 |
|
52824 | 1904 |
|
52828 | 1905 |
subsection {* Quickcheck |
1906 |
\label{ssec:quickcheck} *} |
|
52794 | 1907 |
|
52824 | 1908 |
|
52828 | 1909 |
subsection {* Nitpick |
1910 |
\label{ssec:nitpick} *} |
|
52794 | 1911 |
|
52824 | 1912 |
|
52828 | 1913 |
subsection {* Nominal Isabelle |
1914 |
\label{ssec:nominal-isabelle} *} |
|
53617 | 1915 |
*) |
52794 | 1916 |
|
52805 | 1917 |
|
53617 | 1918 |
(* |
52827 | 1919 |
section {* Known Bugs and Limitations |
52805 | 1920 |
\label{sec:known-bugs-and-limitations} *} |
1921 |
||
1922 |
text {* |
|
53623 | 1923 |
Known open issues of the package. |
52805 | 1924 |
*} |
52794 | 1925 |
|
1926 |
text {* |
|
53617 | 1927 |
%* primcorec is unfinished |
1928 |
% |
|
1929 |
%* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting) |
|
1930 |
% |
|
1931 |
%* issues with HOL-Proofs? |
|
1932 |
% |
|
1933 |
%* partial documentation |
|
1934 |
% |
|
1935 |
%* no way to register "sum" and "prod" as (co)datatypes to enable N2M reduction for them |
|
1936 |
% (for @{command datatype_new_compat} and prim(co)rec) |
|
1937 |
% |
|
53619 | 1938 |
% * a fortiori, no way to register same type as both data- and codatatype |
53617 | 1939 |
% |
1940 |
%* no recursion through unused arguments (unlike with the old package) |
|
1941 |
% |
|
1942 |
%* in a locale, cannot use locally fixed types (because of limitation in typedef)? |
|
53619 | 1943 |
% |
1944 |
% *names of variables suboptimal |
|
52822 | 1945 |
*} |
1946 |
||
1947 |
||
52827 | 1948 |
section {* Acknowledgments |
52822 | 1949 |
\label{sec:acknowledgments} *} |
1950 |
||
1951 |
text {* |
|
53617 | 1952 |
Tobias Nipkow and Makarius Wenzel have encouraged us to implement the new |
1953 |
(co)datatype package. Andreas Lochbihler provided lots of comments on earlier |
|
1954 |
versions of the package, especially for the coinductive part. Brian Huffman |
|
1955 |
suggested major simplifications to the internal constructions, much of which has |
|
1956 |
yet to be implemented. Florian Haftmann and Christian Urban provided general |
|
1957 |
advice advice on Isabelle and package writing. Stefan Milius and Lutz Schr\"oder |
|
1958 |
suggested an elegant proof to eliminate one of the BNF assumptions. |
|
52794 | 1959 |
*} |
53617 | 1960 |
*) |
1961 |
||
52792 | 1962 |
|
1963 |
end |