| 1 |
= TipsForWritingConciseSML |
| 2 |
|
| 3 |
SML is a rich enough language that there are often several ways to |
| 4 |
express things. This page contains miscellaneous tips (ideas not |
| 5 |
rules) for writing concise SML. The metric that we are interested in |
| 6 |
here is the number of tokens or words (rather than the number of |
| 7 |
lines, for example). |
| 8 |
|
| 9 |
== Datatypes in Signatures |
| 10 |
|
| 11 |
A seemingly frequent source of repetition in SML is that of datatype |
| 12 |
definitions in signatures and structures. Actually, it isn't |
| 13 |
repetition at all. A datatype specification in a signature, such as, |
| 14 |
|
| 15 |
[source,sml] |
| 16 |
---- |
| 17 |
signature EXP = sig |
| 18 |
datatype exp = Fn of id * exp | App of exp * exp | Var of id |
| 19 |
end |
| 20 |
---- |
| 21 |
|
| 22 |
is just a specification of a datatype that may be matched by multiple |
| 23 |
(albeit identical) datatype declarations. For example, in |
| 24 |
|
| 25 |
[source,sml] |
| 26 |
---- |
| 27 |
structure AnExp : EXP = struct |
| 28 |
datatype exp = Fn of id * exp | App of exp * exp | Var of id |
| 29 |
end |
| 30 |
|
| 31 |
structure AnotherExp : EXP = struct |
| 32 |
datatype exp = Fn of id * exp | App of exp * exp | Var of id |
| 33 |
end |
| 34 |
---- |
| 35 |
|
| 36 |
the types `AnExp.exp` and `AnotherExp.exp` are two distinct types. If |
| 37 |
such <<GenerativeDatatype#,generativity>> isn't desired or needed, you |
| 38 |
can avoid the repetition: |
| 39 |
|
| 40 |
[source,sml] |
| 41 |
---- |
| 42 |
structure Exp = struct |
| 43 |
datatype exp = Fn of id * exp | App of exp * exp | Var of id |
| 44 |
end |
| 45 |
|
| 46 |
signature EXP = sig |
| 47 |
datatype exp = datatype Exp.exp |
| 48 |
end |
| 49 |
|
| 50 |
structure Exp : EXP = struct |
| 51 |
open Exp |
| 52 |
end |
| 53 |
---- |
| 54 |
|
| 55 |
Keep in mind that this isn't semantically equivalent to the original. |
| 56 |
|
| 57 |
|
| 58 |
== Clausal Function Definitions |
| 59 |
|
| 60 |
The syntax of clausal function definitions is rather repetitive. For |
| 61 |
example, |
| 62 |
|
| 63 |
[source,sml] |
| 64 |
---- |
| 65 |
fun isSome NONE = false |
| 66 |
| isSome (SOME _) = true |
| 67 |
---- |
| 68 |
|
| 69 |
is more verbose than |
| 70 |
|
| 71 |
[source,sml] |
| 72 |
---- |
| 73 |
val isSome = |
| 74 |
fn NONE => false |
| 75 |
| SOME _ => true |
| 76 |
---- |
| 77 |
|
| 78 |
For recursive functions the break-even point is one clause higher. For example, |
| 79 |
|
| 80 |
[source,sml] |
| 81 |
---- |
| 82 |
fun fib 0 = 0 |
| 83 |
| fib 1 = 1 |
| 84 |
| fib n = fib (n-1) + fib (n-2) |
| 85 |
---- |
| 86 |
|
| 87 |
isn't less verbose than |
| 88 |
|
| 89 |
[source,sml] |
| 90 |
---- |
| 91 |
val rec fib = |
| 92 |
fn 0 => 0 |
| 93 |
| 1 => 1 |
| 94 |
| n => fib (n-1) + fib (n-2) |
| 95 |
---- |
| 96 |
|
| 97 |
It is quite often the case that a curried function primarily examines |
| 98 |
just one of its arguments. Such functions can be written particularly |
| 99 |
concisely by making the examined argument last. For example, instead |
| 100 |
of |
| 101 |
|
| 102 |
[source,sml] |
| 103 |
---- |
| 104 |
fun eval (Fn (v, b)) env => ... |
| 105 |
| eval (App (f, a) env => ... |
| 106 |
| eval (Var v) env => ... |
| 107 |
---- |
| 108 |
|
| 109 |
consider writing |
| 110 |
|
| 111 |
[source,sml] |
| 112 |
---- |
| 113 |
fun eval env = |
| 114 |
fn Fn (v, b) => ... |
| 115 |
| App (f, a) => ... |
| 116 |
| Var v => ... |
| 117 |
---- |
| 118 |
|
| 119 |
|
| 120 |
== Parentheses |
| 121 |
|
| 122 |
It is a good idea to avoid using lots of irritating superfluous |
| 123 |
parentheses. An important rule to know is that prefix function |
| 124 |
application in SML has higher precedence than any infix operator. For |
| 125 |
example, the outer parentheses in |
| 126 |
|
| 127 |
[source,sml] |
| 128 |
---- |
| 129 |
(square (5 + 1)) + (square (5 * 2)) |
| 130 |
---- |
| 131 |
|
| 132 |
are superfluous. |
| 133 |
|
| 134 |
People trained in other languages often use superfluous parentheses in |
| 135 |
a number of places. In particular, the parentheses in the following |
| 136 |
examples are practically always superfluous and are best avoided: |
| 137 |
|
| 138 |
[source,sml] |
| 139 |
---- |
| 140 |
if (condition) then ... else ... |
| 141 |
while (condition) do ... |
| 142 |
---- |
| 143 |
|
| 144 |
The same basically applies to case expressions: |
| 145 |
|
| 146 |
[source,sml] |
| 147 |
---- |
| 148 |
case (expression) of ... |
| 149 |
---- |
| 150 |
|
| 151 |
It is not uncommon to match a tuple of two or more values: |
| 152 |
|
| 153 |
[source,sml] |
| 154 |
---- |
| 155 |
case (a, b) of |
| 156 |
(A1, B1) => ... |
| 157 |
| (A2, B2) => ... |
| 158 |
---- |
| 159 |
|
| 160 |
Such case expressions can be written more concisely with an |
| 161 |
<<ProductType#,infix product constructor>>: |
| 162 |
|
| 163 |
[source,sml] |
| 164 |
---- |
| 165 |
case a & b of |
| 166 |
A1 & B1 => ... |
| 167 |
| A2 & B2 => ... |
| 168 |
---- |
| 169 |
|
| 170 |
|
| 171 |
== Conditionals |
| 172 |
|
| 173 |
Repeated sequences of conditionals such as |
| 174 |
|
| 175 |
[source,sml] |
| 176 |
---- |
| 177 |
if x < y then ... |
| 178 |
else if x = y then ... |
| 179 |
else ... |
| 180 |
---- |
| 181 |
|
| 182 |
can often be written more concisely as case expressions such as |
| 183 |
|
| 184 |
[source,sml] |
| 185 |
---- |
| 186 |
case Int.compare (x, y) of |
| 187 |
LESS => ... |
| 188 |
| EQUAL => ... |
| 189 |
| GREATER => ... |
| 190 |
---- |
| 191 |
|
| 192 |
For a custom comparison, you would then define an appropriate datatype |
| 193 |
and a reification function. An alternative to using datatypes is to |
| 194 |
use dispatch functions |
| 195 |
|
| 196 |
[source,sml] |
| 197 |
---- |
| 198 |
comparing (x, y) |
| 199 |
{lt = fn () => ..., |
| 200 |
eq = fn () => ..., |
| 201 |
gt = fn () => ...} |
| 202 |
---- |
| 203 |
|
| 204 |
where |
| 205 |
|
| 206 |
[source,sml] |
| 207 |
---- |
| 208 |
fun comparing (x, y) {lt, eq, gt} = |
| 209 |
(case Int.compare (x, y) of |
| 210 |
LESS => lt |
| 211 |
| EQUAL => eq |
| 212 |
| GREATER => gt) () |
| 213 |
---- |
| 214 |
|
| 215 |
An advantage is that no datatype definition is needed. A disadvantage |
| 216 |
is that you can't combine multiple dispatch results easily. |
| 217 |
|
| 218 |
|
| 219 |
== Command-Query Fusion |
| 220 |
|
| 221 |
Many are familiar with the |
| 222 |
http://en.wikipedia.org/wiki/Command-Query_Separation[Command-Query |
| 223 |
Separation Principle]. Adhering to the principle, a signature for an |
| 224 |
imperative stack might contain specifications |
| 225 |
|
| 226 |
[source,sml] |
| 227 |
---- |
| 228 |
val isEmpty : 'a t -> bool |
| 229 |
val pop : 'a t -> 'a |
| 230 |
---- |
| 231 |
|
| 232 |
and use of a stack would look like |
| 233 |
|
| 234 |
[source,sml] |
| 235 |
---- |
| 236 |
if isEmpty stack |
| 237 |
then ... pop stack ... |
| 238 |
else ... |
| 239 |
---- |
| 240 |
|
| 241 |
or, when the element needs to be named, |
| 242 |
|
| 243 |
[source,sml] |
| 244 |
---- |
| 245 |
if isEmpty stack |
| 246 |
then let val elem = pop stack in ... end |
| 247 |
else ... |
| 248 |
---- |
| 249 |
|
| 250 |
For efficiency, correctness, and conciseness, it is often better to |
| 251 |
combine the query and command and return the result as an option: |
| 252 |
|
| 253 |
[source,sml] |
| 254 |
---- |
| 255 |
val pop : 'a t -> 'a option |
| 256 |
---- |
| 257 |
|
| 258 |
A use of a stack would then look like this: |
| 259 |
|
| 260 |
[source,sml] |
| 261 |
---- |
| 262 |
case pop stack of |
| 263 |
NONE => ... |
| 264 |
| SOME elem => ... |
| 265 |
---- |