A Standard ML to JavaScript Compiler

By Martin Elsman, 2008-2015


SMLtoJs (pronounced "SML toys") is a compiler from Standard ML to JavaScript, which allows programmers to enjoy the power of Standard ML 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 Basis Library. It also has support for calling JavaScript functions and for executing plain JavaScript.


  • 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.
  • JavaScript integration. SMLtoJs has support for calling JavaScript functions and for executing plain JavaScript. Moreover, Standard ML functions may be exported and called by JavaScript code.
  • 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 flags. As a result, SMLtoJs generates fairly efficient JavaScript 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.

Online Demonstration

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 github checkout:

 $ 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.

Building SMLtoJs

To compile SMLtoJs from the sources (MLKit svn checkout; see above), simply type

  $ cd smltojs
  $ ./autobuild
  $ ./configure
  $ make smltojs
If compilation succeeds, an executable file 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 JavaScript files and a file prims.js, which contains a set of primitive JavaScript functions used by the generated code.

Hint: Adding the flag "-o name" as command-line argument to smltojs results in the file name.html being generated instead of run.html.

Testing that it Works

To compile and test the test programs, cd to the js/test directory and run "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 calls are translated into JavaScript calls. We are planning in the 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):

   test13c: WRONG

There are plenty of possibilities for further improvements, including:

  1. Using numbers to represent constructors (instead of strings).
  2. 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 license information.

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 for Hosting a Standard ML Compiler in a Web Browser.
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.
SMLtoJs Examples
program sources
Ex: rwp_ex1 src
Ex: rwp_ex2 src
Ex: rwp_ex3 src
Ex: counter src
Ex: temp src
Ex: life src
Ex: cal src
Test: array2 src
Test: int31 src
Test: int src
Test: vectorslice src
Test: word8arrayslice src
Test: word8vector src
Test: arrayslice src
Test: general src
Test: int32_2 src
Test: math src
Test: stringcvt src
Test: testdyn1 src
Test: vector src
Test: word8array src
Test: array src
Test: int_2 src
Test: int32 src
Test: listpair src
Test: string src
Test: testmatc src
Test: word src
Test: word8 src
Test: word31 src
Test: word32 src
Test: bytechar src
Test: int31_2 src
Test: intinf src
Test: list src
Test: real src
Test: substring src
Test: word8vectorslice src
Test: time src
Test: date src
Test: unixpath src
Test: exec src