A version of nb_setarg ( or duplicate_term ) sharing variables' attributes' arguments, so that they could be handled separately <- nb_link_attr

Greetings!

What would be needed for something like a version of nb_setarg ( or duplicate_term ) sharing( with the original ) variables’ attributes’ arguments, so that they could be handled separately?

nb_setarg ( and duplicate_term ) also recursively duplicates all contained variables’ attributes.
However, it could be known that ( at least some of ) the attribute term arguments have already been ( or will be ) taken care of ( — made back-tracking resistant or duplicated ). It would be good to be able to avoid duplicating everything again.

For example… It is possible to implement a version of duplicate_term that shares variables with the original by using nb_linkarg recursively. But something would be needed to fill the gap between a term and attributes of its variables, so that all parts of the whole structure would be non-backtrackable. This could be done by applying something like “nb_link_attr( +Module , +Var , +Value )” ( if it existed ) to all relevant attributes of all variables.

Thank you in advance!

Seems you would need a non-backtrackable version of put_attrs/2. With some care, I guess that can be implemented. Actually using it is a lot more complicated though as terms with attributes tend to be cyclic over the attributes and cycle detection and handling from Prolog is far from simple.

Smells like a can of worms … Can you provide a more high level view on what you are trying to do? Possibly you can avoid the copy of some part by storing some index atom/integer and use that to lookup the attribute value elsewhere?

Hello, thanks for replying!

Storing ( in variables’ attributes ) back-tracking resistant caches of structures( represented by nested terms ) that ( share parts with other structures in the caches and ) in turn contain other such caches ( nesting arbitrarily deeply ). There( stored in variables’ attributes ) also are references to containing|parent structures|variables.

In simplified form, the caches function similarly to lazy_lists.pl -- Lazy list handling. ( The latter implementation cannot be used due to it also duplicating variable attributes and therefore all nesting caches and everything else connected. )

Yes. It seems that this would also allow things to be constructed non-backtrackably to begin with, removing need for duplication.

I’m afraid this is all a bit too vague to recognise that we really need something that may be possible to implement, but is surely a hacky thing you’d rather not have in the system if it can be avoided.

I would have preferred to omit the nb_linkarg/3 as well, but it turned out to be necessary for dealing with the constraint libraries provided by Leuven.

I’d first consider whether there are other ways to get what you want. If not, you can try to implement a nb_link_attrs/2. If you succeed and there is a need for it in the public version, I’m ok at add it, possibly as undocumented $nb_link_attrs/2