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