Basic prolog issue with syntax of statement - prolog

I'm sort of a beginner to Prolog and I have a quick question. I've written a sister_of/2 predicate that goes like this:
sister_of(X, Y) :-
female(X),
parents(X, Z, W) == parents(Y,Z,W),
X \= Y.
I read this as X is a sister of Y if X is a female and the parents of X which are Z W are the same parents of Y, Z W and X and Y are not the same person. This isn't running for some reason though, so it must be a syntax issue, some insight would be fantastic. Thank you.

Prolog is not a functional language, it's a relational language. You define predicates (i.e. relations), not functions. Thus, your parents(X, Z, W) == parents(Y,Z,W) is just comparing two terms, parents(X, Z, W) and parents(Y,Z,W) for equality. Assuming that a parents/3 predicate is also defined, you want something like:
sister_of(X, Y) :-
female(X),
parents(X, Z, W),
parents(Y, Z, W),
X \= Y.

Well, you began the construction of reasoning alright, but failed during the comparison. Here's what you've missed:
sister_of(X, Y) :-
female(X),
parents(X, Z, W),
parents(Y, Z, W),
X \= Y.
Now the reasoning goes as follows:
X is a sister of Y;
if X is a female;
if X has parents Z and W;
if Y has parents Z and W;
and, of course, X is not Y, so that X is not a sister of itself.
Notice that, the comparison you performed earlier is not necessary (and in fact, does not mean what you expected), since Z and W become limited to be the parents of X in step 3. This means that, Z and W already have a determined value after step 3 -- they are bound, in other words.
After that, in step 4, Y can only assume the same value of X, or some other value that makes parents(Y, Z, W) true. Finally, you remove the cases where X == Y in step 5, which gives you a valid definition of sister_of(X, Y).

Related

Can I use integers as parameters?

How can I know if a person X is descendant of a person Y given the descendancy degree?
I've tried this:
descendant(X, Y, 1) :- son(X, Y).
descendant(X, Y, Degree) :- son(X, Z) , descendant(Z, Y, Degree-1).
Where son(X, Y) returns yes if X is son of Y. If Degree == 1 it returns the correct answer but for descendant(X, Y, 2), for instance, should return yes if X is grandson of Y but returns no.
1) Naming: Does son(X,Y) mean "X is the son of Y"—or vice-versa?
son_of(X,Y) is better.
2) Exploit successor-arithmetics: We don't need to do general arithmetics here... we only need to count.
So let's start in the beginning...
child_of(abel, adam). % from source
child_of(abel, eve).
child_of(cain, adam).
child_of(cain, eve).
child_of(enoch, cain).
child_of(irad, enoch).
child_of(mehujael, irad).
child_of(methushael, mehujael).
child_of(lamech, methushael).
child_of(jabal, lamech).
child_of(jabal, adah).
child_of(jubal, lamech).
child_of(jubal, adah).
child_of(tubal_cain, lamech).
child_of(tubal_cain, zillah).
child_of(naamah, lamech).
child_of(naamah, zillah).
child_of(seth, adam).
child_of(seth, eve).
child_of(enos, seth).
child_of(kenan, enos).
child_of(mahalalel, kenan).
child_of(jared, mahalalel).
child_of(enoch, jared).
child_of(methuselah, enoch).
child_of(lamech, methuselah).
child_of(noah, lamech).
child_of(shem, noah).
child_of(ham, noah).
child_of(japheth, noah).
Based on child_of/2 we first define ancestor_of/2—this should be nothing new to you!
ancestor_of(Y, Z) :-
child_of(Z, Y). % If Z is a child of Y ...
% then Y is an ancestor of Z.
ancestor_of(X, Z) :-
child_of(Z, Y), % If Z is a child of Y ...
ancestor_of(X, Y). % and X is an ancestor of Y ...
% then X is an ancestor of Z.
Next, we add an additional parameter indicating the distance.
We use s/1 terms to represent natural numbers and add a new argument to ancestor_of/2:
ancestor_of_dist(Y, Z, s(0)) :-
child_of(Z, Y). % If Z is a child of Y ...
% then Y is an ancestor of Z with distance = 1."
ancestor_of_dist(X, Z, s(N)) :-
child_of(Z, Y), % If Z is a child of Y ...
ancestor_of_dist(X, Y, N). % and X is an ancestor of Y with distance N ...
% then X is an ancestor of Z with distance N+1.
So ... who is grandparent of whom?
?- ancestor_of_dist(X, Z, s(s(0))).
X = adam, Z = enoch
; X = eve, Z = enoch
; X = cain, Z = irad
; X = jared, Z = irad
; X = enoch, Z = mehujael
; ...
; X = lamech, Z = japheth
; false.
Prolog is not a functional language. Thus, the Degree-1 term is not interpreted and evaluated as an expression. For Prolog, Degree-1 is just a compound term with two arguments. That can be made clear using the standard write_canonical/1 predicate, which writes a term without using operator notation:
?- write_canonical(Degree-1).
-(_,1)
true.
Write instead:
descendant(X, Y, 1) :-
son(X, Y).
descendant(X, Y, Degree) :-
son(X, Z) ,
descendant(Z, Y, Degree0),
Degree is Degree0 + 1.
The is/2 standard built-in predicate unifies the left argument with the value of the arithmetic expression in the right argument.
P.S. Note that the alternative descendant/3 predicate definition I suggest will solve the problem you described but it is not an efficient definition as it is not a tail-recursive definition. I.e. the recursive call in the second clause is not the last call in the clause body. This issue can be easily solved, however, by using an accumulator.

