Subtyping and Algebraic Subtyping
Subtyping is mathematically represented as a subset relation,
The Subsumption Rule
In a type system with subtyping, a type
The subsumption rule can be expressed as:
This means that if we have an expression
Basic Subtype Relationships
Reflexivity of Subtyping
Subtyping is reflexive, implying any type
This property is foundational to any preorder relation that subtyping models.
Transitivity of Subtyping
Transitivity guarantees consistency across multiple subtyping relations. If
Functions
The subtyping relation for function types follows contravariant behavior in the parameter type and covariant behavior in the return type:
Here,
Union and Intersection Types
Union Types
Union types, denoted
This expresses that the union of subtypes remains a subtype of the union of their respective supertypes.
Intersection Types
Intersection types, denoted
The intersection of supertypes forms a supertype of the intersection of their respective subtypes.
Relationships
For function types, union and intersection obey distributive laws:
This states that the intersection of function types corresponds to the union of parameter types and the intersection of return types.
For example:
Similarly, the union of function types results from the intersection of parameter types and the union of return types.
For example:
Records
Subtyping for record types aligns with structural subtyping, where records are compatible if they share the same fields and types. The typing rule for record types is:
Here,
Field access ensures type preservation:
Subtyping among records supports the subset relation:
If fields
Pointwise subtyping applies across all fields in records:
Union and Intersection Rules for Record Types
In algebraic subtyping, union (
Union of Record Types
The union of two record types combines their fields, allowing overlapping fields to resolve to the union of their respective types:
- For shared fields
, the resulting type is the union of their individual types . - For fields exclusive to one record (e.g.,
or ), their types remain unchanged.
For example:
Intersection of Record Types
The intersection of two record types combines their fields similarly, but overlapping fields resolve to the intersection of their types:
- Only fields common to both records (
) are preserved in the resulting record. - For shared fields, the resulting type is the intersection of their individual types
. - Fields exclusive to one record are excluded from the intersection.
For example:
Subtyping for Recursive Type
Dependent Types
Dependent types extend the expressive power of type systems by allowing types to depend on values or other types. Algebraic subtyping supports dependent types, incorporating union and intersection constructs to define relationships between dependent types effectively.
Subtyping Relationships
Dependent -Type
The dependent
Dependent -Type
The dependent
Intersection of Dependent -Types
Type Formation Rule for Intersection of Dependent -Types
Dependent
Refinement ensures that
Type Formation Rule for Intersection of Dependent -Types
Dependent
Union of Dependent -Types
Type Formation Rule for Union of Dependent -Types
The union of dependent
Type Formation Rule for Union of Dependent -Types
The union of dependent