Example
Nested datatypes
Consider the following nested datatype inNested 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 of a function, subroutine or method. A type signature includes the number, types, and order of the function's arguments. One important use of a type sign ...
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 partition, subpool, zone, arena, area, or memory context, is a collection of alloca ...
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 Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
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
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 ...
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 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 ...
, using a Class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
to represent a stack frame. The example given is a solution to the Tower of Hanoi problem wherein a stack simulates polymorphic recursion with a beginning, temporary and ending nested stack substitution structure.
See also
* Higher-ranked polymorphism
In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
Notes
Further reading
*
*
*
*
*
*
* Richard Bird and Lambert Meertens
Lambert Guillaume Louis Théodore Meertens or L.G.L.T. Meertens (born 10 May 1944, in Amsterdam) is a Dutch computer scientist and professor. , he is a researcher at the Kestrel Institute, a nonprofit computer science research center in Palo Al ...
(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, LMU or LMU Munich; ) is a public university, public research university in Munich, Bavaria, Germany. Originally established as the University of Ingolstadt in 1472 by Duke ...
{{Infobox software
Polymorphism (computer science)
Recursion
Object-oriented programming