Hello everyone!

I would like to know if there is a way to write false in SWI Prolog instead of error. The situation is the following:

I have written a prolog program that says if a statement is a formula or not, for example:

?- fmla(or(a,b)). Answer = true

The problem is that if you enter fmla(or(a,ne(b))) prolog will say ERROR since it will try to match ne(b) to my database and it doesn't exist. I would prefer if it said false.

Here is the code just in case you want to check:

neg(F) :- fmla(F).
or(F,G) :- fmla(F), fmla(G).
fmla(F) :- at(F).
fmla(F) :- arg(1, F, A), call(F).
imp(F,G) :- fmla(or(neg(F),G)).
and(F,G) :- fmla(neg(or(neg(F),neg(G)))).
iff(F,G) :- and(imp(F,G),imp(G,F)).

Thank you :P!

PD: in order to simplify the program, the only atomic formulas are a and b.

Edited by Kuroshi

3 Years
Discussion Span
Last Post by rubberman

I haven't written prolog for 30 years, but I think that your problem is that (from what you are indicating) ne(b) is not returning an entity in your database, hence is null, and comparing a non-null with null value is an ERROR as it can't be evaluated either true or false (basic predicate logic). IE, it is doing the right thing, and you need to deal with that possibility.

If you wanted to force an error to return false from a boolean function in C or C++ (and some other languages) you could do something like this (assuming results is a true/false value as the call evaluated and HAVE_ERROR indicates that an error was caught): return (HAVE_ERROR) ? false : results;

Unfortunately I have no clue how you would do that in prolog, and right now I don't have time to dust off my copy of Clocksin & Mellish and look it up... :-)

I hope this is heading you in the right direction in any case.

Edited by rubberman

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.