Datalog 101
shape(▲, triangle).
shape(▶, triangle).
shape(■, rectangle).
shape(●, circle).
corners(triangle, 3).
corners(rectangle, 4).
corners(circle, 0).
query(C,S) :- shape(S, T), corners(T,C).
query(3, X)?
query(3, X)?
% use query(C, S) :- shape(S, T), corners(T, C).
% Unification: C = 3, S = X
query(3,X) :- shape(X, T), corners(T,3).
% use corners(triangle, 3).
% Unification: T = triangle
query(3,X) :- shape(X, triangle).
% use shape(▲, triangle).
% Unification: X = ▲
query(3, ▲).
% Unsafe rule:
foo(A, B) :- bar(A). % What 'B'? What do you want?!
% Another unsafe rule:
foo(A, B) :- bar(A), ~quux(B). % How do I even?
employee(ID, N, D)
Maps naturally to relational:
TABLE Employee (
Employee_Id int,
Name varchar(255),
Department_Id int,
)
And with very little effort to XQuery or RDF.
edge(a, b).
edge(b, c).
edge(c, d).
edge(d, a).
path(X, Y) :- edge(X, Y).
path(X, Y) :- edge(X, Z), path(Z, Y).
path(X, Y)?
Probabilistic Datalog
coin(c1).
result(c1, heads).
result(c1, tails).
result(c1, X)?
result(c1, heads).
result(c1, tails).
coin(c1).
result(c1, heads) [x=1].
result(c1, tails) [x=2].
@uniform p(x).
result(c1, X)?
% iterations: 1000
% root-mean-square error: 0.01570031846810759
result(c1, heads). % p = 0.499
result(c1, tails). % p = 0.501
% Known facts:
name(purple, "Brend") [u=1].
name(orange, "Jan") [u=2].
address(purple, "b.wanders@utwente.nl") [v=1].
address(orange, "jan.flokstra@utwente.nl") [v=2].
% Reconstruct authors:
author(ID, N, A) :- name(ID, N), address(ID, A) [z=2].
author(solo, N, A) :- name(_, N), address(_, A) [z=1].
% Detail predicates for querying:
actual_name(P, N) :- author(P, N, _).
actual_address(P, A) :- author(P, _, A).
actual_person(P) :- author(P, _, _).
/