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