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 termmatches the translated Lean expression and registers acrush_lowerhandler indexed by the pattern's head constant. -
register_lowering result-typematches the expression's inferred type and registers acrush_lower_resulthandler indexed by the type-family head. The right-hand side still produces an SMT term for the original expression. -
register_lowering sortmatches the Lean type being translated and registers acrush_translate_sorthandler. Its right-hand side produces an SMT sort, which is a.fullencoding, or aSortEncoding.
At the root, use a constant such as Int or an application such as
(loweringAddThree (term x)). Within an application:
-
_ignores an argument. -
xbinds the originalLean.Exprtox. -
(term x)callsctx.emitTermon the captured expression and binds the resultingCrush.SMT.Termtox. -
(sort α)checks that the captured expression is a type, callsctx.emitSort, and binds the resultingCrush.SMT.SSorttoα. -
(Constant ...)matches a nested constant application. Use(Int)to match the constantIntas an argument; a bare argumentIntwould 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.