The Python of Formal Mathematics

10 min read Original article ↗

Experimental beta

A hobby project started in 2024. Litex is still experimental.

Litex

The Python of Formal Mathematics

Litex branching tree logo
Pronounced /ˈliːtɛk/ (LEE-tek), not LIE-tek.Named after Lisp and TeX. The binary tree grows from axioms into mathematics.

Pen sketch of Isaac Newton

“Truth is ever to be found in simplicity, and not in the multiplicity and confusion of things.”
— Isaac Newton

Math Is Code

Litex aims to be the Python of formal mathematics. It makes familiar mathematical expressions valid formal code. Write the mathematics, and let Litex explain the logic behind it. On selected measured common statements, its native verification is also 10–100× faster than Lean.

Why √2 is irrational

Assume √2 = p/q in lowest terms. Show that p and q must both be even, contradicting gcd(p, q) = 1.

Loading editor…

Litex Makes Verification Very Fast

On common mathematical statements, Litex’s native verification is 10–100× faster than Lean.

Guided by the philosophy of “proof as search,” Litex introduces an innovative verification model for formal languages: each mathematical statement acts as a key, and the proof context as a database specialized for mathematics, used to find the relevant evidence and verification criteria.

The meaning matters, too.

Lean / Mathlib · real division

(1 : ℝ) / 0 = 0

Mathlib defines real division at zero to return zero. In textbook real arithmetic, the expression is undefined. Definition

Litex checks the domain first: 1 / 0 is rejected before checking the equality.

This is a library convention, not a logical error or a requirement of type theory. Lean can also express explicit domain conditions.

Compare more examples

Subtraction depends on the number type

(1 : ℕ) - 2 = 0

Natural-number subtraction stops at zero; it does not produce an integer −1.

Litex checks 1 - 2 = -1.

Definition

A real square root of a negative input

Real.sqrt (-1) = 0

Undefined in textbook real arithmetic; Real.sqrt returns zero.

Litex requires x >= 0 and rejects sqrt(-1).

Definition

I know x ≥ 0. Why wrap x again?

