2

I'm having an extremely hard time understanding the concept of negation as failure in Prolog compared to default negation in Answer Set Programming. Could someone please explain to me what the difference is?

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • This is not a Stackoverflow question. You need to have a problem with code and show the code and explain the problem and ask how to solve it. This kind of question is better asked on a mailing list or maybe on reddit, on r/prolog. – User9213 Feb 28 '19 at 11:54
  • Prolog uses a simple search strategy to resolve queries. In Prolog, `\+ Q` causes Prolog to attempt to prove `Q`; if it succeeds, then `\+ Q` fails and this usually causes Prolog to begin backtracking and search again. If `Q` cannot be proven, Prolog considers `\+ Q` to have succeeded and continues. In ASP, `\+ Q` creates some sort of constraint that the solver can use to reduce the set of stable models, but it doesn't directly tamper with the search strategy the way it does in Prolog. – Daniel Lyons Feb 28 '19 at 18:06

1 Answers1

1

Sloppyly:

If you don't win the lottery, you need to get a job!

Prolog:

Alright, I'm gonna buy a ticket!

...later...

I guess I need to get a job.

ASP:

Alright, I'm going to find a job (because I don't know that I will will the lottery).

So, "Default Negation" a default no, except known otherwise, while "Negation as Failure" means try first, only then you will know about the failure.

And now in code:

win_lottery :- spend_money_on_ticket,
               fail.  % to actually win.

find_a_job.  % We can do that!

get_money :- win_lottery.
get_money :- not win_lottery, % (or \+)
             find_a_job.

ASP responds with

find_a_job get_money

Prolog will answer get_money with true, but until then it will have done spend_money_on_ticket, which makes you poorer.

(Actually, it will even buy two tickets, on for each clause of get_money. And if it would have won the second time, then get_money wouldn't have succeeded, so the correct Prolog version is:

get_money :- win_lottery,
             !.
get_money :- find_a_job.

But this doesn't use Negation-as-Failure anymore.)

firefrorefiddle
  • 3,795
  • 20
  • 31