ckarakoc
ckarakoc

Reputation: 51

LearnPrologNow compiler doesn't work as intended

This will be my first question on Stack Overflow.
I was reading through the learnPrologNow manual and then I began making the exercises on chapter 3, the answers my compiler gave were not what I expected (and I think not what was supposed to come out). So I tested it out with the examples the book gave me:
the knowledge base:

child(anne,bridget).
child(bridget,caroline).
child(caroline,donna).
child(donna,emily). 
descend(X,Y)  :-  child(X,Y).
descend(X,Y)  :-  child(X,Z), descend(Z,Y).

So I asked the compiler:

12 ?- descend(bridget,caroline).
true ;
false.

13 ?- descend(caroline,bridget).
false.

Where does the second false (of 12) come from? This also occured in excersise 3.2 (whom I took from a GitHub page to check the correct one) with the knowledge base:

greater_than(succ(X),0).
greater_than(succ(X),succ(Y)) :-
          greater_than(X,Y).

with the answers:

14 ?- [ex32].
Warning: c:/users/user/documents/prolog/ex32.pl:1:
        Singleton variables: [X]
true.

15 ?- greater_than(3,2).
false.

16 ?- greater_than(2,3).
false.

I am using SWI-Prolog 7.2.3 64-bit version.
Any suggestions what might be wrong?

Upvotes: 2

Views: 95

Answers (2)

user1812457
user1812457

Reputation:

The other answer explains your first problem. The second problem is different. You are supposed to use natural numbers in successor notation, so instead of 0, 1, 2, 3, ..., you need to use 0, succ(0), succ(succ(0)), succ(succ(succ(0))), ....

This is one of the more famous early Prolog examples, and you can find many questions about it here on Stack Overflow, and many examples of it elsewhere online. My personal opinion is that doing arithmetic in successor notation is just insane, but if you are interested, try to search for "peano arithmetic", or maybe "successor arithmetic", like this.

If you want arithmetic comparisons, use the arithmetic predicates. With those, you can compare numbers and arithmetic expressions:

?- 2 > 3.
false.

?- 3 < 4.
true.

?- 1+2+3 =< 6.
true.

For comparing arbitrary terms, you can use the predicates that compare on standard order of terms. Note that you will get different answers for some cases:

?- 1+2 > 99999.
false.

?- 1+2 @> 99999.
true.

The documentation explains why. By the way, it also explains why natural numbers in successor notation compare correctly if you use standard order of terms:

?- s(s(0)) @> s(0).
true.

?- s(s(0)) @< s(0).
false.

Upvotes: 3

The second false result comes from SWI Prolog trying to continue deducing information.

After the first true, you hit the spacebar, then SWI Prolog starts backtracking to see what else it can deduce. It can't establish a new derivation of descend(bridget,caroline), so this time the result is false, and backtracking ends.

Upvotes: 2

Related Questions