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