Prolog Tutorial: Recording a set of facts

Once installed SWI-Prolog or GNU Prolog, you can enter the system by clicking on the icon on the desktop, which opens an interactive console. You can then enter facts, rules and questions. You will quit the system by typing:

?- halt.  

Note that any command or line of code in Prolog ends in a period.
The first thing to do in a program is to state facts.

Just as the human mind thinks using references, words representing things, Prolog uses references before adding relationships. These references and their relations are called "facts".

Examples of facts:

alyssa.
daughter(alyssa).
daughter(kiera).

No initials capital letters to first names here, because it is reserved for the variables, what we will see further. But we can write:

daughter('Alyssa').
daughter('Kiera'). 

Adding a fact in the program space, a reference or a relationship, as we have just done, affirms their existence. We can then question the base of facts to know if something exists and is true.

But before, to avoid having to enter the same data at each session, we will put these facts in a source file, with the extension ".pl", such as "facts.pl".

The following command in the prolog console:

consult(facts.pl)

has the effect of loading the contents of this file in the memory and parsing its.

We can then query the system by typing requests. For example:

?- daughter('Kiera').

The system responds "yes" because this fact is well recorded.

If you type:

?- daughter('Tom').

it will answer: "no".

A prolog variable begins with an uppercase letter or can consist of a single uppercase letter such as "X". If you query the system with a variable rather than a reference, it will "unify" the variable with all the facts that are verified for the given relation. For example:

?- daughter(X).

The console displays:

X = 'Alyssa' ?

and wait until you press the space bar to get the next line. It will then show:

X = 'Kiera'

Facts can be used in rules to build knowledge through which more complex problems can be solved.

  1. Prolog Tutorial: Recording a set of facts.
  2. Prolog Tutorial: A problem is espressed in the form of rules.
  3. Prolog demo: Looking for an apartment in a catalog.