• No results found

Background - AI Problem Solving Techniques

2.2 Logical Systems

2.2.2 First-order Logic

First-order logic (or predicate logic) is an extension of propositional logic to include predicates and quantifiers. A predicate represents a property of an object or a relation between objects and can be true or false. For example, man(A) expresses the concept of all things, A, that are men, and we call the A a variable. For example, the phrase “Socrates is a man” could be expressed, in predicate logic, as:

man(socrates)

and, similarly, the phrase “David is Matthew’s father” could be described using:

father(david,matthew).

In these predicates, the variables have been instantiated, or ground, to the values socrates, david and matthew, which we call constants. The choice of which predicates and variables to use is up to the logician but their meaning should be consistent. The same sentence could be represented using several other predicates. For example,

2.2. Logical Systems 15

father of matthew(david) son(david,matthew) son of david(matthew)

Quantifiers make it possible to fashion statements that range over variables. Specifically there is a universal quantifier (∀), which considers all possible instantiations of a variable. There is also an existential quantifier (∃), which describes the existence of one variable instantiation which satisfies the formula. For example, it allows statements such as “all men are mortal”

∀ x man(x) → mortal(x)

which reads “for all x, if x is a man then x is mortal”, and “everyone has a father”

∀ x ∃ y father(y, x)

which is read as “for all x there is a y such that y is the father of x”.

The inference rules for predicate logic are similar to those for propositional logic. In addition, there are inference rules affecting the quantifiers. These are described in table 2.4.

Inference Rule Given Result

Forall introduction P (c) true for all possible c ∀ x P (x)

Forall elimination ∀ x P (x) P (c)

Exists introduction P (c) ∃ x P (x)

Exists elimination ∃ x P (x) P (c) for some arbitrary c

Table 2.4: Predicate logic inference rules for quantifiers.

Note that a quantified predicate can be turned into a proposition by assigning values to the variables, whereupon it considers the truth of a particular case. This is referred to as grounding, with the instantiated variables being called ground variables. Predicates and propositions, with no logical connectives, are known as atoms and can take a true or false value. Atoms and their negations are known as literals.

Many AI systems use some form of first-order logic as their representation scheme, including all those we have considered in our projects. In many systems the input process requires that the

logical statements are in a particular format. One such format is a clause, which is a disjunction of literals e.g.

A1∨ A2∨ . . . ∨ An.

Conjunctive Normal Form describes a propositional formula which is a conjunction of clauses.

For example, the following statement is in conjunctive normal form:

(A∨ ¬B) ∧ (B ∨ C).

It is possible to translate any propositional formula into conjunctive normal form, which makes it useful as an input format. The first-order equivalent of this is clausal normal form. Again, all first-order statements can, through a process of logical manipulation, be translated into clausal normal form. In clausal normal form all quantifiers are removed. Existential variables are re-placed by ground equivalents in a process known as skolemisation. All clauses in clausal normal form are assumed to be universally quantified and the quantifiers themselves are omitted. A clausal normal form clause having at most one positive literal is known as a Horn-clause. They are in the format:

¬a ∨ ¬b ∨ ¬c ∨ . . . ∨ z.

which can also be written:

(a∧ b ∧ c . . .) → z.

The Prolog programming language allows developers to program in a form of first-order pred-icate logic. Users create facts and rules in a Prolog database in the form of horn-clauses. If the clause has no negative literal then the clause is a fact. Where there is a negative literal it becomes a rule. The user can query the database by asking Prolog to create an instantiation for a clause they enter. It does this by searching the rules and facts in its database for suitable solutions. For example, consider a Prolog database containing the following clauses:

2.2. Logical Systems 17

man(socrates).

man(aristotle).

mortal(X):-man(X).

The database can be queried using clauses as requests. Figure 2.2 shows a simple Prolog session using this database, which we have encoded into the file example.pl.

SICStus 3.12.8 (x86-linux-glibc2.3):

Tue May 8 13:30:29 CEST 2007 Licensed to doc.ic.ac.uk

| ?- % consulting example.pl...

% consulted example.pl in module user, 0 msec 920 bytes

| ?- man(socrates).

The first two queries in the example session are “man(socrates)” and “man(aristotle)”. These both return “yes” as the queries match explicit facts in the database. The next query returns

“no” as Prolog uses a closed-world assumption and there is no fact to support “man(plato)”.

The user can also query using uninstantiated variables which Prolog will try to find instan-tiations for. For instance, the query “man(X)” returns, first, “yes, X=socrates” and then

“yes, X=aristotle”. The next query demonstrates the use of a Prolog rule. The query “mor-tal(socrates)” cannot be supported by an explicit fact in the database. However, there is a rule

“mortal(X):-man(X)”. This states that, in order to satisfy “mortal(X)” it is sufficient to satisfy

“man(X)”, which is another way of saying “man(X) → mortal(X)” or “all men are mortal”.

Prolog uses this rule to check the validity of “mortal(socrates)”, which it confirms by identifying

the fact “man(socrates)”. Again, we can query this with uninstantiated variables. Notice that, in Prolog format, uninstantiated variables begin with capital letters and ground variables with lower case characters. Prolog also supports anonymous variables. These are denoted by “ ” and, when used in queries or rules, they mean that the we require the variable to be instan-tiated but the actual value it is given is unimportant. For example, on the above database, the query “man( )” would return “yes” because some satisfying instantiation is possible (either

“man(socrates)” or “man(aristotle)”).