Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Representation of a database

1. Tuple facts
2. Attribute facts
3. List of structures
NO 1
?employee1(N,100,P,S).

1. Tuple facts N = brain


P = operator
S = 20000
N = nancy
P = manager
S = 50000
 asserting each tuple as a facts
/* Name Dept Position Salary */
employee1(brain, 100, operator, 20000)
employee1(john, 200, manager, 50000)
employee1(nancy,100, manager, 50000)
EX NO 1
?print_countries.
predicates
country(symbol) english
print_countries france
clauses
germany
country(england).
country(france). denmark
country(germany).
country(denmark).
print_countries :- country(X),
write(X), /* write the value of X */
nl, /* start a new line */
fail.
print_countries.
2. Attribute facts

 asserting each attribute as a facts


 attributes can be brought together as rule NO 1
department(brain, 100).
department(nancy, 100). ?employee2(N,100,P,S).
position(brain,operator).
position(nancy,manager).
salary(brain,20000).
N = brain
salary(nancy,50000). P = operator
employee2(Name, Dept, Pos, Sal) S = 20000
:- department(Name, Dept), N = nancy
position(Name, Pos),
P = manager
salary(Name, Sal).
S = 50000
FACT
domains
thing = book(title,author) ; car(name); shoes

author = author(firstname,lastname)
person,title,name,firstname,lastname,shoes =
symbol
predicates
ownes(person,thing)
clauses
ownes(siri,book("AI book",author(tom,brook))).
ownes(siri,car(honda)).
ownes(siri,shoes).
goal
ownes(X,Y),write(X, “ owns “,Y,"\n "),fail.
LIST
List : simple data structure

L = [man, woman, boy, girl]

L = [a,b,c]
L = [Head|Tail]
L = [a|Tail]
Tail = [b,c]
L = [a,b,c]= [a|[b,c]] = [a,b|[c]]
domains
name = symbol
anylist = name*
print list forward
domains
name = symbol print_list([cat,dog,bird,chicken])
anylist = name* cat
dog
predicates bird
print_list(anylist) chicken

clauses
print_list([]).
print_list([H|T])
:- write(" "),
write(H," \n"),
print_list(T).
print list reverse
domains
print_list_rev([cat,dog,bird,chicken]
name = symbol )
anylist = name* chicken
predicates bird
print_list_rev(anylist) dog
clauses cat
print_list_rev([]).
print_list_rev([H|T])
:- print_list_rev(T),
write(" "),
write(H," \n").
Write a list
domains domains
list = integer* list = symbol*
/* or whatever type you wish to use */
/* or whatever type you wish to use */
predicates predicates
1 man
write_a_list(list) write_a_list(list)
clauses 2 woman
clauses
write_a_list([]). 3 write_a_list([]). girl
/* If the list is empty, do /* If the list is empty, do
nothing more............. */ nothing more....... */
boy
write_a_list([H|T]) :- write_a_list([H|T]) :-
/* Match the head to H and /* Match the head to H and
the tail to T, then...... */ the tail to T, then....... */

write(H), nl, write_a_list(T). write(H), nl,write_a_list(T).


goal goal
write_a_list([1, 2, 3]). write_a_list([man, woman,
girl, boy]).
Write a list in reverse order
domains 3
list = integer*
2
/* or whatever type you wish to use */
1
predicates
write_reverse_list(list)

clauses
write_reverse_list([]).
/* If the list is empty, do nothing more. */
write_reverse_list([H|T]) :-
/* Match the head to H and the tail to
T, then... */
write_reverse_list(T), nl,
write(H).

goal
write_reverse_list([1, 2, 3]).
Member
namelist = name* = list name = symbol
domains

namelist = name*
name = symbol NO 1

predicates ?is_a_member_of(man,
[woman,man, boy])
is_a_member_of(name,namelist)
clauses Yes
is_a_member_of(Name,[Name|_]).
/****/
is_a_member_of(Name,[_|Tail])
:- is_a_member_of(Name,Tail).
/****/
check member
domains
namelist = name* member(bird,
name = symbol [cat,dog,bird,chicke
yes n])
predicates
member(name,namelist)

clauses member(rose,
member(Name,[Name|_]). [cat,dog,bird,chicken])
no
/* find the member in the list */
member(Name,[_|Tail])
:- member(Name,Tail).
find the word in the list
domains NO 1
? findword(house,[cat,is,in,the,house])
namelist = name*
name = symbol house is in the list.

