src/Doc/Implementation/ML.thy
changeset 61439 2bf52eec4e8a
parent 61416 b9a3324e4e62
child 61458 987533262fc2
equal deleted inserted replaced
61438:151f894984d8 61439:2bf52eec4e8a
   258   Proper semantic names override these conventions completely.  For
   258   Proper semantic names override these conventions completely.  For
   259   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
   260   @{ML_text lhs} (not @{ML_text lhs_tm}).  Or a term that is known
   260   @{ML_text lhs} (not @{ML_text lhs_tm}).  Or a term that is known
   261   to be a variable can be called @{ML_text v} or @{ML_text x}.
   261   to be a variable can be called @{ML_text v} or @{ML_text x}.
   262 
   262 
   263   \item Tactics (\secref{sec:tactics}) are sufficiently important to
   263   \<^item> Tactics (\secref{sec:tactics}) are sufficiently important to
   264   have specific naming conventions.  The name of a basic tactic
   264   have specific naming conventions.  The name of a basic tactic
   265   definition always has a @{ML_text "_tac"} suffix, the subgoal index
   265   definition always has a @{ML_text "_tac"} suffix, the subgoal index
   266   (if applicable) is always called @{ML_text i}, and the goal state
   266   (if applicable) is always called @{ML_text i}, and the goal state
   267   (if made explicit) is usually called @{ML_text st} instead of the
   267   (if made explicit) is usually called @{ML_text st} instead of the
   268   somewhat misleading @{ML_text thm}.  Any other arguments are given
   268   somewhat misleading @{ML_text thm}.  Any other arguments are given
   646   @{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\
   646   @{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\
   647   \end{mldecls}
   647   \end{mldecls}
   648 
   648 
   649   \begin{description}
   649   \begin{description}
   650 
   650 
   651   \item @{ML "ML_Context.the_generic_context ()"} refers to the theory
   651   \<^descr> @{ML "ML_Context.the_generic_context ()"} refers to the theory
   652   context of the ML toplevel --- at compile time.  ML code needs to
   652   context of the ML toplevel --- at compile time.  ML code needs to
   653   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   653   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   654   correctly.  Recall that evaluation of a function body is delayed
   654   correctly.  Recall that evaluation of a function body is delayed
   655   until actual run-time.
   655   until actual run-time.
   656 
   656 
   657   \item @{ML "Context.>>"}~@{text f} applies context transformation
   657   \<^descr> @{ML "Context.>>"}~@{text f} applies context transformation
   658   @{text f} to the implicit context of the ML toplevel.
   658   @{text f} to the implicit context of the ML toplevel.
   659 
   659 
   660   \item @{ML ML_Thms.bind_thms}~@{text "(name, thms)"} stores a list of
   660   \<^descr> @{ML ML_Thms.bind_thms}~@{text "(name, thms)"} stores a list of
   661   theorems produced in ML both in the (global) theory context and the
   661   theorems produced in ML both in the (global) theory context and the
   662   ML toplevel, associating it with the provided name.
   662   ML toplevel, associating it with the provided name.
   663 
   663 
   664   \item @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but
   664   \<^descr> @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but
   665   refers to a singleton fact.
   665   refers to a singleton fact.
   666 
   666 
   667   \end{description}
   667   \end{description}
   668 
   668 
   669   It is important to note that the above functions are really
   669   It is important to note that the above functions are really
   725   @@{ML_antiquotation print} @{syntax name}?
   725   @@{ML_antiquotation print} @{syntax name}?
   726   \<close>}
   726   \<close>}
   727 
   727 
   728   \begin{description}
   728   \begin{description}
   729 
   729 
   730   \item @{text "@{make_string}"} inlines a function to print arbitrary values
   730   \<^descr> @{text "@{make_string}"} inlines a function to print arbitrary values
   731   similar to the ML toplevel. The result is compiler dependent and may fall
   731   similar to the ML toplevel. The result is compiler dependent and may fall
   732   back on "?" in certain situations. The value of configuration option
   732   back on "?" in certain situations. The value of configuration option
   733   @{attribute_ref ML_print_depth} determines further details of output.
   733   @{attribute_ref ML_print_depth} determines further details of output.
   734 
   734 
   735   \item @{text "@{print f}"} uses the ML function @{text "f: string ->
   735   \<^descr> @{text "@{print f}"} uses the ML function @{text "f: string ->
   736   unit"} to output the result of @{text "@{make_string}"} above,
   736   unit"} to output the result of @{text "@{make_string}"} above,
   737   together with the source position of the antiquotation.  The default
   737   together with the source position of the antiquotation.  The default
   738   output function is @{ML writeln}.
   738   output function is @{ML writeln}.
   739 
   739 
   740   \end{description}
   740   \end{description}
   907   @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
   907   @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
   908   \end{mldecls}
   908   \end{mldecls}
   909 
   909 
   910   \begin{description}
   910   \begin{description}
   911 
   911 
   912   \item @{ML fold}~@{text f} lifts the parametrized update function
   912   \<^descr> @{ML fold}~@{text f} lifts the parametrized update function
   913   @{text "f"} to a list of parameters.
   913   @{text "f"} to a list of parameters.
   914 
   914 
   915   \item @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text
   915   \<^descr> @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text
   916   "f"}, but works inside-out, as if the list would be reversed.
   916   "f"}, but works inside-out, as if the list would be reversed.
   917 
   917 
   918   \item @{ML fold_map}~@{text "f"} lifts the parametrized update
   918   \<^descr> @{ML fold_map}~@{text "f"} lifts the parametrized update
   919   function @{text "f"} (with side-result) to a list of parameters and
   919   function @{text "f"} (with side-result) to a list of parameters and
   920   cumulative side-results.
   920   cumulative side-results.
   921 
   921 
   922   \end{description}
   922   \end{description}
   923 
   923 
  1033   @{index_ML error: "string -> 'a"} % FIXME Output.error_message (!?) \\
  1033   @{index_ML error: "string -> 'a"} % FIXME Output.error_message (!?) \\
  1034   \end{mldecls}
  1034   \end{mldecls}
  1035 
  1035 
  1036   \begin{description}
  1036   \begin{description}
  1037 
  1037 
  1038   \item @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
  1038   \<^descr> @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
  1039   message.  This is the primary message output operation of Isabelle
  1039   message.  This is the primary message output operation of Isabelle
  1040   and should be used by default.
  1040   and should be used by default.
  1041 
  1041 
  1042   \item @{ML tracing}~@{text "text"} outputs @{text "text"} as special
  1042   \<^descr> @{ML tracing}~@{text "text"} outputs @{text "text"} as special
  1043   tracing message, indicating potential high-volume output to the
  1043   tracing message, indicating potential high-volume output to the
  1044   front-end (hundreds or thousands of messages issued by a single
  1044   front-end (hundreds or thousands of messages issued by a single
  1045   command).  The idea is to allow the user-interface to downgrade the
  1045   command).  The idea is to allow the user-interface to downgrade the
  1046   quality of message display to achieve higher throughput.
  1046   quality of message display to achieve higher throughput.
  1047 
  1047 
  1048   Note that the user might have to take special actions to see tracing
  1048   Note that the user might have to take special actions to see tracing
  1049   output, e.g.\ switch to a different output window.  So this channel
  1049   output, e.g.\ switch to a different output window.  So this channel
  1050   should not be used for regular output.
  1050   should not be used for regular output.
  1051 
  1051 
  1052   \item @{ML warning}~@{text "text"} outputs @{text "text"} as
  1052   \<^descr> @{ML warning}~@{text "text"} outputs @{text "text"} as
  1053   warning, which typically means some extra emphasis on the front-end
  1053   warning, which typically means some extra emphasis on the front-end
  1054   side (color highlighting, icons, etc.).
  1054   side (color highlighting, icons, etc.).
  1055 
  1055 
  1056   \item @{ML error}~@{text "text"} raises exception @{ML ERROR}~@{text
  1056   \<^descr> @{ML error}~@{text "text"} raises exception @{ML ERROR}~@{text
  1057   "text"} and thus lets the Isar toplevel print @{text "text"} on the
  1057   "text"} and thus lets the Isar toplevel print @{text "text"} on the
  1058   error channel, which typically means some extra emphasis on the
  1058   error channel, which typically means some extra emphasis on the
  1059   front-end side (color highlighting, icons, etc.).
  1059   front-end side (color highlighting, icons, etc.).
  1060 
  1060 
  1061   This assumes that the exception is not handled before the command
  1061   This assumes that the exception is not handled before the command
  1215   @{index_ML Runtime.exn_trace: "(unit -> 'a) -> 'a"} \\
  1215   @{index_ML Runtime.exn_trace: "(unit -> 'a) -> 'a"} \\
  1216   \end{mldecls}
  1216   \end{mldecls}
  1217 
  1217 
  1218   \begin{description}
  1218   \begin{description}
  1219 
  1219 
  1220   \item @{ML try}~@{text "f x"} makes the partiality of evaluating
  1220   \<^descr> @{ML try}~@{text "f x"} makes the partiality of evaluating
  1221   @{text "f x"} explicit via the option datatype.  Interrupts are
  1221   @{text "f x"} explicit via the option datatype.  Interrupts are
  1222   \emph{not} handled here, i.e.\ this form serves as safe replacement
  1222   \emph{not} handled here, i.e.\ this form serves as safe replacement
  1223   for the \emph{unsafe} version @{ML_text "(SOME"}~@{text "f
  1223   for the \emph{unsafe} version @{ML_text "(SOME"}~@{text "f
  1224   x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in
  1224   x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in
  1225   books about SML97, but not in Isabelle/ML.
  1225   books about SML97, but not in Isabelle/ML.
  1226 
  1226 
  1227   \item @{ML can} is similar to @{ML try} with more abstract result.
  1227   \<^descr> @{ML can} is similar to @{ML try} with more abstract result.
  1228 
  1228 
  1229   \item @{ML ERROR}~@{text "msg"} represents user errors; this
  1229   \<^descr> @{ML ERROR}~@{text "msg"} represents user errors; this
  1230   exception is normally raised indirectly via the @{ML error} function
  1230   exception is normally raised indirectly via the @{ML error} function
  1231   (see \secref{sec:message-channels}).
  1231   (see \secref{sec:message-channels}).
  1232 
  1232 
  1233   \item @{ML Fail}~@{text "msg"} represents general program failures.
  1233   \<^descr> @{ML Fail}~@{text "msg"} represents general program failures.
  1234 
  1234 
  1235   \item @{ML Exn.is_interrupt} identifies interrupts robustly, without
  1235   \<^descr> @{ML Exn.is_interrupt} identifies interrupts robustly, without
  1236   mentioning concrete exception constructors in user code.  Handled
  1236   mentioning concrete exception constructors in user code.  Handled
  1237   interrupts need to be re-raised promptly!
  1237   interrupts need to be re-raised promptly!
  1238 
  1238 
  1239   \item @{ML reraise}~@{text "exn"} raises exception @{text "exn"}
  1239   \<^descr> @{ML reraise}~@{text "exn"} raises exception @{text "exn"}
  1240   while preserving its implicit position information (if possible,
  1240   while preserving its implicit position information (if possible,
  1241   depending on the ML platform).
  1241   depending on the ML platform).
  1242 
  1242 
  1243   \item @{ML Runtime.exn_trace}~@{ML_text "(fn () =>"}~@{text
  1243   \<^descr> @{ML Runtime.exn_trace}~@{ML_text "(fn () =>"}~@{text
  1244   "e"}@{ML_text ")"} evaluates expression @{text "e"} while printing
  1244   "e"}@{ML_text ")"} evaluates expression @{text "e"} while printing
  1245   a full trace of its stack of nested exceptions (if possible,
  1245   a full trace of its stack of nested exceptions (if possible,
  1246   depending on the ML platform).
  1246   depending on the ML platform).
  1247 
  1247 
  1248   Inserting @{ML Runtime.exn_trace} into ML code temporarily is
  1248   Inserting @{ML Runtime.exn_trace} into ML code temporarily is
  1256   @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
  1256   @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
  1257   \end{matharray}
  1257   \end{matharray}
  1258 
  1258 
  1259   \begin{description}
  1259   \begin{description}
  1260 
  1260 
  1261   \item @{text "@{assert}"} inlines a function
  1261   \<^descr> @{text "@{assert}"} inlines a function
  1262   @{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is
  1262   @{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is
  1263   @{ML false}.  Due to inlining the source position of failed
  1263   @{ML false}.  Due to inlining the source position of failed
  1264   assertions is included in the error output.
  1264   assertions is included in the error output.
  1265 
  1265 
  1266   \end{description}
  1266   \end{description}
  1337   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
  1337   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
  1338   \end{mldecls}
  1338   \end{mldecls}
  1339 
  1339 
  1340   \begin{description}
  1340   \begin{description}
  1341 
  1341 
  1342   \item Type @{ML_type "Symbol.symbol"} represents individual Isabelle
  1342   \<^descr> Type @{ML_type "Symbol.symbol"} represents individual Isabelle
  1343   symbols.
  1343   symbols.
  1344 
  1344 
  1345   \item @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
  1345   \<^descr> @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
  1346   from the packed form.  This function supersedes @{ML
  1346   from the packed form.  This function supersedes @{ML
  1347   "String.explode"} for virtually all purposes of manipulating text in
  1347   "String.explode"} for virtually all purposes of manipulating text in
  1348   Isabelle!\footnote{The runtime overhead for exploded strings is
  1348   Isabelle!\footnote{The runtime overhead for exploded strings is
  1349   mainly that of the list structure: individual symbols that happen to
  1349   mainly that of the list structure: individual symbols that happen to
  1350   be a singleton string do not require extra memory in Poly/ML.}
  1350   be a singleton string do not require extra memory in Poly/ML.}
  1351 
  1351 
  1352   \item @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
  1352   \<^descr> @{ML "Symbol.is_letter"}, @{ML "Symbol.is_digit"}, @{ML
  1353   "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify standard
  1353   "Symbol.is_quasi"}, @{ML "Symbol.is_blank"} classify standard
  1354   symbols according to fixed syntactic conventions of Isabelle, cf.\
  1354   symbols according to fixed syntactic conventions of Isabelle, cf.\
  1355   @{cite "isabelle-isar-ref"}.
  1355   @{cite "isabelle-isar-ref"}.
  1356 
  1356 
  1357   \item Type @{ML_type "Symbol.sym"} is a concrete datatype that
  1357   \<^descr> Type @{ML_type "Symbol.sym"} is a concrete datatype that
  1358   represents the different kinds of symbols explicitly, with
  1358   represents the different kinds of symbols explicitly, with
  1359   constructors @{ML "Symbol.Char"}, @{ML "Symbol.UTF8"},
  1359   constructors @{ML "Symbol.Char"}, @{ML "Symbol.UTF8"},
  1360   @{ML "Symbol.Sym"}, @{ML "Symbol.Ctrl"}, @{ML "Symbol.Raw"},
  1360   @{ML "Symbol.Sym"}, @{ML "Symbol.Ctrl"}, @{ML "Symbol.Raw"},
  1361   @{ML "Symbol.Malformed"}.
  1361   @{ML "Symbol.Malformed"}.
  1362 
  1362 
  1363   \item @{ML "Symbol.decode"} converts the string representation of a
  1363   \<^descr> @{ML "Symbol.decode"} converts the string representation of a
  1364   symbol into the datatype version.
  1364   symbol into the datatype version.
  1365 
  1365 
  1366   \end{description}
  1366   \end{description}
  1367 
  1367 
  1368   \paragraph{Historical note.} In the original SML90 standard the
  1368   \paragraph{Historical note.} In the original SML90 standard the
  1399   @{index_ML_type char} \\
  1399   @{index_ML_type char} \\
  1400   \end{mldecls}
  1400   \end{mldecls}
  1401 
  1401 
  1402   \begin{description}
  1402   \begin{description}
  1403 
  1403 
  1404   \item Type @{ML_type char} is \emph{not} used.  The smallest textual
  1404   \<^descr> Type @{ML_type char} is \emph{not} used.  The smallest textual
  1405   unit in Isabelle is represented as a ``symbol'' (see
  1405   unit in Isabelle is represented as a ``symbol'' (see
  1406   \secref{sec:symbols}).
  1406   \secref{sec:symbols}).
  1407 
  1407 
  1408   \end{description}
  1408   \end{description}
  1409 \<close>
  1409 \<close>
  1416   @{index_ML_type string} \\
  1416   @{index_ML_type string} \\
  1417   \end{mldecls}
  1417   \end{mldecls}
  1418 
  1418 
  1419   \begin{description}
  1419   \begin{description}
  1420 
  1420 
  1421   \item Type @{ML_type string} represents immutable vectors of 8-bit
  1421   \<^descr> Type @{ML_type string} represents immutable vectors of 8-bit
  1422   characters.  There are operations in SML to convert back and forth
  1422   characters.  There are operations in SML to convert back and forth
  1423   to actual byte vectors, which are seldom used.
  1423   to actual byte vectors, which are seldom used.
  1424 
  1424 
  1425   This historically important raw text representation is used for
  1425   This historically important raw text representation is used for
  1426   Isabelle-specific purposes with the following implicit substructures
  1426   Isabelle-specific purposes with the following implicit substructures
  1471   @{index_ML_type int} \\
  1471   @{index_ML_type int} \\
  1472   \end{mldecls}
  1472   \end{mldecls}
  1473 
  1473 
  1474   \begin{description}
  1474   \begin{description}
  1475 
  1475 
  1476   \item Type @{ML_type int} represents regular mathematical integers, which
  1476   \<^descr> Type @{ML_type int} represents regular mathematical integers, which
  1477   are \emph{unbounded}. Overflow is treated properly, but should never happen
  1477   are \emph{unbounded}. Overflow is treated properly, but should never happen
  1478   in practice.\footnote{The size limit for integer bit patterns in memory is
  1478   in practice.\footnote{The size limit for integer bit patterns in memory is
  1479   64\,MB for 32-bit Poly/ML, and much higher for 64-bit systems.} This works
  1479   64\,MB for 32-bit Poly/ML, and much higher for 64-bit systems.} This works
  1480   uniformly for all supported ML platforms (Poly/ML and SML/NJ).
  1480   uniformly for all supported ML platforms (Poly/ML and SML/NJ).
  1481 
  1481 
  1499   @{index_ML seconds: "real -> Time.time"} \\
  1499   @{index_ML seconds: "real -> Time.time"} \\
  1500   \end{mldecls}
  1500   \end{mldecls}
  1501 
  1501 
  1502   \begin{description}
  1502   \begin{description}
  1503 
  1503 
  1504   \item Type @{ML_type Time.time} represents time abstractly according
  1504   \<^descr> Type @{ML_type Time.time} represents time abstractly according
  1505   to the SML97 basis library definition.  This is adequate for
  1505   to the SML97 basis library definition.  This is adequate for
  1506   internal ML operations, but awkward in concrete time specifications.
  1506   internal ML operations, but awkward in concrete time specifications.
  1507 
  1507 
  1508   \item @{ML seconds}~@{text "s"} turns the concrete scalar @{text
  1508   \<^descr> @{ML seconds}~@{text "s"} turns the concrete scalar @{text
  1509   "s"} (measured in seconds) into an abstract time value.  Floating
  1509   "s"} (measured in seconds) into an abstract time value.  Floating
  1510   point numbers are easy to use as configuration options in the
  1510   point numbers are easy to use as configuration options in the
  1511   context (see \secref{sec:config-options}) or system options that
  1511   context (see \secref{sec:config-options}) or system options that
  1512   are maintained externally.
  1512   are maintained externally.
  1513 
  1513 
  1551   @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1551   @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1552   \end{mldecls}
  1552   \end{mldecls}
  1553 
  1553 
  1554   \begin{description}
  1554   \begin{description}
  1555 
  1555 
  1556   \item @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
  1556   \<^descr> @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
  1557 
  1557 
  1558   Tupled infix operators are a historical accident in Standard ML.
  1558   Tupled infix operators are a historical accident in Standard ML.
  1559   The curried @{ML cons} amends this, but it should be only used when
  1559   The curried @{ML cons} amends this, but it should be only used when
  1560   partial application is required.
  1560   partial application is required.
  1561 
  1561 
  1562   \item @{ML member}, @{ML insert}, @{ML remove}, @{ML update} treat
  1562   \<^descr> @{ML member}, @{ML insert}, @{ML remove}, @{ML update} treat
  1563   lists as a set-like container that maintains the order of elements.
  1563   lists as a set-like container that maintains the order of elements.
  1564   See @{file "~~/src/Pure/library.ML"} for the full specifications
  1564   See @{file "~~/src/Pure/library.ML"} for the full specifications
  1565   (written in ML).  There are some further derived operations like
  1565   (written in ML).  There are some further derived operations like
  1566   @{ML union} or @{ML inter}.
  1566   @{ML union} or @{ML inter}.
  1567 
  1567 
  1627   @{index_ML AList.update: "('a * 'a -> bool) -> 'a * 'b -> ('a * 'b) list -> ('a * 'b) list"} \\
  1627   @{index_ML AList.update: "('a * 'a -> bool) -> 'a * 'b -> ('a * 'b) list -> ('a * 'b) list"} \\
  1628   \end{mldecls}
  1628   \end{mldecls}
  1629 
  1629 
  1630   \begin{description}
  1630   \begin{description}
  1631 
  1631 
  1632   \item @{ML AList.lookup}, @{ML AList.defined}, @{ML AList.update}
  1632   \<^descr> @{ML AList.lookup}, @{ML AList.defined}, @{ML AList.update}
  1633   implement the main ``framework operations'' for mappings in
  1633   implement the main ``framework operations'' for mappings in
  1634   Isabelle/ML, following standard conventions for their names and
  1634   Isabelle/ML, following standard conventions for their names and
  1635   types.
  1635   types.
  1636 
  1636 
  1637   Note that a function called @{verbatim lookup} is obliged to express its
  1637   Note that a function called @{verbatim lookup} is obliged to express its
  1834   @{index_ML serial_string: "unit -> string"} \\
  1834   @{index_ML serial_string: "unit -> string"} \\
  1835   \end{mldecls}
  1835   \end{mldecls}
  1836 
  1836 
  1837   \begin{description}
  1837   \begin{description}
  1838 
  1838 
  1839   \item @{ML File.tmp_path}~@{text "path"} relocates the base
  1839   \<^descr> @{ML File.tmp_path}~@{text "path"} relocates the base
  1840   component of @{text "path"} into the unique temporary directory of
  1840   component of @{text "path"} into the unique temporary directory of
  1841   the running Isabelle/ML process.
  1841   the running Isabelle/ML process.
  1842 
  1842 
  1843   \item @{ML serial_string}~@{text "()"} creates a new serial number
  1843   \<^descr> @{ML serial_string}~@{text "()"} creates a new serial number
  1844   that is unique over the runtime of the Isabelle/ML process.
  1844   that is unique over the runtime of the Isabelle/ML process.
  1845 
  1845 
  1846   \end{description}
  1846   \end{description}
  1847 \<close>
  1847 \<close>
  1848 
  1848 
  1881   ('a -> ('b * 'a) option) -> 'b"} \\
  1881   ('a -> ('b * 'a) option) -> 'b"} \\
  1882   \end{mldecls}
  1882   \end{mldecls}
  1883 
  1883 
  1884   \begin{description}
  1884   \begin{description}
  1885 
  1885 
  1886   \item Type @{ML_type "'a Synchronized.var"} represents synchronized
  1886   \<^descr> Type @{ML_type "'a Synchronized.var"} represents synchronized
  1887   variables with state of type @{ML_type 'a}.
  1887   variables with state of type @{ML_type 'a}.
  1888 
  1888 
  1889   \item @{ML Synchronized.var}~@{text "name x"} creates a synchronized
  1889   \<^descr> @{ML Synchronized.var}~@{text "name x"} creates a synchronized
  1890   variable that is initialized with value @{text "x"}.  The @{text
  1890   variable that is initialized with value @{text "x"}.  The @{text
  1891   "name"} is used for tracing.
  1891   "name"} is used for tracing.
  1892 
  1892 
  1893   \item @{ML Synchronized.guarded_access}~@{text "var f"} lets the
  1893   \<^descr> @{ML Synchronized.guarded_access}~@{text "var f"} lets the
  1894   function @{text "f"} operate within a critical section on the state
  1894   function @{text "f"} operate within a critical section on the state
  1895   @{text "x"} as follows: if @{text "f x"} produces @{ML NONE}, it
  1895   @{text "x"} as follows: if @{text "f x"} produces @{ML NONE}, it
  1896   continues to wait on the internal condition variable, expecting that
  1896   continues to wait on the internal condition variable, expecting that
  1897   some other thread will eventually change the content in a suitable
  1897   some other thread will eventually change the content in a suitable
  1898   manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is
  1898   manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is
  1994   @{index_ML Par_Exn.release_first: "'a Exn.result list -> 'a list"} \\
  1994   @{index_ML Par_Exn.release_first: "'a Exn.result list -> 'a list"} \\
  1995   \end{mldecls}
  1995   \end{mldecls}
  1996 
  1996 
  1997   \begin{description}
  1997   \begin{description}
  1998 
  1998 
  1999   \item Type @{ML_type "'a Exn.result"} represents the disjoint sum of
  1999   \<^descr> Type @{ML_type "'a Exn.result"} represents the disjoint sum of
  2000   ML results explicitly, with constructor @{ML Exn.Res} for regular
  2000   ML results explicitly, with constructor @{ML Exn.Res} for regular
  2001   values and @{ML "Exn.Exn"} for exceptions.
  2001   values and @{ML "Exn.Exn"} for exceptions.
  2002 
  2002 
  2003   \item @{ML Exn.capture}~@{text "f x"} manages the evaluation of
  2003   \<^descr> @{ML Exn.capture}~@{text "f x"} manages the evaluation of
  2004   @{text "f x"} such that exceptions are made explicit as @{ML
  2004   @{text "f x"} such that exceptions are made explicit as @{ML
  2005   "Exn.Exn"}.  Note that this includes physical interrupts (see also
  2005   "Exn.Exn"}.  Note that this includes physical interrupts (see also
  2006   \secref{sec:exceptions}), so the same precautions apply to user
  2006   \secref{sec:exceptions}), so the same precautions apply to user
  2007   code: interrupts must not be absorbed accidentally!
  2007   code: interrupts must not be absorbed accidentally!
  2008 
  2008 
  2009   \item @{ML Exn.interruptible_capture} is similar to @{ML
  2009   \<^descr> @{ML Exn.interruptible_capture} is similar to @{ML
  2010   Exn.capture}, but interrupts are immediately re-raised as required
  2010   Exn.capture}, but interrupts are immediately re-raised as required
  2011   for user code.
  2011   for user code.
  2012 
  2012 
  2013   \item @{ML Exn.release}~@{text "result"} releases the original
  2013   \<^descr> @{ML Exn.release}~@{text "result"} releases the original
  2014   runtime result, exposing its regular value or raising the reified
  2014   runtime result, exposing its regular value or raising the reified
  2015   exception.
  2015   exception.
  2016 
  2016 
  2017   \item @{ML Par_Exn.release_all}~@{text "results"} combines results
  2017   \<^descr> @{ML Par_Exn.release_all}~@{text "results"} combines results
  2018   that were produced independently (e.g.\ by parallel evaluation).  If
  2018   that were produced independently (e.g.\ by parallel evaluation).  If
  2019   all results are regular values, that list is returned.  Otherwise,
  2019   all results are regular values, that list is returned.  Otherwise,
  2020   the collection of all exceptions is raised, wrapped-up as collective
  2020   the collection of all exceptions is raised, wrapped-up as collective
  2021   parallel exception.  Note that the latter prevents access to
  2021   parallel exception.  Note that the latter prevents access to
  2022   individual exceptions by conventional @{verbatim "handle"} of ML.
  2022   individual exceptions by conventional @{verbatim "handle"} of ML.
  2023 
  2023 
  2024   \item @{ML Par_Exn.release_first} is similar to @{ML
  2024   \<^descr> @{ML Par_Exn.release_first} is similar to @{ML
  2025   Par_Exn.release_all}, but only the first (meaningful) exception that has
  2025   Par_Exn.release_all}, but only the first (meaningful) exception that has
  2026   occurred in the original evaluation process is raised again, the others are
  2026   occurred in the original evaluation process is raised again, the others are
  2027   ignored.  That single exception may get handled by conventional
  2027   ignored.  That single exception may get handled by conventional
  2028   means in ML.
  2028   means in ML.
  2029 
  2029 
  2055   @{index_ML Par_List.get_some: "('a -> 'b option) -> 'a list -> 'b option"} \\
  2055   @{index_ML Par_List.get_some: "('a -> 'b option) -> 'a list -> 'b option"} \\
  2056   \end{mldecls}
  2056   \end{mldecls}
  2057 
  2057 
  2058   \begin{description}
  2058   \begin{description}
  2059 
  2059 
  2060   \item @{ML Par_List.map}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} is like @{ML
  2060   \<^descr> @{ML Par_List.map}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} is like @{ML
  2061   "map"}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"}, but the evaluation of @{text "f x\<^sub>i"}
  2061   "map"}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"}, but the evaluation of @{text "f x\<^sub>i"}
  2062   for @{text "i = 1, \<dots>, n"} is performed in parallel.
  2062   for @{text "i = 1, \<dots>, n"} is performed in parallel.
  2063 
  2063 
  2064   An exception in any @{text "f x\<^sub>i"} cancels the overall evaluation
  2064   An exception in any @{text "f x\<^sub>i"} cancels the overall evaluation
  2065   process.  The final result is produced via @{ML
  2065   process.  The final result is produced via @{ML
  2066   Par_Exn.release_first} as explained above, which means the first
  2066   Par_Exn.release_first} as explained above, which means the first
  2067   program exception that happened to occur in the parallel evaluation
  2067   program exception that happened to occur in the parallel evaluation
  2068   is propagated, and all other failures are ignored.
  2068   is propagated, and all other failures are ignored.
  2069 
  2069 
  2070   \item @{ML Par_List.get_some}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} produces some
  2070   \<^descr> @{ML Par_List.get_some}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} produces some
  2071   @{text "f x\<^sub>i"} that is of the form @{text "SOME y\<^sub>i"}, if that
  2071   @{text "f x\<^sub>i"} that is of the form @{text "SOME y\<^sub>i"}, if that
  2072   exists, otherwise @{text "NONE"}.  Thus it is similar to @{ML
  2072   exists, otherwise @{text "NONE"}.  Thus it is similar to @{ML
  2073   Library.get_first}, but subject to a non-deterministic parallel
  2073   Library.get_first}, but subject to a non-deterministic parallel
  2074   choice process.  The first successful result cancels the overall
  2074   choice process.  The first successful result cancels the overall
  2075   evaluation process; other exceptions are propagated as for @{ML
  2075   evaluation process; other exceptions are propagated as for @{ML
  2128   @{index_ML Lazy.force: "'a lazy -> 'a"} \\
  2128   @{index_ML Lazy.force: "'a lazy -> 'a"} \\
  2129   \end{mldecls}
  2129   \end{mldecls}
  2130 
  2130 
  2131   \begin{description}
  2131   \begin{description}
  2132 
  2132 
  2133   \item Type @{ML_type "'a lazy"} represents lazy values over type @{verbatim
  2133   \<^descr> Type @{ML_type "'a lazy"} represents lazy values over type @{verbatim
  2134   "'a"}.
  2134   "'a"}.
  2135 
  2135 
  2136   \item @{ML Lazy.lazy}~@{text "(fn () => e)"} wraps the unevaluated
  2136   \<^descr> @{ML Lazy.lazy}~@{text "(fn () => e)"} wraps the unevaluated
  2137   expression @{text e} as unfinished lazy value.
  2137   expression @{text e} as unfinished lazy value.
  2138 
  2138 
  2139   \item @{ML Lazy.value}~@{text a} wraps the value @{text a} as finished lazy
  2139   \<^descr> @{ML Lazy.value}~@{text a} wraps the value @{text a} as finished lazy
  2140   value.  When forced, it returns @{text a} without any further evaluation.
  2140   value.  When forced, it returns @{text a} without any further evaluation.
  2141 
  2141 
  2142   There is very low overhead for this proforma wrapping of strict values as
  2142   There is very low overhead for this proforma wrapping of strict values as
  2143   lazy values.
  2143   lazy values.
  2144 
  2144 
  2145   \item @{ML Lazy.force}~@{text x} produces the result of the lazy value in a
  2145   \<^descr> @{ML Lazy.force}~@{text x} produces the result of the lazy value in a
  2146   thread-safe manner as explained above. Thus it may cause the current thread
  2146   thread-safe manner as explained above. Thus it may cause the current thread
  2147   to wait on a pending evaluation attempt by another thread.
  2147   to wait on a pending evaluation attempt by another thread.
  2148 
  2148 
  2149   \end{description}
  2149   \end{description}
  2150 \<close>
  2150 \<close>
  2221   @{index_ML Future.fulfill: "'a future -> 'a -> unit"} \\
  2221   @{index_ML Future.fulfill: "'a future -> 'a -> unit"} \\
  2222   \end{mldecls}
  2222   \end{mldecls}
  2223 
  2223 
  2224   \begin{description}
  2224   \begin{description}
  2225 
  2225 
  2226   \item Type @{ML_type "'a future"} represents future values over type
  2226   \<^descr> Type @{ML_type "'a future"} represents future values over type
  2227   @{verbatim "'a"}.
  2227   @{verbatim "'a"}.
  2228 
  2228 
  2229   \item @{ML Future.fork}~@{text "(fn () => e)"} registers the unevaluated
  2229   \<^descr> @{ML Future.fork}~@{text "(fn () => e)"} registers the unevaluated
  2230   expression @{text e} as unfinished future value, to be evaluated eventually
  2230   expression @{text e} as unfinished future value, to be evaluated eventually
  2231   on the parallel worker-thread farm. This is a shorthand for @{ML
  2231   on the parallel worker-thread farm. This is a shorthand for @{ML
  2232   Future.forks} below, with default parameters and a single expression.
  2232   Future.forks} below, with default parameters and a single expression.
  2233 
  2233 
  2234   \item @{ML Future.forks}~@{text "params exprs"} is the general interface to
  2234   \<^descr> @{ML Future.forks}~@{text "params exprs"} is the general interface to
  2235   fork several futures simultaneously. The @{text params} consist of the
  2235   fork several futures simultaneously. The @{text params} consist of the
  2236   following fields:
  2236   following fields:
  2237 
  2237 
  2238   \begin{itemize}
  2238   \begin{itemize}
  2239 
  2239 
  2271   the responsibility of the programmer that this special state is retained
  2271   the responsibility of the programmer that this special state is retained
  2272   only briefly.
  2272   only briefly.
  2273 
  2273 
  2274   \end{itemize}
  2274   \end{itemize}
  2275 
  2275 
  2276   \item @{ML Future.join}~@{text x} retrieves the value of an already finished
  2276   \<^descr> @{ML Future.join}~@{text x} retrieves the value of an already finished
  2277   future, which may lead to an exception, according to the result of its
  2277   future, which may lead to an exception, according to the result of its
  2278   previous evaluation.
  2278   previous evaluation.
  2279 
  2279 
  2280   For an unfinished future there are several cases depending on the role of
  2280   For an unfinished future there are several cases depending on the role of
  2281   the current thread and the status of the future. A non-worker thread waits
  2281   the current thread and the status of the future. A non-worker thread waits
  2293 
  2293 
  2294   Whenever possible, static dependencies of futures should be specified
  2294   Whenever possible, static dependencies of futures should be specified
  2295   explicitly when forked (see @{text deps} above). Thus the evaluation can
  2295   explicitly when forked (see @{text deps} above). Thus the evaluation can
  2296   work from the bottom up, without join conflicts and wait states.
  2296   work from the bottom up, without join conflicts and wait states.
  2297 
  2297 
  2298   \item @{ML Future.joins}~@{text xs} joins the given list of futures
  2298   \<^descr> @{ML Future.joins}~@{text xs} joins the given list of futures
  2299   simultaneously, which is more efficient than @{ML "map Future.join"}~@{text
  2299   simultaneously, which is more efficient than @{ML "map Future.join"}~@{text
  2300   xs}.
  2300   xs}.
  2301 
  2301 
  2302   Based on the dependency graph of tasks, the current thread takes over the
  2302   Based on the dependency graph of tasks, the current thread takes over the
  2303   responsibility to evaluate future expressions that are required for the main
  2303   responsibility to evaluate future expressions that are required for the main
  2304   result, working from the bottom up. Waiting on future results that are
  2304   result, working from the bottom up. Waiting on future results that are
  2305   presently evaluated on other threads only happens as last resort, when no
  2305   presently evaluated on other threads only happens as last resort, when no
  2306   other unfinished futures are left over.
  2306   other unfinished futures are left over.
  2307 
  2307 
  2308   \item @{ML Future.value}~@{text a} wraps the value @{text a} as finished
  2308   \<^descr> @{ML Future.value}~@{text a} wraps the value @{text a} as finished
  2309   future value, bypassing the worker-thread farm. When joined, it returns
  2309   future value, bypassing the worker-thread farm. When joined, it returns
  2310   @{text a} without any further evaluation.
  2310   @{text a} without any further evaluation.
  2311 
  2311 
  2312   There is very low overhead for this proforma wrapping of strict values as
  2312   There is very low overhead for this proforma wrapping of strict values as
  2313   futures.
  2313   futures.
  2314 
  2314 
  2315   \item @{ML Future.map}~@{text "f x"} is a fast-path implementation of @{ML
  2315   \<^descr> @{ML Future.map}~@{text "f x"} is a fast-path implementation of @{ML
  2316   Future.fork}~@{text "(fn () => f ("}@{ML Future.join}~@{text "x))"}, which
  2316   Future.fork}~@{text "(fn () => f ("}@{ML Future.join}~@{text "x))"}, which
  2317   avoids the full overhead of the task queue and worker-thread farm as far as
  2317   avoids the full overhead of the task queue and worker-thread farm as far as
  2318   possible. The function @{text f} is supposed to be some trivial
  2318   possible. The function @{text f} is supposed to be some trivial
  2319   post-processing or projection of the future result.
  2319   post-processing or projection of the future result.
  2320 
  2320 
  2321   \item @{ML Future.cancel}~@{text "x"} cancels the task group of the given
  2321   \<^descr> @{ML Future.cancel}~@{text "x"} cancels the task group of the given
  2322   future, using @{ML Future.cancel_group} below.
  2322   future, using @{ML Future.cancel_group} below.
  2323 
  2323 
  2324   \item @{ML Future.cancel_group}~@{text "group"} cancels all tasks of the
  2324   \<^descr> @{ML Future.cancel_group}~@{text "group"} cancels all tasks of the
  2325   given task group for all time. Threads that are presently processing a task
  2325   given task group for all time. Threads that are presently processing a task
  2326   of the given group are interrupted: it may take some time until they are
  2326   of the given group are interrupted: it may take some time until they are
  2327   actually terminated. Tasks that are queued but not yet processed are
  2327   actually terminated. Tasks that are queued but not yet processed are
  2328   dequeued and forced into interrupted state. Since the task group is itself
  2328   dequeued and forced into interrupted state. Since the task group is itself
  2329   invalidated, any further attempt to fork a future that belongs to it will
  2329   invalidated, any further attempt to fork a future that belongs to it will
  2330   yield a canceled result as well.
  2330   yield a canceled result as well.
  2331 
  2331 
  2332   \item @{ML Future.promise}~@{text abort} registers a passive future with the
  2332   \<^descr> @{ML Future.promise}~@{text abort} registers a passive future with the
  2333   given @{text abort} operation: it is invoked when the future task group is
  2333   given @{text abort} operation: it is invoked when the future task group is
  2334   canceled.
  2334   canceled.
  2335 
  2335 
  2336   \item @{ML Future.fulfill}~@{text "x a"} finishes the passive future @{text
  2336   \<^descr> @{ML Future.fulfill}~@{text "x a"} finishes the passive future @{text
  2337   x} by the given value @{text a}. If the promise has already been canceled,
  2337   x} by the given value @{text a}. If the promise has already been canceled,
  2338   the attempt to fulfill it causes an exception.
  2338   the attempt to fulfill it causes an exception.
  2339 
  2339 
  2340   \end{description}
  2340   \end{description}
  2341 \<close>
  2341 \<close>