Typeclasses and Canonical Structures - coq/coq GitHub Wiki

(Part of the Coq FAQ)

What are the differences between type classes and canonical structures?

There is varied opinion about when to use Typeclasses versus when to use Canonical Structures. Some references are listed below:

The mathcomp (Mathematical Components) library uses Canonical Structures, as described in the mathcomp book.

The general opinion seems to be that:

  • Coq typeclasses allow multiple instances, unlike Haskell Typeclasses.
  • The typeclass resolution mechanism uses more powerful unification rules and can be heavily controlled (Hint Mode, Typeclasses Opaque), but is also harder to predict.
  • Debugging typeclasses is harder due to their greater power (in particular, Hint Extern). On the other hand, there is Set Typeclasses Debug (and Set Typeclasses Debug Verbosity 2); no similar mechanism exists for canonical structures.
  • Typeclass resolution can be slow in large developments, and typeclasses can lead to huge terms because of redundancy in class indices.
  • Canonical structures are simple and their unification rule is easy to reason about. However, the way they are triggered by projections can be hard to understand, whereas typeclasses can be explained as "just" an inference mechanism that fills holes.

Debugging

See https://github.com/coq/coq/wiki/Troubleshooting#what-can-i-do-when-setoid_rewrite-hangs