SlideShare a Scribd company logo
1 of 18
Database manipulation in Prolog
OVERVIEW Database Manipulation Collecting solutions ,[object Object]
bagof/3
setof/3Examples
Database Manipulation Prolog has four database manipulation commands: ,[object Object]
retract
asserta
assertz,[object Object]
Suppose we then made four more assert commands: assert(happy(vincent)). yes assert(happy(marcellus)). yes assert(happy(butch)). yes assert(happy(vincent)).  yes Suppose we then ask for a listing: listing.     happy(mia).         happy(vincent).         happy(marcellus).         happy(butch).         happy(vincent).          yes
assert So far, we have only asserted facts into the database, but we can assert new rules as well. Suppose we want to assert the rule that everyone who is happy is naive.  That is, suppose we want to assert that: naive(X) :- happy(X). We can do this as follows: assert( (naive(X) :- happy(X)) ).
retract To remove all of our assertions we can use a variable: retract(happy(X)). X = mia ; X = butch ; X = vincent ; No listing.  Yes (reveals that the database is now empty)
assertz and asserta If we want more control over where the asserted material is placed, there are two variants of assert, namely: 1. assertz. Places asserted material at the end of the database. 2. asserta. Places asserted material at the beginning of the database. For example, suppose we start with an empty database, and then we give the following command: assert( p(b) ), assertz( p(c) ), asserta( p(a) ). Then a listing reveals that we now have the following database: p(a). p(b). p(c). yes
Collecting Solutions There may be many solutions to a query. For example, suppose we are working with the database: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). Then if we pose the query descend(martha,X). There are four solutions (namely X=charlotte, X=caroline, X=laura, and X=rose).
Collecting Solutions Prolog has three built-in predicates that do this: ,[object Object]
 bagof
SetofThese predicates collect all the solutions to a query and put them in a list,
findall/3 findall(Object,Goal,List). produces a list List of all the objects Object that satisfy the goal Goal. Suppose we're working with the previous database (that is, with the information about child and the definition of descend).  Then if we pose the query findall(X,descend(martha,X),Z). we are asking for a list Z containing all the values of X that satisfy descend(martha,X). Prolog will respond as: X = _7489 Z = [charlotte,caroline,laura,rose]
Suppose we want to build a new predicate from Martha/1 that is true only of descendants of Martha. We could do this with the query: findall(fromMartha(X),descend(martha,X),Z). ,[object Object]

More Related Content

What's hot

What's hot (20)

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
A* Search Algorithm
A* Search AlgorithmA* Search Algorithm
A* Search Algorithm
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Heuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligenceHeuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligence
 
strong slot and filler
strong slot and fillerstrong slot and filler
strong slot and filler
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Control Strategies in AI
Control Strategies in AIControl Strategies in AI
Control Strategies in AI
 
Dempster Shafer Theory AI CSE 8th Sem
Dempster Shafer Theory AI CSE 8th SemDempster Shafer Theory AI CSE 8th Sem
Dempster Shafer Theory AI CSE 8th Sem
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Perceptron (neural network)
Perceptron (neural network)Perceptron (neural network)
Perceptron (neural network)
 
AI - Local Search - Hill Climbing
AI - Local Search - Hill ClimbingAI - Local Search - Hill Climbing
AI - Local Search - Hill Climbing
 
Knowledge representation and Predicate logic
Knowledge representation and Predicate logicKnowledge representation and Predicate logic
Knowledge representation and Predicate logic
 
Logics for non monotonic reasoning-ai
Logics for non monotonic reasoning-aiLogics for non monotonic reasoning-ai
Logics for non monotonic reasoning-ai
 
Conversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with StackConversion of Infix to Prefix and Postfix with Stack
Conversion of Infix to Prefix and Postfix with Stack
 
Structure of agents
Structure of agentsStructure of agents
Structure of agents
 
First order logic
First order logicFirst order logic
First order logic
 
AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)AI Lecture 3 (solving problems by searching)
AI Lecture 3 (solving problems by searching)
 
Multilayer & Back propagation algorithm
Multilayer & Back propagation algorithmMultilayer & Back propagation algorithm
Multilayer & Back propagation algorithm
 
Lecture 11 Informed Search
Lecture 11 Informed SearchLecture 11 Informed Search
Lecture 11 Informed Search
 

Viewers also liked

Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
Nitesh Singh
 

Viewers also liked (20)

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: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
PROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologPROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations 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
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In Prolog
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"
 
Prolog Code [Family Tree] by Shahzeb Pirzada
Prolog Code [Family Tree] by Shahzeb PirzadaProlog Code [Family Tree] by Shahzeb Pirzada
Prolog Code [Family Tree] by Shahzeb Pirzada
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
 
BI: Open Source
BI: Open SourceBI: Open Source
BI: Open Source
 
Procedures And Functions in Matlab
Procedures And Functions in MatlabProcedures And Functions in Matlab
Procedures And Functions in Matlab
 
Webmining Overview
Webmining OverviewWebmining Overview
Webmining Overview
 
