This section describes the modules MLton makes available that are not part of the basis library.
MLton: MLTON
signature MLTON =
sig
structure GC:
sig
val messages: bool -> unit
val summary: bool -> unit
end
val cleanAtExit: unit -> unit
val random: unit -> Word32.word
val safe: bool
datatype status = Original | Clone
val saveWorld: string -> status
val size: 'a -> int
type 'a cont
val callcc: ('a cont -> 'a) -> 'a
val throw: 'a cont -> 'a -> 'b
end
GC.messages
controls whether diagnostic messages are
printed at the beginning and end of each garbage collection and at
each stack resizing. It is the same as the gc-messages
runtime system option.
GC.summary
controls whether a summary of garbage
collection statistics is printed upon termination of the program. It
is the same as the gc-summary
runtime system option.
val _ = cleanAtExit()
is concatenated onto the end of
every program. This call flushes and closes IO buffers. There is no
need for a user to call this function.
random()
uses /dev/random
to return a random
word.
safe
is a compile time constant that makes it easy to
include assertions and checks in code that you would like removed when
running at full speed. safe
will be true unless the program
is compiled with the -u
option, in which case it will be
false. The simplification passes of the compiler will remove any uses
of safe
at compile time. For example, array subscript might
be implemented as:
fun sub(a, i) =
if safe andalso (i < 0 orelse i >= length a)
then raise Subscript
else unsafeSub(a, i)
When compiled with the -u
flag, sub
will reduce to
unsafeSub
.
saveWorld "foo"
saves the entire state of the computation in
the file foo
. The computation can then be restarted at a
later time using the load-world
runtime system option. The
call to saveWorld
in the original computation returns
Original
and the call in the restarted world returns
Clone
. The following example is a transcript run from the
examples
directory using examples/save-world.sml
:
% mlton save-world.sml
% save-world
I am the original
% save-world @MLton load-world world --
I am the clone
size x
returns the amount of heap space taken by the
value of x
. Such information can be quite useful in
understanding the space behavior of a program. Size is implemented by
performing a full garbage collection of the heap, so it can be slow
and should be used with care. For an example, see
examples/size.sml
.
callcc
and throw
implement continuations. At
present callcc
is implemented by copying the entire stack
into the heap and throw
is implemented by copying the entire
stack from the heap. Thus, they will be quite slow if the stack is
large.
SMLofNJ: SML_OF_NJ
SMLofNJ
implements a subset of the structure of the same name
provided in Standard ML of New Jersey. It is included to make
it easier to port programs between the two systems.
The semantics of these functions may be different than in SML/NJ.
signature SML_OF_NJ =
sig
structure Cont:
sig
type 'a cont
val callcc: ('a cont -> 'a) -> 'a
val throw: 'a cont -> 'a -> 'b
end
structure SysInfo:
sig
val getHostArch: unit -> string
val getOSName: unit -> string
end
structure Internals:
sig
structure GC:
sig
val messages: bool -> unit
end
end
val exportFn: string * (string * string list -> OS.Process.status) -> unit
val exportML: string -> bool
val getCmdName: unit -> string
val getArgs: unit -> string list
val getAllArgs: unit -> string list
end
SysInfo.getHostArch
returns "X86"
.
SysInfo.getOSName
returns "Linux"
.
Internals.GC.messages
is the same as MLton.GC.messages
.
getCmdName
is the same as CommandLine.name
.
getArgs
is the same as CommandLine.arguments
.
getAllArgs
is the same as getCmdName() :: getArgs()
.
exportFn
is implemented by:
fun exportFn(file, f) =
OS.Process.exit
(case MLton.saveWorld(file ^ ".mlton") of
MLton.Original => 0
| MLton.Clone => f(getCmdName(), getArgs()))
exportML
is implemented by:
fun exportML f =
case MLton.saveWorld f of
MLton.Original => false
| MLton.Clone => true
Unsafe: UNSAFE
This module is a subset of the Unsafe
module provided by
SML/NJ. It is included in MLton because the code generated by ML-Yacc
includes references to unsafe subscript operations.
signature UNSAFE =
sig
structure Vector : UNSAFE_VECTOR
structure Array : UNSAFE_ARRAY
structure CharVector : UNSAFE_MONO_VECTOR
where type vector = CharVector.vector
where type elem = CharVector.elem
structure CharArray : UNSAFE_MONO_ARRAY
where type array = CharArray.array
where type elem = CharArray.elem
structure Word8Vector : UNSAFE_MONO_VECTOR
where type vector = Word8Vector.vector
where type elem = Word8Vector.elem
structure Word8Array : UNSAFE_MONO_ARRAY
where type array = Word8Array.array
where type elem = Word8Array.elem
structure Real64Array : UNSAFE_MONO_ARRAY
where type array = Real64Array.array
where type elem = Real64Array.elem
end