0% found this document useful (0 votes)
264 views

Database Handling in Prolog: Type1: Created at Each Execution. It Grows, Shrinks and

Two types of databases exist in Prolog: static and dynamic. Static databases are compiled with the program and do not change during execution. Dynamic databases can change at runtime and include working memory databases that exist only for a single execution and database files that can be loaded, updated, and saved between program runs using predicates like consult, save, assert, and retract.

Uploaded by

Parinay Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
264 views

Database Handling in Prolog: Type1: Created at Each Execution. It Grows, Shrinks and

Two types of databases exist in Prolog: static and dynamic. Static databases are compiled with the program and do not change during execution. Dynamic databases can change at runtime and include working memory databases that exist only for a single execution and database files that can be loaded, updated, and saved between program runs using predicates like consult, save, assert, and retract.

Uploaded by

Parinay Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Database handling in Prolog

Two types of databases :- static and dynamic.


● Static database is a part of the program that is
complied along with it. It does not change during
execution of the program.
● Dynamic database can change dynamically at
execution time and are of two types.
Type1: created at each execution. It grows, shrinks and
is deleted at the end of program.
● This type of database is no longer available after program
finishes its execution and is called working memory.
Cont…
Type2: Other type of dynamic databases are those
which are stored in files and called database files.
− These are consulted in any program whenever required.
− These types of databases are not part of any particular
program and are available for use in future by different
programs using system defined predicates called save
and consult.
− While executing a Prolog program one can load database
file(s) using 'consult' predicate.
− These files can be updated dynamically at run time and
saved using 'save' predicate.
Cont…
● The format of predicates 'save' and 'consult' are as
follows:
− save(filename) - succeeds after saving the contents of a file
named 'filename'.
− consult(filename) - succeeds after loading or adding all the
clauses from a file stored in 'filename' in the current program
being executed.
− reconsult(filename) - succeeds after loading all the clauses by
superseding all the existing clauses for the same predicate
from a file 'filename'.
● Grouping of rules and facts into partitions can be easily.
● Example: Load a file named 'more' if predicates P and
Q succeed,
R :- P, Q, consult('more').
Cont…
● Clauses can be added to a database at run time using
following predicates.
asserta(X) & assertz(X) - succeed by adding fact X in
the beginning & at the end of database of facts
respectively.
● For example, asserta(father(mike, john)) adds fact
father(mike, john) in the beginning of current database.
● Clauses can be constructed dynamically and asserted
in a dynamic database as follows:
start :- writeln('Input name of mother: '), readln(M),
writeln('Input name of child: '), readln(C),
assert(parent(M, C)), assert(female(M)).
Cont…
● Similarly obsolete clauses can be deleted by using
system defined predicate called retract from dynamic
database at the run time.
● For example, retract(father(mike, X)) deletes the first
fact father(mike, _) from working memory.
● retractall(X) deletes all the clauses from a database
whose head match with X.
● Example, retractall(father(X, Y)) deletes all the facts
father( _ , _ ) and retractall( _ ) deletes all the clauses
from the working memory.

You might also like