tags:

views:

454

answers:

6

Hi,

I've started learning Prolog and wondering about theoritical difference from SQL language. For example they are both declarative paradigma languages, both support fact-driven knowledge database, both support question-styled data-retrieving, both support functional dependencies. So does anybody have an idea?

Tnx in advance.

A: 

I think the main difference is that Prolog is a query language used for matching complicated patterns against a database of simple facts. SQL on the other hand is limited to relational databases.

Iris
Are you saying SQL is more limited than Prolog -- I'm not sure that comparison even makes sense.
Hogan
No, I wasn't making a comparison, I was saying that SQL is only used to query relational logic.
Iris
A: 

There are many differences which I think become clear when you start using them. Remember because of changes in terminology, somethings which are called the same thing in the past mean very different things now.

Very broad overview of difference.

SQL statements work against a relational database and query (ask for) data from that database, changes to that data and the results are exactly expressed in the language, whereas in Prolog you define facts and a logic engine generates new facts based off of the existing facts. New data (facts) are created via evaluation.

They both use something called queries (but they work totally differently) and they both have data (but use it differently.)

The use cases for SQL and Prolog are also totally different. It would never make sense to store an address list in Prolog whereas that is exactly what SQL was designed to do.

Simply put SQL is used to access a data store and Prolog is an expression evaluator.

Hogan
A: 

Just a few thoughts:

  1. SQL is a query language for (maybe arbitrary complex) accessing to (relational) data, it's not a programming language.
  2. Even if treat SQL as programming language - it's not Turing complete. I can hardly imagine sql query returning sum of 1 to 100 (for example).
  3. SQL is language for accessing/manipulating (DML) to data bases, where Prolog is a language for working with knowledge bases (& rule resolution engine, of cause). Virtually Prolog is no more then simply unification & backtracking.
  4. This is not saying about the application domains of SQL & Prolog, which are of cause totally different - efficient (regular) data storage & AI/symbolic computations/parsing/expert systems/constraint solvers/.../much more.
Xonix
SQL *is* a programming language, its just not a GP (general-purpose) programming language. As of ANSI SQL 9x, it *is* turing-complete, though that's largely irrelevant.
RBarryYoung
FYI, this is perfectly correct SQL: SELECT SUM(value) FROM Integers WHERE Value BETWEEN 1 AND 100
RBarryYoung
Well, I had in mind a case when you are not using any table (additional data). Like follows in prolog: sum1(From, From, S, S) :-!. sum1(From, To, S0, S) :- S1 is S0 + From, From1 is From + 1, sum1(From1, To, S1, S). ?- sum1(1, 101, 0, R). R = 5050.I bet this not possible with an SQL. Otherwise consider the case of finding N's Fibonacci number. Will a table help you? Btw, please provide an url, stating that ANSI SQL is Turing complete. I used to know that relational calculus is not Turing complete by design, though some implementations can be..
Xonix
Tables are inherent to a table-based paradigm like SQL. And yes, I can do your Peano's inferencing trick in SQL, but the point is that you do NOT have to. And you would also be wrong about the Fibonacci series in SQL. Just at the crudest level: ANSI/ISO SQL has had recursion since ISO 9x which means that it is fully Turing-Complete (because recursion and iteration are the same in programming theory), which means it can compute anything that any other language can compute. And SQL is a superset of Relational Calculus, so you can not make that logical leap.
RBarryYoung
Are you saying of stored procedures/functions?If yes, then I agree on that, but to me this is another case.Cause, comparing Prolog to stored procedure language (be it pgplsql/sql server dialect/whatever) is not far from comparing Prolog, say, to PHP or Java. Because, technically, stored procedures are not essential to SQL, it's just a metter of optimization (moving application logic closer to data).
Xonix
@Xonix: no, he is speaking about recursive extension to SQL properly, specifically SELECT statement's WITH clause. See, for example: *Understanding the WITH Clause*, by Jonathan Gennick (http://www.gennick.com/with.html).
MaD70
+11  A: 

