Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] How do I implement a range table? #52

Closed
rmgaray opened this issue Apr 6, 2023 · 2 comments
Closed

[Q] How do I implement a range table? #52

rmgaray opened this issue Apr 6, 2023 · 2 comments

Comments

@rmgaray
Copy link

rmgaray commented Apr 6, 2023

Hello!

Admittedly my logic programming is not very good, but I was wondering if it is possible to create an infix rule like this:

range[a, b, x]

where a and b are integers defining a closed interval in Z and x can be any integer within the interval. The most straightforward implementation is this one:

range[x] := x = 0
range[x] := range[y], x = y + 1, x <= 10
?[x] := range[x]

where a = 0 and b = 10. But as you can see, I need to hardcode a and b and I can't really have the range[a, b, x] rule as I originally stated. Is this an inherent limitation of Datalog? I'm thinking this can probably be implemented as a custom fixed rule that takes parameters a and b, but I wonder if something like this could be done only using CozoScript.

Thanks in advance and congrats on the project, I think it's really cool.

@zh217
Copy link
Contributor

zh217 commented Apr 6, 2023

What you are asking for is infinite relations: as a and b are bindings, the relation represented by range must be (at least conceptually) infinite. This has been studied in the academic literature as an extension to Datalog, but I am not aware of any practical Datalog implementation that supports it.

However, writing it in the following way is perfectly acceptable in CozoScript:

range[x] := x = $lower
range[x] := range[y], x = y + 1, x <= $upper
?[x] := range[x]

and at query time you can pass in $lower and $upper as parameters. Does this work for you?

@rmgaray
Copy link
Author

rmgaray commented Apr 6, 2023

I think it will work for my use case, yes.

Thanks for the explanation, I will look up infinite relations since now I am curious about them.

@rmgaray rmgaray closed this as completed Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants