File: Monomorphise

package info (click to toggle)
mlton 20100608-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 34,980 kB
  • ctags: 69,089
  • sloc: ansic: 18,421; lisp: 2,879; makefile: 1,570; sh: 1,325; pascal: 256; asm: 97
file content (135 lines) | stat: -rw-r--r-- 8,539 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!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>