The alkahest_check tactic
alkahest_check brings the reflective kernel to goals you
write yourself in Lean. It reifies the goal into the kernel's AExpr fragment,
collects the hypotheses that are kernel side conditions, and closes the goal
with one of the kernel's soundness theorems, whose Boolean premise Lean's
kernel evaluates with decide +kernel. Alkahest acts as an oracle: it can
compute the witness (atom table, relations, cofactors), but it is not trusted.
A wrong witness makes the tactic fail, and it can never prove a false goal.
The design follows Mathlib's polyrith: call the oracle once, then paste a
self-contained proof that no longer needs it.
import Alkahest.Tactic
example (x : ℝ) (hx : 0 < x) : deriv (fun y => Real.sqrt y) x = 1 / (2 * Real.sqrt x) := by
alkahest_check
example (x : ℝ) : Real.sin x ^ 2 + Real.cos x ^ 2 = 1 := by
alkahest_check using ⟨[⟨.var 0, .zero⟩, ⟨.fn .sin, .var 0⟩, ⟨.fn .cos, .var 0⟩],
[.pyth (.var 0)], [[([], 1, 1)]]⟩Goals
| goal | checker | soundness theorem |
|---|---|---|
HasDerivAt (fun y => F) f x |
checkIntegral H F f w |
integral_sound |
deriv (fun y => F) x = f |
checkIntegral H F f w |
deriv_sound |
a = b (real expressions) |
checkEq H a b w |
checkEq_sound |
F, f, a and b are real expressions built from +, -, *, /, ⁻¹,
^ n (a numeral n : ℕ), numerals, Real.pi, Real.log |·|, and the
functions Real.exp, Real.log, Real.sin, Real.cos, Real.tan,
Real.sqrt, Real.arctan, Real.arcsin, Real.arccos, Real.sinh,
Real.cosh, Real.tanh and Real.arsinh. In an equation, any other subterm
(f t, x ^ (1/3 : ℝ), n : ℕ cast to ℝ) is treated as an opaque atom, as
ring does. In a derivative goal, an opaque subterm may not mention the
variable of differentiation (the kernel has no derivative for it); it may
mention parameters.
The hypotheses used are the local facts of the forms e ≠ 0, ¬ e = 0,
0 < e, e > 0, 0 ≤ e, e ≥ 0 and a < b (and b > a) over real
expressions in the same fragment, and conjunctions of them. The kernel's
sign checker derives further conditions from them structurally: 0 < x gives
x ≠ 0, exp and cosh are positive, even powers of nonzero terms are
positive, and b < a gives 0 < a - b.
The differentiability conditions of F (denominators and log arguments
nonzero, sqrt radicands positive, arcsin arguments in (-1, 1), cos ≠ 0
under tan) must follow from the hypotheses. If one does not, the tactic says
which one to add:
example (x : ℝ) : HasDerivAt (fun y => Real.log y) x⁻¹ x := by
alkahest_check
-- error: alkahest_check: the function is differentiable here only under side
-- conditions that do not follow from the hypotheses; add them to the context:
-- x ≠ 0Where the witness comes from
alkahest_checkfirst computes a relation-free witness itself, in Lean: the atom table of the two sides (for a derivative goal, of the kernel's ownD 0 Fandf). That proves every identity that holds as a polynomial identity in the atoms: polynomial and rational-function algebra where inverses of monomials distribute, and most derivatives of compositions (exp ∘ sin,log (y² - 1),arcsin,arctan, products and quotients). No oracle is involved, and the proof is deterministic.- Identities that need a kernel relation, such as
sin² + cos² = 1,u · u⁻¹ = 1(withu ≠ 0),√u² = u(with0 ≤ u),tan = sin/cos, or the addition formulas forsin,cos,exp,sinhandcosh, need a witness with relations and cofactors. Without one,alkahest_checkfails and prints the reified goal as a JSON query. alkahest_check using wchecks the given witnessw : Witnessand nothing else. This is the reproducible form to commit.alkahest_check?runsalkahest_checkand, on success, suggests the self-contained replacement (a code action in the editor): plainalkahest_checkwhen the relation-free witness was enough, otherwisealkahest_check using ⟨…⟩with the oracle's witness. With no oracle configured it prints the command that answers the query offline.
Offline: Python
example (x : ℝ) : Real.sin (2 * x) = 2 * Real.sin x * Real.cos x := by
alkahest_check?
-- alkahest_check?: no oracle is configured. Compute the witness with
-- python3 -m alkahest.lean_oracle '{"hyps":[],"kind":"eq","lhs":"(.app .sin (.mul (.lit 2) (.var 0)))",…}'alkahest.experimental.lean_witness answers the same query from Python:
from alkahest.experimental import lean_witness
r = lean_witness('{"kind": "eq", "vars": ["x"], "hyps": [], "rhs": ".one", '
'"lhs": "(.add (.pow (.app .sin (.var 0)) 2) (.pow (.app .cos (.var 0)) 2))"}')
r["ok"] # True: a witness, and every side condition follows from the hypotheses
r["tactic"] # 'alkahest_check using ⟨[…], [(.pyth (.var (nat_lit 0)))], […]⟩' — paste this
r["missing"] # side conditions to add as hypotheses, e.g. ['((x : ℝ) + (1 : ℝ)) ≠ 0']
r["detail"] # why there is no witness: 'withheld_identity_not_reduced' for a false claimThe query's kind is "eq" or "deriv" (for both derivative shapes lhs
is F and rhs is f), vars names the atoms (only used in messages),
hyps are Cond data, and lhs/rhs are AExpr data. The answer's
witness is Lean source for Alkahest.Kernel.Witness, with the raw literals
(nat_lit, Int.ofNat) the certificate emitters use.
Online: the oracle option
set_option alkahest.oracle "python3 -m alkahest.lean_oracle"
example (x : ℝ) (h : x + 1 ≠ 0) : x / (x + 1) + 1 / (x + 1) = 1 := by
alkahest_checkThe option names a command, split on spaces, that reads one JSON query on
stdin and prints one JSON answer. With it set, alkahest_check asks the
oracle whenever the relation-free witness is not enough, and
alkahest_check? turns the answer into a using suggestion. It needs a
Python environment with alkahest installed (any build: the oracle is in the
core extension). The option is off by default, and a file that pastes
alkahest_check using w never runs it.
For theorem-proving agents
An LLM prover in the LeanDojo or LeanAgent style interacts with Lean one
tactic at a time. alkahest_check fits that loop as a calculation step:
- When the goal is an equation of real expressions, a
HasDerivAtor aderivequation, tryalkahest_checkfirst. It is cheap (tens of milliseconds; see below) and needs nothing outside Lean. - If it fails with a missing side condition, the message names it in Lean
syntax (
x ≠ 0,0 < x ^ 2 + 1). Prove that fact as a lemma (have h : x ≠ 0 := …, oftenpositivityorby linarith) and retry. - If it fails because the identity needs relations, run
alkahest_check?with the oracle option set (or calllean_witnesson the printed query from the agent's Python side) and replace the tactic with the suggestedalkahest_check using ⟨…⟩. The resulting proof replays without Alkahest installed, which matters for proof search (no external process in the search tree) and for submission to a benchmark checker. - A failure after that is information: the claim is false, or it needs a
relation the kernel does not have (
log (a·b)splitting needs the kernel'slogMul, which the oracle does not generate yet;√(x²) = |x|; special functions). Fall back to other tactics.
Because the checker is verified, an agent cannot be misled by the oracle: the
tactic either closes the goal with a kernel-checked proof or fails, and
#print axioms on the result shows only Lean's standard axioms
(propext, Classical.choice, Quot.sound).
Check times
Measured with Lean's profiler on the two test files (lean/test/TacticSmoke.lean,
lean/test/TacticOracle.lean) on one machine, Mathlib import excluded:
| goals | per goal, all-in | the tactic itself | |
|---|---|---|---|
offline (alkahest_check, alkahest_check using w) |
30 | about 30 ms | median 3 ms, max 10 ms |
| online (oracle asked for every goal) | 15 | about 0.23 s | median 0.17 s, max 0.19 s |
"All-in" is the file's elaboration time above an import-only file, divided by
the number of goals: statement elaboration, reification, and the kernel's
evaluation of the checker. The online figure is almost all the oracle
process: starting Python and importing alkahest once per call. Importing
Alkahest.Tactic (Mathlib included) takes about 15 s, as for any Mathlib file.
Limitations
- The goal must be literally of one of the three shapes; the tactic does not
intro, unfold definitions or rewrite withderivlemmas first. Useshoworunfoldto expose the shape. - In a derivative goal, if the point
xalso occurs insideFas a parameter (HasDerivAt (fun y => x * y) x x), the two occurrences become different variables and the check fails. Rename the point. - Side conditions are only what the structural sign checker derives from the
hypotheses:
1 < xdoes not givex ^ 2 - 1 ≠ 0. State the condition the error message names. - The oracle generates the relations of
alkahest-core's certificate emitter:u·u⁻¹,√u², Pythagoras forsin/cosandcosh/sinh,tan,tanh, thesin/cos/expaddition families on integer multiples of one argument, andlog (exp a). The kernel's other relations (logMul,logPow,logInv,expLog,sqrtMul,sinhAdd,coshAdd) can be used in a hand-written witness. - Inequalities, limits, sums and integrals over intervals are not supported: the kernel decides equalities and derivatives only.