In prolog, we attempt to create a set of facts and rules that describe how the "universe of interest" behaves, and then leave it up to the prolog executable to use those facts and rules to answer any questions we may have.
For instance, we may come up with a fact like:
1 is a positive integer
and then a rule like:
if X is an integer and Y equals X+1 then Y is an integer
We could then issue a query like
is 17 an integer?
and the prolog engine would try to use the facts and rules
to work out a yes/no answer.
Solving a problem then becomes a matter of describing our universe (or at least the relevant portion) succinctly, and phrasing our question appropriately.
Sometimes there are multiple ways to satisfy a query. In these cases prolog will say yes and display the first solution found. If you press ENTER it will complete the query, if you press the semi-colon key ( ; ) it will give the next solution. Continue pressing ; until you have seen all the solutions you wish to see.
For instance, to state the fact that Dave is tired we could use:
tired(dave).
Or to state the fact that 3! is 6 we could use:
factorial(3,6).
Or to state the fact that Homer is Bart's father we could use:
father(homer, bart).
Note that order is important, so if we wanted to use facts
to state that Bart and Lisa are siblings we would have to
use two facts:
sibling(bart, lisa).
sibling(lisa, bart).
Such statements (whether as facts or as parts of queries) are referred to as terms or as structures. The name at the head is called the functor, and the number of arguments it takes is called its arity. These are grouped prolog by the functor/arity, e.g. sibling/2 refers to terms named sibling that expect 2 arguments.
Queries can be made more sophisticated by using variables,
for instance to ask if Bart has any siblings we could use
sibling(bart, X).
Given the facts listed earlier, the prolog engine would respond
with something like
true X=lisa
If we were to make the query sibling(X, Y). the engine
would respond with the first case it could find that made the
query true, e.g.
true X=bart Y=lisa
If we were to ask sibling(maggie, Z). it would have to respond with no, since there are no matching facts yet to describe maggie.
Rules allow us to tell prolog how to combine existing facts to answer more complex queries, and are covered in the next section.