Language Reference/Bounded polymorphism

The following example can be used as a motivation for bounded polymorphism.

In this example the elements in the SortedList has been "downgraded" from user to person. But if they are users before we sort them then they are also users after they have been sorted. So we would like to create a sortByName predicate that does not perform such an element downgrade.

So we want to create a predicate that returns the same kind of elements that it receives as input. A polymorphic declaration like this: predicates sortByName : (Elem* PersonList) -> Elem* SortedList. has the desired property. But it will not work, because this predicate can receive any kind of list as input and you cannot get a name from everything.

This is where bounded polymorphism can help. With bounded polymorphism you can state that a a type parameter cannot be anything it has to fulfill some bound.

Bounds have the form where X supports Y.

Bounds can be put on polymorphic predicates like above, if you need more than one bound you must write like this:

predicates pred : ( P1, P2, ...) ->. where  supports  where  supports  ....       where  supports .

When we have put bounds on the parameters we can exploit them in the clauses.

Bounds can also be put on classes.

In bounds the notion of supports is extended to cover non-object types, in the sense that Sub supports Super if Sub is a subtype of Super. As example integer</vp> is a subtype of real</vp>.