<!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">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<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 MLTONWIKIVERSION</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> CharArray: MLTON_MONO_ARRAY <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> CharArray.array
                                            </FONT></B><B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> elem </FONT></B>=<B><FONT COLOR="#228B22"> CharArray.elem
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> CharVector: MLTON_MONO_VECTOR <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> CharVector.vector
                                              </FONT></B><B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> elem </FONT></B>=<B><FONT COLOR="#228B22"> CharVector.elem
      </FONT></B><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> LargeReal: MLTON_REAL <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> LargeReal.real
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> LargeWord: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> LargeWord.word
      </FONT></B><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> Real: MLTON_REAL <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Real.real
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Real32: <B><FONT COLOR="#0000FF">sig</FONT></B>
                           <B><FONT COLOR="#0000FF">include</FONT></B> MLTON_REAL
                           <B><FONT COLOR="#A020F0">val</FONT></B> castFromWord: Word32.word -&gt; t
                           <B><FONT COLOR="#A020F0">val</FONT></B> castToWord: t -&gt; Word32.word
                        <B><FONT COLOR="#0000FF">end</FONT></B> <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Real32.real
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Real64: <B><FONT COLOR="#0000FF">sig</FONT></B>
                           <B><FONT COLOR="#0000FF">include</FONT></B> MLTON_REAL
                           <B><FONT COLOR="#A020F0">val</FONT></B> castFromWord: Word64.word -&gt; t
                           <B><FONT COLOR="#A020F0">val</FONT></B> castToWord: t -&gt; Word64.word
                        <B><FONT COLOR="#0000FF">end</FONT></B> <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Real64.real
      </FONT></B><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><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> 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><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word8.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word16: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word16.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word32: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word32.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word64: MLTON_WORD <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word64.word
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word8Array: MLTON_MONO_ARRAY <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word8Array.array
                                             </FONT></B><B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> elem </FONT></B>=<B><FONT COLOR="#228B22"> Word8Array.elem
      </FONT></B><B><FONT COLOR="#0000FF">structure</FONT></B> Word8Vector: MLTON_MONO_VECTOR <B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> Word8Vector.vector
                                               </FONT></B><B><FONT COLOR="#0000FF">where</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> elem </FONT></B>=<B><FONT COLOR="#228B22"> Word8Vector.elem
      </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="MLtonMonoArray">MLtonMonoArray</a> 
</p>
</li>
    <li>
<p>
 <a href="MLtonMonoVector">MLtonMonoVector</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="MLtonReal">MLtonReal</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>  <br>
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. 
</p>
</li>
    <li class="gap">
<p>
 <tt>isMLton</tt>  <br>
is always true in a MLton implementation, and is always false in a  stub implementation. 
</p>
</li>
    <li class="gap">
<p>
 <tt>share&nbsp;x</tt>  <br>
maximizes sharing in the heap for the object graph reachable from  <tt>x</tt>. 
</p>
</li>
    <li class="gap">
<p>
 <tt>shareAll&nbsp;()</tt>  <br>
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. 
</p>
</li>
    <li class="gap">
<p>
 <tt>size&nbsp;x</tt>  <br>
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. 
</p>
</li>

    </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> <B><FONT COLOR="#228B22">'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 <B><FONT COLOR="#BC8F8F">&quot;The size of &quot;</FONT></B>
          ; print name
          ; print <B><FONT COLOR="#BC8F8F">&quot; is &gt;= &quot;</FONT></B>
          ; print (Int.toString min)
          ; print <B><FONT COLOR="#BC8F8F">&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 (<B><FONT COLOR="#BC8F8F">&quot;a char&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">0</FONT></B>, #<B><FONT COLOR="#BC8F8F">&quot;c&quot;</FONT></B>)
    ; printSize (<B><FONT COLOR="#BC8F8F">&quot;an int list of length 4&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">48</FONT></B>, l)
    ; printSize (<B><FONT COLOR="#BC8F8F">&quot;a string of length 10&quot;</FONT></B>, <B><FONT COLOR="#5F9EA0">24</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;0123456789&quot;</FONT></B>)
    ; printSize (<B><FONT COLOR="#BC8F8F">&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 (<B><FONT COLOR="#BC8F8F">&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 (<B><FONT COLOR="#BC8F8F">&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 (<B><FONT COLOR="#BC8F8F">&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 <B><FONT COLOR="#BC8F8F">&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, <B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>])
<B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> _ =
   (print <B><FONT COLOR="#BC8F8F">&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 <B><FONT COLOR="#BC8F8F">&quot;&gt; 1000.\n&quot;</FONT></B>
      <B><FONT COLOR="#A020F0">else</FONT></B> print <B><FONT COLOR="#BC8F8F">&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 2007-08-23 03:45:32 by <span title="c-71-57-91-146.hsd1.il.comcast.net"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>
