Skip to content

Subtyping and Algebraic Subtyping

Subtyping is mathematically represented as a subset relation, AB, such that tA,tB. This interpretation allows subtyping to reflect a preorder relation, which becomes a partial order under equivalence (ABBAA=B). The introduction of least upper bounds (lub, AB) and greatest lower bounds (glb, AB) converts this structure into a lattice. When combined with the universal top type () and bottom type (), the lattice becomes bounded, satisfying:

T, TandT, T

The Subsumption Rule

In a type system with subtyping, a type A can be considered a subtype of another type B if A satisfies all the constraints and properties of B, and possibly some additional ones. The subsumption rule formalizes this idea by allowing expressions of a more specific type to be treated as expressions of a more general type.

The subsumption rule can be expressed as:

ABΓe:AΓe:B

This means that if we have an expression e of type A, and we know that A is a subtype of B (denoted AB), then we can treat e as an expression of type B without any further checks.

Basic Subtype Relationships

Reflexivity of Subtyping

Subtyping is reflexive, implying any type T is trivially a subtype of itself:

TT

This property is foundational to any preorder relation that subtyping models.

Transitivity of Subtyping

Transitivity guarantees consistency across multiple subtyping relations. If T1T2 and T2T3, then T1T3. Formally:

ΓT1T2ΓT2T3ΓT1T3

Functions

The subtyping relation for function types follows contravariant behavior in the parameter type and covariant behavior in the return type:

ΓA1B1A2B2ΓA1A2B1B2

Here, A1B1 indicates the parameter type A1 is a supertype of B1, while A2B2 requires the return type A2 to be a subtype of B2.

Union and Intersection Types

Union Types

Union types, denoted , allow combining types such that a value of the union type belongs to at least one constituent type:

i.j.(ΓAiBj)ΓiAijBj

This expresses that the union of subtypes remains a subtype of the union of their respective supertypes.

Intersection Types

Intersection types, denoted , describe a type that belongs simultaneously to multiple constituent types:

Γi.j.(AiBj)ΓiAijBj

The intersection of supertypes forms a supertype of the intersection of their respective subtypes.

Relationships

For function types, union and intersection obey distributive laws:

(A1B1)(A2B2)=(A1A2)(B1B2)

This states that the intersection of function types corresponds to the union of parameter types and the intersection of return types.

For example:

(Int -> String) & (Float -> Bool)

Similarly, the union of function types results from the intersection of parameter types and the union of return types.

(A1B1)(A2B2)=(A1A2)(B1B2)

For example:

(Int -> String) | (Float -> Bool)

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:

Γti:TiiΓ{li=tii}:{li:Tii}

Here, ti:Tii denotes record fields, and field labels li and types Ti determine the record's type.

Field access ensures type preservation:

Γt:{li:Tii}Γt.li:Ti

Subtyping among records supports the subset relation:

S.(SS).({li:TiiS}{li:TiiS})

If fields S are a subset of S, then their respective record types exhibit a subtyping relationship.

Pointwise subtyping applies across all fields in records:

Γi.(AiBi)Γ{ai:Aii}{bi:Bii}

Union and Intersection Rules for Record Types

In algebraic subtyping, union () and intersection () types can also be extended to record types. These operations allow record subtyping to support partial and combined views of records, ensuring compatibility and flexibility in handling structured data.

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:

{li:AiiS}{li:BiiT}={li:AiBiiST,lj:AjjST,lk:BkkTS}
  • For shared fields liST, the resulting type is the union of their individual types AiBi.
  • For fields exclusive to one record (e.g., ljST or lkTS), their types remain unchanged.

For example:

(record { a: Int; b: String }) | (record { b: Float; c: Bool })

Intersection of Record Types

The intersection of two record types combines their fields similarly, but overlapping fields resolve to the intersection of their types:

{li:AiiS}{li:BiiT}={li:AiBiiST}
  • Only fields common to both records (liST) are preserved in the resulting record.
  • For shared fields, the resulting type is the intersection of their individual types AiBi.
  • Fields exclusive to one record are excluded from the intersection.

For example:

(record { a: Int; b: String }) & (record { b: String; c: Bool })

Subtyping for Recursive Type

ΓT:UΓ,x:TσTΓμ(x:T).σT

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 Π-type rule for subtyping is given by:

ΓA2A1Γ,x:A1B1(x)Γ,x:A2B1(x)B2(x)ΓΠ(x:A1).B1(x)Π(x:A2).B2(x)

Dependent Σ-Type

The dependent Σ-type rule for subtyping is given by:

ΓA1A2Γ,x:A1B1(x)B2(x)ΓΣ(x:A1).B1(x)Σ(x:A2).B2(x)

Intersection of Dependent Π-Types

Type Formation Rule for Intersection of Dependent Π-Types

Dependent Π-types are function-like types where the codomain depends on the specific value of the domain. For the intersection of such types:

ΓA1:UΓA2:UΓ,x:A1B1(x):UΓ,x:A2B2(x):UΓΠ(x:A1).B1(x)Π(x:A2).B2(x):U

Refinement ensures that x must be in both A1 and A2, producing:

ΓA=A1A2:UΓ,x:AB(x)=B1(x)B2(x):UΓΠ(x:A).B(x):U

Type Formation Rule for Intersection of Dependent Σ-Types

Dependent Σ-types represent dependent pairs, where the type of the second component depends on the value of the first. The intersection of such types is defined as:

ΓA1:UΓA2:UΓ,x:A1B1(x):UΓ,x:A2B2(x):UΓΣ(x:A1).B1(x)Σ(x:A2).B2(x):U

Union of Dependent Π-Types

Type Formation Rule for Union of Dependent Π-Types

The union of dependent Π-types combines the domains and codomains of the respective types:

ΓA1:UΓA2:UΓ,x:A1B1(x):UΓ,x:A2B2(x):UΓΠ(x:A1).B1(x)Π(x:A2).B2(x):U

Type Formation Rule for Union of Dependent Σ-Types

The union of dependent Σ-types combines the types of the first and second components:

ΓA1:UΓA2:UΓ,x:A1B1(x):UΓ,x:A2B2(x):UΓΣ(x:A1).B1(x)Σ(x:A2).B2(x):U