Building a class hierarchy in Coq?

后端 未结 1 1295
悲&欢浪女
悲&欢浪女 2021-02-14 10:34

I can naively construct a hierarchy of algebraic structures in Coq using type classes. I\'m having some trouble finding resources on Coq\'s syntax and semantics for type classes

相关标签:
1条回答
  • 2021-02-14 11:19

    References:

    The canonical references for type classes in Coq - beyond the manual - are this paper, and the thesis (in french) of Matthieu Sozeau. There are less canonical references (with different points of view) at the research level in a recent paper, and in my thesis. You should also spend some time on the #coq channel on Freenode, and subscribe to the mailing list.

    Your problem:

    The syntax issue is not with Classes per se, but with maximally inserted implicit arguments. The Monoid and AbelianMonoid types have in your definition (implicit) parametric arguments that are, in this order, the domain type, the operation, and the identity - as indexed by the dependent product that you see fully expanded when you print those record types. They are filled automatically when you mention the dependent product without its arguments in a position where it would need them.

    Indeed, implicit argument resolution will automatically insert the required parametric arguments to be identical (for both products that depend on them : P and M's types) if left to its own devices. You just need to specify constraints between those identifiers by specifying variables for the various identifiers, distinct when appropriate :

    Class Semiring A mul add `(P : AbelianMonoid A mul) `(M : Monoid A add) := {
    }.
    

    The result :

    > Print Semiring.
    Record Semiring (A : Type) (mul add : A -> A -> A) 
    (M0 : Semigroup mul) (id0 : A) (M : Monoid M0 id0) 
    (P : AbelianMonoid M) (M1 : Semigroup add) (id1 : A) 
    (M : Monoid M1 id1) : Type := Build_Semiring {  }
    
    For Semiring: Arguments M0, id0, M, M1, id1 are implicit and maximally
                  inserted
    For Semiring: Argument scopes are [type_scope _ _ _ _ _ _ _ _ _]
    For Build_Semiring: Argument scopes are [type_scope _ _ _ _ _ _ _ _ _]
    

    Note the identities for the abelian monoid and monoid are this time distinct. It's a good exercise (even if it makes little mathematical sense) to train yourself to write the record type (aka the Class) you would want if you had the same identity element for the additive and multiplicative structures.

    0 讨论(0)
提交回复
热议问题