4

After doing some research, I was amazed with the power of Prolog to express queries in a very simple way, almost like telling the machine verbally what to do. This happened because I've become really bored with Propel and PHP at work.

So, I've been wondering if there is a way to translate database table rows (Postgres, for example) into Prolog facts. That way, I could stop using so many boring joins and using ORM, and instead write something like this to get what I want:

mantenedora_ies(ID_MANTENEDORA, ID_IES) :- 
  papel_pessoa(ID_PAPEL_MANTENEDORA, ID_MANTENEDORA, 1),
  papel_pessoa(ID_PAPEL_IES, ID_IES, 6),
  relacionamento_pessoa(_, ID_PAPEL_IES, ID_PAPEL_MANTENEDORA, 3).

To see why I've become bored, look at this post. The code there would be replaced for these simple lines ahead, much easier to read and understand. I'm just curious about that, since it will be impossible to replace things around here.

It would also be cool if something like that was possible to be done in PHP. Does anyone know something like that?

Community
  • 1
  • 1
iffs
  • 73
  • 7
  • 1
    http://en.wikipedia.org/wiki/Datalog – starblue Nov 17 '11 at 19:39
  • Thanks @starblue, the language is also part of the point. However, can it integrate with a real database, turning this last one into its fact database? – iffs Nov 17 '11 at 20:05
  • recent versions of DES (http://www.fdi.ucm.es/profesor/fernan/des/) can access ODBC and offer interoperability between Datalog/SQL – CapelliC Nov 18 '11 at 08:37
  • Thanks @chac. I'll take a look at that on the weekend. – iffs Nov 18 '11 at 13:19

3 Answers3

1

check the ODBC interface of swi-prolog (maybe there is something equivalent for other prolog implementations too)

http://www.swi-prolog.org/pldoc/doc_for?object=section%280,%270%27,swi%28%27/doc/packages/odbc.html%27%29%29

Thanos Tintinidis
  • 5,828
  • 1
  • 20
  • 31
  • Maybe I've misread it, since it was very quick, but is it still needed to write SQL like sentences to query a database, using this ODBC interface? – iffs Nov 17 '11 at 19:21
  • (late answer now xd) indeed, you need to construct SQL queries in prolog to interact with the database. I believe that you can write very simple queries, just to transfer the data and let prolog do the 'real' work (or you can write complex queries and have very simple prolog predicates but that's kinda pointless xd). For example, instead of writing SELECT * FROM members WHERE id=foo, write SELECT * FROM members, translate it in prolog and then use pattern matching to pick the right member. – Thanos Tintinidis Nov 17 '11 at 20:26
  • 1
    @ThanosQR: Removing filters from the query it's very inefficient! – CapelliC Nov 17 '11 at 22:41
  • I could, somehow, select only the tables involved in the query. But what about maintaining prolog database and the database itself synchronized? – iffs Nov 17 '11 at 23:14
  • @iffs updating the prolog database could be quite tricky. an easy solution is to use dynamic predicates, assert/retract but that has a performance cost. what I had in mind was to copy all data, perform whatever calculations you want and then update the DB (and dont store any data in prolog). Maybe you could have a small buffer but I do not know if it would be efficient or reliable – Thanos Tintinidis Nov 18 '11 at 06:59
  • @chac indeed, especially in this example it's very inefficient. – Thanos Tintinidis Nov 18 '11 at 06:59
1

There is the Draxler Prolog to SQL compiler, that translates some pattern (like the conjunction you wrote) into the more verbose SQL joins. You can find in the related post (prolog to SQL converter) more info.

But beware that Prolog has its weakness too, especially regarding aggregates. Without a library, getting sums, counts and the like is not very easy. And such libraries aren't so common, and easy to use.

I think you could try to specialize the PHP DB interface for equijoins, using the builtin features that allows to shorten the query text (when this results in more readable code). Working in SWI-Prolog / ODBC, where (like in PHP) you need to compose SQL, I effettively found myself working that way, to handle something very similar to what you have shown in the other post.

Another approach I found useful: I wrote a parser for the subset of SQL used by MySQL backup interface (PHPMyAdmin, really). So routinely I dump locally my CMS' DB, load it memory, apply whathever duty task I need, computing and writing (or applying) the insert/update/delete statements, then upload these. This can be done due to the limited size of the DB, that fits in memory. I've developed and now I'm mantaining this small e-commerce with this naive approach.

Writing Prolog from PHP should be not too much difficult: I'd try to modify an existing interface, like the awesome Adminer, that already offers a choice among basic serialization formats.

Community
  • 1
  • 1
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Thanks a lot @chac. It's an interesting idea, but I'd like to avoid conversion to SQL. In other words, what I wanted is to abstract a database into Prolog database. I've made that query simply writing some facts (table rows) into a file, and that's what I'd like to automate. – iffs Nov 17 '11 at 19:50
1

I can think of a few approaches to this -

  1. On initialization, call a method that performs a selects all data from a table and asserts it into the db. Do this for each db. You will need to declare the shape of each row as :- dynamic ies_row/4 etc

  2. You could modify load_files by overriding user:prolog_load_files. From this activity you could so something similar to #1. This has the benefit of looking like a load_files call. http://www.swi-prolog.org/pldoc/man?predicate=prolog_load_file%2F2 ... This documentation mentions library(http_load), but I cannot find this anywhere (I was interested in this recently)!

DaveEdelstein
  • 1,256
  • 8
  • 14
  • Thanks @DaveEdelstein. About these approaches: 1) I could, somehow, select only the tables involved in the query. But what about maintaining prolog database and the database itself synchronized? 2) I still have to look for this doc, but is it about reading the database file? – iffs Nov 17 '11 at 23:14