Some programs need global information for, e.g.
More...
Some programs need global information for, e.g.
counting or collecting data obtained by backtracking As a rule, to keep this information, the internal data base should be used instead of asserting and retracting clauses (as most novice programmers do), In YAP (as in some other Prolog systems) the internal data base (i.d.b for short) is faster, needs less space and provides a better insulation of program and data than using asserted/retracted clauses The i.d.b is implemented as a set of terms, accessed by keys that unlikely what happens in (non-Prolog) data bases are not part of the term Under each key a list of terms is kept References are provided so that terms can be identified: each term in the i.d.b has a unique reference (references are also available for clauses of dynamic predicates)
There is a strong analogy between the i.d.b and the way dynamic predicates are stored In fact, the main i.d.b predicates might be implemented using dynamic predicates:
recorda(X,
T,R) :- asserta(idb(X,
T),R).
recordz(X,
T,R) :- assertz(idb(X,
T),R).
recorded(X,
T,R) :- clause(idb(X,
T),R).
If you like being short, use T instead of YapTerm.
We can take advantage of this, the other way around, as it is quite easy to write a simple Prolog interpreter, using the i.d.b:
asserta(G) :- recorda(interpreter,G,_).
assertz(G) :- recordz(interpreter,G,_).
retract(G) :- recorded(interpreter,G,R), !, erase(R).
call(V) :- var(V), !, fail.
call((H :- B)) :- !, recorded(interpreter,(H :- B),_), call(B).
call(G) :- recorded(interpreter,G,_).
In YAP, much attention has been given to the implementation of the i.d.b., especially to the problem of accelerating the access to terms kept in a large list under the same key Besides using the key, YAP uses an internal lookup function, transparent to the user, to find only the terms that might unify For instance, in a data base containing the terms
b
b(a)
c(d)
e(g)
b(X)
e(h)
stored under the key k/1 , when executing the query
:- recorded(k(_),c(_),R).
recorded
would proceed directly to the third term, spending almost the time as if a(X)
or b(X)
was being searched The lookup function uses the functor of the term, and its first three arguments (when they exist) So, recorded(k(_),e(h),_)
would go directly to the last term, while recorded(k(_),e(_),_)
would find first the fourth term, and then, after backtracking, the last one
This mechanism may be useful to implement a sort of hierarchy, where the functors of the terms (and eventually the first arguments) work as secondary keys
In the YAP's i.d.b an optimized representation is used for terms without free variables This results in a faster retrieval of terms and better space usage Whenever possible, avoid variables in terms in terms stored in the i.d.b
◆ db_globs
Definition at line 208 of file dbase.c.
◆ found_one
◆ LinkAr
◆ lr
◆ sz
◆ tofref
◆ visitel
Definition at line 585 of file dbase.c.
Public Attributes |
CELL * | addr |
|
◆ addr
◆ recorda_at/3
recorda_at(+ R0, T,- R)
Makes term T the record preceding record with reference R0, and unifies R with its reference
◆ recordz/3
recordz(+ K, T,- R)
Makes term T the last record under key K and unifies R with its reference
◆ recordz_at/3
recordz_at(+ R0, T,- R)
Makes term T the record following record with reference R0, and unifies R with its reference
◆ key_statistics/4
key_statistics(+ K,- Entries,- Size,- IndexSize)
Returns several statistics for a key K Currently, it says how many entries we have for that key, Entries, what is the total size spent on entries, Size, and what is the amount of space spent in indices
◆ erase/1
erase(+ R)
The term referred to by R is erased from the internal database If reference R does not exist in the database, erase
just fails
◆ eraseall/1
eraseall(+ K)
All terms belonging to the key K
are erased from the internal database The predicate always succeeds
◆ erased/1
erased(+ R)
Succeeds if the object whose database reference is R has been erased
◆ instance/2
instance(+ R,- T)
If R refers to a clause or a recorded term, T is unified with its most general instance If R refers to an unit clause C, then T is unified with _C_ :- true
When R is not a reference to an existing clause or to a recorded term, this goal fails
◆ recorded/3
◆ recorda/3
◆ AllocDBSpace
#define AllocDBSpace |
( |
|
V | ) |
((DBRef)Yap_AllocCodeSpace(V)) |
◆ AtomHash
#define AtomHash |
( |
|
t | ) |
(Unsigned(t) >> 4) |
◆ CheckDBOverflow
#define CheckDBOverflow |
( |
|
X | ) |
|
Value: if (CodeMax + X >= (CELL *)
visited - 1024) { \
goto error; \
}
Definition at line 593 of file dbase.c.
◆ CheckVisitOverflow
#define CheckVisitOverflow |
( |
| ) |
|
Value: if ((CELL *)tovisit + 1024 >= ASP) { \
goto error2; \
}
Definition at line 599 of file dbase.c.
◆ db_check_trail
#define db_check_trail |
( |
|
x | ) |
|
Value: { \
if (Unsigned(dbg->tofref) == Unsigned(x)) { \
goto error_tr_overflow; \
} \
}
Definition at line 304 of file dbase.c.
◆ DB_MARKED
#define DB_MARKED |
( |
|
d0 | ) |
((CELL *)(d0) < CodeMax && (CELL *)(d0) >= tbase) |
◆ DB_UNWIND_CUNIF
#define DB_UNWIND_CUNIF |
( |
| ) |
|
◆ DBLength
#define DBLength |
( |
|
V | ) |
(sizeof(DBStruct) + (Int)(V) + CellSize) |
◆ DISCONNECT_OLD_ENTRIES
#define DISCONNECT_OLD_ENTRIES 1 |
◆ DualHashFieldMask
#define DualHashFieldMask ((CELL)0xffffL) |
◆ FourHashFieldMask
#define FourHashFieldMask ((CELL)0xffffffffL) |
◆ FreeDBSpace
#define FreeDBSpace |
( |
|
V | ) |
Yap_FreeCodeSpace(V) |
◆ FrstDBRef
#define FrstDBRef |
( |
|
V | ) |
((V)->First) |
◆ FunctorHash
#define FunctorHash |
( |
|
t | ) |
(Unsigned(t) >> 4) |
◆ HashFieldMask
#define HashFieldMask ((CELL)0xffL) |
◆ InQueue
◆ LARGE_IDB_LINK_TABLE
#define LARGE_IDB_LINK_TABLE 1 |
◆ MarkThisRef
#define MarkThisRef |
( |
|
Ref | ) |
((Ref)->NOfRefsTo++) |
◆ MkCode
◆ MkFirst
◆ MkIfNot
◆ MkLast
◆ NextDBRef
#define NextDBRef |
( |
|
V | ) |
((V)->Next) |
◆ NumberHash
#define NumberHash |
( |
|
t | ) |
(Unsigned(IntOfTerm(t))) |
◆ ONE_FIELD_SHIFT
#define ONE_FIELD_SHIFT 8 |
◆ Register
#define Register register |
◆ SIZEOF_LINK_ENTRY
#define SIZEOF_LINK_ENTRY 4 |
◆ THREE_FIELDS_SHIFT
#define THREE_FIELDS_SHIFT 24 |
◆ ToSmall
#define ToSmall |
( |
|
V | ) |
((link_entry)(Unsigned(V) >> 3)) |
◆ TripleHashFieldMask
#define TripleHashFieldMask ((CELL)0xffffffL) |
◆ TWO_FIELDS_SHIFT
#define TWO_FIELDS_SHIFT 16 |
◆ WithRef
◆ link_entry
typedef BITS32 link_entry |
◆ Yap_db_nth_recorded()
Int Yap_db_nth_recorded |
( |
PredEntry * |
pe, |
|
|
Int Count |
USES_REGS |
|
) |
| |
◆ Yap_dequeue_tqueue()
bool Yap_dequeue_tqueue |
( |
db_queue * |
father_key, |
|
|
Term |
t, |
|
|
bool |
first, |
|
|
bool release |
USES_REGS |
|
) |
| |
◆ Yap_destroy_tqueue()
void Yap_destroy_tqueue |
( |
db_queue *dbq |
USES_REGS | ) |
|
◆ Yap_enqueue_tqueue()
bool Yap_enqueue_tqueue |
( |
db_queue * |
father_key, |
|
|
Term t |
USES_REGS |
|
) |
| |
◆ Yap_ErCl()
◆ Yap_ErDBE()
void Yap_ErDBE |
( |
DBRef |
entryref | ) |
|
◆ Yap_ErLogUpdCl()
◆ Yap_EvalMasks()
CELL Yap_EvalMasks |
( |
register Term |
tm, |
|
|
CELL * |
keyp |
|
) |
| |
◆ Yap_FetchClauseTermFromDB()
Term Yap_FetchClauseTermFromDB |
( |
void * |
ref | ) |
|
◆ Yap_FetchTermFromDB()
Term Yap_FetchTermFromDB |
( |
void * |
ref | ) |
|
◆ Yap_FindLUIntKey()
◆ Yap_init_tqueue()
◆ Yap_InitBackDB()
void Yap_InitBackDB |
( |
void |
| ) |
|
◆ Yap_LUInstance()
◆ Yap_new_ludbe()
◆ Yap_PopTermFromDB()
Term Yap_PopTermFromDB |
( |
void * |
ref | ) |
|
◆ Yap_Recordz()
Int Yap_Recordz |
( |
Atom |
at, |
|
|
Term |
t2 |
|
) |
| |
◆ Yap_ReleaseTermFromDB()
void Yap_ReleaseTermFromDB |
( |
void * |
ref | ) |
|
◆ Yap_StoreTermInDB()
DBTerm * Yap_StoreTermInDB |
( |
Term |
t, |
|
|
int |
nargs |
|
) |
| |
◆ Yap_StoreTermInDBPlusExtraSpace()
DBTerm * Yap_StoreTermInDBPlusExtraSpace |
( |
Term |
t, |
|
|
UInt |
extra_size, |
|
|
UInt * |
sz |
|
) |
| |
◆ Yap_unify_immediate_ref()
Int Yap_unify_immediate_ref |
( |
DBRef ref |
USES_REGS | ) |
|
|
CELL | Yap_EvalMasks (register Term tm, CELL *keyp) |
|
LogUpdClause * | Yap_new_ludbe (Term t, PredEntry *pe, UInt nargs) |
|
Int | Yap_Recordz (Atom at, Term t2) |
|
Int | Yap_unify_immediate_ref (DBRef ref USES_REGS) |
|
PredEntry * | Yap_FindLUIntKey (Int key) |
|
Int | Yap_db_nth_recorded (PredEntry *pe, Int Count USES_REGS) |
|
void | Yap_ErLogUpdCl (LogUpdClause *clau) |
|
void | Yap_ErCl (DynamicClause *clau) |
|
void | Yap_ErDBE (DBRef entryref) |
|
Term | Yap_LUInstance (LogUpdClause *cl, UInt arity) |
|
Term | Yap_FetchTermFromDB (void *ref) |
|
Term | Yap_FetchClauseTermFromDB (void *ref) |
|
Term | Yap_PopTermFromDB (void *ref) |
|
DBTerm * | Yap_StoreTermInDB (Term t, int nargs) |
|
DBTerm * | Yap_StoreTermInDBPlusExtraSpace (Term t, UInt extra_size, UInt *sz) |
|
void | Yap_init_tqueue (db_queue *dbq) |
|
void | Yap_destroy_tqueue (db_queue *dbq USES_REGS) |
|
bool | Yap_enqueue_tqueue (db_queue *father_key, Term t USES_REGS) |
|
bool | Yap_dequeue_tqueue (db_queue *father_key, Term t, bool first, bool release USES_REGS) |
|
void | Yap_ReleaseTermFromDB (void *ref) |
|
void | Yap_InitBackDB (void) |
|