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