Prolog: Cuts And Negation In Prolog

818 views
702 views

Published on

Prolog: Cuts And Negation In Prolog

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
818
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Prolog: Cuts And Negation In Prolog

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

×