In
type theory
In mathematics, logic, and computer science, a type theory is the formal system, formal presentation of a specific type system, and in general type theory is the academic study of type systems. Some type theories serve as alternatives to set theor ...
, bounded quantification (also bounded polymorphism or constrained genericity) refers to
universal or
existential quantifier
In predicate logic, an existential quantification is a type of quantifier, a logical constant which is interpreted as "there exists", "there is at least one", or "for some". It is usually denoted by the logical operator symbol ∃, which, w ...
s which are restricted ("bounded") to range only over the subtypes of a particular type. Bounded quantification is an interaction of
parametric 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 ...
with
subtyping
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, ...
. Bounded quantification has traditionally been studied in the
functional
Functional may refer to:
* Movements in architecture:
** Functionalism (architecture)
** Form follows function
* Functional group, combination of atoms within molecules
* Medical conditions without currently visible organic basis:
** Functional s ...
setting of
System F<:, but is available in modern
object-oriented languages supporting
parametric 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 ...
(
generics
Generic or generics may refer to:
In business
* Generic term, a common name used for a range or class of similar things not protected by trademark
* Generic brand, a brand for a product that does not have an associated brand or trademark, other ...
) such as
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 ...
,
C# and
Scala.
Overview
The purpose of bounded quantification is to allow for
polymorphic functions to depend on some specific behaviour of objects instead of
type inheritance
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new clas ...
. It assumes a record-based model for object classes, where every class member is a record element and all class members are named functions. Object attributes are represented as functions that take no argument and return an object. The specific behaviour is then some function name along with the types of the arguments and the return type. Bounded quantification allows to considers all objects with such a function. An example would be a polymorphic
min
function that considers all objects that are comparable to each other.
F-bounded quantification
''F''-bounded quantification or recursively bounded quantification, introduced in 1989, allows for more precise typing of functions that are applied on recursive types. A recursive type is one that includes a function that uses it as a type for some argument or its return value.
[''F''-bounded polymorphism for object-oriented programming. Canning, Cook, Hill, Olthof and Mitchell. http://dl.acm.org/citation.cfm?id=99392]
Example
This kind of type constraint can be expressed 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 ...
with a generic interface. The following example demonstrates how to describe types that can be compared to each other and use this as typing information in
polymorphic functions. The
Test.min
function uses simple bounded quantification and does not preserve the type of the assigned types, in contrast with the
Test.Fmin
function which uses F-bounded quantification.
In mathematical notation, the types of the two functions are
:min: ∀ T, ∀ S ⊆ . S → S → S
:Fmin: ∀ T ⊆ Comparable
T → T → T
where
:Comparable
=
interface Comparable
class Integer implements Comparable
class String implements Comparable
class Test
See also
*
Covariance and contravariance (computer science)
Many programming language type systems support subtyping. For instance, if the type is a subtype of , then an expression of type should be substitutable wherever an expression of type is used.
Variance refers to how subtyping between more c ...
*
Curiously recurring template pattern
The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X derives from a class template instantiation using X itself as a template argument. More generally it is known as F-bound polymorphism, and it is a ...
*
Wildcard (Java)
In the Java programming language, the wildcard ? is a special kind of type argument that controls the type safety of the use of generic (parameterized) types. It can be used in variable declarations and instantiations as well as in method definiti ...
Notes
References
*
*
Peter S. Canning
Peter may refer to:
People
* List of people named Peter, a list of people and fictional characters with the given name
* Peter (given name)
** Saint Peter (died 60s), apostle of Jesus, leader of the early Christian Church
* Peter (surname), a sur ...
,
William R. Cook,
Walter L. Hill,
John C. Mitchell
John Clifford Mitchell is professor of computer science and (by courtesy) electrical engineering at Stanford University. He has published in the area of programming language theory and computer security.John C. Mitchellwas the Vice Provost for T ...
, and
William Olthoff
William is a masculine given name of Norman French origin.Hanks, Hardcastle and Hodges, ''Oxford Dictionary of First Names'', Oxford University Press, 2nd edition, , p. 276. It became very popular in the English language after the Norman conqu ...
"F-bounded polymorphism for object-oriented programming" In ''Conference on Functional Programming Languages and Computer Architecture'', 1989.
*
Benjamin C. Pierce
Benjamin Crawford Pierce is the Henry Salvatori Professor of computer science at the University of Pennsylvania. Pierce joined Penn in 1998 from Indiana University and held research positions at the University of Cambridge and the University of E ...
"Intersection types and bounded polymorphism". ''Lecture Notes in Computer Science'' 664, 1993.
*
Gilad Bracha,
Martin Odersky
Martin Odersky (born 5 September 1958) is a German computer scientist and professor of programming methods at École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland. He specializes in code analysis and programming languages. He desig ...
,
David Stoutamire
David (; , "beloved one") (traditional spelling), , ''Dāwūd''; grc-koi, Δαυΐδ, Dauíd; la, Davidus, David; gez , ዳዊት, ''Dawit''; xcl, Դաւիթ, ''Dawitʿ''; cu, Давíдъ, ''Davidŭ''; possibly meaning "beloved one". w ...
, and
Philip Wadler
Philip Lee Wadler (born April 8, 1956) is an American computer scientist known for his contributions to programming language design and type theory. He is the chair of Theoretical Computer Science at the Laboratory for Foundations of Computer S ...
. "Making the future safe for the past: Adding genericity to the Java programming language". In ''Object-Oriented Programming: Systems, Languages, Applications'' (OOPSLA). ACM, October 1998.
*
Andrew Kennedy and
Don Syme
Don Syme is an Australian computer scientist and a Principal Researcher at Microsoft Research, Cambridge, U.K. He is the designer and architect of the F# programming language, described by a reporter as being regarded as "the most original new f ...
. "Design and Implementation of Generics for the .NET Common Language Runtime". In ''Programming Language Design and Implementation'', 2001.
* {{cite book, last=Pierce, first=Benjamin C., authorlink=Benjamin C. Pierce, title=Types and Programming Languages, publisher=MIT Press, date=2002, isbn=978-0-262-16209-8, Chapter 26: Bounded quantification
External links
Bounded Polymorphismat the
Portland Pattern Repository
The Portland Pattern Repository (PPR) is a repository for computer programming software design patterns. It was accompanied by a companion website, WikiWikiWeb, which was the world's first wiki. The repository has an emphasis on Extreme Programmin ...
"F-bounded Polymorphism"in ''The Cecil Language: Specification and Rationale''
Polymorphism (computer science)
Object-oriented programming
Type theory
Articles with example Java code