Standard ML
A statically-typed functional programming language with type inference, algebraic data types, and a sophisticated module system that influenced OCaml, Haskell, and Rust.
Created by Robin Milner, Mads Tofte, Robert Harper, David MacQueen
Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is one of the most influential programming languages ever designed, having directly shaped the development of OCaml, Haskell, F#, Rust, and many other modern languages.
History & Origins
Standard ML’s story begins with ML (Meta Language), created by Robin Milner at Stanford University in 1972 as the meta-language for the LCF (Logic for Computable Functions) theorem prover. What started as a scripting language for proofs became recognized as a powerful programming language in its own right.
The Road to Standardization
In the late 1970s and early 1980s, ML implementations began to diverge. Luca Cardelli created the first standalone ML compiler in 1980-81, and the Hope language at Edinburgh introduced new ideas like algebraic data types. By 1983, Milner recognized the need for a standard, and began work on what would become Standard ML.
Key Contributors:
- Robin Milner - The original designer of ML and leader of the standardization effort
- Mads Tofte - Co-author of the Definition, developed the module system semantics
- Robert Harper - Co-author of the Definition, developed type theory foundations
- David MacQueen - Co-author of the 1997 revision, co-created SML/NJ
The Definition
Unlike most languages defined by implementation, Standard ML is defined by a formal semantics document: The Definition of Standard ML. The original 1990 Definition and the 1997 Revised Definition provide a rigorous mathematical specification of the language, making SML one of the few languages with a complete formal definition.
Major Implementations
Standard ML of New Jersey (SML/NJ) - The reference implementation, developed at Bell Labs and Princeton. Known for its interactive environment and extensive library support. Still actively maintained with version 110.99.9 released in 2025.
MLton - A whole-program optimizing compiler that produces highly efficient native code. MLton performs aggressive inlining, unboxing, and defunctionalization, often producing executables that rival hand-written C.
Moscow ML - A lightweight implementation based on the Caml Light runtime, popular for teaching.
Poly/ML - Known for its parallel garbage collector and large address space support.
What Makes Standard ML Different
1. Hindley-Milner Type Inference
SML pioneered type inference - the compiler deduces types without explicit annotations:
| |
2. Algebraic Data Types and Pattern Matching
Define structured data and process it elegantly:
| |
3. The Module System
SML’s module system is considered one of its greatest contributions to programming language design:
| |
Functors allow parameterizing modules over other modules:
| |
4. Strict Evaluation with Explicit Effects
Unlike Haskell, SML uses strict (eager) evaluation, making side effects predictable:
| |
5. Exception Handling
SML has a robust exception system:
| |
The Basis Library
Standard ML comes with a standard library called the Basis Library, providing:
- Core Types:
Int,Real,String,Char,Bool - Collections:
List,Array,Vector - I/O:
TextIO,BinIO - OS Interface:
OS.FileSys,OS.Process - Utilities:
Option,Result,StringCvt
| |
Why Standard ML Matters
Influence on Modern Languages
- OCaml - Direct descendant, added objects and native compilation
- Haskell - Adopted type classes instead of modules, added laziness
- F# - Microsoft’s functional language for .NET, heavily SML-influenced
- Rust - Pattern matching, type inference, and algebraic data types
- Elm - Simplified SML for web programming
- Scala - Module system influenced by SML functors
Academic Impact
Standard ML transformed computer science education. The Definition showed that programming languages could be specified with mathematical precision, spawning the field of programming language theory.
CMU’s influential courses, MIT’s programming language courses, and countless others use SML to teach:
- Functional programming principles
- Type systems and type inference
- Module systems and abstraction
- Formal semantics
Compiler Implementation
SML is uniquely suited to writing compilers:
| |
The pattern matching and algebraic data types make manipulating abstract syntax trees natural and safe.
Getting Started
File Structure
SML source files use the .sml extension. A simple program:
| |
The REPL
SML implementations provide interactive environments:
- 1 + 2;
val it = 3 : int
- fun double x = x * 2;
val double = fn : int -> int
- double 21;
val it = 42 : int
- map double [1, 2, 3];
val it = [2, 4, 6] : int list
The - is the SML prompt. Expressions are terminated with ;.
Compilation
| |
A Complete Example
| |
The SML Community
Resources
- SML Family GitHub - smlfamily.github.io
- SML/NJ Website - smlnj.org
- MLton Website - mlton.org
- The Definition - Available as PDF and in print
Learning Path
- Programming in Standard ML - Robert Harper’s free textbook
- ML for the Working Programmer - Paulson’s classic text
- The Definition of Standard ML - The formal specification
- Purely Functional Data Structures - Okasaki’s thesis (in SML)
Standard ML vs Similar Languages
| Feature | Standard ML | OCaml | Haskell | F# |
|---|---|---|---|---|
| Evaluation | Strict | Strict | Lazy | Strict |
| Type Inference | HM | HM + rows | HM + classes | HM + .NET |
| Modules | Functors | Functors + objects | Type classes | .NET assemblies |
| Definition | Formal | Implementation | Report | Implementation |
| Side Effects | Explicit refs | Explicit refs | Monads | Explicit |
Continue to the Hello World tutorial to write your first Standard ML program.
Timeline
Notable Uses & Legacy
Compiler Research
Standard ML is the canonical language for teaching and researching programming language implementation and type theory.
Proof Assistants
HOL4, Isabelle, and other theorem provers are written in or closely related to Standard ML.
Carnegie Mellon University
SML is used extensively in CMU's programming languages curriculum, including the influential 15-150 course.
Bell Labs
SML/NJ was developed at Bell Labs for systems programming research and compiler construction.
MLton Compiler
MLton itself demonstrates SML's capability by being a high-performance whole-program optimizing compiler written in SML.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull eldesh/smlnj:latestExample usage:
docker run --rm -v $(pwd):/app -w /app eldesh/smlnj:latest sml hello.sml