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:

at(a).
at(b).
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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.