Lean4
/-- If `e` is a coercion of a set to a type, return the set.
Succeeds either for `Set.Elem s` terms or `{x // x ∈ s}` subtype terms. -/
def coeTypeSet? (e : Expr) : Option Expr := do
if e.isAppOfArity ``Set.Elem 2 then
return e.appArg!
else if e.isAppOfArity ``Subtype 2 then
let .lam _ _ body _ := e.appArg! |
failure
guard <| body.isAppOfArity ``Membership.mem 5
let #[_, _, inst, .bvar 0, s] := body.getAppArgs |
failure
guard <| inst.isAppOfArity ``Set.instMembership 1
return s
else
failure