Tutorial Prolog in PDF
Prolog is a language used in the fields of Artificial Intelligence and Constraint Logic Programming. Its syntax and operating principle are radically different from imperative languages such as C or Java. The reasoning is closer to functional languages such as Caml or Lisp. Yet, Prolog is not a functional language. Prolog is the first logical programming language.
Not finding a tutorial that is both affordable for the beginner and advanced enough to acquire a good foundation in Prolog programming, I decided to write one myself.
In those tutorials, I wanted to focus on programming in Prolog itself (section 3). Before that, we will present the language and the basics of Prolog (sections 1 and 2). Finally, some important notions of language will be discussed in section 4.
The plan for the tutorials in this page:
The first section of this tutorial introduces you to the language (tools, sample programs)
The second section of this tutorial allows you to acquire the basics of the language, especially in terms of syntax.
The third section is the most important, because it will allow you to really learn to program in Prolog, using 3 extremely simple and easy to remember patterns.
The last section serves to deepen the basics and to avoid some common mistakes.
Where to get Prolog?
There are different tools for you to program in Prolog:
- SWI Prolog : Has a graphical debugger and several constraint solvers www.swi-prolog.org
- GNU Prolog : Provides a finite domain constraint solver http://gnu-prolog.inria.fr
- Sicstus Prolog (paying) : Has additional extensions, including multiple constraint solvers www.sics.se/sicstus
How to install Prolog?
On Windows: Download one of the programs mentioned above and install it
Under Debian / Ubuntu: In "root": apt-get install swi-prologou good: apt-get install gprolog
Other linux distributions: See the management of pasting for your distribution (RPM, emerge ...) or to compile from the sources.
How to edit a Prolog program?
It's simple: with your favorite text editor!
In Prolog, the variables are represented by an identifier starting with an uppercase letter (example: X, Machin, List, ...).
As long as no value is assigned to a variable, it is called a "free variable". Once a value is assigned, it is called a "linked variable". A variable value is assigned by what is called the principle of "unification".
In Prolog, once a value is assigned to a variable, it can no longer be changed (except in the case of mutables, but we will not talk about it here). To perform new operations, you must declare a new variable and assign it a new value.
Constants and atoms
A constant always starts with a tiny letter (ex: jean, belfort, single ...). It is also possible to define strings by enclosing them in single quotation marks (eg 'The hare and the turtle', 'The Lord of the Rings' ...). Constants and chains constitute atoms.