Tim
Tim

Reputation: 99428

What concept in category theory can be used to represent a typeclass?

In Haskell programming language, according to https://en.wikibooks.org/wiki/Haskell/Category_theory#Translating_categorical_concepts_into_Haskell

59.2.2 Translating categorical concepts into Haskell

  1. We work in the category Hask and its subcategories.

  2. Objects are types.

  3. Morphisms are functions.

  4. Things that take a type and return another type are type constructors.

  5. Things that take a function and return another function are higher-order functions.

  6. Typeclasses, along with the polymorphism they provide, make a nice way of capturing the fact that in category theory things are often defined over a number of objects at once.

What concept in category theory is a typeclass represented as? As a subcategory of Hask?

Upvotes: 5

Views: 542

Answers (1)

Bartosz Milewski
Bartosz Milewski

Reputation: 11630

According to Dominic Orchard, typeclasses correspond to subcategories of Hask:

The instances of a single parameter type class can be interpreted as describing the members of a set of types (or a relation on types for multi-parameter type classes). In a type signature, a universally quantified type variable constrained by a type class constraint represents a collection of types that are members of the class. E.g. for the Eq class, the following type signature describes a collection of types for which there are instances of Eq:

 Eq a => a

The members of Eq are a subcollection of the objects of Hask. Similarly, the type:

 (Eq a, Eq b) => (a -> b)

represents a subcollection of the morphisms of Hask mapping between objects in the subcollection of objects which are members of Eq. Thus, the Eq class defines an Eq-subcategory of Hask with the above subcollections of objects and morphisms.

Upvotes: 8

Related Questions