head 1.133; access; symbols; locks ulrich:1.133; strict; comment @# @; 1.133 date 2023.04.15.07.37.28; author ulrich; state Exp; branches; next 1.132; 1.132 date 2023.04.15.06.48.24; author ulrich; state Exp; branches; next 1.131; 1.131 date 2011.09.21.09.31.44; author ulrich; state Exp; branches; next 1.130; 1.130 date 2011.09.21.08.42.38; author ulrich; state Exp; branches; next 1.129; 1.129 date 2011.09.19.12.43.42; author ulrich; state Exp; branches; next 1.128; 1.128 date 2011.07.20.21.55.50; author ulrich; state Exp; branches; next 1.127; 1.127 date 2011.07.20.21.53.15; author ulrich; state Exp; branches; next 1.126; 1.126 date 2010.07.19.18.41.45; author ulrich; state Exp; branches; next 1.125; 1.125 date 2010.06.29.14.43.06; author ulrich; state Exp; branches; next 1.124; 1.124 date 2010.06.29.14.38.21; author ulrich; state Exp; branches; next 1.123; 1.123 date 2010.06.24.01.55.24; author ulrich; state Exp; branches; next 1.122; 1.122 date 2009.12.16.20.08.58; author ulrich; state Exp; branches; next 1.121; 1.121 date 2009.12.16.19.35.54; author ulrich; state Exp; branches; next 1.120; 1.120 date 2009.12.07.23.05.00; author ulrich; state Exp; branches; next 1.119; 1.119 date 2009.12.07.14.00.22; author ulrich; state Exp; branches; next 1.118; 1.118 date 2009.12.07.13.52.21; author ulrich; state Exp; branches; next 1.117; 1.117 date 2009.12.07.02.40.07; author ulrich; state Exp; branches; next 1.116; 1.116 date 2009.12.06.00.07.36; author ulrich; state Exp; branches; next 1.115; 1.115 date 2009.12.04.00.49.52; author ulrich; state Exp; branches; next 1.114; 1.114 date 2009.12.03.06.31.51; author ulrich; state Exp; branches; next 1.113; 1.113 date 2009.12.03.05.57.45; author ulrich; state Exp; branches; next 1.112; 1.112 date 2009.12.03.05.56.42; author ulrich; state Exp; branches; next 1.111; 1.111 date 2009.11.30.03.27.46; author ulrich; state Exp; branches; next 1.110; 1.110 date 2009.11.28.23.36.50; author ulrich; state Exp; branches; next 1.109; 1.109 date 2009.11.28.23.27.13; author ulrich; state Exp; branches; next 1.108; 1.108 date 2009.11.28.21.28.09; author ulrich; state Exp; branches; next 1.107; 1.107 date 2009.11.13.15.08.46; author ulrich; state Exp; branches; next 1.106; 1.106 date 2009.11.13.15.05.23; author ulrich; state Exp; branches; next 1.105; 1.105 date 2009.11.13.15.04.45; author ulrich; state Exp; branches; next 1.104; 1.104 date 2009.11.13.14.19.57; author ulrich; state Exp; branches; next 1.103; 1.103 date 2009.11.13.13.48.22; author ulrich; state Exp; branches; next 1.102; 1.102 date 2009.11.13.12.12.31; author ulrich; state Exp; branches; next 1.101; 1.101 date 2009.11.10.01.17.21; author ulrich; state Exp; branches; next 1.100; 1.100 date 2009.10.29.00.53.48; author ulrich; state Exp; branches; next 1.99; 1.99 date 2009.10.29.00.45.40; author ulrich; state Exp; branches; next 1.98; 1.98 date 2009.10.29.00.45.00; author ulrich; state Exp; branches; next 1.97; 1.97 date 2009.10.29.00.43.46; author ulrich; state Exp; branches; next 1.96; 1.96 date 2009.10.19.17.14.49; author ulrich; state Exp; branches; next 1.95; 1.95 date 2009.10.17.23.14.19; author ulrich; state Exp; branches; next 1.94; 1.94 date 2009.10.17.23.09.16; author ulrich; state Exp; branches; next 1.93; 1.93 date 2009.10.07.12.03.47; author ulrich; state Exp; branches; next 1.92; 1.92 date 2009.10.07.11.58.25; author ulrich; state Exp; branches; next 1.91; 1.91 date 2009.10.04.20.23.39; author ulrich; state Exp; branches; next 1.90; 1.90 date 2009.10.04.15.27.42; author ulrich; state Exp; branches; next 1.89; 1.89 date 2009.09.29.16.34.09; author ulrich; state Exp; branches; next 1.88; 1.88 date 2009.09.29.15.12.23; author ulrich; state Exp; branches; next 1.87; 1.87 date 2009.09.29.11.17.24; author ulrich; state Exp; branches; next 1.86; 1.86 date 2009.09.29.11.11.37; author ulrich; state Exp; branches; next 1.85; 1.85 date 2009.09.29.10.46.35; author ulrich; state Exp; branches; next 1.84; 1.84 date 2009.09.29.10.32.25; author ulrich; state Exp; branches; next 1.83; 1.83 date 2009.09.29.10.29.29; author ulrich; state Exp; branches; next 1.82; 1.82 date 2009.09.29.01.01.57; author ulrich; state Exp; branches; next 1.81; 1.81 date 2009.09.28.23.11.18; author ulrich; state Exp; branches; next 1.80; 1.80 date 2009.09.24.20.41.10; author ulrich; state Exp; branches; next 1.79; 1.79 date 2009.09.24.20.31.04; author ulrich; state Exp; branches; next 1.78; 1.78 date 2009.09.11.15.35.04; author ulrich; state Exp; branches; next 1.77; 1.77 date 2009.09.05.22.33.24; author uwn; state Exp; branches; next 1.76; 1.76 date 2009.09.05.22.29.24; author uwn; state Exp; branches; next 1.75; 1.75 date 2009.09.05.22.28.40; author uwn; state Exp; branches; next 1.74; 1.74 date 2009.09.01.22.14.10; author uwn; state Exp; branches; next 1.73; 1.73 date 2009.09.01.18.56.53; author uwn; state Exp; branches; next 1.72; 1.72 date 2009.09.01.17.28.58; author uwn; state Exp; branches; next 1.71; 1.71 date 2009.09.01.12.53.41; author uwn; state Exp; branches; next 1.70; 1.70 date 2009.09.01.12.48.01; author uwn; state Exp; branches; next 1.69; 1.69 date 2009.09.01.12.16.45; author uwn; state Exp; branches; next 1.68; 1.68 date 2009.09.01.12.16.21; author uwn; state Exp; branches; next 1.67; 1.67 date 2009.09.01.12.15.46; author uwn; state Exp; branches; next 1.66; 1.66 date 2009.08.31.23.48.38; author uwn; state Exp; branches; next 1.65; 1.65 date 2009.08.31.22.39.53; author uwn; state Exp; branches; next 1.64; 1.64 date 2009.08.29.08.37.16; author uwn; state Exp; branches; next 1.63; 1.63 date 2009.08.29.05.44.27; author uwn; state Exp; branches; next 1.62; 1.62 date 2009.08.28.16.21.10; author uwn; state Exp; branches; next 1.61; 1.61 date 2009.08.28.14.16.57; author ulrich; state Exp; branches; next 1.60; 1.60 date 2009.08.25.10.30.05; author ulrich; state Exp; branches; next 1.59; 1.59 date 2009.08.24.09.42.57; author ulrich; state Exp; branches; next 1.58; 1.58 date 2009.08.24.09.40.18; author ulrich; state Exp; branches; next 1.57; 1.57 date 2009.08.24.09.09.27; author ulrich; state Exp; branches; next 1.56; 1.56 date 2009.08.24.08.49.31; author ulrich; state Exp; branches; next 1.55; 1.55 date 2009.08.22.02.35.32; author ulrich; state Exp; branches; next 1.54; 1.54 date 2009.08.22.02.16.41; author ulrich; state Exp; branches; next 1.53; 1.53 date 2009.08.19.16.22.10; author ulrich; state Exp; branches; next 1.52; 1.52 date 2009.08.19.12.34.17; author ulrich; state Exp; branches; next 1.51; 1.51 date 2009.08.18.19.18.55; author ulrich; state Exp; branches; next 1.50; 1.50 date 2009.08.18.17.41.26; author ulrich; state Exp; branches; next 1.49; 1.49 date 2009.08.18.17.28.30; author ulrich; state Exp; branches; next 1.48; 1.48 date 2009.08.18.15.52.18; author ulrich; state Exp; branches; next 1.47; 1.47 date 2009.08.18.15.31.46; author ulrich; state Exp; branches; next 1.46; 1.46 date 2009.08.18.15.30.01; author ulrich; state Exp; branches; next 1.45; 1.45 date 2009.08.14.01.19.56; author ulrich; state Exp; branches; next 1.44; 1.44 date 2009.08.14.01.01.03; author ulrich; state Exp; branches; next 1.43; 1.43 date 2009.08.14.00.37.59; author ulrich; state Exp; branches; next 1.42; 1.42 date 2009.08.13.16.39.07; author ulrich; state Exp; branches; next 1.41; 1.41 date 2009.08.13.16.35.40; author ulrich; state Exp; branches; next 1.40; 1.40 date 2009.08.13.16.29.05; author ulrich; state Exp; branches; next 1.39; 1.39 date 2009.08.13.16.10.58; author ulrich; state Exp; branches; next 1.38; 1.38 date 2009.08.12.17.28.36; author ulrich; state Exp; branches; next 1.37; 1.37 date 2009.08.12.17.19.31; author ulrich; state Exp; branches; next 1.36; 1.36 date 2009.08.11.19.12.41; author ulrich; state Exp; branches; next 1.35; 1.35 date 2009.08.11.19.11.57; author ulrich; state Exp; branches; next 1.34; 1.34 date 2009.08.11.18.37.11; author ulrich; state Exp; branches; next 1.33; 1.33 date 2009.08.11.18.34.56; author ulrich; state Exp; branches; next 1.32; 1.32 date 2009.08.11.12.33.12; author ulrich; state Exp; branches; next 1.31; 1.31 date 2009.08.04.18.52.45; author ulrich; state Exp; branches; next 1.30; 1.30 date 2009.08.04.18.40.44; author ulrich; state Exp; branches; next 1.29; 1.29 date 2009.08.04.17.57.33; author ulrich; state Exp; branches; next 1.28; 1.28 date 2009.08.03.12.36.51; author ulrich; state Exp; branches; next 1.27; 1.27 date 2009.08.03.11.04.10; author ulrich; state Exp; branches; next 1.26; 1.26 date 2009.07.30.17.05.10; author ulrich; state Exp; branches; next 1.25; 1.25 date 2009.07.21.10.17.23; author ulrich; state Exp; branches; next 1.24; 1.24 date 2009.07.02.23.57.48; author ulrich; state Exp; branches; next 1.23; 1.23 date 2009.07.02.22.22.45; author ulrich; state Exp; branches; next 1.22; 1.22 date 2009.07.02.19.40.51; author ulrich; state Exp; branches; next 1.21; 1.21 date 2009.07.02.19.38.09; author ulrich; state Exp; branches; next 1.20; 1.20 date 2009.07.02.18.02.21; author ulrich; state Exp; branches; next 1.19; 1.19 date 2009.07.02.17.23.03; author ulrich; state Exp; branches; next 1.18; 1.18 date 2009.07.02.17.22.24; author ulrich; state Exp; branches; next 1.17; 1.17 date 2009.07.02.17.13.34; author ulrich; state Exp; branches; next 1.16; 1.16 date 2009.07.02.17.12.21; author ulrich; state Exp; branches; next 1.15; 1.15 date 2009.07.02.16.57.35; author ulrich; state Exp; branches; next 1.14; 1.14 date 2009.07.02.16.03.00; author ulrich; state Exp; branches; next 1.13; 1.13 date 2009.07.02.14.56.19; author ulrich; state Exp; branches; next 1.12; 1.12 date 2009.07.02.14.50.03; author ulrich; state Exp; branches; next 1.11; 1.11 date 2009.07.02.14.28.23; author ulrich; state Exp; branches; next 1.10; 1.10 date 2009.07.02.14.11.51; author ulrich; state Exp; branches; next 1.9; 1.9 date 2009.07.02.13.45.20; author ulrich; state Exp; branches; next 1.8; 1.8 date 2009.07.02.12.28.30; author ulrich; state Exp; branches; next 1.7; 1.7 date 2009.07.02.03.22.29; author ulrich; state Exp; branches; next 1.6; 1.6 date 2009.07.02.03.19.27; author ulrich; state Exp; branches; next 1.5; 1.5 date 2009.07.02.03.04.43; author ulrich; state Exp; branches; next 1.4; 1.4 date 2009.07.02.02.56.30; author ulrich; state Exp; branches; next 1.3; 1.3 date 2009.07.02.02.56.04; author ulrich; state Exp; branches; next 1.2; 1.2 date 2009.07.02.02.54.19; author ulrich; state Exp; branches; next 1.1; 1.1 date 2009.07.02.02.43.55; author ulrich; state Exp; branches; next ; desc @@ 1.133 log @correction @ text @ post-N215 setup_call_cleanup/3

