In computer
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, a recursive data type (also known as a recursively defined, inductively defined or inductive data type) is a
data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
for values that may contain other values of the same 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 type
In computer programming, especially functional programming and type theory, an algebraic data type (ADT) is a kind of composite data type, i.e., a data type formed by combining other types.
Two common classes of algebraic types are product ty ...
s which are not necessarily recursive.
Example
An example is the
list
A list is a Set (mathematics), set of discrete items of information collected and set forth in some format for utility, entertainment, or other purposes. A list may be memorialized in any number of ways, including existing only in the mind of t ...
type, in
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
:
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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
:
class List
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
In botany, a tree is a perennial plant with an elongated stem, or trunk, usually supporting branches and leaves. In some usages, the definition of a tree may be narrower, e.g., including only woody plants with secondary growth, only ...
, which can be defined mutually recursively in terms of a forest (a list of trees). Symbolically:
f:
[1 ..., t[k">[1.html" ;"title="[1">[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
[1 ..., t[k">[1.html" ;"title="[1">[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:
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
In mathematics and theoretical computer science, a type theory is the formal presentation of a specific type system. Type theory is the academic study of type systems.
Some type theories serve as alternatives to set theory as a foundation of ...
, a recursive type has the general form ''μα''.''T'' where the
type variable
In type theory and programming languages, a type variable is a mathematical variable ranging over types. Even in programming languages that allow mutable variables, a type variable remains an abstraction, in the sense that it does not correspond ...
''α'' 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:
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
In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information). The carrier (underlying set) associated with a unit type can be any singleton set. ...
) and Succ takes another Nat (thus another element of
).
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
and its expansion (or ''unrolling'')