define-class defines a type class, its superclass constraints, functional dependencies, and method signatures.
Syntax
(define-class (⟨superclass-context⟩? ⟨class-name⟩ ⟨type-var⟩+ ⟨fundep⟩*)
⟨docstring⟩?
⟨method-decl⟩*)
;; ⟨superclass-context⟩ := ⟨predicate⟩ =>
;; | (⟨predicate⟩)+ =>
;; ⟨predicate⟩ := ⟨superclass-name⟩ ⟨type⟩+
;; ⟨fundep⟩ := (⟨type-var⟩+ -> ⟨type-var⟩+)
;; ⟨method-decl⟩ := (⟨method-name⟩ ⟨docstring⟩? ⟨qualified-type⟩)
Semantics
define-classis a top-level form valid insidecoalton-toplevel.⟨class-name⟩and all⟨method-name⟩symbols must belong to the current package.- A class must have one or more distinct keyword type parameters
⟨type-var⟩+. Parameter kinds (Type,Type -> Type, etc.) are inferred from superclass predicates and method signatures. - Superclasses: Any predicates before
=>are superclass requirements. Defining an instance of⟨class-name⟩requires instances for all superclasses to be available. Cyclic superclass hierarchies are rejected. - Functional dependencies: Each
(⟨from-var⟩+ -> ⟨to-var⟩+)clause declares that the types bound to⟨from-var⟩+uniquely determine⟨to-var⟩+. The compiler uses functional dependencies to improve types during inference and rejects instances that violate determinism. A class’s functional dependencies cannot be changed by redefinition.- A superclass can determine a type that is not a parameter of the subclass. For example, if
A :x :ydeclares(:x -> :y), two subclasses requiringA :x :yagree on:ywhenever they are used with the same:x. Neither subclass needs to expose:yas a parameter. - To return that hidden type from a function, name it in the function’s constraints. For example, if
Child :xrequiresA :x :y, then((Child :x) (A :x :y) => :x -> :y)ties the result to the superclass;(Child :x => :x -> :y)does not. The order of the constraints does not matter.
- A superclass can determine a type that is not a parameter of the subclass. For example, if
- Methods: Each
(⟨method-name⟩ ⟨docstring⟩? ⟨qualified-type⟩)declares a generic function in the current package whose full type adds(⟨class-name⟩ ⟨type-var⟩+)to the front of⟨qualified-type⟩’s constraint context.- Every class type parameter must be reachable from each method’s type. A chain of functional dependencies can use both the class’s own dependencies and those of its superclasses.
- Method types may introduce additional type variables, additional constraints, keyword parameters (
&key), and explicitforallbinders. - Explicit
forallbinders in a method declaration are scoped into the corresponding method definition insidedefine-instance.
- Marker classes: A class with zero methods is permitted and acts as a type-level predicate that must still be instantiated with
define-instance.
Examples
(coalton-toplevel
;; Single-parameter class with a superclass constraint
(define-class (Eq :a => Measurable :a)
"Types with a non-negative integer size."
(measure "Return the size of a value." (:a -> UFix)))
;; Multi-parameter class with a functional dependency:
;; the collection type `:c` uniquely determines the element type `:e`.
(define-class (Collectable :c :e (:c -> :e))
(empty-collection (Void -> :c))
(insert-item (:c * :e -> :c)))
;; Marker class with no methods
(define-class (Sendable :a)
"Marks types that may cross a channel boundary."))
Other notes
- Coalton compiles type classes using dictionary passing: each class compiles to a struct/class holding its superclass dictionaries and method functions, and constrained functions receive the required dictionaries as hidden arguments.
- See
define-instance,derive, andoverlap.