lean-crush

5.4. Pattern-Based Lowerings🔗

register_lowering pairs a structural Lean expression pattern with an SMT quotation or a Lean metaprogram. Like replay registrations, each rule is enclosed in << ... >>:

open Crush def loweringAddThree (x : Int) : Int := x + 3 register_lowering term << (loweringAddThree (term x)) => (smt| (+ $x 3)) >> example (x : Int) : loweringAddThree x = x + 3 := x:Int⊢ loweringAddThree x = x + 3 All goals completed! 🐙

(smt| ...) constructs a structured SMT term from symbols, applications, numerals, Booleans, and strings. $x splices an existing SMT.Term. When an SMT.SSort is expected, the same quotation constructs a sort and its splices must be sorts. The final SMT script is sort-checked before solving. The checker accepts SMT-LIB's n-ary forms, so a lowering may emit (+ $x $y $z): +, *, -, and str.++ associate to the left, => to the right, and = and the integer comparisons are chainable.

There are three registration kinds:

  • register_lowering term matches the translated Lean expression and registers a crush_lower handler indexed by the pattern's head constant.

  • register_lowering result-type matches the expression's inferred type and registers a crush_lower_result handler indexed by the type-family head. The right-hand side still produces an SMT term for the original expression.

  • register_lowering sort matches the Lean type being translated and registers a crush_translate_sort handler. Its right-hand side produces an SMT sort, which is a .full encoding, or a SortEncoding.

At the root, use a constant such as Int or an application such as (loweringAddThree (term x)). Within an application:

  • _ ignores an argument.

  • x binds the original Lean.Expr to x.

  • (term x) calls ctx.emitTerm on the captured expression and binds the resulting Crush.SMT.Term to x.

  • (sort α) checks that the captured expression is a type, calls ctx.emitSort, and binds the resulting Crush.SMT.SSort to α.

  • (Constant ...) matches a nested constant application. Use (Int) to match the constant Int as an argument; a bare argument Int would capture it.

  • (term x : Int) additionally matches the captured expression's inferred type. A parameterized type pattern such as (Family α) can capture its arguments too.

Patterns match exact elaborated application spines, including implicit type and instance arguments. They do not unfold definitions or compare by definitional equality. The optional type pattern follows the same structural rules, with a bare constant interpreted as a nullary type. Captures must have distinct names. A pattern looks through a variable standing for a shared let value only where it matches a nested application, so other captures keep the variable and its sharing. All pattern checks finish before any capture is recursively translated.

The right-hand side can return a pure SMT.Term, a TranslateM SMT.Term, or a TranslateM (Option SMT.Term) to explicitly decline with none. Sort rules accept the corresponding forms with SMT.SSort. Helpers and do blocks have access to the captured values. All three kinds (term, result-type, and sort) support an optional with ctx before << to explicitly bind the original TranslationCtx, exposing its head, arguments, recursive translation callbacks, and declaration callback. Use it when the RHS refers to the context; automatic (term x) and (sort α) captures do not require it. The binder can have any name distinct from the pattern captures; no context name is introduced implicitly:

def loweringAddFour (x : Int) : Int := x + 4 def loweringAddFourTemplate (x : SMT.Term) : SMT.Term := (smt| (+ $x 4)) register_lowering term high with ctx << (loweringAddFour x) => do let x ← ctx.emitTerm x return loweringAddFourTemplate x >> example (x : Int) : loweringAddFour x = x + 4 := x:Int⊢ loweringAddFour x = x + 4 All goals completed! 🐙

An optional numeric priority or high/low comes after the registration kind, before any with binder. Each command registers one pattern; use multiple commands for alternatives. Generated handlers use the existing attribute registries and their ordering, including the precedence of general crush_translate handlers. Constants and helper names are resolved when the command is elaborated, and registrations persist across imports.

The fence reserves >> inside the right-hand side. Put code that needs that operator in a named helper. These rules provide concise handler definitions; they do not prove that the encoding preserves the Lean operation's semantics.