forallTelescope expr fun vars final =>dolet varsAsProp ← vars.mapM inferType
varsAsProp.foldrM (fun var currentBigAnd => mkAppM ``And #[var, currentBigAnd]) final
Checks if an expression contains the pattern ∃ x, P x → Q.
partialdefcheckExistsArrow (stx : Syntax) (e : Expr) : MetaMUnit:=do
body · 23
lines
match e with
| .mdata _ e => checkExistsArrow stx e
| .app .. =>-- Check if this is an application of `Exists`if e.isAppOfArity ``Exists2thenlet lam := e.getAppArgs[1]!
let .lam _ _ target _ := lam
| throwErrorm!"Encountered an ill-formed existential expression {e}"-- If the inside of the lambda expression is not a forall then we're fine.unless target.isForall doreturn
lambdaTelescope lam fun vars target =>doiflet .forallE _ domain codomain _ := target thenif codomain.hasLooseBVars thenreturnunless← isProp domain doreturnelsereturnlet correctCore ← forallToAnd target
let correctLam ←Lean.Meta.mkLambdaFVars vars correctCore
let suggestedExpr ← mkAppM ``Exists #[correctLam]
Lean.Linter.logLintIf linter.style.existsImplication stx
m!"Declaration contains the pattern the expression {e}. \ Did you mean {suggestedExpr}?"
| _ => pure ()
The existsImplicationLinter detects expressions of the form ∃ a, P a → Q and flags them to the
user since those are rarely correct.
-- TODO(Paul-Lez): Running in `StateT Unit MetaM Unit` is a bit of a hack.-- Do this with a non-StateT version of `runTermLinter`?
run :=Lean.Elab.Command.Linter.runTermLinter (σ :=Unit) linter.style.existsImplication
fun expr stx => checkExistsArrow expr stx