cs240A Fall 2016
Hints of Hw1 corrections: Do Exercises 8.1, 8.2,
8.5, and 8.7 of ADS textbook. (hint: for part c.
Set difference is the only nonmonotonic
operation in our RA).
+ Notes on Aggregates and Stratification in DeALS
Deals Programs
1. cities which are reachable from Austin
% city(Name:string, State:string, Population:integer)
% distance(City1:string, City2:string, Distance:real)
database({city(Name:string, State:string, Population:integer),
distance(City1:string, City2:string, Distance:real) }).
fromaustin(City, D) <- distance('Austin', City, D).
fromaustin(C2, D) <- fromaustin(C1, D1), distance(C1, C2, D2), D=D1+D2.
export fromaustin(City, D).
2. Find which of the above city is the furthest from Austin.
%You can express this query using stratified negation.
maxdistance(C, D) <- fromaustin(C, D), ~further (D).
further(D) <- fromaustin(_, D1), D1 >D.
export leastdistance(C, D).
Aggregates in DeALS
%Find which of the above city is the furthest from
%Austin, using stratified negation.
maxdistance(C, D) <- fromaustin(C, D), ~further (D).
further(D) <- fromaustin(_, D1), D1 >D.
%OR… max or D grouped by C
maxdistance(max<D>) <- fromaustin(C, D).
%As in SQL the max and min allow you to compute the max
%distance, but then another rule is needed to find the city.
maxdistancecity(max<D>) <- maxdistance(D), fromaustin(C, D).
%Write a program to add up the population of cities in Texas.
texapop(sum<Pop>) <- city(Name, 'Texas', Pop).
Aggregates Require Stratification (typically)
%1. cities which are reachable from Austin
% city(Name:string, State:string, Population:integer)
% distance(City1:string, City2:string, Distance:real)
database({city(Name:string, State:string, Population:integer),
distance(City1:string, City2:string, Distance:real) }).
fromaustin(City, D) <- distance('Austin', City, D).
fromaustin(C2, D) <- fromaustin(C1, D1), distance(C1, C2, D2), D=D1+D2.
%A city can be reached along multiple paths. Let us find the shortest.
leastdistance(C, min<D>) <- fromaustin(C, D). %min D grouped by C
citydistance(C, D) <- fromaustin(C, D), leastdistance(C, D). %min D grouped
by C
maxdist(<D>) <- citydistance(C, D). %The max distance along the shortest
paths
maxdistcity(D) <- maxdist(D), citydistance(C, D). %the furthest city from
Austin
Beyond Stratification
• Queries with Stratified negation and aggregates are
required in SQL
• But for performance, it is essential that we can push
aggregates into recursive programs. Two solutions
• Non-Monotonic reasoning has seen much research in
the last 40+ years,
• Stable Models (a.k.a. answer set because there answer
might not be unique). Semantically clean but NP.
• Deal supports polynomial-time computable unique stable
models defined using the notion of:
1. XY-stratification (next slde) and
2. Monotonic Aggregates (later)
K-means using XY-stratification
Assume that initialpoints returns (Xc, Yc) for C = 1, …, K,
Y. Bu, V. Borkar, M. Carey, Joshua Rosen, N. Polyzotis, T. Condie, M. Weimer, R. Ramakrishnan:
Scaling Datalog for Machine Learning on Big Data.
Assignments
• Text, pdf, and docs are fine, avoid zip
• Avoid EXEC
• Runnin code is expected.
© Copyright 2026 Paperzz