<!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>SyntacticConventions - 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%;">
      SyntacticConventions
    <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">
Here are a number of syntactic conventions useful for programming in SML. <p>
<ol>
<li>
<a href="#head-9239ee2cda84eca4c3440e2a7b50148af67da3d4">General</a></li>
<li>
<a href="#head-649d2864df18704de32e689e69e63f2483e8b76e">Identifiers</a></li>
<li>
<a href="#head-93b9e289e2842469d001eccf7ad5d79f3c302dc9">Types</a></li>
<li>
<a href="#head-68836c550ee20fae0e06b2994e76a40348d2fc30">Core</a></li>
<li>
<a href="#head-e4cf0392296064579858651f856889aadbc2805e">Signatures</a></li>
<li>
<a href="#head-c64b6d8ab6a3167af6b211df05198e555a8951eb">Structures</a></li>
<li>
<a href="#head-23ac4cb38bc668a9233ce693377f7f5c3879f104">Functors</a></li>
</ol>

 
</p>
<h2 id="head-9239ee2cda84eca4c3440e2a7b50148af67da3d4">General</h2>

    <ul>

    <li>
<p>
 A line of code never exceeds 80 columns. 
</p>
</li>
    <li class="gap">
<p>
 Only split a syntactic entity across multiple lines if it doesn't  fit on one line within 80 columns. 
</p>
</li>
    <li class="gap">
<p>
 Use alphabetical order wherever possible. 
</p>
</li>
    <li class="gap">
<p>
 Avoid redundant parentheses. 
</p>
</li>
    <li class="gap">
<p>
 When using <tt>:</tt>, there is no space before the colon, and a  single space after it. 
</p>
</li>

    </ul>


<h2 id="head-649d2864df18704de32e689e69e63f2483e8b76e">Identifiers</h2>

    <ul>

    <li>
<p>
 Variables, record labels and type constructors begin with and use  small letters, using capital letters to separate words. 
<pre>cost
maxValue</pre> 
</p>
</li>
    <li class="gap">
<p>
 Variables that represent collections of objects (lists, arrays,  vectors, ...) are often suffixed with an <tt>s</tt>.  
<pre>xs
employees</pre>
</p>
</li>
    <li class="gap">
<p>
 Constructors, structure identifiers, and functor identifiers begin  with a capital letter.  
<pre>Queue
LinkedList</pre>
</p>
</li>
    <li class="gap">
<p>
 Signature identifiers are in all capitals, using <tt>_</tt> to  separate words.  
<pre>LIST
BINARY_HEAP</pre>
</p>
</li>

    </ul>


<h2 id="head-93b9e289e2842469d001eccf7ad5d79f3c302dc9">Types</h2>

    <ul>

    <li>
<p>
 Alphabetize record labels.  In a record type, there are spaces  after colons and commas, but not before colons or commas, or at the  delimiters <tt>{</tt> and <tt>}&nbsp;</tt>.  
<pre>{bar: int, foo: int}
</pre>
</p>
</li>
    <li class="gap">
<p>
 Only split a record type across multiple lines if it doesn't fit on  one line. If a record type must be split over multiple lines, put one  field per line.  
<pre>{bar: int,
 foo: real * real, 
 zoo: bool} </pre>
</p>
</li>
    <li class="gap">
<p>
 In a tuple type, there are spaces before and after each <tt>*</tt>.  
<pre>int * bool * real
</pre>
</p>
</li>
    <li class="gap">
<p>
 Only split a tuple type across multiple lines if it doesn't fit on  one line.  In a tuple type split over multiple lines, there is one  type per line, and the <tt>*</tt>s go at the beginning of the lines.  
<pre>int
* bool
* real
</pre> It may also be useful to parenthesize to make the grouping more  apparent.   
<pre>(int
 * bool
 * real)
</pre>
</p>
</li>
    <li class="gap">
<p>
 In an arrow type split over multiple lines, put the arrow at the  beginning of its line.   
<pre>int * real
-&gt; bool
</pre> It may also be useful to parenthesize to make the grouping more  apparent.  
<pre>(int * real
 -&gt; bool)
</pre>
</p>
</li>
    <li class="gap">
<p>
 Avoid redundant parentheses. 
</p>
</li>

        <ul>

        <li>
<p>
 Arrow types associate to the right, so write 
<pre>a -&gt; b -&gt; c</pre> not 
<pre>a -&gt; (b -&gt; c)</pre>
</p>
</li>
        <li class="gap">
<p>
 Type constructor application associates to the left, so write   
<pre>int ref list</pre> not 
<pre>(int ref) list</pre>
</p>
</li>
        <li class="gap">
<p>
 Type constructor application binds more tightly than a tuple type,   so write 
<pre>int list * bool list</pre> not 
<pre>(int list) * (bool list)</pre>
</p>
</li>
        <li class="gap">
