Check Lean 4 code at scale ⚡️
This project serves the [Lean REPL](https://github.com/leanprover-community/repl) using FastAPI. It supports parallelization to check Lean 4 proofs at scale. A Python SDK simplifies interaction with the server's API. Read the [Technical Report](./Technical_Report.pdf) for more details. ## Table of Contents - [Server](#server) - [Client](#client) - [Contributing](#contributing) - [License](#license) - [Citation](#citation) This repository contains the source code for: - the Kimina server - the Kimina client to interact with it ## Server From source with `requirements.txt` (option to use `uv`, see [Contributing](#contributing)): ```sh cp .env.template .env # Optional bash setup.sh # Installs Lean, repl and mathlib4 pip install -r requirements.txt pip install . prisma generate python -m server ``` > [!NOTE] > Make sure `mathlib4` and `repl` exist in the workspace directory before launching the server from source. Or with `docker compose up` (pulls from Docker Hub). Equivalent run command is: ```sh docker run -d \ --name server \ --restart unless-stopped \ --env-file .env \ -p 80:${LEAN_SERVER_PORT} \ projectnumina/kimina-lean-server:2.0.0 ``` To shut down the container / view logs: ```sh docker compose down docker compose logs -f ``` Build your own image with specific Lean version with: ```sh docker build --build-arg=LEAN_SERVER_LEAN_VERSION=v4.21.0 . ``` Test it works with a request: ```sh curl --request POST \ --url http://localhost/verify \ --header 'Content-Type: application/json' \ --data '{ "codes": [ { "custom_id": "1234", "proof": "#check Nat" } ], "infotree_type": "original" }' | jq ``` Or use the client below. ## Client From [PyPI](https://test.pypi.org/project/kimina-client/): ```sh pip install kimina-client ``` Example use: ```python from kimina_client import KiminaClient client = KiminaClient() # Defaults to "http://localhost:8000", no API key client.check("#check Nat") ``` Or from source with `pip install -e .` ## ⚙️ Environment Variables | Variable | Default | Description | | ------------------------------------- | ------------- | ------------------------------------------------------ | | `LEAN_SERVER_HOST` | `0.0.0.0` | Host address to bind the server | | `LEAN_SERVER_PORT` | `8000` | Port number for the server | | `LEAN_SERVER_LOG_LEVEL` | `INFO` | Logging level (`DEBUG`, `INFO`, `ERROR`, etc.) | | `LEAN_SERVER_ENVIRONMENT` | `dev` | Environment `dev` or `prod` | | `LEAN_SERVER_LEAN_VERSION` | `v4.26.0` | Lean version | | `LEAN_SERVER_MAX_REPLS` | CPU count - 1 | Maximum number of REPLs | | `LEAN_SERVER_MAX_REPL_USES` | `-1` | Maximum number of uses per REPL (-1 is no limit) | | `LEAN_SERVER_MAX_REPL_MEM` | `8G` | Maximum memory limit for each REPL (Linux-only) | | `LEAN_SERVER_MAX_WAIT` | `60` | Maximum wait time to wait for a REPL (in seconds) | | `LEAN_SERVER_INIT_REPLS` | `{}` | Map of header to REPL count to initialize with | | `LEAN_SERVER_API_KEY` | `None` | Optional API key for authentication | | `LEAN_SERVER_REPL_PATH` | `repl/.lake/build/bin/repl` | Path to REPL directory, relative to workspace | | `LEAN_SERVER_PROJECT_DIR` | `mathlib4` | Path to Lean 4 project directory, relative to workspace | | `LEAN_SERVER_DATABASE_URL` | | URL for the database (if using one) | `LEAN_SERVER_MAX_REPL_MEM` can help avoid certain OOM issues (see Issue #25) The server also runs all commands with `"gc": true` to automatically discard environments which helps limit memory usage. ## 🚀 Performance Benchmarks You can run benchmarks with the Kimina client on any HuggingFace dataset: the benchmark run expects `id` and `code` columns in the dataset, but you can select your own column names. Example with [Goedel-LM/Lean-workbook-proofs](https://huggingface.co/datasets/Goedel-LM/Lean-workbook-proofs): ```python from kimina_client import KiminaClient client = KiminaClient() client.run_benchmark(dataset_name="Goedel-LM/Lean-workbook-proofs", n=1000, batch_size=8, max_workers=10) ``` If running benchmarks using the synchronous client (`KiminaClient` instead of `AsyncKiminaClient`) from an end-user computer, you may face the following error: > tenacity.before_sleep:log_it:65 - Retrying **main**.Lean4Client.\_query.