12

What are the similarities and differences in terms of the fundamental concepts and implementation between a relational database language sql and a logic programming language such as prolog and clojure's core.logic? Are the two interchangeable?

Alex Miller
  • 69,183
  • 25
  • 122
  • 167
zcaudate
  • 13,998
  • 7
  • 64
  • 124

3 Answers3

6

Similarities are captured by Datalog query language. Here is motivation and better explanation of connection between logic and databases. This excerpt should address your question:

Nevertheless, coupling Prolog and relational databases show some dissonances. Facts and rules in Prolog are organized in a total order and the semantics of a Prolog program depends on this order. In contrast, relations in a database are considered as unordered sets of tuples and the result of a query is independent from any physical order. The processing of Prolog programs is tuple oriented while relational databases are set oriented. Prolog offers procedural features like the cut predicate to allow the programmer to control the inference process. The order of evaluation of a Prolog program is pre-determined, whereas expressions in relational calculus are purely declarative and the actual evaluation is left to a query processor which may rearrange the query for optimization purposes. Optimization of queries was crucial for the success of relational databases. The procedural nature of the Prolog engine leaves the burden of optimization with the programmer.

StasM
  • 10,593
  • 6
  • 56
  • 103
Tegiri Nenashi
  • 3,066
  • 2
  • 19
  • 20
5

One important difference is that SQL is only Turing complete with some pretty crazy tricks which were not possible until ANSI SQL 99. Prolog is Turing complete and is therefore a general-purpose programming language.

Community
  • 1
  • 1
John Watts
  • 8,717
  • 1
  • 31
  • 35
4

Although SQL and Prolog both demonstrate first-order logic concepts, neither is a complete implementation of the predicate calculus.

Prolog and other logic programming languages are heavily dependent on recursion, both for definition of data structures as well as for predicates.

SQL per se does not allow recursion, and the introduction of stored procedures has been done with limitations on the depth of nesting of such calls. Eg. SQL Server 2000 thru 2012 allow at most 32 nested calls.

In relational databases the "relations" are reified as tables (or more flexibly, as views). The most similar aspect of Prolog is dynamic factbases, which in some implementations (SWI, Amzi) allow indexing for performance, very similar to indexing of relational tables for performance in SQL.

Although SQL RDBMS are designed to efficiently work with much large sets of data than a Prolog implementation typically needs, Prolog can at least be used to prototype both the database and process aspects of a system design.

See here for a 2005 thesis that explores extending relational databases with Prolog inference.

hardmath
  • 8,753
  • 2
  • 37
  • 65
  • 1
    What do you mean with 'process aspects of a system design' – rtacconi Oct 14 '19 at 11:41
  • @rtacconi: To be brief, I mean the Data Flow Diagrams (DFD) of structured systems analysis. See this 1995 outline of [A novel course in structured systems analysis through Prolog](https://www.witpress.com/Secure/elibrary/papers/SEHE95/SEHE95020FU.pdf) and its references, esp. the 1989 book by Terry Goble, *Structured Systems Analysis through Prolog*, – hardmath Oct 14 '19 at 13:02