Sie sind auf Seite 1von 12

Logic/Datalog

Primer
Laks V.S. Lakshmanan
UBC

Disclaimers
Ill not be lecturing from these slides
These are primarily intended as additional help for ramping up
on Predicate Logic, Datalog and Datalog-like notation.
No claim of being comprehensive.
See Text, Ch: 25 and http://infolab.stanford.edu/~ullman/
cs345notes/cs345-1.pdf for more material.

You dont need to read those at this stage but youre welcome to.
Both sources above contain many more details about Datalog, its
semantics, query evaluation, and extensions than you need for right
now.

Im going to be very informal about Predicate Logic, Datalog


and Datalog-like notation in these slides.

Predicate Logic Building Blocks 1/2


Predicate Logic (PL) allows us to write precise
statements about various things, e.g., philosophical
statements, integrity constraints in databases, queries
over DBs, the state of a programs execution, the
properties of an algorithm or a piece of software, the
state of a system, the types of connections between
individuals in a social network, and so forth.
Its a language made up of building blocks called
predicates.
A predicate can be either of the form p(X,Y,Z), q(X,Z),
r(W), or of the form X=Y, X>Y, X<=Y, etc.

X,Y,Z,W, are called variables. (start with upper case letters.)


We can also use constants in place of and in addition to
variables.
3

Predicate Logic Building Blocks 2/2


Predicate Logic Statements


Predicate Logic by Examples 1/3


Predicate Logic by Examples 2/3


4. (8X)(9Y
)strong(X,
Y
) says every student
is strong in some subject.
5. Let movie(Title,Year,LeadActor) and
studio(movieTitle,studioName,Address) be
predicates with the obvious meaning. Then:
Y,
L, L 0 )[movie(T,
Y,
L)&movie(T,
Y,
L 0 ) !
L =
L 0 ]
1. (8T,
says every movie has at most one leading
actor.
7

Predicate Logic by Examples 3/3

2. (8T,
Y,
L)[movie(T,
Y,
L)
!
(9S,
A)studio(T,
S,
A)]

says every movie is produced by some studio.
0
0
(8T,
Y,
L,
S,
A,
S
,
A
Y,
L)&studio(T,
S,
A)

3. )[movie(T,
0
0
0
0
&studio(T,
S
,
A
)
!
S
=
S
&A
=
A
]
says if there are studios S and S, with addresses A and A,
that are supposed to have produced a movie, then they
must be identical (both names and addresses), i.e., each
movie is produced by at most one studio.
8

Integrity Constraints as Datalog Rules


Shape of Datalog Rules for Integrity


Constraints

10

Shape of Datalog Rules for Queries


11

Examples of Datalog Rules for Queries


12

Das könnte Ihnen auch gefallen