The docs for transaction/1 say that testing transactions is allowed. I assumed this meant that the inner transaction would commit or rollback changes within its scope, and the outer transaction could choose to commit or rollback its changes (including any committed by the inner scope).
I’m having trouble coming up with any model to understand the behavior below…I expected
getValues(Set) to return
[1, 2, 3], and calling
tx_data(X) afterward to return
X = 3. This is what happens if I remove either one (or both) of the transaction/1 statements.
Is this behavior expected? Can anyone explain what’s going on?
% Test.pl :- dynamic tx_data/1. tx_data(0). getValues([New1, New2, New3]) :- transaction(( increment(New1), increment(New2), increment(New3) )). increment(New) :- transaction(( tx_data(Old), retractall(tx_data(_)), New is Old + 1, assert(tx_data(New)) )).
?- consult('Test.pl'). true. ?- getValues(Set). Set = [1,1,1]. ?- tx_data(X). X = 1 ; X = 1 ; X = 1.