diff --git a/C/dbase.c b/C/dbase.c index 2ff92d08a..b09b672f4 100644 --- a/C/dbase.c +++ b/C/dbase.c @@ -3993,7 +3993,7 @@ find_next_clause(DBRef ref0) UNLOCK(cl->ClLock); } #else - if (!DynamicFlags(newp) & InUseMask) { + if (!(DynamicFlags(newp) & InUseMask)) { DynamicFlags(newp) |= InUseMask; TRAIL_CLREF(ClauseCodeToDynamicClause(newp)); } @@ -5489,7 +5489,7 @@ Yap_InitDBPreds(void) Yap_InitCPred("total_erased", 4, p_total_erased, SyncPredFlag); Yap_InitCPred("key_erased_statistics", 5, p_key_erased_statistics, SyncPredFlag); Yap_InitCPred("heap_space_info", 3, p_heap_space_info, SyncPredFlag); - Yap_InitCPred("$nth_instance", 4, p_nth_instance, SyncPredFlag); + Yap_InitCPred("$nth_instance", 3, p_nth_instance, SyncPredFlag); Yap_InitCPred("$nth_instancep", 3, p_nth_instancep, SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$jump_to_next_dynamic_clause", 0, p_jump_to_next_dynamic_clause, SyncPredFlag|HiddenPredFlag); Yap_InitCPred("$install_thread_local", 2, p_install_thread_local, SafePredFlag|HiddenPredFlag); diff --git a/C/index.c b/C/index.c index ff8221847..6e72ac832 100644 --- a/C/index.c +++ b/C/index.c @@ -7132,12 +7132,12 @@ Yap_NthClause(PredEntry *ap, Int ncls) yamop **jlbl = NULL; /* search every clause */ - if (ncls == 1) + if (ncls > ap->cs.p_code.NOfClauses) + return NULL; + else if (ncls == 1) return to_clause(ap->cs.p_code.FirstClause,ap); else if (ncls == ap->cs.p_code.NOfClauses) return to_clause(ap->cs.p_code.LastClause,ap); - else if (ncls > ap->cs.p_code.NOfClauses) - return NULL; else if (ncls < 0) return NULL; diff --git a/docs/yap.tex b/docs/yap.tex index 40e9e5f71..b10e70b70 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -5966,16 +5966,6 @@ elements of the internal data-base that match the key. match the reference. @end itemize -@item nth_instance(?@var{K},?@var{Index},@var{T},?@var{R}) -@findex nth_recorded/3 -@saindex nth_recorded/3 -@cnindex nth_recorded/3 -Fetches the @var{Index}nth entry in the internal database under the -key @var{K}. Entries are numbered from one. If the key @var{K} are the -@var{Index} are bound, a reference is unified with @var{R}. Otherwise, -the reference @var{R} must be given, and the term the system will find -the matching key and index. - @item erase(+@var{R}) @findex erase/1 @saindex erase/1 @@ -6015,6 +6005,27 @@ Defines the relation: @var{K} is a currently defined database key whose name is the atom @var{A}. It can be used to generate all the keys for the internal data-base. +@item nth_instance(?@var{Key},?@var{Index},?@var{R}) +@findex nth_instance/3 +@saindex nth_instance/3 +@cnindex nth_instance/3 +Fetches the @var{Index}nth entry in the internal database under the key +@var{Key}. Entries are numbered from one. If the key @var{Key} or the +@var{Index} are bound, a reference is unified with @var{R}. Otherwise, +the reference @var{R} must be given, and YAP will find +the matching key and index. + + +@item nth_instance(?@var{Key},?@var{Index},@var{T},?@var{R}) +@findex nth_instance/4 +@saindex nth_instance/4 +@cnindex nth_instance/4 +Fetches the @var{Index}nth entry in the internal database under the key +@var{Key}. Entries are numbered from one. If the key @var{Key} or the +@var{Index} are bound, a reference is unified with @var{R}. Otherwise, +the reference @var{R} must be given, and YAP will find +the matching key and index. + @item key_statistics(+@var{K},-@var{Entries},-@var{Size},-@var{IndexSize}) @findex key_statistics/4 @snindex key_statistics/4 diff --git a/pl/utils.yap b/pl/utils.yap index 8a3544523..b81a7a69b 100644 --- a/pl/utils.yap +++ b/pl/utils.yap @@ -472,12 +472,20 @@ callable(V) :- var(V), !, fail. callable(V) :- atom(V), !. callable(V) :- functor(V,_,Ar), Ar > 0. -nth_instance(X,Y,Z) :- - nonvar(X), var(Y), var(Z), !, - recorded(X,_,Z), - '$nth_instance'(_,Y,Z). -nth_instance(X,Y,Z) :- - '$nth_instance'(X,Y,Z). +nth_instance(Key,Index,Ref) :- + nonvar(Key), var(Index), var(Ref), !, + recorded(Key,_,Ref), + '$nth_instance'(_,Index,Ref). +nth_instance(Key,Index,Ref) :- + '$nth_instance'(Key,Index,Ref). + +nth_instance(Key,Index,T,Ref) :- + nonvar(Key), var(Index), var(Ref), !, + recorded(Key,T,Ref), + '$nth_instance'(_,Index,Ref). +nth_instance(Key,Index,T,Ref) :- + '$nth_instance'(Key,Index,Ref), + instance(Ref,T). nb_current(GlobalVariable, Val) :- var(GlobalVariable), !,