Prolog, X element before element Y on list [duplicate]

This question already has answers here:
Prolog, X before Y in a List
(4 answers)
Closed 6 years ago.
I am going to write predicate which is true iff only and only when element X occurs before Y on list L
before(L, X, Y) :-
nth1(PX, L, X),
nth1(PY, L, Y),
PX < PY.
Above, you can see my solution. What do you think about it ?
When it comes to my specific question:
My predicate returns true when there is exists at least one pair that Y followed X. How to define predicate such that it is true for each pair ?
The solution you show works for the "if one exists" case, but is somewhat imperative in nature. That is, it's a little bit like a C program translated to Prolog. Imperative means you are telling the computer, using the programming language, what steps to execute in order to achieve your results.
To be more declarative or relational, your "exists" solution could be expressed nicely as a DCG:
... --> [].
... --> [_], ... .
before(X, Y) --> ... , [X], ... , [Y], ... .
(NOTE: You can in Prolog have a predicate named ..., which is shown here.) This describes the relationship of X and Y in the list. It does not describe steps to execute, but instead describes the relationship of X and Y in a sequence. This solution has been shown before on SO.
Following this approach (where we describe the relationship of X and Y), one way (not necessarily the only way) to express that all the X precede all the Y would be:
before_all(X, Y) -->
{ dif(X,Y) },
any_sequence_but(Y), [X], any_sequence_but(Y), [Y], any_sequence_but(X).
any_sequence_but(_) --> [].
any_sequence_but(Y) --> [X], { dif(X,Y) }, any_sequence_but(Y).
Which yields a solution like this:
?- phrase(before_all(X,Y), [b,a,b,c,a,b,d]).
X = b,
Y = d ;
X = a,
Y = d ;
X = b,
Y = d ;
X = c,
Y = d ;
X = a,
Y = d ;
X = b,
Y = d ;
false.
?-
If the condition should hold for all pairs, the condition should hold for at least one pair, while its converse shouldn't be true for any pair.
I took the liberty of renaming your before/3 to beforeSome/3.
beforeSome(L, X, Y) :-
nth1(PX, L, X),
nth1(PY, L, Y),
PX < PY.
beforeAll(L, X, Y) :-
beforeSome(X,Y),
not(beforeSome(L, Y, X)).
Which yields the desired results:
?- beforeAll([1,2,3,1,4,5], 1, 4).
true.
?- beforeAll([1,2,3,1,4,5], 1, 2).
false.
Please note that your use of nth1/3 precludes it being used with uninstantiated variables. In other words, beforeAll([1,2,3,1,4,5], X, Y). is false.
A better implementation of beforeSome/3 would be something like
beforeSome([X|T], X, Y) :-
member(Y, T).
beforeSome([_|T], X, Y) :-
beforeSome(T, X, Y).
% no change needed, but repeated here for completeness' sake
beforeAll(L, X, Y) :-
beforeSome(X,Y),
not(beforeSome(L, Y, X)).

