項ベクトルの中で項に上書きします。
tv_set/3は、項ベクトルVectorの中のインデックスIndexで、項をTermと取り替えます。 インデックスはレンジ1 .. Nにおける整数でなければなりません、そこで、Nは項ベクトルのサイズです。(tv_size/2を参照します)
Vector オブジェクト(項ベクトル) Index 整数 Term 項
% find all prime numbers between 1 and N
prime_sieve(N, Primes) :-
% create a term vector
tv_create(Vector),
% set it's size to N and fill all elements to 'prime'
init_sieve(Vector, N),
% set all non prime numbers to 'not_prime'
compute_primes(Vector, N),
% collect all elements still marked with 'prime'
collect_primes(Vector, N, Primes).
init_sieve(Vector, N) :-
% set the vector size to N
tv_setsize(Vector, N),
% set all vector elements to 'prime'
for(1, I, N),
tv_set(Vector, I, prime),
fail.
init_sieve(_, _).
compute_primes(Vector, N) :-
for(2, I, N),
% if I is still marked as prime ...
tv_get(Vector, I, prime),
% ... mark all multiples of I as 'not_prime'
filter_primes(Vector, I, I, N),
fail.
compute_primes(_, _).
filter_primes(Vector, I, Step, N) :-
J is I+Step, J =< N, !,
% mark all multiples of 'Step' as 'not_prime'
tv_set(Vector, J, not_prime),
filter_primes(Vector, J, Step, N).
filter_primes(_, _, _, _).
collect_primes(Vector, N, Primes) :-
% collect all elements still marked as 'prime'
findall(I, isprime(Vector,I,N), Primes).
isprime(Vector, I, N) :-
for(1,I,N),
tv_get(Vector,I,prime).
この述語は、ISO-Prolog 標準の要件ではありません。