5.9. Extending Finite Arrays
Downstream Array operations can reuse lean-crush's canonical finite-array
representation through withFiniteArray.
The callback receives a let-bound Array value, its logical length, its SMT array
data, and a constructor helper:
open Crush
def overwriteFirst {α : Type}
(xs : Array α) (value : α) : Array α :=
xs.setIfInBounds 0 value
@[crush_lower overwriteFirst]
def lowerOverwriteFirst : LoweringHandler := fun ctx => do
let #[elem, xs, value] := ctx.args | return none
let svalue ← ctx.emitTerm value
withFiniteArray ctx elem xs fun view => do
let data := (smt| (store $(view.data) 0 $svalue))
let updated := view.mkValue view.length data
return (smt| (ite (> $(view.length) 0)
$updated $(view.value)))
example (xs : Array Int) (value : Int) (_h : 0 < xs.size) :
(overwriteFirst xs value)[0]! = value := xs:Array Intvalue:Int_h:0 < xs.size⊢ (overwriteFirst xs value)[0]! = value
All goals completed! 🐙
withFiniteArray returns none if a custom sort handler selected another
representation.
It also inserts an SMT let, preventing repeated selectors from duplicating a
nested update term.