Tutorial Introduction to Prolog
What is Prolog? ď ľ Prolog
is a programming language for symbolic, non-numeric computation. It is especially well suited for solving problems that involve objects and relations between objects.
What area Prolog clauses? Each clause terminates with a full stop. Prolog clauses are of three types: facts, rules and questions. Facts declare things that are always, unconditionally true. Rules declare things that are true depending on a given condition. By means of questions, the user can ask the program what things are true.
What are the arguments of a relation in Prolog? The arguments of relations can be atoms or variables. Atoms can consists of :
Strings of letters, digits and underscore characters, starting with lowercase letter. Eg. anna, x_25, x_AB Strings of special characters : eg. <--->, ==>, ::=, .:. Strings of characters enclosed in single quotes. This is useful if we want to have an atom that starts with a capital letter. By enclosing it in quotes we make it distinguishable from variables. Eg. ‘Tom’, ‘Sarah Jones’.
Variables are strings of letters, digits and underscore characters.
They start with an uppercase letter or an underscore character. Eg. X, Result, _result, _23.
What are structured objects? ď ľ Structured
objects (structures) are objects that have several components. The components themselves can be structures. Eg. Assembly(arm, joint(ball,3)).
ď ľ Facts
with arguments are used to describe relationships between arguments
Exercise 1
Which of the following are syntactically correct Prolog objects? What kinds of objects are they (atom, number, variable, structure)?
Diana
diana
‘Diana’
_diana
‘Diana likes jasin’
goes(diana, jasin).
55
Exercise 1 - answer
Which of the following are syntactically correct Prolog objects? What kinds of objects are they (atom, number, variable, structure)?
Diana - variable
diana - atom
‘Diana’ - atom
_Diana – variable
‘Diana likes jasin’ - atom
goes(diana, jasin). – structured object
55 - integer
What are the levels of meaning in Prolog?
Declarative meaning – concerns with how the relations is defined by the program or what will be the output of the program
Procedural meaning – concerns how the relations are evaluated by the Prolog system or how the output is obtained
(it is advisable to write Prolog in declarative way)
Exercise 2
Write a Prolog program that describes about an animal. The facts about the animals are given below.
Cat eats meat and has fur.
Turtle eats vegetables and has shell.
Parrot eats seeds and has feather.
suggestion : construct the following structures : flypet(X) :- animal(X), eat(X,seed), body(X,feather). cuddlypet (X) :- animal(X), eat(X,fish), body(X,fur). lazypet(X) :- animal(X), eat(X,vegies), body(X,shell).
Then, perform the following query :
i. flypet(cat)
ii. flypet(parrot).
iii. eat(cat,X).
Exercise 2 - answer
animal(cat).
animal(turtle).
animal(parrot).
eat(cat, fish).
eat(turtle,vegies).
eat(parrot,seeds).
body(cat,fur).
body(turtle,shell).
body(parrot,feather).
flypet(X):- animal(X), eat(X,seeds), body(X,feather).
cuddlypet(X):- animal(X), eat(X,fish), body(X,fur).
lazypet(X):- animal(X), eat(X, vegies), body(X,shell).
perform the following query :
i. flypet(cat) -- no
ii. flypet(parrot). -- yes
iii. eat(cat,X). -- X=fish
Construct new structures based on the following structures.
mother(X,Y) :- parent(X,Y), female(X).
sibling(X,Y) :- parent(P,Y), parent(P,X), X \= Y.
sister(X,Y) :- sibling(X,Y), female(X).
i.
i. grandparent (X,Y)
ii.
Ii. greatgrandparent (X,Y)
Construct new structures based on the following structures - answer
mother(X,Y) :- parent(X,Y), female(X).
sibling(X,Y) :- parent(P,Y), parent(P,X), X \= Y.
sister(X,Y) :- sibling(X,Y), female(X).
grandparent(X,Y) :- parent(P,Y), parent(X,P).
greatgrandparent(X,Y) :- parent(P,Y), grandparent(X,P).
Self test 1.
Write Prolog clauses and rules that describe about animals where the facts about the animals are given below : An elephant is bigger than a horse.
A horse is bigger than a donkey. A donkey is bigger than a dog. A donkey is bigger than a monkey.