Download A Beginners Guide to Visual Prolog
Transcript
If you know about logic, then you will recognize the pattern that is known as deduction. The
difference is that in logic we are used to write something like:
IF p AND q THEN r
while in prolog we would write:
r :- p, q.
When the premisses are true, then the conclusion is also true. This is more or less the case in Prolog.
The general form of a rule is:
conclusion :- premiss1, premiss2, ...,premissN.
Let’s return to the grandfather rule. As it is formulated, it is difficult to understand. E.g. from the way
it is written, you cannot see directly which Person is supposed to be the grandfather. It is better to use
understandable names for the variables instead of Person1, Person2 and Person3. In Horn Clause
Logic you can do that just as you like it. You are free to choose almost any variable name as you like,
so you can e.g. rephrase the rule to:
grandFather(GrandChild, GrandFather) :father(GrandChild, Father), father(Father, GrandFather).
Realize that this is only convenient for human readers of your code. Computers don’t realize that a
variable “Father” should have a child. Your computer would be just as happy with a rule:
g(X,Y) :- f(X,Z), f(Z,Y).
In the rule we use variables instead of object names. A rule is a general relation that holds between
any objects. As long as there is an object Y that relates as a father to object X and at the same time an
object Z relates to Y as a father, then in the world of your program object Z is the grandfather of
object X. Period.
Summarizing: when reading rules you should interpret the symbol “:-“ as (a logical) if and the comma
that separates the predicates as (a logical) and. In the variable names GrandChild, Father and
Grandfather the first letters are upper case. In Prolog a variable name always starts with upper case. In
this rule “grandFather(GrandChild, GrandFather)” is the conclusion and “father(GrandChild, Father),
father(Father, GrandFather)” are the premisses. The rule is ended with a period.
By choosing variable names that help understanding better than X, Y and Z, I improved the
readability of the rule for humans. Imagine that you had to decide from “grandfather(X,Y)” which one
of X and Y is the grandfather. It could easily lead to confusion. Readability was also improved by
writing the rule on two lines. One line for the conclusion and one line for the premisses. Or when the
premisses are long, you can write every premiss on a new line.
By adding this rule I have introduced a predicate for the grandfather relation. That is efficient. Instead
of writing a fact for every grandfather and grandchild in my world, I only specify one rule and use
implicitly the facts about fathers and children. Again I have chosen that the grandfather should be the
second argument. It is wise to be consistent like this, i.e. that the arguments of the different predicates
follow some common principle.
In Prolog facts and rules are called clauses. With facts and rules we are ready to formulate theories. In
general a theory is a collection of facts and rules, in Prolog we would say a theory is a set of clauses.
Let me state a little theory that consists of three facts and one rule:
father("Bill", "John").
55