Zulip Chat Archive

Stream: mathlib4

Topic: debugging TC synth failure


Johan Commelin (Mar 22 2024 at 14:19):

Can someone recommend a next step in debugging this failure? (This is from a branch...)

instance (X : Cα΅’α΅–) : RingHomId (R.map (πŸ™ X)) where
  eq_id := by rw [R.map_id X]; rfl

instance (X : Cα΅’α΅–) : RingHomId (R.map (πŸ™ X)) := inferInstance -- works

set_option trace.Meta.synthInstance true

@[simp]
theorem map_id (P : PresheafOfModules R) (X : Cα΅’α΅–) :
    P.map (πŸ™ X) = LinearMap.id' := by
                  -- failed to synthesize instance RingHomId ⇑(R.map (πŸ™ X))
  ext
  simp
-- [Meta.synthInstance] ❌ RingHomId ⇑(R.map (πŸ™ X)) β–Ό
--   [] no instances for RingHomId ⇑(R.map (πŸ™ X)) β–Ό
--     [instances] #[]

Johan Commelin (Mar 22 2024 at 14:28):

In particular, how can I get more info from Lean why it thinks there are no instances? Clearly there is an instance a few lines up. (And this instance works when provided manually.)

Adam Topaz (Mar 22 2024 at 14:35):

your code seems to work as is for me?

Johan Commelin (Mar 22 2024 at 15:34):

Right, because I didn't give you my branch. I'll push it now.

Johan Commelin (Mar 22 2024 at 15:34):

branch#jmc-comp-triples

Matthew Ballard (Mar 22 2024 at 15:37):

What’s the imports?

Johan Commelin (Mar 22 2024 at 15:42):

The problem occurs halfway the file Mathlib/Algebra/Category/ModuleCat/Presheaf.lean on that branch.

Matthew Ballard (Mar 22 2024 at 15:52):

It is trying to coerce from a ring hom whereas the instance is coercing from a function.

Johan Commelin (Mar 22 2024 at 15:55):

Can you tell me how you found that?

Matthew Ballard (Mar 22 2024 at 15:58):

Expanding the term coming from the instance and expanding what was expected at the failure

Matthew Ballard (Mar 22 2024 at 15:59):

I can’t really dig anymore at the moment

Johan Commelin (Mar 22 2024 at 16:04):

Ok, I think I understand what you mean:

  • the instance is using RingCat.instFunLike under the hood, whereas
  • the failure expects something using RingHom.instFunLike.

Those two are defeq, but Lean probably doesn't get a chance to realize that.

Matthew Ballard (Mar 22 2024 at 16:06):

Oh right! Sorry \to and \hom look almost the same now in nvim

Johan Commelin (Mar 22 2024 at 16:58):

I guess this note is relevant:

instance instFunLike {X Y : RingCat} : FunLike (X ⟢ Y) X Y :=
  -- Note: this is apparently _not_ defeq to RingHom.instFunLike with reducible transparency
  ConcreteCategory.instFunLike

Johan Commelin (Mar 22 2024 at 16:59):

And I should point out:

example (R S : RingCat) : @RingCat.instFunLike R S = RingHom.instFunLike := rfl -- works

Adam Topaz (Mar 22 2024 at 16:59):

Can we just put Homs of RingCat et al. in a structure wrapper already?

Johan Commelin (Mar 22 2024 at 17:01):

Part of me is a bit sad if that is necessary, but maybe it's the path of least resistance.

Adam Topaz (Mar 22 2024 at 17:02):

The path of more resistance would be to unbundle homs from Category.

Adam Topaz (Mar 22 2024 at 17:03):

or maybe make certain things reducible in a clever way which isn't obvious to me.

Johan Commelin (Mar 22 2024 at 17:12):

This works, but it's an ugly convert hack.

diff --git a/Mathlib/Algebra/Category/ModuleCat/Presheaf.lean b/Mathlib/Algebra/Category/ModuleCat/Presheaf.lean
index 572cc07091..6a36a907ec 100644
--- a/Mathlib/Algebra/Category/ModuleCat/Presheaf.lean
+++ b/Mathlib/Algebra/Category/ModuleCat/Presheaf.lean
@@ -62,37 +62,22 @@ theorem map_apply (P : PresheafOfModules R) {X Y : Cα΅’α΅–} (f : X ⟢ Y) (x) :
     P.map f x = (P.presheaf.map f) x :=
   rfl

-instance (X : Cα΅’α΅–) : RingHomId (R.map (πŸ™ X)) where
+instance ringHomId (X : Cα΅’α΅–) : RingHomId (R.map (πŸ™ X)) where
   eq_id := by rw [R.map_id X]; rfl

-instance {X Y Z : Cα΅’α΅–} (f : X ⟢ Y) (g : Y ⟢ Z) :
+instance ringHomCompTriple {X Y Z : Cα΅’α΅–} (f : X ⟢ Y) (g : Y ⟢ Z) :
     RingHomCompTriple (R.map f) (R.map g) (R.map (f ≫ g)) where
   comp_eq := by rw [R.map_comp f g]; rfl

 @[simp]
 theorem map_id (P : PresheafOfModules R) (X : Cα΅’α΅–) :