⟨x, h⟩ : {y : ℝ // 0 ≤ y}

A subtype-based interface may require ⟨x, h⟩: the value paired with its proof. Knowing 0 ≤ x leaves x’s type as ℝ.

Litex uses the known condition x >= 0 directly when checking the argument.

Definition
Compare syntax, semantics, and foundations

Five characteristics of Litex

01

Fact-oriented

Write the next mathematical fact.

You choose the argument. Litex seeks a justification in the available definitions, facts, and rules.

02

Set-theoretic

Keep familiar mathematical meanings.

Work with sets, elements, functions, and relations. Check that expressions are defined before checking their conclusions.

03

Mathematical workflow

Explore, check, keep, and reuse.

In one live Session, an ordinary rejected step leaves earlier accepted definitions and facts available.

04

Human–AI feedback

People guide. AI proposes. Litex checks.

Structured feedback shows what was accepted and where the next proposal needs repair.

05

Lean-compatible · in progress

Carry supported proofs into Lean.

The compiler replays supported verification records as Lean proofs. It is partially implemented and runnable; see the examples and progress below.

Pen sketch of G. H. Hardy

“A mathematician, like a painter or a poet, is a maker of patterns.”
— G. H. Hardy

What Litex can do

With Litex, you can define mathematical objects, write proofs, and have the computer check each step. Results you have proved can support later calculations and proofs.

Define, prove, and reuse

Edit and run the geometry proof below. Inspect each checked step, its verification record, and the knowledge graph linking definitions and results.

Define an idea

Describe the objects, their properties, and the conditions under which they can be used.

Write and check

Write a statement or proof, run the check, and revise it using the feedback.

Continue the proof

Use earlier accepted definitions and facts in later steps, wherever they remain in scope.

Save and reuse

Save definitions and theorems in files so other files in the project can use them.

Loading editor…

Think, write, and check as you go.

Keep one Session open with -session. A rejected equality leaves the earlier value of a available for the next check.

Terminal REPLRecorded terminal Session · selected JSON fields

litex -strict -lang en -session -e 'have a R = 2'

litex> a = 3
{"run_result": "failure", "session_terminated": false}
litex> a + 1 = 3
{"run_result": "success", "session_terminated": false}

Keep today's results for the next exploration.

Prove in newton.lit that a positive number whose square is 2 stays unchanged by a Newton step. Then use that theorem for √2 in example.lit.

example.litUse the theorem from newton.lit

release obj def newton::newton_sqrt_two
release thm newton::fixed_point(sqrt(2))
View the project files · definition, proof, and configuration

newton.litNewton step and fixed-point theorem

claim:
    ? forall x R+:
        (x + 2 / x) / 2 $in R+
    2 / x > 0
    x + 2 / x > 0
    (x + 2 / x) / 2 > 0

have fn newton_sqrt_two(x R+) R+ = (x + 2 / x) / 2

thm fixed_point:
    ? forall x R+:
        x^2 = 2
        =>:
            newton_sqrt_two(x) = x
    x = x * x / x = x^2 / x = 2 / x
    newton_sqrt_two(x) = (x + 2 / x) / 2 = (x + x) / 2 = x

litex.configLoad the library before the example

[export]
newton = "./newton.lit"
example = "./example.lit"

These files belong to one project. The prefix newton:: identifies a definition or theorem from newton.lit.

Turn Litex proofs into LaTeX.

Litex emphasizes readable mathematical writing. It can turn definitions, statements, and proof steps into LaTeX for notes, teaching materials, and papers. Here, a short proof shows how the Newton step for √2 turns the old error into its square divided by 2x. Compare the checked source with the generated mathematical text.

LitexChecked mathematical source

have r R+ = sqrt(2)
have fn newton(x R+) R = (x + 2 / x) / 2

claim:
    ? forall x R+:
        newton(x) - r = (x - r)^2 / (2 * x)
    r^2 = 2
    (x - r)^2 = x^2 - 2 * x * r + r^2 = x^2 - 2 * x * r + 2
    newton(x) - r = (x^2 - 2 * x * r + 2) / (2 * x) = (x - r)^2 / (2 * x)

Let r∈R>0r\in \mathbb{R}_{>0} satisfy r=2r = \sqrt{2}.

Definition newton.

Let newton:R>0→R\mathit{newton} : \mathbb{R}_{>0}\to \mathbb{R}.

newton(x)=x+2x2\mathit{newton}(x) = \frac{x + \frac{2}{x}}{2}

Claim.

For every x∈R>0x\in \mathbb{R}_{>0}, we have newton(x)−r=(x−r)22⋅x\mathit{newton}(x) - r = \frac{\left(x - r\right)^{2}}{2 \cdot x}.

Proof.

r2=2r^{2} = 2 (x−r)2=x2−2⋅x⋅r+r2=x2−2⋅x⋅r+2\left(x - r\right)^{2} = x^{2} - 2 \cdot x \cdot r + r^{2} = x^{2} - 2 \cdot x \cdot r + 2 newton(x)−r=x2−2⋅x⋅r+22⋅x=(x−r)22⋅x\mathit{newton}(x) - r = \frac{x^{2} - 2 \cdot x \cdot r + 2}{2 \cdot x} = \frac{\left(x - r\right)^{2}}{2 \cdot x}

LaTeX conversion handles presentation; a separate verification step checks whether the proof is valid.View LaTeX conversion on GitHub

Further down, see examples of generating executable code and translating Litex proofs for Lean to check.

Explore code generation and Lean compilation

Pen sketch of Ludwig Wittgenstein

To understand is to see connections.
— Ludwig WittgensteinAdapted from Philosophical Investigations, §122.

How Litex works

You choose the mathematical constructions and key intermediate steps. Litex handles the supported local checks; those steps build up a larger proof.

A database designed for mathematics

Litex records the mathematics behind your code: definitions, accepted facts, equality relationships, and their scopes. This indexed knowledge forms the context that later proofs can query.

A proof-search engine for mathematical facts

The engine matches a goal against this context and hundreds of builtin mathematical rule branches (bt rules). It checks the required premises and domain conditions, then makes accepted conclusions available for later steps. Search is bounded; authors still supply the main mathematical ideas.

Litex verification pipeline: a candidate fact passes through well-definedness and bounded local verification, supported by scoped knowledge and builtin rules. Success stores, infers and commits knowledge; failure preserves earlier context. The inset shows direct evidence, known properties, builtin rules, strategies, definitions and forall, and permitted rewriting.

To check a statement, Litex first checks that its expressions are meaningful, then looks for justification in the available definitions, facts, and rules. A statement that passes can be used in later steps. If it fails, Litex reports where the check stopped and keeps earlier accepted results available.

Open original English diagram

See why each step holds.

Each step can use any available fact in its scope, or be established directly by a builtin rule. It need not follow only from the immediately preceding line.

When a builtin rule is used, the record gives its name, arguments, and premise proofs.

The Lean compiler replays supported checks as Lean proof steps. Each verification route needs a faithful mapping; integration is ongoing.

Explore how Litex works

Pen sketch of Richard Feynman

Math for AI

Give AI a language for checkable mathematical work.

People set the task. AI writes. Litex checks.

Follow a reciprocal proof from a missing condition to success. Click each step to inspect the code and feedback.

Litex agent pipeline: a natural-language reciprocal problem, AI proposals, verification-record feedback and repair, retained facts, a checked Litex file, and illustrative Python, dependency graph, LaTeX and Lean outputs.

A reciprocal proof, repaired from feedback. Output previews are illustrative.

YOUR MATHEMATICAL TASK

Define the reciprocal on nonzero real numbers and prove that multiplying it by its argument gives one.

01 · Start a session

Open one Session.

Run with -session and keep the process open while AI adds or repairs statements.

litex -strict -lang en -session

First accepted definitionOne statement in the live Session

have fn reciprocal(x R: x != 0) R = 1 / x

Litex responseActual interactive success response

success

The definition is accepted. Save it before moving on.

In an expanding set of formalization experiments, Litex has shown advantages in interacting with AI agents on some mathematical tasks.

Pen sketch of Alan Kay

“The best way to predict the future is to invent it.”
— Alan Kay

What is next?

Generate executable code, compile supported proofs to Lean, and learn through mathematical examples.

From checked mathematics to executable code.

Generate Python for a Newton step toward √2, restarting at 1 when the input is zero.

LitexChecked computational definition

algo newton_step(x R) R by cases:
    case x = 0: 1
    case x != 0: (x + 2 / x) / 2

PythonGenerated by the current CLI

def newton_step(x):
    if x == 0.0:
        return 1.0
    elif x != 0.0:
        return ((x + (2.0 / x)) / 2.0)
    raise AssertionError("unreachable verified Litex cases")

Executed: 1.0 → 1.5 → 1.4166666667.

Experimental extraction for supported numeric definitions, with Python and C99 targets. Generated code uses floating-point arithmetic.View Python / C extraction on GitHub

Let Lean recheck a Litex proof.

For supported proofs, the compiler turns verification records into Lean proofs for independent checking.

Litex’s large Rust core handles proof search; Lean’s small kernel offers an independent check of the generated proof.

Code-size comparison and sources

Litex exceeded 200,000 tracked Rust lines under src/ on 15 October 2026, including tests, comments, and blank lines. Lean’s 2015 paper reported about 6,000 lines for its first kernel layer, not today’s Lean 4. These counts cover different scopes; a small kernel aids review but does not guarantee correctness.

LitexWhat you write in Litex

1 = 1

Ordinary LeanTypical handwritten style

example : (1 : ℂ) = 1 := by
  rfl

Lean from LitexCompiler output · excerpt

import Litex

theorem __fact0 : Litex.Same (1 : ℂ) (1 : ℂ) := by
  exact Litex.Same.refl (1 : ℂ)
The generated Lean comes from an earlier compiler experiment. The JSON is an excerpt; compilation uses full execution results. Integration is ongoing.
How compilation uses the verification record

LitexChecked mathematical source

1 = 1

Execute

Verification recordExecution feedback · excerpt

{
  "statement": "1 = 1",
  "statement_result": "accepted",
  "proof_method": {
    "type": "they_are_the_same",
    "rule_name": "Same object"
  }
}

Compile evidence

Lean from LitexEarlier compiler output · excerpt

import Litex

theorem __fact0 : Litex.Same (1 : ℂ) (1 : ℂ) := by
  exact Litex.Same.refl (1 : ℂ)

Mathematics textbooks, from school to university.

Planned lessons span school mathematics and core undergraduate subjects. Browse the examples available so far.

Explore mathematics

A wider horizon

Return to the Essence of Mathematics. Explore the Boundaries of Language.

Pen sketch of Carl Friedrich Gauss

“Mathematics is the queen of the sciences.”

— Carl Friedrich Gauss

Language carries mathematical knowledge. Litex aims to let everyone who uses mathematics read, check, and help build that knowledge.

Litex organizes proof as search: mathematical statements become lookup keys for finding the required conditions and verification rules in a database specialized for mathematics. Rather than organize proof computation around a general-purpose lambda calculus, it aims to reduce the extra layers needed to express everyday mathematics.