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