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