Search code examples
prologiso-prolog

Best way to define predicate in Prolog


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)


Solution

  • In SWI Prolog you can avoid the error. Change the system behaviour using the ISO builtin

    :- set_prolog_flag(unknown, Choice).
    

    The Choice is one of (fail,warning,error).

    So your command line will be:

    swipl -g “set_prolog_flag(unknown,fail),['1.pl','2.pl']."
    

    Another possibility: define a fake procedure

    swipl -g “assert(predicate2(_):-fail),['1.pl','2.pl']."
    

    HTH