Different answers when using anonymous variable and "normal" variable in Prolog - prolog

I have the following database:
vegetarian(jose).
vegetarian(james).
vegetable(carrot).
vegetable(egg_plant).
likes(jose,X):-vegetable(X).
loves(Who,egg_plant):-vegetarian(Who).
When I do the query vegetarian(_). I was expecting to get _ = jose; _ = james. but I am instead getting true; true.
If I instead do vegetarian(X)., then I get the expected answer X = jose; X = james.
Why this difference?

If you're using SWI-Prolog, you can control this with the flag toplevel_print_anon). However, a name consisting of a single underscore (_) is special and never prints.
$ swipl dd.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.16)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- set_prolog_flag(toplevel_print_anon, true).
true.
?- vegetarian(_X).
_X = jose ;
_X = james.
?- vegetarian(_).
true ;
true.
?- set_prolog_flag(toplevel_print_anon, false).
true.
?- vegetarian(_X).
true ;
true.

Related

Prolog argument mode indicator causes output false

I am trying to use Prolog's argument mode indicators in the signature of my method (https://www.swi-prolog.org/pldoc/man?section=argmode).
Without the indicators, my function works as expected (eg. palindrome([1,2,1]) gives true):
palindrome(List) :-
reverse(List, List)
But when I say
palindrome(+List) :-
reverse(List, List)
I get false every time. I don't get any errors or warnings. I also tried the following but had no luck:
palindrome(+List) :-
reverse(+List, +List)
So I'm pretty sure I am using these indicators wrong somehow. Can anyone help? I am using SWI-Prolog and the SWISH online IDE.
Yes, wrong. You shouldn't be using them at all, in the code. Only in the comments.
+ is interpreted as a separate token:
6 ?- atom(+X).
false.
7 ?- +X =.. Z.
Z = [+, X].
8 ?- +X = + X.
true.
9 ?- +X = '+'(X).
true.
You could use the +-using predicate definition as you show, but it's rather pointless:
14 ?- [user].
bar(+X,+X).
|:
true.
15 ?- bar( + 1, +Z).
Z = 1.
There are languages that do let us declare the modes, like I think Mercury does. But not Prolog. In Prolog we only use this as comments, to guide our use and understanding of the code.

Why does this rule with `not` always return false?

I have this prolog file:
daughter(anna, vera).
daughter(vera, oleg).
daughter(olga, pavel).
daughter(olga, alla).
daughter(alla, lidia).
man(oleg).
man(victor).
man(pavel).
not(P) :- (call(P) -> fail ; true).
woman(X) :- not(man(X)).
?- woman(X). always returns false. ?- man(X). returns all three male entries though.
I also tried woman(X) :- \+man(X). but certain syntax is not the problem it seems.
If I try to check a certain person it works: ?- woman(anna). returns true.
I'm quite new to prolog and can't even suggest what is wrong here.
UPD. I want all people who are not men to be classified as men. The question is - why can't I do woman(X) and get all non-men?
?- woman(anna).
true.
?- woman(X).
false.
?- man(X).
X = oleg ;
X = victor ;
X = pavel.
UPD2. Solution
The problem was caused by floundering as was pointed out in the comments. I needed woman(X) rule to implement this rule: mothers_names(X, Y) :- not(man(X)), daughter(Y,X).
In a nutshell, inverting the query works: mothers_names(X, Y) :- daughter(Y,X), not(man(X)). because first predicate makes X in not(man(X)) limited to several values.

Predicate retruns false when callled from other predicate

I have this program in Prolog
su([], Counter, Counter).
su([G|O], N, Count) :- Counter is Count + G, su(O,N,Counter).
custom_sum(L,X) :- su(L,X,0).
write_file :-
write('Type list: '),
read(L1),
tell('file.txt'),
write(L1), write(.), nl,
told.
read_file :-
write('Reading from file...'), nl,
see('file.txt'),
read(L),
seen,
write('sum of list elements: '),
custom_sum(L,Sum),
write(Sum), assertz(my_sum(Sum)).
When I try to use custom_sum, everything is fine. Same with write_file. But read_file returns false right after "write('sum of list elements: ')". As if custom_sum was a problem here.
Out of curiosity I ran your code without changing anything. Your code works as I would expect on my system with SWI-Prolog on Windows 10.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.24)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- consult("C:/Users/Groot/Documents/Projects/Prolog/SO_question_180.pl").
true.
?- custom_sum([1,2,3],R).
R = 6.
?- working_directory(Working_directory,'C:/Users/Groot/Documents/Projects/Prolog/SO_question_180/').
Working_directory = 'c:/users/groot/documents/prolog/'.
?- working_directory(D,D).
D = 'c:/users/groot/documents/projects/prolog/so_question_180/'.
?- write_file.
Type list: [1,2,3].
true.
?- read_file.
Reading from file...
sum of list elements: 6
true.
?-
My only guess is that you are entering the list incorrectly at the prompt.
It should be [1,2,3]. You need the [ ] and the ending period . .
Contents of created file.txt
[1,2,3].

