Recursive data type

Short description: Data type that refers to itself in its definition

In computer programming languages, a recursive data type (also known as a recursively-defined, inductively-defined or inductive data type) is a data type for values that may contain other values of the same type. Data of recursive types are usually viewed as directed graphs[citation needed].

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.

## Contents

* 1 Example
* 1.1 Mutually recursive data types
* 2 Theory
* 2.1 Isorecursive types
* 2.2 Equirecursive types
* 3 Recursive type synonyms
* 4 See also
* 5 References
* 6 Sources

## Example

See also: Recursion (computer science) § Recursive data structures (structural recursion)

An example is the list type, in Haskell:

data List a = Nil | Cons a (List a)

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:

class List<E> {
E value;
List<E> next;

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

Further information: Mutual recursion § 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:[1]

datatype 'a tree = Empty | Node of 'a * 'a forest
and      'a forest = Nil | Cons of 'a tree * 'a forest

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

data Tree a = Empty
| Node (a, Forest a)

data Forest a = Nil
| Cons (Tree a) (Forest a)

## 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:

data Nat = Zero | Succ Nat

In type theory, we would say: nat=μα.1+α 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+α).

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 and its expansion (or unrolling) T[μα.T/α] (where the notation 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: roll:T[μα.T/α]->μα.T and unroll:μα.T->T[μα.T/α], and these two are inverse functions.

### Equirecursive types

Under equirecursive rules, a recursive type μα.T and its unrolling T[μα.T/α] 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.[2]

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.[3]

## Recursive type synonyms

In TypeScript, recursion is allowed in type aliases.[4] Thus, the following example is allowed.

type Tree = number | Tree[];
let tree: Tree = [1, [2, 3]];

However, recursion is not allowed in type synonyms in Miranda, OCaml (unless `-rectypes` flag is used or it's a record or variant), or Haskell; so, for example the following Haskell types are illegal:

type Bad = (Int, Bad)
type Evil = Bool -> Evil

Instead, they must be wrapped inside an algebraic data type (even if they only has one constructor):

data Good = Pair Int Good
data Fine = Fun (Bool -> Fine)

This is because type synonyms, like typedefs in C, are replaced with their definition at compile time. (Type synonyms are not "real" types; they are just "aliases" for convenience of the programmer.) But if this is attempted with a recursive type, it will loop infinitely because no matter how many times the alias is substituted, it still refers to itself, e.g. "Bad" will grow indefinitely: `Bad` → `(Int, Bad)` → `(Int, (Int, Bad))` → `...` .

Another way to see it is that a level of indirection (the algebraic data type) is required to allow the isorecursive type system to figure out when to roll and unroll.

## See also

* Recursive definition
* Algebraic data type
* Inductive type
* Node (computer science)

## References

1. ↑ Harper 1998.
2. ↑ "Numbering Matters: First-Order Canonical Forms for Second-Order Recursive Types". CiteSeerX 10.1.1.4.2276.
3. ↑ Revisiting iso-recursive subtyping | Proceedings of the ACM on Programming Languages
4. ↑ (More) Recursive Type Aliases - Announcing TypeScript 3.7 - TypeScript

## Sources

* Harper, Robert (1998), Datatype Declarations, https://www.cs.cmu.edu/~rwh/introsml/core/datatypes.htm

* v
* t
* e

Data types

Uninterpreted|

* Bit
* Byte
* Trit
* Tryte
* Word
* Bit array

Numeric|

* Arbitrary-precision or bignum
* Complex
* Decimal
* Fixed point
* Floating point
* Double precision
* Extended precision
* Long double
* Octuple precision
* Quadruple precision
* Single precision
* Reduced precision
* Minifloat
* Half precision
* bfloat16
* Integer
* signedness
* Interval
* Rational

Pointer|

* Address
* physical
* virtual
* Reference

Text|

* Character
* String
* null-terminated

Composite|

* Algebraic data type
* generalized
* Array
* Associative array
* Class
* Dependent
* Equality
* Inductive
* Intersection
* List
* Object
* metaobject
* Option type
* Product
* Record or Struct
* Refinement
* Set
* Union
* tagged

Other|

* Boolean
* Bottom type
* Collection
* Enumerated type
* Exception
* Function type
* Opaque data type
* Recursive data type
* Semaphore
* Stream
* Top type
* Type class
* Unit type
* Void

Related
topics|

* Abstract data type
* Data structure
* Generic
* Kind
* metaclass
* Parametric polymorphism
* Primitive data type
* Protocol
* interface
* Subtyping
* Type constructor
* Type conversion
* Type system
* Type theory

See also platform-dependent and independent units of information

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Recursive data type. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Recursive_data_type&oldid=219576"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template