Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

singleton variables in prolog query

Tags:

prolog

I am new to Prolog and running into a problem. When I ask the query ?-grandfather(daniel,george) on the data below, I get the wrong answer. I don't understand why my query doesn't work. I also received a warning message stating that [C,D] are singleton variables. This warning no longer occurs. Again, I'm unsure why.

mother(jules,mary).
mother(jules,martha).
mother(jules,andy).
mother(mary,annie).
mother(mary,george).
mother(martha,jesse).
mother(martha,june).
mother(june,camile).
father(daniel,mary).
father(daniel,martha).
father(daniel,andy).
father(adrian,annie).
father(adrian,george).
father(carlo,camile).

brothers(A,B):-father(C,A),father(C,B);mother(D,A),mother(D,B).

grandfather(E,W):-father(E,father(C,W));father(E,mother(D,W)).
like image 308
user3105533 Avatar asked May 05 '26 08:05

user3105533


1 Answers

You can write your grandfather query more simply:

grandfather(E,W):-father(E,A),father(A,W).
grandfather(E,W):-father(E,A),mother(A,W).

or

grandfather(E,W):-father(E,A),father(A,W);father(E,A),mother(A,W).

and now:

6 ?- grandfather(daniel,george).
true .
7 ?- grandfather(daniel,annie).
true .

Here, we ask Prolog to find the grandson of E where: E is the father of A and A is the father/mother of W.

Or more specifically, to check if daniel has a grandson named george. Prolog checks: daniel has a daughter mary and mary has a son george. So it would return true. If you trace it, you see:

[trace] 3 ?- grandfather(daniel,george).
   Call: (6) grandfather(daniel, george) ? creep
   Call: (7) father(daniel, _G1931) ? creep
   Exit: (7) father(daniel, mary) ? creep
   Call: (7) father(mary, george) ? creep
   Fail: (7) father(mary, george) ? creep
   Redo: (7) father(daniel, _G1931) ? creep
   Exit: (7) father(daniel, martha) ? creep
   Call: (7) father(martha, george) ? creep
   Fail: (7) father(martha, george) ? creep
   Redo: (7) father(daniel, _G1931) ? creep
   Exit: (7) father(daniel, andy) ? creep
   Call: (7) father(andy, george) ? creep
   Fail: (7) father(andy, george) ? creep
   Redo: (6) grandfather(daniel, george) ? creep
   Call: (7) father(daniel, _G1931) ? creep
   Exit: (7) father(daniel, mary) ? creep
   Call: (7) mother(mary, george) ? creep
   Exit: (7) mother(mary, george) ? creep
   Exit: (6) grandfather(daniel, george) ? creep
true .

[C,D] are singleton variables indicates that there is one or more variable in the clause that appears only once. This isn't affecting the program, you can still run your queries.

Singleton Variables on SWI Documentation

like image 73
Shevliaskovic Avatar answered May 11 '26 15:05

Shevliaskovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!