Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

In a Prolog text, a term with principal functor (:-)/1 is a directive.

A dynamic directive has the following effect:

  • A predicate 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.
  • The predicate may be altered by asserta/1, rectract/1 and the like. By default, a predicate is static and attempting to modify it causes a permission error.
  • The clauses may be inspected with 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top