ArticleslgStudy

computer science

Row polymorphism

Row polymorphism is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Row polymorphism rather than just read about it. In short: In programming language type theory, row polymorphism is a kind of polymorphism that allows one to write programs that are structurally (rather than nominally) polymorphic on record types and/or variants. History and theory A row-polymorphic type system and proof of type inference for records was introduced by Mitchell Wand.

Key takeaways

  • Row polymorphism belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Row polymorphism to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Row polymorphism from memory before moving on to harder problems.

Reference excerpt

In programming language type theory, row polymorphism is a kind of polymorphism that allows one to write programs that are structurally (rather than nominally) polymorphic on record types and/or variants.

History and theory A row-polymorphic type system and proof of type inference for records was introduced by Mitchell Wand. The theoretical treatment of row polymorphism is somewhat complicated by the need to have distinct labels in a record. One approach, taken by Rémy and colleagues (and implicitly present in the presentation below, which is heavily inspired by Wand's), is to consider different kinds of row types, depending on their labels. Gaster and Jones unformely extended the approach to variants as well, by making both the record and variant type constructor map from row kinds to types. Blume et al. proposed to take the approach one step further into the realm of composable extensions by adding "first-class cases" handled by a LetCC construct to compose continuations in the case-matching code. Then, a couple of projects (Links, Koka) have used row polymorphism as the type basis of an algebraic effects system, aiming for "free composition" of user-defined effects. Another approach to the issue of unique labels is to extend system F with a "coherent merge" operator, resulting in calculi with so-called disjoint intersection types. Morris and McKinna generalized row types to row theories to uniformly handle varying notions of (record) extension in a single theoretical framework: for instance one application may desire the operation of extension to overwrite existing fields in case of matching names, another may want to keep both of them accessible, possibly some path-based addressing scheme etc.

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 { ℓ 1 : T 1 , … , ℓ n : T n , absent ( f 1 ) , … , absent ( f m ) , ρ } {\displaystyle \{\ell _{1}:T_{1},\dots ,\ell _{n}:T_{n},{\text{absent}}(f_{1}),\dots ,{\text{absent}}(f_{m}),\rho \}} . This indicates a record type that has fields ℓ i {\displaystyle \ell _{i}} with respective types of T i {\displaystyle T_{i}} (for i = 1 … n {\displaystyle i=1\dots n} ), and does not have any of the fields f j {\displaystyle f_{j}} (for j = 1 … m {\displaystyle j=1\dots m} ), while ρ {\displaystyle \rho } expresses the fact the record may contain other fields than ℓ i {\displaystyle \ell _{i}} . 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 , ρ } → { x : Number , y : Number , ρ } {\displaystyle {\text{transform2d}}:\{x:{\text{Number}},y:{\text{Number}},\rho \}\to \{x:{\text{Number}},y:{\text{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 {\displaystyle {\text{Number}}} . There is no loss of information: the type ensures that all the fields represented by the variable ρ {\displaystyle \rho } are present in the return type. In contrast, the type definition { x : Number , y : Number , e m p t y } {\displaystyle \{x:{\text{Number}},y:{\text{Number}},\mathbf {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 . ℓ {\displaystyle r.\ell } , adding a field r [ ℓ := e ] {\displaystyle r[\ell :=e]} , and removing a field r ∖ ℓ {\displaystyle r\backslash \ell } can be given row-polymorphic types.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Row polymorphism

Start with the simplest possible case. Write down what Row polymorphism claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Row polymorphism before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Row polymorphism ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Row polymorphism

In research
Row polymorphism appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Row polymorphism in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Row polymorphism is common in secondary-school and first-year university syllabi. It links to neighbouring topics Polymorphism (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Row polymorphism outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Row polymorphism in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Row polymorphism means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Row polymorphism out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Row polymorphism in simple terms?

In programming language type theory, row polymorphism is a kind of polymorphism that allows one to write programs that are structurally (rather than nominally) polymorphic on record types and/or variants. History and theory A row-polymorphic type system and proof of type inference for records was i…

Why does Row polymorphism matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Row polymorphism?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Row polymorphism.

Tags

  • Polymorphism (computer science)

Keep exploring