src/Doc/Implementation/ML.thy
changeset 59902 6afbe5a99139
parent 59624 6c0e70b01111
child 60270 a147272b16f9
equal deleted inserted replaced
59901:840d03805755 59902:6afbe5a99139
   734 \<close>
   734 \<close>
   735 
   735 
   736 text %mlex \<open>The following artificial examples show how to produce
   736 text %mlex \<open>The following artificial examples show how to produce
   737   adhoc output of ML values for debugging purposes.\<close>
   737   adhoc output of ML values for debugging purposes.\<close>
   738 
   738 
   739 ML \<open>
   739 ML_val \<open>
   740   val x = 42;
   740   val x = 42;
   741   val y = true;
   741   val y = true;
   742 
   742 
   743   writeln (@{make_string} {x = x, y = y});
   743   writeln (@{make_string} {x = x, y = y});
   744 
   744 
   926 text %mlex \<open>The following example shows how to fill a text buffer
   926 text %mlex \<open>The following example shows how to fill a text buffer
   927   incrementally by adding strings, either individually or from a given
   927   incrementally by adding strings, either individually or from a given
   928   list.
   928   list.
   929 \<close>
   929 \<close>
   930 
   930 
   931 ML \<open>
   931 ML_val \<open>
   932   val s =
   932   val s =
   933     Buffer.empty
   933     Buffer.empty
   934     |> Buffer.add "digits: "
   934     |> Buffer.add "digits: "
   935     |> fold (Buffer.add o string_of_int) (0 upto 9)
   935     |> fold (Buffer.add o string_of_int) (0 upto 9)
   936     |> Buffer.content;
   936     |> Buffer.content;
  1089   warning (cat_lines
  1089   warning (cat_lines
  1090    ["Beware the Jabberwock, my son!",
  1090    ["Beware the Jabberwock, my son!",
  1091     "The jaws that bite, the claws that catch!",
  1091     "The jaws that bite, the claws that catch!",
  1092     "Beware the Jubjub Bird, and shun",
  1092     "Beware the Jubjub Bird, and shun",
  1093     "The frumious Bandersnatch!"]);
  1093     "The frumious Bandersnatch!"]);
       
  1094 \<close>
       
  1095 
       
  1096 text \<open>
       
  1097   \medskip An alternative is to make a paragraph of freely-floating words as
       
  1098   follows.
       
  1099 \<close>
       
  1100 
       
  1101 ML_command \<open>
       
  1102   warning (Pretty.string_of (Pretty.para
       
  1103     "Beware the Jabberwock, my son! \
       
  1104     \The jaws that bite, the claws that catch! \
       
  1105     \Beware the Jubjub Bird, and shun \
       
  1106     \The frumious Bandersnatch!"))
       
  1107 \<close>
       
  1108 
       
  1109 text \<open>
       
  1110   This has advantages with variable window / popup sizes, but might make it
       
  1111   harder to search for message content systematically, e.g.\ by other tools or
       
  1112   by humans expecting the ``verse'' of a formal message in a fixed layout.
  1094 \<close>
  1113 \<close>
  1095 
  1114 
  1096 
  1115 
  1097 section \<open>Exceptions \label{sec:exceptions}\<close>
  1116 section \<open>Exceptions \label{sec:exceptions}\<close>
  1098 
  1117 
  1549   extra applications of @{ML rev}.  The alternative @{ML fold_rev} can
  1568   extra applications of @{ML rev}.  The alternative @{ML fold_rev} can
  1550   be used in the few situations, where alternation should be
  1569   be used in the few situations, where alternation should be
  1551   prevented.
  1570   prevented.
  1552 \<close>
  1571 \<close>
  1553 
  1572 
  1554 ML \<open>
  1573 ML_val \<open>
  1555   val items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  1574   val items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  1556 
  1575 
  1557   val list1 = fold cons items [];
  1576   val list1 = fold cons items [];
  1558   @{assert} (list1 = rev items);
  1577   @{assert} (list1 = rev items);
  1559 
  1578 
  1562 \<close>
  1581 \<close>
  1563 
  1582 
  1564 text \<open>The subsequent example demonstrates how to \emph{merge} two
  1583 text \<open>The subsequent example demonstrates how to \emph{merge} two
  1565   lists in a natural way.\<close>
  1584   lists in a natural way.\<close>
  1566 
  1585 
  1567 ML \<open>
  1586 ML_val \<open>
  1568   fun merge_lists eq (xs, ys) = fold_rev (insert eq) ys xs;
  1587   fun merge_lists eq (xs, ys) = fold_rev (insert eq) ys xs;
  1569 \<close>
  1588 \<close>
  1570 
  1589 
  1571 text \<open>Here the first list is treated conservatively: only the new
  1590 text \<open>Here the first list is treated conservatively: only the new
  1572   elements from the second list are inserted.  The inside-out order of
  1591   elements from the second list are inserted.  The inside-out order of
  1815 
  1834 
  1816 text %mlex \<open>The following example shows how to create unique
  1835 text %mlex \<open>The following example shows how to create unique
  1817   temporary file names.
  1836   temporary file names.
  1818 \<close>
  1837 \<close>
  1819 
  1838 
  1820 ML \<open>
  1839 ML_val \<open>
  1821   val tmp1 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
  1840   val tmp1 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
  1822   val tmp2 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
  1841   val tmp2 = File.tmp_path (Path.basic ("foo" ^ serial_string ()));
  1823   @{assert} (tmp1 <> tmp2);
  1842   @{assert} (tmp1 <> tmp2);
  1824 \<close>
  1843 \<close>
  1825 
  1844 
  1876 
  1895 
  1877 text %mlex \<open>The following example implements a counter that produces
  1896 text %mlex \<open>The following example implements a counter that produces
  1878   positive integers that are unique over the runtime of the Isabelle
  1897   positive integers that are unique over the runtime of the Isabelle
  1879   process:\<close>
  1898   process:\<close>
  1880 
  1899 
  1881 ML \<open>
  1900 ML_val \<open>
  1882   local
  1901   local
  1883     val counter = Synchronized.var "counter" 0;
  1902     val counter = Synchronized.var "counter" 0;
  1884   in
  1903   in
  1885     fun next () =
  1904     fun next () =
  1886       Synchronized.guarded_access counter
  1905       Synchronized.guarded_access counter
  1887         (fn i =>
  1906         (fn i =>
  1888           let val j = i + 1
  1907           let val j = i + 1
  1889           in SOME (j, j) end);
  1908           in SOME (j, j) end);
  1890   end;
  1909   end;
  1891 \<close>
  1910 
  1892 
       
  1893 ML \<open>
       
  1894   val a = next ();
  1911   val a = next ();
  1895   val b = next ();
  1912   val b = next ();
  1896   @{assert} (a <> b);
  1913   @{assert} (a <> b);
  1897 \<close>
  1914 \<close>
  1898 
  1915