| 1 |
= StandardMLGotchas |
| 2 |
:toc: |
| 3 |
|
| 4 |
This page contains brief explanations of some recurring sources of |
| 5 |
confusion and problems that SML newbies encounter. |
| 6 |
|
| 7 |
Many confusions about the syntax of SML seem to arise from the use of |
| 8 |
an interactive REPL (Read-Eval Print Loop) while trying to learn the |
| 9 |
basics of the language. While writing your first SML programs, you |
| 10 |
should keep the source code of your programs in a form that is |
| 11 |
accepted by an SML compiler as a whole. |
| 12 |
|
| 13 |
== The `and` keyword |
| 14 |
|
| 15 |
It is a common mistake to misuse the `and` keyword or to not know how |
| 16 |
to introduce mutually recursive definitions. The purpose of the `and` |
| 17 |
keyword is to introduce mutually recursive definitions of functions |
| 18 |
and datatypes. For example, |
| 19 |
|
| 20 |
[source,sml] |
| 21 |
---- |
| 22 |
fun isEven 0w0 = true |
| 23 |
| isEven 0w1 = false |
| 24 |
| isEven n = isOdd (n-0w1) |
| 25 |
and isOdd 0w0 = false |
| 26 |
| isOdd 0w1 = true |
| 27 |
| isOdd n = isEven (n-0w1) |
| 28 |
---- |
| 29 |
|
| 30 |
and |
| 31 |
|
| 32 |
[source,sml] |
| 33 |
---- |
| 34 |
datatype decl = VAL of id * pat * expr |
| 35 |
(* | ... *) |
| 36 |
and expr = LET of decl * expr |
| 37 |
(* | ... *) |
| 38 |
---- |
| 39 |
|
| 40 |
You can also use `and` as a shorthand in a couple of other places, but |
| 41 |
it is not necessary. |
| 42 |
|
| 43 |
== Constructed patterns |
| 44 |
|
| 45 |
It is a common mistake to forget to parenthesize constructed patterns |
| 46 |
in `fun` bindings. Consider the following invalid definition: |
| 47 |
|
| 48 |
[source,sml] |
| 49 |
---- |
| 50 |
fun length nil = 0 |
| 51 |
| length h :: t = 1 + length t |
| 52 |
---- |
| 53 |
|
| 54 |
The pattern `h :: t` needs to be parenthesized: |
| 55 |
|
| 56 |
[source,sml] |
| 57 |
---- |
| 58 |
fun length nil = 0 |
| 59 |
| length (h :: t) = 1 + length t |
| 60 |
---- |
| 61 |
|
| 62 |
The parentheses are needed, because a `fun` definition may have |
| 63 |
multiple consecutive constructed patterns through currying. |
| 64 |
|
| 65 |
The same applies to nonfix constructors. For example, the parentheses |
| 66 |
in |
| 67 |
|
| 68 |
[source,sml] |
| 69 |
---- |
| 70 |
fun valOf NONE = raise Option |
| 71 |
| valOf (SOME x) = x |
| 72 |
---- |
| 73 |
|
| 74 |
are required. However, the outermost constructed pattern in a `fn` or |
| 75 |
`case` expression need not be parenthesized, because in those cases |
| 76 |
there is always just one constructed pattern. So, both |
| 77 |
|
| 78 |
[source,sml] |
| 79 |
---- |
| 80 |
val valOf = fn NONE => raise Option |
| 81 |
| SOME x => x |
| 82 |
---- |
| 83 |
|
| 84 |
and |
| 85 |
|
| 86 |
[source,sml] |
| 87 |
---- |
| 88 |
fun valOf x = case x of |
| 89 |
NONE => raise Option |
| 90 |
| SOME x => x |
| 91 |
---- |
| 92 |
|
| 93 |
are fine. |
| 94 |
|
| 95 |
== Declarations and expressions |
| 96 |
|
| 97 |
It is a common mistake to confuse expressions and declarations. |
| 98 |
Normally an SML source file should only contain declarations. The |
| 99 |
following are declarations: |
| 100 |
|
| 101 |
[source,sml] |
| 102 |
---- |
| 103 |
datatype dt = ... |
| 104 |
fun f ... = ... |
| 105 |
functor Fn (...) = ... |
| 106 |
infix ... |
| 107 |
infixr ... |
| 108 |
local ... in ... end |
| 109 |
nonfix ... |
| 110 |
open ... |
| 111 |
signature SIG = ... |
| 112 |
structure Struct = ... |
| 113 |
type t = ... |
| 114 |
val v = ... |
| 115 |
---- |
| 116 |
|
| 117 |
Note that |
| 118 |
|
| 119 |
[source,sml] |
| 120 |
---- |
| 121 |
let ... in ... end |
| 122 |
---- |
| 123 |
|
| 124 |
isn't a declaration. |
| 125 |
|
| 126 |
To specify a side-effecting computation in a source file, you can write: |
| 127 |
|
| 128 |
[source,sml] |
| 129 |
---- |
| 130 |
val () = ... |
| 131 |
---- |
| 132 |
|
| 133 |
|
| 134 |
== Equality types |
| 135 |
|
| 136 |
SML has a fairly intricate built-in notion of equality. See |
| 137 |
<<EqualityType#>> and <<EqualityTypeVariable#>> for a thorough |
| 138 |
discussion. |
| 139 |
|
| 140 |
|
| 141 |
== Nested cases |
| 142 |
|
| 143 |
It is a common mistake to write nested case expressions without the |
| 144 |
necessary parentheses. See <<UnresolvedBugs#>> for a discussion. |
| 145 |
|
| 146 |
|
| 147 |
== (op *) |
| 148 |
|
| 149 |
It used to be a common mistake to parenthesize `op {asterisk}` as `(op |
| 150 |
{asterisk})`. Before SML'97, `{asterisk})` was considered a comment |
| 151 |
terminator in SML and caused a syntax error. At the time of writing, |
| 152 |
<<SMLNJ#,SML/NJ>> still rejects the code. An extra space may be used |
| 153 |
for portability: `(op {asterisk} )`. However, parenthesizing `op` is |
| 154 |
redundant, even though it is a widely used convention. |
| 155 |
|
| 156 |
|
| 157 |
== Overloading |
| 158 |
|
| 159 |
A number of standard operators (`+`, `-`, `~`, `*`, `<`, `>`, ...) and |
| 160 |
numeric constants are overloaded for some of the numeric types (`int`, |
| 161 |
`real`, `word`). It is a common surprise that definitions using |
| 162 |
overloaded operators such as |
| 163 |
|
| 164 |
[source,sml] |
| 165 |
---- |
| 166 |
fun min (x, y) = if y < x then y else x |
| 167 |
---- |
| 168 |
|
| 169 |
are not overloaded themselves. SML doesn't really support |
| 170 |
(user-defined) overloading or other forms of ad hoc polymorphism. In |
| 171 |
cases such as the above where the context doesn't resolve the |
| 172 |
overloading, expressions using overloaded operators or constants get |
| 173 |
assigned a default type. The above definition gets the type |
| 174 |
|
| 175 |
[source,sml] |
| 176 |
---- |
| 177 |
val min : int * int -> int |
| 178 |
---- |
| 179 |
|
| 180 |
See <<Overloading#>> and <<TypeIndexedValues#>> for further discussion. |
| 181 |
|
| 182 |
|
| 183 |
== Semicolons |
| 184 |
|
| 185 |
It is a common mistake to use redundant semicolons in SML code. This |
| 186 |
is probably caused by the fact that in an SML REPL, a semicolon (and |
| 187 |
enter) is used to signal the REPL that it should evaluate the |
| 188 |
preceding chunk of code as a unit. In SML source files, semicolons |
| 189 |
are really needed in only two places. Namely, in expressions of the |
| 190 |
form |
| 191 |
|
| 192 |
[source,sml] |
| 193 |
---- |
| 194 |
(exp ; ... ; exp) |
| 195 |
---- |
| 196 |
|
| 197 |
and |
| 198 |
|
| 199 |
[source,sml] |
| 200 |
---- |
| 201 |
let ... in exp ; ... ; exp end |
| 202 |
---- |
| 203 |
|
| 204 |
Note that semicolons act as expression (or declaration) separators |
| 205 |
rather than as terminators. |
| 206 |
|
| 207 |
|
| 208 |
== Stale bindings |
| 209 |
|
| 210 |
{empty} |
| 211 |
|
| 212 |
|
| 213 |
== Unresolved records |
| 214 |
|
| 215 |
{empty} |
| 216 |
|
| 217 |
|
| 218 |
== Value restriction |
| 219 |
|
| 220 |
See <<ValueRestriction#>>. |
| 221 |
|
| 222 |
|
| 223 |
== Type Variable Scope |
| 224 |
|
| 225 |
See <<TypeVariableScope#>>. |