<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>MLtonStructure - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton 20051202</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      MLtonStructure
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
The <tt>MLton</tt> structure contains a lot of functionality that is not available in the <a href="BasisLibrary">Basis Library</a>.  As a warning, please keep in mind that the <tt>MLton</tt> structure and its substructures do change from release to release of MLton. 
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> MLton:
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> eq: 'a * 'a -&gt; bool
      <B><FONT COLOR="#A020F0">val</FONT></B> isMLton: bool
      <B><FONT COLOR="#A020F0">val</FONT></B> share: 'a -&gt; unit
      <B><FONT COLOR="#A020F0">val</FONT></B> shareAll: unit -&gt; unit
      <B><FONT COLOR="#A020F0">val</FONT></B> size: 'a -&gt; int

      <B><FONT COLOR="#0000FF">structure</FONT></B> Array: MLTON_ARRAY
      <B><FONT COLOR="#0000FF">structure</FONT></B> BinIO: MLTON_BIN_IO
      <B><FONT COLOR="#0000FF">structure</FONT></B> Cont: MLTON_CONT
      <B><FONT COLOR="#0000FF">structure</FONT></B> Exn: MLTON_EXN
      <B><FONT COLOR="#0000FF">structure</FONT></B> Finalizable: MLTON_FINALIZABLE
      <B><FONT COLOR="#0000FF">structure</FONT></B> GC: MLTON_GC
      <B><FONT COLOR="#0000FF">structure</FONT></B> IntInf: MLTON_INT_INF
      <B><FONT COLOR="#0000FF">structure</FONT></B> Itimer: MLTON_ITIMER
      <B><FONT COLOR="#0000FF">structure</FONT></B> Platform: MLTON_PLATFORM
      <B><FONT COLOR="#0000FF">structure</FONT></B> Pointer: MLTON_POINTER
      <B><FONT COLOR="#0000FF">structure</FONT></B> ProcEnv: MLTON_PROC_ENV
      <B><FONT COLOR="#0000FF">structure</FONT></B> Process: MLTON_PROCESS
      <B><FONT COLOR="#0000FF">structure</FONT></B> Profile: MLTON_PROFILE
      <B><FONT COLOR="#0000FF">structure</FONT></B> Random: MLTON_RANDOM
      <B><FONT COLOR="#0000FF">structure</FONT></B> Rlimit: MLTON_RLIMIT
      <B><FONT COLOR="#0000FF">structure</FONT></B> Rusage: MLTON_RUSAGE
      <B><FONT COLOR="#0000FF">structure</FONT></B> Signal: MLTON_SIGNAL
      <B><FONT COLOR="#0000FF">structure</FONT></B> Socket: MLTON_SOCKET
      <B><FONT COLOR="#0000FF">structure</FONT></B> Syslog: MLTON_SYSLOG
      <B><FONT COLOR="#0000FF">structure</FONT></B> TextIO: MLTON_TEXT_IO
      <B><FONT COLOR="#0000FF">structure</FONT></B> Thread: MLTON_THREAD
      <B><FONT COLOR="#0000FF">structure</FONT></B> Vector: MLTON_VECTOR
      <B><FONT COLOR="#0000FF">structure</FONT></B> Weak: MLTON_WEAK
      <B><FONT COLOR="#0000FF">structure</FONT></B> Word: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><FONT COLOR="#228B22"><B> word </FONT></B>=<FONT COLOR="#228B22"><B> Word.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word8: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><FONT COLOR="#228B22"><B> word </FONT></B>=<FONT COLOR="#228B22"><B> Word8.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> World: MLTON_WORLD
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<h2 id="head-58b7888b27adca6d197bc42df58e8c0c70fe97d0">Substructures</h2>

    <ul>

    <li>
