Comparing SQL and Prolog

后端 未结 8 648
深忆病人
深忆病人 2021-01-29 18:34

I\'ve started learning Prolog and wondering about the theoretical difference from the SQL language.

For example:

  • both are declarative languages
  • b
8条回答
  •  孤独总比滥情好
    2021-01-29 18:57

    You are correct: Prolog and SQL are theoretically related (you asked specifically about theoretical differences).

    I want to complement RBarryYoung's answer, giving you some hints to understand the connection, so that you have a starting point to study and understand the technicalities.

    Prolog and SQL share a core: every query expressible in a subset of Prolog can be expressed in a subset of SQL and viceversa, i.e. these subsets are logically equivalent.

    To understand how this can be true, you need to examine on what theoretical underpinnings both Prolog and SQL are based:

    • SQL1 is a mix of different parts, not always well integrated, apparently from both Relational Algebra (RA) and Tuple Relational Calculus (TRC)2 and other parts, not related to logic (i.e. SUM, AVG operators, ORDER BY, and so on).
    • RA is equivalent in expressive power to safe (domain-independent) TRC (this is known as Codd's theorem).
    • RA is equivalent in expressive power to safe Datalog without recursion and with (stratified) negation.
    • Datalog can be considered a "loose subset" of Prolog3; a "loose subset" in the sense that there are complications w.r.t. the operational semantics of Prolog: "..ordering of the clauses is irrelevant in Datalog in contrast to Prolog which depends on the ordering of clauses for computing the result of the query call" (citing from here).

    Of course something out of these subsets needs more translation effort.

    Nonetheless, I think the claim that equivalence in expressive power of the two subsets is more than an appeal to Turing equivalence4 when considering Prolog-to-SQL translation.

    Notes:

    1) Unfortunately SQL can be used in contrast to RDBMS theoretical foundations (relational algebra-calculi); for example, SQL tables are not necessarily relations - as per RA - i.e. they can be without a (primary) key, so duplicate rows are permitted. Such tables are not sets but multisets (aka bags) of tuples. In such context, all theoretical results for RA, where relations are sets, are not necessarily valid.

    2) For a translation from SQL to TRC, see A note on the translation of SQL to tuple calculus, also here (postscript paper).

    3) For more on the differences between Datalog and Prolog see What You Always Wanted to Know About Datalog (And Never Dared to Ask) (pdf paper - links directly to page 6, heading H. Datalog and Prolog).

    4) For the record: RA (and so their equivalents safe TRC and safe Datalog w/o recursion) is not Turing complete on purpose, to avoid never ending queries.

    Historical note: Prolog and Codd's Relational Algebra were conceived around the same time (end of '60s early '70s) in different contexts - Colmerauer conceived Prolog for natural language processing and Codd conceived RA as a theoretical foundation of Relational DBMS. So, any theoretical connection between Prolog-Datalog-RA-SQL was necessarily established a posteriori and is implicit in the fact that they are all based on first-order predicate calculus (aka first order logic).

提交回复
热议问题