egraphs-good / egglog

egraphs + datalog!
https://egraphs-good.github.io/egglog/
MIT License
400 stars 45 forks source link

How to query for relations #352

Closed rix0rrr closed 5 months ago

rix0rrr commented 5 months ago

I'm just getting my feet wet with egglog, trying to see if I can adapt it for a problem I have. I'm pretty new to this space, but have some familiarity with Prolog, and was hoping to start in a familiar place.

So I started with the graph reachability example:

(relation edge (i64 i64))
(relation path (i64 i64))

(rule ((edge x y))
      ((path x y)))
(rule ((path x y) (edge y z))
      ((path x z)))

(edge 1 2)
(edge 2 3)
(edge 3 4)

(run 100)

I then wanted to find all the nodes that are reachable from node 1, so I did the following:

(query-extract (path 1 x))

And egglog responds with:

()
()
()

I was hoping to get an assignment, something like (x 2) (x 3) (x 4); but instead it seems like it's extracting the unit return values of the function calls (path 1 2), (path 1 3) and (path 1 4). Am I understanding correctly what's happening here? I looked around for other functions that could do what I wanted, but I didn't find any.

Am I misunderstanding how this is supposed to work? It looks like Egglog is supposed to be used differently from Datalog, is that right? How should I query my fact database? (I saw print-function, but that requires me to filter myself)

Thank you for any help!

yihozhang commented 5 months ago

You are completely right here, and yes right now you need to filter yourself to see the result. But I agree what you had is a legit use case and we should probably have something like evaluate-query to support this.

Alternatively, you can do

(ruleset print)
(rule ((path 1 x))
      ((extract x)) :ruleset print)
(run print 1)