13:- module(each_call_cleanup,
14 [
15 redo_call_cleanup/3, 16 each_call_catcher_cleanup/4, 17 each_call_cleanup/3 18 ]).
28:- meta_predicate
29 redo_call_cleanup(0,0,0),
30 each_call_catcher_cleanup(0,0,?,0),
31 each_call_cleanup(0,0,0).
39redo_call_cleanup(Setup,Goal,Cleanup):-
40 must_be(ground,Setup),must_be(ground,Cleanup),
41 42 '$sig_atomic'(Setup),
43 catch(
44 ((Goal, deterministic(DET)),
45 '$sig_atomic'(Cleanup),
46 (DET == true -> !
47 ; (true;('$sig_atomic'(Setup),fail)))),
48 E,
49 ('$sig_atomic'(Cleanup),throw(E))).
58each_call_catcher_cleanup(Setup, Goal, Catcher, Cleanup):-
59 setup_call_catcher_cleanup(true,
60 each_call_cleanup(Setup, Goal, Cleanup), Catcher, true).
69each_call_cleanup(Setup,Goal,Cleanup):-
70 ((ground(Setup);ground(Cleanup)) ->
71 redo_call_cleanup(Setup,Goal,Cleanup);
72 setup_call_cleanup(
73 asserta(('$each_call_cleanup'(Setup):-Cleanup),HND),
74 redo_call_cleanup(pt1(HND),Goal,pt2(HND)),
75 erase(HND))).
76
77:- dynamic('$each_call_cleanup'/1). 78:- dynamic('$each_call_undo'/2). 79
80pt1(HND) :-
81 clause('$each_call_cleanup'(Setup),Cleanup,HND),
82 call(Setup),
83 asserta('$each_call_undo'(HND,Cleanup)).
84
85pt2(HND) :-
86 retract('$each_call_undo'(HND,Cleanup))
87 ->call(Cleanup);
88 true
Each call cleanup
Call Setup Goal Cleanup Each Iteration
*/