In the beginning of a Prolog program I see:
:-dynamic(path/1).
It seems to be a clause that doesn't have a head. What does it mean?
When Prolog interpreter sees :-
in front of a clause when loading the program, the clause gets executed right away, rather than becoming part of the program "database". The most common use of the :-
is defining the starting predicate of your program at the end of your program file, so that loading the file with your program causes it to run.
Specifically, dynamic/1
informs the interpreter that the definition of the specified predicate may change at runtime. This is something the interpeter needs to know before reading the rest of your program, so that it could make special arrangements for uses of the path/1
predicate.
In a Prolog text, a term with principal functor (:-)/1
is a directive.
A dynamic
directive has the following effect:
P/N
is defined, even if no clauses are given. By default, executing a goal for a predicate without any clauses causes an existence error.asserta/1
, rectract/1
and the like. By default, a predicate is static and attempting to modify it causes a permission error.clause/2
. By default, a predicate is private and inspection with clause/2
causes a permission error. On some systems (like SWI), all user-defined predicates are public and thus can be inspected with clause/2
. So this effect cannot be observed in SWI.Many implementations treat directives that are unknown as regular goals and execute them. However, implementations differ a lot as to the precise point in time when these goals are executed. When reloading a file, they differ even more.
If you want to ensure that a goal gets executed immediately after the Prolog text has been prepared for execution, use the directive initialization/1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With