Nathan Ringo
Nathan Ringo

Reputation: 1003

Ignore undefined facts in SWI Prolog

I'm running queries against a generated set of facts, and some facts may not exist. When that happens, SWI Prolog errors out with e.g. Undefined procedure: 'LongIdent'/4. Is there a way to get it to instead simply have the goal involving 'LongIdent'/4 fail?

Upvotes: 1

Views: 818

Answers (1)

coder
coder

Reputation: 12972

Well you could change the default behavior using unknown/2 which is declared as unknown(-Old, +New), Old is the old-current flag, New is the new flag that you use:

?- unknown(trace,fail).
Warning: Using a non-error value for unknown in the global module
Warning: causes most of the development environment to stop working.
Warning: Please use :- dynamic or limit usage of unknown to a module.
Warning: See http://www.swi-prolog.org/howto/database.html
true.

?- a(1).
false.

But you see the warnings explaining that this may not be a good idea...

If you don't know the current/old flag you could use it like:

?- unknown(X,fail).
    Warning: Using a non-error value for unknown in the global module
    Warning: causes most of the development environment to stop working.
    Warning: Please use :- dynamic or limit usage of unknown to a module.
    Warning: See http://www.swi-prolog.org/howto/database.html
    X = trace.

Upvotes: 1

Related Questions