SMLtoJs (pronounced "SML toys") is a compiler from Standard ML to
static typing, higher-order functions, pattern matching, and modules
for programming client-side web applications.
SMLtoJs compiles all of Standard ML, including most of the Standard ML
- All of Standard ML. SMLtoJs has support for all of Standard ML,
including modules, pattern matching, higher-order functions,
generative exceptions, etc.
- Standard ML Basis Library support. SMLtoJs has support for most of
the Standard ML basis library, including the following structures:
Array2 ArraySlice Array Bool Byte Char CharArray CharArraySlice
CharVector CharVectorSlice Date General Int Int31 Int32 IntInf
LargeWord ListPair List Math Option OS.Path Pack32Big
Pack32Little Random Real StringCvt String Substring Text Time
Timer Vector VectorSlice Word Word31 Word32 Word8 Word8Array
Word8ArraySlice Word8Vector Word8VectorSlice.
- Simple DOM access. Simple DOM access and support for installing Standard ML
functions as DOM event handlers and timer call back functions.
- Optimization. All Standard ML module language constructs, including functors,
functor applications, and signature constraints, are eliminated by
SMLtoJs at compile time. Moreover, SMLtoJs performs a series of
compile time optimizations, including function inlining and
specialization of higher-order recursive functions, such as map
and foldl. Optimizations can be controlled using compile-time
code, although there are rooms for improvements; see below.
- Reactive Web Programming. SMLtoJs has library support for
Reactive Web Programming. See the rwp_ex1
example for an introduction to what you can do with this
library. The simple library API captures
the basic notions of behaviors and event streams as well as the
concepts of behavior transformers and event stream transformers.
To see SMLtoJs in action, click on the examples in the table to the right; there are also
links to the Standard ML source code. To learn more about the details of the SMLtoJs implementation, see the paper presented at PLASTIC'2011.
Getting the Sources
SMLtoJs compiles on Debian Linux systems with MLton or MLKit
installed. The latest version of the SMLtoJS sources are available through an MLKit
$ git clone https://github.com/melsman/mlkit.git smltojs
This command copies the sources to the directory smltojs.
For older (and perhaps more stable) versions of SMLtoJS, you may browse the list of releases.
To compile SMLtoJs from the sources (MLKit svn checkout;
see above), simply type
$ cd smltojs
$ make smltojs
If compilation succeeds, an executable
bin/smltojs should now be available.
How it Works
The SMLtoJs executable
bin/smltojs takes as argument an sml-file
(or an mlb-file referencing the sml-files and other mlb-files of the
project) and produces an html file called
run.html provided there are
no type errors! The resulting html-file mentions the generated
prims.js, which contains a set of
Hint: Adding the flag
"-o name" as command-line argument to
results in the file name.html being generated instead of
Testing that it Works
To compile and test the test programs, cd to the
"make clean all":
$ cd js/test
$ make clean all
You can now start Firefox on the generated html-files; the file
all.html includes links to all the test files:
$ firefox all.html
The examples temp.html, counter.html, and life.html are the most
interesting examples at the moment (more will come).
Currently, SMLtoJs does not implement tail-calls properly, as all
near future to implement tail-calls using trampolines.
There is a known issue with a bug in the following test (in some cases,
the implementation pretty prints reals slightly different than suggested by the spec):
There are plenty of possibilities for further improvements, including:
- Using numbers to represent constructors (instead of strings).
- Unboxing of datatypes; currently, a boxed representation is used
for lists, for example.
Copyright and License
SMLtoJs is distributed under the GPL license; see the files in
smltojs/doc/license for copyright notices and detailed
2013-09-17: Version 4.3.7 of SMLtoJs is released.|
2011-04-16: Use SMLtoJsOnline
for compiling and running Standard ML programs
in your browser (improved 2013).|
2010-09-25: Martin gave a talk at the
2010 ML Workshop on using SMLtoJs
a Standard ML Compiler in a Web
2008-08-16: SMLtoJs version 4.3.5 is available.|
2007-11-27: Martin gave a talk in the DIKU TOPPS group (slides).|
2007-09-17: Martin gave a talk on the PLS Monday lunch meeting (slides).|
2007-09-05: SMLtoJs version 4.3.4 is available.|
2007-08-31: SMLtoJs version 4.3.3 is available.|
2007-08-08: SMLtoJs version 4.3.2 is available.|