18static char SccsId[] =
"%W% %G%";
33static Term AddVarIfNotThere(Term var, Term dest USES_REGS) {
35 while (test != TermNil) {
36 if ((RepPair(test))[0] == var)
39 test = (RepPair(test))[1];
41 return (MkPairTerm(var, dest));
45static int can_unify_complex(
register CELL *pt0,
register CELL *pt0_end,
46 register CELL *pt1, Term *Vars USES_REGS) {
53 register CELL **to_visit = (CELL **)Yap_PreAllocCodeSpace();
54 CELL **to_visit_base = to_visit;
63 while (pt0 < pt0_end) {
73 *Vars = AddVarIfNotThere(d0, AddVarIfNotThere(d1, *Vars PASS_REGS)
79 Bind_Global_NonAtt((CELL *)d1, d0);
81 Bind_Global_NonAtt((CELL *)d0, d1);
88 *Vars = AddVarIfNotThere(d0, *Vars PASS_REGS);
91 Bind_Global_NonAtt((CELL *)d0, d1);
93 }
else if (IsVarTerm(d1)) {
94 *Vars = AddVarIfNotThere(d1, *Vars PASS_REGS);
96 Bind_Global_NonAtt((CELL *)d1, d0);
101 if (IsAtomOrIntTerm(d0) || IsAtomOrIntTerm(d1)) {
103 goto comparison_failed;
105 }
else if (IsPairTerm(d0)) {
107 goto comparison_failed;
110 to_visit[1] = pt0_end;
112 to_visit[3] = (CELL *)*pt0;
119 to_visit[1] = pt0_end;
124 pt0 = RepPair(d0) - 1;
125 pt0_end = RepPair(d0) + 1;
126 pt1 = RepPair(d1) - 1;
128 }
else if (IsApplTerm(d0)) {
130 register CELL *ap2, *ap3;
131 if (!IsApplTerm(d1)) {
132 goto comparison_failed;
140 goto comparison_failed;
142 if (IsExtensionFunctor(f)) {
144 case (CELL) FunctorDBRef:
147 goto comparison_failed;
148 case (CELL) FunctorLongInt:
149 if (ap2[1] == ap3[1])
151 goto comparison_failed;
152 case (CELL) FunctorDouble:
153 if (FloatOfTerm(d0) == FloatOfTerm(d1))
155 goto comparison_failed;
156 case (CELL) FunctorString:
157 if (strcmp((
char *)StringOfTerm(d0), (
char *)StringOfTerm(d1)) ==
160 goto comparison_failed;
162 case (CELL) FunctorBigInt:
163 if (Yap_gmp_tcmp_big_big(d0, d1) == 0)
165 goto comparison_failed;
168 goto comparison_failed;
173 to_visit[1] = pt0_end;
175 to_visit[3] = (CELL *)*pt0;
182 to_visit[1] = pt0_end;
187 d0 = ArityOfFunctor(f);
197 if (to_visit > (CELL **)to_visit_base) {
201 pt0_end = to_visit[1];
203 *pt0 = (CELL)to_visit[3];
207 pt0_end = to_visit[1];
213 Yap_ReleasePreAllocCodeSpace((ADDR)to_visit);
218 while (TR != saved_TR) {
219 pt1 = (CELL *)(TrailTerm(--TR));
226 Yap_ReleasePreAllocCodeSpace((ADDR)to_visit);
228 while (to_visit > (CELL **)to_visit_base) {
231 pt0_end = to_visit[1];
233 *pt0 = (CELL)to_visit[3];
240 while (TR != saved_TR) {
241 pt1 = (CELL *)(TrailTerm(--TR));
248static int can_unify(Term t1, Term t2, Term *Vars USES_REGS) {
260 *Vars = MkPairTerm(t1, MkPairTerm(t2, TermNil));
263 *Vars = MkPairTerm(t1, TermNil);
266 }
else if (IsVarTerm(t2)) {
268 *Vars = MkPairTerm(t2, TermNil);
272 if (IsAtomOrIntTerm(t1) || IsAtomOrIntTerm(t2)) {
282 }
else if (IsPairTerm(t1)) {
283 if (IsPairTerm(t2)) {
284 return (can_unify_complex(RepPair(t1) - 1, RepPair(t1) + 1,
285 RepPair(t2) - 1, Vars PASS_REGS));
290 if (f != FunctorOfTerm(t2))
292 if (IsExtensionFunctor(f)) {
294 case (CELL) FunctorDBRef:
298 case (CELL) FunctorLongInt:
299 if (RepAppl(t1)[1] == RepAppl(t2)[1])
302 case (CELL) FunctorString:
303 if (strcmp((
char *)StringOfTerm(t1), (
char *)StringOfTerm(t2)) == 0)
306 case (CELL) FunctorDouble:
307 if (FloatOfTerm(t1) == FloatOfTerm(t2))
311 case (CELL) FunctorBigInt:
312 if (Yap_gmp_tcmp_big_big(t1, t2) == 0)
321 return can_unify_complex(RepAppl(t1), RepAppl(t1) + ArityOfFunctor(f),
322 RepAppl(t2), Vars PASS_REGS);
327static int non_ground_complex(
register CELL *pt0,
register CELL *pt0_end,
328 Term *Var USES_REGS) {
330 register CELL **to_visit = (CELL **)Yap_PreAllocCodeSpace();
331 CELL **to_visit_base = to_visit;
334 while (pt0 < pt0_end) {
342 if (IsPairTerm(d0)) {
343 if (to_visit + 1024 >= (CELL **)AuxSp) {
348 to_visit[1] = pt0_end;
349 to_visit[2] = (CELL *)*pt0;
356 to_visit[1] = pt0_end;
360 pt0 = RepPair(d0) - 1;
361 pt0_end = RepPair(d0) + 1;
362 }
else if (IsApplTerm(d0)) {
370 if (IsExtensionFunctor(f)) {
373 if (to_visit + 1024 >= (CELL **)AuxSp) {
378 to_visit[1] = pt0_end;
379 to_visit[2] = (CELL *)*pt0;
386 to_visit[1] = pt0_end;
390 d0 = ArityOfFunctor(f);
398 if (to_visit > (CELL **)to_visit_base) {
402 pt0_end = to_visit[1];
403 *pt0 = (CELL)to_visit[2];
407 pt0_end = to_visit[1];
413 Yap_ReleasePreAllocCodeSpace((ADDR)to_visit);
418 Yap_ReleasePreAllocCodeSpace((ADDR)to_visit);
420 while (to_visit > (CELL **)to_visit_base) {
423 pt0_end = to_visit[1];
424 *pt0 = (CELL)to_visit[2];
432 Yap_ReleasePreAllocCodeSpace((ADDR)to_visit);
434 while (to_visit > (CELL **)to_visit_base) {
437 *pt0 = (CELL)to_visit[2];
443static int non_ground(Term t, Term *Var USES_REGS) {
452 if (IsPrimitiveTerm(t)) {
454 }
else if (IsPairTerm(t)) {
455 out = non_ground_complex(RepPair(t) - 1, RepPair(t) + 1, Var PASS_REGS);
460 if (IsExtensionFunctor(f)) {
463 out = non_ground_complex(RepAppl(t),
464 RepAppl(t) + ArityOfFunctor(FunctorOfTerm(t)),
469 if (!Yap_ExpandPreAllocCodeSpace(0, NULL, TRUE)) {
470 Yap_Error(RESOURCE_ERROR_AUXILIARY_STACK, ARG1,
"overflow in ground");
481static Int p_can_unify(USES_REGS1) {
484 if (!can_unify(ARG1, ARG2, &r PASS_REGS))
486 return Yap_unify(ARG3, r);
493static Int p_non_ground(USES_REGS1) {
496 if (!non_ground(ARG1, &r PASS_REGS))
498 return (Yap_unify(ARG2, r));
505static Int p_coroutining(USES_REGS1) {
514static Term ListOfWokenGoals(USES_REGS1) {
515 return Yap_ReadTimedVar(LOCAL_WokenGoals);
518Term Yap_ListOfWokenGoals(
void) {
520 return ListOfWokenGoals(PASS_REGS1);
525static Int p_awoken_goals(USES_REGS1) {
527 Term WGs = Yap_ReadTimedVar(LOCAL_WokenGoals);
528 if (WGs == TermNil) {
531 WGs = ListOfWokenGoals(PASS_REGS1);
532 Yap_UpdateTimedVar(LOCAL_WokenGoals, TermTrue);
533 return (Yap_unify(ARG1, WGs));
539static Int p_yap_has_rational_trees(USES_REGS1) {
547static Int p_yap_has_coroutining(USES_REGS1) {
555void Yap_InitCoroutPreds(
void) {
561 pred = RepPredProp(PredPropByFunc(Yap_MkFunctor(at, 2), 0));
564 Yap_InitAttVarPreds();
565 Yap_InitCPred(
"$yap_has_rational_trees", 0, p_yap_has_rational_trees,
567 Yap_InitCPred(
"$yap_has_coroutining", 0, p_yap_has_coroutining, SafePredFlag);
568 Yap_InitCPred(
"$can_unify", 3, p_can_unify, SafePredFlag);
569 Yap_InitCPred(
"$non_ground", 2, p_non_ground, SafePredFlag);
570 Yap_InitCPred(
"$coroutining", 0, p_coroutining, SafePredFlag);
571 Yap_InitCPred(
"$awoken_goals", 1, p_awoken_goals, SafePredFlag);