Documente Academic
Documente Profesional
Documente Cultură
1 Datalog {1{
CZ
took
Name Course Grade
"Joe Doe" cs123 2.7
"Jim Jones" cs101 3.0
"Jim Jones " cs143 3.3
"Jim Black" cs143 3.3
"Jim Black" cs101 2.7
Rules
How to express logical conjunction:
Find the name of junior-level students who have taken
both cs101 and cs143
QUERIES
A closed query; the answer to such query is either yes or not. For
instance,
?firstreq(\Jim Black")
An open query:
?firstreq(X)
firstreq(\Jim Jones")
firstreq(\Jim Black")
Negation in Datalog
Only negated goals are allowed. Negated heads are not
Junior-level Students who did not take course cs143
Additional Operators
Addditional operators of frequent use can be derived from these.
For instance, we have join, semijoin, intersection, division and
generalized projection.
The join operator can be constructed using Cartesian prod-
uct and selection. In general, a join has the following form:
R 1 S , where F = $i11$j1 ^ : : : ^ i $j ; i1; : : : ; i are
F k k k k
Therefore,
R 1 S = 0 (R S )
F F
Relational Operators|Cont
Selection. R denotes the selection on R according to the
F
Relational Operators
Cartesian product. The Cartesian product of R and S is
denoted R S .
RS =
ftj(9r 2 R)(9s 2 S )(t[1; : : : ; n] = r ^ t[n + 1; : : :; n + m] = s)g
If R has n columns and S has m columns, then RS contains
all the possible m + n tuples whose rst m components form
a tuple in R and the last n components form a tuple in S .
Thus, R S has m + n columns and jRjjS j tuples, where
jRj and jS j denote the respective cardinalities of the two
relations.
Projection. Let R be a relation with n columns, and L =
$1; : : : ; $n be a list of the columns of R. Let L be a sublist 0
follows:
0 R = fr[L ] j r 2 Rg
L
0
Commercial DB Languages
The actual query languages of commercial RDMS are largely
based on the formal query languages just discussed. For instance:
Relational DB Languages
The dierences between the various languages so far dened does
not really impact their ultimate expressive power
TRC and DRC are equivalent, and there are mappings that
transform a formula in one language into an equivalent one
in the other.
Also for each TRC or DRC expression there is an equivalent,
nonrecursive Datalog program. The converse is also true,
since a nonrecursive Datalog program can be mapped into an
equivalent DRC query.
9M (student(N; M; junior)) g
The query ?scndreq(N) can be expressed as follows:
Mapping|cont
Step 2 The body of a rule r is translated into the RA expression
Bodyr. Bodyr consists of the cartesian product of all the
base or derived relations in the body, followed by a selection
, where F is the conjunction of the following conditions:
F
(i) inequality conditions for each such goal (e.g., Z > 24:3),
(ii) equality conditions between columns containing the same
variable, (iii) equality conditions between a column and the
constant occurring in such a column:
For the example at hand, (i) the condition Z > 24:3 trans-
lates into the selection condition $5 > 24:3, while (ii) the
equality between the two occurrences of X translates into
$1 = $2, while the equality between the two Y s maps into
$3 = $4, and (iii) the constant in the last column of p maps
into $6 = a. Thus we obtain:
Bodyr = $1=$2 $3=$4 $6=a $5
; ; ; >24:3 (Q P)
Step 3 Each rule r is translated into an extended projection on
Bodyr, according to the patterns in the head of r. For the
rule at hand we obtain:
S = $5 b $5(Bodyr)
; ;
Step 4 Multiple rules with the same head are translated into
union or their equivalent expressions.
Is translated into:
Safe Datalog
The following is an inductive denition of safety for a program P :
is safe.
Safety
In practical languages, it is desirable to allow only
safe formulas, which avoid the problems of innite
answers, and loss of domain independence.
But the problems of domain independence and
niteness of answers are undecidable even for non-
recursive queries. Therefore, necessary and su-
cient syntactic conditions that characterize safe
formulas cannot be given in general.
In practice, therefore, sucient conditions are de-
ned that might be a more restrictive than neces-
sary.
Unsafe Rules
For instance, to nd grades better than the grade Joe Doe got in
cs143, a user might write the following rule:
1. Innite answers. Assuming that, say Joe Doe got the grade
of 3:3 (i.e., B+) in course cs143, then, there are innitely
many numbers that satisfy the conditions of being greater
than 3:3.
2. Lack of domain independence. A query formula is said to
be domain independent when its answer only depends on
the database and the constants in the query, but not on the
domain of interpretation. The set of values for G1 satisfying
the rule above depends on what domain we assume for num-
bers: e.g., integer, rational or real. Thus there is no domain
independence.
3. No relational algebra equivalent. Only database relations
are allowed as operand of a relational algebra expressions.
These relations are nite, and so is the result of every RA ex-
pression over these relations. Therefore, there cannot be any
RA expression over the database relations that is equivalent
to the rule above.
Zaniolo|Ceri|Faloutsos|Snodgrass|Subrahmanian| Zicari|All Rights Reserved
Advanced Database Systems Morgan Kaufmann Copyright
c 1997
Sec: 8.6 Stratication {7{
CZ
Stratication
By sorting on pdg (P ), the nodes of P can partitioned into a
nite set of n strata 1; :::; n, such that, for each rule r 2 P , the
predicate-name, of the head of r belongs to a stratum that
(i) is to each stratum containing some positive goal, and also
(ii) is strictly > than each stratum containing some negated goal.
One-at-the-Time
Set aggregates, such as count or sum, in SQL, require that the
element of a set be visited one-at-the-time. (These aggregates also
require arithmetic predicates, that we will consider later.)
Counting the elements in a set modulo an integer does not require
arithmetic, but still requires the elements of the set be visited one-
at-the-time.
The parity query: how many tuples in the base re-
lation? br(X)
between(X; Z) br(X); br(Y); br(Z); X < Y; Y < Z:
next(X; Y) :
br(X); br(Y); X < Y; between(X; Y):
next(nil; X) :
br(X); smaller(X):
smaller(X) br(X); br(Y); Y < X:
even(nil):
even(Y) odd(X); next(X; Y):
odd(Y) even(X); next(X; Y):
br is even :
even(X); next(X; Y):
The maximum time required for basic subparts of the given as-
sembly
howsoon(AssPart; Time) timeForbasic(AssPart; ; Time);
:larger(AssPart; Time):
larger(Part; Time) timeForbasic(Part; ; Time);
timeForbasic(Part; ; Time1);
Time1 > Time:
Subparts
Expressive Power
1. Expressive Power of a language: the set of functions that can
be written as programs of the language.
2. Data Complexity: query languages are viewed as mappings
from the DB to the answer. The big O is evaluated in terms
of the size of the database, which is always nite.
Lists
[ ] is the empty list.
[HeadjTail] represents a non-empty list.
[mary; mike; seattle]
[mary; [mike; [seattle; [ ]]]]
A list-based representation for suppliers of top tube
;
part sup list(top tube [cinelli columbus mavic]) ; ; :
every model M of P .0
Models of a Program
Let I be an interpretation for a program P . If an atom a 2 I we
say that a is true, otherwise we say that a is false. Conversely for
negated atoms :a.
Satisfaction: A rule r 2 P is said to hold true in interpretation
I , or to be satised in I , if every instance of r is satised in I .
Model. An interpretation I that makes true all rules P is called
a model for P .
I is a model for P i it satises all the rules in ground(P ).
ground(P ) = fground(r) j r 2 P g
Examples
Positive Programs
A denite clause with an empty body is called a unit clause.
It is customary to use the notation \A:" instead of the more
precise notation \A :" for such clauses.
A fact is a unit clause without variables.
A unit clause (everybody loves himself) and three
facts
loves(X X) ; :
;
loves(marc mary) :
loves(mary tom); :
hates(marc tom); :
A positive logic program is a set of denite clauses.
We will use the terms denite clause program and positive pro-
gram as synonyms.
Example:
1. Constants
2. Variables: in addition identiers beginning with upper case,
x, y and z also represent variables in this section.
3. Functions, such as f (t1 ; :::; tn ) where f is an n-ary functor
and t1; :::; tn are the arguments.
4. Predicates
5. Connectives. These include basic logical connectives _, ^,
: and the implication symbol , !, and $.
6. Quantiers. The existential quantier 9 and the universal
quantier 8.
7. Parentheses and punctuation symbols, used liberally as needed
to avoid ambiguities.
nk
:::
TP"n (I ) = TP (TP"n(I ))
+1
I = TP (I )
:::
rj : 0Q0 ::: Qj 0
Qk; ck
:::
r2 : 0 Q0 c0; Q1; c1; Q2; ::: Qk; ck
Rule r2;2 produces only `old' values, and can thus be eliminated.
All is left is rules r1 and r2;1 , below:
"Pn(;) = TP"n(;)
1. M0 = ; and
2. Mj = "P! (Mj 1).
j
Programming in Prolog
A solution to the previous problems is to put the exit rule before
the recursive one.
anc(X Y); parent(X Y); :
anc(X Z); ; ;
anc(X Y) parent(Y Z) ; :
Prolog loops after the generation of all the results. To make things
work parent must be put before anc in the recursive rule. A skill
not hard to learn.
In many cases, however, reordering rules and goals does avoid
innite loops.
anc(X Y) ; ; :
parent(X Y)
anc(X Z) ; ; ; ; :
anc(Y Z) anc(X Y)
Prolog
Depth-rst exploration of alternatives, where goals are al-
ways chosen in a left-to-right order and the heads of the
rules are also considered in the order they appear in the
program.
The programmer is given responsibility for ordering the rules
and their goals in such a fashion as to guide Prolog into suc-
cessful and ecient searches.
The programmer must also make sure that the procedure
never falls into an innite loop.
Refutation
S = fF1; :::; Fng is a nite set of closed formulas, then F is a
logical consequence of S i F1 ^ ::: ^ Fn ! F is valid.
Theorem: Let S be a set of closed formulas and F be a closed
formula. Then F is a logical consequence of S i S [ f:F g is
unsatisable.
Thus to prove a goal G from a set of rules and facts P we simply
have to prove that P [ f Gg is unsatisable|i.e., we have to
refute P [ f Gg.
Resolution theorem proving that exactly that. It refutes the goal
list.
Prolog can be viewed in that light. But in fact there is no real
refutation|just procedural composition via unication.
The term SLD stands for Selected literal Linear resolution (or
refutation) strategy over Denite clauses.
Satisability
Let S be a set of closed formulas. We say that
S is satisable there is an interpretation which is a model for S .
S is valid if every interpretation of L is a model for S .
S is unsatisable if it has no models.
Theorem: Let S be a set of clauses. Then S is unsatisable i
S has no Herbrand models.
Let S be a set of closed formulas and F be a closed formula of a
rst order language L. We say F is a logical consequence of S if,
every interpretation of L that is a model for S is also a model for
F.
Note that if S = fF1; :::; Fng is a nite set of closed formulas,
then F is a logical consequence of S i F1 ^ ::: ^ Fn ! F is
valid.
Equivalent Semantics
Theorem: The success set of a program is equal to its least
Herbrand model.
Success Set
SLD-derivations can be nite or innite.
{ A nite SLD-derivation can be successful or failed.
{ A successful SLD-derivation is a nite one that ends in
the empty clause. This is also called an SLD-refutation.
{ A failed SLD-derivation is a nite one that ends in a non-
empty goal, where the selected atom in this goal does not
unify with the head of any program clause.
Denition Let P be a program. The success set of P is the
set of all A 2 BP such that P [f Ag has an SLD-refutation
(i.e., there exist some successful derivation for it).
innite q(X,a)
failure
.
p(b,b)
@@
1 @@ 2
@@
q(b,Z),
. p(Z, b) X/a
Figure: A nite SLD-tree. denotes success.
Examples of SLD-Resolution
Any realization of the top-down evaluation procedure will have to
make two choices at each step by selecting
In general, there will be more than one goal and many rules to
choose from. The choice aects the eciency of the deduction
process and also the actual result when the search falls into an
innite loop.
PROLOG interpreters usually choose goals in a left-to-right
order and rules in a sequential order that corresponds to
a depth-rst search of the SLD-tree with backtracking when
failure occurs. Thus, PROLOG treats the goal list as a stack
onto which goals are pushed or popped, depending on success
or failure.
SLD-resolution. Example
;
s(X Y) ; ;
p(X Y) q(Y) :
; :
p(X 3)
q(3) :
q(4) :
1. The initial goal list is
s(5; W)
2. This unies the head of the rst rule with mgu: fX=5; Y=W g.
New goal list
p(5; W); q(W)
SLD-Resolution
A rule r : A B1 ; : : : ; Bn , and
A query goal g , r and g have no variables in common.
If 9 a most general unier (mgu) for A and g , the goal list:
B1; : : : ; Bn:
is called resolvent of r and g .
SLD-Resolution Algorithm:
Input: A rst-order program P and a goal list G.
Output: A G that was proved from P , or failure.
begin Set Res = G;
While Res is not empty, repeat the following:
Choose a goal g from Res;
Choose a rule A B1 ; : : : ; Bn (n 0) from P
such that A and g unify under the mgu ,
(renaming the variables in the rule as needed);
If no such rule exists, then
output failure and exit.
else Delete g from Res;
Add B1 ; : : : ; Bn to Res;
Apply to Res and G;
If Res is empty then output G
end
Zaniolo|Ceri|Faloutsos|Snodgrass|Subrahmanian| Zicari|All Rights Reserved
Advanced Database Systems Morgan Kaufmann Copyright
c 1997
Sec: 9.4 Top-Down Execution {5{
CZ
Unication
A substitution is called a unier for (i.e., they cannot be
made identical)
two terms A and B if A = B.
Example The two terms p(f (x); a); and p(y; f (w)) are not
uniable, because the second arguments cannot be unied
The two terms p(f (x); z ); and p(y; a) are uniable, since =
fy=(f (a); x=a; z=a)g is a unier.
Composition of Substitutions
Let = fu1=s1; : : : ; um=smg and = fv1=t1 ; : : : ; vn=tng be sub-
stitutions.
Then the composition of and is the substitution obtained
from the set
fu1 =s1; : : : ; um=sm; v1=t1 ; : : : ; vn=tng
by deleting any binding ui=si for which ui = si and deleting
any binding vj =tj for which vj 2 fu1; : : : ; umg.
Example
Let = f(x=f (y ); y=z )g and = fx=a; y=b; z=y g.
Then = fx=f (b); z=y g.
x=f (y) x=a x=f (b)
y=z y=b y=y
z=z z=y z=y
Substitutions
Substitutions: A substitution is a nite set of the form fv1=t1; : : : ; vn=tng,
where each vi is a distinct variable, and each ti is a term distinct
from vi. Each ti is called a binding for vi.
The substitution is called a ground substitution if every ti is a
ground term. (Then X= is an instantiation of X to .)
E denotes the result of applying the substitution to E; i.e.,
of replacing each variable with its respective binding. For in-
stance, if E = p(x; y; f (a)) and = fx=b; y=xg. Then E =
p(b; x; f (a)). If
= fx=cg, then E
= p(c; y; f (a)).
Thus variables that are not part of the substitution are left un-
changed.
Right-Linear Rules
Consider now the right-linear formulation of ancestor:
Right-linear rules for the descendants of Tom
anc(Old; Young) parent(Old; Young):
anc(Old; Young) parent(Old; Mid); anc(Mid; Young):
?anc(tom; Desc)
anc(Old=tom; Young) parent(Old=tom; Young):
anc(Old=tom; Young) anc(Old=tom; Mid); parent(Mid; Young):
?grandma(GM; marc)
Benets of Memorizing
People who are of the same generation through common an-
cestors who are less than 12 levels remote and always lived
in the same state
?stsg(marc; 12; Z):
stsg(X; K; Y) parent(XP; X); K > 0; KP = K 1;
born(X; St); born(XP; St);
stsg(XP; KP; YP);
parent(YP; Y):
stsg(X; K; X):
Since the rst two arguments of stsg are bound, The supplemen-
tary magic method for this example is:
m:stsg(marc; 12):
spm:stsg(X; K; XP; KP) m:stsg(X; K);
parent(XP; X); K > 0; KP = K 1;
born(X; St); born(XP; St):
m:stsg(X; K) spm:stsg(X; K; XP; KP):
stsg(X; K; X) m:stsg(X; K):
stsg(X; K; Y) stsg(XP; KP; YP); spm:stsg(X; K; XP; KP);
parent(YP; Y):
m:sg(marc):
m:sg(XP) m:sg(X); parent(XP; X):
spm:sg(X; XP) parent(XP; X); m:sg(X):
sg0(X; X) m:sg(X):
sg0(X; Y) sg0(XP; YP); spm:sg(X; XP); parent(YP; Y):
%sg (X; Y)
0
parent(XP; X); sg (XP; YP); parent(YP; Y); m:sg(X):
0
?sg0(marc; Z):
m:sg(marc):
spm:sg(X; XP) m:sg(X); parent(XP; X)
m:sg(XP) spm:sg(X; XP):
?sg(marc; Who):
sg(X; Y) parent(XP; X); sg(XP; YP); parent(YP; Y):
sg(A; A):
sg (X; X)
0
m:sg(X):
sg (X; Y)
0
parent(XP; X); sg0(XP; YP); parent(YP; Y); m:sg(X):
?sg(marc; Who):
sg(X; Y) parent(XP; X); sg(XP; YP); parent(YP; Y):
sg(A; A):
Three-Way Join
Optimization|cont.
Ideally, the cost/benets of dierent recursive methods should be
quantied and compared. But quantication is often expensive
and and prediction is unreliable.
In practice, therefore, only very coarse criteria are given|e.g., use
certain goals as chain goals in the SIP.
Even for nonrecursive rules, full cost-based optimization is prob-
lematic (many goals deeply stacked). Heuristics approaches are
used instead. E.g., Glue/Nail! uses the following Heuristic: Do
First goals with more bound argument; and between two
goals with the same number of bound arguments, select those
which have fewer unbound arguments.
Following the order of goals specied by the user| in LDL++
and Prolog.
Optimization
In relational databases there are two kinds of optimizations
Generalizations
Unique binding property. Relaxing this assumption does
not require major modications or extensions
No Sideway Information Passing (SIP) between recursive
goals: only goals from lower strata can be used as chain goals
This assumption can be removed yielding the Generalized
Magic Set method.
The programs produced by this extension tend to be complex
and inecient to execute.
In the CORAL system, not all the variables are required to
be instantiated after a goal executes.
m:anc(tom):
m:anc(Mid) m:anc(Old); parent(Old; Mid):
anc0(Old; Young) m:anc(Old); father(Old; Young):
?anc0( ; Young):
?anc(tom; Desc):
anc(Old; Young) father(Old; Young):
anc(Old; Young) parent(Old; Mid); anc(Mid; Young):
Magic-set rewriting:
m:anc(tom):
m:anc(Mid) m:anc(Old); parent(Old; Mid):
anc0(Old; Young) m:anc(Old); father(Old; Young):
anc0(Old; Young) parent(Old; Mid); anc0(Mid; Young);
m:anc0(Old):
?anc (tom; Young):
0
Observe that the recursive rule just copies the value of Young
generated by the exity rule, from the tail to the head. This value
of Y oung is returned as an answer if, after a few iterations, Old =
tom. But that is always true since this rule basically re-visits the
magic-set computation.
Thus the recursive rule can be dropped along with the con-
dition in the rst argument of the query goal, yielding:
?anc(tom; Desc):
anc(Old; Young) parent(Old; Young):
anc(Old; Young) anc(Old; Mid); parent(Mid; Young):
m:anc(tom):
m:anc(Old) m:anc(Old):
Safety{Cont
The basic idea behind the notion of chain goals is that the bind-
ing in the head will have to reduce the search space. Any goal
that is called with all its adornment free will not be benecial in
that respect. Also, there is no sideway information passing (SIP)
between two recursive goals; bindings come only from the head
through nonrecursive goals.
If q is not a recursive predicate, then safety is determined as
previously described.
If q is a recursive goal, then it belongs to a lower stratum; there-
fore, safety can be determined independently using the techniques
described here for recursive predicates.
Since we have a nite number of strata the process soon termi-
nates.
?stsgbbf
ZZ
ZZ~
' $
?
stsgbbf
parentbf ; >bb ; =fb ;
& %
bornbf ; bornbb ;
stsgbbf
Recursive Predicates
The treatment of recursive predicates is somewhat more complex
because a choice of recursive methods must be performed along
with the binding passing analysis.
The simplest case occurs when the goal calling a recursive pred-
icate has no bound argument. The recursive predicate, say p,
and all the predicates that are mutually recursive with it, will be
computed in a single dierential xpoint.
The construction of the rule graph for a recursive rule is the same
as for a non-recursive one.
Only the second computation takes place for goals without bound
arguments.
The computation of the heads of the rules follows the computation
of all the goals in the body. Thus, we have a strict stratication
where predicates are computed according to the postorder traver-
sal of the rule-goal graph.
Safety
Safe a-priori:
1. For instance, base predicates are safe for every adornment.
Thus, partfff is safe.
2. The pattern bb is safe for denoting any comparison oper-
ator, such as or >.
3. Moreover, there is the special case of =bf or =fb where the
free argument consists of only one variable; in either case the
arithmetic expression in the bound argument can be com-
puted and the resulting value can be assigned to the free
variable.
(These are the basic patterns: a be more sophisticated com-
piler could solve more equations and accept other patterns as
safe)
Rule-Goal Graph
The graph has as nodes rules with adorned predicate names.
The adornment of the predicate is the subscript that denotes
bound/free argument.
E.g. The rule-goal graph for the Flat Parts Example and
query: ?part weight(Part; Weight).
part weightff
ll
r1 llr2
ll
part weightff part weightff
partfff partfff; area
.% .@ bf; =fb
r3% @@r4
%
%% @@
area bf areabf
=fb =fb
The WITH construct provides another way, and a more direct one,
to express recursion in SQL3.
Find the parts using top tube
WITH RECURSIVE all super(Major, Minor) AS
( SELECT PART, SUBPART
FROM assembly
UNION
SELECT assb.PART, all.Minor
FROM assembly assb, all super all
WHERE assb.SUBPART = all.Major
)
SELECT *
WHERE Minor = 'top tube'
"!
P (M ) (;) = M
This theorem can be used to check whether an interpretation I is
a stable model without having rst to construct groundP (I )|the
two computations are in fact identical.
TP (I ) = P (I ) (I ); where I = BP I
Multiple Models
A program can have several stable models.
p :q
q :p
This has two stable models: M1 = fpg and M2 = fq g.
With multiple models, one needs to decide what the intented
sematnics is: n all models, or nd one?
We take the second interpretation, which leads to the concept of
NonDeterminism.
Stratied Programs, however, always have a unique stable model.
Stratication is easy to check from the structure of the program|
independent of the database.
Thus stratied programs are well-suited for implementation.
Stable Models{cont.
Every stable model for P is a minimal model for P and a minimal
xpoint for TP , however minimal models or minimal xpoints
need not be stable models:
M = fag is the only model and xpoint for this pro-
gram
r1 : a :a:
r2 : a a:
Stable Models
Programs that have Stable Models avoid self-contradictions
Stability Transformation. Let P a program and I BP be
an interpretation of P . Then groundM (P ) denote the program
obtained from ground(P ) by the following transformation:
Example: P = ground(P )
p :q
q :p
Stable Models: Let P be a program with model M . M is
said to be a stable model for P , when M is the least model
of groundM (P ).
groundM (P ) is a positive program, by construction: so, its least
model is T "! (;), where T denotes the immediate consequence
operator for groundM (P ).
Now, BP SP (M ) = BP M = M .
But, BP SP (M ) = "P!M (;).
( )
dene an ascending chain.
The odd powers of SP
SP (A"Pn(;)) = SP" n (;)
2 +1
dene a descending chain.
Every element of the descending chain is than every ele-
ment of the ascending chain.
Thus, have an increasing chain of underestimates dominated
by a decreasing chain of overestimates.
If the two chains ever meet, they dene the (total) well-
founded model for the program.
BP "! (;) = B M =M
P (M ) P
i.e, i SP (M ) = M .
LEMMA 2: If M is a stable model for P then M is a xpoint of
AP .
Proof: Every xpoint for SP is also a xpoint for AP .
Let us dene:
SP (N ) = BP "P!N (;) ( )
Other Operators
For instance, the fact that will never be true can simply be
dened as :F q .
q
?F (newcstl ^ newcstl)
Temporal Operators
In addition to the usual propositional operators _ ^, and :,
;
8 newcastl ^ 9 : newcastl
Recurring Schedules
Trains for Newcastle leave daily at 800 hours and then every
two hours until 2200 hours (military time)
before22(22):
before22(H) before22(H + 1):
leaves(8; newcastle):
leaves(T + 2; newcastle) leaves(T; newcastle);
before22(T):
Datalog1s
Discrete time, can be modeled using Datalog1S.
The discrete temporal domain consists of terms built using the
constant 0 and the unary function symbol +1 (written in postx
notation). For the sake of simplicity, we will write n for
z
n times
}| {
(: : : ((0 +1) + 1) : : : + 1)
z
n times
}| {
(: : : ((T +1) + 1) : : : + 1)
quarter(0; winter):
quarter(T + 1; spring) quarter(T; winter):
quarter(T + 1; summer) quarter(T; spring):
quarter(T + 1; fall) quarter(T; summer):
quarter(T + 1; winter) quarter(T; fall):
Copy rules
When does the computation stop?
The kth stratum contains atoms all anc(k; X); delta anc(k; X).
Thus, this program is locally stratied, since the heads of recursive
rules belong
to strata that are one above those of their goals. he kth stra-
tum
XY-Stratication
Does a program have a well-founded model? In general, the
only way to answer this question is to search for such a model
(e.g., by the alternating xpoint.
For stratied programs, the answer is however easy to answer
at compile time, independent of the database
XY-stratication: is a particular class of locally stratied pro-
grams for which we also have a simple compile-time check,
and an ecient implementation
In fact, XY-stratied programs are particular 1.s programs
Active Rules
A1 : If a student is added to the alumni relation, then delete
his name from the student relation, provided that this is
a senior-level student (otherwise error{using a rule not
shown here).
A2 : If a person takes a course, and the name of this person
is not in the student relation, then add that name to the
student relation, using the (null) value tba for Major
and Level.
Using the \immediately after" activation semantics (under an ea-
ger ring policy) these rules can be modeled as follows:
A1 : student hist(J + 1; ; Name; Major; senior)
alumni hist(J; +; Name; ; ; );
student snap(J; Name; Major; senior):
A2 : student hist(J + 1; +; Name; tba; tba)
took hist(J; +; Name; ; );
:student snap(J; Name; ; ):
An active logic program consists of
(1) the history relations,
(2) the change predicates,
(3) the snapshot predicates, and
(4) the active rules.
The program A so dened is XY-stratied; thus it has a unique
stable model M , which denes the meaning of the program.
Zaniolo|Ceri|Faloutsos|Snodgrass|Subrahmanian| Zicari|All Rights Reserved
Advanced Database Systems Morgan Kaufmann Copyright
c 1997
Sec 10.5 Updates and Active Rules {4{
CZ
Continuity Axioms
If the database consists of three relations:
; ;
student(Name Major Year); took(Name Course Grade) ; ;
; ;
alumni(Name Sex Degree ClassOf) ;
(the last relation stores the alumni who graduated from college in
the previous years.)
Then we need three rules to keep track of all changes (one rule
per relation in the schema):
The rst column is a change counter that is global for the system|
that is, it is incremented for each change request.
Several changes can be made in the same SQL update statement:
4; 007 2; 301 changes in one year.
Updates in Logic
In general, logic-based systems have not dealt well with database
updates.
For instance, Prolog resorts to its operational semantics to
give meaning to assert and retract operations. The result
is that many dierent operational semantics (more than 9)
have been implemented in various systems.
Logic-based semantics for updates is also a major problem
faced by deductive database systems; however, these concen-
trate on changes in the base relations, rather than facts and
rules as in Prolog.
Current DB prototypes feature a strained coexistence of declar-
ative and operational constructs: e.g., in GLUE/Nail! GLUE
is an operationa wrap around the declarative Nail!: same syn-
tax but not same semantics.
Also, a DB system that supports updates and rules should
support active rules too ...
Desiderata:
1. providing a logical model for updates,
2. supporting the same queries that current deductive databases
do, and
3. supporting the same rules that active databases currently do.
Zaniolo|Ceri|Faloutsos|Snodgrass|Subrahmanian| Zicari|All Rights Reserved
Advanced Database Systems Morgan Kaufmann Copyright
c 1997
Sec 10.6 Nondeterministic Reasoning {9{
CZ
Beyond Don't Care Non-Determinism
In many situations, we seek to satisfy a condition that holds or
does not hold depending on the choice made. Thus, we might want
to seek among the choice models one that satisfy the condition.
Alternatively, we might make a choice and then backtrack to the
next choice once we nd that the condition does not hold. Thus
an exponential computation is often required.
Hamiltonian path in a graph: A graph has a Hamiltonian
path i there is a simple path that visits all nodes exactly
once.
simplepath(root root) ; :
simplepath(X Y) ; simple path( X) g(X Y) ; ; ; ;
choice((X) (Y)) choice((Y) (X)) ; ; ; :
nonhppath ;
n(X) : simplepath( X) ; :
q ;
:q nonhppath :
If nonhppath is true in M , then rule q :q must also be
satised by M . Thus, M cannot be a stable model. Thus, this
program has a stable model i there exists a Hamiltonian path.
Thus, deciding whether a stable model exists for a program is
N P -hard.
r0 : A B (Z ); chosen (W ): r
new rule
diffChoice (W ) chosen (W 0); Y 6= Y 0:
r r i i
match(Bname Gname); ;
boy(Bname) girl(Gname) :
choice((Bname) (Gname)); ;
choice((Gname) (Bname)); :
matched boy(Bname) match(Bname Gname) ; :
moreboys ;
boy(Bname) :matched boy(Bname) :
elig adv(S P) ; ;
student(S Majr Year) ; ; professor(P Majr); :
We obtain: