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