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!!
so you "connect PHP to Prolog" by executing a Prolog query in command shell and capturing and analyzing its output. http://www.swi-prolog.org/man/quickstart.html says
So do consult a man page about "-q" switch. Is it the "-q" for quiet perhaps? What does "-f" mean (ok, that’s probably "file")? But the solution is the same – just use a different name for the new predicate.
Notice that in your first attempt,
haltisn’t executed, precisely becausehangs_out_with(erin,kosta)has failed. A comma signifies conjunction (an "and").All you need to do is create a new predicate that reports whether the goal is true or false, and succeeds always:
and use it instead:
Also, Prolog echoing "true" or "false" is part of its interactive session. But you terminate it with
halt!edit: you posted:
So, when you run that query in interactive Prolog shell, it reports the failure.
halt/0exits the Prolog shell. When you run it with a goal specified through command line switch, apparently it does not report the success of failure. That’s the fact of nature as far as we users are concerned (a.o.t. the compiler writers). It is easily addressable with what I’ve shown you. And you yourself say that it works, too. For each predicate that can fail or succeed, define another, reporting predicate, just as I’ve shown you.Here’s a sample transcript (in Windows, but that’s irrelevant). That should clear up your doubts:
So that’s the way it is. Deal with it. 🙂 Or write Jan and ask him to change it. 🙂