SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Cuts and Negation in Prolog
Overview The cut IF-Then-else Examples Negation as failure
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).
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
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
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   ).
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.
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.
?- 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.
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.
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

Weitere ähnliche Inhalte

Was ist angesagt? (9)

Lesson 7: What does f' say about f?
Lesson 7: What does f' say about f?Lesson 7: What does f' say about f?
Lesson 7: What does f' say about f?
 
Unit2
Unit2Unit2
Unit2
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 
Function therory
Function theroryFunction therory
Function therory
 
ML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional LanguageML: A Strongly Typed Functional Language
ML: A Strongly Typed Functional Language
 
re3 - modern regex syntax with a focus on adoption
re3 - modern regex syntax with a focus on adoptionre3 - modern regex syntax with a focus on adoption
re3 - modern regex syntax with a focus on adoption
 
Exercise #8 notes
Exercise #8 notesExercise #8 notes
Exercise #8 notes
 
conditional
conditionalconditional
conditional
 
Quinto Punto Parte B
Quinto Punto Parte BQuinto Punto Parte B
Quinto Punto Parte B
 

Ähnlich wie Prolog: Cuts And Negation In Prolog

09 logic programming
09 logic programming09 logic programming
09 logic programming
saru40
 
LAC2013 UNIT preTESTs!
LAC2013 UNIT preTESTs!LAC2013 UNIT preTESTs!
LAC2013 UNIT preTESTs!
A Jorge Garcia
 

Ähnlich wie Prolog: Cuts And Negation In Prolog (20)

Cut and Goal on prolog
Cut and Goal on prologCut and Goal on prolog
Cut and Goal on prolog
 
20 prolog5
20 prolog520 prolog5
20 prolog5
 
Class 11: Deeper List Procedures
Class 11: Deeper List ProceduresClass 11: Deeper List Procedures
Class 11: Deeper List Procedures
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
C2.0 propositional logic
C2.0 propositional logicC2.0 propositional logic
C2.0 propositional logic
 
Optimal Budget Allocation: Theoretical Guarantee and Efficient Algorithm
Optimal Budget Allocation: Theoretical Guarantee and Efficient AlgorithmOptimal Budget Allocation: Theoretical Guarantee and Efficient Algorithm
Optimal Budget Allocation: Theoretical Guarantee and Efficient Algorithm
 
Signals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptxSignals and Systems Homework Help.pptx
Signals and Systems Homework Help.pptx
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
Funções 3
Funções 3Funções 3
Funções 3
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
09 logic programming
09 logic programming09 logic programming
09 logic programming
 
Optimization of probabilistic argumentation with Markov processes
Optimization of probabilistic argumentation with Markov processesOptimization of probabilistic argumentation with Markov processes
Optimization of probabilistic argumentation with Markov processes
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
LAC2013 UNIT preTESTs!
LAC2013 UNIT preTESTs!LAC2013 UNIT preTESTs!
LAC2013 UNIT preTESTs!
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 

Mehr von PROLOG CONTENT (6)

PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In Prolog
 
PROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologPROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In Prolog
 
PROLOG: Clauses Grammer In Prolog
PROLOG: Clauses Grammer In PrologPROLOG: Clauses Grammer In Prolog
PROLOG: Clauses Grammer In Prolog
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
PROLOG: Matching And Proof Search In Prolog
PROLOG: Matching And Proof Search In PrologPROLOG: Matching And Proof Search In Prolog
PROLOG: Matching And Proof Search In Prolog
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 

Prolog: Cuts And Negation In Prolog

  • 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