src/Doc/Implementation/ML.thy
changeset 61416 b9a3324e4e62
parent 60270 a147272b16f9
child 61439 2bf52eec4e8a
equal deleted inserted replaced
61415:55e73b352287 61416:b9a3324e4e62
    93 
    93 
    94   As in regular typography, there is some extra space \emph{before}
    94   As in regular typography, there is some extra space \emph{before}
    95   section headings that are adjacent to plain text, but not other headings
    95   section headings that are adjacent to plain text, but not other headings
    96   as in the example above.
    96   as in the example above.
    97 
    97 
    98   \medskip The precise wording of the prose text given in these
    98   \<^medskip>
       
    99   The precise wording of the prose text given in these
    99   headings is chosen carefully to introduce the main theme of the
   100   headings is chosen carefully to introduce the main theme of the
   100   subsequent formal ML text.
   101   subsequent formal ML text.
   101 \<close>
   102 \<close>
   102 
   103 
   103 
   104 
   109   \paragraph{Notation.}  A name consists of 1--3 \emph{words} (rarely
   110   \paragraph{Notation.}  A name consists of 1--3 \emph{words} (rarely
   110   4, but not more) that are separated by underscore.  There are three
   111   4, but not more) that are separated by underscore.  There are three
   111   variants concerning upper or lower case letters, which are used for
   112   variants concerning upper or lower case letters, which are used for
   112   certain ML categories as follows:
   113   certain ML categories as follows:
   113 
   114 
   114   \medskip
   115   \<^medskip>
   115   \begin{tabular}{lll}
   116   \begin{tabular}{lll}
   116   variant & example & ML categories \\\hline
   117   variant & example & ML categories \\\hline
   117   lower-case & @{ML_text foo_bar} & values, types, record fields \\
   118   lower-case & @{ML_text foo_bar} & values, types, record fields \\
   118   capitalized & @{ML_text Foo_Bar} & datatype constructors, structures, functors \\
   119   capitalized & @{ML_text Foo_Bar} & datatype constructors, structures, functors \\
   119   upper-case & @{ML_text FOO_BAR} & special values, exception constructors, signatures \\
   120   upper-case & @{ML_text FOO_BAR} & special values, exception constructors, signatures \\
   120   \end{tabular}
   121   \end{tabular}
   121   \medskip
   122   \<^medskip>
   122 
   123 
   123   For historical reasons, many capitalized names omit underscores,
   124   For historical reasons, many capitalized names omit underscores,
   124   e.g.\ old-style @{ML_text FooBar} instead of @{ML_text Foo_Bar}.
   125   e.g.\ old-style @{ML_text FooBar} instead of @{ML_text Foo_Bar}.
   125   Genuine mixed-case names are \emph{not} used, because clear division
   126   Genuine mixed-case names are \emph{not} used, because clear division
   126   of words is essential for readability.\footnote{Camel-case was
   127   of words is essential for readability.\footnote{Camel-case was
   186   \paragraph{Specific conventions.} Here are some specific name forms
   187   \paragraph{Specific conventions.} Here are some specific name forms
   187   that occur frequently in the sources.
   188   that occur frequently in the sources.
   188 
   189 
   189   \begin{itemize}
   190   \begin{itemize}
   190 
   191 
   191   \item A function that maps @{ML_text foo} to @{ML_text bar} is
   192   \<^item> A function that maps @{ML_text foo} to @{ML_text bar} is
   192   called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never
   193   called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never
   193   @{ML_text foo2bar}, nor @{ML_text bar_from_foo}, nor @{ML_text
   194   @{ML_text foo2bar}, nor @{ML_text bar_from_foo}, nor @{ML_text
   194   bar_for_foo}, nor @{ML_text bar4foo}).
   195   bar_for_foo}, nor @{ML_text bar4foo}).
   195 
   196 
   196   \item The name component @{ML_text legacy} means that the operation
   197   \<^item> The name component @{ML_text legacy} means that the operation
   197   is about to be discontinued soon.
   198   is about to be discontinued soon.
   198 
   199 
   199   \item The name component @{ML_text global} means that this works
   200   \<^item> The name component @{ML_text global} means that this works
   200   with the background theory instead of the regular local context
   201   with the background theory instead of the regular local context
   201   (\secref{sec:context}), sometimes for historical reasons, sometimes
   202   (\secref{sec:context}), sometimes for historical reasons, sometimes
   202   due a genuine lack of locality of the concept involved, sometimes as
   203   due a genuine lack of locality of the concept involved, sometimes as
   203   a fall-back for the lack of a proper context in the application
   204   a fall-back for the lack of a proper context in the application
   204   code.  Whenever there is a non-global variant available, the
   205   code.  Whenever there is a non-global variant available, the
   205   application should be migrated to use it with a proper local
   206   application should be migrated to use it with a proper local
   206   context.
   207   context.
   207 
   208 
   208   \item Variables of the main context types of the Isabelle/Isar
   209   \<^item> Variables of the main context types of the Isabelle/Isar
   209   framework (\secref{sec:context} and \chref{ch:local-theory}) have
   210   framework (\secref{sec:context} and \chref{ch:local-theory}) have
   210   firm naming conventions as follows:
   211   firm naming conventions as follows:
   211 
   212 
   212   \begin{itemize}
   213   \begin{itemize}
   213 
   214 
   214   \item theories are called @{ML_text thy}, rarely @{ML_text theory}
   215   \<^item> theories are called @{ML_text thy}, rarely @{ML_text theory}
   215   (never @{ML_text thry})
   216   (never @{ML_text thry})
   216 
   217 
   217   \item proof contexts are called @{ML_text ctxt}, rarely @{ML_text
   218   \<^item> proof contexts are called @{ML_text ctxt}, rarely @{ML_text
   218   context} (never @{ML_text ctx})
   219   context} (never @{ML_text ctx})
   219 
   220 
   220   \item generic contexts are called @{ML_text context}
   221   \<^item> generic contexts are called @{ML_text context}
   221 
   222 
   222   \item local theories are called @{ML_text lthy}, except for local
   223   \<^item> local theories are called @{ML_text lthy}, except for local
   223   theories that are treated as proof context (which is a semantic
   224   theories that are treated as proof context (which is a semantic
   224   super-type)
   225   super-type)
   225 
   226 
   226   \end{itemize}
   227   \end{itemize}
   227 
   228 
   229   well as semantic prefixes like @{ML_text foo_thy} or @{ML_text
   230   well as semantic prefixes like @{ML_text foo_thy} or @{ML_text
   230   bar_ctxt}, but the base conventions above need to be preserved.
   231   bar_ctxt}, but the base conventions above need to be preserved.
   231   This allows to emphasize their data flow via plain regular
   232   This allows to emphasize their data flow via plain regular
   232   expressions in the text editor.
   233   expressions in the text editor.
   233 
   234 
   234   \item The main logical entities (\secref{ch:logic}) have established
   235   \<^item> The main logical entities (\secref{ch:logic}) have established
   235   naming convention as follows:
   236   naming convention as follows:
   236 
   237 
   237   \begin{itemize}
   238   \begin{itemize}
   238 
   239 
   239   \item sorts are called @{ML_text S}
   240   \<^item> sorts are called @{ML_text S}
   240 
   241 
   241   \item types are called @{ML_text T}, @{ML_text U}, or @{ML_text
   242   \<^item> types are called @{ML_text T}, @{ML_text U}, or @{ML_text
   242   ty} (never @{ML_text t})
   243   ty} (never @{ML_text t})
   243 
   244 
   244   \item terms are called @{ML_text t}, @{ML_text u}, or @{ML_text
   245   \<^item> terms are called @{ML_text t}, @{ML_text u}, or @{ML_text
   245   tm} (never @{ML_text trm})
   246   tm} (never @{ML_text trm})
   246 
   247 
   247   \item certified types are called @{ML_text cT}, rarely @{ML_text
   248   \<^item> certified types are called @{ML_text cT}, rarely @{ML_text
   248   T}, with variants as for types
   249   T}, with variants as for types
   249 
   250 
   250   \item certified terms are called @{ML_text ct}, rarely @{ML_text
   251   \<^item> certified terms are called @{ML_text ct}, rarely @{ML_text
   251   t}, with variants as for terms (never @{ML_text ctrm})
   252   t}, with variants as for terms (never @{ML_text ctrm})
   252 
   253 
   253   \item theorems are called @{ML_text th}, or @{ML_text thm}
   254   \<^item> theorems are called @{ML_text th}, or @{ML_text thm}
   254 
   255 
   255   \end{itemize}
   256   \end{itemize}
   256 
   257 
   257   Proper semantic names override these conventions completely.  For
   258   Proper semantic names override these conventions completely.  For
   258   example, the left-hand side of an equation (as a term) can be called
   259   example, the left-hand side of an equation (as a term) can be called
   377   makes it hard to observe its strict length limit (working against
   378   makes it hard to observe its strict length limit (working against
   378   \emph{readability}), it requires extra editing to adapt the layout
   379   \emph{readability}), it requires extra editing to adapt the layout
   379   to changes of the initial text (working against
   380   to changes of the initial text (working against
   380   \emph{maintainability}) etc.
   381   \emph{maintainability}) etc.
   381 
   382 
   382   \medskip For similar reasons, any kind of two-dimensional or tabular
   383   \<^medskip>
       
   384   For similar reasons, any kind of two-dimensional or tabular
   383   layouts, ASCII-art with lines or boxes of asterisks etc.\ should be
   385   layouts, ASCII-art with lines or boxes of asterisks etc.\ should be
   384   avoided.
   386   avoided.
   385 
   387 
   386   \paragraph{Complex expressions} that consist of multi-clausal
   388   \paragraph{Complex expressions} that consist of multi-clausal
   387   function definitions, @{ML_text handle}, @{ML_text case},
   389   function definitions, @{ML_text handle}, @{ML_text case},
   445 
   447 
   446   Extra parentheses around @{ML_text case} expressions are optional,
   448   Extra parentheses around @{ML_text case} expressions are optional,
   447   but help to analyse the nesting based on character matching in the
   449   but help to analyse the nesting based on character matching in the
   448   text editor.
   450   text editor.
   449 
   451 
   450   \medskip There are two main exceptions to the overall principle of
   452   \<^medskip>
       
   453   There are two main exceptions to the overall principle of
   451   compositionality in the layout of complex expressions.
   454   compositionality in the layout of complex expressions.
   452 
   455 
   453   \begin{enumerate}
   456   \begin{enumerate}
   454 
   457 
   455   \item @{ML_text "if"} expressions are iterated as if ML had multi-branch
   458   \<^enum> @{ML_text "if"} expressions are iterated as if ML had multi-branch
   456   conditionals, e.g.
   459   conditionals, e.g.
   457 
   460 
   458   \begin{verbatim}
   461   \begin{verbatim}
   459   (* RIGHT *)
   462   (* RIGHT *)
   460 
   463 
   461   if b1 then e1
   464   if b1 then e1
   462   else if b2 then e2
   465   else if b2 then e2
   463   else e3
   466   else e3
   464   \end{verbatim}
   467   \end{verbatim}
   465 
   468 
   466   \item @{ML_text fn} abstractions are often layed-out as if they
   469   \<^enum> @{ML_text fn} abstractions are often layed-out as if they
   467   would lack any structure by themselves.  This traditional form is
   470   would lack any structure by themselves.  This traditional form is
   468   motivated by the possibility to shift function arguments back and
   471   motivated by the possibility to shift function arguments back and
   469   forth wrt.\ additional combinators.  Example:
   472   forth wrt.\ additional combinators.  Example:
   470 
   473 
   471   \begin{verbatim}
   474   \begin{verbatim}
   513       val y = ...
   516       val y = ...
   514     in
   517     in
   515       ... end
   518       ... end
   516   \end{verbatim}
   519   \end{verbatim}
   517 
   520 
   518   \medskip In general the source layout is meant to emphasize the
   521   \<^medskip>
       
   522   In general the source layout is meant to emphasize the
   519   structure of complex language expressions, not to pretend that SML
   523   structure of complex language expressions, not to pretend that SML
   520   had a completely different syntax (say that of Haskell, Scala, Java).
   524   had a completely different syntax (say that of Haskell, Scala, Java).
   521 \<close>
   525 \<close>
   522 
   526 
   523 
   527 
   582   are no global side-effects involved here.\footnote{Such a stateless
   586   are no global side-effects involved here.\footnote{Such a stateless
   583   compilation environment is also a prerequisite for robust parallel
   587   compilation environment is also a prerequisite for robust parallel
   584   compilation within independent nodes of the implicit theory development
   588   compilation within independent nodes of the implicit theory development
   585   graph.}
   589   graph.}
   586 
   590 
   587   \medskip The next example shows how to embed ML into Isar proofs, using
   591   \<^medskip>
       
   592   The next example shows how to embed ML into Isar proofs, using
   588   @{command_ref "ML_prf"} instead of @{command_ref "ML"}. As illustrated
   593   @{command_ref "ML_prf"} instead of @{command_ref "ML"}. As illustrated
   589   below, the effect on the ML environment is local to the whole proof body,
   594   below, the effect on the ML environment is local to the whole proof body,
   590   but ignoring the block structure. \<close>
   595   but ignoring the block structure.\<close>
   591 
   596 
   592 notepad
   597 notepad
   593 begin
   598 begin
   594   ML_prf %"ML" \<open>val a = 1\<close>
   599   ML_prf %"ML" \<open>val a = 1\<close>
   595   {
   600   {
   601 text \<open>By side-stepping the normal scoping rules for Isar proof
   606 text \<open>By side-stepping the normal scoping rules for Isar proof
   602   blocks, embedded ML code can refer to the different contexts and
   607   blocks, embedded ML code can refer to the different contexts and
   603   manipulate corresponding entities, e.g.\ export a fact from a block
   608   manipulate corresponding entities, e.g.\ export a fact from a block
   604   context.
   609   context.
   605 
   610 
   606   \medskip Two further ML commands are useful in certain situations:
   611   \<^medskip>
       
   612   Two further ML commands are useful in certain situations:
   607   @{command_ref ML_val} and @{command_ref ML_command} are \emph{diagnostic} in
   613   @{command_ref ML_val} and @{command_ref ML_command} are \emph{diagnostic} in
   608   the sense that there is no effect on the underlying environment, and can
   614   the sense that there is no effect on the underlying environment, and can
   609   thus be used anywhere. The examples below produce long strings of digits by
   615   thus be used anywhere. The examples below produce long strings of digits by
   610   invoking @{ML factorial}: @{command ML_val} takes care of printing the ML
   616   invoking @{ML factorial}: @{command ML_val} takes care of printing the ML
   611   toplevel result, but @{command ML_command} is silent so we produce an
   617   toplevel result, but @{command ML_command} is silent so we produce an
   679   \<close>}
   685   \<close>}
   680 
   686 
   681   Here @{syntax nameref} and @{syntax args} are outer syntax categories, as
   687   Here @{syntax nameref} and @{syntax args} are outer syntax categories, as
   682   defined in @{cite "isabelle-isar-ref"}.
   688   defined in @{cite "isabelle-isar-ref"}.
   683 
   689 
   684   \medskip A regular antiquotation @{text "@{name args}"} processes
   690   \<^medskip>
       
   691   A regular antiquotation @{text "@{name args}"} processes
   685   its arguments by the usual means of the Isar source language, and
   692   its arguments by the usual means of the Isar source language, and
   686   produces corresponding ML source text, either as literal
   693   produces corresponding ML source text, either as literal
   687   \emph{inline} text (e.g.\ @{text "@{term t}"}) or abstract
   694   \emph{inline} text (e.g.\ @{text "@{term t}"}) or abstract
   688   \emph{value} (e.g. @{text "@{thm th}"}).  This pre-compilation
   695   \emph{value} (e.g. @{text "@{thm th}"}).  This pre-compilation
   689   scheme allows to refer to formal entities in a robust manner, with
   696   scheme allows to refer to formal entities in a robust manner, with
   779   in their application.  In Isabelle/ML, large portions of text can be
   786   in their application.  In Isabelle/ML, large portions of text can be
   780   written without auxiliary operations like @{text "swap: \<alpha> \<times> \<beta> \<rightarrow> \<beta> \<times>
   787   written without auxiliary operations like @{text "swap: \<alpha> \<times> \<beta> \<rightarrow> \<beta> \<times>
   781   \<alpha>"} or @{text "C: (\<alpha> \<rightarrow> \<beta> \<rightarrow> \<gamma>) \<rightarrow> (\<beta> \<rightarrow> \<alpha> \<rightarrow> \<gamma>)"} (the latter is not
   788   \<alpha>"} or @{text "C: (\<alpha> \<rightarrow> \<beta> \<rightarrow> \<gamma>) \<rightarrow> (\<beta> \<rightarrow> \<alpha> \<rightarrow> \<gamma>)"} (the latter is not
   782   present in the Isabelle/ML library).
   789   present in the Isabelle/ML library).
   783 
   790 
   784   \medskip The main idea is that arguments that vary less are moved
   791   \<^medskip>
       
   792   The main idea is that arguments that vary less are moved
   785   further to the left than those that vary more.  Two particularly
   793   further to the left than those that vary more.  Two particularly
   786   important categories of functions are \emph{selectors} and
   794   important categories of functions are \emph{selectors} and
   787   \emph{updates}.
   795   \emph{updates}.
   788 
   796 
   789   The subsequent scheme is based on a hypothetical set-like container
   797   The subsequent scheme is based on a hypothetical set-like container
   828   becomes hard to read and maintain if the functions are themselves
   836   becomes hard to read and maintain if the functions are themselves
   829   given as complex expressions.  The notation can be significantly
   837   given as complex expressions.  The notation can be significantly
   830   improved by introducing \emph{forward} versions of application and
   838   improved by introducing \emph{forward} versions of application and
   831   composition as follows:
   839   composition as follows:
   832 
   840 
   833   \medskip
   841   \<^medskip>
   834   \begin{tabular}{lll}
   842   \begin{tabular}{lll}
   835   @{text "x |> f"} & @{text "\<equiv>"} & @{text "f x"} \\
   843   @{text "x |> f"} & @{text "\<equiv>"} & @{text "f x"} \\
   836   @{text "(f #> g) x"} & @{text "\<equiv>"} & @{text "x |> f |> g"} \\
   844   @{text "(f #> g) x"} & @{text "\<equiv>"} & @{text "x |> f |> g"} \\
   837   \end{tabular}
   845   \end{tabular}
   838   \medskip
   846   \<^medskip>
   839 
   847 
   840   This enables to write conveniently @{text "x |> f\<^sub>1 |> \<dots> |> f\<^sub>n"} or
   848   This enables to write conveniently @{text "x |> f\<^sub>1 |> \<dots> |> f\<^sub>n"} or
   841   @{text "f\<^sub>1 #> \<dots> #> f\<^sub>n"} for its functional abstraction over @{text
   849   @{text "f\<^sub>1 #> \<dots> #> f\<^sub>n"} for its functional abstraction over @{text
   842   "x"}.
   850   "x"}.
   843 
   851 
   844   \medskip There is an additional set of combinators to accommodate
   852   \<^medskip>
       
   853   There is an additional set of combinators to accommodate
   845   multiple results (via pairs) that are passed on as multiple
   854   multiple results (via pairs) that are passed on as multiple
   846   arguments (via currying).
   855   arguments (via currying).
   847 
   856 
   848   \medskip
   857   \<^medskip>
   849   \begin{tabular}{lll}
   858   \begin{tabular}{lll}
   850   @{text "(x, y) |-> f"} & @{text "\<equiv>"} & @{text "f x y"} \\
   859   @{text "(x, y) |-> f"} & @{text "\<equiv>"} & @{text "f x y"} \\
   851   @{text "(f #-> g) x"} & @{text "\<equiv>"} & @{text "x |> f |-> g"} \\
   860   @{text "(f #-> g) x"} & @{text "\<equiv>"} & @{text "x |> f |-> g"} \\
   852   \end{tabular}
   861   \end{tabular}
   853   \medskip
   862   \<^medskip>
   854 \<close>
   863 \<close>
   855 
   864 
   856 text %mlref \<open>
   865 text %mlref \<open>
   857   \begin{mldecls}
   866   \begin{mldecls}
   858   @{index_ML_op "|> ": "'a * ('a -> 'b) -> 'b"} \\
   867   @{index_ML_op "|> ": "'a * ('a -> 'b) -> 'b"} \\
   942   an extra @{ML "map"} over the given list.  This kind of peephole
   951   an extra @{ML "map"} over the given list.  This kind of peephole
   943   optimization reduces both the code size and the tree structures in
   952   optimization reduces both the code size and the tree structures in
   944   memory (``deforestation''), but it requires some practice to read
   953   memory (``deforestation''), but it requires some practice to read
   945   and write fluently.
   954   and write fluently.
   946 
   955 
   947   \medskip The next example elaborates the idea of canonical
   956   \<^medskip>
       
   957   The next example elaborates the idea of canonical
   948   iteration, demonstrating fast accumulation of tree content using a
   958   iteration, demonstrating fast accumulation of tree content using a
   949   text buffer.
   959   text buffer.
   950 \<close>
   960 \<close>
   951 
   961 
   952 ML \<open>
   962 ML \<open>
  1092     "Beware the Jubjub Bird, and shun",
  1102     "Beware the Jubjub Bird, and shun",
  1093     "The frumious Bandersnatch!"]);
  1103     "The frumious Bandersnatch!"]);
  1094 \<close>
  1104 \<close>
  1095 
  1105 
  1096 text \<open>
  1106 text \<open>
  1097   \medskip An alternative is to make a paragraph of freely-floating words as
  1107   \<^medskip>
       
  1108   An alternative is to make a paragraph of freely-floating words as
  1098   follows.
  1109   follows.
  1099 \<close>
  1110 \<close>
  1100 
  1111 
  1101 ML_command \<open>
  1112 ML_command \<open>
  1102   warning (Pretty.string_of (Pretty.para
  1113   warning (Pretty.string_of (Pretty.para
  1152   Traditionally, the (short) exception message would include the name
  1163   Traditionally, the (short) exception message would include the name
  1153   of an ML function, although this is no longer necessary, because the
  1164   of an ML function, although this is no longer necessary, because the
  1154   ML runtime system attaches detailed source position stemming from the
  1165   ML runtime system attaches detailed source position stemming from the
  1155   corresponding @{ML_text raise} keyword.
  1166   corresponding @{ML_text raise} keyword.
  1156 
  1167 
  1157   \medskip User modules can always introduce their own custom
  1168   \<^medskip>
       
  1169   User modules can always introduce their own custom
  1158   exceptions locally, e.g.\ to organize internal failures robustly
  1170   exceptions locally, e.g.\ to organize internal failures robustly
  1159   without overlapping with existing exceptions.  Exceptions that are
  1171   without overlapping with existing exceptions.  Exceptions that are
  1160   exposed in module signatures require extra care, though, and should
  1172   exposed in module signatures require extra care, though, and should
  1161   \emph{not} be introduced by default.  Surprise by users of a module
  1173   \emph{not} be introduced by default.  Surprise by users of a module
  1162   can be often minimized by using plain user errors instead.
  1174   can be often minimized by using plain user errors instead.
  1264   in itself a small string, which has either one of the following
  1276   in itself a small string, which has either one of the following
  1265   forms:
  1277   forms:
  1266 
  1278 
  1267   \begin{enumerate}
  1279   \begin{enumerate}
  1268 
  1280 
  1269   \item a single ASCII character ``@{text "c"}'', for example
  1281   \<^enum> a single ASCII character ``@{text "c"}'', for example
  1270   ``@{verbatim a}'',
  1282   ``@{verbatim a}'',
  1271 
  1283 
  1272   \item a codepoint according to UTF-8 (non-ASCII byte sequence),
  1284   \<^enum> a codepoint according to UTF-8 (non-ASCII byte sequence),
  1273 
  1285 
  1274   \item a regular symbol ``@{verbatim \<open>\\<close>}@{verbatim "<"}@{text
  1286   \<^enum> a regular symbol ``@{verbatim \<open>\\<close>}@{verbatim "<"}@{text
  1275   "ident"}@{verbatim ">"}'', for example ``@{verbatim "\<alpha>"}'',
  1287   "ident"}@{verbatim ">"}'', for example ``@{verbatim "\<alpha>"}'',
  1276 
  1288 
  1277   \item a control symbol ``@{verbatim \<open>\\<close>}@{verbatim "<^"}@{text
  1289   \<^enum> a control symbol ``@{verbatim \<open>\\<close>}@{verbatim "<^"}@{text
  1278   "ident"}@{verbatim ">"}'', for example ``@{verbatim "\<^bold>"}'',
  1290   "ident"}@{verbatim ">"}'', for example ``@{verbatim "\<^bold>"}'',
  1279 
  1291 
  1280   \item a raw symbol ``@{verbatim \<open>\\<close>}@{verbatim "<^raw:"}@{text
  1292   \<^enum> a raw symbol ``@{verbatim \<open>\\<close>}@{verbatim "<^raw:"}@{text
  1281   text}@{verbatim ">"}'' where @{text text} consists of printable characters
  1293   text}@{verbatim ">"}'' where @{text text} consists of printable characters
  1282   excluding ``@{verbatim "."}'' and ``@{verbatim ">"}'', for example
  1294   excluding ``@{verbatim "."}'' and ``@{verbatim ">"}'', for example
  1283   ``@{verbatim "\<^raw:$\sum_{i = 1}^n$>"}'',
  1295   ``@{verbatim "\<^raw:$\sum_{i = 1}^n$>"}'',
  1284 
  1296 
  1285   \item a numbered raw control symbol ``@{verbatim \<open>\\<close>}@{verbatim
  1297   \<^enum> a numbered raw control symbol ``@{verbatim \<open>\\<close>}@{verbatim
  1286   "<^raw"}@{text n}@{verbatim ">"}, where @{text n} consists of digits, for
  1298   "<^raw"}@{text n}@{verbatim ">"}, where @{text n} consists of digits, for
  1287   example ``@{verbatim "\<^raw42>"}''.
  1299   example ``@{verbatim "\<^raw42>"}''.
  1288 
  1300 
  1289   \end{enumerate}
  1301   \end{enumerate}
  1290 
  1302 
  1301   encoding is processed in a non-strict fashion, such that well-formed code
  1313   encoding is processed in a non-strict fashion, such that well-formed code
  1302   sequences are recognized accordingly. Unicode provides its own collection of
  1314   sequences are recognized accordingly. Unicode provides its own collection of
  1303   mathematical symbols, but within the core Isabelle/ML world there is no link
  1315   mathematical symbols, but within the core Isabelle/ML world there is no link
  1304   to the standard collection of Isabelle regular symbols.
  1316   to the standard collection of Isabelle regular symbols.
  1305 
  1317 
  1306   \medskip Output of Isabelle symbols depends on the print mode. For example,
  1318   \<^medskip>
       
  1319   Output of Isabelle symbols depends on the print mode. For example,
  1307   the standard {\LaTeX} setup of the Isabelle document preparation system
  1320   the standard {\LaTeX} setup of the Isabelle document preparation system
  1308   would present ``@{verbatim "\<alpha>"}'' as @{text "\<alpha>"}, and ``@{verbatim
  1321   would present ``@{verbatim "\<alpha>"}'' as @{text "\<alpha>"}, and ``@{verbatim
  1309   "\<^bold>\<alpha>"}'' as @{text "\<^bold>\<alpha>"}. On-screen rendering usually works by mapping a
  1322   "\<^bold>\<alpha>"}'' as @{text "\<^bold>\<alpha>"}. On-screen rendering usually works by mapping a
  1310   finite subset of Isabelle symbols to suitable Unicode characters.
  1323   finite subset of Isabelle symbols to suitable Unicode characters.
  1311 \<close>
  1324 \<close>
  1413   Isabelle-specific purposes with the following implicit substructures
  1426   Isabelle-specific purposes with the following implicit substructures
  1414   packed into the string content:
  1427   packed into the string content:
  1415 
  1428 
  1416   \begin{enumerate}
  1429   \begin{enumerate}
  1417 
  1430 
  1418   \item sequence of Isabelle symbols (see also \secref{sec:symbols}),
  1431   \<^enum> sequence of Isabelle symbols (see also \secref{sec:symbols}),
  1419   with @{ML Symbol.explode} as key operation;
  1432   with @{ML Symbol.explode} as key operation;
  1420 
  1433 
  1421   \item XML tree structure via YXML (see also @{cite "isabelle-system"}),
  1434   \<^enum> XML tree structure via YXML (see also @{cite "isabelle-system"}),
  1422   with @{ML YXML.parse_body} as key operation.
  1435   with @{ML YXML.parse_body} as key operation.
  1423 
  1436 
  1424   \end{enumerate}
  1437   \end{enumerate}
  1425 
  1438 
  1426   Note that Isabelle/ML string literals may refer Isabelle symbols like
  1439   Note that Isabelle/ML string literals may refer Isabelle symbols like
  1708   @{cite "Wenzel:2009"}. This means, significant parts of theory and proof
  1721   @{cite "Wenzel:2009"}. This means, significant parts of theory and proof
  1709   checking is parallelized by default. In Isabelle2013, a maximum
  1722   checking is parallelized by default. In Isabelle2013, a maximum
  1710   speedup-factor of 3.5 on 4 cores and 6.5 on 8 cores can be expected
  1723   speedup-factor of 3.5 on 4 cores and 6.5 on 8 cores can be expected
  1711   @{cite "Wenzel:2013:ITP"}.
  1724   @{cite "Wenzel:2013:ITP"}.
  1712 
  1725 
  1713   \medskip ML threads lack the memory protection of separate
  1726   \<^medskip>
       
  1727   ML threads lack the memory protection of separate
  1714   processes, and operate concurrently on shared heap memory.  This has
  1728   processes, and operate concurrently on shared heap memory.  This has
  1715   the advantage that results of independent computations are directly
  1729   the advantage that results of independent computations are directly
  1716   available to other threads: abstract values can be passed without
  1730   available to other threads: abstract values can be passed without
  1717   copying or awkward serialization that is typically required for
  1731   copying or awkward serialization that is typically required for
  1718   separate processes.
  1732   separate processes.
  1748   read/write access to shared resources, which are outside the purely
  1762   read/write access to shared resources, which are outside the purely
  1749   functional world of ML.  This covers the following in particular.
  1763   functional world of ML.  This covers the following in particular.
  1750 
  1764 
  1751   \begin{itemize}
  1765   \begin{itemize}
  1752 
  1766 
  1753   \item Global references (or arrays), i.e.\ mutable memory cells that
  1767   \<^item> Global references (or arrays), i.e.\ mutable memory cells that
  1754   persist over several invocations of associated
  1768   persist over several invocations of associated
  1755   operations.\footnote{This is independent of the visibility of such
  1769   operations.\footnote{This is independent of the visibility of such
  1756   mutable values in the toplevel scope.}
  1770   mutable values in the toplevel scope.}
  1757 
  1771 
  1758   \item Global state of the running Isabelle/ML process, i.e.\ raw I/O
  1772   \<^item> Global state of the running Isabelle/ML process, i.e.\ raw I/O
  1759   channels, environment variables, current working directory.
  1773   channels, environment variables, current working directory.
  1760 
  1774 
  1761   \item Writable resources in the file-system that are shared among
  1775   \<^item> Writable resources in the file-system that are shared among
  1762   different threads or external processes.
  1776   different threads or external processes.
  1763 
  1777 
  1764   \end{itemize}
  1778   \end{itemize}
  1765 
  1779 
  1766   Isabelle/ML provides various mechanisms to avoid critical shared
  1780   Isabelle/ML provides various mechanisms to avoid critical shared
  1769   help to make Isabelle/ML programs work smoothly in a concurrent
  1783   help to make Isabelle/ML programs work smoothly in a concurrent
  1770   environment.
  1784   environment.
  1771 
  1785 
  1772   \begin{itemize}
  1786   \begin{itemize}
  1773 
  1787 
  1774   \item Avoid global references altogether.  Isabelle/Isar maintains a
  1788   \<^item> Avoid global references altogether.  Isabelle/Isar maintains a
  1775   uniform context that incorporates arbitrary data declared by user
  1789   uniform context that incorporates arbitrary data declared by user
  1776   programs (\secref{sec:context-data}).  This context is passed as
  1790   programs (\secref{sec:context-data}).  This context is passed as
  1777   plain value and user tools can get/map their own data in a purely
  1791   plain value and user tools can get/map their own data in a purely
  1778   functional manner.  Configuration options within the context
  1792   functional manner.  Configuration options within the context
  1779   (\secref{sec:config-options}) provide simple drop-in replacements
  1793   (\secref{sec:config-options}) provide simple drop-in replacements
  1780   for historic reference variables.
  1794   for historic reference variables.
  1781 
  1795 
  1782   \item Keep components with local state information re-entrant.
  1796   \<^item> Keep components with local state information re-entrant.
  1783   Instead of poking initial values into (private) global references, a
  1797   Instead of poking initial values into (private) global references, a
  1784   new state record can be created on each invocation, and passed
  1798   new state record can be created on each invocation, and passed
  1785   through any auxiliary functions of the component.  The state record
  1799   through any auxiliary functions of the component.  The state record
  1786   contain mutable references in special situations, without requiring any
  1800   contain mutable references in special situations, without requiring any
  1787   synchronization, as long as each invocation gets its own copy and the
  1801   synchronization, as long as each invocation gets its own copy and the
  1788   tool itself is single-threaded.
  1802   tool itself is single-threaded.
  1789 
  1803 
  1790   \item Avoid raw output on @{text "stdout"} or @{text "stderr"}.  The
  1804   \<^item> Avoid raw output on @{text "stdout"} or @{text "stderr"}.  The
  1791   Poly/ML library is thread-safe for each individual output operation,
  1805   Poly/ML library is thread-safe for each individual output operation,
  1792   but the ordering of parallel invocations is arbitrary.  This means
  1806   but the ordering of parallel invocations is arbitrary.  This means
  1793   raw output will appear on some system console with unpredictable
  1807   raw output will appear on some system console with unpredictable
  1794   interleaving of atomic chunks.
  1808   interleaving of atomic chunks.
  1795 
  1809 
  1799   of other transactions.  This means each running Isar command has
  1813   of other transactions.  This means each running Isar command has
  1800   effectively its own set of message channels, and interleaving can
  1814   effectively its own set of message channels, and interleaving can
  1801   only happen when commands use parallelism internally (and only at
  1815   only happen when commands use parallelism internally (and only at
  1802   message boundaries).
  1816   message boundaries).
  1803 
  1817 
  1804   \item Treat environment variables and the current working directory
  1818   \<^item> Treat environment variables and the current working directory
  1805   of the running process as read-only.
  1819   of the running process as read-only.
  1806 
  1820 
  1807   \item Restrict writing to the file-system to unique temporary files.
  1821   \<^item> Restrict writing to the file-system to unique temporary files.
  1808   Isabelle already provides a temporary directory that is unique for
  1822   Isabelle already provides a temporary directory that is unique for
  1809   the running process, and there is a centralized source of unique
  1823   the running process, and there is a centralized source of unique
  1810   serial numbers in Isabelle/ML.  Thus temporary files that are passed
  1824   serial numbers in Isabelle/ML.  Thus temporary files that are passed
  1811   to to some external process will be always disjoint, and thus
  1825   to to some external process will be always disjoint, and thus
  1812   thread-safe.
  1826   thread-safe.
  1911   val a = next ();
  1925   val a = next ();
  1912   val b = next ();
  1926   val b = next ();
  1913   @{assert} (a <> b);
  1927   @{assert} (a <> b);
  1914 \<close>
  1928 \<close>
  1915 
  1929 
  1916 text \<open>\medskip See @{file "~~/src/Pure/Concurrent/mailbox.ML"} how
  1930 text \<open>
       
  1931   \<^medskip>
       
  1932   See @{file "~~/src/Pure/Concurrent/mailbox.ML"} how
  1917   to implement a mailbox as synchronized variable over a purely
  1933   to implement a mailbox as synchronized variable over a purely
  1918   functional list.\<close>
  1934   functional list.\<close>
  1919 
  1935 
  1920 
  1936 
  1921 section \<open>Managed evaluation\<close>
  1937 section \<open>Managed evaluation\<close>
  1931   specifically when and how evaluation happens.  For example, the
  1947   specifically when and how evaluation happens.  For example, the
  1932   Isabelle/ML library supports lazy evaluation with memoing, parallel
  1948   Isabelle/ML library supports lazy evaluation with memoing, parallel
  1933   evaluation via futures, asynchronous evaluation via promises,
  1949   evaluation via futures, asynchronous evaluation via promises,
  1934   evaluation with time limit etc.
  1950   evaluation with time limit etc.
  1935 
  1951 
  1936   \medskip An \emph{unevaluated expression} is represented either as
  1952   \<^medskip>
       
  1953   An \emph{unevaluated expression} is represented either as
  1937   unit abstraction @{verbatim "fn () => a"} of type
  1954   unit abstraction @{verbatim "fn () => a"} of type
  1938   @{verbatim "unit -> 'a"} or as regular function
  1955   @{verbatim "unit -> 'a"} or as regular function
  1939   @{verbatim "fn a => b"} of type @{verbatim "'a -> 'b"}.  Both forms
  1956   @{verbatim "fn a => b"} of type @{verbatim "'a -> 'b"}.  Both forms
  1940   occur routinely, and special care is required to tell them apart ---
  1957   occur routinely, and special care is required to tell them apart ---
  1941   the static type-system of SML is only of limited help here.
  1958   the static type-system of SML is only of limited help here.
  1946   some combinator @{text "('a -> 'b) -> 'a -> 'b"} acts like a
  1963   some combinator @{text "('a -> 'b) -> 'a -> 'b"} acts like a
  1947   modified form of function application; several such combinators may
  1964   modified form of function application; several such combinators may
  1948   be cascaded to modify a given function, before it is ultimately
  1965   be cascaded to modify a given function, before it is ultimately
  1949   applied to some argument.
  1966   applied to some argument.
  1950 
  1967 
  1951   \medskip \emph{Reified results} make the disjoint sum of regular
  1968   \<^medskip>
       
  1969   \emph{Reified results} make the disjoint sum of regular
  1952   values versions exceptional situations explicit as ML datatype:
  1970   values versions exceptional situations explicit as ML datatype:
  1953   @{text "'a result = Res of 'a | Exn of exn"}.  This is typically
  1971   @{text "'a result = Res of 'a | Exn of exn"}.  This is typically
  1954   used for administrative purposes, to store the overall outcome of an
  1972   used for administrative purposes, to store the overall outcome of an
  1955   evaluation process.
  1973   evaluation process.
  1956 
  1974 
  2156   wait operations), or if non-worker threads contend for significant runtime
  2174   wait operations), or if non-worker threads contend for significant runtime
  2157   resources independently. There is a limited number of replacement worker
  2175   resources independently. There is a limited number of replacement worker
  2158   threads that get activated in certain explicit wait conditions, after a
  2176   threads that get activated in certain explicit wait conditions, after a
  2159   timeout.
  2177   timeout.
  2160 
  2178 
  2161   \medskip Each future task belongs to some \emph{task group}, which
  2179   \<^medskip>
       
  2180   Each future task belongs to some \emph{task group}, which
  2162   represents the hierarchic structure of related tasks, together with the
  2181   represents the hierarchic structure of related tasks, together with the
  2163   exception status a that point. By default, the task group of a newly created
  2182   exception status a that point. By default, the task group of a newly created
  2164   future is a new sub-group of the presently running one, but it is also
  2183   future is a new sub-group of the presently running one, but it is also
  2165   possible to indicate different group layouts under program control.
  2184   possible to indicate different group layouts under program control.
  2166 
  2185 
  2171   particular task group, its \emph{group status} cumulates all relevant
  2190   particular task group, its \emph{group status} cumulates all relevant
  2172   exceptions according to its position within the group hierarchy. Interrupted
  2191   exceptions according to its position within the group hierarchy. Interrupted
  2173   tasks that lack regular result information, will pick up parallel exceptions
  2192   tasks that lack regular result information, will pick up parallel exceptions
  2174   from the cumulative group status.
  2193   from the cumulative group status.
  2175 
  2194 
  2176   \medskip A \emph{passive future} or \emph{promise} is a future with slightly
  2195   \<^medskip>
       
  2196   A \emph{passive future} or \emph{promise} is a future with slightly
  2177   different evaluation policies: there is only a single-assignment variable
  2197   different evaluation policies: there is only a single-assignment variable
  2178   and some expression to evaluate for the \emph{failed} case (e.g.\ to clean
  2198   and some expression to evaluate for the \emph{failed} case (e.g.\ to clean
  2179   up resources when canceled). A regular result is produced by external means,
  2199   up resources when canceled). A regular result is produced by external means,
  2180   using a separate \emph{fulfill} operation.
  2200   using a separate \emph{fulfill} operation.
  2181 
  2201 
  2215   fork several futures simultaneously. The @{text params} consist of the
  2235   fork several futures simultaneously. The @{text params} consist of the
  2216   following fields:
  2236   following fields:
  2217 
  2237 
  2218   \begin{itemize}
  2238   \begin{itemize}
  2219 
  2239 
  2220   \item @{text "name : string"} (default @{ML "\"\""}) specifies a common name
  2240   \<^item> @{text "name : string"} (default @{ML "\"\""}) specifies a common name
  2221   for the tasks of the forked futures, which serves diagnostic purposes.
  2241   for the tasks of the forked futures, which serves diagnostic purposes.
  2222 
  2242 
  2223   \item @{text "group : Future.group option"} (default @{ML NONE}) specifies
  2243   \<^item> @{text "group : Future.group option"} (default @{ML NONE}) specifies
  2224   an optional task group for the forked futures. @{ML NONE} means that a new
  2244   an optional task group for the forked futures. @{ML NONE} means that a new
  2225   sub-group of the current worker-thread task context is created. If this is
  2245   sub-group of the current worker-thread task context is created. If this is
  2226   not a worker thread, the group will be a new root in the group hierarchy.
  2246   not a worker thread, the group will be a new root in the group hierarchy.
  2227 
  2247 
  2228   \item @{text "deps : Future.task list"} (default @{ML "[]"}) specifies
  2248   \<^item> @{text "deps : Future.task list"} (default @{ML "[]"}) specifies
  2229   dependencies on other future tasks, i.e.\ the adjacency relation in the
  2249   dependencies on other future tasks, i.e.\ the adjacency relation in the
  2230   global task queue. Dependencies on already finished tasks are ignored.
  2250   global task queue. Dependencies on already finished tasks are ignored.
  2231 
  2251 
  2232   \item @{text "pri : int"} (default @{ML 0}) specifies a priority within the
  2252   \<^item> @{text "pri : int"} (default @{ML 0}) specifies a priority within the
  2233   task queue.
  2253   task queue.
  2234 
  2254 
  2235   Typically there is only little deviation from the default priority @{ML 0}.
  2255   Typically there is only little deviation from the default priority @{ML 0}.
  2236   As a rule of thumb, @{ML "~1"} means ``low priority" and @{ML 1} means
  2256   As a rule of thumb, @{ML "~1"} means ``low priority" and @{ML 1} means
  2237   ``high priority''.
  2257   ``high priority''.
  2240   thread priority. When a worker thread picks up a task for processing, it
  2260   thread priority. When a worker thread picks up a task for processing, it
  2241   runs with the normal thread priority to the end (or until canceled). Higher
  2261   runs with the normal thread priority to the end (or until canceled). Higher
  2242   priority tasks that are queued later need to wait until this (or another)
  2262   priority tasks that are queued later need to wait until this (or another)
  2243   worker thread becomes free again.
  2263   worker thread becomes free again.
  2244 
  2264 
  2245   \item @{text "interrupts : bool"} (default @{ML true}) tells whether the
  2265   \<^item> @{text "interrupts : bool"} (default @{ML true}) tells whether the
  2246   worker thread that processes the corresponding task is initially put into
  2266   worker thread that processes the corresponding task is initially put into
  2247   interruptible state. This state may change again while running, by modifying
  2267   interruptible state. This state may change again while running, by modifying
  2248   the thread attributes.
  2268   the thread attributes.
  2249 
  2269 
  2250   With interrupts disabled, a running future task cannot be canceled.  It is
  2270   With interrupts disabled, a running future task cannot be canceled.  It is