2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: utilpreds.c *
|
|
|
|
* Last rev: 4/03/88 *
|
|
|
|
* mods: *
|
|
|
|
* comments: new utility predicates for YAP *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "@(#)utilpreds.c 1.3";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2008-05-11 00:24:13 +01:00
|
|
|
#include "clause.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "Heap.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#include "eval.h"
|
2005-02-18 21:34:02 +00:00
|
|
|
#include "attvar.h"
|
2008-08-06 18:32:22 +01:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include "string.h"
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Term old_var;
|
|
|
|
Term new_var;
|
|
|
|
} *vcell;
|
|
|
|
|
|
|
|
|
2007-09-21 14:52:52 +01:00
|
|
|
STATIC_PROTO(int copy_complex_term, (CELL *, CELL *, int, int, CELL *, CELL *));
|
2004-02-09 14:19:05 +00:00
|
|
|
STATIC_PROTO(CELL vars_in_complex_term, (CELL *, CELL *, Term));
|
2001-04-09 20:54:03 +01:00
|
|
|
STATIC_PROTO(Int p_non_singletons_in_term, (void));
|
|
|
|
STATIC_PROTO(CELL non_singletons_in_complex_term, (CELL *, CELL *));
|
|
|
|
STATIC_PROTO(Int p_variables_in_term, (void));
|
|
|
|
STATIC_PROTO(Int ground_complex_term, (CELL *, CELL *));
|
|
|
|
STATIC_PROTO(Int p_ground, (void));
|
|
|
|
STATIC_PROTO(Int p_copy_term, (void));
|
|
|
|
STATIC_PROTO(Int var_in_complex_term, (CELL *, CELL *, Term));
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
STATIC_PROTO(Int p_force_trail_expansion, (void));
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
clean_tr(tr_fr_ptr TR0) {
|
|
|
|
if (TR != TR0) {
|
|
|
|
do {
|
|
|
|
Term p = TrailTerm(--TR);
|
2004-04-20 23:08:57 +01:00
|
|
|
RESET_VARIABLE(p);
|
2001-04-09 20:54:03 +01:00
|
|
|
} while (TR != TR0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-16 15:10:20 +01:00
|
|
|
static inline void
|
|
|
|
clean_dirty_tr(tr_fr_ptr TR0) {
|
|
|
|
if (TR != TR0) {
|
|
|
|
tr_fr_ptr pt = TR0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
Term p = TrailTerm(pt++);
|
|
|
|
if (IsVarTerm(p)) {
|
|
|
|
RESET_VARIABLE(p);
|
|
|
|
} else {
|
2004-09-09 21:00:59 +01:00
|
|
|
/* copy downwards */
|
2004-06-16 15:10:20 +01:00
|
|
|
TrailTerm(TR0+1) = TrailTerm(pt);
|
|
|
|
TrailTerm(TR0) = TrailTerm(TR0+2) = p;
|
|
|
|
pt+=2;
|
|
|
|
TR0 += 3;
|
|
|
|
}
|
|
|
|
} while (pt != TR);
|
|
|
|
TR = TR0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-21 14:52:52 +01:00
|
|
|
|
2002-04-08 23:10:14 +01:00
|
|
|
static int
|
2007-09-21 14:52:52 +01:00
|
|
|
copy_complex_term(CELL *pt0, CELL *pt0_end, int share, int newattvs, CELL *ptf, CELL *HLow)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
|
2007-09-21 14:52:52 +01:00
|
|
|
struct cp_frame *to_visit0, *to_visit = (struct cp_frame *)Yap_PreAllocCodeSpace();
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL *HB0 = HB;
|
2004-04-19 18:06:51 +01:00
|
|
|
tr_fr_ptr TR0 = TR;
|
2007-09-21 14:52:52 +01:00
|
|
|
int ground = TRUE;
|
2002-04-24 03:52:34 +01:00
|
|
|
#ifdef COROUTINING
|
|
|
|
CELL *dvars = NULL;
|
|
|
|
#endif
|
2007-09-21 14:52:52 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
HB = HLow;
|
2004-01-23 02:23:51 +00:00
|
|
|
to_visit0 = to_visit;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
++ pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
|
|
|
deref_head(d0, copy_term_unk);
|
|
|
|
copy_term_nvar:
|
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
|
|
|
CELL *ap2 = RepPair(d0);
|
|
|
|
if (ap2 >= HB && ap2 < H) {
|
|
|
|
/* If this is newer than the current term, just reuse */
|
|
|
|
*ptf++ = d0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*ptf = AbsPair(H);
|
|
|
|
ptf++;
|
|
|
|
#ifdef RATIONAL_TREES
|
2007-09-21 14:52:52 +01:00
|
|
|
if (to_visit+1 >= (struct cp_frame *)AuxSp) {
|
2002-04-08 23:10:14 +01:00
|
|
|
goto heap_overflow;
|
|
|
|
}
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit->start_cp = pt0;
|
|
|
|
to_visit->end_cp = pt0_end;
|
|
|
|
to_visit->to = ptf;
|
|
|
|
to_visit->oldv = *pt0;
|
|
|
|
to_visit->ground = ground;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* fool the system into thinking we had a variable there */
|
|
|
|
*pt0 = AbsPair(H);
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit ++;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
2007-09-21 14:52:52 +01:00
|
|
|
if (to_visit+1 >= (struct cp_frame *)AuxSp) {
|
2002-04-08 23:10:14 +01:00
|
|
|
goto heap_overflow;
|
|
|
|
}
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit->start_cp = pt0;
|
|
|
|
to_visit->end_cp = pt0_end;
|
|
|
|
to_visit->to = ptf;
|
|
|
|
to_visit->ground = ground;
|
|
|
|
to_visit ++;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2007-09-21 14:52:52 +01:00
|
|
|
ground = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
pt0 = ap2 - 1;
|
|
|
|
pt0_end = ap2 + 1;
|
|
|
|
ptf = H;
|
|
|
|
H += 2;
|
2002-04-23 23:43:10 +01:00
|
|
|
if (H > ASP - 2048) {
|
2001-04-09 20:54:03 +01:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
2004-04-20 23:08:57 +01:00
|
|
|
if (ap2 >= HB && ap2 <= H) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* If this is newer than the current term, just reuse */
|
|
|
|
*ptf++ = d0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
2008-05-11 00:24:13 +01:00
|
|
|
#if defined(YAPOR) || defined(THREADS)
|
|
|
|
if (f == FunctorDBRef) {
|
|
|
|
DBRef entryref = DBRefOfTerm(d0);
|
|
|
|
if (entryref->Flags & LogUpdMask) {
|
|
|
|
LogUpdClause *luclause = (LogUpdClause *)entryref;
|
|
|
|
LOCK(luclause->ClPred->PELock);
|
|
|
|
UNLOCK(luclause->ClPred->PELock);
|
|
|
|
} else {
|
|
|
|
LOCK(entryref->lock);
|
|
|
|
TRAIL_REF(entryref); /* So that fail will erase it */
|
|
|
|
INC_DBREF_COUNT(entryref);
|
|
|
|
UNLOCK(entryref->lock);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
*ptf++ = d0; /* you can just copy other extensions. */
|
2008-08-12 02:27:23 +01:00
|
|
|
} else
|
2008-05-11 00:24:13 +01:00
|
|
|
#endif
|
2008-08-12 02:27:23 +01:00
|
|
|
if (!share) {
|
2008-08-06 18:32:22 +01:00
|
|
|
UInt sz;
|
|
|
|
|
|
|
|
*ptf++ = AbsAppl(H); /* you can just copy other extensions. */
|
|
|
|
/* make sure to copy floats */
|
|
|
|
if (f== FunctorDouble) {
|
|
|
|
sz = sizeof(Float)/sizeof(CELL)+2;
|
|
|
|
} else if (f== FunctorLongInt) {
|
|
|
|
sz = 3;
|
|
|
|
} else {
|
|
|
|
CELL *pt = ap2+1;
|
|
|
|
sz = 2+sizeof(MP_INT)+(((MP_INT *)(pt+1))->_mp_alloc*sizeof(mp_limb_t));
|
|
|
|
}
|
|
|
|
if (H+sz > ASP - 2048) {
|
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
memcpy((void *)H, (void *)ap2, sz*sizeof(CELL));
|
|
|
|
H += sz;
|
|
|
|
} else {
|
|
|
|
*ptf++ = d0; /* you can just copy other extensions. */
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*ptf = AbsAppl(H);
|
|
|
|
ptf++;
|
|
|
|
/* store the terms to visit */
|
|
|
|
#ifdef RATIONAL_TREES
|
2007-09-21 14:52:52 +01:00
|
|
|
if (to_visit+1 >= (struct cp_frame *)AuxSp) {
|
2002-04-08 23:10:14 +01:00
|
|
|
goto heap_overflow;
|
|
|
|
}
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit->start_cp = pt0;
|
|
|
|
to_visit->end_cp = pt0_end;
|
|
|
|
to_visit->to = ptf;
|
|
|
|
to_visit->oldv = *pt0;
|
|
|
|
to_visit->ground = ground;
|
2001-04-09 20:54:03 +01:00
|
|
|
/* fool the system into thinking we had a variable there */
|
|
|
|
*pt0 = AbsAppl(H);
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit ++;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
2007-09-21 14:52:52 +01:00
|
|
|
if (to_visit+1 >= (struct cp_frame *)AuxSp) {
|
2002-04-08 23:10:14 +01:00
|
|
|
goto heap_overflow;
|
|
|
|
}
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit->start_cp = pt0;
|
|
|
|
to_visit->end_cp = pt0_end;
|
|
|
|
to_visit->to = ptf;
|
|
|
|
to_visit->ground = ground;
|
|
|
|
to_visit ++;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2007-09-21 14:52:52 +01:00
|
|
|
ground = (f != FunctorMutable);
|
2001-04-09 20:54:03 +01:00
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
/* store the functor for the new term */
|
|
|
|
H[0] = (CELL)f;
|
|
|
|
ptf = H+1;
|
|
|
|
H += 1+d0;
|
2002-04-23 23:43:10 +01:00
|
|
|
if (H > ASP - 2048) {
|
2001-04-09 20:54:03 +01:00
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* just copy atoms or integers */
|
|
|
|
*ptf++ = d0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
derefa_body(d0, ptd0, copy_term_unk, copy_term_nvar);
|
2007-09-21 14:52:52 +01:00
|
|
|
ground = FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (ptd0 >= HLow && ptd0 < H) {
|
|
|
|
/* we have already found this cell */
|
|
|
|
*ptf++ = (CELL) ptd0;
|
|
|
|
} else {
|
|
|
|
#if COROUTINING
|
2007-10-11 15:25:09 +01:00
|
|
|
if (newattvs && IsAttachedTerm((CELL)ptd0)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
/* if unbound, call the standard copy term routine */
|
2007-09-21 14:52:52 +01:00
|
|
|
struct cp_frame *bp[1];
|
2004-04-20 23:08:57 +01:00
|
|
|
|
2004-04-19 18:06:51 +01:00
|
|
|
if (dvars == NULL) {
|
2005-02-18 21:34:02 +00:00
|
|
|
dvars = (CELL *)DelayTop();
|
2004-04-19 18:06:51 +01:00
|
|
|
}
|
2006-08-22 17:12:46 +01:00
|
|
|
if (ptd0 < dvars) {
|
2002-04-24 03:52:34 +01:00
|
|
|
*ptf++ = (CELL) ptd0;
|
|
|
|
} else {
|
2004-04-20 23:08:57 +01:00
|
|
|
tr_fr_ptr CurTR;
|
|
|
|
|
|
|
|
CurTR = TR;
|
2002-04-24 03:52:34 +01:00
|
|
|
bp[0] = to_visit;
|
|
|
|
HB = HB0;
|
|
|
|
if (!attas[ExtFromCell(ptd0)].copy_term_op(ptd0, bp, ptf)) {
|
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
to_visit = bp[0];
|
|
|
|
HB = HLow;
|
|
|
|
ptf++;
|
2007-03-27 14:48:51 +01:00
|
|
|
if (TR > (tr_fr_ptr)Yap_TrailTop - 256) {
|
|
|
|
/* Trail overflow */
|
|
|
|
if (!Yap_growtrail((TR-TR0)*sizeof(tr_fr_ptr *), TRUE)) {
|
|
|
|
goto trail_overflow;
|
|
|
|
}
|
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
Bind(ptd0, ptf[-1]);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
/* first time we met this term */
|
|
|
|
RESET_VARIABLE(ptf);
|
2007-11-12 10:39:49 +00:00
|
|
|
if (TR > (tr_fr_ptr)Yap_TrailTop - 256) {
|
|
|
|
/* Trail overflow */
|
|
|
|
if (!Yap_growtrail((TR-TR0)*sizeof(tr_fr_ptr *), TRUE)) {
|
|
|
|
goto trail_overflow;
|
|
|
|
}
|
|
|
|
}
|
2008-08-06 18:32:22 +01:00
|
|
|
Bind(ptd0, (CELL)ptf);
|
2001-04-09 20:54:03 +01:00
|
|
|
ptf++;
|
|
|
|
#ifdef COROUTINING
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
2004-01-23 02:23:51 +00:00
|
|
|
if (to_visit > to_visit0) {
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit --;
|
|
|
|
if (ground && share) {
|
|
|
|
CELL old = to_visit->oldv;
|
|
|
|
CELL *newp = to_visit->to-1;
|
|
|
|
CELL new = *newp;
|
|
|
|
|
|
|
|
*newp = old;
|
|
|
|
if (IsApplTerm(new))
|
|
|
|
H = RepAppl(new);
|
|
|
|
else
|
|
|
|
H = RepPair(new);
|
|
|
|
}
|
|
|
|
pt0 = to_visit->start_cp;
|
|
|
|
pt0_end = to_visit->end_cp;
|
|
|
|
ptf = to_visit->to;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
2007-09-21 14:52:52 +01:00
|
|
|
*pt0 = to_visit->oldv;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
2007-09-21 14:52:52 +01:00
|
|
|
ground = (ground && to_visit->ground);
|
2001-04-09 20:54:03 +01:00
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* restore our nice, friendly, term to its original state */
|
|
|
|
HB = HB0;
|
2004-09-09 21:00:59 +01:00
|
|
|
clean_dirty_tr(TR0);
|
2007-09-21 14:52:52 +01:00
|
|
|
return ground;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
overflow:
|
|
|
|
/* oops, we're in trouble */
|
|
|
|
H = HLow;
|
|
|
|
/* we've done it */
|
|
|
|
/* restore our nice, friendly, term to its original state */
|
|
|
|
HB = HB0;
|
|
|
|
#ifdef RATIONAL_TREES
|
2004-01-23 02:23:51 +00:00
|
|
|
while (to_visit > to_visit0) {
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit --;
|
|
|
|
pt0 = to_visit->start_cp;
|
|
|
|
pt0_end = to_visit->end_cp;
|
|
|
|
ptf = to_visit->to;
|
|
|
|
*pt0 = to_visit->oldv;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-07 21:20:12 +01:00
|
|
|
reset_trail(TR0);
|
|
|
|
return -1;
|
2002-04-08 23:10:14 +01:00
|
|
|
|
2007-03-27 14:48:51 +01:00
|
|
|
trail_overflow:
|
|
|
|
/* oops, we're in trouble */
|
|
|
|
H = HLow;
|
|
|
|
/* we've done it */
|
|
|
|
/* restore our nice, friendly, term to its original state */
|
|
|
|
HB = HB0;
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit --;
|
|
|
|
pt0 = to_visit->start_cp;
|
|
|
|
pt0_end = to_visit->end_cp;
|
|
|
|
ptf = to_visit->to;
|
|
|
|
*pt0 = to_visit->oldv;
|
2007-03-27 14:48:51 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
tr_fr_ptr oTR = TR;
|
|
|
|
reset_trail(TR0);
|
|
|
|
if (!Yap_growtrail((oTR-TR0)*sizeof(tr_fr_ptr *), FALSE)) {
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2002-04-08 23:10:14 +01:00
|
|
|
heap_overflow:
|
|
|
|
/* oops, we're in trouble */
|
|
|
|
H = HLow;
|
|
|
|
/* we've done it */
|
|
|
|
/* restore our nice, friendly, term to its original state */
|
|
|
|
HB = HB0;
|
|
|
|
#ifdef RATIONAL_TREES
|
2004-01-23 02:23:51 +00:00
|
|
|
while (to_visit > to_visit0) {
|
2007-09-21 14:52:52 +01:00
|
|
|
to_visit --;
|
|
|
|
pt0 = to_visit->start_cp;
|
|
|
|
pt0_end = to_visit->end_cp;
|
|
|
|
ptf = to_visit->to;
|
|
|
|
*pt0 = to_visit->oldv;
|
2002-04-08 23:10:14 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-09-09 21:00:59 +01:00
|
|
|
reset_trail(TR0);
|
2007-11-12 10:39:49 +00:00
|
|
|
Yap_Error_Size = (ADDR)AuxSp-(ADDR)to_visit0;
|
2007-03-27 14:48:51 +01:00
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Term
|
|
|
|
handle_cp_overflow(int res, UInt arity, Term t)
|
|
|
|
{
|
|
|
|
XREGS[arity+1] = t;
|
|
|
|
switch(res) {
|
|
|
|
case -1:
|
2008-08-28 04:43:00 +01:00
|
|
|
if (!Yap_gcl((ASP-H)*sizeof(CELL), arity+1, ENV, gc_P(P,CP))) {
|
2007-03-27 14:48:51 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
return Deref(XREGS[arity+1]);
|
|
|
|
case -2:
|
|
|
|
return Deref(XREGS[arity+1]);
|
|
|
|
case -3:
|
2007-11-12 10:39:49 +00:00
|
|
|
{
|
|
|
|
UInt size = Yap_Error_Size;
|
|
|
|
Yap_Error_Size = 0L;
|
|
|
|
if (size > 4*1024*1024)
|
|
|
|
size = 4*1024*1024;
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(size,NULL)) {
|
|
|
|
Yap_Error(OUT_OF_AUXSPACE_ERROR, TermNil, Yap_ErrorMessage);
|
|
|
|
return 0L;
|
|
|
|
}
|
2007-03-27 14:48:51 +01:00
|
|
|
}
|
|
|
|
return Deref(XREGS[arity+1]);
|
|
|
|
default:
|
|
|
|
return 0L;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
2007-09-21 14:52:52 +01:00
|
|
|
CopyTerm(Term inp, UInt arity, int share, int newattvs) {
|
2001-04-09 20:54:03 +01:00
|
|
|
Term t = Deref(inp);
|
|
|
|
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
#if COROUTINING
|
2007-09-21 14:52:52 +01:00
|
|
|
if (newattvs && IsAttachedTerm(t)) {
|
2001-12-11 03:34:03 +00:00
|
|
|
CELL *Hi;
|
2002-04-08 23:10:14 +01:00
|
|
|
int res;
|
2001-12-11 03:34:03 +00:00
|
|
|
restart_attached:
|
|
|
|
|
|
|
|
*H = t;
|
|
|
|
Hi = H+1;
|
|
|
|
H += 2;
|
2007-09-21 14:52:52 +01:00
|
|
|
if ((res = copy_complex_term(Hi-2, Hi-1, share, newattvs, Hi, Hi)) < 0) {
|
2004-12-16 05:57:32 +00:00
|
|
|
H = Hi-1;
|
2007-03-27 14:48:51 +01:00
|
|
|
if ((t = handle_cp_overflow(res,arity,t))== 0L)
|
|
|
|
return FALSE;
|
|
|
|
goto restart_attached;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-10-30 01:32:55 +01:00
|
|
|
return Hi[0];
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2005-10-30 01:32:55 +01:00
|
|
|
return MkVarTerm();
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
2005-10-30 01:32:55 +01:00
|
|
|
return t;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
Term tf;
|
|
|
|
CELL *ap;
|
|
|
|
CELL *Hi;
|
|
|
|
|
|
|
|
restart_list:
|
|
|
|
ap = RepPair(t);
|
|
|
|
Hi = H;
|
|
|
|
tf = AbsPair(H);
|
|
|
|
H += 2;
|
2002-04-08 23:10:14 +01:00
|
|
|
{
|
|
|
|
int res;
|
2007-09-21 14:52:52 +01:00
|
|
|
if ((res = copy_complex_term(ap-1, ap+1, share, newattvs, Hi, Hi)) < 0) {
|
2004-12-16 05:57:32 +00:00
|
|
|
H = Hi;
|
2007-03-27 14:48:51 +01:00
|
|
|
if ((t = handle_cp_overflow(res,arity,t))== 0L)
|
|
|
|
return FALSE;
|
|
|
|
goto restart_list;
|
2007-09-21 14:52:52 +01:00
|
|
|
} else if (res && share && FunctorOfTerm(t) != FunctorMutable) {
|
|
|
|
H = Hi;
|
|
|
|
return t;
|
2002-04-08 23:10:14 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-10-30 01:32:55 +01:00
|
|
|
return tf;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
Term tf;
|
|
|
|
CELL *HB0;
|
|
|
|
CELL *ap;
|
|
|
|
|
|
|
|
restart_appl:
|
|
|
|
f = FunctorOfTerm(t);
|
|
|
|
HB0 = H;
|
|
|
|
ap = RepAppl(t);
|
|
|
|
tf = AbsAppl(H);
|
|
|
|
H[0] = (CELL)f;
|
|
|
|
H += 1+ArityOfFunctor(f);
|
2006-12-27 01:32:38 +00:00
|
|
|
if (H > ASP-128) {
|
2007-03-27 14:48:51 +01:00
|
|
|
H = HB0;
|
|
|
|
if ((t = handle_cp_overflow(-1,arity,t))== 0L)
|
2006-12-27 01:32:38 +00:00
|
|
|
return FALSE;
|
|
|
|
goto restart_appl;
|
|
|
|
} else {
|
2002-04-08 23:10:14 +01:00
|
|
|
int res;
|
2004-12-16 05:57:32 +00:00
|
|
|
|
2007-09-21 14:52:52 +01:00
|
|
|
if ((res = copy_complex_term(ap, ap+ArityOfFunctor(f), share, newattvs, HB0+1, HB0)) < 0) {
|
2004-12-16 05:57:32 +00:00
|
|
|
H = HB0;
|
2007-03-27 14:48:51 +01:00
|
|
|
if ((t = handle_cp_overflow(res,arity,t))== 0L)
|
|
|
|
return FALSE;
|
|
|
|
goto restart_appl;
|
2007-09-21 14:52:52 +01:00
|
|
|
} else if (res && share) {
|
|
|
|
H = HB0;
|
|
|
|
return t;
|
2002-04-08 23:10:14 +01:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-10-30 01:32:55 +01:00
|
|
|
return tf;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_CopyTerm(Term inp) {
|
2007-09-21 14:52:52 +01:00
|
|
|
return CopyTerm(inp, 0, TRUE, TRUE);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2008-08-06 18:32:22 +01:00
|
|
|
Term
|
|
|
|
Yap_CopyTermNoShare(Term inp) {
|
|
|
|
return CopyTerm(inp, 0, FALSE, FALSE);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
|
|
|
p_copy_term(void) /* copy term t to a new instance */
|
|
|
|
{
|
2007-09-21 14:52:52 +01:00
|
|
|
Term t = CopyTerm(ARG1, 2, TRUE, TRUE);
|
2004-12-16 05:57:32 +00:00
|
|
|
if (t == 0L)
|
|
|
|
return FALSE;
|
|
|
|
/* be careful, there may be a stack shift here */
|
2004-09-18 15:03:42 +01:00
|
|
|
return Yap_unify(ARG2,t);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2007-09-21 14:52:52 +01:00
|
|
|
static Int
|
|
|
|
p_duplicate_term(void) /* copy term t to a new instance */
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2007-09-21 14:52:52 +01:00
|
|
|
Term t = CopyTerm(ARG1, 2, FALSE, TRUE);
|
|
|
|
if (t == 0L)
|
|
|
|
return FALSE;
|
|
|
|
/* be careful, there may be a stack shift here */
|
|
|
|
return Yap_unify(ARG2,t);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_copy_term_no_delays(void) /* copy term t to a new instance */
|
|
|
|
{
|
2007-09-21 14:52:52 +01:00
|
|
|
Term t = CopyTerm(ARG1, 2, TRUE, FALSE);
|
2005-02-18 21:34:02 +00:00
|
|
|
if (t == 0L) {
|
2004-12-16 05:57:32 +00:00
|
|
|
return FALSE;
|
2005-02-18 21:34:02 +00:00
|
|
|
}
|
2004-12-16 05:57:32 +00:00
|
|
|
/* be careful, there may be a stack shift here */
|
|
|
|
return(Yap_unify(ARG2,t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-09 14:19:05 +00:00
|
|
|
static Term vars_in_complex_term(register CELL *pt0, register CELL *pt0_end, Term inp)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
register CELL **to_visit0, **to_visit = (CELL **)Yap_PreAllocCodeSpace();
|
2001-04-09 20:54:03 +01:00
|
|
|
register tr_fr_ptr TR0 = TR;
|
|
|
|
CELL *InitialH = H;
|
|
|
|
CELL output = AbsPair(H);
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
to_visit0 = to_visit;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
++ pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
|
|
|
deref_head(d0, vars_in_term_unk);
|
|
|
|
vars_in_term_nvar:
|
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
2007-11-14 09:23:42 +00:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* store the terms to visit */
|
2007-11-14 09:23:42 +00:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar);
|
|
|
|
/* do or pt2 are unbound */
|
|
|
|
*ptd0 = TermNil;
|
|
|
|
/* leave an empty slot to fill in later */
|
2006-03-30 02:11:10 +01:00
|
|
|
if (H+1024 > ASP) {
|
|
|
|
goto global_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
H[1] = AbsPair(H+2);
|
|
|
|
H += 2;
|
|
|
|
H[-2] = (CELL)ptd0;
|
|
|
|
/* next make sure noone will see this as a variable again */
|
2007-03-27 14:48:51 +01:00
|
|
|
if (TR > (tr_fr_ptr)Yap_TrailTop - 256) {
|
|
|
|
/* Trail overflow */
|
|
|
|
if (!Yap_growtrail((TR-TR0)*sizeof(tr_fr_ptr *), TRUE)) {
|
|
|
|
goto trail_overflow;
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
TrailTerm(TR++) = (CELL)ptd0;
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
2004-01-23 02:23:51 +00:00
|
|
|
if (to_visit > to_visit0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
#else
|
|
|
|
to_visit -= 2;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
clean_tr(TR0);
|
2004-01-23 02:23:51 +00:00
|
|
|
Yap_ReleasePreAllocCodeSpace((ADDR)to_visit0);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (H != InitialH) {
|
|
|
|
/* close the list */
|
2004-02-09 14:19:05 +00:00
|
|
|
Term t2 = Deref(inp);
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(t2)) {
|
|
|
|
RESET_VARIABLE(H-1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_unify((CELL)(H-1),ARG2);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
H[-1] = t2; /* don't need to trail */
|
|
|
|
}
|
|
|
|
return(output);
|
|
|
|
} else {
|
2004-02-09 14:19:05 +00:00
|
|
|
return(inp);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2007-03-27 14:48:51 +01:00
|
|
|
|
|
|
|
trail_overflow:
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Yap_Error_TYPE = OUT_OF_TRAIL_ERROR;
|
|
|
|
Yap_Error_Size = (TR-TR0)*sizeof(tr_fr_ptr *);
|
|
|
|
clean_tr(TR0);
|
|
|
|
Yap_ReleasePreAllocCodeSpace((ADDR)to_visit0);
|
|
|
|
H = InitialH;
|
|
|
|
return 0L;
|
|
|
|
|
2007-11-14 09:23:42 +00:00
|
|
|
aux_overflow:
|
|
|
|
Yap_Error_Size = (to_visit-to_visit0)*sizeof(CELL **);
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Yap_Error_TYPE = OUT_OF_AUXSPACE_ERROR;
|
|
|
|
clean_tr(TR0);
|
|
|
|
Yap_ReleasePreAllocCodeSpace((ADDR)to_visit0);
|
|
|
|
H = InitialH;
|
|
|
|
return 0L;
|
|
|
|
|
2006-03-30 02:11:10 +01:00
|
|
|
global_overflow:
|
2007-03-27 14:48:51 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
2006-03-30 02:11:10 +01:00
|
|
|
clean_tr(TR0);
|
|
|
|
Yap_ReleasePreAllocCodeSpace((ADDR)to_visit0);
|
|
|
|
H = InitialH;
|
2007-03-27 14:48:51 +01:00
|
|
|
Yap_Error_TYPE = OUT_OF_STACK_ERROR;
|
2006-08-07 19:51:44 +01:00
|
|
|
Yap_Error_Size = (ASP-H)*sizeof(CELL);
|
2006-03-30 02:11:10 +01:00
|
|
|
return 0L;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2007-03-27 14:48:51 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
expand_vts(void)
|
|
|
|
{
|
|
|
|
UInt expand = Yap_Error_Size;
|
|
|
|
yap_error_number yap_errno = Yap_Error_TYPE;
|
|
|
|
|
|
|
|
Yap_Error_Size = 0;
|
|
|
|
Yap_Error_TYPE = YAP_NO_ERROR;
|
|
|
|
if (yap_errno == OUT_OF_TRAIL_ERROR) {
|
|
|
|
/* Trail overflow */
|
|
|
|
if (!Yap_growtrail(expand, FALSE)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-11-14 09:23:42 +00:00
|
|
|
} else if (yap_errno == OUT_OF_AUXSPACE_ERROR) {
|
|
|
|
/* Aux space overflow */
|
|
|
|
if (expand > 4*1024*1024)
|
|
|
|
expand = 4*1024*1024;
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(expand, NULL)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-03-27 14:48:51 +01:00
|
|
|
} else {
|
2008-08-28 04:43:00 +01:00
|
|
|
if (!Yap_gcl(expand, 3, ENV, gc_P(P,CP))) {
|
2007-03-27 14:48:51 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, "in term_variables");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static Int
|
|
|
|
p_variables_in_term(void) /* variables in term t */
|
|
|
|
{
|
|
|
|
Term out;
|
|
|
|
|
2006-03-30 02:11:10 +01:00
|
|
|
do {
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
out = AbsPair(H);
|
|
|
|
H += 2;
|
|
|
|
RESET_VARIABLE(H-2);
|
|
|
|
RESET_VARIABLE(H-1);
|
|
|
|
Yap_unify((CELL)(H-2),ARG1);
|
|
|
|
Yap_unify((CELL)(H-1),ARG2);
|
|
|
|
} else if (IsPrimitiveTerm(t))
|
|
|
|
out = ARG2;
|
|
|
|
else if (IsPairTerm(t)) {
|
|
|
|
out = vars_in_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1, ARG2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
out = vars_in_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(f), ARG2);
|
|
|
|
}
|
|
|
|
if (out == 0L) {
|
2007-03-27 14:48:51 +01:00
|
|
|
if (!expand_vts())
|
2006-03-30 02:11:10 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} while (out == 0L);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG3,out));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2004-02-09 14:19:05 +00:00
|
|
|
static Int
|
|
|
|
p_term_variables(void) /* variables in term t */
|
|
|
|
{
|
|
|
|
Term out;
|
|
|
|
|
2006-03-30 02:11:10 +01:00
|
|
|
do {
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
2008-04-03 11:50:28 +01:00
|
|
|
Term out = Yap_MkNewPairTerm();
|
|
|
|
return
|
|
|
|
Yap_unify(t,HeadOfTerm(out)) &&
|
|
|
|
Yap_unify(TermNil, TailOfTerm(out)) &&
|
|
|
|
Yap_unify(out, ARG2);
|
2006-03-30 02:11:10 +01:00
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
|
|
|
return Yap_unify(TermNil, ARG2);
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
out = vars_in_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1, TermNil);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
out = vars_in_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(f), TermNil);
|
|
|
|
}
|
|
|
|
if (out == 0L) {
|
2007-03-27 14:48:51 +01:00
|
|
|
if (!expand_vts())
|
2006-03-30 02:11:10 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} while (out == 0L);
|
2004-02-09 14:19:05 +00:00
|
|
|
return Yap_unify(ARG2,out);
|
|
|
|
}
|
|
|
|
|
2005-09-22 04:26:13 +01:00
|
|
|
static Int
|
|
|
|
p_term_variables3(void) /* variables in term t */
|
|
|
|
{
|
|
|
|
Term out;
|
|
|
|
|
2006-03-30 02:11:10 +01:00
|
|
|
do {
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
2008-04-03 11:50:28 +01:00
|
|
|
Term out = Yap_MkNewPairTerm();
|
|
|
|
return
|
|
|
|
Yap_unify(t,HeadOfTerm(out)) &&
|
|
|
|
Yap_unify(ARG3, TailOfTerm(out)) &&
|
|
|
|
Yap_unify(out, ARG2);
|
2006-03-30 02:11:10 +01:00
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
|
|
|
return Yap_unify(ARG2, ARG3);
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
out = vars_in_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1, ARG3);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
out = vars_in_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(f), ARG3);
|
|
|
|
}
|
|
|
|
if (out == 0L) {
|
2007-03-27 14:48:51 +01:00
|
|
|
if (!expand_vts())
|
2006-03-30 02:11:10 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} while (out == 0L);
|
|
|
|
|
2005-09-22 04:26:13 +01:00
|
|
|
return Yap_unify(ARG2,out);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Term non_singletons_in_complex_term(register CELL *pt0, register CELL *pt0_end)
|
|
|
|
{
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
register CELL **to_visit0, **to_visit = (CELL **)Yap_PreAllocCodeSpace();
|
2001-04-09 20:54:03 +01:00
|
|
|
register tr_fr_ptr TR0 = TR;
|
|
|
|
CELL *InitialH = H;
|
|
|
|
CELL output = AbsPair(H);
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
to_visit0 = to_visit;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
++ pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
|
|
|
deref_head(d0, vars_in_term_unk);
|
|
|
|
vars_in_term_nvar:
|
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
2005-04-13 20:37:32 +01:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-13 20:37:32 +01:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
} else if (d0 == TermFoundVar) {
|
|
|
|
CELL *pt2 = pt0;
|
|
|
|
while(IsVarTerm(*pt2))
|
|
|
|
pt2 = (CELL *)(*pt2);
|
|
|
|
H[1] = AbsPair(H+2);
|
|
|
|
H += 2;
|
|
|
|
H[-2] = (CELL)pt2;
|
|
|
|
*pt2 = TermReFoundVar;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar);
|
|
|
|
/* do or pt2 are unbound */
|
|
|
|
*ptd0 = TermFoundVar;
|
|
|
|
/* next make sure we can recover the variable again */
|
|
|
|
TrailTerm(TR++) = (CELL)ptd0;
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
2004-01-23 02:23:51 +00:00
|
|
|
if (to_visit > to_visit0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
#else
|
|
|
|
to_visit -= 2;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
clean_tr(TR0);
|
|
|
|
if (H != InitialH) {
|
|
|
|
/* close the list */
|
|
|
|
RESET_VARIABLE(H-1);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_unify((CELL)(H-1),ARG2);
|
2005-04-13 20:37:32 +01:00
|
|
|
return output;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2005-04-13 20:37:32 +01:00
|
|
|
return ARG2;
|
|
|
|
}
|
|
|
|
|
|
|
|
aux_overflow:
|
2007-03-27 14:48:51 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-13 20:37:32 +01:00
|
|
|
clean_tr(TR0);
|
|
|
|
if (H != InitialH) {
|
|
|
|
/* close the list */
|
|
|
|
RESET_VARIABLE(H-1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2005-04-13 20:37:32 +01:00
|
|
|
return 0L;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_non_singletons_in_term(void) /* non_singletons in term t */
|
|
|
|
{
|
2005-04-13 20:37:32 +01:00
|
|
|
Term t;
|
|
|
|
Term out;
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
out = MkPairTerm(t,ARG2);
|
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
|
|
|
out = ARG2;
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
out = non_singletons_in_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1);
|
|
|
|
} else {
|
|
|
|
out = non_singletons_in_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(FunctorOfTerm(t)));
|
|
|
|
}
|
|
|
|
if (out != 0L) {
|
|
|
|
return Yap_unify(ARG3,out);
|
|
|
|
} else {
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(0, NULL)) {
|
|
|
|
Yap_Error(OUT_OF_AUXSPACE_ERROR, ARG1, "overflow in singletons");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int ground_complex_term(register CELL *pt0, register CELL *pt0_end)
|
|
|
|
{
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
register CELL **to_visit0, **to_visit = (CELL **)Yap_PreAllocCodeSpace();
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
to_visit0 = to_visit;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
|
|
|
|
++pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
|
|
|
deref_head(d0, vars_in_term_unk);
|
|
|
|
vars_in_term_nvar:
|
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
2005-04-13 20:37:32 +01:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-13 20:37:32 +01:00
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar);
|
|
|
|
#ifdef RATIONAL_TREES
|
2004-01-23 02:23:51 +00:00
|
|
|
while (to_visit > to_visit0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-15 21:26:45 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
2004-01-23 02:23:51 +00:00
|
|
|
if (to_visit > to_visit0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
#else
|
|
|
|
to_visit -= 2;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
2005-04-13 20:37:32 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
aux_overflow:
|
2005-04-15 21:26:45 +01:00
|
|
|
/* unwind stack */
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-13 20:37:32 +01:00
|
|
|
return -1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_ground(void) /* ground(+T) */
|
|
|
|
{
|
2005-04-13 20:37:32 +01:00
|
|
|
Term t;
|
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
Int out;
|
|
|
|
|
|
|
|
t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return FALSE;
|
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
|
|
|
return TRUE;
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
if ((out =ground_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1)) >= 0) {
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(fun))
|
2007-11-06 17:02:13 +00:00
|
|
|
return TRUE;
|
2005-04-13 20:37:32 +01:00
|
|
|
else if ((out = ground_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(fun))) >= 0) {
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out < 0) {
|
|
|
|
if (!Yap_ExpandPreAllocCodeSpace(0, NULL)) {
|
|
|
|
Yap_Error(OUT_OF_AUXSPACE_ERROR, ARG1, "overflow in ground");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-06 17:02:13 +00:00
|
|
|
static int
|
|
|
|
SizeOfExtension(Term t)
|
|
|
|
{
|
|
|
|
Functor f = FunctorOfTerm(t);
|
|
|
|
if (f== FunctorDouble) {
|
|
|
|
return 2 + sizeof(Float)/sizeof(CELL);
|
|
|
|
}
|
|
|
|
if (f== FunctorLongInt) {
|
|
|
|
return 2 + sizeof(Float)/sizeof(CELL);
|
|
|
|
}
|
|
|
|
if (f== FunctorDBRef) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (f== FunctorBigInt) {
|
|
|
|
CELL *pt = RepAppl(t)+1;
|
|
|
|
return 2+sizeof(MP_INT)+(((MP_INT *)(pt+1))->_mp_alloc*sizeof(mp_limb_t));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Int sz_ground_complex_term(register CELL *pt0, register CELL *pt0_end, int ground)
|
|
|
|
{
|
|
|
|
|
|
|
|
register CELL **to_visit0, **to_visit = (CELL **)Yap_PreAllocCodeSpace();
|
|
|
|
Int sz = 0;
|
|
|
|
|
|
|
|
to_visit0 = to_visit;
|
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
|
|
|
|
++pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
|
|
|
deref_head(d0, vars_in_term_unk);
|
|
|
|
vars_in_term_nvar:
|
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
|
|
|
sz += 2;
|
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
sz += SizeOfExtension(d0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (to_visit + 1024 >= (CELL **)AuxSp) {
|
|
|
|
goto aux_overflow;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
sz += (1+d0);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
derefa_body(d0, ptd0, vars_in_term_unk, vars_in_term_nvar);
|
|
|
|
if (!ground)
|
|
|
|
continue;
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
|
|
|
if (to_visit > to_visit0) {
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
#else
|
|
|
|
to_visit -= 2;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
return sz;
|
|
|
|
|
|
|
|
aux_overflow:
|
|
|
|
/* unwind stack */
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit > to_visit0) {
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Yap_SizeGroundTerm(Term t, int ground)
|
|
|
|
{
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
if (!ground)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
|
|
|
return 1;
|
|
|
|
} else if (IsPairTerm(t)) {
|
|
|
|
int sz = sz_ground_complex_term(RepPair(t)-1, RepPair(t)+1, ground);
|
|
|
|
if (sz <= 0)
|
|
|
|
return sz;
|
|
|
|
return sz+2;
|
|
|
|
} else {
|
|
|
|
int sz = 0;
|
|
|
|
Functor fun = FunctorOfTerm(t);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(fun))
|
|
|
|
return 1+ SizeOfExtension(t);
|
|
|
|
|
|
|
|
sz = sz_ground_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(fun),
|
|
|
|
ground);
|
|
|
|
if (sz <= 0)
|
|
|
|
return sz;
|
|
|
|
return 1+ArityOfFunctor(fun)+sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int var_in_complex_term(register CELL *pt0,
|
|
|
|
register CELL *pt0_end,
|
2002-03-07 05:13:21 +00:00
|
|
|
Term v)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
register CELL **to_visit0, **to_visit = (CELL **)Yap_PreAllocCodeSpace();
|
2002-03-07 05:13:21 +00:00
|
|
|
register tr_fr_ptr TR0 = TR;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
to_visit0 = to_visit;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0;
|
|
|
|
register CELL *ptd0;
|
|
|
|
++ pt0;
|
|
|
|
ptd0 = pt0;
|
|
|
|
d0 = *ptd0;
|
2002-03-07 05:13:21 +00:00
|
|
|
deref_head(d0, var_in_term_unk);
|
|
|
|
var_in_term_nvar:
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
if (IsPairTerm(d0)) {
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2;
|
|
|
|
/* store the terms to visit */
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = (CELL *)*pt0;
|
|
|
|
to_visit += 3;
|
|
|
|
*pt0 = TermNil;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit += 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-07 05:13:21 +00:00
|
|
|
deref_body(d0, ptd0, var_in_term_unk, var_in_term_nvar);
|
|
|
|
if ((CELL)ptd0 == v) { /* we found it */
|
|
|
|
clean_tr(TR0);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
2002-03-07 05:13:21 +00:00
|
|
|
/* do or pt2 are unbound */
|
|
|
|
*ptd0 = TermNil;
|
|
|
|
/* next make sure noone will see this as a variable again */
|
|
|
|
TrailTerm(TR++) = (CELL)ptd0;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
2004-01-23 02:23:51 +00:00
|
|
|
if (to_visit > to_visit0) {
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
to_visit -= 3;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
*pt0 = (CELL)to_visit[2];
|
|
|
|
#else
|
|
|
|
to_visit -= 2;
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
2002-03-07 05:13:21 +00:00
|
|
|
clean_tr(TR0);
|
2005-04-13 20:37:32 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-03-07 05:13:21 +00:00
|
|
|
static Int
|
2001-04-09 20:54:03 +01:00
|
|
|
var_in_term(Term v, Term t) /* variables in term t */
|
|
|
|
{
|
|
|
|
|
2002-03-07 05:13:21 +00:00
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return(v == t);
|
|
|
|
} else if (IsPrimitiveTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
2002-03-07 05:13:21 +00:00
|
|
|
} else if (IsPairTerm(t)) {
|
2001-04-09 20:54:03 +01:00
|
|
|
return(var_in_complex_term(RepPair(t)-1,
|
|
|
|
RepPair(t)+1, v));
|
|
|
|
}
|
|
|
|
else return(var_in_complex_term(RepAppl(t),
|
|
|
|
RepAppl(t)+
|
|
|
|
ArityOfFunctor(FunctorOfTerm(t)),v));
|
|
|
|
}
|
|
|
|
|
2002-03-07 05:13:21 +00:00
|
|
|
static Int
|
|
|
|
p_var_in_term(void)
|
|
|
|
{
|
|
|
|
return(var_in_term(Deref(ARG2), Deref(ARG1)));
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* The code for TermHash was originally contributed by Gertjen Van Noor */
|
|
|
|
|
|
|
|
/* This code with max_depth == -1 will loop for infinite trees */
|
|
|
|
|
|
|
|
#define GvNht ((UInt *)H)
|
|
|
|
|
|
|
|
#define HASHADD(T) (GvNht[k]+=(T), k=(k<2 ? k+1 : 0))
|
|
|
|
|
|
|
|
static Int TermHash(Term t1, Int depth_lim, Int k)
|
|
|
|
{
|
|
|
|
Int i;
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
return(-1);
|
2006-09-01 21:14:42 +01:00
|
|
|
} else if (IsAtomOrIntTerm(t1)) {
|
|
|
|
if (IsAtomTerm(t1)) {
|
|
|
|
register char *s = AtomName(AtomOfTerm(t1));
|
|
|
|
for (i=0; s[i]; i++)
|
2001-04-09 20:54:03 +01:00
|
|
|
HASHADD(s[i]);
|
2006-09-01 21:14:42 +01:00
|
|
|
return k;
|
|
|
|
} else {
|
|
|
|
HASHADD(IntOfTerm(t1));
|
|
|
|
return k;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsPairTerm(t1)) {
|
|
|
|
HASHADD('.');
|
|
|
|
depth_lim--;
|
|
|
|
if (depth_lim == 0) return(TRUE);
|
|
|
|
k = TermHash(HeadOfTerm(t1),depth_lim,k);
|
2006-09-01 21:14:42 +01:00
|
|
|
if (k < 0) return k;
|
|
|
|
return TermHash(TailOfTerm(t1),depth_lim,k);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
Functor f = FunctorOfTerm(t1);
|
|
|
|
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
if (f == FunctorDouble) {
|
|
|
|
Int *iptr = (Int *)(RepAppl(t1)+1);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(Float) / sizeof(CELL); i++) {
|
|
|
|
HASHADD(*iptr++);
|
|
|
|
}
|
|
|
|
return(k);
|
|
|
|
} else if (f == FunctorLongInt) {
|
|
|
|
HASHADD(LongIntOfTerm(t1));
|
|
|
|
return(k);
|
|
|
|
} else if (f == FunctorDBRef) {
|
|
|
|
HASHADD((Int)DBRefOfTerm(t1));
|
|
|
|
return(k);
|
|
|
|
/* should never happen */
|
|
|
|
} else {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
} else {
|
2006-09-01 21:14:42 +01:00
|
|
|
int ar;
|
|
|
|
char *s;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2006-09-01 21:14:42 +01:00
|
|
|
s = AtomName(NameOfFunctor(f));
|
2001-04-09 20:54:03 +01:00
|
|
|
for (i=0; s[i]; i++)
|
|
|
|
HASHADD(s[i]);
|
2006-09-01 21:14:42 +01:00
|
|
|
depth_lim--;
|
|
|
|
if (depth_lim == 0) return k;
|
|
|
|
ar = ArityOfFunctor(f);
|
|
|
|
for (i=1; i<=ar; i++) {
|
2001-04-09 20:54:03 +01:00
|
|
|
k = TermHash(ArgOfTerm(i,t1),depth_lim,k);
|
2006-09-01 21:14:42 +01:00
|
|
|
if (k < 0) return k;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
return(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
GvNTermHash(void)
|
|
|
|
{
|
|
|
|
unsigned int i1,i2,i3;
|
|
|
|
Term t1 = Deref(ARG1);
|
|
|
|
Term t2 = Deref(ARG2);
|
|
|
|
Term t3 = Deref(ARG3);
|
|
|
|
Term result;
|
|
|
|
Int size, depth;
|
|
|
|
|
|
|
|
|
|
|
|
if (IsVarTerm(t2)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t2,"term_hash/4");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t2)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER,t2,"term_hash/4");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
depth = IntegerOfTerm(t2);
|
|
|
|
if (depth == 0) {
|
|
|
|
if (IsVarTerm(t1)) return(TRUE);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG4,MkIntTerm(0)));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
if (IsVarTerm(t3)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,t3,"term_hash/4");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsIntegerTerm(t3)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER,t3,"term_hash/4");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
size = IntegerOfTerm(t3);
|
|
|
|
GvNht[0] = 0;
|
|
|
|
GvNht[1] = 0;
|
|
|
|
GvNht[2] = 0;
|
|
|
|
|
|
|
|
if (TermHash(t1,depth,0) == -1) return(TRUE);
|
|
|
|
|
|
|
|
i1 = GvNht[0];
|
|
|
|
i2 = GvNht[1];
|
|
|
|
i3 = GvNht[2];
|
|
|
|
i2 ^= i3; i1 ^= i2; i1 = (((i3 << 7) + i2) << 7) + i1;
|
|
|
|
result = MkIntegerTerm(i1 % size);
|
2004-11-19 17:14:15 +00:00
|
|
|
return Yap_unify(ARG4,result);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int variant_complex(register CELL *pt0, register CELL *pt0_end, register
|
|
|
|
CELL *pt1)
|
|
|
|
{
|
|
|
|
tr_fr_ptr OLDTR = TR;
|
|
|
|
register CELL **to_visit = (CELL **)ASP;
|
|
|
|
/* make sure that unification always forces trailing */
|
|
|
|
HBREG = H;
|
2004-09-17 21:47:35 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0, d1;
|
|
|
|
++ pt0;
|
|
|
|
++ pt1;
|
|
|
|
d0 = Derefa(pt0);
|
|
|
|
d1 = Derefa(pt1);
|
|
|
|
if (IsVarTerm(d0)) {
|
|
|
|
if (IsVarTerm(d1)) {
|
2004-07-12 17:01:21 +01:00
|
|
|
CELL *pt0 = VarOfTerm(d0);
|
|
|
|
CELL *pt1 = VarOfTerm(d1);
|
|
|
|
if (pt0 >= HBREG || pt1 >= HBREG) {
|
|
|
|
/* one of the variables has been found before */
|
|
|
|
if (VarOfTerm(d0)+1 == VarOfTerm(d1)) continue;
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* two new occurrences of the same variable */
|
|
|
|
Term n0 = MkVarTerm(), n1 = MkVarTerm();
|
|
|
|
Bind_Global(VarOfTerm(d0), n0);
|
|
|
|
Bind_Global(VarOfTerm(d1), n1);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
} else if (IsVarTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
if (d0 == d1) continue;
|
|
|
|
else if (IsAtomOrIntTerm(d0)) {
|
|
|
|
goto fail;
|
|
|
|
} else if (IsPairTerm(d0)) {
|
|
|
|
if (!IsPairTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
/* now link the two structures so that no one else will */
|
|
|
|
/* come here */
|
|
|
|
to_visit -= 4;
|
2004-09-17 21:47:35 +01:00
|
|
|
if ((CELL *)to_visit < H+1024)
|
|
|
|
goto out_of_stack;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2003-10-28 16:20:44 +00:00
|
|
|
to_visit[3] = (CELL *)*pt0;
|
2001-04-09 20:54:03 +01:00
|
|
|
*pt0 = d1;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit -= 3;
|
2004-09-17 21:47:35 +01:00
|
|
|
if ((CELL *)to_visit < H+1024)
|
|
|
|
goto out_of_stack;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
pt1 = RepPair(d1) - 1;
|
|
|
|
continue;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2, *ap3;
|
|
|
|
if (!IsApplTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* store the terms to visit */
|
|
|
|
Functor f2;
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
ap3 = RepAppl(d1);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
f2 = (Functor)(*ap3);
|
|
|
|
if (f != f2)
|
|
|
|
goto fail;
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
if (!unify_extension(f, d0, ap2, d1))
|
|
|
|
goto fail;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
/* now link the two structures so that no one else will */
|
|
|
|
/* come here */
|
|
|
|
to_visit -= 4;
|
2004-09-17 21:47:35 +01:00
|
|
|
if ((CELL *)to_visit < H+1024)
|
|
|
|
goto out_of_stack;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2003-10-28 16:20:44 +00:00
|
|
|
to_visit[3] = (CELL *)*pt0;
|
2001-04-09 20:54:03 +01:00
|
|
|
*pt0 = d1;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
|
|
|
to_visit -= 3;
|
2004-09-17 21:47:35 +01:00
|
|
|
if ((CELL *)to_visit < H+1024)
|
|
|
|
goto out_of_stack;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
pt1 = ap3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
|
|
|
if (to_visit < (CELL **)ASP) {
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
*pt0 = (CELL)to_visit[3];
|
|
|
|
to_visit += 4;
|
|
|
|
#else
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
to_visit += 3;
|
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
H = HBREG;
|
|
|
|
/* untrail all bindings made by variant */
|
|
|
|
while (TR != (tr_fr_ptr)OLDTR) {
|
|
|
|
CELL *pt1 = (CELL *) TrailTerm(--TR);
|
|
|
|
RESET_VARIABLE(pt1);
|
|
|
|
}
|
|
|
|
HBREG = B->cp_h;
|
2004-09-17 21:47:35 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
out_of_stack:
|
|
|
|
H = HBREG;
|
|
|
|
/* untrail all bindings made by variant */
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit < (CELL **)ASP) {
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
*pt0 = (CELL)to_visit[3];
|
|
|
|
to_visit += 4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
while (TR != (tr_fr_ptr)OLDTR) {
|
|
|
|
CELL *pt1 = (CELL *) TrailTerm(--TR);
|
|
|
|
RESET_VARIABLE(pt1);
|
|
|
|
}
|
|
|
|
HBREG = B->cp_h;
|
|
|
|
return -1;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
/* failure */
|
|
|
|
H = HBREG;
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit < (CELL **)ASP) {
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
*pt0 = (CELL)to_visit[3];
|
|
|
|
to_visit += 4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* untrail all bindings made by variant */
|
|
|
|
while (TR != (tr_fr_ptr)OLDTR) {
|
|
|
|
CELL *pt1 = (CELL *) TrailTerm(--TR);
|
|
|
|
RESET_VARIABLE(pt1);
|
|
|
|
}
|
|
|
|
HBREG = B->cp_h;
|
2004-09-17 21:47:35 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_variant(void) /* variant terms t1 and t2 */
|
|
|
|
{
|
|
|
|
Term t1 = Deref(ARG1);
|
|
|
|
Term t2 = Deref(ARG2);
|
2004-09-17 21:47:35 +01:00
|
|
|
int out;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
if (t1 == t2)
|
|
|
|
return (TRUE);
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
if (IsVarTerm(t2))
|
|
|
|
return(TRUE);
|
|
|
|
return(FALSE);
|
|
|
|
} else if (IsVarTerm(t2))
|
|
|
|
return(FALSE);
|
|
|
|
if (IsAtomOrIntTerm(t1)) {
|
|
|
|
return(t1 == t2);
|
|
|
|
}
|
|
|
|
if (IsPairTerm(t1)) {
|
|
|
|
if (IsPairTerm(t2)) {
|
2004-09-17 21:47:35 +01:00
|
|
|
out = variant_complex(RepPair(t1)-1,
|
|
|
|
RepPair(t1)+1,
|
|
|
|
RepPair(t2)-1);
|
|
|
|
if (out < 0) goto error;
|
2004-10-06 17:55:48 +01:00
|
|
|
return out;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
else return (FALSE);
|
|
|
|
}
|
2004-09-17 21:47:35 +01:00
|
|
|
if (!IsApplTerm(t2)) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
2001-04-09 20:54:03 +01:00
|
|
|
Functor f1 = FunctorOfTerm(t1);
|
2004-09-17 21:47:35 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
if (f1 != FunctorOfTerm(t2)) return(FALSE);
|
|
|
|
if (IsExtensionFunctor(f1)) {
|
|
|
|
return(unify_extension(f1, t1, RepAppl(t1), t2));
|
|
|
|
}
|
2004-09-17 21:47:35 +01:00
|
|
|
out = variant_complex(RepAppl(t1),
|
|
|
|
RepAppl(t1)+ArityOfFunctor(f1),
|
|
|
|
RepAppl(t2));
|
|
|
|
if (out < 0) goto error;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
error:
|
|
|
|
if (out == -1) {
|
2008-08-28 04:43:00 +01:00
|
|
|
if (!Yap_gcl((ASP-H)*sizeof(CELL), 2, ENV, gc_P(P,CP))) {
|
2004-09-17 21:47:35 +01:00
|
|
|
Yap_Error(OUT_OF_STACK_ERROR, TermNil, "in variant");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return p_variant();
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2004-09-17 21:47:35 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int subsumes_complex(register CELL *pt0, register CELL *pt0_end, register
|
|
|
|
CELL *pt1)
|
|
|
|
{
|
|
|
|
register CELL **to_visit = (CELL **)ASP;
|
2004-07-12 17:01:21 +01:00
|
|
|
tr_fr_ptr OLDTR = TR, new_tr;
|
2005-03-13 06:26:13 +00:00
|
|
|
UInt write_mode = TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2004-07-12 17:01:21 +01:00
|
|
|
|
|
|
|
HBREG = H;
|
2001-04-09 20:54:03 +01:00
|
|
|
loop:
|
|
|
|
while (pt0 < pt0_end) {
|
|
|
|
register CELL d0, d1;
|
2005-02-21 16:50:21 +00:00
|
|
|
Int our_write_mode = write_mode;
|
2004-07-12 17:01:21 +01:00
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
++ pt0;
|
|
|
|
++ pt1;
|
2004-07-12 17:01:21 +01:00
|
|
|
/* this is a version of Derefa that checks whether we are trying to
|
|
|
|
do something evil */
|
|
|
|
{
|
|
|
|
CELL *npt0 = pt0;
|
|
|
|
|
|
|
|
restart_d0:
|
|
|
|
if (npt0 >= HBREG) {
|
|
|
|
our_write_mode = FALSE;
|
|
|
|
}
|
|
|
|
d0 = *npt0;
|
|
|
|
if (IsVarTerm(d0) &&
|
|
|
|
d0 != (CELL)npt0
|
|
|
|
) {
|
|
|
|
npt0 = (CELL *)d0;
|
|
|
|
goto restart_d0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CELL *npt1 = pt1;
|
|
|
|
|
|
|
|
restart_d1:
|
|
|
|
d1 = *npt1;
|
|
|
|
if (IsVarTerm(d1)
|
|
|
|
&& d1 != (CELL)npt1
|
|
|
|
) {
|
|
|
|
/* never dereference through a variable from the left-side */
|
|
|
|
if (npt1 >= HBREG) {
|
|
|
|
goto fail;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2004-07-12 17:01:21 +01:00
|
|
|
npt1 = (CELL *)d1;
|
|
|
|
goto restart_d1;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
2004-07-12 17:01:21 +01:00
|
|
|
}
|
|
|
|
if (IsVarTerm(d0)) {
|
|
|
|
if (our_write_mode) {
|
|
|
|
/* generate a new binding */
|
|
|
|
CELL *pt0 = VarOfTerm(d0);
|
|
|
|
Term new = MkVarTerm();
|
|
|
|
|
|
|
|
Bind_Global(pt0, new);
|
|
|
|
if (d0 != d1) { /* avoid loops */
|
|
|
|
Bind_Global(VarOfTerm(new), d1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (d0 == d1) continue;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
continue;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if (IsVarTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
if (d0 == d1) continue;
|
|
|
|
else if (IsAtomOrIntTerm(d0)) {
|
|
|
|
goto fail;
|
|
|
|
} else if (IsPairTerm(d0)) {
|
|
|
|
if (!IsPairTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
/* now link the two structures so that no one else will */
|
|
|
|
/* come here */
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit -= 5;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2003-10-28 16:20:44 +00:00
|
|
|
to_visit[3] = (CELL *)*pt0;
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit[4] = (CELL *)write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
*pt0 = d1;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit -= 4;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit[3] = (CELL *)write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-07-12 17:01:21 +01:00
|
|
|
write_mode = our_write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
pt0 = RepPair(d0) - 1;
|
|
|
|
pt0_end = RepPair(d0) + 1;
|
|
|
|
pt1 = RepPair(d1) - 1;
|
|
|
|
continue;
|
|
|
|
} else if (IsApplTerm(d0)) {
|
|
|
|
register Functor f;
|
|
|
|
register CELL *ap2, *ap3;
|
|
|
|
if (!IsApplTerm(d1)) {
|
|
|
|
goto fail;
|
|
|
|
} else {
|
|
|
|
/* store the terms to visit */
|
|
|
|
Functor f2;
|
|
|
|
ap2 = RepAppl(d0);
|
|
|
|
ap3 = RepAppl(d1);
|
|
|
|
f = (Functor)(*ap2);
|
|
|
|
f2 = (Functor)(*ap3);
|
|
|
|
if (f != f2)
|
|
|
|
goto fail;
|
|
|
|
if (IsExtensionFunctor(f)) {
|
|
|
|
if (!unify_extension(f, d0, ap2, d1))
|
|
|
|
goto fail;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
/* now link the two structures so that no one else will */
|
|
|
|
/* come here */
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit -= 5;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2003-10-28 16:20:44 +00:00
|
|
|
to_visit[3] = (CELL *)*pt0;
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit[4] = (CELL *)write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
*pt0 = d1;
|
|
|
|
#else
|
|
|
|
/* store the terms to visit */
|
|
|
|
if (pt0 < pt0_end) {
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit -= 4;
|
2001-04-09 20:54:03 +01:00
|
|
|
to_visit[0] = pt0;
|
|
|
|
to_visit[1] = pt0_end;
|
|
|
|
to_visit[2] = pt1;
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit[3] = (CELL *)write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-07-12 17:01:21 +01:00
|
|
|
write_mode = our_write_mode;
|
2001-04-09 20:54:03 +01:00
|
|
|
d0 = ArityOfFunctor(f);
|
|
|
|
pt0 = ap2;
|
|
|
|
pt0_end = ap2 + d0;
|
|
|
|
pt1 = ap3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Do we still have compound terms to visit */
|
|
|
|
if (to_visit < (CELL **)ASP) {
|
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
*pt0 = (CELL)to_visit[3];
|
2006-02-01 13:28:57 +00:00
|
|
|
write_mode = (Int)to_visit[ 4];
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit += 5;
|
2001-04-09 20:54:03 +01:00
|
|
|
#else
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
2005-07-20 14:54:42 +01:00
|
|
|
write_mode = (UInt)to_visit[3];
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit += 4;
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
2004-07-12 17:01:21 +01:00
|
|
|
H = HBREG;
|
|
|
|
/* get rid of intermediate variables */
|
|
|
|
new_tr = TR;
|
|
|
|
while (TR != OLDTR) {
|
|
|
|
/* cell we bound */
|
|
|
|
CELL *pt1 = (CELL *) TrailTerm(--TR);
|
|
|
|
/* cell we created */
|
|
|
|
CELL *npt1 = (CELL *)*pt1;
|
|
|
|
/* shorten the chain */
|
2004-09-18 15:03:42 +01:00
|
|
|
if (IsVarTerm(*pt1) && IsUnboundVar(pt1)) {
|
2004-07-12 17:01:21 +01:00
|
|
|
RESET_VARIABLE(pt1);
|
|
|
|
} else {
|
|
|
|
*pt1 = *npt1;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2004-07-12 17:01:21 +01:00
|
|
|
TR = new_tr;
|
|
|
|
HBREG = B->cp_h;
|
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
fail:
|
2004-07-12 17:01:21 +01:00
|
|
|
H = HBREG;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef RATIONAL_TREES
|
|
|
|
while (to_visit < (CELL **)ASP) {
|
|
|
|
pt0 = to_visit[0];
|
|
|
|
pt0_end = to_visit[1];
|
|
|
|
pt1 = to_visit[2];
|
|
|
|
*pt0 = (CELL)to_visit[3];
|
2004-07-12 17:01:21 +01:00
|
|
|
to_visit += 5;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2004-07-12 17:01:21 +01:00
|
|
|
/* untrail all bindings made by variant */
|
|
|
|
while (TR != (tr_fr_ptr)OLDTR) {
|
|
|
|
CELL *pt1 = (CELL *) TrailTerm(--TR);
|
|
|
|
RESET_VARIABLE(pt1);
|
|
|
|
}
|
|
|
|
HBREG = B->cp_h;
|
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
p_subsumes(void) /* subsumes terms t1 and t2 */
|
|
|
|
{
|
|
|
|
Term t1 = Deref(ARG1);
|
|
|
|
Term t2 = Deref(ARG2);
|
|
|
|
|
|
|
|
if (t1 == t2)
|
|
|
|
return (TRUE);
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
Bind(VarOfTerm(t1), t2);
|
|
|
|
return(TRUE);
|
|
|
|
} else if (IsVarTerm(t2))
|
|
|
|
return(FALSE);
|
|
|
|
if (IsAtomOrIntTerm(t1)) {
|
|
|
|
return(t1 == t2);
|
|
|
|
}
|
|
|
|
if (IsPairTerm(t1)) {
|
|
|
|
if (IsPairTerm(t2)) {
|
|
|
|
return(subsumes_complex(RepPair(t1)-1,
|
|
|
|
RepPair(t1)+1,
|
|
|
|
RepPair(t2)-1));
|
|
|
|
}
|
|
|
|
else return (FALSE);
|
|
|
|
} else {
|
|
|
|
Functor f1;
|
|
|
|
|
|
|
|
if (!IsApplTerm(t2)) return(FALSE);
|
|
|
|
f1 = FunctorOfTerm(t1);
|
|
|
|
if (f1 != FunctorOfTerm(t2))
|
|
|
|
return(FALSE);
|
|
|
|
if (IsExtensionFunctor(f1)) {
|
|
|
|
return(unify_extension(f1, t1, RepAppl(t1), t2));
|
|
|
|
}
|
|
|
|
return(subsumes_complex(RepAppl(t1),
|
|
|
|
RepAppl(t1)+ArityOfFunctor(f1),
|
|
|
|
RepAppl(t2)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static Int
|
|
|
|
p_force_trail_expansion()
|
|
|
|
{
|
|
|
|
Int i = IntOfTerm(Deref(ARG1))*1024, j = 0;
|
|
|
|
tr_fr_ptr OTR = TR;
|
|
|
|
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
TrailTerm(TR) = 0;
|
|
|
|
TR++;
|
|
|
|
}
|
|
|
|
TR = OTR;
|
|
|
|
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
|
|
|
camacho_dum(void)
|
|
|
|
{
|
|
|
|
Term t1, t2;
|
|
|
|
int max = 3;
|
|
|
|
|
|
|
|
/* build output list */
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
t1 = MkAtomTerm(Yap_LookupAtom("[]"));
|
2001-04-09 20:54:03 +01:00
|
|
|
t2 = MkPairTerm(MkIntegerTerm(max), t1);
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(t2, ARG1));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2008-03-13 14:38:02 +00:00
|
|
|
static Int
|
|
|
|
p_unifiable(void)
|
|
|
|
{
|
|
|
|
tr_fr_ptr trp;
|
|
|
|
Term tf = TermNil;
|
|
|
|
if (!Yap_unify(ARG1,ARG2)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
trp = TR;
|
|
|
|
while (trp != B->cp_tr) {
|
|
|
|
Term t[2];
|
|
|
|
--trp;
|
|
|
|
t[0] = TrailTerm(trp);
|
|
|
|
t[1] = *(CELL *)t[0];
|
|
|
|
tf = MkPairTerm(Yap_MkApplTerm(FunctorEq,2,t),tf);
|
|
|
|
RESET_VARIABLE(t[0]);
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG3, tf);
|
|
|
|
}
|
|
|
|
|
2002-11-18 18:18:05 +00:00
|
|
|
void Yap_InitUtilCPreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2004-05-13 21:54:58 +01:00
|
|
|
Term cm = CurrentModule;
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("copy_term", 2, p_copy_term, 0);
|
2007-09-21 14:52:52 +01:00
|
|
|
Yap_InitCPred("duplicate_term", 2, p_duplicate_term, 0);
|
2005-10-30 01:32:55 +01:00
|
|
|
Yap_InitCPred("copy_term_nat", 2, p_copy_term_no_delays, 0);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("ground", 1, p_ground, SafePredFlag);
|
2006-03-30 02:11:10 +01:00
|
|
|
Yap_InitCPred("$variables_in_term", 3, p_variables_in_term, HiddenPredFlag);
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$non_singletons_in_term", 3, p_non_singletons_in_term, SafePredFlag|HiddenPredFlag);
|
2006-03-30 02:11:10 +01:00
|
|
|
Yap_InitCPred("term_variables", 2, p_term_variables, 0);
|
|
|
|
Yap_InitCPred("term_variables", 3, p_term_variables3, 0);
|
2008-08-12 02:27:23 +01:00
|
|
|
CurrentModule = TERMS_MODULE;
|
2004-02-09 14:19:05 +00:00
|
|
|
Yap_InitCPred("variable_in_term", 2, p_var_in_term, SafePredFlag);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("term_hash", 4, GvNTermHash, SafePredFlag);
|
2004-10-06 17:55:48 +01:00
|
|
|
Yap_InitCPred("variant", 2, p_variant, 0);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("subsumes", 2, p_subsumes, SafePredFlag);
|
2008-03-13 14:38:02 +00:00
|
|
|
Yap_InitCPred("protected_unifiable", 3, p_unifiable, 0);
|
2004-05-13 21:54:58 +01:00
|
|
|
CurrentModule = cm;
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef DEBUG
|
2004-11-18 22:32:40 +00:00
|
|
|
Yap_InitCPred("$force_trail_expansion", 1, p_force_trail_expansion, SafePredFlag|HiddenPredFlag);
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("dum", 1, camacho_dum, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|