My first prolog tiny program doesn't work - prolog

I've thrown a glance at Prolog, and this is my first little try at writing it: it directly bugs at my first queries. (in the example below i asked for word('test').)
I meant to state:
word/1
alias/2
alias is commutative
if a variable is an alias of another then it is a word
A, C, and D are word
B is an alias of A.
this is the knowledge base when I do a listing. in SWI-Prolog console.
?- listing.
word(A) :-
alias(A, B).
word('A').
word('C').
word('D').
alias(A, B) :-
alias(B, A).
alias('A', 'B').
Yes
3 ?- word('test').
ERROR: Out of local stack
Exception: (59,743) alias(_L147, test) ?
Any clue what's going wrong with my knwoledge base?

alias(A, B) :-
alias(B, A).
alias('A', 'B').
this is an infinite loop.
for example, if you call alias(2,4) then the trace would be something like:
alias(2,4)
alias(4,2)
alias(2,4)
alias(4,2)
alias(2,4)
alias(4,2)
alias(2,4)
alias(4,2)
....

you have a left recursively issue.
Look here at page 133: http://books.google.com/books?id=w-XjuvpOrjMC&pg=PA132&lpg=PA132&dq=prolog+commutativity&source=bl&ots=4Xz2WMBUNt&sig=pj1blZvMvHXoQPBHEPBNnVo2f6E&hl=en&sa=X&ei=xpAcT_ieMIj00gGdhq3oCw&ved=0CCAQ6AEwAA#v=onepage&q=prolog%20commutativity&f=false

Related

Prolog Can't get to the predicate

