A Quine in Lean

A quine is a program whose output is its own source code, byte for byte. Here is one in Lean 4, the functional programming language and proof assistant. Below it, a walk through why it works, and a small interpreter so you can break it and repair it.

The program

Save these two lines as Quine.lean, with a newline at the end of the second line, and run lean --run Quine.lean. The program prints these same two lines back.


  
This runs in a small interpreter in this page (see below). The real check was done with Lean.

Checked with Lean 4.35.0 on 9 November 2026: lean --run Quine.lean > out.txt; cmp Quine.lean out.txt reports no difference across all 168 bytes.

Why it is hard

The obvious attempt is a program that prints its own text. But the text it prints must contain the print statement, which contains the text, which contains the print statement, and so on:


  

Every layer of quoting adds a layer of program, and the regress never ends. A quine escapes by never quoting the whole program. It stores part of the program as data, then uses that data twice: once as a quoted string and once as plain code.

How it works

  1. The string s holds the rest of the file. Everything after the string literal on line 1 is stored in s: a newline, then all of line 2. It does not hold line 1's beginning, def s : String := , or the literal itself. Those are rebuilt from other parts.
  2. s.quote turns the data back into a literal. Lean's String.quote wraps a string in double quotes and escapes what must be escaped. Applied to s, it produces exactly the literal written on line 1, with \n for the newline and \" for each quote. This is the step that breaks the regress: one level of quoting is done by a function, not by hand.
  3. Three pieces are glued together. main prints the start of line 1 (a constant in the code), then s.quote (the rest of line 1), then s itself (line 2).
  4. IO.println adds the final newline. That is why the file has to end with a newline, as most text files do.

Here is the output, coloured by the expression that produced each piece:

The quoting, character by character

Top row: the characters of s. Bottom row: what s.quote writes for each one. The orange cells are where quoting changes a character. The first and last cells are the quotes that String.quote adds around the whole string.

What String.quote escapes

The rules come from Char.quoteCore in Lean's standard library (Init/Data/Repr.lean). Inside a string, Lean escapes these characters:

CharacterWritten as
newline (10)\n
tab (9)\t
backslash\\
double quote\"
other codes 0–31, and 127\x and two hex digits, for example \x0d for carriage return
everything else, including ', é, λ and emojias itself

For the quine to work, the literal on line 1 must be written exactly the way String.quote would write it. Lean would also accept \x22 or \u0022 for a quote, but String.quote writes \", and only that spelling reproduces the file.

Break it, then repair it

Edit the program below. The page runs it at once and compares the output with what you typed. Try the buttons for some typical ways to break it. Repair rebuilds the string on line 1 from the rest of the file, which is how this quine was written in the first place. With IO.print there is no newline at the end of the output, so Repair also drops the file's final newline.

The interpreter here understands only a small slice of Lean: def, let, #eval, string literals, ++, IO.println, IO.print, String.quote (as s.quote or repr s), s.replace, s.length, Char.ofNat and toString. Anything else is reported as unsupported rather than guessed. Its quoting follows Lean's rules exactly; the tests in test.mjs compare it with output from real Lean.

Two more Lean quines

On one line

The same idea fits on one line if the program carries a placeholder. Here the string has an @ where its own quoted copy belongs, and replace swaps it in:


  

This is a single line; it wraps here only to fit the page.

Why (Char.ofNat 64).toString instead of "@"? Everything in the code is also inside the string. If the code said "@", the string would contain an @ there too, and replace would substitute that one as well. Character 64 is @, but the code never spells it out, so the string holds exactly one @.

Printed by the checker

Lean evaluates #eval commands while it checks a file. So this version has no main at all: running lean Eval.lean to check the file prints the file.


  

Both variants were also checked with Lean 4.35.0 and cmp.

Letting Lean check the quine

Lean can also check the claim itself, by reading the file and comparing it with what the program builds. build t is the program's output with t in place of s. The quine claim is that build s equals the file:


  

With Quine.lean next to it, lean Check.lean prints true. The comparison runs at check time but still has to read a file, so it is a test, not a theorem: Lean's logic cannot see its own source files.

Where the trick comes from

Splitting a program into a template plus a quoted copy of that template is a diagonal construction. The same move appears in Kleene's second recursion theorem, which says every programming language with enough power has programs that can get hold of their own code. It also appears in the diagonal lemma behind Gödel's incompleteness theorems, where a sentence talks about its own number. A quine is the plainest example: the template is line 2, and s.quote is the quoting.

Run it yourself


  

Install Lean with elan. No Lake project is needed, since a single file runs with lean --run.