ISO/IEC JTC1 SC22 WG17 post-N215
Draft proposal for setup_call_cleanup/3

Ulrich Neumerkel, 2011-09-21 (Version history)
This draft proposal is based on 8.15.5 of N208 2008-11-17. Earlier versions: N211 2009-07-02. N215 2009-10-29. It is intended to be added as 7.8.11. References refer to 13211-1:1995. Parts needing improvements are underlined.

Contributors

Bart Demoen (Belgium). Paulo Moura (Portugal). Jonathan Hodgson (USA). Richard O'Keefe (New Zealand). Feliks Kluzniak (USA). Jeffrey Rosenwald (USA). Markus Triska (Austria). Jan Wielemaker (The Netherlands). Vítor Santos Costa (Portugal). Jan Burse (Switzerland).

7.8.11 setup_call_cleanup/3

This control construct allows managing resources related to the execution of a goal. It provides protected setup and performs cleanup as soon as the goal has completed execution.
NOTE — A built-in predicate call_cleanup/2 with similar functionality is implemented in many existing processors. It is often used to free temporary resources. In the presence of interrupts (7.12 note c), call_cleanup/2 can cause leakage of resources: A processor receiving interrupts between resource allocation and call_cleanup/2 is unable to free the resource in a timely manner. To overcome this problem, setup_call_cleanup/3 protects resource allocation from interrupts.

7.8.11.1 Description

setup_call_cleanup(S, G, C) is true iff once(S), call(G) is true.

Procedurally, the control construct shall be executed as follows:

a) once(S) is executed while being protected from interrupts:
  1. either once(S) is executed entirely and the cleanup handler C is installed upon success;
  2. or once(S) is interrupted by an implementation dependent interrupt (7.12 note c). In this case, once(S) does not leave an observable effect.
b) After the cleanup handler is installed, call(G) is called.
c) The cleanup handler is called exactly once; no later than upon failure of G. Earlier moments are:
  1. If G is true or false, C is called at an implementation dependent moment after the last solution and after the last observable effect of G.
  2. When G or the continuation of G is interrupted by a call of throw/1 (7.8.9 a, b), whose corresponding call of catch/3 is above G.
    After executing the cleanup C, throw/1 continues to search the corresponding call of catch/3, regardless of the outcome of C. Thus, if C is interrupted by another call of throw/1, that throw/1 is lost.
  3. When the continuation of G executes a cut explicitly or implicitly that is associated with the cutparent of G (7.7.2, 7.8.4.1 Note 1b).
    After the cut suceeds, C is called in the place of the cut. A cut is performed implicitly for (->)/2 - if-then (7.8.7), (;)/2 - if-then-else (7.8.8), (\+)/1 (8.15.1), once/1 (8.15.2).
