
Introduction to Datalog - x775
https://x775.net/2019/03/18/Introduction-to-Datalog.html
======
JoelMcCracken
Can anyone recommend any implementation of Datalog (+ negation) that is not
datomic?

I haven't tried datascript, which appears to support negation. Maybe I will
try that if/when I revisit this interest someday.

~~~
x775
Hi Joel.

You can give [http://www.dlvsystem.com/dlv/](http://www.dlvsystem.com/dlv/) a
shot!

Alternatively, if you prefer open-source solutions, check
[https://abcdatalog.seas.harvard.edu/](https://abcdatalog.seas.harvard.edu/).

Due to a number of complications on my machine, I used DLV.

~~~
JoelMcCracken
Ah, I usually automatically pass on closed source solutions (hence my dislike
for datomic).

TY for the link to abcdatalog though!

------
hombre_fatal
Pretty cool deep-dive on Datalog.

The interactive tutorials on
[http://www.learndatalogtoday.org](http://www.learndatalogtoday.org)
(Datomic's dialect) quickly sold me on the idea.

Though coming from Datomic, I'm curious how much of my knowledge is Datomic-
specific rather than how you'd generally approach a database queryable with
Datalog. For example, do you need four indexes like Datomic
([https://docs.datomic.com/on-prem/indexes.html](https://docs.datomic.com/on-
prem/indexes.html)) to make Datalog queries fast?

~~~
nutjob2
If you have a lot of facts then you need indexes, otherwise you're scanning a
lot of irrelevant data, many times over.

~~~
hombre_fatal
Sure, was just curious where Datomic's EAV, AEV, AVE, VAE indexes fall between
Datomic indexing impl detail and general Datalog indexing solution.

Datalog is fascinating, but the blog post makes me curious about more concrete
impl-related follow-up questions.

------
sdbrady
Thanks for sharing. There is one very significant conceptual error early on,
however, and it is captured first in this statement: "The :- means if and only
if, or iff". `:-` means if - or more precisely represents material conditional
- where the consequent is on the left and the antecedent is on the right. iff
is logical biconditional.

~~~
x775
Hi Stephen. You are absolutely right, thank you for the feedback! I have
edited accordingly.

~~~
maweki
I always remind myself that it's not an if-and-only-if with this argument:
since there could always be another rule that is satisfied to make that fact
true, it can't be iff.

------
nmadden
Great post! Still working through it, but there is a slight error in the
nested diagram at the start. Relational algebra has set difference, which is
akin to negation-as-failure, but it lacks recursion. So the positive Datalog
and RA circles should overlap without either containing the other. See
[http://www.lifl.fr/%7Ekuttler/elfe/biblio/datalog-
overview-g...](http://www.lifl.fr/%7Ekuttler/elfe/biblio/datalog-overview-
gottlob.pdf)

~~~
x775
Hi there. Thanks so much for your feedback, and good catch! I will update the
diagram accordingly.

~~~
x775
I can no longer edit the parent, but this has been updated. Thanks again!

------
burakemir
Nice post. Still, I find the most accessible article describing datalog is
"What you Always Wanted to Know About Datalog (And Never Dared to Ask)." by
Ceri, Gottlob, Tanca (1989)

~~~
x775
Thanks for your feedback!

For those interested in the mentioned paper, see:
[https://www.utdallas.edu/~gupta/courses/acl/papers/datalog-p...](https://www.utdallas.edu/~gupta/courses/acl/papers/datalog-
paper.pdf)

------
bobjordan
Sharing an interesting implementation in python which I stumbled upon
yesterday. Repo:
[https://github.com/pcarbonn/pyDatalog](https://github.com/pcarbonn/pyDatalog)
Tutorial: [https://sites.google.com/site/pydatalog/Online-datalog-
tutor...](https://sites.google.com/site/pydatalog/Online-datalog-tutorial)

------
radomir_cernoch
> The :- means if and only if, or iff.

Is it really the case?

    
    
      Human("Socrates").
      Animal("Turtle").
      Mortal(x) :- Human(x).
      Mortal(x) :- Animal(x).
    

Suppose :- means iff. Turtle is Mortal (lines 2+4, implication to the left).
Because Turtle is Mortal, it must be a Human (line 3, implication to the
right).

Is it really valid according to Datalog semantics?

~~~
x775
Hi! Thank you for highlighting this.

No, you and sdbrady who commented above are correct; the :- only means "if". I
have edited accordingly and apologise for the misunderstanding!

------
ComNik
If model-theoretic semantics and the various ways to slice, dice, and extend
Datalog are interesting to you, then almost any talk by Peter Alvaro might be
as well.

In particular:
[https://www.youtube.com/watch?v=R2Aa4PivG0g](https://www.youtube.com/watch?v=R2Aa4PivG0g)

------
fspeech
Can you specify symmetric and transitive closure in Datalog?

~~~
brian_cloutier
Transitive closure is the first thing nearly every introduction to datalog (or
Prolog, for that matter) will show you. All you had to do was click the link
and scroll down:

    
    
        Edge("a", "b").
        Edge("b", "c").
    
        Path(x, y) :-
            Edge(x, y).
    
        Path(x, z) :-
            Path(x, y),
            Edge(y, z).
    

Symmetric closure, assuming I'm understanding correctly, is also trivial:

    
    
        SymmetricEdge(Left, Right) :-
            Edge(Left, Right).
        SymmetricEdge(Left, Right) :-
            Edge(Right, Left).

~~~
fspeech
I was wondering about termination. With finite ground facts that appears not
to be an issue. Complexity is another matter.

~~~
JadeNB
Termination is achieved in brian_cloutier's example
([https://news.ycombinator.com/item?id=19423320](https://news.ycombinator.com/item?id=19423320))
by the use of different clauses (e.g., `Path` vs. `Edge`), so that all
recursion is productive.

~~~
maweki
Datalog always terminates because there is no way to derive new atoms.

~~~
brian_cloutier
Yes, but there's more to it than just that. It's possible to write Prolog
programs which never derive new atoms yet still fail to terminate.

It might be more accurate to say that Datalog programs cannot derive new atoms
and this allows Datalog interpreters to use a search strategy which is
guaranteed to terminate.

EDIT: Thinking about this more, I'm not sure why Prolog couldn't also use
breadth-first search. So maybe both are necessary: Datalog not only disallows
creating new atoms, but it also has a better search strategy; the combination
of the two results in guaranteed termination.

~~~
maweki
Datalog does not have a search strategy per se. The general "strategy" (or
definition) is the fixpoint of the T_p operator that derives all one-step-
derivable facts from the facts and the rules.

The minimal model (result of derivation) is defined as the fixpoint of this
operation. And the minimal model is finite because of the fixed number of
atoms.

Every other evaluation strategy must be equivalent to this. Both in terms of
termination and minimal model.

Prolog could use breadth first instead of SLD (and there are other Prolog
evaluation strategies that, for example, use tabling for derived facts) but
then you might get an infinite number of backtracking points instead of terms
of infinite depth. You haven't really won anything by doing that.

------
slifin
Thank you, bookmarking this for later

