Is Prolog Better Suited Than SQL?

I am currently reading a Prolog book “Artificial Intelligence Through Prolog”, I have been doing a bit of Prolog when I was very young and wanted to refresh my memory a bit. It is a very interesting read, especially when I take the viewpoint of our current application where no ACID compliance is required.

It seems to me that all the logic we coded to parametrize SQL queries and construct them dynamically could have been avoided if we had chosen Prolog as Prolog expressions would have been very natural to use in our project. With Prolog, there is no need to think about joins, type of joins, SQL syntax. It is at the level just higher. I wonder very much why Prolog did not become more mainstream as it seems to solve some problems in a much nicer, natural way.
Here is a short example to get reviews of things by user or by user and tags or …:

  1. let’s define some facts:
    • is_tag(tag1, user1, thing_id)
    • is_tag(tag2, user1, thing_id)

    • review(user1, thing_id, description, extended, date)

  2. review for user “user_x”
    • ?review(user_x, THING_ID, DESC, EXT, DATE)
  3. review for user “user_x” with tag “tag_y”
    • ?review(user_x, THING_ID, DESC, EXT, DATE), is_tag(tag_y, user_x, THING_ID)
We could imagine some better ways to lay out information. This is just a first draft.

Now of course, Prolog does not necessary makes sense for us because:
a) We already have it working in SQL
b) SQL is much more used and should therefore be more tunable, stable, etc.

Still the Prolog way of things is interesting and powerful. We could have written a code with a logic near Prolog instead of our custom code.

comments powered by Disqus
Tweet Submit to reddit
© 2006-16 Fabien Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License.