The prolog is supposed to find the order of five statements statements. Everything is working fine but when i call the query solution([A, B, C, D, E]) I get a sandbox error like this:
The Error:
Sandbox restriction!
Could not derive which predicate may be called from
call(C)
all(schoolgirl,[A,B,C,D,E])
solution([A,B,C,D,E])
Full Prolog Program:
all(_,[]).
all(Pred, [X|Xs]):-
P =..[Pred,X],
call(P),
all(Pred,Xs).
distinct([]).
distinct([X|Xs]):-
not(member(X, Xs)), distinct(Xs).
x0r(A, B):-
A, not(B).
x0r(A, B):-
not(A), B.
schoolgirl(betty).
schoolgirl(ethel).
schoolgirl(joan).
schoolgirl(kitty).
schoolgirl(mary).
betty(Snd,Trd):-
x0r(Snd=kitty, Trd=betty).
ethel(Fst, Snd):-
x0r(Fst=ethel, Snd=joan).
joan(Trd, Fith):-
x0r(Trd=joan, Fith=ethel).
kitty(Snd, Forth):-
x0r(Snd=kitty, Forth=mary).
mary(Forth, Fst):-
x0r(Forth=mary, Fst-betty).
solution([Fst, Snd, Trd, Forth, Fith]):-
all(schoolgirl, [Fst,Snd, Trd, Forth, Fith]),
distinct([Fst, Snd, Trd, Forth, Fith]),
betty(Snd, Trd),
ethel(Fst, Snd),
joan(Trd, Fith),
kitty(Snd, Forth),
mary(Forth, Fst).
The Call is
solution([A, B, C, D, E])
Like a commenter said, this seems to be a particular restriction when using SWI-Prolog's browser-based SWISH system (https://swish.swi-prolog.org/). It doesn't want you to use call/1 with terms that it doesn't know enough about.
Fortunately, you can give it a bit more information: You use call/1 in the following context:
P =..[Pred,X],
call(P),
That is, for a call to a predicate Pred with exactly one argument X. There is a more direct syntax for this:
call(Pred, X)
and this is enough to make the error go away, and to make SWISH willing to run your program. (In fact this syntax is a bit more general because it takes exactly one additional argument to be added to the ones already in Pred, so call(f(a), b) would call the goal f(a, b).)
Your query will now die with a somewhat obscure error:
procedure `A-B' does not exist
Reachable from:
call(_1690-betty)
not(A-betty)
x0r(A=mary,B-betty)
mary(A,B)
solution(A)
You have a typo in the definition of your mary/2 predicate.

Want PROLOG to require == matches on already matched variables, = for others

I have this code:
contradicts(at(X,_),location(X)).
mustContradict(A, B) :- contradicts(A, B).
contradicts/2 is meant to say: if X is at somewhere, then X can't itself be a location.
mustContradict/2 is meant to say: succeed if A and B are contradictory.
When I run it to detect that if you're at a location, you can't be a location (mustContradict(at(Thing,Location),location(Thing)).) -- it succeeds, as it should. On this one, however:
mustContradict(at(Thing,Location),location(Location)).
it also succeeds, with variable assignment Thing=Location.
I could probably mangle a way to make it ensure all variables are identical when trying to match, something like:
A=at(Thing,Location),B=location(Location),
contradicts(A,AsContradiction),
B==AsContradiction.
but then it would fail on the first test, trying to verify that a Thing that is "at" something can't be a location.
What I think I want is to be able to distinguish variables that are already assigned to other variables from those that are so far not matched to anything.
You shouldn't check your predicates with variables (starting with uppercase), but with terms (starting with lowercase).
mustContradict(at(thing,location),location(location)). fails as it should since thing doesn't equal location. In your example you use variables that can be assigned anything.
I have no idea why you are reassigning mustContradict(A, B) :- contradicts(A, B).
you can change contradicts definition to contradicts(at(X,Y),location(X)) :- not(X == Y).
I wonder if this is not what you wanted to achieve:
at(thing, location).
at(thingLocation, location).
location(location).
location(thingLocation).
contradicts(X) :- at(X,_), location(X).
now contradicts(X). succeeds with X = thingLocation
Looks like the solution was to convert all variables to atoms. Only converting those that were shared or repeated meant that the others could bind to inappropriate things (as in the first example), so the behavior didn't change.
Binding them to atoms, as in this code:
contradicts(at(X,_),location(X)).
mustContradict(X,Y) :-
replaceVariablesWithAtoms(X,NewX), replaceVariablesWithAtoms(Y,NewY),
contradicts(NewX,NewY).
replaceVariablesWithAtoms(Term,NewTerm) :-
Term =.. TermAsList,
terms_to_atoms(TermAsList,NewTermAsList),
NewTerm =..NewTermAsList.
terms_to_atoms([],[]).
terms_to_atoms([H|T],[NewH|NewT]) :-
terms_to_atoms(T,NewT),
term_to_atom(H,NewH).
gives the right answer to both queries.
?- mustContradict(at(Thing,Location),location(Thing)).
true.
?- mustContradict(at(Thing,Location),location(Location)).
false.
Wouldn't it be easier just to write it as you have defined the problem statement:
I have this code:
contradicts(at(X,_),location(X)).
mustContradict(A, B) :- contradicts(A, B).
contradicts/2 is meant to say: "if X is at somewhere, then X can't itself be a location."
mustContradict/2 is meant to say: "succeed if A and B are contradictory."
Assuming that your at/2 and location/1 are facts/predicates in your prolog program . . . why not something like this:
contradiction(X) :- at(X,_), location(X) .

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 print statement does not work as expected

I was trying to define a functor and print each individual items of list in Prolog, but Prolog is not printing in correct format.
rint(L):-
write(H).
the output is like
rint([a, s,v ,c]).
_L139
true.
This is what I expect to achieve by calling the functor, any help or thought is appreciated, I'm new to Prolog and learning it.
?- rint([a,b,c,d]).
.(a, .(b, .(c, .(d, []))))
I think it should be
rint(L) :- write(L).
Also if you want .(a, .(b, .(c, .(d, [])))) and not [a, b, c, d] in output, use display:
rint(L) :- display(L).
The problem is an error in your rule for rint.
Your definition says that rint(L) succeeds if write(H) succeeds. At that point, the interpreter knows nothing about H. So it writes a value it doesn't know, which is why you see the _L139, the internal representation of an uninitialised variable.
Having done that, write(H) has succeed, is true, so rint(L) is true. The interpreter tells you that: true.
To define your own rint/1 without relying on built-ins such as display/1, you would need to do something like
rint([]) :-
write([]).
rint([H|T]) :-
write('.('),
write(H),
write(', '),
rint(T),
write(')').
If you're trying to display an empty list, just write it. If you're trying to display any other list, write the opening period and parenthesis, write the Head, write the following comma and space, then call itself for the Tail of the list, then write the closing parenthesis.

Resources