next up previous contents
Next: Computing in Prolog Up: Introduction to logic programming Previous: Introduction to logic programming   Contents

Facts and rules

We start with two types of simple entities, variables and constants. The convention is that variables start with a capital letter--X, Y, Name, ..., while constants start with a lower case letter--ball, node, graph, a, b, ....

Constants are, in general, ``uninterpreted''--that is, they are not further organized into types like Char and Float with underlying meaning. However, for convenience, we will allow ourselves to use natural numbers as a special type of constant and compute arithmetic expressions over these numbers.

A Prolog program defines a relation through facts and rules. For instance, consider a directed graph with five nodes.


\begin{picture}(40,40)
\put(20,40){\makebox(0,0){1}}
\put(0,20){\makebox(0,0){...
...16}}
\put(38,0){\vector(-1,0){36}}
\put(38,2){\vector(-2,1){36}}
\end{picture}

We can represent its edge relation using the following facts.

  edge(3,4).
  edge(4,5).
  edge(5,1).
  edge(1,2).
  edge(3,5).
  edge(3,2).

Each fact just lists out explicitly a concrete tuple that belongs to the relation edge.

We can now define another relation path using a set of rules, as follows:

  path(X,Y) :- edge(X,Y).
  path(X,Y) :- edge(X,Z), path(Z,Y).

These rules are to be read as follows:

Rule 1
For all X,Y, (X,Y) belongs to path if (X,Y) belongs to edge.

Rule 2
For all X,Y, (X,Y) belongs to path if there exists Z such that (X,Z) belongs to edge and (Z,Y) belongs to path.

Thus, each rule is of the form ``Conclusion if Premise${}_1$ and Premise${}_2$ ...and Premise${}_n$''. This special type of logical formula is called a Horn Clause. In Prolog, the ``if'' is written as :- and the ands on the right hand side are written as commas. The left hand side of a rule is called the goal.

Notice the implicit quantification of variables--variables that appear in the goal are universally quantified (for example, X and Y above) while variables that appear in the premises are existentially quantified (for example, Z above).


next up previous contents
Next: Computing in Prolog Up: Introduction to logic programming Previous: Introduction to logic programming   Contents
Madhavan Mukund 2004-04-29