barbie's notes - PROLOG - Part 4


“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 clausels. In this post I am introducting 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

:- parent(X,Y). ```. We add this line to our family.pl code.
1
2
3
4
5
6
7
8
9
10
11

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:

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

?- children(ismail, abe).
true.

?-

Using the same idea, it’s possible to define mother as following:

. 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

```bash
?- 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