Prolog


Introduction

1 Presentation of Prolog
  1.1 What is a Prolog program
      1.1.1 The Program
      1.1.2 The Query

2 The Facts
  2.1 Simple facts
  2.2 Facts with arguments
  2.3 How to query

3 Variables and Unification
  3.1 Simple unifications
  3.2 Variables unification example

4 Rules
  4.1 Rules
  4.2 How to add a rule with a program
      4.2.1 The instructions
      4.2.2 Example

5 Backtracking
  5.1 Fail
  5.2 Cut
  5.3 Not

6 Recursion
  6.1 What is recursion
  6.2 Examples of Recursion
      6.2.1 Example of the ancestors
      6.2.2 Factorial

7 Lists
  7.1 What is a list in Prolog
  7.2 How to manipulate list

8 Others Elements of Prolog
  8.1 Operators
  8.2 Arithmetic

9 Input and Output
  9.1 Input Output commands
  9.2 Read and Write
  9.3 Examples
      9.3.1 Calculating the cube of an integer
      9.3.2 Treating the terms of a file
      9.3.3 ASCII characters
      9.3.4 Using Another Program

10 SWI-Prolog
  10.1 What is SWI-Prolog
  10.2 Author
  10.3 Platforms
  10.4 FTP Sites


Appendix

A Meta Logical Constructs

B Input and Output
  B.1 input
  B.2 Output

C Some Others usefull predicats
  C.1 True
  C.2 Repeat
  C.3 Call
  C.4 Setof
  C.5 Bagof

D Comparison Operators
  D.1 Arithmetic Comparison Operators
  D.2 Term Comparison


This Report in LaTeX
About us
Links

4   Rules

4.1   Rules

Consider the following sentence : 'All men are mortal' We can express this thing in Prolog by :

     mortal(X) :- human(X).
The clause can be read as 'X is mortal if X is human'.
To continue with this example, let us define the fact that Socrate is a human. Our program will be :
     mortal(X) :- human(X).

     human(socrate).
Now if we ask to prolog :
     ?- mortal(socrate).
Prolog will respond :
     yes
In order to solve the query -? mortal(socrates). Prolog will use the rule we have given. It says that in order to prove that someone is mortal we can prove that he is human. So from the goal mortal(socrate) Prolog generate the subgoal human(socrate).
We can still use variables. For example we might want to know who is mortal :
     ?- mortal(X).
Then Prolog should respond :
     P=socrate
This means that Prolog was able to succed the goal by unifying the variable X to socrates. Again this was done by using the subgoal human(X).
Sometimes we may wish to specify alternatives ways to provre something. We can do this by using differents rules ands facts with the same name. For exeample, we can represent the sentence 'Something is fun if it is a PC running UNIX or am old amiga or an ice cream' with the following program :
     fun(X) :-            /* something is fun if       */

               pc(X),    /* it is a pc and             */

               unix(X).    /* it is running unix       */


     fun(X) :-            /* or it is fun if           */

               old(X),    /* it is old and             */

               amiga(X).  /* it is an amiga            */

     fun(ice_cream).      /* the ice_crean is also fun */
This program says that there are three ways to know if an object is fun or not. Like for pure facts, Prolog will start from the first clause (a clause can be a rule or a fact) of fun and try it. If that does not succed Prolog will try the next clause. If there is no more clauses then it fails and Prolog responds ' no '. We can also see in this example that the 'and' is represented by a ',' and the 'or' by differents clause. If needed the 'or' can also be represebted by ';'. In the previous examples when we was asking eats(fred,What) and pressing the key ';' to see the following results we was in fact asking 'or'.
All identically-named variables in a rule (for example X in the last rule we've seem) are of course considered as one unic variable and must have the same instantiation for each solution in a particular query. Identical variables names in differents rules are considerated as differents variables and are totally independent, this is the same as if we had used differents names.
The following program :
     fun(X) :-
               pc(X),
               unix(X).

     fun(X) :-
               old(X),
               amiga(X).
Will be seen by Prolog as :
     fun(X_1) :-
                 pc(X_1),
                 unix(X_1).

     fun(X_2) :-
                 old(X_2),
                 amiga(X_2).

4.2   How to add a rule with a program

It is possible to add new rules or facts with the instruction Assert(fact1) which will add the fact called fact1. The predicates added with this command are considereted like any other in the source of the program.

4.2.1   The instructions

The usefull instructions are :
assert(c). Add the rule c in the database.assert()
retract(c). Remove the c from the database.retract(c)
asserta(c). Add c at the beginning of the database.asserta()
assertz(c). Add c at the end of the database.assertz()

4.2.2   Example

     ?- sunny.
     no.
     
     ?- assert(sunny).
     yes.
     
     ?- sunny.
     yes

     ?- retract(sunny).
     yes.

     ?- sunny.
     no.



-
Backtracking -->