nielsr's picture
nielsr HF Staff
Add Formal Conjectures (snapshot 86a2f58): statements + files parquet, Lean project (part 7)
8ca48d7 verified
Raw History Blame Contribute Delete
3.74 kB

Changes and expression of the form ∀ (h1 : Prop1) (h2 : Prop2) ..., Propn to Prop1 ∧ Prop2 ∧ ... ∧ Propn.

46
def forallToAnd (expr : Expr) : MetaM Expr :=
body · 3 lines
  forallTelescope expr fun vars final => do
    let 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.

54
partial def checkExistsArrow (stx : Syntax) (e : Expr) : MetaM Unit := do
body · 23 lines
  match e with
  | .mdata _ e => checkExistsArrow stx e
  | .app .. =>
      -- Check if this is an application of `Exists`
      if e.isAppOfArity ``Exists 2 then
        let lam := e.getAppArgs[1]!
        let .lam _ _ target _ := lam
          | throwError m!"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 do return
        lambdaTelescope lam fun vars target => do
          if let .forallE _ domain codomain _ := target then
            if codomain.hasLooseBVars then return
            unless ← isProp domain do return
          else
            return
          let 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.

83
def existsImplicationLinter : Linter where
body · 4 lines
  -- 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