Chapter 13 presents the micro-SML language, a subset of Core Standard ML and an extension of micro-ML. This rounds off the topic of functional languages by putting together many of the previously discussed topics of higher-order functions, polymorphic type inference, heap allocation, garbage collection, continuations and local optimization techniques previously applied to micro-C.
The compiler has been tested on .NET 8, .NET 9 and .NET 10**.
Choose the desired .NET version in the microsmlc.fsprog file by
changing:
<TargetFramework>net10.0</TargetFramework>to one of
net8.0net9.0net10.0
Make MicroSML current directory.
To build the compiler, run:
dotnet build microsmlc.fsprojor simply:
dotnet buildFor example:
dotnet buildRestore complete (0,1s)
microsmlc net10.0 succeeded (0,1s) → bin/Debug/net10.0/microsmlc.dll
Build succeeded in 0,4sThe build process automatically runs the lexer and parser generators
whenever either FunLex.fsl or FunPar.fsy has changed.
The compiled compiler executable is placed in:
bin/Debug/net10.0/microsmlc.dllTo compile a micro-SML program, for example SmlEx/ex01.sml, run:
dotnet run SmlEx/ex01.smlThis compiles ex01.sml from the SmlEx directory and generates the
output file:
SmlEx/ex01.outExample:
dotnet run SmlEx/ex01.smlMicro-SML compiler v 2.0 of 2026-05-30
Compiling SmlEx/ex01.sml to SmlEx/ex01.out.The compiler supports below options:
| Option | Description |
|---|---|
-debug |
outputs intermediate AST and other debug information on terminal |
-verbose |
outputs intermediate program transformations on terminal |
-eval |
Interpretates the program and outputs result on terminal. |
-alpha |
Performs alpha conversion, left as exercise. |
-opt |
Performs local peephole optimizations. |
Example:
dotnet run -eval -verbose SmlEx/ex01.smlOptions can be combined arbitrarily.
The micro virtual machine, micro-VM, is located in the MicroVM
directory, where the file README.md explains how to build and use
it.
The result is an executable named:
microvm(Unix/macOS)microvm.exe(Windows)
located in the MicroVM directory.
The following example demonstrates compiling and executing
queens.sml.
-
Change to the
MicroSMLdirectory. -
Compile the program
dotnet run SmlEx/queens.smlMicro-SML compiler v 2.0 of 2026-05-30
Compiling SmlEx/queens.sml to SmlEx/queens.out.- Run the compiled program using micro-VM:
../MicroVM/microvm SmlEx/queens.out[[[6,5] ,[5,3] ,[4,1] ,[3,6] ,[2,4] ,[1,2] ] ,
[[5,4] ,[4,2] ,[3,5] ,[2,3] ,[1,1] ] ,
[[4,3] ,[3,1] ,[2,4] ,[1,2] ] ,[] ,[] ,[[1,1] ] ]
Result value: #33629982712
Used 1 cpu milli-seconds
Number of GC: 0The micro-SML compiler comes with a test suite of 50 test programs covering both static and dynamic semantics.
The test programs are in the SmlEx directory.
-
Programs named
exXX.smlcompiles and run with micro-SML. The programqueens.smlused above is an example. -
Programs named
exFXX.smldemonstrate expected compile time errors, such as type errors.
For example, exF02.sml demonstrates a problem unifying two types
that cannot be unified.
Example:
dotnet run SmlEx/exF02.smlMicro-SML compiler v 2.0 of 2026-05-30
Compiling SmlEx/exF02.sml to SmlEx/exF02.out.
Type error: bool and intMicro-SML includes an automated test suite located in:
SmlEx/test.fsxThe test suite consists of three categories of test programs:
- Programs that compile and run with Micro-SML,
exXX.sml.
These verify static and dynamic semantic compliance.
-
Same as 1), but focus on generative exceptions,
exnXX.sml. -
Programs that fail to compile with micro-SML due to type errors.
These verify the static semantics.
Run the test suite from the SmlEx directory:
dotnet fsi test.fsxSetting current directory to MicroSML
Delete all *.out files in SmlEx directory.
Compiling file with options -eval: ex01.sml
outMatch: [5]
Compiling file with options : ex01.sml
micro-SML out: [5]
5
...
Result of test.
ex01.sml(Eval): OK
ex01.sml(Comp): OK
ex01.sml(Comp+Opt): OK
ex02.sml(Eval): OK
ex02.sml(Comp): OK
ex02.sml(Comp+Opt): OK
ex03.sml(Eval): OK
...The test script
- compiles and runs all test programs.
- compiles with different combinations of compiler options. See
test.fsxfor the combinations used. - compares each execution with the expected output.
- summarizes the results at the end.
- works across multiple platforms.
Note The test suite runs relatively slow because it launches external system processes to compile and execute the programs.