(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
Cut and Goal on prolog
1. Cuts And Goal Model in
Prolog
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
3. 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)
4. The Cut
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
5. 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
6. Examples
Suppose we have the following facts:
teaches(dr_fred, history).
teaches(dr_fred, english).
teaches(dr_fred, drama).
teaches(dr_fiona, physics).
studies(alice, english).
studies(angus, english).
studies(amelia, drama).
studies(alex, physics).
7. Example
Output :-
?- teaches(dr_fred, Course), studies(Student, Course).
Course = english
Student = alice ;
Course = english
Student = angus ;
Course = drama
Student = amelia ;
false.
8. 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).
9. 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
10. 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.
11. GOAL ORDER
A query to the Prolog interpreter consists of one or more goals. For example,
?- lectures(john, Subject), studies(Student, Subject).
here are two goals, lectures(john, Subject) and studies(Student, Subject).
A goal is something that Prolog tries to satisfy by finding values of the variables (in this
case Student and Subject) that make the goal succeed .
If all the goals in a query succeed, Prolog prints the bindings necessary to make the query succeed.
Goals occur in rules as well as in queries. In
happy(Dog) :-
Is dog(Dog),
go_for_walk(Dog).
is_dog(Dog) and go_for_walk(Dog) are the two goals that form the body of the rule.