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: mavar.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: support from multiple assignment variables in YAP *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
|
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
#include "eval.h"
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static Int p_setarg( USES_REGS1 );
|
|
|
|
static Int p_create_mutable( USES_REGS1 );
|
|
|
|
static Int p_get_mutable( USES_REGS1 );
|
|
|
|
static Int p_update_mutable( USES_REGS1 );
|
|
|
|
static Int p_is_mutable( USES_REGS1 );
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_setarg( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2007-10-10 10:44:28 +01:00
|
|
|
CELL ti = Deref(ARG1), ts = Deref(ARG2), t3 = Deref(ARG3);
|
2001-04-09 20:54:03 +01:00
|
|
|
Int i;
|
2007-10-10 10:44:28 +01:00
|
|
|
|
|
|
|
if (IsVarTerm(t3) &&
|
|
|
|
VarOfTerm(t3) > H &&VarOfTerm(t3) < ASP) {
|
|
|
|
/* local variable */
|
|
|
|
Term tn = MkVarTerm();
|
|
|
|
Bind_Local(VarOfTerm(t3), tn);
|
|
|
|
t3 = tn;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
if (IsVarTerm(ti)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,ti,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
|
|
|
if (IsIntTerm(ti))
|
|
|
|
i = IntOfTerm(ti);
|
|
|
|
else {
|
2008-12-04 23:33:32 +00:00
|
|
|
Term te = Yap_Eval(ti);
|
|
|
|
if (IsIntegerTerm(te)) {
|
|
|
|
i = IntegerOfTerm(te);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_INTEGER,ti,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IsVarTerm(ts)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR,ts,"setarg/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if(IsApplTerm(ts)) {
|
|
|
|
CELL *pt;
|
|
|
|
if (IsExtensionFunctor(FunctorOfTerm(ts))) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_COMPOUND,ts,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2003-08-14 12:59:26 +01:00
|
|
|
if (i < 1 || i > (Int)ArityOfFunctor(FunctorOfTerm(ts))) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (i<0)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO,ts,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2003-08-14 12:59:26 +01:00
|
|
|
if (i==0)
|
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_ZERO,ts,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
pt = RepAppl(ts)+i;
|
|
|
|
/* the evil deed is to be done now */
|
2007-10-10 10:44:28 +01:00
|
|
|
MaBind(pt, t3);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else if(IsPairTerm(ts)) {
|
|
|
|
CELL *pt;
|
2003-08-27 14:37:10 +01:00
|
|
|
if (i < 1 || i > 2) {
|
2001-04-09 20:54:03 +01:00
|
|
|
if (i<0)
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO,ts,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
pt = RepPair(ts)+i-1;
|
|
|
|
/* the evil deed is to be done now */
|
2007-10-10 10:44:28 +01:00
|
|
|
MaBind(pt, t3);
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_COMPOUND,ts,"setarg/3");
|
2007-10-10 10:44:28 +01:00
|
|
|
return FALSE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
2007-10-10 10:44:28 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* One problem with MAVars is that they you always trail on
|
|
|
|
non-determinate bindings. This is not cool if you have a long
|
|
|
|
determinate computation. One alternative could be to use
|
|
|
|
timestamps.
|
|
|
|
|
|
|
|
Because of !, the only timestamp one can trust is the trailpointer
|
2001-05-21 21:00:05 +01:00
|
|
|
(ouch..). The trail is not reclaimed after cuts. Also, if there was
|
|
|
|
a conditional binding, the trail is sure to have been increased
|
|
|
|
since the last choicepoint. For maximum effect, we can actually
|
|
|
|
store the current value of TR in the timestamp field, giving a way
|
|
|
|
to actually follow a link of all trailings for these variables.
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* create and initialise a new timed var. The problem is: how to set
|
|
|
|
the clock?
|
|
|
|
|
|
|
|
If I give it the current value of B->TR, we may have trouble if no
|
|
|
|
non-determinate bindings are made before the next
|
|
|
|
choice-point. Just to make sure this doesn't cause trouble, if (TR
|
|
|
|
== B->TR) we will add a little something ;-).
|
|
|
|
*/
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
2011-03-07 16:02:55 +00:00
|
|
|
NewTimedVar(CELL val USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2009-03-31 21:55:17 +01:00
|
|
|
Term out;
|
2001-06-06 20:10:51 +01:00
|
|
|
timed_var *tv;
|
2009-04-04 23:56:42 +01:00
|
|
|
if (IsVarTerm(val) &&
|
2009-03-31 21:55:17 +01:00
|
|
|
VarOfTerm(val) > H) {
|
|
|
|
Term nval = MkVarTerm();
|
|
|
|
Bind_Local(VarOfTerm(val), nval);
|
|
|
|
val = nval;
|
|
|
|
}
|
|
|
|
out = AbsAppl(H);
|
2001-04-09 20:54:03 +01:00
|
|
|
*H++ = (CELL)FunctorMutable;
|
2001-06-06 20:10:51 +01:00
|
|
|
tv = (timed_var *)H;
|
|
|
|
RESET_VARIABLE(&(tv->clock));
|
|
|
|
tv->value = val;
|
|
|
|
H += sizeof(timed_var)/sizeof(CELL);
|
|
|
|
return(out);
|
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_NewTimedVar(CELL val)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return NewTimedVar(val PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
2011-03-07 16:02:55 +00:00
|
|
|
Yap_NewEmptyTimedVar( void )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2001-07-04 17:48:54 +01:00
|
|
|
Term out = AbsAppl(H);
|
2001-06-06 20:10:51 +01:00
|
|
|
timed_var *tv;
|
2001-04-09 20:54:03 +01:00
|
|
|
*H++ = (CELL)FunctorMutable;
|
2001-06-06 20:10:51 +01:00
|
|
|
tv = (timed_var *)H;
|
|
|
|
RESET_VARIABLE(&(tv->clock));
|
|
|
|
RESET_VARIABLE(&(tv->value));
|
|
|
|
H += sizeof(timed_var)/sizeof(CELL);
|
|
|
|
return(out);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
|
|
|
ReadTimedVar(Term inv)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
timed_var *tv = (timed_var *)(RepAppl(inv)+1);
|
|
|
|
return(tv->value);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReadTimedVar(Term inv)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
|
|
|
return ReadTimedVar(inv);
|
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
/* update a timed var with a new value */
|
2002-11-11 17:38:10 +00:00
|
|
|
static Term
|
2011-03-07 16:02:55 +00:00
|
|
|
UpdateTimedVar(Term inv, Term new USES_REGS)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
timed_var *tv = (timed_var *)(RepAppl(inv)+1);
|
|
|
|
CELL t = tv->value;
|
2001-07-04 17:48:54 +01:00
|
|
|
CELL* timestmp = (CELL *)(tv->clock);
|
2009-04-04 23:56:42 +01:00
|
|
|
if (IsVarTerm(new) &&
|
2009-03-31 21:55:17 +01:00
|
|
|
VarOfTerm(new) > H) {
|
|
|
|
Term nnew = MkVarTerm();
|
|
|
|
Bind_Local(VarOfTerm(new), nnew);
|
|
|
|
new = nnew;
|
|
|
|
}
|
2005-08-05 15:55:03 +01:00
|
|
|
if (timestmp > B->cp_h
|
|
|
|
#if FROZEN_STACKS
|
|
|
|
&& timestmp > H_FZ
|
|
|
|
#endif
|
|
|
|
) {
|
2001-07-04 17:48:54 +01:00
|
|
|
/* last assignment more recent than last B */
|
2011-03-30 15:32:59 +01:00
|
|
|
#if YAPOR_SBA
|
2001-07-04 17:48:54 +01:00
|
|
|
if (Unsigned((Int)(tv)-(Int)(H_FZ)) >
|
|
|
|
Unsigned((Int)(B_FZ)-(Int)(H_FZ)))
|
|
|
|
*STACK_TO_SBA(&(tv->value)) = new;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
tv->value = new;
|
2001-04-09 20:54:03 +01:00
|
|
|
} else {
|
2001-06-06 20:10:51 +01:00
|
|
|
Term nclock = (Term)H;
|
2001-04-09 20:54:03 +01:00
|
|
|
MaBind(&(tv->value), new);
|
2001-06-06 20:10:51 +01:00
|
|
|
*H++ = TermFoundVar;
|
2001-04-09 20:54:03 +01:00
|
|
|
MaBind(&(tv->clock), nclock);
|
|
|
|
}
|
|
|
|
return(t);
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
/* update a timed var with a new value */
|
|
|
|
Term
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_UpdateTimedVar(Term inv, Term new)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
|
|
|
return UpdateTimedVar(inv, new PASS_REGS);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_create_mutable( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
Term t = NewTimedVar(Deref(ARG1) PASS_REGS);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG2,t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_get_mutable( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "get_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsApplTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_COMPOUND,t,"get_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (FunctorOfTerm(t) != FunctorMutable) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_MUTABLE,t,"get_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
t = ReadTimedVar(t);
|
2002-11-18 18:18:05 +00:00
|
|
|
return(Yap_unify(ARG1, t));
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_update_mutable( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, "update_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsApplTerm(t)) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(TYPE_ERROR_COMPOUND,t,"update_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (FunctorOfTerm(t) != FunctorMutable) {
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_Error(DOMAIN_ERROR_MUTABLE,t,"update_mutable/3");
|
2001-04-09 20:54:03 +01:00
|
|
|
return(FALSE);
|
|
|
|
}
|
2011-03-07 16:02:55 +00:00
|
|
|
UpdateTimedVar(t, Deref(ARG1) PASS_REGS);
|
2001-04-09 20:54:03 +01:00
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_is_mutable( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (IsVarTerm(t)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (!IsApplTerm(t)) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
if (FunctorOfTerm(t) != FunctorMutable) {
|
|
|
|
return(FALSE);
|
|
|
|
}
|
|
|
|
return(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitMaVarCPreds(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
|
|
|
#ifdef MULTI_ASSIGNMENT_VARIABLES
|
|
|
|
/* The most famous contributions of SICStus to the Prolog language */
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitCPred("setarg", 3, p_setarg, SafePredFlag);
|
|
|
|
Yap_InitCPred("create_mutable", 2, p_create_mutable, SafePredFlag);
|
|
|
|
Yap_InitCPred("get_mutable", 2, p_get_mutable, SafePredFlag);
|
|
|
|
Yap_InitCPred("update_mutable", 2, p_update_mutable, SafePredFlag);
|
|
|
|
Yap_InitCPred("is_mutable", 1, p_is_mutable, SafePredFlag);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
}
|