Zulip Chat Archive

Stream: mathlib4

Topic: Algebra.Module.Equiv mathlib4#1732


Jakob von Raumer (Jan 23 2023 at 10:05):

I have the issue that we can't infer FunLike (M ≃ₛₗ[σ] M₂) M (fun _ => M₂) anymore, which I think might either be because, as #lint says, there's something wrong with the instance : SemilinearEquivClass (M ≃ₛₗ[σ] M₂) σ M M₂ or, as the trace says, type class inference is running into some really weird dead end:

(Module.EndCat
          (Module.EndCat
            (Module.EndCat
              (Module.EndCat
                (Module.EndCat
                  (Module.EndCat
                    (Module.EndCat
                      (Module.EndCat
                        (Module.EndCat (Module.EndCat (Module.EndCat (Module.EndCat (Module.EndCat  M₂) M₂) M₂) M₂) M₂)
                        M₂)
                      M₂)
                    M₂)
                  M₂)
                M₂)
              M₂)
            M₂)
          M₂)
````

Jakob von Raumer (Jan 23 2023 at 10:29):

Looks like @LinearMap.applyModule shouldn't be an instance, then?

Anne Baanen (Jan 23 2023 at 10:45):

I suppose most instances on Module.EndCat are going to give issues since it's merely an abbrev. So probably we'll have to make EndCat @[irreducible]?

Eric Wieser (Jan 23 2023 at 10:54):

docs4#LinearMap.applyModule doesn't need to be an instance on EndCat if that's causing problems'; the instance would have been fine in Lean 3 on M →ₗ[R] M too

Eric Wieser (Jan 23 2023 at 10:59):

also docs4#Module.EndCat is a porting error, and should be called Module.End to match docs4#Monoid.End (https://github.com/leanprover-community/mathlib4/pull/1778)

Jakob von Raumer (Jan 23 2023 at 11:48):

Eric Wieser said:

docs4#LinearMap.applyModule doesn't need to be an instance on EndCat if that's causing problems'; the instance would have been fine in Lean 3 on M →ₗ[R] M too

That's not it, changing it to M →ₗ[R] M gives the same chain

Eric Wieser (Jan 23 2023 at 11:49):

It's good to know that the abbrev isn't the cause. This instance wasn't a problem in Lean 3...

Jakob von Raumer (Jan 23 2023 at 11:59):

Maybe it's the other problem I described above and it only runs into the dead end because it does not find the correct instance before.

Jakob von Raumer (Jan 23 2023 at 12:00):

So why is instance : SemilinearEquivClass (M ≃ₛₗ[σ] M₂) σ M, according to #lint, causing

#check @SemilinearEquivClass.instSemilinearMapClass /- generates subgoals with metavariables: argument 6 inst✝⁷ : Semiring
  ?R, argument 7 inst✝⁶ : Semiring
  ?S, argument 8 inst✝⁵ : AddCommMonoid
  ?M, argument 9 inst✝⁴ : AddCommMonoid ?M₂, argument 14 inst✝¹ : RingHomInvPair ?σ ?σ' -/

Jakob von Raumer (Jan 23 2023 at 12:02):

Jakob von Raumer said:

Maybe it's the other problem I described above and it only runs into the dead end because it does not find the correct instance before.

Oh wait, that can't really be the case becaus it successfully infers EmbeddingLike (M ≃ₛₗ[σ] M₂) M M₂

Jakob von Raumer (Jan 23 2023 at 13:56):

I think I solved those issues by adding a FunLike instance which hopefully is not a doublet

Eric Wieser (Jan 23 2023 at 14:29):

What's the new instance?

Jakob von Raumer (Jan 23 2023 at 14:50):

instance : FunLike (M ≃ₛₗ[σ] M₂) M (fun _ => M₂) := ⟨(·.toFun),
  fun _ _ _ => by { apply toLinearMap_injective; apply FunLike.coe_injective; assumption } 

Eric Wieser (Jan 23 2023 at 15:15):

That's a bad instance, because it unfolds to the wrong thing

Eric Wieser (Jan 23 2023 at 15:15):

instance : FunLike (M ≃ₛₗ[σ] M₂) M (fun _ => M₂) := FunLike.coe,
  fun _ _ _ => by { apply toLinearMap_injective; apply FunLike.coe_injective; assumption } 

would be ok

Jakob von Raumer (Jan 23 2023 at 16:06):

Your suggestion only makes sense if there's already a FunLike instance.

Jakob von Raumer (Jan 23 2023 at 16:10):

There might or might not be one, but adding one definitely solves type inferernce running into the :end: :cat: problem

Eric Wieser (Jan 23 2023 at 16:11):

Jakob von Raumer said:

Your suggestion only makes sense if there's already a FunLike instance.

There is, via SemilinearEquivClass.instSemilinearMapClass

Eric Wieser (Jan 23 2023 at 16:11):

Perhaps providing the SemilinearMapClass instance directly helps?

