S 24 P 1 Negation As Failure

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CPSC449

Programming Paradigms

Cut & Negation as Failure


(Prolog)
Modified by
Arash Afshar

Dr. Robert Collier


Spring 2015

Reading Assignment

Haskell: The Craft of


Functional Programming
(3rd Edition)

Programming in Prolog
Using the ISO Standard
(5th Edition)

Chapters 1 - 4

Chapter 4

Cut (!/0)
Commits to choices made during the immediate parent
predicate

\+
Implemented using !, fail

A \= B
The same as \+ A = B

A =\= B
Does not use cut fail

Two negation as failures do not cancel out


\+ (\+ (Goal)) is not the same as Goal

Safe usage of cut


If all variables are already instantiated
If the un-instantiated variables are not used elsewhere

Example
overlap(A, B) :- member(X, A), member(X, B).
disjoint(A, B) :- \+ overlap(A, B).

How does it behave on the following queries?


?????-

overlap([1, 2, 3], [3, 2, 5]).


overlap([1, 2, 3], [5]).
disjoint([1, 2, 3], [5]).
disjoint([1, 2, 3], B)
overlap([1, 2, 3], B)
5

You might also like