<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>MatchCompile</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 20130715</a>
</div>
</div>
<div id="header">
<h1>MatchCompile</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="MatchCompile">MatchCompile</a> is a translation pass, agnostic in the
<a href="IntermediateLanguage">IntermediateLanguage</a>s between which it translates.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="MatchCompilation">Match compilation</a> converts a case expression with
nested patterns into a case expression with flat patterns.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://github.com/MLton/mlton/blob/master/mlton/match-compile/match-compile.sig"><span class="monospaced">match-compile.sig</span></a>
</p>
</li>
<li>
<p>
<a href="https://github.com/MLton/mlton/blob/master/mlton/match-compile/match-compile.fun"><span class="monospaced">match-compile.fun</span></a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_details_and_notes">Details and Notes</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">matchCompile</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="p">{</span><span class="n">caseType</span><span class="p">:</span><span class="w"> </span><span class="n">Type</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="cm">(* type of entire expression *)</span><span class="w"></span>
<span class="w">    </span><span class="n">cases</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">NestedPat</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">((</span><span class="n">Var</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Var</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Exp</span><span class="p">.</span><span class="n">t</span><span class="p">))</span><span class="w"> </span><span class="n">vector</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">conTycon</span><span class="p">:</span><span class="w"> </span><span class="n">Con</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Tycon</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">region</span><span class="p">:</span><span class="w"> </span><span class="n">Region</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">test</span><span class="p">:</span><span class="w"> </span><span class="n">Var</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">testType</span><span class="p">:</span><span class="w"> </span><span class="n">Type</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">tyconCons</span><span class="p">:</span><span class="w"> </span><span class="n">Tycon</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">{</span><span class="n">con</span><span class="p">:</span><span class="w"> </span><span class="n">Con</span><span class="p">.</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="n">hasArg</span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="p">}</span><span class="w"> </span><span class="n">vector</span><span class="p">}</span><span class="w"></span>
<span class="w">   </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">Exp</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">Layout</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">{</span><span class="n">isOnlyExns</span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="p">})</span><span class="w"> </span><span class="n">vector</span><span class="p">)</span><span class="w"> </span><span class="n">vector</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p><span class="monospaced">matchCompile</span> is complicated by the desire for modularity between the
match compiler and its caller.  Its caller is responsible for building
the right hand side of a rule <span class="monospaced">p =&gt; e</span>.  On the other hand, the match
compiler is responsible for destructing the test and binding new
variables to the components.  In order to connect the new variables
created by the match compiler with the variables in the pattern <span class="monospaced">p</span>,
the match compiler passes an environment back to its caller that maps
each variable in <span class="monospaced">p</span> to the corresponding variable introduced by the
match compiler.</p></div>
<div class="paragraph"><p>The match compiler builds a tree of n-way case expressions by working
from outside to inside and left to right in the patterns.  For example,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">case</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">  </span><span class="p">(_,</span><span class="w"> </span><span class="n">C1</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">e1</span><span class="w"></span>
<span class="p">|</span><span class="w"> </span><span class="p">(</span><span class="n">C2</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">C3</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">e2</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>is translated to</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">f1</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">e1</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">f2</span><span class="w"> </span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">e2</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">  </span><span class="k">case</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">     </span><span class="p">(</span><span class="n">x1</span><span class="p">,</span><span class="w"> </span><span class="n">x2</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="k">case</span><span class="w"> </span><span class="n">x1</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">          </span><span class="n">C2</span><span class="w"> </span><span class="n">b&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                      </span><span class="n">C1</span><span class="w"> </span><span class="n">a&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f1</span><span class="w"> </span><span class="n">a&#39;</span><span class="w"></span>
<span class="w">                    </span><span class="p">|</span><span class="w"> </span><span class="n">C3</span><span class="w"> </span><span class="n">c&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f2</span><span class="p">(</span><span class="n">b&#39;</span><span class="p">,</span><span class="n">c&#39;</span><span class="p">)</span><span class="w"></span>
<span class="w">                    </span><span class="p">|</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">Match</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">case</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">                  </span><span class="n">C1</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="n">f1</span><span class="w"> </span><span class="n">a_</span><span class="w"></span>
<span class="w">                </span><span class="p">|</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">Match</span><span class="p">))</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here you can see the necessity of abstracting out the ride hand sides
of the cases in order to avoid code duplication.  Right hand sides are
always abstracted.  The simplifier cleans things up.  You can also see
the new (primed) variables introduced by the match compiler and how
the renaming works.  Finally, you can see how the match compiler
introduces the necessary default clauses in order to make a match
exhaustive, i.e. cover all the cases.</p></div>
<div class="paragraph"><p>The match compiler uses <span class="monospaced">numCons</span> and <span class="monospaced">tyconCons</span> to determine
the exhaustivity of matches against constructors.</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>
