| 1 |
= SMLNJDeviations |
| 2 |
|
| 3 |
Here are some deviations of <<SMLNJ#,SML/NJ>> from |
| 4 |
<<DefinitionOfStandardML#,The Definition of Standard ML (Revised)>>. |
| 5 |
Some of these are documented in the |
| 6 |
http://www.smlnj.org/doc/Conversion/index.html[SML '97 Conversion Guide]. |
| 7 |
Since MLton does not deviate from the Definition, you should look here |
| 8 |
if you are having trouble porting a program from MLton to SML/NJ or |
| 9 |
vice versa. If you discover other deviations of SML/NJ that aren't |
| 10 |
listed here, please send mail to |
| 11 |
mailto:[email protected][`[email protected]`]. |
| 12 |
|
| 13 |
* SML/NJ allows spaces in long identifiers, as in `S . x`. Section |
| 14 |
2.5 of the Definition implies that `S . x` should be treated as three |
| 15 |
separate lexical items. |
| 16 |
|
| 17 |
* SML/NJ allows `op` to appear in `val` specifications: |
| 18 |
+ |
| 19 |
[source,sml] |
| 20 |
---- |
| 21 |
signature FOO = sig |
| 22 |
val op + : int * int -> int |
| 23 |
end |
| 24 |
---- |
| 25 |
+ |
| 26 |
The grammar on page 14 of the Definition does not allow it. Recent |
| 27 |
versions of SML/NJ do give a warning. |
| 28 |
|
| 29 |
* SML/NJ rejects |
| 30 |
+ |
| 31 |
[source,sml] |
| 32 |
---- |
| 33 |
(op *) |
| 34 |
---- |
| 35 |
+ |
| 36 |
as an unmatched close comment. |
| 37 |
|
| 38 |
* SML/NJ allows `=` to be rebound by the declaration: |
| 39 |
+ |
| 40 |
[source,sml] |
| 41 |
---- |
| 42 |
val op = = 13 |
| 43 |
---- |
| 44 |
+ |
| 45 |
This is explicitly forbidden on page 5 of the Definition. Recent |
| 46 |
versions of SML/NJ do give a warning. |
| 47 |
|
| 48 |
* SML/NJ allows rebinding `true`, `false`, `nil`, `::`, and `ref` by |
| 49 |
the declarations: |
| 50 |
+ |
| 51 |
[source,sml] |
| 52 |
---- |
| 53 |
fun true () = () |
| 54 |
fun false () = () |
| 55 |
fun nil () = () |
| 56 |
fun op :: () = () |
| 57 |
fun ref () = () |
| 58 |
---- |
| 59 |
+ |
| 60 |
This is explicitly forbidden on page 9 of the Definition. |
| 61 |
|
| 62 |
* SML/NJ extends the syntax of the language to allow vector |
| 63 |
expressions and patterns like the following: |
| 64 |
+ |
| 65 |
[source,sml] |
| 66 |
---- |
| 67 |
val v = #[1,2,3] |
| 68 |
val #[x,y,z] = v |
| 69 |
---- |
| 70 |
+ |
| 71 |
MLton supports vector expressions and patterns with the <<SuccessorML#VectorExpsAndPats,`allowVectorExpsAndPats`>> <<MLBasisAnnotations#,ML Basis annotation>>. |
| 72 |
|
| 73 |
* SML/NJ extends the syntax of the language to allow _or patterns_ |
| 74 |
like the following: |
| 75 |
+ |
| 76 |
[source,sml] |
| 77 |
---- |
| 78 |
datatype foo = Foo of int | Bar of int |
| 79 |
val (Foo x | Bar x) = Foo 13 |
| 80 |
---- |
| 81 |
+ |
| 82 |
MLton supports or patterns with the <<SuccessorML#OrPats,`allowOrPats`>> <<MLBasisAnnotations#,ML Basis annotation>>. |
| 83 |
|
| 84 |
* SML/NJ allows higher-order functors, that is, functors can be |
| 85 |
components of structures and can be passed as functor arguments and |
| 86 |
returned as functor results. As a consequence, SML/NJ allows |
| 87 |
abbreviated functor definitions, as in the following: |
| 88 |
+ |
| 89 |
[source,sml] |
| 90 |
---- |
| 91 |
signature S = |
| 92 |
sig |
| 93 |
type t |
| 94 |
val x: t |
| 95 |
end |
| 96 |
functor F (structure A: S): S = |
| 97 |
struct |
| 98 |
type t = A.t * A.t |
| 99 |
val x = (A.x, A.x) |
| 100 |
end |
| 101 |
functor G = F |
| 102 |
---- |
| 103 |
|
| 104 |
* SML/NJ extends the syntax of the language to allow `functor` and |
| 105 |
`signature` declarations to occur within the scope of `local` and |
| 106 |
`structure` declarations. |
| 107 |
|
| 108 |
* SML/NJ allows duplicate type specifications in signatures when the |
| 109 |
duplicates are introduced by `include`, as in the following: |
| 110 |
+ |
| 111 |
[source,sml] |
| 112 |
---- |
| 113 |
signature SIG1 = |
| 114 |
sig |
| 115 |
type t |
| 116 |
type u |
| 117 |
end |
| 118 |
signature SIG2 = |
| 119 |
sig |
| 120 |
type t |
| 121 |
type v |
| 122 |
end |
| 123 |
signature SIG = |
| 124 |
sig |
| 125 |
include SIG1 |
| 126 |
include SIG2 |
| 127 |
end |
| 128 |
---- |
| 129 |
+ |
| 130 |
This is disallowed by rule 77 of the Definition. |
| 131 |
|
| 132 |
* SML/NJ allows sharing constraints between type abbreviations in |
| 133 |
signatures, as in the following: |
| 134 |
+ |
| 135 |
[source,sml] |
| 136 |
---- |
| 137 |
signature SIG = |
| 138 |
sig |
| 139 |
type t = int * int |
| 140 |
type u = int * int |
| 141 |
sharing type t = u |
| 142 |
end |
| 143 |
---- |
| 144 |
+ |
| 145 |
These are disallowed by rule 78 of the Definition. Recent versions of |
| 146 |
SML/NJ correctly disallow sharing constraints between type |
| 147 |
abbreviations in signatures. |
| 148 |
|
| 149 |
* SML/NJ disallows multiple `where type` specifications of the same |
| 150 |
type name, as in the following |
| 151 |
+ |
| 152 |
[source,sml] |
| 153 |
---- |
| 154 |
signature S = |
| 155 |
sig |
| 156 |
type t |
| 157 |
type u = t |
| 158 |
end |
| 159 |
where type u = int |
| 160 |
---- |
| 161 |
+ |
| 162 |
This is allowed by rule 64 of the Definition. |
| 163 |
|
| 164 |
* SML/NJ allows `and` in `sharing` specs in signatures, as in |
| 165 |
+ |
| 166 |
[source,sml] |
| 167 |
---- |
| 168 |
signature S = |
| 169 |
sig |
| 170 |
type t |
| 171 |
type u |
| 172 |
type v |
| 173 |
sharing type t = u |
| 174 |
and type u = v |
| 175 |
end |
| 176 |
---- |
| 177 |
|
| 178 |
* SML/NJ does not expand the `withtype` derived form as described by |
| 179 |
the Definition. According to page 55 of the Definition, the type |
| 180 |
bindings of a `withtype` declaration are substituted simultaneously in |
| 181 |
the connected datatype. Consider the following program. |
| 182 |
+ |
| 183 |
[source,sml] |
| 184 |
---- |
| 185 |
type u = real ; |
| 186 |
datatype a = |
| 187 |
A of t |
| 188 |
| B of u |
| 189 |
withtype u = int |
| 190 |
and t = u |
| 191 |
---- |
| 192 |
+ |
| 193 |
According to the Definition, it should be expanded to the following. |
| 194 |
+ |
| 195 |
[source,sml] |
| 196 |
---- |
| 197 |
type u = real ; |
| 198 |
datatype a = |
| 199 |
A of u |
| 200 |
| B of int ; |
| 201 |
type u = int |
| 202 |
and t = u |
| 203 |
---- |
| 204 |
+ |
| 205 |
However, SML/NJ expands `withtype` bindings sequentially, meaning that |
| 206 |
earlier bindings are expanded within later ones. Hence, the above |
| 207 |
program is expanded to the following. |
| 208 |
+ |
| 209 |
[source,sml] |
| 210 |
---- |
| 211 |
type u = real ; |
| 212 |
datatype a = |
| 213 |
A of int |
| 214 |
| B of int ; |
| 215 |
type u = int |
| 216 |
type t = int |
| 217 |
---- |
| 218 |
|
| 219 |
* SML/NJ allows `withtype` specifications in signatures. |
| 220 |
+ |
| 221 |
MLton supports `withtype` specifications in signatures with the <<SuccessorML#SigWithtype,`allowSigWithtype`>> <<MLBasisAnnotations#,ML Basis annotation>>. |
| 222 |
|
| 223 |
* SML/NJ allows a `where` structure specification that is similar to a |
| 224 |
`where type` specification. For example: |
| 225 |
+ |
| 226 |
[source,sml] |
| 227 |
---- |
| 228 |
structure S = struct type t = int end |
| 229 |
signature SIG = |
| 230 |
sig |
| 231 |
structure T : sig type t end |
| 232 |
end where T = S |
| 233 |
---- |
| 234 |
+ |
| 235 |
This is equivalent to: |
| 236 |
+ |
| 237 |
[source,sml] |
| 238 |
---- |
| 239 |
structure S = struct type t = int end |
| 240 |
signature SIG = |
| 241 |
sig |
| 242 |
structure T : sig type t end |
| 243 |
end where type T.t = S.t |
| 244 |
---- |
| 245 |
+ |
| 246 |
SML/NJ also allows a definitional structure specification that is |
| 247 |
similar to a definitional type specification. For example: |
| 248 |
+ |
| 249 |
[source,sml] |
| 250 |
---- |
| 251 |
structure S = struct type t = int end |
| 252 |
signature SIG = |
| 253 |
sig |
| 254 |
structure T : sig type t end = S |
| 255 |
end |
| 256 |
---- |
| 257 |
+ |
| 258 |
This is equivalent to the previous examples and to: |
| 259 |
+ |
| 260 |
[source,sml] |
| 261 |
---- |
| 262 |
structure S = struct type t = int end |
| 263 |
signature SIG = |
| 264 |
sig |
| 265 |
structure T : sig type t end where type t = S.t |
| 266 |
end |
| 267 |
---- |
| 268 |
|
| 269 |
* SML/NJ disallows binding non-datatypes with datatype replication. |
| 270 |
For example, it rejects the following program that should be allowed |
| 271 |
according to the Definition. |
| 272 |
+ |
| 273 |
[source,sml] |
| 274 |
---- |
| 275 |
type ('a, 'b) t = 'a * 'b |
| 276 |
datatype u = datatype t |
| 277 |
---- |
| 278 |
+ |
| 279 |
This idiom can be useful when one wants to rename a type without |
| 280 |
rewriting all the type arguments. For example, the above would have |
| 281 |
to be written in SML/NJ as follows. |
| 282 |
+ |
| 283 |
[source,sml] |
| 284 |
---- |
| 285 |
type ('a, 'b) t = 'a * 'b |
| 286 |
type ('a, 'b) u = ('a, 'b) t |
| 287 |
---- |
| 288 |
|
| 289 |
* SML/NJ disallows sharing a structure with one of its substructures. |
| 290 |
For example, SML/NJ disallows the following. |
| 291 |
+ |
| 292 |
[source,sml] |
| 293 |
---- |
| 294 |
signature SIG = |
| 295 |
sig |
| 296 |
structure S: |
| 297 |
sig |
| 298 |
type t |
| 299 |
structure T: sig type t end |
| 300 |
end |
| 301 |
sharing S = S.T |
| 302 |
end |
| 303 |
---- |
| 304 |
+ |
| 305 |
This signature is allowed by the Definition. |
| 306 |
|
| 307 |
* SML/NJ disallows polymorphic generalization of refutable |
| 308 |
patterns. For example, SML/NJ disallows the following. |
| 309 |
+ |
| 310 |
[source,sml] |
| 311 |
---- |
| 312 |
val [x] = [[]] |
| 313 |
val _ = (1 :: x, "one" :: x) |
| 314 |
---- |
| 315 |
+ |
| 316 |
Recent versions of SML/NJ correctly allow polymorphic generalization |
| 317 |
of refutable patterns. |
| 318 |
|
| 319 |
* SML/NJ uses an overly restrictive context for type inference. For |
| 320 |
example, SML/NJ rejects both of the following. |
| 321 |
+ |
| 322 |
[source,sml] |
| 323 |
---- |
| 324 |
structure S = |
| 325 |
struct |
| 326 |
val z = (fn x => x) [] |
| 327 |
val y = z :: [true] :: nil |
| 328 |
end |
| 329 |
---- |
| 330 |
+ |
| 331 |
[source,sml] |
| 332 |
---- |
| 333 |
structure S : sig val z : bool list end = |
| 334 |
struct |
| 335 |
val z = (fn x => x) [] |
| 336 |
end |
| 337 |
---- |
| 338 |
+ |
| 339 |
These structures are allowed by the Definition. |
| 340 |
|
| 341 |
== Deviations from the Basis Library Specification |
| 342 |
|
| 343 |
Here are some deviations of SML/NJ from the <<BasisLibrary#,Basis Library>> |
| 344 |
https://smlfamily.github.io/Basis[specification]. |
| 345 |
|
| 346 |
* SML/NJ exposes the equality of the `vector` type in structures such |
| 347 |
as `Word8Vector` that abstractly match `MONO_VECTOR`, which says |
| 348 |
`type vector`, not `eqtype vector`. So, for example, SML/NJ accepts |
| 349 |
the following program: |
| 350 |
+ |
| 351 |
[source,sml] |
| 352 |
---- |
| 353 |
fun f (v: Word8Vector.vector) = v = v |
| 354 |
---- |
| 355 |
|
| 356 |
* SML/NJ exposes the equality property of the type `status` in |
| 357 |
`OS.Process`. This means that programs which directly compare two |
| 358 |
values of type `status` will work with SML/NJ but not MLton. |
| 359 |
|
| 360 |
* Under SML/NJ on Windows, `OS.Path.validVolume` incorrectly considers |
| 361 |
absolute empty volumes to be valid. In other words, when the |
| 362 |
expression |
| 363 |
+ |
| 364 |
[source,sml] |
| 365 |
---- |
| 366 |
OS.Path.validVolume { isAbs = true, vol = "" } |
| 367 |
---- |
| 368 |
+ |
| 369 |
is evaluated by SML/NJ on Windows, the result is `true`. MLton, on |
| 370 |
the other hand, correctly follows the Basis Library Specification, |
| 371 |
which states that on Windows, `OS.Path.validVolume` should return |
| 372 |
`false` whenever `isAbs = true` and `vol = ""`. |
| 373 |
+ |
| 374 |
This incorrect behavior causes other `OS.Path` functions to behave |
| 375 |
differently. For example, when the expression |
| 376 |
+ |
| 377 |
[source,sml] |
| 378 |
---- |
| 379 |
OS.Path.toString (OS.Path.fromString "\\usr\\local") |
| 380 |
---- |
| 381 |
+ |
| 382 |
is evaluated by SML/NJ on Windows, the result is `"\\usr\\local"`, |
| 383 |
whereas under MLton on Windows, evaluating this expression (correctly) |
| 384 |
causes an `OS.Path.Path` exception to be raised. |