Getting Started with LeanInteract¶
This guide will help you take your first steps with LeanInteract and understand its core concepts.
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
- Save and restore proof states
Quick Example¶
Here's a minimal example to help you get started:
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)
Build completed successfully.
CommandResponse(env=0)
This will:
- Initialize a Lean REPL configuration
- Start a Lean server
- Execute a simple Lean theorem
- Return a response containing the Lean environment state and any messages
Understanding Core Components¶
Let's break down the key components:
LeanREPLConfig¶
LeanREPLConfig
sets up the Lean environment:
config = LeanREPLConfig(
lean_version="v4.19.0", # Specify Lean version (optional)
verbose=True, # Print detailed logs
)
Build completed successfully.
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¶
Now that you understand the basics, you can:
- 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.