Next: When to use type specialization, Up: Type specialization [Contents]
A declaration of the form
:- pragma type_spec(pred(Name/Arity), Subst). :- pragma type_spec(func(Name/Arity), Subst).
suggests to the compiler that it should create a specialized version of the predicate or function with name Name and arity Arity with the type substitution given by Subst applied to the argument types. The substitution is written as a conjunction of bindings of the form ‘TypeVar = Type’, for example ‘K = int’ or ‘(K = int, V = list(int))’. (The conjunction must have parentheses around it if it contains two or more bindings.)
For example, the declarations
:- pred map.lookup(map(K, V), K, V). :- pragma type_spec(pred(map.lookup/3), K = int).
give a hint to the compiler that a version of ‘map.lookup/3’ should be created for integer keys.
Implementations are free to ignore ‘pragma type_spec’ declarations. Implementations are also free to perform type specialization even in the absence of any ‘pragma type_spec’ declarations.
The pragma also has a form that suggests specialization of only one mode of the predicate or function, instead of all of them:
:- pragma type_spec(Name(m1, ... mn), Subst). :- pragma type_spec(Name(m1, ... mn) = mr, Subst).
where m1 etc are the modes of the arguments. If the ‘= mr’ part is present, it gives the mode of the function result; if it is absent, this indicates that Name is a predicate.