foundations of computational agents
Datalog expands the language of propositional definite clauses to include individuals and relations. It can be seen as a restricted form of the predicate logic where
The only formulas allowed are definite clauses, of the form , where is the head and is the body. If , the clause is called a rule. If , the “” is ignored and the clause is called a fact.
There is no explicit quantification; all variables are assumed to be universally quantified at the outside of the clause.
The clause thus means , which is equivalent to .
Datalog is of interest because it is a database language for defining and querying relations.
A relation is often written as a table:
Course | Section | Time | Room |
---|---|---|---|
cs111 | 7 | 830 | dp101 |
cs422 | 2 | 1030 | cc208 |
cs502 | 1 | 1230 | dp202 |
This can be represented using a relation , with the knowledge base containing the facts
Suppose the relation is also defined by facts. The relation , which is the join of these relations, projected into student number and time, can be represented using the rule
A student is busy at a time if they are enrolled in a section of a course that is scheduled at that time.
Example 5.8 represented the electrical environment of Figure 5.2 using propositions. Using individuals and relations can make the representation more intuitive, because the general knowledge about how switches work can be clearly separated from the knowledge about a specific house.
To represent this domain, the first step is to decide what individuals exist in the domain. In what follows, assume that each switch, each light, and each power outlet is an individual. Each wire between two switches and between a switch and a light is also an individual. Someone may claim that, in fact, there are pairs of wires joined by connectors and that the electricity flow must obey Kirchhoff’s laws. Someone else may decide that even that level of abstraction is inappropriate because the flow of electrons should be modeled. However, an appropriate level of abstraction is one that is useful for the task at hand. A resident of the house may not know the whereabouts of the connections between the individual strands of wire or even the voltage. Therefore, let’s assume a flow model of electricity, where power flows from the outside of the house through wires to lights. This model is appropriate for the task of determining whether a light should be lit or not, but it may not be appropriate for other tasks.
Next, give names to each individual to which you want to refer. This is done in Figure 5.2 by writing the name next to the component. For example, the individual is the wire between light and switch .
Next, choose which relationships to represent. Assume the following predicates with their associated intended interpretations:
is true if the individual denoted by is a light.
is true if the light is lit and emitting light.
is true if there is power coming into ; that is, is live.
is true if switch is up.
is true if switch is down.
is true if is not faulty; can be either a circuit breaker or a light.
is true if component is connected to such that current would flow from to .
At this stage, the computer has not been told anything. It does not know what the predicates are, let alone what they mean. It does not know which individuals exist or their names.
Before anything about the particular house is known, the system can be told general rules such as
This means that a light is lit if it has electricity coming in and is not broken.
Recursive rules let you state what is live from what is connected to what:
This specifies that there is electricity coming into the building, and if there is electricity coming into and is connected to , then there will be electricity coming into . Note that the order of clauses and the order of the elements of the body has no effect on the semantics.
For the particular house and configuration of components and their connections, the following facts about the world can be told to the computer:
These rules and atomic clauses are all that the computer is told. It does not know the meaning of these symbols. However, it can now answer queries about this particular house.
Queries are used to ask whether some statement is a logical consequence of a knowledge base. With propositional queries, a user can only ask yes-or-no queries. Queries with variables allow users to ask for the individuals that make the query true.
An instance of a query is obtained by substituting terms for the variables in the query. Each occurrence of a distinct variable in a query must be replaced by the same term; if variable appears in a formula, each occurrence of must be replaced by the same term.
Given a query with free variables, an answer is either an instance of the query that is a logical consequence of the knowledge base, or “”, meaning that no instances of the query logically follow from the knowledge base. Instances of the query are specified by providing values for the variables in the query. Determining which instances of a query follow from a knowledge base is known as answer extraction.
An answer of “no” does not mean that the query is false in the intended interpretation; it simply means that there is no instance of the query that is a logical consequence.
Consider the facts about some of the counties in South America in Figure 15.2. The computer knows nothing about geography or South America. All it knows are the clauses it is given, however it can compute logical consequences. Note that the constants denoting the countries and languages start with a lower-case letter, as that is the convention of the language used; English has the opposite convention, where proper nouns start with an upper-case letter.
The user can ask the following query:
and the answer is yes. The user can ask the query
and the answer is no. This means it is not a logical consequence, not that it is false. There is not enough information in the database to determine the principal language of .
The query
has four answers. The answer with means is a logical consequence of the clauses.
The borders relation is true of two countries when they share a border. This relation is symmetric; if and only if . To represent this, Figure 15.2 represents one of the pairs (arbitrarily, with the alphabetically first one as the first argument) using , and then is derived from this. This uses half as many facts as would be if were represented directly, with the extra cost of two rules.
The query
has no answers. The query
has two answers: and .
The query
has 12 answers.
To ask for a country that borders Chile with an area greater than two million square kilometers, one could give the query
where is a predicate that is true if its arguments are numbers and its left argument is greater than its right argument. As is traditional in mathematics, this predicate is written using infix notation.
Figure 15.3 gives more facts about some South American countries and their capitals. Note how it distinguishes the name of the country from the country itself. The constant chile denotes the county, and the constant ”Chile” denotes the string that is the name of the country. These are very different, for example ”Chile” is five characters long, whereas chile is 4270 kilometers long (from North to South).
To ask for the name of the capital city of Chile, one would ask