The SWI-prolog docs heavily recommend against using setarg/3:
This predicate may be used for destructive assignment to terms, using them as an extra-logical storage bin. Always try hard to avoid the use of setarg/3 as it is not supported by many Prolog systems and one has to be very careful about unexpected copying as well as unexpected noncopying of terms.
I'm not sure I understand, what is "extra-logical" about setarg/3? I understand that things such as cuts and findnsols are extra-logical because they rely on implicit knowledge of the search order Prolog uses, what's so bad about changing an argument of a term?