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

2   The Facts

2.1   Simple facts

In Prolog we can make some statements by using facts. Facts either consist of a particular item or a relation between items. For example we can represent the fact that it is sunny by writing the program :
     sunny.
We can now ask a query of Prolog by asking
     ?- sunny.
?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because (from above) Prolog matches it in its database of facts.
Facts have some simple rules of syntax. Facts should always begin with a lowercase letter and end with a full stop. The facts themselves can consist of any letter or number combination, as well as the underscore _ character. However, names containing the characters -,+,*,/, or other mathematical operators should be avoided.

2.2   Facts with arguments

More complicated facts consist of a relation and the items that this refers to. These items are called arguments. Facts can have arbitrary number of arguments from zero upwards. A general model is shown below:
     relation(<argument1>,<argument2>,....,<argumentN> ).
The arguments can be any legal Prolog term. The basic Prolog terms are an integer, an atom, a variable or a structure. Various Prolog implementation enhance this basic list with other data types, such as floating point numbers, or strings.
Exemple :
     likes(john,mary).
In the above fact john and mary are two atomes. Atoms are usally made from letters and digits with lowercase characters. The underscore (_) can also be used to separe 2 words but is not allowed as the first charactere. Atoms can also be legally made from symbols. The followings are legal atoms :atoms
     hello
     zz42
     two_words
     ====>
The followings are not legal atoms :
     Hello
     4hello
     _Hello
     two words
     two-words
You can use single quotes to make any character combination a legal atom.
     'two words'
     'UpperCase'
     '12444'
     'etc...'
The fact likes(john,mary). say that there is a relation between john and mary. It can be read as either john likes mary or mary likes john. This reversibility can be very useful to the programmer, however it can also be a source of mistakes. You have to be clear on how you intend to interpret the relation.
The number of arguments is the arity of the predicate. A predicate with 2 arguments will be called by predicate_name/2. You can have differents predicats with the same name if they have a different arity.

2.3   How to query

Once you have entered the facts in a program you can ask prolog about it. An exemple programm can be :
     eats(fred,oranges).     /* 'Fred eats oranges' */

     eats(tony,apple).       /* 'Tony eats apple'   */

     eats(john,apple).       /* 'John eats apple'   */
If we now ask some queries we would get the followings things :
     ?- eats(fred,oranges).   
     /* does this match anything in the database? */

     yes                      
     /* yes, that matchs the first clause */

     ?- eats(john,apple).

     yes 

     ?- eats(mike,apple).

     no                       
     /* there is no relation between mike and apple */



-
Variables and Unification -->