head 1.17; access; symbols; locks ulrich:1.17; strict; comment @# @; 1.17 date 2023.07.07.16.47.49; author ulrich; state Exp; branches; next 1.16; 1.16 date 2023.07.06.17.14.50; author ulrich; state Exp; branches; next 1.15; 1.15 date 2023.06.26.08.03.36; author ulrich; state Exp; branches; next 1.14; 1.14 date 2023.06.26.07.56.43; author ulrich; state Exp; branches; next 1.13; 1.13 date 2023.06.10.16.13.43; author ulrich; state Exp; branches; next 1.12; 1.12 date 2023.06.10.15.22.37; author ulrich; state Exp; branches; next 1.11; 1.11 date 2022.08.05.17.26.12; author ulrich; state Exp; branches; next 1.10; 1.10 date 2022.05.18.09.25.58; author ulrich; state Exp; branches; next 1.9; 1.9 date 2022.05.13.06.26.08; author ulrich; state Exp; branches; next 1.8; 1.8 date 2022.04.19.10.45.31; author ulrich; state Exp; branches; next 1.7; 1.7 date 2022.04.19.10.39.51; author ulrich; state Exp; branches; next 1.6; 1.6 date 2022.04.19.10.35.22; author ulrich; state Exp; branches; next 1.5; 1.5 date 2022.04.19.08.24.24; author ulrich; state Exp; branches; next 1.4; 1.4 date 2022.04.19.08.22.28; author ulrich; state Exp; branches; next 1.3; 1.3 date 2022.04.19.08.14.09; author ulrich; state Exp; branches; next 1.2; 1.2 date 2022.04.19.08.11.49; author ulrich; state Exp; branches; next 1.1; 1.1 date 2022.04.19.08.08.37; author ulrich; state Exp; branches; next ; desc @@ 1.17 log @Description updated @ text @ post-N288 call_nth/2

post-N288 call_nth/2.

Ulrich Neumerkel (Version history)
This draft is part of the Prolog prologue. References refer to 13211-1:1995. Parts needing improvements are underlined.

p.p.9 call_nth/2

p.p.9.1 Description

call_nth(G_0, Nth) is true iff call(G_0) is true and N being the number of re-executions of call(G_0), Nth unifies with the integer N+1.

Procedurally, call_nth(G_0, Nth) is executed as follows:

a) Creates a counter N with value zero,
b) If Nth is an integer smaller or equal N, the goal fails,
or b') If there is no value greater N that is unifiable with Nth, the goal fails,
c) (Re-)executes call(G_0),
d) If it fails, the goal fails,
e) Else if it succeeds, sets N to N+1,
f) Unifies Nth with counter N,
g) If the unification succeeds, the goal succeeds,
h) Else proceeds to p.p.9.1 b,
call_nth(G_0, Nth) is re-executable. On backtracking, continue at p.p.9.1 g.

p.p.9.2 Template and modes

call_nth(?term, ?integer)

p.p.9.3 Errors

a) G_0 is a variable and Nth is not zero
instantiation_error.
b) G_0 is neither a variable nor a callable term
and Nth is not zero
type_error(callable, G_0).
c) Nth is neither a variable nor an integer
type_error(integer, Nth).
d) Nth is an integer less than zero
domain_error(not_less_than_zero, Nth).
e) The value of flag bounded is true and at step
p.p.9.1 e N+1 is larger than max_integer
evaluation_error(int_overflow).
representation_error(max_integer).

p.p.9.4 Examples

?- call_nth(true, Nth).
   Nth = 1.

?- call_nth(false, Nth).
   false.

?- call_nth(repeat, Nth).
   Nth = 1
;  Nth = 2
;  Nth = 3
;  Nth = 4
;  Nth = 5
;  ... .

?- call_nth(( N = 1 ; N = 2 ), Nth).
   N = 1, Nth = 1
;  N = 2, Nth = 2.

?- call_nth(true, non_integer).
   type_error(integer,non_integer).

?- call_nth(true, 1.0).
   type_error(integer,1.0).

?- call_nth(true, 0).
   false.

?- call_nth(repeat, 0).
   false.

?- call_nth(repeat, -1).
   domain_error(not_less_than_zero,-1).

?- call_nth(length(L,N), 3).
   L = [_A,_B], N = 2.

?- call_nth(inex, 0).
   false. % thus not existence_error(procedure,inex/0)

?- call_nth(inex, 0).
   existence_error(procedure,_), unexpected.

?- call_nth(1, 0).
   false.

