Pytanque is a Python API for lightweight communication with the Rocq proof assistant via coq-lsp.
- Pytanque is a lightweight Python client for Petanque
- Petanque is part of coq-lsp and provides a machine-to-machine protocol based on JSON-RPC to communicate with the Rocq prover.
- pet-server is the command that starts a Petanque server which can interpret requests for the Rocq prover
- Interactive theorem proving: Execute tactics and commands step by step
- Comprehensive feedback: Access all Rocq messages (errors, warnings, search results)
- AST parsing: Get abstract syntax trees for commands and file positions
- State management: Navigate proof states and compare them
- Position-based queries: Get states at specific file positions
First, install coq-lsp with the required dependencies:
# Install dependencies
opam install lwt logs coq.8.20.0
# Pin the correct version of coq-lsp
opam pin add coq-lsp https://github.com/ejgallego/coq-lsp.git#v8.20
We recommend using a virtual environment. With uv
:
uv venv
uv pip install -e .
source .venv/bin/activate
Or with standard Python:
python -m venv .venv
source .venv/bin/activate
pip install -e .
$ pet-server # Default port: 8765
# Or specify a custom port:
$ pet-server -p 9000
from pytanque import Pytanque
with Pytanque("127.0.0.1", 8765) as client:
# Start a proof
state = client.start("./examples/foo.v", "addnC")
print(f"Initial state: {state.st}, finished: {state.proof_finished}")
# Execute tactics step by step
state = client.run(state, "induction n.", verbose=True)
state = client.run(state, "auto.", verbose=True)
# Check current goals
goals = client.goals(state)
print(f"Current goals: {len(goals)}")
You can quickly try a similar example with:
python examples/foo.py
See also the notebook examples/getting_started.ipynb
for more examples.
start(file, theorem)
: Begin a proof sessionrun(state, command)
: Execute tactics or commandsgoals(state)
: Get current proof goalspremises(state)
: Get available premises/lemmas
ast(state, text)
: Parse command to ASTast_at_pos(file, line, char)
: Get AST at file positionget_state_at_pos(file, line, char)
: Get proof state at positionget_root_state(file)
: Get initial document statestate_equal(st1, st2, kind)
: Compare proof statesstate_hash(state)
: Get state hashtoc(file)
: Get table of contents
All commands return states with feedback containing Rocq messages:
state = client.run(state, "Search nat.")
for level, message in state.feedback:
print(f"Level {level}: {message}")
You can launch all the tests with pytest.
pytest -v .
The complete API documentation is available at: https://llm4rocq.github.io/pytanque
To build the documentation locally:
# Install documentation dependencies
poetry install --with docs
# Build the documentation
cd docs
poetry run sphinx-build -b html . _build/html
# Open the documentation
open _build/html/index.html # macOS
# or
xdg-open _build/html/index.html # Linux
We use atdpy to automatically generate serializers/deserializers from protocol type definitions. These types definition should match the ocaml code of petanque in coq-lsp.
To add a new method:
- Add type definitions in
protocol.atd
- Run
atdpy protocol.atd
to generateprotocol.py
- Update
client.py
with the new method
pytanque/
├── client.py # Main Pytanque client class
├── protocol.py # Auto-generated protocol definitions
├── __init__.py # Package exports
tests/
├── conftest.py # Shared test fixtures
├── test_unit.py # Unit tests for individual methods
├── test_integration.py # Integration and workflow tests
examples/
├── foo.py # Basic usage example
├── getting_started.ipynb # Jupyter notebook tutorial
└── foo.v # Example Coq/Rocq file
Server Connection Errors
- Ensure
pet-server
is running:pet-server -p 8765
- Verify port availability
File Path Issues
- Use absolute paths or ensure working directory is correct
- Check file exists and has proper Coq/Rocq syntax
Installation Issues
- Ensure coq-lsp is properly installed
- Install
lwt
andlogs
beforecoq-lsp
(required forpet-server
)