<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>TipsForWritingConciseSML</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</div>
</div>
<div id="header">
<h1>TipsForWritingConciseSML</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>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).</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_datatypes_in_signatures">Datatypes in Signatures</h2>
<div class="sectionbody">
<div class="paragraph">
<p>A seemingly frequent source of repetition in SML is that of datatype
definitions in signatures and structures.  Actually, it isn&#8217;t
repetition at all.  A datatype specification in a signature, such as,</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">signature EXP = sig
   datatype exp = Fn of id * exp | App of exp * exp | Var of id
end</code></pre>
</div>
</div>
<div class="paragraph">
<p>is just a specification of a datatype that may be matched by multiple
(albeit identical) datatype declarations.  For example, in</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">structure AnExp : EXP = struct
   datatype exp = Fn of id * exp | App of exp * exp | Var of id
end

structure AnotherExp : EXP = struct
   datatype exp = Fn of id * exp | App of exp * exp | Var of id
end</code></pre>
</div>
</div>
<div class="paragraph">
<p>the types <code>AnExp.exp</code> and <code>AnotherExp.exp</code> are two distinct types.  If
such <a href="GenerativeDatatype">generativity</a> isn&#8217;t desired or needed, you
can avoid the repetition:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">structure Exp = struct
   datatype exp = Fn of id * exp | App of exp * exp | Var of id
end

signature EXP = sig
   datatype exp = datatype Exp.exp
end

structure Exp : EXP = struct
   open Exp
end</code></pre>
</div>
</div>
<div class="paragraph">
<p>Keep in mind that this isn&#8217;t semantically equivalent to the original.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_clausal_function_definitions">Clausal Function Definitions</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The syntax of clausal function definitions is rather repetitive.  For
example,</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">fun isSome NONE = false
  | isSome (SOME _) = true</code></pre>
</div>
</div>
<div class="paragraph">
<p>is more verbose than</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">val isSome =
 fn NONE =&gt; false
  | SOME _ =&gt; true</code></pre>
</div>
</div>
<div class="paragraph">
<p>For recursive functions the break-even point is one clause higher.  For example,</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">fun fib 0 = 0
  | fib 1 = 1
  | fib n = fib (n-1) + fib (n-2)</code></pre>
</div>
</div>
<div class="paragraph">
<p>isn&#8217;t less verbose than</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">val rec fib =
 fn 0 =&gt; 0
  | 1 =&gt; 1
  | n =&gt; fib (n-1) + fib (n-2)</code></pre>
</div>
</div>
<div class="paragraph">
<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>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">fun eval (Fn (v, b)) env =&gt; ...
  | eval (App (f, a) env =&gt; ...
  | eval (Var v) env =&gt; ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>consider writing</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">fun eval env =
 fn Fn (v, b) =&gt; ...
  | App (f, a) =&gt; ...
  | Var v =&gt; ...</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_parentheses">Parentheses</h2>
<div class="sectionbody">
<div class="paragraph">
<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>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">(square (5 + 1)) + (square (5 * 2))</code></pre>
</div>
</div>
<div class="paragraph">
<p>are superfluous.</p>
</div>
<div class="paragraph">
<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>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">if (condition) then ... else ...
while (condition) do ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>The same basically applies to case expressions:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">case (expression) of ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>It is not uncommon to match a tuple of two or more values:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">case (a, b) of
   (A1, B1) =&gt; ...
 | (A2, B2) =&gt; ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>Such case expressions can be written more concisely with an
<a href="ProductType">infix product constructor</a>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">case a &amp; b of
   A1 &amp; B1 =&gt; ...
 | A2 &amp; B2 =&gt; ...</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_conditionals">Conditionals</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Repeated sequences of conditionals such as</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">if x &lt; y then ...
else if x = y then ...
else ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>can often be written more concisely as case expressions such as</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">case Int.compare (x, y) of
   LESS =&gt; ...
 | EQUAL =&gt; ...
 | GREATER =&gt; ...</code></pre>
</div>
</div>
<div class="paragraph">
<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>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">comparing (x, y)
{lt = fn () =&gt; ...,
 eq = fn () =&gt; ...,
 gt = fn () =&gt; ...}</code></pre>
</div>
</div>
<div class="paragraph">
<p>where</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">fun comparing (x, y) {lt, eq, gt} =
    (case Int.compare (x, y) of
        LESS =&gt; lt
      | EQUAL =&gt; eq
      | GREATER =&gt; gt) ()</code></pre>
</div>
</div>
<div class="paragraph">
<p>An advantage is that no datatype definition is needed.  A disadvantage
is that you can&#8217;t combine multiple dispatch results easily.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_command_query_fusion">Command-Query Fusion</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Many are familiar with the
<a href="http://en.wikipedia.org/wiki/Command-Query_Separation">Command-Query
Separation Principle</a>.  Adhering to the principle, a signature for an
imperative stack might contain specifications</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">val isEmpty : 'a t -&gt; bool
val pop : 'a t -&gt; 'a</code></pre>
</div>
</div>
<div class="paragraph">
<p>and use of a stack would look like</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">if isEmpty stack
then ... pop stack ...
else ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>or, when the element needs to be named,</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">if isEmpty stack
then let val elem = pop stack in ... end
else ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>For efficiency, correctness, and conciseness, it is often better to
combine the query and command and return the result as an option:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">val pop : 'a t -&gt; 'a option</code></pre>
</div>
</div>
<div class="paragraph">
<p>A use of a stack would then look like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="sml">case pop stack of
   NONE =&gt; ...
 | SOME elem =&gt; ...</code></pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>