In programming language type theory, row polymorphism is a kind of polymorphism that allows one to write programs that are polymorphic on row types such as record types and polymorphic variants.
[1] A row-polymorphic type system and proof of type inference was introduced by Mitchell Wand.
[2][3] The row-polymorphic record type defines a list of fields with their corresponding types, a list of missing fields, and a variable indicating the absence or presence of arbitrary additional fields.
Both lists are optional, and the variable may be constrained.
Specifically, the variable may be "empty", indicating that no additional fields may be present for the record.
) , ρ }
This indicates a record type that has fields
with respective types of
), and does not have any of the fields
expresses the fact the record may contain other fields than
Row-polymorphic record types allow us to write programs that operate only on a section of a record.
For example, one may define a function that performs some two-dimensional transformation that accepts a record with two or more coordinates, and returns an identical type: Thanks to row polymorphism, the function may perform two-dimensional transformation on a three-dimensional (in fact, n-dimensional) point, leaving the z coordinate (or any other coordinates) intact.
In a more general sense, the function can perform on any record that contains the fields
There is no loss of information: the type ensures that all the fields represented by the variable
are present in the return type.
In contrast, the type definition
{\displaystyle \{x:{\text{Number}},y:{\text{Number}},\mathbf {empty} \}}
expresses the fact that a record of that type has exactly the
fields and nothing else.
In this case, a classic record type is obtained.
The record operations of selecting a field
, adding a field
, and removing a field
can be given row-polymorphic types.
( ℓ ) , ρ } →
( ℓ ) , ρ }