getting out of local stack at prolog - prolog

p(0,0).
p(0,1).
p(0,2).
p(0,3).
p(0,4).
p(1,1).
p(1,2).
p(1,3).
p(1,4).
p(1,0).
p(2,0).
p(2,1).
p(2,2).
p(2,3).
p(2,4).
p(3,0).
p(3,1).
p(3,2).
p(3,3).
p(3,4).
p(4,0).
p(4,1).
p(4,2).
p(4,3).
p(4,4).
adjacent(p(X,Y),p(X,Z)) :-
p(X,Y),
p(X,Z),
Z is Y+1.
adjacent(p(X,Y),p(X,Z)) :-
p(X,Y),
p(X,Z),
Z is Y-1.
adjacent(p(X,Y),p(Z,Y)) :-
p(X,Y),
p(X,Z),
Z is X+1.
adjacent(p(X,Y),p(Z,Y)) :-
p(X,Y),
p(X,Z),
Z is X-1.
adjacentC(X,Y) :-
adjacent(X,Y).
adjacentC(X,Y) :-
adjacent(X,Z),
adjacentC(Z,Y).
I don't know why this code I wrote isn't working.
e.g.:
?- adjacentC((0,0),(4,4)). ERROR

Quick answer: The following works and terminates always using closure/3 defined elsewhere.
adjacentD(X,Y) :-
closure(adjacent,X,Y).
However, this approach is extremely slow, due to the inefficient definition of adjacent/3. Here is a better one / oh forget it, here is a more correct one, first:
adjacent2(p(X0,Y0),p(X,Y)) :-
p(X0,Y0),
( X0 = X,
p(X,Y),
abs(Y0-Y) =:= 1
; Y0 = Y,
p(X,Y),
abs(X0-X) =:= 1
).

Related

Not expected building results

I have a little question about my code : (it can be interpreted as a grid for example)
free(1,1).
free(1,3).
free(2,3).
free(2,1).
free(1,5).
free(5,6).
free(5,2).
free(5,4).
busy(5,1,white,pion).
busy(1,2,black,pion).
busy(1,4,black,pion).
busy(1,6,black,pion).
busy(5,3,white,pion).
busy(5,5,white,pion).
%all clauses for move/6
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2=X-1, Y2=Y-1.
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2=X-1, Y2=Y+1.
move(X,Y, X2,Y2,black,pion) :-
busy(X,Y,black,pion), free(X2,Y2), X2=X+1, Y2=Y-1.
move(X,Y, X2,Y2,black,pion) :-
busy(X,Y,black,pion), free(X2,Y2), X2=X+1, Y2=Y+1.
When I execute this :
move(1,2,X,Y,black,pion).
in SWI-Prolog it says : false. whereas it should says true and return two statements :
X2 =2 , Y2= 1
X2=2, Y2=3
I do not understand why it doesn't work, could you please help me ?
It is simply because you use = ("make sure that left-hand-side and right hand side unify") instead of is/2 (evaluate the arithmetic expression on the right-hand side and unify with the left-hand-side) or even better #= (constrain the arithmetic values to be the same, even if not all the variables can be resolved yet), here:
X2=X-1, Y2=Y-1.
This unifies X2 with the actual structure -(X,1). Which will fail, as X2 will be a value.
Use:
:- use_module(library(clpfd)). % for #= predicate
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), free(X2,Y2), X2 #= X-1, Y2 #= Y-1.
Also, move the test forwards:
move(X,Y, X2,Y2,white,pion) :-
busy(X,Y,white,pion), X2 #= X-1, Y2 #= Y-1, free(X2,Y2).

Prolog predecessor math

