I have a problem with defining procedures in Prolog. I have two source files and want to consult Prolog engine with both of them. This can be done by invoking Prolog as swipl -g “['1.pl','2.pl'].
Both of the files are generated by another program written in another programming language and i can't predict the exact content of the files beforehand.
The problem is that in one of the files there is always a rule
predicate1(X):-predicate2(X).
But,sometimes the rule
predicate2(something):-body
does not exist in both of the files and i get a error "predicate2" is undefined, when executing some queries for predicate1.
If i include the line
:- dynamic(predicate2/2).
into one of the files it only helps if predicate/2 is not defined in another file (otherwise i get something like "are you really sure you want to redefine the predicate2/2?". And here i don't want to redefine something to save the data from another file.
So,i have no idea how to make the predicate just "defined". I need a solution for SWI-Prolog or SICStus Prolog. (unfortunately the versions do not have a section for defining predicates,like visual Prolog)
Clearly, Prolog is not able to answer any question we formulate. For instance, if we ask if Donald is a duck
You need to declare
predicate2/2
multifile
with the ISO directive of same name. So in each of the files, you write at the top, or prior to any clauses ofpredicate2/2
:This, regardless of whether or not you are having clauses for that predicate.
The suggestion by @CapelliC is definitely a dangerous move. If you turn off all existence errors you will miss many legitimate errors!
For example, you declared
dynamic(predicate2/2)
but usedpredicate2(_)
. So which arity do you want?In SWI Prolog you can avoid the error. Change the system behaviour using the ISO builtin
The Choice is one of (fail,warning,error).
So your command line will be:
Another possibility: define a fake procedure
HTH