[Next] [Previous] [Up] [Top] [Contents] [Index]

4.7 F-Bounded Polymorphism

4.7.2 F-Bounded Polymorphism in Singly-Dispatched Languages

F-bounded polymorphism [Canning et al. 89, Cook et al. 90] supports parameterization where the upper bound constraint of a type parameter can be a function of the type parameter itself. This enables parameterized types to be used to describe patterns of types that are not necessarily subtypes of one another. Versions of F-bounded polymorphism have appeared in single-dispatching languages such as Emerald [Black & Hutchinson 90], Axiom (formerly Scratchpad II) [Watt et al. 88, Jenks & Sutor 92], Strongtalk [Bracha & Griswold 93], and k-bench [Santas 93].

In a singly-dispatched language with F-bounded polymorphism, the comparable type could be defined as follows:

class comparable[T] {
	signature =(y:T);
	method    !=(y:T):bool { not(x = y) }
	signature < (y:T):bool;
	method    <=(y:T):bool { x = y | x < y }
	method    >=(y:T):bool { x = y | x > y }
	method    > (y:T):bool { y < x }
};
Then comparable types could be declared as subtypes of instances of this parameterized type:

extend num isa comparable[num];
extend collection['T <= comparable[T]] isa comparable[collection[T]];
Functions that are polymorphic over comparable types could be written using explicit parameterization as follows:

method min[T <= comparable[T]](x1:T, x2:T):T {
	if (x1 < x2, { x1 }, { x2 })}
The seemingly recursive nature of the explicit type parameter of both collection and min is not a problem. For a type of the form 'T <= type[T], first the actual instantiating type parameter is bound to the type variable T, then this type is checked against type instantiated with the type bound to T. For example, for the following call:

min[num](3, 4.5)
first T is bound to num, then the system checks that num is a subtype of comparable[num], which holds.


The Cecil Language: Specification and Rationale, Version 2.1 - 25 MARCH 1997
[Next] [Previous] [Up] [Top] [Contents] [Index]

Generated with Harlequin WebMaker