ArticleslgStudy

science

Recursive data type

Recursive data type is a 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 Recursive data type rather than just read about it. In short: In computer programming, a recursive data type is a data type whose definition contains values of the same type. It is also known as a recursively defined, inductively defined or inductive data type.

Key takeaways

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

Reference excerpt

In computer programming, a recursive data type is a data type whose definition contains values of the same type. It is also known as a recursively defined, inductively defined or inductive data type. Data of recursive types are usually viewed as directed graphs. An important application of recursion in computer science is in defining dynamic data structures such as Lists and Trees. Recursive data structures can dynamically grow to an arbitrarily large size in response to runtime requirements; in contrast, a static array's size requirements must be set at compile time. Sometimes the term "inductive data type" is used for algebraic data types which are not necessarily recursive.

Example

An example is the list type, in Haskell:

This indicates that a list of a's is either an empty list or a cons cell containing an 'a' (the "head" of the list) and another list (the "tail"). Another example is a similar singly linked type in Java:

This indicates that non-empty list of type E contains a data member of type E, and a reference to another List object for the rest of the list (or a null reference to indicate that this is the end of the list).

Mutually recursive data types

Data types can also be defined by mutual recursion. The most important basic example of this is a tree, which can be defined mutually recursively in terms of a forest (a list of trees). Symbolically:

f: [t[1], ..., t[k]] t: v f

A forest f consists of a list of trees, while a tree t consists of a pair of a value v and a forest f (its children). This definition is elegant and easy to work with abstractly (such as when proving theorems about properties of trees), as it expresses a tree in simple terms: a list of one type, and a pair of two types. This mutually recursive definition can be converted to a singly recursive definition by inlining the definition of a forest:

t: v [t[1], ..., t[k]]

A tree t consists of a pair of a value v and a list of trees (its children). This definition is more compact, but somewhat messier: a tree consists of a pair of one type and a list another, which require disentangling to prove results about. In Standard ML, the tree and forest data types can be mutually recursively defined as follows, allowing empty trees:

In Haskell, the tree and forest data types can be defined similarly:

Theory In type theory, a recursive type has the general form μα.T where the type variable α may appear in the type T and stands for the entire type itself. For example, the natural numbers (see Peano arithmetic) may be defined by the Haskell datatype:

In type theory, we would say: nat = μ α .1 + α {\displaystyle {\text{nat}}=\mu \alpha .1+\alpha } where the two arms of the sum type represent the Zero and Succ data constructors. Zero takes no arguments (thus represented by the unit type) and Succ takes another Nat (thus another element of μ α .1 + α {\displaystyle \mu \alpha .1+\alpha } ). There are two forms of recursive types: the so-called isorecursive types, and equirecursive types. The two forms differ in how terms of a recursive type are introduced and eliminated.

Isorecursive types With isorecursive types, the recursive type μ α . T {\displaystyle \mu \alpha .T} and its expansion (or unrolling) T [ μ α . T / α ] {\displaystyle T[\mu \alpha .T/\alpha ]} (where the notation X [ Y / Z ] {\displaystyle X[Y/Z]} indicates that all instances of Z are replaced with Y in X) are distinct (and disjoint) types with special term constructs, usually called roll and unroll, that form an isomorphism between them. To be precise: r o l l : T [ μ α . T / α ] → μ α . T {\displaystyle roll:T[\mu \alpha .T/\alpha ]\to \mu \alpha .T} and u n r o l l : μ α . T → T [ μ α . T / α ] {\displaystyle unroll:\mu \alpha .T\to T[\mu \alpha .T/\alpha ]} , and these two are inverse functions.

Equirecursive types Under equirecursive rules, a recursive type μ α . T {\displaystyle \mu \alpha .T} and its unrolling T [ μ α . T / α ] {\displaystyle T[\mu \alpha .T/\alpha ]} are equal – that is, those two type expressions are understood to denote the same type. In fact, most theories of equirecursive types go further and essentially specify that any two type expressions with the same "infinite expansion" are equivalent. As a result of these rules, equirecursive types contribute significantly more complexity to a type system than isorecursive types do. Algorithmic problems such as type checking and type inference are more difficult for equirecursive types as well. Since direct comparison does not make sense on an equirecursive type, they can be converted into a canonical form in O(n log n) time, which can easily be compared. Isorecursive types capture the form of self-referential (or mutually referential) type definitions seen in nominal object-oriented programming languages, and also arise in type-theoretic semantics of objects and classes. In functional programming languages, isorecursive types (in the guise of datatypes) are common too.

Recursive type synonyms

In TypeScript, recursion is allowed in type aliases.

See also Recursive definition Algebraic data type Inductive type Node (computer science)

References

Sources Harper, Robert (1998), Datatype Declarations, archived from the original on 1999-10-01

Worked examples

Example 1 — a first encounter with Recursive data type

Start with the simplest possible case. Write down what Recursive data type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In 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 Recursive data type 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 Recursive data type 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 Recursive data type

In research
Recursive data type appears in 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 Recursive data type 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
Recursive data type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Type theory, so understanding it makes those chapters shorter.
In everyday life
Look for Recursive data type 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 Recursive data type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Recursive data type 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 Recursive data type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Recursive data type in simple terms?

In computer programming, a recursive data type is a data type whose definition contains values of the same type. It is also known as a recursively defined, inductively defined or inductive data type.

Why does Recursive data type matter?

Because it connects several 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 Recursive data type?

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 Recursive data type.

Tags

  • Data types
  • Type theory

Keep exploring