Lean4
/-- If `e` has a structure as type with field `fieldName`, `mkDirectProjection e fieldName` creates
the projection expression `e.fieldName` -/
def mkDirectProjection (e : Expr) (fieldName : Name) : MetaM Expr := do
let type ← whnf (← inferType e)
let .const structName us := type.getAppFn |
throwError "{e} doesn't have a structure as type"
let some projName := getProjFnForField? (← getEnv) structName fieldName |
throwError "{structName } doesn't have field {fieldName}"
return mkAppN (.const projName us) (type.getAppArgs.push e)