17

Without thinking to much, it seems to me that a large set of Prolog's functionality could be implemented as relational calculus (a.k.a. SQL).

Has anyone heard of any tools to automatically convert Prolog to SQL?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
BCS
  • 75,627
  • 68
  • 187
  • 294

6 Answers6

35

Yes, of course.

A premise for skeptics: any semi-decent book on database theory mentions Datalog (which is Prolog-like) and theorems which demonstrate that is possible to translate it to/from Relational Algebra (RA) (under specific restrictions).

SQL is not faithful to RA or relational calculi, but is enough to support Prolog:

MaD70
  • 4,078
  • 1
  • 25
  • 20
4

Recommending:

https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/code/io/pl2sql/0.html

my advice- use Eclipse prolog (http://www.eclipseclp.org/) (not (here) to be confused with prolog in the eclipse IDE).

I spent hours trying to get the code to compile in 4 other prologs(!) and 4 minutes to compile in eclipse.

When it works, it's a thing of beauty.

Credit to Herr Draxler of course

2

The mapping isn't very good. SQL, for example, doesn't do backtracking, unification, lists, or adhoc nested structures.

Prolog doesn't deal well with composite objects, indexes, etc.

I'd say it's a no-go.

MarkusQ
  • 21,814
  • 3
  • 56
  • 68
  • I don't think you would need backtracking or unification as thing would be generated from the other end (with joins) OTOH this would kill all arbitrary depth recursion – BCS Apr 07 '09 at 15:43
  • 3
    Without backtracking, unification, recursion, and lists there isn't a whole lot left of prolog. And you can't generate the same class of things with joins, unless you're thinking of writing self modifying SQL (queries that build queries) which I suppose is _possible_ but... – MarkusQ Apr 07 '09 at 16:07
1

I wrote a translator that converts a subset of Prolog into SQL user-defined functions.

This predicate in Prolog can be translated into SQL:

is_between(A,B,C) :- 
    A<B,B<C.

This is the translator's output as a MySQL function:

CREATE FUNCTION is_between(A double,B double,C double) RETURNS BIT BEGIN 
    RETURN A>B and B>C;
END

Similarly, there is a Prolog-to-SQL compiler compiler for SWI-Prolog, and another translator that converts a non-recursive subset of Datalog into SQL.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
1

It makes more sense to do an sql query from prolog, which can then be translated into prolog facts. e.g. Prolog ODBC Library

This removes all restrictions and keeps the two languages separated into their proper places.

0

CQL, a SWI Prolog add-on pack, compiles Prolog, or rather a Prolog DSL, to SQL:

https://www.swi-prolog.org/pldoc/man?section=cql-examples

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111