<!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>TipsForWritingConciseSML - 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%;">
      TipsForWritingConciseSML
    <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 = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
SML is a rich enough language that there are often several ways to express things.  This page contains miscellaneous tips (ideas not rules) for writing concise SML.  The metric that we are interested in here is the number of tokens or words (rather than the number of lines, for example). <h3 id="head-65c685e9ed20ec616092db6f1a6b1cd1c8ccf78e">Datatypes in Signatures</h3>
<p>
A seemingly frequent source of repetition in SML is that of datatype definitions in signatures and structures.  Actually, it isn't repetition at all.  A datatype specification in a signature, such as, 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> EXP = <B><FONT COLOR="#0000FF">sig</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> exp </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Fn</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">App</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exp * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Var</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>

        </ul>


<p>
is just a specification of a datatype that may be matched by multiple (albeit identical) datatype declarations.  For example, in 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> AnExp : EXP = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> exp </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Fn</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">App</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exp * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Var</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">structure</FONT></B> AnotherExp : EXP = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> exp </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Fn</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">App</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exp * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Var</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>

        </ul>


<p>
the types <tt>AnExp.exp</tt> and <tt>AnotherExp.exp</tt> are two distinct types. If such <a href="GenerativeDatatype">generativity</a> isn't desired or needed, you can avoid the repetition: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Exp = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> exp </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Fn</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">App</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exp * exp </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Var</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> id
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">signature</FONT></B> EXP = <B><FONT COLOR="#0000FF">sig</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> exp </FONT></B>=<B><FONT COLOR="#228B22"> <B><FONT COLOR="#A020F0">datatype</FONT></B> Exp.exp
</FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">structure</FONT></B> Exp : EXP = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Exp
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>

        </ul>


<p>
Keep in mind that this isn't semantically equivalent to the original. 
</p>
<h3 id="head-2105c3211851ea31a30912769b0532f49c4f1135">Clausal Function Definitions</h3>
<p>
The syntax of clausal function definitions is rather repetitive.  For example, 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> isSome NONE = false
  | isSome (SOME _) = true
</PRE>
<p>
 
</p>

        </ul>


<p>
is more verbose than 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> isSome =
 <B><FONT COLOR="#A020F0">fn</FONT></B> NONE =&gt; false
  | SOME _ =&gt; true
</PRE>
<p>
 
</p>

        </ul>


<p>
For recursive functions the break-even point is one clause higher.  For example, 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> fib <B><FONT COLOR="#5F9EA0">0</FONT></B> = <B><FONT COLOR="#5F9EA0">0</FONT></B>
  | fib <B><FONT COLOR="#5F9EA0">1</FONT></B> = <B><FONT COLOR="#5F9EA0">1</FONT></B>
  | fib n = fib (n-<B><FONT COLOR="#5F9EA0">1</FONT></B>) + fib (n-<B><FONT COLOR="#5F9EA0">2</FONT></B>)
</PRE>
<p>
 
</p>

        </ul>


<p>
isn't less verbose than 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#A020F0">rec</FONT></B> fib =
 <B><FONT COLOR="#A020F0">fn</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> =&gt; <B><FONT COLOR="#5F9EA0">0</FONT></B>
  | <B><FONT COLOR="#5F9EA0">1</FONT></B> =&gt; <B><FONT COLOR="#5F9EA0">1</FONT></B>
  | n =&gt; fib (n-<B><FONT COLOR="#5F9EA0">1</FONT></B>) + fib (n-<B><FONT COLOR="#5F9EA0">2</FONT></B>)
</PRE>
<p>
 
</p>

        </ul>