NO 2
predicates
? findword(dog,[cat,is,in,the,house])
findword(name,namelist)
dog not found.
clauses
findword(Name,[]):- write(Name), write(“ not found.”),nl./*1*/
findword(Name,[Name,_]):- write(Name), write(“ is in the
list.”),nl. /*2*/
findword(Name,[_|Tail]) :- findword(Name,Tail). /*3*/
NO 1
? one_tuple_1(e(N,100,P,S),

3. List of structures
[e(brain, 100, operator, 20000),
e(john, 200, manager, 50000),
e(nancy,100, manager, 50000)])
N = brain
 asserting a stream of tuples in a LIST Structure
/* Name Dept Position Salary */ P = operator
[e(brain, 100, operator, 20000), S = 20000
e(john, 200, manager, 50000),
N = nancy
e(nancy,100, manager, 50000)]
one_tuple_1(e(N,D,P,S),[e(N,D,P,S)|Tail]). P = manager
/*1*/ S = 50000

one_tuple_1(e(N,D,P,S),[e(_,_,_,_)|Tail])
:- one_tuple_1(e(N,D,P,S), Tail). /*2*/
check member predicate

domains
thing = book(title,author)
namelist = thing*
title, author = symbol
predicates
member(thing,namelist)

clauses
member(Name,[Name|_]). /* find the member in the list */
member(Name,[_|Tail]) :- member(Name,Tail).
/*
member(book(tiger,jonh),[book(cat,jim),book(tiger,john),
book(bird,jane)]).
*/
Find length of list

domains NO 1.

list = integer* ? Length_of([1,2,5,6,7],What)

predicates
length_of(list, integer) What = 5

clauses NO2.

length_of([], 0). /*….*/ ?length_of([122,123,111],9)

length_of([_|T], L) :- No
length_of(T,TailLength),

L = TailLength + 1.
Append
domains NO 1

integerlist = integer* ?append ([1], [2,3,4], What)

What = [1,2,3,4]
predicates
NO 2
append(integerlist,
?append ([1,2,3], [4,5,6,7],What)
integerlist,integerlist)
What = [1,2,3,4,5,6,7]
clauses
append([],List,List).
append([X|L1],List2,[X|L3])

:- append(L1,List2,L3).
Find average age
domains
name, address = string L = keep age list
age = integer Sum= sum all ages
list = age*
predicate
N = list length of L
person(name, address, age) Ave = average age value
sumlist(list, age, integer)
goal
findall(Age, person(_, _, Age), L),
sumlist(L, Sum, N),
Ave = Sum/N,
write("Average =", Ave), nl.
clauses
sumlist([], 0, 0). /****/
sumlist([H|T], Sum, N) :- sumlist(T, S1, N1),

Sum = H +S1, N =1+N1. /****/


person("Sherlock Holmes", "22B Baker Street", 42).
person("Pete Spiers", "Apt. 22, 21st Street", 36).
person("Mary Darrow", "Suite 2, Omega Home", 51).
Read Integer into the list
domains
list = integer*
predicates
readlist(list)
goal
makewindow(1, 7, 7, " Integer List ", 5, 5, 15, 70),
write(" Type in a column of integers, like this:",
"\n\n integer (press ENTER)\n integer (press ENTER)\n",
" etc.\n\n Type X (and press ENTER) to end the list.\n\n"),
readlist(TheList),
write("\nThe Turbo Prolog list is: ",TheList).
clauses
readlist([H|T]) :-
write("\16 "), /* This prints the prompt symbol */
readint(H), !,
readlist(T).
readlist([]).
Write Character list
domains
charlist = char*

predicates
string_chlist(string, charlist)

clauses
string_chlist("", []).
string_chlist(S, [H|T]) :-
frontchar(S, H, S1),
write(" * ",H," * \n "),
string_chlist(S1, T).
Writelist
domains
integerlist = integer*
predicates
writelist(integerlist)
write5(integerlist, integer)
clauses
writelist(NL ) :- nl, write5(NL, 0 ), nl.
write5(TL, 5 ) :- !, nl, write5(TL, 0).
write5([H|T], N ) :- !, write(H," "), N1=N+1,
write5(T, N1).
write5([], _ ).

You might also like