Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent addPPExplicitToExposeDiff from assigning metavariables #5276

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

kmill
Copy link
Collaborator

@kmill kmill commented Sep 6, 2024

Type mismatch errors have a nice feature where expressions are annotated with pp.explicit to expose differences via isDefEq checking. However, this procedure has side effects since isDefEq may assign metavariables. This PR wraps the procedure with withoutModifyingState to prevent assignments from escaping.

Assignments can lead to confusing behavior. For example, in the following a higher-order unification fails, but the difference-finding procedure unifies metavariables in a naive way, producing a baffling error message:

theorem test {f g : Nat → Nat} (n : Nat) (hfg : ∀a, f (g a) = a) :
    f (g n) = n := hfg n

example {g2 : ℕ → ℕ} (n2 : ℕ) : (λx => x * 2) (g2 n2) = n2 := by
  with_reducible refine test n2 ?_
  /-
  type mismatch
    test n2 ?m.648
  has type
    (fun x ↦ x * 2) (g2 n2) = n2 : Prop
  but is expected to have type
    (fun x ↦ x * 2) (g2 n2) = n2 : Prop
  -/

With the change, it now says has type ?m.153 (?m.154 n2) = n2.

Note: this uses withoutModifyingState instead of withNewMCtxDepth because we want to know something about where isDefEq failed — we are trying to simulate a very basic version of isDefEq for function applications, and we want the state at the point of failure to know which argument is "at fault".

@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 6, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Sep 6, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 6, 2024
@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Sep 6, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Sep 6, 2024

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-5276 has successfully built against this PR. (2024-09-06 20:25:41) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 8b5443eb22252c9982c23e8fd15eb67d902da65d --onto 193b6f2bec332ac0bce33e10856a96163d4be456. (2024-10-27 00:23:24)

@kmill kmill added the awaiting-review Waiting for someone to review the PR label Sep 7, 2024
tests/lean/run/4405.lean Show resolved Hide resolved
has type
OfNat.ofNat 0 = OfNat.ofNat 0 : Prop
?_ (?_ ?_) = ?_ (?_ ?_) : Prop
but is expected to have type
OfNat.ofNat 0 = OfNat.ofNat 1 : Prop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, in this instance it’s not really an improvement. As a user I look at this and wonder why these metavariables couldn’t just be instantiated. But maybe this example is somewhat artificial.

Maybe add the test from the issue description to see one where it’s a UX improvement?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this one is a bit obscure. I've added some tests.

I tried making a version that is consistent about partially assigning metavariables to mimic a half-successful isDefEq, but I think in the end that's more confusing, since you don't know what the real type is. It might be nice having a real isDefEq diagnostic function instead.

@Kha Kha added awaiting-author Waiting for PR author to address issues and removed awaiting-review Waiting for someone to review the PR labels Sep 13, 2024
Type mismatch errors have a nice feature where expressions are annotated with `pp.explicit` to expose differences via `isDefEq` checking. However, this procedure has side effects since `isDefEq` may assign metavariables. This PR wraps the procedure with `withNewMCtxDepth` to prevent such assignments.

Assignments can lead to confusing behavior. For example, in the following a higher-order unification fails, but the difference-finding procedure unifies metavariables in a naive way, producing a baffling error message:
```lean
theorem test {f g : Nat → Nat} (n : Nat) (hfg : ∀a, f (g a) = a) :
    f (g n) = n := hfg n

example {g2 : ℕ → ℕ} (n2 : ℕ) : (λx => x * 2) (g2 n2) = n2 := by
  with_reducible refine test n2 ?_
  /-
  type mismatch
    test n2 ?m.648
  has type
    (fun x ↦ x * 2) (g2 n2) = n2 : Prop
  but is expected to have type
    (fun x ↦ x * 2) (g2 n2) = n2 : Prop
  -/
```
With the change, the reported type is now `?m.153 (?m.154 n2) = n2`.
@kmill kmill force-pushed the fix_addppexplicittoexposediff branch from c94f8a3 to d018246 Compare October 26, 2024 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-author Waiting for PR author to address issues builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants