lean-crush

5.6. Dependent Result Lowerings🔗

@[crush_lower_result T] dispatches on a term's result-family head instead of its application head. It first checks the immediate head of the inferred type. If that type is syntactically a dependent function, it opens the binders and checks the codomain head. Named aliases remain separate dispatch keys, so register the alias too when callers may retain it in the inferred type. This is useful for generated declarations and lambdas whose head is unstable, but whose result family is known. The handler still receives the original term's ctx.fn and ctx.args; peeled binders are used only to select the handler.

For a named operation such as MappedInt.next, head dispatch is more precise. Result dispatch is useful for generated functions and dependent results.

The term representation must agree with a sort handler. Here every IndexedInt index is representation-isomorphic to SMT Int, even though the result type depends on the input:

open Crush structure IndexedInt (index : Int) where value : Int def indexedInt (index : Int) : IndexedInt index := ⟨index⟩ register_lowering sort << (IndexedInt _) => (smt| Int) >> register_lowering result-type with ctx << (IndexedInt (term index)) => do unless ctx.fn.isConstOf ``indexedInt && ctx.args.size == 1 do return none return some index >> register_lowering term << (IndexedInt.value _ (term value)) => (smt| $value) >> example (index : Int) : (indexedInt index).value = index := index:Int⊢ (indexedInt index).value = index All goals completed! 🐙

The result pattern captures the index from the inferred type. The metaprogram then accepts only the named constructor function indexedInt: an arbitrary value of IndexedInt index need not contain index. The pattern syntax matches the complete inferred type; use the attribute API when a handler needs to open dependent function binders or reduce the type before matching.

The built-in Decidable encoding uses result dispatch. Decidable p and dependent functions ending in it are represented by an axiomatized singleton SMT sort, matching Lean's proof-irrelevant subsingleton. Concretely, the lowering emits the equivalent of:

(declare-sort Decision 0)
(declare-fun decision () Decision)
(assert (forall ((d Decision)) (= d decision)))

Observing the value with decide p lowers to p; consequently, DecidableEq is observed as SMT equality without encoding the implementation of a particular decision procedure. The built-in handler is registered for both Decidable and the named DecidableEq alias.