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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:38:01+00:00 2026-06-14T19:38:01+00:00

There are four baskets, and each with a unique color. I write a prolog

  • 0

There are four baskets, and each with a unique color. I write a prolog program to tell the color oder based on some facts and rules. This is the .pl file:

iright(L, R, [L | [R | _]]).
iright(L, R, [_ | Rest]) :- iright(L, R, Rest).
nextto(L, R, List) :- iright(L, R, List).
nextto(L, R, List) :- iright(R, L, List).

myprogram(Data) :-     =(Data, [_,red,_,_]), 
\+nextto(red,blue,Data), % blue is not next to red
iright(red,green,Data), %green is right to red
member(red,Data),
member(blue,Data),
member(green,Data),
member(yellow,Data).

The iright and nextto predicates are correct. My query is myprogram(Data), and I expect the result should be

Data = [yellow,red, green, blue]?
yes

but actually the prompt show that

| ?- myprogram(Data).

no

I know the problem is the negation, but idk how and why. Plz help.

When I use trace.

      1    1  Call: myprogram(_16) ? 
      2    2  Call: \+nextto(red,blue,[_46,red,_50,_52]) ? 
      3    3  Call: nextto(red,blue,[_46,red,_50,_52]) ? 
      4    4  Call: iright(red,blue,[_46,red,_50,_52]) ? 
      5    5  Call: iright(red,blue,[red,_50,_52]) ? 
      5    5  Exit: iright(red,blue,[red,blue,_52]) ? 
      4    4  Exit: iright(red,blue,[_46,red,blue,_52]) ? 
      3    3  Exit: nextto(red,blue,[_46,red,blue,_52]) ? 
      2    2  Fail: \+nextto(red,blue,[_46,red,_50,_52]) ? 
      1    1  Fail: myprogram(_16) ? 

(2 ms) no
  • 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-14T19:38:02+00:00Added an answer on June 14, 2026 at 7:38 pm

    If you move the \+nextto(red, blue, Data) to the last line of myprogram, it works.

    It’s a little unintuitive but you need to think a bit about how Prolog evaluates expressions & what negation really means. Negation means “there’s no possible way for this to be true”, not “can’t be true for a particular set of values”. If you strip your program down to simply:

     myprogram(Data) :-
        =(Data, [_,red,_,_]),
        \+nextto(red,blue,Data).
    

    You will still get a No.. This is because, with what you’ve declared, Data can be [_, red, blue, _] – you see this decision made at Exit: iright(red,blue,[red,blue,_52]). Prolog tries to backtrack, but there’s nothing else it can try – you haven’t constrained any of the _s to have a particular value yet.

    If, OTOH, you put all your nextto, member & iright statements before your negated statement, there’s multiple possible solutions (well, two) to try when it gets to the negated expression: Data = [blue,red,green,yellow] and Data = [yellow,red,green,blue]. At this point, when it sees the negation it ‘throws away’ the ‘branch’ where red is next to blue, but it’s able to backtrack and has a possible world state that makes the negation true. That is to say that:

    myprogram(Data) :-
        =(Data, [_,red,_,_]),
        iright(red,green,Data),
        member(red,Data),
        member(blue,Data),
        member(green,Data),
        member(yellow,Data),
        \+nextto(red,blue,Data).
    

    …gives you the desired results.

    TL;DR – negation (and cuts, when you get to them) in Prolog are a lot more powerful than you think at first. Make sure you’ve figured everything else out first before using them.

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

Sidebar

Related Questions

Hey there. I'm trying to write a small program that will read the four
There are four tables Employee (Employee_id,name,chief_id) Department(Department_Id,name) Emp_Dep(Employee_id,Department_Id) Emp_Sal(Employee_id,salary) Now I need to write
There are four files, a.txt , b.txt , c.txt , d.txt . Each file
There are four images , and each images has to be border effect(css) by
In my application - there are four buttons named as follows: Top - left
According to this there are four size screens, small, normal, large and xlarge. So
I have a Database named CarsType.accdb there are four fields in the data base
I've got collection of geo objects in database: There are four Tables: Countries Regions
Yes, I know there are four Memory windows, but I much prefer the display
I have made an html wizzard for making order. There are four steps, in

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.