<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>ArrayLiteral</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20201002</a>
</div>
</div>
<div id="header">
<h1>ArrayLiteral</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="StandardML">Standard ML</a> does not have a syntax for array literals or
vector literals.  The only way to write down an array is like</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">Array</span><span class="p">.</span><span class="n">fromList</span><span class="w"> </span><span class="p">[</span><span class="n">w</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">]</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>No SML compiler produces efficient code for the above expression.  The
generated code allocates a list and then converts it to an array.  To
alleviate this, one could write down the same array using
<span class="monospaced">Array.tabulate</span>, or even using <span class="monospaced">Array.array</span> and <span class="monospaced">Array.update</span>, but
that is syntactically unwieldy.</p></div>
<div class="paragraph"><p>Fortunately, using <a href="Fold">Fold</a>, it is possible to define constants <span class="monospaced">A</span>,
and <span class="monospaced">&grave;</span> so that one can write down an array like:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">A</span><span class="w"> </span><span class="n">`w</span><span class="w"> </span><span class="n">`x</span><span class="w"> </span><span class="n">`y</span><span class="w"> </span><span class="n">`z</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>This is as syntactically concise as the <span class="monospaced">fromList</span> expression.
Furthermore, MLton, at least, will generate the efficient code as if
one had written down a use of <span class="monospaced">Array.array</span> followed by four uses of
<span class="monospaced">Array.update</span>.</p></div>
<div class="paragraph"><p>Along with <span class="monospaced">A</span> and <span class="monospaced">&grave;</span>, one can define a constant <span class="monospaced">V</span> that makes
it possible to define vector literals with the same syntax, e.g.,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">V</span><span class="w"> </span><span class="n">`w</span><span class="w"> </span><span class="n">`x</span><span class="w"> </span><span class="n">`y</span><span class="w"> </span><span class="n">`z</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Note that the same element indicator, <span class="monospaced">&grave;</span>, serves for both array
and vector literals.  Of course, the <span class="monospaced">$</span> is the end-of-arguments
marker always used with <a href="Fold">Fold</a>.  The only difference between an
array literal and vector literal is the <span class="monospaced">A</span> or <span class="monospaced">V</span> at the beginning.</p></div>
<div class="paragraph"><p>Here is the implementation of <span class="monospaced">A</span>, <span class="monospaced">V</span>, and <span class="monospaced">&grave;</span>.  We place them
in a structure and use signature abstraction to hide the type of the
accumulator.  See <a href="Fold">Fold</a> for more on this technique.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">Literal</span><span class="p">:&gt;</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">A</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">array</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">V</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">vector</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">`</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">)</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"></span>
<span class="w">         </span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">NONE</span><span class="p">,</span><span class="w"> </span><span class="n">ignore</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">opt</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">          </span><span class="k">case</span><span class="w"> </span><span class="n">opt</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">             </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                </span><span class="n">Array</span><span class="p">.</span><span class="n">tabulate</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="s">&quot;array0&quot;</span><span class="p">)</span><span class="w"></span>
<span class="w">           </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                </span><span class="k">let</span><span class="w"></span>
<span class="w">                   </span><span class="k">val</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">array</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">                   </span><span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
<span class="w">                </span><span class="k">in</span><span class="w"></span>
<span class="w">                   </span><span class="n">a</span><span class="w"></span>
<span class="w">                </span><span class="k">end</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="n">z</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">V</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">A</span><span class="p">,</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">vector</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">`</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">         </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">opt</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="p">))</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">           </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"></span>
<span class="w">           </span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">Array</span><span class="p">.</span><span class="n">update</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="n">a</span><span class="p">)))</span><span class="w"></span>
<span class="w">         </span><span class="n">z</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The idea of the code is for the fold to accumulate a count of the
number of elements, a sample element, and a function that fills in all
the elements.  When the fold is complete, the finishing function
allocates the array, applies the fill function, and returns the array.
The only difference between <span class="monospaced">A</span> and <span class="monospaced">V</span> is at the very end; <span class="monospaced">A</span> just
returns the array, while <span class="monospaced">V</span> converts it to a vector using
post-composition, which is further described on the <a href="Fold">Fold</a> page.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