<p>
 Tuple types bind more tightly than arrow types, so write 
<pre>int * bool -&gt; real
</pre> not 
<pre>(int * bool) -&gt; real</pre>
</p>
</li>

        </ul>



    </ul>


<h2 id="head-68836c550ee20fae0e06b2994e76a40348d2fc30">Core</h2>

    <ul>

    <li>
<p>
 A core expression or declaration split over multiple lines does not  contain any blank lines. 
</p>
</li>
    <li class="gap">
<p>
 A record field selector has no space between the  <tt>#</tt> and the record label.  So, write 
<pre class=code>
#foo
</PRE>
 not 
<pre class=code>
# foo
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A tuple has a space after each comma, but not before, and not at  the delimiters <tt>(</tt> <tt>)</tt>.  
<pre class=code>
(e1, e2, e3)
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A tuple split over multiple lines has one element per line, and the  commas go at the end of the lines.  
<pre class=code>
(e1,
 e2,
 e3)
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A list has a space after each comma, but not before, and not at the  delimiters <tt>[</tt> <tt>]</tt>. 
<pre class=code>
[e1, e2, e3]
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A list split over multiple lines has one element per line, and the  commas at the end of the lines. 
<pre class=code>
[e1,
 e2,
 e3]
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A record has spaces before and after <tt>=</tt>, a space after each  comma, and no space at the delimiters.  Field names appear in  alphabetical order.  
<pre class=code>
{bar = <B><FONT COLOR="#5F9EA0">13</FONT></B>, foo = true} 
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A sequence expression has a space after each semicolon, but not  before.  
<pre class=code>
(e1; e2; e3)
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A sequence expression split over multiple lines has one expression  per line, and the semicolons at the beginning of lines.  Lisp and  Scheme programmers may find this hard to read at first.  
<pre class=code>
(e1
 ; e2
 ; e3)
</PRE>
  <em>Rationale</em>: this makes it easy to visually spot the beginning of  each expression, which becomes more valuable as the expressions  themselves are split across multiple lines. 
</p>
</li>
    <li class="gap">
<p>
 An application expression has a space between the function and the  argument.  There are no parens unless the argument is a tuple (in  which case the parens are really part of the tuple, not the  application).  
<pre class=code>
f a
f (a1, a2, a3)
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 Avoid redundant parentheses.  Application associates to left, so  write 
<pre class=code>
f a1 a2 a3
</PRE>
 not 
<pre class=code>
((f a1) a2) a3
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 Infix operators have a space before and after the operator. 
<pre class=code>
x + y
x * y - z
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 Avoid redundant parentheses.  Use <a href="OperatorPrecedence">OperatorPrecedence</a>.  So, write 
<pre class=code>
x + y * z
</PRE>
 not 
<pre class=code>
x + (y * z)
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 An <tt>andalso</tt> expression split over multiple lines has the  <tt>andalso</tt> at the beginning of subsequent lines. 
<pre class=code>
e1
<B><FONT COLOR="#A020F0">andalso</FONT></B> e2
<B><FONT COLOR="#A020F0">andalso</FONT></B> e3
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>case</tt> expression is indented as follows 
<pre class=code>
<B><FONT COLOR="#A020F0">case</FONT></B> e1 <B><FONT COLOR="#A020F0">of</FONT></B>
   p1 =&gt; e1
 | p2 =&gt; e2
 | p3 =&gt; e3
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>datatype</tt>'s constructors are alphabetized.  
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">A</FONT> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B</FONT> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">C</FONT>
</FONT></B></PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>datatype</tt> declaration has a space before and after each  <tt>|</tt>. 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">A</FONT> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">C</FONT>
</FONT></B></PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>datatype</tt> split over multiple lines has one constructor per  line, with the <tt>|</tt> at the beginning of lines and the constructors  beginning 3 columns to the right of the <tt>datatype</tt>. 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> 
   <FONT COLOR="#B8860B">A</FONT> 
 </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">B</FONT> 
 </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">C</FONT>
</FONT></B></PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>fun</tt> declaration may start its body on the subsequent line,  indented 3 spaces. 
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> f x y =
   <B><FONT COLOR="#A020F0">let</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> z = x + y + z
   <B><FONT COLOR="#A020F0">in</FONT></B>
      z
   <B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 An <tt>if</tt> expression is indented as follows. 
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> e1
   <B><FONT COLOR="#A020F0">then</FONT></B> e2
<B><FONT COLOR="#A020F0">else</FONT></B> e3
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A sequence of <tt>if</tt>-<tt>then</tt>-<tt>else</tt>s is indented as  follows. 
<pre class=code>
<B><FONT COLOR="#A020F0">if</FONT></B> e1
   <B><FONT COLOR="#A020F0">then</FONT></B> e2
