LeanDojo

repository·main·Indexed 21 days ago

https://github.com/lean-dojo/leandojo

A Python library for machine learning research in theorem proving within the Lean ecosystem. LeanDojo enables the extraction of proof states, tactics, and premises from Lean 3 and Lean 4 repositories and provides a programmatic interface (Dojo) for interacting with Lean theorems. Note: This version is deprecated; new projects should use LeanDojo-v2.

Tokens
14.8K
Snippets
49
Records
77
Agent score
74%

What's inside LeanDojo

  1. What is LeanDojo?

    main

    LeanDojo is a Python library designed for learning-based theorem provers in Lean. It supports both Lean 3 and Lean 4. The library provides two primary capabilities:

    1. Data Extraction: Extracting proof states, tactics, premises, and other relevant data from Lean repositories.
    2. Programmatic Interaction: Interacting with Lean programmatically to facilitate machine learning workflows.

    Note: This original LeanDojo library is deprecated. For all new projects, please use LeanDojo-v2.

  2. Understand the limitations of LeanDojo

    main

    When using LeanDojo for theorem proving and data extraction, be aware of the following technical constraints:

    • Repository Compatibility: LeanDojo cannot extract data from or interact with theorems within the official lean4 repository.
    • FFI Support: Lean repositories that utilize Foreign Function Interface (FFI), such as LeanCopilot, are currently unsupported.
    • Proof Types: LeanDojo does not support term-based proofs or proofs that mix tactics and terms.
    • Theorem Extraction Scope:
      • LeanDojo only extracts "syntactic theorems"—Lean constants defined specifically using the theorem or lemma keywords.
      • Constants defined using def are not extracted.
      • Extracted theorems are not guaranteed to be real theorems (they are simply Lean constants of type Prop).
  3. Understand the files generated by LeanDojo tracing

    main

    When you run trace on a Lean 4 repository, LeanDojo generates a directory structure where each *.lean file is accompanied by several auxiliary files. These files provide the necessary context for machine learning tasks:

    • *.olean: Lean's compiled object file.
    • *.dep_paths: Paths of dependencies imported by the current file.
    • *.ast.json: Abstract Syntax Trees (ASTs) exported by ExtractData.lean.
    • *.trace.xml: The most critical file, containing detailed syntactic and semantic information extracted from Lean (e.g., command nodes, theorem names, tactic sequences, and state changes).
  4. The structure of Traced Repositories

    main

    A traced repo is a collection of traced files forming a Directed Acyclic Graph (DAG) via imports. It includes the target repo and the specific dependencies used by it.

    Directory Structure (Lean 3)

    The root directory is the name of the traced repo. Dependencies are stored in _target/deps/:

    • [root]/src: Source files of the target repo.
    • [root]/_target/deps/lean/library: Lean's standard library.
    • [root]/_target/deps/[dependency_name]: Other dependencies like mathlib.

    Directory Structure (Lean 4)

    In Lean 4, dependencies are stored in the lake-packages directory instead of _target/deps.

    Traced repositories are represented by the lean_dojo.data_extraction.traced_data.TracedRepo class.

  5. Configure LeanDojo caching behavior

    main

    Tracing large repositories like mathlib is resource-intensive (e.g., ~1 hour with 32 CPUs and at least 32 GB RAM). LeanDojo caches traced repositories to allow fast access in the future.

    • Default Cache Location: ~/.cache/lean_dojo.
    • Custom Cache Location: Set the CACHE_DIR environment variable.
    • Read-Only Cache: Traced repos in the cache are read-only. You may need to use chmod to clean the cache, but avoid manual modifications while LeanDojo is running to prevent unpredictable behavior.
    • Remote Caching: LeanDojo can automatically download hosted repos from AWS S3 if they are missing from your local cache. To force LeanDojo to build all repositories locally and disable remote downloads, set the DISABLE_REMOTE_CACHE environment variable to any value.
    # Example: Setting a custom cache directory and disabling remote cache
    export CACHE_DIR="/path/to/your/custom/cache"
    export DISABLE_REMOTE_CACHE=1
  6. Understand Lean Repositories and Build Systems

    main

    Lean projects consist of .lean source files containing theorems and proofs.

    Lean 3

    Lean 3 repositories use a leanpkg.toml configuration file at the root. Key fields include:

    • lean_version: The required Lean version.
    • path: The directory for source files.
    • [dependencies]: Specific commits/versions of required libraries (e.g., mathlib).

    To compile a Lean 3 repo, use leanpkg build, which runs:

    1. lean configure: Pulls dependencies into _target/deps/.
    2. lean --make <path>: Compiles source files into .olean object files.

    Lean 4

    Lean 4 uses the lake build system. While the configuration and directory structures differ, the high-level concept of compiling source files into object files remains the same.

    [package]
    name = "lean-liquid"
    version = "0.1"
    lean_version = "leanprover-community/lean:3.48.0"
    path = "src"
    
    [dependencies]
    mathlib = {git = "https://github.com/leanprover-community/mathlib", rev = "5947fb69cc1fdfebaba1e1b1f0a04f26f0f612bf"}
  7. How LeanDojo works: Tracing and Dojo

    main

    LeanDojo facilitates machine learning for theorem proving through a two-step process:

    1. Tracing: A Lean repository is traced to extract rich syntactic and semantic information (tactic states, tactics, and premises) that is not readily available in the raw source code. This results in a collection of traced files, repos, and theorems.
    2. Dojo Interaction: Once traced, LeanDojo allows an external prover (like an ML model) to interact with a theorem by replacing the original human-written proof with a special repl tactic. This repl tactic reads tactics from the external prover, executes them in Lean, and reports the results back. The resulting environment is called a Dojo.

    This workflow allows machine learning models to 'practice' theorem proving by interacting with the Lean environment via the Dojo.

  8. Extract data from Lean 4 repositories

    main

    You can use LeanDojo to extract syntactic and semantic information from Lean 4 repositories. This process involves specifying a Git repository URL and a specific commit hash. The trace function will then process the repository and generate a directory containing the source files along with several metadata files that describe the structure and semantics of the Lean code.

    To perform a trace, use the LeanGitRepo class to define the repository and the trace function to execute the extraction.

    from lean_dojo import LeanGitRepo, trace
    
    # Define the repository with its URL and a specific commit hash
    repo = LeanGitRepo("https://github.com/yangky11/lean4-example", "7b6ecb9ad4829e4e73600a3329baeb3b5df8d23f")
    
    # Trace the repository and specify the destination directory
    trace(repo, dst_dir="traced_lean4-example")
  9. LeanDojo system requirements and environment setup

    main

    Before installing, ensure your system meets the following requirements:

    Supported Platforms

    • Linux
    • Windows WSL
    • macOS

    Software Dependencies

    • Git >= 2.25
    • Python: 3.9 <= Python < 3.13
    • wget
    • elan (Lean toolchain manager)

    Environment Variables

    To interact with GitHub repositories, you must generate a GitHub personal access token and set it as an environment variable:

    • GITHUB_ACCESS_TOKEN: Your GitHub personal access token.