<p>
It is quite often the case that a curried function primarily examines just one of its arguments.  Such functions can be written particularly concisely by making the examined argument last.  For example, instead of 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> eval (Fn (v, b)) env =&gt; ...
  | eval (App (f, a) env =&gt; ...
  | eval (Var v) env =&gt; ...
</PRE>
<p>
 
</p>

        </ul>


<p>
consider writing 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> eval env =
 <B><FONT COLOR="#A020F0">fn</FONT></B> Fn (v, b) =&gt; ...
  | App (f, a) =&gt; ...
  | Var v =&gt; ...
</PRE>
<p>
 
</p>

        </ul>


<h3 id="head-f9627107a88876f4aaf6d7e0b0c19984c3a631ae">Parentheses</h3>
<p>
It is a good idea to avoid using lots of irritating superfluous parentheses.  An important rule to know is that prefix function application in SML has higher precedence than any infix operator.  For example, the outer parentheses in 
</p>

        <ul>

  
<pre class=code>
(square (<B><FONT COLOR="#5F9EA0">5</FONT></B> + <B><FONT COLOR="#5F9EA0">1</FONT></B>)) + (square (<B><FONT COLOR="#5F9EA0">5</FONT></B> * <B><FONT COLOR="#5F9EA0">2</FONT></B>))
</PRE>
<p>
 
</p>

        </ul>


<p>
are superfluous. 
</p>
<p>
People trained in other languages often use superfluous parentheses in a number of places.  In particular, the parentheses in the following examples are practically always superfluous and are best avoided: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> (condition) <B><FONT COLOR="#A020F0">then</FONT></B> ... <B><FONT COLOR="#A020F0">else</FONT></B> ...
<B><FONT COLOR="#A020F0">while</FONT></B> (condition) <B><FONT COLOR="#A020F0">do</FONT></B> ...
</PRE>
<p>
 
</p>

        </ul>


<p>
The same basically applies to case expressions: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> (expression) <B><FONT COLOR="#A020F0">of</FONT></B> ...
</PRE>
<p>
 
</p>

        </ul>


<p>
It is not uncommon to match a tuple of two or more values: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> (a, b) <B><FONT COLOR="#A020F0">of</FONT></B>
   (A1, B1) =&gt; ...
 | (A2, B2) =&gt; ...
</PRE>
<p>
 
</p>

        </ul>


<p>
Such case expressions can be written more concisely with an <a href="ProductType">infix product constructor</a>: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> a &amp; b <B><FONT COLOR="#A020F0">of</FONT></B>
   A1 &amp; B1 =&gt; ...
 | A2 &amp; B2 =&gt; ...
</PRE>
<p>
 
</p>

        </ul>


<h3 id="head-d6a56385d0729f6ff4bf27671cc845cb6f2cfde5">Conditionals</h3>
<p>
Repeated sequences of conditionals such as 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> x &lt; y <B><FONT COLOR="#A020F0">then</FONT></B> ...
<B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> x = y <B><FONT COLOR="#A020F0">then</FONT></B> ...
<B><FONT COLOR="#A020F0">else</FONT></B> ...
</PRE>
<p>
 
</p>

        </ul>


<p>
can often be written more concisely as case expressions such as 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> Int.compare (x, y) <B><FONT COLOR="#A020F0">of</FONT></B>
   LESS =&gt; ...
 | EQUAL =&gt; ...
 | GREATER =&gt; ...
</PRE>
<p>
 
</p>

        </ul>


<p>
For a custom comparison, you would then define an appropriate datatype and a reification function.  An alternative to using datatypes is to use dispatch functions 
</p>

        <ul>

  
<pre class=code>
comparing (x, y)
{lt = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; ...,
 eq = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; ...,
 gt = <B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; ...}
</PRE>
<p>
 
</p>

        </ul>


<p>
where 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> comparing (x, y) {lt, eq, gt} =
    (<B><FONT COLOR="#A020F0">case</FONT></B> Int.compare (x, y) <B><FONT COLOR="#A020F0">of</FONT></B>
        LESS =&gt; lt
      | EQUAL =&gt; eq
      | GREATER =&gt; gt) ()
</PRE>
<p>
 
</p>

        </ul>


<p>
An advantage is that no datatype definition is needed.  A disadvantage is that you can't combine multiple dispatch results easily. 
</p>
<h3 id="head-926be1ebb441febcf6d968cc7f35fbf513798f31">Command-Query Fusion</h3>
<p>
Many are familiar with the <a class="external" href="http://en.wikipedia.org/wiki/Command-Query_Separation"><img src="moin-www.png" alt="[WWW]" height="11" width="11">Command-Query Separation Principle</a>. Adhering to the principle, a signature for an imperative stack might contain specifications 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> isEmpty : 'a t -&gt; bool
<B><FONT COLOR="#A020F0">val</FONT></B> pop : 'a t -&gt; 'a
</PRE>
<p>
 
</p>

        </ul>


<p>
and use of a stack would look like 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> isEmpty stack
<B><FONT COLOR="#A020F0">then</FONT></B> ... pop stack ...
<B><FONT COLOR="#A020F0">else</FONT></B> ...
</PRE>
<p>
 
</p>

        </ul>


<p>
or, when the element needs to be named, 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> isEmpty stack
<B><FONT COLOR="#A020F0">then</FONT></B> <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#A020F0">val</FONT></B> elem = pop stack <B><FONT COLOR="#A020F0">in</FONT></B> ... <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">else</FONT></B> ...
</PRE>
<p>
 
</p>

        </ul>


<p>
For efficiency, correctness, and conciseness, it is often better to combine the query and command and return the result as an option: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> pop : 'a t -&gt; 'a option
</PRE>
<p>
 
</p>

        </ul>


<p>
A use of a stack would then look like this: 
</p>

        <ul>

  
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> pop stack <B><FONT COLOR="#A020F0">of</FONT></B>
   NONE =&gt; ...
 | SOME elem =&gt; ...
</PRE>
<p>
 
</p>
</ul>

</div>



<p>
<hr>
Last edited on 2007-02-12 07:34:53 by <span title="cs181143070.pp.htv.fi"><a href="VesaKarvonen">VesaKarvonen</a></span>.
</body></html>
