views:

51

answers:

2

In reading up on constraint-logic programming, I can't help but notice an apparent relationship with SQL programming. Is SQL an example of "constraint logic programming" in action?

A: 

i don't have a full answer, but you might find it interesting to look at Datalog and DLV. They are perhaps the "missing link" between Logic Programming (but not CLP) and SQL that will help you understand things more clearly.

Since this is more about LP than CLP I may have missed something - if so, sorry.

andrew cooke
+2  A: 

They are very similar from a high level. Both are declarative or set based rather than iterative (meaning you ask for what you want - you don't loop through and process individual items one by one).

CLP can be modeled in SQL but it is a case of finding a better solution to a problem. SQL is good for finding answers in a given data set that is already explicitly defined. CLP is good for finding answers in domains that are loosely or sometimes even not even completely defined.

As an example. If I wanted to to return all the even numbers between 1 and 10 million using SQL I would need a table with all the numbers listed to select from (an existing data set). Using CLP I would just need a bound (10M) but wouldn't have to explicit create all the records.

Internally CLP engines (CSP problems) can also infer constraints to make them faster. SQL you would have to figure out those rules and explicitly state them. For example if your SQL is where A=B and B=C, a CLP engine might figure out that A=C and use that to run faster where SQL would not. They can similarly infer domains as well to optimize run time (if I know that the even numbers are never returned a CLP will throw them out from consideration - SQL will still consider them but not return as a "solution").

Hopefully that helps - I can get more technical if you need it. The key point to remember is the two are similar but which you use depends on the problem model. If you can say "these are my variables, and these are how the interrelate, give me any valid solution" then CLP is a good candidate. If you can say "there are my variables, and these are the specifics, give me back a subset of my existing data that fits" then SQL is a better candidate.

ktharsis
Thank you for your answer!
bobobobo