HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, polymorphic recursion (also referred to as MilnerMycroft typability or the Milner–Mycroft calculus) refers to a recursive parametrically polymorphic function where the type parameter changes with each recursive invocation made, instead of staying constant.
Type inference Type inference refers to the automatic detection of the type of an expression in a formal language. These include programming languages and mathematical type systems, but also natural languages in some branches of computer science and linguistics. ...
for polymorphic recursion is equivalent to semi-unification and therefore undecidable and requires the use of a
semi-algorithm In computability theory and computational complexity theory, RE (Recursively enumerable set, recursively enumerable) is the complexity class, class of decision problems for which a 'yes' answer can be verified by a Turing machine in a finite amount ...
or programmer-supplied type annotations.


Example


Nested datatypes

Consider the following
nested datatype ''Nested'' is the seventh studio album by Bronx-born singer, songwriter and pianist Laura Nyro, released in 1978 on Columbia Records. Following on from her extensive tour to promote 1976's '' Smile'', which resulted in the 1977 live album ''Sea ...
: data Nested a = a :<: (Nested , Epsilon infixr 5 :<: nested = 1 :<: ,3,4:<: 5,6 ,9 :<: Epsilon A length function defined over this datatype will be polymorphically recursive, as the type of the argument changes from Nested a to Nested /code> in the recursive call: length :: Nested a -> Int length Epsilon = 0 length (_ :<: xs) = 1 + length xs Note that Haskell normally infers the
type signature In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is ty ...
for a function as simple-looking as this, but here it cannot be omitted without triggering a type error.


Higher-ranked types


Applications


Program analysis

In type-based program analysis polymorphic recursion is often essential in gaining high precision of the analysis. Notable examples of systems employing polymorphic recursion include Dussart, Henglein and Mossin's binding-time analysis and the Tofte–Talpin
region-based memory management In computer science, region-based memory management is a type of memory management in which each allocated object is assigned to a region. A region, also called a zone, arena, area, or memory context, is a collection of allocated objects that c ...
system. As these systems assume the expressions have already been typed in an underlying type system (not necessary employing polymorphic recursion), inference can be made decidable again.


Data structures, error detection, graph solutions

Functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
data structures often use polymorphic recursion to simplify type error checks and solve problems with nasty "middle" temporary solutions that devour memory in more traditional data structures such as trees. In the two citations that follow, Okasaki (pp. 144–146) gives a CONS example in Haskell wherein the polymorphic type system automatically flags programmer errors. The recursive aspect is that the type definition assures that the outermost constructor has a single element, the second a pair, the third a pair of pairs, etc. recursively, setting an automatic error finding pattern in the data type. Roberts (p. 171) gives a related example in
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, using a Class to represent a stack frame. The example given is a solution to the
Tower of Hanoi The Tower of Hanoi (also called The problem of Benares Temple or Tower of Brahma or Lucas' Tower and sometimes pluralized as Towers, or simply pyramid puzzle) is a mathematical game or puzzle consisting of three rods and a number of disks of v ...
problem wherein a stack simulates polymorphic recursion with a beginning, temporary and ending nested stack substitution structure.


See also

* Higher-ranked polymorphism


Notes


Further reading

* * * * * * * Richard Bird and Lambert Meertens (1998)
"Nested Datatypes"
* C. Vasconcellos, L. Figueiredo, C. Camarao (2003).
Practical Type Inference for Polymorphic Recursion: an Implementation in Haskell
. ''Journal of Universal Computer Science''. * L. Figueiredo, C. Camarao.
Type Inference for Polymorphic Recursive Definitions: a Specification in Haskell
. *


External links



by Hans Leiß,
University of Munich The Ludwig Maximilian University of Munich (simply University of Munich or LMU; german: link=no, Ludwig-Maximilians-Universität München) is a public research university in Munich, Bavaria, Germany. Originally established as the University of ...
{{Infobox software Polymorphism (computer science) Recursion Object-oriented programming