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

8   Others Elements of Prolog

8.1   Operators

For the arithmetic operators, prolog has already a list of predefined predicates. These are :
     =,is,<,>,=<,>=,==,=:=,/,*,+,-,mod,div
In Prolog, the calculations don't write like we have learned.They are written as a binary tree.That is to say that :
     y*5+10*x are written in Prolog : +(*(y,5),*(10,x)).
Because we don't have learned to write like this, Prolog accept our way of writing calculations.Nevertheless, we have to say to him the rules of priority on the operators.For instance, we have to say to Prolog that * is prior on +.We have also to indicate to Prolog the prior calculations with '()'.Prolog allows the programmer to define his own operators with the relations next :
     Op(P,xfy,name).
Where P is the priority of the operators(between 1 and 1200), xfy indicates if the operator is infix(xfx,xfy,yfx) or postfix(fx,fy).The name is, of course, the name of the operator.Note that the prior operator has the lowest priority. Prolog has already these predefined operators :
     Op(1200,xfx,':-').
     Op(1200,fx,[:-,?-]).
     Op(1100,xfy,';').
     Op(1000,xfy,',').
     Op(700,xfx,[=,is,<,>,=<,>=,==,=:=]).
     Op(500,yfx,[+.-]).
     Op(500,fx,[+,-,not]).
     Op(400,yfx,[*,/,div]).
     Op(300,xfx,mod).

8.2   Arithmetic

Prolog use the infix operator 'is' to give the result of an arithmetic operation to a variable.is
     X is 3 + 4.
Prolog responds
     X = 7
     yes
When the goal operator 'is' is used the variables on the right must have been unified to numbers before. Prolog is not oriented calculations, so, after a certain point, he approximates the calculations and he don't answer the exact number but his approximation Fo example :
     ?- X is 1000 + .0001
     X = 1000
     yes



<-- Lists
-
Input and Output -->