doc-src/IsarImplementation/Thy/ML.thy
author wenzelm
Fri, 22 Oct 2010 11:27:05 +0100
changeset 39879 be8a7334e4ec
parent 39878 31dd361a3060
child 39880 7deb6a741626
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29755
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29646
diff changeset
     1
theory "ML"
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29646
diff changeset
     2
imports Base
d66b34e46bdf observe usual theory naming conventions;
wenzelm
parents: 29646
diff changeset
     3
begin
18538
88fe84d4d151 outline;
wenzelm
parents: 18537
diff changeset
     4
39822
0de42180febe basic setup for Chapter 0: Isabelle/ML;
wenzelm
parents: 36164
diff changeset
     5
chapter {* Isabelle/ML *}
20489
a684fc70d04e tentative appendix C;
wenzelm
parents: 18554
diff changeset
     6
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
     7
text {* Isabelle/ML is best understood as a certain culture based on
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
     8
  Standard ML.  Thus it is not a new programming language, but a
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
     9
  certain way to use SML at an advanced level within the Isabelle
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    10
  environment.  This covers a variety of aspects that are geared
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    11
  towards an efficient and robust platform for applications of formal
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    12
  logic with fully foundational proof construction --- according to
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    13
  the well-known \emph{LCF principle}.  There are specific library
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    14
  modules and infrastructure to address the needs for such difficult
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    15
  tasks.  For example, the raw parallel programming model of Poly/ML
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    16
  is presented as considerably more abstract concept of \emph{future
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    17
  values}, which is then used to augment the inference kernel, proof
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    18
  interpreter, and theory loader accordingly.
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    19
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    20
  The main aspects of Isabelle/ML are introduced below.  These
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    21
  first-hand explanations should help to understand how proper
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    22
  Isabelle/ML is to be read and written, and to get access to the
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    23
  wealth of experience that is expressed in the source text and its
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    24
  history of changes.\footnote{See
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    25
  \url{http://isabelle.in.tum.de/repos/isabelle} for the full
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    26
  Mercurial history.  There are symbolic tags to refer to official
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    27
  Isabelle releases, as opposed to arbitrary \emph{tip} versions that
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    28
  merely reflect snapshots that are never really up-to-date.}  *}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    29
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
    30
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    31
section {* Style and orthography *}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    32
39879
wenzelm
parents: 39878
diff changeset
    33
text {* The sources of Isabelle/Isar are optimized for
wenzelm
parents: 39878
diff changeset
    34
  \emph{readability} and \emph{maintainability}.  The main purpose is
wenzelm
parents: 39878
diff changeset
    35
  to tell an informed reader what is really going on and how things
wenzelm
parents: 39878
diff changeset
    36
  really work.  This is a non-trivial aim, but it is supported by a
wenzelm
parents: 39878
diff changeset
    37
  certain style of writing Isabelle/ML that has emerged from long
wenzelm
parents: 39878
diff changeset
    38
  years of system development.\footnote{See also the interesting style
wenzelm
parents: 39878
diff changeset
    39
  guide for OCaml
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    40
  \url{http://caml.inria.fr/resources/doc/guides/guidelines.en.html}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    41
  which shares many of our means and ends.}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    42
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    43
  The main principle behind any coding style is \emph{consistency}.
39879
wenzelm
parents: 39878
diff changeset
    44
  For a single author of a small program this merely means ``choose
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    45
  your style and stick to it''.  A complex project like Isabelle, with
39879
wenzelm
parents: 39878
diff changeset
    46
  long years of development and different contributors, requires more
wenzelm
parents: 39878
diff changeset
    47
  standardization.  A coding style that is changed every few years or
wenzelm
parents: 39878
diff changeset
    48
  with every new contributor is no style at all, because consistency
wenzelm
parents: 39878
diff changeset
    49
  is quickly lost.  Global consistency is hard to achieve, though.
wenzelm
parents: 39878
diff changeset
    50
  One should always strife at least for local consistency of modules
wenzelm
parents: 39878
diff changeset
    51
  and sub-systems, without deviating from some general principles how
wenzelm
parents: 39878
diff changeset
    52
  to write Isabelle/ML.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    53
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    54
  In a sense, a common coding style is like an \emph{orthography} for
39879
wenzelm
parents: 39878
diff changeset
    55
  the sources: it helps to read quickly over the text and see through
wenzelm
parents: 39878
diff changeset
    56
  the main points, without getting distracted by accidental
wenzelm
parents: 39878
diff changeset
    57
  presentation of free-style source.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    58
*}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    59
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    60
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    61
subsection {* Header and sectioning *}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    62
39879
wenzelm
parents: 39878
diff changeset
    63
text {* Isabelle source files have a certain standardized header
wenzelm
parents: 39878
diff changeset
    64
  format (with precise spacing) that follows ancient traditions
wenzelm
parents: 39878
diff changeset
    65
  reaching back to the earliest versions of the system by Larry
wenzelm
parents: 39878
diff changeset
    66
  Paulson.  E.g.\ see @{"file" "~~/src/Pure/thm.ML"}.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    67
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    68
  The header includes at least @{verbatim Title} and @{verbatim
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    69
  Author} entries, followed by a prose description of the purpose of
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    70
  the module.  The latter can range from a single line to several
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    71
  paragraphs of explanations.
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    72
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    73
  The rest of the file is divided into sections, subsections,
39879
wenzelm
parents: 39878
diff changeset
    74
  subsubsections, paragraphs etc.\ using some a layout via ML comments
wenzelm
parents: 39878
diff changeset
    75
  as follows.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    76
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    77
\begin{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    78
(*** section ***)
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    79
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    80
(** subsection **)
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    81
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    82
(* subsubsection *)
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    83
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    84
(*short paragraph*)
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    85
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    86
(*
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    87
  long paragraph
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    88
  more text
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    89
*)
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    90
\end{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    91
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    92
  As in regular typography, there is some extra space \emph{before}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    93
  section headings that are adjacent to plain text (not other headings
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    94
  as in the example above).
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    95
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
    96
  \medskip The precise wording of the prose text given in these
39879
wenzelm
parents: 39878
diff changeset
    97
  headings is chosen carefully to indicate the main theme of the
wenzelm
parents: 39878
diff changeset
    98
  subsequent formal ML text.
wenzelm
parents: 39878
diff changeset
    99
*}
wenzelm
parents: 39878
diff changeset
   100
wenzelm
parents: 39878
diff changeset
   101
wenzelm
parents: 39878
diff changeset
   102
subsection {* Basic naming conventions *}
wenzelm
parents: 39878
diff changeset
   103
wenzelm
parents: 39878
diff changeset
   104
text {* Since ML is the primary medium to express the meaning of the
wenzelm
parents: 39878
diff changeset
   105
  source text, naming of ML entities requires special care.
wenzelm
parents: 39878
diff changeset
   106
wenzelm
parents: 39878
diff changeset
   107
  FIXME
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   108
*}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   109
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   110
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   111
subsection {* General source layout *}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   112
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   113
text {* The general Isabelle/ML source layout imitates regular
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   114
  type-setting to some extent, augmented by the requirements for
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   115
  deeply nested expressions that are commonplace in functional
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   116
  programming.
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   117
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   118
  \paragraph{Line length} is 80 characters according to ancient
39879
wenzelm
parents: 39878
diff changeset
   119
  standards, but we allow as much as 100 characters, not more.  This
wenzelm
parents: 39878
diff changeset
   120
  acknowledges extra the space requirements due to qualified library
wenzelm
parents: 39878
diff changeset
   121
  references in Isabelle/ML.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   122
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   123
  \paragraph{White-space} is used to emphasize the structure of
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   124
  expressions, following mostly standard conventions for mathematical
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   125
  typesetting, as can be seen in plain {\TeX} or {\LaTeX}.  This
39879
wenzelm
parents: 39878
diff changeset
   126
  defines positioning of spaces for parentheses, punctuation, and
wenzelm
parents: 39878
diff changeset
   127
  infixes as illustrated here:
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   128
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   129
  \begin{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   130
  val x = y + z * (a + b);
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   131
  val pair = (a, b);
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   132
  val record = {foo = 1, bar = 2};
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   133
  \end{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   134
39879
wenzelm
parents: 39878
diff changeset
   135
  Lines are normally broken \emph{after} an infix operator or
wenzelm
parents: 39878
diff changeset
   136
  punctuation character.  For example:
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   137
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   138
  \begin{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   139
  val x =
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   140
    a +
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   141
    b +
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   142
    c;
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   143
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   144
  val tuple =
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   145
   (a,
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   146
    b,
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   147
    c);
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   148
  \end{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   149
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   150
  Some special infixes (e.g.\ @{verbatim "|>"}) work better at the
39879
wenzelm
parents: 39878
diff changeset
   151
  start of the line, but punctuation is always at the end.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   152
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   153
  Function application follows the tradition of @{text "\<lambda>"}-calculus,
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   154
  not informal mathematics.  For example: @{verbatim "f a b"} for a
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   155
  curried function, or @{verbatim "g (a, b)"} for a tupled function.
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   156
  Note that the space between @{verbatim g} and the pair @{verbatim
39879
wenzelm
parents: 39878
diff changeset
   157
  "(a, b)"} follows the important principle of
wenzelm
parents: 39878
diff changeset
   158
  \emph{compositionality}: the layout of @{verbatim "g p"} does not
wenzelm
parents: 39878
diff changeset
   159
  change when @{verbatim "p"} is refined to a concrete pair.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   160
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   161
  \paragraph{Indentation} uses plain spaces, never hard
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   162
  tabulators.\footnote{Tabulators were invented to move the carriage
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   163
  of a type-writer to certain predefined positions.  In software they
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   164
  could be used as a primitive run-length compression of consecutive
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   165
  spaces, but the precise result would depend on non-standardized
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   166
  editor configuration.}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   167
39879
wenzelm
parents: 39878
diff changeset
   168
  Each level of nesting is indented by 2 spaces, sometimes 1, very
wenzelm
parents: 39878
diff changeset
   169
  rarely 4, never 8.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   170
39879
wenzelm
parents: 39878
diff changeset
   171
  Indentation follows a simple logical format that only depends on the
wenzelm
parents: 39878
diff changeset
   172
  nesting depth, not the accidental length of the text that initiates
wenzelm
parents: 39878
diff changeset
   173
  a level of nesting.  Example:
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   174
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   175
  \begin{verbatim}
39879
wenzelm
parents: 39878
diff changeset
   176
  (*RIGHT*)
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   177
  if b then
39879
wenzelm
parents: 39878
diff changeset
   178
    expr1_part1
wenzelm
parents: 39878
diff changeset
   179
    expr1_part2
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   180
  else
39879
wenzelm
parents: 39878
diff changeset
   181
    expr2_part1
wenzelm
parents: 39878
diff changeset
   182
    expr2_part2
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   183
39879
wenzelm
parents: 39878
diff changeset
   184
  (*WRONG*)
wenzelm
parents: 39878
diff changeset
   185
  if b then expr1_part1
wenzelm
parents: 39878
diff changeset
   186
            expr1_part2
wenzelm
parents: 39878
diff changeset
   187
  else expr2_part1
wenzelm
parents: 39878
diff changeset
   188
       expr2_part2
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   189
  \end{verbatim}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   190
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   191
  The second form has many problems: it assumes a fixed-width font
39879
wenzelm
parents: 39878
diff changeset
   192
  when viewing the sources, it uses more space on the line and thus
wenzelm
parents: 39878
diff changeset
   193
  makes it hard to observe its strict length limit (working against
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   194
  \emph{readability}), it requires extra editing to adapt the layout
39879
wenzelm
parents: 39878
diff changeset
   195
  to changes of the initial text (working against
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   196
  \emph{maintainability}) etc.
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   197
39879
wenzelm
parents: 39878
diff changeset
   198
  \medskip For similar reasons, any kind of two-dimensional or tabular
wenzelm
parents: 39878
diff changeset
   199
  layouts, ASCII-art with lines or boxes of stars etc. should be
wenzelm
parents: 39878
diff changeset
   200
  avoided.
39878
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   201
*}
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   202
31dd361a3060 more on "Style and orthography";
wenzelm
parents: 39875
diff changeset
   203
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   204
section {* SML embedded into Isabelle/Isar *}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   205
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   206
text {* ML and Isar are intertwined via an open-ended bootstrap
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   207
  process that provides more and more programming facilities and
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   208
  logical content in an alternating manner.  Bootstrapping starts from
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   209
  the raw environment of existing implementations of Standard ML
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   210
  (mainly Poly/ML, but also SML/NJ).
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   211
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   212
  Isabelle/Pure marks the point where the original ML toplevel is
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   213
  superseded by the Isar toplevel that maintains a uniform environment
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   214
  for arbitrary ML values (see also \secref{sec:context}).  This
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   215
  formal context holds logical entities as well as ML compiler
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   216
  bindings, among many other things.  Raw Standard ML is never
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   217
  encountered again after the initial bootstrap of Isabelle/Pure.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   218
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   219
  Object-logics such as Isabelle/HOL are built within the
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   220
  Isabelle/ML/Isar environment of Pure by introducing suitable
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   221
  theories with associated ML text, either inlined or as separate
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   222
  files.  Thus Isabelle/HOL is defined as a regular user-space
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   223
  application within the Isabelle framework.  Further add-on tools can
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   224
  be implemented in ML within the Isar context in the same manner: ML
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   225
  is part of the regular user-space repertoire of Isabelle.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   226
*}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   227
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   228
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   229
subsection {* Isar ML commands *}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   230
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   231
text {* The primary Isar source language provides various facilities
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   232
  to open a ``window'' to the underlying ML compiler.  Especially see
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   233
  @{command_ref "use"} and @{command_ref "ML"}, which work exactly the
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   234
  same way, only the source text is provided via a file vs.\ inlined,
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   235
  respectively.  Apart from embedding ML into the main theory
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   236
  definition like that, there are many more commands that refer to ML
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   237
  source, such as @{command_ref setup} or @{command_ref declaration}.
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   238
  An example of even more fine-grained embedding of ML into Isar is
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   239
  the proof method @{method_ref tactic}, which refines the pending goal state
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   240
  via a given expression of type @{ML_type tactic}.
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   241
*}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   242
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   243
text %mlex {* The following artificial example demonstrates some ML
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   244
  toplevel declarations within the implicit Isar theory context.  This
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   245
  is regular functional programming without referring to logical
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   246
  entities yet.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   247
*}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   248
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   249
ML {*
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   250
  fun factorial 0 = 1
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   251
    | factorial n = n * factorial (n - 1)
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   252
*}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   253
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   254
text {* Here the ML environment is really managed by Isabelle, i.e.\
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   255
  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
   256
  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
   257
  current one in the import hierarchy.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   258
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   259
  Removing the above ML declaration from the source text will remove
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   260
  any trace of this definition as expected.  The Isabelle/ML toplevel
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   261
  environment is managed in a \emph{stateless} way: unlike the raw ML
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   262
  toplevel or similar command loops of Computer Algebra systems, there
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   263
  are no global side-effects involved here.\footnote{Such a stateless
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   264
  compilation environment is also a prerequisite for robust parallel
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   265
  compilation within independent nodes of the implicit theory
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   266
  development graph.}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   267
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   268
  \medskip The next example shows how to embed ML into Isar proofs.
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   269
  Instead of @{command_ref "ML"} for theory mode, we use @{command_ref
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   270
  "ML_prf"} for proof mode.  As illustrated below, its effect on the
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   271
  ML environment is local to the whole proof body, while ignoring its
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   272
  internal block structure.  *}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   273
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   274
example_proof
39851
7219a771ab63 more examples;
wenzelm
parents: 39850
diff changeset
   275
  ML_prf %"ML" {* val a = 1 *}
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   276
  { -- {* Isar block structure ignored by ML environment *}
39851
7219a771ab63 more examples;
wenzelm
parents: 39850
diff changeset
   277
    ML_prf %"ML" {* val b = a + 1 *}
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   278
  } -- {* Isar block structure ignored by ML environment *}
39851
7219a771ab63 more examples;
wenzelm
parents: 39850
diff changeset
   279
  ML_prf %"ML" {* val c = b + 1 *}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   280
qed
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   281
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   282
text {* By side-stepping the normal scoping rules for Isar proof
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   283
  blocks, embedded ML code can refer to the different contexts
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   284
  explicitly, and manipulate corresponding entities, e.g.\ export a
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   285
  fact from a block context.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   286
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   287
  \medskip Two further ML commands are useful in certain situations:
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   288
  @{command_ref ML_val} and @{command_ref ML_command} are both
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   289
  \emph{diagnostic} in the sense that there is no effect on the
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   290
  underlying environment, and can thus used anywhere (even outside a
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   291
  theory).  The examples below produce long strings of digits by
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   292
  invoking @{ML factorial}: @{command ML_val} already takes care of
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   293
  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
   294
  so we produce an explicit output message.  *}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   295
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   296
ML_val {* factorial 100 *}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   297
ML_command {* writeln (string_of_int (factorial 100)) *}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   298
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   299
example_proof
39824
679075565542 misc tuning;
wenzelm
parents: 39823
diff changeset
   300
  ML_val {* factorial 100 *}  (* FIXME check/fix indentation *)
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   301
  ML_command {* writeln (string_of_int (factorial 100)) *}
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   302
qed
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   303
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   304
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   305
subsection {* Compile-time context *}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   306
39825
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   307
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
   308
  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
   309
  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
   310
  or writing the (local) theory under construction.  Note that such
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   311
  direct access to the compile-time context is rare; in practice it is
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   312
  typically via some derived ML functions.
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   313
*}
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   314
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   315
text %mlref {*
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   316
  \begin{mldecls}
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   317
  @{index_ML ML_Context.the_generic_context: "unit -> Context.generic"} \\
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   318
  @{index_ML "Context.>> ": "(Context.generic -> Context.generic) -> unit"} \\
39850
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   319
  @{index_ML bind_thms: "string * thm list -> unit"} \\
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   320
  @{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
   321
  \end{mldecls}
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   322
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   323
  \begin{description}
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   324
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   325
  \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
   326
  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
   327
  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
   328
  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
   329
  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
   330
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   331
  \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
   332
  @{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
   333
39850
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   334
  \item @{ML bind_thms}~@{text "(name, thms)"} stores a list of
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   335
  theorems produced in ML both in the (global) theory context and the
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   336
  ML toplevel, associating it with the provided name.  Theorems are
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   337
  put into a global ``standard'' format before being stored.
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   338
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   339
  \item @{ML bind_thm} is similar to @{ML bind_thms} but refers to a
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   340
  singleton theorem.
f4c614ece7ed moved bind_thm(s) to implementation manual;
wenzelm
parents: 39835
diff changeset
   341
39825
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   342
  \end{description}
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   343
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   344
  It is very important to note that the above functions are really
f9066b94bf07 eliminated fancy \ML logo for the sake of simpler source text (less dependence on LaTeX);
wenzelm
parents: 39824
diff changeset
   345
  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
   346
  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
   347
  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
   348
  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
   349
*}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   350
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   351
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   352
subsection {* Antiquotations \label{sec:ML-antiq} *}
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   353
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   354
text {* A very important consequence of embedding SML into Isar is the
39829
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   355
  concept of \emph{ML antiquotation}.  First, the standard token
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   356
  language of ML is augmented by special syntactic entities of the
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   357
  following form:
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   358
39829
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   359
  \indexouternonterm{antiquote}
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   360
  \begin{rail}
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   361
  antiquote: atsign lbrace nameref args rbrace | lbracesym | rbracesym
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   362
  ;
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   363
  \end{rail}
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   364
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   365
  Here @{syntax nameref} and @{syntax args} are regular outer syntax
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   366
  categories.  Note that attributes and proof methods use similar
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   367
  syntax.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   368
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   369
  \medskip A regular antiquotation @{text "@{name args}"} processes
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   370
  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
   371
  produces corresponding ML source text, either as literal
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   372
  \emph{inline} text (e.g. @{text "@{term t}"}) or abstract
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   373
  \emph{value} (e.g. @{text "@{thm th}"}).  This pre-compilation
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   374
  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
   375
  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
   376
  small portions of the code.
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   377
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   378
  Special antiquotations like @{text "@{let \<dots>}"} or @{text "@{note
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   379
  \<dots>}"} augment the compilation context without generating code.  The
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   380
  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
   381
  effect by introducing local blocks within the pre-compilation
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   382
  environment.
39829
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   383
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   384
  \medskip See also \cite{Wenzel-Chaieb:2007b} for a slightly broader
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   385
  perspective on Isabelle/ML antiquotations.  *}
39829
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   386
39830
7c501d7f1e45 clarified tag markup;
wenzelm
parents: 39829
diff changeset
   387
text %mlantiq {*
39829
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   388
  \begin{matharray}{rcl}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   389
  @{ML_antiquotation_def "let"} & : & @{text ML_antiquotation} \\
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   390
  @{ML_antiquotation_def "note"} & : & @{text ML_antiquotation} \\
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   391
  \end{matharray}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   392
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   393
  \begin{rail}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   394
  'let' ((term + 'and') '=' term + 'and')
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   395
  ;
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   396
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   397
  'note' (thmdef? thmrefs + 'and')
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   398
  ;
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   399
  \end{rail}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   400
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   401
  \begin{description}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   402
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   403
  \item @{text "@{let p = t}"} binds schematic variables in the
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   404
  pattern @{text "p"} by higher-order matching against the term @{text
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   405
  "t"}.  This is analogous to the regular @{command_ref let} command
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   406
  in the Isar proof language.  The pre-compilation environment is
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   407
  augmented by auxiliary term bindings, without emitting ML source.
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   408
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   409
  \item @{text "@{note a = b\<^sub>1 \<dots> b\<^sub>n}"} recalls existing facts @{text
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   410
  "b\<^sub>1, \<dots>, b\<^sub>n"}, binding the result as @{text a}.  This is analogous to
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   411
  the regular @{command_ref note} command in the Isar proof language.
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   412
  The pre-compilation environment is augmented by auxiliary fact
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   413
  bindings, without emitting ML source.
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   414
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   415
  \end{description}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   416
*}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   417
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   418
text %mlex {* The following artificial example gives a first
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   419
  impression about using the antiquotation elements introduced so far,
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   420
  together with the basic @{text "@{thm}"} antiquotation defined
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   421
  later.
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   422
*}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   423
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   424
ML {*
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   425
  \<lbrace>
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   426
    @{let ?t = my_term}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   427
    @{note my_refl = reflexive [of ?t]}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   428
    fun foo th = Thm.transitive th @{thm my_refl}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   429
  \<rbrace>
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   430
*}
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   431
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   432
text {*
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   433
  The extra block delimiters do not affect the compiled code itself, i.e.\
f5ea50decd9f more on ML antiquotations;
wenzelm
parents: 39827
diff changeset
   434
  function @{ML foo} is available in the present context.
39827
d829ce302ca4 basic setup for ML antiquotations -- with rail diagrams;
wenzelm
parents: 39825
diff changeset
   435
*}
39823
61e7935a6858 more on Isabelle/ML;
wenzelm
parents: 39822
diff changeset
   436
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   437
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   438
section {* Message output channels \label{sec:message-channels} *}
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   439
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   440
text {* Isabelle provides output channels for different kinds of
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   441
  messages: regular output, high-volume tracing information, warnings,
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   442
  and errors.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   443
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   444
  Depending on the user interface involved, these messages may appear
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   445
  in different text styles or colours.  The standard output for
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   446
  terminal sessions prefixes each line of warnings by @{verbatim
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   447
  "###"} and errors by @{verbatim "***"}, but leaves anything else
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   448
  unchanged.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   449
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   450
  Messages are associated with the transaction context of the running
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   451
  Isar command.  This enables the front-end to manage commands and
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   452
  resulting messages together.  For example, after deleting a command
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   453
  from a given theory document version, the corresponding message
39872
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   454
  output can be retracted from the display.
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   455
*}
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   456
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   457
text %mlref {*
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   458
  \begin{mldecls}
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   459
  @{index_ML writeln: "string -> unit"} \\
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   460
  @{index_ML tracing: "string -> unit"} \\
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   461
  @{index_ML warning: "string -> unit"} \\
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   462
  @{index_ML error: "string -> 'a"} \\
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   463
  \end{mldecls}
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   464
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   465
  \begin{description}
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   466
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   467
  \item @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   468
  message.  This is the primary message output operation of Isabelle
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   469
  and should be used by default.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   470
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   471
  \item @{ML tracing}~@{text "text"} outputs @{text "text"} as special
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   472
  tracing message, indicating potential high-volume output to the
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   473
  front-end (hundreds or thousands of messages issued by a single
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   474
  command).  The idea is to allow the user-interface to downgrade the
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   475
  quality of message display to achieve higher throughput.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   476
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   477
  Note that the user might have to take special actions to see tracing
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   478
  output, e.g.\ switch to a different output window.  So this channel
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   479
  should not be used for regular output.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   480
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   481
  \item @{ML warning}~@{text "text"} outputs @{text "text"} as
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   482
  warning, which typically means some extra emphasis on the front-end
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   483
  side (color highlighting, icon).
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   484
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   485
  \item @{ML error}~@{text "text"} raises exception @{ML ERROR}~@{text
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   486
  "text"} and thus lets the Isar toplevel print @{text "text"} on the
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   487
  error channel, which typically means some extra emphasis on the
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   488
  front-end side (color highlighting, icon).
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   489
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   490
  This assumes that the exception is not handled before the command
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   491
  terminates.  Handling exception @{ML ERROR}~@{text "text"} is a
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   492
  perfectly legal alternative: it means that the error is absorbed
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   493
  without any message output.
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   494
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   495
  \begin{warn}
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   496
  The actual error channel is accessed via @{ML Output.error_msg}, but
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   497
  the interaction protocol of Proof~General \emph{crashes} if that
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   498
  function is used in regular ML code: error output and toplevel
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   499
  command failure always need to coincide here.
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   500
  \end{warn}
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   501
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   502
  \end{description}
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   503
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   504
  \begin{warn}
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   505
  Regular Isabelle/ML code should output messages exclusively by the
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   506
  official channels.  Using raw I/O on \emph{stdout} or \emph{stderr}
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   507
  instead (e.g.\ via @{ML TextIO.output}) is apt to cause problems in
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   508
  the presence of parallel and asynchronous processing of Isabelle
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   509
  theories.  Such raw output might be displayed by the front-end in
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   510
  some system console log, with a low chance that the user will ever
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   511
  see it.  Moreover, as a genuine side-effect on global process
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   512
  channels, there is no proper way to retract output when Isar command
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   513
  transactions are reset.
39861
b8d89db3e238 use continental paragraph style, which works better with mixture of (in)formal text;
wenzelm
parents: 39859
diff changeset
   514
  \end{warn}
39872
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   515
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   516
  \begin{warn}
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   517
  The message channels should be used in a message-oriented manner.
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   518
  This means that multi-line output that logically belongs together
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   519
  needs to be issued by a \emph{single} invocation of @{ML writeln}
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   520
  etc.  with the functional concatenation of all message constituents.
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   521
  \end{warn}
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   522
*}
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   523
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   524
text %mlex {* The following example demonstrates a multi-line
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   525
  warning.  Note that in some situations the user sees only the first
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   526
  line, so the most important point should be made first.
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   527
*}
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   528
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   529
ML_command {*
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   530
  warning (cat_lines
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   531
   ["Beware the Jabberwock, my son!",
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   532
    "The jaws that bite, the claws that catch!",
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   533
    "Beware the Jubjub Bird, and shun",
2b88d00d6790 more on messages;
wenzelm
parents: 39871
diff changeset
   534
    "The frumious Bandersnatch!"]);
39835
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   535
*}
6cefd96bb71d modernized version of "Message output channels";
wenzelm
parents: 39830
diff changeset
   536
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   537
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   538
section {* Exceptions \label{sec:exceptions} *}
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   539
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   540
text {* The Standard ML semantics of strict functional evaluation
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   541
  together with exceptions is rather well defined, but some delicate
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   542
  points need to be observed to avoid that ML programs go wrong
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   543
  despite static type-checking.  Exceptions in Isabelle/ML are
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   544
  subsequently categorized as follows.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   545
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   546
  \paragraph{Regular user errors.}  These are meant to provide
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   547
  informative feedback about malformed input etc.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   548
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   549
  The \emph{error} function raises the corresponding \emph{ERROR}
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   550
  exception, with a plain text message as argument.  \emph{ERROR}
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   551
  exceptions can be handled internally, in order to be ignored, turned
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   552
  into other exceptions, or cascaded by appending messages.  If the
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   553
  corresponding Isabelle/Isar command terminates with an \emph{ERROR}
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   554
  exception state, the toplevel will print the result on the error
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   555
  channel (see \secref{sec:message-channels}).
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   556
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   557
  It is considered bad style to refer to internal function names or
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   558
  values in ML source notation in user error messages.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   559
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   560
  Grammatical correctness of error messages can be improved by
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   561
  \emph{omitting} final punctuation: messages are often concatenated
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   562
  or put into a larger context (e.g.\ augmented with source position).
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   563
  By not insisting in the final word at the origin of the error, the
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   564
  system can perform its administrative tasks more easily and
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   565
  robustly.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   566
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   567
  \paragraph{Program failures.}  There is a handful of standard
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   568
  exceptions that indicate general failure situations, or failures of
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   569
  core operations on logical entities (types, terms, theorems,
39856
wenzelm
parents: 39855
diff changeset
   570
  theories, see \chref{ch:logic}).
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   571
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   572
  These exceptions indicate a genuine breakdown of the program, so the
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   573
  main purpose is to determine quickly what has happened where.
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   574
  Traditionally, the (short) exception message would include the name
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   575
  of an ML function, although this no longer necessary, because the ML
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   576
  runtime system prints a detailed source position of the
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   577
  corresponding @{verbatim raise} keyword.
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   578
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   579
  \medskip User modules can always introduce their own custom
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   580
  exceptions locally, e.g.\ to organize internal failures robustly
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   581
  without overlapping with existing exceptions.  Exceptions that are
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   582
  exposed in module signatures require extra care, though, and should
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   583
  \emph{not} be introduced by default.  Surprise by end-users or ML
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   584
  users of a module can be often minimized by using plain user errors.
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   585
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   586
  \paragraph{Interrupts.}  These indicate arbitrary system events:
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   587
  both the ML runtime system and the Isabelle/ML infrastructure signal
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   588
  various exceptional situations by raising the special
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   589
  \emph{Interrupt} exception in user code.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   590
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   591
  This is the one and only way that physical events can intrude an
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   592
  Isabelle/ML program.  Such an interrupt can mean out-of-memory,
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   593
  stack overflow, timeout, internal signaling of threads, or the user
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   594
  producing a console interrupt manually etc.  An Isabelle/ML program
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   595
  that intercepts interrupts becomes dependent on physical effects of
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   596
  the environment.  Even worse, exception handling patterns that are
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   597
  too general by accident, e.g.\ by mispelled exception constructors,
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   598
  will cover interrupts unintentionally, and thus render the program
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   599
  semantics ill-defined.
39854
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   600
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   601
  Note that the Interrupt exception dates back to the original SML90
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   602
  language definition.  It was excluded from the SML97 version to
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   603
  avoid its malign impact on ML program semantics, but without
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   604
  providing a viable alternative.  Isabelle/ML recovers physical
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   605
  interruptibility (which an indispensable tool to implement managed
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   606
  evaluation of Isar command transactions), but requires user code to
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   607
  be strictly transparent wrt.\ interrupts.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   608
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   609
  \begin{warn}
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   610
  Isabelle/ML user code needs to terminate promptly on interruption,
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   611
  without guessing at its meaning to the system infrastructure.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   612
  Temporary handling of interrupts for cleanup of global resources
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   613
  etc.\ needs to be followed immediately by re-raising of the original
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   614
  exception.
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   615
  \end{warn}
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   616
*}
7480a7a159cb more on "Exceptions";
wenzelm
parents: 39851
diff changeset
   617
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   618
text %mlref {*
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   619
  \begin{mldecls}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   620
  @{index_ML try: "('a -> 'b) -> 'a -> 'b option"} \\
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   621
  @{index_ML can: "('a -> 'b) -> 'a -> bool"} \\
39856
wenzelm
parents: 39855
diff changeset
   622
  @{index_ML ERROR: "string -> exn"} \\
wenzelm
parents: 39855
diff changeset
   623
  @{index_ML Fail: "string -> exn"} \\
wenzelm
parents: 39855
diff changeset
   624
  @{index_ML Exn.is_interrupt: "exn -> bool"} \\
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   625
  @{index_ML reraise: "exn -> 'a"} \\
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   626
  @{index_ML exception_trace: "(unit -> 'a) -> 'a"} \\
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   627
  \end{mldecls}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   628
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   629
  \begin{description}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   630
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   631
  \item @{ML try}~@{text "f x"} makes the partiality of evaluating
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   632
  @{text "f x"} explicit via the option datatype.  Interrupts are
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   633
  \emph{not} handled here, i.e.\ this form serves as safe replacement
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   634
  for the \emph{unsafe} version @{verbatim "(SOME"}~@{text "f
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   635
  x"}~@{verbatim "handle _ => NONE)"} that is occasionally seen in
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   636
  books about SML.
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   637
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   638
  \item @{ML can} is similar to @{ML try} with more abstract result.
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   639
39856
wenzelm
parents: 39855
diff changeset
   640
  \item @{ML ERROR}~@{text "msg"} represents user errors; this
wenzelm
parents: 39855
diff changeset
   641
  exception is always raised via the @{ML error} function (see
wenzelm
parents: 39855
diff changeset
   642
  \secref{sec:message-channels}).
wenzelm
parents: 39855
diff changeset
   643
wenzelm
parents: 39855
diff changeset
   644
  \item @{ML Fail}~@{text "msg"} represents general program failures.
wenzelm
parents: 39855
diff changeset
   645
wenzelm
parents: 39855
diff changeset
   646
  \item @{ML Exn.is_interrupt} identifies interrupts robustly, without
wenzelm
parents: 39855
diff changeset
   647
  mentioning concrete exception constructors in user code.  Handled
wenzelm
parents: 39855
diff changeset
   648
  interrupts need to be re-raised promptly!
wenzelm
parents: 39855
diff changeset
   649
39855
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   650
  \item @{ML reraise}~@{text "exn"} raises exception @{text "exn"}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   651
  while preserving its implicit position information (if possible,
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   652
  depending on the ML platform).
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   653
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   654
  \item @{ML exception_trace}~@{verbatim "(fn _ =>"}~@{text
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   655
  "e"}@{verbatim ")"} evaluates expression @{text "e"} while printing
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   656
  a full trace of its stack of nested exceptions (if possible,
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   657
  depending on the ML platform).\footnote{In various versions of
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   658
  Poly/ML the trace will appear on raw stdout of the Isabelle
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   659
  process.}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   660
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   661
  Inserting @{ML exception_trace} into ML code temporarily is useful
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   662
  for debugging, but not suitable for production code.
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   663
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   664
  \end{description}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   665
*}
d4299b415879 more on "Exceptions";
wenzelm
parents: 39854
diff changeset
   666
39866
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   667
text %mlantiq {*
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   668
  \begin{matharray}{rcl}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   669
  @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   670
  \end{matharray}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   671
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   672
  \begin{description}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   673
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   674
  \item @{text "@{assert}"} inlines a function @{text "bool \<Rightarrow> unit"}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   675
  that raises @{ML Fail} if the argument is @{ML false}.  Due to
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   676
  inlining the source position of failed assertions is included in the
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   677
  error output.
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   678
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   679
  \end{description}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   680
*}
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   681
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   682
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   683
section {* Basic data types *}
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   684
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   685
text {* The basis library proposal of SML97 need to be treated with
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   686
  caution.  Many of its operations simply do not fit with important
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   687
  Isabelle/ML conventions (like ``canonical argument order'', see
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   688
  \secref{sec:canonical-argument-order}), others can cause serious
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   689
  problems with the parallel evaluation model of Isabelle/ML (such as
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   690
  @{ML TextIO.print} or @{ML OS.Process.system}).
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   691
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   692
  Subsequently we give a brief overview of important operations on
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   693
  basic ML data types.
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   694
*}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   695
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   696
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   697
subsection {* Characters *}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   698
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   699
text %mlref {*
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   700
  \begin{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   701
  @{index_ML_type char} \\
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   702
  \end{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   703
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   704
  \begin{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   705
39864
wenzelm
parents: 39863
diff changeset
   706
  \item Type @{ML_type char} is \emph{not} used.  The smallest textual
wenzelm
parents: 39863
diff changeset
   707
  unit in Isabelle is represented a ``symbol'' (see
wenzelm
parents: 39863
diff changeset
   708
  \secref{sec:symbols}).
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   709
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   710
  \end{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   711
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   712
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   713
39862
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   714
subsection {* Integers *}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   715
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   716
text %mlref {*
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   717
  \begin{mldecls}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   718
  @{index_ML_type int} \\
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   719
  \end{mldecls}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   720
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   721
  \begin{description}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   722
39864
wenzelm
parents: 39863
diff changeset
   723
  \item Type @{ML_type int} represents regular mathematical integers,
wenzelm
parents: 39863
diff changeset
   724
  which are \emph{unbounded}.  Overflow never happens in
39862
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   725
  practice.\footnote{The size limit for integer bit patterns in memory
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   726
  is 64\,MB for 32-bit Poly/ML, and much higher for 64-bit systems.}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   727
  This works uniformly for all supported ML platforms (Poly/ML and
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   728
  SML/NJ).
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   729
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   730
  Literal integers in ML text (e.g.\ @{ML
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   731
  123456789012345678901234567890}) are forced to be of this one true
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   732
  integer type --- overloading of SML97 is disabled.
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   733
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   734
  Structure @{ML_struct IntInf} of SML97 is obsolete, always use
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   735
  @{ML_struct Int}.  Structure @{ML_struct Integer} in @{"file"
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   736
  "~~/src/Pure/General/integer.ML"} provides some additional
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   737
  operations.
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   738
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   739
  \end{description}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   740
*}
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   741
78e6ec83762a more on "Integers";
wenzelm
parents: 39861
diff changeset
   742
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   743
subsection {* Options *}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   744
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   745
text %mlref {*
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   746
  \begin{mldecls}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   747
  @{index_ML Option.map: "('a -> 'b) -> 'a option -> 'b option"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   748
  @{index_ML is_some: "'a option -> bool"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   749
  @{index_ML is_none: "'a option -> bool"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   750
  @{index_ML the: "'a option -> 'a"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   751
  @{index_ML these: "'a list option -> 'a list"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   752
  @{index_ML the_list: "'a option -> 'a list"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   753
  @{index_ML the_default: "'a -> 'a option -> 'a"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   754
  \end{mldecls}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   755
*}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   756
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   757
text {* Apart from @{ML Option.map} most operations defined in
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   758
  structure @{ML_struct Option} are alien to Isabelle/ML.  The
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   759
  operations shown above are defined in @{"file"
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   760
  "~~/src/Pure/General/basics.ML"}, among others.  *}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   761
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   762
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   763
subsection {* Lists *}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   764
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   765
text {* Lists are ubiquitous in ML as simple and light-weight
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   766
  ``collections'' for many everyday programming tasks.  Isabelle/ML
39874
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   767
  provides important additions and improvements over operations that
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   768
  are predefined in the SML97 library. *}
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   769
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   770
text %mlref {*
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   771
  \begin{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   772
  @{index_ML cons: "'a -> 'a list -> 'a list"} \\
39874
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   773
  @{index_ML member: "('b * 'a -> bool) -> 'a list -> 'b -> bool"} \\
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   774
  @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   775
  @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   776
  @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   777
  \end{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   778
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   779
  \begin{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   780
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   781
  \item @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   782
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   783
  Tupled infix operators are a historical accident in Standard ML.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   784
  The curried @{ML cons} amends this, but it should be only used when
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   785
  partial application is required.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   786
39874
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   787
  \item @{ML member}, @{ML insert}, @{ML remove}, @{ML update} treat
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   788
  lists as a set-like container that maintains the order of elements.
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   789
  See @{"file" "~~/src/Pure/library.ML"} for the full specifications
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   790
  (written in ML).  There are some further derived operations like
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   791
  @{ML union} or @{ML inter}.
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   792
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   793
  Note that @{ML insert} is conservative about elements that are
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   794
  already a @{ML member} of the list, while @{ML update} ensures that
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   795
  the last entry is always put in front.  The latter discipline is
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   796
  often more appropriate in declarations of context data
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   797
  (\secref{sec:context-data}) that are issued by the user in Isar
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   798
  source: more recent declarations normally take precedence over
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   799
  earlier ones.
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   800
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   801
  \end{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   802
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   803
39874
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   804
text %mlex {* The following example demonstrates how to \emph{merge}
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   805
  two lists in a natural way. *}
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   806
39874
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   807
ML {*
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   808
  fun merge_lists eq (xs, ys) = fold_rev (insert eq) ys xs;
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   809
*}
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   810
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   811
text {* Here the first list is treated conservatively: only the new
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   812
  elements from the second list are inserted.  The inside-out order of
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   813
  insertion via @{ML fold_rev} attempts to preserve the order of
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   814
  elements in the result.
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   815
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   816
  This way of merging lists is typical for context data
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   817
  (\secref{sec:context-data}).  See also @{ML merge} as defined in
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   818
  @{"file" "~~/src/Pure/library.ML"}.
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   819
*}
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   820
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   821
bbac63bbcffe clarified "lists as a set-like container";
wenzelm
parents: 39872
diff changeset
   822
subsubsection {* Canonical iteration *}  (* FIXME move!? *)
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   823
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   824
text {* A function @{text "f: \<alpha> \<rightarrow> \<beta> \<rightarrow> \<beta>"} can be understood as update
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   825
  on a configuration of type @{text "\<beta>"} that is parametrized by
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   826
  arguments of type @{text "\<alpha>"}.  Given @{text "a: \<alpha>"} the partial
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   827
  application @{text "(f a): \<beta> \<rightarrow> \<beta>"} operates homogeneously on @{text
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   828
  "\<beta>"}.  This can be iterated naturally over a list of parameters
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   829
  @{text "[a\<^sub>1, \<dots>, a\<^sub>n]"} as @{text "f a\<^sub>1; \<dots>; f a\<^bsub>n\<^esub>\<^bsub>\<^esub>"} (where the
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   830
  semicolon represents left-to-right composition).  The latter
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   831
  expression is again a function @{text "\<beta> \<rightarrow> \<beta>"}.  It can be applied
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   832
  to an initial configuration @{text "b: \<beta>"} to start the iteration
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   833
  over the given list of arguments: each @{text "a"} in @{text "a\<^sub>1, \<dots>,
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   834
  a\<^sub>n"} is applied consecutively by updating a cumulative
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   835
  configuration.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   836
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   837
  The @{text fold} combinator in Isabelle/ML lifts a function @{text
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   838
  "f"} as above to its iterated version over a list of arguments.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   839
  Lifting can be repeated, e.g.\ @{text "(fold \<circ> fold) f"} iterates
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   840
  over a list of lists as expected.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   841
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   842
  The variant @{text "fold_rev"} works inside-out over the list of
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   843
  arguments, such that @{text "fold_rev f \<equiv> fold f \<circ> rev"} holds.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   844
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   845
  The @{text "fold_map"} combinator essentially performs @{text
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   846
  "fold"} and @{text "map"} simultaneously: each application of @{text
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   847
  "f"} produces an updated configuration together with a side-result;
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   848
  the iteration collects all such side-results as a separate list.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   849
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   850
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   851
text %mlref {*
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   852
  \begin{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   853
  @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   854
  @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   855
  @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   856
  \end{mldecls}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   857
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   858
  \begin{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   859
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   860
  \item @{ML fold}~@{text f} lifts the parametrized update function
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   861
  @{text "f"} to a list of parameters.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   862
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   863
  \item @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   864
  "f"}, but works inside-out.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   865
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   866
  \item @{ML fold_map}~@{text "f"} lifts the parametrized update
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   867
  function @{text "f"} (with side-result) to a list of parameters and
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   868
  cumulative side-results.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   869
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   870
  \end{description}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   871
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   872
  \begin{warn}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   873
  The literature on functional programming provides a multitude of
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   874
  combinators called @{text "foldl"}, @{text "foldr"} etc.  SML97
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   875
  provides its own variations as @{ML List.foldl} and @{ML
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   876
  List.foldr}, while the classic Isabelle library still has the
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   877
  slightly more convenient historic @{ML Library.foldl} and @{ML
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   878
  Library.foldr}.  To avoid further confusion, all of this should be
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   879
  ignored, and @{ML fold} (or @{ML fold_rev}) used exclusively.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   880
  \end{warn}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   881
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   882
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   883
text %mlex {* Using canonical @{ML fold} together with canonical @{ML
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   884
  cons}, or similar standard operations, alternates the orientation of
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   885
  data.  The is quite natural and should not altered forcible by
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   886
  inserting extra applications @{ML rev}.  The alternative @{ML
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   887
  fold_rev} can be used in the relatively few situations, where
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   888
  alternation should be prevented.
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   889
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   890
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   891
ML {*
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   892
  val items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   893
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   894
  val list1 = fold cons items [];
39866
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   895
  @{assert} (list1 = rev items);
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   896
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   897
  val list2 = fold_rev cons items [];
39866
5ec01d5acd0c more robust examples: explicit @{assert} instead of unchecked output;
wenzelm
parents: 39864
diff changeset
   898
  @{assert} (list2 = items);
39863
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   899
*}
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   900
c0de5386017e more on "Basic data types";
wenzelm
parents: 39862
diff changeset
   901
39875
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   902
subsection {* Association lists *}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   903
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   904
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
   905
  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
   906
  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
   907
  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
   908
  occurrence into account.
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   909
*}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   910
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   911
text {*
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   912
  \begin{mldecls}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   913
  @{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
   914
  @{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
   915
  @{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
   916
  \end{mldecls}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   917
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   918
  \begin{description}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   919
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   920
  \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
   921
  implement the main ``framework operations'' for mappings in
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   922
  Isabelle/ML, with standard conventions for names and types.
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   923
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   924
  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
   925
  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
   926
  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
   927
  @{text "the_element"} or @{text "get"}.
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   928
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   929
  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
   930
  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
   931
  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
   932
  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
   933
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   934
  \end{description}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   935
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   936
  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
   937
  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
   938
  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
   939
  "~~/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
   940
  thousands or millions of elements.
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   941
*}
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   942
648c930125f6 more on "Association lists", based on more succinct version of older material;
wenzelm
parents: 39874
diff changeset
   943
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   944
subsection {* Unsynchronized references *}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   945
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   946
text %mlref {*
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   947
  \begin{mldecls}
39870
wenzelm
parents: 39868
diff changeset
   948
  @{index_ML_type "'a Unsynchronized.ref"} \\
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   949
  @{index_ML Unsynchronized.ref: "'a -> 'a Unsynchronized.ref"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   950
  @{index_ML "!": "'a Unsynchronized.ref -> 'a"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   951
  @{index_ML "op :=": "'a Unsynchronized.ref * 'a -> unit"} \\
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   952
  \end{mldecls}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   953
*}
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   954
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   955
text {* Due to ubiquitous parallelism in Isabelle/ML (see also
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   956
  \secref{sec:multi-threading}), the mutable reference cells of
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   957
  Standard ML are notorious for causing problems.  In a highly
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   958
  parallel system, both correctness \emph{and} performance are easily
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   959
  degraded when using mutable data.
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   960
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   961
  The unwieldy name of @{ML Unsynchronized.ref} for the constructor
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   962
  for references in Isabelle/ML emphasizes the inconveniences caused by
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   963
  mutability.  Existing operations @{ML "!"}  and @{ML "op :="} are
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   964
  unchanged, but should be used with special precautions, say in a
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   965
  strictly local situation that is guaranteed to be restricted to
39870
wenzelm
parents: 39868
diff changeset
   966
  sequential evaluation --- now and in the future.  *}
39859
381e16bb5f46 more on "Basic ML data types";
wenzelm
parents: 39856
diff changeset
   967
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   968
39870
wenzelm
parents: 39868
diff changeset
   969
section {* Thread-safe programming \label{sec:multi-threading} *}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   970
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   971
text {* Multi-threaded execution has become an everyday reality in
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   972
  Isabelle since Poly/ML 5.2.1 and Isabelle2008.  Isabelle/ML provides
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   973
  implicit and explicit parallelism by default, and there is no way
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   974
  for user-space tools to ``opt out''.  ML programs that are purely
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   975
  functional, output messages only via the official channels
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   976
  (\secref{sec:message-channels}), and do not intercept interrupts
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   977
  (\secref{sec:exceptions}) can participate in the multi-threaded
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   978
  environment immediately without further ado.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   979
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   980
  More ambitious tools with more fine-grained interaction with the
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   981
  environment need to observe the principles explained below.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   982
*}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   983
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   984
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   985
subsection {* Multi-threading with shared memory *}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   986
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   987
text {* Multiple threads help to organize advanced operations of the
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   988
  system, such as real-time conditions on command transactions,
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   989
  sub-components with explicit communication, general asynchronous
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   990
  interaction etc.  Moreover, parallel evaluation is a prerequisite to
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   991
  make adequate use of the CPU resources that are available on
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   992
  multi-core systems.\footnote{Multi-core computing does not mean that
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   993
  there are ``spare cycles'' to be wasted.  It means that the
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   994
  continued exponential speedup of CPU performance due to ``Moore's
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   995
  Law'' follows different rules: clock frequency has reached its peak
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   996
  around 2005, and applications need to be parallelized in order to
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   997
  avoid a perceived loss of performance.  See also
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
   998
  \cite{Sutter:2005}.}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
   999
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1000
  Isabelle/Isar exploits the inherent structure of theories and proofs
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1001
  to support \emph{implicit parallelism} to a large extent.  LCF-style
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1002
  theorem provides almost ideal conditions for that; see also
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1003
  \cite{Wenzel:2009}.  This means, significant parts of theory and
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1004
  proof checking is parallelized by default.  A maximum speedup-factor
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1005
  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
  1006
  expected.\footnote{Further scalability is limited due to garbage
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1007
  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
  1008
  helps to provide initial heap space generously, using the
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1009
  \texttt{-H} option.  Initial heap size needs to be scaled-up
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1010
  together with the number of CPU cores: approximately 1--2\,GB per
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1011
  core..}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1012
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1013
  \medskip ML threads lack the memory protection of separate
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1014
  processes, and operate concurrently on shared heap memory.  This has
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1015
  the advantage that results of independent computations are
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1016
  immediately available to other threads: abstract values can be
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1017
  passed between threads without copying or awkward serialization that
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1018
  is typically required for explicit message passing between separate
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1019
  processes.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1020
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1021
  To make shared-memory multi-threading work robustly and efficiently,
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1022
  some programming guidelines need to be observed.  While the ML
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1023
  system is responsible to maintain basic integrity of the
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1024
  representation of ML values in memory, the application programmer
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1025
  needs to ensure that multi-threaded execution does not break the
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1026
  intended semantics.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1027
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1028
  \begin{warn}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1029
  To participate in implicit parallelism, tools need to be
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1030
  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
  1031
  performance of the whole system.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1032
  \end{warn}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1033
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1034
  Apart from observing the principles of thread-safeness passively,
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1035
  advanced tools may also exploit parallelism actively, e.g.\ by using
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1036
  ``future values'' (\secref{sec:futures}) or the more basic library
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1037
  functions for parallel list operations (\secref{sec:parlist}).
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1038
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1039
  \begin{warn}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1040
  Parallel computing resources are managed centrally by the
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1041
  Isabelle/ML infrastructure.  User programs must not fork their own
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1042
  ML threads to perform computations.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1043
  \end{warn}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1044
*}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1045
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1046
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1047
subsection {* Critical shared resources *}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1048
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1049
text {* Thread-safeness is mainly concerned about concurrent
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1050
  read/write access to shared resources, which are outside the purely
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1051
  functional world of ML.  This covers the following in particular.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1052
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1053
  \begin{itemize}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1054
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1055
  \item Global references (or arrays), i.e.\ mutable memory cells that
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1056
  persist over several invocations of associated
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1057
  operations.\footnote{This is independent of the visibility of such
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1058
  mutable values in the toplevel scope.}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1059
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1060
  \item Global state of the running Isabelle/ML process, i.e.\ raw I/O
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1061
  channels, environment variables, current working directory.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1062
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1063
  \item Writable resources in the file-system that are shared among
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1064
  different threads or other processes.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1065
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1066
  \end{itemize}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1067
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1068
  Isabelle/ML provides various mechanisms to avoid critical shared
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1069
  resources in most practical situations.  As last resort there are
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1070
  some mechanisms for explicit synchronization.  The following
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1071
  guidelines help to make Isabelle/ML programs work smoothly in a
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1072
  concurrent environment.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1073
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1074
  \begin{itemize}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1075
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1076
  \item Avoid global references altogether.  Isabelle/Isar maintains a
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1077
  uniform context that incorporates arbitrary data declared by user
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1078
  programs (\secref{sec:context-data}).  This context is passed as
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1079
  plain value and user tools can get/map their own data in a purely
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1080
  functional manner.  Configuration options within the context
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1081
  (\secref{sec:config-options}) provide simple drop-in replacements
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1082
  for formerly writable flags.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1083
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1084
  \item Keep components with local state information re-entrant.
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1085
  Instead of poking initial values into (private) global references, a
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1086
  new state record can be created on each invocation, and passed
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1087
  through any auxiliary functions of the component.  The state record
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1088
  may well contain mutable references, without requiring any special
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1089
  synchronizations, as long as each invocation gets its own copy.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1090
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1091
  \item Avoid raw output on @{text "stdout"} or @{text "stderr"}.  The
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1092
  Poly/ML library is thread-safe for each individual output operation,
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1093
  but the ordering of parallel invocations is arbitrary.  This means
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1094
  raw output will appear on some system console with unpredictable
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1095
  interleaving of atomic chunks.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1096
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1097
  Note that this does not affect regular message output channels
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1098
  (\secref{sec:message-channels}).  An official message is associated
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1099
  with the command transaction from where it originates, independently
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1100
  of other transactions.  This means each running Isar command has
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1101
  effectively its own set of message channels, and interleaving can
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1102
  only happen when commands use parallelism internally (and only at
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1103
  message boundaries).
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1104
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1105
  \item Treat environment variables and the current working directory
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1106
  of the running process as strictly read-only.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1107
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1108
  \item Restrict writing to the file-system to unique temporary files.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1109
  Isabelle already provides a temporary directory that is unique for
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1110
  the running process, and there is a centralized source of unique
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1111
  serial numbers in Isabelle/ML.  Thus temporary files that are passed
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1112
  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
  1113
  thread-safe.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1114
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1115
  \end{itemize}
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1116
*}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1117
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1118
text %mlref {*
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1119
  \begin{mldecls}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1120
  @{index_ML File.tmp_path: "Path.T -> Path.T"} \\
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1121
  @{index_ML serial_string: "unit -> string"} \\
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1122
  \end{mldecls}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1123
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1124
  \begin{description}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1125
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1126
  \item @{ML File.tmp_path}~@{text "path"} relocates the base
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1127
  component of @{text "path"} into the unique temporary directory of
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1128
  the running Isabelle/ML process.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1129
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1130
  \item @{ML serial_string}~@{text "()"} creates a new serial number
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1131
  that is unique over the runtime of the Isabelle/ML process.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1132
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1133
  \end{description}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1134
*}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1135
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1136
text %mlex {* The following example shows how to create unique
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1137
  temporary file names.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1138
*}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1139
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1140
ML {*
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1141
  val tmp1 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1142
  val tmp2 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1143
  @{assert} (tmp1 <> tmp2);
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1144
*}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1145
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1146
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1147
subsection {* Explicit synchronization *}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1148
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1149
text {* Isabelle/ML also provides some explicit synchronization
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1150
  mechanisms, for the rare situations where mutable shared resources
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1151
  are really required.  These are based on the synchronizations
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1152
  primitives of Poly/ML, which have been adapted to the specific
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1153
  assumptions of the concurrent Isabelle/ML environment.  User code
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1154
  must not use the Poly/ML primitives directly!
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1155
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1156
  \medskip The most basic synchronization concept is a single
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1157
  \emph{critical section} (also called ``monitor'' in the literature).
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1158
  A thread that enters the critical section prevents all other threads
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1159
  from doing the same.  A thread that is already within the critical
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1160
  section may re-enter it in an idempotent manner.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1161
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1162
  Such centralized locking is convenient, because it prevents
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1163
  deadlocks by construction.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1164
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1165
  \medskip More fine-grained locking works via \emph{synchronized
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1166
  variables}.  An explicit state component is associated with
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1167
  mechanisms for locking and signaling.  There are operations to
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1168
  await a condition, change the state, and signal the change to all
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1169
  other waiting threads.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1170
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1171
  Here the synchronized access to the state variable is \emph{not}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1172
  re-entrant: direct or indirect nesting within the same thread will
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1173
  cause a deadlock!
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1174
*}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1175
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1176
text %mlref {*
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1177
  \begin{mldecls}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1178
  @{index_ML NAMED_CRITICAL: "string -> (unit -> 'a) -> 'a"} \\
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1179
  @{index_ML CRITICAL: "(unit -> 'a) -> 'a"} \\
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1180
  \end{mldecls}
39871
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1181
  \begin{mldecls}
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1182
  @{index_ML_type "'a Synchronized.var"} \\
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1183
  @{index_ML Synchronized.var: "string -> 'a -> 'a Synchronized.var"} \\
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1184
  @{index_ML Synchronized.guarded_access: "'a Synchronized.var ->
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1185
  ('a -> ('b * 'a) option) -> 'b"} \\
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1186
  \end{mldecls}
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1187
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1188
  \begin{description}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1189
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1190
  \item @{ML NAMED_CRITICAL}~@{text "name e"} evaluates @{text "e ()"}
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1191
  within the central critical section of Isabelle/ML.  No other thread
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1192
  may do so at the same time, but non-critical parallel execution will
39871
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1193
  continue.  The @{text "name"} argument is used for tracing and might
39868
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1194
  help to spot sources of congestion.
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1195
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1196
  Entering the critical section without contention is very fast, and
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1197
  several basic system operations do so frequently.  Each thread
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1198
  should leave the critical section quickly, otherwise parallel
732ab20fec3b misc tuning;
wenzelm
parents: 39867
diff changeset
  1199
  performance may degrade.
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1200
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1201
  \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
  1202
  name argument.
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1203
39871
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1204
  \item Type @{ML_type "'a Synchronized.var"} represents synchronized
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1205
  variables with state of type @{ML_type 'a}.
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1206
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1207
  \item @{ML Synchronized.var}~@{text "name x"} creates a synchronized
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1208
  variable that is initialized with value @{text "x"}.  The @{text
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1209
  "name"} is used for tracing.
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1210
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1211
  \item @{ML Synchronized.guarded_access}~@{text "var f"} lets the
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1212
  function @{text "f"} operate within a critical section on the state
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1213
  @{text "x"} as follows: if @{text "f x"} produces @{ML NONE}, we
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1214
  continue to wait on the internal condition variable, expecting that
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1215
  some other thread will eventually change the content in a suitable
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1216
  manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} we
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1217
  are satisfied and assign the new state value @{text "x'"}, broadcast
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1218
  a signal to all waiting threads on the associated condition
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1219
  variable, and return the result @{text "y"}.
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1220
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1221
  \end{description}
39871
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1222
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1223
  There are some further variants of the general @{ML
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1224
  Synchronized.guarded_access} combinator, see @{"file"
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1225
  "~~/src/Pure/Concurrent/synchronized.ML"} for details.
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1226
*}
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1227
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1228
text %mlex {* See @{"file" "~~/src/Pure/Concurrent/mailbox.ML"} how to
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1229
  implement a mailbox as synchronized variable over a purely
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1230
  functional queue.
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1231
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1232
  \medskip The following example implements a counter that produces
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1233
  positive integers that are unique over the runtime of the Isabelle
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1234
  process:
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1235
*}
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1236
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1237
ML {*
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1238
  local
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1239
    val counter = Synchronized.var "counter" 0;
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1240
  in
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1241
    fun next () =
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1242
      Synchronized.guarded_access counter
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1243
        (fn i =>
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1244
          let val j = i + 1
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1245
          in SOME (j, j) end);
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1246
  end;
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1247
*}
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1248
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1249
ML {*
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1250
  val a = next ();
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1251
  val b = next ();
b905971407a1 more on synchronized variables;
wenzelm
parents: 39870
diff changeset
  1252
  @{assert} (a <> b);
39867
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1253
*}
a8363532cd4d somewhat modernized version of "Thread-safe programming";
wenzelm
parents: 39866
diff changeset
  1254
30270
61811c9224a6 dummy changes to produce a new changeset of these files;
wenzelm
parents: 29755
diff changeset
  1255
end