What is the relationship between CLP and SQL?

While reading the constraint is logical programming , I cannot help but notice the obvious connection with SQL programming. Is SQL an example of "constraint logic programming" in action?

+4
source share
2 answers

They are very similar from a high level. Both are declarative or set based, not iterative (which means that you are asking for what you want - you are not looping and not processing individual elements one by one).

CLP can be modeled in SQL, but this is a case of finding a better solution to the problem. SQL is good for finding answers in a specific dataset that is already explicitly defined. CLP is suitable for finding answers in domains that are free or even not fully defined.

As an example. If I wanted to return all the even numbers from 1 to 10 million using SQL, I would need a table with all the numbers indicated for selection from (existing dataset). Using CLP, I just need a binding (10M), but I will not need to create all the records.

CLP internal engines (CSP problems) can also cause restrictions to make them faster. SQL, you will need to figure out these rules and clearly indicate them. For example, if your SQL is where A = B and B = C, then the CLP mechanism can determine what A = C is and use it to speed things up where there is no SQL. They can also call domains, as well as optimize runtimes (if I know that even numbers never return, the CLP will throw them out of consideration - SQL will still consider them, but will not return them as a "solution").

Hope this helps - I can get more technical if you need it. The key point to remember is that the two are similar, but which ones you use depends on the model of the problem. If you can say, “these are my variables, and this is how they are interconnected, give me some right solution,” then CLP is a good candidate. If you can say "there are my variables, and this is specific, give me back a subset of my existing data that is suitable," then SQL is the best candidate.

+5
source

I don't have a complete answer, but you might find it interesting to watch Datalog and DLV. This is possibly the “missing link” between logical programming (but not CLP) and SQL, which will help you understand the situation more clearly.

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

0
source

Source: https://habr.com/ru/post/1302989/


All Articles