Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:33:24+00:00 2026-06-11T12:33:24+00:00

I have successfully connected PHP with Prolog and managed to query the desired knowledge

  • 0

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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:33:25+00:00Added an answer on June 11, 2026 at 12:33 pm

    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

    "2.1.1.1 Starting SWI-Prolog on Unix

    By default, SWI-Prolog is installed as ‘swipl’. The command-line arguments of SWI-Prolog itself and its utility programs are documented using standard Unix man pages."

    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,

    C:>swipl -q -f knowledge_database.pl -g hangs_out_with(erin,kosta),halt.
    1 ?-halt. (the halt is inputted by me.)
    

    halt isn’t executed, precisely because hangs_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:

    report_hangs_out_with(A,B):-
      hangs_out_with(A,B)- > writeln(['YES',A,B]) ; writeln('NO').
    

    and use it instead:

    C:>swipl -q -f knowledge_database.pl -g report_hangs_out_with(erin,kosta),halt.
    

    Also, Prolog echoing "true" or "false" is part of its interactive session. But you terminate it with halt!


    edit: you posted:

    1 ?-hangs_out_with(erin,kosta).
    false.
    

    So, when you run that query in interactive Prolog shell, it reports the failure. halt/0 exits 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:

    C:\Program Files\pl\bin>plcon -q -g (writeln('****'),halt).
    ****                          // didn't say Yes, right??
    
    C:\Program Files\pl\bin>plcon -q
    1 ?- writeln('****'),halt.
    ****                          // didn't say Yes here either
    
    C:\Program Files\pl\bin>plcon -q
    1 ?- writeln('****').
    ****
    
    Yes                           // does say Yes as part of interaction
    2 ?- halt.
    
    C:\Program Files\pl\bin>      
    

    So that’s the way it is. Deal with it. 🙂 Or write Jan and ask him to change it. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully connected to my FTP using PHP and I can see all
I have successfully connected my Oracle 11g XEdatabase with java in Netbeans 7.1. Class.forName(oracle.jdbc.OracleDriver);
I have connected with the database successfully by using wizard in server explorer. but
I have successfully created a feature in sharepoint that modifies the existing edit dialog
I have Xdebug 2.1 installed, and running with PHP 5.2.13. It can successfully connect
Using Socket.IO's WebSocket I have successfully managed a chat application from my home computer
I have a PHP script that imports and parses XML files and saves the
I have a simple php script on a server that's using fsockopen to connect
I have successful connected to the login page, however, i'm not sure how to
Have successfully gotten the csrf middleware working in express as per previous SO questions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.