2
votes

I came across this example of a fact in Prolog:

vertical(line(point(X,Y),point(X,Z))).

This seems strange to me as I thought facts always contain only a functor and atoms (or structures of atoms) and the above code seems more like a rule (if .... then line is vertical).

This has really confused me and I am now trying to figure out the difference between a fact containing variables and a rule and when one or the other should be used. Someone please help me out! :S

Also, how could I write this code using rules?

1

1 Answers

4
votes

Technically, yes, you can have variables in facts, and it is common practice. Although, I suppose they might then become rules. :)

Let's consider your example:

vertical(line(point(X,Y),point(X,Z))).

This is an implicit way of expressing the following rule:

vertical(line(point(X1,Y), point(X2,Z))) :- X1 = X2.

This of course would give a warning about Y and Z being singleton variables. Since you don't care what they are, you can use _ and express the rule as follows:

vertical(line(point(X1,_), point(X2,_))) :- X1 = X2.

Or, simply:

vertical(line(point(X,_), point(X,_))).

One definition of "fact" versus "rule" is given in this article on Facts, Rules, and Queries. Following this definition, the above could be considered a fact since it would be true for all X.