Lean4
/-- Return the subexpression at position `pos` in `e` together with an occurrence number
that allows the expression to be found by `kabstract`.
Return `none` when the subexpression contains loose bound variables. -/
def viewKAbstractSubExpr (e : Expr) (pos : SubExpr.Pos) : MetaM (Option (Expr × Option Nat)) := do
let subExpr ← Core.viewSubexpr pos e
if subExpr.hasLooseBVars then
return none
let positions ← kabstractPositions subExpr e
let some n := positions.idxOf? pos |
unreachable!
return some (subExpr, if positions.size == 1 then none else some (n + 1))