Like this presentation? Why not share!

# PROLOG: Cuts And Negation In Prolog

## by DataminingTools Inc on Mar 29, 2010

• 7,380 views

PROLOG: Cuts And Negation In Prolog

PROLOG: Cuts And Negation In Prolog

### Views

Total Views
7,380
Views on SlideShare
7,363
Embed Views
17

Likes
0
0
0

### 2 Embeds17

 http://www.slideshare.net 16 http://oliva.uhk.cz 1

### Categories

Uploaded via SlideShare as Microsoft PowerPoint

## PROLOG: Cuts And Negation In PrologPresentation Transcript

• Cuts and Negation in Prolog
• Overview
The cut
IF-Then-else
Examples
Negation as failure
• The Cut
Automatic backtracking is one of the most characteristic features of Prolog.
There is an inbuilt Prolog predicate !, called cut, which offers a more direct way of exercising control over the way Prolog looks for solutions.
Cut is simply a special atom that we can use when writing clauses.
Ex: p(X) :- b(X),c(X),!,d(X),e(X).
• consider the following piece of cut-free code:
p(X) :- a(X).
p(X) :- b(X),c(X),d(X),e(X).
p(X) :- f(X).
a(1).
b(1).
c(1).
b(2).
c(2).
d(2).
e(2).
f(3).
If we pose the query p(X) we will get the following responses:
X = 1 ;
X = 2 ;
X = 3 ;
No
• The Cut
But now suppose we insert a cut in the second clause:
p(X) :- b(X),c(X),!,d(X),e(X).
If we now pose the query p(X) we will get the following responses:
X = 1 ;
No
• If-Then-Else
In Prolog, if A then B else C is written as ( A -> B ; C).
To Prolog this means: try A. If you can prove it, go on to prove B and ignore C.
If A fails, however, go on to prove C ignoring B.
The max predicate using the if-then-else construct looks as follows:
max(X,Y,Z) :-
( X =< Y
-> Z = Y
; Z = X
).
• Negation as a Failure
Predicate fail/0As its name suggests, fail is a special symbol that will immediately fail when Prolog encounters it as a goal.
when Prolog fails, it tries to backtrack.
Thus fail can be viewed as an instruction to force backtracking.
And when used in combination with cut, which blocks backtracking, fail enables us to write some interesting programs.
• Consider the following code:
enjoys(vincent,X) :- big_kahuna_burger(X),!,fail.
enjoys(vincent,X) :- burger(X).
burger(X) :- big_mac(X).
burger(X) :- big_kahuna_burger(X).
burger(X) :- whopper(X).
big_mac(a).
big_kahuna_burger(b).
big_mac(c).
whopper(d).
The first two lines describe Vincent's preferences. The last six lines describe a world containing four burgers, a, b, c, and d.
• ?- enjoys(vincent,a).
yes
?- enjoys(vincent,b).
no
?- enjoys(vincent,c).
yes
?- enjoys(vincent,d).
yes
Here, the key is the combination of ! and fail in the first line.
When we pose the query enjoys(vincent,b), the first rule applies, and we reach the cut.
This commits us to the choices we have made, and in particular, blocks access to the second rule.
But then we hit fail. This tries to force backtracking, but the cut blocks it, and so our query fails.
• Negation as a failure
suppose that we need to write code to capture the following condition: p holds if a and b hold, or if a does not hold and c holds too.
This can be captured with the help of negation as failure very directly:
p :- a,b.
p :- + a, c.
But suppose that a is a very complicated goal, it would be better to use the following program:
p :- a,!,b.
p :- c.
• Visit more self help tutorials
Pick a tutorial of your choice and browse through it at your own pace.
The tutorials section is free, self-guiding and will not involve any additional support.
Visit us at www.dataminingtools.net