?- call_nth(V, 0).
   false.

?- call_nth(N = 1, N).
   N = 1.

?- call_nth(N = -1, N).
   false.

?- call_nth(repeat,1+1).
   type_error(integer,1+1).

History


2008-01-27 First definition for SWI
2012-07-09 Implementations for SWI, SICStus, YAP, ECLiPSe.
2017-10-21 Added to SWI
2020-08-22 Added to Scryer library
2020-09-01 Added to Trealla
2022-04-24 Added to Ichiban
2022-06-06 Added to Prolog prologue
2023-06-25 Added to GNU Prolog
2023-07-03 Added to YAP
Validated HTML @ 1.16 log @+YAP @ text @d83 2 a84 2 true and Nth unifies with the integer being the N+1st re-execution of call(G_0). @ 1.15 log @*** empty log message *** @ text @d199 3 d214 1 @ 1.14 log @*** empty log message *** @ text @d196 3 @ 1.13 log @Cleanup from previous alternative @ text @d206 2 @ 1.12 log @*** empty log message *** @ text @d189 1 a189 1 false. % or type_error(callable,1) d192 1 a192 1 false. % or instantiation_error @ 1.11 log @post, example added @ text @d71 1 a71 1 This draft is proposed for consideration of WG17 for its inclusion into the Prolog d77 1 a77 1

n.n.n call_nth/2

d80 1 a80 1

n.n.n.1 Description

d109 1 a109 1
h) Else proceeds to n.n.n.1 b, d114 1 a114 1 continue at n.n.n.1 g. d117 1 a117 1

n.n.n.2 Template and modes

d119 2 a120 5 call_nth(?term, ?integer)
or
call_nth(+callable_term, ?integer)

n.n.n.3 Errors

d137 1 a137 1 at step
n.n.n.1 e N+1 is larger d143 1 a143 1

n.n.n.4 Examples

@ 1.10 log @N288 @ text @d7 1 a7 1 N288 call_nth/2N288 call_nth/2. d196 3 @ 1.9 log @*** empty log message *** @ text @d7 1 a7 1 call_nth/2call_nth/N. d71 2 a72 2 This draft is proposed for consideration of WG17. d201 2 a202 3
2012-07-09
First public mentioning, implementations for SWI, SICStus, YAP, ECLiPSe.
2017-10-21 Added to SWI library d204 2 a205 1
2020-09-01 Added to Trealla as an implementatin specific built-in (qua 5.5.9) @ 1.8 log @*** empty log message *** @ text @d140 4 a143 2 at step
n.n.n.1 e N+1 is larger than max_integer
representation_error(max_integer). d187 3 @ 1.7 log @m @ text @d149 1 d152 1 d160 1 d164 1 d167 1 d170 1 d173 1 d176 1 d179 1 d182 1 d185 1 d188 1 @ 1.6 log @Complete change of procedural description @ text @d121 1 a121 1 call_nth(+callable_term, ?integer)or d130 1 a130 1 and Nth is not zero— d140 1 a140 2 at step n.n.n.1 e N+1 is larger than max_integer — @ 1.5 log @These comma rules... @ text @d91 1 a91 1
a) Creates a counter N with value one, d93 2 a94 1
b) Executes call(G_0), d96 2 a97 1
b) If it fails, the goal fails, d99 1 a99 1
c) Else if it succeeds, proceeds to step n.n.n.1 d, d101 1 a101 1
d) Unifies Nth with counter N, d103 1 a103 1
e) If the unification succeeds, the goal succeeds, d105 1 a105 1
f) Else proceeds to n.n.n.1 g, d107 1 a107 1
g) Re-executes call(G_0), d109 1 a109 1
h) Proceeds to n.n.n.1 b. d119 3 a121 3 call_nth(+callable_term, ?integer)
or
call_nth(?term, ?integer) d139 4 @ 1.4 log @.1 was missing @ text @d97 1 a97 1
c) Else if it succeeds, proceeds to step n.n.n.1 d. d103 1 a103 1
f) Else proceeds to n.n.n.1 g. @ 1.3 log @s: more descriptive, less imperative @ text @d97 1 a97 1
c) Else if it succeeds, proceeds to step n.n.n d. d103 1 a103 1
f) Else proceeds to n.n.n g. d107 1 a107 1
h) Proceeds to n.n.n b. @ 1.2 log @Added history @ text @d93 1 a93 1
b) Execute call(G_0), @ 1.1 log @Initial revision @ text @d68 2 a69 2 2022-04-02 @