Search code examples
databasedatalog

datalog for finding same value of attributes from a model


I am new to datalog and want to solve the following:

I have a student model

student(name, rollno, city)

Now I want to write a datalog program for finding the pair of students that are from the same city. Is it correct if I write the program as following?

result(n1, r1, c1, n2, r2, c2) :- student(n1, r1,c1) AND student(n2,r2,c2) AND c1= c2

As r1 and r2 are not of much importance here can i replace it by _ in the both head and the body?

Any feedback is welcome!


Solution

  • It is correct to use underscore in place of the rollno in the right-hand side of your logic.

    I would have picked a slightly different predicate style to hold the results. I write in the LogicBlox variant of Datalog for work and I think our notation varies from yours a bit, but here is how I would do it...

    student(name, rollno, city) -> string(name), string(rollno), string(city).
    
    studentsPairsFromSameCity(nameA, nameB, city) -> string(nameA), string(nameB), string(city).
    
    studentPairsFromSameCity(nameA, nameB, city)
    <-
       student(nameA, _, cityA),
       student(nameB, _, cityB),
       nameA != nameB,
       cityA = cityB.