Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Preventing pollution of typeclass resolution when Require Importing a MathClasses module

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Preventing pollution of typeclass resolution when Require Importing a MathClasses module


Chronological Thread 
  • From: Yannick Zakowski <zakowski AT seas.upenn.edu>
  • To: coq-club AT inria.fr
  • Subject: Re: [Coq-Club] Preventing pollution of typeclass resolution when Require Importing a MathClasses module
  • Date: Thu, 16 Jul 2020 13:21:21 -0400
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=zakowski AT seas.upenn.edu; spf=Pass smtp.mailfrom=zakowski AT seas.upenn.edu; spf=None smtp.helo=postmaster AT mail-qk1-f176.google.com
  • Ironport-phdr: 9a23:3kF/aRy9jYyIrSDXCy+O+j09IxM/srCxBDY+r6Qd2+8QIJqq85mqBkHD//Il1AaPAdyFrakYwLOP6ujJYi8p2d65qncMcZhBBVcuqP49uEgeOvODElDxN/XwbiY3T4xoXV5h+GynYwAOQJ6tL1LdrWev4jEMBx7xKRR6JvjvGo7Vks+7y/2+94fcbglVhTexe71/IRq5oQnMq8UanJZpJ7osxBfOvnZGYfldy3lyJVKUkRb858Ow84Bm/i9Npf8v9NNOXLvjcaggQrNWEDopM2Yu5M32rhbDVheA5mEdUmoNjBVFBRXO4QzgUZfwtiv6sfd92DWfMMbrQ704RSiu4qF2QxLulSwJNSM28HvPh8JtjKxVvhyhqR9xzYHab46aNuZxc7jHct8GX2dMRNpdWiJDD466coABD/ABPeFdr4TlpVQBtx2+BQ+tBOzzyT9Dm3z50rc50+QmHwDGxw8gH9MTu3nTrdT6LqQSUee7zanTzjXDbuhb2Szj54fSaRAtu+yMXapufsrXz0kjDR3KgUiNqYH8OT6ey+sCvXSB4eV6SeKvl3Aoqxt3ojW3xcohl4fHi58Lx13F9ih13og7K9O3RUJnfNKoDpVeuSGeOoZ4TM4uXmVltTomxrMJvZO3YDQGxpsmyhDQafGJfI6F6Q/gWuaJOTp0mm5pdbalixux8UWs0PPwWteq3FpQsyZIkNjBumgM2hHX8MSLVPtw80m71TqRywzf9vtILEE6mKfdNpUv2KQ/loAJvkTGBiL2mFv5jKuRdkg8/+in8eXnYrH/qp+FNI94lxjyMqozlsG9AOk0KAcOX2+c+eSz0L3s41f1T6lNjv0ziqXZsZbaKtoHpqOhHQNZzoIu5wy8AjqmytgUg2cLIExfdB6ajYXkOknCIPXiAve+h1Ssni1rx/fDPrD5A5TCNHvDkLDvfLZm6E5cyRE+zctB6J1OEbENOu/zWlfvu9zeCB81KRK7zPv6CNllzIMRRXqPArOFMKPVqVKH+uUvI/CVaIAJvDb9NuMq6uX1jX45nF8dZbOm0YEWaHC+BPRmIl+WbWDigtcbQi82uV81S/Wvg1mfWxZSYWyzVuQy/GIVEoWjWLbKQIasi7/J7i64GYJNZWEOXkKFEnD3bIaFc+wBYTnUP9dslDpCWLS8HdxynSqyvRP3nuI0ZtHf/TcV4Mq6iIpFotbLnBR3zgRaStyH2jjTHXp5l3hOWic72qY5rEBgmA/agPpIxsdAHNkW3MtnFwIzNJrS1et/Uo6gQQ/IZZGUUFugRJOrDSxjF4tske9LWF50HpCZtj6G3yeuBOVLxbmCBZhx66GFmnaoeJg7xHHB260syVIhR5kXOA==

Apologies for the duplicate email, but I had made an idiotic typo:

Remove Hints equivalence_proper : typeclass_instances.

Without the plural does work. It barely shortens the trace though, several other hints are problematic.

My take away so far is that there is currently no good support for the problem at large, but that I can remove manually Instances and non-external hints.

I will try to see if I can reach a workable situation with this.

Thanks for all the help,
Yannick