I have an add2 predicate which resolves like this where s(0) is the successor of 0 i.e 1
?- add2(s(0)+s(s(0)), s(s(0)), Z).
Z = s(s(s(s(s(0)))))
?- add2(0, s(0)+s(s(0)), Z).
Z = s(s(s(0)))
?- add2(s(s(0)), s(0)+s(s(0)), Z).
Z = s(s(s(s(s(0)))))
etc..
I'm trying to do add in a predecessor predicate which will work like so
?- add2(p(s(0)), s(s(0)), Z).
Z = s(s(0))
?- add2(0, s(p(0)), Z).
Z = 0
?- add2(p(0)+s(s(0)),s(s(0)),Z).
Z = s(s(s(0)))
?- add2(p(0), p(0)+s(p(0)), Z).
Z = p(p(0))
I can't seem to find a way to do this. My code is below.
numeral(0).
numeral(s(X)) :- numeral(X).
numeral(X+Y) :- numeral(X), numeral(Y).
numeral(p(X)) :- numeral(X).
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
add(p(X),Y,p(Z)) :- add(X,Y,Z).
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(p(X),p(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
add2(A,B,C) :-
resolve(A,RA),
resolve(B,RB),
add(RA,RB,C).
In general, adding with successor arithmetic means handling successor terms, which have the shape 0 or s(X) where X is also a successor term. This is addressed completely by this part of your code:
add(0,X,X).
add(s(X),Y,s(Z)) :- add(X,Y,Z).
Now you have to make a decision; you can either handle the predecessors and the addition terms here, in add/3, or you can wrap this predicate in another one that will handle them. You appear to have chosen to wrap add/3 with add2/3. In that case, you will definitely need to create a reducing term, such as you've built here with resolve/2, and I agree with your implementation of part of it:
resolve(0,0).
resolve(s(X),s(Y)) :-
resolve(X,Y).
resolve(X+Y,Z) :-
resolve(X,RX),
resolve(Y,RY),
add(RX,RY,Z).
This is all good. What you're missing now is a way to handle p(X) terms. The right way to do this is to notice that you already have a way of deducting by one, by using add/3 with s(0):
resolve(p(X), R) :-
resolve(X, X1),
add(s(0), R, X1).
In other words, instead of computing X using X = Y - 1, we are computing X using X + 1 = Y.
Provided your inputs are never negative, your add2/3 predicate will now work.

Get unique results with Prolog

I have this Prolog code that returns: [[vincent,vincent],[vincent,marcellus],[marcellus,vincent],[marcellus,marcellus],[pumpkin,pumpkin],[honey_bunny,honey_bunny]].
:- initialization main.
loves(vincent, mia).
loves(marcellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
jealous(X, Y) :-
loves(X, Z),
loves(Y, Z).
main :-
findall([X, Y], jealous(X, Y), L),
write(L),
halt.
How to get the only results when X != Y?
I tried the following code to get the same results as before.
jealous(X, Y) :-
X \== Y,
loves(X, Z),
loves(Y, Z).
With \=, I got [].
How to get only [vincent,marcellus] as a result?
The order of the goals in your attempted solution is wrong. When called with two distinct variables, the (\==)/2 standard predicate always succeed. The solution is to call the predicate only when its arguments are instantiated:
jealous(X, Y) :-
loves(X, Z),
loves(Y, Z),
X \== Y.
With this fix, your query now returns:
?- findall([X, Y], jealous(X, Y), L).
L = [[vincent, marcellus], [marcellus, vincent]].
So, no one is jealous of himself anymore. But you still get a redundant solution. We can modify the jealous/2 predicate to sort the names in the returned solutions. For example:
jealous(X, Y) :-
loves(X0, Z),
loves(Y0, Z),
X0 \== Y0,
( X0 #< Y0 ->
X = X0, Y = Y0
; X = Y0, Y = X0
).
Now, by using setof/3 instead of findall/3, we get:
?- setof([X, Y], jealous(X, Y), L).
L = [[marcellus, vincent]].
One final observation. A list is a poor solution for representing a pair. The traditional way is to use either X-Y or (X, Y).
Whenever possible, use dif/2 instead of (\==)/2.
dif/2 will help you write logically sound programs.
For details, look at prolog-dif!

Prolog: Unification or Backtracking errors in program

I have a simple knowledge base that encodes a family tree. Some important rules in this representation are as follows:
% fathers
father(michael,cathy).
father(michael,sharon).
father(charles_gordon,michael).
father(charles_gordon,julie).
father(charles,charles_gordon).
father(jim,melody).
father(jim,crystal).
father(elmo,jim).
father(greg,stephanie).
father(greg,danielle).
% mothers
mother(melody,cathy).
mother(melody,sharon).
mother(hazel,michael).
mother(hazel,julie).
mother(eleanor,melody).
mother(eleanor,crystal).
mother(crystal,stephanie).
mother(crystal,danielle).
% parents
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
% men
male(michael).
male(charles_gordon).
male(charles).
male(jim).
male(elmo).
male(greg).
% women
female(cathy).
female(sharon).
female(julie).
female(hazel).
female(eleanor).
female(melody).
female(crystal).
female(stephanie).
female(danielle).
person(X) :- male(X) ; female(X).
parent(X,Y) :- father(X,Y) ; mother(X,Y). % X is parent of Y
child(X,Y) :- parent(Y,X).
elder(X,Y) :- parent(X,Y). % X is an elder of Y, meaning X is a parent or an ancestor of Y
elder(X,Y) :- parent(X,Z), elder(Z,Y).
junior(X,Y) :- child(X,Y). % X is a junior of Y, meaning X is a child or some descendant of Y
junior(X,Y) :- child(X,Z), junior(Z,Y).
I am attempting to find the nearest elder between two individuals(predicate ne(X,Y,Z)). This individual Z is the elder of both X and Y, and no junior of Z is also an elder of BOTH X and Y.
My attempt looks like this:
ne(X,Y,Z) :- person(X),
person(Y),
X \= Y,
elder(Z,X),
elder(Z,Y),
junior(A,Z),
not(elder(A,X)),
not(elder(A,Y)).
but this is somehow incorrect, because whenever I run ?- ne(stephanie,cathy,Z). i get
Z = jim ;
Z = jim ;
Z = jim ;
Z = jim ;
Z = elmo ;
Z = elmo ;
Z = elmo ;
Z = elmo ;
Z = eleanor ;
Z = eleanor ;
Z = eleanor ;
Z = eleanor ;
but i'm only supposed to get one answer, and I can't figure out what's wrong. Thanks!
from this graph
seems this answer is correct
?- ne(stephanie,cathy,A).
A = eleanor ;
A = jim.
here is my attempt to ne/3
ne(X,Y,Z) :-
setof(A, (
elder(A, X),
elder(A, Y),
X \= Y,
\+ (elder(A, T), elder(T, X) , elder(T, Y) ))
, As), member(Z, As).
not sure it's the better way...
Setof/3 (joined with member/2) is used to eliminate duplicate answers, since we get
?- aggregate(count,A^ne(stephanie,cathy,A),N).
N = 32.
with this core logic
ne(X,Y,A) :-
elder(A, X),
elder(A, Y),
X \= Y,
\+ (elder(A, T), elder(T, X) , elder(T, Y)).
note variable A replaces locally the original Z
edit
I didn't took in account the savvy comment by #Boris, but after removing the duplicate parent/2 definition, the setof/3+member/2 trick become useless.

Euclidean recursive algorithm

Ok, I know it's really a stupid question, but I can't get it.
There is a task where I should find a recursive algorithm of
Euclid (gcd). I've done it for one case, here:
nondeterm nod (integer,integer,integer)
CLAUSES
nod (X,0,X):- !.
nod (0,X,X):- !.
nod (X,0,X):-X>0.
nod (X,Y,G):-Y>0, Z = X mod Y, nod (Y,Z,G).
I need to do another case, where recursion is beginnig from х0, when Xi then calling for function counting Xi+1.
It should be sort of it:
PREDICATES
nondeterm nod (integer,integer,integer)
nondeterm nod1 (integer,integer,integer,integer,integer)
CLAUSES
nod(X,Y,Z):- nod1(X,Y,Z,0,0).
nod1 (X,Y,Z,X,Y):- Otvet = Z, write("Otvet=", Otvet, "\n"), !.
nod1 (X,Y,X,Y):- nod1 (X,Y,X,Y).
nod1 (X,Y,Z,X1,Y1):-
X1>Y1, X>0, Y>0,
Y2 = X1 mod Y1,
X2 = Y1,
nod1(X,Y,Z,X2,Y2).
But it doesn't work. Please, help me with that.
The following code works for me. Please note the use of
rem, but I guess you could also use mod:
% sys_gcd(+Integer, +Integer, -Integer)
sys_gcd(X, 0, X) :- !.
sys_gcd(X, Y, Z) :-
H is X rem Y,
sys_gcd(Y, H, Z).
Here are some example runs with SWI-Prolog:
?- sys_gcd(20,30,X).
X = 10.
?- sys_gcd(-20,30,X).
X = 10.
?- sys_gcd(20,-30,X).
X = -10.
?- sys_gcd(-20,-30,X).
X = -10.
If you want a particular sign of the result, you
need additional code around it.
Bye

Resources