lean-crush

5.12. Extending Alethe Replay🔗

Alethe replay has two extension layers:

  1. certificate terms are decoded back into Lean expressions;

  2. certificate inference rules are proved from their replayed premises.

The register_crush_replay term and register_crush_replay rule commands extend these layers without requiring Lean metaprogramming. Trust mode does not replay a certificate. Alethe reconstruction must decode every relevant certificate term and construct a proof for every inference; core reconstruction does not use these registrations.

A term registration is an inverse translation: use it when a custom lowering introduces an SMT operator that may appear in the certificate. A rule registration is a proof procedure: use it when terms already decode but one named Alethe inference is unsupported. Adding one does not substitute for the other.

5.12.1. Replay DSL Syntax🔗

The command forms are shown below. Square brackets denote optional syntax, braces denote repetition, and quoted tokens are literal. lean-term and tactic-sequence are ordinary Lean syntax; priority is low, high, or a natural number.

term-registration ::=
  "register_crush_replay" "term" [priority] "<<"
    term-pattern {"|" term-pattern}
    "=>" lean-term ">>"

rule-registration ::=
  "register_crush_replay" "rule" [priority] "<<"
    rule-pattern {"|" rule-pattern}
    ["if" lean-term]
    "=>" "by" tactic-sequence ">>"

term-pattern ::=
    "(" symbol {expr-pattern} ")"
  | "(" "(" "_" symbol {sexp-pattern} ")" {expr-pattern} ")"

rule-pattern ::= "(" symbol {sexp-pattern} ")"
symbol       ::= identifier | string-literal

An ordinary term pattern matches an SMT operator whose arguments have already been decoded to Lean expressions. The (_ operator indices...) form additionally matches an indexed SMT identifier, while a rule pattern matches an Alethe rule name and its raw :args. Decoded argument patterns (expr-pattern) are _, .., (term x), or (term x : T). Raw patterns (sexp-pattern) also support literal matches, nested lists, and the metadata captures described below. .. is permitted only as the final pattern in an argument list. Every capture name must occur once per alternative, and all | alternatives must bind the same names. Only rule registrations accept an if condition, and only their right-hand side is a tactic script. The outer << ... >> fence reserves >> as its closing delimiter, and => separates a rule condition from its tactic. Define a named Lean value when a condition itself would otherwise need either token.

5.12.2. Define an Inverse Term🔗

The following lowering emits ((_ divisible 3) value). The inverse registration also accepts (divisible 3 value), the normalized form that cvc5 may use in a certificate:

open Lean Meta open Crush Crush.Alethe Crush.SMT def MultipleOfThree (value : Int) : Prop := value % 3 = 0 @[crush_lower MultipleOfThree] def lowerMultipleOfThree : LoweringHandler := fun ctx => do let #[value] := ctx.args | return none return some (.app (.indexed "divisible" #[.inr 3]) #[← ctx.emitTerm value]) register_crush_replay term << ((_ divisible (int divisor)) (term value : Int)) | (divisible (term divisor : Int) (term value : Int)) => value % divisor = 0 >>

The raw pattern language provides:

  • _ for one ignored item and a final .. for the remaining items;

  • bare symbols, numerals, and strings for exact matches;

  • (sexp x) for a raw Sexp;

  • (term x) for a decoded Lean expression;

  • (term x : T) for a decoded expression whose Lean type is definitionally equal to T;

  • (nat x), (int x), (string x), and (atom x) for parsed raw values;

  • (sort x) for a decoded SMT sort and (prop x) for a decoded proposition.

Unlike lowering patterns, a bare symbol here is an exact match, not an expression capture. Type annotations are Lean types checked by definitional equality, rather than the structural type patterns used by register_lowering.

nat and int parse certificate metadata directly. They are not abbreviations for typed term captures. For example, (nat width) parses a raw decimal width as a Lean Nat, whereas (term width : Nat) asks the certificate term decoder to reconstruct a Nat-valued SMT term.

Built-in term decoding runs before custom registrations. Use register_crush_replay term high, low, or a numeric priority when multiple custom registrations can match the same operator. The right-hand side is type-checked, but Crush cannot infer whether it is the intended inverse of a custom lowering. An inaccurate inverse causes source-assumption bridging or later replay steps to fail; it is never accepted in place of a kernel-checked proof.

5.12.2.1. Parametric Type Guards🔗

A replay type guard can use a hole to accept any bitwidth:

def replayBitwiseAnd {n : Nat} (x y : BitVec n) : BitVec n := x &&& y register_crush_replay term << (docs_bvand (term x : BitVec _) (term y : BitVec _)) => replayBitwiseAnd x y >>

The two holes are independent; RHS elaboration enforces equal widths through the helper's type. BitVec n does not introduce a new capture named n: that name must already be in scope, for example from an earlier (nat n) index capture. A polymorphic helper makes the inferred width available in its body.

5.12.3. Register an Inference Rule🔗

