What does a clause without a head mean in prolog?
Asked Answered
A

2

6

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?

Autograph answered 5/1, 2013 at 3:47 Comment(0)
R
6

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.

Rinee answered 5/1, 2013 at 4:4 Comment(0)
N
5

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.

Ns answered 5/1, 2013 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.