-    P.map (πŸ™ X) = LinearMap.id' := by
+    P.map (πŸ™ X) = by convert LinearMap.id'; apply ringHomId := by
   ext
   simp

 @[simp]
 theorem map_comp (P : PresheafOfModules R) {X Y Z : Cα΅’α΅–} (f : X ⟢ Y) (g : Y ⟢ Z) :
-    P.map (f ≫ g) = (P.map g).comp (P.map f) := by
+    P.map (f ≫ g) = by convert (P.map g).comp (P.map f); apply ringHomCompTriple := by
   ext
   simp

Adam Topaz (Mar 22 2024 at 17:14):

I suppose RingHom.comp (P.map g) (P.map f) should also work instead of convert ...?

Matthew Ballard (Mar 22 2024 at 17:14):

Got a little time. Let me poke at the reducible and instances defeq failure

Johan Commelin (Mar 22 2024 at 17:15):

I opened #11590 with this changes.

Johan Commelin (Mar 22 2024 at 17:15):

Feel free to push to that branch

Johan Commelin (Mar 22 2024 at 17:15):

The goal is to refactor RingHomCompTriple so that it no longer talks about ring homs, but just any functions. Then the infrastructure can be reused for other kinds of equivariant homs.

Johan Commelin (Mar 22 2024 at 17:16):

@Antoine Chambert-Loir needs something like that for #6057

Matthew Ballard (Mar 22 2024 at 17:16):

It won’t unfold CategoryTheory.forget RingCat

Matthew Ballard (Mar 22 2024 at 17:20):

Strange

Matthew Ballard (Mar 22 2024 at 17:23):

Here is a default transparency successful trace

