I have successfully connected PHP with Prolog and managed to query the desired knowledge base that resides in knowledge_base.pl
file and managed to echo the results via php exec
function.
I encountered a problem in echoing the true
/false
value that Prolog returns after each query execution (see previous question) so I came up with a solution that I have trouble implementing.
Let's assume I have a simple knowledge_base.pl
file with these facts and rules:
girl(erin).
boy(john).
likes(erin, reading).
likes(john, reading).
hangs_out_with(erin, X) :-
likes(X, reading),
boy(X),
writeln('Someone s got a new friend!!').
Lets say that I want to see if erin
is a girl and if so, write that it is true, or else write that it is false. I added this to my knowledge_base.pl
file:
girl(erin) :-
girl(erin)
-> write('it is true')
; write('it is not true').
When I enter the query: ?- girl(erin).
I get an out of local stack
error. I searched the Web and found out that this is due to infinite recursion.
Can someone give me a hint in how to write
girl(X) :-
( girl(X)
-> write('it is true')
; write('it is not true')).
in Prolog? Thanks in advance.
As a new user I'm not allowed to post pictures.
SWI-Prolog's output:
1 ?-hangs_out_with(erin,kosta).
false.
2 ?-hangs_out_with(erin,john).
Someone s got a new friend!!
true.
Command prompt's output:
C:\(directory)>swipl -q -f knowledge_database.pl -g hangs_out_with(erin,kosta),halt.
1 ?-halt. (the halt is inputted by me.)
C:\(directory)>swipl -q -f knowledge_database.pl -g hangs_out_with(erin,john),halt.
Someone s got a new friend!!
The first query fails and the second succeds. As you can see, prolog after the query executions outputs true/false but when i execute the same query in command prompt the true/false values do not get echoed!!
likes(erin, reading). likes(john, erin).
:) just kidding. :) ... joel76 is right - you can't use the same name for the new predicate. This is what causes the runaway recursion and henceout of local stack
error. You already have a predicate namedgirl
, it defines a factgirl(erin)
in your database. Now you define new predicate,report_whether_it_is_a_girl(X):- girl(X) -> write('IS A GIRL') ; write('IS NOT')
. Just using a different name for a different predicate is enough, and it makes sense to. One succeeds or fails ; the other writes a message. – Will Ness