1
votes

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?

1
What if you just write another predicate to check it and if exists call the predicate else fail e.g: check(LongIdent(....) ). -> false if LongIdent/4 does not exist else call LongIdent/4 would this be ok?? - coder
If these are dynamic facts, you should use the directive, :- dynamic('LongIdent'/4).. This tells Prolog that it's a real predicate or fact but may not have been asserted (yet). - lurker
@coder Not unless I was able to apply that globally / to a whole file? - Nathan Ringo
@lurker The issue there is that the set of facts may change; for each data file it's constant, but there might be different facts generated. It'd be a bit of a pain to enumerate them, too... - Nathan Ringo
@NathanRingo, let's say you define a predicate check as I described, the only thing you need to do is firstly load the predicate check definition and you will call your predicate snot directly like: predicate1(...) but check(predicate1(...)) if that's ok then this could work for a file of predicates... - coder

1 Answers

1
votes

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.