File: TipsForWritingConciseSML

package info (click to toggle)
mlton 20100608-5.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,628 kB
  • ctags: 70,047
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (523 lines) | stat: -rw-r--r-- 13,421 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!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 = "TitleIndex">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>