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

3   Variables and Unification

3.1   Simples unifications

How can we ask something like ''what does Fred eat ?'' If we have the following program :
     eats(fred,oranges).
How do we ask what fred eats ? We could write something like this :
     ?- eats(fred,what).
But Prolog will say no. The reason is that Prolog can't find the relation eats(fred,what) in hi database. In this case we have to use a variable wich will be unified to match a relation given in the program. This process is known as unification.
Variables are distinguished from atoms by starting with a capital letter. Here are some exemples of variables :variables
   X            /* a single capital letter                    */
 
   VaRiAbLe     /* a word beginning with an upper case letter */

   Two_words    /* two words separated with an underscore     */
Now that we know how to use a variables, we can ask the same question as before using the variable What instead of an atom.
   ?- eats(fred,What)

   What=oranges
   
   yes
In this case Prolog try to unifie the variable with an atom. ''What=oranges'' means that the query is successfull when What is unified with oranges.
With the same program if we ask :
     ?- eats(Who,oranges).
In this example we ask who eats oranges. To this query Prolog should answer :
     ?- eats(Who,oranges).

     Who=fred

     yes
Now if we ask :
     ?- eats(Who,apple).

     no
Prolog answer no because he can't find in his database any atom than can match with this relation.
Now if we only want to know if something is eated by anyone and we don't care about that person we can use the underscore. The '_' can be used like any variable.
For example if we ask eats(fred,_) the result will be :
     ?- eats(fred,_).

     yes
The result will be yes because Prolog can find a relation of eat between fred and something. But Prolog will not tell use the value of '_'.
Now if we have the following program :
     eats(fred,apple).

     eats(fred,oranges).
Now if we ask :
     ?- eats(fred,What).
The first answer will be ''What=apple'' because that is the unification that match the first relation of eats with fred in the database. Then prolog will be waiting for you to press a key. If you press enter Prolog will be ready for a new query. In most implemetation if you press the key '';'' then Prolog will try to find if there is any other successful unification. Prolog will give the second result ''What=orange'' because this the second one in the program. If you press again the key '';'' then Prolog will try to find a third unification. The result will be ''no'' because he is not able to find any other successful unification.
     ?- eats(fred,What).

     What=apple ;

     What=oranges ;

     no

3.2   Variables unification example

Consider this exemple of program that could be used by a library :
     book(1,title1,author1).

     book(2,title2,author1).

     book(3,title3,author2).

     book(4,title4,author3).
Now if we want to know if we have a book from the author2 we can ask :
     ?- book(_,_,author2).

     yes
If we want to know which book from the author1 we have :
     ?- book(_,X,author1).

     X=title1 ;

     X=title2 ;



-
Rules -->