<B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> e3
   <B><FONT COLOR="#A020F0">then</FONT></B> e4
<B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> e5
   <B><FONT COLOR="#A020F0">then</FONT></B> e6
<B><FONT COLOR="#A020F0">else</FONT></B> e7
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>let</tt> expression has the <tt>let</tt>, <tt>in</tt>, and <tt>end</tt>  on their own lines, starting in the same column.  Declarations and the  body are indented 3 spaces.  
<pre class=code>
<B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#5F9EA0">13</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> y = <B><FONT COLOR="#5F9EA0">14</FONT></B>
<B><FONT COLOR="#A020F0">in</FONT></B>
   x + y
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>local</tt> declaration has the <tt>local</tt>, <tt>in</tt>, and  <tt>end</tt> on their own lines, starting in the same column.  Declarations are indented 3 spaces. 
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#5F9EA0">13</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> y = x
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 An <tt>orelse</tt> expression split over multiple lines has the  <tt>orelse</tt> at the beginning of subsequent lines. 
<pre class=code>
e1
<B><FONT COLOR="#A020F0">orelse</FONT></B> e2
<B><FONT COLOR="#A020F0">orelse</FONT></B> e3
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>val</tt> declaration has a space before and after the  <tt>=</tt>. 
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> p = e
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>val</tt> declaration can start the expression on the subsequent  line, indented 3 spaces. 
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> p =
   <B><FONT COLOR="#A020F0">if</FONT></B> e1 <B><FONT COLOR="#A020F0">then</FONT></B> e2 <B><FONT COLOR="#A020F0">else</FONT></B> e3
</PRE>
 
</p>
</li>

    </ul>


<h2 id="head-e4cf0392296064579858651f856889aadbc2805e">Signatures</h2>

    <ul>

    <li>
<p>
 A <tt>signature</tt> declaration is indented as follows. 
<pre class=code>
<B><FONT COLOR="#0000FF">signature</FONT></B> FOO =
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> x: int
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>val</tt> specification has a space after the colon, but not  before.  
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> x: int
</PRE>
  <em>Exception</em>: in the case of operators (like <tt>+</tt>), there is a  space before the colon to avoid lexing the colon as part of the  operator. 
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> + : t * t -&gt; t
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 Alphabetize specifications in signatures.  
<pre class=code>
<B><FONT COLOR="#0000FF">sig</FONT></B> 
   <B><FONT COLOR="#A020F0">val</FONT></B> x: int 
   <B><FONT COLOR="#A020F0">val</FONT></B> y: bool 
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>

    </ul>


<h2 id="head-c64b6d8ab6a3167af6b211df05198e555a8951eb">Structures</h2>

    <ul>

    <li>
<p>
 A <tt>structure</tt> declaration has a space on both sides of the  <tt>=</tt>. 
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Foo = Bar
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 A <tt>structure</tt> declaration split over multiple lines is indented  as follows. 
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> S =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#5F9EA0">13</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 Declarations in a <tt>struct</tt> are separated by blank lines.  
<pre class=code>
<B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> x =
      <B><FONT COLOR="#A020F0">let</FONT></B>
         y = <B><FONT COLOR="#5F9EA0">13</FONT></B>
      <B><FONT COLOR="#A020F0">in</FONT></B> 
         y + <B><FONT COLOR="#5F9EA0">1</FONT></B>
      <B><FONT COLOR="#A020F0">end</FONT></B>

   <B><FONT COLOR="#A020F0">val</FONT></B> z = <B><FONT COLOR="#5F9EA0">14</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>

    </ul>


<h2 id="head-23ac4cb38bc668a9233ce693377f7f5c3879f104">Functors</h2>

    <ul>

    <li>
<p>
 A <tt>functor</tt> declaration has spaces after each <tt>:</tt> (or  <tt>:&gt;</tt>) but not before, and a space before and after the  <tt>=</tt>.  It is indented as follows 
<pre class=code>
<B><FONT COLOR="#0000FF">functor</FONT></B> Foo (S: FOO_ARG): FOO =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
       <B><FONT COLOR="#A020F0">val</FONT></B> x = S.x
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
  <em>Exception</em>: a functor declaration in a file to itself can omit  the indentation to save horizontal space. 
<pre class=code>
<B><FONT COLOR="#0000FF">functor</FONT></B> Foo (S: FOO_ARG): FOO =
<B><FONT COLOR="#0000FF">struct</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> x = S.x

<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
    In this case, there should be a blank line after the <tt>struct</tt>  and before the <tt>end</tt>. 
</p>
</li>
</ul>

</div>



<p>
<hr>
Last edited on 2006-08-24 23:26:33 by <span title="raceme.rsise.anu.edu.au"><a href="MichaelNorrish">MichaelNorrish</a></span>.
</body></html>
