This commit is contained in:
Vitor Santos Costa 2019-02-14 00:38:14 +00:00
parent 71940dec67
commit 302519868f
4 changed files with 480 additions and 467 deletions

208
C/terms.c
View File

@ -29,7 +29,7 @@
#include "YapHeap.h" #include "YapHeap.h"
#define debug_pop_text_stack(l) [ if (to_visit != to_visit0) printf("%d\n",__LINE__); pop_text_stack(l) \ #define debug_pop_text_stack(l) [ if (to_visit != to_visit0) printf("%d\n",__LINE__); pop_text_stack(l) \
} }
#include "attvar.h" #include "attvar.h"
#include "yapio.h" #include "yapio.h"
@ -90,26 +90,26 @@ static inline void clean_tr(tr_fr_ptr TR0 USES_REGS) {
//} non_singletons_t; //} non_singletons_t;
#define IS_VISIT_MARKER \ #define IS_VISIT_MARKER \
(IsPairTerm(d0) && RepPair(d0) >= (CELL *)to_visit0 && \ (IsPairTerm(d0) && RepPair(d0) >= (CELL *)to_visit0 && \
RepPair(d0) <= (CELL *)to_visit) RepPair(d0) <= (CELL *)to_visit)
#define VISIT_MARKER AbsPair((CELL *)to_visit) #define VISIT_MARKER AbsPair((CELL *)to_visit)
#define CYC_MARK_LIST \ #define CYC_MARK_LIST \
if (IsPairTerm(d0) && RepPair(d0) >= (CELL *)to_visit0 && \ if (IsPairTerm(d0) && RepPair(d0) >= (CELL *)to_visit0 && \
RepPair(d0) <= (CELL *)to_visit) { \ RepPair(d0) <= (CELL *)to_visit) { \
/*fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \ /*fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \
*ptf++ = BREAK_LOOP(to_visit - to_visit0); \ *ptf++ = BREAK_LOOP(to_visit - to_visit0); \
continue; \ continue; \
} }
#define CYC_MARK_APPL \ #define CYC_MARK_APPL \
if (IsApplTerm(d0) && RepAppl(d0) >= (Term *)to_visit0 && \ if (IsApplTerm(d0) && RepAppl(d0) >= (Term *)to_visit0 && \
RepAppl(d0) <= (Term *)to_visit) { \ RepAppl(d0) <= (Term *)to_visit) { \
/*fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \ /*fprintf(stderr,"+%ld at %s\n", to_visit-to_visit0, __FUNCTION__);*/ \
*ptf++ = BREAK_LOOP(to_visit - to_visit0); \ *ptf++ = BREAK_LOOP(to_visit - to_visit0); \
continue; \ continue; \
} }
typedef struct { typedef struct {
Term old_var; Term old_var;
@ -123,24 +123,24 @@ typedef struct non_single_struct_t {
} non_singletons_t; } non_singletons_t;
#define WALK_COMPLEX_TERM__(LIST0, STRUCT0, PRIMI0) \ #define WALK_COMPLEX_TERM__(LIST0, STRUCT0, PRIMI0) \
\ \
struct non_single_struct_t *to_visit = Malloc( \ struct non_single_struct_t *to_visit = Malloc( \
1024 * sizeof(struct non_single_struct_t)), \ 1024 * sizeof(struct non_single_struct_t)), \
*to_visit0 = to_visit, \ *to_visit0 = to_visit, \
*to_visit_max = to_visit + 1024; \ *to_visit_max = to_visit + 1024; \
\ \
while (to_visit >= to_visit0) { \ while (to_visit >= to_visit0) { \
CELL d0; \ CELL d0; \
CELL *ptd0; \ CELL *ptd0; \
restart: \ restart: \
while (pt0 < pt0_end) { \ while (pt0 < pt0_end) { \
++pt0; \ ++pt0; \
ptd0 = pt0; \ ptd0 = pt0; \
d0 = *ptd0; \ d0 = *ptd0; \
list_loop: \ list_loop: \
/*fprintf(stderr, "%ld at %s\n", to_visit - to_visit0, __FUNCTION__);*/ \ /*fprintf(stderr, "%ld at %s\n", to_visit - to_visit0, __FUNCTION__);*/ \
deref_head(d0, var_in_term_unk); \ deref_head(d0, var_in_term_unk); \
var_in_term_nvar : { \ var_in_term_nvar : { \
if (IsPairTerm(d0)) { \ if (IsPairTerm(d0)) { \
if (to_visit + 32 >= to_visit_max) { \ if (to_visit + 32 >= to_visit_max) { \
goto aux_overflow; \ goto aux_overflow; \
@ -164,6 +164,9 @@ var_in_term_nvar : { \
/* store the terms to visit */ \ /* store the terms to visit */ \
ptd0 = RepAppl(d0); \ ptd0 = RepAppl(d0); \
f = (Functor)(d0 = *ptd0); \ f = (Functor)(d0 = *ptd0); \
if (IsExtensionFunctor(f)) {\
continue;\
}\
\ \
if (to_visit + 32 >= to_visit_max) { \ if (to_visit + 32 >= to_visit_max) { \
goto aux_overflow; \ goto aux_overflow; \
@ -202,11 +205,11 @@ var_in_term_nvar : { \
pt0_end = to_visit->pt0_end; \ pt0_end = to_visit->pt0_end; \
*to_visit->ptd0 = to_visit->d0; \ *to_visit->ptd0 = to_visit->d0; \
} \ } \
} \ } \
pop_text_stack(lvl); pop_text_stack(lvl);
#define def_aux_overflow() \ #define def_aux_overflow() \
aux_overflow : { \ aux_overflow : { \
size_t d1 = to_visit - to_visit0; \ size_t d1 = to_visit - to_visit0; \
size_t d2 = to_visit_max - to_visit0; \ size_t d2 = to_visit_max - to_visit0; \
to_visit0 = \ to_visit0 = \
@ -214,21 +217,21 @@ var_in_term_nvar : { \
to_visit = to_visit0 + d1; \ to_visit = to_visit0 + d1; \
to_visit_max = to_visit0 + (d2 + 128); \ to_visit_max = to_visit0 + (d2 + 128); \
pt0--; \ pt0--; \
} \ } \
goto restart; goto restart;
#define def_trail_overflow() \ #define def_trail_overflow() \
trail_overflow : { \ trail_overflow : { \
LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL; \ LOCAL_Error_TYPE = RESOURCE_ERROR_TRAIL; \
LOCAL_Error_Size = (TR - TR0) * sizeof(tr_fr_ptr *); \ LOCAL_Error_Size = (TR - TR0) * sizeof(tr_fr_ptr *); \
clean_tr(TR0 PASS_REGS); \ clean_tr(TR0 PASS_REGS); \
HR = InitialH; \ HR = InitialH; \
pop_text_stack(lvl); \ pop_text_stack(lvl); \
return 0L; \ return 0L; \
} }
#define def_global_overflow() \ #define def_global_overflow() \
global_overflow : { \ global_overflow : { \
while (to_visit > to_visit0) { \ while (to_visit > to_visit0) { \
to_visit--; \ to_visit--; \
CELL *ptd0 = to_visit->ptd0; \ CELL *ptd0 = to_visit->ptd0; \
@ -240,27 +243,27 @@ var_in_term_nvar : { \
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \ LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
LOCAL_Error_Size = (ASP - HR) * sizeof(CELL); \ LOCAL_Error_Size = (ASP - HR) * sizeof(CELL); \
return false; \ return false; \
} }
#define CYC_LIST \ #define CYC_LIST \
if (IS_VISIT_MARKER) { \ if (IS_VISIT_MARKER) { \
while (to_visit > to_visit0) { \ while (to_visit > to_visit0) { \
to_visit--; \ to_visit--; \
to_visit->ptd0[0] = to_visit->d0; \ to_visit->ptd0[0] = to_visit->d0; \
} \ } \
pop_text_stack(lvl); \ pop_text_stack(lvl); \
return true; \ return true; \
} }
#define CYC_APPL \ #define CYC_APPL \
if (IS_VISIT_MARKER) { \ if (IS_VISIT_MARKER) { \
while (to_visit > to_visit0) { \ while (to_visit > to_visit0) { \
to_visit--; \ to_visit--; \
to_visit->ptd0[0] = to_visit->d0; \ to_visit->ptd0[0] = to_visit->d0; \
} \ } \
pop_text_stack(lvl); \ pop_text_stack(lvl); \
return true; \ return true; \
} }
/** /**
@brief routine to locate all variables in a term, and its applications */ @brief routine to locate all variables in a term, and its applications */
@ -363,15 +366,15 @@ static int cycles_in_complex_term(register CELL *pt0,
/* store the terms to visit */ /* store the terms to visit */
ptd0 = RepAppl(d0); ptd0 = RepAppl(d0);
f = (Functor)(d0 = *ptd0); f = (Functor)(d0 = *ptd0);
if (IsExtensionFunctor(f)) {
*ptf++ = d0;
continue;
}
if (IS_VISIT_MARKER) { if (IS_VISIT_MARKER) {
rc++; rc++;
*ptf++ = BREAK_LOOP(to_visit - to_visit0); *ptf++ = BREAK_LOOP(to_visit - to_visit0);
continue; continue;
} }
if (IsExtensionFunctor(f)) {
*ptf++ = d0;
continue;
}
if (to_visit + 32 >= to_visit_max) { if (to_visit + 32 >= to_visit_max) {
goto aux_overflow; goto aux_overflow;
} }
@ -398,22 +401,22 @@ static int cycles_in_complex_term(register CELL *pt0,
derefa_body(d0, ptd0, var_in_term_unk, var_in_term_nvar); derefa_body(d0, ptd0, var_in_term_unk, var_in_term_nvar);
*ptf++ = d0; *ptf++ = d0;
} }
} }
/* Do we still have compound terms to visit */ /* Do we still have compound terms to visit */
to_visit--; to_visit--;
if (to_visit >= to_visit0) { if (to_visit >= to_visit0) {
pt0 = to_visit->pt0; pt0 = to_visit->pt0;
pt0_end = to_visit->pt0_end; pt0_end = to_visit->pt0_end;
ptf = to_visit->ptf; ptf = to_visit->ptf;
*to_visit->ptd0 = to_visit->d0; *to_visit->ptd0 = to_visit->d0;
} }
} }
pop_text_stack(lvl); pop_text_stack(lvl);
return rc; return rc;
def_aux_overflow(); def_aux_overflow();
return -1; return -1;
} }
Term Yap_CyclesInTerm(Term t USES_REGS) { Term Yap_CyclesInTerm(Term t USES_REGS) {
@ -515,22 +518,22 @@ static Int var_in_complex_term(register CELL * pt0, register CELL * pt0_end,
} }
pop_text_stack(lvl); pop_text_stack(lvl);
return true; return true;
} }
goto restart; goto restart;
END_WALK(); END_WALK();
if (to_visit > to_visit0) { if (to_visit > to_visit0) {
to_visit--; to_visit--;
CELL *ptd0 = to_visit->ptd0; CELL *ptd0 = to_visit->ptd0;
*ptd0 = to_visit->d0; *ptd0 = to_visit->d0;
pt0 = to_visit->pt0; pt0 = to_visit->pt0;
pt0_end = to_visit->pt0_end; pt0_end = to_visit->pt0_end;
} }
pop_text_stack(lvl); pop_text_stack(lvl);
return false; return false;
def_aux_overflow(); def_aux_overflow();
} }
static Int var_in_term( static Int var_in_term(
@ -655,9 +658,9 @@ static Int variables_in_term(
if (!expand_vts(3 PASS_REGS)) if (!expand_vts(3 PASS_REGS))
return false; return false;
} }
} while (out == 0L); } while (out == 0L);
clean_tr(TR - count PASS_REGS); clean_tr(TR - count PASS_REGS);
return Yap_unify(ARG3, out); return Yap_unify(ARG3, out);
} }
/** @pred term_variables(? _Term_, - _Variables_, +_ExternalVars_) is iso /** @pred term_variables(? _Term_, - _Variables_, +_ExternalVars_) is iso
@ -880,23 +883,23 @@ static Term new_vars_in_complex_term(
if (!Yap_growtrail((TR - TR0) * sizeof(tr_fr_ptr *), true)) { if (!Yap_growtrail((TR - TR0) * sizeof(tr_fr_ptr *), true)) {
goto trail_overflow; goto trail_overflow;
} }
} }
/* leave an empty slot to fill in later */ /* leave an empty slot to fill in later */
if (HR + 1024 > ASP) { if (HR + 1024 > ASP) {
goto global_overflow; goto global_overflow;
} }
END_WALK(); END_WALK();
clean_tr(TR0 PASS_REGS); clean_tr(TR0 PASS_REGS);
pop_text_stack(lvl); pop_text_stack(lvl);
HB = B->cp_h; HB = B->cp_h;
return output; return output;
def_aux_overflow(); def_aux_overflow();
def_trail_overflow(); def_trail_overflow();
def_global_overflow(); def_global_overflow();
} }
/** @pred new_variables_in_term(+_CurrentVariables_, ? _Term_, -_Variables_) /** @pred new_variables_in_term(+_CurrentVariables_, ? _Term_, -_Variables_)
@ -931,7 +934,7 @@ static Int p_new_variables_in_term(
} }
#define FOUND_VAR() \ #define FOUND_VAR() \
if (d0 == TermFoundVar) { \ if (d0 == TermFoundVar) { \
/* leave an empty slot to fill in later */ \ /* leave an empty slot to fill in later */ \
if (HR + 1024 > ASP) { \ if (HR + 1024 > ASP) { \
goto global_overflow; \ goto global_overflow; \
@ -940,7 +943,7 @@ static Int p_new_variables_in_term(
HR += 2; \ HR += 2; \
HR[-2] = (CELL)ptd0; \ HR[-2] = (CELL)ptd0; \
*ptd0 = TermNil; \ *ptd0 = TermNil; \
} }
static Term vars_within_complex_term( static Term vars_within_complex_term(
register CELL * pt0, register CELL * pt0_end, Term inp USES_REGS) { register CELL * pt0, register CELL * pt0_end, Term inp USES_REGS) {
@ -972,13 +975,13 @@ static Term vars_within_complex_term(
if (HR != InitialH) { if (HR != InitialH) {
HR[-1] = TermNil; HR[-1] = TermNil;
return output; return output;
} else { } else {
return TermNil; return TermNil;
} }
def_aux_overflow(); def_aux_overflow();
def_global_overflow(); def_global_overflow();
} }
/** @pred variables_within_term(+_CurrentVariables_, ? _Term_, -_Variables_) /** @pred variables_within_term(+_CurrentVariables_, ? _Term_, -_Variables_)
@ -1120,18 +1123,18 @@ static Int p_free_variables_in_term(
if (!expand_vts(3 PASS_REGS)) if (!expand_vts(3 PASS_REGS))
return false; return false;
} }
} while (out == 0L); } while (out == 0L);
if (found_module && t != t0) { if (found_module && t != t0) {
Term ts[2]; Term ts[2];
ts[0] = found_module; ts[0] = found_module;
ts[1] = t; ts[1] = t;
t = Yap_MkApplTerm(FunctorModule, 2, ts); t = Yap_MkApplTerm(FunctorModule, 2, ts);
} }
return Yap_unify(ARG2, t) && Yap_unify(ARG3, out); return Yap_unify(ARG2, t) && Yap_unify(ARG3, out);
} }
#define FOUND_VAR_AGAIN() \ #define FOUND_VAR_AGAIN() \
if (d0 == TermFoundVar) { \ if (d0 == TermFoundVar) { \
CELL *pt2 = pt0; \ CELL *pt2 = pt0; \
while (IsVarTerm(*pt2)) \ while (IsVarTerm(*pt2)) \
pt2 = (CELL *)(*pt2); \ pt2 = (CELL *)(*pt2); \
@ -1139,7 +1142,7 @@ static Int p_free_variables_in_term(
HR[0] = (CELL)pt2; \ HR[0] = (CELL)pt2; \
HR += 2; \ HR += 2; \
*pt2 = TermRefoundVar; \ *pt2 = TermRefoundVar; \
} }
static Term non_singletons_in_complex_term(CELL * pt0, static Term non_singletons_in_complex_term(CELL * pt0,
CELL * pt0_end USES_REGS) { CELL * pt0_end USES_REGS) {
@ -1208,10 +1211,10 @@ static void renumbervar(Term t, Int me USES_REGS) {
} }
#define RENUMBER_SINGLES \ #define RENUMBER_SINGLES \
if (singles) { \ if (singles) { \
renumbervar(d0, numbv++ PASS_REGS); \ renumbervar(d0, numbv++ PASS_REGS); \
goto restart; \ goto restart; \
} }
static Int numbervars_in_complex_term(CELL * pt0, CELL * pt0_end, Int numbv, static Int numbervars_in_complex_term(CELL * pt0, CELL * pt0_end, Int numbv,
int singles USES_REGS) { int singles USES_REGS) {
@ -1297,13 +1300,13 @@ static Int p_numbervars(USES_REGS1) {
} }
#define MAX_NUMBERED \ #define MAX_NUMBERED \
if (FunctorOfTerm(d0) == FunctorDollarVar) { \ if (FunctorOfTerm(d0) == FunctorDollarVar) { \
Term t1 = ArgOfTerm(1, d0); \ Term t1 = ArgOfTerm(1, d0); \
Int i; \ Int i; \
if (IsIntegerTerm(t1) && ((i = IntegerOfTerm(t1)) > *maxp)) \ if (IsIntegerTerm(t1) && ((i = IntegerOfTerm(t1)) > *maxp)) \
*maxp = i; \ *maxp = i; \
goto restart; \ goto restart; \
} }
static int max_numbered_var(CELL * pt0, CELL * pt0_end, static int max_numbered_var(CELL * pt0, CELL * pt0_end,
Int * maxp USES_REGS) { Int * maxp USES_REGS) {
@ -1382,30 +1385,31 @@ static Int create_entry(Term t, Int i, Int j, cl_connector * q, Int max) {
Term ref, h, *s, *ostart; Term ref, h, *s, *ostart;
ssize_t n; ssize_t n;
// first time, create a new term // first time, create a new term
if (i==0)
return 0;
if (IsVarTerm(t)) { if (IsVarTerm(t)) {
return -1; return -1;
} }
if (IsPairTerm(t)) { if (IsPairTerm(t)) {
Int me;
s = RepPair(t); s = RepPair(t);
h = s[0]; h = s[0];
if (IsAtomTerm(h)) { if (IsAtomTerm(h) &&
return t_ref((cl_connector*)AtomOfTerm(h),q,max); (me = t_ref((cl_connector*)AtomOfTerm(h),q,max)) >= 0 ) {
return me;
} }
n = 2; n = 2;
ostart = HR; ostart = HR;
ref = AbsPair(ostart); ref = AbsPair(ostart);
HR += 2; HR += 2;
q[max].header = Deref(s[0]);
} else if (IsApplTerm(t)) { } else if (IsApplTerm(t)) {
Int me;
h = (CELL)FunctorOfTerm(t); h = (CELL)FunctorOfTerm(t);
n = ArityOfFunctor((Functor)h); n = ArityOfFunctor((Functor)h);
if (IsExtensionFunctor((Functor)h)) { if (IsExtensionFunctor((Functor)h)) {
return -1; return -1;
} }
if (IsAtomTerm(h)) { if (IsAtomTerm(h) &&
return t_ref((cl_connector*)AtomOfTerm(h),q,max); (me = t_ref((cl_connector*)AtomOfTerm(h),q,max)) >= 0) {
return me;
} }
s = RepAppl(t); s = RepAppl(t);
q[max].header = s[0]; q[max].header = s[0];
@ -1413,13 +1417,16 @@ static Int create_entry(Term t, Int i, Int j, cl_connector * q, Int max) {
ref = AbsAppl(ostart); ref = AbsAppl(ostart);
*ostart++ = s[0]; *ostart++ = s[0];
HR=ostart+n; HR=ostart+n;
} else if (IsAtomTerm(t) && } else {
(max = t_ref((cl_connector*)AtomOfTerm(t),q,max)) >= 0) { Int me;
return max; if (IsAtomTerm(t) &&
(me = t_ref((cl_connector*)AtomOfTerm(t),q,max)) >= 0 ) {
return me;
} else { } else {
return -1; return -1;
} }
}
q[max].header = h;
q[max].parent = q[i].copy+j; q[max].parent = q[i].copy+j;
q[max].source = t; q[max].source = t;
q[max].copy = ostart; q[max].copy = ostart;
@ -1452,9 +1459,9 @@ Int cp_link(Term t, Int i, Int j, cl_connector * q, Int max, CELL * tailp) {
q[i].reference = v; q[i].reference = v;
} }
return max; return max;
} }
q[i].copy[j] = t; q[i].copy[j] = t;
return me; return me;
} }
Term Yap_BreakCycles(Term inp, UInt arity, Term * listp USES_REGS) { Term Yap_BreakCycles(Term inp, UInt arity, Term * listp USES_REGS) {
@ -1494,10 +1501,15 @@ Term Yap_BreakCycles(Term inp, UInt arity, Term * listp USES_REGS) {
} }
} }
pop_text_stack(lvl); for (i=0; i< qlen; i++) {
CELL *p = IsPairTerm(q[i].source) ? RepPair(q[i].source) : RepAppl(q[i].source);
p[0] = q[i].header;
}
HB = B->cp_h; pop_text_stack(lvl);
return q[0].reference;
HB = B->cp_h;
return q[0].reference;
} }
/** @pred rational_term_to_tree(? _TI_,- _TF_, ?SubTerms, ?MoreSubterms) /** @pred rational_term_to_tree(? _TI_,- _TF_, ?SubTerms, ?MoreSubterms)

View File

@ -85,7 +85,7 @@ native_sources = ["yap4py/yap_wrap.cxx","yap4py/yapi.cpp"]
extensions = [Extension('_yap', native_sources, extensions = [Extension('_yap', native_sources,
define_macros=[('MAJOR_VERSION', '6'), define_macros=[('MAJOR_VERSION', '6'),
('MINOR_VERSION', '4'), ('MINOR_VERSION', '5'),
('_YAP_NOT_INSTALLED_', '1'), ('_YAP_NOT_INSTALLED_', '1'),
('YAP_PYTHON', '1'), ('YAP_PYTHON', '1'),
('PYTHONSWIG', '1'), ('PYTHONSWIG', '1'),
@ -115,7 +115,7 @@ package_data = {
data_files=[] data_files=[]
version_ns = {'__version__': '6.4.1', 'major-version': '6', 'minor-version': '4', 'patch': '1'} version_ns = {'__version__': '6.5.0', 'major-version': '6', 'minor-version': '5', 'patch': '0'}
setup_args = dict( setup_args = dict(
name=name, name=name,

View File

@ -94,7 +94,7 @@ undefined_query(G0, M0, Cut) :-
'$get_undefined_predicates'(M0:G0, MG), !. '$get_undefined_predicates'(M0:G0, MG), !.
% undef handler % undef handler
'$undefp_'([M0|G0],MG) :- '$undefp'([M0|G0],MG) :-
% make sure we do not loop on undefined predicates % make sure we do not loop on undefined predicates
'$undef_setup'(M0:G0, Action,Debug,Current, MGI), '$undef_setup'(M0:G0, Action,Debug,Current, MGI),
('$get_undefined_predicates'( MGI, MG ) ('$get_undefined_predicates'( MGI, MG )

View File

@ -72,6 +72,7 @@ d(X, ( X= f(A,A,X)) ).
d(X, ( X= f(A,A,g(A))) ). d(X, ( X= f(A,A,g(A))) ).
d(X, ( X= f(A,g(X,[A|A]),X)) ). d(X, ( X= f(A,g(X,[A|A]),X)) ).
d(X, ( X= f(X,[X,X])) ). d(X, ( X= f(X,[X,X])) ).
d(X, ( X= f(3.14,[22.3,X])) ).
d(X, ( X= f(X,[X,g(X)])) ). d(X, ( X= f(X,[X,g(X)])) ).
d(X, ( X= f(_,X/[X])) ). d(X, ( X= f(_,X/[X])) ).
d(X, ( X= f(_,A/[A]), A= f(X,[X,g(X)])) ). d(X, ( X= f(_,A/[A]), A= f(X,[X,g(X)])) ).