Need help Converting to Prolog rule

Everybody likes a job if it's fun and it pays
we=>likes(X, Job):-fun(Job), pay_well(Job).
Not sure if it's correct and if it matters that I put Job as a variable?
Just provide definitions for the other two predicates and you have a working program. Something along these lines:
likes_job(_Person, Job) :-
is_fun(Job),
pays_well(Job).
is_fun('scuba diving instructor').
is_fun('tour guide').
pays_well('software developer').
pays_well('scuba diving instructor').
A couple of examples:
?- likes_job('Peter', Job).
Job = 'scuba diving instructor' ;
false.
?- likes_job('Peter', 'software developer').
false.
?- likes_job('Peter', 'tour guide').
false.
?- likes_job('John', 'scuba diving instructor').
true.
?- likes_job(X, 'scuba diving instructor').
true.
Instead of defining the two predicates, you can just declare them as "dynamic" and they are now empty (instead of absent):
likes_job(_Person, Job) :-
is_fun(Job),
pays_well(Job).
:- dynamic is_fun/1.
:- dynamic pays_well/1.
$ swipl -q
?- [likesjob].
true.
?- likes_job(A, B).
false.
?- assertz(is_fun(x)).
true.
?- assertz(pays_well(x)).
true.
?- likes_job(A, B).
B = x.

Prolog: stop condition?

Here's a very trivial Prolog knowledge base:
spouse(bill,cheryl).
married(X,Y) :- spouse(X,Y).
married(X,Y) :- spouse(Y,X).
I ran the following queries. Note that sometimes the answer is the correct name (only), but other times the answer is the correct name and "false".
1 ?- married(bill,X).
X = cheryl ;
false.
2 ?- married(cheryl,X).
X = bill.
3 ?- married(X,bill).
X = cheryl.
4 ?- married(X,cheryl).
X = bill ;
false.
Can someone explain this seemingly inconsistent behavior? Thanks in advance.
The false response from Prolog means that Prolog had a choice point to go back to in an attempt to find further answers and it found no more. The order in which your predicates and facts are set up can impact whether it thinks it has more choices to explore.
In the given case:
spouse(bill,cheryl).
married(X,Y) :- spouse(X,Y).
married(X,Y) :- spouse(Y,X).
1 ?- married(bill,X).
X = cheryl ;
false.
2 ?- married(cheryl,X).
X = bill.
3 ?- married(X,bill).
X = cheryl.
4 ?- married(X,cheryl).
X = bill ;
false.
In the two false cases, the query married/2 is satisfied by the first of two married/2 clauses. Once satisified, Prolog realizes it has another choice to make (the second married/2 clause), and prompts for you to look for more. You press ;, then Prolog explores the second (and final) clause, finds no more solutions, and comes back false.
Swap the order of your married/2 clauses and see what happens:
spouse(bill,cheryl).
married(X,Y) :- spouse(Y,X).
married(X,Y) :- spouse(X,Y).
?- married(bill,X).
X = cheryl.
?- married(cheryl,X).
X = bill ;
false.
?- married(X,bill).
X = cheryl ;
false.
?- married(X,cheryl).
X = bill.
As expected, the results are reversed since we've changed which queries are satisfied by the first clause.
The false response can appear inconsistent to beginning Prolog programmers and "feel" like an error or warning, but it's actually a perfectly normal Prolog response. Prolog is quite consistent in its behavior of attempting to find solutions and, when no more choices exist, will return false. If Prolog has exhausted all the other choices before it finds the final solution, it displays the solution and doesn't return false (as in the case above in which the second clause is the only solution).
There is a temptation to try and "clean up" the false responses by using cuts. Although this can have the desired short-term result, it is risky since you are removing choice points from the predicate and as you add data and logic may eliminate solutions which you really want.
Thus, in the modified case:
spouse(bill,cheryl).
spouse(emma,nate).
married(X,Y) :- spouse(X,Y), !. % If we found the spouse, we're done, no more!
married(X,Y) :- spouse(Y,X).
?- married(bill,X).
X = cheryl.
?- married(cheryl,X).
X = bill.
?- married(X,bill).
X = cheryl.
?- married(X, cheryl).
X = bill.
Yay, life is good! But wait, what if we do this:
?- married(X,Y).
X = bill,
Y = cheryl.
?-
Are bill and cheryl the only married couple? No... it left out nate and emma. The cut eliminated the rest of the solutions.

Resources