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 3979128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:07:55+00:00 2026-05-20T05:07:55+00:00

I am new in Prolog and I was just thinking that why this rule

  • 0

I am new in Prolog and I was just thinking that why this rule giving me false result after one true.

likes(1,banana).
likes(1,mango).

test :- likes(1,banana),likes(1,mango).

?- test.  
true;  
false.

I want to know the reason behind this false.

  • 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-05-20T05:07:55+00:00Added an answer on May 20, 2026 at 5:07 am

    The way prolog works is by evaluating queries until a fail by negation.

    Here, you’ve established two facts:

    likes(1, banana). which says “1 likes banana”

    likes(1, mango). which says “1 likes mango”

    Then you have established a rule, which basically evaluates as:

    left_hand_side :- right_hand_side. left_hand_side if right_hand_side

    Evaluation of the rule as a query tries to match facts and returns true if it can, and false if it cannot match. One important thing to note is that, if specified, prolog will continue to match facts as long as rules evaluate to true.

    So let’s step through test :- likes(1,banana),likes(1,mango).

    If test is run as a query, prolog first tries likes(1,banana) which is a previously established fact, and is true. Then, it moves on to likes(1,mango) which, again, is a fact, and is true. Prolog has then reached the end of the rule, and outputs true.

    At this point, if you’re not searching for more matches you can cut the query short and just have true. However, if you’re looking for more (all) matches, prolog backtracks and tries to evaluate the rule again, searching for more matches.

    However, since your rule is only matching “likes banana and likes mango” and we already matched likes(1,banana), when prolog backtracks and tries evaluating likes(1,banana) again, since we already matched it before, this time there is not another fact (in other words, 1 cannot “like” banana more than once, unless that has been defined) to match. So that’s where the false comes from.

    In your prolog interpreter you may be able to trace the execution of your program by typing trace. then running your query. My trace is given below:

    | ?- trace
    .
    The debugger will first creep -- showing everything (trace)
    
    (1 ms) yes
    {trace}
    | ?- test.
          1    1  Call: test ? 
          2    2  Call: likes(1,banana) ? 
          2    2  Exit: likes(1,banana) ? 
          3    2  Call: likes(1,mango) ? 
          3    2  Exit: likes(1,mango) ? 
          1    1  Exit: test ? 
    
    true ? ;
          1    1  Redo: test ? 
          2    2  Redo: likes(1,banana) ? 
          2    2  Fail: likes(1,banana) ? 
          1    1  Fail: test ? 
    
    (1 ms) no
    {trace}
    | ?-
    

    One last thing to note: If, instead of pressing ; at the true ? prompt, had I pressed <ENTER>, the script would have finished with only the true.

    I’m glad you asked this question ’cause it allowed me a tiny refresher on prolog, which I really like but haven’t used in a long time.

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

Sidebar

Related Questions

I'm new to prolog and I just can't figure this out. I'm trying to
I am new to Prolog and I'm trying to to create function that will
I am new to Prolog. I need to write an integer adder that will
I'm new to Prolog and stuck on some programming homework. one of them should
I am new to prolog and i want to solve this problem. Suppose I
I have a prolog homework, which should work like this: singles([1,2,3,2,2,4,1], [3,4]). true Now
I am completely new to Prolog and trying some exercises. One of them is:
I'm new to Prolog. Using this basic 'database' structure, I thought I would be
I am really new in prolog programing and I can just ask simple questions.
New to PHP and MySQL, have heard amazing things about this website from Leo

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.