[Meta.isDefEq] βœ… RingCat.instFunLike =?= RingHom.instFunLike β–Ό
  [] βœ… RingCat.instFunLike =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
    [] βœ… CategoryTheory.ConcreteCategory.instFunLike =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
      [] βœ… { coe := fun f ↦ (CategoryTheory.forget RingCat).map f,
            coe_injective' := β‹― } =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
        [] βœ… fun f ↦ (CategoryTheory.forget RingCat).map f =?= fun f ↦ f.toFun β–Ό
          [] βœ… R ⟢ S =?= ↑R β†’+* ↑S β–Ό
            [] βœ… CategoryTheory.CategoryStruct.toQuiver.1 R S =?= ↑R β†’+* ↑S β–Ό
              [] βœ… CategoryTheory.BundledHom.MapHom SemiRingCat.AssocRingHom (@Ring.toSemiring) R.str S.str =?= ↑R β†’+* ↑S β–Ό
                [] βœ… SemiRingCat.AssocRingHom ↑R ↑S =?= ↑R β†’+* ↑S β–Ό
                  [] βœ… ↑R β†’+* ↑S =?= ↑R β†’+* ↑S
          [] βœ… (CategoryTheory.forget RingCat).map f =?= f.toFun β–Ό
            [] βœ… (CategoryTheory.forget RingCat).toPrefunctor.2 f =?= (↑↑f).1 β–Ό
              [] βœ… (CategoryTheory.BundledHom.bundledHomOfParentProjection SemiRingCat.AssocRingHom @Ring.toSemiring).toFun R.str S.str
                    f =?= (↑↑f).1 β–Ό
                [] βœ… (CategoryTheory.BundledHom.bundledHomOfParentProjection SemiRingCat.AssocRingHom @Ring.toSemiring).1 R.str S.str
                      f =?= (↑↑f).1 β–Ό
                  [] βœ… SemiRingCat.bundledHom.toFun Ring.toSemiring Ring.toSemiring f =?= (↑↑f).1 β–Ό
                    [] βœ… SemiRingCat.bundledHom.1 Ring.toSemiring Ring.toSemiring f =?= (↑↑f).1 β–Ό
                      [] βœ… ⇑f =?= (↑↑f).1 β–Ό
                        [] βœ… RingHom.instFunLike.1 f =?= (↑↑f).1 β–Ό
                          [] βœ… f.toFun =?= (↑↑f).1 β–Ό
                            [] βœ… (↑↑f).1 =?= (↑↑f).1 β–Ό
                              [] βœ… ↑↑f =?= ↑↑f β–Ό
                                [] βœ… (↑f).1 =?= (↑f).1 β–Ό
                                  [] βœ… ↑f =?= ↑f β–Ό
                                    [] βœ… f.1 =?= f.1
        [] βœ… R ⟢ S =?= ↑R β†’+* ↑S
        [] βœ… (CategoryTheory.forget RingCat).obj R =?= ↑R β–Ό
          [] βœ… (CategoryTheory.forget RingCat).toPrefunctor.1 R =?= R.1 β–Ό
            [] βœ… ↑R =?= R.1 β–Ό
              [] βœ… R.1 =?= R.1
        [] βœ… fun x ↦ (CategoryTheory.forget RingCat).obj S =?= fun x ↦ ↑S β–Ό
          [] βœ… (CategoryTheory.forget RingCat).obj R =?= ↑R
          [] βœ… (CategoryTheory.forget RingCat).obj S =?= ↑S β–Ό
            [] βœ… (CategoryTheory.forget RingCat).toPrefunctor.1 S =?= S.1 β–Ό
              [] βœ… ↑S =?= S.1 β–Ό
                [] βœ… S.1 =?= S.1

Matthew Ballard (Mar 22 2024 at 17:26):

Versus the unsuccessful with_reducible_and_instances rfl

[Meta.isDefEq] ❌ RingCat.instFunLike =?= RingHom.instFunLike β–Ό
  [] ❌ RingCat.instFunLike =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
    [] ❌ CategoryTheory.ConcreteCategory.instFunLike =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
      [] ❌ { coe := fun f ↦ (CategoryTheory.forget RingCat).map f,
            coe_injective' := β‹― } =?= { coe := fun f ↦ f.toFun, coe_injective' := β‹― } β–Ό
        [] ❌ fun f ↦ (CategoryTheory.forget RingCat).map f =?= fun f ↦ f.toFun β–Ό
          [] βœ… R ⟢ S =?= ↑R β†’+* ↑S β–Ό
            [] ❌ @Quiver.Hom =?= RingHom
            [onFailure] ❌ R ⟢ S =?= ↑R β†’+* ↑S
            [] βœ… CategoryTheory.BundledHom.MapHom SemiRingCat.AssocRingHom (@Ring.toSemiring) R.str S.str =?= ↑R β†’+* ↑S β–Ό
              [] βœ… SemiRingCat.AssocRingHom ↑R ↑S =?= ↑R β†’+* ↑S β–Ό
                [] βœ… ↑R β†’+* ↑S =?= ↑R β†’+* ↑S
          [] ❌ (CategoryTheory.forget RingCat).map f =?= f.toFun β–Ό
            [] ❌ (CategoryTheory.forget RingCat).toPrefunctor.2 f =?= (↑↑f).1 β–Ό
              [onFailure] ❌ (CategoryTheory.forget RingCat).toPrefunctor.2 f =?= (↑↑f).1

Matthew Ballard (Mar 22 2024 at 17:36):

This is a possibility

instance bundledHom : BundledHom AssocRingHom where
  toFun _ _ f := f
  id _ := RingHom.id _
  comp _ _ _ f g := f.comp g

Matthew Ballard (Mar 22 2024 at 17:37):

I see two non-reducible declarations in there

Matthew Ballard (Mar 22 2024 at 17:47):

No it is most likely non-reducibility of RingCat

Matthew Ballard (Mar 22 2024 at 17:51):

Hmm. Nope

Matthew Ballard (Mar 22 2024 at 17:57):

Well I fixed it Nevermind the error wasn’t highlighted

Matthew Ballard (Mar 22 2024 at 18:00):

Apparently you can delete deriving for LargeCategory RingCat and nothing between there and this file breaks

Johan Commelin (Mar 22 2024 at 18:03):

Lol, crazy mathlib facts you're spitting there (-;

Matthew Ballard (Mar 22 2024 at 18:12):

Ok now I think I really did fix it. Make RingCat reducible and then ConcreteCategory RingCat becomes a inferInstance but you need to change the proof to that. Or you can just get rid of the explicit LargeCategory and ConcreteCategory instances since Lean can find them with RingCat reducible

Matthew Ballard (Mar 22 2024 at 18:13):

And duh. That deletion I mentioned only works because I made RingCat reducible

Matthew Ballard (Mar 22 2024 at 18:14):

It seems fine to make RingCat reducible since it wraps Bundled Ring which is a structure instance

Matthew Ballard (Mar 22 2024 at 18:17):

I guess one should check performance to be safe

Matthew Ballard (Mar 22 2024 at 18:22):

Unfortunately it still doesn’t fix the error you originally presented since Bundled.\a is not reducible (I think)

Matthew Ballard (Mar 22 2024 at 18:28):

Making that reducible doesn’t help

Johan Commelin (Mar 22 2024 at 18:37):

Hmmz, ok. So maybe the conclusion should be that these lemmas are crossing API boundaries?

Matthew Ballard (Mar 22 2024 at 18:38):

I certainly think cleaner boundaries would help

Matthew Ballard (Mar 22 2024 at 18:49):

Here is _a_ fix to the original problem given the above is already implemented:

def bizzle (X : Cα΅’α΅–) : R.obj X β†’+* R.obj X := R.map (πŸ™ X)

instance (X : Cα΅’α΅–) : RingHomId (bizzle (R := R) X) where
  eq_id := sorry

Matthew Ballard (Mar 22 2024 at 18:50):

I haven’t tried to disentangle the two things

Matthew Ballard (Mar 22 2024 at 21:48):

#11595 makes SemiRingCat and RingCat reducible. Performance impact currently being assessed

Matthew Ballard (Mar 22 2024 at 22:14):

At worst neutral


Last updated: May 02 2025 at 03:31 UTC