The Magic of Partial Evaluation jimmyhmiller.com · 44 points · 4 comments · HN t...

@best

The Magic of Partial Evaluation jimmyhmiller.com · 44 points · 4 comments · HN thread

TL;DR Jimmy Miller's essay explains partial evaluation: specializing a program on its statically known inputs, and how applying it to interpreters yields compilers (the first, second and third Futamura projections). He illustrates with factorial, a calculator, a brainfuck interpreter and a schema validator, and announces a small JavaScript partial evaluator he wrote in Rust.

  • Partial evaluation runs the parts of a program whose inputs are known and leaves residual code for the dynamic parts, e.g. power(process.argv[2], 5) reduces to five multiplications of argv[2].
  • Partially evaluating a brainfuck interpreter with a fixed brainfuck program yields compiled JavaScript for that program, with the interpreter removed.
  • The first Futamura projection: partial_eval(interpreter, prog) gives a compiled program.
  • The second projection: specializing the partial evaluator on an interpreter produces a compiler for that interpreter's language, shown with a toy prompt/print language.
  • The third projection: applying the partial evaluator to itself yields a compiler generator.
  • The author's Rust-based JavaScript partial evaluator is limited, is released for experimentation rather than production use, and reaches only partial versions of the second and third projections.

Discussion The thread is short, with four comments, and is mostly informal. Commenters mention related real-world work: a C++ to JavaScript/WebAssembly compiler that used similar techniques, and GraalVM's Truffle, which combines partial evaluation with a JIT. One commenter points out that the concept is essentially compile-time constant folding and raises a binary compatibility concern when it extends to function calls. The mood is appreciative and technical, with no real disagreement.

  • Partial evaluation is compile-time constant folding extended to function calls (a few): One commenter notes the idea is a familiar optimization under another name. They warn that inlining function calls this way breaks binary compatibility, since a changed callee is not picked up until callers are recompiled.
  • Related to production systems like Truffle/Graal and Cheerp (a few): Commenters point to existing practical uses: GraalVM's Truffle framework pairs partial evaluation with a JIT, and the Cheerp team applied similar ideas in a C++ to JavaScript/WebAssembly compiler.

Links from the comments

#partialEvaluation #compilers #interpreters #futamuraProjections #programmingLanguages