support for init, callbacks

This commit is contained in:
Vítor Santos Costa 2014-06-11 19:27:54 +01:00
parent 2455a1d83c
commit 0241151599
2 changed files with 815 additions and 657 deletions

View File

@ -2,290 +2,299 @@
#define YAP_CPP_INTERFACE 1
#include "yapi.hh"
#include "SWI-Stream.h"
YAPAtomTerm::YAPAtomTerm(char *s) { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
}
YAPAtomTerm::YAPAtomTerm(char *s, size_t len) { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
}
YAPAtomTerm::YAPAtomTerm(wchar_t *s): YAPTerm() { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
}
YAPAtomTerm::YAPAtomTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_ATOM|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( MkAtomTerm(out.val.a) );
else t = 0L;
RECOVER_H();
}
YAPStringTerm::YAPStringTerm(char *s) { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
}
YAPStringTerm::YAPStringTerm(char *s, size_t len) { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.c = s;
inp.type = YAP_STRING_CHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
}
YAPStringTerm::YAPStringTerm(wchar_t *s): YAPTerm() { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
}
YAPStringTerm::YAPStringTerm(wchar_t *s, size_t len) : YAPTerm() { // build string
BACKUP_H();
BACKUP_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
CACHE_REGS
seq_tv_t inp, out;
inp.val.w = s;
inp.type = YAP_STRING_WCHARS;
out.type = YAP_STRING_STRING|YAP_STRING_NCHARS|YAP_STRING_TRUNC;
out.sz = len;
out.max = len;
if (Yap_CVT_Text(&inp, &out PASS_REGS))
mk ( out.val.t );
else t = 0L;
RECOVER_H();
}
YAPApplTerm::YAPApplTerm(YAPFunctor f, YAPTerm ts[]) : YAPTerm() {
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkApplTerm( f.f, arity, (Term *)ts) );
}
YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() {
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkNewApplTerm( f.f, arity) );
UInt arity = ArityOfFunctor(f.f);
mk ( Yap_MkNewApplTerm( f.f, arity) );
}
YAPTerm YAPApplTerm::getArg(int arg) {
return YAPTerm( ArgOfTerm(arg, gt() ) );
return YAPTerm( ArgOfTerm(arg, gt() ) );
}
YAPFunctor YAPApplTerm::getFunctor() {
return YAPFunctor( FunctorOfTerm( gt( )) );
return YAPFunctor( FunctorOfTerm( gt( )) );
}
YAPPairTerm::YAPPairTerm(YAPTerm th, YAPTerm tl) : YAPTerm() {
CACHE_REGS
mk ( MkPairTerm( th.term(), tl.term() ) );
CACHE_REGS
mk ( MkPairTerm( th.term(), tl.term() ) );
}
YAPPairTerm::YAPPairTerm() : YAPTerm() {
t = Yap_MkNewPairTerm( );
t = Yap_MkNewPairTerm( );
}
void YAPTerm::mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
Term YAPTerm::gt() { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
YAP_tag_t YAPTerm::tag() {
Term tt = gt( );
if (IsVarTerm(tt)) {
CELL *pt = VarOfTerm(tt);
if (IsUnboundVar(pt)) {
CACHE_REGS
if (IsAttVar(pt))
return YAP_TAG_ATT;
return YAP_TAG_UNBOUND;
}
return YAP_TAG_REF;
}
if (IsPairTerm(tt))
return YAP_TAG_PAIR;
if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt))
return YAP_TAG_ATOM;
return YAP_TAG_INT;
} else {
Functor f = FunctorOfTerm(tt);
if (IsVarTerm(tt)) {
CELL *pt = VarOfTerm(tt);
if (IsUnboundVar(pt)) {
CACHE_REGS
if (IsAttVar(pt))
return YAP_TAG_ATT;
return YAP_TAG_UNBOUND;
}
return YAP_TAG_REF;
}
if (IsPairTerm(tt))
return YAP_TAG_PAIR;
if (IsAtomOrIntTerm(tt)) {
if (IsAtomTerm(tt))
return YAP_TAG_ATOM;
return YAP_TAG_INT;
} else {
Functor f = FunctorOfTerm(tt);
if (IsExtensionFunctor(f)) {
if (f == FunctorDBRef) {
return YAP_TAG_DBREF;
}
if (f == FunctorLongInt) {
return YAP_TAG_LONG_INT;
}
if (f == FunctorBigInt) {
big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
switch (bt) {
case BIG_INT:
return YAP_TAG_BIG_INT;
case BIG_RATIONAL:
return YAP_TAG_RATIONAL;
default:
return YAP_TAG_OPAQUE;
}
}
}
return YAP_TAG_APPL;
}
if (IsExtensionFunctor(f)) {
if (f == FunctorDBRef) {
return YAP_TAG_DBREF;
}
if (f == FunctorLongInt) {
return YAP_TAG_LONG_INT;
}
if (f == FunctorBigInt) {
big_blob_type bt = (big_blob_type)RepAppl(tt)[1];
switch (bt) {
case BIG_INT:
return YAP_TAG_BIG_INT;
case BIG_RATIONAL:
return YAP_TAG_RATIONAL;
default:
return YAP_TAG_OPAQUE;
}
}
}
return YAP_TAG_APPL;
}
}
YAPTerm YAPTerm::deepCopy() {
Term tn;
BACKUP_MACHINE_REGS();
Term tn;
BACKUP_MACHINE_REGS();
tn = Yap_CopyTerm( gt() );
tn = Yap_CopyTerm( gt() );
RECOVER_MACHINE_REGS();
return new YAPTerm( tn );
RECOVER_MACHINE_REGS();
return new YAPTerm( tn );
}
bool YAPTerm::exactlyEqual(YAPTerm t1) {
int out;
BACKUP_MACHINE_REGS();
int out;
BACKUP_MACHINE_REGS();
out = Yap_eq(gt(), t1.term());
out = Yap_eq(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
RECOVER_MACHINE_REGS();
return out;
}
bool YAPTerm::unify(YAPTerm t1) {
int out;
BACKUP_MACHINE_REGS();
int out;
BACKUP_MACHINE_REGS();
out = Yap_unify(gt(), t1.term());
out = Yap_unify(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
RECOVER_MACHINE_REGS();
return out;
}
bool YAPTerm::unifiable(YAPTerm t1) {
int out;
BACKUP_MACHINE_REGS();
int out;
BACKUP_MACHINE_REGS();
out = Yap_Unifiable(gt(), t1.term());
out = Yap_Unifiable(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
RECOVER_MACHINE_REGS();
return out;
}
bool YAPTerm::variant(YAPTerm t1) {
int out;
BACKUP_MACHINE_REGS();
int out;
BACKUP_MACHINE_REGS();
out = Yap_Variant(gt(), t1.term());
out = Yap_Variant(gt(), t1.term());
RECOVER_MACHINE_REGS();
return out;
RECOVER_MACHINE_REGS();
return out;
}
intptr_t YAPTerm::hash(size_t sz, size_t depth, bool variant) {
intptr_t out;
BACKUP_MACHINE_REGS();
BACKUP_MACHINE_REGS();
out = Yap_TermHash(gt(), sz, depth, variant) ;
out = Yap_TermHash(gt(), sz, depth, variant) ;
RECOVER_MACHINE_REGS();
return out;
RECOVER_MACHINE_REGS();
return out;
}
char *YAPTerm::text(void) {
size_t sze = 4096, sz, length;
char *buf = new char[sze], *b;
size_t sze = 4096, length;
char *os;
int enc;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "I LCL0+%p/t=(%d) %x", LCL0, t, LCL0[-15]) ; }
BACKUP_MACHINE_REGS();
if ((b = Yap_TermToString(gt(), buf, sze, &length, &enc, 0)) != buf) {
if (b) free(b);
if (!(os = Yap_HandleToString(t, sze, &length, &enc, 0))) {
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "IIa LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
RECOVER_MACHINE_REGS();
return NULL;
}
sz = length+1;
char *os = new char[sz];
if (!os) {
RECOVER_MACHINE_REGS();
return NULL;
}
memcpy(os, buf, sz);
delete buf;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "II LCL0+t=(%p) %x", LCL0, LCL0[-15]) ; }
RECOVER_MACHINE_REGS();
return os;
}
bool YAPListTerm::nil() {
CACHE_REGS
__android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ;
return gt() == TermNil;
}
YAPIntegerTerm::YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
/*
YAPTerm *YAPTerm::vars()
{
@ -295,185 +304,327 @@ YAPTerm *YAPTerm::vars()
RECOVER_MACHINE_REGS();
return lv;
}
*/
*/
YAPTerm::YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
YAPTerm::YAPTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
YAPTerm YAPListTerm::car()
{
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "t=%d LCL0+t=(%p)", t, LCL0+t) ; }
Term to = gt();
if (IsPairTerm(to))
return YAPTerm(HeadOfTerm(to));
else
return MkIntTerm(-1);
}
YAPVarTerm::YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
char *YAPAtom::name(void) {
if (IsWideAtom(a)) {
// return an UTF-8 version
size_t sz = 512;
wchar_t * ptr = a->WStrOfAE;
int ch = -1;
char *s = new char[sz], *op = s;
while (ch) {
ch = *ptr++;
utf8_put_char( op, ch );
}
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
} else if (IsBlob(a)) {
PL_blob_t *type = RepBlobProp(a->PropsOfAE)->blob_t;
size_t sz = 512;
if (IsWideAtom(a)) {
// return an UTF-8 version
size_t sz = 512;
wchar_t * ptr = a->WStrOfAE;
int ch = -1;
char *s = new char[sz], *op = s;
while (ch) {
ch = *ptr++;
utf8_put_char( op, ch );
}
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
} else if (IsBlob(a)) {
PL_blob_t *type = RepBlobProp(a->PropsOfAE)->blob_t;
size_t sz = 512;
if (type->write) {
char *s = new char[sz];
IOSTREAM *stream = Sopenmem(&s, &sz, "w");
stream->encoding = ENC_UTF8;
atom_t at = YAP_SWIAtomFromAtom(AbsAtom(a));
type->write(stream, at, 0);
Sclose(stream);
popOutputContext();
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
} else {
char *s = new char[sz];
if (type->write) {
char *s = new char[sz];
IOSTREAM *stream = Sopenmem(&s, &sz, "w");
stream->encoding = ENC_UTF8;
atom_t at = YAP_SWIAtomFromAtom(AbsAtom(a));
type->write(stream, at, 0);
Sclose(stream);
popOutputContext();
sz = strlen(s)+1;
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
} else {
char *s = new char[sz];
#if defined(__linux__) || defined(__APPLE__)
snprintf(s, sz, "'%s'(%p)", AtomSWIStream->StrOfAE, a);
snprintf(s, sz, "'%s'(%p)", AtomSWIStream->StrOfAE, a);
#else
snprintf(s, sz, "'%s'(0x%p)", AtomSWIStream->StrOfAE, a);
snprintf(s, sz, "'%s'(0x%p)", AtomSWIStream->StrOfAE, a);
#endif
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
}
} else {
return a->StrOfAE;
}
char *os = new char[sz];
memcpy(os, s, sz);
delete s;
return os;
}
} else {
return a->StrOfAE;
}
}
YAPPredicate::YAPPredicate(const char *s, Term **outp, term_t &vnames) {
CACHE_REGS
vnames = Yap_NewSlots(1 PASS_REGS);
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames), m;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
*outp = NULL;
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepAppl(t)+1;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepPair(t);
}
Term t = Yap_StringToTerm(s, strlen(s)+1, vnames);
ap = getPred( t, outp );
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "OUT vnames=%d ap=%p LCL0=%p", vnames, ap, LCL0) ; }
}
YAPPredicate::YAPPredicate(YAPAtom at) {
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
YAPPredicate::YAPPredicate(YAPAtom at, arity_t arity) {
CACHE_REGS
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
} else {
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
}
/// auxiliary routine to find a predicate in the current module.
PredEntry *YAPPredicate::getPred( Term t, Term **outp ) {
CACHE_REGS
Term m = CurrentModule ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t)) {
ap = NULL;
if (outp) *outp = NULL;
}
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
if (outp) *outp = NULL;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
if (outp) *outp = HR;
HR[0] = RepPair(t)[0];
HR[1] = m;
HR+=2;
} else {
Functor f = FunctorOfTerm(t);
if (IsExtensionFunctor(f)) {
ap = NULL;
if (outp) *outp = NULL;
}
ap = RepPredProp(PredPropByFunc(f, m));
if (outp) *outp = RepAppl(t)+1;
}
return ap;
}
YAPPredicate::YAPPredicate(YAPFunctor f) {
CACHE_REGS
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
}
void
YAPQuery::initQuery( Term *ts )
{
CACHE_REGS
CACHE_REGS
this->oq = (YAPQuery *)LOCAL_execution;
LOCAL_execution = (struct open_query_struct *)this;
this->q_open=1;
this->q_state=0;
this->q_flags = 0;
this->q_g = ts;
this->oq = (YAPQuery *)LOCAL_execution;
LOCAL_execution = (struct open_query_struct *)this;
this->q_open=1;
this->q_state=0;
this->q_flags = 0;
this->q_g = ts;
}
void
YAPQuery::initQuery( YAPTerm t[], arity_t arity )
{
Term *ts = new Term[arity];
for (int i = 0; i < arity; i++)
ts[i] = t[i].term();
Term *ts = new Term[arity];
for (arity_t i = 0; i < arity; i++)
ts[i] = t[i].term();
return initQuery( ts );
return initQuery( ts );
}
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]): YAPPredicate(f, mod)
{
/* ignore flags for now */
initQuery( t , f.arity());
/* ignore flags for now */
initQuery( t , f.arity());
}
YAPQuery::YAPQuery(YAPFunctor f, YAPTerm t[]): YAPPredicate(f)
{
/* ignore flags for now */
initQuery( t , f.arity());
/* ignore flags for now */
initQuery( t , f.arity());
}
YAPQuery::YAPQuery(YAPPredicate p, YAPTerm t[]): YAPPredicate(p.ap)
{
initQuery( t , p.ap->ArityOfPE);
initQuery( t , p.ap->ArityOfPE);
}
int YAPQuery::next()
YAPListTerm YAPQuery::namedVars() {
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "vnames=%d LCL0=%p %x", vnames, LCL0, LCL0[vnames]) ; }
CACHE_REGS
Term o = Yap_GetFromSlot( vnames PASS_REGS );
return YAPListTerm( o );
}
bool YAPQuery::next()
{
CACHE_REGS
int result;
if (this->q_open != 1) return 0;
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
return 0;
// don't forget, on success these guys must create slots
if (this->q_state == 0) {
result = YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
} else {
LOCAL_AllowRestart = this->q_open;
result = YAP_RetryGoal(&this->q_h);
}
this->q_state = 1;
if (result == 0) {
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
}
return result;
CACHE_REGS
int result;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, " CALL vnames=%d ap=%p LCL0=%p %x", vnames, ap, LCL0, LCL0[vnames]) ; }
if (this->q_open != 1) return false;
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
return false;
// don't forget, on success these guys must create slots
if (this->q_state == 0) {
extern void toggle_low_level_trace(void);
//toggle_low_level_trace();
result = (bool)YAP_EnterGoal((YAP_PredEntryPtr)this->ap, this->q_g, &this->q_h);
} else {
LOCAL_AllowRestart = this->q_open;
result = (bool)YAP_RetryGoal(&this->q_h);
}
this->q_state = 1;
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "FAIL vnames=%d %d LCL0=(%p) %x", vnames, result, LCL0, LCL0[vnames]) ; }
if (!result) {
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
}
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "SUCCESS vnames=%d ap=%p LCL0=(%p) %x", vnames, ap, LCL0, LCL0[vnames]) ; }
return result;
}
void YAPQuery::cut()
{
CACHE_REGS
CACHE_REGS
if (this->q_open != 1 || this->q_state == 0) return;
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
if (this->q_open != 1 || this->q_state == 0) return;
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
}
void YAPQuery::close()
{
CACHE_REGS
CACHE_REGS
if (EX && !(this->q_flags & (PL_Q_CATCH_EXCEPTION))) {
EX = NULL;
}
/* need to implement backtracking here */
if (this->q_open != 1 || this->q_state == 0) {
return;
}
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
if (EX && !(this->q_flags & (PL_Q_CATCH_EXCEPTION))) {
EX = NULL;
}
/* need to implement backtracking here */
if (this->q_open != 1 || this->q_state == 0) {
return;
}
YAP_LeaveGoal(FALSE, &this->q_h);
this->q_open = 0;
LOCAL_execution = (struct open_query_struct *)this->oq;
}
int YAPPredicate::call(YAPTerm t[])
{
YAPQuery q = YAPQuery(*this, t);
int ret = q.next();
q.cut();
q.close();
return ret;
YAPQuery q = YAPQuery(*this, t);
int ret = q.next();
q.cut();
q.close();
return ret;
}
YAPEngine::YAPEngine(YAPParams const& params)
{ YAP_Init( (YAP_init_args *)&params.init_args ); }
static YAPEngine *curren;
#if __ANDROID__
YAPEngine::YAPEngine()
#include <jni.h>
#include <string.h>
extern AAssetManager *assetManager;
extern char *Yap_AndroidBufp;
static size_t Yap_AndroidMax, Yap_AndroidSz;
extern void(*Yap_DisplayWithJava)(int c);
static void
displayWithJava(int c)
{
YAPParams *params = new YAPParams();
YAP_Init( &params->init_args );
char *ptr = Yap_AndroidBufp;
ptr[ Yap_AndroidSz++ ] = c;
if (Yap_AndroidMax-1 == Yap_AndroidSz) {
if (Yap_AndroidMax < 32*1024) {
Yap_AndroidMax *= 2;
} else {
Yap_AndroidMax += 32*1024;
}
Yap_AndroidBufp = (char *)realloc( ptr, Yap_AndroidMax);
}
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
if (c == '\n' ) {
Yap_AndroidBufp[Yap_AndroidSz] = '\0';
__android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "after char %c:%s %p",c, Yap_AndroidBufp, curren);
curren->display(Yap_AndroidBufp);
Yap_AndroidSz = 0;
}
}
#endif
YAPEngine::YAPEngine( char *savedState,
size_t stackSize,
size_t trailSize,
size_t maxStackSize,
size_t maxTrailSize,
char *libDir,
char *bootFile,
char *goal,
char *topLevel,
bool script,
bool fastBoot,
YAPCallback *cb): _callback(0)
{ // a single engine can be active
#if __ANDROID__
if (assetManager == NULL)
return;
Yap_DisplayWithJava = displayWithJava;
Yap_AndroidBufp = (char *)malloc(Yap_AndroidMax = 4096);
Yap_AndroidBufp[0] = '\0';
Yap_AndroidSz = 0;
#endif
memset((void *)&init_args, 0, sizeof(init_args));
init_args.SavedState = savedState;
init_args.StackSize = stackSize;
init_args.TrailSize = trailSize;
init_args.MaxStackSize = maxStackSize;
init_args.MaxTrailSize = maxTrailSize;
init_args.YapLibDir = libDir;
init_args.YapPrologBootFile = bootFile;
init_args.YapPrologGoal = goal;
init_args.YapPrologTopLevelGoal = topLevel;
init_args.HaltAfterConsult = script;
init_args.FastBoot = fastBoot;
delYAPCallback();
if (cb) setYAPCallback(cb);
curren = this;
YAP_Init( &init_args );
}
YAPQuery *YAPEngine::query( char *s ) {
YAPQuery *n = new YAPQuery( s );
return n;
}

