fix arithmetic dependence on Prolog table.

This commit is contained in:
Vitor Santos Costa 2009-02-25 00:12:36 +00:00
parent ffb621c53b
commit 2bc5d8425a
4 changed files with 75 additions and 61 deletions

View File

@ -781,6 +781,40 @@ p_unary_is(void)
return(FALSE);
}
static Int
p_unary_op_as_integer(void)
{ /* X is Y */
Term t = Deref(ARG1);
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,t, "X is Y");
return(FALSE);
}
if (IsIntTerm(t)) {
return Yap_unify_constant(ARG2,t);
}
if (IsAtomTerm(t)) {
Atom name = AtomOfTerm(t);
ExpEntry *p;
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 1)))) {
Term ti[2];
/* error */
ti[0] = t;
ti[1] = MkIntTerm(1);
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
Yap_Error(TYPE_ERROR_EVALUABLE, t,
"functor %s/%d for arithmetic expression",
RepAtom(name)->StrOfAE,2);
P = (yamop *)FAILCODE;
return(FALSE);
}
return Yap_unify_constant(ARG2,MkIntTerm(p->FOfEE));
}
return(FALSE);
}
void
Yap_InitUnaryExps(void)
{
@ -808,7 +842,7 @@ Yap_InitUnaryExps(void)
WRITE_UNLOCK(ae->ARWLock);
}
Yap_InitCPred("is", 3, p_unary_is, TestPredFlag | SafePredFlag);
}
Yap_InitCPred("$unary_op_as_integer", 2, p_unary_op_as_integer, TestPredFlag|SafePredFlag);}
/* This routine is called from Restore to make sure we have the same arithmetic operators */
int

View File

@ -1102,6 +1102,41 @@ p_binary_is(void)
return(FALSE);
}
static Int
p_binary_op_as_integer(void)
{ /* X is Y */
Term t = Deref(ARG1);
if (IsVarTerm(t)) {
Yap_Error(INSTANTIATION_ERROR,t, "X is Y");
return(FALSE);
}
if (IsIntTerm(t)) {
return Yap_unify_constant(ARG2,t);
}
if (IsAtomTerm(t)) {
Atom name = AtomOfTerm(t);
ExpEntry *p;
if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 2)))) {
Term ti[2];
/* error */
ti[0] = t;
ti[1] = MkIntTerm(1);
t = Yap_MkApplTerm(FunctorSlash, 2, ti);
Yap_Error(TYPE_ERROR_EVALUABLE, t,
"functor %s/%d for arithmetic expression",
RepAtom(name)->StrOfAE,2);
P = (yamop *)FAILCODE;
return(FALSE);
}
return Yap_unify_constant(ARG2,MkIntTerm(p->FOfEE));
}
return(FALSE);
}
void
Yap_InitBinaryExps(void)
{
@ -1125,6 +1160,7 @@ Yap_InitBinaryExps(void)
WRITE_UNLOCK(ae->ARWLock);
}
Yap_InitCPred("is", 4, p_binary_is, TestPredFlag | SafePredFlag);
Yap_InitCPred("$binary_op_as_integer", 2, p_binary_op_as_integer, TestPredFlag|SafePredFlag);
}
/* This routine is called from Restore to make sure we have the same arithmetic operators */

View File

@ -131,16 +131,16 @@ BEAM_is(void)
#endif
static Int
p_is(CELL result, CELL in)
p_is(void)
{ /* X is Y */
Term out;
out = Eval(Deref(in));
out = Eval(Deref(ARG2));
if (out == 0L) {
Yap_Error(EVALUATION_ERROR_INT_OVERFLOW, ARG2, "is/2");
return FALSE;
}
return Yap_unify_constant(result,out);
return Yap_unify_constant(ARG1,out);
}
void
@ -150,6 +150,6 @@ Yap_InitEval(void)
Yap_InitConstExps();
Yap_InitUnaryExps();
Yap_InitBinaryExps();
Yap_InitCmpPred("is", 2, p_is, BinaryPredFlag | TestPredFlag | SafePredFlag);
Yap_InitCPred("is", 2, p_is, SafePredFlag);
}

View File

@ -321,62 +321,6 @@ do_not_compile_expressions :- set_value('$c_arith',[]).
'$binaryop'(atan2(X,Y) ,atan2 ,X,Y).
% The table number for each operation is given here
% Depends on eval.c
'$unary_op_as_integer'(+,0).
'$unary_op_as_integer'(-,1).
'$unary_op_as_integer'(\,2).
'$unary_op_as_integer'(exp,3).
'$unary_op_as_integer'(log,4).
'$unary_op_as_integer'(log10,5).
'$unary_op_as_integer'(sqrt,6).
'$unary_op_as_integer'(sin,7).
'$unary_op_as_integer'(cos,8).
'$unary_op_as_integer'(tan,9).
'$unary_op_as_integer'(sinh,10).
'$unary_op_as_integer'(cosh,11).
'$unary_op_as_integer'(tanh,12).
'$unary_op_as_integer'(asin,13).
'$unary_op_as_integer'(acos,14).
'$unary_op_as_integer'(atan,15).
'$unary_op_as_integer'(asinh,16).
'$unary_op_as_integer'(acosh,17).
'$unary_op_as_integer'(atanh,18).
'$unary_op_as_integer'(floor,19).
'$unary_op_as_integer'(ceiling,20).
'$unary_op_as_integer'(round,21).
'$unary_op_as_integer'(truncate,22).
'$unary_op_as_integer'(integer,23).
'$unary_op_as_integer'(float,24).
'$unary_op_as_integer'(abs,25).
'$unary_op_as_integer'(msb,26).
'$unary_op_as_integer'(float_fractional_part,27).
'$unary_op_as_integer'(float_integer_part,28).
'$unary_op_as_integer'(sign,29).
'$unary_op_as_integer'(lgamma,30).
'$unary_op_as_integer'(random,31).
'$binary_op_as_integer'(+,0).
'$binary_op_as_integer'(-,1).
'$binary_op_as_integer'(*,2).
'$binary_op_as_integer'(/,3).
'$binary_op_as_integer'(mod,4).
'$binary_op_as_integer'(rem,5).
'$binary_op_as_integer'(//,6).
'$binary_op_as_integer'(<<,7).
'$binary_op_as_integer'(>>,8).
'$binary_op_as_integer'(/\,9).
'$binary_op_as_integer'(\/,10).
'$binary_op_as_integer'('#',11).
'$binary_op_as_integer'(atan2,12).
'$binary_op_as_integer'(^,13).
'$binary_op_as_integer'('**',14).
'$binary_op_as_integer'(exp,15).
'$binary_op_as_integer'(gcd,16).
'$binary_op_as_integer'(min,17).
'$binary_op_as_integer'(max,18).
%'$binary_op_as_integer'(gcdmult,28).
/* Arithmetics */
% M and N nonnegative integers, N is the successor of M