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.

This is due to the limited grammar which AI::Prolog supports. Changing your query to a rule will permit this. Add the following rule to the mytables file:

city_usr(City, Name, Id) :- city(City, City_ID), usr(City_ID, Name, Id).

Then you can run the shell:

AI-NeuralNet-Simple-0.03 $ aiprolog mytables.pro Welcome to AI::Prolog v 0.732 Copyright (c) 2005, Curtis "Ovid" Poe. AI::Prolog comes with ABSOLUTELY NO WARRANTY. This library is free so +ftware; you can redistribute it and/or modify it under the same terms as Perl +itself. Type '?' for help. ?- listing. 1. city_usr/3: city_usr(A, B, C) :- city(A, D), usr(D, B, C). 2. usr/3: usr(1, John, 1). usr(1, Marry, 2). usr(2, Eva, 3). usr(3, Zby, 4). 3. city/2: city('New York', 1). city(London, 2). city(Warsaw, 3). listing Yes ?- city_usr('New York', User, ID). city_usr('New York', John, 1) ; city_usr('New York', Marry, 2) ; No ?-

There are other techniques, but that's the easiest.

As for why databases don't support Prolog, there are a variety of reasons for that. The primary reason is that DBMS don't really handle the relational model all that well. Queries frequently return bags instead of sets, SQL does not handle recursion well -- a core requirement of Prolog -- and Prolog allows lists, something databases also don't handle well for a single column.

Querying databases is something I would like to have in AI::Prolog, but in a more general nature. However, what if you've retrieved 15 out of 20 results and the engine backtracks over your function? You need to be able to add previous results to the AI::Prolog::KnowledgeBase and fetch them from there, if appropriate. Only if they are not found should subsequent results be pulled from the database.

To handle this, we'd need some way of registering external functions and having them call the correct primitive from AI::Prolog::Engine::Primitives (a primitive for external functions would need to be added). There's also the question of side effects. What happens when you backtrack over a predicate which deletes file? diotalevi and I have discussed much of this, but regrettably, I'm terribly busy at work and don't have time to work on this.

Cheers,
Ovid

New address of my CGI Course.


In reply to Re: Query database in Prolog by Ovid
in thread 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.