improv C++ interface.
This commit is contained in:
parent
bdcd06cf04
commit
a8a63a31ca
96
CXX/yapa.hh
Normal file
96
CXX/yapa.hh
Normal file
@ -0,0 +1,96 @@
|
||||
#ifndef YAPA_HH
|
||||
#define YAPA_HH 1
|
||||
|
||||
/**
|
||||
Prolog operates over constants, called atoms
|
||||
YAP, like lisp, associates properties with atoms.
|
||||
*/
|
||||
enum PropTag {
|
||||
/// predicate
|
||||
PRED_TAG = PEProp, // 0x0000,
|
||||
/// db key, may be associated with a functor
|
||||
DB_TAG = DBProperty, // 0x8000,
|
||||
/// generic functor, may include sub-properties
|
||||
FUNCTOR_TAG = FunctorProperty, // 0xBB00,
|
||||
// SPARSE_FUNCTOR_TAG = 0xFFDF,
|
||||
/// arithmetic function
|
||||
ARITHMETIC_PROPERTY_TAG = ExpProperty, // 0xFFE3,
|
||||
/// map the atom to an integer
|
||||
TRANSLATION_TAG = TranslationProperty, // 0xFFF4,
|
||||
/// SWI-STYLE ATOM Extension
|
||||
BLOB_TAG = BlobProperty, // 0xFFF5,
|
||||
/// named mutEX
|
||||
MUTEX_TAG = MutexProperty, // 0xFFF6,
|
||||
/// A typed array, may be in-db or in-stack deped
|
||||
ARRAY_TAG = ArrayProperty, // 0xFFF7,
|
||||
/// atom does not fit ISO-LATIN-1
|
||||
WIDE_TAG = WideAtomProperty, // 0xFFF8,
|
||||
/// module
|
||||
MODULE_TAG = ModProperty, // 0xFFFA,
|
||||
/// the original SICStus blackboard
|
||||
BLACKBOARD_TAG = BBProperty, // 0xFFFB,
|
||||
/// asociate an atomic value with the atom
|
||||
VALUE_TAG = ValProperty, // 0xFFFC,
|
||||
/// Demoen's proposal for gkobal variables
|
||||
GLOBAL_VAR_TAG = GlobalProperty, // 0xFFFD
|
||||
/// ensure the atom may not be garbafe colected
|
||||
HOLD_TAG = HoldProperty, // 0xFFF
|
||||
/// Prolog operator,
|
||||
OPERATOR_TAG = OpProperty, // 0xFFFF,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Atom
|
||||
* A YAP data-base is a collection of atoms, where each atom has a name
|
||||
* and a set of Properties. Examples of properties include functors,
|
||||
* predicates, operators, modules, almost everything.
|
||||
*
|
||||
*/
|
||||
class YAPAtom {
|
||||
friend class YAPModuleProp;
|
||||
friend class YAPPredicate;
|
||||
friend class YAPFunctor;
|
||||
friend class YAPAtomTerm;
|
||||
friend class YAProp;
|
||||
friend class YAPModule;
|
||||
Atom a;
|
||||
/// construct new YAPAtom from Atom
|
||||
YAPAtom( Atom at ) { a = at; }
|
||||
public:
|
||||
/// construct new YAPAtom from string
|
||||
YAPAtom( const char * s) { a = Yap_LookupAtom( s ); }
|
||||
/// construct new YAPAtom from wide string
|
||||
YAPAtom( const wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
|
||||
/// construct new YAPAtom from max-length string
|
||||
YAPAtom( const char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
|
||||
/// construct new YAPAtom from max-length wide string
|
||||
YAPAtom( const wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
|
||||
/// get name of atom
|
||||
char *getName(void);
|
||||
/// get prop of type
|
||||
Prop getProp( PropTag tag ) { return Yap_GetAProp( a , (PropFlags)tag ); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Prop
|
||||
* A YAP Propery is ameta-class for everything one can in a atom.
|
||||
* Examples of properties include functors,
|
||||
* predicates, operators, modules, almost everything.
|
||||
*
|
||||
*/
|
||||
class YAPProp {
|
||||
friend class YAPModuleProp;
|
||||
friend class YAPFunctor;
|
||||
/// does nothing, p is defined by the subclass
|
||||
YAPProp() { }
|
||||
/// get type of prop
|
||||
PropTag tag( Prop p ) { return (PropTag)(p->KindOfPE); }
|
||||
public:
|
||||
/// get name of property
|
||||
virtual YAPAtom name() = 0;
|
||||
virtual ~YAPProp() {};
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif /* YAPA_HH */
|
318
CXX/yapdb.hh
Normal file
318
CXX/yapdb.hh
Normal file
@ -0,0 +1,318 @@
|
||||
|
||||
#define YAP_CPP_DB_INTERFACE 1
|
||||
|
||||
//! @{
|
||||
|
||||
/**
|
||||
*
|
||||
* @defgroup yap-cplus-db-interface Data-Base Component of YAP interface.
|
||||
*
|
||||
* @ingroup yap-cplus-interface
|
||||
* @tableofcontents
|
||||
*
|
||||
*
|
||||
* Data-base component of C++ interface to YAP. It manipulates sets of
|
||||
* atoms, each one containing a number of props.
|
||||
*/
|
||||
|
||||
class YAPTerm;
|
||||
|
||||
class YAPError;
|
||||
|
||||
class YAPModule;
|
||||
|
||||
extern "C" {
|
||||
static inline Term
|
||||
Yap_CurrentModule( void )
|
||||
{
|
||||
CACHE_REGS
|
||||
return CurrentModule;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief YAPModule
|
||||
* A YAPModule describes a bare module, which in YAP is just a name.
|
||||
*
|
||||
* Info about the module is in YAPModuleProp
|
||||
*
|
||||
*/
|
||||
class YAPModule : protected YAPAtomTerm {
|
||||
friend class YAPPredicate;
|
||||
YAPModule( Term t ): YAPAtomTerm( t ) {};
|
||||
Term t() { return gt(); }
|
||||
public:
|
||||
~YAPModule( ) {};
|
||||
YAPModule( ): YAPAtomTerm( Yap_CurrentModule() ) {};
|
||||
YAPModule( YAPAtom t ): YAPAtomTerm( t ) {};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAPModuleProp
|
||||
* A YAPModuleProp controls access to a module property.
|
||||
*
|
||||
*/
|
||||
class YAPModuleProp: public YAPProp {
|
||||
friend class YAPPredicate;
|
||||
ModEntry *m;
|
||||
|
||||
YAPModuleProp(ModEntry *mod) {m = mod;};
|
||||
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
|
||||
public:
|
||||
YAPModuleProp() { m = Yap_GetModuleEntry(Yap_CurrentModule()); };
|
||||
YAPModuleProp(YAPModule tmod) ;
|
||||
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAPFunctor represents Prolog functors Name/Arity
|
||||
*/
|
||||
class YAPFunctor: public YAPProp {
|
||||
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( ) { };
|
||||
/// 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 ); }
|
||||
|
||||
/// Getter: extract name of functor as an atom
|
||||
///
|
||||
/// this is for external usage.
|
||||
YAPAtom name(void) {
|
||||
return YAPAtom( NameOfFunctor( f ) );
|
||||
}
|
||||
|
||||
/// Getter: extract arity of functor as an unsigned integer
|
||||
///
|
||||
/// this is for external usage.
|
||||
arity_t arity(void) {
|
||||
return ArityOfFunctor( f );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Predicates
|
||||
*
|
||||
* This class interfaces with PredEntry in Yatom.
|
||||
*/
|
||||
class YAPPredicate: public YAPModuleProp {
|
||||
friend class YAPQuery;
|
||||
|
||||
protected:
|
||||
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[]
|
||||
/// and the array of variables
|
||||
/// back to yapquery
|
||||
YAPPredicate(const char *s, Term* &out, Term& vnames ) throw (int);
|
||||
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(Term t) {
|
||||
CELL * v = NULL;
|
||||
ap = getPred( t , v );
|
||||
}
|
||||
|
||||
/// Cast constructor for predicates,
|
||||
/// if we have the implementation data.
|
||||
///
|
||||
inline YAPPredicate(PredEntry *pe) {
|
||||
ap = pe;
|
||||
}
|
||||
|
||||
public:
|
||||
~YAPPredicate() {};
|
||||
|
||||
/// Functor constructor for predicates
|
||||
///
|
||||
/// Asssumes that we use the current module.
|
||||
YAPPredicate(YAPFunctor f) {
|
||||
ap = RepPredProp(PredPropByFunc(f.f,Yap_CurrentModule()));
|
||||
};
|
||||
|
||||
/// 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, YAPModule 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(const char *s) throw (int) {
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
CELL * v;
|
||||
ap = getPred( t, v);
|
||||
}
|
||||
|
||||
|
||||
/// 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(const char *s, Term* outp) throw (int) {
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
ap = getPred( t, outp );
|
||||
}
|
||||
|
||||
/// module of a predicate
|
||||
///
|
||||
/// notice that modules are currently treated as atoms, this should change.
|
||||
YAPModule module() {
|
||||
if (ap->ModuleOfPred == PROLOG_MODULE)
|
||||
return YAPModule(AtomProlog);
|
||||
else
|
||||
return YAPModule(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; }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief PrologPredicate
|
||||
*
|
||||
* This class interfaces with Predicates Implemented in Prolog.
|
||||
*/
|
||||
class YAPPrologPredicate: public YAPPredicate {
|
||||
public:
|
||||
YAPPrologPredicate(YAPAtom name,
|
||||
arity_t arity,
|
||||
YAPModule module = YAPModule(),
|
||||
bool tabled = false,
|
||||
bool logical_updates = false,
|
||||
bool local = false,
|
||||
bool sourced = true,
|
||||
bool discontiguous = false,
|
||||
bool multiFile = false,
|
||||
bool hidden = false,
|
||||
bool untraceable = false,
|
||||
bool unspyable = false,
|
||||
bool meta = false,
|
||||
bool sync = false,
|
||||
bool quasi_quotable = false,
|
||||
size_t mega_clause = 0
|
||||
);
|
||||
void *assertClause( YAPTerm clause, bool last=true, YAPTerm source= YAPTerm(TermNil));
|
||||
void *retractClause( YAPTerm skeleton, bool all=false);
|
||||
void *clause( YAPTerm skeleton, YAPTerm &body );
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief PrologPredicate
|
||||
*
|
||||
* This class interfaces with Predicates Implemented in Prolog.
|
||||
*/
|
||||
class YAPFLIP: public YAPPredicate {
|
||||
public:
|
||||
YAPFLIP(CPredicate call,
|
||||
YAPAtom name,
|
||||
arity_t arity,
|
||||
YAPModule module = YAPModule(),
|
||||
CPredicate retry = 0,
|
||||
CPredicate cut = 0,
|
||||
size_t extra = 0,
|
||||
bool test = false
|
||||
) : YAPPredicate( name, arity, module) {
|
||||
if (retry) {
|
||||
Yap_InitCPredBackCut(name.getName(), arity, extra, call, retry, cut, UserCPredFlag);
|
||||
} else {
|
||||
if (test) {
|
||||
YAP_UserCPredicate (name.getName(),
|
||||
call, arity);
|
||||
} else {
|
||||
YAP_UserCPredicate (name.getName(),
|
||||
call, arity);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
YAPFLIP(const char *name,
|
||||
arity_t arity,
|
||||
YAPModule module = YAPModule(),
|
||||
bool backtrackable = false
|
||||
) : YAPPredicate( YAPAtom(name), arity, module) {
|
||||
if (backtrackable) {
|
||||
Yap_InitCPredBackCut(name, arity, 0, 0, 0, 0, UserCPredFlag);
|
||||
} else {
|
||||
YAP_UserCPredicate (name,
|
||||
0, arity);
|
||||
}
|
||||
};
|
||||
bool addCall(CPredicate call) {
|
||||
return Yap_AddCallToFli( ap, call );
|
||||
}
|
||||
bool addRetry(CPredicate call) {
|
||||
return Yap_AddRetryToFli( ap, call );
|
||||
}
|
||||
bool addCut(CPredicate call) {
|
||||
return Yap_AddCutToFli( ap, call );
|
||||
}
|
||||
|
||||
};
|
||||
|
240
CXX/yapi.cpp
240
CXX/yapi.cpp
@ -5,11 +5,12 @@
|
||||
#include "SWI-Stream.h"
|
||||
|
||||
|
||||
|
||||
YAPAtomTerm::YAPAtomTerm(char *s) { // build string
|
||||
BACKUP_H();
|
||||
|
||||
CACHE_REGS
|
||||
seq_tv_t inp, out;
|
||||
seq_tv_t inp, out;
|
||||
inp.val.c = s;
|
||||
inp.type = YAP_STRING_CHARS;
|
||||
out.type = YAP_STRING_ATOM;
|
||||
@ -267,7 +268,7 @@ intptr_t YAPTerm::hashTerm(size_t sz, size_t depth, bool variant) {
|
||||
return out;
|
||||
}
|
||||
|
||||
char *YAPTerm::text(void) {
|
||||
char *YAPTerm::text() {
|
||||
size_t sze = 4096, length;
|
||||
char *os;
|
||||
int enc;
|
||||
@ -281,7 +282,7 @@ char *YAPTerm::text(void) {
|
||||
return os;
|
||||
}
|
||||
|
||||
char *YAPQuery::text(void) {
|
||||
char *YAPQuery::text() {
|
||||
size_t sze = 4096, length;
|
||||
char *os;
|
||||
int enc;
|
||||
@ -387,81 +388,18 @@ char *YAPAtom::getName(void) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
YAPPredicate::YAPPredicate(const char *s, Term* &outp, YAPTerm &vnames) throw (int) {
|
||||
CACHE_REGS
|
||||
vnames = Yap_NewSlots(1 PASS_REGS);
|
||||
Term t = Yap_StringToTerm(s, strlen(s)+1, Yap_GetFromSlot( vnames.t PASS_REGS));
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
ap = getPred( t, outp );
|
||||
}
|
||||
|
||||
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 ;
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "H= %p, outp=%p", HR, outp) ; }
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||
ap = (PredEntry *)NULL;
|
||||
outp = (Term *)NULL;
|
||||
}
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
if (outp) outp = (Term *)NULL;
|
||||
} else if (IsPairTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
|
||||
outp = HR;
|
||||
HR[0] = RepPair(t)[0];
|
||||
HR[1] = m;
|
||||
HR+=2;
|
||||
} else {
|
||||
Functor f = FunctorOfTerm(t);
|
||||
if (IsExtensionFunctor(f)) {
|
||||
ap = (PredEntry *)NULL;
|
||||
outp = (Term *)NULL;
|
||||
} else {
|
||||
ap = RepPredProp(PredPropByFunc(f, m));
|
||||
outp = RepAppl(t)+1;
|
||||
}
|
||||
}
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "done H= %p, outp=%p", HR, outp) ; }
|
||||
return ap;
|
||||
}
|
||||
|
||||
YAPPredicate::YAPPredicate(YAPFunctor f) {
|
||||
CACHE_REGS
|
||||
ap = RepPredProp(PredPropByFunc(f.f,CurrentModule));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
YAPQuery::initQuery( Term *ts )
|
||||
{
|
||||
CACHE_REGS
|
||||
|
||||
this->oq = (YAPQuery *)LOCAL_execution;
|
||||
this->oq = (YAPQuery *)LOCAL_execution;
|
||||
LOCAL_execution = (struct open_query_struct *)this;
|
||||
this->q_open=1;
|
||||
this->q_state=0;
|
||||
this->q_flags = PL_Q_PASS_EXCEPTION;
|
||||
this->q_g = ts;
|
||||
this->q_g = ts;
|
||||
this->q_p = P;
|
||||
this->q_cp = CP;
|
||||
}
|
||||
|
||||
void
|
||||
@ -502,6 +440,7 @@ bool YAPQuery::next()
|
||||
{
|
||||
CACHE_REGS
|
||||
int result;
|
||||
|
||||
if (q_open != 1) return false;
|
||||
if (setjmp(((YAPQuery *)LOCAL_execution)->q_env))
|
||||
return false;
|
||||
@ -549,17 +488,6 @@ void YAPQuery::close()
|
||||
LOCAL_execution = (struct open_query_struct *)this->oq;
|
||||
}
|
||||
|
||||
int YAPPredicate::call(YAPTerm t[])
|
||||
{
|
||||
YAPQuery q = YAPQuery(*this, t);
|
||||
int ret;
|
||||
|
||||
ret = q.next();
|
||||
q.cut();
|
||||
q.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static YAPEngine *curren;
|
||||
|
||||
#if __ANDROID__
|
||||
@ -644,3 +572,153 @@ YAPQuery *YAPEngine::query( char *s ) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
YAPPredicate::YAPPredicate(const char *s, Term* &outp, Term &vnames) throw (int) {
|
||||
CACHE_REGS
|
||||
yhandle_t sl = Yap_NewSlots(1 PASS_REGS);
|
||||
Term t = Yap_StringToTerm(s, strlen(s)+1, sl );
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
vnames = Yap_GetFromSlot( sl PASS_REGS );
|
||||
ap = getPred( t, outp );
|
||||
}
|
||||
|
||||
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 ;
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "H= %p, outp=%p", HR, outp) ; }
|
||||
t = Yap_StripModule(t, &m);
|
||||
if (IsVarTerm(t) || IsNumTerm(t)) {
|
||||
ap = (PredEntry *)NULL;
|
||||
outp = (Term *)NULL;
|
||||
}
|
||||
if (IsAtomTerm(t)) {
|
||||
ap = RepPredProp(PredPropByAtom(AtomOfTerm(t), m));
|
||||
if (outp) outp = (Term *)NULL;
|
||||
} else if (IsPairTerm(t)) {
|
||||
ap = RepPredProp(PredPropByFunc(FunctorCsult, PROLOG_MODULE));
|
||||
outp = HR;
|
||||
HR[0] = RepPair(t)[0];
|
||||
HR[1] = m;
|
||||
HR+=2;
|
||||
} else {
|
||||
Functor f = FunctorOfTerm(t);
|
||||
if (IsExtensionFunctor(f)) {
|
||||
ap = (PredEntry *)NULL;
|
||||
outp = (Term *)NULL;
|
||||
} else {
|
||||
ap = RepPredProp(PredPropByFunc(f, m));
|
||||
outp = RepAppl(t)+1;
|
||||
}
|
||||
}
|
||||
{ CACHE_REGS __android_log_print(ANDROID_LOG_ERROR, __FUNCTION__, "done H= %p, outp=%p", HR, outp) ; }
|
||||
return ap;
|
||||
}
|
||||
|
||||
|
||||
YAPPrologPredicate::YAPPrologPredicate(YAPAtom name,
|
||||
arity_t arity,
|
||||
YAPModule mod,
|
||||
bool tabled,
|
||||
bool logical_updates,
|
||||
bool thread_local,
|
||||
bool sourced,
|
||||
bool discontiguous,
|
||||
bool multiFile,
|
||||
bool hidden,
|
||||
bool untraceable,
|
||||
bool unspyable,
|
||||
bool meta,
|
||||
bool moduleTransparent,
|
||||
bool quasiQuotable,
|
||||
size_t mega_clause
|
||||
) : YAPPredicate(name, arity, mod) {
|
||||
if (!ap) return;
|
||||
if (thread_local) {
|
||||
if (ap->cs.p_code.NOfClauses || tabled)
|
||||
return;
|
||||
ap->PredFlags |= (ThreadLocalPredFlag|LogUpdatePredFlag);
|
||||
} else if (logical_updates) {
|
||||
if (ap->cs.p_code.NOfClauses || tabled)
|
||||
return;
|
||||
ap->PredFlags |= LogUpdatePredFlag;
|
||||
ap->CodeOfPred = FAILCODE;
|
||||
ap->OpcodeOfPred = FAILCODE->opc;
|
||||
}
|
||||
if (tabled) {
|
||||
ap->PredFlags |= TabledPredFlag;
|
||||
if (ap->cs.p_code.NOfClauses || tabled)
|
||||
return;
|
||||
ap->PredFlags |= TabledPredFlag;
|
||||
}
|
||||
if (sourced) {
|
||||
ap->PredFlags |= SourcePredFlag;
|
||||
}
|
||||
if (discontiguous) {
|
||||
ap->PredFlags |= DiscontiguousPredFlag;
|
||||
}
|
||||
if (multiFile) {
|
||||
ap->PredFlags |= MultiFileFlag;
|
||||
}
|
||||
if (hidden) {
|
||||
ap->PredFlags |= HiddenPredFlag;
|
||||
}
|
||||
if (untraceable) {
|
||||
ap->PredFlags |= SourcePredFlag;
|
||||
}
|
||||
if (unspyable) {
|
||||
ap->PredFlags |= NoSpyPredFlag;
|
||||
}
|
||||
if (meta) {
|
||||
ap->PredFlags |= MetaPredFlag;
|
||||
} else if (moduleTransparent) {
|
||||
ap->PredFlags |= ModuleTransparentPredFlag;
|
||||
}
|
||||
if (quasiQuotable) {
|
||||
ap->PredFlags |= QuasiQuotationPredFlag;
|
||||
}
|
||||
if (untraceable) {
|
||||
ap->PredFlags |= SourcePredFlag;
|
||||
}
|
||||
if (hidden) {
|
||||
ap->PredFlags |= SourcePredFlag;
|
||||
}
|
||||
}
|
||||
|
||||
void *YAPPrologPredicate::assertClause( YAPTerm clause, bool last, YAPTerm source) {
|
||||
CACHE_REGS
|
||||
|
||||
Term tt = clause.gt();
|
||||
Term sourcet = source.gt();
|
||||
yamop *codeaddr = Yap_cclause(tt, PP->ArityOfPE, CurrentModule, sourcet); /* vsc: give the number of arguments
|
||||
to cclause in case there is overflow */
|
||||
Term ntt = clause.gt();
|
||||
if (LOCAL_ErrorMessage)
|
||||
return 0;
|
||||
Term *tref = &ntt;
|
||||
if (Yap_addclause(ntt, codeaddr, (last ? 0 : 2), CurrentModule, tref))
|
||||
return tref;
|
||||
return 0;
|
||||
}
|
||||
void* YAPPrologPredicate::retractClause( YAPTerm skeleton, bool all) {
|
||||
return 0;
|
||||
}
|
||||
void* YAPPrologPredicate::clause( YAPTerm skeleton, YAPTerm &body ) {
|
||||
return 0;
|
||||
}
|
||||
|
513
CXX/yapi.hh
513
CXX/yapi.hh
@ -31,15 +31,7 @@
|
||||
extern "C" {
|
||||
|
||||
#include <stddef.h>
|
||||
#ifdef __cplusplus
|
||||
#define old_cplusplus __cplusplus
|
||||
#undef __cplusplus
|
||||
#endif
|
||||
#ifdef old_cplusplus
|
||||
#define __cplusplus old_cplusplus
|
||||
#undef old_cplusplus
|
||||
#endif
|
||||
|
||||
|
||||
#include "Yap.h"
|
||||
|
||||
#include "Yatom.h"
|
||||
@ -60,8 +52,6 @@ extern "C" {
|
||||
|
||||
#include "YapText.h"
|
||||
|
||||
#include "yapie.hh"
|
||||
|
||||
#if HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
@ -88,6 +78,7 @@ extern "C" {
|
||||
// it shouldn't
|
||||
}
|
||||
|
||||
|
||||
//#include <vector>
|
||||
|
||||
|
||||
@ -97,503 +88,19 @@ class YAPFunctor;
|
||||
class YAPApplTerm;
|
||||
class YAPPairTerm;
|
||||
class YAPQuery;
|
||||
class YAPModule;
|
||||
class YAPError;
|
||||
class YAPPredicate;
|
||||
|
||||
/**
|
||||
* @brief Generic Prolog Term
|
||||
*/
|
||||
class YAPTerm {
|
||||
friend class YAPPredicate;
|
||||
friend class YAPApplTerm;
|
||||
friend class YAPPairTerm;
|
||||
friend class YAPListTerm;
|
||||
friend class YAPQuery;
|
||||
protected:
|
||||
yhandle_t t; /// handle to term, equivalent to term_t
|
||||
void mk(Term t0); /// internal method to convert from term to handle
|
||||
Term gt(); /// get handle and obtain term
|
||||
YAPTerm(Term tn) { mk( tn ); } /// private method to convert from Term (internal YAP representation) to YAPTerm
|
||||
inline Term term() { return gt(); } /// private method to convert from YAPTerm to Term (internal YAP representation)
|
||||
public:
|
||||
// do nothing constructor
|
||||
YAPTerm() { mk(TermNil); }
|
||||
/// integer to term
|
||||
YAPTerm(intptr_t i);
|
||||
/// pointer to term
|
||||
YAPTerm(void *ptr);
|
||||
/// parse string s and construct a term.
|
||||
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
|
||||
/// extract the tag of a term, after dereferencing.
|
||||
YAP_tag_t tag();
|
||||
/// copy the term ( term copy )
|
||||
YAPTerm deepCopy();
|
||||
//const YAPTerm *vars();
|
||||
/// this term is == to t1
|
||||
bool exactlyEqual(YAPTerm t1);
|
||||
bool unify(YAPTerm t1); /// t = t1
|
||||
bool unifiable(YAPTerm t1); /// we can unify t and t1
|
||||
bool variant(YAPTerm t1); /// t =@= t1, the two terms are equal up to variable renaming
|
||||
intptr_t hashTerm(size_t sz, size_t depth, bool variant); /// term hash,
|
||||
bool isVar() { return IsVarTerm( gt() ); } /// type check for unbound
|
||||
bool isAtom() { return IsAtomTerm( gt() ); } /// type check for atom
|
||||
bool isInteger() { return IsIntegerTerm( gt() ); } /// type check for integer
|
||||
bool isFloat() { return IsFloatTerm( gt() ); } /// type check for floating-point
|
||||
bool isString() { return IsStringTerm( gt() ); } /// type check for a string " ... "
|
||||
bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); } /// is a primitive term
|
||||
bool isAppl() { return IsApplTerm( gt() ); } /// is a structured term
|
||||
bool isPair() { return IsPairTerm( gt() ); } /// is a pair term
|
||||
bool isGround() { return Yap_IsGroundTerm( gt() ); } /// term is ground
|
||||
bool isList() { return Yap_IsListTerm( gt() ); } /// term is a list
|
||||
#include "yapa.hh"
|
||||
|
||||
/// extract the argument i of the term, where i in 1...arity
|
||||
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);
|
||||
}
|
||||
#include "yapie.hh"
|
||||
|
||||
/// return a string with a textual representation of the term
|
||||
char *text();
|
||||
};
|
||||
#include "yapt.hh"
|
||||
|
||||
/**
|
||||
* @brief Variable Term
|
||||
*/
|
||||
class YAPVarTerm: public YAPTerm {
|
||||
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
|
||||
public:
|
||||
/// constructor
|
||||
YAPVarTerm();
|
||||
/// get the internal representation
|
||||
CELL *getVar() { return VarOfTerm( gt() ); }
|
||||
/// is the variable bound to another one
|
||||
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
|
||||
};
|
||||
#include "yapdb.hh"
|
||||
|
||||
/**
|
||||
* @brief Compound Term
|
||||
*/
|
||||
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);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief List Constructor Term
|
||||
*/
|
||||
class YAPPairTerm: public YAPTerm {
|
||||
friend class YAPTerm;
|
||||
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() )); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Integer Term
|
||||
*/
|
||||
|
||||
class YAPIntegerTerm: public YAPTerm {
|
||||
public:
|
||||
YAPIntegerTerm(intptr_t i);
|
||||
intptr_t getInteger() { return IntegerOfTerm( gt() ); }
|
||||
bool isTagged() { return IsIntTerm( gt() ); }
|
||||
};
|
||||
|
||||
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 array of terms
|
||||
/// @param[in] the length of the array
|
||||
YAPListTerm(YAPTerm ts[], size_t n);
|
||||
*/
|
||||
// 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); }
|
||||
/// 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;
|
||||
/// construct new YAPAtom from Atom
|
||||
YAPAtom( Atom at ) { a = at; }
|
||||
public:
|
||||
/// construct new YAPAtom from string
|
||||
YAPAtom( char * s) { a = Yap_LookupAtom( s ); }
|
||||
/// construct new YAPAtom from wide string
|
||||
YAPAtom( wchar_t * s) { a = Yap_LookupMaybeWideAtom( s ); }
|
||||
/// construct new YAPAtom from max-length string
|
||||
YAPAtom( char * s, size_t len) { a = Yap_LookupAtomWithLength( s, len ); }
|
||||
/// construct new YAPAtom from max-length wide string
|
||||
YAPAtom( wchar_t * s, size_t len) { a = Yap_LookupMaybeWideAtomWithLength( s, len ); }
|
||||
/// get name of atom
|
||||
char *getName(void);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief String Term
|
||||
*/
|
||||
class YAPStringTerm: public YAPTerm {
|
||||
public:
|
||||
/// your standard constructor
|
||||
YAPStringTerm(char *s) ;
|
||||
/// use this one to construct length limited strings
|
||||
YAPStringTerm(char *s, size_t len);
|
||||
/// construct using wide chars
|
||||
YAPStringTerm(wchar_t *s) ;
|
||||
/// construct using length-limited wide chars
|
||||
YAPStringTerm(wchar_t *s, size_t len);
|
||||
const char *getString() { return StringOfTerm( gt() ); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Atom Term
|
||||
*/
|
||||
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() )); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAPFunctor represents Prolog functors Name/Arity
|
||||
*/
|
||||
class YAPFunctor {
|
||||
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:
|
||||
/// 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 ); }
|
||||
|
||||
/// Getter: extract name of functor as an atom
|
||||
///
|
||||
/// this is for external usage.
|
||||
YAPAtom name(void) {
|
||||
return YAPAtom( NameOfFunctor( f ) );
|
||||
}
|
||||
|
||||
/// Getter: extract arity of functor as an unsigned integer
|
||||
///
|
||||
/// this is for external usage.
|
||||
arity_t arity(void) {
|
||||
return ArityOfFunctor( f );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Predicates
|
||||
*
|
||||
* This class interfaces with PredEntry in Yatom.g
|
||||
*/
|
||||
class YAPPredicate {
|
||||
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, YAPTerm& vnames ) throw (int);
|
||||
|
||||
/// Term constructor for predicates
|
||||
///
|
||||
/// It is just a call to getPred
|
||||
inline YAPPredicate(Term t) {
|
||||
CELL * v = NULL;
|
||||
ap = getPred( t , v );
|
||||
}
|
||||
|
||||
/// Cast constructor for predicates,
|
||||
/// if we have the implementation data.
|
||||
///
|
||||
inline YAPPredicate(PredEntry *pe) {
|
||||
ap = pe;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/// 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) throw (int) {
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
CELL * v;
|
||||
ap = getPred( t, v);
|
||||
}
|
||||
|
||||
|
||||
/// 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) throw (int) {
|
||||
Term t, tp;
|
||||
t = YAP_ReadBuffer(s,&tp);
|
||||
if (t == 0L)
|
||||
throw YAPError::YAP_SYNTAX_ERROR;
|
||||
ap = getPred( t, outp );
|
||||
}
|
||||
|
||||
/// 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; }
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Queries
|
||||
*
|
||||
* interface to a YAP Query;
|
||||
* uses an SWI-like status info internally.
|
||||
*/
|
||||
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;
|
||||
YAPTerm 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 without varnames
|
||||
///
|
||||
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable
|
||||
/// goal. It does not ask for a list of variables.
|
||||
inline YAPQuery(char *s): YAPPredicate(s, q_g)
|
||||
{
|
||||
initQuery( q_g );
|
||||
}
|
||||
/// 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, YAPTerm &vnames): YAPPredicate(s, q_g, vnames)
|
||||
{
|
||||
initQuery( q_g );
|
||||
}
|
||||
/// set flags for query execution, currently only for exception handling
|
||||
void setFlag(int flag) {q_flags |= flag; }
|
||||
/// reset flags for query execution, currently only for exception handling
|
||||
void resetFlag(int flag) {q_flags &= ~flag; }
|
||||
/// 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();
|
||||
/// represent the top-goal
|
||||
char *text();
|
||||
/// 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
|
||||
|
||||
/// This class implements a callback Prolog-side. It will be inherited by the Java or Python
|
||||
/// class that actually implements the callback.
|
||||
class YAPCallback {
|
||||
public:
|
||||
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
|
||||
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
|
||||
virtual void run(char *s) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAP Engine: takes care of the execution environment
|
||||
where we can go executing goals.
|
||||
*
|
||||
*
|
||||
*/
|
||||
class YAPEngine {
|
||||
private:
|
||||
YAPCallback *_callback;
|
||||
YAP_init_args init_args;
|
||||
YAPError yerror;
|
||||
public:
|
||||
YAPEngine(char *savedState = (char *)NULL,
|
||||
size_t stackSize = 0,
|
||||
size_t trailSize = 0,
|
||||
size_t maxStackSize = 0,
|
||||
size_t maxTrailSize = 0,
|
||||
char *libDir = (char *)NULL,
|
||||
char *bootFile = (char *)NULL,
|
||||
char *goal = (char *)NULL,
|
||||
char *topLevel = (char *)NULL,
|
||||
bool script = FALSE,
|
||||
bool fastBoot = FALSE,
|
||||
YAPCallback *callback=(YAPCallback *)NULL); /// construct a new engine, including aaccess to callbacks
|
||||
/// kill engine
|
||||
~YAPEngine() { delYAPCallback(); }
|
||||
/// remove current callback
|
||||
void delYAPCallback() { _callback = 0; }
|
||||
/// set a new callback
|
||||
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; }
|
||||
/// execute the callback.
|
||||
void run() { if (_callback) _callback->run(); }
|
||||
/// execute the callback with a text argument.
|
||||
void run( char *s) { if (_callback) _callback->run(s); }
|
||||
/// execute the callback with a text argument.
|
||||
YAPError hasError( ) { return yerror; }
|
||||
/// build a query on the engine
|
||||
YAPQuery *query( char *s );
|
||||
};
|
||||
#include "yapq.hh"
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
127
CXX/yapq.hh
Normal file
127
CXX/yapq.hh
Normal file
@ -0,0 +1,127 @@
|
||||
#ifndef YAPQ_HH
|
||||
#define YAPQ_HH 1
|
||||
|
||||
/**
|
||||
Queries and engines
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Queries
|
||||
*
|
||||
* interface to a YAP Query;
|
||||
* uses an SWI-like status info internally.
|
||||
*/
|
||||
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;
|
||||
Term vs;
|
||||
YAP_dogoalinfo q_h;
|
||||
YAPQuery *oq;
|
||||
YAPTerm 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 without varnames
|
||||
///
|
||||
/// It is given a string, calls the parser and obtains a Prolog term that should be a callable
|
||||
/// goal. It does not ask for a list of variables.
|
||||
inline YAPQuery(const char *s): YAPPredicate(s, q_g, vs)
|
||||
{
|
||||
vnames = YAPTerm( vs );
|
||||
initQuery( q_g );
|
||||
}
|
||||
|
||||
/// set flags for query execution, currently only for exception handling
|
||||
void setFlag(int flag) {q_flags |= flag; }
|
||||
/// reset flags for query execution, currently only for exception handling
|
||||
void resetFlag(int flag) {q_flags &= ~flag; }
|
||||
/// 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();
|
||||
/// represent the top-goal
|
||||
char *text();
|
||||
/// 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
|
||||
|
||||
/// This class implements a callback Prolog-side. It will be inherited by the Java or Python
|
||||
/// class that actually implements the callback.
|
||||
class YAPCallback {
|
||||
public:
|
||||
virtual ~YAPCallback() { printf("~YAPCallback\n"); }
|
||||
virtual void run() { __android_log_print(ANDROID_LOG_INFO, __FUNCTION__, "callback"); }
|
||||
virtual void run(char *s) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief YAP Engine: takes care of the execution environment
|
||||
where we can go executing goals.
|
||||
*
|
||||
*
|
||||
*/
|
||||
class YAPEngine {
|
||||
private:
|
||||
YAPCallback *_callback;
|
||||
YAP_init_args init_args;
|
||||
YAPError yerror;
|
||||
public:
|
||||
YAPEngine(char *savedState = (char *)NULL,
|
||||
size_t stackSize = 0,
|
||||
size_t trailSize = 0,
|
||||
size_t maxStackSize = 0,
|
||||
size_t maxTrailSize = 0,
|
||||
char *libDir = (char *)NULL,
|
||||
char *bootFile = (char *)NULL,
|
||||
char *goal = (char *)NULL,
|
||||
char *topLevel = (char *)NULL,
|
||||
bool script = FALSE,
|
||||
bool fastBoot = FALSE,
|
||||
YAPCallback *callback=(YAPCallback *)NULL); /// construct a new engine, including aaccess to callbacks
|
||||
/// kill engine
|
||||
~YAPEngine() { delYAPCallback(); }
|
||||
/// remove current callback
|
||||
void delYAPCallback() { _callback = 0; }
|
||||
/// set a new callback
|
||||
void setYAPCallback(YAPCallback *cb) { delYAPCallback(); _callback = cb; }
|
||||
/// execute the callback.
|
||||
void run() { if (_callback) _callback->run(); }
|
||||
/// execute the callback with a text argument.
|
||||
void run( char *s) { if (_callback) _callback->run(s); }
|
||||
/// execute the callback with a text argument.
|
||||
YAPError hasError( ) { return yerror; }
|
||||
/// build a query on the engine
|
||||
YAPQuery *query( char *s );
|
||||
/// current module for the engine
|
||||
YAPModule currentModule( ) { return YAPModule( ) ; }
|
||||
};
|
||||
|
||||
#endif /* YAPQ_HH */
|
202
CXX/yapt.hh
Normal file
202
CXX/yapt.hh
Normal file
@ -0,0 +1,202 @@
|
||||
#ifndef YAPT_HH
|
||||
#define YAPT_HH 1
|
||||
|
||||
/**
|
||||
* @brief Generic Prolog Term
|
||||
*/
|
||||
class YAPTerm {
|
||||
friend class YAPPredicate;
|
||||
friend class YAPPrologPredicate;
|
||||
friend class YAPQuery;
|
||||
friend class YAPModule;
|
||||
friend class YAPModuleProp;
|
||||
protected:
|
||||
yhandle_t t; /// handle to term, equivalent to term_t
|
||||
void mk(Term t0); /// internal method to convert from term to handle
|
||||
Term gt(); /// get handle and obtain term
|
||||
public:
|
||||
~YAPTerm() {};
|
||||
YAPTerm(Term tn) { mk( tn ); } /// private method to convert from Term (internal YAP representation) to YAPTerm
|
||||
// do nothing constructor
|
||||
YAPTerm() { mk(TermNil); }
|
||||
/// integer to term
|
||||
YAPTerm(intptr_t i);
|
||||
/// pointer to term
|
||||
YAPTerm(void *ptr);
|
||||
/// parse string s and construct a term.
|
||||
YAPTerm(char *s) { Term tp ; mk( YAP_ReadBuffer(s,&tp) ); }
|
||||
/// extract the tag of a term, after dereferencing.
|
||||
YAP_tag_t tag();
|
||||
/// copy the term ( term copy )
|
||||
YAPTerm deepCopy();
|
||||
inline Term term() { return gt(); } /// private method to convert from YAPTerm to Term (internal YAP representation)
|
||||
//const YAPTerm *vars();
|
||||
/// this term is == to t1
|
||||
bool exactlyEqual(YAPTerm t1);
|
||||
bool unify(YAPTerm t1); /// t = t1
|
||||
bool unifiable(YAPTerm t1); /// we can unify t and t1
|
||||
bool variant(YAPTerm t1); /// t =@= t1, the two terms are equal up to variable renaming
|
||||
intptr_t hashTerm(size_t sz, size_t depth, bool variant); /// term hash,
|
||||
bool isVar() { return IsVarTerm( gt() ); } /// type check for unbound
|
||||
bool isAtom() { return IsAtomTerm( gt() ); } /// type check for atom
|
||||
bool isInteger() { return IsIntegerTerm( gt() ); } /// type check for integer
|
||||
bool isFloat() { return IsFloatTerm( gt() ); } /// type check for floating-point
|
||||
bool isString() { return IsStringTerm( gt() ); } /// type check for a string " ... "
|
||||
bool isCompound() { return !(IsVarTerm( gt() ) || IsNumTerm( gt() )); } /// is a primitive term
|
||||
bool isAppl() { return IsApplTerm( gt() ); } /// is a structured term
|
||||
bool isPair() { return IsPairTerm( gt() ); } /// is a pair term
|
||||
bool isGround() { return Yap_IsGroundTerm( gt() ); } /// term is ground
|
||||
bool isList() { return Yap_IsListTerm( gt() ); } /// term is a list
|
||||
|
||||
/// extract the argument i of the term, where i in 1...arity
|
||||
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);
|
||||
}
|
||||
|
||||
/// return a string with a textual representation of the term
|
||||
char *text();
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Variable Term
|
||||
*/
|
||||
class YAPVarTerm: private YAPTerm {
|
||||
YAPVarTerm(Term t) { if (IsVarTerm(t)) mk( t ); }
|
||||
public:
|
||||
/// constructor
|
||||
YAPVarTerm();
|
||||
/// get the internal representation
|
||||
CELL *getVar() { return VarOfTerm( gt() ); }
|
||||
/// is the variable bound to another one
|
||||
bool unbound() { return IsUnboundVar(VarOfTerm( gt() )); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Compound Term
|
||||
*/
|
||||
class YAPApplTerm: YAPTerm {
|
||||
friend class YAPTerm;
|
||||
YAPApplTerm(Term t0) { mk(t0); }
|
||||
public:
|
||||
~YAPApplTerm() { }
|
||||
YAPApplTerm(YAPFunctor f, YAPTerm ts[]);
|
||||
YAPApplTerm(YAPFunctor f);
|
||||
YAPFunctor getFunctor();
|
||||
YAPTerm getArg(int i);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief List Constructor Term
|
||||
*/
|
||||
class YAPPairTerm: YAPTerm {
|
||||
friend class YAPTerm;
|
||||
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() )); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Integer Term
|
||||
*/
|
||||
|
||||
class YAPIntegerTerm: private YAPTerm {
|
||||
public:
|
||||
YAPIntegerTerm(intptr_t i);
|
||||
intptr_t getInteger() { return IntegerOfTerm( gt() ); }
|
||||
bool isTagged() { return IsIntTerm( gt() ); }
|
||||
};
|
||||
|
||||
class YAPListTerm: private 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 array of terms
|
||||
/// @param[in] the length of the array
|
||||
YAPListTerm(YAPTerm ts[], size_t n);
|
||||
*/
|
||||
// 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); }
|
||||
/// 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 String Term
|
||||
*/
|
||||
class YAPStringTerm: private YAPTerm {
|
||||
public:
|
||||
/// your standard constructor
|
||||
YAPStringTerm(char *s) ;
|
||||
/// use this one to construct length limited strings
|
||||
YAPStringTerm(char *s, size_t len);
|
||||
/// construct using wide chars
|
||||
YAPStringTerm(wchar_t *s) ;
|
||||
/// construct using length-limited wide chars
|
||||
YAPStringTerm(wchar_t *s, size_t len);
|
||||
const char *getString() { return StringOfTerm( gt() ); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Atom Term
|
||||
* Term Representation of an Atom
|
||||
*/
|
||||
class YAPAtomTerm: YAPTerm {
|
||||
friend class YAPModule;
|
||||
// Constructor: receives a C-atom;
|
||||
YAPAtomTerm(Atom a) { mk( MkAtomTerm(a) ); }
|
||||
YAPAtomTerm(Term t): YAPTerm(t) { IsAtomTerm(t); }
|
||||
// Getter for Prolog atom
|
||||
Term getTerm() { return t; }
|
||||
public:
|
||||
// Constructor: receives an atom;
|
||||
YAPAtomTerm(YAPAtom a): YAPTerm() { mk( MkAtomTerm(a.a) ); }
|
||||
// Constructor: receives a sequence of ISO-LATIN1 codes;
|
||||
YAPAtomTerm(char *s) ;
|
||||
// Constructor: receives a sequence of up to n ISO-LATIN1 codes;
|
||||
YAPAtomTerm(char *s, size_t len);
|
||||
// Constructor: receives a sequence of wchar_ts, whatever they may be;
|
||||
YAPAtomTerm(wchar_t *s) ;
|
||||
// Constructor: receives a sequence of n wchar_ts, whatever they may be;
|
||||
YAPAtomTerm(wchar_t *s, size_t len);
|
||||
// Getter: outputs the atom;
|
||||
YAPAtom getAtom() { return YAPAtom(AtomOfTerm( gt() )); }
|
||||
// Getter: outputs the name as a sequence of ISO-LATIN1 codes;
|
||||
const char *getName() { return AtomOfTerm( gt() )->StrOfAE; }
|
||||
};
|
||||
|
||||
#endif /* YAPT_HH */
|
Reference in New Issue
Block a user