Prologue textbook: The problem is set out in the form of rules

The prologue program consists mainly of facts and rules. The rules are a way to express what you want to do with the facts we've captured.

For example, we know in which cities the list of people lives...

habite('Julia', 'New York').
habite('Tom', 'San Francisco').
habite('Naomi', 'New York').
habite('Harrison', 'San Francisco').

Julia lives in New York, Tom in San Francisco, etc.

One way to use this knowledge might be to find which people live in the same city. To do this, we define a rule.

voisin(X,Y) :- habite(X, Ville), habite(Y, Ville).

This reads:

  1. X and Y are neighbors if
  2. X lives in such a city
  3. Lives there in such a city
  4. de facto variable City has the same content in both ads

This will give us Julia Noami's neighbor, Tom Harrison's neighbor. But this will give us a neighbor Julia, because the prologue does not assume from itself that X and Y are two different people. therefore, it is necessary to add another condition: the variables X and Y have different values.

voisin(X,Y) :- habite(X, Ville), habite(Y, Ville), X \= Y.

We will have the next result.

Julia voisin de Naomi 
Tom voisin de Harrison
Naomi voisin de Julia
Harrison voisin de Tom

To view these results, we will add a small program. A program is a rule that contains instructions. We use three instructions from the standard library:

So here's the program:

program :-
write('Voisins...\n'),
forall(voisin(X,Y), format("~a voisin de ~a ~n", [X, Y])),
write('Fin').

Since "program" is a rule, you enter the following line in the prolog console to execute it:

program.

It will show:

Voisins...
Julia voisin de Naomi 
Tom voisin de Harrison
Naomi voisin de Julia
Harrison voisin de Tom Fin

You can also directly call the neighbor rule:

voisin('Julia', 'Naomi').

which will return "yes."

Knowing now how to state facts and rules, you have everything you need to solve complex problems thanks to the Prologue...

  1. Prologue language.
  2. Prologue textbook: A set of facts is recorded.
  3. Prologue textbook: The problem is set out in the form of rules
  4. .
  5. Demo Prologue: Looking for an apartment
  6. .