Prolog Resolution

Examples

Consider the following

| ?- [user].
a.
b :- a.
c :- a.
c :- b.
d :- a;b;c.
e :- f.
g :- a.
g :- g.

Now some queries.

| ?- a.

yes
| ?- b.

yes
| ?- c.

true ? ;

(1 ms) yes
| ?- d.

true ? a

true

true

yes
| ?- e.
uncaught exception: error(existence_error(procedure,f/0),e/0)
| ?- f.
uncaught exception: error(existence_error(procedure,f/0),top_level/0)
| ?- g.

true ? ;

true ? ;

true ? ;

true ? ;

true ? ;

true ? ;

true ? ;

true ? ;

true ? ;

true ? 

yes

Procedure Box Model

Also called the Byrd Box model since it is due to Lawrence Byrd, the box model is used to understand the control flow in a prolog program.

                  +----+
call       --->|    |---> exit
fail       <---|    |
exception  <---|    |<--- redo
                  +----+

Tracing programs is a good way to monitor using the procedure box model of execution.

| ?- trace.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?- c.
      1    1  Call: c ?
      2    2  Call: a ?
      2    2  Exit: a ?
      1    1  Exit: c ?

true ? ;
      1    1  Redo: c ?
      2    2  Call: b ?
      3    3  Call: a ?
      3    3  Exit: a ?
      2    2  Exit: b ?
      1    1  Exit: c ?

(2 ms) yes
{trace}
| ?- notrace.
The debugger is switched off

yes
| ?-

Resolution with Variables

Now with variables

| ?- [user].
p(1).
p(2).
q(2).
q(3).
r(Y) :- p(Y), q(Y).

The comma is pronounced "and".

Notice that when we execute a query, we may ask for a specific value for the term or use a variable.

| ?- r(2).

yes
| ?- r(E).

E = 2

yes

Note that when there is a variable in the query, Prolog tries to prove the thruth of the query, and responds with the value to which that variable must to be unified.

Can change the meaning with semicolon, which is "or",

r(Y) :- p(Y); q(Y).

, but better to use two rules.

r(Y) :- p(Y).
r(Y) :- q(Y).