This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CXX/yapdb.hh

328 lines
8.9 KiB
C++
Raw Permalink Normal View History

2017-04-07 23:10:59 +01:00
/// @file yapdb.hh
///
/// @brief C++ Interface to generated code.
2016-07-31 16:22:24 +01:00
#ifndef _YAPDB_H
#define _YAPDB_H
2018-06-15 16:44:16 +01:00
#include <YapInterface.h>
2015-02-09 01:52:10 +00:00
#define YAP_CPP_DB_INTERFACE 1
/**
*
* @defgroup yap-cplus-db-interface Data-Base Component of YAP interface.
*
* @ingroup yap-cplus-interface
2017-04-07 23:10:59 +01:00
* @{
2015-02-09 01:52:10 +00:00
* @tableofcontents
*
*
2017-04-07 23:10:59 +01:00
* These classes define the main data-structures stored to represent compiled
* programs:
*
* + YAPFunctor represents a name/arity combination.
2017-05-02 07:38:23 +01:00
*
2017-04-07 23:10:59 +01:00
* + YAPModule wraps the YAP module implementation.
*
2017-05-02 07:38:23 +01:00
* + YAPPredicate and subclasses store the actual program, Preliminary
* support covers Prolog and C-defined predicates.
2015-02-09 01:52:10 +00:00
*/
class YAPTerm;
2016-07-31 16:22:24 +01:00
class YAPAtomTerm;
2015-02-09 01:52:10 +00:00
class YAPError;
class YAPModule;
/**
* @brief YAPModule
* A YAPModule describes a bare module, which in YAP is just a name.
*
* Info about the module is in YAPModuleProp
*
*/
2017-06-12 18:00:47 +01:00
class X_API YAPModule : protected YAPAtomTerm {
2015-02-09 01:52:10 +00:00
friend class YAPPredicate;
2017-02-20 14:38:00 +00:00
friend class YAPModuleProp;
2015-02-09 01:52:10 +00:00
Term t() { return gt(); }
2016-09-21 20:41:23 +01:00
Term curModule() { CACHE_REGS return Yap_CurrentModule(); }
2015-02-09 01:52:10 +00:00
public:
2017-06-05 13:06:12 +01:00
YAPModule(YAP_Term t) : YAPAtomTerm(t){};
2017-03-20 15:52:48 +00:00
YAPModule() : YAPAtomTerm(curModule()){};
YAPModule(YAPAtom t) : YAPAtomTerm(t){};
2018-07-21 01:56:48 +01:00
YAPModule(YAPStringTerm t) : YAPAtomTerm(t.getString()){};
2017-06-05 13:06:12 +01:00
Term term() { return gt(); };
2015-02-09 01:52:10 +00:00
};
/**
* @brief YAPModuleProp
* A YAPModuleProp controls access to a module property.
*
*/
2017-06-12 18:00:47 +01:00
class X_API YAPModuleProp : public YAPProp {
2015-02-09 01:52:10 +00:00
friend class YAPPredicate;
ModEntry *m;
2015-04-13 13:28:17 +01:00
2016-09-21 20:41:23 +01:00
YAPModuleProp(ModEntry *mod) { m = mod; };
2017-03-20 15:52:48 +00:00
YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
2016-09-21 20:41:23 +01:00
2015-04-13 13:28:17 +01:00
public:
2017-02-20 14:38:00 +00:00
YAPModuleProp(YAPModule tmod) { m = Yap_GetModuleEntry(tmod.gt()); };
2018-07-17 11:43:57 +01:00
YAPModuleProp() { m = Yap_GetModuleEntry(Yap_CurrentModule()); };
2017-03-20 15:52:48 +00:00
virtual YAPModule module() { return YAPModule(m->AtomOfME); };
2015-02-09 01:52:10 +00:00
};
/**
* @brief Predicates
*
* This class interfaces with PredEntry in Yatom.
*/
2017-06-12 18:00:47 +01:00
class X_API YAPPredicate : public YAPModuleProp {
2015-02-09 01:52:10 +00:00
friend class YAPQuery;
2016-09-21 20:41:23 +01:00
friend class YAPEngine;
2015-02-09 01:52:10 +00:00
protected:
PredEntry *ap;
/// auxiliary routine to find a predicate in the current module.
2017-11-29 13:47:57 +00:00
2018-07-17 11:43:57 +01:00
/// auxiliary routine to find a predicate in the current module.
PredEntry *getPred(Term &t, Term &tm, CELL *&outp);
2016-09-21 20:41:23 +01:00
PredEntry *asPred() { return ap; };
2015-02-09 01:52:10 +00:00
2018-07-17 11:43:57 +01:00
/// Empty constructor for predicates
2017-05-19 09:56:37 +01:00
///
/// Just do nothing.
2018-07-17 11:43:57 +01:00
inline YAPPredicate() {}
YAPPredicate(Term &to, Term &tmod, CELL *&ts, const char *pname);
2018-06-18 10:51:01 +01:00
2015-02-09 01:52:10 +00:00
/// Term constructor for predicates
///
/// It is just a call to getPred
2018-06-01 08:37:25 +01:00
inline YAPPredicate(Term t, CELL *&v) {
if (t) {
2018-07-17 11:43:57 +01:00
Term tm = Yap_CurrentModule();
ap = getPred(t, tm, v);
2018-06-01 08:37:25 +01:00
}
}
2018-06-15 13:50:55 +01:00
2018-07-17 11:43:57 +01:00
inline YAPPredicate(Term t) {
2018-06-01 08:37:25 +01:00
if (t) {
CELL *v = nullptr;
2018-07-17 11:43:57 +01:00
Term tm = Yap_CurrentModule();
ap = getPred(t, tm, v);
2018-06-01 08:37:25 +01:00
}
2015-02-09 01:52:10 +00:00
}
2016-09-27 18:28:54 +01:00
/// Term constructor for predicates
///
/// It is just a call to getPred
2018-06-01 08:37:25 +01:00
inline YAPPredicate(YAPTerm t, CELL *&v) {
2018-07-17 11:43:57 +01:00
Term tp = t.term(), tm = Yap_CurrentModule();
ap = getPred(tp, tm, v);
2018-06-01 08:37:25 +01:00
}
2018-07-17 11:43:57 +01:00
inline YAPPredicate(YAPTerm t) {
CELL *v = nullptr;
Term tp = t.term();
Term tm = Yap_CurrentModule();
ap = getPred(tp, tm, v);
2016-09-27 18:28:54 +01:00
}
2015-02-09 01:52:10 +00:00
/// Cast constructor for predicates,
/// if we have the implementation data.
///
2016-09-21 20:41:23 +01:00
inline YAPPredicate(PredEntry *pe) { ap = pe; }
2015-02-09 01:52:10 +00:00
2018-07-17 11:43:57 +01:00
/// Functor constructor for predicates, is given a specific module.
2017-05-19 09:56:37 +01:00
/// This version avoids manufacturing objects
inline YAPPredicate(Functor f, Term mod) {
ap = RepPredProp(PredPropByFunc(f, mod));
}
2015-02-09 01:52:10 +00:00
public:
2018-07-17 11:43:57 +01:00
/// String constructor for predicates
2018-06-01 08:37:25 +01:00
///
/// It also communicates the array of arguments t[]
/// and the array of variables
/// back to yapquery
2018-06-18 12:16:36 +01:00
YAPPredicate(const char *s0, Term &tout, YAPPairTerm &names, CELL *&nts) {
2018-06-01 08:37:25 +01:00
CACHE_REGS
const char *s = (const char *)s0;
2018-06-18 12:16:36 +01:00
Term tnames = MkVarTerm();
2018-06-01 08:37:25 +01:00
tout =
2018-07-17 11:43:57 +01:00
Yap_BufferToTermWithPrioBindings(s, TermNil, tnames, strlen(s0), 1200);
2018-06-01 08:37:25 +01:00
// fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
// Yap_DebugPlWrite(out);
if (tout == 0L) {
2018-07-17 11:43:57 +01:00
return;
2018-06-01 08:37:25 +01:00
throw YAPError();
2018-07-17 11:43:57 +01:00
}
Term tm = Yap_CurrentModule();
ap = getPred(tout, tm, nts);
tout = Yap_SaveTerm(tout);
names = YAPPairTerm(tnames);
2018-06-01 08:37:25 +01:00
}
2015-02-09 01:52:10 +00:00
/// Functor constructor for predicates
///
/// Asssumes that we use the current module.
2017-03-20 15:52:48 +00:00
YAPPredicate(YAPFunctor f) {
CACHE_REGS
2018-07-17 11:43:57 +01:00
ap = RepPredProp(PredPropByFunc(f.f, Yap_CurrentModule()));
2016-09-27 18:28:54 +01:00
}
2015-02-09 01:52:10 +00:00
/// Functor constructor for predicates, is given a specific module.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(YAPFunctor f, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByFunc(f.f, mod.term()));
2015-02-09 01:52:10 +00:00
}
/// Name/arity constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(YAPAtom at, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
2015-02-09 01:52:10 +00:00
}
/// Name/0 constructor for predicates.
///
2017-03-20 15:52:48 +00:00
YAPPredicate(YAPAtom at);
2015-02-09 01:52:10 +00:00
/// Mod:Name/Arity constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
2015-02-09 01:52:10 +00:00
if (arity) {
Functor f = Yap_MkFunctor(at.a, arity);
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByFunc(f, mod.term()));
2015-02-09 01:52:10 +00:00
} else {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
2015-02-09 01:52:10 +00:00
}
}
/// Atom/Arity constructor for predicates.
///
2017-03-20 15:52:48 +00:00
YAPPredicate(YAPAtom at, uintptr_t arity);
2015-02-09 01:52:10 +00:00
2016-09-30 23:11:13 +01:00
/// char */module constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(const char *at, uintptr_t arity) {
2016-12-10 07:01:10 +00:00
ap = RepPredProp(PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity),
2018-07-17 11:43:57 +01:00
Yap_CurrentModule()));
2016-09-30 23:11:13 +01:00
};
/// char */module constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
2016-12-10 07:01:10 +00:00
ap = RepPredProp(
2018-07-17 11:43:57 +01:00
PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity), mod.term()));
2016-09-30 23:11:13 +01:00
};
/// char */module constructor for predicates.
///
2017-03-20 15:52:48 +00:00
inline YAPPredicate(const char *at, YAPTerm mod) {
2017-05-02 07:38:23 +01:00
ap = RepPredProp(PredPropByAtom(Yap_LookupAtom(at), mod.term()));
2016-09-30 23:11:13 +01:00
}
2015-02-09 01:52:10 +00:00
/// module of a predicate
///
/// notice that modules are currently treated as atoms, this should change.
2017-03-20 15:52:48 +00:00
YAPModule module() {
2015-02-09 01:52:10 +00:00
if (ap->ModuleOfPred == PROLOG_MODULE)
2016-09-21 20:41:23 +01:00
return YAPModule(AtomProlog);
2015-02-09 01:52:10 +00:00
else
2016-09-21 20:41:23 +01:00
return YAPModule(AtomOfTerm(ap->ModuleOfPred));
2015-02-09 01:52:10 +00:00
}
/// name of predicate
///
/// notice that we return the atom, not a string.
2016-09-21 20:41:23 +01:00
YAPAtom name() {
if (ap->ArityOfPE)
return YAPAtom((Atom)ap->FunctorOfPred);
2015-02-09 01:52:10 +00:00
else
2016-09-21 20:41:23 +01:00
return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
2015-02-09 01:52:10 +00:00
}
2017-02-20 14:38:00 +00:00
/// functor of predicate
///
/// onlu defined if arity >= 1
YAPFunctor functor() {
if (ap->ArityOfPE)
2017-03-20 15:52:48 +00:00
return YAPFunctor(ap->FunctorOfPred);
2018-07-17 11:43:57 +01:00
Yap_ThrowError(DOMAIN_ERROR_OUT_OF_RANGE, MkIntTerm(0),
"YAPFunctor::functor");
2017-02-20 14:38:00 +00:00
}
2015-02-09 01:52:10 +00:00
/// arity of predicate
///
/// we return a positive number.
2016-07-31 16:22:24 +01:00
uintptr_t getArity() { return ap->ArityOfPE; }
2016-09-27 18:28:54 +01:00
arity_t arity() { return ap->ArityOfPE; }
2017-06-05 13:06:12 +01:00
PredEntry *predEntry() { return ap; }
2015-02-09 01:52:10 +00:00
};
/**
* @brief PrologPredicate
*
* This class interfaces with Predicates Implemented in Prolog.
*/
2017-06-12 18:00:47 +01:00
class X_API YAPPrologPredicate : public YAPPredicate {
2015-02-09 01:52:10 +00:00
public:
2017-03-20 15:52:48 +00:00
YAPPrologPredicate(YAPTerm t) : YAPPredicate(t){};
YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
2017-07-03 21:18:01 +01:00
YAPPrologPredicate(YAPAtom s, arity_t arity) : YAPPredicate(s, arity){};
2016-09-27 18:28:54 +01:00
/// add a new clause
2017-03-20 15:52:48 +00:00
bool assertClause(YAPTerm clause, bool last = true,
2018-07-17 11:43:57 +01:00
YAPTerm source = YAPTerm());
2016-09-30 23:11:13 +01:00
/// add a new tuple
2017-03-20 15:52:48 +00:00
bool assertFact(YAPTerm *tuple, bool last = true);
2016-09-27 18:28:54 +01:00
/// retract at least the first clause matching the predicate.
2017-03-20 15:52:48 +00:00
void *retractClause(YAPTerm skeleton, bool all = false);
2016-09-27 18:28:54 +01:00
/// return the Nth clause (if source is available)
// YAPTerm clause(size_t index, YAPPredicate p) { return YAPTerm(); };
/// return the Nth clause (if source is available)
YAPTerm *nextClause() { return nullptr; };
2015-02-09 01:52:10 +00:00
};
/**
* @brief PrologPredicate
*
* This class interfaces with Predicates Implemented in Prolog.
*/
2017-06-12 18:00:47 +01:00
class X_API YAPFLIP : public YAPPredicate {
2015-02-09 01:52:10 +00:00
public:
2017-11-29 13:47:57 +00:00
YAPFLIP(YAP_UserCPred call, YAPAtom name, YAP_Arity arity,
YAPModule module = YAPModule(), YAP_UserCPred retry = 0,
YAP_UserCPred cut = 0, YAP_Arity extra = 0, bool test = false)
2018-07-17 11:43:57 +01:00
: YAPPredicate(name, arity, module) {
2015-02-09 01:52:10 +00:00
if (retry) {
2018-07-17 11:43:57 +01:00
YAP_UserBackCutCPredicate(name.getName(), call, retry, cut, arity, extra);
2015-02-09 01:52:10 +00:00
} else {
if (test) {
2016-09-21 20:41:23 +01:00
YAP_UserCPredicate(name.getName(), call, arity);
2015-02-09 01:52:10 +00:00
} else {
2016-09-21 20:41:23 +01:00
YAP_UserCPredicate(name.getName(), call, arity);
2015-02-09 01:52:10 +00:00
}
}
};
2017-03-20 15:52:48 +00:00
YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
2016-09-21 20:41:23 +01:00
bool backtrackable = false)
2018-07-17 11:43:57 +01:00
: YAPPredicate(YAPAtom(name), arity, module) {
2015-02-09 01:52:10 +00:00
if (backtrackable) {
Yap_InitCPredBackCut(name, arity, 0, 0, 0, 0, UserCPredFlag);
} else {
2016-09-21 20:41:23 +01:00
YAP_UserCPredicate(name, 0, arity);
}
2015-02-09 01:52:10 +00:00
};
2017-03-20 15:52:48 +00:00
bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
2016-09-21 20:41:23 +01:00
bool addCut(CPredicate call) { return Yap_AddCutToFli(ap, call); }
2015-04-13 13:28:17 +01:00
};
2016-07-31 16:22:24 +01:00
#endif
2017-03-20 15:52:48 +00:00
/// @}