You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classFunny (F : Type _) (A B : outParam (Type _)) where toFun : F → A → B
instance [Funny F A B] : CoeFun F fun _ => A → B where coe := Funny.toFun
classMulHomClass (F) (A B : outParam _) [Mul A] [Mul B] extends Funny F A B
classMonoid (M) extends Mul M
instance [Mul A] : Mul (Id A) := ‹_›
set_option trace.Meta.synthInstance true
example [Monoid A] [Monoid B] [MulHomClass F A B] (f : F) (a : A) : f a = f a := rfl -- infinite loop
The core issue is the instance:
MulHomClass.toFunny [Mul A] [Mul B] [MulHomClass F A B] : Funny F A B
When this instance is applied to Funny F ?A ?B (which is perfectly reasonable), we get a subgoal of the form Mul ?A (which is completely evil, as it causes us to enumerate all multiplicative structures).
We have tried various things in the past to address this issue, see 3f497b8 and #1852. But there doesn't seem to be a good description of the intended behavior we want in the end, and as a result these bandaid fixes rot over time.
For example 3f497b8 changes the arguments to {inst : Mul A} {inst : Mul B} because they're outParam-instances, in the projection construction. This was silently broken by several changes: 1) #1852 which removed the outParam-marker, and 2) by multiple inheritance for structures, where the non-subobject projections are generated differently.
The quickest short-term fix is probably to write a function makeOutParamArgsImplicit : Expr → CoreM Expr, which uses the same logic as checkOutParam and then call it from both mkCoercionToCopiedParent and mk_projections.
Reopening the issue. The initial bug was only intended to be a discussion starter so that we can converge on a solid design here.
There are still some open ends:
UX-wise, it is cumbersome and inconsistent to manually write {_ : Mul A} in instances. If you use a variable [Mul A] [MulHomClass F A B] block, you might need to duplicate it.
Potential scalability issues. With an instance like {inst1 : Group A} {inst2 : Group B} [MulHomClass F A B] : Funny F A B, the arguments inst1 and inst2 are filled in via nested type class search (due to the unification problems like @Group.toMul A ?inst1 =?= instMulA). AFAICT this completely disables the tabled resolution cache.
Ad 1, I think it's important to support the [Mul A] [MulHomClass F A B] syntax, if only for consistency. It also allows us to change the behavior in the future, without having to change the workaround all over mathlib. This could be as simple as a Boolean array stored in the instance entry (for arguments that are marked as instance-implicit but should be ignored during TC search).
Ad 2, one proposal that Mario brought up on Zulip was that subgoals such as [Mul A] here should be delayed after [MulHomClass F A B]. That is, the subgoals of @MulHomClass.toFunny F ?A ?B ?instMulA ?instMulB ?instMulHomClass when resolved with Funny F ?A ?B should be (in this order):
?instMulHomClass : @MulHomClass F ?A ?B ?instMulA ?instMulB
?instMulA : Mul ?A (note that ?A has been assigned after solving the first subgoal)
?instMulB : Mul ?B (note that ?B has been assigned after solving the first subgoal)
The reshuffling of the subgoals is of course completely useless by itself. In the previous example both ?instMulA and ?instMulB are already filled in via unification. It gets interesting with a funnyOfGroup [Group A] [Group B] [MulHomClass F A B] : Funny F A B instance. Here we would get the subgoal ?instMulHomClass : @MulHomClass F ?A ?B (@Group.toMul ?A ?instGroupA) (@Group.toMul ?B ?instGroupB). Also here, both ?instGroupA and ?instGroupB would be assigned after solving the subgoal.
What I would propose is to use the out-param preprocessing as inprocessing. The subgoals would then be:
?instMulHomClass : @MulHomClass F ?m1 ?m2 ?m3 ?m4 (all arguments depending on out-params are replaced by metavariables, avoiding the nested TC problems)
Unify ?m1 =?= ?A
Unify ?m2 =?= ?B
?instGroupA : Group ?A (?A has already been assigned so this is kosher)
Unify ?m3 =?= @Group.toMul ?A ?instGroupA (note that this doesn't require any mvar assignments or nested TC search)
?instGroupB : Group ?B (?B has already been assigned so this is kosher)
Unify ?m4 =?= @Group.toMul ?B ?instGroupB (note that this doesn't require any mvar assignments or nested TC search)
Note that nested TC problems from out-param type class arguments are particularly nasty, since the type (of the instance to be synthesized) can contain metavariables. For example, unifying @Group.toMul ?B ?groupB and @CommMonoid.toMul ?B ?commMonoidB would generate a nested TC problem for Group ?B or CommMonoid ?B (unsolvable since ?B is nonassignable).
(Incidentally this also improves the tabled resolution cache, because both MulHomClass F ?A ?B and MulHomClass F ?A ?B would get the same key (even though they have different type class instance arguments).)
(NB: this proposal would not get rid of all doubly nested TC problems. Consider e.g. the type MVPolynomial (I R : Type) [CommSemiring R] and the instance [CommRing R] : CommRing (MVPolynomial I R). Unifying the instance type with a given CommRing (MVPolynomial I R) subgoal requires us to solve the nested CommRing R problem (which notably doesn't have any mvars).)
Activity
gebner commentedon Nov 30, 2022
The quickest short-term fix is probably to write a function
makeOutParamArgsImplicit : Expr → CoreM Expr, which uses the same logic ascheckOutParamand then call it from bothmkCoercionToCopiedParentandmk_projections.eric-wieser commentedon Dec 1, 2022
Was @digama0's concern in 51a2909#commitcomment-90468951 addressed?
gebner commentedon Dec 1, 2022
Mario's question is unrelated to today's bug.
feat: port Algebra.Hom.Group (#659)
Squashed commit of the following:
Squashed commit of the following:
gebner commentedon Dec 12, 2022
Reopening the issue. The initial bug was only intended to be a discussion starter so that we can converge on a solid design here.
There are still some open ends:
{_ : Mul A}in instances. If you use avariable [Mul A] [MulHomClass F A B]block, you might need to duplicate it.{inst1 : Group A} {inst2 : Group B} [MulHomClass F A B] : Funny F A B, the argumentsinst1andinst2are filled in via nested type class search (due to the unification problems like@Group.toMul A ?inst1 =?= instMulA). AFAICT this completely disables the tabled resolution cache.Ad 1, I think it's important to support the
[Mul A] [MulHomClass F A B]syntax, if only for consistency. It also allows us to change the behavior in the future, without having to change the workaround all over mathlib. This could be as simple as a Boolean array stored in the instance entry (for arguments that are marked as instance-implicit but should be ignored during TC search).Ad 2, one proposal that Mario brought up on Zulip was that subgoals such as
[Mul A]here should be delayed after[MulHomClass F A B]. That is, the subgoals of@MulHomClass.toFunny F ?A ?B ?instMulA ?instMulB ?instMulHomClasswhen resolved withFunny F ?A ?Bshould be (in this order):?instMulHomClass : @MulHomClass F ?A ?B ?instMulA ?instMulB?instMulA : Mul ?A(note that?Ahas been assigned after solving the first subgoal)?instMulB : Mul ?B(note that?Bhas been assigned after solving the first subgoal)The reshuffling of the subgoals is of course completely useless by itself. In the previous example both
?instMulAand?instMulBare already filled in via unification. It gets interesting with afunnyOfGroup [Group A] [Group B] [MulHomClass F A B] : Funny F A Binstance. Here we would get the subgoal?instMulHomClass : @MulHomClass F ?A ?B (@Group.toMul ?A ?instGroupA) (@Group.toMul ?B ?instGroupB). Also here, both?instGroupAand?instGroupBwould be assigned after solving the subgoal.What I would propose is to use the out-param preprocessing as inprocessing. The subgoals would then be:
?instMulHomClass : @MulHomClass F ?m1 ?m2 ?m3 ?m4(all arguments depending on out-params are replaced by metavariables, avoiding the nested TC problems)?m1 =?= ?A?m2 =?= ?B?instGroupA : Group ?A(?Ahas already been assigned so this is kosher)?m3 =?= @Group.toMul ?A ?instGroupA(note that this doesn't require any mvar assignments or nested TC search)?instGroupB : Group ?B(?Bhas already been assigned so this is kosher)?m4 =?= @Group.toMul ?B ?instGroupB(note that this doesn't require any mvar assignments or nested TC search)Note that nested TC problems from out-param type class arguments are particularly nasty, since the type (of the instance to be synthesized) can contain metavariables. For example, unifying
@Group.toMul ?B ?groupBand@CommMonoid.toMul ?B ?commMonoidBwould generate a nested TC problem forGroup ?BorCommMonoid ?B(unsolvable since?Bis nonassignable).(Incidentally this also improves the tabled resolution cache, because both
MulHomClass F ?A ?BandMulHomClass F ?A ?Bwould get the same key (even though they have different type class instance arguments).)(NB: this proposal would not get rid of all doubly nested TC problems. Consider e.g. the type
MVPolynomial (I R : Type) [CommSemiring R]and the instance[CommRing R] : CommRing (MVPolynomial I R). Unifying the instance type with a givenCommRing (MVPolynomial I R)subgoal requires us to solve the nestedCommRing Rproblem (which notably doesn't have any mvars).)gebner commentedon May 19, 2023
Closed by #2117.