author | wenzelm |
Sat, 21 Jun 2014 21:33:00 +0200 | |
changeset 57348 | a6b1847b6335 |
parent 57347 | 5c8f4a35d8d7 |
child 57349 | 4817154180d6 |
permissions | -rw-r--r-- |
57347 | 1 |
(*:wrap=hard:maxLineLen=78:*) |
2 |
||
29755 | 3 |
theory "ML" |
4 |
imports Base |
|
5 |
begin |
|
18538 | 6 |
|
39822 | 7 |
chapter {* Isabelle/ML *} |
20489 | 8 |
|
39823 | 9 |
text {* Isabelle/ML is best understood as a certain culture based on |
10 |
Standard ML. Thus it is not a new programming language, but a |
|
11 |
certain way to use SML at an advanced level within the Isabelle |
|
12 |
environment. This covers a variety of aspects that are geared |
|
13 |
towards an efficient and robust platform for applications of formal |
|
14 |
logic with fully foundational proof construction --- according to |
|
40126 | 15 |
the well-known \emph{LCF principle}. There is specific |
16 |
infrastructure with library modules to address the needs of this |
|
17 |
difficult task. For example, the raw parallel programming model of |
|
18 |
Poly/ML is presented as considerably more abstract concept of |
|
19 |
\emph{future values}, which is then used to augment the inference |
|
20 |
kernel, proof interpreter, and theory loader accordingly. |
|
39823 | 21 |
|
22 |
The main aspects of Isabelle/ML are introduced below. These |
|
23 |
first-hand explanations should help to understand how proper |
|
24 |
Isabelle/ML is to be read and written, and to get access to the |
|
25 |
wealth of experience that is expressed in the source text and its |
|
26 |
history of changes.\footnote{See |
|
54703 | 27 |
@{url "http://isabelle.in.tum.de/repos/isabelle"} for the full |
39823 | 28 |
Mercurial history. There are symbolic tags to refer to official |
29 |
Isabelle releases, as opposed to arbitrary \emph{tip} versions that |
|
30 |
merely reflect snapshots that are never really up-to-date.} *} |
|
31 |
||
32 |
||
39878 | 33 |
section {* Style and orthography *} |
34 |
||
39879 | 35 |
text {* The sources of Isabelle/Isar are optimized for |
36 |
\emph{readability} and \emph{maintainability}. The main purpose is |
|
37 |
to tell an informed reader what is really going on and how things |
|
38 |
really work. This is a non-trivial aim, but it is supported by a |
|
39 |
certain style of writing Isabelle/ML that has emerged from long |
|
40 |
years of system development.\footnote{See also the interesting style |
|
41 |
guide for OCaml |
|
54703 | 42 |
@{url "http://caml.inria.fr/resources/doc/guides/guidelines.en.html"} |
39878 | 43 |
which shares many of our means and ends.} |
44 |
||
45 |
The main principle behind any coding style is \emph{consistency}. |
|
39879 | 46 |
For a single author of a small program this merely means ``choose |
39878 | 47 |
your style and stick to it''. A complex project like Isabelle, with |
39879 | 48 |
long years of development and different contributors, requires more |
49 |
standardization. A coding style that is changed every few years or |
|
50 |
with every new contributor is no style at all, because consistency |
|
51 |
is quickly lost. Global consistency is hard to achieve, though. |
|
40126 | 52 |
Nonetheless, one should always strive at least for local consistency |
53 |
of modules and sub-systems, without deviating from some general |
|
54 |
principles how to write Isabelle/ML. |
|
39878 | 55 |
|
40126 | 56 |
In a sense, good coding style is like an \emph{orthography} for the |
57 |
sources: it helps to read quickly over the text and see through the |
|
58 |
main points, without getting distracted by accidental presentation |
|
59 |
of free-style code. |
|
39878 | 60 |
*} |
61 |
||
62 |
||
63 |
subsection {* Header and sectioning *} |
|
64 |
||
39879 | 65 |
text {* Isabelle source files have a certain standardized header |
66 |
format (with precise spacing) that follows ancient traditions |
|
67 |
reaching back to the earliest versions of the system by Larry |
|
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
68 |
Paulson. See @{file "~~/src/Pure/thm.ML"}, for example. |
39878 | 69 |
|
70 |
The header includes at least @{verbatim Title} and @{verbatim |
|
71 |
Author} entries, followed by a prose description of the purpose of |
|
72 |
the module. The latter can range from a single line to several |
|
73 |
paragraphs of explanations. |
|
74 |
||
75 |
The rest of the file is divided into sections, subsections, |
|
40126 | 76 |
subsubsections, paragraphs etc.\ using a simple layout via ML |
77 |
comments as follows. |
|
39878 | 78 |
|
79 |
\begin{verbatim} |
|
80 |
(*** section ***) |
|
81 |
||
82 |
(** subsection **) |
|
83 |
||
84 |
(* subsubsection *) |
|
85 |
||
86 |
(*short paragraph*) |
|
87 |
||
88 |
(* |
|
40126 | 89 |
long paragraph, |
90 |
with more text |
|
39878 | 91 |
*) |
92 |
\end{verbatim} |
|
93 |
||
94 |
As in regular typography, there is some extra space \emph{before} |
|
95 |
section headings that are adjacent to plain text (not other headings |
|
96 |
as in the example above). |
|
97 |
||
98 |
\medskip The precise wording of the prose text given in these |
|
40126 | 99 |
headings is chosen carefully to introduce the main theme of the |
39879 | 100 |
subsequent formal ML text. |
101 |
*} |
|
102 |
||
103 |
||
39880 | 104 |
subsection {* Naming conventions *} |
39879 | 105 |
|
106 |
text {* Since ML is the primary medium to express the meaning of the |
|
107 |
source text, naming of ML entities requires special care. |
|
108 |
||
39881 | 109 |
\paragraph{Notation.} A name consists of 1--3 \emph{words} (rarely |
110 |
4, but not more) that are separated by underscore. There are three |
|
40126 | 111 |
variants concerning upper or lower case letters, which are used for |
39881 | 112 |
certain ML categories as follows: |
39880 | 113 |
|
114 |
\medskip |
|
115 |
\begin{tabular}{lll} |
|
116 |
variant & example & ML categories \\\hline |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
117 |
lower-case & @{ML_text foo_bar} & values, types, record fields \\ |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
118 |
capitalized & @{ML_text Foo_Bar} & datatype constructors, structures, functors \\ |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
119 |
upper-case & @{ML_text FOO_BAR} & special values, exception constructors, signatures \\ |
39880 | 120 |
\end{tabular} |
121 |
\medskip |
|
122 |
||
123 |
For historical reasons, many capitalized names omit underscores, |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
124 |
e.g.\ old-style @{ML_text FooBar} instead of @{ML_text Foo_Bar}. |
47180 | 125 |
Genuine mixed-case names are \emph{not} used, because clear division |
40126 | 126 |
of words is essential for readability.\footnote{Camel-case was |
127 |
invented to workaround the lack of underscore in some early |
|
128 |
non-ASCII character sets. Later it became habitual in some language |
|
129 |
communities that are now strong in numbers.} |
|
39880 | 130 |
|
39881 | 131 |
A single (capital) character does not count as ``word'' in this |
40126 | 132 |
respect: some Isabelle/ML names are suffixed by extra markers like |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
133 |
this: @{ML_text foo_barT}. |
39881 | 134 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
135 |
Name variants are produced by adding 1--3 primes, e.g.\ @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
136 |
foo'}, @{ML_text foo''}, or @{ML_text foo'''}, but not @{ML_text |
39881 | 137 |
foo''''} or more. Decimal digits scale better to larger numbers, |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
138 |
e.g.\ @{ML_text foo0}, @{ML_text foo1}, @{ML_text foo42}. |
39880 | 139 |
|
140 |
\paragraph{Scopes.} Apart from very basic library modules, ML |
|
141 |
structures are not ``opened'', but names are referenced with |
|
39881 | 142 |
explicit qualification, as in @{ML Syntax.string_of_term} for |
39880 | 143 |
example. When devising names for structures and their components it |
144 |
is important aim at eye-catching compositions of both parts, because |
|
40126 | 145 |
this is how they are seen in the sources and documentation. For the |
146 |
same reasons, aliases of well-known library functions should be |
|
147 |
avoided. |
|
39880 | 148 |
|
40126 | 149 |
Local names of function abstraction or case/let bindings are |
39880 | 150 |
typically shorter, sometimes using only rudiments of ``words'', |
151 |
while still avoiding cryptic shorthands. An auxiliary function |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
152 |
called @{ML_text helper}, @{ML_text aux}, or @{ML_text f} is |
39880 | 153 |
considered bad style. |
154 |
||
155 |
Example: |
|
156 |
||
157 |
\begin{verbatim} |
|
158 |
(* RIGHT *) |
|
159 |
||
160 |
fun print_foo ctxt foo = |
|
161 |
let |
|
39881 | 162 |
fun print t = ... Syntax.string_of_term ctxt t ... |
163 |
in ... end; |
|
164 |
||
165 |
||
166 |
(* RIGHT *) |
|
167 |
||
168 |
fun print_foo ctxt foo = |
|
169 |
let |
|
39880 | 170 |
val string_of_term = Syntax.string_of_term ctxt; |
171 |
fun print t = ... string_of_term t ... |
|
172 |
in ... end; |
|
173 |
||
174 |
||
175 |
(* WRONG *) |
|
176 |
||
177 |
val string_of_term = Syntax.string_of_term; |
|
178 |
||
179 |
fun print_foo ctxt foo = |
|
180 |
let |
|
181 |
fun aux t = ... string_of_term ctxt t ... |
|
182 |
in ... end; |
|
183 |
||
184 |
\end{verbatim} |
|
185 |
||
186 |
||
40126 | 187 |
\paragraph{Specific conventions.} Here are some specific name forms |
188 |
that occur frequently in the sources. |
|
39881 | 189 |
|
190 |
\begin{itemize} |
|
191 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
192 |
\item A function that maps @{ML_text foo} to @{ML_text bar} is |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
193 |
called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
194 |
@{ML_text foo2bar}, @{ML_text bar_from_foo}, @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
195 |
bar_for_foo}, or @{ML_text bar4foo}). |
39881 | 196 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
197 |
\item The name component @{ML_text legacy} means that the operation |
39881 | 198 |
is about to be discontinued soon. |
199 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
200 |
\item The name component @{ML_text old} means that this is historic |
39881 | 201 |
material that might disappear at some later stage. |
202 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
203 |
\item The name component @{ML_text global} means that this works |
39881 | 204 |
with the background theory instead of the regular local context |
205 |
(\secref{sec:context}), sometimes for historical reasons, sometimes |
|
206 |
due a genuine lack of locality of the concept involved, sometimes as |
|
207 |
a fall-back for the lack of a proper context in the application |
|
208 |
code. Whenever there is a non-global variant available, the |
|
209 |
application should be migrated to use it with a proper local |
|
210 |
context. |
|
211 |
||
212 |
\item Variables of the main context types of the Isabelle/Isar |
|
213 |
framework (\secref{sec:context} and \chref{ch:local-theory}) have |
|
40126 | 214 |
firm naming conventions as follows: |
39881 | 215 |
|
216 |
\begin{itemize} |
|
217 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
218 |
\item theories are called @{ML_text thy}, rarely @{ML_text theory} |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
219 |
(never @{ML_text thry}) |
39881 | 220 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
221 |
\item proof contexts are called @{ML_text ctxt}, rarely @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
222 |
context} (never @{ML_text ctx}) |
39881 | 223 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
224 |
\item generic contexts are called @{ML_text context}, rarely |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
225 |
@{ML_text ctxt} |
39881 | 226 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
227 |
\item local theories are called @{ML_text lthy}, except for local |
40126 | 228 |
theories that are treated as proof context (which is a semantic |
229 |
super-type) |
|
39881 | 230 |
|
231 |
\end{itemize} |
|
232 |
||
233 |
Variations with primed or decimal numbers are always possible, as |
|
56579 | 234 |
well as semantic prefixes like @{ML_text foo_thy} or @{ML_text |
39881 | 235 |
bar_ctxt}, but the base conventions above need to be preserved. |
40126 | 236 |
This allows to visualize the their data flow via plain regular |
237 |
expressions in the editor. |
|
39881 | 238 |
|
40126 | 239 |
\item The main logical entities (\secref{ch:logic}) have established |
240 |
naming convention as follows: |
|
39881 | 241 |
|
242 |
\begin{itemize} |
|
243 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
244 |
\item sorts are called @{ML_text S} |
39881 | 245 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
246 |
\item types are called @{ML_text T}, @{ML_text U}, or @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
247 |
ty} (never @{ML_text t}) |
39881 | 248 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
249 |
\item terms are called @{ML_text t}, @{ML_text u}, or @{ML_text |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
250 |
tm} (never @{ML_text trm}) |
39881 | 251 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
252 |
\item certified types are called @{ML_text cT}, rarely @{ML_text |
39881 | 253 |
T}, with variants as for types |
254 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
255 |
\item certified terms are called @{ML_text ct}, rarely @{ML_text |
52733 | 256 |
t}, with variants as for terms (never @{ML_text ctrm}) |
39881 | 257 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
258 |
\item theorems are called @{ML_text th}, or @{ML_text thm} |
39881 | 259 |
|
260 |
\end{itemize} |
|
261 |
||
262 |
Proper semantic names override these conventions completely. For |
|
263 |
example, the left-hand side of an equation (as a term) can be called |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
264 |
@{ML_text lhs} (not @{ML_text lhs_tm}). Or a term that is known |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
265 |
to be a variable can be called @{ML_text v} or @{ML_text x}. |
39881 | 266 |
|
40310 | 267 |
\item Tactics (\secref{sec:tactics}) are sufficiently important to |
268 |
have specific naming conventions. The name of a basic tactic |
|
269 |
definition always has a @{ML_text "_tac"} suffix, the subgoal index |
|
270 |
(if applicable) is always called @{ML_text i}, and the goal state |
|
271 |
(if made explicit) is usually called @{ML_text st} instead of the |
|
272 |
somewhat misleading @{ML_text thm}. Any other arguments are given |
|
273 |
before the latter two, and the general context is given first. |
|
274 |
Example: |
|
275 |
||
276 |
\begin{verbatim} |
|
277 |
fun my_tac ctxt arg1 arg2 i st = ... |
|
278 |
\end{verbatim} |
|
279 |
||
280 |
Note that the goal state @{ML_text st} above is rarely made |
|
281 |
explicit, if tactic combinators (tacticals) are used as usual. |
|
282 |
||
39881 | 283 |
\end{itemize} |
39878 | 284 |
*} |
285 |
||
286 |
||
287 |
subsection {* General source layout *} |
|
288 |
||
289 |
text {* The general Isabelle/ML source layout imitates regular |
|
290 |
type-setting to some extent, augmented by the requirements for |
|
291 |
deeply nested expressions that are commonplace in functional |
|
292 |
programming. |
|
293 |
||
294 |
\paragraph{Line length} is 80 characters according to ancient |
|
40126 | 295 |
standards, but we allow as much as 100 characters (not |
296 |
more).\footnote{Readability requires to keep the beginning of a line |
|
39881 | 297 |
in view while watching its end. Modern wide-screen displays do not |
40126 | 298 |
change the way how the human brain works. Sources also need to be |
299 |
printable on plain paper with reasonable font-size.} The extra 20 |
|
39880 | 300 |
characters acknowledge the space requirements due to qualified |
301 |
library references in Isabelle/ML. |
|
39878 | 302 |
|
303 |
\paragraph{White-space} is used to emphasize the structure of |
|
304 |
expressions, following mostly standard conventions for mathematical |
|
305 |
typesetting, as can be seen in plain {\TeX} or {\LaTeX}. This |
|
39879 | 306 |
defines positioning of spaces for parentheses, punctuation, and |
307 |
infixes as illustrated here: |
|
39878 | 308 |
|
309 |
\begin{verbatim} |
|
310 |
val x = y + z * (a + b); |
|
311 |
val pair = (a, b); |
|
312 |
val record = {foo = 1, bar = 2}; |
|
313 |
\end{verbatim} |
|
314 |
||
39879 | 315 |
Lines are normally broken \emph{after} an infix operator or |
316 |
punctuation character. For example: |
|
39878 | 317 |
|
318 |
\begin{verbatim} |
|
319 |
val x = |
|
320 |
a + |
|
321 |
b + |
|
322 |
c; |
|
323 |
||
324 |
val tuple = |
|
325 |
(a, |
|
326 |
b, |
|
327 |
c); |
|
328 |
\end{verbatim} |
|
329 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
330 |
Some special infixes (e.g.\ @{ML_text "|>"}) work better at the |
39879 | 331 |
start of the line, but punctuation is always at the end. |
39878 | 332 |
|
333 |
Function application follows the tradition of @{text "\<lambda>"}-calculus, |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
334 |
not informal mathematics. For example: @{ML_text "f a b"} for a |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
335 |
curried function, or @{ML_text "g (a, b)"} for a tupled function. |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
336 |
Note that the space between @{ML_text g} and the pair @{ML_text |
39879 | 337 |
"(a, b)"} follows the important principle of |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
338 |
\emph{compositionality}: the layout of @{ML_text "g p"} does not |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
339 |
change when @{ML_text "p"} is refined to the concrete pair |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
340 |
@{ML_text "(a, b)"}. |
39878 | 341 |
|
342 |
\paragraph{Indentation} uses plain spaces, never hard |
|
343 |
tabulators.\footnote{Tabulators were invented to move the carriage |
|
344 |
of a type-writer to certain predefined positions. In software they |
|
345 |
could be used as a primitive run-length compression of consecutive |
|
346 |
spaces, but the precise result would depend on non-standardized |
|
347 |
editor configuration.} |
|
348 |
||
39879 | 349 |
Each level of nesting is indented by 2 spaces, sometimes 1, very |
40126 | 350 |
rarely 4, never 8 or any other odd number. |
39878 | 351 |
|
39879 | 352 |
Indentation follows a simple logical format that only depends on the |
353 |
nesting depth, not the accidental length of the text that initiates |
|
354 |
a level of nesting. Example: |
|
39878 | 355 |
|
356 |
\begin{verbatim} |
|
39880 | 357 |
(* RIGHT *) |
358 |
||
39878 | 359 |
if b then |
39879 | 360 |
expr1_part1 |
361 |
expr1_part2 |
|
39878 | 362 |
else |
39879 | 363 |
expr2_part1 |
364 |
expr2_part2 |
|
39878 | 365 |
|
39880 | 366 |
|
367 |
(* WRONG *) |
|
368 |
||
39879 | 369 |
if b then expr1_part1 |
370 |
expr1_part2 |
|
371 |
else expr2_part1 |
|
372 |
expr2_part2 |
|
39878 | 373 |
\end{verbatim} |
374 |
||
375 |
The second form has many problems: it assumes a fixed-width font |
|
39879 | 376 |
when viewing the sources, it uses more space on the line and thus |
377 |
makes it hard to observe its strict length limit (working against |
|
39878 | 378 |
\emph{readability}), it requires extra editing to adapt the layout |
39879 | 379 |
to changes of the initial text (working against |
39878 | 380 |
\emph{maintainability}) etc. |
381 |
||
39879 | 382 |
\medskip For similar reasons, any kind of two-dimensional or tabular |
40126 | 383 |
layouts, ASCII-art with lines or boxes of asterisks etc.\ should be |
39879 | 384 |
avoided. |
39881 | 385 |
|
40126 | 386 |
\paragraph{Complex expressions} that consist of multi-clausal |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
387 |
function definitions, @{ML_text handle}, @{ML_text case}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
388 |
@{ML_text let} (and combinations) require special attention. The |
40126 | 389 |
syntax of Standard ML is quite ambitious and admits a lot of |
390 |
variance that can distort the meaning of the text. |
|
39881 | 391 |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
392 |
Clauses of @{ML_text fun}, @{ML_text fn}, @{ML_text handle}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
393 |
@{ML_text case} get extra indentation to indicate the nesting |
40126 | 394 |
clearly. Example: |
39881 | 395 |
|
396 |
\begin{verbatim} |
|
397 |
(* RIGHT *) |
|
398 |
||
399 |
fun foo p1 = |
|
400 |
expr1 |
|
401 |
| foo p2 = |
|
402 |
expr2 |
|
403 |
||
404 |
||
405 |
(* WRONG *) |
|
406 |
||
407 |
fun foo p1 = |
|
408 |
expr1 |
|
409 |
| foo p2 = |
|
410 |
expr2 |
|
411 |
\end{verbatim} |
|
412 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
413 |
Body expressions consisting of @{ML_text case} or @{ML_text let} |
39881 | 414 |
require care to maintain compositionality, to prevent loss of |
40126 | 415 |
logical indentation where it is especially important to see the |
416 |
structure of the text. Example: |
|
39881 | 417 |
|
418 |
\begin{verbatim} |
|
419 |
(* RIGHT *) |
|
420 |
||
421 |
fun foo p1 = |
|
422 |
(case e of |
|
423 |
q1 => ... |
|
424 |
| q2 => ...) |
|
425 |
| foo p2 = |
|
426 |
let |
|
427 |
... |
|
428 |
in |
|
429 |
... |
|
430 |
end |
|
431 |
||
432 |
||
433 |
(* WRONG *) |
|
434 |
||
435 |
fun foo p1 = case e of |
|
436 |
q1 => ... |
|
437 |
| q2 => ... |
|
438 |
| foo p2 = |
|
439 |
let |
|
440 |
... |
|
441 |
in |
|
442 |
... |
|
443 |
end |
|
444 |
\end{verbatim} |
|
445 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
446 |
Extra parentheses around @{ML_text case} expressions are optional, |
40126 | 447 |
but help to analyse the nesting based on character matching in the |
448 |
editor. |
|
39881 | 449 |
|
450 |
\medskip There are two main exceptions to the overall principle of |
|
451 |
compositionality in the layout of complex expressions. |
|
452 |
||
453 |
\begin{enumerate} |
|
454 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
455 |
\item @{ML_text "if"} expressions are iterated as if there would be |
39881 | 456 |
a multi-branch conditional in SML, e.g. |
457 |
||
458 |
\begin{verbatim} |
|
459 |
(* RIGHT *) |
|
460 |
||
461 |
if b1 then e1 |
|
462 |
else if b2 then e2 |
|
463 |
else e3 |
|
464 |
\end{verbatim} |
|
465 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
466 |
\item @{ML_text fn} abstractions are often layed-out as if they |
39881 | 467 |
would lack any structure by themselves. This traditional form is |
468 |
motivated by the possibility to shift function arguments back and |
|
40126 | 469 |
forth wrt.\ additional combinators. Example: |
39881 | 470 |
|
471 |
\begin{verbatim} |
|
472 |
(* RIGHT *) |
|
473 |
||
474 |
fun foo x y = fold (fn z => |
|
475 |
expr) |
|
476 |
\end{verbatim} |
|
477 |
||
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
478 |
Here the visual appearance is that of three arguments @{ML_text x}, |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
479 |
@{ML_text y}, @{ML_text z}. |
39881 | 480 |
|
481 |
\end{enumerate} |
|
482 |
||
483 |
Such weakly structured layout should be use with great care. Here |
|
40153 | 484 |
are some counter-examples involving @{ML_text let} expressions: |
39881 | 485 |
|
486 |
\begin{verbatim} |
|
487 |
(* WRONG *) |
|
488 |
||
489 |
fun foo x = let |
|
490 |
val y = ... |
|
491 |
in ... end |
|
492 |
||
41162 | 493 |
|
494 |
(* WRONG *) |
|
495 |
||
40153 | 496 |
fun foo x = let |
497 |
val y = ... |
|
498 |
in ... end |
|
499 |
||
41162 | 500 |
|
501 |
(* WRONG *) |
|
502 |
||
39881 | 503 |
fun foo x = |
504 |
let |
|
505 |
val y = ... |
|
506 |
in ... end |
|
507 |
\end{verbatim} |
|
508 |
||
509 |
\medskip In general the source layout is meant to emphasize the |
|
510 |
structure of complex language expressions, not to pretend that SML |
|
511 |
had a completely different syntax (say that of Haskell or Java). |
|
39878 | 512 |
*} |
513 |
||
514 |
||
39823 | 515 |
section {* SML embedded into Isabelle/Isar *} |
516 |
||
39824 | 517 |
text {* ML and Isar are intertwined via an open-ended bootstrap |
518 |
process that provides more and more programming facilities and |
|
519 |
logical content in an alternating manner. Bootstrapping starts from |
|
520 |
the raw environment of existing implementations of Standard ML |
|
521 |
(mainly Poly/ML, but also SML/NJ). |
|
39823 | 522 |
|
39824 | 523 |
Isabelle/Pure marks the point where the original ML toplevel is |
40126 | 524 |
superseded by the Isar toplevel that maintains a uniform context for |
525 |
arbitrary ML values (see also \secref{sec:context}). This formal |
|
526 |
environment holds ML compiler bindings, logical entities, and many |
|
527 |
other things. Raw SML is never encountered again after the initial |
|
528 |
bootstrap of Isabelle/Pure. |
|
39823 | 529 |
|
40126 | 530 |
Object-logics like Isabelle/HOL are built within the |
531 |
Isabelle/ML/Isar environment by introducing suitable theories with |
|
532 |
associated ML modules, either inlined or as separate files. Thus |
|
533 |
Isabelle/HOL is defined as a regular user-space application within |
|
534 |
the Isabelle framework. Further add-on tools can be implemented in |
|
535 |
ML within the Isar context in the same manner: ML is part of the |
|
536 |
standard repertoire of Isabelle, and there is no distinction between |
|
537 |
``user'' and ``developer'' in this respect. |
|
39823 | 538 |
*} |
539 |
||
39824 | 540 |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
541 |
subsection {* Isar ML commands *} |
39823 | 542 |
|
40126 | 543 |
text {* The primary Isar source language provides facilities to ``open |
544 |
a window'' to the underlying ML compiler. Especially see the Isar |
|
51295 | 545 |
commands @{command_ref "ML_file"} and @{command_ref "ML"}: both work the |
39824 | 546 |
same way, only the source text is provided via a file vs.\ inlined, |
547 |
respectively. Apart from embedding ML into the main theory |
|
548 |
definition like that, there are many more commands that refer to ML |
|
549 |
source, such as @{command_ref setup} or @{command_ref declaration}. |
|
40126 | 550 |
Even more fine-grained embedding of ML into Isar is encountered in |
551 |
the proof method @{method_ref tactic}, which refines the pending |
|
552 |
goal state via a given expression of type @{ML_type tactic}. |
|
39824 | 553 |
*} |
39823 | 554 |
|
39824 | 555 |
text %mlex {* The following artificial example demonstrates some ML |
556 |
toplevel declarations within the implicit Isar theory context. This |
|
557 |
is regular functional programming without referring to logical |
|
558 |
entities yet. |
|
39823 | 559 |
*} |
560 |
||
561 |
ML {* |
|
562 |
fun factorial 0 = 1 |
|
563 |
| factorial n = n * factorial (n - 1) |
|
564 |
*} |
|
565 |
||
40126 | 566 |
text {* Here the ML environment is already managed by Isabelle, i.e.\ |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
567 |
the @{ML factorial} function is not yet accessible in the preceding |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
568 |
paragraph, nor in a different theory that is independent from the |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
569 |
current one in the import hierarchy. |
39823 | 570 |
|
571 |
Removing the above ML declaration from the source text will remove |
|
572 |
any trace of this definition as expected. The Isabelle/ML toplevel |
|
573 |
environment is managed in a \emph{stateless} way: unlike the raw ML |
|
40126 | 574 |
toplevel there are no global side-effects involved |
575 |
here.\footnote{Such a stateless compilation environment is also a |
|
576 |
prerequisite for robust parallel compilation within independent |
|
577 |
nodes of the implicit theory development graph.} |
|
39823 | 578 |
|
40126 | 579 |
\medskip The next example shows how to embed ML into Isar proofs, using |
580 |
@{command_ref "ML_prf"} instead of Instead of @{command_ref "ML"}. |
|
581 |
As illustrated below, the effect on the ML environment is local to |
|
582 |
the whole proof body, ignoring the block structure. |
|
583 |
*} |
|
39823 | 584 |
|
40964 | 585 |
notepad |
586 |
begin |
|
39851 | 587 |
ML_prf %"ML" {* val a = 1 *} |
40126 | 588 |
{ |
39851 | 589 |
ML_prf %"ML" {* val b = a + 1 *} |
39824 | 590 |
} -- {* Isar block structure ignored by ML environment *} |
39851 | 591 |
ML_prf %"ML" {* val c = b + 1 *} |
40964 | 592 |
end |
39823 | 593 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
594 |
text {* By side-stepping the normal scoping rules for Isar proof |
40126 | 595 |
blocks, embedded ML code can refer to the different contexts and |
596 |
manipulate corresponding entities, e.g.\ export a fact from a block |
|
597 |
context. |
|
39823 | 598 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
599 |
\medskip Two further ML commands are useful in certain situations: |
40126 | 600 |
@{command_ref ML_val} and @{command_ref ML_command} are |
39824 | 601 |
\emph{diagnostic} in the sense that there is no effect on the |
602 |
underlying environment, and can thus used anywhere (even outside a |
|
603 |
theory). The examples below produce long strings of digits by |
|
604 |
invoking @{ML factorial}: @{command ML_val} already takes care of |
|
605 |
printing the ML toplevel result, but @{command ML_command} is silent |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
606 |
so we produce an explicit output message. *} |
39823 | 607 |
|
608 |
ML_val {* factorial 100 *} |
|
609 |
ML_command {* writeln (string_of_int (factorial 100)) *} |
|
610 |
||
40964 | 611 |
notepad |
612 |
begin |
|
52417 | 613 |
ML_val {* factorial 100 *} |
39823 | 614 |
ML_command {* writeln (string_of_int (factorial 100)) *} |
40964 | 615 |
end |
39823 | 616 |
|
617 |
||
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
618 |
subsection {* Compile-time context *} |
39823 | 619 |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
620 |
text {* Whenever the ML compiler is invoked within Isabelle/Isar, the |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
621 |
formal context is passed as a thread-local reference variable. Thus |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
622 |
ML code may access the theory context during compilation, by reading |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
623 |
or writing the (local) theory under construction. Note that such |
40126 | 624 |
direct access to the compile-time context is rare. In practice it |
625 |
is typically done via some derived ML functions instead. |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
626 |
*} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
627 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
628 |
text %mlref {* |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
629 |
\begin{mldecls} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
630 |
@{index_ML ML_Context.the_generic_context: "unit -> Context.generic"} \\ |
40126 | 631 |
@{index_ML "Context.>>": "(Context.generic -> Context.generic) -> unit"} \\ |
56199 | 632 |
@{index_ML ML_Thms.bind_thms: "string * thm list -> unit"} \\ |
633 |
@{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\ |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
634 |
\end{mldecls} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
635 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
636 |
\begin{description} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
637 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
638 |
\item @{ML "ML_Context.the_generic_context ()"} refers to the theory |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
639 |
context of the ML toplevel --- at compile time. ML code needs to |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
640 |
take care to refer to @{ML "ML_Context.the_generic_context ()"} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
641 |
correctly. Recall that evaluation of a function body is delayed |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
642 |
until actual run-time. |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
643 |
|
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
644 |
\item @{ML "Context.>>"}~@{text f} applies context transformation |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
645 |
@{text f} to the implicit context of the ML toplevel. |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
646 |
|
56199 | 647 |
\item @{ML ML_Thms.bind_thms}~@{text "(name, thms)"} stores a list of |
39850 | 648 |
theorems produced in ML both in the (global) theory context and the |
649 |
ML toplevel, associating it with the provided name. Theorems are |
|
650 |
put into a global ``standard'' format before being stored. |
|
651 |
||
56199 | 652 |
\item @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but refers to a |
40126 | 653 |
singleton fact. |
39850 | 654 |
|
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
655 |
\end{description} |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
656 |
|
40126 | 657 |
It is important to note that the above functions are really |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
658 |
restricted to the compile time, even though the ML compiler is |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
659 |
invoked at run-time. The majority of ML code either uses static |
39825
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
660 |
antiquotations (\secref{sec:ML-antiq}) or refers to the theory or |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
661 |
proof context at run-time, by explicit functional abstraction. |
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents:
39824
diff
changeset
|
662 |
*} |
39823 | 663 |
|
664 |
||
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
665 |
subsection {* Antiquotations \label{sec:ML-antiq} *} |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
666 |
|
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
667 |
text {* A very important consequence of embedding SML into Isar is the |
40126 | 668 |
concept of \emph{ML antiquotation}. The standard token language of |
669 |
ML is augmented by special syntactic entities of the following form: |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
670 |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
671 |
@{rail \<open> |
53167 | 672 |
@{syntax_def antiquote}: '@{' nameref args '}' |
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
673 |
\<close>} |
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
674 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
675 |
Here @{syntax nameref} and @{syntax args} are regular outer syntax |
40126 | 676 |
categories \cite{isabelle-isar-ref}. Attributes and proof methods |
677 |
use similar syntax. |
|
39823 | 678 |
|
39827
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
679 |
\medskip A regular antiquotation @{text "@{name args}"} processes |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
680 |
its arguments by the usual means of the Isar source language, and |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
681 |
produces corresponding ML source text, either as literal |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
682 |
\emph{inline} text (e.g. @{text "@{term t}"}) or abstract |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
683 |
\emph{value} (e.g. @{text "@{thm th}"}). This pre-compilation |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
684 |
scheme allows to refer to formal entities in a robust manner, with |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
685 |
proper static scoping and with some degree of logical checking of |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
686 |
small portions of the code. |
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents:
39825
diff
changeset
|
687 |
*} |
39823 | 688 |
|
39835 | 689 |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
690 |
subsection {* Printing ML values *} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
691 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
692 |
text {* The ML compiler knows about the structure of values according |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
693 |
to their static type, and can print them in the manner of the |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
694 |
toplevel loop, although the details are non-portable. The |
56399 | 695 |
antiquotations @{ML_antiquotation_def "make_string"} and |
696 |
@{ML_antiquotation_def "print"} provide a quasi-portable way to |
|
697 |
refer to this potential capability of the underlying ML system in |
|
698 |
generic Isabelle/ML sources. |
|
699 |
||
700 |
This is occasionally useful for diagnostic or demonstration |
|
701 |
purposes. Note that production-quality tools require proper |
|
702 |
user-level error messages. *} |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
703 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
704 |
text %mlantiq {* |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
705 |
\begin{matharray}{rcl} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
706 |
@{ML_antiquotation_def "make_string"} & : & @{text ML_antiquotation} \\ |
56399 | 707 |
@{ML_antiquotation_def "print"} & : & @{text ML_antiquotation} \\ |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
708 |
\end{matharray} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
709 |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
710 |
@{rail \<open> |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
711 |
@@{ML_antiquotation make_string} |
56399 | 712 |
; |
713 |
@@{ML_antiquotation print} @{syntax name}? |
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
54703
diff
changeset
|
714 |
\<close>} |
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
715 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
716 |
\begin{description} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
717 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
718 |
\item @{text "@{make_string}"} inlines a function to print arbitrary |
56399 | 719 |
values similar to the ML toplevel. The result is compiler dependent |
720 |
and may fall back on "?" in certain situations. |
|
721 |
||
722 |
\item @{text "@{print f}"} uses the ML function @{text "f: string -> |
|
723 |
unit"} to output the result of @{text "@{make_string}"} above, |
|
724 |
together with the source position of the antiquotation. The default |
|
725 |
output function is @{ML writeln}. |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
726 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
727 |
\end{description} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
728 |
*} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
729 |
|
56399 | 730 |
text %mlex {* The following artificial examples show how to produce |
731 |
adhoc output of ML values for debugging purposes. *} |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
732 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
733 |
ML {* |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
734 |
val x = 42; |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
735 |
val y = true; |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
736 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
737 |
writeln (@{make_string} {x = x, y = y}); |
56399 | 738 |
|
739 |
@{print} {x = x, y = y}; |
|
740 |
@{print tracing} {x = x, y = y}; |
|
51636
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
741 |
*} |
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
742 |
|
e49bf0be79ba
document @{make_string}, cf. NEWS of Isabelle2009-2 (June 2010);
wenzelm
parents:
51295
diff
changeset
|
743 |
|
39883 | 744 |
section {* Canonical argument order \label{sec:canonical-argument-order} *} |
745 |
||
746 |
text {* Standard ML is a language in the tradition of @{text |
|
747 |
"\<lambda>"}-calculus and \emph{higher-order functional programming}, |
|
748 |
similar to OCaml, Haskell, or Isabelle/Pure and HOL as logical |
|
749 |
languages. Getting acquainted with the native style of representing |
|
750 |
functions in that setting can save a lot of extra boiler-plate of |
|
751 |
redundant shuffling of arguments, auxiliary abstractions etc. |
|
752 |
||
40126 | 753 |
Functions are usually \emph{curried}: the idea of turning arguments |
754 |
of type @{text "\<tau>\<^sub>i"} (for @{text "i \<in> {1, \<dots> n}"}) into a result of |
|
755 |
type @{text "\<tau>"} is represented by the iterated function space |
|
756 |
@{text "\<tau>\<^sub>1 \<rightarrow> \<dots> \<rightarrow> \<tau>\<^sub>n \<rightarrow> \<tau>"}. This is isomorphic to the well-known |
|
757 |
encoding via tuples @{text "\<tau>\<^sub>1 \<times> \<dots> \<times> \<tau>\<^sub>n \<rightarrow> \<tau>"}, but the curried |
|
758 |
version fits more smoothly into the basic calculus.\footnote{The |
|
759 |
difference is even more significant in higher-order logic, because |
|
760 |
the redundant tuple structure needs to be accommodated by formal |
|
761 |
reasoning.} |
|
39883 | 762 |
|
56594 | 763 |
Currying gives some flexibility due to \emph{partial application}. A |
53071 | 764 |
function @{text "f: \<tau>\<^sub>1 \<rightarrow> \<tau>\<^sub>2 \<rightarrow> \<tau>"} can be applied to @{text "x: \<tau>\<^sub>1"} |
40126 | 765 |
and the remaining @{text "(f x): \<tau>\<^sub>2 \<rightarrow> \<tau>"} passed to another function |
39883 | 766 |
etc. How well this works in practice depends on the order of |
767 |
arguments. In the worst case, arguments are arranged erratically, |
|
768 |
and using a function in a certain situation always requires some |
|
56579 | 769 |
glue code. Thus we would get exponentially many opportunities to |
39883 | 770 |
decorate the code with meaningless permutations of arguments. |
771 |
||
772 |
This can be avoided by \emph{canonical argument order}, which |
|
40126 | 773 |
observes certain standard patterns and minimizes adhoc permutations |
40229 | 774 |
in their application. In Isabelle/ML, large portions of text can be |
52416 | 775 |
written without auxiliary operations like @{text "swap: \<alpha> \<times> \<beta> \<rightarrow> \<beta> \<times> |
776 |
\<alpha>"} or @{text "C: (\<alpha> \<rightarrow> \<beta> \<rightarrow> \<gamma>) \<rightarrow> (\<beta> \<rightarrow> \<alpha> \<rightarrow> \<gamma>)"} (the latter not |
|
777 |
present in the Isabelle/ML library). |
|
39883 | 778 |
|
779 |
\medskip The basic idea is that arguments that vary less are moved |
|
780 |
further to the left than those that vary more. Two particularly |
|
781 |
important categories of functions are \emph{selectors} and |
|
782 |
\emph{updates}. |
|
783 |
||
784 |
The subsequent scheme is based on a hypothetical set-like container |
|
785 |
of type @{text "\<beta>"} that manages elements of type @{text "\<alpha>"}. Both |
|
786 |
the names and types of the associated operations are canonical for |
|
787 |
Isabelle/ML. |
|
788 |
||
52416 | 789 |
\begin{center} |
39883 | 790 |
\begin{tabular}{ll} |
791 |
kind & canonical name and type \\\hline |
|
792 |
selector & @{text "member: \<beta> \<rightarrow> \<alpha> \<rightarrow> bool"} \\ |
|
793 |
update & @{text "insert: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} \\ |
|
794 |
\end{tabular} |
|
52416 | 795 |
\end{center} |
39883 | 796 |
|
797 |
Given a container @{text "B: \<beta>"}, the partially applied @{text |
|
798 |
"member B"} is a predicate over elements @{text "\<alpha> \<rightarrow> bool"}, and |
|
799 |
thus represents the intended denotation directly. It is customary |
|
800 |
to pass the abstract predicate to further operations, not the |
|
801 |
concrete container. The argument order makes it easy to use other |
|
802 |
combinators: @{text "forall (member B) list"} will check a list of |
|
803 |
elements for membership in @{text "B"} etc. Often the explicit |
|
40126 | 804 |
@{text "list"} is pointless and can be contracted to @{text "forall |
805 |
(member B)"} to get directly a predicate again. |
|
39883 | 806 |
|
40126 | 807 |
In contrast, an update operation varies the container, so it moves |
39883 | 808 |
to the right: @{text "insert a"} is a function @{text "\<beta> \<rightarrow> \<beta>"} to |
809 |
insert a value @{text "a"}. These can be composed naturally as |
|
40126 | 810 |
@{text "insert c \<circ> insert b \<circ> insert a"}. The slightly awkward |
40229 | 811 |
inversion of the composition order is due to conventional |
40126 | 812 |
mathematical notation, which can be easily amended as explained |
813 |
below. |
|
39883 | 814 |
*} |
815 |
||
816 |
||
817 |
subsection {* Forward application and composition *} |
|
818 |
||
819 |
text {* Regular function application and infix notation works best for |
|
820 |
relatively deeply structured expressions, e.g.\ @{text "h (f x y + g |
|
40126 | 821 |
z)"}. The important special case of \emph{linear transformation} |
822 |
applies a cascade of functions @{text "f\<^sub>n (\<dots> (f\<^sub>1 x))"}. This |
|
823 |
becomes hard to read and maintain if the functions are themselves |
|
824 |
given as complex expressions. The notation can be significantly |
|
39883 | 825 |
improved by introducing \emph{forward} versions of application and |
826 |
composition as follows: |
|
827 |
||
828 |
\medskip |
|
829 |
\begin{tabular}{lll} |
|
830 |
@{text "x |> f"} & @{text "\<equiv>"} & @{text "f x"} \\ |
|
41162 | 831 |
@{text "(f #> g) x"} & @{text "\<equiv>"} & @{text "x |> f |> g"} \\ |
39883 | 832 |
\end{tabular} |
833 |
\medskip |
|
834 |
||
835 |
This enables to write conveniently @{text "x |> f\<^sub>1 |> \<dots> |> f\<^sub>n"} or |
|
836 |
@{text "f\<^sub>1 #> \<dots> #> f\<^sub>n"} for its functional abstraction over @{text |
|
837 |
"x"}. |
|
838 |
||
839 |
\medskip There is an additional set of combinators to accommodate |
|
840 |
multiple results (via pairs) that are passed on as multiple |
|
841 |
arguments (via currying). |
|
842 |
||
843 |
\medskip |
|
844 |
\begin{tabular}{lll} |
|
845 |
@{text "(x, y) |-> f"} & @{text "\<equiv>"} & @{text "f x y"} \\ |
|
41162 | 846 |
@{text "(f #-> g) x"} & @{text "\<equiv>"} & @{text "x |> f |-> g"} \\ |
39883 | 847 |
\end{tabular} |
848 |
\medskip |
|
849 |
*} |
|
850 |
||
851 |
text %mlref {* |
|
852 |
\begin{mldecls} |
|
46262 | 853 |
@{index_ML_op "|> ": "'a * ('a -> 'b) -> 'b"} \\ |
854 |
@{index_ML_op "|-> ": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\ |
|
855 |
@{index_ML_op "#> ": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\ |
|
856 |
@{index_ML_op "#-> ": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\ |
|
39883 | 857 |
\end{mldecls} |
858 |
*} |
|
859 |
||
860 |
||
861 |
subsection {* Canonical iteration *} |
|
862 |
||
863 |
text {* As explained above, a function @{text "f: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} can be |
|
40126 | 864 |
understood as update on a configuration of type @{text "\<beta>"}, |
39883 | 865 |
parametrized by arguments of type @{text "\<alpha>"}. Given @{text "a: \<alpha>"} |
866 |
the partial application @{text "(f a): \<beta> \<rightarrow> \<beta>"} operates |
|
867 |
homogeneously on @{text "\<beta>"}. This can be iterated naturally over a |
|
53071 | 868 |
list of parameters @{text "[a\<^sub>1, \<dots>, a\<^sub>n]"} as @{text "f a\<^sub>1 #> \<dots> #> f a\<^sub>n"}. |
869 |
The latter expression is again a function @{text "\<beta> \<rightarrow> \<beta>"}. |
|
39883 | 870 |
It can be applied to an initial configuration @{text "b: \<beta>"} to |
871 |
start the iteration over the given list of arguments: each @{text |
|
872 |
"a"} in @{text "a\<^sub>1, \<dots>, a\<^sub>n"} is applied consecutively by updating a |
|
873 |
cumulative configuration. |
|
874 |
||
875 |
The @{text fold} combinator in Isabelle/ML lifts a function @{text |
|
876 |
"f"} as above to its iterated version over a list of arguments. |
|
877 |
Lifting can be repeated, e.g.\ @{text "(fold \<circ> fold) f"} iterates |
|
878 |
over a list of lists as expected. |
|
879 |
||
880 |
The variant @{text "fold_rev"} works inside-out over the list of |
|
881 |
arguments, such that @{text "fold_rev f \<equiv> fold f \<circ> rev"} holds. |
|
882 |
||
883 |
The @{text "fold_map"} combinator essentially performs @{text |
|
884 |
"fold"} and @{text "map"} simultaneously: each application of @{text |
|
885 |
"f"} produces an updated configuration together with a side-result; |
|
886 |
the iteration collects all such side-results as a separate list. |
|
887 |
*} |
|
888 |
||
889 |
text %mlref {* |
|
890 |
\begin{mldecls} |
|
891 |
@{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\ |
|
892 |
@{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\ |
|
893 |
@{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\ |
|
894 |
\end{mldecls} |
|
895 |
||
896 |
\begin{description} |
|
897 |
||
898 |
\item @{ML fold}~@{text f} lifts the parametrized update function |
|
899 |
@{text "f"} to a list of parameters. |
|
900 |
||
901 |
\item @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text |
|
902 |
"f"}, but works inside-out. |
|
903 |
||
904 |
\item @{ML fold_map}~@{text "f"} lifts the parametrized update |
|
905 |
function @{text "f"} (with side-result) to a list of parameters and |
|
906 |
cumulative side-results. |
|
907 |
||
908 |
\end{description} |
|
909 |
||
910 |
\begin{warn} |
|
911 |
The literature on functional programming provides a multitude of |
|
912 |
combinators called @{text "foldl"}, @{text "foldr"} etc. SML97 |
|
913 |
provides its own variations as @{ML List.foldl} and @{ML |
|
40126 | 914 |
List.foldr}, while the classic Isabelle library also has the |
915 |
historic @{ML Library.foldl} and @{ML Library.foldr}. To avoid |
|
52416 | 916 |
unnecessary complication and confusion, all these historical |
917 |
versions should be ignored, and @{ML fold} (or @{ML fold_rev}) used |
|
918 |
exclusively. |
|
39883 | 919 |
\end{warn} |
920 |
*} |
|
921 |
||
922 |
text %mlex {* The following example shows how to fill a text buffer |
|
923 |
incrementally by adding strings, either individually or from a given |
|
924 |
list. |
|
925 |
*} |
|
926 |
||
927 |
ML {* |
|
928 |
val s = |
|
929 |
Buffer.empty |
|
930 |
|> Buffer.add "digits: " |
|
931 |
|> fold (Buffer.add o string_of_int) (0 upto 9) |
|
932 |
|> Buffer.content; |
|
933 |
||
934 |
@{assert} (s = "digits: 0123456789"); |
|
935 |
*} |
|
936 |
||
937 |
text {* Note how @{ML "fold (Buffer.add o string_of_int)"} above saves |
|
938 |
an extra @{ML "map"} over the given list. This kind of peephole |
|
939 |
optimization reduces both the code size and the tree structures in |
|
52416 | 940 |
memory (``deforestation''), but it requires some practice to read |
941 |
and write fluently. |
|
39883 | 942 |
|
40126 | 943 |
\medskip The next example elaborates the idea of canonical |
944 |
iteration, demonstrating fast accumulation of tree content using a |
|
945 |
text buffer. |
|
39883 | 946 |
*} |
947 |
||
948 |
ML {* |
|
949 |
datatype tree = Text of string | Elem of string * tree list; |
|
950 |
||
951 |
fun slow_content (Text txt) = txt |
|
952 |
| slow_content (Elem (name, ts)) = |
|
953 |
"<" ^ name ^ ">" ^ |
|
954 |
implode (map slow_content ts) ^ |
|
955 |
"</" ^ name ^ ">" |
|
956 |
||
957 |
fun add_content (Text txt) = Buffer.add txt |
|
958 |
| add_content (Elem (name, ts)) = |
|
959 |
Buffer.add ("<" ^ name ^ ">") #> |
|
960 |
fold add_content ts #> |
|
961 |
Buffer.add ("</" ^ name ^ ">"); |
|
962 |
||
963 |
fun fast_content tree = |
|
964 |
Buffer.empty |> add_content tree |> Buffer.content; |
|
965 |
*} |
|
966 |
||
967 |
text {* The slow part of @{ML slow_content} is the @{ML implode} of |
|
968 |
the recursive results, because it copies previously produced strings |
|
40126 | 969 |
again. |
39883 | 970 |
|
971 |
The incremental @{ML add_content} avoids this by operating on a |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
972 |
buffer that is passed through in a linear fashion. Using @{ML_text |
40126 | 973 |
"#>"} and contraction over the actual buffer argument saves some |
974 |
additional boiler-plate. Of course, the two @{ML "Buffer.add"} |
|
975 |
invocations with concatenated strings could have been split into |
|
976 |
smaller parts, but this would have obfuscated the source without |
|
977 |
making a big difference in allocations. Here we have done some |
|
39883 | 978 |
peephole-optimization for the sake of readability. |
979 |
||
980 |
Another benefit of @{ML add_content} is its ``open'' form as a |
|
40126 | 981 |
function on buffers that can be continued in further linear |
982 |
transformations, folding etc. Thus it is more compositional than |
|
983 |
the naive @{ML slow_content}. As realistic example, compare the |
|
984 |
old-style @{ML "Term.maxidx_of_term: term -> int"} with the newer |
|
985 |
@{ML "Term.maxidx_term: term -> int -> int"} in Isabelle/Pure. |
|
39883 | 986 |
|
40126 | 987 |
Note that @{ML fast_content} above is only defined as example. In |
988 |
many practical situations, it is customary to provide the |
|
989 |
incremental @{ML add_content} only and leave the initialization and |
|
990 |
termination to the concrete application by the user. |
|
39883 | 991 |
*} |
992 |
||
993 |
||
39854 | 994 |
section {* Message output channels \label{sec:message-channels} *} |
39835 | 995 |
|
996 |
text {* Isabelle provides output channels for different kinds of |
|
997 |
messages: regular output, high-volume tracing information, warnings, |
|
998 |
and errors. |
|
999 |
||
1000 |
Depending on the user interface involved, these messages may appear |
|
1001 |
in different text styles or colours. The standard output for |
|
1002 |
terminal sessions prefixes each line of warnings by @{verbatim |
|
1003 |
"###"} and errors by @{verbatim "***"}, but leaves anything else |
|
1004 |
unchanged. |
|
1005 |
||
1006 |
Messages are associated with the transaction context of the running |
|
1007 |
Isar command. This enables the front-end to manage commands and |
|
1008 |
resulting messages together. For example, after deleting a command |
|
1009 |
from a given theory document version, the corresponding message |
|
39872 | 1010 |
output can be retracted from the display. |
1011 |
*} |
|
39835 | 1012 |
|
1013 |
text %mlref {* |
|
1014 |
\begin{mldecls} |
|
1015 |
@{index_ML writeln: "string -> unit"} \\ |
|
1016 |
@{index_ML tracing: "string -> unit"} \\ |
|
1017 |
@{index_ML warning: "string -> unit"} \\ |
|
1018 |
@{index_ML error: "string -> 'a"} \\ |
|
1019 |
\end{mldecls} |
|
1020 |
||
1021 |
\begin{description} |
|
1022 |
||
1023 |
\item @{ML writeln}~@{text "text"} outputs @{text "text"} as regular |
|
1024 |
message. This is the primary message output operation of Isabelle |
|
1025 |
and should be used by default. |
|
1026 |
||
1027 |
\item @{ML tracing}~@{text "text"} outputs @{text "text"} as special |
|
1028 |
tracing message, indicating potential high-volume output to the |
|
1029 |
front-end (hundreds or thousands of messages issued by a single |
|
1030 |
command). The idea is to allow the user-interface to downgrade the |
|
1031 |
quality of message display to achieve higher throughput. |
|
1032 |
||
1033 |
Note that the user might have to take special actions to see tracing |
|
1034 |
output, e.g.\ switch to a different output window. So this channel |
|
1035 |
should not be used for regular output. |
|
1036 |
||
1037 |
\item @{ML warning}~@{text "text"} outputs @{text "text"} as |
|
1038 |
warning, which typically means some extra emphasis on the front-end |
|
40126 | 1039 |
side (color highlighting, icons, etc.). |
39835 | 1040 |
|
1041 |
\item @{ML error}~@{text "text"} raises exception @{ML ERROR}~@{text |
|
1042 |
"text"} and thus lets the Isar toplevel print @{text "text"} on the |
|
1043 |
error channel, which typically means some extra emphasis on the |
|
40126 | 1044 |
front-end side (color highlighting, icons, etc.). |
39835 | 1045 |
|
1046 |
This assumes that the exception is not handled before the command |
|
1047 |
terminates. Handling exception @{ML ERROR}~@{text "text"} is a |
|
1048 |
perfectly legal alternative: it means that the error is absorbed |
|
1049 |
without any message output. |
|
1050 |
||
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1051 |
\begin{warn} |
54387 | 1052 |
The actual error channel is accessed via @{ML Output.error_message}, but |
51058 | 1053 |
the old interaction protocol of Proof~General \emph{crashes} if that |
39835 | 1054 |
function is used in regular ML code: error output and toplevel |
52416 | 1055 |
command failure always need to coincide in classic TTY interaction. |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1056 |
\end{warn} |
39835 | 1057 |
|
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1058 |
\end{description} |
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1059 |
|
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1060 |
\begin{warn} |
39835 | 1061 |
Regular Isabelle/ML code should output messages exclusively by the |
1062 |
official channels. Using raw I/O on \emph{stdout} or \emph{stderr} |
|
1063 |
instead (e.g.\ via @{ML TextIO.output}) is apt to cause problems in |
|
1064 |
the presence of parallel and asynchronous processing of Isabelle |
|
1065 |
theories. Such raw output might be displayed by the front-end in |
|
1066 |
some system console log, with a low chance that the user will ever |
|
1067 |
see it. Moreover, as a genuine side-effect on global process |
|
1068 |
channels, there is no proper way to retract output when Isar command |
|
40126 | 1069 |
transactions are reset by the system. |
39861
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents:
39859
diff
changeset
|
1070 |
\end{warn} |
39872 | 1071 |
|
1072 |
\begin{warn} |
|
1073 |
The message channels should be used in a message-oriented manner. |
|
40126 | 1074 |
This means that multi-line output that logically belongs together is |
1075 |
issued by a \emph{single} invocation of @{ML writeln} etc.\ with the |
|
1076 |
functional concatenation of all message constituents. |
|
39872 | 1077 |
\end{warn} |
1078 |
*} |
|
1079 |
||
1080 |
text %mlex {* The following example demonstrates a multi-line |
|
1081 |
warning. Note that in some situations the user sees only the first |
|
1082 |
line, so the most important point should be made first. |
|
1083 |
*} |
|
1084 |
||
1085 |
ML_command {* |
|
1086 |
warning (cat_lines |
|
1087 |
["Beware the Jabberwock, my son!", |
|
1088 |
"The jaws that bite, the claws that catch!", |
|
1089 |
"Beware the Jubjub Bird, and shun", |
|
1090 |
"The frumious Bandersnatch!"]); |
|
39835 | 1091 |
*} |
1092 |
||
39854 | 1093 |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1094 |
section {* Exceptions \label{sec:exceptions} *} |
39854 | 1095 |
|
1096 |
text {* The Standard ML semantics of strict functional evaluation |
|
1097 |
together with exceptions is rather well defined, but some delicate |
|
1098 |
points need to be observed to avoid that ML programs go wrong |
|
1099 |
despite static type-checking. Exceptions in Isabelle/ML are |
|
1100 |
subsequently categorized as follows. |
|
1101 |
||
1102 |
\paragraph{Regular user errors.} These are meant to provide |
|
1103 |
informative feedback about malformed input etc. |
|
1104 |
||
1105 |
The \emph{error} function raises the corresponding \emph{ERROR} |
|
1106 |
exception, with a plain text message as argument. \emph{ERROR} |
|
1107 |
exceptions can be handled internally, in order to be ignored, turned |
|
1108 |
into other exceptions, or cascaded by appending messages. If the |
|
1109 |
corresponding Isabelle/Isar command terminates with an \emph{ERROR} |
|
39855 | 1110 |
exception state, the toplevel will print the result on the error |
1111 |
channel (see \secref{sec:message-channels}). |
|
39854 | 1112 |
|
1113 |
It is considered bad style to refer to internal function names or |
|
1114 |
values in ML source notation in user error messages. |
|
1115 |
||
1116 |
Grammatical correctness of error messages can be improved by |
|
1117 |
\emph{omitting} final punctuation: messages are often concatenated |
|
1118 |
or put into a larger context (e.g.\ augmented with source position). |
|
1119 |
By not insisting in the final word at the origin of the error, the |
|
1120 |
system can perform its administrative tasks more easily and |
|
1121 |
robustly. |
|
1122 |
||
1123 |
\paragraph{Program failures.} There is a handful of standard |
|
1124 |
exceptions that indicate general failure situations, or failures of |
|
1125 |
core operations on logical entities (types, terms, theorems, |
|
39856 | 1126 |
theories, see \chref{ch:logic}). |
39854 | 1127 |
|
1128 |
These exceptions indicate a genuine breakdown of the program, so the |
|
1129 |
main purpose is to determine quickly what has happened where. |
|
39855 | 1130 |
Traditionally, the (short) exception message would include the name |
40126 | 1131 |
of an ML function, although this is no longer necessary, because the |
1132 |
ML runtime system prints a detailed source position of the |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
1133 |
corresponding @{ML_text raise} keyword. |
39854 | 1134 |
|
1135 |
\medskip User modules can always introduce their own custom |
|
1136 |
exceptions locally, e.g.\ to organize internal failures robustly |
|
1137 |
without overlapping with existing exceptions. Exceptions that are |
|
1138 |
exposed in module signatures require extra care, though, and should |
|
40126 | 1139 |
\emph{not} be introduced by default. Surprise by users of a module |
1140 |
can be often minimized by using plain user errors instead. |
|
39854 | 1141 |
|
1142 |
\paragraph{Interrupts.} These indicate arbitrary system events: |
|
1143 |
both the ML runtime system and the Isabelle/ML infrastructure signal |
|
1144 |
various exceptional situations by raising the special |
|
1145 |
\emph{Interrupt} exception in user code. |
|
1146 |
||
1147 |
This is the one and only way that physical events can intrude an |
|
1148 |
Isabelle/ML program. Such an interrupt can mean out-of-memory, |
|
1149 |
stack overflow, timeout, internal signaling of threads, or the user |
|
39855 | 1150 |
producing a console interrupt manually etc. An Isabelle/ML program |
1151 |
that intercepts interrupts becomes dependent on physical effects of |
|
1152 |
the environment. Even worse, exception handling patterns that are |
|
56579 | 1153 |
too general by accident, e.g.\ by misspelled exception constructors, |
40126 | 1154 |
will cover interrupts unintentionally and thus render the program |
39855 | 1155 |
semantics ill-defined. |
39854 | 1156 |
|
1157 |
Note that the Interrupt exception dates back to the original SML90 |
|
1158 |
language definition. It was excluded from the SML97 version to |
|
1159 |
avoid its malign impact on ML program semantics, but without |
|
1160 |
providing a viable alternative. Isabelle/ML recovers physical |
|
40229 | 1161 |
interruptibility (which is an indispensable tool to implement |
1162 |
managed evaluation of command transactions), but requires user code |
|
1163 |
to be strictly transparent wrt.\ interrupts. |
|
39854 | 1164 |
|
1165 |
\begin{warn} |
|
1166 |
Isabelle/ML user code needs to terminate promptly on interruption, |
|
1167 |
without guessing at its meaning to the system infrastructure. |
|
1168 |
Temporary handling of interrupts for cleanup of global resources |
|
1169 |
etc.\ needs to be followed immediately by re-raising of the original |
|
1170 |
exception. |
|
1171 |
\end{warn} |
|
1172 |
*} |
|
1173 |
||
39855 | 1174 |
text %mlref {* |
1175 |
\begin{mldecls} |
|
1176 |
@{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\ |
|
1177 |
@{index_ML can: "('a -> 'b) -> 'a -> bool"} \\ |
|
55838 | 1178 |
@{index_ML_exception ERROR: string} \\ |
1179 |
@{index_ML_exception Fail: string} \\ |
|
39856 | 1180 |
@{index_ML Exn.is_interrupt: "exn -> bool"} \\ |
39855 | 1181 |
@{index_ML reraise: "exn -> 'a"} \\ |
56303
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents:
56199
diff
changeset
|
1182 |
@{index_ML Runtime.exn_trace: "(unit -> 'a) -> 'a"} \\ |
39855 | 1183 |
\end{mldecls} |
1184 |
||
1185 |
\begin{description} |
|
1186 |
||
1187 |
\item @{ML try}~@{text "f x"} makes the partiality of evaluating |
|
1188 |
@{text "f x"} explicit via the option datatype. Interrupts are |
|
1189 |
\emph{not} handled here, i.e.\ this form serves as safe replacement |
|
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
1190 |
for the \emph{unsafe} version @{ML_text "(SOME"}~@{text "f |
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
1191 |
x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in |
52417 | 1192 |
books about SML97, not Isabelle/ML. |
39855 | 1193 |
|
1194 |
\item @{ML can} is similar to @{ML try} with more abstract result. |
|
1195 |
||
39856 | 1196 |
\item @{ML ERROR}~@{text "msg"} represents user errors; this |
40126 | 1197 |
exception is normally raised indirectly via the @{ML error} function |
1198 |
(see \secref{sec:message-channels}). |
|
39856 | 1199 |
|
1200 |
\item @{ML Fail}~@{text "msg"} represents general program failures. |
|
1201 |
||
1202 |
\item @{ML Exn.is_interrupt} identifies interrupts robustly, without |
|
1203 |
mentioning concrete exception constructors in user code. Handled |
|
1204 |
interrupts need to be re-raised promptly! |
|
1205 |
||
39855 | 1206 |
\item @{ML reraise}~@{text "exn"} raises exception @{text "exn"} |
1207 |
while preserving its implicit position information (if possible, |
|
1208 |
depending on the ML platform). |
|
1209 |
||
56303
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents:
56199
diff
changeset
|
1210 |
\item @{ML Runtime.exn_trace}~@{ML_text "(fn () =>"}~@{text |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
1211 |
"e"}@{ML_text ")"} evaluates expression @{text "e"} while printing |
39855 | 1212 |
a full trace of its stack of nested exceptions (if possible, |
53739 | 1213 |
depending on the ML platform). |
39855 | 1214 |
|
56303
4cc3f4db3447
clarified Isabelle/ML bootstrap, such that Execution does not require ML_Compiler;
wenzelm
parents:
56199
diff
changeset
|
1215 |
Inserting @{ML Runtime.exn_trace} into ML code temporarily is |
53709
84522727f9d3
improved printing of exception trace in Poly/ML 5.5.1;
wenzelm
parents:
53167
diff
changeset
|
1216 |
useful for debugging, but not suitable for production code. |
39855 | 1217 |
|
1218 |
\end{description} |
|
1219 |
*} |
|
1220 |
||
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1221 |
text %mlantiq {* |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1222 |
\begin{matharray}{rcl} |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1223 |
@{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\ |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1224 |
\end{matharray} |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1225 |
|
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1226 |
\begin{description} |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1227 |
|
40110 | 1228 |
\item @{text "@{assert}"} inlines a function |
1229 |
@{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is |
|
1230 |
@{ML false}. Due to inlining the source position of failed |
|
1231 |
assertions is included in the error output. |
|
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1232 |
|
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1233 |
\end{description} |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1234 |
*} |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1235 |
|
39859 | 1236 |
|
52421
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1237 |
section {* Strings of symbols \label{sec:symbols} *} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1238 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1239 |
text {* A \emph{symbol} constitutes the smallest textual unit in |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1240 |
Isabelle/ML --- raw ML characters are normally not encountered at |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1241 |
all! Isabelle strings consist of a sequence of symbols, represented |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1242 |
as a packed string or an exploded list of strings. Each symbol is |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1243 |
in itself a small string, which has either one of the following |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1244 |
forms: |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1245 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1246 |
\begin{enumerate} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1247 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1248 |
\item a single ASCII character ``@{text "c"}'', for example |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1249 |
``\verb,a,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1250 |
|
56579 | 1251 |
\item a codepoint according to UTF-8 (non-ASCII byte sequence), |
52421
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1252 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1253 |
\item a regular symbol ``\verb,\,\verb,<,@{text "ident"}\verb,>,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1254 |
for example ``\verb,\,\verb,<alpha>,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1255 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1256 |
\item a control symbol ``\verb,\,\verb,<^,@{text "ident"}\verb,>,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1257 |
for example ``\verb,\,\verb,<^bold>,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1258 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1259 |
\item a raw symbol ``\verb,\,\verb,<^raw:,@{text text}\verb,>,'' |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1260 |
where @{text text} consists of printable characters excluding |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1261 |
``\verb,.,'' and ``\verb,>,'', for example |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1262 |
``\verb,\,\verb,<^raw:$\sum_{i = 1}^n$>,'', |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1263 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1264 |
\item a numbered raw control symbol ``\verb,\,\verb,<^raw,@{text |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1265 |
n}\verb,>, where @{text n} consists of digits, for example |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1266 |
``\verb,\,\verb,<^raw42>,''. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1267 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1268 |
\end{enumerate} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1269 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1270 |
The @{text "ident"} syntax for symbol names is @{text "letter |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1271 |
(letter | digit)\<^sup>*"}, where @{text "letter = A..Za..z"} and @{text |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1272 |
"digit = 0..9"}. There are infinitely many regular symbols and |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1273 |
control symbols, but a fixed collection of standard symbols is |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1274 |
treated specifically. For example, ``\verb,\,\verb,<alpha>,'' is |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1275 |
classified as a letter, which means it may occur within regular |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1276 |
Isabelle identifiers. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1277 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1278 |
The character set underlying Isabelle symbols is 7-bit ASCII, but |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1279 |
8-bit character sequences are passed-through unchanged. Unicode/UCS |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1280 |
data in UTF-8 encoding is processed in a non-strict fashion, such |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1281 |
that well-formed code sequences are recognized |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1282 |
accordingly.\footnote{Note that ISO-Latin-1 differs from UTF-8 only |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1283 |
in some special punctuation characters that even have replacements |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1284 |
within the standard collection of Isabelle symbols. Text consisting |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1285 |
of ASCII plus accented letters can be processed in either encoding.} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1286 |
Unicode provides its own collection of mathematical symbols, but |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1287 |
within the core Isabelle/ML world there is no link to the standard |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1288 |
collection of Isabelle regular symbols. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1289 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1290 |
\medskip Output of Isabelle symbols depends on the print mode |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1291 |
\cite{isabelle-isar-ref}. For example, the standard {\LaTeX} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1292 |
setup of the Isabelle document preparation system would present |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1293 |
``\verb,\,\verb,<alpha>,'' as @{text "\<alpha>"}, and |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1294 |
``\verb,\,\verb,<^bold>,\verb,\,\verb,<alpha>,'' as @{text "\<^bold>\<alpha>"}. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1295 |
On-screen rendering usually works by mapping a finite subset of |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1296 |
Isabelle symbols to suitable Unicode characters. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1297 |
*} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1298 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1299 |
text %mlref {* |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1300 |
\begin{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1301 |
@{index_ML_type "Symbol.symbol": string} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1302 |
@{index_ML Symbol.explode: "string -> Symbol.symbol list"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1303 |
@{index_ML Symbol.is_letter: "Symbol.symbol -> bool"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1304 |
@{index_ML Symbol.is_digit: "Symbol.symbol -> bool"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1305 |
@{index_ML Symbol.is_quasi: "Symbol.symbol -> bool"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1306 |
@{index_ML Symbol.is_blank: "Symbol.symbol -> bool"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1307 |
\end{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1308 |
\begin{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1309 |
@{index_ML_type "Symbol.sym"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1310 |
@{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1311 |
\end{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1312 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1313 |
\begin{description} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1314 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1315 |
\item Type @{ML_type "Symbol.symbol"} represents individual Isabelle |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1316 |
symbols. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1317 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1318 |
\item @{ML "Symbol.explode"}~@{text "str"} produces a symbol list |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1319 |
from the packed form. This function supersedes @{ML |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1320 |
"String.explode"} for virtually all purposes of manipulating text in |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1321 |
Isabelle!\footnote{The runtime overhead for exploded strings is |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1322 |
mainly that of the list structure: individual symbols that happen to |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1323 |
be a singleton string do not require extra memory in Poly/ML.} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1324 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1325 |
\item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1326 |
"Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify standard |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1327 |
symbols according to fixed syntactic conventions of Isabelle, cf.\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1328 |
\cite{isabelle-isar-ref}. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1329 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1330 |
\item Type @{ML_type "Symbol.sym"} is a concrete datatype that |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1331 |
represents the different kinds of symbols explicitly, with |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1332 |
constructors @{ML "Symbol.Char"}, @{ML "Symbol.Sym"}, @{ML |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1333 |
"Symbol.UTF8"}, @{ML "Symbol.Ctrl"}, @{ML "Symbol.Raw"}. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1334 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1335 |
\item @{ML "Symbol.decode"} converts the string representation of a |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1336 |
symbol into the datatype version. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1337 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1338 |
\end{description} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1339 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1340 |
\paragraph{Historical note.} In the original SML90 standard the |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1341 |
primitive ML type @{ML_type char} did not exists, and @{ML_text |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1342 |
"explode: string -> string list"} produced a list of singleton |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1343 |
strings like @{ML "raw_explode: string -> string list"} in |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1344 |
Isabelle/ML today. When SML97 came out, Isabelle did not adopt its |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1345 |
somewhat anachronistic 8-bit or 16-bit characters, but the idea of |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1346 |
exploding a string into a list of small strings was extended to |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1347 |
``symbols'' as explained above. Thus Isabelle sources can refer to |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1348 |
an infinite store of user-defined symbols, without having to worry |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1349 |
about the multitude of Unicode encodings that have emerged over the |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1350 |
years. *} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1351 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1352 |
|
39863 | 1353 |
section {* Basic data types *} |
39859 | 1354 |
|
40126 | 1355 |
text {* The basis library proposal of SML97 needs to be treated with |
39859 | 1356 |
caution. Many of its operations simply do not fit with important |
1357 |
Isabelle/ML conventions (like ``canonical argument order'', see |
|
40126 | 1358 |
\secref{sec:canonical-argument-order}), others cause problems with |
1359 |
the parallel evaluation model of Isabelle/ML (such as @{ML |
|
1360 |
TextIO.print} or @{ML OS.Process.system}). |
|
39859 | 1361 |
|
1362 |
Subsequently we give a brief overview of important operations on |
|
1363 |
basic ML data types. |
|
1364 |
*} |
|
1365 |
||
1366 |
||
39863 | 1367 |
subsection {* Characters *} |
1368 |
||
1369 |
text %mlref {* |
|
1370 |
\begin{mldecls} |
|
1371 |
@{index_ML_type char} \\ |
|
1372 |
\end{mldecls} |
|
1373 |
||
1374 |
\begin{description} |
|
1375 |
||
39864 | 1376 |
\item Type @{ML_type char} is \emph{not} used. The smallest textual |
40126 | 1377 |
unit in Isabelle is represented as a ``symbol'' (see |
39864 | 1378 |
\secref{sec:symbols}). |
39863 | 1379 |
|
1380 |
\end{description} |
|
1381 |
*} |
|
1382 |
||
1383 |
||
52421
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1384 |
subsection {* Strings *} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1385 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1386 |
text %mlref {* |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1387 |
\begin{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1388 |
@{index_ML_type string} \\ |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1389 |
\end{mldecls} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1390 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1391 |
\begin{description} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1392 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1393 |
\item Type @{ML_type string} represents immutable vectors of 8-bit |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1394 |
characters. There are operations in SML to convert back and forth |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1395 |
to actual byte vectors, which are seldom used. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1396 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1397 |
This historically important raw text representation is used for |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1398 |
Isabelle-specific purposes with the following implicit substructures |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1399 |
packed into the string content: |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1400 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1401 |
\begin{enumerate} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1402 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1403 |
\item sequence of Isabelle symbols (see also \secref{sec:symbols}), |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1404 |
with @{ML Symbol.explode} as key operation; |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1405 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1406 |
\item XML tree structure via YXML (see also \cite{isabelle-sys}), |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1407 |
with @{ML YXML.parse_body} as key operation. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1408 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1409 |
\end{enumerate} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1410 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1411 |
Note that Isabelle/ML string literals may refer Isabelle symbols |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1412 |
like ``\verb,\,\verb,<alpha>,'' natively, \emph{without} escaping |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1413 |
the backslash. This is a consequence of Isabelle treating all |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1414 |
source text as strings of symbols, instead of raw characters. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1415 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1416 |
\end{description} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1417 |
*} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1418 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1419 |
text %mlex {* The subsequent example illustrates the difference of |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1420 |
physical addressing of bytes versus logical addressing of symbols in |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1421 |
Isabelle strings. |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1422 |
*} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1423 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1424 |
ML_val {* |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1425 |
val s = "\<A>"; |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1426 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1427 |
@{assert} (length (Symbol.explode s) = 1); |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1428 |
@{assert} (size s = 4); |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1429 |
*} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1430 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1431 |
text {* Note that in Unicode renderings of the symbol @{text "\<A>"}, |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1432 |
variations of encodings like UTF-8 or UTF-16 pose delicate questions |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1433 |
about the multi-byte representations its codepoint, which is outside |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1434 |
of the 16-bit address space of the original Unicode standard from |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1435 |
the 1990-ies. In Isabelle/ML it is just ``\verb,\,\verb,<A>,'' |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1436 |
literally, using plain ASCII characters beyond any doubts. *} |
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1437 |
|
6d93140a206c
clarified strings of symbols, including ML string literals;
wenzelm
parents:
52420
diff
changeset
|
1438 |
|
39862 | 1439 |
subsection {* Integers *} |
1440 |
||
1441 |
text %mlref {* |
|
1442 |
\begin{mldecls} |
|
1443 |
@{index_ML_type int} \\ |
|
1444 |
\end{mldecls} |
|
1445 |
||
1446 |
\begin{description} |
|
1447 |
||
39864 | 1448 |
\item Type @{ML_type int} represents regular mathematical integers, |
1449 |
which are \emph{unbounded}. Overflow never happens in |
|
39862 | 1450 |
practice.\footnote{The size limit for integer bit patterns in memory |
1451 |
is 64\,MB for 32-bit Poly/ML, and much higher for 64-bit systems.} |
|
1452 |
This works uniformly for all supported ML platforms (Poly/ML and |
|
1453 |
SML/NJ). |
|
1454 |
||
40126 | 1455 |
Literal integers in ML text are forced to be of this one true |
52417 | 1456 |
integer type --- adhoc overloading of SML97 is disabled. |
39862 | 1457 |
|
55837 | 1458 |
Structure @{ML_structure IntInf} of SML97 is obsolete and superseded by |
1459 |
@{ML_structure Int}. Structure @{ML_structure Integer} in @{file |
|
39862 | 1460 |
"~~/src/Pure/General/integer.ML"} provides some additional |
1461 |
operations. |
|
1462 |
||
1463 |
\end{description} |
|
1464 |
*} |
|
1465 |
||
1466 |
||
40302 | 1467 |
subsection {* Time *} |
1468 |
||
1469 |
text %mlref {* |
|
1470 |
\begin{mldecls} |
|
1471 |
@{index_ML_type Time.time} \\ |
|
1472 |
@{index_ML seconds: "real -> Time.time"} \\ |
|
1473 |
\end{mldecls} |
|
1474 |
||
1475 |
\begin{description} |
|
1476 |
||
1477 |
\item Type @{ML_type Time.time} represents time abstractly according |
|
1478 |
to the SML97 basis library definition. This is adequate for |
|
1479 |
internal ML operations, but awkward in concrete time specifications. |
|
1480 |
||
1481 |
\item @{ML seconds}~@{text "s"} turns the concrete scalar @{text |
|
1482 |
"s"} (measured in seconds) into an abstract time value. Floating |
|
52417 | 1483 |
point numbers are easy to use as configuration options in the |
1484 |
context (see \secref{sec:config-options}) or system preferences that |
|
1485 |
are maintained externally. |
|
40302 | 1486 |
|
1487 |
\end{description} |
|
1488 |
*} |
|
1489 |
||
1490 |
||
39859 | 1491 |
subsection {* Options *} |
1492 |
||
1493 |
text %mlref {* |
|
1494 |
\begin{mldecls} |
|
1495 |
@{index_ML Option.map: "('a -> 'b) -> 'a option -> 'b option"} \\ |
|
1496 |
@{index_ML is_some: "'a option -> bool"} \\ |
|
1497 |
@{index_ML is_none: "'a option -> bool"} \\ |
|
1498 |
@{index_ML the: "'a option -> 'a"} \\ |
|
1499 |
@{index_ML these: "'a list option -> 'a list"} \\ |
|
1500 |
@{index_ML the_list: "'a option -> 'a list"} \\ |
|
1501 |
@{index_ML the_default: "'a -> 'a option -> 'a"} \\ |
|
1502 |
\end{mldecls} |
|
1503 |
*} |
|
1504 |
||
52417 | 1505 |
text {* Apart from @{ML Option.map} most other operations defined in |
55837 | 1506 |
structure @{ML_structure Option} are alien to Isabelle/ML an never |
52417 | 1507 |
used. The operations shown above are defined in @{file |
1508 |
"~~/src/Pure/General/basics.ML"}. *} |
|
39859 | 1509 |
|
1510 |
||
39863 | 1511 |
subsection {* Lists *} |
1512 |
||
1513 |
text {* Lists are ubiquitous in ML as simple and light-weight |
|
1514 |
``collections'' for many everyday programming tasks. Isabelle/ML |
|
39874 | 1515 |
provides important additions and improvements over operations that |
1516 |
are predefined in the SML97 library. *} |
|
39863 | 1517 |
|
1518 |
text %mlref {* |
|
1519 |
\begin{mldecls} |
|
1520 |
@{index_ML cons: "'a -> 'a list -> 'a list"} \\ |
|
39874 | 1521 |
@{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\ |
1522 |
@{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\ |
|
1523 |
@{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\ |
|
1524 |
@{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\ |
|
39863 | 1525 |
\end{mldecls} |
1526 |
||
1527 |
\begin{description} |
|
1528 |
||
1529 |
\item @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}. |
|
1530 |
||
1531 |
Tupled infix operators are a historical accident in Standard ML. |
|
1532 |
The curried @{ML cons} amends this, but it should be only used when |
|
1533 |
partial application is required. |
|
1534 |
||
39874 | 1535 |
\item @{ML member}, @{ML insert}, @{ML remove}, @{ML update} treat |
1536 |
lists as a set-like container that maintains the order of elements. |
|
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
1537 |
See @{file "~~/src/Pure/library.ML"} for the full specifications |
39874 | 1538 |
(written in ML). There are some further derived operations like |
1539 |
@{ML union} or @{ML inter}. |
|
1540 |
||
1541 |
Note that @{ML insert} is conservative about elements that are |
|
1542 |
already a @{ML member} of the list, while @{ML update} ensures that |
|
40126 | 1543 |
the latest entry is always put in front. The latter discipline is |
39874 | 1544 |
often more appropriate in declarations of context data |
1545 |
(\secref{sec:context-data}) that are issued by the user in Isar |
|
52417 | 1546 |
source: later declarations take precedence over earlier ones. |
39874 | 1547 |
|
39863 | 1548 |
\end{description} |
1549 |
*} |
|
1550 |
||
52417 | 1551 |
text %mlex {* Using canonical @{ML fold} together with @{ML cons} (or |
1552 |
similar standard operations) alternates the orientation of data. |
|
40126 | 1553 |
The is quite natural and should not be altered forcible by inserting |
1554 |
extra applications of @{ML rev}. The alternative @{ML fold_rev} can |
|
1555 |
be used in the few situations, where alternation should be |
|
1556 |
prevented. |
|
39863 | 1557 |
*} |
1558 |
||
1559 |
ML {* |
|
1560 |
val items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
1561 |
||
1562 |
val list1 = fold cons items []; |
|
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1563 |
@{assert} (list1 = rev items); |
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1564 |
|
39863 | 1565 |
val list2 = fold_rev cons items []; |
39866
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents:
39864
diff
changeset
|
1566 |
@{assert} (list2 = items); |
39863 | 1567 |
*} |
1568 |
||
39883 | 1569 |
text {* The subsequent example demonstrates how to \emph{merge} two |
1570 |
lists in a natural way. *} |
|
1571 |
||
1572 |
ML {* |
|
1573 |
fun merge_lists eq (xs, ys) = fold_rev (insert eq) ys xs; |
|
1574 |
*} |
|
1575 |
||
1576 |
text {* Here the first list is treated conservatively: only the new |
|
1577 |
elements from the second list are inserted. The inside-out order of |
|
1578 |
insertion via @{ML fold_rev} attempts to preserve the order of |
|
1579 |
elements in the result. |
|
1580 |
||
1581 |
This way of merging lists is typical for context data |
|
1582 |
(\secref{sec:context-data}). See also @{ML merge} as defined in |
|
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
1583 |
@{file "~~/src/Pure/library.ML"}. |
39883 | 1584 |
*} |
1585 |
||
39863 | 1586 |
|
39875
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1587 |
subsection {* Association lists *} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1588 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1589 |
text {* The operations for association lists interpret a concrete list |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1590 |
of pairs as a finite function from keys to values. Redundant |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1591 |
representations with multiple occurrences of the same key are |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1592 |
implicitly normalized: lookup and update only take the first |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1593 |
occurrence into account. |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1594 |
*} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1595 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1596 |
text {* |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1597 |
\begin{mldecls} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1598 |
@{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\ |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1599 |
@{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\ |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1600 |
@{index_ML AList.update: "('a * 'a -> bool) -> 'a * 'b -> ('a * 'b) list -> ('a * 'b) list"} \\ |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1601 |
\end{mldecls} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1602 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1603 |
\begin{description} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1604 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1605 |
\item @{ML AList.lookup}, @{ML AList.defined}, @{ML AList.update} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1606 |
implement the main ``framework operations'' for mappings in |
40126 | 1607 |
Isabelle/ML, following standard conventions for their names and |
1608 |
types. |
|
39875
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1609 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1610 |
Note that a function called @{text lookup} is obliged to express its |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1611 |
partiality via an explicit option element. There is no choice to |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1612 |
raise an exception, without changing the name to something like |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1613 |
@{text "the_element"} or @{text "get"}. |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1614 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1615 |
The @{text "defined"} operation is essentially a contraction of @{ML |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1616 |
is_some} and @{text "lookup"}, but this is sufficiently frequent to |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1617 |
justify its independent existence. This also gives the |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1618 |
implementation some opportunity for peep-hole optimization. |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1619 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1620 |
\end{description} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1621 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1622 |
Association lists are adequate as simple and light-weight |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1623 |
implementation of finite mappings in many practical situations. A |
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
1624 |
more heavy-duty table structure is defined in @{file |
39875
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1625 |
"~~/src/Pure/General/table.ML"}; that version scales easily to |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1626 |
thousands or millions of elements. |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1627 |
*} |
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1628 |
|
648c930125f6
more on "Association lists", based on more succinct version of older material;
wenzelm
parents:
39874
diff
changeset
|
1629 |
|
39859 | 1630 |
subsection {* Unsynchronized references *} |
1631 |
||
1632 |
text %mlref {* |
|
1633 |
\begin{mldecls} |
|
39870 | 1634 |
@{index_ML_type "'a Unsynchronized.ref"} \\ |
39859 | 1635 |
@{index_ML Unsynchronized.ref: "'a -> 'a Unsynchronized.ref"} \\ |
1636 |
@{index_ML "!": "'a Unsynchronized.ref -> 'a"} \\ |
|
46262 | 1637 |
@{index_ML_op ":=": "'a Unsynchronized.ref * 'a -> unit"} \\ |
39859 | 1638 |
\end{mldecls} |
1639 |
*} |
|
1640 |
||
1641 |
text {* Due to ubiquitous parallelism in Isabelle/ML (see also |
|
1642 |
\secref{sec:multi-threading}), the mutable reference cells of |
|
1643 |
Standard ML are notorious for causing problems. In a highly |
|
1644 |
parallel system, both correctness \emph{and} performance are easily |
|
1645 |
degraded when using mutable data. |
|
1646 |
||
1647 |
The unwieldy name of @{ML Unsynchronized.ref} for the constructor |
|
1648 |
for references in Isabelle/ML emphasizes the inconveniences caused by |
|
46262 | 1649 |
mutability. Existing operations @{ML "!"} and @{ML_op ":="} are |
39859 | 1650 |
unchanged, but should be used with special precautions, say in a |
1651 |
strictly local situation that is guaranteed to be restricted to |
|
40508 | 1652 |
sequential evaluation --- now and in the future. |
1653 |
||
1654 |
\begin{warn} |
|
1655 |
Never @{ML_text "open Unsynchronized"}, not even in a local scope! |
|
1656 |
Pretending that mutable state is no problem is a very bad idea. |
|
1657 |
\end{warn} |
|
1658 |
*} |
|
39859 | 1659 |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1660 |
|
39870 | 1661 |
section {* Thread-safe programming \label{sec:multi-threading} *} |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1662 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1663 |
text {* Multi-threaded execution has become an everyday reality in |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1664 |
Isabelle since Poly/ML 5.2.1 and Isabelle2008. Isabelle/ML provides |
39868 | 1665 |
implicit and explicit parallelism by default, and there is no way |
1666 |
for user-space tools to ``opt out''. ML programs that are purely |
|
1667 |
functional, output messages only via the official channels |
|
1668 |
(\secref{sec:message-channels}), and do not intercept interrupts |
|
1669 |
(\secref{sec:exceptions}) can participate in the multi-threaded |
|
1670 |
environment immediately without further ado. |
|
1671 |
||
1672 |
More ambitious tools with more fine-grained interaction with the |
|
1673 |
environment need to observe the principles explained below. |
|
1674 |
*} |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1675 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1676 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1677 |
subsection {* Multi-threading with shared memory *} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1678 |
|
39868 | 1679 |
text {* Multiple threads help to organize advanced operations of the |
1680 |
system, such as real-time conditions on command transactions, |
|
1681 |
sub-components with explicit communication, general asynchronous |
|
1682 |
interaction etc. Moreover, parallel evaluation is a prerequisite to |
|
1683 |
make adequate use of the CPU resources that are available on |
|
1684 |
multi-core systems.\footnote{Multi-core computing does not mean that |
|
1685 |
there are ``spare cycles'' to be wasted. It means that the |
|
1686 |
continued exponential speedup of CPU performance due to ``Moore's |
|
1687 |
Law'' follows different rules: clock frequency has reached its peak |
|
1688 |
around 2005, and applications need to be parallelized in order to |
|
1689 |
avoid a perceived loss of performance. See also |
|
1690 |
\cite{Sutter:2005}.} |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1691 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1692 |
Isabelle/Isar exploits the inherent structure of theories and proofs |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1693 |
to support \emph{implicit parallelism} to a large extent. LCF-style |
40126 | 1694 |
theorem provides almost ideal conditions for that, see also |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1695 |
\cite{Wenzel:2009}. This means, significant parts of theory and |
52418 | 1696 |
proof checking is parallelized by default. In Isabelle2013, a |
1697 |
maximum speedup-factor of 3.5 on 4 cores and 6.5 on 8 cores can be |
|
1698 |
expected. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1699 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1700 |
\medskip ML threads lack the memory protection of separate |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1701 |
processes, and operate concurrently on shared heap memory. This has |
40126 | 1702 |
the advantage that results of independent computations are directly |
1703 |
available to other threads: abstract values can be passed without |
|
1704 |
copying or awkward serialization that is typically required for |
|
1705 |
separate processes. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1706 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1707 |
To make shared-memory multi-threading work robustly and efficiently, |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1708 |
some programming guidelines need to be observed. While the ML |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1709 |
system is responsible to maintain basic integrity of the |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1710 |
representation of ML values in memory, the application programmer |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1711 |
needs to ensure that multi-threaded execution does not break the |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1712 |
intended semantics. |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1713 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1714 |
\begin{warn} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1715 |
To participate in implicit parallelism, tools need to be |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1716 |
thread-safe. A single ill-behaved tool can affect the stability and |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1717 |
performance of the whole system. |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1718 |
\end{warn} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1719 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1720 |
Apart from observing the principles of thread-safeness passively, |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1721 |
advanced tools may also exploit parallelism actively, e.g.\ by using |
39868 | 1722 |
``future values'' (\secref{sec:futures}) or the more basic library |
1723 |
functions for parallel list operations (\secref{sec:parlist}). |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1724 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1725 |
\begin{warn} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1726 |
Parallel computing resources are managed centrally by the |
39868 | 1727 |
Isabelle/ML infrastructure. User programs must not fork their own |
1728 |
ML threads to perform computations. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1729 |
\end{warn} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1730 |
*} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1731 |
|
39868 | 1732 |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1733 |
subsection {* Critical shared resources *} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1734 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1735 |
text {* Thread-safeness is mainly concerned about concurrent |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1736 |
read/write access to shared resources, which are outside the purely |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1737 |
functional world of ML. This covers the following in particular. |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1738 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1739 |
\begin{itemize} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1740 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1741 |
\item Global references (or arrays), i.e.\ mutable memory cells that |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1742 |
persist over several invocations of associated |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1743 |
operations.\footnote{This is independent of the visibility of such |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1744 |
mutable values in the toplevel scope.} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1745 |
|
39868 | 1746 |
\item Global state of the running Isabelle/ML process, i.e.\ raw I/O |
1747 |
channels, environment variables, current working directory. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1748 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1749 |
\item Writable resources in the file-system that are shared among |
40126 | 1750 |
different threads or external processes. |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1751 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1752 |
\end{itemize} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1753 |
|
39868 | 1754 |
Isabelle/ML provides various mechanisms to avoid critical shared |
40126 | 1755 |
resources in most situations. As last resort there are some |
1756 |
mechanisms for explicit synchronization. The following guidelines |
|
1757 |
help to make Isabelle/ML programs work smoothly in a concurrent |
|
1758 |
environment. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1759 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1760 |
\begin{itemize} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1761 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1762 |
\item Avoid global references altogether. Isabelle/Isar maintains a |
39868 | 1763 |
uniform context that incorporates arbitrary data declared by user |
1764 |
programs (\secref{sec:context-data}). This context is passed as |
|
1765 |
plain value and user tools can get/map their own data in a purely |
|
1766 |
functional manner. Configuration options within the context |
|
1767 |
(\secref{sec:config-options}) provide simple drop-in replacements |
|
40126 | 1768 |
for historic reference variables. |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1769 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1770 |
\item Keep components with local state information re-entrant. |
39868 | 1771 |
Instead of poking initial values into (private) global references, a |
1772 |
new state record can be created on each invocation, and passed |
|
1773 |
through any auxiliary functions of the component. The state record |
|
1774 |
may well contain mutable references, without requiring any special |
|
1775 |
synchronizations, as long as each invocation gets its own copy. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1776 |
|
39868 | 1777 |
\item Avoid raw output on @{text "stdout"} or @{text "stderr"}. The |
1778 |
Poly/ML library is thread-safe for each individual output operation, |
|
1779 |
but the ordering of parallel invocations is arbitrary. This means |
|
1780 |
raw output will appear on some system console with unpredictable |
|
1781 |
interleaving of atomic chunks. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1782 |
|
39868 | 1783 |
Note that this does not affect regular message output channels |
1784 |
(\secref{sec:message-channels}). An official message is associated |
|
1785 |
with the command transaction from where it originates, independently |
|
1786 |
of other transactions. This means each running Isar command has |
|
1787 |
effectively its own set of message channels, and interleaving can |
|
1788 |
only happen when commands use parallelism internally (and only at |
|
1789 |
message boundaries). |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1790 |
|
39868 | 1791 |
\item Treat environment variables and the current working directory |
1792 |
of the running process as strictly read-only. |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1793 |
|
39868 | 1794 |
\item Restrict writing to the file-system to unique temporary files. |
1795 |
Isabelle already provides a temporary directory that is unique for |
|
1796 |
the running process, and there is a centralized source of unique |
|
1797 |
serial numbers in Isabelle/ML. Thus temporary files that are passed |
|
1798 |
to to some external process will be always disjoint, and thus |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1799 |
thread-safe. |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1800 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1801 |
\end{itemize} |
39868 | 1802 |
*} |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1803 |
|
39868 | 1804 |
text %mlref {* |
1805 |
\begin{mldecls} |
|
1806 |
@{index_ML File.tmp_path: "Path.T -> Path.T"} \\ |
|
1807 |
@{index_ML serial_string: "unit -> string"} \\ |
|
1808 |
\end{mldecls} |
|
1809 |
||
1810 |
\begin{description} |
|
1811 |
||
1812 |
\item @{ML File.tmp_path}~@{text "path"} relocates the base |
|
1813 |
component of @{text "path"} into the unique temporary directory of |
|
1814 |
the running Isabelle/ML process. |
|
1815 |
||
1816 |
\item @{ML serial_string}~@{text "()"} creates a new serial number |
|
1817 |
that is unique over the runtime of the Isabelle/ML process. |
|
1818 |
||
1819 |
\end{description} |
|
1820 |
*} |
|
1821 |
||
1822 |
text %mlex {* The following example shows how to create unique |
|
1823 |
temporary file names. |
|
1824 |
*} |
|
1825 |
||
1826 |
ML {* |
|
1827 |
val tmp1 = File.tmp_path (Path.basic ("foo" ^ serial_string ())); |
|
1828 |
val tmp2 = File.tmp_path (Path.basic ("foo" ^ serial_string ())); |
|
1829 |
@{assert} (tmp1 <> tmp2); |
|
1830 |
*} |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1831 |
|
39868 | 1832 |
|
1833 |
subsection {* Explicit synchronization *} |
|
1834 |
||
1835 |
text {* Isabelle/ML also provides some explicit synchronization |
|
1836 |
mechanisms, for the rare situations where mutable shared resources |
|
1837 |
are really required. These are based on the synchronizations |
|
1838 |
primitives of Poly/ML, which have been adapted to the specific |
|
1839 |
assumptions of the concurrent Isabelle/ML environment. User code |
|
1840 |
must not use the Poly/ML primitives directly! |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1841 |
|
39868 | 1842 |
\medskip The most basic synchronization concept is a single |
1843 |
\emph{critical section} (also called ``monitor'' in the literature). |
|
1844 |
A thread that enters the critical section prevents all other threads |
|
1845 |
from doing the same. A thread that is already within the critical |
|
1846 |
section may re-enter it in an idempotent manner. |
|
1847 |
||
1848 |
Such centralized locking is convenient, because it prevents |
|
1849 |
deadlocks by construction. |
|
1850 |
||
1851 |
\medskip More fine-grained locking works via \emph{synchronized |
|
1852 |
variables}. An explicit state component is associated with |
|
1853 |
mechanisms for locking and signaling. There are operations to |
|
1854 |
await a condition, change the state, and signal the change to all |
|
1855 |
other waiting threads. |
|
1856 |
||
1857 |
Here the synchronized access to the state variable is \emph{not} |
|
1858 |
re-entrant: direct or indirect nesting within the same thread will |
|
1859 |
cause a deadlock! |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1860 |
*} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1861 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1862 |
text %mlref {* |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1863 |
\begin{mldecls} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1864 |
@{index_ML NAMED_CRITICAL: "string -> (unit -> 'a) -> 'a"} \\ |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1865 |
@{index_ML CRITICAL: "(unit -> 'a) -> 'a"} \\ |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1866 |
\end{mldecls} |
39871 | 1867 |
\begin{mldecls} |
1868 |
@{index_ML_type "'a Synchronized.var"} \\ |
|
1869 |
@{index_ML Synchronized.var: "string -> 'a -> 'a Synchronized.var"} \\ |
|
1870 |
@{index_ML Synchronized.guarded_access: "'a Synchronized.var -> |
|
1871 |
('a -> ('b * 'a) option) -> 'b"} \\ |
|
1872 |
\end{mldecls} |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1873 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1874 |
\begin{description} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1875 |
|
39868 | 1876 |
\item @{ML NAMED_CRITICAL}~@{text "name e"} evaluates @{text "e ()"} |
1877 |
within the central critical section of Isabelle/ML. No other thread |
|
1878 |
may do so at the same time, but non-critical parallel execution will |
|
39871 | 1879 |
continue. The @{text "name"} argument is used for tracing and might |
39868 | 1880 |
help to spot sources of congestion. |
1881 |
||
52418 | 1882 |
Entering the critical section without contention is very fast. Each |
1883 |
thread should stay within the critical section only very briefly, |
|
40126 | 1884 |
otherwise parallel performance may degrade. |
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1885 |
|
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1886 |
\item @{ML CRITICAL} is the same as @{ML NAMED_CRITICAL} with empty |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1887 |
name argument. |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1888 |
|
39871 | 1889 |
\item Type @{ML_type "'a Synchronized.var"} represents synchronized |
1890 |
variables with state of type @{ML_type 'a}. |
|
1891 |
||
1892 |
\item @{ML Synchronized.var}~@{text "name x"} creates a synchronized |
|
1893 |
variable that is initialized with value @{text "x"}. The @{text |
|
1894 |
"name"} is used for tracing. |
|
1895 |
||
1896 |
\item @{ML Synchronized.guarded_access}~@{text "var f"} lets the |
|
1897 |
function @{text "f"} operate within a critical section on the state |
|
40126 | 1898 |
@{text "x"} as follows: if @{text "f x"} produces @{ML NONE}, it |
1899 |
continues to wait on the internal condition variable, expecting that |
|
39871 | 1900 |
some other thread will eventually change the content in a suitable |
40126 | 1901 |
manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is |
1902 |
satisfied and assigns the new state value @{text "x'"}, broadcasts a |
|
1903 |
signal to all waiting threads on the associated condition variable, |
|
1904 |
and returns the result @{text "y"}. |
|
39871 | 1905 |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1906 |
\end{description} |
39871 | 1907 |
|
40126 | 1908 |
There are some further variants of the @{ML |
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
1909 |
Synchronized.guarded_access} combinator, see @{file |
39871 | 1910 |
"~~/src/Pure/Concurrent/synchronized.ML"} for details. |
1911 |
*} |
|
1912 |
||
40126 | 1913 |
text %mlex {* The following example implements a counter that produces |
39871 | 1914 |
positive integers that are unique over the runtime of the Isabelle |
1915 |
process: |
|
1916 |
*} |
|
1917 |
||
1918 |
ML {* |
|
1919 |
local |
|
1920 |
val counter = Synchronized.var "counter" 0; |
|
1921 |
in |
|
1922 |
fun next () = |
|
1923 |
Synchronized.guarded_access counter |
|
1924 |
(fn i => |
|
1925 |
let val j = i + 1 |
|
1926 |
in SOME (j, j) end); |
|
1927 |
end; |
|
1928 |
*} |
|
1929 |
||
1930 |
ML {* |
|
1931 |
val a = next (); |
|
1932 |
val b = next (); |
|
1933 |
@{assert} (a <> b); |
|
39867
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1934 |
*} |
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
wenzelm
parents:
39866
diff
changeset
|
1935 |
|
40800
330eb65c9469
Parse.liberal_name for document antiquotations and attributes;
wenzelm
parents:
40508
diff
changeset
|
1936 |
text {* \medskip See @{file "~~/src/Pure/Concurrent/mailbox.ML"} how |
40126 | 1937 |
to implement a mailbox as synchronized variable over a purely |
1938 |
functional queue. *} |
|
1939 |
||
52419 | 1940 |
|
1941 |
section {* Managed evaluation *} |
|
1942 |
||
1943 |
text {* Execution of Standard ML follows the model of strict |
|
1944 |
functional evaluation with optional exceptions. Evaluation happens |
|
1945 |
whenever some function is applied to (sufficiently many) |
|
1946 |
arguments. The result is either an explicit value or an implicit |
|
1947 |
exception. |
|
1948 |
||
1949 |
\emph{Managed evaluation} in Isabelle/ML organizes expressions and |
|
1950 |
results to control certain physical side-conditions, to say more |
|
1951 |
specifically when and how evaluation happens. For example, the |
|
1952 |
Isabelle/ML library supports lazy evaluation with memoing, parallel |
|
1953 |
evaluation via futures, asynchronous evaluation via promises, |
|
1954 |
evaluation with time limit etc. |
|
1955 |
||
1956 |
\medskip An \emph{unevaluated expression} is represented either as |
|
1957 |
unit abstraction @{verbatim "fn () => a"} of type |
|
1958 |
@{verbatim "unit -> 'a"} or as regular function |
|
1959 |
@{verbatim "fn a => b"} of type @{verbatim "'a -> 'b"}. Both forms |
|
1960 |
occur routinely, and special care is required to tell them apart --- |
|
1961 |
the static type-system of SML is only of limited help here. |
|
1962 |
||
1963 |
The first form is more intuitive: some combinator @{text "(unit -> |
|
1964 |
'a) -> 'a"} applies the given function to @{text "()"} to initiate |
|
1965 |
the postponed evaluation process. The second form is more flexible: |
|
1966 |
some combinator @{text "('a -> 'b) -> 'a -> 'b"} acts like a |
|
1967 |
modified form of function application; several such combinators may |
|
1968 |
be cascaded to modify a given function, before it is ultimately |
|
1969 |
applied to some argument. |
|
1970 |
||
1971 |
\medskip \emph{Reified results} make the disjoint sum of regular |
|
1972 |
values versions exceptional situations explicit as ML datatype: |
|
1973 |
@{text "'a result = Res of 'a | Exn of exn"}. This is typically |
|
1974 |
used for administrative purposes, to store the overall outcome of an |
|
1975 |
evaluation process. |
|
1976 |
||
1977 |
\emph{Parallel exceptions} aggregate reified results, such that |
|
1978 |
multiple exceptions are digested as a collection in canonical form |
|
1979 |
that identifies exceptions according to their original occurrence. |
|
1980 |
This is particular important for parallel evaluation via futures |
|
1981 |
\secref{sec:futures}, which are organized as acyclic graph of |
|
1982 |
evaluations that depend on other evaluations: exceptions stemming |
|
1983 |
from shared sub-graphs are exposed exactly once and in the order of |
|
1984 |
their original occurrence (e.g.\ when printed at the toplevel). |
|
1985 |
Interrupt counts as neutral element here: it is treated as minimal |
|
1986 |
information about some canceled evaluation process, and is absorbed |
|
1987 |
by the presence of regular program exceptions. *} |
|
1988 |
||
1989 |
text %mlref {* |
|
1990 |
\begin{mldecls} |
|
1991 |
@{index_ML_type "'a Exn.result"} \\ |
|
1992 |
@{index_ML Exn.capture: "('a -> 'b) -> 'a -> 'b Exn.result"} \\ |
|
1993 |
@{index_ML Exn.interruptible_capture: "('a -> 'b) -> 'a -> 'b Exn.result"} \\ |
|
1994 |
@{index_ML Exn.release: "'a Exn.result -> 'a"} \\ |
|
1995 |
@{index_ML Par_Exn.release_all: "'a Exn.result list -> 'a list"} \\ |
|
1996 |
@{index_ML Par_Exn.release_first: "'a Exn.result list -> 'a list"} \\ |
|
1997 |
\end{mldecls} |
|
1998 |
||
1999 |
\begin{description} |
|
2000 |
||
2001 |
\item Type @{ML_type "'a Exn.result"} represents the disjoint sum of |
|
2002 |
ML results explicitly, with constructor @{ML Exn.Res} for regular |
|
2003 |
values and @{ML "Exn.Exn"} for exceptions. |
|
2004 |
||
2005 |
\item @{ML Exn.capture}~@{text "f x"} manages the evaluation of |
|
2006 |
@{text "f x"} such that exceptions are made explicit as @{ML |
|
2007 |
"Exn.Exn"}. Note that this includes physical interrupts (see also |
|
2008 |
\secref{sec:exceptions}), so the same precautions apply to user |
|
2009 |
code: interrupts must not be absorbed accidentally! |
|
2010 |
||
2011 |
\item @{ML Exn.interruptible_capture} is similar to @{ML |
|
2012 |
Exn.capture}, but interrupts are immediately re-raised as required |
|
2013 |
for user code. |
|
2014 |
||
2015 |
\item @{ML Exn.release}~@{text "result"} releases the original |
|
2016 |
runtime result, exposing its regular value or raising the reified |
|
2017 |
exception. |
|
2018 |
||
2019 |
\item @{ML Par_Exn.release_all}~@{text "results"} combines results |
|
2020 |
that were produced independently (e.g.\ by parallel evaluation). If |
|
2021 |
all results are regular values, that list is returned. Otherwise, |
|
2022 |
the collection of all exceptions is raised, wrapped-up as collective |
|
2023 |
parallel exception. Note that the latter prevents access to |
|
2024 |
individual exceptions by conventional @{verbatim "handle"} of SML. |
|
2025 |
||
2026 |
\item @{ML Par_Exn.release_first} is similar to @{ML |
|
2027 |
Par_Exn.release_all}, but only the first exception that has occurred |
|
2028 |
in the original evaluation process is raised again, the others are |
|
2029 |
ignored. That single exception may get handled by conventional |
|
2030 |
means in SML. |
|
2031 |
||
2032 |
\end{description} |
|
2033 |
*} |
|
2034 |
||
2035 |
||
52420 | 2036 |
subsection {* Parallel skeletons \label{sec:parlist} *} |
2037 |
||
2038 |
text {* |
|
2039 |
Algorithmic skeletons are combinators that operate on lists in |
|
2040 |
parallel, in the manner of well-known @{text map}, @{text exists}, |
|
2041 |
@{text forall} etc. Management of futures (\secref{sec:futures}) |
|
2042 |
and their results as reified exceptions is wrapped up into simple |
|
2043 |
programming interfaces that resemble the sequential versions. |
|
2044 |
||
2045 |
What remains is the application-specific problem to present |
|
2046 |
expressions with suitable \emph{granularity}: each list element |
|
2047 |
corresponds to one evaluation task. If the granularity is too |
|
2048 |
coarse, the available CPUs are not saturated. If it is too |
|
2049 |
fine-grained, CPU cycles are wasted due to the overhead of |
|
2050 |
organizing parallel processing. In the worst case, parallel |
|
2051 |
performance will be less than the sequential counterpart! |
|
2052 |
*} |
|
2053 |
||
2054 |
text %mlref {* |
|
2055 |
\begin{mldecls} |
|
2056 |
@{index_ML Par_List.map: "('a -> 'b) -> 'a list -> 'b list"} \\ |
|
2057 |
@{index_ML Par_List.get_some: "('a -> 'b option) -> 'a list -> 'b option"} \\ |
|
2058 |
\end{mldecls} |
|
2059 |
||
2060 |
\begin{description} |
|
2061 |
||
2062 |
\item @{ML Par_List.map}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} is like @{ML |
|
2063 |
"map"}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"}, but the evaluation of @{text "f x\<^sub>i"} |
|
2064 |
for @{text "i = 1, \<dots>, n"} is performed in parallel. |
|
2065 |
||
2066 |
An exception in any @{text "f x\<^sub>i"} cancels the overall evaluation |
|
2067 |
process. The final result is produced via @{ML |
|
2068 |
Par_Exn.release_first} as explained above, which means the first |
|
2069 |
program exception that happened to occur in the parallel evaluation |
|
2070 |
is propagated, and all other failures are ignored. |
|
2071 |
||
2072 |
\item @{ML Par_List.get_some}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} produces some |
|
2073 |
@{text "f x\<^sub>i"} that is of the form @{text "SOME y\<^sub>i"}, if that |
|
2074 |
exists, otherwise @{text "NONE"}. Thus it is similar to @{ML |
|
2075 |
Library.get_first}, but subject to a non-deterministic parallel |
|
2076 |
choice process. The first successful result cancels the overall |
|
2077 |
evaluation process; other exceptions are propagated as for @{ML |
|
2078 |
Par_List.map}. |
|
2079 |
||
2080 |
This generic parallel choice combinator is the basis for derived |
|
2081 |
forms, such as @{ML Par_List.find_some}, @{ML Par_List.exists}, @{ML |
|
2082 |
Par_List.forall}. |
|
2083 |
||
2084 |
\end{description} |
|
2085 |
*} |
|
2086 |
||
2087 |
text %mlex {* Subsequently, the Ackermann function is evaluated in |
|
2088 |
parallel for some ranges of arguments. *} |
|
2089 |
||
2090 |
ML_val {* |
|
2091 |
fun ackermann 0 n = n + 1 |
|
2092 |
| ackermann m 0 = ackermann (m - 1) 1 |
|
2093 |
| ackermann m n = ackermann (m - 1) (ackermann m (n - 1)); |
|
2094 |
||
2095 |
Par_List.map (ackermann 2) (500 upto 1000); |
|
2096 |
Par_List.map (ackermann 3) (5 upto 10); |
|
2097 |
*} |
|
2098 |
||
2099 |
||
2100 |
subsection {* Lazy evaluation *} |
|
2101 |
||
2102 |
text {* |
|
57347 | 2103 |
Classic lazy evaluation works via two operations: @{text lazy} to wrap an |
2104 |
unevaluated expression, and @{text force} to evaluate it once and store its |
|
2105 |
result persistently. Later invocations of @{text force} retrieve the stored |
|
2106 |
result without another evaluation. Isabelle/ML refines this idea to |
|
2107 |
accommodate the aspects of multi-threading, synchronous program exceptions |
|
2108 |
and asynchronous interrupts. |
|
2109 |
||
2110 |
The first thread that invokes @{text force} on an unfinished lazy value |
|
2111 |
changes its state into a \emph{promise} of the eventual result and starts |
|
2112 |
evaluating it. Any other threads that @{text force} the same lazy value in |
|
2113 |
the meantime need to wait for it to finish, by producing a regular result or |
|
2114 |
program exception. If the evaluation attempt is interrupted, this event is |
|
2115 |
propagated to all waiting threads and the lazy value is reset to its |
|
2116 |
original state. |
|
2117 |
||
2118 |
This means a lazy value is completely evaluated at most once, in a |
|
2119 |
thread-safe manner. There might be multiple interrupted evaluation attempts, |
|
2120 |
and multiple receivers of intermediate interrupt events. Interrupts are |
|
2121 |
\emph{not} made persistent: later evaluation attempts start again from the |
|
2122 |
original expression. |
|
2123 |
*} |
|
2124 |
||
2125 |
text %mlref {* |
|
2126 |
\begin{mldecls} |
|
2127 |
@{index_ML_type "'a lazy"} \\ |
|
2128 |
@{index_ML Lazy.lazy: "(unit -> 'a) -> 'a lazy"} \\ |
|
2129 |
@{index_ML Lazy.value: "'a -> 'a lazy"} \\ |
|
2130 |
@{index_ML Lazy.force: "'a lazy -> 'a"} \\ |
|
2131 |
\end{mldecls} |
|
2132 |
||
2133 |
\begin{description} |
|
2134 |
||
2135 |
\item Type @{ML_type "'a lazy"} represents lazy values over type @{verbatim |
|
2136 |
"'a"}. |
|
2137 |
||
2138 |
\item @{ML Lazy.lazy}~@{text "(fn () => e)"} wraps the unevaluated |
|
2139 |
expression @{text e} as unfinished lazy value. |
|
2140 |
||
2141 |
\item @{ML Lazy.value}~@{text a} wraps the value @{text a} as finished lazy |
|
2142 |
value. When forced, it returns @{text a} without any further evaluation. |
|
2143 |
||
2144 |
The overhead of this pro-forma wrapping of strict values as lazy values is |
|
2145 |
very low. |
|
2146 |
||
2147 |
\item @{ML Lazy.force}~@{text x} produces the result of the lazy value in a |
|
2148 |
thread-safe manner as explained above. Thus it may cause the current thread |
|
2149 |
to wait on a pending evaluation attempt by another thread. |
|
2150 |
||
2151 |
\end{description} |
|
52420 | 2152 |
*} |
2153 |
||
2154 |
||
2155 |
subsection {* Future values \label{sec:futures} *} |
|
2156 |
||
2157 |
text {* |
|
2158 |
%FIXME |
|
2159 |
||
53982 | 2160 |
See also @{file "~~/src/Pure/Concurrent/future.ML"}. |
52420 | 2161 |
*} |
2162 |
||
2163 |
||
57348 | 2164 |
text %mlref {* |
2165 |
\begin{mldecls} |
|
2166 |
@{index_ML_type "'a future"} \\ |
|
2167 |
@{index_ML Future.fork: "(unit -> 'a) -> 'a future"} \\ |
|
2168 |
@{index_ML Future.forks: "Future.params -> (unit -> 'a) list -> 'a future list"} \\ |
|
2169 |
@{index_ML Future.value: "'a -> 'a future"} \\ |
|
2170 |
@{index_ML Future.join: "'a future -> 'a"} \\ |
|
2171 |
@{index_ML Future.map: "('a -> 'b) -> 'a future -> 'b future"} \\ |
|
2172 |
@{index_ML Future.cancel: "'a future -> unit"} \\ |
|
2173 |
@{index_ML Future.cancel_group: "Future.group -> unit"} \\[0.5ex] |
|
2174 |
@{index_ML Future.promise: "(unit -> unit) -> 'a future"} \\ |
|
2175 |
@{index_ML Future.fulfill: "'a future -> 'a -> unit"} \\ |
|
2176 |
\end{mldecls} |
|
2177 |
||
2178 |
\begin{description} |
|
2179 |
||
2180 |
\item Type @{ML_type "'a future"} represents future values over type |
|
2181 |
@{verbatim "'a"}. |
|
2182 |
||
2183 |
\item @{ML Future.fork}~@{text "(fn () => e)"} registers the unevaluated |
|
2184 |
expression @{text e} as unfinished future value, to be evaluated eventually |
|
2185 |
on the parallel worker-thread farm. This is a shorthand for @{ML |
|
2186 |
Future.forks} below, with default parameters and a single expression. |
|
2187 |
||
2188 |
\item @{ML Future.forks}~@{text "params exprs"} is the general interface to |
|
2189 |
fork several futures simultaneously. The @{text params} consist of the |
|
2190 |
following fields: |
|
2191 |
||
2192 |
\begin{itemize} |
|
2193 |
||
2194 |
\item @{text "name : string"} (default @{ML "\"\""}) specifies a common name |
|
2195 |
for the tasks of the forked futures, which serves diagnostic purposes. |
|
2196 |
||
2197 |
\item @{text "group : Future.group option"} (default @{ML NONE}) specifies |
|
2198 |
an optional task group for the forked futures. @{ML NONE} means that a new |
|
2199 |
sub-group of the current worker-thread task context is created. If this is |
|
2200 |
not a worker thread, the group will be a new root in the group hierarchy. |
|
2201 |
||
2202 |
\item @{text "deps : Future.task list"} (default @{ML "[]"}) specifies |
|
2203 |
dependencies on other future tasks, i.e.\ the adjacency relation in the |
|
2204 |
global task queue. Dependencies on already finished future tasks are |
|
2205 |
ignored. |
|
2206 |
||
2207 |
\item @{text "pri : int"} (default @{ML 0}) specifies a priority within the |
|
2208 |
task queue. |
|
2209 |
||
2210 |
Typically there is only little deviation from the default priority @{ML 0}. |
|
2211 |
As a rule of thumb, @{ML "~1"} means ``low priority" and @{ML 1} means |
|
2212 |
``high priority''. |
|
2213 |
||
2214 |
Note that the priority only affects the position in the task queue, not a |
|
2215 |
thread priority. When a worker thread picks up a task for processing, it |
|
2216 |
runs with the normal thread priority to the end (or until canceled). Higher |
|
2217 |
priority tasks that are queued later need to wait until this (or another) |
|
2218 |
worker thread becomes free again. |
|
2219 |
||
2220 |
\item @{text "interrupts : bool"} (default @{ML true}) tells whether the |
|
2221 |
worker thread that processes the corresponding task is initially put into |
|
2222 |
interruptible state. Note that it may change this state later while running, |
|
2223 |
by modifying the thread attributes. |
|
2224 |
||
2225 |
With interrupts disabled, a running future task cannot be canceled. It is |
|
2226 |
the responsibility of the programmer that this special state is retained |
|
2227 |
only briefly. |
|
2228 |
||
2229 |
\end{itemize} |
|
2230 |
||
2231 |
\item @{ML Future.value}~@{text a} wraps the value @{text a} as finished |
|
2232 |
future value, bypassing the worker-thread farm. When joined, it returns |
|
2233 |
@{text a} without any further evaluation. |
|
2234 |
||
2235 |
The overhead of this pro-forma wrapping of strict values as future values is |
|
2236 |
very low. |
|
2237 |
||
2238 |
\item @{ML Future.join}~@{text x} retrieves the value of an already finished |
|
2239 |
future, which may lead to an exception, according to the result of its |
|
2240 |
previous evaluation. |
|
2241 |
||
2242 |
For an unfinished future there are several cases depending on the role of |
|
2243 |
the current thread and the status of the future. A non-worker thread waits |
|
2244 |
passively until the future is eventually evaluated. A worker thread |
|
2245 |
temporarily changes its task context and takes over the responsibility to |
|
2246 |
evaluate the future expression on the spot; this is done in a thread-safe |
|
2247 |
manner: other threads that intend to join the same future need to wait until |
|
2248 |
the ongoing evaluation is finished. |
|
2249 |
||
2250 |
Excessive use of dynamic dependencies of futures by adhoc joining may lead |
|
2251 |
to bad utilization of CPU cores, due to threads waiting on other threads to |
|
2252 |
finish required futures. The future task farm has a limited amount of |
|
2253 |
replacement threads that continue working on other tasks after some timeout. |
|
2254 |
||
2255 |
Whenever possible, static dependencies of futures should be specified |
|
2256 |
explicitly when forked. Thus the evaluation can work from the bottom up, |
|
2257 |
without join conflicts and wait states. |
|
2258 |
||
2259 |
\item @{ML Future.map}~@{text "f x"} is a fast-path implementation of @{ML |
|
2260 |
Future.fork}~@{text "(fn () => f ("}@{ML Future.join}~@{text "x))"}, which |
|
2261 |
avoids the full overhead of the task queue and worker-thread farm as far as |
|
2262 |
possible. The function @{text f} is supposed to be some trivial |
|
2263 |
post-processing or projection of the future result. |
|
2264 |
||
2265 |
\item @{ML Future.cancel}~@{text "x"} cancels the task group of the given |
|
2266 |
future, using @{ML Future.cancel_group} below. |
|
2267 |
||
2268 |
\item @{ML Future.cancel_group}~@{text "g"} cancels all tasks of the given |
|
2269 |
group for all time. Threads that are presently processing a task of the |
|
2270 |
given group are interrupted. Tasks that are queued but not yet processed are |
|
2271 |
dequeued and forced into interrupted state. Since the task group is itself |
|
2272 |
invalidated, any further attempt to fork a future that belongs to it will |
|
2273 |
yield a canceled result as well. |
|
2274 |
||
2275 |
\item @{ML Future.promise}~@{text abort} registers a passive future with the |
|
2276 |
given @{text abort} operation: it is invoked when the future task group is |
|
2277 |
canceled. |
|
2278 |
||
2279 |
\item @{ML Future.fulfill}~@{text "x a"} finishes the passive future @{text |
|
2280 |
x} by the given value @{text a}. If the promise has already been canceled, |
|
2281 |
the attempt to fulfill it causes an exception. |
|
2282 |
||
2283 |
\end{description} |
|
2284 |
*} |
|
2285 |
||
2286 |
||
47180 | 2287 |
end |