Is there an equivalent to a constant label in Prolog? I’m using arg/3 to pull out “fields” from a frequently used Prolog term I build in my app. If the structure of that term changes, I’d like to be able to update a constant label that identifies that field’s argument position in the term, instead of finding all instances of arg/3 in my code that access that term and field and then updating the integer value in for the argument number I find in each place.
Not clear how that frequently used term will be represented and accessed. Will it be a term found in the Prolog database? A term being passed as argument to predicates? Can there be multiple instances of the term at any given time?
An alternative is to use Logtalk parametric objects and parameter variables (which are logical variables providing O(1) access). The parameter variable names will work as your “constant labels”.
Continuing your cat example, assume your frequently used term is cat/3 but you want to (1) access its arguments independently of position and (2) be able to add/remove arguments with minimal changes to your code.
We can easily define a parametric object that uses as identifier the compound term and use parameter variables (written as VarName) to abstract position:
This is also a good solution if you want to do more than simply access a named argument. E.g. define a predicate that makes some computations over the field name but keep them together with the access predicates.
Now assume that we also want to represent each cat mortal enemy. E.g.
Old queries that used cat/3 will continue to work as-is and new ones can start using the updated cat/4 representation. Of course, the cat terms don’t need to exist as clauses in the database. They can simply be a term being passed around that you query. Re-using your example predicate:
And there is library(record) for O(1) access. It is a bit verbose though. Dict access is indeed O(log(N)), but the constant factor is pretty low. Up to hundreds of keys you are generally pretty ok. They come also very handy if the set of keys is not known in advance.