| author | wenzelm | 
| Tue, 02 Nov 2010 21:21:07 +0100 | |
| changeset 40302 | 2a33038d858b | 
| parent 40229 | e00a2edd1dc6 | 
| child 40310 | a0698ec82e6e | 
| permissions | -rw-r--r-- | 
| 29755 | 1  | 
theory "ML"  | 
2  | 
imports Base  | 
|
3  | 
begin  | 
|
| 18538 | 4  | 
|
| 39822 | 5  | 
chapter {* Isabelle/ML *}
 | 
| 20489 | 6  | 
|
| 39823 | 7  | 
text {* Isabelle/ML is best understood as a certain culture based on
 | 
8  | 
Standard ML. Thus it is not a new programming language, but a  | 
|
9  | 
certain way to use SML at an advanced level within the Isabelle  | 
|
10  | 
environment. This covers a variety of aspects that are geared  | 
|
11  | 
towards an efficient and robust platform for applications of formal  | 
|
12  | 
logic with fully foundational proof construction --- according to  | 
|
| 40126 | 13  | 
  the well-known \emph{LCF principle}.  There is specific
 | 
14  | 
infrastructure with library modules to address the needs of this  | 
|
15  | 
difficult task. For example, the raw parallel programming model of  | 
|
16  | 
Poly/ML is presented as considerably more abstract concept of  | 
|
17  | 
  \emph{future values}, which is then used to augment the inference
 | 
|
18  | 
kernel, proof interpreter, and theory loader accordingly.  | 
|
| 39823 | 19  | 
|
20  | 
The main aspects of Isabelle/ML are introduced below. These  | 
|
21  | 
first-hand explanations should help to understand how proper  | 
|
22  | 
Isabelle/ML is to be read and written, and to get access to the  | 
|
23  | 
wealth of experience that is expressed in the source text and its  | 
|
24  | 
  history of changes.\footnote{See
 | 
|
25  | 
  \url{http://isabelle.in.tum.de/repos/isabelle} for the full
 | 
|
26  | 
Mercurial history. There are symbolic tags to refer to official  | 
|
27  | 
  Isabelle releases, as opposed to arbitrary \emph{tip} versions that
 | 
|
28  | 
merely reflect snapshots that are never really up-to-date.} *}  | 
|
29  | 
||
30  | 
||
| 39878 | 31  | 
section {* Style and orthography *}
 | 
32  | 
||
| 39879 | 33  | 
text {* The sources of Isabelle/Isar are optimized for
 | 
34  | 
  \emph{readability} and \emph{maintainability}.  The main purpose is
 | 
|
35  | 
to tell an informed reader what is really going on and how things  | 
|
36  | 
really work. This is a non-trivial aim, but it is supported by a  | 
|
37  | 
certain style of writing Isabelle/ML that has emerged from long  | 
|
38  | 
  years of system development.\footnote{See also the interesting style
 | 
|
39  | 
guide for OCaml  | 
|
| 39878 | 40  | 
  \url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}
 | 
41  | 
which shares many of our means and ends.}  | 
|
42  | 
||
43  | 
  The main principle behind any coding style is \emph{consistency}.
 | 
|
| 39879 | 44  | 
For a single author of a small program this merely means ``choose  | 
| 39878 | 45  | 
your style and stick to it''. A complex project like Isabelle, with  | 
| 39879 | 46  | 
long years of development and different contributors, requires more  | 
47  | 
standardization. A coding style that is changed every few years or  | 
|
48  | 
with every new contributor is no style at all, because consistency  | 
|
49  | 
is quickly lost. Global consistency is hard to achieve, though.  | 
|
| 40126 | 50  | 
Nonetheless, one should always strive at least for local consistency  | 
51  | 
of modules and sub-systems, without deviating from some general  | 
|
52  | 
principles how to write Isabelle/ML.  | 
|
| 39878 | 53  | 
|
| 40126 | 54  | 
  In a sense, good coding style is like an \emph{orthography} for the
 | 
55  | 
sources: it helps to read quickly over the text and see through the  | 
|
56  | 
main points, without getting distracted by accidental presentation  | 
|
57  | 
of free-style code.  | 
|
| 39878 | 58  | 
*}  | 
59  | 
||
60  | 
||
61  | 
subsection {* Header and sectioning *}
 | 
|
62  | 
||
| 39879 | 63  | 
text {* Isabelle source files have a certain standardized header
 | 
64  | 
format (with precise spacing) that follows ancient traditions  | 
|
65  | 
reaching back to the earliest versions of the system by Larry  | 
|
| 40126 | 66  | 
  Paulson.  See @{"file" "~~/src/Pure/thm.ML"}, for example.
 | 
| 39878 | 67  | 
|
68  | 
  The header includes at least @{verbatim Title} and @{verbatim
 | 
|
69  | 
Author} entries, followed by a prose description of the purpose of  | 
|
70  | 
the module. The latter can range from a single line to several  | 
|
71  | 
paragraphs of explanations.  | 
|
72  | 
||
73  | 
The rest of the file is divided into sections, subsections,  | 
|
| 40126 | 74  | 
subsubsections, paragraphs etc.\ using a simple layout via ML  | 
75  | 
comments as follows.  | 
|
| 39878 | 76  | 
|
77  | 
\begin{verbatim}
 | 
|
78  | 
(*** section ***)  | 
|
79  | 
||
80  | 
(** subsection **)  | 
|
81  | 
||
82  | 
(* subsubsection *)  | 
|
83  | 
||
84  | 
(*short paragraph*)  | 
|
85  | 
||
86  | 
(*  | 
|
| 40126 | 87  | 
long paragraph,  | 
88  | 
with more text  | 
|
| 39878 | 89  | 
*)  | 
90  | 
\end{verbatim}
 | 
|
91  | 
||
92  | 
  As in regular typography, there is some extra space \emph{before}
 | 
|
93  | 
section headings that are adjacent to plain text (not other headings  | 
|
94  | 
as in the example above).  | 
|
95  | 
||
96  | 
\medskip The precise wording of the prose text given in these  | 
|
| 40126 | 97  | 
headings is chosen carefully to introduce the main theme of the  | 
| 39879 | 98  | 
subsequent formal ML text.  | 
99  | 
*}  | 
|
100  | 
||
101  | 
||
| 39880 | 102  | 
subsection {* Naming conventions *}
 | 
| 39879 | 103  | 
|
104  | 
text {* Since ML is the primary medium to express the meaning of the
 | 
|
105  | 
source text, naming of ML entities requires special care.  | 
|
106  | 
||
| 39881 | 107  | 
  \paragraph{Notation.}  A name consists of 1--3 \emph{words} (rarely
 | 
108  | 
4, but not more) that are separated by underscore. There are three  | 
|
| 40126 | 109  | 
variants concerning upper or lower case letters, which are used for  | 
| 39881 | 110  | 
certain ML categories as follows:  | 
| 39880 | 111  | 
|
112  | 
\medskip  | 
|
113  | 
  \begin{tabular}{lll}
 | 
|
114  | 
variant & example & ML categories \\\hline  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
115  | 
  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
 | 
116  | 
  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
 | 
117  | 
  upper-case & @{ML_text FOO_BAR} & special values, exception constructors, signatures \\
 | 
| 39880 | 118  | 
  \end{tabular}
 | 
119  | 
\medskip  | 
|
120  | 
||
121  | 
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
 | 
122  | 
  e.g.\ old-style @{ML_text FooBar} instead of @{ML_text Foo_Bar}.
 | 
| 40126 | 123  | 
  Genuine mixed-case names are \emph{not} used, bacause clear division
 | 
124  | 
  of words is essential for readability.\footnote{Camel-case was
 | 
|
125  | 
invented to workaround the lack of underscore in some early  | 
|
126  | 
non-ASCII character sets. Later it became habitual in some language  | 
|
127  | 
communities that are now strong in numbers.}  | 
|
| 39880 | 128  | 
|
| 39881 | 129  | 
A single (capital) character does not count as ``word'' in this  | 
| 40126 | 130  | 
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
 | 
131  | 
  this: @{ML_text foo_barT}.
 | 
| 39881 | 132  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
133  | 
  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
 | 
134  | 
  foo'}, @{ML_text foo''}, or @{ML_text foo'''}, but not @{ML_text
 | 
| 39881 | 135  | 
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
 | 
136  | 
  e.g.\ @{ML_text foo0}, @{ML_text foo1}, @{ML_text foo42}.
 | 
| 39880 | 137  | 
|
138  | 
  \paragraph{Scopes.}  Apart from very basic library modules, ML
 | 
|
139  | 
structures are not ``opened'', but names are referenced with  | 
|
| 39881 | 140  | 
  explicit qualification, as in @{ML Syntax.string_of_term} for
 | 
| 39880 | 141  | 
example. When devising names for structures and their components it  | 
142  | 
is important aim at eye-catching compositions of both parts, because  | 
|
| 40126 | 143  | 
this is how they are seen in the sources and documentation. For the  | 
144  | 
same reasons, aliases of well-known library functions should be  | 
|
145  | 
avoided.  | 
|
| 39880 | 146  | 
|
| 40126 | 147  | 
Local names of function abstraction or case/let bindings are  | 
| 39880 | 148  | 
typically shorter, sometimes using only rudiments of ``words'',  | 
149  | 
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
 | 
150  | 
  called @{ML_text helper}, @{ML_text aux}, or @{ML_text f} is
 | 
| 39880 | 151  | 
considered bad style.  | 
152  | 
||
153  | 
Example:  | 
|
154  | 
||
155  | 
  \begin{verbatim}
 | 
|
156  | 
(* RIGHT *)  | 
|
157  | 
||
158  | 
fun print_foo ctxt foo =  | 
|
159  | 
let  | 
|
| 39881 | 160  | 
fun print t = ... Syntax.string_of_term ctxt t ...  | 
161  | 
in ... end;  | 
|
162  | 
||
163  | 
||
164  | 
(* RIGHT *)  | 
|
165  | 
||
166  | 
fun print_foo ctxt foo =  | 
|
167  | 
let  | 
|
| 39880 | 168  | 
val string_of_term = Syntax.string_of_term ctxt;  | 
169  | 
fun print t = ... string_of_term t ...  | 
|
170  | 
in ... end;  | 
|
171  | 
||
172  | 
||
173  | 
(* WRONG *)  | 
|
174  | 
||
175  | 
val string_of_term = Syntax.string_of_term;  | 
|
176  | 
||
177  | 
fun print_foo ctxt foo =  | 
|
178  | 
let  | 
|
179  | 
fun aux t = ... string_of_term ctxt t ...  | 
|
180  | 
in ... end;  | 
|
181  | 
||
182  | 
  \end{verbatim}
 | 
|
183  | 
||
184  | 
||
| 40126 | 185  | 
  \paragraph{Specific conventions.} Here are some specific name forms
 | 
186  | 
that occur frequently in the sources.  | 
|
| 39881 | 187  | 
|
188  | 
  \begin{itemize}
 | 
|
189  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
190  | 
  \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
 | 
191  | 
  called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
192  | 
  @{ML_text foo2bar}, @{ML_text bar_from_foo}, @{ML_text
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
193  | 
  bar_for_foo}, or @{ML_text bar4foo}).
 | 
| 39881 | 194  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
195  | 
  \item The name component @{ML_text legacy} means that the operation
 | 
| 39881 | 196  | 
is about to be discontinued soon.  | 
197  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
198  | 
  \item The name component @{ML_text old} means that this is historic
 | 
| 39881 | 199  | 
material that might disappear at some later stage.  | 
200  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
201  | 
  \item The name component @{ML_text global} means that this works
 | 
| 39881 | 202  | 
with the background theory instead of the regular local context  | 
203  | 
  (\secref{sec:context}), sometimes for historical reasons, sometimes
 | 
|
204  | 
due a genuine lack of locality of the concept involved, sometimes as  | 
|
205  | 
a fall-back for the lack of a proper context in the application  | 
|
206  | 
code. Whenever there is a non-global variant available, the  | 
|
207  | 
application should be migrated to use it with a proper local  | 
|
208  | 
context.  | 
|
209  | 
||
210  | 
\item Variables of the main context types of the Isabelle/Isar  | 
|
211  | 
  framework (\secref{sec:context} and \chref{ch:local-theory}) have
 | 
|
| 40126 | 212  | 
firm naming conventions as follows:  | 
| 39881 | 213  | 
|
214  | 
  \begin{itemize}
 | 
|
215  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
216  | 
  \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
 | 
217  | 
  (never @{ML_text thry})
 | 
| 39881 | 218  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
219  | 
  \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
 | 
220  | 
  context} (never @{ML_text ctx})
 | 
| 39881 | 221  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
222  | 
  \item generic contexts are called @{ML_text context}, rarely
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
223  | 
  @{ML_text ctxt}
 | 
| 39881 | 224  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
225  | 
  \item local theories are called @{ML_text lthy}, except for local
 | 
| 40126 | 226  | 
theories that are treated as proof context (which is a semantic  | 
227  | 
super-type)  | 
|
| 39881 | 228  | 
|
229  | 
  \end{itemize}
 | 
