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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
<!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>VariableArityPolymorphism - 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%;">
VariableArityPolymorphism
<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>
<a href = "TitleIndex">Index</a>
</table>
<div id="content" lang="en" dir="ltr">
<a href="StandardML">Standard ML</a> programmers often face the problem of how to provide a variable-arity polymorphic function. For example, suppose one is defining a combinator library, e.g. for parsing or pickling. The signature for such a library might look something like the following.
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> COMBINATOR =
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> int: int t
<B><FONT COLOR="#A020F0">val</FONT></B> real: real t
<B><FONT COLOR="#A020F0">val</FONT></B> string: string t
<B><FONT COLOR="#A020F0">val</FONT></B> unit: unit t
<B><FONT COLOR="#A020F0">val</FONT></B> tuple2: 'a1 t * 'a2 t -> ('a1 * 'a2) t
<B><FONT COLOR="#A020F0">val</FONT></B> tuple3: 'a1 t * 'a2 t * 'a3 t -> ('a1 * 'a2 * 'a3) t
<B><FONT COLOR="#A020F0">val</FONT></B> tuple4: 'a1 t * 'a2 t * 'a3 t * 'a4 t
-> ('a1 * 'a2 * 'a3 * 'a4) t
...
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
The question is how to define a variable-arity tuple combinator. Traditionally, the only way to take a variable number of arguments in SML is to put the arguments in a list (or vector) and pass that. So, one might define a tuple combinator with the following signature.
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> tupleN: 'a list -> 'a list t
</PRE>
<p>
</p>
<p>
The problem with this approach is that as soon as one places values in a list, they must all have the same type. So, programmers often take an alternative approach, and define a family of <tt>tuple<N></tt> functions, as we see in the <tt>COMBINATOR</tt> signature above.
</p>
<p>
The family-of-functions approach is ugly for many reasons. First, it clutters the signature with a number of functions when there should really only be one. Second, it is <em>closed</em>, in that there are a fixed number of tuple combinators in the interface, and should a client need a combinator for a large tuple, he is out of luck. Third, this approach often requires a lot of duplicate code in the implementation of the combinators.
</p>
<p>
Fortunately, using <a href="Fold01N">Fold01N</a> and <a href="ProductType">products</a>, one can provide an interface and implementation that solves all these problems. Here is a simple pickling module that converts values to strings.
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Pickler =
<B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> 'a -> string
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> unit = <B><FONT COLOR="#A020F0">fn</FONT></B> () => <B><FONT COLOR="#BC8F8F">""</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> int = Int.toString
<B><FONT COLOR="#A020F0">val</FONT></B> real = Real.toString
<B><FONT COLOR="#A020F0">val</FONT></B> string = id
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a accum </FONT></B>=<B><FONT COLOR="#228B22"> 'a * string list -> string list
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> tuple =
<B><FONT COLOR="#A020F0">fn</FONT></B> z =>
Fold01N.fold
{finish = <B><FONT COLOR="#A020F0">fn</FONT></B> ps => <B><FONT COLOR="#A020F0">fn</FONT></B> x => concat (rev (ps (x, []))),
start = <B><FONT COLOR="#A020F0">fn</FONT></B> p => <B><FONT COLOR="#A020F0">fn</FONT></B> (x, l) => p x :: l,
zero = unit}
z
<B><FONT COLOR="#A020F0">val</FONT></B> ` =
<B><FONT COLOR="#A020F0">fn</FONT></B> z =>
Fold01N.step1
{combine = (<B><FONT COLOR="#A020F0">fn</FONT></B> (p, p') => <B><FONT COLOR="#A020F0">fn</FONT></B> (x & x', l) => p' x' :: <B><FONT COLOR="#BC8F8F">","</FONT></B> :: p (x, l))}
z
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
If one has <tt>n</tt> picklers of types
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> p1: a1 Pickler.t
<B><FONT COLOR="#A020F0">val</FONT></B> p2: a2 Pickler.t
...
<B><FONT COLOR="#A020F0">val</FONT></B> pn: an Pickler.t
</PRE>
<p>
</p>
<p>
then one can construct a pickler for n-ary products as follows.
</p>
<pre class=code>
tuple `p1 `p2 ... `pn $ : (a1 & a2 & ... & an) Pickler.t
</PRE>
<p>
</p>
<p>
For example, with <tt>Pickler</tt> in scope, one can prove the following equations.
</p>
<pre class=code>
<B><FONT COLOR="#BC8F8F">""</FONT></B> = tuple $ ()
<B><FONT COLOR="#BC8F8F">"1"</FONT></B> = tuple `int $ <B><FONT COLOR="#5F9EA0">1</FONT></B>
<B><FONT COLOR="#BC8F8F">"1,2.0"</FONT></B> = tuple `int `real $ (<B><FONT COLOR="#5F9EA0">1</FONT></B> & <B><FONT COLOR="#5F9EA0">2.0</FONT></B>)
<B><FONT COLOR="#BC8F8F">"1,2.0,three"</FONT></B> = tuple `int `real `string $ (<B><FONT COLOR="#5F9EA0">1</FONT></B> & <B><FONT COLOR="#5F9EA0">2.0</FONT></B> & <B><FONT COLOR="#BC8F8F">"three"</FONT></B>)
</PRE>
<p>
</p>
<p>
Here is the signature for <tt>Pickler</tt>. It shows why the <tt>accum</tt> type is useful.
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> PICKLER =
<B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> int: int t
<B><FONT COLOR="#A020F0">val</FONT></B> real: real t
<B><FONT COLOR="#A020F0">val</FONT></B> string: string t
<B><FONT COLOR="#A020F0">val</FONT></B> unit: unit t
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a accum
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> ` : ('a accum, 'b t, ('a, 'b) prod accum,
'z1, 'z2, 'z3, 'z4, 'z5, 'z6, 'z7) Fold01N.step1
<B><FONT COLOR="#A020F0">val</FONT></B> tuple: ('a t, 'a accum, 'b accum, 'b t, unit t,
'z1, 'z2, 'z3, 'z4, 'z5) Fold01N.t
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> Pickler: PICKLER = Pickler
</PRE>
<p>
</p>
</div>
<p>
<hr>
Last edited on 2006-03-21 22:06:02 by <span title="adsl-71-141-16-94.dsl.snfc21.sbcglobal.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>
|