PROLOG: Database Manipulation In Prolog


Published on

PROLOG: Database Manipulation In Prolog

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PROLOG: Database Manipulation In Prolog

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