Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical OR in Prolog

Tags:

prolog

I´m new in Prolog learning and tried out a logical OR-operator for that context:

%a.      
b.

foo:- a ; b.

I have commented a. to try out the logical OR-Operator but it does not work. If you query with ?-foo. you get an exception. Prolog only checks the first term but not the second. Can anyone help me please?

Best regards.

like image 543
Freedom Wanna Avatar asked May 22 '26 18:05

Freedom Wanna


2 Answers

You get the exception as a/0 is undefined. In order to check the or-operator you could explicitely define a as being false.

a:-false.
b.

foo:- a ; b.

Now ?- foo. gives the answer true.

Prolog makes a 'closed world' assumption. It can only evaluate the trueness or falseness of a predicate that is defined. That means that you need at least one clause that has the predicate on its left hand side (or a fact, as this will be interpreted as a clausel with no condition thus without a right hand side).

like image 91
Joan C Avatar answered May 24 '26 08:05

Joan C


This is essentially equivalent to:

foo :- a.
foo :- b.

The first clause tries to assess whether foo is true or not, by evaluating whether a is true or not. a doesn't even exist in the database, therefore you get an error.

like image 28
Tasos Papastylianou Avatar answered May 24 '26 06:05

Tasos Papastylianou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!