Info Chimps: What Makes Infochimps.org Unique
Info Chimps: What Makes Infochimps.org UniqueInfo Chimps: What Makes Infochimps.org Unique
Info Chimps: What Makes Infochimps.org Unique
 
Welcome
WelcomeWelcome
Welcome
 
Data Applied:Tree Maps
Data Applied:Tree MapsData Applied:Tree Maps
Data Applied:Tree Maps
 
MS Sql Server: Manipulating Database
MS Sql Server: Manipulating DatabaseMS Sql Server: Manipulating Database
MS Sql Server: Manipulating Database
 
Graph Plots in Matlab
Graph Plots in MatlabGraph Plots in Matlab
Graph Plots in Matlab
 
Ccc
CccCcc
Ccc
 
InfoChimps.Org
InfoChimps.OrgInfoChimps.Org
InfoChimps.Org
 
Data Mining The Sky
Data Mining The SkyData Mining The Sky
Data Mining The Sky
 

More from DataminingTools Inc

More from 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
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
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 ...
 

PROLOG: Database Manipulation In Prolog

  • 2.
  • 5.
  • 8.
  • 9. Suppose we then made four more assert commands: assert(happy(vincent)). yes assert(happy(marcellus)). yes assert(happy(butch)). yes assert(happy(vincent)).  yes Suppose we then ask for a listing: listing.  happy(mia). happy(vincent). happy(marcellus). happy(butch). happy(vincent). yes
  • 10. assert So far, we have only asserted facts into the database, but we can assert new rules as well. Suppose we want to assert the rule that everyone who is happy is naive. That is, suppose we want to assert that: naive(X) :- happy(X). We can do this as follows: assert( (naive(X) :- happy(X)) ).
  • 11. retract To remove all of our assertions we can use a variable: retract(happy(X)). X = mia ; X = butch ; X = vincent ; No listing.  Yes (reveals that the database is now empty)
  • 12. assertz and asserta If we want more control over where the asserted material is placed, there are two variants of assert, namely: 1. assertz. Places asserted material at the end of the database. 2. asserta. Places asserted material at the beginning of the database. For example, suppose we start with an empty database, and then we give the following command: assert( p(b) ), assertz( p(c) ), asserta( p(a) ). Then a listing reveals that we now have the following database: p(a). p(b). p(c). yes
  • 13. Collecting Solutions There may be many solutions to a query. For example, suppose we are working with the database: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). Then if we pose the query descend(martha,X). There are four solutions (namely X=charlotte, X=caroline, X=laura, and X=rose).
  • 14.
  • 16. SetofThese predicates collect all the solutions to a query and put them in a list,
  • 17. findall/3 findall(Object,Goal,List). produces a list List of all the objects Object that satisfy the goal Goal. Suppose we're working with the previous database (that is, with the information about child and the definition of descend). Then if we pose the query findall(X,descend(martha,X),Z). we are asking for a list Z containing all the values of X that satisfy descend(martha,X). Prolog will respond as: X = _7489 Z = [charlotte,caroline,laura,rose]
  • 18.
  • 19. Prolog will respondX = _7616 Z = [fromMartha(charlotte),fromMartha(caroline), fromMartha(laura),fromMartha(rose)]
  • 20. bagof/3 suppose we pose the query findall(Child,descend(Mother,Child),List). We get the response Child = _6947 Mother = _6951 List = [charlotte,caroline,laura,rose,caroline,laura,rose, laura,rose,rose] sometimes it would be useful if we had a separate list for each of the different instantiations of Mother.
  • 21. bagof lets us do. If we pose the query bagof(Child,descend(Mother,Child),List). we get the response Child = _7736 Mother = caroline List = [laura,rose] ; Child = _7736 Mother = charlotte List = [caroline,laura,rose] ; Child = _7736 Mother = laura List = [rose] ; Child = _7736 Mother = martha List = [charlotte,caroline,laura,rose] ; no
  • 22. bagof/3 If we pose the query bagof(Child,Mother ^descend(Mother,Child),List). This says: give me a list of all the values of Child such that descend(Mother,Child), and put the result in a list. So posing this query yields: Child = _7870 Mother = _7874 List = [charlotte,caroline,laura,rose,caroline,laura,rose, laura,rose,rose]
  • 23. setof/3 In setof/3 predicate, the lists contained are ordered and contain no redundancies. Ex:suppose we have the following database age(harry,13). age(draco,14). age(ron,13). age(hermione,13). age(dumbledore,60). age(hagrid,30).
  • 24. Now suppose we want a list of everyone whose age is recorded in the database. We can do this with the query: findall(X,age(X,Y),Out). X = _8443 Y = _8448 Out = [harry,draco,ron,hermione,dumbledore,hagrid] If we want the list to be ordered, we can achive this by the following query: setof(X,Y ^ age(X,Y),Out). X = _8711 Y = _8715 Out = [draco,dumbledore,hagrid,harry,hermione,ron]  here, the list is alphabatically ordered.
  • 25. 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