-
Notifications
You must be signed in to change notification settings - Fork 126
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
π§ Logic: Non-determinism of NewVariable
in prolog interpreter
#690
Comments
@bdeneux Thanks for this complete analysis! π The proposed solution seems ideal to me assuming the order of variable creation is deterministic as you mentioned, should be the case from what AFAIS but a deeper look should be welcomed. Regarding the implementation If I understand well we need to expose this |
@amimart, I agree that the current workaround is not the most elegant solution. I consider two possibles approaches to address the variable counter management issue:
As you've said, I think we can proceed with the first solution for now and reserve further investigation for a proper implementation later, one that includes a VM implementation more suited to our needs and addresses all other issues that need to be resolved. |
NewVariable
in prolog interpreterNewVariable
in prolog interpreter
When the
functor/3
predicate is called multiple times, it returns different results. This is demonstrated in the following example:The inconsistency arises because
functor/3
uses the built-inNewVariable()
function to allocate a new variable for each term's arity. This function increments a global counter each time a new variable is created. However, this counter is stored in the node's memory and is not synchronized across nodes. As a result, the counter can be incremented on one node and then called again through block validation on another node, leading to inconsistent results.https://github.com/axone-protocol/prolog/blob/d6ea3496c94deb1dbb755c4e2c78914507c67d0b/engine/builtin.go#L291-L293
https://github.com/axone-protocol/prolog/blob/d6ea3496c94deb1dbb755c4e2c78914507c67d0b/engine/variable.go#L9-L23
This issue is not limited to the
functor/3
predicate. It also affects other predicates that allocate new variables, such aslength/2
, and when variables are displayed as result solutions.Proposed solution(s)
To avoid a complete engine refactor, I propose adding a new function to reset the counter. This function should be called each time a new interpreter is instantiated. Since each GRPC query or execution instantiates a new interpreter, all nodes should align with the same counter count, assuming each internal engine call is deterministic.
It should result with
While investigating the implementation of the
functor/3
predicate, I noticed a code snippet that could lead to high memory consumption and potentially halt the node since it's the same used in the length/2 predicate : #618 (comment). If a large arity count is provided, as in the following example, a loop is executed that callsNewVariable()
for each arity, which can lead to a node crash.The text was updated successfully, but these errors were encountered: