SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Loops in flow
Loops in flowLoops in flow
Loops in flow
 
Lecture 14 Heuristic Search-A star algorithm
Lecture 14 Heuristic Search-A star algorithmLecture 14 Heuristic Search-A star algorithm
Lecture 14 Heuristic Search-A star algorithm
 
State Space Search Strategies in Artificial Intelligence (AI)
State Space Search Strategies in Artificial Intelligence (AI)State Space Search Strategies in Artificial Intelligence (AI)
State Space Search Strategies in Artificial Intelligence (AI)
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
And or graph
And or graphAnd or graph
And or graph
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
A* algorithm
A* algorithmA* algorithm
A* algorithm
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Problem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.pptProblem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.ppt
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
Push down automata
Push down automataPush down automata
Push down automata
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Semelhante a 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
 

Semelhante a 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
 

Mais de DataminingTools Inc

Mais de DataminingTools Inc (20)

Terminology Machine Learning
Terminology Machine LearningTerminology Machine Learning
Terminology Machine Learning
 
Techniques Machine Learning
Techniques Machine LearningTechniques Machine Learning
Techniques Machine Learning
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
 
Areas of machine leanring
Areas of machine leanringAreas of machine leanring
Areas of machine leanring
 
AI: Planning and AI
AI: Planning and AIAI: Planning and AI
AI: Planning and AI
 
AI: Logic in AI 2
AI: Logic in AI 2AI: Logic in AI 2
AI: Logic in AI 2
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
AI: Learning in AI 2
AI: Learning in AI 2AI: Learning in AI 2
AI: Learning in AI 2
 
AI: Learning in AI
AI: Learning in AI AI: Learning in AI
AI: Learning in AI
 
AI: Introduction to artificial intelligence
AI: Introduction to artificial intelligenceAI: Introduction to artificial intelligence
AI: Introduction to artificial intelligence
 
AI: Belief Networks
AI: Belief NetworksAI: Belief Networks
AI: Belief Networks
 
AI: AI & Searching
AI: AI & SearchingAI: AI & Searching
AI: AI & Searching
 
AI: AI & Problem Solving
AI: AI & Problem SolvingAI: AI & Problem Solving
AI: AI & Problem Solving
 
Data Mining: Text and web mining
Data Mining: Text and web miningData Mining: Text and web mining
Data Mining: Text and web mining
 
Data Mining: Outlier analysis
Data Mining: Outlier analysisData Mining: Outlier analysis
Data Mining: Outlier analysis
 
Data Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence dataData Mining: Mining stream time series and sequence data
Data Mining: Mining stream time series and sequence data
 
Data Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlationsData Mining: Mining ,associations, and correlations
Data Mining: Mining ,associations, and correlations
 
Data Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysisData Mining: Graph mining and social network analysis
Data Mining: Graph mining and social network analysis
 
Data warehouse and olap technology
Data warehouse and olap technologyData warehouse and olap technology
Data warehouse and olap technology
 
Data Mining: Data processing
Data Mining: Data processingData Mining: Data processing
Data Mining: Data processing
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.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