Related
I am trying to understand how to solve the following problem in Prolog. Given the following Prolog code, list all solutions in order for the query c(X,Y,Z).
a(1).
a(2).
b(a).
c(A,B,C) :- a(A),d(B,C).
c(A,B,C) :- b(A),d(B,C).
d(B,C) :- a(B),!,a(C).
d(B,_) :- b(B).
I loaded up SWI Prolog to try it out myself, but I am unsure how it reaches the conclusion:
X = Y, Y = Z, Z = 1 ;
X = Y, Y = 1,
Z = 2 ;
X = 2,
Y = Z, Z = 1 ;
X = Z, Z = 2,
Y = 1 ;
X = a,
Y = Z, Z = 1 ;
X = a,
Y = 1,
Z = 2.
Starting small, I tried to query just d(B,C). with the same code, leading to the following conclusion:
Y = Z, Z = 1 ;
Y = 1,
Z = 2.
I understand how to get the Y=1 and Z=1,2, but I am unsure how the code leads to Y=Z. If anyone could help me with the logic to reach these conclusions, that would be well appreciated!
Let's start by looking at the predicates a/1 and b/1: We expect them to produce two and one answer(s) respectively and indeed that is the case:
?- a(X).
X = 1 ? ;
X = 2
?- b(X).
X = a
Next let's look at the predicate d/2 but without the cut, that is:
d(B,C) :- a(B),a(C).
d(B,_) :- b(B).
If we query this predicate:
?- d(B,C).
Prolog will start with the first rule of the predicate d/2, that is d(B,C) :- a(B),a(C). and tries to prove the first goal a(B). That succeeds with the substitution B = 1. So Prolog goes on to prove the goal a(C). Again that succeeds with the substitution C = 1. Prolog then reports the first solution to the query:
?- d(B,C).
B = C = 1 ?
So we ask if there are any more solutions by pressing ;:
?- d(B,C).
B = C = 1 ?;
Now Prolog tries to find another solution for a(C) and finds the substitution C = 2:
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ?
Again we ask if there's more:
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
Now Prolog fails to find another solution for a(C), so it backtracks to the first goal and tries to find an alternative solution for a(B) and succeeds with the substitution B = 2. So it goes on to prove the second goal a(C) to which again the substitution C = 1 is found. So Prolog reports this new solution.
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
B = 2,
C = 1 ?
We ask for yet more solutions:
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
B = 2,
C = 1 ? ;
So Prolog looks for another solution for a(C) and finds C = 2. So it answers:
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
B = 2,
C = 1 ? ;
B = C = 2 ?
We press ; again
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
B = 2,
C = 1 ? ;
B = C = 2 ? ;
And Prolog looks for other solutions for a(C) but can't find any. So it backtracks to the goal a(B) and again fails to produce new answers here. So those are all solutions for the first rule of d/2. Prolog now goes on and tries to find a solution for the second rule d(B,_) :- b(B).. This rule only contains one goal: b(B) and Prolog finds a substitution: B = a, so it answers:
?- d(B,C).
B = C = 1 ?;
B = 1,
C = 2 ? ;
B = 2,
C = 1 ? ;
B = C = 2 ? ;
B = a
?-
And there are no more solutions. Let's observe that while Prolog traverses the proof tree as seen above, every time alternative substitutions can't be ruled out a choice point is created and in the course of the search for answers Prolog can backtrack to these choicepoints to look for alternative solutions. Every time an answer in the above query ended with ? Prolog still had some choice points left to explore and by pressing ; we asked it to explore them.
Now let's move our attention back to your posted version of d/2 with the cut in the first rule:
d(B,C) :- a(B),!,a(C).
d(B,_) :- b(B).
What a cut does is to prune away choice points back to where it was when the predicate that contains it was called. So it is essentially throwing away untried alternatives, thereby committing Prolog to the first substitution(s) it finds. So if we issue the query again with this version:
?- d(B,C).
Prolog again starts with the first rule, looking for a solution for a(B), successfully substituting B = 1 encountering the cut, therefore committing to this solution and then searching for a proof for a(C), again successfully substituting C = 1. Prolog then answers:
?- d(B,C).
B = C = 1 ?
indicating that there are open choice points. Again we want more:
?- d(B,C).
B = C = 1 ? ;
Prolog finds another substitution C=2 for the goal a(C) and reports:
?- d(B,C).
B = C = 1 ? ;
B = 1,
C = 2
?-
But this time the query terminates. That is because the choicepoint for an alternative solution for the goal a(B) was pruned away thereby leaving us without the solutions B = 2, C = 1 and B = C = 2. Furthermore the choicepoint that would have led to the second rule being explored was also pruned away thereby robbing us of the solution B = a. Cuts that prune away correct solutions as seen above are often referred to as red cuts in the literature.
Note that by cutting away actual solutions this program is not logically correct anymore. With the above query you asked for all solutions of the predicate d/2 but got only two of them. However, if you ask for the other solutions specifically, the respective queries still succeed:
?- d(2,1).
yes
?- d(2,2).
yes
?- d(a,_).
yes
?-
Returning to your original question, let's compare the query ?- c(X,Y,Z). with both versions of d/2. To make comparisons easier, the variables X, Y and Z are replaced with A, B and C just like they occur in the predicate definitions:
% cut-free version % version with the cut:
?- c(A,B,C). ?- c(A,B,C).
A = B = C = 1 ? ; A = B = C = 1 ? ;
A = B = 1, A = B = 1,
C = 2 ? ; C = 2 ? ;
A = C = 1,
B = 2 ? ;
A = 1,
B = C = 2 ? ;
A = 1,
B = a ? ;
A = 2, A = 2,
B = C = 1 ? ; B = C = 1 ? ;
A = C = 2, A = C = 2,
B = 1 ? ; B = 1 ? ;
A = B = 2,
C = 1 ? ;
A = B = C = 2 ? ;
A = 2,
B = a ? ;
A = a, A = a,
B = C = 1 ? ; B = C = 1 ? ;
A = a, A = a,
B = 1, B = 1,
C = 2 ? ; C = 2
A = a,
B = 2,
C = 1 ? ;
A = a,
B = C = 2 ? ;
A = B = a
For the sake of brevity I'm not going to iterate through the steps of the proof, but let's observe a few key things: Firstly, in both rules of c/3 the predicate d/2 is called by the same goal d(B,C) and we have already looked at that goal in detail for both versions. Secondly, we can observe that both rules of c/3 are taken into regard in the search for answers with both versions of the predicate by seeing all three substitutions, A = 1, A = 2 and A = a appearing in both answer-sets. So the cut doesn't prune away the choice point here. Thirdly, the three solutions of d/2 that are pruned away by the cut: B = 2, C = 1, B = 2, C = 2 and B = a are missing in the answers for all three substitutions of A in the version with the cut. And finally, the version of c/3 with the cut is also not logically correct, as the most general query does not give you all the answers but you can query for the thrown away solutions directly and Prolog will find them, e.g.:
?- c(1,2,1).
yes
?- % etc.
Try the query trace, c(X,Y,Z).
I'm testing Prolog ability to test contradiction. To test this, I came up with the following scenario:
There are three suspects: a, b, and c, who are on trial, and one of the suspect is guilty and the rest are innocent.
The facts of the case are,
(Fact 1) if a is innocent then c must be guilty, and
(Fact 2) if a is innocent then c must be innocent.
The answer to who is guilty is suspect 'a' because suspect 'c' cannot be both guilty and innocent. The following code is my implementation:
who_guilty(Suspects) :-
% Knowledge Base
Suspects=[[Name1,Sentence1],
[Name2, Sentence2],
[Name3,Sentence3]],
% Suspects
Names=[a,b,c],
Names=[Name1,Name2,Name3],
% One Is Guilty
Sentences=[innocent,innocent,guilty],
permutation(Sentences,[Sentence1,Sentence2,Sentence3]),
% If A Innocent Then C Is Guilty
(member([a,innocent],Suspects) -> member([c,guilty], Suspects) ; true),
% If A Innocent Then C Is Innocent
(member([a,innocent],Suspects) -> member([c,innocent], Suspects) ; true).
To get Prolog's answer, the query that needs to be run is who_guilty(S). Prolog will then output two identical answers:
S = [[a, guilty], [b, innocent], [c, innocent]] ?
S = [[a, guilty], [b, innocent], [c, innocent]]
My central question is how can I get only one answer instead of two?
Using clpfd library, you can solve this problem easily:
solve(L):-
L = [A,B,C], %0 innocent, 1 guilty
L ins 0..1,
A + B + C #= 1, %one is guilty
A #= 0 #==> C #= 1, %if a is innocent then c must be guilty
A #= 0 #==> C #= 0, %if a is innocent then c must be innocent
label(L).
?- solve(L).
L = [1, 0, 0]
Using clpb :
:- use_module(library(clpb)).
% 0 means guilty
% 1 means innocent
guilty(A,B,C) :-
% only one is guilty
sat(~A * B * C + A * ~B * C + A * B * ~C),
% Fact 1
sat(A =< ~C),
% Fact 2
sat(A =< C).
?- guilty(A,B,C).
A = 0,
B = C, C = 1.
A compact solution, that follows your intuition about expressing the facts.
who_guilty(L) :-
select(guilty,L,[innocent,innocent]),
( L = [innocent,_,_] -> L = [_,_,guilty] ; true ),
( L = [innocent,_,_] -> L = [_,_,innocent] ; true ).
yields:
?- who_guilty(L).
L = [guilty, innocent, innocent] ;
false.
thanks to joel76 (+1), here is a more synthetic solution based on library(clpb)
?- sat(card([1],[A,B,C])*(~A =< ~C)*(~A =< C)).
A = 1,
B = C, C = 0.
1 means guilty...
You should add a new predicate which checks whether someone is both innocent and guilty, which then answers yes to whether there are contradictory outcomes. You are giving Prolog two facts, meaning two correct conclusions to the query. Your real question is "do I have facts contradicting each other?", which means A and NOT A are both true at the same time. contradiction(A, not(A)).
All truths are universal and you are giving two truths that are contradictory to Prolog, so both are true to Prolog.
Consider the following simple program:
h(n0).
h(p(A,N)) :- (A=a, h(N)) ; (A=b , h(N)).
Query:
1 ?- h(p(A,N)).
A = a,
N = n0 ;
A = a,
N = p(a, n0) ;
A = a,
N = p(a, p(a, n0)) ;
A = a,
N = p(a, p(a, p(a, n0))) ;
A = a,
N = p(a, p(a, p(a, p(a, n0)))) ;
...
Since the first disjunct [(A=a, h(N))] produces infinite number of answers it cannot show the answers produced by the second disjunct [(A=b , h(N))].
The question is:
Is it possible to change the code so that on the query, it alternates between the solutions from the first disjunct and the second one?
To get a fair listing of the results of h/1 you can use an auxiliary predicate, say h2/2 that consists of two goals: 1) A predicate pairs/1 that is only describing the the structure of your solution without concrete values:
pairs(n0).
pairs(p(A,N)) :-
pairs(N).
And yields the following answers:
?- pairs(X).
X = n0 ? ;
X = p(_A,n0) ? ;
X = p(_A,p(_B,n0)) ? ;
X = p(_A,p(_B,p(_C,n0))) ?
...
2) Your predicate h/1 as the second goal that describes what the variables _A, _B, _C,... actually are:
h2(X) :-
pairs(X),
h(X).
If you query this predicate you get the desired results:
?- h2(X).
X = n0 ? ;
X = p(a,n0) ? ;
X = p(b,n0) ? ;
X = p(a,p(a,n0)) ? ;
X = p(a,p(b,n0)) ? ;
X = p(b,p(a,n0)) ? ;
X = p(b,p(b,n0)) ? ;
X = p(a,p(a,p(a,n0))) ? ;
...
Note, how the first goal pairs/2 is producing your nested pair-structure one pair at a time. Then the second goal, your original predicate, is producing all possible combinations of a's and b's for that very pair. Then h/2 is backtracking to the next pair produced by pairs/2. And so on.
I cannot figure out why the following query from the given Prolog code generates the error Out of local stack.
Prolog code:
likes(g,c).
likes(c,a).
likes(c,b).
likes(b,a).
likes(b,d).
likes(X,Z) :- likes(X,Y), likes(Y,Z).
the query
?- likes(g,X).
results in
X = c ;
X = a ;
X = b ;
ERROR: Out of local stack
Edit 1 This is the way I think that Prolog should deal with this query,
likes(g,c) is a fact, so X={c}
likes(g,b) <= likes(g,c) and likes(c,b), so X={c,b}
likes(g,a) <= likes(g,b) and likes(b,a), so X={c,b,a}
likes(g,d) <= likes(g,b) and likes(b,d), so X={c,b,a,d}
likes(g,a) and false, so nothing to add to X
likes(g,d) and false, so nothing to add to X
end of backtracking search.
Edit 2 I managed to get what I was looking for by the following modification of the code:
likes(g,c).
likes(c,a).
likes(c,b).
likes(b,a).
likes(b,d).
indirect_likes(A,B):- likes(A,B).
indirect_likes(A,C):- likes(B,C), indirect_likes(A,B).
the query
?- indirect_likes(g,Which).
results in
Which = c ;
Which = a ;
Which = b ;
Which = a ;
Which = d ;
false.
However, there is still something which I cannot figure out the rationale behind. If I change the last rule to be
indirect_likes(A,C):- indirect_likes(A,B), likes(B,C).
Then I get ERROR: Out of local stack! As far as I know, logical conjunction is commutative.
To get the transitive-closure of binary relation R_2, use meta-predicate closure/3 like so:
?- closure(R_2,From,To).
Let's run a sample query of closure/3 together with likes/2!
?- closure(likes,X,Y).
X = g, Y = c
; X = g, Y = a
; X = g, Y = b
; X = g, Y = a % redundant
; X = g, Y = d
; X = c, Y = a
; X = c, Y = b
; X = c, Y = a % redundant
; X = c, Y = d
; X = b, Y = a
; X = b, Y = d
; false. % query terminates universally
We get the same answers when we use indirect_likes/2, but in a different order:
?- indirect_likes(X,Y).
X = g, Y = c
; X = c, Y = a
; X = c, Y = b
; X = b, Y = a
; X = b, Y = d
; X = g, Y = a
; X = g, Y = b
; X = c, Y = a % redundant
; X = g, Y = a % redundant
; X = c, Y = d
; X = g, Y = d
; false. % query terminates universally
As you stated in your comments to #C.B.'s answer, binary relations are not necessarily reflexive and/or symmetric. With the definition you gave, likes/2 is neither:
?- likes(X,X).
false. % not reflexive (not even close)
?- likes(X,Y), likes(Y,X).
false. % not symmetric (not even close)
So far, so good!
Let's tentatively add the following additional fact to your database:
likes(b,b).
With this expanded definition, indirect_likes/2 behaves erratically:
?- indirect_likes(b,b).
true
; true
; true
... % does not terminate universally
?- indirect_likes(X,Y), false. % do we get finite failure?
... % no! query does not terminate universally
What can we do? Let's use meta-predicate closure/3 with the expanded version of likes/2!
?- closure(likes,b,b).
true % succeeds non-deterministically
; false. % query terminates universally
?- closure(likes,X,Y), false. % do we get finite failure?
false. % yes! query terminates universally
The bottom line?
In pure Prolog, conjunction is commutative, as far as the logical meaning is concerned.
Due to Prolog's SLD resolution, the goal false,repeat finitely fails, but repeat,false does not.
The programmer needs to take care of termination, but usually this is a small price to pay for the raw performance and control that Prolog offers.
In this answer I passed "termination worries" on to the implementor of meta-predicate closure/3 :)
You spin off into infinite recursion.
Once you get to likes(b,a), you call likes(a,_G4382), which has no fact defined so it switches to the rule likes(X,Z) :- likes(X,Y), likes(Y,Z). So it calls likes(a,_G4383) which calls likes(X,Z) :- likes(X,Y), likes(Y,Z). over and over and over.
You might want to define and auxillary predicate aux_likes/2 that hosts all your facts. This will only work if there are no circular relationships, i.e. aux_likes(b,c) and aux_likes(c,b). You would also need to define likes(X,X). This is essentially a graph problem and in graph theory a node has to be connected to itself. If you use it as a generator, it will go off into into infinite recursion (if you have cycles) unless you add cuts which are not ideal.
If using swi-prolog, feel free to enter the debug or spy query to see what's going on.
Code:
aux_likes(g,c).
aux_likes(c,a).
aux_likes(c,b).
aux_likes(b,a).
aux_likes(b,d).
likes(X,Z) :- aux_likes(X,Y), likes(Y,Z).
likes(X,X).
Output with new predicate:
?- likes(g,X),print(X),nl,fail.
a
a
d
b
c
g
false.
This says g can like a through c or through b. It likes d through b, it likes b through c and it likes c directly. It also must like itself inorder to query like this. If you would rather have the usage mode (+,+) meaning you supply it with both terms and no variables (as a checker) you can do
?- likes(g,c).
true .
I am working on an assignment that deals with lists in prolog. The basic idea is that given a list, prolog should be able to determine if a value is repeated at all, repeated only once, or repeated only twice, etc. I thought the simplest solution would be to count the number of times a value occurs and then use that count to determine how many times it is repeated.
list_count([],X,0).
list_count([X|T],X,Y) :- list_count(T,X,Z), Y is 1 + Z.
list_count([X1|T],X,Z) :- X1 \= X, list_count(T,X,Z).
repeated_in(+E,+List) :- list_count(List,E,Num), Num >= 2.
No matter what I do though my first predicate always fails. Help?
list_count/3 does work. I think the only issue is the improper usage of prefix '+': try
% repeated_in(+E,+List)
repeated_in(E,List):- list_count(List,E,Num), Num >= 2.
note: prefixing arguments is used for documentation purpose, as a recap about mode usage
Here a logically pure implementation, based on
if_/3 and (=)/3 by #false.
atLeastOnceMember_of(E,[X|Xs]) :-
if_(E = X, true, atLeastOnceMember_of(E,Xs)).
atLeastTwiceMember_of(E,[X|Xs]) :-
if_(E = X, atLeastOnceMember_of(E,Xs), atLeastTwiceMember_of(E,Xs)).
First, let's look at the queries you suggested in your question:
?- atLeastTwiceMember_of(a,[a,b,a,b,a,c]).
true. % succeeds deterministically
?- atLeastTwiceMember_of(b,[a,b,a,b,a,c]).
true. % succeeds deterministically
?- atLeastTwiceMember_of(c,[a,b,a,b,a,c]).
false.
?- atLeastTwiceMember_of(x,[a,b,a,b,a,c]).
false.
The code is monotone, so we get logically sound answers for more general uses, too!
?- atLeastTwiceMember_of(X,[a,b,a,b,a,c]).
X = a ;
X = b ;
false.
At last, let us consider a generalization of above query:
?- atLeastTwiceMember_of(X,[A,B,C,D,E,F]).
X = A, A = B ;
X = A, A = C, dif(C,B) ;
X = A, A = D, dif(D,C), dif(D,B) ;
X = A, A = E, dif(E,D), dif(E,C), dif(E,B) ;
X = A, A = F, dif(F,E), dif(F,D), dif(F,C), dif(F,B) ;
X = B, B = C, dif(C,A) ;
X = B, B = D, dif(D,C), dif(D,A) ;
X = B, B = E, dif(E,D), dif(E,C), dif(E,A) ;
X = B, B = F, dif(F,E), dif(F,D), dif(F,C), dif(F,A) ;
X = C, C = D, dif(D,B), dif(D,A) ;
X = C, C = E, dif(E,D), dif(E,B), dif(E,A) ;
X = C, C = F, dif(F,E), dif(F,D), dif(F,B), dif(F,A) ;
X = D, D = E, dif(E,C), dif(E,B), dif(E,A) ;
X = D, D = F, dif(F,E), dif(F,C), dif(F,B), dif(F,A) ;
X = E, E = F, dif(F,D), dif(F,C), dif(F,B), dif(F,A) ;
false.