Getting Started with LeanInteract¶
Overview¶
LeanInteract provides a Python interface to the Lean 4 theorem prover via the Lean REPL (Read-Evaluate-Print Loop). It enables you to:
- Execute Lean code from Python
- Process Lean files
- Interact with proofs step by step
Quick Example¶
from lean_interact import LeanREPLConfig, LeanServer, Command
# Create a Lean REPL configuration
config = LeanREPLConfig(verbose=True)
# Start a Lean server with the configuration
server = LeanServer(config)
# Execute a simple theorem
response = server.run(Command(cmd="theorem ex (n : Nat) : n = 5 → n = 5 := id"))
# Print the response
print(response)
This will:
- Initialize a Lean REPL configuration: downloads and initializes the Lean environment
- Start a Lean server
- Execute a simple Lean theorem
- Return a response containing the Lean environment state and any messages
Core Components¶
LeanREPLConfig¶
LeanREPLConfig sets up the Lean environment:
config = LeanREPLConfig(
lean_version="v4.19.0", # Specify Lean version (optional, default is latest)
verbose=True, # Print detailed logs
)
LeanServer¶
LeanServer manages communication with the Lean REPL:
A more robust alternative is AutoLeanServer, which automatically recovers from (some) crashes:
Commands¶
LeanInteract provides several types of commands:
Command: Execute Lean code directlyFileCommand: Process Lean filesProofStep: Work with proofs step by step using tactics
Basic command execution:
Next Steps¶
- Learn about basic usage patterns
- Explore tactic mode for step-by-step proof interaction
- Configure custom Lean environments
Or check out the API Reference for detailed information on all available classes and methods.