d) C is called as once(C). Failure of C is ignored. An explicit or implicit throw/1 is ignored in case 7.8.11.1 c2; otherwise (case 7.8.11.1 c1 or 7.8.11.1 c3), it is passed towards the corresponding call of catch/3.
Several cleanup goals triggered simultaneously are executed in reversed order of installation.

C shares bindings and variables with S, with G and with the continuation. The bindings up to and including S are always present. No further bindings are present when C is executed at 7.8.11.1 c2 and at the latest moment of 7.8.11.1 c1. At 7.8.11.1 c2, and at the earliest moment of 7.8.11.1 c1, all bindings are present at the time of calling the cut. All other cases of 7.8.11.1 c1 are implementation dependent.

A processor may restrict execution of C in an implementation defined manner. For example, a processor may restrict the handling of interrupts within C or limit resource consumption.

NOTES
1 Existing processors locate c1 either when the last solution is found, or upon failure. The precise moment is implementation dependent due to the varying ability of Prolog processors to detect determinism. For example, a processor may execute call((call(G);fail)) as call(G) and vice versa.
2 For nonterminating goals like repeat (8.15.3) only c2 and c3 can trigger the cleanup. And if a nonterminating goal does not find a solution, only c2 can trigger.
3 A throw/1 of G has priority over one of C (7.8.11.1 c2). This is an advantage when recovering errors, like resource errors (7.12.2 h), where the error of C is often a consequence of the error of G.

7.8.11.2 Template and modes

setup_call_cleanup(goal, goal, goal)
NOTE — The arguments should eventually be +callable_term. However, neither type nor mode is checked when this control construct is executed (8.1.2.2).

7.8.11.3 Errors

a) S is a callable term that is not permitted due to an implementation defined restriction
representation_error(setup_goal).
b) S finds a solution, but C is a variable
instantiation_error. Goal G is not executed.
c) S finds a solution, but C is neither a variable nor a callable term
type_error(callable, C). Goal G is not executed.
NOTE — Further errors in S and G may happen due to calling once/1 or call/1 (see 7.8.11.1).

7.8.11.4 Examples

In the following, write/1 uses the standard operator table (6.3.4.4, table 7). Unique variables are written as _ which is one possible way to realize 7.10.5 a. The different outcomes (labeled either/or) are due to the implementation dependence in c1.
setup_call_cleanup(fail, _, _).
   Fails.  Neither the goal nor the cleanup is executed.

setup_call_cleanup(throw(ex), _, _).
   System error due to uncaught ex.

setup_call_cleanup(true, throw(unthrown),_).
   Instantiation error.

setup_call_cleanup(true, true, ( true ; throw(x) )).
   Succeeds. No system error.

setup_call_cleanup(true, X = 1, X = 2).
   Succeeds, unifying X = 1.

setup_call_cleanup(true, true, X = 2).
   Either: Succeeds, unifying X = 2.
   Or: Succeeds.

setup_call_cleanup(true, X=true,X).
   Instantiation error.

setup_call_cleanup(X=throw(ex), true, X).
   Either: System error due to uncaught ex.
   Or: Succeeds. System error on backtracking.

setup_call_cleanup(true, true, fail).
   Succeeds.

setup_call_cleanup(S=1, G=2, C=3).
   Either: Succeeds, unifying S = 1, G = 2, C = 3.
   Or:  Succeeds, unifying S = 1, G = 2.

setup_call_cleanup((S=1;S=2), G=3, C=4).
   Either: Succeeds, unifying
           S = 1, G = 3, C = 4.
   Or: Succeeds, unifying S = 1, G = 3.

setup_call_cleanup(S=1,G=2,write(S+G)).
   Succeeds, unifying S = 1, G = 2.
   Either: outputs '1+2'
   Or: outputs on backtracking '1+_' prior to failure.
   Or (?): outputs on backtracking '1+2' prior to failure.

setup_call_cleanup(S=1,(G=2;G=3),write(S+G)).
   Succeeds, unifying S = 1, G = 2.
   On backtracking, succeeds unifying S = 1 and G = 3.
   Either: outputs '1+3'
   Or: on backtracking outputs '1+_' prior to failing
   Or (?): on backtracking outputs '1+3' prior to failing.

setup_call_cleanup(S=1,G=2,write(S+G>A+B)), A=3, B=4.
   Succeeds, unifying S=1,G=2,A=3,B=4.
   Either:  Outputs one of the following before succeeding
   1+2>_+_.   1+2>3+_.   1+2>3+4.   Disputable: 1+2>_+4.
   Or: outputs one of the above outputs on backtracking
   prior to failing.

setup_call_cleanup(S=1,(G=2;G=3,throw(x)),write(S+G)).
   Succeeds, unifying S = 1, G = 2.
   On backtracking, outputs '1+_' prior
   to system error due to uncaught x.

setup_call_cleanup(open(f,read,S),read(S,X),close(S)).
   Opens file f for reading, reads a term and closes the file.
   Succeeds, unifying S with an implementation dependent stream
   term, unifying X with the term read.  The file is closed, either
   immediately, or on backtracking, and even if there is an error in
   read.

Events in the continuation

setup_call_cleanup(S=1,(G=2;G=3),write(S+G>B)), B=4, !.
   Outputs '1+2>4'. Succeeds, unifying S = 1, G = 2, B = 4.

setup_call_cleanup(S=1,G=2,write(S+G>B)),B=3,!.
   Either: Outputs  '1+2>_'. Succeeds, unifying
      S = 1, G = 2, B = 3.
   Or:  Outputs '1+2>3'. Succeeds, unifying
      S = 1, G = 2, B = 3.

setup_call_cleanup(S=1,(G=2;fail),write(S+G>B)), B=3, !.
   Same as above.

setup_call_cleanup(S=1,(G=2;S=2),write(S+G>B)), B=3, !.
   Same as above.

setup_call_cleanup(S=1,(G=2;G=3), write(S+G>B)), B=4, throw(x).
   Outputs '1+_>_'. system_error due to uncaught x.

setup_call_cleanup(S=1,(G=2;G=3), write(S+G>B)), B=4, !, throw(x).
   Outputs '1+2>4'. system_error due to uncaught x.

setup_call_cleanup(true, (X=1;X=2), write(a)),
      setup_call_cleanup(true,(Y=1;Y=2),write(b)), throw(x).
   Outputs 'ba'. system_error due to uncaught x.

setup_call_cleanup(true, (X=1;X=2), write(a)),
      setup_call_cleanup(true,(Y=1;Y=2),write(b)), !.
   Outputs 'ba'.  Succeeds, unifying X = 1, Y = 1.

Multiple exceptions

catch(setup_call_cleanup(true,throw(goal),throw(cl)), Pat, true).
   Succeeds unifying Pat = goal.

catch(( setup_call_cleanup(true,(G=1;G=2),throw(cl)), throw(cont)), Pat, true).
   Succeeds unifying Pat = cont.

catch(
   setup_call_cleanup(true, throw(a),
      setup_call_cleanup(true,fail,throw(b))
      ),
   Pat, true).
   Succeeds unifying Pat with a.

Implementation status

Systems with setup_call_cleanup/3

SWI: fully compilant since 2009-11-13. Almost fully since Autumn 2007.
YAP-6: only difference: c3 and order
Qu-Prolog: almost fully compliant. Open: error b, c, bindings for c2.
B-Prolog: very close. Open: bindings (in d) for c2 different. Different priorities in c2.
ECLiPSe: As implementation specific library. No bindings between Cleanup and Goal. throw/1 from Cleanup not visible.

Systems with call_cleanup/2

The following implementations are compared using
setup_call_cleanup(S, G, C) :-
   once(S),
   call_cleanup(G, C).
SICStus: original implementation 1997. c2 different. d is not called as once(C). fixed in 4.8.0 2022-12.
CxProlog: No bindings between Cleanup and Goal
XSB: sometimes the cleanup is not executed for c2 and c3 (as of 2009-07)
untested: K-Prolog

