YAP 7.1.0
yapdb.hh
Go to the documentation of this file.
1
4
5#ifndef _YAPDB_H
6#define _YAPDB_H
7
8#include <YapInterface.h>
9
10#define YAP_CPP_DB_INTERFACE 1
11
32class YAPTerm;
33class YAPAtomTerm;
34
35class YAPError;
36
37class YAPModule;
38
46class X_API YAPModule : protected YAPAtomTerm {
47 friend class YAPPredicate;
48 friend class YAPModuleProp;
49 Term t() { return gt(); }
50 Term curModule() { CACHE_REGS return Yap_CurrentModule(); }
51
52public:
53 YAPModule(YAP_Term t) : YAPAtomTerm(t){};
54 YAPModule() : YAPAtomTerm(curModule()){};
56 YAPModule(YAPStringTerm t) : YAPAtomTerm(t.getString()){};
57 Term term() { return gt(); };
58};
59
65class X_API YAPModuleProp : public YAPProp {
66 friend class YAPPredicate;
67 ModEntry *m;
68
69 YAPModuleProp(ModEntry *mod) { m = mod; };
70 YAPModuleProp(Term tmod) { m = Yap_GetModuleEntry(tmod); };
71
72public:
73 YAPModuleProp(YAPModule tmod) { m = Yap_GetModuleEntry(tmod.gt()); };
74 YAPModuleProp() { m = Yap_GetModuleEntry(Yap_CurrentModule()); };
75 virtual YAPModule module() { return YAPModule(m->AtomOfME); };
76};
77
83class X_API YAPPredicate : public YAPModuleProp {
84 friend class YAPQuery;
85 friend class YAPEngine;
86
87protected:
88 PredEntry *ap;
89
91
93 PredEntry *getPred(Term &t, Term &tm, CELL *&outp);
94
95 PredEntry *asPred() { return ap; };
96
100 inline YAPPredicate() {}
101 YAPPredicate(Term &to, Term &tmod, CELL *&ts, const char *pname);
102
106 inline YAPPredicate(Term t, CELL *&v) {
107 if (t) {
108 Term tm = Yap_CurrentModule();
109 ap = getPred(t, tm, v);
110 }
111 }
112
113 inline YAPPredicate(Term t) {
114 if (t) {
115 CELL *v = nullptr;
116 Term tm = Yap_CurrentModule();
117 ap = getPred(t, tm, v);
118 }
119 }
120
124 inline YAPPredicate(YAPTerm t, CELL *&v) {
125 Term tp = t.term(), tm = Yap_CurrentModule();
126 ap = getPred(tp, tm, v);
127 }
128 inline YAPPredicate(YAPTerm t) {
129 CELL *v = nullptr;
130 Term tp = t.term();
131 Term tm = Yap_CurrentModule();
132 ap = getPred(tp, tm, v);
133 }
134
138 inline YAPPredicate(PredEntry *pe) { ap = pe; }
139
142 inline YAPPredicate(Functor f, Term mod) {
143 ap = RepPredProp(PredPropByFunc(f, mod));
144 }
145
146public:
152 YAPPredicate(const char *s0, Term &tout, YAPPairTerm * &names, CELL *&nts) {
153 CACHE_REGS
154 const char *s = (const char *)s0;
155 Term tnames = MkVarTerm(),
156 parameters = TermNil;
157 tout =
158 Yap_BufferToTermWithPrioBindings(s, parameters, tnames, strlen(s0)+1, 1200);
159
160 // fprintf(stderr,"ap=%p arity=%d text=%s", ap, ap->ArityOfPE, s);
161 if (tout == 0L) {
162 return;
163 throw YAPError();
164 }
165 Term tm = Yap_CurrentModule();
166 ap = getPred(tout, tm, nts);
167 tout = Yap_SaveTerm(tout);
168
169 names = new YAPPairTerm(tnames);
170 }
171
176 CACHE_REGS
177 ap = RepPredProp(PredPropByFunc(f.f, Yap_CurrentModule()));
178 }
179
183 ap = RepPredProp(PredPropByFunc(f.f, mod.term()));
184 }
185
188 inline YAPPredicate(YAPAtom at, YAPTerm mod) {
189 ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
190 }
191
195
198 inline YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod) {
199 if (arity) {
200 Functor f = Yap_MkFunctor(at.a, arity);
201 ap = RepPredProp(PredPropByFunc(f, mod.term()));
202 } else {
203 ap = RepPredProp(PredPropByAtom(at.a, mod.term()));
204 }
205 }
206
209 YAPPredicate(YAPAtom at, uintptr_t arity);
210
213 inline YAPPredicate(const std::string at, uintptr_t arity, std::string mod="") {
214 Term m;
215 if (//mod != nullptr &&
216 mod[0] != 0)
217 m = MkAtomTerm(Yap_LookupAtom(mod.c_str()));
218 else
220 ap = RepPredProp(PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at.c_str()), arity),
221 m));
222}
223
226 inline YAPPredicate(const char *at, uintptr_t arity) {
227 ap = RepPredProp(PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity),
229 };
230
233 inline YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod) {
234 ap = RepPredProp(
235 PredPropByFunc(Yap_MkFunctor(Yap_LookupAtom(at), arity), mod.term()));
236 };
237
240 inline YAPPredicate(const char *at, YAPTerm mod) {
241 ap = RepPredProp(PredPropByAtom(Yap_LookupAtom(at), mod.term()));
242 }
243
248 if (ap->ModuleOfPred == PROLOG_MODULE)
249 return YAPModule(AtomProlog);
250 else
251 return YAPModule(AtomOfTerm(ap->ModuleOfPred));
252 }
253
258 if (ap->ArityOfPE)
259 return YAPAtom((Atom)ap->FunctorOfPred);
260 else
261 return YAPAtom(NameOfFunctor(ap->FunctorOfPred));
262 }
263
268 if (ap->ArityOfPE)
269 return YAPFunctor(ap->FunctorOfPred);
270 Yap_ThrowError(DOMAIN_ERROR_OUT_OF_RANGE, MkIntTerm(0),
271 "YAPFunctor::functor");
272 }
273
277 uintptr_t getArity() { return ap->ArityOfPE; }
278 arity_t arity() { return ap->ArityOfPE; }
279 PredEntry *predEntry() { return ap; }
280};
281
287class X_API YAPPrologPredicate : public YAPPredicate {
288public:
290 YAPPrologPredicate(const char *s, arity_t arity) : YAPPredicate(s, arity){};
291 YAPPrologPredicate(YAPAtom s, arity_t arity) : YAPPredicate(s, arity){};
293 bool assertClause(YAPTerm clause, bool last = true,
296 bool assertFact(YAPTerm *tuple, bool last = true);
298 void *retractClause(YAPTerm skeleton, bool all = false);
300 // YAPTerm clause(size_t index, YAPPredicate p) { return YAPTerm(); };
302 YAPTerm *nextClause() { return nullptr; };
303};
304
310class X_API YAPFLIP : public YAPPredicate {
311public:
312 YAPFLIP(YAP_UserCPred call, std::string name, YAP_Arity arity,
313 const std::string module = std::string(RepAtom(AtomOfTerm(CurrentModule))->StrOfAE), YAP_UserCPred retry = 0,
314 YAP_UserCPred cut = 0, YAP_Arity extra = 0, bool test = false)
315 : YAPPredicate(name.c_str(), arity, MkAtomTerm(Yap_LookupAtom(module.c_str()))) {
316 if (retry) {
317 YAP_UserBackCutCPredicate(name.c_str(), call, retry, cut, arity, extra);
318 } else {
319 if (test) {
320 YAP_UserCPredicate(name.c_str(), call, arity);
321 } else {
322 YAP_UserCPredicate(name.c_str(), call, arity);
323 }
324 }
325 };
326 YAPFLIP(const char *name, uintptr_t arity, YAPModule module = YAPModule(),
327 bool backtrackable = false)
328 : YAPPredicate(YAPAtom(name), arity, module) {
329 if (backtrackable) {
330 Yap_InitCPredBackCut(name, arity, 0, 0, 0, 0, UserCPredFlag);
331 } else {
332 YAP_UserCPredicate(name, 0, arity);
333 }
334 };
335 bool addCall(CPredicate call) { return Yap_AddCallToFli(ap, call); }
336 bool addRetry(CPredicate call) { return Yap_AddRetryToFli(ap, call); }
337 bool addCut(CPredicate call) { return Yap_AddCutToFli(ap, call); }
338
339 //> access to input argument i as a term
340 Term x(int i) { return XREGS[i]; }
341 //> access to input argument as a YAPTerm
342 YAPTerm X(int i) { return YAPTerm(XREGS[i]); }
343
344 //> unify term t with argumentr i
345 bool output( Term t, int i) { return Yap_unify(XREGS[i], t); };
346 bool output( YAPTerm t, int i) { return t.unify(XREGS[i]); };
347
348 //> ensure at least cells cells are available
349 bool ensureStorage( size_t cells) { return Yap_dogcl(cells*sizeof(CELL) PASS_REGS); }
350 //> ensure memory but take care to first save the terms in
351 //> ts. YAPTerms do no require this.
352 bool ensureStorage( size_t cells, std::vector<Term> ts)
353 { return Yap_dogcl(cells*sizeof(CELL) PASS_REGS); };
354};
355
356#endif
357
358
359
360
@ source
If true maintain the source for all clauses.
Definition: YapGFlagInfo.h:601
YAPPredicate(YAPAtom at, YAPTerm mod)
Name/arity constructor for predicates.
Definition: yapdb.hh:188
YAPPredicate()
Empty constructor for predicates.
Definition: yapdb.hh:100
YAPPredicate(const char *at, uintptr_t arity)
char *‍/arity constructor for predicates
Definition: yapdb.hh:226
YAPPredicate(YAPAtom at, uintptr_t arity, YAPModule mod)
Mod:Name/Arity constructor for predicates.
Definition: yapdb.hh:198
YAPTerm * nextClause()
return the Nth clause (if source is available)
Definition: yapdb.hh:302
YAPPredicate(Functor f, Term mod)
Functor constructor for predicates, is given a specific module.
Definition: yapdb.hh:142
YAPPredicate(PredEntry *pe)
Cast constructor for predicates, if we have the implementation data.
Definition: yapdb.hh:138
YAPPredicate(YAPTerm t, CELL *&v)
Term constructor for predicates.
Definition: yapdb.hh:124
YAPPredicate(const std::string at, uintptr_t arity, std::string mod="")
std::string/Arity constructor for predicates
Definition: yapdb.hh:213
YAPPredicate(const char *at, YAPTerm mod)
char *‍/module constructor for predicates
Definition: yapdb.hh:240
YAPAtom name()
name of predicate
Definition: yapdb.hh:257
YAPPredicate(Term t, CELL *&v)
Term constructor for predicates.
Definition: yapdb.hh:106
YAPPredicate(YAPFunctor f, YAPTerm mod)
Functor constructor for predicates, is given a specific module.
Definition: yapdb.hh:182
YAPFunctor functor()
functor of predicate
Definition: yapdb.hh:267
YAPPredicate(const char *at, uintptr_t arity, YAPTerm mod)
char *‍/module constructor for predicates
Definition: yapdb.hh:233
YAPModule module()
module of a predicate
Definition: yapdb.hh:247
YAPPredicate(const char *s0, Term &tout, YAPPairTerm *&names, CELL *&nts)
String constructor for predicates.
Definition: yapdb.hh:152
YAPPredicate(YAPFunctor f)
Functor constructor for predicates.
Definition: yapdb.hh:175
uintptr_t getArity()
arity of predicate
Definition: yapdb.hh:277
PrologPredicate.
Definition: yapdb.hh:310
YAPModule A YAPModule describes a bare module, which in YAP is just a name.
Definition: yapdb.hh:46
YAPModuleProp A YAPModuleProp controls access to a module property.
Definition: yapdb.hh:65
Predicates.
Definition: yapdb.hh:83
PrologPredicate.
Definition: yapdb.hh:287
take information on a Prolog error:
Definition: yapie.hh:31
Atom A YAP data-base is a collection of atoms, where each atom has a name and a set of Properties.
Definition: yapa.hh:71
YAPFunctor represents Prolog functors Name/Arity.
Definition: yapa.hh:125
Prop A YAP Propery is ameta-class for everything one can in a atom.
Definition: yapa.hh:109
Term Yap_CurrentModule()
current module for the engine
Definition: yapq.hh:371
YAP Engine: takes care of the execution environment where we can go executing goals.
Definition: yapq.hh:317
Queries and engines.
Definition: yapq.hh:38
Term gt()
handle to term, equivalent to term_t
Definition: yapt.hh:57
YAPTerm()
private method to convert from Term (internal YAP representation) to YAPTerm
Definition: yapt.hh:96
virtual arity_t arity()
extract the arity of the term variables have arity 0
Definition: yapt.hh:228
Atom Term Term Representation of an Atom.
Definition: yapt.hh:613
List Constructor Term.
Definition: yapt.hh:448
String Term.
Definition: yapt.hh:596
Generic Prolog Term.
Definition: yapt.hh:42
A matrix.
Definition: matrix.c:68
Module property: low-level data used to manage modes.
Definition: Yatom.h:209
Definition: Yatom.h:544