1 Plain SML code
2 Interaction between ML and Scheme

Strandard ML

Chongkai Zhu

This is an implementation of the Standard ML programming language [SML'97]. It features all the requirements of the language, except static type checking. The main aim of this project is to provide interaction between Scheme and ML, i.e, a prgram can be written partly in ML and partly in Scheme, with the ability of either side calling the other side. Part of the standard ML basis library [LIB] is also provided.

1 Plain SML code

To execute ML code in PLT Scheme, simply start your program with

  #lang planet chongkai/sml

No ML interaction mode is supported. To get output from ML code, use the ML print procedure. Notice that the code is executed in an untyped manner.

You can also split big ML programs into seperate files. The unit of seperation can be either a ML signature, structure, or functor. For example, you can put a ML structure named FOO in file "FOO-struct.ss", and reference it in another file that lives in the same directory (no sepcial syntax needed). The file name of signarue/functor should be "FOO-sig.ss" and "FOO-functor.ss" respectively.

2 Interaction between ML and Scheme

Most ML datatypes have a obvious match in PLT Scheme. However, several compound datatypes need some elaboration. ML records are mapped to Scheme association lists, using symbols as labels. ML tuples are mapped to Scheme vectors. Note that ML vectors and arrays are also mapped to Scheme vectors.

ML datatype is mapped roughly to structure type in PLT Scheme. For example,

;datatype t = FOO | BAR of s

is equivalent to

  (define-syntax t-type (list #'FOO-datatype #'BAR-datatype))
  (define-values (FOO FOO?)
    (let ()
      (define-struct FOO () #:transparent)
      (values (make-FOO) FOO?)))
  (define-syntax FOO-datatype (list #'FOO #'FOO?))
  (define-values (BAR BAR? BAR-content)
    (let ()
      (define-struct BAR (content) #:transparent)
      (values make-BAR BAR? BAR-content)))
  (define-syntax BAR-datatype (list #'BAR #'BAR? #'BAR-content))

ML exception is mapped to Scheme exn in a similar way:

exception E1;

is equivalent to

  (define-values (E1 E1?)
    (let ()
      (define-struct (E1 exn) () #:transparent)
      (values (lambda (m) (make-E1 "E1" m))
              (lambda (t) (E1? (t (current-continuation-marks)))))))
  (define-syntax E1-datatype
    (list #'E1 #'E1?))

The most used part (but not all) of the Standard ML Basis Library are provided. They also serve as a good example of interactions between ML and Scheme. Interested readers are encourged to study the code under /lib subdirectory.

An ML structure is implemented as an "ml-package", which is very similar to scheme/package. It is implemented in (planet chongkai/sml/ml-package). The difference is that ml-package works on symbols while scheme/package works on identifers (syntax object). So they are different and not interchangeable.

See file sml/lib/List-struct.ss as an example of how to implement an ML structure directly in Scheme.

The other way around, to access ML code in Scheme, just require the name-struct module, and then open-package, as if the ML code is implemented in Scheme by the aforementioned method.


[SML'97] Robin Milner, Mads Tofte, Robert Harper, David MacQueen, The Definition of Standard ML (Revised), MIT Press, 1997. http://mitpress.mit.edu/book-home.tcl?isbn=0262631814
[LIB] Emden Gansner, John Reppy, “The Standard ML Basis Library,” Cambridge University Press, 2004. http://www.standardml.org/Basis/