Prolog/Datalog Notation

26.5.2 Prolog/Datalog Notation

The notation used in Prolog/Datalog is based on providing predicates with unique names. A predicate has an implicit meaning, which is suggested by the predicate name, and a fixed number of arguments. If the arguments are all constant values, the predicate simply states that a certain fact is true. If, on the other hand, the pred- icate has variables as arguments, it is either considered as a query or as part of a rule or constraint. In our discussion, we adopt the Prolog convention that all constant

26.5 Introduction to Deductive Databases 971

(a)

james SUPERVISE(franklin, john). SUPERVISE(franklin, ramesh). SUPERVISE(franklin, joyce).

Facts

(b)

SUPERVISE(jennifer, alicia).

jennifer SUPERVISE(jennifer, ahmad).

franklin

SUPERVISE(james, franklin). SUPERVISE(james, jennifer). ...

alicia ahmad Rules

SUPERIOR(X, Y ) :– SUPERVISE(X, Y ). SUPERIOR(X, Y ) :– SUPERVISE(X, Z ), SUPERIOR(Z, Y ). SUBORDINATE(X, Y ) :– SUPERIOR(Y, X ).

Queries

Figure 26.11

SUPERIOR(james, Y )? (a) Prolog notation. SUPERIOR(james, joyce)?

(b) The supervisory tree.

values in a predicate are either numeric or character strings; they are represented as identifiers (or names) that start with a lowercase letter, whereas variable names always start with an uppercase letter.

Consider the example shown in Figure 26.11, which is based on the relational data- base in Figure 3.6, but in a much simplified form. There are three predicate names: supervise, superior, and subordinate. The SUPERVISE predicate is defined via a set of

facts, each of which has two arguments: a supervisor name, followed by the name of

a direct supervisee (subordinate) of that supervisor. These facts correspond to the actual data that is stored in the database, and they can be considered as constituting

a set of tuples in a relation SUPERVISE with two attributes whose schema is SUPERVISE(Supervisor, Supervisee) Thus, SUPERVISE (X, Y ) states the fact that X supervises Y. Notice the omission of

the attribute names in the Prolog notation. Attribute names are only represented by virtue of the position of each argument in a predicate: the first argument represents the supervisor, and the second argument represents a direct subordinate.

The other two predicate names are defined by rules. The main contributions of deductive databases are the ability to specify recursive rules and to provide a frame- work for inferring new information based on the specified rules. A rule is of the form head :– body, where :– is read as if and only if. A rule usually has a single pred- icate to the left of the :– symbol—called the head or left-hand side (LHS) or conclusion of the rule—and one or more predicates to the right of the :– symbol— called the body or right-hand side (RHS) or premise(s) of the rule. A predicate

with constants as arguments is said to be ground; we also refer to it as an instantiated predicate . The arguments of the predicates that appear in a rule typi- cally include a number of variable symbols, although predicates can also contain

972 Chapter 26 Enhanced Data Models for Advanced Applications

constants as arguments. A rule specifies that, if a particular assignment or binding of constant values to the variables in the body (RHS predicates) makes all the RHS predicates true, it also makes the head (LHS predicate) true by using the same assignment of constant values to variables. Hence, a rule provides us with a way of generating new facts that are instantiations of the head of the rule. These new facts are based on facts that already exist, corresponding to the instantiations (or bind- ings) of predicates in the body of the rule. Notice that by listing multiple predicates in the body of a rule we implicitly apply the logical AND operator to these predi- cates. Hence, the commas between the RHS predicates may be read as meaning and.

Consider the definition of the predicate SUPERIOR in Figure 26.11, whose first argument is an employee name and whose second argument is an employee who is either a direct or an indirect subordinate of the first employee. By indirect subordi- nate, we mean the subordinate of some subordinate down to any number of levels. Thus SUPERIOR (X, Y) stands for the fact that X is a superior of Y through direct or indirect supervision. We can write two rules that together specify the meaning of the new predicate. The first rule under Rules in the figure states that for every value of X and Y, if SUPERVISE (X, Y)—the rule body—is true, then SUPERIOR (X, Y)—the rule head—is also true, since Y would be a direct subordinate of X (at one level down). This rule can be used to generate all direct superior/subordinate relation- ships from the facts that define the SUPERVISE predicate. The second recursive rule states that if SUPERVISE (X, Z) and SUPERIOR (Z, Y ) are both true, then SUPERIOR (X, Y) is also true. This is an example of a recursive rule, where one of the rule body predicates in the RHS is the same as the rule head predicate in the LHS. In general, the rule body defines a number of premises such that if they are all true, we can deduce that the conclusion in the rule head is also true. Notice that if we have two (or more) rules with the same head (LHS predicate), it is equivalent to saying that the predicate is true (that is, that it can be instantiated) if either one of the bodies is true; hence, it is equivalent to a logical OR operation. For example, if we have two rules X :– Y and X :– Z, they are equivalent to a rule X :– Y OR Z. The latter form is not used in deductive systems, however, because it is not in the stan- dard form of rule, called a Horn clause, as we discuss in Section 26.5.4.

A Prolog system contains a number of built-in predicates that the system can inter- pret directly. These typically include the equality comparison operator = (X, Y), which returns true if X and Y are identical and can also be written as X = Y by using

the standard infix notation. 31 Other comparison operators for numbers, such as <, <=, >, and >=, can be treated as binary predicates. Arithmetic functions such as +, –, *, and / can be used as arguments in predicates in Prolog. In contrast, Datalog (in its basic form) does not allow functions such as arithmetic operations as arguments; indeed, this is one of the main differences between Prolog and Datalog. However, extensions to Datalog have been proposed that do include functions.

31 A Prolog system typically has a number of different equality predicates that have different interpreta-

26.5 Introduction to Deductive Databases 973

A query typically involves a predicate symbol with some variable arguments, and its meaning (or answer) is to deduce all the different constant combinations that, when bound (assigned) to the variables, can make the predicate true. For example, the first query in Figure 26.11 requests the names of all subordinates of james at any level. A different type of query, which has only constant symbols as arguments, returns either a true or a false result, depending on whether the arguments provided can be deduced from the facts and rules. For example, the second query in Figure

26.11 returns true, since SUPERIOR (james, joyce) can be deduced.