src/Doc/Implementation/ML.thy
changeset 61458 987533262fc2
parent 61439 2bf52eec4e8a
child 61459 5f2ddeb15c06
equal deleted inserted replaced
61457:3e21699bb83b 61458:987533262fc2
    74 
    74 
    75   The rest of the file is divided into sections, subsections,
    75   The rest of the file is divided into sections, subsections,
    76   subsubsections, paragraphs etc.\ using a simple layout via ML
    76   subsubsections, paragraphs etc.\ using a simple layout via ML
    77   comments as follows.
    77   comments as follows.
    78 
    78 
    79   \begin{verbatim}
    79   @{verbatim [display]
    80   (*** section ***)
    80 \<open>  (*** section ***)
    81 
    81 
    82   (** subsection **)
    82   (** subsection **)
    83 
    83 
    84   (* subsubsection *)
    84   (* subsubsection *)
    85 
    85 
    86   (*short paragraph*)
    86   (*short paragraph*)
    87 
    87 
    88   (*
    88   (*
    89     long paragraph,
    89     long paragraph,
    90     with more text
    90     with more text
    91   *)
    91   *)\<close>}
    92   \end{verbatim}
       
    93 
    92 
    94   As in regular typography, there is some extra space \emph{before}
    93   As in regular typography, there is some extra space \emph{before}
    95   section headings that are adjacent to plain text, but not other headings
    94   section headings that are adjacent to plain text, but not other headings
    96   as in the example above.
    95   as in the example above.
    97 
    96 
   153   called @{ML_text helper}, @{ML_text aux}, or @{ML_text f} is
   152   called @{ML_text helper}, @{ML_text aux}, or @{ML_text f} is
   154   considered bad style.
   153   considered bad style.
   155 
   154 
   156   Example:
   155   Example:
   157 
   156 
   158   \begin{verbatim}
   157   @{verbatim [display]
   159   (* RIGHT *)
   158 \<open>  (* RIGHT *)
   160 
   159 
   161   fun print_foo ctxt foo =
   160   fun print_foo ctxt foo =
   162     let
   161     let
   163       fun print t = ... Syntax.string_of_term ctxt t ...
   162       fun print t = ... Syntax.string_of_term ctxt t ...
   164     in ... end;
   163     in ... end;
   178   val string_of_term = Syntax.string_of_term;
   177   val string_of_term = Syntax.string_of_term;
   179 
   178 
   180   fun print_foo ctxt foo =
   179   fun print_foo ctxt foo =
   181     let
   180     let
   182       fun aux t = ... string_of_term ctxt t ...
   181       fun aux t = ... string_of_term ctxt t ...
   183     in ... end;
   182     in ... end;\<close>}
   184   \end{verbatim}
       
   185 
   183 
   186 
   184 
   187   \paragraph{Specific conventions.} Here are some specific name forms
   185   \paragraph{Specific conventions.} Here are some specific name forms
   188   that occur frequently in the sources.
   186   that occur frequently in the sources.
   189 
       
   190   \begin{itemize}
       
   191 
   187 
   192   \<^item> A function that maps @{ML_text foo} to @{ML_text bar} is
   188   \<^item> A function that maps @{ML_text foo} to @{ML_text bar} is
   193   called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never
   189   called @{ML_text foo_to_bar} or @{ML_text bar_of_foo} (never
   194   @{ML_text foo2bar}, nor @{ML_text bar_from_foo}, nor @{ML_text
   190   @{ML_text foo2bar}, nor @{ML_text bar_from_foo}, nor @{ML_text
   195   bar_for_foo}, nor @{ML_text bar4foo}).
   191   bar_for_foo}, nor @{ML_text bar4foo}).
   210   framework (\secref{sec:context} and \chref{ch:local-theory}) have
   206   framework (\secref{sec:context} and \chref{ch:local-theory}) have
   211   firm naming conventions as follows:
   207   firm naming conventions as follows:
   212 
   208 
   213   \begin{itemize}
   209   \begin{itemize}
   214 
   210 
   215   \<^item> theories are called @{ML_text thy}, rarely @{ML_text theory}
   211     \item theories are called @{ML_text thy}, rarely @{ML_text theory}
   216   (never @{ML_text thry})
   212     (never @{ML_text thry})
   217 
   213 
   218   \<^item> proof contexts are called @{ML_text ctxt}, rarely @{ML_text
   214     \item proof contexts are called @{ML_text ctxt}, rarely @{ML_text
   219   context} (never @{ML_text ctx})
   215     context} (never @{ML_text ctx})
   220 
   216 
   221   \<^item> generic contexts are called @{ML_text context}
   217     \item generic contexts are called @{ML_text context}
   222 
   218 
   223   \<^item> local theories are called @{ML_text lthy}, except for local
   219     \item local theories are called @{ML_text lthy}, except for local
   224   theories that are treated as proof context (which is a semantic
   220     theories that are treated as proof context (which is a semantic
   225   super-type)
   221     super-type)
   226 
   222 
   227   \end{itemize}
   223   \end{itemize}
   228 
   224 
   229   Variations with primed or decimal numbers are always possible, as
   225   Variations with primed or decimal numbers are always possible, as
   230   well as semantic prefixes like @{ML_text foo_thy} or @{ML_text
   226   well as semantic prefixes like @{ML_text foo_thy} or @{ML_text
   235   \<^item> The main logical entities (\secref{ch:logic}) have established
   231   \<^item> The main logical entities (\secref{ch:logic}) have established
   236   naming convention as follows:
   232   naming convention as follows:
   237 
   233 
   238   \begin{itemize}
   234   \begin{itemize}
   239 
   235 
   240   \<^item> sorts are called @{ML_text S}
   236     \item sorts are called @{ML_text S}
   241 
   237 
   242   \<^item> types are called @{ML_text T}, @{ML_text U}, or @{ML_text
   238     \item types are called @{ML_text T}, @{ML_text U}, or @{ML_text
   243   ty} (never @{ML_text t})
   239     ty} (never @{ML_text t})
   244 
   240 
   245   \<^item> terms are called @{ML_text t}, @{ML_text u}, or @{ML_text
   241     \item terms are called @{ML_text t}, @{ML_text u}, or @{ML_text
   246   tm} (never @{ML_text trm})
   242     tm} (never @{ML_text trm})
   247 
   243 
   248   \<^item> certified types are called @{ML_text cT}, rarely @{ML_text
   244     \item certified types are called @{ML_text cT}, rarely @{ML_text
   249   T}, with variants as for types
   245     T}, with variants as for types
   250 
   246 
   251   \<^item> certified terms are called @{ML_text ct}, rarely @{ML_text
   247     \item certified terms are called @{ML_text ct}, rarely @{ML_text
   252   t}, with variants as for terms (never @{ML_text ctrm})
   248     t}, with variants as for terms (never @{ML_text ctrm})
   253 
   249 
   254   \<^item> theorems are called @{ML_text th}, or @{ML_text thm}
   250     \item theorems are called @{ML_text th}, or @{ML_text thm}
   255 
   251 
   256   \end{itemize}
   252   \end{itemize}
   257 
   253 
   258   Proper semantic names override these conventions completely.  For
   254   Proper semantic names override these conventions completely.  For
   259   example, the left-hand side of an equation (as a term) can be called
   255   example, the left-hand side of an equation (as a term) can be called
   267   (if made explicit) is usually called @{ML_text st} instead of the
   263   (if made explicit) is usually called @{ML_text st} instead of the
   268   somewhat misleading @{ML_text thm}.  Any other arguments are given
   264   somewhat misleading @{ML_text thm}.  Any other arguments are given
   269   before the latter two, and the general context is given first.
   265   before the latter two, and the general context is given first.
   270   Example:
   266   Example:
   271 
   267 
   272   \begin{verbatim}
   268   @{verbatim [display] \<open>  fun my_tac ctxt arg1 arg2 i st = ...\<close>}
   273   fun my_tac ctxt arg1 arg2 i st = ...
       
   274   \end{verbatim}
       
   275 
   269 
   276   Note that the goal state @{ML_text st} above is rarely made
   270   Note that the goal state @{ML_text st} above is rarely made
   277   explicit, if tactic combinators (tacticals) are used as usual.
   271   explicit, if tactic combinators (tacticals) are used as usual.
   278 
   272 
   279   A tactic that requires a proof context needs to make that explicit as seen
   273   A tactic that requires a proof context needs to make that explicit as seen
   280   in the @{verbatim ctxt} argument above. Do not refer to the background
   274   in the @{verbatim ctxt} argument above. Do not refer to the background
   281   theory of @{verbatim st} -- it is not a proper context, but merely a formal
   275   theory of @{verbatim st} -- it is not a proper context, but merely a formal
   282   certificate.
   276   certificate.
   283 
       
   284   \end{itemize}
       
   285 \<close>
   277 \<close>
   286 
   278 
   287 
   279 
   288 subsection \<open>General source layout\<close>
   280 subsection \<open>General source layout\<close>
   289 
   281 
   305   expressions, following mostly standard conventions for mathematical
   297   expressions, following mostly standard conventions for mathematical
   306   typesetting, as can be seen in plain {\TeX} or {\LaTeX}.  This
   298   typesetting, as can be seen in plain {\TeX} or {\LaTeX}.  This
   307   defines positioning of spaces for parentheses, punctuation, and
   299   defines positioning of spaces for parentheses, punctuation, and
   308   infixes as illustrated here:
   300   infixes as illustrated here:
   309 
   301 
   310   \begin{verbatim}
   302   @{verbatim [display]
   311   val x = y + z * (a + b);
   303 \<open>  val x = y + z * (a + b);
   312   val pair = (a, b);
   304   val pair = (a, b);
   313   val record = {foo = 1, bar = 2};
   305   val record = {foo = 1, bar = 2};\<close>}
   314   \end{verbatim}
       
   315 
   306 
   316   Lines are normally broken \emph{after} an infix operator or
   307   Lines are normally broken \emph{after} an infix operator or
   317   punctuation character.  For example:
   308   punctuation character.  For example:
   318 
   309 
   319   \begin{verbatim}
   310   @{verbatim [display]
       
   311 \<open>
   320   val x =
   312   val x =
   321     a +
   313     a +
   322     b +
   314     b +
   323     c;
   315     c;
   324 
   316 
   325   val tuple =
   317   val tuple =
   326    (a,
   318    (a,
   327     b,
   319     b,
   328     c);
   320     c);
   329   \end{verbatim}
   321 \<close>}
   330 
   322 
   331   Some special infixes (e.g.\ @{ML_text "|>"}) work better at the
   323   Some special infixes (e.g.\ @{ML_text "|>"}) work better at the
   332   start of the line, but punctuation is always at the end.
   324   start of the line, but punctuation is always at the end.
   333 
   325 
   334   Function application follows the tradition of @{text "\<lambda>"}-calculus,
   326   Function application follows the tradition of @{text "\<lambda>"}-calculus,
   352 
   344 
   353   Indentation follows a simple logical format that only depends on the
   345   Indentation follows a simple logical format that only depends on the
   354   nesting depth, not the accidental length of the text that initiates
   346   nesting depth, not the accidental length of the text that initiates
   355   a level of nesting.  Example:
   347   a level of nesting.  Example:
   356 
   348 
   357   \begin{verbatim}
   349   @{verbatim [display]
   358   (* RIGHT *)
   350 \<open>  (* RIGHT *)
   359 
   351 
   360   if b then
   352   if b then
   361     expr1_part1
   353     expr1_part1
   362     expr1_part2
   354     expr1_part2
   363   else
   355   else
   368   (* WRONG *)
   360   (* WRONG *)
   369 
   361 
   370   if b then expr1_part1
   362   if b then expr1_part1
   371             expr1_part2
   363             expr1_part2
   372   else expr2_part1
   364   else expr2_part1
   373        expr2_part2
   365        expr2_part2\<close>}
   374   \end{verbatim}
       
   375 
   366 
   376   The second form has many problems: it assumes a fixed-width font
   367   The second form has many problems: it assumes a fixed-width font
   377   when viewing the sources, it uses more space on the line and thus
   368   when viewing the sources, it uses more space on the line and thus
   378   makes it hard to observe its strict length limit (working against
   369   makes it hard to observe its strict length limit (working against
   379   \emph{readability}), it requires extra editing to adapt the layout
   370   \emph{readability}), it requires extra editing to adapt the layout
   393 
   384 
   394   Multiple clauses of @{ML_text fun}, @{ML_text fn}, @{ML_text handle},
   385   Multiple clauses of @{ML_text fun}, @{ML_text fn}, @{ML_text handle},
   395   @{ML_text case} get extra indentation to indicate the nesting
   386   @{ML_text case} get extra indentation to indicate the nesting
   396   clearly.  Example:
   387   clearly.  Example:
   397 
   388 
   398   \begin{verbatim}
   389   @{verbatim [display]
   399   (* RIGHT *)
   390 \<open>  (* RIGHT *)
   400 
   391 
   401   fun foo p1 =
   392   fun foo p1 =
   402         expr1
   393         expr1
   403     | foo p2 =
   394     | foo p2 =
   404         expr2
   395         expr2
   407   (* WRONG *)
   398   (* WRONG *)
   408 
   399 
   409   fun foo p1 =
   400   fun foo p1 =
   410     expr1
   401     expr1
   411     | foo p2 =
   402     | foo p2 =
   412     expr2
   403     expr2\<close>}
   413   \end{verbatim}
       
   414 
   404 
   415   Body expressions consisting of @{ML_text case} or @{ML_text let}
   405   Body expressions consisting of @{ML_text case} or @{ML_text let}
   416   require care to maintain compositionality, to prevent loss of
   406   require care to maintain compositionality, to prevent loss of
   417   logical indentation where it is especially important to see the
   407   logical indentation where it is especially important to see the
   418   structure of the text.  Example:
   408   structure of the text.  Example:
   419 
   409 
   420   \begin{verbatim}
   410   @{verbatim [display]
   421   (* RIGHT *)
   411 \<open>  (* RIGHT *)
   422 
   412 
   423   fun foo p1 =
   413   fun foo p1 =
   424         (case e of
   414         (case e of
   425           q1 => ...
   415           q1 => ...
   426         | q2 => ...)
   416         | q2 => ...)
   440     | foo p2 =
   430     | foo p2 =
   441     let
   431     let
   442       ...
   432       ...
   443     in
   433     in
   444       ...
   434       ...
   445     end
   435     end\<close>}
   446   \end{verbatim}
       
   447 
   436 
   448   Extra parentheses around @{ML_text case} expressions are optional,
   437   Extra parentheses around @{ML_text case} expressions are optional,
   449   but help to analyse the nesting based on character matching in the
   438   but help to analyse the nesting based on character matching in the
   450   text editor.
   439   text editor.
   451 
   440 
   452   \<^medskip>
   441   \<^medskip>
   453   There are two main exceptions to the overall principle of
   442   There are two main exceptions to the overall principle of
   454   compositionality in the layout of complex expressions.
   443   compositionality in the layout of complex expressions.
   455 
   444 
   456   \begin{enumerate}
       
   457 
       
   458   \<^enum> @{ML_text "if"} expressions are iterated as if ML had multi-branch
   445   \<^enum> @{ML_text "if"} expressions are iterated as if ML had multi-branch
   459   conditionals, e.g.
   446   conditionals, e.g.
   460 
   447 
   461   \begin{verbatim}
   448   @{verbatim [display]
   462   (* RIGHT *)
   449 \<open>  (* RIGHT *)
   463 
   450 
   464   if b1 then e1
   451   if b1 then e1
   465   else if b2 then e2
   452   else if b2 then e2
   466   else e3
   453   else e3\<close>}
   467   \end{verbatim}
       
   468 
   454 
   469   \<^enum> @{ML_text fn} abstractions are often layed-out as if they
   455   \<^enum> @{ML_text fn} abstractions are often layed-out as if they
   470   would lack any structure by themselves.  This traditional form is
   456   would lack any structure by themselves.  This traditional form is
   471   motivated by the possibility to shift function arguments back and
   457   motivated by the possibility to shift function arguments back and
   472   forth wrt.\ additional combinators.  Example:
   458   forth wrt.\ additional combinators.  Example:
   473 
   459 
   474   \begin{verbatim}
   460   @{verbatim [display]
   475   (* RIGHT *)
   461 \<open>  (* RIGHT *)
   476 
   462 
   477   fun foo x y = fold (fn z =>
   463   fun foo x y = fold (fn z =>
   478     expr)
   464     expr)\<close>}
   479   \end{verbatim}
       
   480 
   465 
   481   Here the visual appearance is that of three arguments @{ML_text x},
   466   Here the visual appearance is that of three arguments @{ML_text x},
   482   @{ML_text y}, @{ML_text z} in a row.
   467   @{ML_text y}, @{ML_text z} in a row.
   483 
   468 
   484   \end{enumerate}
       
   485 
   469 
   486   Such weakly structured layout should be use with great care.  Here
   470   Such weakly structured layout should be use with great care.  Here
   487   are some counter-examples involving @{ML_text let} expressions:
   471   are some counter-examples involving @{ML_text let} expressions:
   488 
   472 
   489   \begin{verbatim}
   473   @{verbatim [display]
   490   (* WRONG *)
   474 \<open>  (* WRONG *)
   491 
   475 
   492   fun foo x = let
   476   fun foo x = let
   493       val y = ...
   477       val y = ...
   494     in ... end
   478     in ... end
   495 
   479 
   513 
   497 
   514   fun foo x =
   498   fun foo x =
   515     let
   499     let
   516       val y = ...
   500       val y = ...
   517     in
   501     in
   518       ... end
   502       ... end\<close>}
   519   \end{verbatim}
       
   520 
   503 
   521   \<^medskip>
   504   \<^medskip>
   522   In general the source layout is meant to emphasize the
   505   In general the source layout is meant to emphasize the
   523   structure of complex language expressions, not to pretend that SML
   506   structure of complex language expressions, not to pretend that SML
   524   had a completely different syntax (say that of Haskell, Scala, Java).
   507   had a completely different syntax (say that of Haskell, Scala, Java).
   644   @{index_ML "Context.>>": "(Context.generic -> Context.generic) -> unit"} \\
   627   @{index_ML "Context.>>": "(Context.generic -> Context.generic) -> unit"} \\
   645   @{index_ML ML_Thms.bind_thms: "string * thm list -> unit"} \\
   628   @{index_ML ML_Thms.bind_thms: "string * thm list -> unit"} \\
   646   @{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\
   629   @{index_ML ML_Thms.bind_thm: "string * thm -> unit"} \\
   647   \end{mldecls}
   630   \end{mldecls}
   648 
   631 
   649   \begin{description}
       
   650 
       
   651   \<^descr> @{ML "ML_Context.the_generic_context ()"} refers to the theory
   632   \<^descr> @{ML "ML_Context.the_generic_context ()"} refers to the theory
   652   context of the ML toplevel --- at compile time.  ML code needs to
   633   context of the ML toplevel --- at compile time.  ML code needs to
   653   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   634   take care to refer to @{ML "ML_Context.the_generic_context ()"}
   654   correctly.  Recall that evaluation of a function body is delayed
   635   correctly.  Recall that evaluation of a function body is delayed
   655   until actual run-time.
   636   until actual run-time.
   662   ML toplevel, associating it with the provided name.
   643   ML toplevel, associating it with the provided name.
   663 
   644 
   664   \<^descr> @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but
   645   \<^descr> @{ML ML_Thms.bind_thm} is similar to @{ML ML_Thms.bind_thms} but
   665   refers to a singleton fact.
   646   refers to a singleton fact.
   666 
   647 
   667   \end{description}
       
   668 
   648 
   669   It is important to note that the above functions are really
   649   It is important to note that the above functions are really
   670   restricted to the compile time, even though the ML compiler is
   650   restricted to the compile time, even though the ML compiler is
   671   invoked at run-time.  The majority of ML code either uses static
   651   invoked at run-time.  The majority of ML code either uses static
   672   antiquotations (\secref{sec:ML-antiq}) or refers to the theory or
   652   antiquotations (\secref{sec:ML-antiq}) or refers to the theory or
   723   @@{ML_antiquotation make_string}
   703   @@{ML_antiquotation make_string}
   724   ;
   704   ;
   725   @@{ML_antiquotation print} @{syntax name}?
   705   @@{ML_antiquotation print} @{syntax name}?
   726   \<close>}
   706   \<close>}
   727 
   707 
   728   \begin{description}
       
   729 
       
   730   \<^descr> @{text "@{make_string}"} inlines a function to print arbitrary values
   708   \<^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
   709   similar to the ML toplevel. The result is compiler dependent and may fall
   732   back on "?" in certain situations. The value of configuration option
   710   back on "?" in certain situations. The value of configuration option
   733   @{attribute_ref ML_print_depth} determines further details of output.
   711   @{attribute_ref ML_print_depth} determines further details of output.
   734 
   712 
   735   \<^descr> @{text "@{print f}"} uses the ML function @{text "f: string ->
   713   \<^descr> @{text "@{print f}"} uses the ML function @{text "f: string ->
   736   unit"} to output the result of @{text "@{make_string}"} above,
   714   unit"} to output the result of @{text "@{make_string}"} above,
   737   together with the source position of the antiquotation.  The default
   715   together with the source position of the antiquotation.  The default
   738   output function is @{ML writeln}.
   716   output function is @{ML writeln}.
   739 
       
   740   \end{description}
       
   741 \<close>
   717 \<close>
   742 
   718 
   743 text %mlex \<open>The following artificial examples show how to produce
   719 text %mlex \<open>The following artificial examples show how to produce
   744   adhoc output of ML values for debugging purposes.\<close>
   720   adhoc output of ML values for debugging purposes.\<close>
   745 
   721 
   905   @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   881   @{index_ML fold: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   906   @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   882   @{index_ML fold_rev: "('a -> 'b -> 'b) -> 'a list -> 'b -> 'b"} \\
   907   @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
   883   @{index_ML fold_map: "('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b"} \\
   908   \end{mldecls}
   884   \end{mldecls}
   909 
   885 
   910   \begin{description}
       
   911 
       
   912   \<^descr> @{ML fold}~@{text f} lifts the parametrized update function
   886   \<^descr> @{ML fold}~@{text f} lifts the parametrized update function
   913   @{text "f"} to a list of parameters.
   887   @{text "f"} to a list of parameters.
   914 
   888 
   915   \<^descr> @{ML fold_rev}~@{text "f"} is similar to @{ML fold}~@{text
   889   \<^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.
   890   "f"}, but works inside-out, as if the list would be reversed.
   917 
   891 
   918   \<^descr> @{ML fold_map}~@{text "f"} lifts the parametrized update
   892   \<^descr> @{ML fold_map}~@{text "f"} lifts the parametrized update
   919   function @{text "f"} (with side-result) to a list of parameters and
   893   function @{text "f"} (with side-result) to a list of parameters and
   920   cumulative side-results.
   894   cumulative side-results.
   921 
   895 
   922   \end{description}
       
   923 
   896 
   924   \begin{warn}
   897   \begin{warn}
   925   The literature on functional programming provides a confusing multitude of
   898   The literature on functional programming provides a confusing multitude of
   926   combinators called @{text "foldl"}, @{text "foldr"} etc. SML97 provides its
   899   combinators called @{text "foldl"}, @{text "foldr"} etc. SML97 provides its
   927   own variations as @{ML List.foldl} and @{ML List.foldr}, while the classic
   900   own variations as @{ML List.foldl} and @{ML List.foldr}, while the classic
  1031   @{index_ML tracing: "string -> unit"} \\
  1004   @{index_ML tracing: "string -> unit"} \\
  1032   @{index_ML warning: "string -> unit"} \\
  1005   @{index_ML warning: "string -> unit"} \\
  1033   @{index_ML error: "string -> 'a"} % FIXME Output.error_message (!?) \\
  1006   @{index_ML error: "string -> 'a"} % FIXME Output.error_message (!?) \\
  1034   \end{mldecls}
  1007   \end{mldecls}
  1035 
  1008 
  1036   \begin{description}
       
  1037 
       
  1038   \<^descr> @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
  1009   \<^descr> @{ML writeln}~@{text "text"} outputs @{text "text"} as regular
  1039   message.  This is the primary message output operation of Isabelle
  1010   message.  This is the primary message output operation of Isabelle
  1040   and should be used by default.
  1011   and should be used by default.
  1041 
  1012 
  1042   \<^descr> @{ML tracing}~@{text "text"} outputs @{text "text"} as special
  1013   \<^descr> @{ML tracing}~@{text "text"} outputs @{text "text"} as special
  1066   \begin{warn}
  1037   \begin{warn}
  1067   The actual error channel is accessed via @{ML Output.error_message}, but
  1038   The actual error channel is accessed via @{ML Output.error_message}, but
  1068   this is normally not used directly in user code.
  1039   this is normally not used directly in user code.
  1069   \end{warn}
  1040   \end{warn}
  1070 
  1041 
  1071   \end{description}
       
  1072 
  1042 
  1073   \begin{warn}
  1043   \begin{warn}
  1074   Regular Isabelle/ML code should output messages exclusively by the
  1044   Regular Isabelle/ML code should output messages exclusively by the
  1075   official channels.  Using raw I/O on \emph{stdout} or \emph{stderr}
  1045   official channels.  Using raw I/O on \emph{stdout} or \emph{stderr}
  1076   instead (e.g.\ via @{ML TextIO.output}) is apt to cause problems in
  1046   instead (e.g.\ via @{ML TextIO.output}) is apt to cause problems in
  1213   @{index_ML Exn.is_interrupt: "exn -> bool"} \\
  1183   @{index_ML Exn.is_interrupt: "exn -> bool"} \\
  1214   @{index_ML reraise: "exn -> 'a"} \\
  1184   @{index_ML reraise: "exn -> 'a"} \\
  1215   @{index_ML Runtime.exn_trace: "(unit -> 'a) -> 'a"} \\
  1185   @{index_ML Runtime.exn_trace: "(unit -> 'a) -> 'a"} \\
  1216   \end{mldecls}
  1186   \end{mldecls}
  1217 
  1187 
  1218   \begin{description}
       
  1219 
       
  1220   \<^descr> @{ML try}~@{text "f x"} makes the partiality of evaluating
  1188   \<^descr> @{ML try}~@{text "f x"} makes the partiality of evaluating
  1221   @{text "f x"} explicit via the option datatype.  Interrupts are
  1189   @{text "f x"} explicit via the option datatype.  Interrupts are
  1222   \emph{not} handled here, i.e.\ this form serves as safe replacement
  1190   \emph{not} handled here, i.e.\ this form serves as safe replacement
  1223   for the \emph{unsafe} version @{ML_text "(SOME"}~@{text "f
  1191   for the \emph{unsafe} version @{ML_text "(SOME"}~@{text "f
  1224   x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in
  1192   x"}~@{ML_text "handle _ => NONE)"} that is occasionally seen in
  1245   a full trace of its stack of nested exceptions (if possible,
  1213   a full trace of its stack of nested exceptions (if possible,
  1246   depending on the ML platform).
  1214   depending on the ML platform).
  1247 
  1215 
  1248   Inserting @{ML Runtime.exn_trace} into ML code temporarily is
  1216   Inserting @{ML Runtime.exn_trace} into ML code temporarily is
  1249   useful for debugging, but not suitable for production code.
  1217   useful for debugging, but not suitable for production code.
  1250 
       
  1251   \end{description}
       
  1252 \<close>
  1218 \<close>
  1253 
  1219 
  1254 text %mlantiq \<open>
  1220 text %mlantiq \<open>
  1255   \begin{matharray}{rcl}
  1221   \begin{matharray}{rcl}
  1256   @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
  1222   @{ML_antiquotation_def "assert"} & : & @{text ML_antiquotation} \\
  1257   \end{matharray}
  1223   \end{matharray}
  1258 
  1224 
  1259   \begin{description}
       
  1260 
       
  1261   \<^descr> @{text "@{assert}"} inlines a function
  1225   \<^descr> @{text "@{assert}"} inlines a function
  1262   @{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is
  1226   @{ML_type "bool -> unit"} that raises @{ML Fail} if the argument is
  1263   @{ML false}.  Due to inlining the source position of failed
  1227   @{ML false}.  Due to inlining the source position of failed
  1264   assertions is included in the error output.
  1228   assertions is included in the error output.
  1265 
       
  1266   \end{description}
       
  1267 \<close>
  1229 \<close>
  1268 
  1230 
  1269 
  1231 
  1270 section \<open>Strings of symbols \label{sec:symbols}\<close>
  1232 section \<open>Strings of symbols \label{sec:symbols}\<close>
  1271 
  1233 
  1274   all.  Isabelle strings consist of a sequence of symbols, represented
  1236   all.  Isabelle strings consist of a sequence of symbols, represented
  1275   as a packed string or an exploded list of strings.  Each symbol is
  1237   as a packed string or an exploded list of strings.  Each symbol is
  1276   in itself a small string, which has either one of the following
  1238   in itself a small string, which has either one of the following
  1277   forms:
  1239   forms:
  1278 
  1240 
  1279   \begin{enumerate}
       
  1280 
       
  1281   \<^enum> a single ASCII character ``@{text "c"}'', for example
  1241   \<^enum> a single ASCII character ``@{text "c"}'', for example
  1282   ``@{verbatim a}'',
  1242   ``@{verbatim a}'',
  1283 
  1243 
  1284   \<^enum> a codepoint according to UTF-8 (non-ASCII byte sequence),
  1244   \<^enum> a codepoint according to UTF-8 (non-ASCII byte sequence),
  1285 
  1245 
  1296 
  1256 
  1297   \<^enum> a numbered raw control symbol ``@{verbatim \<open>\\<close>}@{verbatim
  1257   \<^enum> a numbered raw control symbol ``@{verbatim \<open>\\<close>}@{verbatim
  1298   "<^raw"}@{text n}@{verbatim ">"}, where @{text n} consists of digits, for
  1258   "<^raw"}@{text n}@{verbatim ">"}, where @{text n} consists of digits, for
  1299   example ``@{verbatim "\<^raw42>"}''.
  1259   example ``@{verbatim "\<^raw42>"}''.
  1300 
  1260 
  1301   \end{enumerate}
       
  1302 
  1261 
  1303   The @{text "ident"} syntax for symbol names is @{text "letter
  1262   The @{text "ident"} syntax for symbol names is @{text "letter
  1304   (letter | digit)\<^sup>*"}, where @{text "letter = A..Za..z"} and @{text
  1263   (letter | digit)\<^sup>*"}, where @{text "letter = A..Za..z"} and @{text
  1305   "digit = 0..9"}.  There are infinitely many regular symbols and
  1264   "digit = 0..9"}.  There are infinitely many regular symbols and
  1306   control symbols, but a fixed collection of standard symbols is
  1265   control symbols, but a fixed collection of standard symbols is
  1335   \begin{mldecls}
  1294   \begin{mldecls}
  1336   @{index_ML_type "Symbol.sym"} \\
  1295   @{index_ML_type "Symbol.sym"} \\
  1337   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
  1296   @{index_ML Symbol.decode: "Symbol.symbol -> Symbol.sym"} \\
  1338   \end{mldecls}
  1297   \end{mldecls}
  1339 
  1298 
  1340   \begin{description}
       
  1341 
       
  1342   \<^descr> Type @{ML_type "Symbol.symbol"} represents individual Isabelle
  1299   \<^descr> Type @{ML_type "Symbol.symbol"} represents individual Isabelle
  1343   symbols.
  1300   symbols.
  1344 
  1301 
  1345   \<^descr> @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
  1302   \<^descr> @{ML "Symbol.explode"}~@{text "str"} produces a symbol list
  1346   from the packed form.  This function supersedes @{ML
  1303   from the packed form.  This function supersedes @{ML
  1361   @{ML "Symbol.Malformed"}.
  1318   @{ML "Symbol.Malformed"}.
  1362 
  1319 
  1363   \<^descr> @{ML "Symbol.decode"} converts the string representation of a
  1320   \<^descr> @{ML "Symbol.decode"} converts the string representation of a
  1364   symbol into the datatype version.
  1321   symbol into the datatype version.
  1365 
  1322 
  1366   \end{description}
       
  1367 
  1323 
  1368   \paragraph{Historical note.} In the original SML90 standard the
  1324   \paragraph{Historical note.} In the original SML90 standard the
  1369   primitive ML type @{ML_type char} did not exists, and @{ML_text
  1325   primitive ML type @{ML_type char} did not exists, and @{ML_text
  1370   "explode: string -> string list"} produced a list of singleton
  1326   "explode: string -> string list"} produced a list of singleton
  1371   strings like @{ML "raw_explode: string -> string list"} in
  1327   strings like @{ML "raw_explode: string -> string list"} in
  1397 text %mlref \<open>
  1353 text %mlref \<open>
  1398   \begin{mldecls}
  1354   \begin{mldecls}
  1399   @{index_ML_type char} \\
  1355   @{index_ML_type char} \\
  1400   \end{mldecls}
  1356   \end{mldecls}
  1401 
  1357 
  1402   \begin{description}
       
  1403 
       
  1404   \<^descr> Type @{ML_type char} is \emph{not} used.  The smallest textual
  1358   \<^descr> Type @{ML_type char} is \emph{not} used.  The smallest textual
  1405   unit in Isabelle is represented as a ``symbol'' (see
  1359   unit in Isabelle is represented as a ``symbol'' (see
  1406   \secref{sec:symbols}).
  1360   \secref{sec:symbols}).
  1407 
       
  1408   \end{description}
       
  1409 \<close>
  1361 \<close>
  1410 
  1362 
  1411 
  1363 
  1412 subsection \<open>Strings\<close>
  1364 subsection \<open>Strings\<close>
  1413 
  1365 
  1414 text %mlref \<open>
  1366 text %mlref \<open>
  1415   \begin{mldecls}
  1367   \begin{mldecls}
  1416   @{index_ML_type string} \\
  1368   @{index_ML_type string} \\
  1417   \end{mldecls}
  1369   \end{mldecls}
  1418 
  1370 
  1419   \begin{description}
       
  1420 
       
  1421   \<^descr> Type @{ML_type string} represents immutable vectors of 8-bit
  1371   \<^descr> Type @{ML_type string} represents immutable vectors of 8-bit
  1422   characters.  There are operations in SML to convert back and forth
  1372   characters.  There are operations in SML to convert back and forth
  1423   to actual byte vectors, which are seldom used.
  1373   to actual byte vectors, which are seldom used.
  1424 
  1374 
  1425   This historically important raw text representation is used for
  1375   This historically important raw text representation is used for
  1426   Isabelle-specific purposes with the following implicit substructures
  1376   Isabelle-specific purposes with the following implicit substructures
  1427   packed into the string content:
  1377   packed into the string content:
  1428 
  1378 
  1429   \begin{enumerate}
  1379   \begin{enumerate}
  1430 
  1380 
  1431   \<^enum> sequence of Isabelle symbols (see also \secref{sec:symbols}),
  1381     \item sequence of Isabelle symbols (see also \secref{sec:symbols}),
  1432   with @{ML Symbol.explode} as key operation;
  1382     with @{ML Symbol.explode} as key operation;
  1433 
  1383   
  1434   \<^enum> XML tree structure via YXML (see also @{cite "isabelle-system"}),
  1384     \item XML tree structure via YXML (see also @{cite "isabelle-system"}),
  1435   with @{ML YXML.parse_body} as key operation.
  1385     with @{ML YXML.parse_body} as key operation.
  1436 
  1386   
  1437   \end{enumerate}
  1387   \end{enumerate}
  1438 
  1388 
  1439   Note that Isabelle/ML string literals may refer Isabelle symbols like
  1389   Note that Isabelle/ML string literals may refer Isabelle symbols like
  1440   ``@{verbatim \<alpha>}'' natively, \emph{without} escaping the backslash. This is a
  1390   ``@{verbatim \<alpha>}'' natively, \emph{without} escaping the backslash. This is a
  1441   consequence of Isabelle treating all source text as strings of symbols,
  1391   consequence of Isabelle treating all source text as strings of symbols,
  1442   instead of raw characters.
  1392   instead of raw characters.
  1443 
       
  1444   \end{description}
       
  1445 \<close>
  1393 \<close>
  1446 
  1394 
  1447 text %mlex \<open>The subsequent example illustrates the difference of
  1395 text %mlex \<open>The subsequent example illustrates the difference of
  1448   physical addressing of bytes versus logical addressing of symbols in
  1396   physical addressing of bytes versus logical addressing of symbols in
  1449   Isabelle strings.
  1397   Isabelle strings.
  1469 text %mlref \<open>
  1417 text %mlref \<open>
  1470   \begin{mldecls}
  1418   \begin{mldecls}
  1471   @{index_ML_type int} \\
  1419   @{index_ML_type int} \\
  1472   \end{mldecls}
  1420   \end{mldecls}
  1473 
  1421 
  1474   \begin{description}
       
  1475 
       
  1476   \<^descr> Type @{ML_type int} represents regular mathematical integers, which
  1422   \<^descr> Type @{ML_type int} represents regular mathematical integers, which
  1477   are \emph{unbounded}. Overflow is treated properly, but should never happen
  1423   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
  1424   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
  1425   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).
  1426   uniformly for all supported ML platforms (Poly/ML and SML/NJ).
  1484 
  1430 
  1485   Structure @{ML_structure IntInf} of SML97 is obsolete and superseded by
  1431   Structure @{ML_structure IntInf} of SML97 is obsolete and superseded by
  1486   @{ML_structure Int}.  Structure @{ML_structure Integer} in @{file
  1432   @{ML_structure Int}.  Structure @{ML_structure Integer} in @{file
  1487   "~~/src/Pure/General/integer.ML"} provides some additional
  1433   "~~/src/Pure/General/integer.ML"} provides some additional
  1488   operations.
  1434   operations.
  1489 
       
  1490   \end{description}
       
  1491 \<close>
  1435 \<close>
  1492 
  1436 
  1493 
  1437 
  1494 subsection \<open>Time\<close>
  1438 subsection \<open>Time\<close>
  1495 
  1439 
  1497   \begin{mldecls}
  1441   \begin{mldecls}
  1498   @{index_ML_type Time.time} \\
  1442   @{index_ML_type Time.time} \\
  1499   @{index_ML seconds: "real -> Time.time"} \\
  1443   @{index_ML seconds: "real -> Time.time"} \\
  1500   \end{mldecls}
  1444   \end{mldecls}
  1501 
  1445 
  1502   \begin{description}
       
  1503 
       
  1504   \<^descr> Type @{ML_type Time.time} represents time abstractly according
  1446   \<^descr> Type @{ML_type Time.time} represents time abstractly according
  1505   to the SML97 basis library definition.  This is adequate for
  1447   to the SML97 basis library definition.  This is adequate for
  1506   internal ML operations, but awkward in concrete time specifications.
  1448   internal ML operations, but awkward in concrete time specifications.
  1507 
  1449 
  1508   \<^descr> @{ML seconds}~@{text "s"} turns the concrete scalar @{text
  1450   \<^descr> @{ML seconds}~@{text "s"} turns the concrete scalar @{text
  1509   "s"} (measured in seconds) into an abstract time value.  Floating
  1451   "s"} (measured in seconds) into an abstract time value.  Floating
  1510   point numbers are easy to use as configuration options in the
  1452   point numbers are easy to use as configuration options in the
  1511   context (see \secref{sec:config-options}) or system options that
  1453   context (see \secref{sec:config-options}) or system options that
  1512   are maintained externally.
  1454   are maintained externally.
  1513 
       
  1514   \end{description}
       
  1515 \<close>
  1455 \<close>
  1516 
  1456 
  1517 
  1457 
  1518 subsection \<open>Options\<close>
  1458 subsection \<open>Options\<close>
  1519 
  1459 
  1549   @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1489   @{index_ML insert: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1550   @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
  1490   @{index_ML remove: "('b * 'a -> bool) -> 'b -> 'a list -> 'a list"} \\
  1551   @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1491   @{index_ML update: "('a * 'a -> bool) -> 'a -> 'a list -> 'a list"} \\
  1552   \end{mldecls}
  1492   \end{mldecls}
  1553 
  1493 
  1554   \begin{description}
       
  1555 
       
  1556   \<^descr> @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
  1494   \<^descr> @{ML cons}~@{text "x xs"} evaluates to @{text "x :: xs"}.
  1557 
  1495 
  1558   Tupled infix operators are a historical accident in Standard ML.
  1496   Tupled infix operators are a historical accident in Standard ML.
  1559   The curried @{ML cons} amends this, but it should be only used when
  1497   The curried @{ML cons} amends this, but it should be only used when
  1560   partial application is required.
  1498   partial application is required.
  1569   already a @{ML member} of the list, while @{ML update} ensures that
  1507   already a @{ML member} of the list, while @{ML update} ensures that
  1570   the latest entry is always put in front.  The latter discipline is
  1508   the latest entry is always put in front.  The latter discipline is
  1571   often more appropriate in declarations of context data
  1509   often more appropriate in declarations of context data
  1572   (\secref{sec:context-data}) that are issued by the user in Isar
  1510   (\secref{sec:context-data}) that are issued by the user in Isar
  1573   source: later declarations take precedence over earlier ones.
  1511   source: later declarations take precedence over earlier ones.
  1574 
       
  1575   \end{description}
       
  1576 \<close>
  1512 \<close>
  1577 
  1513 
  1578 text %mlex \<open>Using canonical @{ML fold} together with @{ML cons} (or
  1514 text %mlex \<open>Using canonical @{ML fold} together with @{ML cons} (or
  1579   similar standard operations) alternates the orientation of data.
  1515   similar standard operations) alternates the orientation of data.
  1580   The is quite natural and should not be altered forcible by inserting
  1516   The is quite natural and should not be altered forcible by inserting
  1625   @{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\
  1561   @{index_ML AList.lookup: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option"} \\
  1626   @{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\
  1562   @{index_ML AList.defined: "('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool"} \\
  1627   @{index_ML AList.update: "('a * 'a -> bool) -> 'a * 'b -> ('a * 'b) list -> ('a * 'b) list"} \\
  1563   @{index_ML AList.update: "('a * 'a -> bool) -> 'a * 'b -> ('a * 'b) list -> ('a * 'b) list"} \\
  1628   \end{mldecls}
  1564   \end{mldecls}
  1629 
  1565 
  1630   \begin{description}
       
  1631 
       
  1632   \<^descr> @{ML AList.lookup}, @{ML AList.defined}, @{ML AList.update}
  1566   \<^descr> @{ML AList.lookup}, @{ML AList.defined}, @{ML AList.update}
  1633   implement the main ``framework operations'' for mappings in
  1567   implement the main ``framework operations'' for mappings in
  1634   Isabelle/ML, following standard conventions for their names and
  1568   Isabelle/ML, following standard conventions for their names and
  1635   types.
  1569   types.
  1636 
  1570 
  1642   The @{text "defined"} operation is essentially a contraction of @{ML
  1576   The @{text "defined"} operation is essentially a contraction of @{ML
  1643   is_some} and @{verbatim "lookup"}, but this is sufficiently frequent to
  1577   is_some} and @{verbatim "lookup"}, but this is sufficiently frequent to
  1644   justify its independent existence.  This also gives the
  1578   justify its independent existence.  This also gives the
  1645   implementation some opportunity for peep-hole optimization.
  1579   implementation some opportunity for peep-hole optimization.
  1646 
  1580 
  1647   \end{description}
       
  1648 
  1581 
  1649   Association lists are adequate as simple implementation of finite mappings
  1582   Association lists are adequate as simple implementation of finite mappings
  1650   in many practical situations. A more advanced table structure is defined in
  1583   in many practical situations. A more advanced table structure is defined in
  1651   @{file "~~/src/Pure/General/table.ML"}; that version scales easily to
  1584   @{file "~~/src/Pure/General/table.ML"}; that version scales easily to
  1652   thousands or millions of elements.
  1585   thousands or millions of elements.
  1760 
  1693 
  1761 text \<open>Thread-safeness is mainly concerned about concurrent
  1694 text \<open>Thread-safeness is mainly concerned about concurrent
  1762   read/write access to shared resources, which are outside the purely
  1695   read/write access to shared resources, which are outside the purely
  1763   functional world of ML.  This covers the following in particular.
  1696   functional world of ML.  This covers the following in particular.
  1764 
  1697 
  1765   \begin{itemize}
       
  1766 
       
  1767   \<^item> Global references (or arrays), i.e.\ mutable memory cells that
  1698   \<^item> Global references (or arrays), i.e.\ mutable memory cells that
  1768   persist over several invocations of associated
  1699   persist over several invocations of associated
  1769   operations.\footnote{This is independent of the visibility of such
  1700   operations.\footnote{This is independent of the visibility of such
  1770   mutable values in the toplevel scope.}
  1701   mutable values in the toplevel scope.}
  1771 
  1702 
  1773   channels, environment variables, current working directory.
  1704   channels, environment variables, current working directory.
  1774 
  1705 
  1775   \<^item> Writable resources in the file-system that are shared among
  1706   \<^item> Writable resources in the file-system that are shared among
  1776   different threads or external processes.
  1707   different threads or external processes.
  1777 
  1708 
  1778   \end{itemize}
       
  1779 
  1709 
  1780   Isabelle/ML provides various mechanisms to avoid critical shared
  1710   Isabelle/ML provides various mechanisms to avoid critical shared
  1781   resources in most situations.  As last resort there are some
  1711   resources in most situations.  As last resort there are some
  1782   mechanisms for explicit synchronization.  The following guidelines
  1712   mechanisms for explicit synchronization.  The following guidelines
  1783   help to make Isabelle/ML programs work smoothly in a concurrent
  1713   help to make Isabelle/ML programs work smoothly in a concurrent
  1784   environment.
  1714   environment.
  1785 
       
  1786   \begin{itemize}
       
  1787 
  1715 
  1788   \<^item> Avoid global references altogether.  Isabelle/Isar maintains a
  1716   \<^item> Avoid global references altogether.  Isabelle/Isar maintains a
  1789   uniform context that incorporates arbitrary data declared by user
  1717   uniform context that incorporates arbitrary data declared by user
  1790   programs (\secref{sec:context-data}).  This context is passed as
  1718   programs (\secref{sec:context-data}).  This context is passed as
  1791   plain value and user tools can get/map their own data in a purely
  1719   plain value and user tools can get/map their own data in a purely
  1822   Isabelle already provides a temporary directory that is unique for
  1750   Isabelle already provides a temporary directory that is unique for
  1823   the running process, and there is a centralized source of unique
  1751   the running process, and there is a centralized source of unique
  1824   serial numbers in Isabelle/ML.  Thus temporary files that are passed
  1752   serial numbers in Isabelle/ML.  Thus temporary files that are passed
  1825   to to some external process will be always disjoint, and thus
  1753   to to some external process will be always disjoint, and thus
  1826   thread-safe.
  1754   thread-safe.
  1827 
       
  1828   \end{itemize}
       
  1829 \<close>
  1755 \<close>
  1830 
  1756 
  1831 text %mlref \<open>
  1757 text %mlref \<open>
  1832   \begin{mldecls}
  1758   \begin{mldecls}
  1833   @{index_ML File.tmp_path: "Path.T -> Path.T"} \\
  1759   @{index_ML File.tmp_path: "Path.T -> Path.T"} \\
  1834   @{index_ML serial_string: "unit -> string"} \\
  1760   @{index_ML serial_string: "unit -> string"} \\
  1835   \end{mldecls}
  1761   \end{mldecls}
  1836 
  1762 
  1837   \begin{description}
       
  1838 
       
  1839   \<^descr> @{ML File.tmp_path}~@{text "path"} relocates the base
  1763   \<^descr> @{ML File.tmp_path}~@{text "path"} relocates the base
  1840   component of @{text "path"} into the unique temporary directory of
  1764   component of @{text "path"} into the unique temporary directory of
  1841   the running Isabelle/ML process.
  1765   the running Isabelle/ML process.
  1842 
  1766 
  1843   \<^descr> @{ML serial_string}~@{text "()"} creates a new serial number
  1767   \<^descr> @{ML serial_string}~@{text "()"} creates a new serial number
  1844   that is unique over the runtime of the Isabelle/ML process.
  1768   that is unique over the runtime of the Isabelle/ML process.
  1845 
       
  1846   \end{description}
       
  1847 \<close>
  1769 \<close>
  1848 
  1770 
  1849 text %mlex \<open>The following example shows how to create unique
  1771 text %mlex \<open>The following example shows how to create unique
  1850   temporary file names.
  1772   temporary file names.
  1851 \<close>
  1773 \<close>
  1878   @{index_ML_type "'a Synchronized.var"} \\
  1800   @{index_ML_type "'a Synchronized.var"} \\
  1879   @{index_ML Synchronized.var: "string -> 'a -> 'a Synchronized.var"} \\
  1801   @{index_ML Synchronized.var: "string -> 'a -> 'a Synchronized.var"} \\
  1880   @{index_ML Synchronized.guarded_access: "'a Synchronized.var ->
  1802   @{index_ML Synchronized.guarded_access: "'a Synchronized.var ->
  1881   ('a -> ('b * 'a) option) -> 'b"} \\
  1803   ('a -> ('b * 'a) option) -> 'b"} \\
  1882   \end{mldecls}
  1804   \end{mldecls}
  1883 
       
  1884   \begin{description}
       
  1885 
  1805 
  1886   \<^descr> Type @{ML_type "'a Synchronized.var"} represents synchronized
  1806   \<^descr> Type @{ML_type "'a Synchronized.var"} represents synchronized
  1887   variables with state of type @{ML_type 'a}.
  1807   variables with state of type @{ML_type 'a}.
  1888 
  1808 
  1889   \<^descr> @{ML Synchronized.var}~@{text "name x"} creates a synchronized
  1809   \<^descr> @{ML Synchronized.var}~@{text "name x"} creates a synchronized
  1898   manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is
  1818   manner; if @{text "f x"} produces @{ML SOME}~@{text "(y, x')"} it is
  1899   satisfied and assigns the new state value @{text "x'"}, broadcasts a
  1819   satisfied and assigns the new state value @{text "x'"}, broadcasts a
  1900   signal to all waiting threads on the associated condition variable,
  1820   signal to all waiting threads on the associated condition variable,
  1901   and returns the result @{text "y"}.
  1821   and returns the result @{text "y"}.
  1902 
  1822 
  1903   \end{description}
       
  1904 
  1823 
  1905   There are some further variants of the @{ML
  1824   There are some further variants of the @{ML
  1906   Synchronized.guarded_access} combinator, see @{file
  1825   Synchronized.guarded_access} combinator, see @{file
  1907   "~~/src/Pure/Concurrent/synchronized.ML"} for details.
  1826   "~~/src/Pure/Concurrent/synchronized.ML"} for details.
  1908 \<close>
  1827 \<close>
  1992   @{index_ML Exn.release: "'a Exn.result -> 'a"} \\
  1911   @{index_ML Exn.release: "'a Exn.result -> 'a"} \\
  1993   @{index_ML Par_Exn.release_all: "'a Exn.result list -> 'a list"} \\
  1912   @{index_ML Par_Exn.release_all: "'a Exn.result list -> 'a list"} \\
  1994   @{index_ML Par_Exn.release_first: "'a Exn.result list -> 'a list"} \\
  1913   @{index_ML Par_Exn.release_first: "'a Exn.result list -> 'a list"} \\
  1995   \end{mldecls}
  1914   \end{mldecls}
  1996 
  1915 
  1997   \begin{description}
       
  1998 
       
  1999   \<^descr> Type @{ML_type "'a Exn.result"} represents the disjoint sum of
  1916   \<^descr> Type @{ML_type "'a Exn.result"} represents the disjoint sum of
  2000   ML results explicitly, with constructor @{ML Exn.Res} for regular
  1917   ML results explicitly, with constructor @{ML Exn.Res} for regular
  2001   values and @{ML "Exn.Exn"} for exceptions.
  1918   values and @{ML "Exn.Exn"} for exceptions.
  2002 
  1919 
  2003   \<^descr> @{ML Exn.capture}~@{text "f x"} manages the evaluation of
  1920   \<^descr> @{ML Exn.capture}~@{text "f x"} manages the evaluation of
  2024   \<^descr> @{ML Par_Exn.release_first} is similar to @{ML
  1941   \<^descr> @{ML Par_Exn.release_first} is similar to @{ML
  2025   Par_Exn.release_all}, but only the first (meaningful) exception that has
  1942   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
  1943   occurred in the original evaluation process is raised again, the others are
  2027   ignored.  That single exception may get handled by conventional
  1944   ignored.  That single exception may get handled by conventional
  2028   means in ML.
  1945   means in ML.
  2029 
       
  2030   \end{description}
       
  2031 \<close>
  1946 \<close>
  2032 
  1947 
  2033 
  1948 
  2034 subsection \<open>Parallel skeletons \label{sec:parlist}\<close>
  1949 subsection \<open>Parallel skeletons \label{sec:parlist}\<close>
  2035 
  1950 
  2053   \begin{mldecls}
  1968   \begin{mldecls}
  2054   @{index_ML Par_List.map: "('a -> 'b) -> 'a list -> 'b list"} \\
  1969   @{index_ML Par_List.map: "('a -> 'b) -> 'a list -> 'b list"} \\
  2055   @{index_ML Par_List.get_some: "('a -> 'b option) -> 'a list -> 'b option"} \\
  1970   @{index_ML Par_List.get_some: "('a -> 'b option) -> 'a list -> 'b option"} \\
  2056   \end{mldecls}
  1971   \end{mldecls}
  2057 
  1972 
  2058   \begin{description}
       
  2059 
       
  2060   \<^descr> @{ML Par_List.map}~@{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} is like @{ML
  1973   \<^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"}
  1974   "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.
  1975   for @{text "i = 1, \<dots>, n"} is performed in parallel.
  2063 
  1976 
  2064   An exception in any @{text "f x\<^sub>i"} cancels the overall evaluation
  1977   An exception in any @{text "f x\<^sub>i"} cancels the overall evaluation
  2076   Par_List.map}.
  1989   Par_List.map}.
  2077 
  1990 
  2078   This generic parallel choice combinator is the basis for derived
  1991   This generic parallel choice combinator is the basis for derived
  2079   forms, such as @{ML Par_List.find_some}, @{ML Par_List.exists}, @{ML
  1992   forms, such as @{ML Par_List.find_some}, @{ML Par_List.exists}, @{ML
  2080   Par_List.forall}.
  1993   Par_List.forall}.
  2081 
       
  2082   \end{description}
       
  2083 \<close>
  1994 \<close>
  2084 
  1995 
  2085 text %mlex \<open>Subsequently, the Ackermann function is evaluated in
  1996 text %mlex \<open>Subsequently, the Ackermann function is evaluated in
  2086   parallel for some ranges of arguments.\<close>
  1997   parallel for some ranges of arguments.\<close>
  2087 
  1998 
  2126   @{index_ML Lazy.lazy: "(unit -> 'a) -> 'a lazy"} \\
  2037   @{index_ML Lazy.lazy: "(unit -> 'a) -> 'a lazy"} \\
  2127   @{index_ML Lazy.value: "'a -> 'a lazy"} \\
  2038   @{index_ML Lazy.value: "'a -> 'a lazy"} \\
  2128   @{index_ML Lazy.force: "'a lazy -> 'a"} \\
  2039   @{index_ML Lazy.force: "'a lazy -> 'a"} \\
  2129   \end{mldecls}
  2040   \end{mldecls}
  2130 
  2041 
  2131   \begin{description}
       
  2132 
       
  2133   \<^descr> Type @{ML_type "'a lazy"} represents lazy values over type @{verbatim
  2042   \<^descr> Type @{ML_type "'a lazy"} represents lazy values over type @{verbatim
  2134   "'a"}.
  2043   "'a"}.
  2135 
  2044 
  2136   \<^descr> @{ML Lazy.lazy}~@{text "(fn () => e)"} wraps the unevaluated
  2045   \<^descr> @{ML Lazy.lazy}~@{text "(fn () => e)"} wraps the unevaluated
  2137   expression @{text e} as unfinished lazy value.
  2046   expression @{text e} as unfinished lazy value.
  2143   lazy values.
  2052   lazy values.
  2144 
  2053 
  2145   \<^descr> @{ML Lazy.force}~@{text x} produces the result of the lazy value in a
  2054   \<^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
  2055   thread-safe manner as explained above. Thus it may cause the current thread
  2147   to wait on a pending evaluation attempt by another thread.
  2056   to wait on a pending evaluation attempt by another thread.
  2148 
       
  2149   \end{description}
       
  2150 \<close>
  2057 \<close>
  2151 
  2058 
  2152 
  2059 
  2153 subsection \<open>Futures \label{sec:futures}\<close>
  2060 subsection \<open>Futures \label{sec:futures}\<close>
  2154 
  2061 
  2219   @{index_ML Future.cancel_group: "Future.group -> unit"} \\[0.5ex]
  2126   @{index_ML Future.cancel_group: "Future.group -> unit"} \\[0.5ex]
  2220   @{index_ML Future.promise: "(unit -> unit) -> 'a future"} \\
  2127   @{index_ML Future.promise: "(unit -> unit) -> 'a future"} \\
  2221   @{index_ML Future.fulfill: "'a future -> 'a -> unit"} \\
  2128   @{index_ML Future.fulfill: "'a future -> 'a -> unit"} \\
  2222   \end{mldecls}
  2129   \end{mldecls}
  2223 
  2130 
  2224   \begin{description}
       
  2225 
       
  2226   \<^descr> Type @{ML_type "'a future"} represents future values over type
  2131   \<^descr> Type @{ML_type "'a future"} represents future values over type
  2227   @{verbatim "'a"}.
  2132   @{verbatim "'a"}.
  2228 
  2133 
  2229   \<^descr> @{ML Future.fork}~@{text "(fn () => e)"} registers the unevaluated
  2134   \<^descr> @{ML Future.fork}~@{text "(fn () => e)"} registers the unevaluated
  2230   expression @{text e} as unfinished future value, to be evaluated eventually
  2135   expression @{text e} as unfinished future value, to be evaluated eventually
  2235   fork several futures simultaneously. The @{text params} consist of the
  2140   fork several futures simultaneously. The @{text params} consist of the
  2236   following fields:
  2141   following fields:
  2237 
  2142 
  2238   \begin{itemize}
  2143   \begin{itemize}
  2239 
  2144 
  2240   \<^item> @{text "name : string"} (default @{ML "\"\""}) specifies a common name
  2145     \item @{text "name : string"} (default @{ML "\"\""}) specifies a common name
  2241   for the tasks of the forked futures, which serves diagnostic purposes.
  2146     for the tasks of the forked futures, which serves diagnostic purposes.
  2242 
  2147 
  2243   \<^item> @{text "group : Future.group option"} (default @{ML NONE}) specifies
  2148     \item @{text "group : Future.group option"} (default @{ML NONE}) specifies
  2244   an optional task group for the forked futures. @{ML NONE} means that a new
  2149     an optional task group for the forked futures. @{ML NONE} means that a new
  2245   sub-group of the current worker-thread task context is created. If this is
  2150     sub-group of the current worker-thread task context is created. If this is
  2246   not a worker thread, the group will be a new root in the group hierarchy.
  2151     not a worker thread, the group will be a new root in the group hierarchy.
  2247 
  2152 
  2248   \<^item> @{text "deps : Future.task list"} (default @{ML "[]"}) specifies
  2153     \item @{text "deps : Future.task list"} (default @{ML "[]"}) specifies
  2249   dependencies on other future tasks, i.e.\ the adjacency relation in the
  2154     dependencies on other future tasks, i.e.\ the adjacency relation in the
  2250   global task queue. Dependencies on already finished tasks are ignored.
  2155     global task queue. Dependencies on already finished tasks are ignored.
  2251 
  2156 
  2252   \<^item> @{text "pri : int"} (default @{ML 0}) specifies a priority within the
  2157     \item @{text "pri : int"} (default @{ML 0}) specifies a priority within the
  2253   task queue.
  2158     task queue.
  2254 
  2159 
  2255   Typically there is only little deviation from the default priority @{ML 0}.
  2160     Typically there is only little deviation from the default priority @{ML 0}.
  2256   As a rule of thumb, @{ML "~1"} means ``low priority" and @{ML 1} means
  2161     As a rule of thumb, @{ML "~1"} means ``low priority" and @{ML 1} means
  2257   ``high priority''.
  2162     ``high priority''.
  2258 
  2163 
  2259   Note that the task priority only affects the position in the queue, not the
  2164     Note that the task priority only affects the position in the queue, not the
  2260   thread priority. When a worker thread picks up a task for processing, it
  2165     thread priority. When a worker thread picks up a task for processing, it
  2261   runs with the normal thread priority to the end (or until canceled). Higher
  2166     runs with the normal thread priority to the end (or until canceled). Higher
  2262   priority tasks that are queued later need to wait until this (or another)
  2167     priority tasks that are queued later need to wait until this (or another)
  2263   worker thread becomes free again.
  2168     worker thread becomes free again.
  2264 
  2169 
  2265   \<^item> @{text "interrupts : bool"} (default @{ML true}) tells whether the
  2170     \item @{text "interrupts : bool"} (default @{ML true}) tells whether the
  2266   worker thread that processes the corresponding task is initially put into
  2171     worker thread that processes the corresponding task is initially put into
  2267   interruptible state. This state may change again while running, by modifying
  2172     interruptible state. This state may change again while running, by modifying
  2268   the thread attributes.
  2173     the thread attributes.
  2269 
  2174 
  2270   With interrupts disabled, a running future task cannot be canceled.  It is
  2175     With interrupts disabled, a running future task cannot be canceled.  It is
  2271   the responsibility of the programmer that this special state is retained
  2176     the responsibility of the programmer that this special state is retained
  2272   only briefly.
  2177     only briefly.
  2273 
  2178 
  2274   \end{itemize}
  2179   \end{itemize}
  2275 
  2180 
  2276   \<^descr> @{ML Future.join}~@{text x} retrieves the value of an already finished
  2181   \<^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
  2182   future, which may lead to an exception, according to the result of its
  2334   canceled.
  2239   canceled.
  2335 
  2240 
  2336   \<^descr> @{ML Future.fulfill}~@{text "x a"} finishes the passive future @{text
  2241   \<^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,
  2242   x} by the given value @{text a}. If the promise has already been canceled,
  2338   the attempt to fulfill it causes an exception.
  2243   the attempt to fulfill it causes an exception.
  2339 
       
  2340   \end{description}
       
  2341 \<close>
  2244 \<close>
  2342 
  2245 
  2343 end
  2246 end