Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Canonical Structures vs. Type Classes vs. Module Systems

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Canonical Structures vs. Type Classes vs. Module Systems


Chronological Thread 
  • From: Matthieu Sozeau <mattam AT mattam.org>
  • To: coq-club AT inria.fr
  • Cc: John Wiegley <johnw AT newartisans.com>
  • Subject: Re: [Coq-Club] Canonical Structures vs. Type Classes vs. Module Systems
  • Date: Fri, 13 Apr 2018 16:30:16 +0000
  • Authentication-results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=mattam AT mattam.org; spf=Pass smtp.mailfrom=matthieu.sozeau AT gmail.com; spf=None smtp.helo=postmaster AT mail-wr0-f175.google.com
  • Ironport-phdr: 9a23:ud1WrRMa58BNY/DqNBMl6mtUPXoX/o7sNwtQ0KIMzox0LfXzrarrMEGX3/hxlliBBdydt6ofzbKO+4nbGkU4qa6bt34DdJEeHzQksu4x2zIaPcieFEfgJ+TrZSFpVO5LVVti4m3peRMNQJW2aFLduGC94iAPERvjKwV1Ov71GonPhMiryuy+4ZLebxlGiTanfb9+MAi9oBnMuMURnYZsMLs6xAHTontPdeRWxGdoKkyWkh3h+Mq+/4Nt/jpJtf45+MFOTav1f6IjTbxFFzsmKHw65NfqtRbYUwSC4GYXX3gMnRpJBwjF6wz6Xov0vyDnuOdxxDWWMMvrRr0vRz+s87lkRwPpiCcfNj427mfXitBrjKlGpB6tvgFzz5LIbI2QMvd1Y6HTcs4ARWdZUclRWS5ODIOyYYUMEuQPI/pXopLnqFcStxazHxWgCP/txzJOm3T43bc60+MkEQze2wIvBc4OsGjUrNX0KqgSTP2+wrTPzTTCcfxZwyr945XPfxAku/6MQa5wftDUyUQ0EgPKlEmQqZD/MDOQzOgNtHKb7+V5WO+plmUppQZxoj21ycctjInEnoQVxU7K9Cpj2oY1Ody4SFVhbt6iC5tcrT2VN4xzQs4kXmpmuz46x6UYtZKneCUG0pcqyh7FZ/CabYSF4QjvWPuTLDp4gn9uZaixiAyo8Ue6z+3xTsm030hOripCitTMs2oC1x3X6sSeRPp95Fut1S+B1wzO6OxJIVo4laXcK54mzb4wkoQcvV7fES/xnUX6lK6WdkM69ei08+nrfKnqq5uGO4J3igzyKLkiltK8DOgiLwQDXWiW9fy51LL5/E35RLtKjucxkqncqJ3aIN4UprKlAw9V04Ys9Qu/Ay290NQfnHgIMkhKeA+cgojmPlHBOvH4DfOlj1uwlzdrwujKPqf9DZXVMnjDjLDhcK5h5E5b0Qo/1MxQ55ZJCr4aO//zQU/wtNnADhAjKQC0wuDnCM981owEQ26PDLWZY+vutgqD4ftqKO2RbqcUviz8Ir4r/a3Al3g8zHoUYbWp3JYKIEu/DPlvPg3NZHPwnt4EOWIDohY3SartklLUAm0bXGq7Q69pvmJzM4mhF4qWA9n12OXQjhf+JYVfYyV9Mn7JFH7pc4ueXPJVMXCdJ8ZglnoPUr3zEtZ9hyHrjxfzzv9cFsSR4jcR7Mix0dFp++TW0xYo+m4sVpnP4yS2V2hx21gwaXo20aR4+xIvz16C1e1nmaQdG4UMurVGVQA1MZOaxOt/WYj/

Hi Yao,

  If you understand well the way canonical structures operate then you can do many things similarly to type classes (e.g. overlapping and priorities, and more generally proof-search), but you have to do a non-trivial amount of massaging of definitions (see Ziliani's et al ICFP paper and Mahboubi and Tassi's paper on "Canonical Structures for the working Coq user" for some expositions of what is involved). Mainly the ability of type classes to be driven by Hint Extern give them kind of unbounded (and sometimes hard to control) expressivity.

  There is a kind of informal continuum between type classes and canonical structures: type classes allow indexing by parameters of the record type while canonical structures allow indexing by their fields. In particular canonical structures avoid a combinatorial explosion in term size, type-checking and unification/reduction time when your class has many indexes that is due to the naïve representation of record projections as defined constants taking all parameters of the record as argument. The primitive projections feature hopefully alleviates most of this problem (you still get larger proof contexts/typeclass binders in the "unpacked"/"unbundled" style), but I don't think a proper study of the performance differences on practical, at least medium sizes examples has been pursued yet. The Mathematical Components team has refined the canonical structure usage to an art, after hitting problems that only appear in really large developments. 

  The fact that canonical structures are mixed with unification can also help in some cases where "cascading"/nested resolutions are necessary and relying on just the dependency order followed by type class resolution is not enough.

Hope this helps,
-- Matthieu

On Fri, Apr 13, 2018 at 5:07 PM Yao Li <liyao AT seas.upenn.edu> wrote:
That is a very good argument. Thanks!

Still I wonder what’s the reason that some libraries like mathcomp choose to use canonical structures? What are the advantages of them comparing with type classes?

Best,
Yao

> On 12 Apr 2018, at 3:05 PM, John Wiegley <johnw AT newartisans.com> wrote:
>
>>>>>> "YL" == Yao Li <liyao AT seas.upenn.edu> writes:
>
> YL> 2. How do people usually choose among canonical structures, type classes,
> YL> and module systems? Do you have some specific use cases that only one of
> YL> them is most suitable? Or do you think people should only use one of them
> YL> (and which one would it be)?
>
> I usually choose type classes because: The errors I get from misusing
> canonical structures continue to mystify me, and module functors do not
> extract to Haskell. Type classes are at least familiar, allow for setting
> priorities, and come with Show Typeclasses Debug.
>
> --
> John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
> http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2




Archive powered by MHonArc 2.6.18.

Top of Page