Ambo University Institute of Technology
Department of Computer Science
Logic in Computer Science
Chapter 5:Introduction to Prolog
Bayisa Gutema(MSc)
08/14/2025 Hachalu Hundessa Campus 1
Introduction
Prolog is the most popular language of the logic programing
languages.
It is goal based language, it has automatic backtracking and uses
recursion.
Prolog stands for programmation en logique", or programming
in logic”.
Invented by Alain Colmerauer and Philippe Roussel of the
Groupe d'Intelligence Articielle (GIA) in early 1970s.
The earliest Prolog interpreter was written in Algol in1972 by
Roussel.
The official ISO standard for Prolog was published in 1996.
08/14/2025 Hachalu Hundessa Campus 2
Content of Logic Programming
A program is a collection of axioms
Each axiom is a Horn clause of the form:
H :- B1, B2, ..., Bn. where H is the head term and Bi are the
body terms
– Meaning H is true if all Bi are true
More efficient than forward chaining for larger
collections of axioms
Applications: expert systems, artificial intelligence, natural
language understanding, logical puzzles and games.
08/14/2025 Hachalu Hundessa Campus 3
CURRENT IMPLEMENTATIONS
There are several popular Prolog interpreters available. They
include:
GNU Prolog (http://pauillac.inria.fr/vdiaz/gnuprolog/)SWI-
Prolog (http://www.swi-prolog.org/)
Visual Prolog (http://
www.visualprolog.com/vip6/Download/Default.htm)
Bprolog
(http://www.cad.mse.kyutech.ac.jp/people/zhou/bpr
olog.html
08/14/2025 Hachalu Hundessa Campus 4
Prolog Paradigm
PROgramming in LOGic
– Draw inferences from facts and rules
PROLOG is
– declarative - specify facts and logical relationships
• Non-procedural: "what", not "how". (we specify the
problem but not the solution)
• Database language with automated search and the ability
to follow general rules.
– symbolic - symbols are used to represent objects
– high level - contains a built-in problem solving
mechanism
08/14/2025 Hachalu Hundessa Campus 5
Cont..
When we program in ProLog we need to
provide following three things
1. Declaring some facts about objects and their relationships
2. Defining some rules about objects and their relationships
3. Asking questions about objects and their relationships.
Query Prolog Database
Fact + Rules
08/14/2025 Hachalu Hundessa Campus 6
The PROLOG Programmer
The PROLOG Programmer
Loads facts and rules into the database.
– Makes queries to the database to see if a
fact is:
• in the database or
• can be implied from the facts and rules
therein
08/14/2025 Hachalu Hundessa Campus 7
System Interaction
Problem solving in PROLOG
– 1. insert facts and rules into the database
– 2. ask questions (queries) based on the contents of the
database
• Facts
– Used to represent unchanging information about objects
and their relationships.
– Only facts in the PROLOG database can be used for
problem solving.
– Insert facts into the database by,
• typing the facts into a file and loading (consulting) the file
into a running PROLOG system
08/14/2025 Hachalu Hundessa Campus 8
Queries
Retrieve information from the database by entering QUERIES
A query, is a pattern that PROLOG is asked to match against
the database
has the syntax of a compound query
• may contain variables
A query will cause PROLOG to
• look at the database
• try to find a match for the query pattern
• execute the body of the matching head
• return an answer
08/14/2025 Hachalu Hundessa Campus 9
Logic Programming(Cot..)
Example(Facts)
English Prolog
“A dog is a mammal” isa(dog, mammal).
“A sparrow is a bird” isa(sparrow, bird).
Examples (Rules)
English PROLOG
“Something is an animal animal(X) :- isa(X,mammal).
if it is a mammal or a bird” animal(X) :- isa(X, bird).
08/14/2025 Hachalu Hundessa Campus 10
Logic Programming(Cont..)
English PROLOG
“is a sparrow an animal?” ?- animal(sparrow).
answer: “yes” yes
“is a table an animal?” ?- animal(table).
answer: “no” no
“what is a dog?” ?- isa(dog, X).
answer: “a mammal” X = mammal
08/14/2025 Hachalu Hundessa Campus 11
PROLOG syntax
Constants
Atoms
• Alphanumeric atoms - alphabetic character sequence starting
with a lower case letter. Examples: apple a1 apple_cart
• Quoted atoms “String” - sequence of characters surrounded
by single quotes. Examples: ‘Apple’ ‘hello world’
• Symbolic atoms - sequence of symbolic characters
Examples: & < > * - + >>
• Special atoms ,Examples: ! ; [ ] {}
– Numbers : Integers and Floating Point numbers
Examples: 0 1 9821 -10 1.3 -1.3E102
08/14/2025 Hachalu Hundessa Campus 12
PROLOG Syntax(Cont..)
Variable Names :-a sequence of alphanumeric characters
beginning with an upper case letter or an underscore
Examples: Anything _var X _
Compound Terms (structures) :-an atom followed by
an argument list containing terms.
The arguments are enclosed within brackets and separated by
commas
Example: isa(dog, mammal)
The names of all relationships and objects must
begin with a lower case letter. For example
studies,
ali, programming
08/14/2025 Hachalu Hundessa Campus 13
PROLOG Syntax(Cont..)
The relationship is written first and the objects are enclosed
within parentheses and are written separated by commas. For
example, the prolog program for “Ali is studies programming”
studies(ali, programming)
• The full stop character ‘.’ must come at the end of a fact.
• Order is arbitrary but it should be consistent
Example
Program with three facts and one rule:
rainy(columbo) ?-snowy(C)
rainy(ayubia) C=ayubia
cold(ayubia)
08/14/2025 Hachalu Hundessa Campus 14
Example(Cont..)
Rule: snowy(X) :- rainy(X), cold(X).
Query and response:
?- snowy(ayubia).
yes because rainy(ayubia) and cold(ayubia) are sub-goals
that are both true facts in the database
Query and response:
?- snowy(columbo).
no because snowy(X) with X=columbo is a goal that fails,
because cold(X) fails, triggering backtracking
08/14/2025 Hachalu Hundessa Campus 15
Example of family: Propositional Calculus
Statement Logic
p:Tola is father of
Boru. p Tola is not father of Boru
q: Tola is brother of
Buraka pq Tola is father of Boru or
r: Buraka is uncle Tola is brother of Buraka
of Boru
p q r If Tola is father of Boru and
Tola is brother of Buraka
then Buraka is uncle of
Boru
Complex statement
08/14/2025 Hachalu Hundessa Campus 16
Prolog Programming for slide 18
man(tola)
Predicate(facts) Predicate
father(tola,boru) man(symbol)
brother(boru,gurm father(symbol,symbol)
u) brother(symbol,symbol)
brother(gurmu,ku brother(symbol,symbol)
msa) owns(symbol,symbol)
owns(boru,car) tall(symbol)
tall(boru) hates(symbol,symbol)
hates(tola,gurmu) Family();
family();
X,Y,Z(man(y)father(z,y)father(z,x)brother(x
,y))
Variable: X,Y,Z
08/14/2025 Hachalu Hundessa Campus 17
Cont..
Clause
man(Tola)
father(Tola,Boru) Rule:
brother(Boru,Gur brother(X,Y),
mu) man(X),
brother(gurmu,ku man(Y)
msa) father(Z,X),
owns(Boru,car) father(Z,Y)
tall(Boru)
Hates(Tola,Gurmu)
Family();
Goal:
Constant: Brother(Boru,
Tola, Gurmu)
Boru,Gurmu,kumsa,car
08/14/2025 Hachalu Hundessa Campus 18
Section of PROLOG Program
Predicates
– Declarations of Relations or Rules
– Just like function prototype (declaration).
– Zero or more arguments
Example
Predicates
man(symbol)
family()
a_new_predicate (integer, char)
08/14/2025 Hachalu Hundessa Campus 19
Clause
Clauses
– Definition(s) of Predicate = Sentence OR Phrase
Types
Rules (function definitions): 0 or more conditions
(statements)
Facts : 0 conditions
– All parameters constant
Examples
• Fact : brother(Gurmu, Kumsa).
• Rule :brother(X,Y):-man(X),man(Y), father(Z,Y),
father(Z,X).
08/14/2025 Hachalu Hundessa Campus 20
Sections of Prolog Program
Goal
Goal of inference
Only and exactly one instance
The only tentative section of the program
The main() of prolog
Goal truth value = output of program
Syntactically and Semantically just another clause
• Empty, simple (one goal), or compound (sub-goals)
Examples
• Goal
brother(X,chand). <or> brother(ahmed,chand).
• Goal
brother(X,chand),
father()
08/14/2025 Hachalu Hundessa Campus 21
Prolog Variables
Constant placeholders (NOT variables)
– Bounded once
• Loosely typed
• Start with Capital letter or underscore
• Examples
– brother(ahmed, Jemal)
– brother(ahmed, _x)
– Brother(ahmed, X)
• Anonymous variable
– The _
– Some value that isn’t required
Example
brother(ahmed, _)
08/14/2025 Hachalu Hundessa Campus 22