A Machine-to-Machine Interaction Interface for Lean 4
Go to file
Leni Aniva d7457cd386 Merge pull request 'fix: Capture nested tactic failure' (#135) from bug/nested-tactic-failure into dev
Reviewed-on: #135
2024-12-07 18:51:25 -08:00
Pantograph fix: Only signal failure when there is error 2024-12-06 00:08:20 -08:00
Test chore: Cleanup old `TestM` 2024-12-05 22:16:20 -08:00
doc Merge branch 'dev' into doc/rationale 2024-12-05 17:23:32 -08:00
.gitignore doc: Remove outdated documentation 2024-12-05 17:18:35 -08:00
LICENSE doc: Change license to Apache2 2024-10-21 09:59:13 -07:00
Main.lean feat: Catch IO errors in json format 2024-10-08 00:45:58 -07:00
Pantograph.lean feat: Environment pickling 2024-11-08 14:49:49 -08:00
README.md Merge branch 'dev' into doc/rationale 2024-12-05 17:23:32 -08:00
Repl.lean feat: Goal State IO in REPL 2024-12-05 14:31:43 -08:00
flake.lock chore: Update lean4-nix 2024-11-15 14:56:51 -08:00
flake.nix feat: Expose iTree for LSP Configuration 2024-11-14 22:51:25 -08:00
lake-manifest.json chore: Update Lean to v4.12.0 2024-10-06 16:10:18 -07:00
lakefile.lean chore: Update Lean to v4.12.0 2024-10-06 16:10:18 -07:00
lean-toolchain chore: Update Lean to v4.12.0 2024-10-06 16:10:18 -07:00

README.md

Pantograph

A Machine-to-Machine interaction system for Lean 4.

Pantograph

Pantograph provides interfaces to execute proofs, construct expressions, and examine the symbol list of a Lean project for machine learning.

See documentations for design rationale and references.

Installation

For Nix users, run

nix build .#{sharedLib,executable}

to build either the shared library or executable.

Install lake and lean fixed to the version of the lean-toolchain file, and run

lake build

This builds the executable in .lake/build/bin/pantograph-repl.

Executable Usage

pantograph-repl MODULES|LEAN_OPTIONS

The pantograph-repl executable must be run with a list of modules to import. It can also accept lean options of the form --key=value e.g. --pp.raw=true.

The REPL loop accepts commands as single-line JSON inputs and outputs either an Error: (indicating malformed command) or a JSON return value indicating the result of a command execution. The command can be passed in one of two formats

command { ... }
{ "cmd": command, "payload": ... }

The list of available commands can be found in Pantograph/Protocol.lean and below. An empty command aborts the REPL.

Example: (~5k symbols)

$ pantograph Init
env.catalog
env.inspect {"name": "Nat.le_add_left"}

Example with mathlib4 (~90k symbols, may stack overflow, see troubleshooting)

$ pantograph Mathlib.Analysis.Seminorm
env.catalog

Example proving a theorem: (alternatively use goal.start {"copyFrom": "Nat.add_comm"}) to prime the proof

$ pantograph Init
goal.start {"expr": "∀ (n m : Nat), n + m = m + n"}
goal.tactic {"stateId": 0, "goalId": 0, "tactic": "intro n m"}
goal.tactic {"stateId": 1, "goalId": 0, "tactic": "assumption"}
goal.delete {"stateIds": [0]}
stat {}
goal.tactic {"stateId": 1, "goalId": 0, "tactic": "rw [Nat.add_comm]"}
stat

where the application of assumption should lead to a failure.

For a list of commands, see REPL Documentation.

Project Environment

To use Pantograph in a project environment, setup the LEAN_PATH environment variable so it contains the library path of lean libraries. The libraries must be built in advance. For example, if mathlib4 is stored at ../lib/mathlib4, the environment might be setup like this:

LIB="../lib"
LIB_MATHLIB="$LIB/mathlib4/.lake"
export LEAN_PATH="$LIB/mathlib4/build/lib:$LIB_MATHLIB/aesop/build/lib:$LIB_MATHLIB/Qq/build/lib:$LIB_MATHLIB/std/build/lib"

LEAN_PATH=$LEAN_PATH build/bin/pantograph $@

The $LEAN_PATH executable of any project can be extracted by

lake env printenv LEAN_PATH

Troubleshooting

If lean encounters stack overflow problems when printing catalog, execute this before running lean:

ulimit -s unlimited

Library Usage

Pantograph/Library.lean exposes a series of interfaces which allow FFI call with Pantograph which mirrors the REPL commands above. It is recommended to call Pantograph via this FFI since it provides a tremendous speed up.

The executable can be used as-is, but linking against the shared library requires the presence of lean-all. Note that there isn't a 1-1 correspondence between executable (REPL) commands and library functions.

Inject any project path via the pantograph_init_search function.

Developing

A Lean development shell is provided in the Nix flake.

Testing

The tests are based on LSpec. To run tests, use either

nix flake check

or

lake test

You can run an individual test by specifying a prefix

lake test -- "Tactic/No Confuse"