From 0409449a23e76b85b48cd6af75cc801be2a04663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Fri, 18 Nov 2011 16:26:11 +0000 Subject: [PATCH 1/2] expand interface to create list from array of terms. --- C/c_interface.c | 39 +++++++++++++++++++++++++++++++++++++++ docs/yap.tex | 11 +++++++++++ include/YapInterface.h | 2 ++ 3 files changed, 52 insertions(+) diff --git a/C/c_interface.c b/C/c_interface.c index cac674149..271e72638 100644 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -412,6 +412,7 @@ X_API int STD_PROTO(YAP_IsWideAtom,(Atom)); X_API char *STD_PROTO(YAP_AtomName,(Atom)); X_API wchar_t *STD_PROTO(YAP_WideAtomName,(Atom)); X_API Term STD_PROTO(YAP_MkPairTerm,(Term,Term)); +X_API Term STD_PROTO(YAP_MkListFromTerms,(Term *,Int)); X_API Term STD_PROTO(YAP_MkNewPairTerm,(void)); X_API Term STD_PROTO(YAP_HeadOfTerm,(Term)); X_API Term STD_PROTO(YAP_TailOfTerm,(Term)); @@ -982,6 +983,44 @@ YAP_MkPairTerm(Term t1, Term t2) return t; } +X_API Term +YAP_MkListFromTerms(Term *ta, Int sz) +{ + CACHE_REGS + Term t; + CELL *h; + if (sz == 0) + return TermNil; + BACKUP_H(); + if (H+sz*2 > ASP-1024) { + Int sl1 = Yap_InitSlot((CELL)ta PASS_REGS); + RECOVER_H(); + if (!dogc( PASS_REGS1 )) { + return TermNil; + } + BACKUP_H(); + ta = (CELL *)Yap_GetFromSlot(sl1 PASS_REGS); + Yap_RecoverSlots(1 PASS_REGS); + } + h = H; + t = AbsPair(h); + while (sz--) { + Term ti = *ta++; + if (IsVarTerm(ti)) { + RESET_VARIABLE(h); + Yap_unify(ti, h[0]); + } else { + h[0] = ti; + } + h[1] = AbsPair(h+2); + h += 2; + } + h[-1] = TermNil; + H = h; + RECOVER_H(); + return t; +} + X_API Term YAP_MkNewPairTerm() { diff --git a/docs/yap.tex b/docs/yap.tex index c3de0939b..dea9938de 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -16351,6 +16351,7 @@ hook on garbage collection: @findex YAP_MkNewPairTerm (C-Interface function) @findex YAP_HeadOfTerm (C-Interface function) @findex YAP_TailOfTerm (C-Interface function) +@findex YAP_MkListFromTerms (C-Interface function) A @i{pair} is a Prolog term which consists of a tuple of two Prolog terms designated as the @i{head} and the @i{tail} of the term. Pairs are most often used to build @emph{lists}. The following primitives can be @@ -16360,11 +16361,18 @@ used to manipulate pairs: YAP_Term YAP_MkNewPairTerm(void) YAP_Term YAP_HeadOfTerm(YAP_Term @var{t}) YAP_Term YAP_TailOfTerm(YAP_Term @var{t}) + YAP_Term YAP_MkListFromTerms(YAP_Term *@var{pt}, YAP_Int *@var{sz}) @end example One can construct a new pair from two terms, or one can just build a pair whose head and tail are new unbound variables. Finally, one can fetch the head or the tail. +The last function supports the common operation of constructing a list from an +array of terms of size @var{sz} in a simple sweep. + +Notice that the list constructors can call the garbage collector if +there is not enough space in the global stack. + @findex YAP_MkApplTerm (C-Interface function) @findex YAP_MkNewApplTerm (C-Interface function) @findex YAP_ArgOfTerm (C-Interface function) @@ -16392,6 +16400,9 @@ to a compound term. @code{argno} should be greater or equal to 1 and less or equal to the arity of the functor. @code{YAP_ArgsOfTerm} returns a pointer to an array of arguments. +Notice that the compound term constructors can call the garbage +collector if there is not enough space in the global stack. + YAP allows one to manipulate the functors of compound term. The function @code{YAP_FunctorOfTerm} allows one to obtain a variable of type @code{YAP_Functor} with the functor to a term. The following functions diff --git a/include/YapInterface.h b/include/YapInterface.h index ebb20ff53..069fdfd0c 100644 --- a/include/YapInterface.h +++ b/include/YapInterface.h @@ -181,6 +181,8 @@ extern X_API CONST wchar_t *PROTO(YAP_WideAtomName,(YAP_Atom)); /* YAP_Term MkPairTerm(YAP_Term Head, YAP_Term Tail) */ extern X_API YAP_Term PROTO(YAP_MkPairTerm,(YAP_Term,YAP_Term)); +extern X_API YAP_Term PROTO(YAP_MkListFromTerms,(YAP_Term *,YAP_Int)); + /* YAP_Term MkNewPairTerm(void) */ extern X_API YAP_Term PROTO(YAP_MkNewPairTerm,(void)); From ae29efec0c563b3875cea5de6099ca39ade278d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Fri, 18 Nov 2011 16:26:41 +0000 Subject: [PATCH 2/2] iupdate version. --- packages/YapR | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/YapR b/packages/YapR index 91e16377c..f218eaacc 160000 --- a/packages/YapR +++ b/packages/YapR @@ -1 +1 @@ -Subproject commit 91e16377cb763014f23f39b81ecfbff902274dab +Subproject commit f218eaacc9ef3922829ff4a8c5cd64cca9c19dc7