5.8. Parameterized Sort Handlers
@[crush_translate_sort] is the sort-level counterpart to
@[crush_translate].
It can inspect type arguments and look up their sorts with ctx.emitSort.
Use crush_map_sort when a monomorphic Lean type always maps to one existing
nullary SMT sort.
Use a sort handler when the target sort depends on Lean type arguments, needs
guards, or requires declarations.
A sort handler returns the encoding it chooses, a SortEncoding:
-
.sameAs Tencodes the type exactly asT: the same sort and the same validity predicate, so the values of the two types correspond one to one. An alias can delegate to its definition this way, and a one-field wrapper to its field. -
.full sdeclares that every value of the SMT sortsstands for a value of the type, ascrush_map_sortdoes. A sort built from a type with a validity predicate is not full:(Array Int Int)has arrays that no map withNatvalues gives.Crush.needsWFGuard Ttells whetherThas such a predicate. -
.totalMap key value lookupdeclares thatm ↦ lookup mmatches the values of the type one to one with the functionskey → value. crush encodes the type as(Array ⟦key⟧ ⟦value⟧), whereselect a kreadslookup m k, and adds the validity predicate itself.
ctx.emitSort only looks a sort up, so the result decides the encoding.
A type that a sort handler claims gets none of its built-in encoding: no
selectors, constructors, or validity predicate of its own.
Lean passes values between definitionally equal types, including types that
differ only in an argument such as Key LeftTag and Key RightTag when both tags
unfold to Unit. Operations read values in the encoding of the type they expect.
crush therefore refuses an encoding that gives a sort used by a definitionally
equal type a different meaning: .full Int for an alias of Nat, because
built-in operations would read its values as natural numbers, or the same new
sort for two aliases of Nat. define-sort synonyms are expanded first. Two
written forms of one type that should share an encoding can return .sameAs
the same type.
A total Lean map is exactly the model provided by SMT Array theory.
The sort rule encodes TotalMap key value with .totalMap, and term handlers
map lookup and update to select and store.
When keys or values need a guard, as Nat does, a valid array holds a valid
value at every Lean key and one fixed padding value at every other key, so
each map has exactly one array.
Two encodings of definitionally equal types share a sort only when their keys,
values and lookups agree.
In a sort rule, (smt| ...) constructs an SMT.SSort and $key splices a
sort. This also works in helpers with an expected SMT.SSort type, including
indexed sorts such as (smt| (_ BitVec 8)). Without an expected sort type,
quotations continue to construct SMT.Term values.
open Crush
structure TotalMap (κ ν : Type) where
fn : κ → ν
namespace TotalMap
def get {κ ν : Type}
(m : TotalMap κ ν) (k : κ) : ν :=
m.fn k
def set {κ ν : Type} [DecidableEq κ]
(m : TotalMap κ ν)
(k : κ) (v : ν) : TotalMap κ ν :=
⟨fun k' => if k' = k then v else m.fn k'⟩
end TotalMap
def totalMapLookup (key value : Lean.Expr) : Lean.Expr :=
Lean.mkApp2 (Lean.mkConst ``TotalMap.get) key value
register_lowering sort <<
(TotalMap key value) =>
let lookup := totalMapLookup key value
SortEncoding.totalMap key value lookup
>>
-- Whether `TotalMap key value` uses this encoding.
def totalMapEncoded (key value : Lean.Expr) :
TranslateM Bool := do
let map :=
Lean.mkApp2 (Lean.mkConst ``TotalMap) key value
let lookup := totalMapLookup key value
return (← totalMapEncoding? map lookup).isSome
@[crush_lower TotalMap.get]
def translateTotalMapGet :
TranslationHandler := fun ctx => do
let #[keyTy, valueTy, map, key] := ctx.args
| return none
unless ← totalMapEncoded keyTy valueTy do
return none
let smap ← ctx.emitTerm map
let skey ← ctx.emitTerm key
return some (smt| (select $smap $skey))
@[crush_lower TotalMap.set]
def translateTotalMapSet :
TranslationHandler := fun ctx => do
let #[keyTy, valueTy, _, map, key, value] := ctx.args
| return none
unless ← totalMapEncoded keyTy valueTy do
return none
let smap ← ctx.emitTerm map
let skey ← ctx.emitTerm key
let svalue ← ctx.emitTerm value
return some (smt| (store $smap $skey $svalue))
example (m : TotalMap Int Int) (k value : Int) :
TotalMap.get (TotalMap.set m k value) k = value := m:TotalMap Int Intk:Intvalue:Int⊢ (m.set k value).get k = value
All goals completed! 🐙
example (m : TotalMap Nat Int) (k j : Nat) (value : Int)
(h : k ≠ j) :
TotalMap.get (TotalMap.set m k value) j =
TotalMap.get m j := m:TotalMap Nat Intk:Natj:Natvalue:Inth:k ≠ j⊢ (m.set k value).get j = m.get j
All goals completed! 🐙
Operation lowerings must agree with the selected representation.
totalMapEncoding? ty lookup confirms that ty uses a total-map encoding with
that lookup and returns its key and value sorts, key guard and padding value.
Given valid inputs, a lowering must build a valid array: a valid value at every
Lean key and the padding value at every other key. select, and store at a
Lean key with a valid value, meet this. A constant array meets it only when
every SMT key stands for a Lean key or the constant is the padding value.
A map whose value type contains the map itself, such as a tree whose children
are a TotalMap Nat Tree, makes that datatype opaque.