3
votes

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.

2

2 Answers

3
votes

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).

2
votes

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.