<p>
 <a href="MLtonArray">MLtonArray</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonBinIO">MLtonBinIO</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonCont">MLtonCont</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonExn">MLtonExn</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonFinalizable">MLtonFinalizable</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonGC">MLtonGC</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonIntInf">MLtonIntInf</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonIO">MLtonIO</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonItimer">MLtonItimer</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonPlatform">MLtonPlatform</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonPointer">MLtonPointer</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonProcEnv">MLtonProcEnv</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonProcess">MLtonProcess</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonRandom">MLtonRandom</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonRlimit">MLtonRlimit</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonRusage">MLtonRusage</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonSignal">MLtonSignal</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonSocket">MLtonSocket</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonSyslog">MLtonSyslog</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonTextIO">MLtonTextIO</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonThread">MLtonThread</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonVector">MLtonVector</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonWeak">MLtonWeak</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonWord">MLtonWord</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonWorld">MLtonWorld</a> 
</p>
</li>

    </ul>


<h2 id="head-b1564f6b1512cbfa3cfcebc9a5badb6b239954f1">Values</h2>

    <ul>

    <li>
<p>
 <tt>eq&nbsp;(x,&nbsp;y)</tt> 
</p>
</li>

        <ul>

  returns true if <tt>x</tt> and <tt>y</tt> are equal as pointers.  For   simple types like <tt>char</tt>, <tt>int</tt>, and <tt>word</tt>, this is the   same as equals.  For arrays, datatypes, strings, tuples, and   vectors, this is a simple pointer equality.  The semantics is a bit   murky. 
        </ul>


    <li class="gap">
<p>
 <tt>isMLton</tt> 
</p>
</li>

        <ul>

  is always true in a MLton implementation, and is always false in a   stub implementation. 
        </ul>


    <li class="gap">
<p>
 <tt>share&nbsp;x</tt> 
</p>
</li>

        <ul>

  maximizes sharing in the heap for the object graph reachable from   <tt>x</tt>. 
        </ul>


    <li class="gap">
<p>
 <tt>shareAll&nbsp;()</tt> 
</p>
</li>

        <ul>

  maximizes sharing in the heap by sharing space for equivalent   immutable objects.  A call to <tt>shareAll</tt> performs a major   garbage collection, and takes time proportional to the size of the   heap. 
        </ul>


    <li class="gap">
<p>
 <tt>size&nbsp;x</tt> 
</p>
</li>

        <ul>

  returns the amount of heap space (in bytes) taken by the value of   <tt>x</tt>, including all objects reachable from <tt>x</tt> by following   pointers.  It takes time proportional to the size of <tt>x</tt>.  See   below for an example. 
        </ul>



    </ul>


<h2 id="head-48c21e9f702d3a4e4ebc0ce38e1d6c7f6bf73f5b">Example of MLton.size</h2>
<a id="size"></a> <p>
This example, <tt>size.sml</tt>, demonstrates the application of <tt>MLton.size</tt> to many different kinds of objects. 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> <FONT COLOR="#228B22"><B>'a</FONT></B> printSize (name: string, min: int, value: 'a): unit=
   <B><FONT COLOR="#A020F0">if</FONT></B> MLton.size value &gt;= min
      <B><FONT COLOR="#A020F0">then</FONT></B>
         (print <FONT COLOR="#BC8F8F"><B>&quot;The size of &quot;</FONT></B>
          ; print name
          ; print <FONT COLOR="#BC8F8F"><B>&quot; is &gt;= &quot;</FONT></B>
          ; print (Int.toString min)
          ; print <FONT COLOR="#BC8F8F"><B>&quot; bytes.\n&quot;</FONT></B>)
   <B><FONT COLOR="#A020F0">else</FONT></B> ()

<B><FONT COLOR="#A020F0">val</FONT></B> l = [<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>, <B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>]

