Is it possible in Datalog to write a query for facts where there is exactly one value for one of the variables, for each possible value of the other variables?
e.g. find all X
such that there is only one X
for each Y
in expr(X, Y)
Is it possible in Datalog to write a query for facts where there is exactly one value for one of the variables, for each possible value of the other variables?
e.g. find all X
such that there is only one X
for each Y
in expr(X, Y)
In plain Datalog you can express this by first computing the Y's that have more than one X, and then using that to compute the Y's with 1 X.
problem(y) <- expr(x1, y), expr(x2, y), x1 != x2.
exactly_one_opt1(y) <- expr(_, y), !problem(y).
Most systems also support aggregations that will probably be a more efficient solution, but aggregation syntax is not standard Datalog. I'm using the LogiQL (LogicBlox Datalog) syntax here:
count[y] = c <- agg<<c = count()>> expr(_, y).
exactly_one_opt2(y) <- count[y] = 1.