Most of the incorrect answers here are a reflection of the fact that most people do not know what SQL is (its an implementation of Relational Calculus) or what that means (that it's a form of Predicate Logic). The following statements are true of both Prolog and SQL:

  • they are both logic-driven
  • they can both store, express and use relations (logical relationships in Prolog)
  • they can both store and express complex logical conditions
  • they both have facts(data in SQL) and can derive conclusions from those facts
  • they both have queries, that do in fact mean the same thing
  • they both have data(facts in Prolog) and use them similarly
  • they are both programming languages
  • they are both turing-complete (though it is somewhat difficult to access this in both of them)
  • etc, etc..

Generally, people are not aware of these equivalences between them:

  1. "Facts" and "Data" are the same thing. This is straight out of Codd's original paper.
  2. A "Relation" in Relational Theory is the same thing as a "Table" in SQL, is the same thing as a Relation or relational function in Predicate Logic and is the same thing as a tuple-set in Set Theory
  3. An aliased table-expression (i.e., a View, etc.) in SQL is the same thing as a Rule in Prolog.

So what are their differences? Although they operate across the same conceptual domains, their focuses are in completely different directions. In Prolog terms, SQL is primarily a Fact and Relation(set) engine, whereas Prolog is primarily a Rules and Inferencing engine. Each can do the other, to a limited extent, but it becomes increasingly difficult with even small increases in complexity. For instance, you can do inferencing in SQL, but it is almost entirely manual in nature and not at all like the automatic forward-inferencing of Prolog. And yes, you can store data(facts) in Prolog, but it is not at all designed for the "storage, retrieval, projection and reduction of Trillions of rows with thousands of simultaneous users" that SQL is.

Plus, SQL is primarily a Server-language paradigm, whereas Prolog is primarily a Client-language paradigm.

RBarryYoung
hmm.. while your last paragraph makes sense to me (says basically what I said) the points list does not. As far as I can tell all of those points can be said about any (general purpose) programming language. What makes them different and useful is their "focus".
Hogan
+1. Worth a mention Datalog as the "missing link" between Prolog and Relational Calculi (and then SQL).
MaD70
For a practical application of such "convertibility" see my answer to "Prolog to SQL converter" (http://stackoverflow.com/questions/724377/prolog-to-sql-converter/1682486#1682486) where I list some programs for such conversion.
MaD70
@Hogan: my impression is that you misread his points.
MaD70
And you have not metioned the wonders of the logical variable in prolog and all the fantastic things you can do with it.
rvirding
@MaD70: How so, since I can implement Prolog in C++ (or C as I did in school), then all of these points can be said about C++ too; however for the purpose of comparing languages and when you would use one or the other of them, what their primary function is, it is not useful. @RBarryYoung even said "Each can do the other, to a limited extent but it becomes increasingly difficult..." This is a clear indication to me that languages are fundamentally different and designed for different tasks. Finding technical similarities is not useful and would not help OP and question.
Hogan
@MaD70 (cont): This answer is basically a mathematics hat trick. While smart and clever it is not getting at the heart of the matter which is to explain how the two languages are different and from a design point of view.
Hogan
@Hogan: no, invoking Turing-equivalence is pointless. SQL and Prolog are both declarative programming languages with a semantics based on logical inferences. Logical equivalences are established between (a relevant subset of) SQL->(Safe) Relational Calculi->Relational Algebra->Datalog->Prolog.
MaD70
@Hogan: if you call "mathematics hat trick" logical equivalence of semantics we have no common base for a fruitful discussion, sorry.
MaD70
@Hogan: also note what the OP wrote in his question: "I've started learning Prolog and wondering about **theoritical** [sic] difference from SQL language."
MaD70
Hogan: yes, my last (full) paragraph is basically my answer to the Question, the preceding points list (and numbered points) are really a response to other statements here: that is, that these items are not useful ways to distinguish the two. Finally, I basically agree both with your Answer and your comments, though I might differ in areas of specificity, scope and wording of our answers.
RBarryYoung
ALL: Generally I think that giving a useful answer to a question like "what's the difference between these two languages" for someone unfamiliar with both is actually very difficult and most responses tend to make the mistakes of being either 1) overly narrow and rigid in their assumptions and descriptions of languages, effectively "type-casting", or 2) impractically vague and nebulous, or 3) being overly technical in the "Formal Language Theory" sense, which is rarely of any use to someone new to a language (just try and get a good description of "Functional Languages" sometime).
RBarryYoung
@RBarryYoung: so you think that Serhiy Ryabokon, a CS student, is using the term "theoretical" in a loose sense? This is not my impression.
MaD70
@RBarryYoung: Excellent comments. I will admit I tend to view things less in the theoretical and more in the applied point of view. @MaD70 point is well taken in this case since the OP did ask for theoretical. Mea Culpa
Hogan
MaD70: well, I believe that he is probably using it in what would be commonly thought of as the *informal* sense. I think this because it is very unusual for an actual Theoretician to use the word "theoretical" to mean what non-Theoreticians think of as the formal sense. Theoreticians have more precise words for this like "Formal", "Grammar", "Chomsky Hierarchy", etc. To them using "theoretical" in a question like this would be seem as hopelessly vague and would lead to questions like "Exactly which theory are you alluding to?"
RBarryYoung
mad70: Of course you may be right, the OP can set either of us straight whenever he chooses. :-)
RBarryYoung
@RBarryYoung: of course we don't mind-reading! :-) But I have supposed that he is simply a confused student, not a theoretician. Sometimes (often?) educators doesn't state some facts explicitly when they think are obvious.
MaD70
A: 

This will get you started: Programming in Prolog

Very early on, you can read this:

Prolog, as a programming language, is a little unusual. It can be understood as a standard procedural language with two unusual properties. It is a procedural language like Pascal or Algol. One programs in a procedural language by writing procedures that carry out particular operations. One specifies what a procedure is to do by using primitive statements and by invoking other procedures. Prolog procedures have only local variables and all the information that a procedure can use or produce must be passed through its arguments.

C can be viewed as a procedural language by thinking of using it without functions; i.e., all functions return void, and information is passed to and from functions through their arguments only.

In Prolog, procedures are called predicates. The two unusual aspects of Prolog are:

1. Prolog has assign-once variables, and 2. Prolog is nondeterministic.

Enjoy.

Trevoke
This doesn't answer the question.
Kaarel
+8  A: 

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:

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 to 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).

MaD70
+1 Excellent overview of some of the theoretical issues at hand.
Hogan
Thanks Hogan. Hoping to not have introduced some imprecision...
MaD70