On 7/16/20 1:11 PM, Yannick Zakowski wrote:

On 7/16/20 12:16 PM, Matthieu Sozeau wrote:

Le 16 juil. 2020 à 15:45, Yannick Zakowski <zakowski AT seas.upenn.edu> a écrit :

Hello,

That's a sensible suggestion, unfortunately I do not quite understand why this hint gets applied. In the minimal example from my original message, the beginning of the debug trace goes as follows:

Debug: 1: looking for (Proper (rel ==> ?r) op) with backtracking
Debug: 1.1: exact rel_op_Proper on (Proper (rel ==> ?r) op), 0 subgoal(s)
Debug: 2: looking for (Proper (rel ==> ?r ==> flip impl) rel) with backtracking
Debug: 2.1: simple apply @sm_proper on (Proper (rel ==> ?r ==> flip impl) rel), 1 subgoal(s)
Debug: 2.1-1 : (Setoid_Morphism rel)

We are looking for a Proper instance as expected, nothing in this goal explicitly refers to equiv (i.e. "=" from MathClasses perspective), and yet `sm_proper` gets applied.
Maybe then declaring `Typeclasses Opaque equiv` will avoid this unexpected unification. Also, it looks like this application 
(`simple apply @sm_proper`) is coming from a regular hint rather than the extern hint you mentioned (they are marked with
*external* in the traces).
— Matthieu

`Typeclasses Opaque equiv` does not change the behavior.

You are however absolutely right, raising the level of debug to 2 shows something that makes more sense: there is a file "MathClasses.misc.workarounds.v" that actually contains the real culprit this time I think:

Instance equivalence_proper `{Equivalence A R} : Proper (R ==> R ==> iff) R | 0 := _.

So that any search for a Proper instance gets hijacked into an Equivalence one!

I gave a naive shot at

Existing Instance equivalence_proper | 50.

and:

Remove Hints equivalence_proper : typeclasses_instances.

to try to stop it from triggering first, but to no success in both cases.
Would you have a suggestion to kill such an instance?

Thanks,
Yannick

Yannick

On 7/16/20 6:23 AM, Matthieu Sozeau wrote:
Dear Yannick,


Le 15 juil. 2020 à 16:32, Yannick Zakowski <zakowski AT seas.upenn.edu>
 a écrit :

Dear all,

Consider the following self-contained example:

From Coq Require Import
     Setoid
     Morphisms
     RelationClasses.

Section Foo.

  Variable op: nat -> nat.

  Variable rel: nat -> nat -> Prop.

  Context {rel_op_Proper: (Proper (rel ==> rel) op)}.
  Context {rel_trans: Transitive rel}.

  Goal forall (a b: nat) (REL: rel a b),
      rel (op a) (op b).
  Proof.
    intros.
    Typeclasses eauto := debug.
    rewrite REL.
    (* Resolution in 6 steps *)
    Undo.
    Require Import MathClasses.interfaces.abstract_algebra.
    rewrite REL.
    (* Resolution in over 100 steps *)
  Abort.

End Foo.

I am looking for a way to avoid this side-effect of loading this MathClass module.
The problem touches to the overall issues of Hints being aggressively exported, and instances all sharing a single namespace. A general solution would naturally be welcome, but I would already be very happy with a hacky local solution allowing me to get back into my proof. In this specific case, it indeed looks to me like the main culprit that gets applied early and completely derail the proof search is the following External Hint declared in `abstract_algebra.v`:

Hint Extern 4 (?f _ = ?f _) => unshelve eapply (sm_proper (f:=f)).

I have given a shot at `Set Loose Hint Behavior "Strict".` without success. For good conscience I also tried `Remove Hints sm_proper: core_db.`, but as described in the documentation, it does not apply to External Hints.

Any idea as to how to solve this problem would help me a great deal.

I don’t see a way to avoid the import of instances from math-classes if you have to Require Import. 
However, the shape of the hint here might leave some hope for finding a workaround, because generalized
rewriting itself does not generate `(_ = _)` subgoals to be solved by type class resolution, only
`Proper R f`, `ProperProxy R f` and `subrelation R R’` goals. So you might have more luck disabling
the hint that produces the `(_ = _)` subgoal?

Best,
— Matthieu




Archive powered by MHonArc 2.6.19+.

Top of Page