2:- module(test_tcf, []). 3
4:- set_module(class(library)). 5
6
7:- meta_predicate(try_call_finally(0,0,0)). 8:- export(try_call_finally/3). 9try_call_finally(S, G, C) :-
10 call_cleanup_(S, C),
11 deterministic_(G, F),
12 (F == true, !; call_cleanup_(C, S)).
13
20call_cleanup_(G, C) :-
21 call_cleanup((G; fail), C). 22
23
44is_try_call_finally_pred(try_call_finally).
45is_try_call_finally_pred(each_call_cleanup).
46
47try_try_call_finally1:- test_try_call_finally(1,try_call_finally).
48
49test_try_call_finally(1,P):-
50 is_try_call_finally_pred(P),
51 doall((
52
53 assert((try_c_f_state(W):- writeln(W=true))),
54
55 WriteFalse = ( try_c_f_state(W):- writeln(W=false), ! ),
56 call(P,
57 asserta(WriteFalse,Ref),
58 ( member(X,[1,2,3]), try_c_f_state(X) ),
59 erase(Ref)),
60 try_c_f_state(X)
61
62 )).
63
76try_try_call_finally2:-
77 try_call_finally(
78 writeln('in'),
79 member(X,[1,2,3]),
80 writeln('out')),
81 writeln('X'=X).
82 fail.
83try_try_call_finally2.
84
97
98try_try_call_finally3:-
99 try_call_finally(
100 gensym(hi_,X),
101 member(N,[1,2,3]),
102 writeln(c(X=N))),
103 writeln(o(X=N)),
104 fail.
105try_try_call_finally3.
106
117
118redo_call_cleanup_v1(Setup,Call,Cleanup):-
119 CallCleanup = call(Cleanup),
120 CleanupOnce = (CallCleanup, b_setarg(1,CallCleanup,true)),
121 SetupAndClean = (Setup,undo(CleanupOnce)),
122 call_cleanup(
123 (SetupAndClean, Call, undo(SetupAndClean)),
124 CleanupOnce).
125
126redo_call_cleanup_v2(Setup,Call,Cleanup):-
127 CallCleanup = call(Cleanup),
128 CleanupOnce = (CallCleanup, b_setarg(1,CallCleanup,true)),
129 call_cleanup(
130 (repeat,Setup,undo(CleanupOnce),
131 (Call*-> true
132
133 ;(!,fail)),
134 (deterministic(true) -> ! ; CleanupOnce)),
135 CleanupOnce).
144:- fixup_exports.