<B><FONT COLOR="#A020F0">val</FONT></B> _ =
   (
    printSize (<FONT COLOR="#BC8F8F"><B>&quot;a char&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">0</FONT></B>, #<FONT COLOR="#BC8F8F"><B>&quot;c&quot;</FONT></B>)
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;an int list of length 4&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">48</FONT></B>, l)
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;a string of length 10&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">24</FONT></B>, <FONT COLOR="#BC8F8F"><B>&quot;0123456789&quot;</FONT></B>)
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;an int array of length 10&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">52</FONT></B>, Array.tabulate (<B><FONT COLOR="#5F9EA0">10</FONT></B>, <B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; <B><FONT COLOR="#5F9EA0">0</FONT></B>))
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;a double array of length 10&quot;</FONT></B>,
                 <B><FONT COLOR="#5F9EA0">92</FONT></B>, Array.tabulate (<B><FONT COLOR="#5F9EA0">10</FONT></B>, <B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; <B><FONT COLOR="#5F9EA0">0.0</FONT></B>))
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;an array of length 10 of 2-ples of ints&quot;</FONT></B>,
                 <B><FONT COLOR="#5F9EA0">92</FONT></B>, Array.tabulate (<B><FONT COLOR="#5F9EA0">10</FONT></B>, <B><FONT COLOR="#A020F0">fn</FONT></B> i =&gt; (i, i + <B><FONT COLOR="#5F9EA0">1</FONT></B>)))
    ; printSize (<FONT COLOR="#BC8F8F"><B>&quot;a useless function&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">0</FONT></B>, <B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; <B><FONT COLOR="#5F9EA0">13</FONT></B>)
    )

<I><FONT COLOR="#B22222">(* This is here so that the list is &quot;useful&quot;.
 * If it were removed, then the optimizer (remove-unused-constructors)
 * would remove l entirely.
 *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = <B><FONT COLOR="#A020F0">if</FONT></B> <B><FONT COLOR="#5F9EA0">10</FONT></B> = foldl (<B><FONT COLOR="#A020F0">op</FONT></B> +) <B><FONT COLOR="#5F9EA0">0</FONT></B> l
           <B><FONT COLOR="#A020F0">then</FONT></B> ()
        <B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <FONT COLOR="#BC8F8F"><B>&quot;bug&quot;</FONT></B>
   
<B><FONT COLOR="#0000FF">local</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> MLton.Cont
<B><FONT COLOR="#0000FF">in</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> rc: int option t option ref = ref NONE
   <B><FONT COLOR="#A020F0">val</FONT></B> _ =
      <B><FONT COLOR="#A020F0">case</FONT></B> callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k: int option t =&gt; (rc := SOME k; throw (k, NONE))) <B><FONT COLOR="#A020F0">of</FONT></B>
         NONE =&gt; ()
       | SOME i =&gt; print (concat [Int.toString i, <FONT COLOR="#BC8F8F"><B>&quot;\n&quot;</FONT></B>])
<B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> _ =
   (print <FONT COLOR="#BC8F8F"><B>&quot;The size of a continuation option ref is &quot;</FONT></B>
    ; <B><FONT COLOR="#A020F0">if</FONT></B> MLton.size rc &gt; <B><FONT COLOR="#5F9EA0">1000</FONT></B>
         <B><FONT COLOR="#A020F0">then</FONT></B> print <FONT COLOR="#BC8F8F"><B>&quot;&gt; 1000.\n&quot;</FONT></B>
      <B><FONT COLOR="#A020F0">else</FONT></B> print <FONT COLOR="#BC8F8F"><B>&quot;&lt; 1000.\n&quot;</FONT></B>)

<B><FONT COLOR="#A020F0">val</FONT></B> _ =
   <B><FONT COLOR="#A020F0">case</FONT></B> !rc <B><FONT COLOR="#A020F0">of</FONT></B>
      NONE =&gt; ()
    | SOME k =&gt; (rc := NONE; MLton.Cont.throw (k, SOME <B><FONT COLOR="#5F9EA0">13</FONT></B>))
</PRE>
<p>
 
</p>
<p>
Compile and run as usual. 
<pre>% mlton size.sml
% ./size
The size of a char is &gt;= 0 bytes.
The size of an int list of length 4 is &gt;= 48 bytes.
The size of a string of length 10 is &gt;= 24 bytes.
The size of an int array of length 10 is &gt;= 52 bytes.
The size of a double array of length 10 is &gt;= 92 bytes.
The size of an array of length 10 of 2-ples of ints is &gt;= 92 bytes.
The size of a useless function is &gt;= 0 bytes.
The size of a continuation option ref is &gt; 1000.
13
The size of a continuation option ref is &lt; 1000.
</pre>
</p>
</div>



<p>
<hr>
Last edited on 2005-12-01 23:10:46 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>
