Pangram verdict · v3.3
We believe that this entire text is human-written.
AI likelihood · overall
HumanArticle text · 1,474 words · 1 segments analyzed
This article is crossposted between Prof. Adam Chlipala’s blog Structure and Guarantees and the blog of CSAIL, the MIT lab where the research being discussed took place.I’m in the home stretch of a series of articles on constructing correct and efficient programs step-by-step using proven rewrite rules, which I argue is an important alternative to the ways AI coding agents are being used widely today. Previous examples have included tensor programs, digital hardware, and low-level cryptographic software. The last article introduced e-graphs, a data structure and set of related algorithms for efficiently exploring many alternative ways to write algebraic expressions. That technology has proven very useful for both program verification and program optimization.I’m going to give an example now of this approach in action, in hot-off-the-presses research. Our paper to appear at POPL 2027 in January in Mexico City uses e-graphs to automate formal verification of compilers. It seems to be the first example of a general proof-automation tool that can take in a compiler definition for a new programming language the tool has never seen before, with no extra annotations meant just to guide theorem proving, and prove it correct automatically. (There are past examples that applied to fixed programming languages, as in Cobalt and Rhodium.) The final version of the paper isn’t quite posted as of this writing, but you can find the details in first author Dustin Jamner’s PhD dissertation. Undergraduate Tilek Askerbekov also contributed to this paper.First I need to level with you about what kind of compiler we are talking about. It’s not one that looks especially like GCC or Clang, the canonical ways to resolve that word in industry. Instead, let me welcome you to the mathematically intriguing parallel universe of compilers for functional programming languages. The compilers our research project deals with follow in the tradition of classic research paper “From System F to Typed Assembly Language”. I’m not exactly an unbiased evaluator of this set of ideas, having done a postdoc with faculty author of the paper Greg Morrisett and undergraduate research with his former PhD advisor Bob Harper. Let’s take a quick look, though, at how this kind of compiler works. The idea is that compilation is broken into stages, and every intermediate form of a program is checked in a sound type system that guarantees lack of crashes, which is very different from classic compilers that quickly drop down to intermediate languages where all safety bets are off.To avoid gratuitous dependencies on functional-programming lore, I’ll use an example program from Python, which we want to compile, gradually, until it becomes sufficiently like assembly language. Our starting program is a nice simple one.def square(x): return x * x def compute(x): return square(x + 1) + 3 print(compute(4))Programmers generally take for granted the ability for one function to call another, which calls another, and so on. This freedom is usually implemented behind the scenes with a call stack. Our first compiler transformation will make the call stack explicit. In fact, the call stack will manifest as a chain of first-class functions, with each calling the function for the next lower level of the stack. This transformation is conversion to continuation-passing style, where we use “continuation” to refer to the functions we are introducing.def square(x, k): k(x * x) def compute(x, k): def after_square(y): k(y + 3) square(x + 1, after_square) compute(4, print)Notice what happened: each function now takes a continuation k as an explicit argument. Where a function would have returned, it instead calls its continuation with the result. Now no function (beyond certain primitives) ever returns, which lines up well with how hardware execution actually works. The call stack was just an opt-in fiction that feels natural to us because our brains evolved to understand natural language with similar structure (a soap box I’ll climb back onto in a future article or two, to explain some downsides for practical programming of giving this fiction too much special status). This example also depends somewhat on tail-call optimization for its reasonableness, which Python doesn’t actually provide, but let’s imagine that it did.What about this code is still too distant from what hardware can support directly? The function definition of after_square casually mentions a variable k from the enclosing scope. The term of art is that after_square is a lexical closure that captures variable k. This kind of mechanism is rather remote from what is natural to implement in hardware, so we also want a closure conversion compiler phase that removes use of captured variables, instead changing those variables into explicit function arguments, packaged up in first-class environments. The result for our running example is:def invoke(closure, value): code, env = closure code(env, value) def square(env, x, k): invoke(k, x * x) def after_square(env, y): k = env["k"] invoke(k, y + 3) def compute(env, x, k): continuation = (after_square, {"k": k}) square({}, x + 1, continuation) def print_result(env, value): print(value) initial_continuation = (print_result, {}) compute({}, 4, initial_continuation)Every place a first-class function value appeared before, we now see an explicit closure, a pair of a function reference and an environment dictionary that packages up all of the captured variables that function is allowed to access. A useful property of this code is that all control flow, or code that determines which other code should run, now uses features that map fairly directly to assembly language. In contrast, program features like nested function definitions with variable capture wind up looking rather different in assembly. The generated code still uses data structures like tuples and dictionaries, so further compilation phases are needed to explain those features with more-primitive constructs, but I’ll cut off the example here.The paper I cited earlier in this section associates such programs and transformations with static types, which can be checked to ensure absence of crashes and other problems. It would be too much of a digression for me to review the type-system features needed to handle everything we’ve just seen in code, but the types are also a central part of our new verification flow.This kind of compiler is already hard enough to get right, but our new result is based on the framework from the first half of Dustin’s PhD thesis, Pyrosome, which shows how to define programming languages and their formally verified compilers modularly. That is, with Pyrosome, we can define language-feature modules that explain features worth adding to programming languages, tagged with snippets of compiler code that should be used to implement those features. More importantly, a module can be formally verified in isolation. The upshot is that any set of modules compatible in a certain syntactic sense can be linked together to form a full programming language with its compiler ready to go, getting a correctness theorem for that compiler “for free.”The challenge of this setting is to allow verifying snippets of compilers in a way that guarantees correct behavior when they are linked together with arbitrary other snippets, up to the framework-level rules we set about what makes a valid feature module and when linking is permitted.Pyrosome is organized in a way very compatible with the worldview of rewrite rules and logical guarantees that I’ve been pushing. Here is the basic anatomy of a language-feature module. (I will take some liberties simplifying syntax and other aspects for this article.)The most fundamental thing a module does is extend a grammar of syntactic language features, which I’ll write here using syntax inspired by Backus-Naur form. Here is how a module introducing length-two tuples would extend a grammar of expressions.exp ::= ... | (exp, exp) | exp.1 | exp.2We should also introduce a new shape of type for tuples and give typing rules for tuple expressions, where I write e : t for the fact that expression e has type t. Rules are considered to quantify implicitly over all of the variables they mention.type ::= ... | type * type rule: e1 : t1 && e2 : t2 -> (e1, e2) : t1 * t2 rule: e : t1 * t2 -> e.1 : t1 rule: e : t1 * t2 -> e.2 : t2The next two ingredients are the most interesting for how compiler correctness is formulated in Pyrosome. First, a module declares equational rules that explain how expressions evaluate. Providing these rules is, in some sense, extra work to help the automated theorem prover, but the rules also feed into the definition of correctness for a compiler. We would need them just to make clear what the theorem statement is. Here a v variable enforces that a subterm is a value, e.g. a constant of some primitive type. This restriction is important to keep the following equations from dropping subterms that produce important side effects; even if the language-feature module we are writing involves no direct use of side effects, it might be linked later with one that does.rule: (v1, v2).1 = v1