What is Prolog?
Prolog stands for programming in logic (PROgrammation en LOgique).
Prolog is the most widely used language to have been inspired by logic
Prolog is the only successful example of the family of logic programming
A Prolog program is a theory written in a subset of first-order logic, called
Horn clause logic.
Prolog is declarative. A Prolog programmer concentrates on what the program
needs to do, not on how to do it.
A Little History
Prolog was invented by Alain Colmerauer, a professor of computer science at
the university of Aix-Marseille in France, in 1972.
The first application of Prolog was in natural language processing.
Its theoretical underpinning are due to Donald Loveland of Duke university
through Robert Kowalski (formerly) of the university of Edinburgh
Ease of representing knowledge.
Natural support of pattern-matching.
Natural support of meta-programming.
Meaning of programs is independent of how they are executed.
Simple connection between programs and computed answers and
No need to distinguish programs from databases.
Anatomy of Prolog Program
Prolog programs are made up of facts and rules.
A fact asserts some property of an object, or relation between two or more
Can be read as “Jane is the parent of Alan.”
Rules allow us to infer that a property or relationship holds based on
e.g. parent(X,Y) :- mother(X,Y).
= “Person X is the parent of person Y if X is Y‟s mother.”
Both facts and rules are predicate definitions.
‘Predicate’is the name given to the word occurring before the bracket in a fact
By defining a predicate you are specifying which information needs to be
known for the property denoted by the predicate to be true.
Predicate definitions consist of clauses.
= An individual definition (whether it be a fact or rule).
e.g. mother(jane, alan). = Fact
parent(P1,P2):- mother(P1,P2). = Rule
A clause consists of a head
And sometimes a body.
-- Facts don‟t have a body because they are always true.
A predicate head consists of a predicate name and sometimes some arguments
contained within brackets and separated by commas.
A body can be made up of any number of subgoals (calls to other predicates) and
Arguments also consist of terms, which can be:
-- Constants e.g. jane,
-- Variables e.g. Person1, or
-- Compound terms
Predicate name Arguments
Constants can either be:
integers are the usual form (e.g. 1, 0, -1, etc), but
floating-point numbers can also be used (e.g. 3.0E7)
always start with a lower case alphabetic character and contain any mixture of
letters, digits, and underscores (but no spaces, punctuation, or an initial capital).
e.g. abc, big_long_constant, x4_3t.
are anything between single quotes e.g. „Like this‟.
Variables always start with an upper case alphabetic character or an underscore.
Other than the first character they can be made up of any mixture of letters, digits, and
e.g. X, ABC, _89two5, _very_long_variable
There are no “types” for variables (or constants) – a variable can take any value.
All Prolog variables have a “local” scope:
--- they only keep the same value within a clause; the same variable used outside
of a clause does not inherit the value (this would be a “global” scope).
Use real English when naming predicates, constants, and variables.
e.g. “John wants to help Somebody.”
Could be: wants(john, to_help, Somebody).
Use a Verb Subject Object structure:
BUT do not assume Prolog Understands the meaning of your chosen
-- You create meaning by specifying the body (i.e. preconditions) of a clause.
Using Predicate Definitions
Command line programming is tedious
e.g. | ?- write(„What is your name?‟), nl, read(X),
write(„Hello „), write(X).
We can define predicates to automate commands:
| ?- greetings.
What is your name?
X = tim ?
Prolog Code Terminal
write(‘What is your name?’),
Running prolog program on windows
After SWI-Prolog has been installed on a Windows system, the following important new things
are available to the user:
A folder (called directory in the remainder of this document) called swipl containing the
executable, libraries, etc., of the system.
No files are installed outside this directory.
A program swipl-win.exe, providing a window for interaction with Prolog.
The program swipl.exe is a version of SWI-Prolog that runs in a console window.
The file extension .p1 is associated with the program swipl-win.exe.
Opening a .p1 file will cause swipl-win.exe to start, change directory to the directory in which
the file to open resides, and load this file.
Executing a query
After loading a program, one can ask Prolog queries about the program.
?- likes (sam, x) .
X = dahl ;
X = tandoori ;
X = chips ;
Most Prolog clauses have both a declarative reading and a procedural reading.
Whenever possible, the declarative reading is to be preferred.
mother (X, Y) :- parent (X, Y), female (X) .
Declarative reading: x is the mother of y
if x is parent of y and x is female
Procedural reading :
To show that x is the mother of y, first show that x is a parent of y, then show that x is female.
parent (john, bill) .
parent (jane, bill) .
| ?- mother (M, bill) .
The clause of mother /2 will be located, and the unification X=M, Y=bill will occur.
Then parent (M, bill) will be attempted, resulting in the unification M=john.
Next, female (john) will be attempted, but will fail.
Prolog will backtrack to parent (M, bill) and look for another solution for this; it will
succeed and unify M=jane.
Finally, it will attempt female (jane), and succeed; so the inquiry will succeed,
having performed the unification M=jane.