axone-protocol / axoned

⛓️ Axone blockchain πŸ’«
https://axone.xyz
Apache License 2.0
162 stars 121 forks source link

🧠 Logic: `current_predicate` random display #695

Closed bdeneux closed 1 month ago

bdeneux commented 2 months ago

πŸ“ 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:

Those modifications should relay on prolog interpreter module.

bdeneux commented 2 months ago

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 commented 2 months ago

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?