Parameterized Specifications



next up previous contents index
Next: Type Specification Examples Up: Specifications Previous: Type Specifications

Parameterized Specifications

Routine and type specifications can be parameterized by types, and so can specifications of methods, even those contained within parameterized type specifications. In all cases, legal instantiations can be constrained by where clauses.
        <parms> -> "[" <idn_list> "]"
        <where> -> where <restriction> ["," <restriction>]*
        <restriction> -> <idn> has <nonparam_interface> [, <nonparam_interface>]*
        <nonparam_interface> -> <idn> <nonparam_proc_sig> | <idn> <nonparam_iter_sig>
        <nonparam_proc_sig> -> "(" [ <type_list> ] ")" [<returns>] [<signals>]
        <nonparam_iter_sig> -> "(" [ <type_list> ] ")" <yields> [<signals>]
        <type_list> -> <type_designator> ["," <type_designator>]*
All the parameters are types. The where clause lists any restrictions on a parameter by stating methods that objects of the parameter type must have. For example,
find [T] (a: collection[T], v: T) returns (int) signals (not_found)
      where T has equal (T) returns (bool)
defines a parameterized routine with one parameter "T". Within the parameterized definition, objects of a parameter type can be assumed to have the methods listed in the where clause; any instantiation of the parameterized definition will provide a binding for each of these methods (3.2.2,7.7).

Within the scope corresponding to a parameterized specification, parameter names can be used to denote types. If parameters are used in instantiations, any requirements of the parameterized types being instantiated must be met in the usual way. For example, consider the instantiation "collection[T]" used in the specification of [tex2html_wrap2956]find; if "collection" required an "lt" method, the Theta compiler would reject the specification of "find" because the instantiation of "collection" is illegal.

A parameterized type may have declared supertypes. For example

stack = type[T] < item, bag[T], stack_pair[T, int]
states that every instantiation of stack is a subtype of "item" and of the corresponding instantiations of "bag" and "stack_pair" (e.g., "stack[foo]" is a subtype of "item", "bag[foo]", and "stack_pair[foo, int]"). There is no subtype/supertype relationship on parameters: for example, "stack[foo]" is not a subtype of "stack[bar]" even if "foo" is a declared subtype of "bar". Subtyping operates only ``outside the square brackets'' of a type definition.



theta-questions@lcs.mit.edu