I’m using: SWI-Prolog version 8.3.8
I use a concurrent_forall
to search for solutions. When one is found, I use assertz(result(S)
to store it. After the concurrent piece, I use a sequential forall
to write each result to disc. When I’m done, I retract it all for cleanup. But, it doesn’t clean up. If I run the program again, the output contains the original solutions and a duplicate. The next time there are two duplicates, etc. I have to exit Prolog and restart in order to get only the output I want. Deleting the output file before re-running does not help.
parallel(Puzzle, Scope):-
retractall(result/1),
rules(Puzzle, [Rule1|Rules]),
concurrent_forall(generate(Puzzle, Rule1, Sequence), test(Puzzle, Rules, Sequence, Scope)),
forall(result(S), wrt_nl(S, Puzzle, tw)),
retractall(result/1).
test(_, [], Sequence, _):-
% write(Sequence).
assertz(result(Sequence)).
test(_, [Scope|_], Sequence, Scope):-
assertz(result(Sequence)).
test(Puzzle, [Rule|Rules], Sequence, Scope):-
plb(Puzzle, Lock, Bond),
check_rule(Puzzle, Rule, Sequence, Lock, Bond, Rule_Flag),
Rule_Flag = true_flag,
test(Puzzle, Rules, Sequence, Scope).
test(Puzzle, [Rule|_], Sequence, _):-
plb(Puzzle, Lock, Bond),
check_rule(Puzzle, Rule, Sequence, Lock, Bond, Rule_Flag),
dif(Rule_Flag, true_flag).