Eric Wieser (Jan 23 2023 at 16:11):

That doesn't have the problem of unfolding badly like your approach does

Jakob von Raumer (Jan 23 2023 at 16:14):

I still don't really get whether the EndCat dead end and the FunLike instance not being found are two separate issues or just one

Jakob von Raumer (Jan 23 2023 at 19:41):

Eric Wieser said:

Perhaps providing the SemilinearMapClass instance directly helps?

This works:

instance : FunLike (M ≃ₛₗ[σ] M₂) M (fun _ => M₂) := EmbeddingLike.toFunLike

Eric Wieser (Jan 23 2023 at 21:44):

That looks safe to me

Jakob von Raumer (Jan 24 2023 at 11:41):

Still bad that it's needed at all to not descent into the EndCat stuff

Eric Wieser (Jan 24 2023 at 11:58):

We had this type of shortcut instance in Lean3, implemented in the style you suggested earlier

Eric Wieser (Jan 24 2023 at 11:58):

For now we've just removed all those shortcuts because they mean the wrong thing with the new coe

Eric Wieser (Jan 24 2023 at 11:58):

But maybe we should instead be replacing them with shortcuts in the style of your most recent suggestion

Johan Commelin (Jan 24 2023 at 12:00):

@Jakob von Raumer Note that EndCat was an oopsie by mathport. It should just be called End. This has been fixed in master.

Eric Wieser (Jan 24 2023 at 12:01):

(docs4#Module.End)

Jakob von Raumer (Jan 24 2023 at 12:02):

But right now, in Lean 3 we don't require any short cut, or am I missing something?

Jakob von Raumer (Jan 24 2023 at 12:03):

@Johan Commelin Thanks, just using it as a catchy name for the issue described at the start of the thread here :grinning_face_with_smiling_eyes:

Jakob von Raumer (Jan 24 2023 at 15:37):

When trace.Meta.synthInstance says apply X to Y, then Y must have been a subgoal before, right? Because I don't know why it attempts to solve Module (Module.EndCat ?m.64808 M₂) M₂ in the first place, that is obviously a bad subgoal to have with that metavar.

Jakob von Raumer (Jan 24 2023 at 17:41):

Okay, the error is definitely not in Algebra.Module.Equiv itself: Inferring FunLike (R →+* S) R fun a => S runs into the same :cat: issue

Eric Wieser (Jan 24 2023 at 17:43):

Does the error message actually mention Cat? Can you give a mwe?

Jakob von Raumer (Jan 24 2023 at 17:51):

Took me a while to actually get the message to display, since it's too big to handle, but it does not mention the cat, instead it gets worked up in this:

 []  apply Nat.addMonoid to AddMonoid  
  [resume] propagating AddMonoid  to subgoal AddMonoid  of AddMonoid ᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈ to subgoal AddMonoid ᵒᵈ of AddMonoid ᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 
  [resume] propagating AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ to subgoal AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ of AddMonoid ᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈᵒᵈ 

Jakob von Raumer (Jan 24 2023 at 17:54):

Aha, all is good at the top of the file, but this instance seems to break it already:

Jakob von Raumer (Jan 24 2023 at 17:54):

instance (priority := 100) [RingHomInvPair σ σ'] [RingHomInvPair σ' σ]
    [s : SemilinearEquivClass F σ M M₂] : SemilinearMapClass F σ M M₂ :=
  { s with
    coe := (s.coe : F  M  M₂)
    coe_injective' := @FunLike.coe_injective F _ _ _ }

Jakob von Raumer (Jan 24 2023 at 17:57):

I thin I'm close to getting what's wrong

Jakob von Raumer (Jan 24 2023 at 18:02):

Nah I don't

Jakob von Raumer (Jan 24 2023 at 18:08):

But I guess it's just that the linter's warning that the instance is dangerious is on point...

Jakob von Raumer (Jan 24 2023 at 18:39):

-- σ' becomes a metavariable, but it's OK since it's an outparam the comment above it says

Anne Baanen (Jan 25 2023 at 15:16):

The Lean 4 dangerous instance linter now handles outParams correctly, so I don't think we should easily assume false positives anymore. In this case the comment is probably not applicable anymore. In particular the comment doesn't apply anymore: Lean 3 metavariables would block synthesis, so Lean would first search for a RingHomInvPair instance, find a value for σ' and be ready to start inferring the rest. Lean 4 often proceeds with synthesis even if there are still metavariables, so the presence of outParams doesn't help.

Jakob von Raumer (Jan 25 2023 at 22:10):

@Sebastian Ullrich has meanwhile pointed me to the existence of the attribute @[infer_tc_goals_rl] which does solve the issue for this particular instance. Not sure if it's a super stable workaround, though

Jakob von Raumer (Jan 26 2023 at 14:33):

Should be ready to review, with the @[infer_tc_goals_rl] trick at least


Last updated: Dec 20 2023 at 11:08 UTC