Skip to main content

Interact with the Lean theorem prover language server

Project description

leanclient

Interact with the lean4 language server.

PyPI version last update license

Key FeaturesQuickstartCurrently in BetaDocumentationTestingLicense

leanclient is a thin wrapper around the native Lean language server. It enables interaction with a Lean language server instance running in a subprocess.

Check out the documentation for more information.

Key Features

  • Interact: Query and change lean files via the LSP.
  • Thin wrapper: Directly expose the Lean Language Server.
  • Synchronous: Requests block until a response is received.
  • Fast: Typically more than 99% of time is spent waiting.
  • Parallel: Easy batch processing of files using all your cores.

Quickstart

The best way to get started is to check out this minimal example in Google Colab:

Open in Colab

Or try it locally:

  1. Setup a new lean project or use an existing one. See the colab notebook for a basic Ubuntu setup.

  2. Install the package:

pip install leanclient
  1. In your python code:
import leanclient as lc

# Start a new client, point it to your lean project root (where lakefile.toml is located).
PROJECT_PATH = "path/to/your/lean/project/root/"
client = lc.LeanLSPClient(PROJECT_PATH)

# Query a lean file in your project
file_path = "MyProject/Basic.lean"
result = client.get_goal(file_path, line=1, character=2)
print(result)

# Use a SingleFileClient for simplified interaction with a single file.
sfc = client.create_file_client(file_path)
result = sfc.get_term_goal(line=1, character=2)
print(result)

# Use a LeanClientPool for easy parallel processing multiple files.
files = ["MyProject/Basic.lean", "Main.lean"]

# Define a function that takes a SingleFileClient as its only parameter.
def count_tokens(client: lc.SingleFileClient):
    return len(client.get_semantic_tokens())

with lc.LeanClientPool(PROJECT_PATH, num_workers=8) as pool:
    results = pool.map(count_tokens, files)

    # Or use pool.submit() for increased control.
    futures = [pool.submit(count_tokens, path) for path in files]
    res_fut = [f.get() for f in futures]

print(results)

Currently in Beta

  • Missing features.
  • Needs more testing with different setups.
  • Any feedback is appreciated!

Next Features

  • Documentation: Real examples

Potential Features

  • Use document versions to handle evolving file states
  • Automatic lean env setup for non Debian-based systems
  • Parallel implementation (multiple requests in-flight) like multilspy
  • Allow interaction before waitForDiagnostics returns

Missing LSP Interactions

Might be implemented in the future:

  • workspace/symbol, workspace/didChangeWatchedFiles, workspace/applyEdit, ...
  • textDocument/codeAction
  • textDocument/prepareRename, textDocument/rename

Internal Lean methods:

  • $/lean/ileanInfoUpdate, $/lean/ileanInfoFinal, $/lean/importClosure, $/lean/staleDependency
  • $/lean/rpc/connect, $/lean/rpc/call, $/lean/rpc/release, $/lean/rpc/keepAlive

Documentation

Read the documentation at leanclient.readthedocs.io.

Run make docs to build the documentation locally.

Testing

# python3 -m venv venv  # Or similar: Create environment
make install            # Installs python package and dev dependencies
make test               # Run all tests, also installs fresh lean env if not found
make test-profile       # Run all tests with cProfile

License

MIT

Citing this repository is highly appreciated but not required by the license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leanclient-0.1.0b5.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leanclient-0.1.0b5-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file leanclient-0.1.0b5.tar.gz.

File metadata

  • Download URL: leanclient-0.1.0b5.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for leanclient-0.1.0b5.tar.gz
Algorithm Hash digest
SHA256 8dec1deb8fd747e4e686cb84c608451b67841da7a7bab1bc1919ccdbfcaa91c8
MD5 591db8c77116d6411def1572bf96d9bc
BLAKE2b-256 03f3b355900eb25c66c1c1a1a394a102dd9314c0d2cd0ad35f975e97c1c6abf8

See more details on using hashes here.

File details

Details for the file leanclient-0.1.0b5-py3-none-any.whl.

File metadata

  • Download URL: leanclient-0.1.0b5-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.10.12 Linux/6.8.0-49-generic

File hashes

Hashes for leanclient-0.1.0b5-py3-none-any.whl
Algorithm Hash digest
SHA256 77010d59cba1d35ab74a7a32eec931e9f157dc127b5a9c4851b1701cfbf0ff9e
MD5 0209eff1262cd660765d61f599f69f03
BLAKE2b-256 d4b531418b9c32091db6ffb55c8fe0012d5927e6eaa64a83bda783a7e4c967ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page