Get only one solution for sibling pairs in family tree

I have the following Prolog code, modified from Wikipedia:
mother_child(trude, sally).
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
different(X, Y) :- X \== Y.
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y), different(X, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
I get the following:
?- sibling(X, Y).
X = sally,
Y = erica ;
X = erica,
Y = sally ;
false.
Is it possible to modify my code so that ?- sibling(X, Y) to return only: X = sally, y = erica ; false.? That is, I want to eliminate instances where X = Y and Y = X (converse).
The comment by #lurker says it all. Ask yourself what it is that the query is asking:
Is there a pair X, Y such that:
X has the parent Z:
Z is a father
Z is a mother
Y has the parent Z:
Z is a father
Z is a mother
X and Y are not the same
In your current database, a child can have at most one mother and one father, so the two clauses of parent_child/2 happen to be mutually exclusive for a single child.
But you have the subgoal parent_child/2 twice in sibling/2, so for every pair of siblings, you can have either one of the two in the first, and then the other one in the second, so you have two proofs for each pair. Saying X #< Y instead of X \== Y will make sure that only one of the two possible combinations of X and Y lead to a successful proof.

Don't repeat solutions in Prolog

Suppose you have a database with the following content:
son(a, d).
son(b, d).
son(a, c).
son(b, c).
So a and b are sons of d and c. Now you want to know, given a bigger database, who is brother to who. A solution would be:
brother(X, Y) :-
son(X, P),
son(Y, P),
X \= Y.
The problem with this is that if you ask "brother(X, Y)." and start pressing ";" you'll get redundant results like:
X = a, Y = b;
X = b, Y = a;
X = a, Y = b;
X = b, Y = a;
I can understand why I get these results but I am looking for a way to fix this. What can I do?
Prolog will always try to find every possible solution available for your statements considering your set of truths. The expansion works as depth-first search:
son(a, d).
son(b, d).
son(a, c).
son(b, c).
brother(X, Y) :-
son(X, P),
son(Y, P),
X \= Y.
brother(X, Y)
_______________________|____________________________ [son(X, P)]
| | | |
X = a, P = d X = b, P = d X = a, P = c X = a, P = b
| | | |
| ... ... ...
|
| (X and P are already defined for this branch;
| the algorithm now looks for Y's)
|__________________________________________ [son(Y, d)]
| |
son(a, d) -> Y = a son(b, d) -> Y = b
| |
| | [X \= Y]
X = a, Y = a -> false X = a, Y = b -> true
|
|
solution(X = a, Y = b, P = d)
But, as you can see, the expansion will be performed in all the branches, so you'll end up with more of the same solution as the final answer. As pointed by #Daniel Lyons, you may use the setof built-in.
You may also use the ! -- cut operator -- that stops the "horizontal" expansion, once a branch has been found to be valid, or add some statement that avoids the multiple solutions.
For further information, take a look at the Unification algorithm.
First, I would advise against updating the Prolog database dynamically. For some reasons, consider the article
"How to deal with the Prolog dynamic database?".
You could use a combination of the builtin setof/3 and member/2, as #DanielLyons has suggested in his answer.
As yet another alternative, consider the following query which uses setof/3 in a rather unusual way, like this:
?- setof(t,brother(X,Y),_).
X = a, Y = b ;
X = b, Y = a.
You can eliminate one set with a comparison:
brother(X, Y) :-
son(X, P),
son(Y, P),
X \= Y, X #< Y.
?- brother(X, Y).
X = a,
Y = b ;
X = a,
Y = b ;
false.
Since X and Y will be instantiated both ways, requiring X be less than Y is a good way to cut the solutions in half.
Your second problem is that X and Y are brothers by more than one parent. The easiest solution here would be to make your rules more explicit:
mother(a, d).
mother(b, d).
father(a, c).
father(b, c).
brother(X, Y) :-
mother(X, M), mother(Y, M),
father(X, F), father(Y, F),
X \= Y, X #< Y.
?- brother(X, Y).
X = a,
Y = b ;
false.
This method is very specific to this particular problem, but the underlying reasoning is not: you had two copies because a and b are "brothers" by c and also by d—Prolog was right to produce that solution twice because there was a hidden variable being instantiated to two different values.
A more elegant solution would probably be to use setof/3 to get the solutions. This can work even with your original code:
?- setof(X-Y, (brother(X, Y), X #< Y), Brothers).
Brothers = [a-b].
The downside to this approach is that you wind up with a list rather than Prolog generating different solutions, though you can recover that behavior with member/2.
This should work. But I think it can be improved (I am not a Prolog specialist):
brother(X, Y) :-
son(X, P1),
son(Y, P1),
X #< Y,
(son(X, P2), son(Y, P2), P1 #< P2 -> false; true).
If you're using Strawberry Prolog compiler,you won't get all the answers by typing this:
?- brother(X, Y),
write(X), nl,
write(Y), nl.
In order to get all the answers write this:
?- brother(X, Y),
write(X), nl,
write(Y), nl,
fail.
I hope it helps you.:)
I got to an answer.
% Include the dictionary
:- [p1]. % The dictionary with sons
:- dynamic(found/2).
brother(X, Y) :-
% Get two persons from the database to test
son(X, P),
son(Y, P),
% Test if the two persons are different and were not already used
testBrother(X, Y).
% If it got here it's because there is no one else to test above, so just fail and retract all
brother(_, _) :-
retract(found(_, _)),
fail.
testBrother(X, Y) :-
X \= Y,
\+found(X, Y),
\+found(Y, X),
% If they were not used succed and assert what was found
assert(found(X, Y)).
It always returns fails in the end but it succeeds with the following.
brother(X, Y). % Every brother without repetition
brother('Urraca', X). % Every brother of Urraca without repetition
brother('Urraca', 'Sancho I'). % True, because Urraca and Sancho I have the same father and mother. In fact, even if they only had the same mother or the same father it would return true. A little off context but still valid, if they have three or more common parents it would still work
It fails with the following:
brother(X, X). % False because it's the same person
brother('Nope', X). % False because not is not even in the database
brother('Nope', 'Sancho I'). % False, same reason
So like this I can, for example, ask: brother(X, Y), and start pressing ";" to see every brother and sister without any repetition.
I can also do brother(a, b) and brother(b, a), assuming a and b are persons in the database. This is important because some solutions would use #< to test things and like so brother(b, a) would fail.
So there it is.

prolog, test(X, Y, Z) :- Y is X + Z

How to get Y and Z in prolog, when I only know X?
For example:
test(X, Y, Z) :- X is Y + Z.
but error:
?- test(2, Y, Z).
ERROR: is/2: Arguments are not sufficiently instantiated
It's not possible, because you can choose Y to be anything you want and them compute Z or vice versa.
Although if you know that Y and Z are from some limited set (e.g. positive integers less than 5), you can do something like:
valid_number(1).
valid_number(2).
valid_number(3).
valid_number(4).
test(X, Y, Z) :- valid_number(Y), valid_number(Z), X is Y + Z.
You have to pass them as arguments. Prolog arithmetic (is/2) is not a magic wand, its right argument must be fully instantiated (no variables) before it can be evaluated.
If you want the predicate to work in several "directions", with multiple combinations of ground terms and variables, you'll want to use Constraint Logic Programming, but that's a rather advanced area of logic programming. In CLP on finite domains, you can say
:- use_module(library(clpfd)). % this differs between implementations
test(X,Y,Z) :- X #= Y + Z.

Resources