Row polymorphism explained

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]

Row-polymorphic record type definition

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.

It may be written as

\{\ell1:T1,...,\elln:Tn,absent(f1),...,absent(fm),\rho\}

. This indicates a record type that has fields

\elli

with respective types of

Ti

(for

i=1...n

), and does not have any of the fields

fj

(for

j=1...m

), while

\rho

expresses the fact the record may contain other fields than

\elli

.

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:

transform2d:\{x:Number,y:Number,\rho\}\to\{x:Number,y:Number,\rho\}

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

x

and

y

with type

Number

. There is no loss of information: the type ensures that all the fields represented by the variable

\rho

are present in the return type. In contrast, the type definition

\{x:Number,y:Number,empty\}

expresses the fact that a record of that type has exactly the

x

and

y

fields and nothing else. In this case, a classic record type is obtained.

Typing operations on records

The record operations of selecting a field

r.\ell

, adding a field

r[\ell:=e]

, and removing a field

r\backslash\ell

can be given row-polymorphic types.
select\ell

=λr.(r.\ell):\{\ell:T,\rho\}T

add\ell

=λr.λe.r[\ell:=e]:\{absent(\ell),\rho\}T\{\ell:T,\rho\}

remove\ell

=λr.r\backslash\ell:\{\ell:T,\rho\}\{absent(\ell),\rho\}

Notes

  1. Web site: OCaml - Polymorphic variants . 2022-12-03 . v2.ocaml.org.
  2. Mitchell . Wand . Type inference for record concatenation and multiple inheritance . Proceedings. Fourth Annual Symposium on Logic in Computer Science . 92–97 . June 1989 . 10.1109/LICS.1989.39162 .
  3. Mitchell . Wand . Type inference for record concatenation and multiple inheritance . Information and Computation . 93 . Selections from 1989 IEEE Symposium on Logic in Computer Science . 1–15 . 1991 . 10.1016/0890-5401(91)90050-C . 0890-5401 .