A rule pattern matches the Alethe rule name followed by its raw :args:

register_crush_replay rule low << (docs_arith_commute (term left : Int) (term right : Int)) | (docs_arith_swap (term left : Int) (term right : Int)) => by omega >>

The right-hand side is an ordinary Lean tactic script. Its goal is the decoded conclusion of that certificate step. The tactic first receives the step's replayed premises and pattern captures. If needed, it retries with the enclosing subproof's facts. Unrelated hypotheses from the user's theorem remain unavailable. Failure delegates to the next registration.

The synthetic rule assume validates a decoded SMT assumption against its source Lean fact. A subproof rule can normalize the implication produced when an anchor discharges its local assumptions. Both use the same registration API.

Alternatives and captures follow the same rules as term registrations. Use register_crush_replay rule high, low, or a numeric priority to control dispatch. Exact-rule handlers run before wildcard low-level handlers.

Append if condition when matching the certificate syntax alone is not enough. The condition is a metaprogrammed dispatch guard, not a Lean proposition:

structure ReplayStringBindingIs where name : Name expected : String instance : ReplayCondition ReplayStringBindingIs where check condition ctx := do let captured := ctx.binding? condition.name let some (.lit (.strVal value)) := captured | return false return value == condition.expected def docsEnabledReplay : ReplayStringBindingIs where name := `mode expected := "enabled" register_crush_replay rule low << (docs_conditional_rule (atom mode) ..) if docsEnabledReplay => by exact True.intro >>

ReplayCondition α defines check : α → ReplayRuleContext → MetaM Bool. The guard runs after the pattern has matched, so it can inspect the target, premises, raw arguments, and named captures through ctx.binding?. It runs without retaining metavariable-state changes. Returning false delegates to the next registration; an exception reports a broken condition. When a registration has several | alternatives, the condition applies to all of them. A callback of type ReplayConditionHandler can be used directly through the built-in instance.

Every proof returned by a rule registration is checked against the step target with Lean.Kernel.check. Declarations generated while the tactic runs are checked recursively as well. A failed check is an error, not a replay miss.

5.12.4. Low-Level Handlers🔗

Use attributes when matching requires arbitrary metaprogramming:

@[crush_replay "docs.zero"] def replayDocsZero : ReplayTermHandler := fun ctx => do let #[] := ctx.indices | return none let #[] := ctx.args | return none return some (Lean.toExpr (0 : Int)) @[crush_replay_rule "docs_rule" low] def replayDocsRule : ReplayRuleHandler := fun ctx => do unless ctx.args.isEmpty do return none ctx.runTacticWithScopeFallback (← `(tactic| grind))

A ReplayTermContext exposes head, raw indices, and recursively decoded args. A ReplayRuleContext additionally exposes the step target, clause literals, replayed premises, raw rule arguments, term/sort decoders, and enclosing subproof facts. Returning none delegates to the next handler. runTactic uses only ordinary premises; runTacticWithScopeFallback retries with enclosing subproof facts when the certificate rule requires them.

Prefer the register_crush_replay pattern DSL for a fixed certificate shape. Use @[crush_replay] or @[crush_replay_rule] only when matching requires recursive inspection, dynamic operator selection, or custom metavariable control.

5.12.5. Test Term Decoding🔗

A solver may simplify away a custom operator. For deterministic decoding tests, construct a TermCtx with its symbol map and registered decoders, then call Crush.Alethe.toExpr? on each accepted spelling. Executable examples are in Test/AletheExtension.lean. Pair these checks with a live certificate test:

5.12.6. Require Alethe in an Integration Test🔗

Run a symbolic theorem with cvc5 and Alethe-only reconstruction. Using "auto" here is insufficient because core-directed reconstruction could hide a broken replay registration:

section set_option crush.backend "cvc5" set_option crush.trust "reconstruct" set_option crush.reconstruct "alethe" set_option crush.timeout 10 theorem customDivisibilityReplay (x : Int) (hx : MultipleOfThree x) : ¬x % 3 ≠ 0 := x:Inthx:MultipleOfThree x⊢ ¬x % 3 ≠ 0 All goals completed! 🐙 'customDivisibilityReplay' depends on axioms: [propext, Classical.choice, Quot.sound]#print axioms customDivisibilityReplay end

The theorem must elaborate without Crush.crushSorry in the #print axioms output. Use symbolic operands and a property that depends on the custom operator; the solver may simplify away operators on closed inputs. The Alethe-only policy above disables Lean's early proof shortcut, so a successful test must replay a certificate.

The complete executable tests, including alternatives, priorities, context isolation, compatibility, and kernel rejection, are in Test/AletheExtension.lean.

5.12.7. Diagnose Replay Failures🔗

Use the reconstruction diagnostics to distinguish missing certificates, term decoding, rule replay, and invalid proof terms. The built-in handlers in ArithmeticRules.lean and ReplayRules.lean use the same extension interface.