“Bad reasoning as well as good reasoning is possible; and this fact is the foundation of the practical side of logic.” Charles Sanders Peirce

Including rules

Until the last post, we have been working with simple clauses. In this post I am giving an introduction the concept of rules.

Back to our family example

Right now we have a list of parents. If we want, we can also implement the same list for children, exchanging the order of the names inside the property. OR we can do it a bit better: Using rules!

Defining children in a PROLOGish way, we think in logic: if X is parent of Y then Y is children of X. In PROLOG children(Y,X) :- parent(X,Y).. We add this line to our family.pl code.

It’s important to notice the differences between facts and rules in PROLOG. Facts are always true and rules become true, if and only if the conditions are satisfied. On the left side you have the head of the rule, that describes the conclusion and on the right side you have the body of the rule, which lists the conditions to be meet. Now we can ask PROLOG if Ismail is Abe’s son like that:

?- consult("family.pl").
true.

?- children(ismail, abe).
true.

?-

Using the same idea, it’s possible to define mother as following: mother(X,Y):-parent(X,Y),female(X). . This means that if X is female and parent of Y, then X is mother of Y. Adding this line to our family.pl file we can ask PROLOG more questions:

?- consult("family.pl").
true.

?- mother(X, abe).
false.

?- mother(X, Y).
X = sarah,
Y = isaac ;
false.

?-
  • Remember that comma [,] act as a logical AND in PROLOG.
  • Create a rule yourself to define father!

We can also implement more rules like: grandparent(X,Z):-parent(X,Y), parent(Y,Z)..

  • Create another rule yourself to define ancestor!

Next post: IO in PROLOG