Home Contents Index Summary Previous Next

3.13 Declaring Properties of Predicates

This section describes directives which manipulate attributes of predicate definitions. The functors dynamic/1, multifile/1 and discontiguous/1 are operators of priority 1150 (see op/3), which implies the list of predicates they involve can just be a comma separated list:


:- dynamic
        foo/0, 
        baz/2.

On SWI-Prolog all these directives are just predicates. This implies they can also be called by a program. Do not rely on this feature if you want to maintain portability to other Prolog implementations.

dynamic +Functor/+Arity, \ldots
Informs the interpreter that the definition of the predicate(s) may change during execution (using assert/1 and/or retract/1). Currently dynamic/1 only stops the interpreter from complaining about undefined predicates (see unknown/2). Future releases might prohibit assert/1 and retract/1 for not-dynamic declared procedures.

multifile +Functor/+Arity, \ldots
Informs the system that the specified predicate(s) may be defined over more than one file. This stops consult/1 from redefining a predicate when a new definition is found.

discontiguous +Functor/+Arity, \ldots
Informs the system that the clauses of the specified predicate(s) might not be together in the source file. See also style_check/1.

index(+Head)
Index the clauses of the predicate with the same name and arity as Head on the specified arguments. Head is a term of which all arguments are either `1' (denoting `index this argument') or `0' (denoting `do not index this argument'). Indexing has no implications for the semantics of a predicate, only on its performance. If indexing is enabled on a predicate a special purpose algorithm is used to select candidate clauses based on the actual arguments of the goal. This algorithm checks whether indexed arguments might unify in the clause head. Only atoms, integers and functors (e.g. name and arity of a term) are considered. Indexing is very useful for predicates with many clauses representing facts.

Due to the representation technique used at most 4 arguments can be indexed. All indexed arguments should be in the first 32 arguments of the predicate. If more than 4 arguments are specified for indexing only the first 4 will be accepted. Arguments above 32 are ignored for indexing.

By default all predicates with <arity> >= 1 are indexed on their first argument. It is possible to redefine indexing on predicates that already have clauses attached to them. This will initiate a scan through the predicates clause list to update the index summary information stored with each clause.

If---for example---one wants to represents sub-types using a fact list `sub_type(Sub, Super)' that should be used both to determine sub- and super types one should declare sub_type/2 as follows:


:- index(sub_type(1, 1)).

sub_type(horse, animal).
...
...