Validated HTML @ 1.132 log @m @ text @d326 4 a329 3 Succeeds, unifying S with the term read. The file is closed, either immediately, or on backtracking, and even if there is an error in read. d412 3 a414 2
SICStus: original implementation 1997. c2 different. d is not called as once(C). @ 1.131 log @*** empty log message *** @ text @d265 1 a265 1 Sytem error due to uncaught ex. d411 2 a412 1
SICStus: original implementation 1997. c2 different. @ 1.130 log @*** empty log message *** @ text @d60 1 a60 1 2009-12-16 @ 1.129 log @*** empty log message *** @ text @d374 1 a374 1 P, true). @ 1.128 log @*** empty log message *** @ text @d81 1 d167 1 a167 1 7.8.11.1 c3), it is passed to the corresponding call of catch/3. d369 7 @ 1.127 log @?term to goal. Actually happend 2010-07-24 14:58:38 already. @ text @d244 3 a246 2 NOTE — Further errors in S and G are already implied by 7.8.11.1. @ 1.126 log @*** empty log message *** @ text @d214 1 a214 1 setup_call_cleanup(?term, ?term, ?term) @ 1.125 log @Consistent variable names @ text @d397 1 a397 1 S, d404 2 d409 1 a409 1
untested: CxProlog, K-Prolog @ 1.124 log @After message to WG17-list @ text @d394 1 a394 1 The following implementations are compared with d396 3 a398 2 call_cleanup(Call, Cleanup) :- setup_call_cleanup(true, Call, Cleanup). @ 1.123 log @Reformatting @ text @d394 5 @ 1.122 log @*** empty log message *** @ text @d351 2 a352 1 setup_call_cleanup(true, (X=1;X=2), write(a)), setup_call_cleanup(true,(Y=1;Y=2),write(b)), throw(x). d355 2 a356 1 setup_call_cleanup(true, (X=1;X=2), write(a)), setup_call_cleanup(true,(Y=1;Y=2),write(b)), !. d368 3 @ 1.121 log @*** empty log message *** @ text @d60 1 a60 1 2009-12-04 @ 1.120 log @*** empty log message *** @ text @d382 2 a383 1
ECLiPSe: As implementation specific library. No bindings for Cleanup. @ 1.119 log @disambiuguation of 1b finished (?) @ text @d120 1 a120 1 dependent interrupt (7.12 note c). once(S) does not leave an d122 1 a122 1 effect @ 1.118 log @a1 @ text @d120 1 a120 1 dependent interrupt (7.12 note c) without leaving an d122 1 a122 1 effect. @ 1.117 log @Problematic case @ text @d118 1 a118 1
  • or setup_call_cleanup/3 is interrupted by an @ 1.116 log @*** empty log message *** @ text @d120 1 a120 1 dependent interrupt (7.12 note c) without leaving an d122 1 a122 1 effect. @ 1.115 log @Vitor's example @ text @d153 1 a153 1 After the cut has been executed, C is called in d244 2 a245 2 NOTE — Further errors due to S and G are already implied by the present definition. d262 1 a262 1 setup_call_cleanup(throw(ex),true,true). @ 1.114 log @ECLiPSe status @ text @d60 1 a60 1 2009-12-03 d80 1 d257 1 d262 3 d375 1 a375 1
    YAP-6: almost fully compliant since June 2009. Open: error b. @ 1.113 log @*** empty log message *** @ text @d377 1 @ 1.112 log @Qu progress @ text @d60 1 a60 1 2009-11-29 @ 1.111 log @Qu update @ text @d372 2 a376 1
    Qu-Prolog: functional for simple uses. @ 1.110 log @Datum @ text @d374 3 d388 1 a388 1
    untested: CxProlog, Qu-Prolog, K-Prolog @ 1.109 log @*** empty log message *** @ text @d60 1 a60 1 2009-11-13 @ 1.108 log @*** empty log message *** @ text @d370 1 a370 1
    YAP-6: almost fully compliant since June 2009. Open: errors b and c. @ 1.107 log @*** empty log message *** @ text @a9 6 d346 3 @ 1.106 log @*** empty log message *** @ text @d66 1 a66 1 2009-10-29 @ 1.105 log @SWI is now fully compilant! @ text @d370 1 a370 1
    SWI: fully compilant since Novemnber 2009. Almost fully since @ 1.104 log @Currected SWI status @ text @d370 2 a371 2
    SWI: almost fully compliant since Autumn 2007. Open: c 2 for continuation case - the other throw should have priority. @ 1.103 log @SWI status update (only one difference left) @ text @d371 1 a371 1 continuation case. @ 1.102 log @Small (obvious) changes to examples @ text @d370 2 a371 2
    SWI: almost fully compliant since Autumn 2007. Open: bindings (in d) for c2 different. @ 1.101 log @Jonathan Hodgson, Date: Mon, 09 Nov 2009 13:33:23 -0500 @ text @d314 1 a314 1 1+2>_+_. Disputable: 1+2>3+_. 1+2>3+4. 1+2>_+4. d332 1 a332 1 Outputs '1+2>4'. Succeeds, unifying S = 1, G = 2, B = b. d335 1 a335 1 Either: Outputs '1+2>3'. Succeeds, unifying d337 1 a337 1 Or: Outputs '1+2>_'. Succeeds, unifying @ 1.100 log @*** empty log message *** @ text @d169 3 a171 2 implicit throw/1 is ignored for c2; otherwise (c1 or c3), it is passed to the corresponding call of catch/3. d180 4 a183 3 are present when C is executed at c2 and at the latest moment of c1. At c2, and at the earliest moment of c1, all bindings are present at the time of calling the cut. All other cases of c1 are implementation dependent. d213 1 a213 1 of C (c2). This is an advantage when recovering @ 1.99 log @post @ text @d71 3 a73 1 of N208 2008-11-17. Earlier version: N211 2009-07-02. d107 1 a107 1 iff once(S), call(G) is true. @ 1.98 log @N215 final @ text @d7 1 a7 1 N215 setup_call_cleanup/3 d63 1 a63 1

    ISO/IEC JTC1 SC22 WG17 N215
    Draft proposal for setup_call_cleanup/3

    @ 1.97 log @N215 @ text @d66 1 a66 1 2009-10-19 @ 1.96 log @Date @ text @d7 1 a7 1 pre-N215 setup_call_cleanup/3 d63 1 a63 1

    ISO/IEC JTC1 SC22 WG17 pre-N215
    Draft proposal for setup_call_cleanup/3

    d70 2 a71 4 This draft proposal is based on Paulo Moura's draft core 8.15.5 13211-1:2006 of 2008-11-17. Since July 2009 it is N211. From 1st November 2009 on, it will be N215. @ 1.95 log @Better harmonization with 7.8.9.1, first par. @ text @d66 1 a66 1 2009-09-29 @ 1.94 log @*** empty log message *** @ text @d146 1 a146 1 executes a throw/1 explicitly or implicitly (7.8.9 a, d152 2 a153 2 Thus, if C executes another relevant throw/1, that throw/1 is lost. @ 1.93 log @Removed errors for S. They are implicitely defined by calling once(S) @ text @d71 1 a71 1 13211-1:2006 of 2008-11-17. d221 1 a221 1 NOTE — The second and third argument should eventually be d223 1 a223 1 checked when the control construct is executed (8.1.2.2). d247 2 a248 2 NOTE — Errors due to S and G are already implied by the present definition. a251 1 @ 1.92 log @Shorter Note @ text @d217 1 a217 1 setup_call_cleanup(+callable_term, ?term, ?term) d228 1 d231 1 a231 8
    a) S is a variable
    instantiation_error.
    b) S is neither a variable nor a callable term
    type_error(callable, S).
    c) S is a callable term that is not permitted due d235 1 a235 1
    d) S finds a solution, but C is d239 1 a239 1
    e) S finds a solution, but C is d247 2 a248 2 NOTE — Errors due to G are already implied by the present definition. d372 1 a372 1
    YAP-6: almost fully compliant since June 2009. Open: errors d and e. @ 1.91 log @*** empty log message *** @ text @a251 2 NOTE — The following errors are already implied by the present definition: d253 2 a254 12
    f) C is installed, but G is a variable
    instantiation_error. Cleanup C is executed.
    g) C is installed, but G is neither a variable nor a callable term
    type_error(callable, G). Cleanup C is executed.
    @ 1.90 log @*** empty log message *** @ text @d252 1 a252 1 NOTE — The following two errors are already implied by the @ 1.89 log @*** empty log message *** @ text @d217 9 a225 1 setup_call_cleanup(+callable_term, ?callable_term, ?callable_term) d249 10 a258 2
    f) C is installed, but G is a variable
    — d268 2 @ 1.88 log @New number N215 @ text @d71 4 a74 1 13211-1:2006 of 2008-11-17. Since July 2009 it is N211. It is intended to be added as 7.8.11. @ 1.87 log @*** empty log message *** @ text @d7 1 a7 1 N211 setup_call_cleanup/3 d63 1 a63 1

    ISO/IEC JTC1 SC22 WG17 N211
    Draft proposal for setup_call_cleanup/3

    @ 1.86 log @*** empty log message *** @ text @a375 1 See note in clause 7.8.11 @ 1.85 log @Undone - was actually correct @ text @d380 1 a380 1
    SICStus: call_cleanup/2 since 1997 (origin). c2 different. @ 1.84 log @Reformatted Contributors @ text @d340 1 a340 1 Same as above. @ 1.83 log @*** empty log message *** @ text @d74 9 a82 4

    Contributors

    Bart Demoen (Belgium). Paulo Moura (Portugal). Jonathan Hodgson (USA). Richard O'Keefe (New Zealand). Feliks Kluzniak (USA). Jeffrey Rosenwald (USA). Markus Triska (Austria). Jan Wielemaker (The Netherlands). @ 1.82 log @B-Prolog news @ text @d334 2 a335 2 setup_call_cleanup(S=1,(G=2;S=2),write(S+G>B)), B=3, !. % consider! Same as above. d359 1 a360 4
    SICStus: call_cleanup/2 since 1997 (origin). Open: setup_call_cleanup/3 missing. c2 different. d368 4 d373 5 a377 1
    XSB: call_cleanup/2, sometimes the d380 1 a380 3
    CxProlog: call_cleanup/2 (untested)
    Qu-Prolog: call_cleanup/2 (untested) @ 1.81 log @Paulo Moura's contribution @ text @d66 1 a66 1 2009-09-06 d322 1 a322 1 setup_call_cleanup(S=1,(G=2;G=3),write(S+G>B))), B=4, !. d334 1 a334 1 setup_call_cleanup(S=1,(G=2;S=2),write(S+G>B)), B=3, !. d361 1 a361 3
    SICStus
    call_cleanup/2 since 1997 (origin). Open: setup_call_cleanup/3 d364 1 a364 3
    SWI
    almost fully compliant since Autumn 2007. Open: bindings (in d) for c2 d367 1 d369 2 a370 1
    YAP-6 d372 2 a373 1
    almost fully compliant since June 2009. Open: errors d and e. d375 1 a375 1
    Other Prologs d377 1 a377 3
    as of 2009-07 XSB, B-Prolog have call_cleanup/2, but sometimes the cleanup is not executed for c2 and c3.
    call_cleanup/2 (untested): CxProlog, Qu-Prolog @ 1.80 log @*** empty log message *** @ text @d380 1 @ 1.79 log @*** empty log message *** @ text @d368 1 a368 1
    almost fully compliant since Autumn 2007. Open: d for c2 @ 1.78 log @Change back as ulrich @ text @d378 2 a379 2
    as of 2009-07 XSB, B-Prolog have call_cleanup/2, but c2 and c3 do apply always. @ 1.77 log @*** empty log message *** @ text @a31 1 @ 1.76 log @date @ text @d127 1 a127 1
    c) Eventually, the cleanup handler is called exactly once; no @ 1.75 log @m @ text @d67 1 a67 1 2009-09-01 @ 1.74 log @Note 2 nonterminating @ text @d166 2 a167 2 When several cleanup goals are triggered simultaneously, the goals are executed in reversed order of their installation. d188 3 a190 1 The precise moment in c1 is implementation dependent due to the a193 2 In existing processors, c1 is either when the last solution is found, or upon failure. d293 2 a294 4 Or: outputs on backtracking '1+_' prior to failure. Or (?): outputs on backtracking '1+2' prior to failure. @ 1.73 log @date @ text @d198 1 a198 1 c3 can trigger the cleanup. And if such a goal does not find a @ 1.72 log @c @ text @d67 1 a67 1 2009-08-28 @ 1.71 log @c @ text @d127 1 a127 1
    c) Eventually, the cleanup handler is called exactly once and no @ 1.70 log @c1 @ text @d127 2 a128 2
    c) Eventually, the cleanup handler is called exactly once no later than upon failure of G. Earlier moments are: @ 1.69 log @c version 2 @ text @d134 1 a134 1
  • If G is true or false, C will be @ 1.68 log @c version 1 @ text @d127 2 a128 3
    c) Eventually, the cleanup handler is called exactly once. The latest moment for this is upon failure of G. Earlier moments are: @ 1.67 log @c improvements & notes @ text @d127 2 a128 2
    c) Eventually, the cleanup handler is called exactly once latest moment for this is upon failure of G. Earlier @ 1.66 log @c1 @ text @d127 3 a129 3
    c) The latest moment to call the cleanup handler C shall be the failure over G. d131 1 a131 1
    It is called earlier in the following situations: d142 3 a144 3 After executing C, the "ball" of throw/1 will be passed, regardless of the outcome of C. d193 2 d198 11 a208 5 There are several advantages for giving priority to the throw/1 of G in c2 in the context of error recovery. The error of C is often a consequence of the error of G. This is particularly true for resource errors (7.12.2 h). @ 1.65 log @First attempt to simplify c1. @ text @d135 3 a137 7
  • If G is true, C will be called at an implementation dependent moment after the last solution of G is found; and after the last observable effect of G. @ 1.64 log @m @ text @d107 1 a107 1
    a) once(S) is executed while being protected from interrupts: d109 1 d124 2 a125 2
    b) After the cleanup handler is installed, call(G) is called. d127 5 a131 3
    c) The cleanup handler is called after G has completed execution. It is called exactly once at one of the following moments: d135 2 a136 10
  • When G has completed execution by success or failure. The precise moment is implementation dependent within the interval defined by the following bounds:
    • after the cleanup handler is installed;
    • after the last solution of G is found, if G is true; d138 1 a138 1
    • after the last observable effect of G, if applicable; d140 1 a140 1
    • up to the failure of G. a141 1
    d164 1 a164 1
    d) d170 2 a171 1

    When several cleanup goals are triggered simultaneously, the goals @ 1.63 log @Reorder Notes to the end of clause 7.8.11.1. This corresponds to the convention of 13211-1:1995. @ text @d150 1 a150 1 executes a throw/1 (explicitly or implicitly — 7.8.9 a or d160 1 a160 1 (explicitly or implicitly) that is associated with the cutparent of G d196 1 a196 1

    1 d203 1 a203 1

    2 @ 1.62 log @First change on gupu2 @ text @a148 6

    NOTE — The precise moment is implementation dependent due to the varying ability of Prolog processors to detect determinism. For example, a processor may execute call((call(G);fail)) as call(G) and vice versa. d156 2 a157 15

    NOTES

    1. Thus, if C executes another relevant throw/1, that throw/1 is lost.
    2. There are several advantages for giving priority to the throw/1 of G in the context of error recovery. The error of C is often a consequence of the error of G. This is particularly true for resource errors (7.12.2 h).
    d194 16 @ 1.61 log @Highlighted improvable areas, e-mail Feliks Kluzniak, Wed, 26 Aug 2009 20:24:48 -0500 @ text @d67 1 a67 1 2009-08-25 @ 1.60 log @*** empty log message *** @ text @d124 1 a124 1 After the cleanup handler is installed, call(G) is called. d138 1 a138 1
  • after the cleanup handler is installed; d170 1 a170 1
  • There are several advantages for giving priority to @ 1.59 log @*** empty log message *** @ text @d67 1 a67 1 2009-08-24 d95 2 a96 2 To overcome this problem, setup_call_cleanup/3 protects a goal for setup in a separate argument. d132 1 a132 1
  • When G has completed execution normally.
    d151 2 a152 2 For example, a particular processor may execute a goal call((call(G);fail)) a188 3

    When several cleanup goals are triggered simltaneously, the goals are executed in reversed order of their installation. d193 4 a196 1 c3), it is passed to the next matching catch/3. d205 1 d325 2 a326 3 The file is closed, either immediately or on backtracking. In any case, even if there is an error in read, the file will be closed. d386 2 a387 2

    (currently, 2009-07) XSB, B-Prolog have call_cleanup/2 but do not guarantee that cleanup will be called eventually. @ 1.58 log @Use S G C as variable names instead of Setup Goal Cleanup. Simplifies some formulations. More uniform with 7.8.9 and other control construct @ text @d109 1 a109 1
      d121 1 a121 1
    @ 1.57 log @YAP @ text @d99 2 a100 2 setup_call_cleanup(Setup, Goal, Cleanup) is true iff once(Setup), call(Goal) is true. d103 1 a103 1 it d107 1 a107 1
    a) once(Setup) is executed while being protected from interrupts: d111 2 a112 2
  • either once(Setup) is executed entirely and the cleanup handler Cleanup is installed d124 1 a124 1 After the cleanup handler is installed, call(Goal) is called. d127 1 a127 1 The cleanup handler is called after Goal has completed d132 1 a132 1
  • When Goal has completed execution normally.
    d140 2 a141 2
  • after the last solution of Goal is found, if Goal is true; d143 1 a143 1
  • after the last observable effect of Goal, if applicable; d145 1 a145 1
  • up to the failure of Goal. d152 2 a153 2 goal call((call(Goal);fail)) as call(Goal) and vice versa. d155 1 a155 1
  • When Goal or the continuation of Goal d157 2 a158 2 b), whose corresponding call of catch/3 is above Goal.
    After executing Cleanup, the "ball" d160 1 a160 1 outcome of Cleanup. d166 1 a166 1
  • Thus, if Cleanup executes another d171 3 a173 3 the throw/1 of Goal in the context of error recovery. The error of Cleanup is often a consequence of the error of Goal. This is d178 2 a179 2
  • When the continuation of Goal executes a cut (explicitly or implicitly) that is associated with the cutparent of Goal d181 1 a181 1 After the cut has been executed, Cleanup is called in d193 2 a194 2 Cleanup is called as once(Cleanup). Failure of Cleanup is ignored. An explicit or d198 4 a201 4

    Cleanup shares bindings and variables with Setup, with Goal and with the continuation. The bindings up to and including Setup are always present. No further bindings are present when Cleanup is executed at c2 and at the latest moment of c1. At c2, and at the d205 1 a205 1

    A processor may restrict execution of Cleanup in an d207 1 a207 1 handling of interrupts within Cleanup or limit resource d219 1 a219 1

    a) Setup is a variable
    — d222 1 a222 1
    b) Setup is neither a variable nor a callable d224 1 a224 1 type_error(callable,Setup). d226 1 a226 1
    c) Setup is a callable term that is not permitted due d230 1 a230 1
    d) Setup finds a solution, but Cleanup is d232 1 a232 1 instantiation_error. Goal is not executed. d234 1 a234 1
    e) Setup finds a solution, but Cleanup is d236 1 a236 1 type_error(callable, Cleanup). Goal is not executed. d238 1 a238 1
    f) Cleanup is installed, but Goal d240 1 a240 1 instantiation_error. The Cleanup is executed. d242 1 a242 1
    g) Cleanup is installed, but Goal is d244 1 a244 1 type_error(callable, Goal). The Cleanup is d260 1 a260 1 Fails. Neither Goal nor Cleanup is executed. d371 2 a372 2
    call_cleanup/2 since 1997. (origin) Open: setup_call_cleanup/3 missing. Different handling of multiple simultaneous exceptions. d376 3 a378 2
    almost fully compliant since Autumn 2007. Open: unwinding for exceptions from aside. d387 1 a387 1 guarantee that Cleanup will be called eventually. @ 1.56 log @First weekend-change: Reordered errors, reduced implementation dependence @ text @d381 1 a381 2
    almost fully compliant since June 2009. Open: instantiation error for unbound Cleanup. @ 1.55 log @*** empty log message *** @ text @d67 1 a67 1 2009-08-19 d87 3 a89 3 NOTE — A built-in predicate call_cleanup/2 is implemented in many existing processors to provide similar functionality as setup_call_cleanup/3. It is often used d96 1 a96 1 goal in a separate argument. d102 3 d124 1 a124 2 After the cleanup handler is installed, the Goal is called as in call(Goal) with the cleanup handler present. d127 2 a128 1 The cleanup handler is called exactly once at one of the following moments: a177 4

    When several Cleanup goals are triggered, the goals are executed in reversed order of their installation. d179 1 a179 1 (explicitly or implicitly) that is associated with the cutparent of Goal d186 2 d189 2 a190 6

    When a cut will trigger several Cleanup goals, the order in which those goals are executed is implementation dependent. d206 1 a206 1 implementation dependent manner. For example, a processor may restrict the d230 9 a238 1

    d) Setup finds a solution, but Goal d242 1 a242 1
    e) Setup finds a solution, but Goal is a246 8
    f) Setup finds a solution, but Cleanup is a variable
    instantiation_error. Goal is not executed.
    g) Setup finds a solution, but Cleanup is neither a variable nor a callable term
    typ_error(callable, Cleanup). Goal is not executed. a349 1 a350 2

    Ambiguous cuts

    d352 1
    a352 2
       Either: Outputs 'ab'.  Succeeds, unifying X = 1, Y = 1.
       Or: Outputs 'ba'.  Succeeds, unifying X = 1, Y = 1.
    d382 1
    a382 1
    for unbound Cleanup, once for Setup due to Pasadena resolution
    @
    
    
    1.54
    log
    @Changes of Feliks Kluzniak
    Mail Fri, 21 Aug 2009 19:48:37 -0500
    @
    text
    @d138 1
    a138 1
          there are solutions;
    @
    
    
    1.53
    log
    @*** empty log message ***
    @
    text
    @d135 1
    a135 1
          
  • after calling Goal; d137 2 a138 1
  • after the last solution of Goal is found, if applicable; @ 1.52 log @Corrected 7.8.11.3 g, minor @ text @d203 1 a203 1 are present when Cleanup is executed at c2 and at the latest moment of c1. At c2, and at the @ 1.51 log @*** empty log message *** @ text @d67 1 a67 1 2009-08-18 d73 1 a73 1 References refer to 13211-1:1995. Parts needing improvements are underlined d100 1 a100 2 iff once(Setup), call(Goal) is true, and Cleanup is not replaced by throw/1. d115 1 d175 1 a175 1 younger goals is executed first. d234 1 a234 1 instantiation_error. The Cleanup is still executed. d239 1 a239 1 still executed. d247 1 a247 1 instantiation_error. Goal is not executed. d257 1 a257 3 realize 7.10.5 a)

    The different outcomes (labeled either/or) are due to the d275 1 a275 1 Or: Succeeds. Fails on backtracking d289 1 a289 2 Or: Succeeds, unifying S = 1, G = 2. Fails on backtracking. d299 4 a302 4 Or: outputs on backtracking '1+_', prior to failing. Or (?): outputs on backtracking '1+2', prior to failing. @ 1.50 log @Better justification of c2 @ text @d201 1 a201 1 with Setup, with Goal and with the continuation. The @ 1.49 log @State order of Cleanup goals for c2 and c3 @ text @d158 15 a172 2

    NOTE — Thus, if Cleanup executes another relevant throw/1, that throw/1 is lost. @ 1.48 log @Multiple exception case clarified @ text @d161 4 d174 4 d342 7 @ 1.47 log @Better mode, as Setup may instantiate Goal and Cleanup @ text @d158 2 a159 3

    NOTE — As a consequence, the "ball" of Cleanup is lost in case Cleanup executes another throw/1. @ 1.46 log @Many comments from Feliks Kluzniak Mail Fri, 14 Aug 2009 13:20:01 -0500 @ text @d196 1 a196 1 setup_call_cleanup(+callable_term,+callable_term, +callable_term) @ 1.45 log @*** empty log message *** @ text @d66 3 a68 1

    Ulrich Neumerkel, 2009-08-11 (Version history) d76 2 a77 1 (Portugal). Jonathan Hodgson (USA). Richard O'Keefe (New Zealand). Jeffrey Rosenwald (USA). Markus Triska d93 2 a94 1 call_cleanup/2 is unable to free the resource timely. d105 1 a105 1
    a) once(Setup) is executed protected from interrupts. d109 1 a109 1
  • Either once(Setup) is executed d111 1 a111 1 upon success. d113 1 a113 1
  • Or setup_call_cleanup/3 is interrupted by an d131 1 a131 1 within the interval defined by the following bounds. d135 1 a135 1
  • after calling Goal d137 1 a137 1
  • after the last solution of Goal, if applicable d139 1 a139 1
  • after the last observable effect of Goal, if applicable d141 1 a141 1
  • up to the failing of Goal d145 1 a145 1

    NOTE — The implementation dependence is due to d177 1 a177 1 implicit throw/1 is ignored for c2. Otherwise (c1 or d181 3 a183 3 with Setup, Goal and the continuation. The bindings of Setup are always present. No further bindings are present in c2 and at the latest moment of c1. In c2, and in the d187 2 a188 2

    A processor may restrict execution of Cleanup in an implementation dependent manner. E.g., a processor may restrict the d201 1 a201 1

    a) Setup is a variable.
    — d212 2 a213 2
    d) For the solution of Setup, Goal is a variable
    — d216 1 a216 1
    e) For the solution of Setup, Goal is d221 1 a221 1
    f) For the solution of Setup, Cleanup is d225 1 a225 1
    g) For the solution of Setup, Cleanup is d234 2 a235 2 In the following, write/1 outputs using standard operators (6.3.4.4, table 7). Unique variables are written d237 1 a237 1 realize the implementation dependent manner of 7.10.5 a) d373 1 @ 1.44 log @*** empty log message *** @ text @d214 1 a214 1 type_error(callable, Goal). The Cleanupx is @ 1.43 log @Better error formatting @ text @d1 1 a1 1 d66 1 a66 1
    Ulrich Neumerkel, 2009-08-11 (Version history) d99 1 a99 1
      d101 1 a101 2
    1. once(Setup) is executed protected from interrupts. d116 1 a116 1
    2. d120 1 a120 1
    3. d170 1 a170 2
    4. d188 1 a188 1
    d192 1 a192 1 setup_call_cleanup(+callable_term,+callable_term, +callable_term) d201 1 a201 1 term— d333 1 a333 1

    Multiple exceptions

    @ 1.42 log @*** empty log message *** @ text @d199 2 a200 1
    a) Setup is a variable.
    — instantiation_error. d202 24 a225 18
    b) Setup is neither a variable nor a callable term— type_error(callable,Setup).
    c) Setup is a callable term that is not permitted due to an implementation defined restriction
    — representation_error(setup_goal).
    d) For the solution of Setup, Goal is a variable
    — instantiation_error. The Cleanup is still executed.
    e) For the solution of Setup, Goal is neither a variable nor a callable term
    — type_error(callable, Goal). The Cleanup is still executed.
    f) For the solution of Setup, Cleanup is a variable
    &mdash instantiation_error. Goal is not executed.
    g) For the solution of Setup, Cleanup is neither a variable nor a callable term
    — instantiation_error. Goal is not executed. d362 1 a362 1 for unbound Cleanup, once for Setup due to Pasadena @ 1.41 log @Entered Jan. @ text @d5 2 d8 2 d66 1 a66 1
    Ulrich Neumerkel, 2009-08-11 (Version history) @ 1.40 log @*** empty log message *** @ text @d71 1 a71 1 (Austria). @ 1.39 log @Official header @ text @d5 1 a5 1 N211 Draft proposal for setup_call_cleanup/3 @ 1.38 log @Uncommented REL links @ text @d5 1 a5 1 N211 Cleanup Mechanism d23 6 d60 1 a60 2

    N211 Cleanup mechanism for ISO Prolog

    @ 1.37 log @*** empty log message *** @ text @d6 1 d11 1 @ 1.36 log @Clearer error conditions @ text @d80 2 a81 2 To overcome this problem, setup_call_cleanup/3 protects goals in a separate argument. @ 1.35 log @Added link to WG17 Document register @ text @d55 1 a55 1
    Ulrich Neumerkel, 2009-07-21 (Version history) d196 1 a196 1
    d) For a solution of Setup, Goal is a variable
    — d199 1 a199 1
    e) For a solution of Setup, Goal is neither a variable nor a d203 1 a203 1
    f) For a solution of Setup, Cleanup is a variable
    &mdash d206 1 a206 1
    g) For a solution of Setup, Cleanup is neither a variable nor a @ 1.34 log @Further reference @ text @d52 2 a53 1

    N211 Cleanup mechanism for ISO Prolog

    @ 1.33 log @Reference for cutparent @ text @d149 1 a149 1 (7.8.4.1 Note 1b).
    @ 1.32 log @Document number N211 added @ text @d148 2 a149 1 (explicitly or implicitly) that is associated with the cutparent of Goal.
    @ 1.31 log @YAP-6 missing @ text @d5 1 a5 1 Cleanup Mechanism d52 1 a52 1

    Cleanup mechanism for ISO Prolog

    d57 2 a58 2 This draft is based on Paulo Moura's draft core 8.15.5 13211-1:2006 of 2008-11-17. It is intended to be added as 7.8.11. @ 1.30 log @Highlight underlined parts that need to be improved @ text @d342 1 a342 1 for unbound Cleanup. @ 1.29 log @SWI status updated @ text @d45 3 d59 1 a59 1 References refer to 13211-1:1995. d85 1 a85 1 and Cleanup is not replaced by throw/1. @ 1.28 log @renumbering from 8.15.5 to 7.8.11 @ text @d334 1 a334 1 exceptions from aside. Instantiation error for unbound Cleanup. @ 1.27 log @Correction SWI status @ text @d62 1 a62 1

    8.15.5 setup_call_cleanup/3

    d79 1 a79 1

    8.15.5.1 Description

    d177 1 a177 1

    8.15.5.2 Template and modes

    d180 1 a180 1

    8.15.5.3 Errors

    d207 1 a207 1

    8.15.5.4 Examples

    @ 1.26 log @Date updated @ text @d334 1 a334 2 exceptions from aside. Instantiation error for unbound Cleanup. There is only one call to Cleanup (Manual is correct though). @ 1.25 log @Pasadena WG17 comments @ text @d51 1 a51 1
    Ulrich Neumerkel, 2009-07-02 (Version history) @ 1.24 log @Wording. Richard O'Keefe. @ text @d55 1 a55 1 13211-1:2006 of 2008-11-17. It is intended to replace 8.15.5 in it. d59 1 a59 1 (Portugal). Richard O'Keefe (New Zealand). Jeffrey Rosenwald (USA). Markus Triska d76 1 a76 1 For this reason, setup_call_cleanup/3 protects goals in d81 1 a81 1 iff call(Setup), call(Goal) is true, d87 1 a87 1 Setup is executed protected from interrupts. d91 1 a91 1
  • Either Setup is executed d93 1 a93 1 for each solution of Setup. d103 1 a103 2 After the cleanup handler is installed for a solution of Setup, the Goal is called as d145 1 a145 1 (explicitly or implicitly) that concerns the cutparent of Goal.
    d231 2 a232 1 Succeeds, unifying X = 2. d238 2 a239 1 System error due to uncaught ex. d250 3 a252 9 a) Either: Succeeds, unifying S = 1, G = 3, C = 4. On backtracking proceed to b) Or: Succeeds, unifying S = 1, G = 3. On backtracking proceed to b) b) Either: Succeeds, unifying S = 2, G = 3, C = 4. Or: Succeeds, unifying S = 1, G = 3. Fails on backtracking. d296 1 a296 1 Or: Outputs '1+2>_'. Succeeds, unifying @ 1.23 log @Modifications due to Bart Demoen's comments in the SWI-Prolog mailing list. @ text @d58 3 a60 2

    Contributors

    Paulo Moura (Portugal). Jeffrey Rosenwald (USA). Markus Triska (Austria). d64 1 a64 1 This control construct allows to manage resources related to the @ 1.22 log @Added remark on XSB, B-Prolog. Paulo Moura @ text @d107 1 a107 1 The cleanup handler is called exactly once in one of the following moments: d135 1 a135 1 b), whose matching catch/3 is above Goal.
    d165 1 a165 1 are present in c2 and the latest moment of c1. In c2, and in the @ 1.21 log @Name correction, Jeffrey Rosenwald @ text @d348 2 a349 2
    (currently, 2009-07) Many have call_cleanup/2 but do not guarantee that Cleanup will be called eventually. @ 1.20 log @Further SWI specific difference. Markus Triska @ text @d58 1 a58 1

    Contributors

    Paulo Moura (Portugal). Jeff Rose (USA). Markus Triska @ 1.19 log @*** empty log message *** @ text @d338 2 a339 1 exceptions from aside. Instantiation error for unbound Cleanup. @ 1.18 log @*** empty log message *** @ text @d56 1 a56 1 References are referring to 13211-1:1995. @ 1.17 log @draft @ text @d56 1 a56 1 References below are referring to 13211-1:1995. @ 1.16 log @Call for comments @ text @d54 1 a54 1 This document is based on Paulo Moura's draft core 8.15.5 @ 1.15 log @*** empty log message *** @ text @d111 1 a111 1
  • When Goal has completed execution normally. d117 1 a117 1
  • after the cleanup handler is installed d135 1 a135 1 b), whose matching catch/3 is above Goal. d145 3 a147 3 (explicitly or implicitly) that concerns the cutparent of Goal. Cleanup is called in the place of the cut, after the cut has been executed. d157 1 a157 2 When Cleanup is called, it is called as once(Cleanup). Failure @ 1.14 log @*** empty log message *** @ text @d111 3 a113 3
  • Within the interval defined by the following bounds. The precise moment in this interval is implementation dependent and may thus be overshadowed by 2 or 3. d127 5 a131 5

    NOTE — The implementation dependence is due to the varying abilities of Prolog processors to detect determinism. For example, a particular processor may execute a goal call(call(Goal) ; fail) as call(Goal) and vice versa. d148 3 a150 2 A cut is performed implicitly for (->)/2 - if-then (7.8.7), (;)/2 - if-then-else (7.8.8), (\+)/1 (8.15.1), once/1 (8.15.2). @ 1.13 log @*** empty log message *** @ text @d86 1 a86 3 Setup is executed, while being protected from implementation dependent interrupts (7.12 note c). I.e. one of the following steps are performed. d91 2 a92 1 entirely and the cleanup handler Cleanup is installed. d94 3 a96 1

  • Or Setup is interrupted without leaving an d102 3 a104 2 After the cleanup handler is installed, the Goal is called as in call(Goal) with the cleanup handler present. d111 15 a125 5
  • Within the following interval. After the last solution of Goal and after the last observable effect of Goal — whichever comes last — and up to the failing of Goal. The precise moment is implementation dependent and may thus be overshadowed by 2 or 3. @ 1.12 log @*** empty log message *** @ text @d239 1 a239 1 Or: Succeeds, unifying S = 1, G = 2. @ 1.11 log @*** empty log message *** @ text @d87 12 a98 4 dependent interrupts (7.12 note c). I.e., either Setup is executed entirely and a Cleanup handler is installed. Or: Setup is interrupted prior to having performed an observable effect. d157 5 d203 1 a203 1 implementation dependent manner of being able to detect determinism. @ 1.10 log @*** empty log message *** @ text @d71 1 a71 1 to free temporary resources. In the presence of interrupts (7.12 NOTE d75 2 a76 2 For this reason, there is a separate argument for protected goals in setup_call_cleanup/3. d81 1 a81 1 and Cleanup did not execute a throw/1. d87 1 a87 1 dependent interrupts. I.e., either Setup is executed d101 3 a103 2
  • Within the interval: After the last solution of Goal and after the last observable effect of Goal and up to the failing @ 1.9 log @*** empty log message *** @ text @d56 1 a56 1 Below, we are referring to 13211-1:1995. d71 3 a73 3 to free temporary resources. In the presence of interrupts, call_cleanup/2 can cause leakage of resources: A processor receiving interrupts between resource allocation and d75 2 a76 2 For this reason, setup_call_cleanup/3 takes goals to be protected as a separate argument. d79 3 a81 3 setup_call_cleanup(Setup, Goal, Cleanup) is true iff call(Setup), call(Goal) is true, and Cleanup did not execute a throw/1. d86 5 a90 5 Setup is executed, being protected from implementation dependent interrupts (7.12 NOTE c). This means that either, Setup is interrupted prior to having performed some visible effect, or it is executed entirely. The protection includes the installation of a Cleanup handler. d108 1 a108 1 varying indexing abilities of Prolog processors. A @ 1.8 log @Comments Markus Triska @ text @d81 1 a81 1 produce an exception. d97 1 a97 1 The cleanup handler is called in one of the following situations: d101 10 a110 3
  • In the interval after the last solution of Goal and after the last observable effect of Goal and up to the failing of Goal. The precise moment is implementation dependent. d114 1 a114 2 b), whose matching catch/3 is above Goal, Cleanup is called as if Goal fails. d116 1 a116 1 of throw/1 is passed, regardless of the d119 3 a121 2
    NOTE — In case Cleanup executes another throw/1, the "ball" of Cleanup is lost. d125 2 d133 1 a133 9 The cleanup handler shares the bindings with Setup and Goal. (NOTE No copying takes place as in throw/1). When the cleanup is called, the bindings of the setup are all present, whereas the bindings of goal depend on the specific situation.
    The cleanup is called as once(Cleanup). Success or failure is always ignored. d135 12 a146 25
    A throw/1 is passed on.
    NOTES
    1. For each solution of Setup and thus for each invocation of Goal the Cleanup will be called exactly once.
    2. The implementation dependence of 1c is caused by the varying indexing abilities of Prolog processors. Note, that a particular processor may execute a goal call(call(Goal) ; fail) as call(Goal) and vice versa.
    3. When a cut is encountered, removing otherwise possible solutions, the Cleanup is called at the place (and with the bindings) of the cut.
    4. A Cleanup triggered by a ! is performed after the ! has removed alternatives. a149 1
    @ 1.7 log @*** empty log message *** @ text @d242 1 a242 1 Or: Succeeds, unifying S = 1, G = 2. @ 1.6 log @*** empty log message *** @ text @d134 4 @ 1.5 log @*** empty log message *** @ text @d80 1 a80 1 iff call(Setup), call(Goal) is true, and Cleanup did not d114 1 a114 1 another throw/1, this "ball" is lost. @ 1.4 log @*** empty log message *** @ text @d72 1 a72 1 interrupts call_cleanup/2 can cause leakage of resources: d110 1 a110 1 of throw/1 is passed, regardless @ 1.3 log @Spelling @ text @d148 1 a148 1 fail) as call(Goal). @ 1.2 log @Note style @ text @d106 1 a106 1 executes a throw/1 (explicitely or implicitely — 7.8.9 a or d117 2 a118 2 (explicitely or implicitely) that concerns the cutparent of Goal. A cut is performed implictely for (->)/2 - if-then (7.8.7), (;)/2 - d127 1 a127 1 the setup are all present, wheras the bindings of goal depend on the d146 1 a146 1 varying indexing abilties of Prolog processors. Note, that a d197 1 a197 1

    The different outcomes (labelled either/or) are due to the d211 1 a211 1 Suceeds, unifying X = 1. @ 1.1 log @Initial revision @ text @d16 5 d51 1 a51 1

    Ulrich Neumerkel, 2009-07-02 d67 2 a68 1

    NOTE — A built-in predicate call_cleanup/2 is d76 1 a76 1 protected as a separate argument. d136 2 a137 1
    NOTES d157 2 @