|
230  | 
||
231  | 
Variations with primed or decimal numbers are always possible, as  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
232  | 
  well as sematic prefixes like @{ML_text foo_thy} or @{ML_text
 | 
| 39881 | 233  | 
bar_ctxt}, but the base conventions above need to be preserved.  | 
| 40126 | 234  | 
This allows to visualize the their data flow via plain regular  | 
235  | 
expressions in the editor.  | 
|
| 39881 | 236  | 
|
| 40126 | 237  | 
  \item The main logical entities (\secref{ch:logic}) have established
 | 
238  | 
naming convention as follows:  | 
|
| 39881 | 239  | 
|
240  | 
  \begin{itemize}
 | 
|
241  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
242  | 
  \item sorts are called @{ML_text S}
 | 
| 39881 | 243  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
244  | 
  \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
 | 
245  | 
  ty} (never @{ML_text t})
 | 
| 39881 | 246  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
247  | 
  \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
 | 
248  | 
  tm} (never @{ML_text trm})
 | 
| 39881 | 249  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
250  | 
  \item certified types are called @{ML_text cT}, rarely @{ML_text
 | 
| 39881 | 251  | 
T}, with variants as for types  | 
252  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
253  | 
  \item certified terms are called @{ML_text ct}, rarely @{ML_text
 | 
| 39881 | 254  | 
t}, with variants as for terms  | 
255  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
256  | 
  \item theorems are called @{ML_text th}, or @{ML_text thm}
 | 
| 39881 | 257  | 
|
258  | 
  \end{itemize}
 | 
|
259  | 
||
260  | 
Proper semantic names override these conventions completely. For  | 
|
261  | 
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
 | 
262  | 
  @{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
 | 
263  | 
  to be a variable can be called @{ML_text v} or @{ML_text x}.
 | 
| 39881 | 264  | 
|
265  | 
  \end{itemize}
 | 
|
| 39878 | 266  | 
*}  | 
267  | 
||
268  | 
||
269  | 
subsection {* General source layout *}
 | 
