Prolog Databases Lab #6
LAB # 6
Write programs to implement a database in prolog.
Databases in Prolog are a bit different from other (procedural) databases. In Prolog there are two types of databases: static database and dynamic database .
A static database is one whose component’s (field records) information, as well as rules about their relationship, are stored in memory at run time.
A dynamic database is composed of facts gathered by the user during program execution. These facts are stored in a separate (dynamic) database. A dynamic database is stored in memory along with a static database – more memory is needed here.
You can name facts section (which creates a corresponding internal domain); the default domain for (unnamed) facts sections is dbasedom . Your program can have multiple facts sections, but each one must have a unique name. You can declare a given facts predicate in only one facts section.
With the standard predicates assert , asserta , assertz , and consult , you can add facts to the facts section at run time. You can remove such facts at run time with the standard predicates retract and retractall .
The save predicate saves facts from a facts section to a file (in a specific format).
You can create or edit such a fact file with an editor, and then insert facts from the file into your running program with consult .
You can call database predicates in your program just like you call other predicates.
You can handle facts as terms when using the domain internally generated for a database section.
EXAMPLE 6.1
DOMAINS
screenname, fname, type = symbol
row,col,len = integer
DATABASE
CE-415:Artificial Intelligence
27
Prolog Databases Lab #6
screen(symbol,dbasedom) screens */
DATABASE - screen
/* Saving different
field(fname, type, row, col, len)
I/O field on screen */
txtfield(row, col, len, string)
/* Definitions of
/* Showing textfields */
windowsize(row,col)
PREDICATES
shiftscreen(symbol)
CLAUSES
shiftscreen(_):- retract(field(_,_,_,_,_)), fail.
shiftscreen(_):- retract(txtfield(_,_,_,_)), fail.
shiftscreen(_):- retract(windowsize(_,_)), fail.
shiftscreen(Name):- screen(Name,Term), assert(Term), fail. shiftscreen(_).
GOAL
shiftscreen(person).
EXAMPLE 6.2
DATABASE
person(string,integer,real)
PREDICATES
run
CE-415:Artificial Intelligence
28
Prolog Databases Lab #6
CLAUSES
person("Pete Ashton",20,11111.111).
person("Marc Spiers",32,33333.333).
person("Kim Clark",28,66666.666).
run:-
% Name is left-justified, at least 15 characters wide
% Age is right-justified, 2 characters wide
% Income is right-justified, 9 characters wide, with
2
% decimal places, printed in fixed-decimal notation person(N, A, I), writef("Name= %-15, Age= %2, Income= $%9.2f
\n",N,A,I), fail
; true.
GOAL
run.
1.
Type the above examples in Visual Prolog environment and execute them individually.
2.
Explain the working of the above examples.
3.
What types of databases are used in the above programs?
4.
Make a Prolog program, which takes a date in number format and prints it back in word format, e.g.:
INPUT: 22-07-2001, OUTPUT: 22nd July, 2001 .Use Prolog database to implement it.
CE-415:Artificial Intelligence
29