<!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 20061025</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="#5F9EA0">structure</FONT></B> MLton:
   <B><FONT COLOR="#5F9EA0">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> eq: 'a <B><FONT COLOR="#5F9EA0">*</FONT></B> 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">bool</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> isMLton: <B><FONT COLOR="#228B22">bool</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> share: 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> shareAll: <B><FONT COLOR="#228B22">unit</FONT></B> <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">unit</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> size: 'a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>

      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Array: MLTON_ARRAY
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> BinIO: MLTON_BIN_IO
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Cont: MLTON_CONT
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Exn: MLTON_EXN
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Finalizable: MLTON_FINALIZABLE
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> GC: MLTON_GC
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> IntInf: MLTON_INT_INF
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Itimer: MLTON_ITIMER
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Platform: MLTON_PLATFORM
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Pointer: MLTON_POINTER
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> ProcEnv: MLTON_PROC_ENV
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Process: MLTON_PROCESS
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Profile: MLTON_PROFILE
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Random: MLTON_RANDOM
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Rlimit: MLTON_RLIMIT
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Rusage: MLTON_RUSAGE
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Signal: MLTON_SIGNAL
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Socket: MLTON_SOCKET
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Syslog: MLTON_SYSLOG
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> TextIO: MLTON_TEXT_IO
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Thread: MLTON_THREAD
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Vector: MLTON_VECTOR
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Weak: MLTON_WEAK
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Word: MLTON_WORD <B><FONT COLOR="#A020F0">where</FONT></B> <B><FONT COLOR="#A020F0">type</FONT></B> <B><FONT COLOR="#228B22">word</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> Word.<B><FONT COLOR="#228B22">word</FONT></B>
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> Word8: MLTON_WORD <B><FONT COLOR="#A020F0">where</FONT></B> <B><FONT COLOR="#A020F0">type</FONT></B> <B><FONT COLOR="#228B22">word</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> Word8.<B><FONT COLOR="#228B22">word</FONT></B>
      <B><FONT COLOR="#5F9EA0">structure</FONT></B> World: MLTON_WORLD
   <B><FONT COLOR="#A020F0">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> 'a printSize (name: <B><FONT COLOR="#228B22">string</FONT></B>, min: <B><FONT COLOR="#228B22">int</FONT></B>, value: 'a): <B><FONT COLOR="#228B22">unit</FONT></B><B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">if</FONT></B> MLton.size value <B><FONT COLOR="#5F9EA0">&gt;=</FONT></B> min
      <B><FONT COLOR="#A020F0">then</FONT></B>
         (<B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;The size of &quot;</FONT></B>
          ; <B><FONT COLOR="#A020F0">print</FONT></B> name
          ; <B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot; is &gt;= &quot;</FONT></B>
          ; <B><FONT COLOR="#A020F0">print</FONT></B> (Int.toString min)
          ; <B><FONT COLOR="#A020F0">print</FONT></B> <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">=</FONT></B> [1, 2, 3, 4]

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

<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="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> 10 <B><FONT COLOR="#5F9EA0">=</FONT></B> foldl (<B><FONT COLOR="#A020F0">op</FONT></B> <B><FONT COLOR="#5F9EA0">+</FONT></B>) 0 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="#A020F0">local</FONT></B>
   <B><FONT COLOR="#A020F0">open</FONT></B> MLton.Cont
<B><FONT COLOR="#A020F0">in</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> rc: <B><FONT COLOR="#228B22">int</FONT></B> option t option <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B>
      <B><FONT COLOR="#A020F0">case</FONT></B> callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k: <B><FONT COLOR="#228B22">int</FONT></B> option t <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (rc <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> k; throw (k, <B><FONT COLOR="#5F9EA0">NONE</FONT></B>))) <B><FONT COLOR="#A020F0">of</FONT></B>
         <B><FONT COLOR="#5F9EA0">NONE</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
       <B><FONT COLOR="#5F9EA0">|</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> i <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> (<B><FONT COLOR="#A020F0">concat</FONT></B> [Int.toString i, <B><FONT COLOR="#BC8F8F">&quot;\n&quot;</FONT></B>])
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B>
   (<B><FONT COLOR="#A020F0">print</FONT></B> <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 <B><FONT COLOR="#5F9EA0">&gt;</FONT></B> 1000
         <B><FONT COLOR="#A020F0">then</FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;&gt; 1000.\n&quot;</FONT></B>
      <B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">print</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;&lt; 1000.\n&quot;</FONT></B>)

<B><FONT COLOR="#A020F0">val</FONT></B> _ <B><FONT COLOR="#5F9EA0">=</FONT></B>
   <B><FONT COLOR="#A020F0">case</FONT></B> <B><FONT COLOR="#5F9EA0">!</FONT></B>rc <B><FONT COLOR="#A020F0">of</FONT></B>
      <B><FONT COLOR="#5F9EA0">NONE</FONT></B> <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> ()
    <B><FONT COLOR="#5F9EA0">|</FONT></B> <B><FONT COLOR="#5F9EA0">SOME</FONT></B> k <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (rc <B><FONT COLOR="#5F9EA0">:=</FONT></B> <B><FONT COLOR="#5F9EA0">NONE</FONT></B>; MLton.Cont.throw (k, <B><FONT COLOR="#5F9EA0">SOME</FONT></B> 13))
</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>
