CSc 8710 Fall 2005
Homework 3 (Due: October 12, 2005 - Wednesday)

Note: displayRelation, rename, and project are due: Monday, October 3rd.

Implement the Relational Algebra operations (rename, project, union, minus, intersect, select, join, and times) in Prolog. Each operator will be provided with an OutputRelationName by the user, the name for the output relation. Your program should create a "relation" fact for the output relation (using the assert predicate of SWI-Prolog). We will assume that the user does not provide the same names twice in a session.

You should also implement a displayRelation(Rname) predicate that displays the relation with name Rname on the terminal as follows:

$ pl
Welcome to SWI-Prolog (Multi-threaded, Version 5.4.7)
Copyright (c) 1990-2003 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- ['hw3.pl'].
% hw3.pl compiled 0.01 sec, 17,972 bytes

Yes
?- displayRelation(suppliers).
suppliers(sno:string,sname:string,city:string)

Number of Tuples=3
S1:Jones:Atlanta:
S2:Smith:Duluth:
S3:Blake:Marietta:

Yes
?- halt.
$
Assume the following data structure to store relations and their schemas in Prolog's memory:
?-dynamic(relation/4).
relation(c1,[city],[string],[["Atlanta"],["Duluth"],["Wichita"],["Dodge City"]]).
relation(c2,[city],[string],[["Carson City"],["Duluth"],["Chennai"]]).
relation(
  suppliers,
  [sno,sname,city],
  [string,string,string],
  [["S1","Jones","Atlanta"],
   ["S2","Smith","Duluth"],
   ["S3","Blake","Marietta"]]
).
relation(
  parts,
  [pno,pname,color,city],
  [string,string,string,string],
  [["P1","Nut","Red","Duluth"],
   ["P2","Bolt","Blue","Marietta"],
   ["P3","Screw","Red","Dunwoody"],
   ["P4","Axle","Green","Marietta"]]
).
relation(
  projects,
  [jno,jname,city],
  [string,string,string],
  [["J1","Bicycles","Duluth"],
  ["J2","Motorcycles","Dunwoody"]]
).
relation(
  spj,
  [sno,pno,jno,qty],
  [string,string,string,number],
  [["S1","P1","J1",100],
   ["S1","P2","J2",100],
   ["S1","P1","J2",100],
   ["S2","P1","J1",100],
   ["S2","P3","J1",100],
   ["S2","P3","J2",100],
   ["S3","P1","J1",100],
   ["S3","P2","J2",100]]
).
Some details of the various operations to be implemented:
  1. rename(Rname,Attrs,OutputRelationName)
    Semantic Checks: (1) No duplicates in Attrs and (2) Attrs has same number of attributes as that of relation with name Rname.
    Example Usage:
    ?- rename(suppliers,[s,n,c],temp1), displayRelation(temp1).
    ?- rename(suppliers,['S','N','C'],temp2), displayRelation(temp2).
  2. project(Rname,Attrs,OutputRelationName).
    Semantic Checks: (1) No duplicates in Attrs and (2) Attrs contains only attributes from the attributes of Rname.
    Example Usage:
    ?- project(suppliers,[sno,city],temp1), displayRelation(temp1).
  3. select(Rname,Condition,OutputRelationName).
    Note: Condition is a list of atomic condition; each atomic condition is a list [X,op,Y], where X and Y may be attribute names or constants and op can be one of the six operators: =, <>, <, >, <=, >=.
    Semantic Checks: (1) attributes appearing in Condition also appear in attributes of relation Rname and (2) Type Checks.
    Example Usage:
    ?- select(suppliers,[[sno,=,"S1"],[city,=,"Duluth"]],temp), displayRelation(temp).
  4. unionRA(Rname1,Rname2,OutRelationName).
    Semantic Checks: Compatible schemas (same number of attributes and same data types for corresponding attributes).
    Example Usage:
    ?- unionRA(c1,c2,temp1), displayRelation(temp1).
    ?- project(suppliers,[city],t1), project(suppliers,[city],t2), unionRA(t1,t2,t3), displayRelation(t3).
  5. minus - similar to unionRA
  6. intersect - similar to unionRA
  7. times(Rname1,Rname2,OutputRelationName).
    Semantic Checks: None (Note: while naming attributes in resulting relation resolve conflict of names by prefixing the name with the relation name. For example when you take cartesian product - times - of suppliers and parts you should name the city column in suppliers by supplierscity and that in parts by partscity.
    Example Usage:
    ?- times(suppliers,parts,temp), displayRelation(temp).
  8. join(Rname1,Rname2,OutputRelationName).
    Semantic Checks: None.
    Example Usage:
    ?- join(suppliers,spj,temp), displayRelation(temp).