Datasets:
|
Download README.md from Debargha/leetcode-rust-lean: direct link, hf CLI and curl.
- Browser
- Download file 21.1 kB
-
https://huggingface.co/datasets/Debargha/leetcode-rust-lean/resolve/main/README.md
- Command line
-
hf download hf://datasets/Debargha/leetcode-rust-lean/README.md
-
curl -L -o README.md https://huggingface.co/datasets/Debargha/leetcode-rust-lean/resolve/main/README.md
21.1 kB
| license: mit | |
| pretty_name: LeetCode Rust-to-Lean (unofficial) | |
| language: | |
| - en | |
| task_categories: | |
| - text-generation | |
| tags: | |
| - code | |
| - rust | |
| - lean4 | |
| - formal-verification | |
| - theorem-proving | |
| - autoformalization | |
| - aeneas | |
| size_categories: | |
| - 10K<n<100K | |
| configs: | |
| - config_name: programs | |
| default: true | |
| data_files: | |
| - split: train | |
| path: data/programs/train.parquet | |
| - split: test | |
| path: data/programs/test.parquet | |
| - config_name: specs | |
| data_files: | |
| - split: train | |
| path: data/specs/train.parquet | |
| - split: test | |
| path: data/specs/test.parquet | |
| # LeetCode Rust-to-Lean (unofficial) | |
| > Unofficial. This dataset is an independent reproduction of the methodology of | |
| > [*Scaling Lean Theorem Proving for Real-World Code Verification*](https://arxiv.org/abs/2610.38174), | |
| > made by the paper's first author. It is not an official release of the paper. | |
| > If you use it, please cite the paper (BibTeX at the bottom). | |
| Theorem provers are now very good at competition mathematics, and the benchmarks for it are | |
| close to saturated. Proving that real code is correct is a different and much harder problem, | |
| and it is the one that matters for software. This dataset applies the methodology of the paper | |
| above from scratch and contributes 24,617 new theorems about real code for the community to | |
| prove. | |
| It contains 2,126 programs and 24,617 theorems about them: | |
| 1. Each program is a Rust solution to a LeetCode problem that passes every test case of the | |
| problem, together with its translation to Lean 4 by the [Aeneas](https://github.com/AeneasVerif/aeneas) | |
| tool. The Lean code is the program itself, with machine integers, overflow checks, loops and | |
| panics, not a clean mathematical model of it. The translation was run on test cases and | |
| reproduces the Rust program's answers. | |
| 2. Each theorem states one property of one of these programs, in natural language and in | |
| Lean 4: functional correctness, absence of failure, bounds, edge cases, relations between | |
| inputs and outputs, and more. Each program's theorems include one main theorem, which | |
| relates its result to the answer the problem asks for. | |
| The task is to prove the theorems. The release contains the statements only; the proofs are open. | |
| ## What it is for | |
| - Benchmarking provers on code. Every theorem is a self-contained, mechanically checked task. | |
| - Reinforcement learning. A proof is accepted or rejected by Lean's kernel, so every theorem is | |
| a reward signal that cannot be gamed by a judge model. | |
| - Autoformalization. Every theorem comes in natural language and in Lean, against the same | |
| program: 24,617 aligned pairs for learning to turn a specification into a formal statement. | |
| - Verified code generation. Every program comes with its problem, its test cases, its Lean | |
| translation and the full record of how it was written. | |
| ## The task | |
| You are given one program (its Lean translation) and one theorem about it, stated in Lean with | |
| `sorry` as its proof. Prove the theorem, or prove its negation. Every theorem comes with its | |
| negation already stated (`lean_negation`), so a refutation always targets the same statement. | |
| A solution is a copy of the program's specification file (`lean_specs`) in which that one | |
| theorem (or its negation) has a proof. You may add any definitions and lemmas you need before | |
| it, and you may add imports from the same Lean, Mathlib and Aeneas versions. You may not change | |
| the statement. The proof must not use `sorry`, and it may only use the standard axioms | |
| `propext`, `Quot.sound` and `Classical.choice`. This rules out `native_decide`, which adds an | |
| axiom of its own. | |
| The check is mechanical. Lean's built-in `lake comparator` compares the solution with the | |
| original statement, checks the axioms, and replays the proof in the Lean kernel (see "Checking a | |
| proof" below). Nothing is graded by hand or by a model. | |
| ## An example | |
| LeetCode 633, `sum-of-square-numbers`: given `c`, decide whether `c = a² + b²` for some integers | |
| `a` and `b`. The Rust program: | |
| ```rust | |
| pub fn judge_square_sum(c: i64) -> bool { | |
| let mut b: i64 = 0; | |
| while (b + 1) * (b + 1) <= c { | |
| b += 1; | |
| } | |
| let mut a: i64 = 0; | |
| while a <= b { | |
| let sum = a * a + b * b; | |
| if sum == c { | |
| return true; | |
| } | |
| if sum < c { | |
| a += 1; | |
| } else { | |
| b -= 1; | |
| } | |
| } | |
| false | |
| } | |
| ``` | |
| Its main theorem, `spec_1`, and the theorem that it never fails, `spec_2`: | |
| ```lean | |
| /-- Whenever judge_square_sum(c) returns a result r, r = true if and only if there exist integers a and b such that a*a + b*b = c. | |
| Preconditions: c is an integer with 0 <= c <= 2147483647 (2^31 - 1). -/ | |
| theorem spec_1 (c : Std.I64) (r : Bool) | |
| (hc0 : 0 ≤ c.val) (hc1 : c.val ≤ 2147483647) | |
| (hr : judge_square_sum c = ok r) : | |
| (r = true ↔ ∃ a b : ℤ, a * a + b * b = c.val) := by | |
| sorry | |
| /-- The function judge_square_sum(c) returns a result r; it does not fail, panic, or overflow. | |
| Preconditions: c is an integer with 0 <= c <= 2147483647 (2^31 - 1). -/ | |
| theorem spec_2 (c : Std.I64) | |
| (hc0 : 0 ≤ c.val) (hc1 : c.val ≤ 2147483647) : | |
| ∃ r : Bool, judge_square_sum c = ok r := by | |
| sorry | |
| ``` | |
| Together the two say that the program always returns, and that what it returns is the right | |
| answer. Neither says anything about how the program works. To prove `spec_1` you need an | |
| invariant for the two-pointer loop: it never skips a pair `(a, b)` that could sum to `c`. To | |
| prove `spec_2` you need to show that both loops terminate and that `(b + 1) * (b + 1)` and | |
| `a * a + b * b` never overflow 64 bits for any `c` in the range. The program has 6 more | |
| theorems, about witnesses, invariance and the relation between inputs and outputs. | |
| ## Why this is hard | |
| The Lean code is a faithful model of the Rust program, so a proof has to deal with everything | |
| the program does: | |
| - Integers are 64-bit machine integers. Every `+`, `*` and `-` can overflow, and the proof | |
| must show it does not. | |
| - Every operation that can panic in Rust (overflow, index out of bounds, division by zero) | |
| returns a `Result` in Lean, and the proof must rule the failure out or reason about it. | |
| - Loops become recursive functions. Each one needs an invariant and a termination argument. | |
| - Vectors are lists, mutation becomes passing updated copies, and the code is written for | |
| execution, not for proofs. | |
| - The theorems are stated against the problem, not the code. The main theorems relate the | |
| program to a mathematical definition of the answer (a maximum over all subsets, the existence | |
| of a valid ordering, the least number of insertions that makes a string a palindrome), so a | |
| proof must connect the algorithm to that definition. | |
| The paper that introduced this methodology found that tasks of this kind stay hard even for | |
| provers that nearly saturate competition benchmarks. For a sense of scale here: a machine-found | |
| proof of the main theorem of | |
| `maximum-tastiness-of-candy-basket`, a Medium problem solved by binary search with a greedy | |
| check, is about 1,000 lines of Lean with 44 lemmas and definitions. | |
| ## Quick start | |
| ```python | |
| from datasets import load_dataset | |
| programs = load_dataset("Debargha/leetcode-rust-lean", "programs") | |
| specs = load_dataset("Debargha/leetcode-rust-lean", "specs") | |
| s = specs["test"][0] | |
| print(s["task_id"], s["theorem"]) # e.g. shortest-distance-after-road-addition-queries-i AegisSpec.ShortestDistanceAfterQueries.spec_1 | |
| print(s["statement"]) # the property in natural language | |
| print(s["lean_statement"]) # the same property in Lean | |
| p = programs["test"].filter(lambda r: r["task_id"] == s["task_id"])[0] | |
| print(p["lean_specs"]) # the Lean file with every theorem of this program | |
| ``` | |
| ## The two subsets | |
| `programs` has one row per program. `specs` has one row per theorem. They join on `task_id`. | |
| ### `programs` | |
| | Column | What it is | | |
| |---|---| | |
| | `task_id`, `question_id` | LeetCode slug and number, e.g. `running-sum-of-1d-array`, `1480` | | |
| | `difficulty` | `Easy`, `Medium` or `Hard` | | |
| | `tags` | LeetCode topic tags | | |
| | `description` | the problem statement | | |
| | `leetcode_signature` | the original (Python-style) signature, e.g. `runningSum(nums: list_int) -> list_int` | | |
| | `rust_signature` | the Rust signature the solution implements, e.g. `pub fn running_sum(nums: &[i64]) -> Vec<i64>` | | |
| | `rust_solution` | the Rust program (safe Rust only; see "The Rust subset") | | |
| | `lean_function` | full Lean name of the translated function, e.g. `Aegis.RunningSum.running_sum` | | |
| | `lean_types` | Lean type definitions produced by Aeneas (`Types.lean`) | | |
| | `lean_funs` | Lean function definitions produced by Aeneas (`Funs.lean`). This is the main Lean file. | | |
| | `lean_funs_external` | Lean models of Rust library functions Aeneas cannot translate itself (e.g. `Vec::pop`, `sort`). Empty for most rows (non-empty for 168). | | |
| | `lean_specs` | the Lean file stating this program's theorems (see below) | | |
| | `num_specs` | number of theorems in `lean_specs` | | |
| | `lean_eval` | a Lean file that runs the Lean function on test cases and prints `CASE <i> true/false` | | |
| | `test_cases` | all test cases: list of `{input, output}` (JSON strings), sorted from smallest to largest input | | |
| | `num_test_cases` | how many test cases (3 to 450, median 97) | | |
| | `order_insensitive` | `true` if the problem says "return the answer in any order". Then outputs were compared as multisets (64 rows). | | |
| | `rust_cases_passed` | test cases the Rust passes. Always equal to `num_test_cases`. | | |
| | `lean_check` | result of running the Lean translation on the 10 smallest test cases: `verified` or `verified_partial` (see "How the programs were checked") | | |
| | `lean_check_cases`, `lean_check_cases_true` | how many cases that check used, and how many the Lean reproduced | | |
| | `num_llm_calls` | how many times the model was called to write this program (1 for 1,801 rows) | | |
| | `total_completion_tokens` | total tokens the model generated for it, reasoning included | | |
| | `trajectory` | the full history of how the program was written; see "The trajectory" | | |
| ### `specs` | |
| | Column | What it is | | |
| |---|---| | |
| | `task_id` | the program this theorem is about | | |
| | `spec_id` | `n` in `spec_n`, numbered in file order | | |
| | `theorem` | full Lean name, e.g. `AegisSpec.JudgeSquareSum.spec_1` | | |
| | `main` | `true` for the program's main theorem (exactly one per program) | | |
| | `category` | what kind of property it is (table below) | | |
| | `statement` | the property in natural language | | |
| | `preconditions` | the assumptions on the inputs, in natural language (usually the problem's constraints) | | |
| | `lean_statement` | the theorem in Lean, exactly as in `lean_specs` | | |
| | `lean_negation` | its negation, `theorem spec_n_neg : ¬ (∀ ..., ...)`, to prove if the theorem is false | | |
| | `difficulty` | difficulty of the problem | | |
| | `category` | Theorems | 24,617 (train 22,557 / test 2,060), on 2,124 programs | | |
| |---|---|---| | |
| | `relational` | 4,683 | how the output relates to the input or to itself (e.g. the output is a permutation of the input) | | |
| | `edge` | 4,429 | behaviour on special inputs (empty, single element, all equal, ...) | | |
| | `bounds` | 4,102 | the output lies in a range | | |
| | `invariance` | 3,015 | the output does not change under some change of the input (reversal, relabelling, ...) | | |
| | `functional` | 2,904 | the output is exactly a mathematically defined answer. All main theorems are here. | | |
| | `witness` | 2,140 | the output is achieved by some explicit object (a subset, a path, an index) | | |
| | `no_failure` | 2,100 | the program returns a result: no overflow, no out-of-bounds access, no panic | | |
| | `shape` | 1,183 | length, sortedness or structure of the output | | |
| | `monotonicity` | 61 | the output grows or shrinks with the input | | |
| ### The Lean specification file | |
| `lean_specs` is a complete Lean file. It imports the program (`import Aegis.<Problem>.Code.Funs`), | |
| defines the helper notions the theorems need, and states every theorem of the program inside | |
| `namespace AegisSpec.<Problem>`, each with its natural-language statement as its docstring and | |
| `sorry` as its proof. Some files also contain small helper lemmas with complete proofs. Every | |
| file in the dataset builds, and so does every theorem's negation. | |
| As in all Aeneas-based verification, the theorems are about the Lean translation of the program, | |
| with the definitions in `lean_funs_external` taken as given. | |
| ## Checking a proof | |
| Lay out a Lake project like this (`<Problem>` is the second component of `lean_function`): | |
| ``` | |
| lakefile.toml | |
| lean-toolchain leanprover/lean4:v4.36.0 | |
| config.json | |
| Aegis/<Problem>/Code/Types.lean lean_types | |
| Aegis/<Problem>/Code/Funs.lean lean_funs | |
| Aegis/<Problem>/Code/FunsExternal.lean lean_funs_external (only if non-empty) | |
| Proofs/Challenge.lean lean_specs, unchanged | |
| Proofs/Solution.lean your copy of lean_specs, with the proof | |
| ``` | |
| `lakefile.toml`: | |
| ```toml | |
| name = "aegis" | |
| [[lean_lib]] | |
| name = "Aegis" | |
| globs = ["Aegis.+"] | |
| [[lean_lib]] | |
| name = "Proofs" | |
| globs = ["Proofs.+"] | |
| [[require]] | |
| name = "aeneas" | |
| git = "https://github.com/AeneasVerif/aeneas.git" | |
| rev = "0c19cd5bff9298565362c6ca9c4f48a29033a0e9" | |
| subDir = "backends/lean" | |
| ``` | |
| `config.json`, naming the theorem you proved: | |
| ```json | |
| { | |
| "challenge_module": "Proofs.Challenge", | |
| "solution_module": "Proofs.Solution", | |
| "theorem_names": ["AegisSpec.JudgeSquareSum.spec_2"], | |
| "permitted_axioms": ["propext", "Quot.sound", "Classical.choice"] | |
| } | |
| ``` | |
| Then: | |
| ```bash | |
| lake update && lake exe cache get # fetch Aeneas and Mathlib, download the Mathlib cache | |
| lake build Proofs.Challenge Proofs.Solution | |
| lake comparator --config config.json # needs bwrap (Bubblewrap) on PATH | |
| ``` | |
| The last command prints `Your solution is okay!` when the proof is accepted. To refute a theorem | |
| instead, add its `lean_negation` right after the theorem in both files, prove it in | |
| `Solution.lean`, and name `spec_n_neg` in `config.json`. Several theorems of the same | |
| program can be checked at once by listing them all in `theorem_names`. | |
| ### How the theorems were written | |
| The theorems were written the way the paper describes, in three steps: | |
| 1. A model wrote 5 to 20 properties per program in natural language, from the problem | |
| statement, the function signature and examples, but not from the code. The properties | |
| describe what the program should do, not what it happens to do. | |
| 2. A second, independent review pass checked every property, fixed or removed unclear and | |
| redundant ones, and added the ones that were missing. | |
| 3. A Lean agent stated each property as a Lean theorem about the extracted program. | |
| Every resulting file was then checked mechanically: it builds from scratch, every property has | |
| its theorem, every theorem's negation elaborates, the only `sorry`s are the theorem proofs, and | |
| every theorem constrains the program's actual behaviour (statements that never mention the | |
| program or never use its result were removed). The model is DeepSeek-V4.1-Flash throughout. | |
| The theorems were fixed before any proof was attempted. A theorem that claims more than the | |
| program guarantees is solved by proving its negation. | |
| ## Evaluating a prover | |
| We suggest reporting results on the `test` split: 2,060 theorems about 175 programs. Report | |
| the fraction of theorems solved (a proof of the theorem or of its negation), overall and for the | |
| main theorems, by difficulty and by category, together with the attempt and time budget per | |
| theorem. The paper uses 30 minutes per theorem. A prover may read everything in the dataset | |
| row: the problem, the Rust code, the Lean code and the other theorems of the same program. | |
| ## How the programs were checked | |
| Rust: every program passes all of its test cases with `cargo test` (debug build, so an | |
| integer overflow counts as a failure, not a silent wrap-around). | |
| Lean: Aeneas translates the Rust program into Lean. We then run the Lean version with | |
| Lean's `#eval` on the 10 smallest test cases and check that it returns the same answers: | |
| - `verified`: the Lean reproduced all 10 (2,094 rows). | |
| - `verified_partial`: the Lean reproduced at least 3, never gave a wrong answer, and the | |
| rest were too slow or too big to finish (32 rows, mostly dynamic programming). | |
| Lean is slow on large inputs because its version of the program is written for proofs, not | |
| speed. Aeneas represents a Rust vector as a list (so `v[i]` takes time proportional to `i`) | |
| and every integer as a bit-vector with overflow checks. | |
| ## How to read the Lean code | |
| Aeneas turns each Rust function into a Lean function that returns a `Result`: | |
| ```lean | |
| def running_sum (nums : Slice Std.I64) : Result (alloc.vec.Vec Std.I64) := | |
| ... | |
| ``` | |
| - `Result α` is "either a value of type `α`, or an error". `ok x` means success with | |
| value `x`. `fail e` means the Rust program would have panicked: integer overflow, | |
| index out of bounds, division by zero, and so on. | |
| - `do` and `←` chain steps that can fail, like Rust's `?`. `let x ← f y` runs `f y`, | |
| stops with the error if it fails, and otherwise names the result `x`. | |
| - Machine integers are `Std.I64`, `Std.Usize`, `Std.U8`, ... `x.val` is the value of `x` as | |
| a mathematical integer, which is how the theorems talk about numbers. Literals look like | |
| `5#i64`. Arithmetic such as `x + y` returns a `Result`, because it can overflow. | |
| - `Slice α` is a Rust slice `&[α]`, and `alloc.vec.Vec α` is a Rust `Vec<α>`. Their | |
| contents as a plain Lean list are `s.val`. | |
| - Loops become two functions: `f_loop0.body` (one iteration) and `f_loop0` (the loop), | |
| built from `loop`, which repeats the body until it returns `done`. | |
| - Strings are byte arrays: Rust `&[u8]` / `Vec<u8>`, Lean `Slice Std.U8`. | |
| - Trees and linked lists are Rust enums, translated to Lean inductive types in `lean_types`. | |
| - `lean_funs_external` holds hand-written Lean models of the few Rust library functions | |
| Aeneas cannot translate (`Vec::pop`, `[T]::sort`, `Vec` comparison, the panic raised by | |
| `%` on zero). These are trusted definitions, not machine translations. | |
| ## The trajectory | |
| `trajectory` (in `programs`) is the list of events recorded while the program was produced. | |
| | `type` | Meaning | Main fields | | |
| |---|---|---| | |
| | `run_start` | start of an attempt. A retry says what failed before in `note`. | `note` | | |
| | `llm` | one call to the language model | `messages` (full input), `response`, `reasoning`, `finish_reason`, `prompt_tokens`, `completion_tokens`, `cached_prompt_tokens` | | |
| | `tool` | one tool run | `command`, `exit_code`, `timed_out`, `output`, `duration_ms` | | |
| | `llm_error` | an API error that was retried | `note` | | |
| | `run_end` | end of an attempt | `verdict`, `note` | | |
| `phase` tells you which step an event belongs to: `rust` (write the program and run | |
| `cargo test`), `extract` (translate with Charon and Aeneas), `lean` (build the Lean and | |
| evaluate it), or `lean_repair_rust` / `lean_repair_extract` (rewrite the program after the | |
| Lean failed to build). When a step fails, its error output goes back to the model, which | |
| writes a new version. The model is DeepSeek-V4.1-Flash at its default reasoning effort. | |
| ## The Rust subset | |
| Aeneas only translates part of Rust. The solutions therefore use plain code only: `i64`, | |
| `usize`, `bool`, `u8` values; slices and `Vec`s; `while`/`for` loops with explicit indexing; | |
| no `HashMap`/`HashSet`, no `String` (strings are `&[u8]` bytes), no closures or iterator | |
| chains, no `unsafe`. Backtracking is written with an explicit stack rather than recursion | |
| inside a loop. The code can look unusual compared to idiomatic Rust. | |
| ## Numbers | |
| | | | | |
| |---|---| | |
| | Programs | 2,126 (train 1,951 / test 175, the split of the source dataset) | | |
| | Difficulty | Easy 523, Medium 1,124, Hard 479 | | |
| | Theorems | 24,617 (train 22,557 / test 2,060), on 2,124 programs | | |
| | Theorems per program | 2 to 20, median 12 | | |
| | Theorems by difficulty | Easy 5,600, Medium 13,319, Hard 5,698 | | |
| | Test cases | 202,511 (3 to 450 per program, median 97) | | |
| | Median size | Rust 49 lines, Lean `Funs.lean` 172 lines | | |
| The train/test split follows the source dataset. Theorems inherit the split of their program, | |
| so no program or theorem appears in both splits. | |
| ## Versions | |
| Lean 4 `v4.36.0`, Aeneas `0c19cd5b`, Charon `5a2a17bd`, Mathlib at the revision Aeneas pins | |
| (`fc4f59e5`). | |
| ## Source and license | |
| Problems, statements and test cases come from | |
| [newfacade/LeetCodeDataset](https://huggingface.co/datasets/newfacade/LeetCodeDataset) | |
| (Apache-2.0). The problems themselves are from LeetCode. This dataset is released under | |
| the MIT license. | |
| ## Citation | |
| If you use this dataset, please cite: | |
| ```bibtex | |
| @misc{ganguly2026scalingleantheoremproving, | |
| title={Scaling Lean Theorem Proving for Real-World Code Verification}, | |
| author={Debargha Ganguly and Divya Raghunathan and Dejan Jovanović and Vikash Singh and Soonho Kong and Aaron Tomb and Aman Goel and Ferhat Erata and Sam Bayless}, | |
| year={2026}, | |
| eprint={2610.38174}, | |
| archivePrefix={arXiv}, | |
| primaryClass={cs.CL}, | |
| url={https://arxiv.org/abs/2610.38174}, | |
| } | |
| ``` | |