Every type below was inferred (Hindley–Milner, with let-polymorphism). Click a function to jump to it.
The A-normal-form IR after closure conversion and pattern-match compilation, for the functions that survive dead-code elimination. Variables marked # hold only immediates, so they stay out of the garbage collector's root set.
The exact module that runs, printed as WebAssembly text. The runtime (allocator, copying collector, structural equality, strings) is hand-written wasm emitted by the compiler.
The fuzzer writes random, well-typed Lumen programs and runs each one three ways: in the reference interpreter, as compiled WebAssembly, and as WebAssembly that collects garbage on every allocation. All three must print exactly the same bytes and stop with the same error.
Press “Start fuzzing” to watch programs being generated and checked.
To show the fuzzer can find real bugs, fuzz/mutants.js injects each of these compiler bugs into a copy of the compiler and counts how many random programs it takes to notice. All 27 were caught.
| Injected bug | Caught after |
|---|
Lumen's compiler is also written in Lumen: selfhost/lumenc.lm, 3,300 lines covering the same lexer, type inference, exhaustiveness checker, lowering, code generator, garbage-collected runtime and binary encoder. The JavaScript compiler builds it (stage 1). That binary then compiles lumenc's own source (stage 2), and stage 2 does it again (stage 3). All three binaries must be identical byte for byte, and they must also match what the JavaScript compiler emits.
| Stage | Time | Size | SHA-256 |
|---|---|---|---|
| 1 · JavaScript compiler builds lumenc | – | – | – |
| 2 · stage 1 compiles lumenc | – | – | – |
| 3 · stage 2 compiles lumenc | – | – | – |
A superoptimizer (superopt/) mined the compiler's own output for straight-line code with a shorter equivalent. Each rewrite below was found by enumeration and then proved for every possible input: a SAT solver written for this project shows that "the two sides differ" has no solution. An independent proof checker re-validates every step of that refutation. -O applies these rules. K stands for any constant, a and b for values on the stack, and x0 for a local variable.
| # | Stack code | Becomes | As an expression | Proof |
|---|
-O was built as a measured campaign. Each step started from a profile of where executed instructions went. Each one was kept only after it passed a gate: the test suite, 4,500 fresh fuzz programs run five ways, 34 injected bugs that must all be caught, every rule re-proved, and the self-hosted bootstrap. The result is 31% fewer instructions executed and 15–61% less wall time on 9 of 10 timed benchmarks. The price is up to 2.9× the memory. The full log, including the experiments that were rejected, is CAMPAIGN.md.
A compiler you can watch work
Lumen is a small, statically typed functional language. Everything on this page runs in your browser. The compiler is about 4,000 lines of dependency-free JavaScript. It infers every type, checks that every match is exhaustive, and emits a WebAssembly binary byte by byte. Programs that allocate carry their own garbage collector inside that binary.
Pipeline
- Lexer and parserRecursive descent with precedence climbing; list literals and
|>are desugared.src/lexer.js, parser.js · 526 lines - Type inferenceHindley–Milner with level-based generalization. Top-level functions are grouped into strongly connected components so they can appear in any order.types.js, typecheck.js · 722
- ExhaustivenessMaranget’s usefulness algorithm. It reports a concrete value that no arm handles, and warns about unreachable arms.exhaustive.js · 157
- LoweringA-normal form, closure conversion, lambda lifting, pattern-match compilation and type-directed root elision.lower.js · 355
- Code generationShadow-stack frames for GC roots,
return_callfor tail calls, static closures for capture-free functions.codegen.js · 422 - Optimizer (-O)Forwards temporaries and frame-slot loads, zeroes only the frame slots a collection could see unwritten, turns self tail calls into loops, and applies the superoptimizer's 40 proved rewrite rules.optimize.js, peephole.js · 376
- RuntimeBump allocator, Cheney copying collector with growing semispaces, structural equality and ordering, strings. Written as raw wasm instructions.runtime.js · 378
- Binary encoderLEB128, sections, label-depth resolution, a name section, and the WAT printer you see in the WebAssembly tab.wasm.js · 342
- Reference interpreterAn independent tree-walking evaluator over the typed AST, used as the oracle for differential testing.interp.js · 244
How it is verified
Golden tests cover every example. Semantic tests run each program five ways: in the interpreter, and as default and -O wasm, each with and without a collection on every allocation. The differential fuzzer (the Fuzzer tab) has checked more than 40,000 random programs. Its one disagreement was a real interpreter bug, now fixed. Mutation testing confirms that the fuzzer catches all 27 deliberately injected compiler and optimizer bugs.
Beyond the compiler
Self-hosting. The compiler is also written in Lumen, and it reaches a byte-identical fixed point when it compiles itself. See the Self-hosting tab. Proofs. A superoptimizer found the rewrite rules that -O uses, and each one is proved for every input by a SAT solver whose proofs an independent checker validates. See the Proved rules tab. Measurement. -O was built step by step, and every step passed the full test, fuzz, mutation, proof and bootstrap gate before it was kept. See the -O campaign tab.
Source
The compiler, tests, fuzzer, language spec and a model-comparison challenge are on GitHub.
The language in one breath
31-bit integers that wrap around, booleans, byte strings, tuples, and generic algebraic data types. Pattern matching with guards and nested patterns. First-class functions and closures. Structural == and < on any type except functions. A prelude of list, option and string functions, itself written in Lumen. Run it locally with npm run serve, or use the CLI: node bin/lumen.js run examples/huffman.lm --stats.