Prolog no operand Predicates - prolog

I am examining a Prolog example as a tutorial. In this example I have a predicate with Zero operand like this:
print_all_solutions :-
findall(_,print_solution,_).
how can I call print_all_solution in the console?
When I ask this in the Prolog console I get nothing:
?- print_all_solutions
no answer, also :
?- print_all_solutions.
no answer.
While i get correct answers to other questions like:
?- goal(state([],right,[a,b,c,d],12)).
true.
?- goal(state([],right,[a,b,c,d],19)).
false.
How should I ask a question about predicates with no operand ( /0 ), to see the solution in the console?

I guess your problem is that either init(State) or, more probably, solve(State,Solution,EndState) don't work.
You need to debug: enter these commands after you consulted the source file
?- leash(-all),trace.
?- print_solution.
and you'll get some clue from the Prolog engine

I simply typed this in the console:
findall(_,print_all_solutions,_).
and i get the answer.

Related

Prolog singleton variables in rule head causes program to output booleans for all queries

I have this prolog program.
red(rose).
red(anthurium).
white(rose).
white(gardenia).
white(jasmine).
like(Y,X) :-
red(X),!,
fail
;
white(X).
And below is how it responds to different queries.
?- like(rose,gardenia).
true.
?- like(rose,P).
false.
?- like(Val,anthurium).
false.
?- like(rose,X).
false
The problem I now have is this:
When querying with a variable within the query (Eg: ?- like(rose,X).), Prolog usually responds by returning a value, (something like X=some_val). Why I don't get any value for those variables, but either true or false?
All helpful answers are highly appreciated. Thanks in advance.
Think about what Prolog is doing here:
like(rose,P) succeeds if red(P), so it grabs a possible substitution for P, namely rose or anthurium. Then it traverses the cut and then it fails. But "failing" means that the proof search down that path didn't bring any solution, there are no successful bindings to report (the only fail to get information out of a failing branch is to side-effect to a log file and read check it later). In fact, all bindings will be undone on backtracking. The second branch is white(X), but rose is not white, so we fail here, too.
You can also write:
like(_,X) :- \+ red(X).
like(_,X) :- white(X).
which is a bit more readable. One notices that when calling like(_,X), the goal enclosed by the negation-as-failure operator \+ is nonground. This is bad, and causes a floundering query (in other words, don't do that). I have written this little page on "floundering".

When does Prolog prompts 'yes' and when does it says 'true'

I wrote the following knowledge base in Prolog:
likes(yamini,chocolate).
likes(anuj,apple).
likes(yamini,book).
likes(john,book).
likes(john,france).
Now, when I consult the above file and try the following commands:
| ?- likes(anuj,apple).
(1 ms) yes
| ?- likes(yamini,chocolate).
true ? ;
no
I want to understand when does Prolog replies a 'yes' versus when does it replies 'true'.
That output depends on the toplevel you are using. It seems you are using GProlog. It will output yes when the query succeeds and there are no choice points left (no other possible solutions).
And it will answer true when the query succeeds but there are still other possible solutions to be checked.
This is an artefact of the toplevel (Prolog command line) implementation. Apparently you implementation says true when it could prove a goal and it is unsure whether there may be more solutions. If it is sure that there is only one solution, it says yes.
Here is what SWI-Prolog does:
?- likes(anuj,apple).
true.
Prolog could successfully prove the goal likes(anuj,apple) and it is also sure there are no other ways to prove it otherwise one would see something like this:
?- member(X,[1,2]).
X = 1 ;
X = 2.
where an X that makes the goal true has been found as 1 but there may be other solutions. And indeed there are, namely 2.
Back to our example:
?- likes(yamini,chocolate).
true.

turning off Redefined static procedure in prolog

anyone of you could tell me how to turn off "Redefined static procedure" warnings?
I red online documentation of swi-prolog and i found this predicate no_style_check(ultimate) that in principle should turn off these warnings, but when i execute this predicate
main:-
no_style_check(singleton),
no_style_check(discontiguous),
no_style_check(multiple),
require,
test_all.
i received this error
ERROR: Domain error: style_name' expected, foundmultiple'
Anyone knows an alternative way to do this or could tell me why i receive this error ?
Thanks in advance!
Prolog is a pretty loosey-goosey language, so by default it warns you when you do certain things that are not wrong per se, but tend to be a good indication that you've made a typo.
Now, suppose you write something like this:
myfoo(3, 3).
myfoo(N, M) :- M is N*4+1.
Then from the prompt you write this:
?- asserta(myfoo(7,9)).
ERROR: asserta/1: No permission to modify static procedure `myfoo/2'
ERROR: Defined at user://1:9
What's happening here is that you haven't told Prolog that it's OK for you to modify myfoo/2 so it is stopping you. The trick is to add a declaration:
:- dynamic myfoo/2.
myfoo(3, 3).
myfoo(N, M) :- M is N*4+1.
Now it will let you modify it just fine:
?- asserta(myfoo(7,9)).
true.
Now suppose you have three modules and they each advertise themselves by defining some predicate. For instance, you might have three files.
foo.pl
can_haz(foo).
bar.pl
can_haz(bar).
When you load them both you're going to get a warning:
?- [foo].
true.
?- [bar].
Warning: /home/fox/HOME/Projects/bar.pl:1:
Redefined static procedure can_haz/1
Previously defined at /home/fox/HOME/Projects/foo.pl:1
true.
And notice this:
?- can_haz(X).
X = bar.
The foo solution is gone.
The trick here is to tell Prolog that clauses of this predicate may be defined in different files. The trick is multifile:
foo.pl
:- multifile can_haz/1.
can_haz(foo).
bar.pl
:- multifile can_haz/1.
can_haz(bar).
In use:
?- [foo].
true.
?- [bar].
true.
?- can_haz(X).
X = foo ;
X = bar.
:- discontiguous does the same thing as multifile except in a single file; so you define clauses of the same predicate in different places in one file.
Again, singleton warnings are a completely different beast and I would absolutely not modify the warnings on them, they're too useful in debugging.

Prolog "Out of local stack" Error

Here is my simple Prolog program:
friend(X,Y):-
knows(X,Y).
friend(X,Z):-
friend(X,Y),
friend(Y,Z).
knows(brian,tom).
knows(tom,peter).
If I type the following query
friend(brian,peter).
Prolog will give the following output:
?- friend(brian,peter).
true
If a further type a semicolon, Prolog will say:
ERROR: Out of local stack
What am I doing wrong here?
The error is in the second clause. It should be instead:
friend(X,Z):-
knows(X,Y),
friend(Y,Z).
Otherwise, when you ask Prolog for more solutions, you end up having the friend/2predicate recursively calling itself without first establishing a knows/2intermediate relation. You can learn more about the bug in your program by tracing the calls to the friend/2 predicate. Try:
?- trace, friend(brian,peter).
The understand the source of non-termination in your program it suffices to look at the following failure-slice:
friend(X,Y):- false,
knows(X,Y).
friend(X,Z):-
friend(X,Y), false,
friend(Y,Z).
knows(brian,tom) :- false.
knows(tom,peter) :- false.
It is because of friend(X, Z) :- friend(X, Y), ... that your program will not terminate. It will produce answers, here and there, but ultimately it will loop. For more, see failure-slice.

Prolog error: Predicate Protected

I'm new to prolog and I'm trying to write a predicate that returns "Yes" if two train stations are on the same line.
line(1,[a,b,c,d,e]);
line(2,[b,g,f,e,i,h]);
line(3,[l,m,g,n,f,o,p,q,i,j]);
same_line(X,Y):-
line(_,L),
member(X,L),
member(Y,L).
Example:
?- same_line(n,j).
Yes
However, I get this error in WIN-PROLOG when compiling: ! Error 67 : Predicate Protected
What am I doing wrong?
Just answering, so that the question goes off the list of unanswered
questions: Yes the (;)/2 is a problem. Further if you really only
want a yes or no, you could also try memberchk/2 instead of member/2.
The code would read:
line(1,[a,b,c,d,e]).
line(2,[b,g,f,e,i,h]).
line(3,[l,m,g,n,f,o,p,q,i,j]).
same_linechk(X,Y):-
line(_,L),
memberchk(X,L),
memberchk(Y,L).
And works as expected:
?- same_linechk(n,j).
true.
The predicate memberchk/2 is part of SWI-Prolog and does not
backtrack after the first time it has found a matching member.
It is essentially a menber/2 with a builtin cut and thus in
the average twice as fast.
Bye

Resources