top | item 44273430

(no title)

philzook | 8 months ago

Nice!

I'll note there is a really shallow version of naive datalog I rather like if you're willing to compromise on syntax and nonlinear variable use.

   edge = {(1,2), (2,3)}
   path = set()
   for i in range(10):
       # path(x,y) :- edge(x,y).
       path |= edge
       # path(x,z) :- edge(x,y), path(y,z).
       path |= {(x,z) for x,y in edge for (y1,z) in path if y == y1}

Similarly it's pretty easy to hand write SQL in a style that looks similar and gain a lot of functionality and performance from stock database engines. https://www.philipzucker.com/tiny-sqlite-datalog/

I wrote a small datalog from the Z3 AST to sqlite recently along these lines https://github.com/philzook58/knuckledragger/blob/main/kdrag...

discuss

order

sirwhinesalot|8 months ago

If I ever get around to writing my own at least somewhat serious Datalog engine, I definitely want to add a "translate to SQL" capability. Your work looks like the perfect inspiration, thanks!

(Also added a link to your article on what you can do with Datalog, excellent stuff, couldn't have written it better myself)

philzook|8 months ago

Thanks! I wouldn't so much say it was written so much as it was vomited out in a year of enthusiasm, but I'm glad it has some value.

richard_shelton|8 months ago

Or you can use Python AST + Z3 :) Here is a toy implementation:

https://github.com/true-grue/python-dsls/blob/main/datalog/d...

philzook|8 months ago

Love it! I was trying to use python as a dsl frontend to Z3 in a different way https://github.com/philzook58/knuckledragger/blob/ecac7a568a... (it probably has to be done syntactically rather than by overloading in order to make `if` `and` etc work). Still not sure if it is ultimately a good idea. I think it's really neat how you're abusing `,` and `:` in these examples

ulrikrasmussen|8 months ago

Thank you! I have been searching for something like this but for some reason couldn't find your work.

I am currently implementing a Datalog to PostgreSQL query engine at work as we want to experiment with modeling authorization rules in Datalog and then run authorization queries directly in the database. As I want to minimize the round trips to the database I use a different approach than yours and translate Datalog programs to recursive CTEs. These are a bit limited, so we have to restrict ourselves to linearly recursive Datalog programs, but for the purpose of modeling authorization rules that seems to be enough (e.g. you can still model things such as "permissions propagate from groups to group members").

philzook|8 months ago

My suspicion is that if you can get away with it that recursive CTEs would be more performant than doing the datalog iteration query by query. AFAIK for general rules the latter is the only option though. I had a scam in that post to do seminaive using timestamps but it was ugly. I recently came across this new feature in duckdb and was wondering if there is some way to use it to make a nice datalog https://duckdb.org/2025/05/23/using-key.html . Anything that adds expressiveness to recursive CTEs is a possibility in that direction

whitten|8 months ago

What does CTE stand for, and how do I research it ?

barrenko|8 months ago

This requires some discrete math knowledge?

kragen|8 months ago

That's exciting!