• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PROLOG: Database Manipulation In Prolog
 

PROLOG: Database Manipulation In Prolog

on

  • 8,289 views

PROLOG: Database Manipulation In Prolog

PROLOG: Database Manipulation In Prolog

Statistics

Views

Total Views
8,289
Views on SlideShare
8,263
Embed Views
26

Actions

Likes
1
Downloads
0
Comments
0

4 Embeds 26

http://www.slideshare.net 23
http://translate.googleusercontent.com 1
http://www.dataminingtools.net 1
http://dataminingtools.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    PROLOG: Database Manipulation In Prolog PROLOG: Database Manipulation In Prolog Presentation Transcript

    • Database manipulation in Prolog
    • OVERVIEW
      Database Manipulation
      Collecting solutions
      • findall/3
      • bagof/3
      • setof/3
      Examples
    • Database Manipulation
      Prolog has four database manipulation commands:
      • assert
      • retract
      • asserta
      • assertz
    • assert
      Consider 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)).
      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:
      • findall
      • bagof
      • Setof
      These 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).
      • That is, we are asking for a list Z containing all the values of fromMartha(X) that satisfy the goal descend(martha,X).
      • Prolog will respond
      X = _7616
      Z = [fromMartha(charlotte),fromMartha(caroline),
      fromMartha(laura),fromMartha(rose)]
    • 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.
    • 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
    • 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]
    • 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).
    • 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.
    • 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