52792
|
1 |
(* Title: Doc/Datatypes/Datatypes.thy
|
|
2 |
Author: Jasmin Blanchette, TU Muenchen
|
|
3 |
|
|
4 |
Tutorial for (co)datatype definitions with the new package.
|
|
5 |
*)
|
|
6 |
|
|
7 |
theory Datatypes
|
|
8 |
imports BNF
|
|
9 |
begin
|
|
10 |
|
|
11 |
section {* Introduction *}
|
|
12 |
|
|
13 |
text {*
|
52794
|
14 |
The 2013 edition of Isabelle introduced new definitional package for datatypes
|
|
15 |
and codatatypes. The datatype support is similar to that provided by the earlier
|
|
16 |
package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL};
|
|
17 |
indeed, replacing \cmd{datatype} by \cmd{datatype\_new} is usually sufficient
|
|
18 |
to port existing specifications to the new package. What makes the new package
|
|
19 |
attractive is that it supports definitions with recursion through a large class
|
|
20 |
of non-datatypes, notably finite sets:
|
52792
|
21 |
*}
|
|
22 |
|
|
23 |
datatype_new 'a tree = Node 'a "('a tree fset)"
|
|
24 |
|
|
25 |
text {*
|
52794
|
26 |
\noindent
|
|
27 |
Another advantage of the new package is that it supports local definitions:
|
52792
|
28 |
*}
|
|
29 |
|
|
30 |
context linorder
|
|
31 |
begin
|
52794
|
32 |
datatype_new flag = Less | Eq | Greater
|
52792
|
33 |
end
|
|
34 |
|
|
35 |
text {*
|
52794
|
36 |
\noindent
|
|
37 |
Finally, the package also provides some convenience, notably automatically
|
|
38 |
generated destructors. For example, the command
|
|
39 |
*}
|
52792
|
40 |
|
52794
|
41 |
(*<*)hide_const Nil Cons hd tl(*>*)
|
|
42 |
datatype_new 'a list = null: Nil | Cons (hd: 'a) (tl: "'a list")
|
|
43 |
|
|
44 |
text {*
|
|
45 |
\noindent
|
|
46 |
introduces a discriminator @{const null} and a pair of selectors @{const hd} and
|
|
47 |
@{const tl} characterized as follows:
|
|
48 |
|
|
49 |
@{thm [display] list.collapse[no_vars]}
|
|
50 |
|
|
51 |
The command \cmd{datatype\_new} is expected to displace \cmd{datatype} in a future
|
|
52 |
release. Authors of new theories are encouraged to use \cmd{datatype\_new}, and
|
|
53 |
maintainers of older theories might want to consider upgrading now.
|
|
54 |
|
|
55 |
The package also provides codatatypes (or ``coinductive datatypes''), which may
|
|
56 |
have infinite values. The following command introduces a type of infinite
|
|
57 |
streams:
|
|
58 |
*}
|
|
59 |
|
|
60 |
codatatype 'a stream = Stream 'a "('a stream)"
|
|
61 |
|
|
62 |
text {*
|
|
63 |
\noindent
|
|
64 |
Mixed inductive--coinductive recursion is possible via nesting.
|
|
65 |
Compare the following four examples:
|
|
66 |
*}
|
|
67 |
|
|
68 |
datatype_new 'a treeFF = TreeFF 'a "('a treeFF list)"
|
|
69 |
datatype_new 'a treeFI = TreeFI 'a "('a treeFF stream)"
|
|
70 |
codatatype 'a treeIF = TreeIF 'a "('a treeFF list)"
|
|
71 |
codatatype 'a treeII = TreeII 'a "('a treeFF stream)"
|
52792
|
72 |
|
52794
|
73 |
text {*
|
|
74 |
To use the package, it is necessary to import the @{theory BNF} theory, which
|
|
75 |
can be precompiled as the \textit{HOL-BNF}:
|
|
76 |
*}
|
|
77 |
|
|
78 |
text {*
|
|
79 |
\noindent
|
|
80 |
\texttt{isabelle build -b HOL-BNF}
|
|
81 |
*}
|
|
82 |
|
|
83 |
text {*
|
|
84 |
* TODO: LCF tradition
|
|
85 |
* internals: LICS paper
|
52792
|
86 |
*}
|
|
87 |
|
52794
|
88 |
text {*
|
|
89 |
This tutorial is organized as follows:
|
|
90 |
|
|
91 |
* datatypes
|
|
92 |
* primitive recursive functions
|
|
93 |
* codatatypes
|
|
94 |
* primitive corecursive functions
|
|
95 |
|
|
96 |
the following sections focus on more technical aspects:
|
|
97 |
how to register bounded natural functors (BNFs), i.e., type
|
|
98 |
constructors via which (co)datatypes can (co)recurse,
|
|
99 |
and how to derive convenience theorems for free constructors,
|
|
100 |
as performed internally by \cmd{datatype\_new} and \cmd{codatatype}.
|
|
101 |
|
|
102 |
then: Standard ML interface
|
|
103 |
|
|
104 |
interaction with other tools
|
|
105 |
* code generator (and Quickcheck)
|
|
106 |
* Nitpick
|
|
107 |
*}
|
|
108 |
|
52792
|
109 |
section {* Defining Datatypes *}
|
|
110 |
|
52794
|
111 |
subsection {* Introductory Examples *}
|
|
112 |
|
|
113 |
subsubsection {* Nonrecursive Types *}
|
|
114 |
|
|
115 |
subsubsection {* Simple Recursion *}
|
|
116 |
|
|
117 |
subsubsection {* Mutual Recursion *}
|
|
118 |
|
|
119 |
subsubsection {* Nested Recursion *}
|
|
120 |
|
|
121 |
subsubsection {* Auxiliary Constants *}
|
|
122 |
|
|
123 |
subsection {* General Syntax *}
|
|
124 |
|
|
125 |
subsection {* Characteristic Theorems *}
|
|
126 |
|
|
127 |
subsection {* Compatibility Issues *}
|
|
128 |
|
52792
|
129 |
|
|
130 |
section {* Defining Primitive Recursive Functions *}
|
|
131 |
|
52794
|
132 |
subsection {* Introductory Examples *}
|
|
133 |
|
|
134 |
subsection {* General Syntax *}
|
|
135 |
|
|
136 |
subsection {* Characteristic Theorems *}
|
|
137 |
|
|
138 |
subsection {* Compatibility Issues *}
|
|
139 |
|
|
140 |
|
52792
|
141 |
section {* Defining Codatatypes *}
|
|
142 |
|
52794
|
143 |
subsection {* Introductory Examples *}
|
|
144 |
|
|
145 |
subsection {* General Syntax *}
|
|
146 |
|
|
147 |
subsection {* Characteristic Theorems *}
|
|
148 |
|
|
149 |
|
52792
|
150 |
section {* Defining Primitive Corecursive Functions *}
|
|
151 |
|
52794
|
152 |
subsection {* Introductory Examples *}
|
|
153 |
|
|
154 |
subsection {* General Syntax *}
|
|
155 |
|
|
156 |
subsection {* Characteristic Theorems *}
|
|
157 |
|
|
158 |
subsection {* Compatibility Issues *}
|
|
159 |
|
|
160 |
|
52792
|
161 |
section {* Registering Bounded Natural Functors *}
|
|
162 |
|
52794
|
163 |
subsection {* Introductory Example *}
|
|
164 |
|
|
165 |
subsection {* General Syntax *}
|
|
166 |
|
|
167 |
|
52792
|
168 |
section {* Generating Free Constructor Theorems *}
|
|
169 |
|
52794
|
170 |
subsection {* Introductory Example *}
|
|
171 |
|
|
172 |
subsection {* General Syntax *}
|
|
173 |
|
|
174 |
section {* Registering Bounded Natural Functors *}
|
|
175 |
|
|
176 |
section {* Standard ML Interface *}
|
|
177 |
|
|
178 |
section {* Interoperability Issues *}
|
|
179 |
|
|
180 |
subsection {* Transfer and Lifting *}
|
|
181 |
|
|
182 |
subsection {* Code Generator *}
|
|
183 |
|
|
184 |
subsection {* Quickcheck *}
|
|
185 |
|
|
186 |
subsection {* Nitpick *}
|
|
187 |
|
|
188 |
subsection {* Nominal Isabelle *}
|
|
189 |
|
|
190 |
section {* Known Bugs and Limitations *}
|
|
191 |
|
|
192 |
text {*
|
|
193 |
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
|
|
194 |
*}
|
52792
|
195 |
|
|
196 |
end
|