Prolog: Cuts And Negation In Prolog

  • 601 views
Uploaded on

Prolog: Cuts And Negation In Prolog

Prolog: Cuts And Negation In Prolog

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
601
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Cuts and Negation in Prolog
  • 2. Overview
    The cut
    IF-Then-else
    Examples
    Negation as failure
  • 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. 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. 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
    ).
  • 7. 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.
  • 8. 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.
  • 9. ?- 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.
  • 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. 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