I am quite new with Programming in Logic, but it seems to be a great language for querying a data store and storing facts. In many ways it is so similar to SQL, and in many cases, I am sure, it would be much more convenient. Read Logic Programming with Perl and Prolog if you want an introduction to Prolog with some Perl modules or Other online Prolog tutorials to learn more about the power of programming in logic. So why there are no databases built to support Prolog? Is it too difficult?

At least with Perl you can dump your database as a Prolog program:

use strict; use DBIx::Class::Loader; use AI::Prolog; my $loader = DBIx::Class::Loader->new( dsn => $ARGV[0], user => $ARGV[1], password => $ARGV[2], namespace => 'Data', ); my $prog; for my $c ( $loader->classes ) { my $rs = $c->search(); $prog .= '% ' . join ( ',', $c->columns ) . "\n"; while ( my $row = $rs->next ){ $prog .= $c->table . '('; my @quoted = map {AI::Prolog->quote($row->$_)} $c->columns; $prog .= join ',', @quoted; $prog .= ").\n"; } } print $prog;
Let's see what we can get with this from a simple database like this:
create table usr(id integer primary key, name varchar(100), cityid int +eger); create table city (id integer primary key, name varchar(100)); insert into usr values (1, 'John', 1); insert into usr values (2, 'Marry', 1); insert into usr values (3, 'Eva', 2); insert into usr values (4, 'Zby', 3); insert into city values (1, 'New York'); insert into city values (2, 'London'); insert into city values (3, 'Warsaw');
Run the dumper:
perl dump.pl dbi:Pg:dbname=prolog > mytables
What I get in the mytables file is:
% name,id city('New York',1). city('London',2). city('Warsaw',3). % cityid,name,id usr(1,'John',1). usr(1,'Marry',2). usr(2,'Eva',3). usr(3,'Zby',4).
The sequence of columns is not retained by DBIx::Class::Loader - that's why I added the column names in the Prolog comments (lines starting with '%'), this will be usefull for creating the queries. That's our logic database. Now let's query it. I'll run the full SWI prolog engine here and load the above program, I do this because I cannot get AI::Prolog to work with compound queries that I need.
zby@zby:~/progs/prologdata$ swipl Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13) Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- [mytables]. % mytables compiled 0.00 sec, 1,400 bytes Yes ?-
Now the database is loaded, and we can query it. How about a table join? Let's look for all users that are from 'New York'. Now I need the comments that tell me the sequence of columns.
?- usr(Cityid, Name, Id), city('New York', Cityid). Cityid = 1 Name = 'John' Id = 1 ; Cityid = 1 Name = 'Marry' Id = 2 ; No ?-
Great, it found both.

And here is some code you can add to the first program to ask the query at once without saving the intermediate Prolog program. Unfortunately I did not found any way how to do conjunction in the queries (needed for table joins here).

my $prologDB = AI::Prolog->new( $prog ); $prologDB->query( "usr(1,Name,Uid)." ); while (my $results = $prologDB->results) { print "@$results\n"; }

For sure there are other ways how to convert relational databases to logic, I hope other monks here will share their ideas on how to do that and how to use it.


In reply to Query database in Prolog by zby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.