Skip to content

type error in example, sensitive to simp attribute #1829

Closed

Description

@kim-em

Prerequisites

  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn't already filed.
    • Reduced the issue to a self-contained, reproducible test case.

Description

A type error that comes and goes depending on whether we use simp [attribute] X in ... on an earlier declaration.

Steps to Reproduce

theorem eq_iff_true_of_subsingleton [Subsingleton α] (x y : α) : x = y ↔ True :=
  ⟨fun _ => ⟨⟩, fun _ => (Subsingleton.elim ..)⟩

/- comment out following `attribute [simp]` to make the example below work -/
attribute [simp] eq_iff_true_of_subsingleton in
example : True := trivial

structure Func' (α : Sort _) (β : Sort _) :=
(toFun    : α → β)

def r : Func' α α := ⟨id⟩

@[simp] theorem r_toFun {α : Sort u_1} (a : α) : Func'.toFun r a = id a := rfl

example (x y : α) (h : x = y) : r.toFun x = y := by simp <;> rw [h]
/-
in the presence of `attribute [simp] eq_iff_true_of_subsingleton in`, one gets

application type mismatch
  Eq
argument has type
  Sort u_1
but function has type
  {α : Prop} → α → α → Prop
-/

-- If we change `example` to `theorem`, it doesn't care about the `attribute [simp]`.
theorem noissue (x y : α) (h : x = y) : r.toFun x = y := by simp <;> rw [h]

With the attribute [simp] eq_iff_true_of_subsingleton in line, we get an error on the later example. Without the attribute, the error goes away.

Expected behavior: [What you expect to happen]

No errors!

Versions

leanprover/lean4:nightly-2022-11-10

Other information

(Minimised slightly from Yakov Pechersky's minimisation at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/application.20type.20mismatch.20in.20example/near/309533447. We're seeing this in the mathlib4 port, e.g. https://github.com/leanprover-community/mathlib4/actions/runs/3460347374/jobs/5776794830.)

Activity

  1. kbuzzard commented on Nov 14, 2022

    @kbuzzard
    Contributor

    #1814 also mentioned the same simp lemma

  2. gebner commented on Nov 15, 2022

    @gebner
    Member

    The in example .. is a red herring. attribute [simp] foo in ... does exactly the same as attribute [simp] foo.

  3. gebner commented on Nov 15, 2022

    @gebner
    Member

    Another change that makes the issue disappear is to write example {α : Sort u} .... It seems to be related to the universe level metavariable.

  4. gebner commented on Nov 15, 2022

    @gebner
    Member

    What happens is that the typeclass synthesis in simp assigns the universe metavariable (since it finds the instance Subsingleton (α : Prop)). Simplified repro:

    theorem eq_iff_true_of_subsingleton [Subsingleton α] (x y : α) : x = y ↔ True :=
      ⟨fun _ => ⟨⟩, fun _ => (Subsingleton.elim ..)⟩
    
    example (x y : α) : x = y := by simp [eq_iff_true_of_subsingleton]
  5. gebner commented on Nov 15, 2022

    @gebner
    Member

    Okay, now I'm really surprised. Apparently @leodemoura added this bug/feature explicitly for simp lemmas in 99e8a98:

    We claim it is reasonable to ignore this restriction for universe metavariables because their values are often
    contrained by the terms is instances and simp theorems.

  6. digama0 commented on Nov 15, 2022

    @digama0
    Collaborator

    The commit references leanprover-community/mathport#16 as motivation, although I'm not sure what is going on there.

  7. gebner commented on Nov 15, 2022

    @gebner
    Member

    Looking through the commit, I believe the motivation was to make CoeSort work. If that is the only application, we could just remove ignoreLevelMVarDepth entirely, and add the coerceSort? case to coerce?.

  8. leodemoura commented on Nov 15, 2022

    @leodemoura
    Member

    Looking through the commit, I believe the motivation was to make CoeSort work. If that is the only application, we could just remove ignoreLevelMVarDepth entirely, and add the coerceSort? case to coerce?.

    It was just the first example we hit in practice, there are many more now.

  9. added a commit that references this issue on Nov 30, 2022
    b1012ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions