<!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>MLBasisExamples - 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%;">
      MLBasisExamples
    <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">
Here are some example uses of <a href="MLBasis">ML Basis</a> files. <h2 id="head-47367b6e18a7e13a70299d934342eaad85f98a84">Complete program</h2>
Suppose your complete program consists of the files <tt>file1.sml</tt>, ..., <tt>filen.sml</tt>, which depend upon libraries <tt>lib1.mlb</tt>, ..., <tt>libm.mlb</tt>. 
<pre>(* import libraries *)
lib1.mlb
...
libm.mlb

(* program files *)
file1.sml
...
filen.sml
</pre><p>
The bases denoted by <tt>lib1.mlb</tt>, ..., <tt>libm.mlb</tt> are merged (bindings of names in later bases take precedence over bindings of the same name in earlier bases), producing a basis in which <tt>file1.sml</tt>, ..., <tt>filen.sml</tt> are elaborated, adding additional bindings to the basis. 
</p>
<h2 id="head-bbdea99ed8fa0bf96f01819de00940a9a5bd15df">Export filter</h2>
<p>
Suppose you only want to export certain structures, signatures, and functors from a collection of files. 
</p>

<pre>local
  file1.sml
  ...
  filen.sml
in
  (* export filter here *)
  functor F
  structure S
end
</pre><p>
While <tt>file1.sml</tt>, ..., <tt>filen.sml</tt> may declare top-level identifiers in addition to <tt>F</tt> and <tt>S</tt>, such names are not accessible to programs and libraries that import this <tt>.mlb</tt>. 
</p>
<h2 id="head-109e200ced2bf5d4c6ce6619a4e1f67caf0b81a5">Export filter with renaming</h2>
<p>
Suppose you want an export filter, but want to rename one of the modules. 
</p>

<pre>local
  file1.sml
  ...
  filen.sml
in
  (* export filter, with renaming, here *)
  functor F
  structure S' = S
end
</pre><p>
Note that <tt>functor&nbsp;F</tt> is an abbreviation for <tt>functor&nbsp;F&nbsp;=&nbsp;F</tt>, which simply exports an identifier under the same name. 
</p>
<h2 id="head-f266e8ecbc717ff659224d8ad3a7d4a937429adb">Import filter</h2>
<p>
Suppose you only want to import a functor <tt>F</tt> from one library and a structure <tt>S</tt> from another library. 
</p>

<pre>local
  lib1.mlb
in
  (* import filter here *)
  functor F
end
local
  lib2.mlb
in
  (* import filter here *)
  structure S
end
file1.sml
...
filen.sml
</pre><h2 id="head-90f8bc63c1e2a572a4d5c05af4cd2c6c8ebd0080">Import filter with renaming</h2>
<p>
Suppose you want to import a structure <tt>S</tt> from one library and another structure <tt>S</tt> from another library. 
</p>

<pre>local
  lib1.mlb
in
  (* import filter, with renaming, here *)
  structure S1 = S
end
local
  lib2.mlb
in
  (* import filter, with renaming, here *)
  structure S2 = S
end
file1.sml
...
filen.sml
</pre><h2 id="head-b0df1400a6248030b137c5676e8feb1a2195343d">Full Basis</h2>
<p>
Since the Modules level of SML is the natural means for organizing program and library components, MLB files provide convenient syntax for renaming Modules level identifiers (in fact, renaming of functor identifiers provides a mechanism that is not available in SML). However, please note that <tt>.mlb</tt> files elaborate to full bases including top-level types and values (including infix status), in addition to structures, signatures, and functors.  For example, suppose you wished to extend the <a href="BasisLibrary">Basis Library</a> with an <tt>('a,&nbsp;'b)&nbsp;either</tt> datatype corresponding to a disjoint sum; the type and some operations should be available at the top-level; additionally, a signature and structure provide the complete interface. 
</p>
<p>
We could use the following files. 
</p>
<p>
<tt>either-sigs.sml</tt> 
</p>

    <ul>

 
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> EITHER_GLOBAL =
  <B><FONT COLOR="#0000FF">sig</FONT></B>
    <B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> ('a, 'b) either </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">Left</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a </FONT></B>|<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">Right</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'b
    </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> &amp;  : ('a -&gt; 'c) * ('b -&gt; 'c) -&gt; ('a, 'b) either -&gt; 'c
    <B><FONT COLOR="#A020F0">val</FONT></B> &amp;&amp; : ('a -&gt; 'c) * ('b -&gt; 'd) -&gt; ('a, 'b) either -&gt; ('c, 'd) either
  <B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">signature</FONT></B> EITHER =
  <B><FONT COLOR="#0000FF">sig</FONT></B>
    <B><FONT COLOR="#0000FF">include</FONT></B> EITHER_GLOBAL
    <B><FONT COLOR="#A020F0">val</FONT></B> isLeft  : ('a, 'b) either -&gt; bool
    <B><FONT COLOR="#A020F0">val</FONT></B> isRight : ('a, 'b) either -&gt; bool
    ...
  <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>

    </ul>


<p>
<tt>either-strs.sml</tt> 
</p>

    <ul>

 
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Either : EITHER =
  <B><FONT COLOR="#0000FF">struct</FONT></B>
    <B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> ('a, 'b) either </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">Left</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a </FONT></B>|<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">Right</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'b
    </FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> f &amp; g = <B><FONT COLOR="#A020F0">fn</FONT></B> x =&gt;
      <B><FONT COLOR="#A020F0">case</FONT></B> x <B><FONT COLOR="#A020F0">of</FONT></B> Left z =&gt; f z | Right z =&gt; g z
    <B><FONT COLOR="#A020F0">fun</FONT></B> f &amp;&amp; g = (Left o f) &amp; (Right o g)
    <B><FONT COLOR="#A020F0">fun</FONT></B> isLeft x = ((<B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; true) &amp; (<B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; false)) x
    <B><FONT COLOR="#A020F0">fun</FONT></B> isRight x = (not o isLeft) x
    ...
  <B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> EitherGlobal : EITHER_GLOBAL = Either
</PRE>
<p>
 
</p>

    </ul>


<p>
<tt>either-infixes.sml</tt> 
</p>

    <ul>

 
<pre class=code>
<B><FONT COLOR="#A020F0">infixr</FONT></B> 3 &amp; &amp;&amp;
</PRE>
<p>
 
</p>

    </ul>


<p>
<tt>either-open.sml</tt> 
</p>

    <ul>

 
<pre class=code>
<B><FONT COLOR="#0000FF">open</FONT></B> EitherGlobal
</PRE>
<p>
 
</p>

    </ul>


<p>
<tt>either.mlb</tt> 
</p>

    <ul>

 
<pre>either-infixes.sml
local
  (* import Basis Library *)
  $(SML_LIB)/basis/basis.mlb
  either-sigs.sml
  either-strs.sml
in
  signature EITHER
  structure Either
  either-open.sml
end
</pre>
    </ul>


<p>
A client that imports <tt>either.mlb</tt> will have access to neither <tt>EITHER_GLOBAL</tt> nor <tt>EitherGlobal</tt>, but will have access to the type <tt>either</tt> and the values <tt>&amp;</tt> and <tt>&amp;&amp;</tt> (with infix status) in the top-level environment.  Note that  <tt>either-infixes.sml</tt> is outside the scope of the local, because we want the infixes available in the implementation of the library and to clients of the library. 
</p>
</div>



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