Course contentsShow
← Computer Science
Lesson 2164 of 2,87248. Programming ParadigmsFree lesson

Negation as Failure

How Prolog implements negation: assuming a goal is false if it cannot be proven true.

Negation as Failure

You'll learn how Prolog interprets negation by treating a goal as false when it cannot prove it true—a pragmatic shortcut with important limitations.

The Core Idea

Unlike classical logic where something is either provably true or provably false, Prolog makes a closed-world assumption: if you can't prove a fact from the available knowledge, assume it's false. This is called negation as failure (often written \+ or not in Prolog).

When you query \+ Goal, Prolog tries to prove Goal. If that proof fails—meaning Prolog backtracks through all possibilities and finds nothing—it concludes \+ Goal succeeds. If Goal succeeds even once, \+ Goal fails.

How It Works

Think of it like a database search: if you ask "Is there a customer named Zeus?" and the system finds no record, it answers "no"—even though Zeus might exist somewhere outside the database. Prolog doesn't know what it doesn't know.

likes(mary, pizza).
likes(john, pasta).

% Query: Does Mary like sushi?
% \+ likes(mary, sushi) succeeds because it cannot prove likes(mary, sushi)

Important Limitation

Negation as failure is non-logical when combined with unification. It doesn't generate solutions; it only tests them. You must ground (instantiate) variables before negating, or you'll get unexpected results.

Key Takeaway:
Prolog's negation as failure assumes a goal is false if it cannot be proven true from known facts—a practical but incomplete form of negation that reflects the closed-world assumption.