Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > d07d7ab417d79053e7e0155c99e1a1c8 > files > 2349

mlton-20100608-3.fc15.i686.rpm

<!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>Monomorphise - 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%;">
      Monomorphise
    <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 = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
Monomorphise is a translation pass from the <a href="XML">XML</a> <a href="IntermediateLanguage">IntermediateLanguage</a> to the <a href="SXML">SXML</a> <a href="IntermediateLanguage">IntermediateLanguage</a>. <h2 id="head-55f8ebc805e65b5b71ddafdae390e3be2bcd69af">Description</h2>
<p>
Monomorphisation eliminates polymorphic values and datatype declarations by duplicating them for each type at which they are used. 
</p>
<p>
Consider the following <a href="XML">XML</a> program.  
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> <B><FONT COLOR="#228B22">'a</FONT></B> f (x: 'a) = T x
<B><FONT COLOR="#A020F0">val</FONT></B> a = f <B><FONT COLOR="#5F9EA0">1</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> b = f <B><FONT COLOR="#5F9EA0">2</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> z = f (<B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>)
</PRE>
  The result of monomorphising this program is the following <a href="SXML">SXML</a> program:  
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t1 </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T1</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int
</FONT></B><B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t2 </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">T2</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int * int
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> f1 (x: t1) = T1 x
<B><FONT COLOR="#A020F0">fun</FONT></B> f2 (x: t2) = T2 x
<B><FONT COLOR="#A020F0">val</FONT></B> a = f1 <B><FONT COLOR="#5F9EA0">1</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> b = f1 <B><FONT COLOR="#5F9EA0">2</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> z = f2 (<B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>)
</PRE>
 
</p>
<h2 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h2>
<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/xml/monomorphise.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">monomorphise.sig</a> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/xml/monomorphise.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">monomorphise.fun</a> <h2 id="head-35ec00231a68203708e39f0e2cc10b50c6bf62de">Details and Notes</h2>
<p>
The monomorphiser works by making one pass over the entire program. On the way down, it creates a cache for each variable declared in a polymorphic declaration that maps a lists of type arguments to a new variable name.  At a variable reference, it consults the cache (based on the types the variable is applied to).  If there is already an entry in the cache, it is used.  If not, a new entry is created.  On the way up, the monomorphiser duplicates a variable declaration for each entry in the cache. 
</p>
<p>
As with variables, the monomorphiser records all of the type at which constructors are used.  After the entire program is processed, the monomorphiser duplicates each datatype declaration and its associated constructors. 
</p>
<p>
The monomorphiser duplicates all of the functions declared in a <tt>fun</tt> declaration as a unit.  Consider the following program 
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> <B><FONT COLOR="#228B22">'a</FONT></B> f (x: 'a) = g x
<B><FONT COLOR="#A020F0">and</FONT></B> g (y: 'a) = f y
<B><FONT COLOR="#A020F0">val</FONT></B> a = f <B><FONT COLOR="#5F9EA0">13</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> b = g <B><FONT COLOR="#5F9EA0">14</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> c = f (<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>)
</PRE>
 and its monomorphisation 
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> f1 (x: int) = g1 x
<B><FONT COLOR="#A020F0">and</FONT></B> g1 (y: int) = f1 y
<B><FONT COLOR="#A020F0">fun</FONT></B> f2 (x : int * int) = g2 x
<B><FONT COLOR="#A020F0">and</FONT></B> g2 (y : int * int) = f2 y
<B><FONT COLOR="#A020F0">val</FONT></B> a = f1 <B><FONT COLOR="#5F9EA0">13</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> b = g1 <B><FONT COLOR="#5F9EA0">14</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> c = f2 (<B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">2</FONT></B>)
</PRE>
 
</p>
<h2 id="head-d9df837d9182159c3848b74ca46f0a57ec960cdb">Pathological datatype declarations</h2>
<p>
SML allows a pathological polymorphic datatype declaration in which recursive uses of the defined type constructor are applied to different type arguments than the definition.  This has been disallowed by others on type theoretic grounds.  A canonical example is the following. 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">A</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> ('a * 'a) t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> z : int t = B (B (A ((<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>))))
</PRE>
 The presence of the recursion in the datatype declaration might appear to cause the need for the monomorphiser to create an infinite number of types.  However, due to the absence of polymorphic recursion in SML, there are in fact only a finite number of instances of such types in any given program.  The monomorphiser translates the above program to the following one. 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t1 </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B1</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t2
</FONT></B><B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t2 </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B2</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t3
</FONT></B><B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t3 </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">A3</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> (int * int) * (int * int)
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> z : int t = B1 (B2 (A3 ((<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>))))
</PRE>
 It is crucial that the monomorphiser be allowed to drop unused constructors from datatype declarations in order for the translation to terminate. 
</p>
</div>



<p>
<hr>
Last edited on 2009-12-18 11:00:50 by <span title="58.211.218.74">BaojianHua</span>.
</body></html>