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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:51:35+00:00 2026-06-16T14:51:35+00:00

I want to know how Prolog solves this program: test(X, Y). test(X, X):-!, fail.

  • 0

I want to know how Prolog solves this program:

test(X, Y).
test(X, X):-!, fail.

I googled “negation as failure” but I am confused!

  • 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-16T14:51:36+00:00Added an answer on June 16, 2026 at 2:51 pm

    Consider the following example:

    father(nick, john).
    

    We use the predicate father(X,Y) to denote that the father of X is Y.
    Let’s query the database:

    ?- father(nick,X).
    X = john.
    
    ?- father(john,Y).
    false.
    

    In both cases we asked who is the father of someone (nick, john respectively). In the first case, prolog knew the answer (john) however in the second it didn’t and so the answer was false, meaning that john does not have any father. We might expect that, as we gave prolog no information about john’s father, it would respond with unknown. That would be an open-world where if something is not known we don’t assume that it’s false. On the contrary, in the closed world of prolog, if we don’t know something, we assume that it’s false.

    Note that a world where we say that we don’t know who the father of john is, based on knowing that anyone must have a father is not an open world; it can be easily modelled in prolog:

    data_father(nick, john).
    father(X,Y):-
        data_father(X,Y) -> true ; true.
    

    On the other hand, in an open world prolog you would write facts and counter facts:

    father(nick, john).
    not father(adam, X).
    

    And this is negation as failure. However, this is not what happens in your program:

    test(X, Y).
    test(X, X):-!, fail.
    

    The first clause will always succeed, regardless of the value of the arguments. In fact, exactly because of that, there is no point in naming the arguments and prolog will give you a singleton warning; you can write the clause as test(_, _).

    On the other hand, the second clause will always fail. It can fail in two ways: (1) the arguments may be different (2) the arguments are unifiable so prolog moves to the body and then fails.

    Precisely because prolog is using a closed world model there is no point of having clauses (without side-effects (but that’s considered bad practise anyway)) that always fail. On the contrary, these extra calls cause your program to run slower and use more memory.

    It is also worth noting that the cut (!/0) does nothing here since when you reach it there are no more choice points. Consider however this example:

    test(X, Y).
    test(X, X):-!, fail.
    test(X, 42).
    
    ?- test(1,42).
    true ;
    true.
    
    ?- test(42,42).
    true ;
    false.
    

    In both cases prolog will create 3 choice points, one for each clause.
    In the first case, Prolog will successfully match the head of the first clause and succeed since there is no body.
    Then, it will fail matching the head of the second clause and the body will not be “executed”.
    Finally, it will match the head of the third clause and succeed since there is no body.

    However, on the second case:
    Prolog will succeed in matching the head of the first clause and succeed since there is no body.
    Then, it will succeed in matching the head of the second clause; the cut will remove all other choice points and then it will fail due to fail.
    Therefore, prolog will not try the third clause.

    A few words about negation as failure since you mentioned it. Negation as failure is based on the closed world assumption; since we assume that anything that cannot be deduced from the facts we already have is wrong, if we fail to prove something it means that the opposite of it is considered true. For example, consider this:

    father(nick, john).
    fatherless(X) :- \+ father(X, _).
    

    And

    ?- fatherless(nick).
    false.
    
    ?- fatherless(john).
    true.
    

    On the contrary, in an open world prolog with the following code:

    father(nick, john).
    not father(adam, X).
    fatherless(X) :- \+ father(X, _).
    

    fatherless/1 would succeed only for adam, fail for nick and return unknown for anything else

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

Sidebar

Related Questions

I want to program this algorithms in Prolog, and first I need to create
I want know when all thread has been finished in a multithread program without
This is going to sound super hackish but does anyone know of a way
I know this very regular question But i didn't get that's way ask here
I want to know how I would get something like this to work. It
I want to write a program in Prolog that builds a list of all
I am a beginner to Prolog and i want help with this this functor
as titled. I have use synchronized to guard multi-thread write. but I want know
i want know how i can manage multiple twitter account on iOS in my
i want know if is possible, to get a specific element value of a

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.