Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- PROLOG: Fact Roles And Queries In P... by PROLOG CONTENT 6219 views
- PROLOG: Recursion And Lists In Prolog by PROLOG CONTENT 2214 views
- PROLOG: Recursion And Lists In Prolog by DataminingTools Inc 15243 views
- Prolog basics by shivani saluja 3207 views
- PROLOG: Introduction To Prolog by DataminingTools Inc 3599 views
- PROLOG: Cuts And Negation In Prolog by DataminingTools Inc 10499 views

818 views

702 views

702 views

Published on

Prolog: Cuts And Negation In Prolog

Published in:
Technology

No Downloads

Total views

818

On SlideShare

0

From Embeds

0

Number of Embeds

42

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Cuts and Negation in Prolog<br />
- 2. Overview<br />The cut<br />IF-Then-else<br />Examples<br />Negation as failure<br />
- 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. 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. 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. 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. Negation as a Failure<br />Predicate fail/0As 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. 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. ?- 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. 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. 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 />

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment