SlideShare a Scribd company logo
3
Most read
5
Most read
17
Most read
Database manipulation in Prolog
OVERVIEWDatabase ManipulationCollecting solutionsfindall/3
bagof/3
setof/3Examples
Database ManipulationProlog has four database manipulation commands: assert
retract
asserta
assertzassertConsider an empty database. So if we give the command:listing.we simply get a yes; the listing (of course) is empty.Suppose we now give this command:assert(happy(mia)).It succeeds (assert commands always succeed). If we now give the command:listing.we get the listing:happy(mia).That is, the database is no longer empty: it now contains the fact we asserted.
Suppose we then made four more assert commands:assert(happy(vincent)).yesassert(happy(marcellus)).yesassert(happy(butch)).yesassert(happy(vincent)). yesSuppose we then ask for a listing:listing.    happy(mia).        happy(vincent).        happy(marcellus).        happy(butch).        happy(vincent).         yes
assertSo 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)) ).
retractTo remove all of our assertions we can use a variable:retract(happy(X)).X = mia ;X = butch ;X = vincent ;Nolisting. Yes (reveals that the database is now empty)
assertz and assertaIf 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 SolutionsThere 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 SolutionsProlog has three built-in predicates that do this: findall
 bagof
SetofThese predicates collect all the solutions to a query and put them in a list,
findall/3findall(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 = _7489Z = [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).That is, we are asking for a list Z containing all the values of fromMartha(X) that satisfy the goal descend(martha,X).

More Related Content

PDF
Relation Hasse diagram
PPTX
Top down parsering and bottom up parsering.pptx
PPTX
PROLOG: Database Manipulation In Prolog
PPTX
Traveling salesman problem
PPT
Expression evaluation
PPTX
Garbage collection
PPTX
Data structure tries
PDF
I.BEST FIRST SEARCH IN AI
Relation Hasse diagram
Top down parsering and bottom up parsering.pptx
PROLOG: Database Manipulation In Prolog
Traveling salesman problem
Expression evaluation
Garbage collection
Data structure tries
I.BEST FIRST SEARCH IN AI

What's hot (20)

PPTX
Bruteforce algorithm
PPTX
Ambiguous & Unambiguous Grammar
PPTX
Tsp branch and-bound
PPTX
Stressen's matrix multiplication
PPTX
Hash table in data structure and algorithm
PPTX
Dijkstra's Algorithm
PPTX
Fuzzy arithmetic
PPTX
Exception Handling in object oriented programming using C++
PPTX
Python Exception Handling
PPTX
Open addressiing &rehashing,extendiblevhashing
PPT
Problems, Problem spaces and Search
PPTX
Hamiltonian path
PDF
Informed search
PPT
Binary Search
PPT
Hash table
PPTX
MATCHING GRAPH THEORY
PPTX
Hamilton path and euler path
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
PDF
PPTX
Topological Sorting
Bruteforce algorithm
Ambiguous & Unambiguous Grammar
Tsp branch and-bound
Stressen's matrix multiplication
Hash table in data structure and algorithm
Dijkstra's Algorithm
Fuzzy arithmetic
Exception Handling in object oriented programming using C++
Python Exception Handling
Open addressiing &rehashing,extendiblevhashing
Problems, Problem spaces and Search
Hamiltonian path
Informed search
Binary Search
Hash table
MATCHING GRAPH THEORY
Hamilton path and euler path
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Topological Sorting
Ad

Viewers also liked (20)

PPTX
PROLOG: Fact Roles And Queries In Prolog
PPTX
Prolog: Arithmetic Operations In Prolog
PPT
Prolog programming
PPTX
PROLOG: Arithmetic Operations In Prolog
PPTX
PROLOG: Recursion And Lists In Prolog
PPTX
PROLOG: Cuts And Negation In Prolog
PDF
"That scripting language called Prolog"
TXT
Prolog Code [Family Tree] by Shahzeb Pirzada
PDF
Logic programming (1)
PPTX
BI: Open Source
PPTX
Procedures And Functions in Matlab
PPT
Webmining Overview
PPTX
Info Chimps: What Makes Infochimps.org Unique
PPTX
PPTX
Data Applied:Tree Maps
PPTX
MS Sql Server: Manipulating Database
PPTX
Graph Plots in Matlab
PPTX
InfoChimps.Org
PPTX
Data Mining The Sky
PROLOG: Fact Roles And Queries In Prolog
Prolog: Arithmetic Operations In Prolog
Prolog programming
PROLOG: Arithmetic Operations In Prolog
PROLOG: Recursion And Lists In Prolog
PROLOG: Cuts And Negation In Prolog
"That scripting language called Prolog"
Prolog Code [Family Tree] by Shahzeb Pirzada
Logic programming (1)
BI: Open Source
Procedures And Functions in Matlab
Webmining Overview
Info Chimps: What Makes Infochimps.org Unique
Data Applied:Tree Maps
MS Sql Server: Manipulating Database
Graph Plots in Matlab
InfoChimps.Org
Data Mining The Sky
Ad

Similar to PROLOG: Database Manipulation In Prolog (20)

PPTX
DOCX
AI Lab Manual.docx
PPT
Prolog 01
PPTX
PROLOG: Recursion And Lists In Prolog
PDF
ICS1019.pdf
PPTX
Unit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit
PPTX
Ics1019 ics5003
PPTX
Ics1019 ics5003
PDF
Introduction to prolog
PPTX
Introduction to Prolog
PPTX
Artificial Intelligence and Expert System
PDF
PROLOG in artificial intelligence(Basic of pprolog)).pdf
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPTX
PROLOG: Introduction To Prolog
PPTX
Prolog 7-Languages
AI Lab Manual.docx
Prolog 01
PROLOG: Recursion And Lists In Prolog
ICS1019.pdf
Unit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit 3 Prolog.pptxUnit
Ics1019 ics5003
Ics1019 ics5003
Introduction to prolog
Introduction to Prolog
Artificial Intelligence and Expert System
PROLOG in artificial intelligence(Basic of pprolog)).pdf
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
PROLOG: Introduction To Prolog
Prolog 7-Languages

