<!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>ProductType</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>ProductType</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="StandardML">Standard ML</a> has special syntax for products (tuples). A
product type is written as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">t1</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">t2</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">tN</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>and a product pattern is written as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">(</span><span class="n">p1</span><span class="p">,</span><span class="w"> </span><span class="n">p2</span><span class="p">,</span><span class="w"> </span><span class="p">...,</span><span class="w"> </span><span class="n">pN</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>In most situations the syntax is quite convenient.  However, there are
situations where the syntax is cumbersome.  There are also situations
in which it is useful to construct and destruct n-ary products
inductively, especially when using <a href="Fold">Fold</a>.</p></div>
<div class="paragraph"><p>In such situations, it is useful to have a binary product datatype
with an infix constructor defined as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">datatype</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;b</span><span class="p">)</span><span class="w"> </span><span class="n">product</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"></span>
<span class="k">infix</span><span class="w"> </span><span class="n">&amp;</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With these definitions, one can write an n-ary product as a nested
binary product quite conveniently.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">x1</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">xn</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Because of left associativity, this is the same as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">(((</span><span class="n">x1</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">x2</span><span class="p">)</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="p">...)</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">xn</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Because <span class="monospaced">&amp;</span> is a constructor, the syntax can also be used for
patterns.</p></div>
<div class="paragraph"><p>The symbol <span class="monospaced">&amp;</span> is inspired by the Curry-Howard isomorphism: the proof
of a conjunction <span class="monospaced">(A &amp; B)</span> is a pair of proofs <span class="monospaced">(a, b)</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_parser_combinators">Example: parser combinators</h2>
<div class="sectionbody">
<div class="paragraph"><p>A typical parser combinator library provides a combinator that has a
type of the form.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">&#39;a</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">parser</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>and produces a parser for the concatenation of two parsers. When more
than two parsers are concatenated, the result of the resulting parser
is a nested structure of pairs</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">(...((</span><span class="n">p1</span><span class="p">,</span><span class="w"> </span><span class="n">p2</span><span class="p">),</span><span class="w"> </span><span class="n">p3</span><span class="p">)...,</span><span class="w"> </span><span class="n">pN</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>which is somewhat cumbersome.</p></div>
<div class="paragraph"><p>By using a product type, the type of the concatenation combinator then
becomes</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">&#39;a</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">parser</span><span class="w"> </span><span class="p">-&gt;</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;b</span><span class="p">)</span><span class="w"> </span><span class="n">product</span><span class="w"> </span><span class="n">parser</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>While this doesn&#8217;t stop the nesting, it makes the pattern significantly
easier to write. Instead of</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">(...((</span><span class="n">p1</span><span class="p">,</span><span class="w"> </span><span class="n">p2</span><span class="p">),</span><span class="w"> </span><span class="n">p3</span><span class="p">)...,</span><span class="w"> </span><span class="n">pN</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>the pattern is written as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">p1</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">p2</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">p3</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">pN</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>which is considerably more concise.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="VariableArityPolymorphism">VariableArityPolymorphism</a>
</p>
</li>
<li>
<p>
<a href="Utilities">Utilities</a>
</p>
</li>
</ul></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>
