COP 4020 Lecture -*- Outline -*- * databases (9.6) A sweet spot in programming language design seems to be relational data + inference, sometimes called "datalog" ------------------------------------------ DATABASES (9.6) Relational Database = set of relations Relation = set of tuples The Relational DB ADT Rel = {New RelationClass init} {Rel assertall(Ts)} -- puts all tuples in Ts in the DB {Rel assert(T)} -- puts the tuple T in the DB {Rel query(X)} -- looks up X (unifies with DB) Advantages of using logic programs for queries: - queries can use logical operations - rules can deduce consequences of data ------------------------------------------ Q: How would you implement the Relation ADT? use a mutable dictionary Q: What if the database was in XML format? you could still model it as data in Oz... ** example ------------------------------------------ EXAMPLE \insert 'RelationClass.oz' declare ClassRel = {New RelationClass init} {ClassRel assertall([name(cop3223 'C prog') name(cot3100 'intro to discrete') name(cop3502 'CS I') name(cop3330 'OOP and UML') name(cda3103 'comp. org.') name(cot3960 'foundation exam') name(cop3503 'CS II') name(cop3402 'systems software') name(cop4020 'prog lang I')])} Prereqs = {New RelationClass init} {Prereqs assertall([prereq(cop3502 cop3223) prereq(cop3330 cop3223) prereq(cda3103 cop3223) prereq(cop3960 cop3502) prereq(cop3960 cot3100) prereq(cop3503 cop3502) prereq(cop3503 cop3330) prereq(cop3503 cot3100) prereq(cop3402 cop3502) prereq(cop4020 cot3960) prereq(cop4020 cot3503)])} % What are the prerequisites of cop4020? {Test {SolveAll proc {$ ?Pre} {PrereQ cop4020 Pre} end} '==' [cot3960 cot3503]} FOR YOU TO DO % What are the names of the classes that precede 'prog lang I'? ------------------------------------------ See CSClasses.oz Q: How would you ask: What classes have the most prerequisites? Q: How would you ask: What are all the classes you take before cop4020? ** implementation This is a class, as in chapter 7 ------------------------------------------ %% Figure 9.11 in CTM declare % A choice between all elements in a list (the second argument) proc {Choose ?X Ys} choice Ys=X|_ [] Yr in Ys=_|Yr {Choose X Yr} end end class RelationClass attr d meth init d:={NewDictionary} end meth assertall(Is) for I in Is do {self assert(I)} end end meth assert(I) if {IsDet I.1} then Is={Dictionary.condGet @d I.1 nil} in {Dictionary.put @d I.1 {Append Is [I]}} else raise databaseError(nonground(I)) end end end meth query(I) if {IsDet I} andthen {IsDet I.1} then {Choose I {Dictionary.condGet @d I.1 nil}} else {Choose I {Flatten {Dictionary.items @d}}} end end end ------------------------------------------ Q: Why is IsDet being called? This is to do indexing, which is an optimization. Note that it doesn't change the answer, just helps find it faster.