View File

@ -13,16 +13,26 @@
* with the swig interface language generator. It uses ideas from the old YAP
* interface and from the SWI foreign language interface.
*
* @{
*
*/
#include <stdlib.h>
#include <config.h>
extern "C" {
#ifdef __cplusplus
#define old_cplusplus __cplusplus
#undef __cplusplus
#endif
#if USE_GMP
#include <gmp.h>
#endif
extern "C" {
#ifdef old_cplusplus
#define __cplusplus old_cplusplus
#undef old_cplusplus
#endif
#include "Yap.h"
@ -60,348 +70,374 @@ extern "C" {
#include <windows.h>
#endif
// taken from yap_structs.h
// taken from yap_structs.h
#include "iopreds.h"
extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
extern Term Yap_StringToTerm(const char *s, size_t len, term_t bindings);
// we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't
// we cannot consult YapInterface.h, that conflicts with what we declare, though
// it shouldn't
}
//#include <vector>
class YAPEngine;
class YAPTermHandle;
class YAPAtom;
class YAPFunctor;
class YAPApplTerm;
class YAPPairTerm;
class YAPQuery;
class TypeError {};
/**
* @brief Generic Prolog Term
*/
class YAPTerm {
friend class YAPPredicate;
friend class YAPTermHandle;
friend class YAPApplTerm;
friend class YAPPairTerm;
friend class YAPPredicate;
friend class YAPApplTerm;
friend class YAPPairTerm;
friend class YAPListTerm;
protected:
term_t t;
inline void mk(Term t0) { CACHE_REGS t = Yap_InitSlot( t0 PASS_REGS); }
inline Term gt(void) { CACHE_REGS return Yap_GetFromSlot( t PASS_REGS); }
handle_t t;
void mk(Term t0);
Term gt();
YAPTerm(Term tn) { mk( tn ); }
public:
YAPTerm() { mk(TermNil); } // do nothing constructor
YAPTerm(void *ptr) { CACHE_REGS mk( MkIntegerTerm( (Int)ptr ) );}
YAPTerm(Term tn) { mk( tn ); }
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
/*~YAPTerm(void) {
YAPTerm() { mk(TermNil); } // do nothing constructor
YAPTerm(intptr_t i);
YAPTerm(void *ptr);
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
/*~YAPTerm(void) {
CACHE_REGS
Yap_RecoverSlots(1, t PASS_REGS);
}*/
Term term() { return gt(); }
YAP_tag_t tag();
YAPTerm deepCopy();
//const YAPTerm *vars();
bool exactlyEqual(YAPTerm t1);
bool unify(YAPTerm t1);
bool unifiable(YAPTerm t1);
bool variant(YAPTerm t1);
intptr_t hash(size_t sz, size_t depth, bool variant);
bool isVar() { return IsVarTerm( gt() ); }
bool isAtom() { return IsAtomTerm( gt() ); }
bool isInteger() { return IsIntegerTerm( gt() ); }
bool isFloat() { return IsFloatTerm( gt() ); }
bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); }
bool isAppl() { return IsApplTerm( gt() ); }
bool isPair() { return IsPairTerm( gt() ); }
bool isGround() { return Yap_IsGroundTerm( gt() ); }
bool isList() { return Yap_IsListTerm( gt() ); }
bool isString() { return IsStringTerm( gt() ); }
Term term() { return gt(); }
YAP_tag_t tag();
YAPTerm deepCopy();
//const YAPTerm *vars();
bool exactlyEqual(YAPTerm t1);
bool unify(YAPTerm t1);
bool unifiable(YAPTerm t1);
bool variant(YAPTerm t1);
intptr_t hash(size_t sz, size_t depth, bool variant);
bool isVar() { return IsVarTerm( gt() ); }
bool isAtom() { return IsAtomTerm( gt() ); }
bool isInteger() { return IsIntegerTerm( gt() ); }
bool isFloat() { return IsFloatTerm( gt() ); }
bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); }
bool isAppl() { return IsApplTerm( gt() ); }
bool isPair() { return IsPairTerm( gt() ); }
bool isGround() { return Yap_IsGroundTerm( gt() ); }
bool isList() { return Yap_IsListTerm( gt() ); }
bool isString() { return IsStringTerm( gt() ); }
inline YAPTerm getArg(int i) {
Term t0 = gt();
if (IsApplTerm(t0))
return YAPTerm(ArgOfTerm(i, t0));
else if (IsPairTerm(t0)) {
if (i==1)
return YAPTerm(HeadOfTerm(t0));
if (i==2)
return YAPTerm(TailOfTerm(t0));
}
return YAPTerm((Term)0);
}
char *text();
inline YAPTerm getArg(int i) {
Term t0 = gt();
if (IsApplTerm(t0))
return YAPTerm(ArgOfTerm(i, t0));
else if (IsPairTerm(t0)) {
if (i==1)
return YAPTerm(HeadOfTerm(t0));
if (i==2)
return YAPTerm(TailOfTerm(t0));
}
return YAPTerm((Term)0);
}
char *text();
};
/**
* @brief Variable Term
*/
class YAPVarTerm: private YAPTerm {
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
class YAPVarTerm: public YAPTerm {
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
public:
YAPVarTerm() { CACHE_REGS mk( MkVarTerm( ) ); }
CELL *getVar() { return VarOfTerm( gt() ); }
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
YAPVarTerm();
CELL *getVar() { return VarOfTerm( gt() ); }
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
};
/**
* @brief Compound Term
*/
class YAPApplTerm: private YAPTerm {
class YAPApplTerm: public YAPTerm {
friend class YAPTerm;
YAPApplTerm(Term t0) { mk(t0); }
public:
YAPApplTerm(YAPTerm t0) { mk(t0.term()); }
YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
YAPApplTerm(YAPFunctor f);
YAPFunctor getFunctor();
YAPTerm getArg(int i);
YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
YAPApplTerm(YAPFunctor f);
YAPFunctor getFunctor();
YAPTerm getArg(int i);
};
/**
* @brief List Constructor Term
*/
class YAPPairTerm: private YAPTerm {
class YAPPairTerm: public YAPTerm {
friend class YAPTerm;
YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); }
YAPPairTerm(Term t0) { if (IsPairTerm(t0)) mk( t0 ); else mk(0); }
public:
YAPPairTerm(YAPTerm hd, YAPTerm tl);
YAPPairTerm();
YAPTerm getHead() { return YAPTerm(HeadOfTerm( gt() )); }
YAPTerm getTail() { return YAPTerm(TailOfTerm( gt() )); }
YAPPairTerm(YAPTerm hd, YAPTerm tl);
YAPPairTerm();
YAPTerm getHead() { return YAPTerm(HeadOfTerm( gt() )); }
YAPTerm getTail() { return YAPTerm(TailOfTerm( gt() )); }
};
/**
* @brief Integer Term
*/
class YAPIntegerTerm: private YAPTerm {
class YAPIntegerTerm: public YAPTerm {
public:
YAPIntegerTerm(intptr_t i) { CACHE_REGS Term tn = MkIntegerTerm( i ); mk( tn ); }
intptr_t getInteger() { return IntegerOfTerm( gt() ); }
bool isTagged() { return IsIntTerm( gt() ); }
YAPIntegerTerm(intptr_t i);
intptr_t getInteger() { return IntegerOfTerm( gt() ); }
bool isTagged() { return IsIntTerm( gt() ); }
};
class YAPListTerm: private YAPTerm {
class YAPListTerm: public YAPTerm {
public:
/// Create a list term out of a standard term. Check if a valid operation.
///
/// @param[in] the term
YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
/* /// Create a list term out of an array of terms.
///
/// @param[in] the term
YAPListTerm(Term t0) { mk(t0); /* else type_error */ }
/* /// Create a list term out of an array of terms.
///
/// @param[in] the array of terms
/// @param[in] the length of the array
YAPListTerm(YAPTerm ts[], size_t n);
/// Create a list term out of an array of Prolog terms.
///
/// @param[in] the array of terms
/// @param[in] the length of the array
YAPListTerm(Term ts[], size_t n);
*/
// YAPListTerm( vector<YAPTerm> v );
*/
// YAPListTerm( vector<YAPTerm> v );
/// Return the number of elements in a list term.
size_t length() { Term *tailp; Term t1 = gt(); return Yap_SkipList(&t1, &tailp); }
// vector<YAPTerm> toVector();
/// Create an array of term out of a list.
///
/// @param[in] the list
YAPTerm car() { if (IsPairTerm(gt())) return YAPTerm(HeadOfTerm(gt())); else return YAPTerm((term_t)0); }
YAPListTerm cdr() { if (IsPairTerm(gt())) return YAPListTerm(TailOfTerm(gt())); else return YAPListTerm((term_t)0); }
bool nil() { return gt() == TermNil; }
/// Extract the first element of a list.
///
/// @param[in] the list
YAPTerm car();
/// Extract the tail elements of a list.
///
/// @param[in] the list
YAPListTerm cdr()
{
Term to = gt();
if (IsPairTerm( to ))
return YAPListTerm(TailOfTerm( to ));
else
return MkIntTerm(-1);
}
/// Check if the list is empty.
///
/// @param[in] the list
bool nil();
};
/**
* @brief Atom
*/
class YAPAtom {
friend class YAPPredicate;
friend class YAPFunctor;
friend class YAPAtomTerm;
Atom a;
friend class YAPPredicate;
friend class YAPFunctor;
friend class YAPAtomTerm;
Atom a;
public:
YAPAtom( Atom at ) { a = at; }
YAPAtom( char * s) { a = Yap_LookupAtom( s ); }
YAPAtom( wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
YAPAtom( char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
YAPAtom( wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
char *name(void);
YAPAtom( Atom at ) { a = at; }
YAPAtom( char * s) { a = Yap_LookupAtom( s ); }
YAPAtom( wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
YAPAtom( char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
YAPAtom( wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
char *name(void);
};
/**
* @brief String Term
*/
class YAPStringTerm: private YAPTerm {
class YAPStringTerm: public YAPTerm {
public:
YAPStringTerm(char *s) ;
YAPStringTerm(char *s, size_t len);
YAPStringTerm(wchar_t *s) ;
YAPStringTerm(wchar_t *s, size_t len);
const char *getString() { return StringOfTerm( gt() ); }
YAPStringTerm(char *s) ;
YAPStringTerm(char *s, size_t len);
YAPStringTerm(wchar_t *s) ;
YAPStringTerm(wchar_t *s, size_t len);
const char *getString() { return StringOfTerm( gt() ); }
};
/**
* @brief Atom Term
*/
class YAPAtomTerm: private YAPTerm {
class YAPAtomTerm: public YAPTerm {
public:
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); }
YAPAtomTerm(char *s) ;
YAPAtomTerm(char *s, size_t len);
YAPAtomTerm(wchar_t *s) ;
YAPAtomTerm(wchar_t *s, size_t len);
YAPAtom getAtom() { return YAPAtom(AtomOfTerm( gt() )); }
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
YAPAtomTerm(Atom a): YAPTerm() { mk( MkAtomTerm(a) ); }
YAPAtomTerm(char *s) ;
YAPAtomTerm(char *s, size_t len);
YAPAtomTerm(wchar_t *s) ;
YAPAtomTerm(wchar_t *s, size_t len);
YAPAtom getAtom() { return YAPAtom(AtomOfTerm( gt() )); }
};
/**
* @brief Functor
* @brief YAPFunctor represents Prolog functors Name/Arity
*/
class YAPFunctor {
friend class YAPApplTerm;
friend class YAPPredicate;
Functor f;
friend class YAPApplTerm;
friend class YAPPredicate;
Functor f;
/// Constructor: receives Prolog functor and casts it to YAPFunctor
///
/// Notice that this is designed for internal use only.
YAPFunctor( Functor ff) { f = ff; }
public:
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); }
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
YAPFunctor( Functor ff) { f = ff; }
/// Constructor: receives name as a string plus arity
///
/// Notice that this is designed for ISO-LATIN-1 right now
YAPFunctor( char * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupAtom( s ), arity ); }
/// Constructor: receives name as a wide string plus arity
///
/// Notice that this is designed for UNICODE right now
YAPFunctor( wchar_t * s, arity_t arity) { f = Yap_MkFunctor( Yap_LookupWideAtom( s ), arity ); }
/// Constructor: receives name as an atom, plus arity
///
/// This is the default method, and the most popi;at
YAPFunctor( YAPAtom at, arity_t arity) { f = Yap_MkFunctor( at.a, arity ); }
Atom name(void) {
return NameOfFunctor( f );
}
/// Getter: extract name of functor as an atom
///
/// this is for external usage.
YAPAtom name(void) {
return YAPAtom( NameOfFunctor( f ) );
}
arity_t arity(void) {
return ArityOfFunctor( f );
}
/// Getter: extract arity of functor as an unsigned integer
///
/// this is for external usage.
arity_t arity(void) {
return ArityOfFunctor( f );
}
};
/**
* @brief Term Handle
*/
class YAPTermHandle {
long int handle;
public:
YAPTermHandle(Term t) {
CACHE_REGS
handle = Yap_InitSlot(t PASS_REGS);
}
void set(YAPTerm t) {
CACHE_REGS
Yap_PutInSlot(handle, t.t PASS_REGS);
}
};
/**
* @brief Predicate
* @brief Predicates
*
* This class interfaces with PredEntry in Yatom.g
*/
class YAPPredicate {
friend class YAPQuery;
PredEntry *ap;
// trick to communicate t[] back to yapquery
YAPPredicate(const char *s, Term **outp, term_t& vnames );
inline YAPPredicate(Term t) {
CACHE_REGS
Term m = CurrentModule ;
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
} else {
ap = NULL;
}
}
inline YAPPredicate(PredEntry *pe) {
ap = pe;
}
friend class YAPQuery;
private:
PredEntry *ap;
/// auxiliary routine to find a predicate in the current module.
PredEntry *getPred( Term t, Term **outp ) ;
/// String constructor for predicates
///
/// It also communicates the array of arguments t[] abd the array of variables
/// back to yapquery
YAPPredicate(const char *s, Term **outp, handle_t& vnames );
/// Term constructor for predicates
///
/// It is just a call to getPred
inline YAPPredicate(Term t) {
ap = getPred( t , NULL );
}
/// Cast constructor for predicates,
/// if we have the implementation data.
///
inline YAPPredicate(PredEntry *pe) {
ap = pe;
}
public:
inline YAPPredicate(YAPFunctor f) {
CACHE_REGS
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
}
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
ap = RepPredProp(PredPropByFunc(f.f,mod.t));
}
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
}
inline YAPPredicate(YAPAtom at) {
CACHE_REGS
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) {
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,mod.t));
} else {
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
}
}
inline YAPPredicate(YAPAtom at, arity_t arity) {
CACHE_REGS
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,CurrentModule));
} else {
ap = RepPredProp(PredPropByAtom(at.a,CurrentModule));
}
}
inline YAPPredicate(char *s) {
CACHE_REGS
Term t, tp, m = CurrentModule ;
t = YAP_ReadBuffer(s,&tp);
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
} else {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
}
}
inline YAPPredicate(char *s, Term **outp) {
CACHE_REGS
Term t, tp, m = CurrentModule ;
t = YAP_ReadBuffer(s,&tp);
t = Yap_StripModule(t, &m);
if (IsVarTerm(t) || IsNumTerm(t))
ap = NULL;
if (IsAtomTerm(t)) {
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
*outp = NULL;
} else if (IsApplTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepAppl(t)+1;
} else if (IsPairTerm(t)) {
ap = RepPredProp(PredPropByFunc(FunctorOfTerm(t), m));
*outp = RepPair(t);
}
}
int call(YAPTerm ts[]);
arity_t arity() { return ap->ArityOfPE; }
/// module of predicate
///
/// notice that modules are currently treated as atoms, this should change.
YAPAtom module() { if (ap->ModuleOfPred == PROLOG_MODULE)
return YAPAtom(AtomProlog);
else
return YAPAtom(AtomOfTerm(ap->ModuleOfPred)); }
/// name of predicate
///
/// notice that we return the atom, not a string.
YAPAtom name() { if (ap->ArityOfPE)
return YAPAtom((Atom)ap->FunctorOfPred);
else
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
}
/// Functor constructor for predicates
///
/// Asssumes that we use the current module.
YAPPredicate(YAPFunctor f);
/// Functor constructor for predicates, is given a specific module.
///
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
ap = RepPredProp(PredPropByFunc(f.f,mod.t));
}
/// Name/arity constructor for predicates.
///
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
}
/// Name/0 constructor for predicates.
///
YAPPredicate(YAPAtom at);
/// Mod:Name/Arity constructor for predicates.
///
inline YAPPredicate(YAPAtom at, arity_t arity, YAPTerm mod) {
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
ap = RepPredProp(PredPropByFunc(f,mod.t));
} else {
ap = RepPredProp(PredPropByAtom(at.a,mod.t));
}
}
/// Atom/Arity constructor for predicates.
///
YAPPredicate(YAPAtom at, arity_t arity);
/// String constructor for predicates.
///
/// String is a Prolog term, we extract the main functor after considering the module qualifiers.
inline YAPPredicate(char *s) {
Term t, tp;
t = YAP_ReadBuffer(s,&tp);
ap = getPred( t, NULL );
}
/// String constructor for predicates, also keeps arguments in tp[]
///
/// String is a Prolog term, we extract the main functor after considering the module qualifiers.
inline YAPPredicate(char *s, Term **outp) {
Term t, tp;
t = YAP_ReadBuffer(s,&tp);
ap = getPred( t, NULL );
}
/// meta-call this predicate, with arguments ts[]
///
int call(YAPTerm ts[]);
/// module of a predicate
///
/// notice that modules are currently treated as atoms, this should change.
YAPAtom module() {
if (ap->ModuleOfPred == PROLOG_MODULE)
return YAPAtom(AtomProlog);
else
return YAPAtom(AtomOfTerm(ap->ModuleOfPred));
}
/// name of predicate
///
/// notice that we return the atom, not a string.
YAPAtom name() { if (ap->ArityOfPE)
return YAPAtom((Atom)ap->FunctorOfPred);
else
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
}
/// arity of predicate
///
/// we return a positive number.
arity_t getArity() { return ap->ArityOfPE; }
};
/**
@ -410,129 +446,100 @@ public:
* interface to a YAP Query;
* uses an SWI-like status info internally.
*/
class YAPQuery: private YAPPredicate {
int q_open;
int q_state;
Term *q_g;
yamop *q_p, *q_cp;
jmp_buf q_env;
int q_flags;
YAP_dogoalinfo q_h;
YAPQuery *oq;
term_t vnames;
void initQuery( Term ts[] );
void initQuery( YAPTerm t[], arity_t arity );
class YAPQuery: public YAPPredicate {
int q_open;
int q_state;
Term *q_g;
yamop *q_p, *q_cp;
jmp_buf q_env;
int q_flags;
YAP_dogoalinfo q_h;
YAPQuery *oq;
handle_t vnames;
void initQuery( Term ts[] );
void initQuery( YAPTerm t[], arity_t arity );
public:
/// main constructor, uses a predicate and an array of terms
///
/// It is given a YAPPredicate _p_ , and an array of terms that must have at least
/// the same arity as the functor.
YAPQuery(YAPPredicate p, YAPTerm t[]);
/// full constructor,
///
/// It is given a functor, module, and an array of terms that must have at least
/// the same arity as the functor.
YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]);
/// functor/term constructor,
///
/// It is given a functor, and an array of terms that must have at least
/// the same arity as the functor. Works within the current module.
YAPQuery(YAPFunctor f, YAPTerm t[]);
/// string constructor with varnames
///
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable
/// goal and a list of variables. Useful for top-level simulation. Works within the current module.
inline YAPQuery(char *s): YAPPredicate(s, &this->q_g, vnames)
{
Term *ts = this->q_g;
/// main constructor, uses a predicate and an array of terms
///
/// It is given a YAPPredicate _p_ , and an array of terms that must have at least
/// the same arity as the functor.
YAPQuery(YAPPredicate p, YAPTerm t[]);
/// full constructor,
///
/// It is given a functor, module, and an array of terms that must have at least
/// the same arity as the functor.
YAPQuery(YAPFunctor f, YAPTerm mod, YAPTerm t[]);
/// functor/term constructor,
///
/// It is given a functor, and an array of terms that must have at least
/// the same arity as the functor. Works within the current module.
YAPQuery(YAPFunctor f, YAPTerm t[]);
/// string constructor with varnames
///
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable
/// goal and a list of variables. Useful for top-level simulation. Works within the current module.
inline YAPQuery(char *s): YAPPredicate(s, &this->q_g, vnames)
{
Term *ts = this->q_g;
initQuery( ts );
}
/// first query
///
/// actually implemented by calling the next();
inline int first() { return next(); }
/// ask for the next solution of the current query
/// same call for every solution
int next();
/// remove alternatives in the current search space, and finish the current query
void cut();
/// finish the current query: undo all bindings.
void close();
/// query variables.
YAPListTerm namedVars() { CACHE_REGS Term o = Yap_GetFromSlot( vnames PASS_REGS ); return YAPListTerm( o ); }
};
initQuery( ts );
}
/// first query
///
/// actually implemented by calling the next();
inline bool first() { return next(); }
/// ask for the next solution of the current query
/// same call for every solution
bool next();
/// remove alternatives in the current search space, and finish the current query
void cut();
/// finish the current query: undo all bindings.
void close();
/// query variables.
YAPListTerm namedVars();
};
// Java support
class YAPParams;
// This class implements a callback Prolog-side
class YAPCallback {
public:
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
virtual void displayInWindow(char *s) { }
};
/**
* @brief YAP Constructor
* @brief YAP Engine: takes care of constructing an execution environment where we can go executing goals
*
*/
class YAPEngine {
private:
YAPCallback *_callback;
char **buf;
YAP_init_args init_args;
public:
YAPEngine(YAPParams const& params); /// construct a new engine
YAPEngine(); /// construct a new engine, no arguments
YAPQuery *query( char *s ) { return new YAPQuery( s ); } /// build a query on the engine
YAPEngine(char *savedState = NULL,
size_t stackSize = 0,
size_t trailSize = 0,
size_t maxStackSize = 0,
size_t maxTrailSize = 0,
char *libDir = NULL,
char *bootFile = NULL,
char *goal = NULL,
char *topLevel = NULL,
bool script = FALSE,
bool fastBoot = FALSE,
YAPCallback *callback=NULL); /// construct a new engine, including aaccess to callbacks
~YAPEngine() { delYAPCallback(); } /// kill engine
void delYAPCallback() { _callback = 0; } /// remove current callback
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; __android_log_print(ANDROID_LOG_INFO, __FILE__, "after loading startup %p",cb); } /// set a new callback
void call() { if (_callback) _callback->run(); } /// execute the callback.
void display( char *s) { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "bef calling disp %s %p",s, _callback); if (_callback) _callback->displayInWindow(s); } /// execute the callback.
YAPQuery *query( char *s ); /// build a query on the engine
};
/**
* @brief Parameters for YAP Constructor
/*
* @}
*
*/
class YAPParams {
friend class YAPEngine;
YAP_init_args init_args;
public:
YAPParams();
// sets all the default values for each data member
YAPParams& savedState( char * f);
YAPParams& stackSize(size_t sz);
YAPParams& trailSize(size_t sz);
YAPParams& maxStackSize(size_t sz);
YAPParams& maxTrailSize(size_t sz);
YAPParams& libDir(char *p);
YAPParams& bootFile(char *f);
YAPParams& goal(char *g);
YAPParams& topLevel(char *g);
YAPParams& script(bool v);
YAPParams& fastBoot(bool v);
};
inline YAPParams::YAPParams()
{ Yap_InitDefaults( &init_args, NULL ); }
inline YAPParams& YAPParams::savedState( char * f)
{ init_args.SavedState = f; return *this; }
inline YAPParams& YAPParams::stackSize(size_t sz)
{ init_args.StackSize = sz; return *this; }
inline YAPParams& YAPParams::trailSize(size_t sz)
{ init_args.TrailSize = sz; return *this; }
inline YAPParams& YAPParams::maxStackSize(size_t sz)
{ init_args.MaxStackSize = sz; return *this; }
inline YAPParams& YAPParams::maxTrailSize(size_t sz)
{ init_args.MaxTrailSize = sz; return *this; }
inline YAPParams& YAPParams::libDir(char *p)
{ init_args.YapLibDir = p; return *this; }
inline YAPParams& YAPParams::bootFile(char *f)
{ init_args.YapPrologBootFile = f; return *this; }
inline YAPParams& YAPParams::goal(char *g)
{ init_args.YapPrologGoal = g; return *this; }
inline YAPParams& YAPParams::topLevel(char *g)
{ init_args.YapPrologTopLevelGoal = g; return *this; }
inline YAPParams& YAPParams::script(bool v)
{ init_args.HaltAfterConsult = v; return *this; }
inline YAPParams& YAPParams::fastBoot(bool v)
{ init_args.FastBoot = v; return *this; }