3 ms·
I guess the appeal of Datalog is the recursion that can be expressed in rules: has_descendant(?ancestor, ?descendant) :- has_child(?ancestor, ?chil
by hibbelig 6y ago
I guess the appeal of Datalog is the recursion that can be expressed in rules:
has_descendant(?ancestor, ?descendant) :-
has_child(?ancestor, ?child),
has_descendant(?child, ?descendant).
Here, the assumption is that you have explicit `has_child` facts (expressing vertices in a graph, essentially), and the above rule gives you paths of arbitrary length.
In SQL, given a table has_child(parent, child), it is not clear to me how you can get all descendants of a given person, or all ancestors.
Other people talk about recursive extensions to SQL, maybe that provides a way.
- HelloNurse 6y agoThe normal SQL way is with a recursive CTE and help from the query optimizer. Differs more in feeling (constructing relations with joins vs. defining predicates with logic) than in substance from the Datalog way. Sqlite official examples: https://sqlite.org/lang_with.html https://sqlite.org/lang_with.html MySql official examples: https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html
- quelltext 6y agoDepends on the specific SQL flavor, e.g. https://www.postgresql.org/docs/9.1/queries-with.html https://www.postgresql.org/docs/9.1/queries-with.html Logica instead seems to create tables and drop tables? https://github.com/EvgSkv/logica/blob/main/examples/Logica_example_Reachability.ipynb https://github.com/EvgSkv/logica/blob/main/examples/Logica_e... It looks like Logica code isn't actually translated to a (large) SQL query, but Logica code is dynamically interpreted by some interpreter that calls into a SQL database as the virtual machine. Not sure what Logica really provides here. Datalog usually comes with elaborate techniques to make sure only what's really needed is calculated instead of just generating all values every fact generation "iteration".
- quelltext 6y ago> It looks like Logica code isn't actually translated to a (large) SQL query, but Logica code is dynamically interpreted by some interpreter that calls into a SQL database as the virtual machine. Actually, I might have jumped to a wrong conclusion. I really don't know if that's what happens here, but if not then it would imply that recursion isn't actually driven by the evaluation mechanism, but by Logica creating enough tables for a given program to ensure that iterative evaluation of rules ends up with a fixed point result set. Not quite sure. None of the other examples show recursion in the SQL output itself either. Well, at any rate I'd like to learn more about the actual mechanism here but they are a bit light on documentation.