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:
- X and Y are neighbors if
- X lives in such a city
- Lives there in such a city
- 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:
- write -Displays a character string.
- forall - finds a list of all facts that satisfy the condition as the first parameter, and for each element of this list executes the statement given in the second parameter.
- Format - A more complex form of recording that displays text in the specified format. Similar to printf de C .
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...