author | blanchet |
Mon, 02 Dec 2013 20:31:54 +0100 | |
changeset 54624 | 36301c99ed26 |
parent 54621 | 82a78bc9fa0d |
child 54626 | 8a5e82425e55 |
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 |
|
54402 | 11 |
imports Setup "~~/src/HOL/Library/Simps_Case_Conv" |
52792 | 12 |
begin |
13 |
||
52828 | 14 |
section {* Introduction |
15 |
\label{sec:introduction} *} |
|
52792 | 16 |
|
17 |
text {* |
|
53028 | 18 |
The 2013 edition of Isabelle introduced a new definitional package for freely |
19 |
generated datatypes and codatatypes. The datatype support is similar to that |
|
20 |
provided by the earlier package due to Berghofer and Wenzel |
|
21 |
\cite{Berghofer-Wenzel:1999:TPHOL}, documented in the Isar reference manual |
|
53535 | 22 |
\cite{isabelle-isar-ref}; indeed, replacing the keyword \keyw{datatype} by |
53028 | 23 |
@{command datatype_new} is usually all that is needed to port existing theories |
24 |
to use the new package. |
|
52840 | 25 |
|
52841 | 26 |
Perhaps the main advantage of the new package is that it supports recursion |
53619 | 27 |
through a large class of non-datatypes, such as finite sets: |
52792 | 28 |
*} |
29 |
||
53644 | 30 |
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 | 31 |
|
32 |
text {* |
|
52794 | 33 |
\noindent |
53025 | 34 |
Another strong point is the support for local definitions: |
52792 | 35 |
*} |
36 |
||
52805 | 37 |
context linorder |
38 |
begin |
|
52841 | 39 |
datatype_new flag = Less | Eq | Greater |
52805 | 40 |
end |
52792 | 41 |
|
42 |
text {* |
|
52794 | 43 |
\noindent |
54187 | 44 |
Furthermore, the package provides a lot of convenience, including automatically |
45 |
generated discriminators, selectors, and relators as well as a wealth of |
|
46 |
properties about them. |
|
47 |
||
48 |
In addition to inductive datatypes, the new package supports coinductive |
|
49 |
datatypes, or \emph{codatatypes}, which allow infinite values. For example, the |
|
50 |
following command introduces the type of lazy lists, which comprises both finite |
|
51 |
and infinite values: |
|
52794 | 52 |
*} |
53 |
||
53623 | 54 |
(*<*) |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
55 |
locale early |
54072 | 56 |
locale late |
53623 | 57 |
(*>*) |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
58 |
codatatype (*<*)(in early) (*>*)'a llist = LNil | LCons 'a "'a llist" |
52794 | 59 |
|
60 |
text {* |
|
61 |
\noindent |
|
52840 | 62 |
Mixed inductive--coinductive recursion is possible via nesting. Compare the |
53028 | 63 |
following four Rose tree examples: |
52794 | 64 |
*} |
65 |
||
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
66 |
datatype_new (*<*)(in early) (*>*)'a tree\<^sub>f\<^sub>f = Node\<^sub>f\<^sub>f 'a "'a tree\<^sub>f\<^sub>f list" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
67 |
datatype_new (*<*)(in early) (*>*)'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i 'a "'a tree\<^sub>f\<^sub>i llist" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
68 |
codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>f = Node\<^sub>i\<^sub>f 'a "'a tree\<^sub>i\<^sub>f list" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
69 |
codatatype (*<*)(in early) (*>*)'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i 'a "'a tree\<^sub>i\<^sub>i llist" |
52792 | 70 |
|
52794 | 71 |
text {* |
54187 | 72 |
The first two tree types allow only paths of finite length, whereas the last two |
73 |
allow infinite paths. Orthogonally, the nodes in the first and third types have |
|
74 |
finitely many direct subtrees, whereas those of the second and fourth may have |
|
75 |
infinite branching. |
|
52840 | 76 |
|
52794 | 77 |
To use the package, it is necessary to import the @{theory BNF} theory, which |
53552 | 78 |
can be precompiled into the \texttt{HOL-BNF} image. The following commands show |
52806 | 79 |
how to launch jEdit/PIDE with the image loaded and how to build the image |
80 |
without launching jEdit: |
|
52794 | 81 |
*} |
82 |
||
83 |
text {* |
|
84 |
\noindent |
|
52806 | 85 |
\ \ \ \ \texttt{isabelle jedit -l HOL-BNF} \\ |
52827 | 86 |
\noindent |
52828 | 87 |
\hbox{}\ \ \ \ \texttt{isabelle build -b HOL-BNF} |
52794 | 88 |
*} |
89 |
||
90 |
text {* |
|
52805 | 91 |
The package, like its predecessor, fully adheres to the LCF philosophy |
92 |
\cite{mgordon79}: The characteristic theorems associated with the specified |
|
93 |
(co)datatypes are derived rather than introduced axiomatically.% |
|
53543 | 94 |
\footnote{If the @{text quick_and_dirty} option is enabled, some of the |
52841 | 95 |
internal constructions and most of the internal proof obligations are skipped.} |
52805 | 96 |
The package's metatheory is described in a pair of papers |
53552 | 97 |
\cite{traytel-et-al-2012,blanchette-et-al-wit}. The central notion is that of a |
98 |
\emph{bounded natural functor} (BNF)---a well-behaved type constructor for which |
|
99 |
nested (co)recursion is supported. |
|
52792 | 100 |
|
52794 | 101 |
This tutorial is organized as follows: |
102 |
||
52805 | 103 |
\begin{itemize} |
52822 | 104 |
\setlength{\itemsep}{0pt} |
105 |
||
52805 | 106 |
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,'' |
52822 | 107 |
describes how to specify datatypes using the @{command datatype_new} command. |
52805 | 108 |
|
53018 | 109 |
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive |
52805 | 110 |
Functions,'' describes how to specify recursive functions using |
53535 | 111 |
@{command primrec_new}, \keyw{fun}, and \keyw{function}. |
52805 | 112 |
|
113 |
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,'' |
|
53829 | 114 |
describes how to specify codatatypes using the @{command codatatype} command. |
52805 | 115 |
|
53018 | 116 |
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive |
52805 | 117 |
Functions,'' describes how to specify corecursive functions using the |
53826 | 118 |
@{command primcorec} and @{command primcorecursive} commands. |
52794 | 119 |
|
53018 | 120 |
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering |
53552 | 121 |
Bounded Natural Functors,'' explains how to use the @{command bnf} command |
122 |
to register arbitrary type constructors as BNFs. |
|
52805 | 123 |
|
53552 | 124 |
\item Section |
53617 | 125 |
\ref{sec:deriving-destructors-and-theorems-for-free-constructors}, |
126 |
``Deriving Destructors and Theorems for Free Constructors,'' explains how to |
|
53552 | 127 |
use the command @{command wrap_free_constructors} to derive destructor constants |
128 |
and theorems for freely generated types, as performed internally by @{command |
|
53829 | 129 |
datatype_new} and @{command codatatype}. |
52794 | 130 |
|
53617 | 131 |
%\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,'' |
132 |
%describes the package's programmatic interface. |
|
52794 | 133 |
|
53617 | 134 |
%\item Section \ref{sec:interoperability}, ``Interoperability,'' |
135 |
%is concerned with the packages' interaction with other Isabelle packages and |
|
136 |
%tools, such as the code generator and the counterexample generators. |
|
52805 | 137 |
|
53617 | 138 |
%\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and |
139 |
%Limitations,'' concludes with known open issues at the time of writing. |
|
52805 | 140 |
\end{itemize} |
52822 | 141 |
|
142 |
||
143 |
\newbox\boxA |
|
54185 | 144 |
\setbox\boxA=\hbox{\texttt{NOSPAM}} |
145 |
||
146 |
\newcommand\authoremaili{\texttt{blan{\color{white}NOSPAM}\kern-\wd\boxA{}chette@\allowbreak |
|
52822 | 147 |
in.\allowbreak tum.\allowbreak de}} |
54185 | 148 |
\newcommand\authoremailii{\texttt{lore{\color{white}NOSPAM}\kern-\wd\boxA{}nz.panny@\allowbreak |
53028 | 149 |
\allowbreak tum.\allowbreak de}} |
54185 | 150 |
\newcommand\authoremailiii{\texttt{pope{\color{white}NOSPAM}\kern-\wd\boxA{}scua@\allowbreak |
52822 | 151 |
in.\allowbreak tum.\allowbreak de}} |
54185 | 152 |
\newcommand\authoremailiv{\texttt{tray{\color{white}NOSPAM}\kern-\wd\boxA{}tel@\allowbreak |
52822 | 153 |
in.\allowbreak tum.\allowbreak de}} |
154 |
||
53028 | 155 |
The commands @{command datatype_new} and @{command primrec_new} are expected to |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
156 |
replace \keyw{datatype} and \keyw{primrec} in a future release. Authors of new |
53535 | 157 |
theories are encouraged to use the new commands, and maintainers of older |
53028 | 158 |
theories may want to consider upgrading. |
52841 | 159 |
|
160 |
Comments and bug reports concerning either the tool or this tutorial should be |
|
53028 | 161 |
directed to the authors at \authoremaili, \authoremailii, \authoremailiii, |
162 |
and \authoremailiv. |
|
52794 | 163 |
*} |
164 |
||
53491 | 165 |
|
52827 | 166 |
section {* Defining Datatypes |
52805 | 167 |
\label{sec:defining-datatypes} *} |
168 |
||
169 |
text {* |
|
53646 | 170 |
Datatypes can be specified using the @{command datatype_new} command. |
52805 | 171 |
*} |
52792 | 172 |
|
52824 | 173 |
|
53617 | 174 |
subsection {* Introductory Examples |
175 |
\label{ssec:datatype-introductory-examples} *} |
|
52794 | 176 |
|
53646 | 177 |
text {* |
178 |
Datatypes are illustrated through concrete examples featuring different flavors |
|
179 |
of recursion. More examples can be found in the directory |
|
54185 | 180 |
\verb|~~/src/HOL/|\allowbreak\verb|BNF/Examples|. |
53646 | 181 |
*} |
53621 | 182 |
|
183 |
subsubsection {* Nonrecursive Types |
|
184 |
\label{sssec:datatype-nonrecursive-types} *} |
|
52794 | 185 |
|
52805 | 186 |
text {* |
53028 | 187 |
Datatypes are introduced by specifying the desired names and argument types for |
53491 | 188 |
their constructors. \emph{Enumeration} types are the simplest form of datatype. |
53028 | 189 |
All their constructors are nullary: |
52805 | 190 |
*} |
191 |
||
52828 | 192 |
datatype_new trool = Truue | Faalse | Perhaaps |
52805 | 193 |
|
194 |
text {* |
|
53028 | 195 |
\noindent |
53491 | 196 |
Here, @{const Truue}, @{const Faalse}, and @{const Perhaaps} have the type @{typ trool}. |
53028 | 197 |
|
198 |
Polymorphic types are possible, such as the following option type, modeled after |
|
199 |
its homologue from the @{theory Option} theory: |
|
52805 | 200 |
*} |
201 |
||
53025 | 202 |
(*<*) |
203 |
hide_const None Some |
|
204 |
(*>*) |
|
205 |
datatype_new 'a option = None | Some 'a |
|
52805 | 206 |
|
207 |
text {* |
|
53028 | 208 |
\noindent |
53491 | 209 |
The constructors are @{text "None :: 'a option"} and |
210 |
@{text "Some :: 'a \<Rightarrow> 'a option"}. |
|
53028 | 211 |
|
212 |
The next example has three type parameters: |
|
52805 | 213 |
*} |
214 |
||
215 |
datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c |
|
216 |
||
53028 | 217 |
text {* |
218 |
\noindent |
|
219 |
The constructor is |
|
53491 | 220 |
@{text "Triple :: 'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> ('a, 'b, 'c) triple"}. |
53028 | 221 |
Unlike in Standard ML, curried constructors are supported. The uncurried variant |
222 |
is also possible: |
|
223 |
*} |
|
224 |
||
225 |
datatype_new ('a, 'b, 'c) triple\<^sub>u = Triple\<^sub>u "'a * 'b * 'c" |
|
226 |
||
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
227 |
text {* |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
228 |
\noindent |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
229 |
Occurrences of nonatomic types on the right-hand side of the equal sign must be |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
230 |
enclosed in double quotes, as is customary in Isabelle. |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
231 |
*} |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
232 |
|
52824 | 233 |
|
53621 | 234 |
subsubsection {* Simple Recursion |
235 |
\label{sssec:datatype-simple-recursion} *} |
|
52794 | 236 |
|
52805 | 237 |
text {* |
53491 | 238 |
Natural numbers are the simplest example of a recursive type: |
52805 | 239 |
*} |
240 |
||
53332 | 241 |
datatype_new nat = Zero | Suc nat |
52805 | 242 |
|
243 |
text {* |
|
53491 | 244 |
\noindent |
54187 | 245 |
Lists were shown in the introduction. Terminated lists are a variant that |
246 |
stores a value of type @{typ 'b} at the very end: |
|
52805 | 247 |
*} |
248 |
||
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
249 |
datatype_new (*<*)(in early) (*>*)('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist" |
52805 | 250 |
|
52824 | 251 |
|
53621 | 252 |
subsubsection {* Mutual Recursion |
253 |
\label{sssec:datatype-mutual-recursion} *} |
|
52794 | 254 |
|
52805 | 255 |
text {* |
53552 | 256 |
\emph{Mutually recursive} types are introduced simultaneously and may refer to |
257 |
each other. The example below introduces a pair of types for even and odd |
|
258 |
natural numbers: |
|
52805 | 259 |
*} |
260 |
||
53623 | 261 |
datatype_new even_nat = Even_Zero | Even_Suc odd_nat |
262 |
and odd_nat = Odd_Suc even_nat |
|
52805 | 263 |
|
264 |
text {* |
|
53491 | 265 |
\noindent |
266 |
Arithmetic expressions are defined via terms, terms via factors, and factors via |
|
267 |
expressions: |
|
52805 | 268 |
*} |
269 |
||
52841 | 270 |
datatype_new ('a, 'b) exp = |
271 |
Term "('a, 'b) trm" | Sum "('a, 'b) trm" "('a, 'b) exp" |
|
52805 | 272 |
and ('a, 'b) trm = |
52841 | 273 |
Factor "('a, 'b) fct" | Prod "('a, 'b) fct" "('a, 'b) trm" |
274 |
and ('a, 'b) fct = |
|
275 |
Const 'a | Var 'b | Expr "('a, 'b) exp" |
|
52805 | 276 |
|
52824 | 277 |
|
53621 | 278 |
subsubsection {* Nested Recursion |
279 |
\label{sssec:datatype-nested-recursion} *} |
|
52794 | 280 |
|
52805 | 281 |
text {* |
53491 | 282 |
\emph{Nested recursion} occurs when recursive occurrences of a type appear under |
283 |
a type constructor. The introduction showed some examples of trees with nesting |
|
53675 | 284 |
through lists. A more complex example, that reuses our @{type option} type, |
53491 | 285 |
follows: |
52805 | 286 |
*} |
287 |
||
52843 | 288 |
datatype_new 'a btree = |
53025 | 289 |
BNode 'a "'a btree option" "'a btree option" |
52805 | 290 |
|
291 |
text {* |
|
53491 | 292 |
\noindent |
293 |
Not all nestings are admissible. For example, this command will fail: |
|
52805 | 294 |
*} |
295 |
||
54187 | 296 |
datatype_new 'a wrong = W1 | W2 (*<*)'a |
53542 | 297 |
typ (*>*)"'a wrong \<Rightarrow> 'a" |
52806 | 298 |
|
299 |
text {* |
|
53491 | 300 |
\noindent |
301 |
The issue is that the function arrow @{text "\<Rightarrow>"} allows recursion |
|
302 |
only through its right-hand side. This issue is inherited by polymorphic |
|
303 |
datatypes defined in terms of~@{text "\<Rightarrow>"}: |
|
304 |
*} |
|
305 |
||
306 |
datatype_new ('a, 'b) fn = Fn "'a \<Rightarrow> 'b" |
|
54187 | 307 |
datatype_new 'a also_wrong = W1 | W2 (*<*)'a |
53542 | 308 |
typ (*>*)"('a also_wrong, 'a) fn" |
53491 | 309 |
|
310 |
text {* |
|
311 |
\noindent |
|
53621 | 312 |
This is legal: |
313 |
*} |
|
314 |
||
315 |
datatype_new 'a ftree = FTLeaf 'a | FTNode "'a \<Rightarrow> 'a ftree" |
|
316 |
||
317 |
text {* |
|
318 |
\noindent |
|
53491 | 319 |
In general, type constructors @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
320 |
allow recursion on a subset of their type arguments @{text 'a\<^sub>1}, \ldots, |
|
321 |
@{text 'a\<^sub>m}. These type arguments are called \emph{live}; the remaining |
|
322 |
type arguments are called \emph{dead}. In @{typ "'a \<Rightarrow> 'b"} and |
|
323 |
@{typ "('a, 'b) fn"}, the type variable @{typ 'a} is dead and @{typ 'b} is live. |
|
53552 | 324 |
|
53647 | 325 |
Type constructors must be registered as BNFs to have live arguments. This is |
326 |
done automatically for datatypes and codatatypes introduced by the @{command |
|
53829 | 327 |
datatype_new} and @{command codatatype} commands. |
53552 | 328 |
Section~\ref{sec:registering-bounded-natural-functors} explains how to register |
329 |
arbitrary type constructors as BNFs. |
|
54187 | 330 |
|
331 |
Here is another example that fails: |
|
52806 | 332 |
*} |
333 |
||
54187 | 334 |
datatype_new 'a pow_list = PNil 'a (*<*)'a |
335 |
datatype_new 'a pow_list' = PNil' 'a (*>*)| PCons "('a * 'a) pow_list" |
|
336 |
||
337 |
text {* |
|
338 |
\noindent |
|
339 |
This one features a different flavor of nesting, where the recursive call in the |
|
340 |
type specification occurs around (rather than inside) another type constructor. |
|
341 |
*} |
|
342 |
||
343 |
subsubsection {* Auxiliary Constants and Properties |
|
344 |
\label{sssec:datatype-auxiliary-constants-and-properties} *} |
|
52805 | 345 |
|
346 |
text {* |
|
53491 | 347 |
The @{command datatype_new} command introduces various constants in addition to |
53617 | 348 |
the constructors. With each datatype are associated set functions, a map |
349 |
function, a relator, discriminators, and selectors, all of which can be given |
|
54187 | 350 |
custom names. In the example below, the familiar names @{text null}, @{text hd}, |
351 |
@{text tl}, @{text set}, @{text map}, and @{text list_all2}, override the |
|
352 |
default names @{text is_Nil}, @{text un_Cons1}, @{text un_Cons2}, |
|
54491 | 353 |
@{text set_list}, @{text map_list}, and @{text rel_list}: |
52822 | 354 |
*} |
355 |
||
52841 | 356 |
(*<*) |
357 |
no_translations |
|
358 |
"[x, xs]" == "x # [xs]" |
|
359 |
"[x]" == "x # []" |
|
360 |
||
361 |
no_notation |
|
362 |
Nil ("[]") and |
|
363 |
Cons (infixr "#" 65) |
|
364 |
||
53543 | 365 |
hide_type list |
54494 | 366 |
hide_const Nil Cons hd tl set map list_all2 |
53025 | 367 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
368 |
context early begin |
52841 | 369 |
(*>*) |
53025 | 370 |
datatype_new (set: 'a) list (map: map rel: list_all2) = |
52822 | 371 |
null: Nil (defaults tl: Nil) |
53025 | 372 |
| Cons (hd: 'a) (tl: "'a list") |
52822 | 373 |
|
374 |
text {* |
|
375 |
\noindent |
|
54187 | 376 |
|
377 |
\begin{tabular}{@ {}ll@ {}} |
|
378 |
Constructors: & |
|
379 |
@{text "Nil \<Colon> 'a list"} \\ |
|
380 |
& |
|
381 |
@{text "Cons \<Colon> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"} \\ |
|
382 |
Discriminator: & |
|
383 |
@{text "null \<Colon> 'a list \<Rightarrow> bool"} \\ |
|
384 |
Selectors: & |
|
385 |
@{text "hd \<Colon> 'a list \<Rightarrow> 'a"} \\ |
|
386 |
& |
|
387 |
@{text "tl \<Colon> 'a list \<Rightarrow> 'a list"} \\ |
|
388 |
Set function: & |
|
389 |
@{text "set \<Colon> 'a list \<Rightarrow> 'a set"} \\ |
|
390 |
Map function: & |
|
391 |
@{text "map \<Colon> ('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"} \\ |
|
392 |
Relator: & |
|
393 |
@{text "list_all2 \<Colon> ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"} |
|
394 |
\end{tabular} |
|
395 |
||
396 |
The discriminator @{const null} and the selectors @{const hd} and @{const tl} |
|
397 |
are characterized as follows: |
|
52822 | 398 |
% |
53025 | 399 |
\[@{thm list.collapse(1)[of xs, no_vars]} |
400 |
\qquad @{thm list.collapse(2)[of xs, no_vars]}\] |
|
52822 | 401 |
% |
54187 | 402 |
For two-constructor datatypes, a single discriminator constant is sufficient. |
403 |
The discriminator associated with @{const Cons} is simply |
|
53491 | 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 |
|
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
418 |
The usual mixfix syntax annotations are available for both types and |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
419 |
constructors. For 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 |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
435 |
Incidentally, this is how the traditional syntax 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 {* |
53829 | 455 |
\begin{matharray}{rcl} |
456 |
@{command_def "datatype_new"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
457 |
\end{matharray} |
|
52822 | 458 |
|
52824 | 459 |
@{rail " |
53829 | 460 |
@@{command datatype_new} target? @{syntax dt_options}? \\ |
52824 | 461 |
(@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and') |
52828 | 462 |
; |
53623 | 463 |
@{syntax_def dt_options}: '(' (('no_discs_sels' | 'rep_compat') + ',') ')' |
52824 | 464 |
"} |
465 |
||
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
466 |
The syntactic entity \synt{target} can be used to specify a local |
53534 | 467 |
context---e.g., @{text "(in linorder)"}. It is documented in the Isar reference |
468 |
manual \cite{isabelle-isar-ref}. |
|
469 |
% |
|
470 |
The optional target is optionally followed by datatype-specific options: |
|
52822 | 471 |
|
52824 | 472 |
\begin{itemize} |
473 |
\setlength{\itemsep}{0pt} |
|
474 |
||
475 |
\item |
|
53623 | 476 |
The @{text "no_discs_sels"} option indicates that no discriminators or selectors |
53543 | 477 |
should be generated. |
52822 | 478 |
|
52824 | 479 |
\item |
53644 | 480 |
The @{text "rep_compat"} option indicates that the generated names should |
481 |
contain optional (and normally not displayed) ``@{text "new."}'' components to |
|
482 |
prevent clashes with a later call to \keyw{rep\_datatype}. See |
|
52824 | 483 |
Section~\ref{ssec:datatype-compatibility-issues} for details. |
484 |
\end{itemize} |
|
52822 | 485 |
|
52827 | 486 |
The left-hand sides of the datatype equations specify the name of the type to |
53534 | 487 |
define, its type parameters, and additional information: |
52822 | 488 |
|
52824 | 489 |
@{rail " |
53534 | 490 |
@{syntax_def dt_name}: @{syntax tyargs}? name @{syntax map_rel}? mixfix? |
52824 | 491 |
; |
53534 | 492 |
@{syntax_def tyargs}: typefree | '(' ((name ':')? typefree + ',') ')' |
52824 | 493 |
; |
53534 | 494 |
@{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')' |
52824 | 495 |
"} |
52822 | 496 |
|
52827 | 497 |
\noindent |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
498 |
The syntactic entity \synt{name} denotes an identifier, \synt{typefree} |
53534 | 499 |
denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and \synt{mixfix} |
500 |
denotes the usual parenthesized mixfix notation. They are documented in the Isar |
|
501 |
reference manual \cite{isabelle-isar-ref}. |
|
52822 | 502 |
|
52827 | 503 |
The optional names preceding the type variables allow to override the default |
54491 | 504 |
names of the set functions (@{text set1_t}, \ldots, @{text setM_t}). |
53647 | 505 |
Inside a mutually recursive specification, all defined datatypes must |
506 |
mention exactly the same type variables in the same order. |
|
52822 | 507 |
|
52824 | 508 |
@{rail " |
53534 | 509 |
@{syntax_def ctor}: (name ':')? name (@{syntax ctor_arg} * ) \\ |
510 |
@{syntax dt_sel_defaults}? mixfix? |
|
52824 | 511 |
"} |
512 |
||
53535 | 513 |
\medskip |
514 |
||
52827 | 515 |
\noindent |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
516 |
The main constituents of a constructor specification are the name of the |
52827 | 517 |
constructor and the list of its argument types. An optional discriminator name |
53554 | 518 |
can be supplied at the front to override the default name |
519 |
(@{text t.is_C\<^sub>j}). |
|
52822 | 520 |
|
52824 | 521 |
@{rail " |
53534 | 522 |
@{syntax_def ctor_arg}: type | '(' name ':' type ')' |
52827 | 523 |
"} |
524 |
||
53535 | 525 |
\medskip |
526 |
||
52827 | 527 |
\noindent |
528 |
In addition to the type of a constructor argument, it is possible to specify a |
|
529 |
name for the corresponding selector to override the default name |
|
53554 | 530 |
(@{text un_C\<^sub>ji}). The same selector names can be reused for several |
531 |
constructors as long as they share the same type. |
|
52827 | 532 |
|
533 |
@{rail " |
|
53621 | 534 |
@{syntax_def dt_sel_defaults}: '(' 'defaults' (name ':' term +) ')' |
52824 | 535 |
"} |
52827 | 536 |
|
537 |
\noindent |
|
538 |
Given a constructor |
|
539 |
@{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"}, |
|
540 |
default values can be specified for any selector |
|
541 |
@{text "un_D \<Colon> \<sigma> \<Rightarrow> \<tau>"} |
|
53535 | 542 |
associated with other constructors. The specified default value must be of type |
52828 | 543 |
@{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"} |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
544 |
(i.e., it may depend on @{text C}'s arguments). |
52822 | 545 |
*} |
546 |
||
53617 | 547 |
|
53621 | 548 |
subsubsection {* \keyw{datatype\_new\_compat} |
549 |
\label{sssec:datatype-new-compat} *} |
|
53617 | 550 |
|
551 |
text {* |
|
53829 | 552 |
\begin{matharray}{rcl} |
553 |
@{command_def "datatype_new_compat"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
554 |
\end{matharray} |
|
555 |
||
556 |
@{rail " |
|
557 |
@@{command datatype_new_compat} names |
|
558 |
"} |
|
559 |
||
560 |
\noindent |
|
53621 | 561 |
The old datatype package provides some functionality that is not yet replicated |
562 |
in the new package: |
|
563 |
||
564 |
\begin{itemize} |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
565 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
566 |
|
53621 | 567 |
\item It is integrated with \keyw{fun} and \keyw{function} |
568 |
\cite{isabelle-function}, Nitpick \cite{isabelle-nitpick}, Quickcheck, |
|
569 |
and other packages. |
|
570 |
||
571 |
\item It is extended by various add-ons, notably to produce instances of the |
|
572 |
@{const size} function. |
|
573 |
\end{itemize} |
|
574 |
||
575 |
\noindent |
|
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
576 |
New-style datatypes can in most cases be registered as old-style datatypes using |
53829 | 577 |
@{command datatype_new_compat}. The \textit{names} argument is a space-separated |
578 |
list of type names that are mutually recursive. For example: |
|
53621 | 579 |
*} |
580 |
||
53623 | 581 |
datatype_new_compat even_nat odd_nat |
53621 | 582 |
|
583 |
text {* \blankline *} |
|
584 |
||
53623 | 585 |
thm even_nat_odd_nat.size |
53621 | 586 |
|
587 |
text {* \blankline *} |
|
588 |
||
53623 | 589 |
ML {* Datatype_Data.get_info @{theory} @{type_name even_nat} *} |
53621 | 590 |
|
591 |
text {* |
|
53748 | 592 |
A few remarks concern nested recursive datatypes only: |
593 |
||
594 |
\begin{itemize} |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
595 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
596 |
|
53748 | 597 |
\item The old-style, nested-as-mutual induction rule, iterator theorems, and |
598 |
recursor theorems are generated under their usual names but with ``@{text |
|
599 |
"compat_"}'' prefixed (e.g., @{text compat_tree.induct}). |
|
600 |
||
601 |
\item All types through which recursion takes place must be new-style datatypes |
|
602 |
or the function type. In principle, it should be possible to support old-style |
|
603 |
datatypes as well, but the command does not support this yet (and there is |
|
604 |
currently no way to register old-style datatypes as new-style datatypes). |
|
54184 | 605 |
|
606 |
\item The recursor produced for types that recurse through functions has a |
|
607 |
different signature than with the old package. This makes it impossible to use |
|
608 |
the old \keyw{primrec} command. |
|
53748 | 609 |
\end{itemize} |
610 |
||
611 |
An alternative to @{command datatype_new_compat} is to use the old package's |
|
612 |
\keyw{rep\_datatype} command. The associated proof obligations must then be |
|
613 |
discharged manually. |
|
53617 | 614 |
*} |
615 |
||
616 |
||
617 |
subsection {* Generated Constants |
|
618 |
\label{ssec:datatype-generated-constants} *} |
|
619 |
||
620 |
text {* |
|
53623 | 621 |
Given a datatype @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
53617 | 622 |
with $m > 0$ live type variables and $n$ constructors |
623 |
@{text "t.C\<^sub>1"}, \ldots, @{text "t.C\<^sub>n"}, the |
|
624 |
following auxiliary constants are introduced: |
|
625 |
||
626 |
\begin{itemize} |
|
627 |
\setlength{\itemsep}{0pt} |
|
628 |
||
54494 | 629 |
\item \relax{Case combinator}: @{text t.case_t} (rendered using the familiar |
53617 | 630 |
@{text case}--@{text of} syntax) |
631 |
||
632 |
\item \relax{Discriminators}: @{text "t.is_C\<^sub>1"}, \ldots, |
|
633 |
@{text "t.is_C\<^sub>n"} |
|
634 |
||
635 |
\item \relax{Selectors}: |
|
636 |
@{text t.un_C\<^sub>11}$, \ldots, @{text t.un_C\<^sub>1k\<^sub>1}, \\ |
|
637 |
\phantom{\relax{Selectors:}} \quad\vdots \\ |
|
638 |
\phantom{\relax{Selectors:}} @{text t.un_C\<^sub>n1}$, \ldots, @{text t.un_C\<^sub>nk\<^sub>n}. |
|
639 |
||
640 |
\item \relax{Set functions} (or \relax{natural transformations}): |
|
54491 | 641 |
@{text set1_t}, \ldots, @{text t.setm_t} |
642 |
||
643 |
\item \relax{Map function} (or \relax{functorial action}): @{text t.map_t} |
|
644 |
||
645 |
\item \relax{Relator}: @{text t.rel_t} |
|
646 |
||
54494 | 647 |
\item \relax{Iterator}: @{text t.fold_t} |
648 |
||
649 |
\item \relax{Recursor}: @{text t.rec_t} |
|
53617 | 650 |
|
651 |
\end{itemize} |
|
652 |
||
653 |
\noindent |
|
654 |
The case combinator, discriminators, and selectors are collectively called |
|
655 |
\emph{destructors}. The prefix ``@{text "t."}'' is an optional component of the |
|
54491 | 656 |
names and is normally hidden. |
53617 | 657 |
*} |
658 |
||
659 |
||
52840 | 660 |
subsection {* Generated Theorems |
661 |
\label{ssec:datatype-generated-theorems} *} |
|
52828 | 662 |
|
663 |
text {* |
|
53544 | 664 |
The characteristic theorems generated by @{command datatype_new} are grouped in |
53623 | 665 |
three broad categories: |
53535 | 666 |
|
53543 | 667 |
\begin{itemize} |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
668 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
669 |
|
53543 | 670 |
\item The \emph{free constructor theorems} are properties about the constructors |
671 |
and destructors that can be derived for any freely generated type. Internally, |
|
53542 | 672 |
the derivation is performed by @{command wrap_free_constructors}. |
53535 | 673 |
|
53552 | 674 |
\item The \emph{functorial theorems} are properties of datatypes related to |
675 |
their BNF nature. |
|
676 |
||
677 |
\item The \emph{inductive theorems} are properties of datatypes related to |
|
53544 | 678 |
their inductive nature. |
53552 | 679 |
|
53543 | 680 |
\end{itemize} |
53535 | 681 |
|
682 |
\noindent |
|
53542 | 683 |
The full list of named theorems can be obtained as usual by entering the |
53543 | 684 |
command \keyw{print\_theorems} immediately after the datatype definition. |
53542 | 685 |
This list normally excludes low-level theorems that reveal internal |
53552 | 686 |
constructions. To make these accessible, add the line |
53542 | 687 |
*} |
53535 | 688 |
|
53542 | 689 |
declare [[bnf_note_all]] |
690 |
(*<*) |
|
691 |
declare [[bnf_note_all = false]] |
|
692 |
(*>*) |
|
53535 | 693 |
|
53552 | 694 |
text {* |
695 |
\noindent |
|
696 |
to the top of the theory file. |
|
697 |
*} |
|
53535 | 698 |
|
53621 | 699 |
subsubsection {* Free Constructor Theorems |
700 |
\label{sssec:free-constructor-theorems} *} |
|
53535 | 701 |
|
53543 | 702 |
(*<*) |
53837 | 703 |
consts nonnull :: 'a |
53543 | 704 |
(*>*) |
705 |
||
53535 | 706 |
text {* |
54621 | 707 |
The free constructor theorems are partitioned in three subgroups. The first |
708 |
subgroup of properties is concerned with the constructors. They are listed below |
|
709 |
for @{typ "'a list"}: |
|
53543 | 710 |
|
53552 | 711 |
\begin{indentblock} |
53543 | 712 |
\begin{description} |
53544 | 713 |
|
53642 | 714 |
\item[@{text "t."}\hthm{inject} @{text "[iff, induct_simp]"}\rm:] ~ \\ |
53544 | 715 |
@{thm list.inject[no_vars]} |
716 |
||
53642 | 717 |
\item[@{text "t."}\hthm{distinct} @{text "[simp, induct_simp]"}\rm:] ~ \\ |
53543 | 718 |
@{thm list.distinct(1)[no_vars]} \\ |
719 |
@{thm list.distinct(2)[no_vars]} |
|
720 |
||
53642 | 721 |
\item[@{text "t."}\hthm{exhaust} @{text "[cases t, case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53543 | 722 |
@{thm list.exhaust[no_vars]} |
723 |
||
53642 | 724 |
\item[@{text "t."}\hthm{nchotomy}\rm:] ~ \\ |
53543 | 725 |
@{thm list.nchotomy[no_vars]} |
726 |
||
727 |
\end{description} |
|
53552 | 728 |
\end{indentblock} |
53543 | 729 |
|
730 |
\noindent |
|
53621 | 731 |
In addition, these nameless theorems are registered as safe elimination rules: |
732 |
||
733 |
\begin{indentblock} |
|
734 |
\begin{description} |
|
735 |
||
54386 | 736 |
\item[@{text "t."}\hthm{distinct {\upshape[}THEN notE}@{text ", elim!"}\hthm{\upshape]}\rm:] ~ \\ |
53621 | 737 |
@{thm list.distinct(1)[THEN notE, elim!, no_vars]} \\ |
738 |
@{thm list.distinct(2)[THEN notE, elim!, no_vars]} |
|
739 |
||
740 |
\end{description} |
|
741 |
\end{indentblock} |
|
742 |
||
743 |
\noindent |
|
53543 | 744 |
The next subgroup is concerned with the case combinator: |
745 |
||
53552 | 746 |
\begin{indentblock} |
53543 | 747 |
\begin{description} |
53544 | 748 |
|
53798 | 749 |
\item[@{text "t."}\hthm{case} @{text "[simp, code]"}\rm:] ~ \\ |
53543 | 750 |
@{thm list.case(1)[no_vars]} \\ |
751 |
@{thm list.case(2)[no_vars]} |
|
752 |
||
53642 | 753 |
\item[@{text "t."}\hthm{case\_cong}\rm:] ~ \\ |
53543 | 754 |
@{thm list.case_cong[no_vars]} |
755 |
||
53642 | 756 |
\item[@{text "t."}\hthm{weak\_case\_cong} @{text "[cong]"}\rm:] ~ \\ |
53543 | 757 |
@{thm list.weak_case_cong[no_vars]} |
758 |
||
53642 | 759 |
\item[@{text "t."}\hthm{split}\rm:] ~ \\ |
53543 | 760 |
@{thm list.split[no_vars]} |
761 |
||
53642 | 762 |
\item[@{text "t."}\hthm{split\_asm}\rm:] ~ \\ |
53543 | 763 |
@{thm list.split_asm[no_vars]} |
764 |
||
53544 | 765 |
\item[@{text "t."}\hthm{splits} = @{text "split split_asm"}] |
53543 | 766 |
|
767 |
\end{description} |
|
53552 | 768 |
\end{indentblock} |
53543 | 769 |
|
770 |
\noindent |
|
54621 | 771 |
The third subgroup revolves around discriminators and selectors: |
53543 | 772 |
|
53552 | 773 |
\begin{indentblock} |
53543 | 774 |
\begin{description} |
53544 | 775 |
|
53694 | 776 |
\item[@{text "t."}\hthm{disc} @{text "[simp]"}\rm:] ~ \\ |
777 |
@{thm list.disc(1)[no_vars]} \\ |
|
778 |
@{thm list.disc(2)[no_vars]} |
|
779 |
||
53703 | 780 |
\item[@{text "t."}\hthm{discI}\rm:] ~ \\ |
781 |
@{thm list.discI(1)[no_vars]} \\ |
|
782 |
@{thm list.discI(2)[no_vars]} |
|
783 |
||
53805 | 784 |
\item[@{text "t."}\hthm{sel} @{text "[simp, code]"}\rm:] ~ \\ |
53694 | 785 |
@{thm list.sel(1)[no_vars]} \\ |
786 |
@{thm list.sel(2)[no_vars]} |
|
53543 | 787 |
|
53642 | 788 |
\item[@{text "t."}\hthm{collapse} @{text "[simp]"}\rm:] ~ \\ |
53543 | 789 |
@{thm list.collapse(1)[no_vars]} \\ |
790 |
@{thm list.collapse(2)[no_vars]} |
|
791 |
||
53837 | 792 |
\item[@{text "t."}\hthm{disc\_exclude} @{text "[dest]"}\rm:] ~ \\ |
53543 | 793 |
These properties are missing for @{typ "'a list"} because there is only one |
794 |
proper discriminator. Had the datatype been introduced with a second |
|
53837 | 795 |
discriminator called @{const nonnull}, they would have read thusly: \\[\jot] |
796 |
@{prop "null list \<Longrightarrow> \<not> nonnull list"} \\ |
|
797 |
@{prop "nonnull list \<Longrightarrow> \<not> null list"} |
|
53543 | 798 |
|
53642 | 799 |
\item[@{text "t."}\hthm{disc\_exhaust} @{text "[case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53543 | 800 |
@{thm list.disc_exhaust[no_vars]} |
801 |
||
53916 | 802 |
\item[@{text "t."}\hthm{sel\_exhaust} @{text "[case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
803 |
@{thm list.sel_exhaust[no_vars]} |
|
804 |
||
53642 | 805 |
\item[@{text "t."}\hthm{expand}\rm:] ~ \\ |
53543 | 806 |
@{thm list.expand[no_vars]} |
807 |
||
53917 | 808 |
\item[@{text "t."}\hthm{sel\_split}\rm:] ~ \\ |
809 |
@{thm list.sel_split[no_vars]} |
|
810 |
||
811 |
\item[@{text "t."}\hthm{sel\_split\_asm}\rm:] ~ \\ |
|
812 |
@{thm list.sel_split_asm[no_vars]} |
|
813 |
||
54491 | 814 |
\item[@{text "t."}\hthm{case\_eq\_if}\rm:] ~ \\ |
815 |
@{thm list.case_eq_if[no_vars]} |
|
53543 | 816 |
|
817 |
\end{description} |
|
53552 | 818 |
\end{indentblock} |
54152 | 819 |
|
820 |
\noindent |
|
821 |
In addition, equational versions of @{text t.disc} are registered with the @{text "[code]"} |
|
822 |
attribute. |
|
53552 | 823 |
*} |
824 |
||
825 |
||
53621 | 826 |
subsubsection {* Functorial Theorems |
827 |
\label{sssec:functorial-theorems} *} |
|
53552 | 828 |
|
829 |
text {* |
|
54621 | 830 |
The functorial theorems are partitioned in two subgroups. The first subgroup |
831 |
consists of properties involving the constructors and either a set function, the |
|
832 |
map function, or the relator: |
|
53552 | 833 |
|
834 |
\begin{indentblock} |
|
835 |
\begin{description} |
|
836 |
||
53798 | 837 |
\item[@{text "t."}\hthm{set} @{text "[simp, code]"}\rm:] ~ \\ |
53694 | 838 |
@{thm list.set(1)[no_vars]} \\ |
839 |
@{thm list.set(2)[no_vars]} |
|
53552 | 840 |
|
53798 | 841 |
\item[@{text "t."}\hthm{map} @{text "[simp, code]"}\rm:] ~ \\ |
53552 | 842 |
@{thm list.map(1)[no_vars]} \\ |
843 |
@{thm list.map(2)[no_vars]} |
|
844 |
||
54146 | 845 |
\item[@{text "t."}\hthm{rel\_inject} @{text "[simp]"}\rm:] ~ \\ |
53552 | 846 |
@{thm list.rel_inject(1)[no_vars]} \\ |
847 |
@{thm list.rel_inject(2)[no_vars]} |
|
848 |
||
54146 | 849 |
\item[@{text "t."}\hthm{rel\_distinct} @{text "[simp]"}\rm:] ~ \\ |
53552 | 850 |
@{thm list.rel_distinct(1)[no_vars]} \\ |
851 |
@{thm list.rel_distinct(2)[no_vars]} |
|
852 |
||
853 |
\end{description} |
|
854 |
\end{indentblock} |
|
54146 | 855 |
|
856 |
\noindent |
|
857 |
In addition, equational versions of @{text t.rel_inject} and @{text |
|
858 |
rel_distinct} are registered with the @{text "[code]"} attribute. |
|
54621 | 859 |
|
860 |
The second subgroup consists of more abstract properties of the set functions, |
|
861 |
the map function, and the relator: |
|
862 |
||
863 |
\begin{indentblock} |
|
864 |
\begin{description} |
|
865 |
||
866 |
\item[@{text "t."}\hthm{map\_comp}\rm:] ~ \\ |
|
867 |
@{thm list.map_cong0[no_vars]} |
|
868 |
||
54624
36301c99ed26
revert making 'map_cong' a 'cong' -- it breaks too many proofs in the AFP
blanchet
parents:
54621
diff
changeset
|
869 |
\item[@{text "t."}\hthm{map\_cong} @{text "[fundef_cong]"}\rm:] ~ \\ |
54621 | 870 |
@{thm list.map_cong[no_vars]} |
871 |
||
872 |
\item[@{text "t."}\hthm{map\_id}\rm:] ~ \\ |
|
873 |
@{thm list.map_id[no_vars]} |
|
874 |
||
875 |
\item[@{text "t."}\hthm{rel\_compp}\rm:] ~ \\ |
|
876 |
@{thm list.rel_compp[no_vars]} |
|
877 |
||
878 |
\item[@{text "t."}\hthm{rel\_conversep}\rm:] ~ \\ |
|
879 |
@{thm list.rel_conversep[no_vars]} |
|
880 |
||
881 |
\item[@{text "t."}\hthm{rel\_eq}\rm:] ~ \\ |
|
882 |
@{thm list.rel_eq[no_vars]} |
|
883 |
||
884 |
\item[@{text "t."}\hthm{rel\_flip}\rm:] ~ \\ |
|
885 |
@{thm list.rel_flip[no_vars]} |
|
886 |
||
887 |
\item[@{text "t."}\hthm{rel\_mono}\rm:] ~ \\ |
|
888 |
@{thm list.rel_mono[no_vars]} |
|
889 |
||
890 |
\item[@{text "t."}\hthm{set\_map}\rm:] ~ \\ |
|
891 |
@{thm list.set_map[no_vars]} |
|
892 |
||
893 |
\end{description} |
|
894 |
\end{indentblock} |
|
53535 | 895 |
*} |
896 |
||
897 |
||
53621 | 898 |
subsubsection {* Inductive Theorems |
899 |
\label{sssec:inductive-theorems} *} |
|
53535 | 900 |
|
901 |
text {* |
|
53623 | 902 |
The inductive theorems are as follows: |
53544 | 903 |
|
53552 | 904 |
\begin{indentblock} |
53544 | 905 |
\begin{description} |
906 |
||
53642 | 907 |
\item[@{text "t."}\hthm{induct} @{text "[induct t, case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53544 | 908 |
@{thm list.induct[no_vars]} |
909 |
||
53642 | 910 |
\item[@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{induct} @{text "[case_names C\<^sub>1 \<dots> C\<^sub>n]"}\rm:] ~ \\ |
53544 | 911 |
Given $m > 1$ mutually recursive datatypes, this induction rule can be used to |
912 |
prove $m$ properties simultaneously. |
|
52828 | 913 |
|
53798 | 914 |
\item[@{text "t."}\hthm{fold} @{text "[simp, code]"}\rm:] ~ \\ |
53544 | 915 |
@{thm list.fold(1)[no_vars]} \\ |
916 |
@{thm list.fold(2)[no_vars]} |
|
917 |
||
53798 | 918 |
\item[@{text "t."}\hthm{rec} @{text "[simp, code]"}\rm:] ~ \\ |
53544 | 919 |
@{thm list.rec(1)[no_vars]} \\ |
920 |
@{thm list.rec(2)[no_vars]} |
|
921 |
||
922 |
\end{description} |
|
53552 | 923 |
\end{indentblock} |
53544 | 924 |
|
925 |
\noindent |
|
926 |
For convenience, @{command datatype_new} also provides the following collection: |
|
927 |
||
53552 | 928 |
\begin{indentblock} |
53544 | 929 |
\begin{description} |
930 |
||
931 |
\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}] ~ \\ |
|
53694 | 932 |
@{text t.rel_distinct} @{text t.set} |
53544 | 933 |
|
934 |
\end{description} |
|
53552 | 935 |
\end{indentblock} |
52828 | 936 |
*} |
937 |
||
52794 | 938 |
|
52827 | 939 |
subsection {* Compatibility Issues |
52824 | 940 |
\label{ssec:datatype-compatibility-issues} *} |
52794 | 941 |
|
52828 | 942 |
text {* |
53997 | 943 |
The command @{command datatype_new} has been designed to be highly compatible |
944 |
with the old \keyw{datatype}, to ease migration. There are nonetheless a few |
|
53647 | 945 |
incompatibilities that may arise when porting to the new package: |
946 |
||
947 |
\begin{itemize} |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
948 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
949 |
|
53647 | 950 |
\item \emph{The Standard ML interfaces are different.} Tools and extensions |
951 |
written to call the old ML interfaces will need to be adapted to the new |
|
952 |
interfaces. Little has been done so far in this direction. Whenever possible, it |
|
953 |
is recommended to use @{command datatype_new_compat} or \keyw{rep\_datatype} |
|
954 |
to register new-style datatypes as old-style datatypes. |
|
955 |
||
54537 | 956 |
\item \emph{The constants @{text t_case} and @{text t_rec} are now called |
957 |
@{text case_t} and @{text rec_t}.} |
|
958 |
||
959 |
\item \emph{The recursor @{text rec_t} has a different signature for nested |
|
54185 | 960 |
recursive datatypes.} In the old package, nested recursion through non-functions |
961 |
was internally reduced to mutual recursion. This reduction was visible in the |
|
962 |
type of the recursor, used by \keyw{primrec}. Recursion through functions was |
|
963 |
handled specially. In the new package, nested recursion (for functions and |
|
964 |
non-functions) is handled in a more modular fashion. The old-style recursor can |
|
965 |
be generated on demand using @{command primrec_new}, as explained in |
|
53647 | 966 |
Section~\ref{sssec:primrec-nested-as-mutual-recursion}, if the recursion is via |
967 |
new-style datatypes. |
|
968 |
||
54287 | 969 |
\item \emph{Accordingly, the induction rule is different for nested recursive |
970 |
datatypes.} Again, the old-style induction rule can be generated on demand using |
|
971 |
@{command primrec_new}, as explained in |
|
53647 | 972 |
Section~\ref{sssec:primrec-nested-as-mutual-recursion}, if the recursion is via |
973 |
new-style datatypes. |
|
52828 | 974 |
|
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
975 |
\item \emph{The internal constructions are completely different.} Proof texts |
53647 | 976 |
that unfold the definition of constants introduced by \keyw{datatype} will be |
977 |
difficult to port. |
|
978 |
||
979 |
\item \emph{A few theorems have different names.} |
|
53997 | 980 |
The properties @{text t.cases} and @{text t.recs} have been renamed |
53647 | 981 |
@{text t.case} and @{text t.rec}. For non-mutually recursive datatypes, |
982 |
@{text t.inducts} is available as @{text t.induct}. |
|
983 |
For $m > 1$ mutually recursive datatypes, |
|
53997 | 984 |
@{text "t\<^sub>1_\<dots>_t\<^sub>m.inducts(i)"} has been renamed |
53647 | 985 |
@{text "t\<^sub>i.induct"}. |
986 |
||
987 |
\item \emph{The @{text t.simps} collection has been extended.} |
|
988 |
Previously available theorems are available at the same index. |
|
989 |
||
990 |
\item \emph{Variables in generated properties have different names.} This is |
|
991 |
rarely an issue, except in proof texts that refer to variable names in the |
|
992 |
@{text "[where \<dots>]"} attribute. The solution is to use the more robust |
|
993 |
@{text "[of \<dots>]"} syntax. |
|
994 |
\end{itemize} |
|
995 |
||
996 |
In the other direction, there is currently no way to register old-style |
|
997 |
datatypes as new-style datatypes. If the goal is to define new-style datatypes |
|
998 |
with nested recursion through old-style datatypes, the old-style |
|
999 |
datatypes can be registered as a BNF |
|
1000 |
(Section~\ref{sec:registering-bounded-natural-functors}). If the goal is |
|
1001 |
to derive discriminators and selectors, this can be achieved using @{command |
|
1002 |
wrap_free_constructors} |
|
1003 |
(Section~\ref{sec:deriving-destructors-and-theorems-for-free-constructors}). |
|
52828 | 1004 |
*} |
1005 |
||
52792 | 1006 |
|
52827 | 1007 |
section {* Defining Recursive Functions |
52805 | 1008 |
\label{sec:defining-recursive-functions} *} |
1009 |
||
1010 |
text {* |
|
54183 | 1011 |
Recursive functions over datatypes can be specified using the @{command |
1012 |
primrec_new} command, which supports primitive recursion, or using the more |
|
1013 |
general \keyw{fun} and \keyw{function} commands. Here, the focus is on @{command |
|
53644 | 1014 |
primrec_new}; the other two commands are described in a separate tutorial |
53646 | 1015 |
\cite{isabelle-function}. |
52828 | 1016 |
|
53621 | 1017 |
%%% TODO: partial_function |
52805 | 1018 |
*} |
52792 | 1019 |
|
52805 | 1020 |
|
53617 | 1021 |
subsection {* Introductory Examples |
1022 |
\label{ssec:primrec-introductory-examples} *} |
|
52828 | 1023 |
|
53646 | 1024 |
text {* |
1025 |
Primitive recursion is illustrated through concrete examples based on the |
|
1026 |
datatypes defined in Section~\ref{ssec:datatype-introductory-examples}. More |
|
1027 |
examples can be found in the directory \verb|~~/src/HOL/BNF/Examples|. |
|
1028 |
*} |
|
1029 |
||
53621 | 1030 |
|
1031 |
subsubsection {* Nonrecursive Types |
|
1032 |
\label{sssec:primrec-nonrecursive-types} *} |
|
52828 | 1033 |
|
52841 | 1034 |
text {* |
53621 | 1035 |
Primitive recursion removes one layer of constructors on the left-hand side in |
1036 |
each equation. For example: |
|
52841 | 1037 |
*} |
1038 |
||
1039 |
primrec_new bool_of_trool :: "trool \<Rightarrow> bool" where |
|
53621 | 1040 |
"bool_of_trool Faalse \<longleftrightarrow> False" | |
1041 |
"bool_of_trool Truue \<longleftrightarrow> True" |
|
52841 | 1042 |
|
53621 | 1043 |
text {* \blankline *} |
52841 | 1044 |
|
53025 | 1045 |
primrec_new the_list :: "'a option \<Rightarrow> 'a list" where |
1046 |
"the_list None = []" | |
|
1047 |
"the_list (Some a) = [a]" |
|
52841 | 1048 |
|
53621 | 1049 |
text {* \blankline *} |
1050 |
||
53025 | 1051 |
primrec_new the_default :: "'a \<Rightarrow> 'a option \<Rightarrow> 'a" where |
1052 |
"the_default d None = d" | |
|
1053 |
"the_default _ (Some a) = a" |
|
52843 | 1054 |
|
53621 | 1055 |
text {* \blankline *} |
1056 |
||
52841 | 1057 |
primrec_new mirrror :: "('a, 'b, 'c) triple \<Rightarrow> ('c, 'b, 'a) triple" where |
1058 |
"mirrror (Triple a b c) = Triple c b a" |
|
1059 |
||
53621 | 1060 |
text {* |
1061 |
\noindent |
|
1062 |
The equations can be specified in any order, and it is acceptable to leave out |
|
1063 |
some cases, which are then unspecified. Pattern matching on the left-hand side |
|
1064 |
is restricted to a single datatype, which must correspond to the same argument |
|
1065 |
in all equations. |
|
1066 |
*} |
|
52828 | 1067 |
|
53621 | 1068 |
|
1069 |
subsubsection {* Simple Recursion |
|
1070 |
\label{sssec:primrec-simple-recursion} *} |
|
52828 | 1071 |
|
52841 | 1072 |
text {* |
53621 | 1073 |
For simple recursive types, recursive calls on a constructor argument are |
1074 |
allowed on the right-hand side: |
|
52841 | 1075 |
*} |
1076 |
||
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1077 |
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
|
1078 |
"replicate Zero _ = []" | |
53644 | 1079 |
"replicate (Suc n) x = x # replicate n x" |
52841 | 1080 |
|
53621 | 1081 |
text {* \blankline *} |
52843 | 1082 |
|
53332 | 1083 |
primrec_new at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where |
53644 | 1084 |
"at (x # xs) j = |
52843 | 1085 |
(case j of |
53644 | 1086 |
Zero \<Rightarrow> x |
1087 |
| Suc j' \<Rightarrow> at xs j')" |
|
52843 | 1088 |
|
53621 | 1089 |
text {* \blankline *} |
1090 |
||
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1091 |
primrec_new (*<*)(in early) (*>*)tfold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) tlist \<Rightarrow> 'b" where |
53644 | 1092 |
"tfold _ (TNil y) = y" | |
1093 |
"tfold f (TCons x xs) = f x (tfold f xs)" |
|
52841 | 1094 |
|
53025 | 1095 |
text {* |
53621 | 1096 |
\noindent |
54402 | 1097 |
Pattern matching is only available for the argument on which the recursion takes |
1098 |
place. Fortunately, it is easy to generate pattern-maching equations using the |
|
1099 |
\keyw{simps\_of\_case} command provided by the theory |
|
1100 |
\verb|~~/src/HOL/Library/Simps_Case_Conv|. |
|
1101 |
*} |
|
1102 |
||
1103 |
simps_of_case at_simps: at.simps |
|
1104 |
||
1105 |
text {* |
|
1106 |
This generates the lemma collection @{thm [source] at_simps}: |
|
1107 |
% |
|
1108 |
\[@{thm at_simps(1)[no_vars]} |
|
1109 |
\qquad @{thm at_simps(2)[no_vars]}\] |
|
1110 |
% |
|
54184 | 1111 |
The next example is defined using \keyw{fun} to escape the syntactic |
1112 |
restrictions imposed on primitive recursive functions. The |
|
1113 |
@{command datatype_new_compat} command is needed to register new-style datatypes |
|
1114 |
for use with \keyw{fun} and \keyw{function} |
|
53621 | 1115 |
(Section~\ref{sssec:datatype-new-compat}): |
53025 | 1116 |
*} |
52828 | 1117 |
|
53621 | 1118 |
datatype_new_compat nat |
1119 |
||
1120 |
text {* \blankline *} |
|
1121 |
||
1122 |
fun at_least_two :: "nat \<Rightarrow> bool" where |
|
1123 |
"at_least_two (Suc (Suc _)) \<longleftrightarrow> True" | |
|
1124 |
"at_least_two _ \<longleftrightarrow> False" |
|
1125 |
||
1126 |
||
1127 |
subsubsection {* Mutual Recursion |
|
1128 |
\label{sssec:primrec-mutual-recursion} *} |
|
52828 | 1129 |
|
52841 | 1130 |
text {* |
53621 | 1131 |
The syntax for mutually recursive functions over mutually recursive datatypes |
1132 |
is straightforward: |
|
52841 | 1133 |
*} |
1134 |
||
1135 |
primrec_new |
|
53623 | 1136 |
nat_of_even_nat :: "even_nat \<Rightarrow> nat" and |
1137 |
nat_of_odd_nat :: "odd_nat \<Rightarrow> nat" |
|
52841 | 1138 |
where |
53623 | 1139 |
"nat_of_even_nat Even_Zero = Zero" | |
1140 |
"nat_of_even_nat (Even_Suc n) = Suc (nat_of_odd_nat n)" | |
|
1141 |
"nat_of_odd_nat (Odd_Suc n) = Suc (nat_of_even_nat n)" |
|
52841 | 1142 |
|
53752 | 1143 |
text {* \blankline *} |
1144 |
||
52841 | 1145 |
primrec_new |
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1146 |
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
|
1147 |
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
|
1148 |
eval\<^sub>f :: "('a \<Rightarrow> int) \<Rightarrow> ('b \<Rightarrow> int) \<Rightarrow> ('a, 'b) fct \<Rightarrow> int" |
52841 | 1149 |
where |
1150 |
"eval\<^sub>e \<gamma> \<xi> (Term t) = eval\<^sub>t \<gamma> \<xi> t" | |
|
1151 |
"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
|
1152 |
"eval\<^sub>t \<gamma> \<xi> (Factor f) = eval\<^sub>f \<gamma> \<xi> f" | |
52841 | 1153 |
"eval\<^sub>t \<gamma> \<xi> (Prod f t) = eval\<^sub>f \<gamma> \<xi> f + eval\<^sub>t \<gamma> \<xi> t" | |
1154 |
"eval\<^sub>f \<gamma> _ (Const a) = \<gamma> a" | |
|
1155 |
"eval\<^sub>f _ \<xi> (Var b) = \<xi> b" | |
|
1156 |
"eval\<^sub>f \<gamma> \<xi> (Expr e) = eval\<^sub>e \<gamma> \<xi> e" |
|
1157 |
||
53621 | 1158 |
text {* |
1159 |
\noindent |
|
53647 | 1160 |
Mutual recursion is possible within a single type, using \keyw{fun}: |
53621 | 1161 |
*} |
52828 | 1162 |
|
53621 | 1163 |
fun |
1164 |
even :: "nat \<Rightarrow> bool" and |
|
1165 |
odd :: "nat \<Rightarrow> bool" |
|
1166 |
where |
|
1167 |
"even Zero = True" | |
|
1168 |
"even (Suc n) = odd n" | |
|
1169 |
"odd Zero = False" | |
|
1170 |
"odd (Suc n) = even n" |
|
1171 |
||
1172 |
||
1173 |
subsubsection {* Nested Recursion |
|
1174 |
\label{sssec:primrec-nested-recursion} *} |
|
1175 |
||
1176 |
text {* |
|
1177 |
In a departure from the old datatype package, nested recursion is normally |
|
1178 |
handled via the map functions of the nesting type constructors. For example, |
|
1179 |
recursive calls are lifted to lists using @{const map}: |
|
1180 |
*} |
|
52828 | 1181 |
|
52843 | 1182 |
(*<*) |
53644 | 1183 |
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") |
52843 | 1184 |
(*>*) |
53028 | 1185 |
primrec_new at\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" where |
1186 |
"at\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js = |
|
52843 | 1187 |
(case js of |
1188 |
[] \<Rightarrow> a |
|
53028 | 1189 |
| j # js' \<Rightarrow> at (map (\<lambda>t. at\<^sub>f\<^sub>f t js') ts) j)" |
52843 | 1190 |
|
53025 | 1191 |
text {* |
53647 | 1192 |
\noindent |
53621 | 1193 |
The next example features recursion through the @{text option} type. Although |
53623 | 1194 |
@{text option} is not a new-style datatype, it is registered as a BNF with the |
54491 | 1195 |
map function @{const map_option}: |
53025 | 1196 |
*} |
52843 | 1197 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1198 |
primrec_new (*<*)(in early) (*>*)sum_btree :: "('a\<Colon>{zero,plus}) btree \<Rightarrow> 'a" where |
52843 | 1199 |
"sum_btree (BNode a lt rt) = |
54491 | 1200 |
a + the_default 0 (map_option sum_btree lt) + |
1201 |
the_default 0 (map_option sum_btree rt)" |
|
52843 | 1202 |
|
53136 | 1203 |
text {* |
53621 | 1204 |
\noindent |
1205 |
The same principle applies for arbitrary type constructors through which |
|
1206 |
recursion is possible. Notably, the map function for the function type |
|
1207 |
(@{text \<Rightarrow>}) is simply composition (@{text "op \<circ>"}): |
|
53136 | 1208 |
*} |
52828 | 1209 |
|
54182 | 1210 |
primrec_new (*<*)(in early) (*>*)relabel_ft :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree \<Rightarrow> 'a ftree" where |
1211 |
"relabel_ft f (FTLeaf x) = FTLeaf (f x)" | |
|
1212 |
"relabel_ft f (FTNode g) = FTNode (relabel_ft f \<circ> g)" |
|
1213 |
||
1214 |
text {* |
|
1215 |
\noindent |
|
1216 |
For convenience, recursion through functions can also be expressed using |
|
1217 |
$\lambda$-abstractions and function application rather than through composition. |
|
1218 |
For example: |
|
1219 |
*} |
|
1220 |
||
1221 |
primrec_new relabel_ft :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree \<Rightarrow> 'a ftree" where |
|
1222 |
"relabel_ft f (FTLeaf x) = FTLeaf (f x)" | |
|
1223 |
"relabel_ft f (FTNode g) = FTNode (\<lambda>x. relabel_ft f (g x))" |
|
52828 | 1224 |
|
54183 | 1225 |
text {* \blankline *} |
1226 |
||
1227 |
primrec_new subtree_ft :: "'a \<Rightarrow> 'a ftree \<Rightarrow> 'a ftree" where |
|
1228 |
"subtree_ft x (FTNode g) = g x" |
|
1229 |
||
52843 | 1230 |
text {* |
53621 | 1231 |
\noindent |
54182 | 1232 |
For recursion through curried $n$-ary functions, $n$ applications of |
1233 |
@{term "op \<circ>"} are necessary. The examples below illustrate the case where |
|
1234 |
$n = 2$: |
|
53621 | 1235 |
*} |
1236 |
||
54182 | 1237 |
datatype_new 'a ftree2 = FTLeaf2 'a | FTNode2 "'a \<Rightarrow> 'a \<Rightarrow> 'a ftree2" |
1238 |
||
1239 |
text {* \blankline *} |
|
1240 |
||
1241 |
primrec_new (*<*)(in early) (*>*)relabel_ft2 :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree2 \<Rightarrow> 'a ftree2" where |
|
1242 |
"relabel_ft2 f (FTLeaf2 x) = FTLeaf2 (f x)" | |
|
1243 |
"relabel_ft2 f (FTNode2 g) = FTNode2 (op \<circ> (op \<circ> (relabel_ft2 f)) g)" |
|
1244 |
||
1245 |
text {* \blankline *} |
|
1246 |
||
1247 |
primrec_new relabel_ft2 :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a ftree2 \<Rightarrow> 'a ftree2" where |
|
1248 |
"relabel_ft2 f (FTLeaf2 x) = FTLeaf2 (f x)" | |
|
1249 |
"relabel_ft2 f (FTNode2 g) = FTNode2 (\<lambda>x y. relabel_ft2 f (g x y))" |
|
54031 | 1250 |
|
54183 | 1251 |
text {* \blankline *} |
1252 |
||
1253 |
primrec_new subtree_ft2 :: "'a \<Rightarrow> 'a \<Rightarrow> 'a ftree2 \<Rightarrow> 'a ftree2" where |
|
1254 |
"subtree_ft2 x y (FTNode2 g) = g x y" |
|
1255 |
||
53621 | 1256 |
|
1257 |
subsubsection {* Nested-as-Mutual Recursion |
|
53644 | 1258 |
\label{sssec:primrec-nested-as-mutual-recursion} *} |
53621 | 1259 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1260 |
(*<*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1261 |
locale n2m begin |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1262 |
(*>*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1263 |
|
53621 | 1264 |
text {* |
1265 |
For compatibility with the old package, but also because it is sometimes |
|
1266 |
convenient in its own right, it is possible to treat nested recursive datatypes |
|
1267 |
as mutually recursive ones if the recursion takes place though new-style |
|
1268 |
datatypes. For example: |
|
52843 | 1269 |
*} |
1270 |
||
53331
20440c789759
prove theorem in the right context (that knows about local variables)
traytel
parents:
53330
diff
changeset
|
1271 |
primrec_new |
53647 | 1272 |
at\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" and |
1273 |
ats\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f list \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> 'a" |
|
52843 | 1274 |
where |
53647 | 1275 |
"at\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js = |
52843 | 1276 |
(case js of |
1277 |
[] \<Rightarrow> a |
|
53647 | 1278 |
| j # js' \<Rightarrow> ats\<^sub>f\<^sub>f ts j js')" | |
1279 |
"ats\<^sub>f\<^sub>f (t # ts) j = |
|
52843 | 1280 |
(case j of |
53647 | 1281 |
Zero \<Rightarrow> at\<^sub>f\<^sub>f t |
1282 |
| Suc j' \<Rightarrow> ats\<^sub>f\<^sub>f ts j')" |
|
52843 | 1283 |
|
53647 | 1284 |
text {* |
1285 |
\noindent |
|
54287 | 1286 |
Appropriate induction rules are generated as |
54031 | 1287 |
@{thm [source] at\<^sub>f\<^sub>f.induct}, |
1288 |
@{thm [source] ats\<^sub>f\<^sub>f.induct}, and |
|
54287 | 1289 |
@{thm [source] at\<^sub>f\<^sub>f_ats\<^sub>f\<^sub>f.induct}. The |
1290 |
induction rules and the underlying recursors are generated on a per-need basis |
|
1291 |
and are kept in a cache to speed up subsequent definitions. |
|
53647 | 1292 |
|
1293 |
Here is a second example: |
|
1294 |
*} |
|
53621 | 1295 |
|
53331
20440c789759
prove theorem in the right context (that knows about local variables)
traytel
parents:
53330
diff
changeset
|
1296 |
primrec_new |
53330
77da8d3c46e0
fixed docs w.r.t. availability of "primrec_new" and friends
blanchet
parents:
53262
diff
changeset
|
1297 |
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
|
1298 |
sum_btree_option :: "'a btree option \<Rightarrow> 'a" |
52843 | 1299 |
where |
1300 |
"sum_btree (BNode a lt rt) = |
|
53025 | 1301 |
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
|
1302 |
"sum_btree_option None = 0" | |
53025 | 1303 |
"sum_btree_option (Some t) = sum_btree t" |
52843 | 1304 |
|
1305 |
text {* |
|
53621 | 1306 |
% * can pretend a nested type is mutually recursive (if purely inductive) |
1307 |
% * avoids the higher-order map |
|
1308 |
% * e.g. |
|
1309 |
||
53617 | 1310 |
% * this can always be avoided; |
1311 |
% * e.g. in our previous example, we first mapped the recursive |
|
1312 |
% calls, then we used a generic at function to retrieve the result |
|
1313 |
% |
|
1314 |
% * there's no hard-and-fast rule of when to use one or the other, |
|
1315 |
% just like there's no rule when to use fold and when to use |
|
1316 |
% primrec_new |
|
1317 |
% |
|
1318 |
% * higher-order approach, considering nesting as nesting, is more |
|
1319 |
% compositional -- e.g. we saw how we could reuse an existing polymorphic |
|
53647 | 1320 |
% at or the_default, whereas @{const ats\<^sub>f\<^sub>f} is much more specific |
53617 | 1321 |
% |
1322 |
% * but: |
|
1323 |
% * is perhaps less intuitive, because it requires higher-order thinking |
|
1324 |
% * may seem inefficient, and indeed with the code generator the |
|
1325 |
% mutually recursive version might be nicer |
|
1326 |
% * is somewhat indirect -- must apply a map first, then compute a result |
|
1327 |
% (cannot mix) |
|
53647 | 1328 |
% * the auxiliary functions like @{const ats\<^sub>f\<^sub>f} are sometimes useful in own right |
53617 | 1329 |
% |
1330 |
% * impact on automation unclear |
|
1331 |
% |
|
52843 | 1332 |
*} |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1333 |
(*<*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1334 |
end |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1335 |
(*>*) |
52843 | 1336 |
|
52824 | 1337 |
|
53617 | 1338 |
subsection {* Command Syntax |
1339 |
\label{ssec:primrec-command-syntax} *} |
|
1340 |
||
1341 |
||
53621 | 1342 |
subsubsection {* \keyw{primrec\_new} |
1343 |
\label{sssec:primrec-new} *} |
|
52828 | 1344 |
|
1345 |
text {* |
|
53829 | 1346 |
\begin{matharray}{rcl} |
1347 |
@{command_def "primrec_new"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
1348 |
\end{matharray} |
|
52794 | 1349 |
|
52840 | 1350 |
@{rail " |
53829 | 1351 |
@@{command primrec_new} target? fixes \\ @'where' (@{syntax pr_equation} + '|') |
52840 | 1352 |
; |
53829 | 1353 |
@{syntax_def pr_equation}: thmdecl? prop |
52840 | 1354 |
"} |
52828 | 1355 |
*} |
1356 |
||
52840 | 1357 |
|
53619 | 1358 |
(* |
52840 | 1359 |
subsection {* Generated Theorems |
1360 |
\label{ssec:primrec-generated-theorems} *} |
|
52824 | 1361 |
|
52828 | 1362 |
text {* |
53617 | 1363 |
% * synthesized nonrecursive definition |
1364 |
% * user specification is rederived from it, exactly as entered |
|
1365 |
% |
|
1366 |
% * induct |
|
1367 |
% * mutualized |
|
1368 |
% * without some needless induction hypotheses if not used |
|
1369 |
% * fold, rec |
|
1370 |
% * mutualized |
|
52828 | 1371 |
*} |
53619 | 1372 |
*) |
1373 |
||
52824 | 1374 |
|
52840 | 1375 |
subsection {* Recursive Default Values for Selectors |
53623 | 1376 |
\label{ssec:primrec-recursive-default-values-for-selectors} *} |
52827 | 1377 |
|
1378 |
text {* |
|
1379 |
A datatype selector @{text un_D} can have a default value for each constructor |
|
1380 |
on which it is not otherwise specified. Occasionally, it is useful to have the |
|
1381 |
default value be defined recursively. This produces a chicken-and-egg situation |
|
53621 | 1382 |
that may seem unsolvable, because the datatype is not introduced yet at the |
52827 | 1383 |
moment when the selectors are introduced. Of course, we can always define the |
1384 |
selectors manually afterward, but we then have to state and prove all the |
|
1385 |
characteristic theorems ourselves instead of letting the package do it. |
|
1386 |
||
1387 |
Fortunately, there is a fairly elegant workaround that relies on overloading and |
|
1388 |
that avoids the tedium of manual derivations: |
|
1389 |
||
1390 |
\begin{enumerate} |
|
1391 |
\setlength{\itemsep}{0pt} |
|
1392 |
||
1393 |
\item |
|
1394 |
Introduce a fully unspecified constant @{text "un_D\<^sub>0 \<Colon> 'a"} using |
|
1395 |
@{keyword consts}. |
|
1396 |
||
1397 |
\item |
|
53535 | 1398 |
Define the datatype, specifying @{text "un_D\<^sub>0"} as the selector's default |
1399 |
value. |
|
52827 | 1400 |
|
1401 |
\item |
|
53535 | 1402 |
Define the behavior of @{text "un_D\<^sub>0"} on values of the newly introduced |
1403 |
datatype using the \keyw{overloading} command. |
|
52827 | 1404 |
|
1405 |
\item |
|
1406 |
Derive the desired equation on @{text un_D} from the characteristic equations |
|
1407 |
for @{text "un_D\<^sub>0"}. |
|
1408 |
\end{enumerate} |
|
1409 |
||
53619 | 1410 |
\noindent |
52827 | 1411 |
The following example illustrates this procedure: |
1412 |
*} |
|
1413 |
||
1414 |
consts termi\<^sub>0 :: 'a |
|
1415 |
||
53619 | 1416 |
text {* \blankline *} |
1417 |
||
53491 | 1418 |
datatype_new ('a, 'b) tlist = |
52827 | 1419 |
TNil (termi: 'b) (defaults ttl: TNil) |
53491 | 1420 |
| TCons (thd: 'a) (ttl : "('a, 'b) tlist") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs") |
52827 | 1421 |
|
53619 | 1422 |
text {* \blankline *} |
1423 |
||
52827 | 1424 |
overloading |
53491 | 1425 |
termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist \<Rightarrow> 'b" |
52827 | 1426 |
begin |
53491 | 1427 |
primrec_new termi\<^sub>0 :: "('a, 'b) tlist \<Rightarrow> 'b" where |
53621 | 1428 |
"termi\<^sub>0 (TNil y) = y" | |
1429 |
"termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs" |
|
52827 | 1430 |
end |
1431 |
||
53619 | 1432 |
text {* \blankline *} |
1433 |
||
52827 | 1434 |
lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs" |
1435 |
by (cases xs) auto |
|
1436 |
||
1437 |
||
52828 | 1438 |
subsection {* Compatibility Issues |
53617 | 1439 |
\label{ssec:primrec-compatibility-issues} *} |
52828 | 1440 |
|
1441 |
text {* |
|
53997 | 1442 |
The command @{command primrec_new} has been designed to be highly compatible |
1443 |
with the old \keyw{primrec}, to ease migration. There is nonetheless at least |
|
1444 |
one incompatibility that may arise when porting to the new package: |
|
1445 |
||
1446 |
\begin{itemize} |
|
1447 |
\setlength{\itemsep}{0pt} |
|
1448 |
||
54185 | 1449 |
\item \emph{Some theorems have different names.} |
53997 | 1450 |
For $m > 1$ mutually recursive functions, |
54023
cede3c1d2417
minor doc fix (there is no guarantee that the equations for a given f_i are contiguous in the collection)
blanchet
parents:
54014
diff
changeset
|
1451 |
@{text "f\<^sub>1_\<dots>_f\<^sub>m.simps"} has been broken down into separate |
cede3c1d2417
minor doc fix (there is no guarantee that the equations for a given f_i are contiguous in the collection)
blanchet
parents:
54014
diff
changeset
|
1452 |
subcollections @{text "f\<^sub>i.simps"}. |
53997 | 1453 |
\end{itemize} |
52828 | 1454 |
*} |
52794 | 1455 |
|
1456 |
||
52827 | 1457 |
section {* Defining Codatatypes |
52805 | 1458 |
\label{sec:defining-codatatypes} *} |
1459 |
||
1460 |
text {* |
|
53829 | 1461 |
Codatatypes can be specified using the @{command codatatype} command. The |
53623 | 1462 |
command is first illustrated through concrete examples featuring different |
1463 |
flavors of corecursion. More examples can be found in the directory |
|
53997 | 1464 |
\verb|~~/src/HOL/|\allowbreak\verb|BNF/Examples|. The |
1465 |
\emph{Archive of Formal Proofs} also includes some useful codatatypes, notably |
|
1466 |
for lazy lists \cite{lochbihler-2010}. |
|
52805 | 1467 |
*} |
52792 | 1468 |
|
52824 | 1469 |
|
53617 | 1470 |
subsection {* Introductory Examples |
1471 |
\label{ssec:codatatype-introductory-examples} *} |
|
52794 | 1472 |
|
53623 | 1473 |
|
1474 |
subsubsection {* Simple Corecursion |
|
1475 |
\label{sssec:codatatype-simple-corecursion} *} |
|
1476 |
||
52805 | 1477 |
text {* |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
1478 |
Noncorecursive codatatypes coincide with the corresponding datatypes, so they |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
1479 |
are useless in practice. \emph{Corecursive codatatypes} have the same syntax |
53623 | 1480 |
as recursive datatypes, except for the command name. For example, here is the |
1481 |
definition of lazy lists: |
|
1482 |
*} |
|
1483 |
||
1484 |
codatatype (lset: 'a) llist (map: lmap rel: llist_all2) = |
|
1485 |
lnull: LNil (defaults ltl: LNil) |
|
1486 |
| LCons (lhd: 'a) (ltl: "'a llist") |
|
1487 |
||
1488 |
text {* |
|
1489 |
\noindent |
|
1490 |
Lazy lists can be infinite, such as @{text "LCons 0 (LCons 0 (\<dots>))"} and |
|
53647 | 1491 |
@{text "LCons 0 (LCons 1 (LCons 2 (\<dots>)))"}. Here is a related type, that of |
1492 |
infinite streams: |
|
1493 |
*} |
|
1494 |
||
1495 |
codatatype (sset: 'a) stream (map: smap rel: stream_all2) = |
|
1496 |
SCons (shd: 'a) (stl: "'a stream") |
|
1497 |
||
1498 |
text {* |
|
1499 |
\noindent |
|
1500 |
Another interesting type that can |
|
53623 | 1501 |
be defined as a codatatype is that of the extended natural numbers: |
1502 |
*} |
|
1503 |
||
53644 | 1504 |
codatatype enat = EZero | ESuc enat |
53623 | 1505 |
|
1506 |
text {* |
|
1507 |
\noindent |
|
1508 |
This type has exactly one infinite element, @{text "ESuc (ESuc (ESuc (\<dots>)))"}, |
|
1509 |
that represents $\infty$. In addition, it has finite values of the form |
|
1510 |
@{text "ESuc (\<dots> (ESuc EZero)\<dots>)"}. |
|
53675 | 1511 |
|
1512 |
Here is an example with many constructors: |
|
52805 | 1513 |
*} |
53623 | 1514 |
|
53675 | 1515 |
codatatype 'a process = |
1516 |
Fail |
|
1517 |
| Skip (cont: "'a process") |
|
1518 |
| Action (prefix: 'a) (cont: "'a process") |
|
1519 |
| Choice (left: "'a process") (right: "'a process") |
|
1520 |
||
53750 | 1521 |
text {* |
53829 | 1522 |
\noindent |
53750 | 1523 |
Notice that the @{const cont} selector is associated with both @{const Skip} |
54146 | 1524 |
and @{const Action}. |
53750 | 1525 |
*} |
1526 |
||
53623 | 1527 |
|
1528 |
subsubsection {* Mutual Corecursion |
|
1529 |
\label{sssec:codatatype-mutual-corecursion} *} |
|
1530 |
||
1531 |
text {* |
|
1532 |
\noindent |
|
1533 |
The example below introduces a pair of \emph{mutually corecursive} types: |
|
1534 |
*} |
|
1535 |
||
1536 |
codatatype even_enat = Even_EZero | Even_ESuc odd_enat |
|
1537 |
and odd_enat = Odd_ESuc even_enat |
|
1538 |
||
1539 |
||
1540 |
subsubsection {* Nested Corecursion |
|
1541 |
\label{sssec:codatatype-nested-corecursion} *} |
|
1542 |
||
1543 |
text {* |
|
1544 |
\noindent |
|
53675 | 1545 |
The next examples feature \emph{nested corecursion}: |
53623 | 1546 |
*} |
1547 |
||
53644 | 1548 |
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") |
53675 | 1549 |
|
53752 | 1550 |
text {* \blankline *} |
1551 |
||
53644 | 1552 |
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 | 1553 |
|
53752 | 1554 |
text {* \blankline *} |
1555 |
||
53675 | 1556 |
codatatype 'a state_machine = |
53751 | 1557 |
State_Machine (accept: bool) (trans: "'a \<Rightarrow> 'a state_machine") |
53675 | 1558 |
|
52824 | 1559 |
|
53617 | 1560 |
subsection {* Command Syntax |
1561 |
\label{ssec:codatatype-command-syntax} *} |
|
52805 | 1562 |
|
53619 | 1563 |
|
53621 | 1564 |
subsubsection {* \keyw{codatatype} |
1565 |
\label{sssec:codatatype} *} |
|
53619 | 1566 |
|
52824 | 1567 |
text {* |
53829 | 1568 |
\begin{matharray}{rcl} |
1569 |
@{command_def "codatatype"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
1570 |
\end{matharray} |
|
1571 |
||
1572 |
@{rail " |
|
1573 |
@@{command codatatype} target? \\ |
|
1574 |
(@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and') |
|
1575 |
"} |
|
1576 |
||
1577 |
\noindent |
|
52827 | 1578 |
Definitions of codatatypes have almost exactly the same syntax as for datatypes |
53829 | 1579 |
(Section~\ref{ssec:datatype-command-syntax}). The @{text "no_discs_sels"} option |
1580 |
is not available, because destructors are a crucial notion for codatatypes. |
|
53623 | 1581 |
*} |
1582 |
||
1583 |
||
1584 |
subsection {* Generated Constants |
|
1585 |
\label{ssec:codatatype-generated-constants} *} |
|
1586 |
||
1587 |
text {* |
|
1588 |
Given a codatatype @{text "('a\<^sub>1, \<dots>, 'a\<^sub>m) t"} |
|
1589 |
with $m > 0$ live type variables and $n$ constructors @{text "t.C\<^sub>1"}, |
|
1590 |
\ldots, @{text "t.C\<^sub>n"}, the same auxiliary constants are generated as for |
|
1591 |
datatypes (Section~\ref{ssec:datatype-generated-constants}), except that the |
|
1592 |
iterator and the recursor are replaced by dual concepts: |
|
1593 |
||
1594 |
\begin{itemize} |
|
1595 |
\setlength{\itemsep}{0pt} |
|
1596 |
||
54494 | 1597 |
\item \relax{Coiterator}: @{text unfold_t} |
1598 |
||
1599 |
\item \relax{Corecursor}: @{text corec_t} |
|
53623 | 1600 |
|
1601 |
\end{itemize} |
|
1602 |
*} |
|
1603 |
||
1604 |
||
1605 |
subsection {* Generated Theorems |
|
1606 |
\label{ssec:codatatype-generated-theorems} *} |
|
1607 |
||
1608 |
text {* |
|
53829 | 1609 |
The characteristic theorems generated by @{command codatatype} are grouped in |
53623 | 1610 |
three broad categories: |
1611 |
||
1612 |
\begin{itemize} |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1613 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1614 |
|
53623 | 1615 |
\item The \emph{free constructor theorems} are properties about the constructors |
1616 |
and destructors that can be derived for any freely generated type. |
|
1617 |
||
1618 |
\item The \emph{functorial theorems} are properties of datatypes related to |
|
1619 |
their BNF nature. |
|
1620 |
||
1621 |
\item The \emph{coinductive theorems} are properties of datatypes related to |
|
1622 |
their coinductive nature. |
|
1623 |
\end{itemize} |
|
1624 |
||
1625 |
\noindent |
|
1626 |
The first two categories are exactly as for datatypes and are described in |
|
53642 | 1627 |
Sections |
1628 |
\ref{sssec:free-constructor-theorems}~and~\ref{sssec:functorial-theorems}. |
|
52824 | 1629 |
*} |
1630 |
||
53617 | 1631 |
|
53623 | 1632 |
subsubsection {* Coinductive Theorems |
1633 |
\label{sssec:coinductive-theorems} *} |
|
1634 |
||
1635 |
text {* |
|
54031 | 1636 |
The coinductive theorems are listed below for @{typ "'a llist"}: |
53623 | 1637 |
|
1638 |
\begin{indentblock} |
|
1639 |
\begin{description} |
|
1640 |
||
53643 | 1641 |
\item[\begin{tabular}{@ {}l@ {}} |
1642 |
@{text "t."}\hthm{coinduct} @{text "[coinduct t, consumes m, case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1643 |
\phantom{@{text "t."}\hthm{coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1644 |
\end{tabular}] ~ \\ |
|
53623 | 1645 |
@{thm llist.coinduct[no_vars]} |
53617 | 1646 |
|
53643 | 1647 |
\item[\begin{tabular}{@ {}l@ {}} |
1648 |
@{text "t."}\hthm{strong\_coinduct} @{text "[consumes m, case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1649 |
\phantom{@{text "t."}\hthm{strong\_coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1650 |
\end{tabular}] ~ \\ |
|
1651 |
@{thm llist.strong_coinduct[no_vars]} |
|
53617 | 1652 |
|
53643 | 1653 |
\item[\begin{tabular}{@ {}l@ {}} |
1654 |
@{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]"} \\ |
|
1655 |
@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{strong\_coinduct} @{text "[case_names t\<^sub>1 \<dots> t\<^sub>m,"} \\ |
|
1656 |
\phantom{@{text "t\<^sub>1_\<dots>_t\<^sub>m."}\hthm{strong\_coinduct} @{text "["}}@{text "case_conclusion D\<^sub>1 \<dots> D\<^sub>n]"}\rm: |
|
1657 |
\end{tabular}] ~ \\ |
|
1658 |
Given $m > 1$ mutually corecursive codatatypes, these coinduction rules can be |
|
1659 |
used to prove $m$ properties simultaneously. |
|
1660 |
||
54031 | 1661 |
\item[@{text "t."}\hthm{unfold}\rm:] ~ \\ |
53623 | 1662 |
@{thm llist.unfold(1)[no_vars]} \\ |
1663 |
@{thm llist.unfold(2)[no_vars]} |
|
1664 |
||
54031 | 1665 |
\item[@{text "t."}\hthm{corec}\rm:] ~ \\ |
53623 | 1666 |
@{thm llist.corec(1)[no_vars]} \\ |
1667 |
@{thm llist.corec(2)[no_vars]} |
|
1668 |
||
53703 | 1669 |
\item[@{text "t."}\hthm{disc\_unfold}\rm:] ~ \\ |
53643 | 1670 |
@{thm llist.disc_unfold(1)[no_vars]} \\ |
1671 |
@{thm llist.disc_unfold(2)[no_vars]} |
|
1672 |
||
53703 | 1673 |
\item[@{text "t."}\hthm{disc\_corec}\rm:] ~ \\ |
53643 | 1674 |
@{thm llist.disc_corec(1)[no_vars]} \\ |
1675 |
@{thm llist.disc_corec(2)[no_vars]} |
|
1676 |
||
1677 |
\item[@{text "t."}\hthm{disc\_unfold\_iff} @{text "[simp]"}\rm:] ~ \\ |
|
1678 |
@{thm llist.disc_unfold_iff(1)[no_vars]} \\ |
|
1679 |
@{thm llist.disc_unfold_iff(2)[no_vars]} |
|
1680 |
||
1681 |
\item[@{text "t."}\hthm{disc\_corec\_iff} @{text "[simp]"}\rm:] ~ \\ |
|
1682 |
@{thm llist.disc_corec_iff(1)[no_vars]} \\ |
|
1683 |
@{thm llist.disc_corec_iff(2)[no_vars]} |
|
1684 |
||
1685 |
\item[@{text "t."}\hthm{sel\_unfold} @{text "[simp]"}\rm:] ~ \\ |
|
1686 |
@{thm llist.sel_unfold(1)[no_vars]} \\ |
|
1687 |
@{thm llist.sel_unfold(2)[no_vars]} |
|
1688 |
||
1689 |
\item[@{text "t."}\hthm{sel\_corec} @{text "[simp]"}\rm:] ~ \\ |
|
1690 |
@{thm llist.sel_corec(1)[no_vars]} \\ |
|
1691 |
@{thm llist.sel_corec(2)[no_vars]} |
|
1692 |
||
53623 | 1693 |
\end{description} |
1694 |
\end{indentblock} |
|
1695 |
||
1696 |
\noindent |
|
53829 | 1697 |
For convenience, @{command codatatype} also provides the following collection: |
53623 | 1698 |
|
1699 |
\begin{indentblock} |
|
1700 |
\begin{description} |
|
1701 |
||
54031 | 1702 |
\item[@{text "t."}\hthm{simps} = @{text t.inject} @{text t.distinct} @{text t.case} @{text t.disc_corec} @{text t.disc_corec_iff}] ~ \\ |
1703 |
@{text t.sel_corec} @{text t.disc_unfold} @{text t.disc_unfold_iff} @{text t.sel_unfold} @{text t.map} \\ |
|
1704 |
@{text t.rel_inject} @{text t.rel_distinct} @{text t.set} |
|
53623 | 1705 |
|
1706 |
\end{description} |
|
1707 |
\end{indentblock} |
|
1708 |
*} |
|
52805 | 1709 |
|
1710 |
||
52827 | 1711 |
section {* Defining Corecursive Functions |
52805 | 1712 |
\label{sec:defining-corecursive-functions} *} |
1713 |
||
1714 |
text {* |
|
54183 | 1715 |
Corecursive functions can be specified using the @{command primcorec} and |
1716 |
\keyw{prim\-corec\-ursive} commands, which support primitive corecursion, or |
|
1717 |
using the more general \keyw{partial\_function} command. Here, the focus is on |
|
1718 |
the first two. More examples can be found in the directory |
|
53753
ae7f50e70c09
renamed "primcorec" to "primcorecursive", to open the door to a 'theory -> theory' command called "primcorec" (cf. "fun" vs. "function")
blanchet
parents:
53752
diff
changeset
|
1719 |
\verb|~~/src/HOL/BNF/Examples|. |
53644 | 1720 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1721 |
Whereas recursive functions consume datatypes one constructor at a time, |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1722 |
corecursive functions construct codatatypes one constructor at a time. |
53752 | 1723 |
Partly reflecting a lack of agreement among proponents of coalgebraic methods, |
1724 |
Isabelle supports three competing syntaxes for specifying a function $f$: |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1725 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1726 |
\begin{itemize} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1727 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1728 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1729 |
\abovedisplayskip=.5\abovedisplayskip |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1730 |
\belowdisplayskip=.5\belowdisplayskip |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1731 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1732 |
\item The \emph{destructor view} specifies $f$ by implications of the form |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1733 |
\[@{text "\<dots> \<Longrightarrow> is_C\<^sub>j (f x\<^sub>1 \<dots> x\<^sub>n)"}\] and |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1734 |
equations of the form |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1735 |
\[@{text "un_C\<^sub>ji (f x\<^sub>1 \<dots> x\<^sub>n) = \<dots>"}\] |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1736 |
This style is popular in the coalgebraic literature. |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1737 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1738 |
\item The \emph{constructor view} specifies $f$ by equations of the form |
54183 | 1739 |
\[@{text "\<dots> \<Longrightarrow> f x\<^sub>1 \<dots> x\<^sub>n = C\<^sub>j \<dots>"}\] |
53752 | 1740 |
This style is often more concise than the previous one. |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1741 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1742 |
\item The \emph{code view} specifies $f$ by a single equation of the form |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1743 |
\[@{text "f x\<^sub>1 \<dots> x\<^sub>n = \<dots>"}\] |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1744 |
with restrictions on the format of the right-hand side. Lazy functional |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1745 |
programming languages such as Haskell support a generalized version of this |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1746 |
style. |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1747 |
\end{itemize} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1748 |
|
53753
ae7f50e70c09
renamed "primcorec" to "primcorecursive", to open the door to a 'theory -> theory' command called "primcorec" (cf. "fun" vs. "function")
blanchet
parents:
53752
diff
changeset
|
1749 |
All three styles are available as input syntax. Whichever syntax is chosen, |
ae7f50e70c09
renamed "primcorec" to "primcorecursive", to open the door to a 'theory -> theory' command called "primcorec" (cf. "fun" vs. "function")
blanchet
parents:
53752
diff
changeset
|
1750 |
characteristic theorems for all three styles are generated. |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1751 |
|
52828 | 1752 |
%%% TODO: partial_function? E.g. for defining tail recursive function on lazy |
1753 |
%%% lists (cf. terminal0 in TLList.thy) |
|
52805 | 1754 |
*} |
1755 |
||
52824 | 1756 |
|
53617 | 1757 |
subsection {* Introductory Examples |
1758 |
\label{ssec:primcorec-introductory-examples} *} |
|
52805 | 1759 |
|
53646 | 1760 |
text {* |
1761 |
Primitive corecursion is illustrated through concrete examples based on the |
|
1762 |
codatatypes defined in Section~\ref{ssec:codatatype-introductory-examples}. More |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1763 |
examples can be found in the directory \verb|~~/src/HOL/BNF/Examples|. The code |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1764 |
view is favored in the examples below. Sections |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1765 |
\ref{ssec:primrec-constructor-view} and \ref{ssec:primrec-destructor-view} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1766 |
present the same examples expressed using the constructor and destructor views. |
53646 | 1767 |
*} |
1768 |
||
53644 | 1769 |
subsubsection {* Simple Corecursion |
1770 |
\label{sssec:primcorec-simple-corecursion} *} |
|
1771 |
||
53646 | 1772 |
text {* |
53752 | 1773 |
Following the code view, corecursive calls are allowed on the right-hand side as |
1774 |
long as they occur under a constructor, which itself appears either directly to |
|
1775 |
the right of the equal sign or in a conditional expression: |
|
53646 | 1776 |
*} |
1777 |
||
53826 | 1778 |
primcorec literate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist" where |
54072 | 1779 |
"literate g x = LCons x (literate g (g x))" |
53647 | 1780 |
|
53677 | 1781 |
text {* \blankline *} |
1782 |
||
53826 | 1783 |
primcorec siterate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a stream" where |
54072 | 1784 |
"siterate g x = SCons x (siterate g (g x))" |
53644 | 1785 |
|
53646 | 1786 |
text {* |
1787 |
\noindent |
|
1788 |
The constructor ensures that progress is made---i.e., the function is |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1789 |
\emph{productive}. The above functions compute the infinite lazy list or stream |
54072 | 1790 |
@{text "[x, g x, g (g x), \<dots>]"}. Productivity guarantees that prefixes |
1791 |
@{text "[x, g x, g (g x), \<dots>, (g ^^ k) x]"} of arbitrary finite length |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1792 |
@{text k} can be computed by unfolding the code equation a finite number of |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
1793 |
times. |
53646 | 1794 |
|
53752 | 1795 |
Corecursive functions construct codatatype values, but nothing prevents them |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
1796 |
from also consuming such values. The following function drops every second |
53675 | 1797 |
element in a stream: |
1798 |
*} |
|
1799 |
||
53826 | 1800 |
primcorec every_snd :: "'a stream \<Rightarrow> 'a stream" where |
53675 | 1801 |
"every_snd s = SCons (shd s) (stl (stl s))" |
1802 |
||
1803 |
text {* |
|
53752 | 1804 |
\noindent |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1805 |
Constructs such as @{text "let"}---@{text "in"}, @{text |
53646 | 1806 |
"if"}---@{text "then"}---@{text "else"}, and @{text "case"}---@{text "of"} may |
1807 |
appear around constructors that guard corecursive calls: |
|
1808 |
*} |
|
1809 |
||
54072 | 1810 |
primcorec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
53644 | 1811 |
"lappend xs ys = |
1812 |
(case xs of |
|
1813 |
LNil \<Rightarrow> ys |
|
1814 |
| LCons x xs' \<Rightarrow> LCons x (lappend xs' ys))" |
|
1815 |
||
53646 | 1816 |
text {* |
53752 | 1817 |
\noindent |
54402 | 1818 |
Pattern matching is not supported by @{command primcorec}. Fortunately, it is |
1819 |
easy to generate pattern-maching equations using the \keyw{simps\_of\_case} |
|
1820 |
command provided by the theory \verb|~~/src/HOL/Library/Simps_Case_Conv|. |
|
1821 |
*} |
|
1822 |
||
1823 |
simps_of_case lappend_simps: lappend.code |
|
1824 |
||
1825 |
text {* |
|
1826 |
This generates the lemma collection @{thm [source] lappend_simps}: |
|
1827 |
% |
|
1828 |
\[@{thm lappend_simps(1)[no_vars]} |
|
1829 |
\qquad @{thm lappend_simps(2)[no_vars]}\] |
|
1830 |
% |
|
53646 | 1831 |
Corecursion is useful to specify not only functions but also infinite objects: |
1832 |
*} |
|
1833 |
||
53826 | 1834 |
primcorec infty :: enat where |
53644 | 1835 |
"infty = ESuc infty" |
1836 |
||
53646 | 1837 |
text {* |
53752 | 1838 |
\noindent |
1839 |
The example below constructs a pseudorandom process value. It takes a stream of |
|
53675 | 1840 |
actions (@{text s}), a pseudorandom function generator (@{text f}), and a |
1841 |
pseudorandom seed (@{text n}): |
|
1842 |
*} |
|
1843 |
||
54072 | 1844 |
primcorec |
53752 | 1845 |
random_process :: "'a stream \<Rightarrow> (int \<Rightarrow> int) \<Rightarrow> int \<Rightarrow> 'a process" |
1846 |
where |
|
53675 | 1847 |
"random_process s f n = |
1848 |
(if n mod 4 = 0 then |
|
1849 |
Fail |
|
1850 |
else if n mod 4 = 1 then |
|
1851 |
Skip (random_process s f (f n)) |
|
1852 |
else if n mod 4 = 2 then |
|
1853 |
Action (shd s) (random_process (stl s) f (f n)) |
|
1854 |
else |
|
1855 |
Choice (random_process (every_snd s) (f \<circ> f) (f n)) |
|
1856 |
(random_process (every_snd (stl s)) (f \<circ> f) (f (f n))))" |
|
1857 |
||
1858 |
text {* |
|
1859 |
\noindent |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1860 |
The main disadvantage of the code view is that the conditions are tested |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1861 |
sequentially. This is visible in the generated theorems. The constructor and |
53752 | 1862 |
destructor views offer nonsequential alternatives. |
53675 | 1863 |
*} |
1864 |
||
53644 | 1865 |
|
1866 |
subsubsection {* Mutual Corecursion |
|
1867 |
\label{sssec:primcorec-mutual-corecursion} *} |
|
1868 |
||
53647 | 1869 |
text {* |
1870 |
The syntax for mutually corecursive functions over mutually corecursive |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
1871 |
datatypes is unsurprising: |
53647 | 1872 |
*} |
1873 |
||
53826 | 1874 |
primcorec |
53644 | 1875 |
even_infty :: even_enat and |
1876 |
odd_infty :: odd_enat |
|
1877 |
where |
|
1878 |
"even_infty = Even_ESuc odd_infty" | |
|
1879 |
"odd_infty = Odd_ESuc even_infty" |
|
1880 |
||
1881 |
||
1882 |
subsubsection {* Nested Corecursion |
|
1883 |
\label{sssec:primcorec-nested-corecursion} *} |
|
1884 |
||
53647 | 1885 |
text {* |
1886 |
The next pair of examples generalize the @{const literate} and @{const siterate} |
|
1887 |
functions (Section~\ref{sssec:primcorec-nested-corecursion}) to possibly |
|
1888 |
infinite trees in which subnodes are organized either as a lazy list (@{text |
|
54072 | 1889 |
tree\<^sub>i\<^sub>i}) or as a finite set (@{text tree\<^sub>i\<^sub>s}). They rely on the map functions of |
1890 |
the nesting type constructors to lift the corecursive calls: |
|
53647 | 1891 |
*} |
1892 |
||
53826 | 1893 |
primcorec iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" where |
54072 | 1894 |
"iterate\<^sub>i\<^sub>i g x = Node\<^sub>i\<^sub>i x (lmap (iterate\<^sub>i\<^sub>i g) (g x))" |
53644 | 1895 |
|
53677 | 1896 |
text {* \blankline *} |
1897 |
||
53826 | 1898 |
primcorec iterate\<^sub>i\<^sub>s :: "('a \<Rightarrow> 'a fset) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>s" where |
54072 | 1899 |
"iterate\<^sub>i\<^sub>s g x = Node\<^sub>i\<^sub>s x (fimage (iterate\<^sub>i\<^sub>s g) (g x))" |
53644 | 1900 |
|
52805 | 1901 |
text {* |
53752 | 1902 |
\noindent |
54072 | 1903 |
Both examples follow the usual format for constructor arguments associated |
1904 |
with nested recursive occurrences of the datatype. Consider |
|
1905 |
@{const iterate\<^sub>i\<^sub>i}. The term @{term "g x"} constructs an @{typ "'a llist"} |
|
1906 |
value, which is turned into an @{typ "'a tree\<^sub>i\<^sub>i llist"} value using |
|
1907 |
@{const lmap}. |
|
1908 |
||
1909 |
This format may sometimes feel artificial. The following function constructs |
|
1910 |
a tree with a single, infinite branch from a stream: |
|
1911 |
*} |
|
1912 |
||
1913 |
primcorec tree\<^sub>i\<^sub>i_of_stream :: "'a stream \<Rightarrow> 'a tree\<^sub>i\<^sub>i" where |
|
1914 |
"tree\<^sub>i\<^sub>i_of_stream s = |
|
1915 |
Node\<^sub>i\<^sub>i (shd s) (lmap tree\<^sub>i\<^sub>i_of_stream (LCons (stl s) LNil))" |
|
1916 |
||
1917 |
text {* |
|
1918 |
\noindent |
|
54278 | 1919 |
Fortunately, it is easy to prove the following lemma, where the corecursive call |
1920 |
is moved inside the lazy list constructor, thereby eliminating the need for |
|
1921 |
@{const lmap}: |
|
54072 | 1922 |
*} |
1923 |
||
54278 | 1924 |
lemma tree\<^sub>i\<^sub>i_of_stream_alt: |
54072 | 1925 |
"tree\<^sub>i\<^sub>i_of_stream s = Node\<^sub>i\<^sub>i (shd s) (LCons (tree\<^sub>i\<^sub>i_of_stream (stl s)) LNil)" |
54278 | 1926 |
by (subst tree\<^sub>i\<^sub>i_of_stream.code) simp |
54072 | 1927 |
|
1928 |
text {* |
|
1929 |
The next example illustrates corecursion through functions, which is a bit |
|
1930 |
special. Deterministic finite automata (DFAs) are traditionally defined as |
|
1931 |
5-tuples @{text "(Q, \<Sigma>, \<delta>, q\<^sub>0, F)"}, where @{text Q} is a finite set of states, |
|
53675 | 1932 |
@{text \<Sigma>} is a finite alphabet, @{text \<delta>} is a transition function, @{text q\<^sub>0} |
1933 |
is an initial state, and @{text F} is a set of final states. The following |
|
1934 |
function translates a DFA into a @{type state_machine}: |
|
1935 |
*} |
|
1936 |
||
54071 | 1937 |
primcorec |
1938 |
(*<*)(in early) (*>*)sm_of_dfa :: "('q \<Rightarrow> 'a \<Rightarrow> 'q) \<Rightarrow> 'q set \<Rightarrow> 'q \<Rightarrow> 'a state_machine" |
|
53752 | 1939 |
where |
54182 | 1940 |
"sm_of_dfa \<delta> F q = State_Machine (q \<in> F) (sm_of_dfa \<delta> F \<circ> \<delta> q)" |
53675 | 1941 |
|
53751 | 1942 |
text {* |
1943 |
\noindent |
|
1944 |
The map function for the function type (@{text \<Rightarrow>}) is composition |
|
54181 | 1945 |
(@{text "op \<circ>"}). For convenience, corecursion through functions can |
54182 | 1946 |
also be expressed using $\lambda$-abstractions and function application rather |
54031 | 1947 |
than through composition. For example: |
53751 | 1948 |
*} |
1949 |
||
53826 | 1950 |
primcorec |
53752 | 1951 |
sm_of_dfa :: "('q \<Rightarrow> 'a \<Rightarrow> 'q) \<Rightarrow> 'q set \<Rightarrow> 'q \<Rightarrow> 'a state_machine" |
1952 |
where |
|
54182 | 1953 |
"sm_of_dfa \<delta> F q = State_Machine (q \<in> F) (\<lambda>a. sm_of_dfa \<delta> F (\<delta> q a))" |
53752 | 1954 |
|
1955 |
text {* \blankline *} |
|
1956 |
||
53826 | 1957 |
primcorec empty_sm :: "'a state_machine" where |
53752 | 1958 |
"empty_sm = State_Machine False (\<lambda>_. empty_sm)" |
53751 | 1959 |
|
53752 | 1960 |
text {* \blankline *} |
1961 |
||
53826 | 1962 |
primcorec not_sm :: "'a state_machine \<Rightarrow> 'a state_machine" where |
53752 | 1963 |
"not_sm M = State_Machine (\<not> accept M) (\<lambda>a. not_sm (trans M a))" |
53751 | 1964 |
|
53752 | 1965 |
text {* \blankline *} |
1966 |
||
53826 | 1967 |
primcorec |
53752 | 1968 |
or_sm :: "'a state_machine \<Rightarrow> 'a state_machine \<Rightarrow> 'a state_machine" |
1969 |
where |
|
54072 | 1970 |
"or_sm M N = State_Machine (accept M \<or> accept N) |
1971 |
(\<lambda>a. or_sm (trans M a) (trans N a))" |
|
53751 | 1972 |
|
54182 | 1973 |
text {* |
1974 |
\noindent |
|
1975 |
For recursion through curried $n$-ary functions, $n$ applications of |
|
1976 |
@{term "op \<circ>"} are necessary. The examples below illustrate the case where |
|
1977 |
$n = 2$: |
|
1978 |
*} |
|
1979 |
||
1980 |
codatatype ('a, 'b) state_machine2 = |
|
1981 |
State_Machine2 (accept2: bool) (trans2: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) state_machine2") |
|
1982 |
||
1983 |
text {* \blankline *} |
|
1984 |
||
1985 |
primcorec |
|
1986 |
(*<*)(in early) (*>*)sm2_of_dfa :: "('q \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'q) \<Rightarrow> 'q set \<Rightarrow> 'q \<Rightarrow> ('a, 'b) state_machine2" |
|
1987 |
where |
|
1988 |
"sm2_of_dfa \<delta> F q = State_Machine2 (q \<in> F) (op \<circ> (op \<circ> (sm2_of_dfa \<delta> F)) (\<delta> q))" |
|
1989 |
||
1990 |
text {* \blankline *} |
|
1991 |
||
1992 |
primcorec |
|
1993 |
sm2_of_dfa :: "('q \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'q) \<Rightarrow> 'q set \<Rightarrow> 'q \<Rightarrow> ('a, 'b) state_machine2" |
|
1994 |
where |
|
1995 |
"sm2_of_dfa \<delta> F q = State_Machine2 (q \<in> F) (\<lambda>a b. sm2_of_dfa \<delta> F (\<delta> q a b))" |
|
1996 |
||
53644 | 1997 |
|
1998 |
subsubsection {* Nested-as-Mutual Corecursion |
|
1999 |
\label{sssec:primcorec-nested-as-mutual-corecursion} *} |
|
2000 |
||
53647 | 2001 |
text {* |
2002 |
Just as it is possible to recurse over nested recursive datatypes as if they |
|
2003 |
were mutually recursive |
|
2004 |
(Section~\ref{sssec:primrec-nested-as-mutual-recursion}), it is possible to |
|
53752 | 2005 |
pretend that nested codatatypes are mutually corecursive. For example: |
53647 | 2006 |
*} |
2007 |
||
54287 | 2008 |
(*<*) |
2009 |
context late |
|
2010 |
begin |
|
2011 |
(*>*) |
|
54072 | 2012 |
primcorec |
54287 | 2013 |
iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" and |
53644 | 2014 |
iterates\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a llist \<Rightarrow> 'a tree\<^sub>i\<^sub>i llist" |
2015 |
where |
|
54072 | 2016 |
"iterate\<^sub>i\<^sub>i g x = Node\<^sub>i\<^sub>i x (iterates\<^sub>i\<^sub>i g (g x))" | |
2017 |
"iterates\<^sub>i\<^sub>i g xs = |
|
53644 | 2018 |
(case xs of |
2019 |
LNil \<Rightarrow> LNil |
|
54072 | 2020 |
| LCons x xs' \<Rightarrow> LCons (iterate\<^sub>i\<^sub>i g x) (iterates\<^sub>i\<^sub>i g xs'))" |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2021 |
|
54287 | 2022 |
text {* |
2023 |
\noindent |
|
2024 |
Coinduction rules are generated as |
|
2025 |
@{thm [source] iterate\<^sub>i\<^sub>i.coinduct}, |
|
2026 |
@{thm [source] iterates\<^sub>i\<^sub>i.coinduct}, and |
|
2027 |
@{thm [source] iterate\<^sub>i\<^sub>i_iterates\<^sub>i\<^sub>i.coinduct} |
|
2028 |
and analogously for @{text strong_coinduct}. These rules and the |
|
2029 |
underlying corecursors are generated on a per-need basis and are kept in a cache |
|
2030 |
to speed up subsequent definitions. |
|
2031 |
*} |
|
2032 |
||
2033 |
(*<*) |
|
2034 |
end |
|
2035 |
(*>*) |
|
2036 |
||
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2037 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2038 |
subsubsection {* Constructor View |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2039 |
\label{ssec:primrec-constructor-view} *} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2040 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2041 |
(*<*) |
54182 | 2042 |
locale ctr_view |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2043 |
begin |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2044 |
(*>*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2045 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2046 |
text {* |
53750 | 2047 |
The constructor view is similar to the code view, but there is one separate |
2048 |
conditional equation per constructor rather than a single unconditional |
|
2049 |
equation. Examples that rely on a single constructor, such as @{const literate} |
|
2050 |
and @{const siterate}, are identical in both styles. |
|
2051 |
||
2052 |
Here is an example where there is a difference: |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2053 |
*} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2054 |
|
53826 | 2055 |
primcorec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2056 |
"lnull xs \<Longrightarrow> lnull ys \<Longrightarrow> lappend xs ys = LNil" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2057 |
"_ \<Longrightarrow> lappend xs ys = LCons (lhd (if lnull xs then ys else xs)) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2058 |
(if xs = LNil then ltl ys else lappend (ltl xs) ys)" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2059 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2060 |
text {* |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2061 |
\noindent |
53752 | 2062 |
With the constructor view, we must distinguish between the @{const LNil} and |
2063 |
the @{const LCons} case. The condition for @{const LCons} is |
|
2064 |
left implicit, as the negation of that for @{const LNil}. |
|
53750 | 2065 |
|
2066 |
For this example, the constructor view is slighlty more involved than the |
|
2067 |
code equation. Recall the code view version presented in |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2068 |
Section~\ref{sssec:primcorec-simple-corecursion}. |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2069 |
% TODO: \[{thm code_view.lappend.code}\] |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2070 |
The constructor view requires us to analyze the second argument (@{term ys}). |
53752 | 2071 |
The code equation generated from the constructor view also suffers from this. |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2072 |
% TODO: \[{thm lappend.code}\] |
53750 | 2073 |
|
53752 | 2074 |
In contrast, the next example is arguably more naturally expressed in the |
2075 |
constructor view: |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2076 |
*} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2077 |
|
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53829
diff
changeset
|
2078 |
primcorec |
53752 | 2079 |
random_process :: "'a stream \<Rightarrow> (int \<Rightarrow> int) \<Rightarrow> int \<Rightarrow> 'a process" |
2080 |
where |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2081 |
"n mod 4 = 0 \<Longrightarrow> random_process s f n = Fail" | |
53752 | 2082 |
"n mod 4 = 1 \<Longrightarrow> |
2083 |
random_process s f n = Skip (random_process s f (f n))" | |
|
2084 |
"n mod 4 = 2 \<Longrightarrow> |
|
2085 |
random_process s f n = Action (shd s) (random_process (stl s) f (f n))" | |
|
2086 |
"n mod 4 = 3 \<Longrightarrow> |
|
2087 |
random_process s f n = Choice (random_process (every_snd s) f (f n)) |
|
53826 | 2088 |
(random_process (every_snd (stl s)) f (f n))" |
2089 |
(*<*) |
|
53644 | 2090 |
end |
2091 |
(*>*) |
|
52805 | 2092 |
|
53750 | 2093 |
text {* |
53752 | 2094 |
\noindent |
53750 | 2095 |
Since there is no sequentiality, we can apply the equation for @{const Choice} |
53752 | 2096 |
without having first to discharge @{term "n mod (4\<Colon>int) \<noteq> 0"}, |
2097 |
@{term "n mod (4\<Colon>int) \<noteq> 1"}, and |
|
2098 |
@{term "n mod (4\<Colon>int) \<noteq> 2"}. |
|
53750 | 2099 |
The price to pay for this elegance is that we must discharge exclusivity proof |
2100 |
obligations, one for each pair of conditions |
|
53752 | 2101 |
@{term "(n mod (4\<Colon>int) = i, n mod (4\<Colon>int) = j)"} |
2102 |
with @{term "i < j"}. If we prefer not to discharge any obligations, we can |
|
2103 |
enable the @{text "sequential"} option. This pushes the problem to the users of |
|
2104 |
the generated properties. |
|
53750 | 2105 |
%Here are more examples to conclude: |
2106 |
*} |
|
2107 |
||
52824 | 2108 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2109 |
subsubsection {* Destructor View |
53752 | 2110 |
\label{ssec:primrec-destructor-view} *} |
2111 |
||
2112 |
(*<*) |
|
54182 | 2113 |
locale dtr_view |
53752 | 2114 |
begin |
2115 |
(*>*) |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2116 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2117 |
text {* |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2118 |
The destructor view is in many respects dual to the constructor view. Conditions |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2119 |
determine which constructor to choose, and these conditions are interpreted |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2120 |
sequentially or not depending on the @{text "sequential"} option. |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2121 |
Consider the following examples: |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2122 |
*} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2123 |
|
53826 | 2124 |
primcorec literate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a llist" where |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2125 |
"\<not> lnull (literate _ x)" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2126 |
"lhd (literate _ x) = x" | |
54072 | 2127 |
"ltl (literate g x) = literate g (g x)" |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2128 |
|
53752 | 2129 |
text {* \blankline *} |
2130 |
||
53826 | 2131 |
primcorec siterate :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a stream" where |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2132 |
"shd (siterate _ x) = x" | |
54072 | 2133 |
"stl (siterate g x) = siterate g (g x)" |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2134 |
|
53752 | 2135 |
text {* \blankline *} |
2136 |
||
53826 | 2137 |
primcorec every_snd :: "'a stream \<Rightarrow> 'a stream" where |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2138 |
"shd (every_snd s) = shd s" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2139 |
"stl (every_snd s) = stl (stl s)" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2140 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2141 |
text {* |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2142 |
\noindent |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2143 |
The first formula in the @{const literate} specification indicates which |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2144 |
constructor to choose. For @{const siterate} and @{const every_snd}, no such |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2145 |
formula is necessary, since the type has only one constructor. The last two |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2146 |
formulas are equations specifying the value of the result for the relevant |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2147 |
selectors. Corecursive calls appear directly to the right of the equal sign. |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2148 |
Their arguments are unrestricted. |
53750 | 2149 |
|
2150 |
The next example shows how to specify functions that rely on more than one |
|
2151 |
constructor: |
|
2152 |
*} |
|
2153 |
||
53826 | 2154 |
primcorec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
53750 | 2155 |
"lnull xs \<Longrightarrow> lnull ys \<Longrightarrow> lnull (lappend xs ys)" | |
2156 |
"lhd (lappend xs ys) = lhd (if lnull xs then ys else xs)" | |
|
2157 |
"ltl (lappend xs ys) = (if xs = LNil then ltl ys else lappend (ltl xs) ys)" |
|
2158 |
||
2159 |
text {* |
|
2160 |
\noindent |
|
2161 |
For a codatatype with $n$ constructors, it is sufficient to specify $n - 1$ |
|
2162 |
discriminator formulas. The command will then assume that the remaining |
|
2163 |
constructor should be taken otherwise. This can be made explicit by adding |
|
2164 |
*} |
|
2165 |
||
2166 |
(*<*) |
|
2167 |
end |
|
2168 |
||
54182 | 2169 |
locale dtr_view2 |
2170 |
begin |
|
2171 |
||
53826 | 2172 |
primcorec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
53750 | 2173 |
"lnull xs \<Longrightarrow> lnull ys \<Longrightarrow> lnull (lappend xs ys)" | |
2174 |
(*>*) |
|
53752 | 2175 |
"_ \<Longrightarrow> \<not> lnull (lappend xs ys)" |
2176 |
(*<*) | |
|
53750 | 2177 |
"lhd (lappend xs ys) = lhd (if lnull xs then ys else xs)" | |
2178 |
"ltl (lappend xs ys) = (if xs = LNil then ltl ys else lappend (ltl xs) ys)" |
|
2179 |
(*>*) |
|
2180 |
||
2181 |
text {* |
|
2182 |
\noindent |
|
53752 | 2183 |
to the specification. The generated selector theorems are conditional. |
2184 |
||
2185 |
The next example illustrates how to cope with selectors defined for several |
|
53750 | 2186 |
constructors: |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2187 |
*} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2188 |
|
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53829
diff
changeset
|
2189 |
primcorec |
53752 | 2190 |
random_process :: "'a stream \<Rightarrow> (int \<Rightarrow> int) \<Rightarrow> int \<Rightarrow> 'a process" |
2191 |
where |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2192 |
"n mod 4 = 0 \<Longrightarrow> is_Fail (random_process s f n)" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2193 |
"n mod 4 = 1 \<Longrightarrow> is_Skip (random_process s f n)" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2194 |
"n mod 4 = 2 \<Longrightarrow> is_Action (random_process s f n)" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2195 |
"n mod 4 = 3 \<Longrightarrow> is_Choice (random_process s f n)" | |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53829
diff
changeset
|
2196 |
"cont (random_process s f n) = random_process s f (f n)" of Skip | |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2197 |
"prefix (random_process s f n) = shd s" | |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53829
diff
changeset
|
2198 |
"cont (random_process s f n) = random_process (stl s) f (f n)" of Action | |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2199 |
"left (random_process s f n) = random_process (every_snd s) f (f n)" | |
53831
80423b9080cf
support "of" syntax to disambiguate selector equations
panny
parents:
53829
diff
changeset
|
2200 |
"right (random_process s f n) = random_process (every_snd (stl s)) f (f n)" |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2201 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2202 |
text {* |
53750 | 2203 |
\noindent |
2204 |
Using the @{text "of"} keyword, different equations are specified for @{const |
|
2205 |
cont} depending on which constructor is selected. |
|
2206 |
||
2207 |
Here are more examples to conclude: |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2208 |
*} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2209 |
|
53826 | 2210 |
primcorec |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2211 |
even_infty :: even_enat and |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2212 |
odd_infty :: odd_enat |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2213 |
where |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2214 |
"\<not> is_Even_EZero even_infty" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2215 |
"un_Even_ESuc even_infty = odd_infty" | |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2216 |
"un_Odd_ESuc odd_infty = even_infty" |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2217 |
|
53752 | 2218 |
text {* \blankline *} |
2219 |
||
53826 | 2220 |
primcorec iterate\<^sub>i\<^sub>i :: "('a \<Rightarrow> 'a llist) \<Rightarrow> 'a \<Rightarrow> 'a tree\<^sub>i\<^sub>i" where |
54072 | 2221 |
"lbl\<^sub>i\<^sub>i (iterate\<^sub>i\<^sub>i g x) = x" | |
2222 |
"sub\<^sub>i\<^sub>i (iterate\<^sub>i\<^sub>i g x) = lmap (iterate\<^sub>i\<^sub>i g) (g x)" |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2223 |
(*<*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2224 |
end |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2225 |
(*>*) |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2226 |
|
53750 | 2227 |
|
53617 | 2228 |
subsection {* Command Syntax |
2229 |
\label{ssec:primcorec-command-syntax} *} |
|
2230 |
||
2231 |
||
53826 | 2232 |
subsubsection {* \keyw{primcorec} and \keyw{primcorecursive} |
53753
ae7f50e70c09
renamed "primcorec" to "primcorecursive", to open the door to a 'theory -> theory' command called "primcorec" (cf. "fun" vs. "function")
blanchet
parents:
53752
diff
changeset
|
2233 |
\label{sssec:primcorecursive-and-primcorec} *} |
52840 | 2234 |
|
2235 |
text {* |
|
53829 | 2236 |
\begin{matharray}{rcl} |
2237 |
@{command_def "primcorec"} & : & @{text "local_theory \<rightarrow> local_theory"} \\ |
|
2238 |
@{command_def "primcorecursive"} & : & @{text "local_theory \<rightarrow> proof(prove)"} |
|
2239 |
\end{matharray} |
|
52840 | 2240 |
|
2241 |
@{rail " |
|
53829 | 2242 |
(@@{command primcorec} | @@{command primcorecursive}) target? \\ @{syntax pcr_option}? fixes @'where' |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2243 |
(@{syntax pcr_formula} + '|') |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2244 |
; |
53828 | 2245 |
@{syntax_def pcr_option}: '(' ('sequential' | 'exhaustive') ')' |
52840 | 2246 |
; |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2247 |
@{syntax_def pcr_formula}: thmdecl? prop (@'of' (term * ))? |
52840 | 2248 |
"} |
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2249 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2250 |
The optional target is optionally followed by a corecursion-specific option: |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2251 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2252 |
\begin{itemize} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2253 |
\setlength{\itemsep}{0pt} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2254 |
|
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2255 |
\item |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2256 |
The @{text "sequential"} option indicates that the conditions in specifications |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2257 |
expressed using the constructor or destructor view are to be interpreted |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2258 |
sequentially. |
53826 | 2259 |
|
2260 |
\item |
|
2261 |
The @{text "exhaustive"} option indicates that the conditions in specifications |
|
2262 |
expressed using the constructor or destructor view cover all possible cases. |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2263 |
\end{itemize} |
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2264 |
|
53826 | 2265 |
\noindent |
2266 |
The @{command primcorec} command is an abbreviation for @{command primcorecursive} with |
|
2267 |
@{text "by auto?"} to discharge any emerging proof obligations. |
|
52840 | 2268 |
*} |
52794 | 2269 |
|
52824 | 2270 |
|
53619 | 2271 |
(* |
52840 | 2272 |
subsection {* Generated Theorems |
2273 |
\label{ssec:primcorec-generated-theorems} *} |
|
53619 | 2274 |
*) |
52794 | 2275 |
|
2276 |
||
53623 | 2277 |
(* |
2278 |
subsection {* Recursive Default Values for Selectors |
|
2279 |
\label{ssec:primcorec-recursive-default-values-for-selectors} *} |
|
2280 |
||
2281 |
text {* |
|
2282 |
partial_function to the rescue |
|
2283 |
*} |
|
2284 |
*) |
|
2285 |
||
2286 |
||
52827 | 2287 |
section {* Registering Bounded Natural Functors |
52805 | 2288 |
\label{sec:registering-bounded-natural-functors} *} |
52792 | 2289 |
|
52805 | 2290 |
text {* |
53647 | 2291 |
The (co)datatype package can be set up to allow nested recursion through |
2292 |
arbitrary type constructors, as long as they adhere to the BNF requirements and |
|
2293 |
are registered as BNFs. |
|
52805 | 2294 |
*} |
2295 |
||
52824 | 2296 |
|
53619 | 2297 |
(* |
53617 | 2298 |
subsection {* Introductory Example |
2299 |
\label{ssec:bnf-introductory-example} *} |
|
52805 | 2300 |
|
2301 |
text {* |
|
2302 |
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and |
|
2303 |
\verb|~~/src/HOL/BNF/More_BNFs.thy|. |
|
52806 | 2304 |
|
53617 | 2305 |
%Mention distinction between live and dead type arguments; |
2306 |
% * and existence of map, set for those |
|
2307 |
%mention =>. |
|
52805 | 2308 |
*} |
53619 | 2309 |
*) |
52794 | 2310 |
|
52824 | 2311 |
|
53617 | 2312 |
subsection {* Command Syntax |
2313 |
\label{ssec:bnf-command-syntax} *} |
|
2314 |
||
2315 |
||
53621 | 2316 |
subsubsection {* \keyw{bnf} |
2317 |
\label{sssec:bnf} *} |
|
52794 | 2318 |
|
53028 | 2319 |
text {* |
53829 | 2320 |
\begin{matharray}{rcl} |
2321 |
@{command_def "bnf"} & : & @{text "local_theory \<rightarrow> proof(prove)"} |
|
2322 |
\end{matharray} |
|
2323 |
||
53028 | 2324 |
@{rail " |
54421 | 2325 |
@@{command bnf} target? (name ':')? typ \\ |
2326 |
'map:' term ('sets:' (term +))? 'bd:' term \\ |
|
2327 |
('wits:' (term +))? ('rel:' term)? |
|
53028 | 2328 |
"} |
2329 |
*} |
|
52805 | 2330 |
|
53617 | 2331 |
|
54187 | 2332 |
subsubsection {* \keyw{bnf\_decl} |
2333 |
\label{sssec:bnf-decl} *} |
|
2334 |
||
2335 |
text {* |
|
2336 |
%%% TODO: use command_def once the command is available |
|
2337 |
\begin{matharray}{rcl} |
|
2338 |
@{text "bnf_decl"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
2339 |
\end{matharray} |
|
2340 |
||
2341 |
@{rail " |
|
54602 | 2342 |
@@{command bnf_decl} target? @{syntax dt_name} |
2343 |
; |
|
2344 |
@{syntax_def dt_name}: @{syntax tyargs}? name @{syntax map_rel}? mixfix? |
|
2345 |
; |
|
2346 |
@{syntax_def tyargs}: typefree | '(' (((name | '-') ':')? typefree + ',') ')' |
|
2347 |
; |
|
2348 |
@{syntax_def map_rel}: '(' ((('map' | 'rel') ':' name) +) ')' |
|
54187 | 2349 |
"} |
54602 | 2350 |
|
2351 |
Declares a fresh type and fresh constants (map, set, relator, cardinal bound) |
|
2352 |
and asserts the bnf properties for these constants as axioms. Additionally, |
|
2353 |
type arguments may be marked as dead (by using @{syntax "-"} instead of a name for the |
|
2354 |
set function)---this is the only difference of @{syntax dt_name} compared to |
|
2355 |
the syntax used by the @{command datatype_new}/@{command codatatype} commands. |
|
2356 |
||
2357 |
The axioms are sound, since one there exists a bnf of any given arity. |
|
54187 | 2358 |
*} |
2359 |
||
2360 |
||
53621 | 2361 |
subsubsection {* \keyw{print\_bnfs} |
2362 |
\label{sssec:print-bnfs} *} |
|
53617 | 2363 |
|
2364 |
text {* |
|
53829 | 2365 |
\begin{matharray}{rcl} |
2366 |
@{command_def "print_bnfs"} & : & @{text "local_theory \<rightarrow>"} |
|
2367 |
\end{matharray} |
|
2368 |
||
53647 | 2369 |
@{rail " |
53829 | 2370 |
@@{command print_bnfs} |
53647 | 2371 |
"} |
53617 | 2372 |
*} |
2373 |
||
2374 |
||
2375 |
section {* Deriving Destructors and Theorems for Free Constructors |
|
2376 |
\label{sec:deriving-destructors-and-theorems-for-free-constructors} *} |
|
52794 | 2377 |
|
52805 | 2378 |
text {* |
53623 | 2379 |
The derivation of convenience theorems for types equipped with free constructors, |
53829 | 2380 |
as performed internally by @{command datatype_new} and @{command codatatype}, |
53623 | 2381 |
is available as a stand-alone command called @{command wrap_free_constructors}. |
52794 | 2382 |
|
53617 | 2383 |
% * need for this is rare but may arise if you want e.g. to add destructors to |
2384 |
% a type not introduced by ... |
|
2385 |
% |
|
2386 |
% * also useful for compatibility with old package, e.g. add destructors to |
|
2387 |
% old \keyw{datatype} |
|
2388 |
% |
|
2389 |
% * @{command wrap_free_constructors} |
|
53623 | 2390 |
% * @{text "no_discs_sels"}, @{text "rep_compat"} |
53617 | 2391 |
% * hack to have both co and nonco view via locale (cf. ext nats) |
54616 | 2392 |
% * code generator |
2393 |
% * eq, refl, simps |
|
52805 | 2394 |
*} |
52792 | 2395 |
|
52824 | 2396 |
|
53619 | 2397 |
(* |
53617 | 2398 |
subsection {* Introductory Example |
2399 |
\label{ssec:ctors-introductory-example} *} |
|
53619 | 2400 |
*) |
52794 | 2401 |
|
52824 | 2402 |
|
53617 | 2403 |
subsection {* Command Syntax |
2404 |
\label{ssec:ctors-command-syntax} *} |
|
2405 |
||
2406 |
||
53621 | 2407 |
subsubsection {* \keyw{wrap\_free\_constructors} |
53675 | 2408 |
\label{sssec:wrap-free-constructors} *} |
52828 | 2409 |
|
53018 | 2410 |
text {* |
53829 | 2411 |
\begin{matharray}{rcl} |
2412 |
@{command_def "wrap_free_constructors"} & : & @{text "local_theory \<rightarrow> proof(prove)"} |
|
2413 |
\end{matharray} |
|
53018 | 2414 |
|
2415 |
@{rail " |
|
53829 | 2416 |
@@{command wrap_free_constructors} target? @{syntax dt_options} \\ |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
2417 |
term_list name @{syntax wfc_discs_sels}? |
53018 | 2418 |
; |
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
2419 |
@{syntax_def wfc_discs_sels}: name_list (name_list_list name_term_list_list? )? |
53018 | 2420 |
; |
53534 | 2421 |
@{syntax_def name_term}: (name ':' term) |
54421 | 2422 |
; |
2423 |
X_list: '[' (X + ',') ']' |
|
53018 | 2424 |
"} |
2425 |
||
53617 | 2426 |
% options: no_discs_sels rep_compat |
53028 | 2427 |
|
53829 | 2428 |
\noindent |
53542 | 2429 |
Section~\ref{ssec:datatype-generated-theorems} lists the generated theorems. |
53018 | 2430 |
*} |
52828 | 2431 |
|
52794 | 2432 |
|
53617 | 2433 |
(* |
52827 | 2434 |
section {* Standard ML Interface |
52805 | 2435 |
\label{sec:standard-ml-interface} *} |
52792 | 2436 |
|
52805 | 2437 |
text {* |
53623 | 2438 |
The package's programmatic interface. |
52805 | 2439 |
*} |
53617 | 2440 |
*) |
52794 | 2441 |
|
2442 |
||
53617 | 2443 |
(* |
52827 | 2444 |
section {* Interoperability |
52805 | 2445 |
\label{sec:interoperability} *} |
52794 | 2446 |
|
52805 | 2447 |
text {* |
53623 | 2448 |
The package's interaction with other Isabelle packages and tools, such as the |
2449 |
code generator and the counterexample generators. |
|
52805 | 2450 |
*} |
52794 | 2451 |
|
52824 | 2452 |
|
52828 | 2453 |
subsection {* Transfer and Lifting |
2454 |
\label{ssec:transfer-and-lifting} *} |
|
52794 | 2455 |
|
52824 | 2456 |
|
52828 | 2457 |
subsection {* Code Generator |
2458 |
\label{ssec:code-generator} *} |
|
52794 | 2459 |
|
52824 | 2460 |
|
52828 | 2461 |
subsection {* Quickcheck |
2462 |
\label{ssec:quickcheck} *} |
|
52794 | 2463 |
|
52824 | 2464 |
|
52828 | 2465 |
subsection {* Nitpick |
2466 |
\label{ssec:nitpick} *} |
|
52794 | 2467 |
|
52824 | 2468 |
|
52828 | 2469 |
subsection {* Nominal Isabelle |
2470 |
\label{ssec:nominal-isabelle} *} |
|
53617 | 2471 |
*) |
52794 | 2472 |
|
52805 | 2473 |
|
53617 | 2474 |
(* |
52827 | 2475 |
section {* Known Bugs and Limitations |
52805 | 2476 |
\label{sec:known-bugs-and-limitations} *} |
2477 |
||
2478 |
text {* |
|
53623 | 2479 |
Known open issues of the package. |
52805 | 2480 |
*} |
52794 | 2481 |
|
2482 |
text {* |
|
53753
ae7f50e70c09
renamed "primcorec" to "primcorecursive", to open the door to a 'theory -> theory' command called "primcorec" (cf. "fun" vs. "function")
blanchet
parents:
53752
diff
changeset
|
2483 |
%* primcorecursive and primcorec is unfinished |
53617 | 2484 |
% |
2485 |
%* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting) |
|
2486 |
% |
|
2487 |
%* issues with HOL-Proofs? |
|
2488 |
% |
|
2489 |
%* partial documentation |
|
2490 |
% |
|
2491 |
%* no way to register "sum" and "prod" as (co)datatypes to enable N2M reduction for them |
|
2492 |
% (for @{command datatype_new_compat} and prim(co)rec) |
|
2493 |
% |
|
53619 | 2494 |
% * a fortiori, no way to register same type as both data- and codatatype |
53617 | 2495 |
% |
2496 |
%* no recursion through unused arguments (unlike with the old package) |
|
2497 |
% |
|
2498 |
%* in a locale, cannot use locally fixed types (because of limitation in typedef)? |
|
53619 | 2499 |
% |
2500 |
% *names of variables suboptimal |
|
52822 | 2501 |
*} |
53675 | 2502 |
*) |
52822 | 2503 |
|
2504 |
||
2505 |
text {* |
|
53863
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
2506 |
\section*{Acknowledgment} |
c7364dca96f2
textual improvements following Christian Sternagel's feedback
blanchet
parents:
53857
diff
changeset
|
2507 |
|
53749
b37db925b663
adapted primcorec documentation to reflect the three views
blanchet
parents:
53748
diff
changeset
|
2508 |
Tobias Nipkow and Makarius Wenzel encouraged us to implement the new |
53617 | 2509 |
(co)datatype package. Andreas Lochbihler provided lots of comments on earlier |
2510 |
versions of the package, especially for the coinductive part. Brian Huffman |
|
2511 |
suggested major simplifications to the internal constructions, much of which has |
|
2512 |
yet to be implemented. Florian Haftmann and Christian Urban provided general |
|
53675 | 2513 |
advice on Isabelle and package writing. Stefan Milius and Lutz Schr\"oder |
54146 | 2514 |
found an elegant proof to eliminate one of the BNF assumptions. Andreas |
2515 |
Lochbihler and Christian Sternagel suggested many textual improvements to this |
|
2516 |
tutorial. |
|
52794 | 2517 |
*} |
53617 | 2518 |
|
52792 | 2519 |
end |