More from DataminingTools Inc (20)

PPTX
Terminology Machine Learning
PPTX
Techniques Machine Learning
PPTX
Machine learning Introduction
PPTX
Areas of machine leanring
PPTX
AI: Planning and AI
PPTX
AI: Logic in AI 2
PPTX
AI: Logic in AI
PPTX
AI: Learning in AI 2
PPTX
AI: Learning in AI
PPTX
AI: Introduction to artificial intelligence
PPTX
AI: Belief Networks
PPTX
AI: AI & Searching
PPTX
AI: AI & Problem Solving
PPTX
Data Mining: Text and web mining
PPTX
Data Mining: Outlier analysis
PPTX
Data Mining: Mining stream time series and sequence data
PPTX
Data Mining: Mining ,associations, and correlations
PPTX
Data Mining: Graph mining and social network analysis
PPTX
Data warehouse and olap technology
PPTX
Data Mining: Data processing
Terminology Machine Learning
Techniques Machine Learning
Machine learning Introduction
Areas of machine leanring
AI: Planning and AI
AI: Logic in AI 2
AI: Logic in AI
AI: Learning in AI 2
AI: Learning in AI
AI: Introduction to artificial intelligence
AI: Belief Networks
AI: AI & Searching
AI: AI & Problem Solving
Data Mining: Text and web mining
Data Mining: Outlier analysis
Data Mining: Mining stream time series and sequence data
Data Mining: Mining ,associations, and correlations
Data Mining: Graph mining and social network analysis
Data warehouse and olap technology
Data Mining: Data processing

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced IT Governance
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced IT Governance
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Sensors and Actuators in IoT Systems using pdf
madgavkar20181017ppt McKinsey Presentation.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....

PROLOG: Database Manipulation In Prolog

  • 5. Database ManipulationProlog has four database manipulation commands: assert
  • 8. assertzassertConsider an empty database. So if we give the command:listing.we simply get a yes; the listing (of course) is empty.Suppose we now give this command:assert(happy(mia)).It succeeds (assert commands always succeed). If we now give the command:listing.we get the listing:happy(mia).That is, the database is no longer empty: it now contains the fact we asserted.
  • 9. Suppose we then made four more assert commands:assert(happy(vincent)).yesassert(happy(marcellus)).yesassert(happy(butch)).yesassert(happy(vincent)). yesSuppose we then ask for a listing:listing. happy(mia). happy(vincent). happy(marcellus). happy(butch). happy(vincent). yes
  • 10. assertSo 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. retractTo remove all of our assertions we can use a variable:retract(happy(X)).X = mia ;X = butch ;X = vincent ;Nolisting. Yes (reveals that the database is now empty)
  • 12. assertz and assertaIf 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 SolutionsThere 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. Collecting SolutionsProlog has three built-in predicates that do this: findall
  • 16. SetofThese predicates collect all the solutions to a query and put them in a list,
  • 17. findall/3findall(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 = _7489Z = [charlotte,caroline,laura,rose]
  • 18. 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).That is, we are asking for a list Z containing all the values of fromMartha(X) that satisfy the goal descend(martha,X).
  • 19. Prolog will respondX = _7616Z = [fromMartha(charlotte),fromMartha(caroline),fromMartha(laura),fromMartha(rose)]
  • 20. bagof/3suppose we pose the queryfindall(Child,descend(Mother,Child),List).We get the responseChild = _6947Mother = _6951List = [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 querybagof(Child,descend(Mother,Child),List). we get the responseChild = _7736Mother = carolineList = [laura,rose] ;Child = _7736Mother = charlotteList = [caroline,laura,rose] ;Child = _7736Mother = lauraList = [rose] ;Child = _7736Mother = marthaList = [charlotte,caroline,laura,rose] ;no
  • 22. bagof/3If 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 = _7870Mother = _7874List = [charlotte,caroline,laura,rose,caroline,laura,rose,laura,rose,rose]
  • 23. setof/3In setof/3 predicate, the lists contained are ordered and contain no redundancies.Ex:suppose we have the following databaseage(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 = _8443Y = _8448Out = [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 tutorialsPick 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