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

🧠 Logic: current_predicate random display #695

Closed
bdeneux opened this issue Jul 8, 2024 · 2 comments · Fixed by #703
Closed

🧠 Logic: current_predicate random display #695

bdeneux opened this issue Jul 8, 2024 · 2 comments · Fixed by #703
Assignees

Comments

@bdeneux
Copy link
Contributor

bdeneux commented Jul 8, 2024

📝 Description

The Prolog predicate current_predicate/1 is used to verify the existence of a predicate with a specified name and arity within the current context. Additionally, when used in addition with findall/3, it can retrieve a complete list of predicates registered in the interpreter. However, an issue arises when requesting this list of registered predicates: the returned list is displayed in a random order, varying with each request.

🔎 Analysis

The root of the issue stems from the VM, which maintains the list of registered procedures through a map[procedureIndicator]procedure. Given that maps in Go are inherently unordered, iterating over this procedures map to return the list of predicates results in a display order that is random and inconsistent.

There are multiple potential solutions to address this issue:

  • Sort the Procedures Map: Implement sorting on the procedures map before iterating over it to return a sorted list. This approach would require modifications to the current_predicate/1 predicate to alter its behavior accordingly.
  • Use an Ordered Map for VM Procedures: Replace the current VM procedures map with an ordered map. This ordered map would maintain the sequence of additions and removals, allowing the list to be returned in this consistent order. Such an order would be preserved throughout the VM's lifecycle, benefiting other predicates that might rely on this ordered list.

Those modifications should relay on prolog interpreter module.

@bdeneux
Copy link
Contributor Author

bdeneux commented Jul 8, 2024

By implementing ordered procedures in the prolog interpreter (axone-protocol/prolog), we should ensure in the logic module that registration of predicate is also ordered. It's not the case now. Registry also rely on a builtin map[string]any with a for each iteration that is not ordered, in consequence registration is not ordered.

A modification in the logic module should handle the registration of all predicates in a same order across nodes.

@amimart
Copy link
Member

amimart commented Jul 10, 2024

By implementing ordered procedures in the prolog interpreter (axone-protocol/prolog), we should ensure in the logic module that registration of predicate is also ordered. It's not the case now. Registry also rely on a builtin map[string]any with a for each iteration that is not ordered, in consequence registration is not ordered.

A modification in the logic module should handle the registration of all predicates in a same order across nodes.

I agree with that strategy, to make the management of predicates deterministic in the VM. An easier solution would've been to provide an alternative implementation of the current_predicate/1 predicate in the logic module sorting entries at each call, but I think it'll be safer to make it a default behaviour at the VM level.

@ccamel What do you think?

@bdeneux bdeneux changed the title current_predicate random display 🧠 Logic: current_predicate random display Jul 19, 2024
@bdeneux bdeneux linked a pull request Jul 22, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in 💻 Development Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

2 participants