prolog questions.. fails to invoke predicate - prolog

I am trying to understand why duck2 is not called.. Should I cover all cases of the two list being empty, non empty.
duck([H|T], something ) :-
write("*"),
write("?"),
duck2([H|T], T, something_else),
write("Over").
duck2([], [], something_else) :- write("AllDone").
duck2([H|T], [H1,T1], something_else) :-
write("I am here").
trace gives this..
Call: (16) duck([dog, eats], [eats], something) ? creep
Call: (17) write("*") ? creep
*
Exit: (17) write("*") ? creep
Call: (17) write("?") ? creep
?
Exit: (17) write("?") ? creep
Call: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (16) duck([dog, eats], [eats], something) ? creep

duck2([H|T], [H1,T1], something_else) :-
% spaced out to line up with the below trace
duck2([H|T], [H1,T1], something_else) :-
Call: (17) `duck2([dog, eats], [eats], something_else)` ? creep
[H1, T1] is a list of two items and [eats] is a list of one item; if you try and unify them, they don't unify:
?- [H1, T1] = [eats].
false
so the call fails.
Should I cover all cases
Well, at least cover enough cases to make the code work. All cases is up to you.

Related

Prolog: Why doesn't this predicate fail with an empty list passed as an argument

I made a pretty basic predicate to explain my problem, so don't mind it's uselessness. I want the predicate to fail if an empty list is passed as the B argument,
predicate(_,[],_) :- fail.
predicate(A,B,C) :-
write(A),
writeln(B),
C = true.
But when I enter this query:
?- predicate(test,[],X).
Instead of returning false(since it's supposed to fail), it execute the predicate and the output is:
test[]
X = true.
Why won't the predicate fail even if B is an empty list?
In fact, the first clause fails for the query ?- predicate(test,[],X).. However, when the first clause fails, the second clause is trigged and produces the observed output. To check that, you can use predicate trace/0:
?- trace, predicate(test,[],X).
Call: (11) predicate(test, [], _26304) ? creep % <= try first clause
Call: (12) fail ? creep
Fail: (12) fail ? creep
Redo: (11) predicate(test, [], _26304) ? creep % <= try second clause
Call: (12) write(test) ? creep
test
Exit: (12) write(test) ? creep
Call: (12) writeln([]) ? creep
[]
Exit: (12) writeln([]) ? creep
Call: (12) _26304=true ? creep
Exit: (12) true=true ? creep
Exit: (11) predicate(test, [], true) ? creep
X = true.
If you want that predicate/3 fails when called with []as second argument, then you can define that predicate as follows:
predicate(A, B, C) :-
B \= [], % this condition forces failure when B is the empty list
write(A),
writeln(B),
C = true.
Examples:
[trace] ?- predicate(test,[],X).
Call: (10) predicate(test, [], _28066) ? creep
Call: (11) []\=[] ? creep
Fail: (11) []\=[] ? creep
Fail: (10) predicate(test, [], _28066) ? creep
false.
[trace] ?- nodebug.
true.
?- predicate(test,[],X).
false.
?- predicate(test,[a],X).
test[a]
X = true.

Message:1031 Arithmetic overflow in prolog

predicates
nondeterm s(integer,integer)
nondeterm p(integer,integer,integer)
clauses
s(V,R) :-
p(0,V,R).
%,write(R),nl.
p(R,0,R).
p(Inc,V,R) :-
I2=Inc+V,
N1=V-1,
p(I2,N1,R).
goal
s(9,O).
gives an arithmetic overflow,,,why??
In Prolog mathematical expressions are not done with assignment, e.g. =. Prolog does not use assignment like many common programming languages, Prolog uses unification. If you want to do math expressions in Prolog and unify the result with a variable then you need to use the is/2 operator
I2 is Inc+V
N1 is V-1
You also need a guard statement to keep the second clause from being executed if the second parameter is 0.
s(V,O) :-
p(0,V,O).
p(R,0,R).
p(Inc,V,R) :-
V \= 0,
I2 is Inc+V,
N1 is V-1,
p(I2,N1,R).
Now your query returns
?- s(9,O).
O = 45 ;
false.
Complementing Guy's answer, it is instructive to run your query in the Prolog tracer as it clearly shows that, because you used unification (=/2) instead of arithmetic expression evaluation (is/2), your query build an increasingly bigger compound term at each recursive call until the global stack space is exhausted:
[trace] ?- s(9,O).
Call: (8) s(9, _3176) ? creep
Call: (9) p(0, 9, _3176) ? creep
Call: (10) _3396=0+9 ? creep
Exit: (10) 0+9=0+9 ? creep
Call: (10) _3402=9-1 ? creep
Exit: (10) 9-1=9-1 ? creep
Call: (10) p(0+9, 9-1, _3176) ? creep
Call: (11) _3408=0+9+(9-1) ? creep
Exit: (11) 0+9+(9-1)=0+9+(9-1) ? creep
Call: (11) _3414=9-1-1 ? creep
Exit: (11) 9-1-1=9-1-1 ? creep
Call: (11) p(0+9+(9-1), 9-1-1, _3176) ? creep
Call: (12) _3420=0+9+(9-1)+(9-1-1) ? creep
Exit: (12) 0+9+(9-1)+(9-1-1)=0+9+(9-1)+(9-1-1) ? creep
Call: (12) _3426=9-1-1-1 ?
...

Brother in law predicate for Prolog not working

Here's the relevant code:
married(X,Y) :- wife(X,Y);husband(X,Y).
parent(X,Y) :- father(X,Y) ;mother(X,Y).
brother(X,Y) :-
man(X),
parent(Z,X),
parent(Z,Y),
X \= Y.
brother_in_law(X,Y) :-
brother(X,Z),married(Z,Y).
I've googled and it seems other have been using the exact code for the brother in law predicate so it should be fine? I checked the other predicates and they seem good too.. not sure what is happening.
Also, by not working I mean that it's not acknowledging the relevant relation when I check.
Look at the trace and you will see the problem:
?- trace, brother_in_law(prins-daniel, Y).
Call: (9) brother_in_law(prins-daniel, _11346) ? creep
Call: (10) brother(prins-daniel, _11680) ? creep
Call: (11) man(prins-daniel) ? creep
Exit: (11) man(prins-daniel) ? creep
Call: (11) parent(_11678, prins-daniel) ? creep
Call: (12) father(_11678, prins-daniel) ? creep
Fail: (12) father(_11678, prins-daniel) ? creep
Redo: (11) parent(_11678, prins-daniel) ? creep
Call: (12) mother(_11678, prins-daniel) ? creep
Fail: (12) mother(_11678, prins-daniel) ? creep
Fail: (11) parent(_11678, prins-daniel) ? creep
Redo: (11) man(prins-daniel) ? creep
Fail: (11) man(prins-daniel) ? creep
Fail: (10) brother(prins-daniel, _11680) ? creep
Fail: (9) brother_in_law(prins-daniel, _11346) ? creep
false.
Who is prins-daniel's father? You don't have a fact for that. Who is prins-daniel's mother? You don't have a fact for that either. As a result, you can't find any brothers, so the query fails.
Does this mean you are missing facts or missing code? The code says X and Y are brothers-in-law if X has a brother Z who is married to Y. Is this the only way to have a brother-in-law?
Side note: prins-daniel is not an atom in Prolog as it would be in Lisp. This is a term:
?- write_canonical(prins-daniel).
-(prins,daniel)
The situation is compounded by longer terms:
?- write_canonical(johann-georg-av-hohenzollern).
-(-(-(johann,georg),av),hohenzollern)
Just something to be aware of.

Infinite loop in Prolog. How do I prevent this?

I am writing a program in Prolog that represents the rules of a card game. However, I am running into an infinite loop with the following code.
succ(seven_of_hearts,eight_of_hearts).
succ(eight_of_hearts,nine_of_hearts).
succ(nine_of_hearts,ten_of_hearts).
succ(ten_of_hearts,jack_of_hearts).
succ(jack_of_hearts,queen_of_hearts).
succ(queen_of_hearts,king_of_hearts).
succ(king_of_hearts,ace_of_hearts).
succ(X,Y) :-
succ(X,Z),
succ(Z,Y),
!.
beats(X,Y) :-
succ(Y,X).
Basically, my attempt with these few lines is to set up a system for determining if one card beats another according to the succ (or succession) relationships I have established. So the query I am running is beats(X,Y), where X and Y and atoms corresponding to cards. This query terminates successfully if it is true. For example, if I query beats(jack_of_hearts,seven_of_hearts), it returns true and terminates.
However, the query enters an infinite loop if it is false. For example, when I query beats(seven_of_hearts,jack_of_hearts). I traced the query and found that due to the recursive nature of the last succ statement, the query follows the chain of succ statements all the way down to the seven_of_hearts, or all the way up to the ace_of_hearts, and then continuously tries to evaluate succ(seven_of_hearts,X) or succ(ace_of_hearts,X) instead of returning false.
I have two questions: using this current structure, how could I prevent this? Or, if that's not possible, what is an alternative structure I could use to accomplish my goal?
EDIT:
Here is a screenshot of my trace for one of the failing queries:
Call: (267) beats(seven_of_hearts, jack_of_hearts) ? creep
Call: (268) succ(jack_of_hearts, seven_of_hearts) ? creep
Call: (269) succ(jack_of_hearts, _G7104) ? creep
Exit: (269) succ(jack_of_hearts, queen_of_hearts) ? creep
Call: (269) succ(queen_of_hearts, seven_of_hearts) ? creep
Call: (270) succ(queen_of_hearts, _G7104) ? creep
Exit: (270) succ(queen_of_hearts, king_of_hearts) ? creep
Call: (270) succ(king_of_hearts, seven_of_hearts) ? creep
Call: (271) succ(king_of_hearts, _G7104) ? creep
Exit: (271) succ(king_of_hearts, ace_of_hearts) ? creep
Call: (271) succ(ace_of_hearts, seven_of_hearts) ? creep
Call: (272) false ? creep
Fail: (272) false ? creep
Redo: (271) succ(ace_of_hearts, seven_of_hearts) ? creep
Call: (272) succ(ace_of_hearts, _G7104) ? creep
Call: (273) false ? creep
Fail: (273) false ? creep
Redo: (272) succ(ace_of_hearts, _G7104) ? creep
Call: (273) succ(ace_of_hearts, _G7104) ? creep
Call: (274) false ? creep
Fail: (274) false ? creep
Redo: (273) succ(ace_of_hearts, _G7104) ? creep
Call: (274) succ(ace_of_hearts, _G7104) ? creep
Call: (275) false ? creep
Your predicate obviously keeps calling itself: succ(X, Y) :- succ(Y, X), .... A simple solution is not to use the same name for your predicate as you do your facts. Get rid of your succ/2 predicate (leaving the facts) in favor of:
successor(X, Y) :- succ(X, Y).
successor(X, Y) :- succ(X, Z), succ(Z, Y).
beats(X, Y) :- successor(Y, X).

Some doubts about declarative meaning of a program that prints a list of lists in Prolog

Write a rule that take a list whose elements are themselves lists and
print on each line the elements of internals list:
Example:
?- printList[[1,a],[2,b]]).
1 a
2 b
The solution it the following one:
/* BASE CASE: The list is empty, so there is nothing to print */
printList([]).
printList([P|R]):- printList(P),
!,
nl,
printList(R).
printList([X|R]):- write(X),
!,
printList(R).
I have the printList rule that reaches the base case when the list is empty and in this case there is nothing to print.
If I am not in the base case (the list is not empty) it calls the second rule printList([P|R]) and now I have my first doubt:
The element P is a list because Prolog automatically handles an internal list as an element?
So, If I have something like:
[[1,a],[2,b],[3,c],[4,d]] I have that Prolog automatically match in this way:
P = [1,a] (the first list as the head element)
R = [[2,b],[3,c],[4,d]] (the other 3 list are the element of a tail list)
And then the program calls the printList predicate on the head element (the first list) and this version of the rule writes all the elements in the current list.
Is this right this interpretation?
Now I have some doubts about about the trace of this program, for example if I execute this statement I obtain the following trace:
[trace] ?- printList([[1,a], [2,b]]).
Call: (6) printList([[1, a], [2, b]]) ? creep
Call: (7) printList([1, a]) ? creep
Call: (8) printList(1) ? creep
Fail: (8) printList(1) ? creep
Redo: (7) printList([1, a]) ? creep
Call: (8) write(1) ? creep
1
Exit: (8) write(1) ? creep
Call: (8) printList([a]) ? creep
Call: (9) printList(a) ? creep
Fail: (9) printList(a) ? creep
Redo: (8) printList([a]) ? creep
Call: (9) write(a) ? creep
a
Exit: (9) write(a) ? creep
Call: (9) printList([]) ? creep
Exit: (9) printList([]) ? creep
Exit: (8) printList([a]) ? creep
Exit: (7) printList([1, a]) ? creep
Call: (7) nl ? creep
Exit: (7) nl ? creep
Call: (7) printList([[2, b]]) ? creep
Call: (8) printList([2, b]) ? creep
Call: (9) printList(2) ? creep
Fail: (9) printList(2) ? creep
Redo: (8) printList([2, b]) ? creep
Call: (9) write(2) ? creep
2
Exit: (9) write(2) ? creep
Call: (9) printList([b]) ? creep
Call: (10) printList(b) ? creep
Fail: (10) printList(b) ? creep
Redo: (9) printList([b]) ? creep
Call: (10) write(b) ? creep
b
Exit: (10) write(b) ? creep
Call: (10) printList([]) ? creep
Exit: (10) printList([]) ? creep
Exit: (9) printList([b]) ? creep
Exit: (8) printList([2, b]) ? creep
Call: (8) nl ? creep
Exit: (8) nl ? creep
Call: (8) printList([]) ? creep
Exit: (8) printList([]) ? creep
Exit: (7) printList([[2, b]]) ? creep
Exit: (6) printList([[1, a], [2, b]]) ? creep
true.
This is quite clear for me (I think that my previous reasoning is right) but I am not understanding why whenever it reaches an element in an internal list it calls the printList relation on it (that is a simple element and not a list), for example here:
Call: (8) printList(1) ? creep
Fail: (8) printList(1) ? creep
the program had considered the first list of the original list and then, into it to have to print its first element, why call the printList relation on this simple element
Is it because this simple element may in turn be internal lists?
Something like:
[[[1.1,a1,a2],[1.2,b1,b2]], [2,b]] (in which I have a list that contains 2 lists and the first element it is a list that contains 2 lists. So the program checks if an element it is an element or an internal list?
I think you're over-thinking it. Look at the code:
printList([P|R]):- printList(P),
Right there you can see that printList/1 is being unified with the head of the list. The fact that all the rules of printList/1 match lists and only lists is a fact that you, the human, can immediately see. But Prolog does not "notice" this fact, so if you are to call, say,
printList([1])
the first matching rule is the one above, so it will immediately try to unify printList(1). This will fail, naturally, because 1 is not a list and so doesn't match any of the rules for printList/1. Prolog then backtracks and tries the next rule, which is the one that starts like this:
printList([X|R]):- write(X),
This is clearly going to unify [1] with X = 1, R = [], so it's clearly going to write the first element, the one, and then proceed as usual. There is no magic here involving "internal lists," which as far as I'm aware is not a concept in Prolog at all (if such a thing is treated in the compiler it is well hidden from the user of Prolog).
Prolog isn't psychic; it has to try the rules to see if they fail, even if that attempt is essentially a failing pattern-match in the head of the Horn clause.
I am unable to distinguish your first from your second question so I hope this answers both. :)

Resources