|
270  | 
||
271  | 
text {* The general Isabelle/ML source layout imitates regular
 | 
|
272  | 
type-setting to some extent, augmented by the requirements for  | 
|
273  | 
deeply nested expressions that are commonplace in functional  | 
|
274  | 
programming.  | 
|
275  | 
||
276  | 
  \paragraph{Line length} is 80 characters according to ancient
 | 
|
| 40126 | 277  | 
standards, but we allow as much as 100 characters (not  | 
278  | 
  more).\footnote{Readability requires to keep the beginning of a line
 | 
|
| 39881 | 279  | 
in view while watching its end. Modern wide-screen displays do not  | 
| 40126 | 280  | 
change the way how the human brain works. Sources also need to be  | 
281  | 
printable on plain paper with reasonable font-size.} The extra 20  | 
|
| 39880 | 282  | 
characters acknowledge the space requirements due to qualified  | 
283  | 
library references in Isabelle/ML.  | 
|
| 39878 | 284  | 
|
285  | 
  \paragraph{White-space} is used to emphasize the structure of
 | 
|
286  | 
expressions, following mostly standard conventions for mathematical  | 
|
287  | 
  typesetting, as can be seen in plain {\TeX} or {\LaTeX}.  This
 | 
|
| 39879 | 288  | 
defines positioning of spaces for parentheses, punctuation, and  | 
289  | 
infixes as illustrated here:  | 
|
| 39878 | 290  | 
|
291  | 
  \begin{verbatim}
 | 
|
292  | 
val x = y + z * (a + b);  | 
|
293  | 
val pair = (a, b);  | 
|
294  | 
  val record = {foo = 1, bar = 2};
 | 
|
295  | 
  \end{verbatim}
 | 
|
296  | 
||
| 39879 | 297  | 
  Lines are normally broken \emph{after} an infix operator or
 | 
298  | 
punctuation character. For example:  | 
|
| 39878 | 299  | 
|
300  | 
  \begin{verbatim}
 | 
|
301  | 
val x =  | 
|
302  | 
a +  | 
|
303  | 
b +  | 
|
304  | 
c;  | 
|
305  | 
||
306  | 
val tuple =  | 
|
307  | 
(a,  | 
|
308  | 
b,  | 
|
309  | 
c);  | 
|
310  | 
  \end{verbatim}
 | 
|
311  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
312  | 
  Some special infixes (e.g.\ @{ML_text "|>"}) work better at the
 | 
| 39879 | 313  | 
start of the line, but punctuation is always at the end.  | 
| 39878 | 314  | 
|
315  | 
  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
 | 
316  | 
  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
 | 
317  | 
  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
 | 
318  | 
  Note that the space between @{ML_text g} and the pair @{ML_text
 | 
| 39879 | 319  | 
"(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
 | 
320  | 
  \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
 | 
321  | 
  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
 | 
322  | 
  @{ML_text "(a, b)"}.
 | 
| 39878 | 323  | 
|
324  | 
  \paragraph{Indentation} uses plain spaces, never hard
 | 
|
325  | 
  tabulators.\footnote{Tabulators were invented to move the carriage
 | 
|
326  | 
of a type-writer to certain predefined positions. In software they  | 
|
327  | 
could be used as a primitive run-length compression of consecutive  | 
|
328  | 
spaces, but the precise result would depend on non-standardized  | 
|
329  | 
editor configuration.}  | 
|
330  | 
||
| 39879 | 331  | 
Each level of nesting is indented by 2 spaces, sometimes 1, very  | 
| 40126 | 332  | 
rarely 4, never 8 or any other odd number.  | 
| 39878 | 333  | 
|
| 39879 | 334  | 
Indentation follows a simple logical format that only depends on the  | 
335  | 
nesting depth, not the accidental length of the text that initiates  | 
|
336  | 
a level of nesting. Example:  | 
|
| 39878 | 337  | 
|
338  | 
  \begin{verbatim}
 | 
|
| 39880 | 339  | 
(* RIGHT *)  | 
340  | 
||
| 39878 | 341  | 
if b then  | 
| 39879 | 342  | 
expr1_part1  | 
343  | 
expr1_part2  | 
|
| 39878 | 344  | 
else  | 
| 39879 | 345  | 
expr2_part1  | 
346  | 
expr2_part2  | 
|
| 39878 | 347  | 
|
| 39880 | 348  | 
|
349  | 
(* WRONG *)  | 
|
350  | 
||
| 39879 | 351  | 
if b then expr1_part1  | 
352  | 
expr1_part2  | 
|
353  | 
else expr2_part1  | 
|
354  | 
expr2_part2  | 
|
| 39878 | 355  | 
  \end{verbatim}
 | 
356  | 
||
357  | 
The second form has many problems: it assumes a fixed-width font  | 
|
| 39879 | 358  | 
when viewing the sources, it uses more space on the line and thus  | 
359  | 
makes it hard to observe its strict length limit (working against  | 
|
| 39878 | 360  | 
  \emph{readability}), it requires extra editing to adapt the layout
 | 
| 39879 | 361  | 
to changes of the initial text (working against  | 
| 39878 | 362  | 
  \emph{maintainability}) etc.
 | 
363  | 
||
| 39879 | 364  | 
\medskip For similar reasons, any kind of two-dimensional or tabular  | 
| 40126 | 365  | 
layouts, ASCII-art with lines or boxes of asterisks etc.\ should be  | 
| 39879 | 366  | 
avoided.  | 
| 39881 | 367  | 
|
| 40126 | 368  | 
  \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
 | 
369  | 
  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
 | 
370  | 
  @{ML_text let} (and combinations) require special attention.  The
 | 
| 40126 | 371  | 
syntax of Standard ML is quite ambitious and admits a lot of  | 
372  | 
variance that can distort the meaning of the text.  | 
|
| 39881 | 373  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
374  | 
  Clauses of @{ML_text fun}, @{ML_text fn}, @{ML_text handle},
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
375  | 
  @{ML_text case} get extra indentation to indicate the nesting
 | 
| 40126 | 376  | 
clearly. Example:  | 
| 39881 | 377  | 
|
378  | 
  \begin{verbatim}
 | 
|
379  | 
(* RIGHT *)  | 
|
380  | 
||
381  | 
fun foo p1 =  | 
|
382  | 
expr1  | 
|
383  | 
| foo p2 =  | 
|
384  | 
expr2  | 
|
385  | 
||
386  | 
||
387  | 
(* WRONG *)  | 
|
388  | 
||
389  | 
fun foo p1 =  | 
|
390  | 
expr1  | 
|
391  | 
| foo p2 =  | 
|
392  | 
expr2  | 
|
393  | 
  \end{verbatim}
 | 
|
394  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
395  | 
  Body expressions consisting of @{ML_text case} or @{ML_text let}
 | 
| 39881 | 396  | 
require care to maintain compositionality, to prevent loss of  | 
| 40126 | 397  | 
logical indentation where it is especially important to see the  | 
398  | 
structure of the text. Example:  | 
|
| 39881 | 399  | 
|
400  | 
  \begin{verbatim}
 | 
|
401  | 
(* RIGHT *)  | 
|
402  | 
||
403  | 
fun foo p1 =  | 
|
404  | 
(case e of  | 
|
405  | 
q1 => ...  | 
|
406  | 
| q2 => ...)  | 
|
407  | 
| foo p2 =  | 
|
408  | 
let  | 
|
409  | 
...  | 
|
410  | 
in  | 
|
411  | 
...  | 
|
412  | 
end  | 
|
413  | 
||
414  | 
||
415  | 
(* WRONG *)  | 
|
416  | 
||
417  | 
fun foo p1 = case e of  | 
|
418  | 
q1 => ...  | 
|
419  | 
| q2 => ...  | 
|
420  | 
| foo p2 =  | 
|
421  | 
let  | 
|
422  | 
...  | 
|
423  | 
in  | 
|
424  | 
...  | 
|
425  | 
end  | 
|
426  | 
  \end{verbatim}
 | 
|
427  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
428  | 
  Extra parentheses around @{ML_text case} expressions are optional,
 | 
| 40126 | 429  | 
but help to analyse the nesting based on character matching in the  | 
430  | 
editor.  | 
|
| 39881 | 431  | 
|
432  | 
\medskip There are two main exceptions to the overall principle of  | 
|
433  | 
compositionality in the layout of complex expressions.  | 
|
434  | 
||
435  | 
  \begin{enumerate}
 | 
|
436  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
437  | 
  \item @{ML_text "if"} expressions are iterated as if there would be
 | 
| 39881 | 438  | 
a multi-branch conditional in SML, e.g.  | 
439  | 
||
440  | 
  \begin{verbatim}
 | 
|
441  | 
(* RIGHT *)  | 
|
442  | 
||
443  | 
if b1 then e1  | 
|
444  | 
else if b2 then e2  | 
|
445  | 
else e3  | 
|
446  | 
  \end{verbatim}
 | 
|
447  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
448  | 
  \item @{ML_text fn} abstractions are often layed-out as if they
 | 
| 39881 | 449  | 
would lack any structure by themselves. This traditional form is  | 
450  | 
motivated by the possibility to shift function arguments back and  | 
|
| 40126 | 451  | 
forth wrt.\ additional combinators. Example:  | 
| 39881 | 452  | 
|
453  | 
  \begin{verbatim}
 | 
|
454  | 
(* RIGHT *)  | 
|
455  | 
||
456  | 
fun foo x y = fold (fn z =>  | 
|
457  | 
expr)  | 
|
458  | 
  \end{verbatim}
 | 
|
459  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
460  | 
  Here the visual appearance is that of three arguments @{ML_text x},
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
461  | 
  @{ML_text y}, @{ML_text z}.
 | 
| 39881 | 462  | 
|
463  | 
  \end{enumerate}
 | 
|
464  | 
||
465  | 
Such weakly structured layout should be use with great care. Here  | 
|
| 40153 | 466  | 
  are some counter-examples involving @{ML_text let} expressions:
 | 
| 39881 | 467  | 
|
468  | 
  \begin{verbatim}
 | 
|
469  | 
(* WRONG *)  | 
|
470  | 
||
471  | 
fun foo x = let  | 
|
472  | 
val y = ...  | 
|
473  | 
in ... end  | 
|
474  | 
||
| 40153 | 475  | 
fun foo x = let  | 
476  | 
val y = ...  | 
|
477  | 
in ... end  | 
|
478  | 
||
| 39881 | 479  | 
fun foo x =  | 
480  | 
let  | 
|
481  | 
val y = ...  | 
|
482  | 
in ... end  | 
|
483  | 
  \end{verbatim}
 | 
|
484  | 
||
485  | 
\medskip In general the source layout is meant to emphasize the  | 
|
486  | 
structure of complex language expressions, not to pretend that SML  | 
|
487  | 
had a completely different syntax (say that of Haskell or Java).  | 
|
| 39878 | 488  | 
*}  | 
489  | 
||
490  | 
||
| 39823 | 491  | 
section {* SML embedded into Isabelle/Isar *}
 | 
492  | 
||
| 39824 | 493  | 
text {* ML and Isar are intertwined via an open-ended bootstrap
 | 
494  | 
process that provides more and more programming facilities and  | 
|
495  | 
logical content in an alternating manner. Bootstrapping starts from  | 
|
496  | 
the raw environment of existing implementations of Standard ML  | 
|
497  | 
(mainly Poly/ML, but also SML/NJ).  | 
|
| 39823 | 498  | 
|
| 39824 | 499  | 
Isabelle/Pure marks the point where the original ML toplevel is  | 
| 40126 | 500  | 
superseded by the Isar toplevel that maintains a uniform context for  | 
501  | 
  arbitrary ML values (see also \secref{sec:context}).  This formal
 | 
|
502  | 
environment holds ML compiler bindings, logical entities, and many  | 
|
503  | 
other things. Raw SML is never encountered again after the initial  | 
|
504  | 
bootstrap of Isabelle/Pure.  | 
|
| 39823 | 505  | 
|
| 40126 | 506  | 
Object-logics like Isabelle/HOL are built within the  | 
507  | 
Isabelle/ML/Isar environment by introducing suitable theories with  | 
|
508  | 
associated ML modules, either inlined or as separate files. Thus  | 
|
509  | 
Isabelle/HOL is defined as a regular user-space application within  | 
|
510  | 
the Isabelle framework. Further add-on tools can be implemented in  | 
|
511  | 
ML within the Isar context in the same manner: ML is part of the  | 
|
512  | 
standard repertoire of Isabelle, and there is no distinction between  | 
|
513  | 
``user'' and ``developer'' in this respect.  | 
|
| 39823 | 514  | 
*}  | 
515  | 
||
| 39824 | 516  | 
|
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
517  | 
subsection {* Isar ML commands *}
 | 
| 39823 | 518  | 
|
| 40126 | 519  | 
text {* The primary Isar source language provides facilities to ``open
 | 
520  | 
a window'' to the underlying ML compiler. Especially see the Isar  | 
|
521  | 
  commands @{command_ref "use"} and @{command_ref "ML"}: both work the
 | 
|
| 39824 | 522  | 
same way, only the source text is provided via a file vs.\ inlined,  | 
523  | 
respectively. Apart from embedding ML into the main theory  | 
|
524  | 
definition like that, there are many more commands that refer to ML  | 
|
525  | 
  source, such as @{command_ref setup} or @{command_ref declaration}.
 | 
|
| 40126 | 526  | 
Even more fine-grained embedding of ML into Isar is encountered in  | 
527  | 
  the proof method @{method_ref tactic}, which refines the pending
 | 
|
528  | 
  goal state via a given expression of type @{ML_type tactic}.
 | 
|
| 39824 | 529  | 
*}  | 
| 39823 | 530  | 
|
| 39824 | 531  | 
text %mlex {* The following artificial example demonstrates some ML
 | 
532  | 
toplevel declarations within the implicit Isar theory context. This  | 
|
533  | 
is regular functional programming without referring to logical  | 
|
534  | 
entities yet.  | 
|
| 39823 | 535  | 
*}  | 
536  | 
||
537  | 
ML {*
 | 
|
538  | 
fun factorial 0 = 1  | 
|
539  | 
| factorial n = n * factorial (n - 1)  | 
|
540  | 
*}  | 
|
541  | 
||
| 40126 | 542  | 
text {* Here the ML environment is already managed by Isabelle, i.e.\
 | 
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
543  | 
  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
 | 
544  | 
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
 | 
545  | 
current one in the import hierarchy.  | 
| 39823 | 546  | 
|
547  | 
Removing the above ML declaration from the source text will remove  | 
|
548  | 
any trace of this definition as expected. The Isabelle/ML toplevel  | 
|
549  | 
  environment is managed in a \emph{stateless} way: unlike the raw ML
 | 
|
| 40126 | 550  | 
toplevel there are no global side-effects involved  | 
551  | 
  here.\footnote{Such a stateless compilation environment is also a
 | 
|
552  | 
prerequisite for robust parallel compilation within independent  | 
|
553  | 
nodes of the implicit theory development graph.}  | 
|
| 39823 | 554  | 
|
| 40126 | 555  | 
\medskip The next example shows how to embed ML into Isar proofs, using  | 
556  | 
 @{command_ref "ML_prf"} instead of Instead of @{command_ref "ML"}.
 | 
|
557  | 
As illustrated below, the effect on the ML environment is local to  | 
|
558  | 
the whole proof body, ignoring the block structure.  | 
|
559  | 
*}  | 
|
| 39823 | 560  | 
|
561  | 
example_proof  | 
|
| 39851 | 562  | 
  ML_prf %"ML" {* val a = 1 *}
 | 
| 40126 | 563  | 
  {
 | 
| 39851 | 564  | 
    ML_prf %"ML" {* val b = a + 1 *}
 | 
| 39824 | 565  | 
  } -- {* Isar block structure ignored by ML environment *}
 | 
| 39851 | 566  | 
  ML_prf %"ML" {* val c = b + 1 *}
 | 
| 39823 | 567  | 
qed  | 
568  | 
||
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
569  | 
text {* By side-stepping the normal scoping rules for Isar proof
 | 
| 40126 | 570  | 
blocks, embedded ML code can refer to the different contexts and  | 
571  | 
manipulate corresponding entities, e.g.\ export a fact from a block  | 
|
572  | 
context.  | 
|
| 39823 | 573  | 
|
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
574  | 
\medskip Two further ML commands are useful in certain situations:  | 
| 40126 | 575  | 
  @{command_ref ML_val} and @{command_ref ML_command} are
 | 
| 39824 | 576  | 
  \emph{diagnostic} in the sense that there is no effect on the
 | 
577  | 
underlying environment, and can thus used anywhere (even outside a  | 
|
578  | 
theory). The examples below produce long strings of digits by  | 
|
579  | 
  invoking @{ML factorial}: @{command ML_val} already takes care of
 | 
|
580  | 
  printing the ML toplevel result, but @{command ML_command} is silent
 | 
|
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
581  | 
so we produce an explicit output message. *}  | 
| 39823 | 582  | 
|
583  | 
ML_val {* factorial 100 *}
 | 
|
584  | 
ML_command {* writeln (string_of_int (factorial 100)) *}
 | 
|
585  | 
||
586  | 
example_proof  | 
|
| 39824 | 587  | 
  ML_val {* factorial 100 *}  (* FIXME check/fix indentation *)
 | 
| 39823 | 588  | 
  ML_command {* writeln (string_of_int (factorial 100)) *}
 | 
589  | 
qed  | 
|
590  | 
||
591  | 
||
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
592  | 
subsection {* Compile-time context *}
 | 
| 39823 | 593  | 
|
| 
39825
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
594  | 
text {* Whenever the ML compiler is invoked within Isabelle/Isar, the
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
595  | 
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
 | 
596  | 
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
 | 
597  | 
or writing the (local) theory under construction. Note that such  | 
| 40126 | 598  | 
direct access to the compile-time context is rare. In practice it  | 
599  | 
is typically done via some derived ML functions instead.  | 
|
| 
39825
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
600  | 
*}  | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
601  | 
|
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
602  | 
text %mlref {*
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
603  | 
  \begin{mldecls}
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
604  | 
  @{index_ML ML_Context.the_generic_context: "unit -> Context.generic"} \\
 | 
| 40126 | 605  | 
  @{index_ML "Context.>>": "(Context.generic -> Context.generic) -> unit"} \\
 | 
| 39850 | 606  | 
  @{index_ML bind_thms: "string * thm list -> unit"} \\
 | 
607  | 
  @{index_ML 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
 | 
608  | 
  \end{mldecls}
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
609  | 
|
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
610  | 
  \begin{description}
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
611  | 
|
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
612  | 
  \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
 | 
613  | 
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
 | 
614  | 
  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
 | 
615  | 
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
 | 
616  | 
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
 | 
617  | 
|
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
618  | 
  \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
 | 
619  | 
  @{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
 | 
620  | 
|
| 39850 | 621  | 
  \item @{ML bind_thms}~@{text "(name, thms)"} stores a list of
 | 
622  | 
theorems produced in ML both in the (global) theory context and the  | 
|
623  | 
ML toplevel, associating it with the provided name. Theorems are  | 
|
624  | 
put into a global ``standard'' format before being stored.  | 
|
625  | 
||
626  | 
  \item @{ML bind_thm} is similar to @{ML bind_thms} but refers to a
 | 
|
| 40126 | 627  | 
singleton fact.  | 
| 39850 | 628  | 
|
| 
39825
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
629  | 
  \end{description}
 | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
630  | 
|
| 40126 | 631  | 
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
 | 
632  | 
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
 | 
633  | 
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
 | 
634  | 
  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
 | 
635  | 
proof context at run-time, by explicit functional abstraction.  | 
| 
 
f9066b94bf07
eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
 
wenzelm 
parents: 
39824 
diff
changeset
 | 
636  | 
*}  | 
| 39823 | 637  | 
|
638  | 
||
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
639  | 
subsection {* Antiquotations \label{sec:ML-antiq} *}
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
640  | 
|
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
641  | 
text {* A very important consequence of embedding SML into Isar is the
 | 
| 40126 | 642  | 
  concept of \emph{ML antiquotation}.  The standard token language of
 | 
643  | 
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
 | 
644  | 
|
| 39829 | 645  | 
  \indexouternonterm{antiquote}
 | 
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
646  | 
  \begin{rail}
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
647  | 
antiquote: atsign lbrace nameref args rbrace | lbracesym | rbracesym  | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
648  | 
;  | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
649  | 
  \end{rail}
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
650  | 
|
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
651  | 
  Here @{syntax nameref} and @{syntax args} are regular outer syntax
 | 
| 40126 | 652  | 
  categories \cite{isabelle-isar-ref}.  Attributes and proof methods
 | 
653  | 
use similar syntax.  | 
|
| 39823 | 654  | 
|
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
655  | 
  \medskip A regular antiquotation @{text "@{name args}"} processes
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
656  | 
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
 | 
657  | 
produces corresponding ML source text, either as literal  | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
658  | 
  \emph{inline} text (e.g. @{text "@{term t}"}) or abstract
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
659  | 
  \emph{value} (e.g. @{text "@{thm th}"}).  This pre-compilation
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
660  | 
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
 | 
661  | 
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
 | 
662  | 
small portions of the code.  | 
| 39823 | 663  | 
|
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
664  | 
  Special antiquotations like @{text "@{let \<dots>}"} or @{text "@{note
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
665  | 
\<dots>}"} augment the compilation context without generating code. The  | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
666  | 
  non-ASCII braces @{text "\<lbrace>"} and @{text "\<rbrace>"} allow to delimit the
 | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
667  | 
effect by introducing local blocks within the pre-compilation  | 
| 
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
668  | 
environment.  | 
| 39829 | 669  | 
|
| 40126 | 670  | 
  \medskip See also \cite{Wenzel-Chaieb:2007b} for a broader
 | 
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
671  | 
perspective on Isabelle/ML antiquotations. *}  | 
| 39829 | 672  | 
|
| 39830 | 673  | 
text %mlantiq {*
 | 
| 39829 | 674  | 
  \begin{matharray}{rcl}
 | 
675  | 
  @{ML_antiquotation_def "let"} & : & @{text ML_antiquotation} \\
 | 
|
676  | 
  @{ML_antiquotation_def "note"} & : & @{text ML_antiquotation} \\
 | 
|
677  | 
  \end{matharray}
 | 
|
678  | 
||
679  | 
  \begin{rail}
 | 
|
680  | 
'let' ((term + 'and') '=' term + 'and')  | 
|
681  | 
;  | 
|
682  | 
||
683  | 
'note' (thmdef? thmrefs + 'and')  | 
|
684  | 
;  | 
|
685  | 
  \end{rail}
 | 
|
686  | 
||
687  | 
  \begin{description}
 | 
|
688  | 
||
689  | 
  \item @{text "@{let p = t}"} binds schematic variables in the
 | 
|
690  | 
  pattern @{text "p"} by higher-order matching against the term @{text
 | 
|
691  | 
  "t"}.  This is analogous to the regular @{command_ref let} command
 | 
|
692  | 
in the Isar proof language. The pre-compilation environment is  | 
|
693  | 
augmented by auxiliary term bindings, without emitting ML source.  | 
|
694  | 
||
695  | 
  \item @{text "@{note a = b\<^sub>1 \<dots> b\<^sub>n}"} recalls existing facts @{text
 | 
|
696  | 
  "b\<^sub>1, \<dots>, b\<^sub>n"}, binding the result as @{text a}.  This is analogous to
 | 
|
697  | 
  the regular @{command_ref note} command in the Isar proof language.
 | 
|
698  | 
The pre-compilation environment is augmented by auxiliary fact  | 
|
699  | 
bindings, without emitting ML source.  | 
|
700  | 
||
701  | 
  \end{description}
 | 
|
702  | 
*}  | 
|
703  | 
||
| 40126 | 704  | 
text %mlex {* The following artificial example gives some impression
 | 
705  | 
about the antiquotation elements introduced so far, together with  | 
|
706  | 
  the important @{text "@{thm}"} antiquotation defined later.
 | 
|
| 39829 | 707  | 
*}  | 
708  | 
||
709  | 
ML {*
 | 
|
710  | 
\<lbrace>  | 
|
711  | 
    @{let ?t = my_term}
 | 
|
712  | 
    @{note my_refl = reflexive [of ?t]}
 | 
|
713  | 
    fun foo th = Thm.transitive th @{thm my_refl}
 | 
|
714  | 
\<rbrace>  | 
|
715  | 
*}  | 
|
716  | 
||
| 40126 | 717  | 
text {* The extra block delimiters do not affect the compiled code
 | 
718  | 
  itself, i.e.\ function @{ML foo} is available in the present context
 | 
|
719  | 
of this paragraph.  | 
|
| 
39827
 
d829ce302ca4
basic setup for ML antiquotations -- with rail diagrams;
 
wenzelm 
parents: 
39825 
diff
changeset
 | 
720  | 
*}  | 
| 39823 | 721  | 
|
| 39835 | 722  | 
|
| 39883 | 723  | 
section {* Canonical argument order \label{sec:canonical-argument-order} *}
 | 
724  | 
||
725  | 
text {* Standard ML is a language in the tradition of @{text
 | 
|
726  | 
  "\<lambda>"}-calculus and \emph{higher-order functional programming},
 | 
|
727  | 
similar to OCaml, Haskell, or Isabelle/Pure and HOL as logical  | 
|
728  | 
languages. Getting acquainted with the native style of representing  | 
|
729  | 
functions in that setting can save a lot of extra boiler-plate of  | 
|
730  | 
redundant shuffling of arguments, auxiliary abstractions etc.  | 
|
731  | 
||
| 40126 | 732  | 
  Functions are usually \emph{curried}: the idea of turning arguments
 | 
733  | 
  of type @{text "\<tau>\<^sub>i"} (for @{text "i \<in> {1, \<dots> n}"}) into a result of
 | 
|
734  | 
  type @{text "\<tau>"} is represented by the iterated function space
 | 
|
735  | 
  @{text "\<tau>\<^sub>1 \<rightarrow> \<dots> \<rightarrow> \<tau>\<^sub>n \<rightarrow> \<tau>"}.  This is isomorphic to the well-known
 | 
|
736  | 
  encoding via tuples @{text "\<tau>\<^sub>1 \<times> \<dots> \<times> \<tau>\<^sub>n \<rightarrow> \<tau>"}, but the curried
 | 
|
737  | 
  version fits more smoothly into the basic calculus.\footnote{The
 | 
|
738  | 
difference is even more significant in higher-order logic, because  | 
|
739  | 
the redundant tuple structure needs to be accommodated by formal  | 
|
740  | 
reasoning.}  | 
|
| 39883 | 741  | 
|
742  | 
  Currying gives some flexiblity due to \emph{partial application}.  A
 | 
|
743  | 
  function @{text "f: \<tau>\<^sub>1 \<rightarrow> \<tau>\<^bsub>2\<^esub> \<rightarrow> \<tau>"} can be applied to @{text "x: \<tau>\<^sub>1"}
 | 
|
| 40126 | 744  | 
  and the remaining @{text "(f x): \<tau>\<^sub>2 \<rightarrow> \<tau>"} passed to another function
 | 
| 39883 | 745  | 
etc. How well this works in practice depends on the order of  | 
746  | 
arguments. In the worst case, arguments are arranged erratically,  | 
|
747  | 
and using a function in a certain situation always requires some  | 
|
748  | 
glue code. Thus we would get exponentially many oppurtunities to  | 
|
749  | 
decorate the code with meaningless permutations of arguments.  | 
|
750  | 
||
751  | 
  This can be avoided by \emph{canonical argument order}, which
 | 
|
| 40126 | 752  | 
observes certain standard patterns and minimizes adhoc permutations  | 
| 40229 | 753  | 
in their application. In Isabelle/ML, large portions of text can be  | 
| 40126 | 754  | 
  written without ever using @{text "swap: \<alpha> \<times> \<beta> \<rightarrow> \<beta> \<times> \<alpha>"}, or the
 | 
755  | 
  combinator @{text "C: (\<alpha> \<rightarrow> \<beta> \<rightarrow> \<gamma>) \<rightarrow> (\<beta> \<rightarrow> \<alpha> \<rightarrow> \<gamma>)"} that is not even
 | 
|
| 39883 | 756  | 
defined in our library.  | 
757  | 
||
758  | 
\medskip The basic idea is that arguments that vary less are moved  | 
|
759  | 
further to the left than those that vary more. Two particularly  | 
|
760  | 
  important categories of functions are \emph{selectors} and
 | 
|
761  | 
  \emph{updates}.
 | 
|
762  | 
||
763  | 
The subsequent scheme is based on a hypothetical set-like container  | 
|
764  | 
  of type @{text "\<beta>"} that manages elements of type @{text "\<alpha>"}.  Both
 | 
|
765  | 
the names and types of the associated operations are canonical for  | 
|
766  | 
Isabelle/ML.  | 
|
767  | 
||
768  | 
\medskip  | 
|
769  | 
  \begin{tabular}{ll}
 | 
|
770  | 
kind & canonical name and type \\\hline  | 
|
771  | 
  selector & @{text "member: \<beta> \<rightarrow> \<alpha> \<rightarrow> bool"} \\
 | 
|
772  | 
  update & @{text "insert: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} \\
 | 
|
773  | 
  \end{tabular}
 | 
|
774  | 
\medskip  | 
|
775  | 
||
776  | 
  Given a container @{text "B: \<beta>"}, the partially applied @{text
 | 
|
777  | 
  "member B"} is a predicate over elements @{text "\<alpha> \<rightarrow> bool"}, and
 | 
|
778  | 
thus represents the intended denotation directly. It is customary  | 
|
779  | 
to pass the abstract predicate to further operations, not the  | 
|
780  | 
concrete container. The argument order makes it easy to use other  | 
|
781  | 
  combinators: @{text "forall (member B) list"} will check a list of
 | 
|
782  | 
  elements for membership in @{text "B"} etc. Often the explicit
 | 
|
| 40126 | 783  | 
  @{text "list"} is pointless and can be contracted to @{text "forall
 | 
784  | 
(member B)"} to get directly a predicate again.  | 
|
| 39883 | 785  | 
|
| 40126 | 786  | 
In contrast, an update operation varies the container, so it moves  | 
| 39883 | 787  | 
  to the right: @{text "insert a"} is a function @{text "\<beta> \<rightarrow> \<beta>"} to
 | 
788  | 
  insert a value @{text "a"}.  These can be composed naturally as
 | 
|
| 40126 | 789  | 
  @{text "insert c \<circ> insert b \<circ> insert a"}.  The slightly awkward
 | 
| 40229 | 790  | 
inversion of the composition order is due to conventional  | 
| 40126 | 791  | 
mathematical notation, which can be easily amended as explained  | 
792  | 
below.  | 
|
| 39883 | 793  | 
*}  | 
794  | 
||
795  | 
||
796  | 
subsection {* Forward application and composition *}
 | 
|
797  | 
||
798  | 
text {* Regular function application and infix notation works best for
 | 
|
799  | 
  relatively deeply structured expressions, e.g.\ @{text "h (f x y + g
 | 
|
| 40126 | 800  | 
  z)"}.  The important special case of \emph{linear transformation}
 | 
801  | 
  applies a cascade of functions @{text "f\<^sub>n (\<dots> (f\<^sub>1 x))"}.  This
 | 
|
802  | 
becomes hard to read and maintain if the functions are themselves  | 
|
803  | 
given as complex expressions. The notation can be significantly  | 
|
| 39883 | 804  | 
  improved by introducing \emph{forward} versions of application and
 | 
805  | 
composition as follows:  | 
|
806  | 
||
807  | 
\medskip  | 
|
808  | 
  \begin{tabular}{lll}
 | 
|
809  | 
  @{text "x |> f"} & @{text "\<equiv>"} & @{text "f x"} \\
 | 
|
810  | 
  @{text "f #> g"} & @{text "\<equiv>"} & @{text "x |> f |> g"} \\
 | 
|
811  | 
  \end{tabular}
 | 
|
812  | 
\medskip  | 
|
813  | 
||
814  | 
  This enables to write conveniently @{text "x |> f\<^sub>1 |> \<dots> |> f\<^sub>n"} or
 | 
|
815  | 
  @{text "f\<^sub>1 #> \<dots> #> f\<^sub>n"} for its functional abstraction over @{text
 | 
|
816  | 
"x"}.  | 
|
817  | 
||
818  | 
\medskip There is an additional set of combinators to accommodate  | 
|
819  | 
multiple results (via pairs) that are passed on as multiple  | 
|
820  | 
arguments (via currying).  | 
|
821  | 
||
822  | 
\medskip  | 
|
823  | 
  \begin{tabular}{lll}
 | 
|
824  | 
  @{text "(x, y) |-> f"} & @{text "\<equiv>"} & @{text "f x y"} \\
 | 
|
825  | 
  @{text "f #-> g"} & @{text "\<equiv>"} & @{text "x |> f |-> g"} \\
 | 
|
826  | 
  \end{tabular}
 | 
|
827  | 
\medskip  | 
|
828  | 
*}  | 
|
829  | 
||
830  | 
text %mlref {*
 | 
|
831  | 
  \begin{mldecls}
 | 
|
832  | 
  @{index_ML "op |> ": "'a * ('a -> 'b) -> 'b"} \\
 | 
|
833  | 
  @{index_ML "op |-> ": "('c * 'a) * ('c -> 'a -> 'b) -> 'b"} \\
 | 
|
834  | 
  @{index_ML "op #> ": "('a -> 'b) * ('b -> 'c) -> 'a -> 'c"} \\
 | 
|
835  | 
  @{index_ML "op #-> ": "('a -> 'c * 'b) * ('c -> 'b -> 'd) -> 'a -> 'd"} \\
 | 
|
836  | 
  \end{mldecls}
 | 
|
837  | 
||
838  | 
%FIXME description!?  | 
|
839  | 
*}  | 
|
840  | 
||
841  | 
||
842  | 
subsection {* Canonical iteration *}
 | 
|
843  | 
||
844  | 
text {* As explained above, a function @{text "f: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} can be
 | 
|
| 40126 | 845  | 
  understood as update on a configuration of type @{text "\<beta>"},
 | 
| 39883 | 846  | 
  parametrized by arguments of type @{text "\<alpha>"}.  Given @{text "a: \<alpha>"}
 | 
847  | 
  the partial application @{text "(f a): \<beta> \<rightarrow> \<beta>"} operates
 | 
|
848  | 
  homogeneously on @{text "\<beta>"}.  This can be iterated naturally over a
 | 
|
849  | 
  list of parameters @{text "[a\<^sub>1, \<dots>, a\<^sub>n]"} as @{text "f a\<^sub>1 #> \<dots> #> f
 | 
|
850  | 
  a\<^bsub>n\<^esub>\<^bsub>\<^esub>"}.  The latter expression is again a function @{text "\<beta> \<rightarrow> \<beta>"}.
 | 
|
851  | 
  It can be applied to an initial configuration @{text "b: \<beta>"} to
 | 
|
852  | 
  start the iteration over the given list of arguments: each @{text
 | 
|
853  | 
  "a"} in @{text "a\<^sub>1, \<dots>, a\<^sub>n"} is applied consecutively by updating a
 | 
|
854  | 
cumulative configuration.  | 
|
855  | 
||
856  | 
  The @{text fold} combinator in Isabelle/ML lifts a function @{text
 | 
|
857  | 
"f"} as above to its iterated version over a list of arguments.  | 
|
858  | 
  Lifting can be repeated, e.g.\ @{text "(fold \<circ> fold) f"} iterates
 | 
|
859  | 
over a list of lists as expected.  | 
|
860  | 
||
861  | 
  The variant @{text "fold_rev"} works inside-out over the list of
 | 
|
862  | 
  arguments, such that @{text "fold_rev f \<equiv> fold f \<circ> rev"} holds.
 | 
|
863  | 
||
864  | 
  The @{text "fold_map"} combinator essentially performs @{text
 | 
|
865  | 
  "fold"} and @{text "map"} simultaneously: each application of @{text
 | 
|
866  | 
"f"} produces an updated configuration together with a side-result;  | 
|
867  | 
the iteration collects all such side-results as a separate list.  | 
|
868  | 
*}  | 
|
869  | 
||
870  | 
text %mlref {*
 | 
|
871  | 
  \begin{mldecls}
 | 
|
872  | 
  @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
 | 
|
873  | 
  @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
 | 
|
874  | 
  @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
 | 
|
875  | 
  \end{mldecls}
 | 
|
876  | 
||
877  | 
  \begin{description}
 | 
|
878  | 
||
879  | 
  \item @{ML fold}~@{text f} lifts the parametrized update function
 | 
|
880  | 
  @{text "f"} to a list of parameters.
 | 
|
881  | 
||
882  | 
  \item @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text
 | 
|
883  | 
"f"}, but works inside-out.  | 
|
884  | 
||
885  | 
  \item @{ML fold_map}~@{text "f"} lifts the parametrized update
 | 
|
886  | 
  function @{text "f"} (with side-result) to a list of parameters and
 | 
|
887  | 
cumulative side-results.  | 
|
888  | 
||
889  | 
  \end{description}
 | 
|
890  | 
||
891  | 
  \begin{warn}
 | 
|
892  | 
The literature on functional programming provides a multitude of  | 
|
893  | 
  combinators called @{text "foldl"}, @{text "foldr"} etc.  SML97
 | 
|
894  | 
  provides its own variations as @{ML List.foldl} and @{ML
 | 
|
| 40126 | 895  | 
List.foldr}, while the classic Isabelle library also has the  | 
896  | 
  historic @{ML Library.foldl} and @{ML Library.foldr}.  To avoid
 | 
|
897  | 
  further confusion, all of this should be ignored, and @{ML fold} (or
 | 
|
898  | 
  @{ML fold_rev}) used exclusively.
 | 
|
| 39883 | 899  | 
  \end{warn}
 | 
900  | 
*}  | 
|
901  | 
||
902  | 
text %mlex {* The following example shows how to fill a text buffer
 | 
|
903  | 
incrementally by adding strings, either individually or from a given  | 
|
904  | 
list.  | 
|
905  | 
*}  | 
|
906  | 
||
907  | 
ML {*
 | 
|
908  | 
val s =  | 
|
909  | 
Buffer.empty  | 
|
910  | 
|> Buffer.add "digits: "  | 
|
911  | 
|> fold (Buffer.add o string_of_int) (0 upto 9)  | 
|
912  | 
|> Buffer.content;  | 
|
913  | 
||
914  | 
  @{assert} (s = "digits: 0123456789");
 | 
|
915  | 
*}  | 
|
916  | 
||
917  | 
text {* Note how @{ML "fold (Buffer.add o string_of_int)"} above saves
 | 
|
918  | 
  an extra @{ML "map"} over the given list.  This kind of peephole
 | 
|
919  | 
optimization reduces both the code size and the tree structures in  | 
|
920  | 
memory (``deforestation''), but requires some practice to read and  | 
|
921  | 
write it fluently.  | 
|
922  | 
||
| 40126 | 923  | 
\medskip The next example elaborates the idea of canonical  | 
924  | 
iteration, demonstrating fast accumulation of tree content using a  | 
|
925  | 
text buffer.  | 
|
| 39883 | 926  | 
*}  | 
927  | 
||
928  | 
ML {*
 | 
|
929  | 
datatype tree = Text of string | Elem of string * tree list;  | 
|
930  | 
||
931  | 
fun slow_content (Text txt) = txt  | 
|
932  | 
| slow_content (Elem (name, ts)) =  | 
|
933  | 
"<" ^ name ^ ">" ^  | 
|
934  | 
implode (map slow_content ts) ^  | 
|
935  | 
"</" ^ name ^ ">"  | 
|
936  | 
||
937  | 
fun add_content (Text txt) = Buffer.add txt  | 
|
938  | 
| add_content (Elem (name, ts)) =  | 
|
939  | 
        Buffer.add ("<" ^ name ^ ">") #>
 | 
|
940  | 
fold add_content ts #>  | 
|
941  | 
        Buffer.add ("</" ^ name ^ ">");
 | 
|
942  | 
||
943  | 
fun fast_content tree =  | 
|
944  | 
Buffer.empty |> add_content tree |> Buffer.content;  | 
|
945  | 
*}  | 
|
946  | 
||
947  | 
text {* The slow part of @{ML slow_content} is the @{ML implode} of
 | 
|
948  | 
the recursive results, because it copies previously produced strings  | 
|
| 40126 | 949  | 
again.  | 
| 39883 | 950  | 
|
951  | 
  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
 | 
952  | 
  buffer that is passed through in a linear fashion.  Using @{ML_text
 | 
| 40126 | 953  | 
"#>"} and contraction over the actual buffer argument saves some  | 
954  | 
  additional boiler-plate.  Of course, the two @{ML "Buffer.add"}
 | 
|
955  | 
invocations with concatenated strings could have been split into  | 
|
956  | 
smaller parts, but this would have obfuscated the source without  | 
|
957  | 
making a big difference in allocations. Here we have done some  | 
|
| 39883 | 958  | 
peephole-optimization for the sake of readability.  | 
959  | 
||
960  | 
  Another benefit of @{ML add_content} is its ``open'' form as a
 | 
|
| 40126 | 961  | 
function on buffers that can be continued in further linear  | 
962  | 
transformations, folding etc. Thus it is more compositional than  | 
|
963  | 
  the naive @{ML slow_content}.  As realistic example, compare the
 | 
|
964  | 
  old-style @{ML "Term.maxidx_of_term: term -> int"} with the newer
 | 
|
965  | 
  @{ML "Term.maxidx_term: term -> int -> int"} in Isabelle/Pure.
 | 
|
| 39883 | 966  | 
|
| 40126 | 967  | 
  Note that @{ML fast_content} above is only defined as example.  In
 | 
968  | 
many practical situations, it is customary to provide the  | 
|
969  | 
  incremental @{ML add_content} only and leave the initialization and
 | 
|
970  | 
termination to the concrete application by the user.  | 
|
| 39883 | 971  | 
*}  | 
972  | 
||
973  | 
||
| 39854 | 974  | 
section {* Message output channels \label{sec:message-channels} *}
 | 
| 39835 | 975  | 
|
976  | 
text {* Isabelle provides output channels for different kinds of
 | 
|
977  | 
messages: regular output, high-volume tracing information, warnings,  | 
|
978  | 
and errors.  | 
|
979  | 
||
980  | 
Depending on the user interface involved, these messages may appear  | 
|
981  | 
in different text styles or colours. The standard output for  | 
|
982  | 
  terminal sessions prefixes each line of warnings by @{verbatim
 | 
|
983  | 
  "###"} and errors by @{verbatim "***"}, but leaves anything else
 | 
|
984  | 
unchanged.  | 
|
985  | 
||
986  | 
Messages are associated with the transaction context of the running  | 
|
987  | 
Isar command. This enables the front-end to manage commands and  | 
|
988  | 
resulting messages together. For example, after deleting a command  | 
|
989  | 
from a given theory document version, the corresponding message  | 
|
| 39872 | 990  | 
output can be retracted from the display.  | 
991  | 
*}  | 
|
| 39835 | 992  | 
|
993  | 
text %mlref {*
 | 
|
994  | 
  \begin{mldecls}
 | 
|
995  | 
  @{index_ML writeln: "string -> unit"} \\
 | 
|
996  | 
  @{index_ML tracing: "string -> unit"} \\
 | 
|
997  | 
  @{index_ML warning: "string -> unit"} \\
 | 
|
998  | 
  @{index_ML error: "string -> 'a"} \\
 | 
|
999  | 
  \end{mldecls}
 | 
|
1000  | 
||
1001  | 
  \begin{description}
 | 
|
1002  | 
||
1003  | 
  \item @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
 | 
|
1004  | 
message. This is the primary message output operation of Isabelle  | 
|
1005  | 
and should be used by default.  | 
|
1006  | 
||
1007  | 
  \item @{ML tracing}~@{text "text"} outputs @{text "text"} as special
 | 
|
1008  | 
tracing message, indicating potential high-volume output to the  | 
|
1009  | 
front-end (hundreds or thousands of messages issued by a single  | 
|
1010  | 
command). The idea is to allow the user-interface to downgrade the  | 
|
1011  | 
quality of message display to achieve higher throughput.  | 
|
1012  | 
||
1013  | 
Note that the user might have to take special actions to see tracing  | 
|
1014  | 
output, e.g.\ switch to a different output window. So this channel  | 
|
1015  | 
should not be used for regular output.  | 
|
1016  | 
||
1017  | 
  \item @{ML warning}~@{text "text"} outputs @{text "text"} as
 | 
|
1018  | 
warning, which typically means some extra emphasis on the front-end  | 
|
| 40126 | 1019  | 
side (color highlighting, icons, etc.).  | 
| 39835 | 1020  | 
|
1021  | 
  \item @{ML error}~@{text "text"} raises exception @{ML ERROR}~@{text
 | 
|
1022  | 
  "text"} and thus lets the Isar toplevel print @{text "text"} on the
 | 
|
1023  | 
error channel, which typically means some extra emphasis on the  | 
|
| 40126 | 1024  | 
front-end side (color highlighting, icons, etc.).  | 
| 39835 | 1025  | 
|
1026  | 
This assumes that the exception is not handled before the command  | 
|
1027  | 
  terminates.  Handling exception @{ML ERROR}~@{text "text"} is a
 | 
|
1028  | 
perfectly legal alternative: it means that the error is absorbed  | 
|
1029  | 
without any message output.  | 
|
1030  | 
||
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
1031  | 
  \begin{warn}
 | 
| 39835 | 1032  | 
  The actual error channel is accessed via @{ML Output.error_msg}, but
 | 
1033  | 
  the interaction protocol of Proof~General \emph{crashes} if that
 | 
|
1034  | 
function is used in regular ML code: error output and toplevel  | 
|
| 40126 | 1035  | 
command failure always need to coincide.  | 
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
1036  | 
  \end{warn}
 | 
| 39835 | 1037  | 
|
| 
39861
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
1038  | 
  \end{description}
 | 
| 
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
1039  | 
|
| 
 
b8d89db3e238
use continental paragraph style, which works better with mixture of (in)formal text;
 
wenzelm 
parents: 
39859 
diff
changeset
 | 
1040  | 
  \begin{warn}
 | 
| 39835 | 1041  | 
Regular Isabelle/ML code should output messages exclusively by the  | 
1042  | 
  official channels.  Using raw I/O on \emph{stdout} or \emph{stderr}
 | 
|
1043  | 
  instead (e.g.\ via @{ML TextIO.output}) is apt to cause problems in
 | 
|
1044  | 
the presence of parallel and asynchronous processing of Isabelle  | 
|
1045  | 
theories. Such raw output might be displayed by the front-end in  | 
|
1046  | 
some system console log, with a low chance that the user will ever  | 
|
1047  | 
see it. Moreover, as a genuine side-effect on global process  | 
|
1048  | 
channels, there is no proper way to retract output when Isar command  | 
|
| 40126 | 1049  | 
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
 | 
1050  | 
  \end{warn}
 | 
| 39872 | 1051  | 
|
1052  | 
  \begin{warn}
 | 
|
1053  | 
The message channels should be used in a message-oriented manner.  | 
|
| 40126 | 1054  | 
This means that multi-line output that logically belongs together is  | 
1055  | 
  issued by a \emph{single} invocation of @{ML writeln} etc.\ with the
 | 
|
1056  | 
functional concatenation of all message constituents.  | 
|
| 39872 | 1057  | 
  \end{warn}
 | 
1058  | 
*}  | 
|
1059  | 
||
1060  | 
text %mlex {* The following example demonstrates a multi-line
 | 
|
1061  | 
warning. Note that in some situations the user sees only the first  | 
|
1062  | 
line, so the most important point should be made first.  | 
|
1063  | 
*}  | 
|
1064  | 
||
1065  | 
ML_command {*
 | 
|
1066  | 
warning (cat_lines  | 
|
1067  | 
["Beware the Jabberwock, my son!",  | 
|
1068  | 
"The jaws that bite, the claws that catch!",  | 
|
1069  | 
"Beware the Jubjub Bird, and shun",  | 
|
1070  | 
"The frumious Bandersnatch!"]);  | 
|
| 39835 | 1071  | 
*}  | 
1072  | 
||
| 39854 | 1073  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1074  | 
section {* Exceptions \label{sec:exceptions} *}
 | 
| 39854 | 1075  | 
|
1076  | 
text {* The Standard ML semantics of strict functional evaluation
 | 
|
1077  | 
together with exceptions is rather well defined, but some delicate  | 
|
1078  | 
points need to be observed to avoid that ML programs go wrong  | 
|
1079  | 
despite static type-checking. Exceptions in Isabelle/ML are  | 
|
1080  | 
subsequently categorized as follows.  | 
|
1081  | 
||
1082  | 
  \paragraph{Regular user errors.}  These are meant to provide
 | 
|
1083  | 
informative feedback about malformed input etc.  | 
|
1084  | 
||
1085  | 
  The \emph{error} function raises the corresponding \emph{ERROR}
 | 
|
1086  | 
  exception, with a plain text message as argument.  \emph{ERROR}
 | 
|
1087  | 
exceptions can be handled internally, in order to be ignored, turned  | 
|
1088  | 
into other exceptions, or cascaded by appending messages. If the  | 
|
1089  | 
  corresponding Isabelle/Isar command terminates with an \emph{ERROR}
 | 
|
| 39855 | 1090  | 
exception state, the toplevel will print the result on the error  | 
1091  | 
  channel (see \secref{sec:message-channels}).
 | 
|
| 39854 | 1092  | 
|
1093  | 
It is considered bad style to refer to internal function names or  | 
|
1094  | 
values in ML source notation in user error messages.  | 
|
1095  | 
||
1096  | 
Grammatical correctness of error messages can be improved by  | 
|
1097  | 
  \emph{omitting} final punctuation: messages are often concatenated
 | 
|
1098  | 
or put into a larger context (e.g.\ augmented with source position).  | 
|
1099  | 
By not insisting in the final word at the origin of the error, the  | 
|
1100  | 
system can perform its administrative tasks more easily and  | 
|
1101  | 
robustly.  | 
|
1102  | 
||
1103  | 
  \paragraph{Program failures.}  There is a handful of standard
 | 
|
1104  | 
exceptions that indicate general failure situations, or failures of  | 
|
1105  | 
core operations on logical entities (types, terms, theorems,  | 
|
| 39856 | 1106  | 
  theories, see \chref{ch:logic}).
 | 
| 39854 | 1107  | 
|
1108  | 
These exceptions indicate a genuine breakdown of the program, so the  | 
|
1109  | 
main purpose is to determine quickly what has happened where.  | 
|
| 39855 | 1110  | 
Traditionally, the (short) exception message would include the name  | 
| 40126 | 1111  | 
of an ML function, although this is no longer necessary, because the  | 
1112  | 
ML runtime system prints a detailed source position of the  | 
|
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
1113  | 
  corresponding @{ML_text raise} keyword.
 | 
| 39854 | 1114  | 
|
1115  | 
\medskip User modules can always introduce their own custom  | 
|
1116  | 
exceptions locally, e.g.\ to organize internal failures robustly  | 
|
1117  | 
without overlapping with existing exceptions. Exceptions that are  | 
|
1118  | 
exposed in module signatures require extra care, though, and should  | 
|
| 40126 | 1119  | 
  \emph{not} be introduced by default.  Surprise by users of a module
 | 
1120  | 
can be often minimized by using plain user errors instead.  | 
|
| 39854 | 1121  | 
|
1122  | 
  \paragraph{Interrupts.}  These indicate arbitrary system events:
 | 
|
1123  | 
both the ML runtime system and the Isabelle/ML infrastructure signal  | 
|
1124  | 
various exceptional situations by raising the special  | 
|
1125  | 
  \emph{Interrupt} exception in user code.
 | 
|
1126  | 
||
1127  | 
This is the one and only way that physical events can intrude an  | 
|
1128  | 
Isabelle/ML program. Such an interrupt can mean out-of-memory,  | 
|
1129  | 
stack overflow, timeout, internal signaling of threads, or the user  | 
|
| 39855 | 1130  | 
producing a console interrupt manually etc. An Isabelle/ML program  | 
1131  | 
that intercepts interrupts becomes dependent on physical effects of  | 
|
1132  | 
the environment. Even worse, exception handling patterns that are  | 
|
1133  | 
too general by accident, e.g.\ by mispelled exception constructors,  | 
|
| 40126 | 1134  | 
will cover interrupts unintentionally and thus render the program  | 
| 39855 | 1135  | 
semantics ill-defined.  | 
| 39854 | 1136  | 
|
1137  | 
Note that the Interrupt exception dates back to the original SML90  | 
|
1138  | 
language definition. It was excluded from the SML97 version to  | 
|
1139  | 
avoid its malign impact on ML program semantics, but without  | 
|
1140  | 
providing a viable alternative. Isabelle/ML recovers physical  | 
|
| 40229 | 1141  | 
interruptibility (which is an indispensable tool to implement  | 
1142  | 
managed evaluation of command transactions), but requires user code  | 
|
1143  | 
to be strictly transparent wrt.\ interrupts.  | 
|
| 39854 | 1144  | 
|
1145  | 
  \begin{warn}
 | 
|
1146  | 
Isabelle/ML user code needs to terminate promptly on interruption,  | 
|
1147  | 
without guessing at its meaning to the system infrastructure.  | 
|
1148  | 
Temporary handling of interrupts for cleanup of global resources  | 
|
1149  | 
etc.\ needs to be followed immediately by re-raising of the original  | 
|
1150  | 
exception.  | 
|
1151  | 
  \end{warn}
 | 
|
1152  | 
*}  | 
|
1153  | 
||
| 39855 | 1154  | 
text %mlref {*
 | 
1155  | 
  \begin{mldecls}
 | 
|
1156  | 
  @{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\
 | 
|
1157  | 
  @{index_ML can: "('a -> 'b) -> 'a -> bool"} \\
 | 
|
| 39856 | 1158  | 
  @{index_ML ERROR: "string -> exn"} \\
 | 
1159  | 
  @{index_ML Fail: "string -> exn"} \\
 | 
|
1160  | 
  @{index_ML Exn.is_interrupt: "exn -> bool"} \\
 | 
|
| 39855 | 1161  | 
  @{index_ML reraise: "exn -> 'a"} \\
 | 
1162  | 
  @{index_ML exception_trace: "(unit -> 'a) -> 'a"} \\
 | 
|
1163  | 
  \end{mldecls}
 | 
|
1164  | 
||
1165  | 
  \begin{description}
 | 
|
1166  | 
||
1167  | 
  \item @{ML try}~@{text "f x"} makes the partiality of evaluating
 | 
|
1168  | 
  @{text "f x"} explicit via the option datatype.  Interrupts are
 | 
|
1169  | 
  \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
 | 
1170  | 
  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
 | 
1171  | 
  x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in
 | 
| 39855 | 1172  | 
books about SML.  | 
1173  | 
||
1174  | 
  \item @{ML can} is similar to @{ML try} with more abstract result.
 | 
|
1175  | 
||
| 39856 | 1176  | 
  \item @{ML ERROR}~@{text "msg"} represents user errors; this
 | 
| 40126 | 1177  | 
  exception is normally raised indirectly via the @{ML error} function
 | 
1178  | 
  (see \secref{sec:message-channels}).
 | 
|
| 39856 | 1179  | 
|
1180  | 
  \item @{ML Fail}~@{text "msg"} represents general program failures.
 | 
|
1181  | 
||
1182  | 
  \item @{ML Exn.is_interrupt} identifies interrupts robustly, without
 | 
|
1183  | 
mentioning concrete exception constructors in user code. Handled  | 
|
1184  | 
interrupts need to be re-raised promptly!  | 
|
1185  | 
||
| 39855 | 1186  | 
  \item @{ML reraise}~@{text "exn"} raises exception @{text "exn"}
 | 
1187  | 
while preserving its implicit position information (if possible,  | 
|
1188  | 
depending on the ML platform).  | 
|
1189  | 
||
| 
40149
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
1190  | 
  \item @{ML exception_trace}~@{ML_text "(fn () =>"}~@{text
 | 
| 
 
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
 
wenzelm 
parents: 
40126 
diff
changeset
 | 
1191  | 
  "e"}@{ML_text ")"} evaluates expression @{text "e"} while printing
 | 
| 39855 | 1192  | 
a full trace of its stack of nested exceptions (if possible,  | 
| 40126 | 1193  | 
  depending on the ML platform).\footnote{In versions of Poly/ML the
 | 
1194  | 
trace will appear on raw stdout of the Isabelle process.}  | 
|
| 39855 | 1195  | 
|
1196  | 
  Inserting @{ML exception_trace} into ML code temporarily is useful
 | 
|
1197  | 
for debugging, but not suitable for production code.  | 
|
1198  | 
||
1199  | 
  \end{description}
 | 
|
1200  | 
*}  | 
|
1201  | 
||
| 
39866
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1202  | 
text %mlantiq {*
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1203  | 
  \begin{matharray}{rcl}
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1204  | 
  @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1205  | 
  \end{matharray}
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1206  | 
|
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1207  | 
  \begin{description}
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1208  | 
|
| 40110 | 1209  | 
  \item @{text "@{assert}"} inlines a function
 | 
1210  | 
  @{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is
 | 
|
1211  | 
  @{ML false}.  Due to inlining the source position of failed
 | 
|
1212  | 
assertions is included in the error output.  | 
|
| 
39866
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1213  | 
|
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1214  | 
  \end{description}
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1215  | 
*}  | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1216  | 
|
| 39859 | 1217  | 
|
| 39863 | 1218  | 
section {* Basic data types *}
 | 
| 39859 | 1219  | 
|
| 40126 | 1220  | 
text {* The basis library proposal of SML97 needs to be treated with
 | 
| 39859 | 1221  | 
caution. Many of its operations simply do not fit with important  | 
1222  | 
Isabelle/ML conventions (like ``canonical argument order'', see  | 
|
| 40126 | 1223  | 
  \secref{sec:canonical-argument-order}), others cause problems with
 | 
1224  | 
  the parallel evaluation model of Isabelle/ML (such as @{ML
 | 
|
1225  | 
  TextIO.print} or @{ML OS.Process.system}).
 | 
|
| 39859 | 1226  | 
|
1227  | 
Subsequently we give a brief overview of important operations on  | 
|
1228  | 
basic ML data types.  | 
|
1229  | 
*}  | 
|
1230  | 
||
1231  | 
||
| 39863 | 1232  | 
subsection {* Characters *}
 | 
1233  | 
||
1234  | 
text %mlref {*
 | 
|
1235  | 
  \begin{mldecls}
 | 
|
1236  | 
  @{index_ML_type char} \\
 | 
|
1237  | 
  \end{mldecls}
 | 
|
1238  | 
||
1239  | 
  \begin{description}
 | 
|
1240  | 
||
| 39864 | 1241  | 
  \item Type @{ML_type char} is \emph{not} used.  The smallest textual
 | 
| 40126 | 1242  | 
unit in Isabelle is represented as a ``symbol'' (see  | 
| 39864 | 1243  | 
  \secref{sec:symbols}).
 | 
| 39863 | 1244  | 
|
1245  | 
  \end{description}
 | 
|
1246  | 
*}  | 
|
1247  | 
||
1248  | 
||
| 39862 | 1249  | 
subsection {* Integers *}
 | 
1250  | 
||
1251  | 
text %mlref {*
 | 
|
1252  | 
  \begin{mldecls}
 | 
|
1253  | 
  @{index_ML_type int} \\
 | 
|
1254  | 
  \end{mldecls}
 | 
|
1255  | 
||
1256  | 
  \begin{description}
 | 
|
1257  | 
||
| 39864 | 1258  | 
  \item Type @{ML_type int} represents regular mathematical integers,
 | 
1259  | 
  which are \emph{unbounded}.  Overflow never happens in
 | 
|
| 39862 | 1260  | 
  practice.\footnote{The size limit for integer bit patterns in memory
 | 
1261  | 
is 64\,MB for 32-bit Poly/ML, and much higher for 64-bit systems.}  | 
|
1262  | 
This works uniformly for all supported ML platforms (Poly/ML and  | 
|
1263  | 
SML/NJ).  | 
|
1264  | 
||
| 40126 | 1265  | 
Literal integers in ML text are forced to be of this one true  | 
| 39862 | 1266  | 
integer type --- overloading of SML97 is disabled.  | 
1267  | 
||
| 40126 | 1268  | 
  Structure @{ML_struct IntInf} of SML97 is obsolete and superseded by
 | 
| 39862 | 1269  | 
  @{ML_struct Int}.  Structure @{ML_struct Integer} in @{"file"
 | 
1270  | 
"~~/src/Pure/General/integer.ML"} provides some additional  | 
|
1271  | 
operations.  | 
|
1272  | 
||
1273  | 
  \end{description}
 | 
|
1274  | 
*}  | 
|
1275  | 
||
1276  | 
||
| 40302 | 1277  | 
subsection {* Time *}
 | 
1278  | 
||
1279  | 
text %mlref {*
 | 
|
1280  | 
  \begin{mldecls}
 | 
|
1281  | 
  @{index_ML_type Time.time} \\
 | 
|
1282  | 
  @{index_ML seconds: "real -> Time.time"} \\
 | 
|
1283  | 
  \end{mldecls}
 | 
|
1284  | 
||
1285  | 
  \begin{description}
 | 
|
1286  | 
||
1287  | 
  \item Type @{ML_type Time.time} represents time abstractly according
 | 
|
1288  | 
to the SML97 basis library definition. This is adequate for  | 
|
1289  | 
internal ML operations, but awkward in concrete time specifications.  | 
|
1290  | 
||
1291  | 
  \item @{ML seconds}~@{text "s"} turns the concrete scalar @{text
 | 
|
1292  | 
"s"} (measured in seconds) into an abstract time value. Floating  | 
|
1293  | 
point numbers are easy to use as context parameters (e.g.\ via  | 
|
1294  | 
  configuration options, see \secref{sec:config-options}) or
 | 
|
1295  | 
preferences that are maintained by external tools as well.  | 
|
1296  | 
||
1297  | 
  \end{description}
 | 
|
1298  | 
*}  | 
|
1299  | 
||
1300  | 
||
| 39859 | 1301  | 
subsection {* Options *}
 | 
1302  | 
||
1303  | 
text %mlref {*
 | 
|
1304  | 
  \begin{mldecls}
 | 
|
1305  | 
  @{index_ML Option.map: "('a -> 'b) -> 'a option -> 'b option"} \\
 | 
|
1306  | 
  @{index_ML is_some: "'a option -> bool"} \\
 | 
|
1307  | 
  @{index_ML is_none: "'a option -> bool"} \\
 | 
|
1308  | 
  @{index_ML the: "'a option -> 'a"} \\
 | 
|
1309  | 
  @{index_ML these: "'a list option -> 'a list"} \\
 | 
|
1310  | 
  @{index_ML the_list: "'a option -> 'a list"} \\
 | 
|
1311  | 
  @{index_ML the_default: "'a -> 'a option -> 'a"} \\
 | 
|
1312  | 
  \end{mldecls}
 | 
|
1313  | 
*}  | 
|
1314  | 
||
1315  | 
text {* Apart from @{ML Option.map} most operations defined in
 | 
|
1316  | 
  structure @{ML_struct Option} are alien to Isabelle/ML.  The
 | 
|
1317  | 
  operations shown above are defined in @{"file"
 | 
|
1318  | 
"~~/src/Pure/General/basics.ML"}, among others. *}  | 
|
1319  | 
||
1320  | 
||
| 39863 | 1321  | 
subsection {* Lists *}
 | 
1322  | 
||
1323  | 
text {* Lists are ubiquitous in ML as simple and light-weight
 | 
|
1324  | 
``collections'' for many everyday programming tasks. Isabelle/ML  | 
|
| 39874 | 1325  | 
provides important additions and improvements over operations that  | 
1326  | 
are predefined in the SML97 library. *}  | 
|
| 39863 | 1327  | 
|
1328  | 
text %mlref {*
 | 
|
1329  | 
  \begin{mldecls}
 | 
|
1330  | 
  @{index_ML cons: "'a -> 'a list -> 'a list"} \\
 | 
|
| 39874 | 1331  | 
  @{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\
 | 
1332  | 
  @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
 | 
|
1333  | 
  @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
 | 
|
1334  | 
  @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
 | 
|
| 39863 | 1335  | 
  \end{mldecls}
 | 
1336  | 
||
1337  | 
  \begin{description}
 | 
|
1338  | 
||
1339  | 
  \item @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
 | 
|
1340  | 
||
1341  | 
Tupled infix operators are a historical accident in Standard ML.  | 
|
1342  | 
  The curried @{ML cons} amends this, but it should be only used when
 | 
|
1343  | 
partial application is required.  | 
|
1344  | 
||
| 39874 | 1345  | 
  \item @{ML member}, @{ML insert}, @{ML remove}, @{ML update} treat
 | 
1346  | 
lists as a set-like container that maintains the order of elements.  | 
|
1347  | 
  See @{"file" "~~/src/Pure/library.ML"} for the full specifications
 | 
|
1348  | 
(written in ML). There are some further derived operations like  | 
|
1349  | 
  @{ML union} or @{ML inter}.
 | 
|
1350  | 
||
1351  | 
  Note that @{ML insert} is conservative about elements that are
 | 
|
1352  | 
  already a @{ML member} of the list, while @{ML update} ensures that
 | 
|
| 40126 | 1353  | 
the latest entry is always put in front. The latter discipline is  | 
| 39874 | 1354  | 
often more appropriate in declarations of context data  | 
1355  | 
  (\secref{sec:context-data}) that are issued by the user in Isar
 | 
|
1356  | 
source: more recent declarations normally take precedence over  | 
|
1357  | 
earlier ones.  | 
|
1358  | 
||
| 39863 | 1359  | 
  \end{description}
 | 
1360  | 
*}  | 
|
1361  | 
||
| 40126 | 1362  | 
text %mlex {* Using canonical @{ML fold} together with @{ML cons}, or
 | 
1363  | 
similar standard operations, alternates the orientation of data.  | 
|
1364  | 
The is quite natural and should not be altered forcible by inserting  | 
|
1365  | 
  extra applications of @{ML rev}.  The alternative @{ML fold_rev} can
 | 
|
1366  | 
be used in the few situations, where alternation should be  | 
|
1367  | 
prevented.  | 
|
| 39863 | 1368  | 
*}  | 
1369  | 
||
1370  | 
ML {*
 | 
|
1371  | 
val items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];  | 
|
1372  | 
||
1373  | 
val list1 = fold cons items [];  | 
|
| 
39866
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1374  | 
  @{assert} (list1 = rev items);
 | 
| 
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1375  | 
|
| 39863 | 1376  | 
val list2 = fold_rev cons items [];  | 
| 
39866
 
5ec01d5acd0c
more robust examples: explicit @{assert} instead of unchecked output;
 
wenzelm 
parents: 
39864 
diff
changeset
 | 
1377  | 
  @{assert} (list2 = items);
 | 
| 39863 | 1378  | 
*}  | 
1379  | 
||
| 39883 | 1380  | 
text {* The subsequent example demonstrates how to \emph{merge} two
 | 
1381  | 
lists in a natural way. *}  | 
|
1382  | 
||
1383  | 
ML {*
 | 
|
1384  | 
fun merge_lists eq (xs, ys) = fold_rev (insert eq) ys xs;  | 
|
1385  | 
*}  | 
|
1386  | 
||
1387  | 
text {* Here the first list is treated conservatively: only the new
 | 
|
1388  | 
elements from the second list are inserted. The inside-out order of  | 
|
1389  | 
  insertion via @{ML fold_rev} attempts to preserve the order of
 | 
|
1390  | 
elements in the result.  | 
|
1391  | 
||
1392  | 
This way of merging lists is typical for context data  | 
|
1393  | 
  (\secref{sec:context-data}).  See also @{ML merge} as defined in
 | 
|
1394  | 
  @{"file" "~~/src/Pure/library.ML"}.
 | 
|
1395  | 
*}  | 
|
1396  | 
||
| 39863 | 1397  | 
|
| 
39875
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1398  | 
subsection {* Association lists *}
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1399  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1400  | 
text {* The operations for association lists interpret a concrete list
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1401  | 
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
 | 
1402  | 
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
 | 
1403  | 
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
 | 
1404  | 
occurrence into account.  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1405  | 
*}  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1406  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1407  | 
text {*
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1408  | 
  \begin{mldecls}
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1409  | 
  @{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
 | 
1410  | 
  @{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
 | 
1411  | 
  @{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
 | 
1412  | 
  \end{mldecls}
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1413  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1414  | 
  \begin{description}
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1415  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1416  | 
  \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
 | 
1417  | 
implement the main ``framework operations'' for mappings in  | 
| 40126 | 1418  | 
Isabelle/ML, following standard conventions for their names and  | 
1419  | 
types.  | 
|
| 
39875
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1420  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1421  | 
  Note that a function called @{text lookup} is obliged to express its
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1422  | 
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
 | 
1423  | 
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
 | 
1424  | 
  @{text "the_element"} or @{text "get"}.
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1425  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1426  | 
  The @{text "defined"} operation is essentially a contraction of @{ML
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1427  | 
  is_some} and @{text "lookup"}, but this is sufficiently frequent to
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1428  | 
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
 | 
1429  | 
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
 | 
1430  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1431  | 
  \end{description}
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1432  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1433  | 
Association lists are adequate as simple and light-weight  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1434  | 
implementation of finite mappings in many practical situations. A  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1435  | 
  more heavy-duty table structure is defined in @{"file"
 | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1436  | 
"~~/src/Pure/General/table.ML"}; that version scales easily to  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1437  | 
thousands or millions of elements.  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1438  | 
*}  | 
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1439  | 
|
| 
 
648c930125f6
more on "Association lists", based on more succinct version of older material;
 
wenzelm 
parents: 
39874 
diff
changeset
 | 
1440  | 
|
| 39859 | 1441  | 
subsection {* Unsynchronized references *}
 | 
1442  | 
||
1443  | 
text %mlref {*
 | 
|
1444  | 
  \begin{mldecls}
 | 
|
| 39870 | 1445  | 
  @{index_ML_type "'a Unsynchronized.ref"} \\
 | 
| 39859 | 1446  | 
  @{index_ML Unsynchronized.ref: "'a -> 'a Unsynchronized.ref"} \\
 | 
1447  | 
  @{index_ML "!": "'a Unsynchronized.ref -> 'a"} \\
 | 
|
1448  | 
  @{index_ML "op :=": "'a Unsynchronized.ref * 'a -> unit"} \\
 | 
|
1449  | 
  \end{mldecls}
 | 
|
1450  | 
*}  | 
|
1451  | 
||
1452  | 
text {* Due to ubiquitous parallelism in Isabelle/ML (see also
 | 
|
1453  | 
  \secref{sec:multi-threading}), the mutable reference cells of
 | 
|
1454  | 
Standard ML are notorious for causing problems. In a highly  | 
|
1455  | 
  parallel system, both correctness \emph{and} performance are easily
 | 
|
1456  | 
degraded when using mutable data.  | 
|
1457  | 
||
1458  | 
  The unwieldy name of @{ML Unsynchronized.ref} for the constructor
 | 
|
1459  | 
for references in Isabelle/ML emphasizes the inconveniences caused by  | 
|
1460  | 
  mutability.  Existing operations @{ML "!"}  and @{ML "op :="} are
 | 
|
1461  | 
unchanged, but should be used with special precautions, say in a  | 
|
1462  | 
strictly local situation that is guaranteed to be restricted to  | 
|
| 39870 | 1463  | 
sequential evaluation --- now and in the future. *}  | 
| 39859 | 1464  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1465  | 
|
| 39870 | 1466  | 
section {* Thread-safe programming \label{sec:multi-threading} *}
 | 
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1467  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1468  | 
text {* Multi-threaded execution has become an everyday reality in
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1469  | 
Isabelle since Poly/ML 5.2.1 and Isabelle2008. Isabelle/ML provides  | 
| 39868 | 1470  | 
implicit and explicit parallelism by default, and there is no way  | 
1471  | 
for user-space tools to ``opt out''. ML programs that are purely  | 
|
1472  | 
functional, output messages only via the official channels  | 
|
1473  | 
  (\secref{sec:message-channels}), and do not intercept interrupts
 | 
|
1474  | 
  (\secref{sec:exceptions}) can participate in the multi-threaded
 | 
|
1475  | 
environment immediately without further ado.  | 
|
1476  | 
||
1477  | 
More ambitious tools with more fine-grained interaction with the  | 
|
1478  | 
environment need to observe the principles explained below.  | 
|
1479  | 
*}  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1480  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1481  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1482  | 
subsection {* Multi-threading with shared memory *}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1483  | 
|
| 39868 | 1484  | 
text {* Multiple threads help to organize advanced operations of the
 | 
1485  | 
system, such as real-time conditions on command transactions,  | 
|
1486  | 
sub-components with explicit communication, general asynchronous  | 
|
1487  | 
interaction etc. Moreover, parallel evaluation is a prerequisite to  | 
|
1488  | 
make adequate use of the CPU resources that are available on  | 
|
1489  | 
  multi-core systems.\footnote{Multi-core computing does not mean that
 | 
|
1490  | 
there are ``spare cycles'' to be wasted. It means that the  | 
|
1491  | 
continued exponential speedup of CPU performance due to ``Moore's  | 
|
1492  | 
Law'' follows different rules: clock frequency has reached its peak  | 
|
1493  | 
around 2005, and applications need to be parallelized in order to  | 
|
1494  | 
avoid a perceived loss of performance. See also  | 
|
1495  | 
  \cite{Sutter:2005}.}
 | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1496  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1497  | 
Isabelle/Isar exploits the inherent structure of theories and proofs  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1498  | 
  to support \emph{implicit parallelism} to a large extent.  LCF-style
 | 
| 40126 | 1499  | 
theorem provides almost ideal conditions for that, see also  | 
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1500  | 
  \cite{Wenzel:2009}.  This means, significant parts of theory and
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1501  | 
proof checking is parallelized by default. A maximum speedup-factor  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1502  | 
of 3.0 on 4 cores and 5.0 on 8 cores can be  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1503  | 
  expected.\footnote{Further scalability is limited due to garbage
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1504  | 
collection, which is still sequential in Poly/ML 5.2/5.3/5.4. It  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1505  | 
helps to provide initial heap space generously, using the  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1506  | 
  \texttt{-H} option.  Initial heap size needs to be scaled-up
 | 
| 39868 | 1507  | 
together with the number of CPU cores: approximately 1--2\,GB per  | 
1508  | 
core..}  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1509  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1510  | 
\medskip ML threads lack the memory protection of separate  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1511  | 
processes, and operate concurrently on shared heap memory. This has  | 
| 40126 | 1512  | 
the advantage that results of independent computations are directly  | 
1513  | 
available to other threads: abstract values can be passed without  | 
|
1514  | 
copying or awkward serialization that is typically required for  | 
|
1515  | 
separate processes.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1516  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1517  | 
To make shared-memory multi-threading work robustly and efficiently,  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1518  | 
some programming guidelines need to be observed. While the ML  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1519  | 
system is responsible to maintain basic integrity of the  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1520  | 
representation of ML values in memory, the application programmer  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1521  | 
needs to ensure that multi-threaded execution does not break the  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1522  | 
intended semantics.  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1523  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1524  | 
  \begin{warn}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1525  | 
To participate in implicit parallelism, tools need to be  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1526  | 
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
 | 
1527  | 
performance of the whole system.  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1528  | 
  \end{warn}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1529  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1530  | 
Apart from observing the principles of thread-safeness passively,  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1531  | 
advanced tools may also exploit parallelism actively, e.g.\ by using  | 
| 39868 | 1532  | 
  ``future values'' (\secref{sec:futures}) or the more basic library
 | 
1533  | 
  functions for parallel list operations (\secref{sec:parlist}).
 | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1534  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1535  | 
  \begin{warn}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1536  | 
Parallel computing resources are managed centrally by the  | 
| 39868 | 1537  | 
Isabelle/ML infrastructure. User programs must not fork their own  | 
1538  | 
ML threads to perform computations.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1539  | 
  \end{warn}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1540  | 
*}  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1541  | 
|
| 39868 | 1542  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1543  | 
subsection {* Critical shared resources *}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1544  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1545  | 
text {* Thread-safeness is mainly concerned about concurrent
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1546  | 
read/write access to shared resources, which are outside the purely  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1547  | 
functional world of ML. This covers the following in particular.  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1548  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1549  | 
  \begin{itemize}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1550  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1551  | 
\item Global references (or arrays), i.e.\ mutable memory cells that  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1552  | 
persist over several invocations of associated  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1553  | 
  operations.\footnote{This is independent of the visibility of such
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1554  | 
mutable values in the toplevel scope.}  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1555  | 
|
| 39868 | 1556  | 
\item Global state of the running Isabelle/ML process, i.e.\ raw I/O  | 
1557  | 
channels, environment variables, current working directory.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1558  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1559  | 
\item Writable resources in the file-system that are shared among  | 
| 40126 | 1560  | 
different threads or external processes.  | 
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1561  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1562  | 
  \end{itemize}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1563  | 
|
| 39868 | 1564  | 
Isabelle/ML provides various mechanisms to avoid critical shared  | 
| 40126 | 1565  | 
resources in most situations. As last resort there are some  | 
1566  | 
mechanisms for explicit synchronization. The following guidelines  | 
|
1567  | 
help to make Isabelle/ML programs work smoothly in a concurrent  | 
|
1568  | 
environment.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1569  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1570  | 
  \begin{itemize}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1571  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1572  | 
\item Avoid global references altogether. Isabelle/Isar maintains a  | 
| 39868 | 1573  | 
uniform context that incorporates arbitrary data declared by user  | 
1574  | 
  programs (\secref{sec:context-data}).  This context is passed as
 | 
|
1575  | 
plain value and user tools can get/map their own data in a purely  | 
|
1576  | 
functional manner. Configuration options within the context  | 
|
1577  | 
  (\secref{sec:config-options}) provide simple drop-in replacements
 | 
|
| 40126 | 1578  | 
for historic reference variables.  | 
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1579  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1580  | 
\item Keep components with local state information re-entrant.  | 
| 39868 | 1581  | 
Instead of poking initial values into (private) global references, a  | 
1582  | 
new state record can be created on each invocation, and passed  | 
|
1583  | 
through any auxiliary functions of the component. The state record  | 
|
1584  | 
may well contain mutable references, without requiring any special  | 
|
1585  | 
synchronizations, as long as each invocation gets its own copy.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1586  | 
|
| 39868 | 1587  | 
  \item Avoid raw output on @{text "stdout"} or @{text "stderr"}.  The
 | 
1588  | 
Poly/ML library is thread-safe for each individual output operation,  | 
|
1589  | 
but the ordering of parallel invocations is arbitrary. This means  | 
|
1590  | 
raw output will appear on some system console with unpredictable  | 
|
1591  | 
interleaving of atomic chunks.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1592  | 
|
| 39868 | 1593  | 
Note that this does not affect regular message output channels  | 
1594  | 
  (\secref{sec:message-channels}).  An official message is associated
 | 
|
1595  | 
with the command transaction from where it originates, independently  | 
|
1596  | 
of other transactions. This means each running Isar command has  | 
|
1597  | 
effectively its own set of message channels, and interleaving can  | 
|
1598  | 
only happen when commands use parallelism internally (and only at  | 
|
1599  | 
message boundaries).  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1600  | 
|
| 39868 | 1601  | 
\item Treat environment variables and the current working directory  | 
1602  | 
of the running process as strictly read-only.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1603  | 
|
| 39868 | 1604  | 
\item Restrict writing to the file-system to unique temporary files.  | 
1605  | 
Isabelle already provides a temporary directory that is unique for  | 
|
1606  | 
the running process, and there is a centralized source of unique  | 
|
1607  | 
serial numbers in Isabelle/ML. Thus temporary files that are passed  | 
|
1608  | 
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
 | 
1609  | 
thread-safe.  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1610  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1611  | 
  \end{itemize}
 | 
| 39868 | 1612  | 
*}  | 
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1613  | 
|
| 39868 | 1614  | 
text %mlref {*
 | 
1615  | 
  \begin{mldecls}
 | 
|
1616  | 
  @{index_ML File.tmp_path: "Path.T -> Path.T"} \\
 | 
|
1617  | 
  @{index_ML serial_string: "unit -> string"} \\
 | 
|
1618  | 
  \end{mldecls}
 | 
|
1619  | 
||
1620  | 
  \begin{description}
 | 
|
1621  | 
||
1622  | 
  \item @{ML File.tmp_path}~@{text "path"} relocates the base
 | 
|
1623  | 
  component of @{text "path"} into the unique temporary directory of
 | 
|
1624  | 
the running Isabelle/ML process.  | 
|
1625  | 
||
1626  | 
  \item @{ML serial_string}~@{text "()"} creates a new serial number
 | 
|
1627  | 
that is unique over the runtime of the Isabelle/ML process.  | 
|
1628  | 
||
1629  | 
  \end{description}
 | 
|
1630  | 
*}  | 
|
1631  | 
||
1632  | 
text %mlex {* The following example shows how to create unique
 | 
|
1633  | 
temporary file names.  | 
|
1634  | 
*}  | 
|
1635  | 
||
1636  | 
ML {*
 | 
|
1637  | 
  val tmp1 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
 | 
|
1638  | 
  val tmp2 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
 | 
|
1639  | 
  @{assert} (tmp1 <> tmp2);
 | 
|
1640  | 
*}  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1641  | 
|
| 39868 | 1642  | 
|
1643  | 
subsection {* Explicit synchronization *}
 | 
|
1644  | 
||
1645  | 
text {* Isabelle/ML also provides some explicit synchronization
 | 
|
1646  | 
mechanisms, for the rare situations where mutable shared resources  | 
|
1647  | 
are really required. These are based on the synchronizations  | 
|
1648  | 
primitives of Poly/ML, which have been adapted to the specific  | 
|
1649  | 
assumptions of the concurrent Isabelle/ML environment. User code  | 
|
1650  | 
must not use the Poly/ML primitives directly!  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1651  | 
|
| 39868 | 1652  | 
\medskip The most basic synchronization concept is a single  | 
1653  | 
  \emph{critical section} (also called ``monitor'' in the literature).
 | 
|
1654  | 
A thread that enters the critical section prevents all other threads  | 
|
1655  | 
from doing the same. A thread that is already within the critical  | 
|
1656  | 
section may re-enter it in an idempotent manner.  | 
|
1657  | 
||
1658  | 
Such centralized locking is convenient, because it prevents  | 
|
1659  | 
deadlocks by construction.  | 
|
1660  | 
||
1661  | 
  \medskip More fine-grained locking works via \emph{synchronized
 | 
|
1662  | 
variables}. An explicit state component is associated with  | 
|
1663  | 
mechanisms for locking and signaling. There are operations to  | 
|
1664  | 
await a condition, change the state, and signal the change to all  | 
|
1665  | 
other waiting threads.  | 
|
1666  | 
||
1667  | 
  Here the synchronized access to the state variable is \emph{not}
 | 
|
1668  | 
re-entrant: direct or indirect nesting within the same thread will  | 
|
1669  | 
cause a deadlock!  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1670  | 
*}  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1671  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1672  | 
text %mlref {*
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1673  | 
  \begin{mldecls}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1674  | 
  @{index_ML NAMED_CRITICAL: "string -> (unit -> 'a) -> 'a"} \\
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1675  | 
  @{index_ML CRITICAL: "(unit -> 'a) -> 'a"} \\
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1676  | 
  \end{mldecls}
 | 
| 39871 | 1677  | 
  \begin{mldecls}
 | 
1678  | 
  @{index_ML_type "'a Synchronized.var"} \\
 | 
|
1679  | 
  @{index_ML Synchronized.var: "string -> 'a -> 'a Synchronized.var"} \\
 | 
|
1680  | 
  @{index_ML Synchronized.guarded_access: "'a Synchronized.var ->
 | 
|
1681  | 
  ('a -> ('b * 'a) option) -> 'b"} \\
 | 
|
1682  | 
  \end{mldecls}
 | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1683  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1684  | 
  \begin{description}
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1685  | 
|
| 39868 | 1686  | 
  \item @{ML NAMED_CRITICAL}~@{text "name e"} evaluates @{text "e ()"}
 | 
1687  | 
within the central critical section of Isabelle/ML. No other thread  | 
|
1688  | 
may do so at the same time, but non-critical parallel execution will  | 
|
| 39871 | 1689  | 
  continue.  The @{text "name"} argument is used for tracing and might
 | 
| 39868 | 1690  | 
help to spot sources of congestion.  | 
1691  | 
||
1692  | 
Entering the critical section without contention is very fast, and  | 
|
1693  | 
several basic system operations do so frequently. Each thread  | 
|
| 40126 | 1694  | 
should stay within the critical section quickly only very briefly,  | 
1695  | 
otherwise parallel performance may degrade.  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1696  | 
|
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1697  | 
  \item @{ML CRITICAL} is the same as @{ML NAMED_CRITICAL} with empty
 | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1698  | 
name argument.  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1699  | 
|
| 39871 | 1700  | 
  \item Type @{ML_type "'a Synchronized.var"} represents synchronized
 | 
1701  | 
  variables with state of type @{ML_type 'a}.
 | 
|
1702  | 
||
1703  | 
  \item @{ML Synchronized.var}~@{text "name x"} creates a synchronized
 | 
|
1704  | 
  variable that is initialized with value @{text "x"}.  The @{text
 | 
|
1705  | 
"name"} is used for tracing.  | 
|
1706  | 
||
1707  | 
  \item @{ML Synchronized.guarded_access}~@{text "var f"} lets the
 | 
|
1708  | 
  function @{text "f"} operate within a critical section on the state
 | 
|
| 40126 | 1709  | 
  @{text "x"} as follows: if @{text "f x"} produces @{ML NONE}, it
 | 
1710  | 
continues to wait on the internal condition variable, expecting that  | 
|
| 39871 | 1711  | 
some other thread will eventually change the content in a suitable  | 
| 40126 | 1712  | 
  manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is
 | 
1713  | 
  satisfied and assigns the new state value @{text "x'"}, broadcasts a
 | 
|
1714  | 
signal to all waiting threads on the associated condition variable,  | 
|
1715  | 
  and returns the result @{text "y"}.
 | 
|
| 39871 | 1716  | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1717  | 
  \end{description}
 | 
| 39871 | 1718  | 
|
| 40126 | 1719  | 
  There are some further variants of the @{ML
 | 
| 39871 | 1720  | 
  Synchronized.guarded_access} combinator, see @{"file"
 | 
1721  | 
"~~/src/Pure/Concurrent/synchronized.ML"} for details.  | 
|
1722  | 
*}  | 
|
1723  | 
||
| 40126 | 1724  | 
text %mlex {* The following example implements a counter that produces
 | 
| 39871 | 1725  | 
positive integers that are unique over the runtime of the Isabelle  | 
1726  | 
process:  | 
|
1727  | 
*}  | 
|
1728  | 
||
1729  | 
ML {*
 | 
|
1730  | 
local  | 
|
1731  | 
val counter = Synchronized.var "counter" 0;  | 
|
1732  | 
in  | 
|
1733  | 
fun next () =  | 
|
1734  | 
Synchronized.guarded_access counter  | 
|
1735  | 
(fn i =>  | 
|
1736  | 
let val j = i + 1  | 
|
1737  | 
in SOME (j, j) end);  | 
|
1738  | 
end;  | 
|
1739  | 
*}  | 
|
1740  | 
||
1741  | 
ML {*
 | 
|
1742  | 
val a = next ();  | 
|
1743  | 
val b = next ();  | 
|
1744  | 
  @{assert} (a <> b);
 | 
|
| 
39867
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1745  | 
*}  | 
| 
 
a8363532cd4d
somewhat modernized version of "Thread-safe programming";
 
wenzelm 
parents: 
39866 
diff
changeset
 | 
1746  | 
|
| 40126 | 1747  | 
text {* \medskip See @{"file" "~~/src/Pure/Concurrent/mailbox.ML"} how
 | 
1748  | 
to implement a mailbox as synchronized variable over a purely  | 
|
1749  | 
functional queue. *}  | 
|
1750  | 
||
| 
30270
 
61811c9224a6
dummy changes to produce a new changeset of these files;
 
wenzelm 
parents: 
29755 
diff
changeset
 | 
1751  | 
end  |