This article provides insufficient context for those unfamiliar with the subject.October 2018) (Learn how and when to remove this template message) ( |
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 an 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.
It may be written as [math]\displaystyle{ \{\ell_1 : T_1, \dots, \ell_n : T_n, \text{absent}(f_1), \dots, \text{absent}(f_m), \rho\} }[/math]. This indicates a record type that has fields [math]\displaystyle{ \ell_i }[/math] with respective types of [math]\displaystyle{ T_i }[/math] (for [math]\displaystyle{ i = 1 \dots n }[/math]), and does not have any of the fields [math]\displaystyle{ f_j }[/math] (for [math]\displaystyle{ j = 1 \dots m }[/math]), while [math]\displaystyle{ \rho }[/math] expresses the fact the record may contain other fields than [math]\displaystyle{ \ell_i }[/math].
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:
[math]\displaystyle{ \text{transform2d} : \{x : \text{Number}, y : \text{Number}, \rho\} \to \{x : \text{Number}, y : \text{Number}, \rho\} }[/math]
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 [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] with type [math]\displaystyle{ \text{Number} }[/math]. There is no loss of information: the type ensures that all the fields represented by the variable [math]\displaystyle{ \rho }[/math] are present in the return type. In contrast, the type definition [math]\displaystyle{ \{x : \text{Number}, y : \text{Number}, \mathbf{empty}\} }[/math] expresses the fact that a record of that type has exactly the [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] fields and nothing else. In this case, a classic record type is obtained.
The record operations of selecting a field [math]\displaystyle{ r.\ell }[/math], adding a field [math]\displaystyle{ r[\ell:=e] }[/math], and removing a field [math]\displaystyle{ r \backslash \ell }[/math] can be given row-polymorphic types.
[math]\displaystyle{ \mathrm{select_\ell} = \lambda r. (r.\ell) \;:\; \{ \ell : T, \rho \} \rightarrow T }[/math]
[math]\displaystyle{ \mathrm{add_\ell} = \lambda r. \lambda e. r[\ell := e] \;:\; \{\mathrm{absent}(\ell), \rho\} \rightarrow T \rightarrow \{\ell : T, \rho\} }[/math]
[math]\displaystyle{ \mathrm{remove_\ell} = \lambda r. r \backslash \ell \;:\; \{\ell : T, \rho\} \rightarrow \{\mathrm{absent}(\ell), \rho \} }[/math]
Original source: https://en.wikipedia.org/wiki/Row polymorphism.
Read more |