Skip to content

Type class parameters in type classes #1901

Closed

Description

@gebner

This issue came up again today on Zulip:

class Funny (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
class MulHomClass (F) (A B : outParam _) [Mul A] [Mul B] extends Funny F A B
class Monoid (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.

Activity

  1. added
    Mathlib4 high prio
    on Nov 30, 2022
  2. gebner commented on Nov 30, 2022

    @gebner
    MemberAuthor

    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.

  3. eric-wieser commented on Dec 1, 2022

    @eric-wieser
    Contributor

    Was @digama0's concern in 51a2909#commitcomment-90468951 addressed?

  4. gebner commented on Dec 1, 2022

    @gebner
    MemberAuthor

    Mario's question is unrelated to today's bug.

  5. added a commit that references this issue on Dec 2, 2022
  6. added 2 commits that reference this issue on Dec 10, 2022
    aef704d
    07d2fae
  7. reopened this on Dec 12, 2022
  8. gebner commented on Dec 12, 2022

    @gebner
    MemberAuthor

    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:

    1. 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.
    2. 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).)

  9. self-assigned this
    on Feb 10, 2023
  10. gebner commented on May 19, 2023

    @gebner
    MemberAuthor

    Closed by #2117.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Mathlib4 high prio

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions