Information Technology Reference
In-Depth Information
The Horn clause indexed by i) is a rule, with P(X), Q(Y) the body and W(X,Y)
the head. The Horn clause indexed by ii) is a query with R(X,Y),Q(Y) the body.
The intuition of the query indexed by ii) is that whether R(X,Y) and Q(Y) hold
and what are the value of X and Y in the case that R(X,Y)∧Q(Y) holds.
We are now to formally define Logic Programs.
Definition 2.2 A logic program is a collection of Horn clauses. In logic program
clauses with same predicate symbol are called the definition of the predicate.
For example, the following two rules forms a logic program:
Father(X,Y) :- Child(Y,X), Male(X).
Son(Y,X) :- Child(Y,X), Male(Y).
This program can also be extended with the following facts:
Child( xiao-li, lao-li).
Male(xiao-li).
Male(lao-li).
Taken these rules and facts as inputs of the Prolog engine, we can compile
and execute it. Then the following queries can be carried out:
(1) query: ?- Father(X,Y), we will get the result Father(lao-li, xiao-li);
(2) query: ?- Son(Y,X), we will get the result Son(xiao-li, lao-li).
2.2.2 Data structure and recursion in Prolog
An important and powerful tool in problem solving and programming, recursion
is extensively used in data structures and programs of Prolog.
Term is a basic data structure in Prolog. Everything including program and
data is expressed in form of term. Terms of Prolog are defined recursively by the
following BNF rule:
<term> ::= <constant> | <variable> | <structure> |<term>
where structures are also called compound terms, and are generated by the BNF
rule:
Search WWH ::




Custom Search