Declarative Programming
It is a programming paradigm where the desired
outcome or result is specified rather than the steps
needed to achieve it.
Focuses on what needs to be done rather than how.
Includes:
o Logic Programming (e.g., Prolog)
Logic Programming
Based on formal logic (rules and facts).
Used in Prolog (e.g., AI, expert systems).
Example (Prolog):
There are three basic constructs in Prolog:
Facts
Rules
Queries
Clauses
The program logic is expressed using clauses (facts and rules). Problems
are solved using a query (goal).
A collection of clauses is called a knowledge base. Writing a Prolog
program means writing a knowledge base as a collection of clauses. We
use the program by writing queries.
A clause is of the form :
Head :- Body
Note: A clause always terminates with a full stop ( . ).
Prolog
• Prolog has a single data type, called a “term”. A term can be:
• An atom – a general-purpose name with no inherent meaning that
always starts with a lower-case letter.
• A number, integer or float (real)
• A variable, denoted by an identifier that starts with a capital letter or
an underscore ( _ ).
• A compound term, a predicate, consisting of an atom and a number
of arguments in parentheses ( ).
• The arguments themselves can be compound terms. A predicate has
an arity (that is, the number of arguments in parentheses).
• ** Prolog is case sensitive.
FACTS IN PROLOG
• A clause without a head is a fact, eg.
capitalCity(harare).
capitalCity(lusaka).
capitalCity(pretoria).
• The meaning of the first clause is Harare is a capital city.
• capitalCity(harare) is a compound term. Both capitalCity and harare
are atoms.
• capitalCity is called a predicate and harare is the argument.
• capitalCity has arity 1, as it has just one argument. This can be written
as capitalCity/1. The /1 is showing that it takes one argument.
Example 1
capitalCity(harare).
capitalCity(lusaka).
capitalCity(pretoria).
The 3 clauses above are the knowledge base. We can run a query on this knowledge base. To ask the
question whether Harare is a capital city. We type:
capitalCity(harare).
Prolog answers true.
This means, yes, Harare is a capital city.
To ask whether Maputo is a capital city. We type:
capitalCity(maputo).
Prolog answers, false.
This means: no Maputo is not a capital city.
This is because the fact that Maputo is a capital city has not been included in the knowledge base.
PROLOG VARIABLES
We are adding some facts to our existing knowledge base as follows:
cityInCountry(harare, zimbabwe). /* Harare is a capital city in Zimbabwe */
cityInCountry(lusaka, zambia).
cityInCountry (pretoria, southafrica).
Note: /* and */ enclose a comment.
To find out which country Harare is in, we can run the query:
cityInCountry(harare, Country).
Note: Country is a variable as it starts with a capital letter.
To find out which cities are in Zimbabwe, we type:
cityInCountry(City, zimbabwe).
Cont…
If in our knowledge base, we have:
cityInCountry(harare, zimbabwe).
cityInCountry(bulawayo, zimbabwe).
To find out which cities are in Zimbabwe, we type:
cityInCountry(City, zimbabwe).
The answer would be:
City = harare;
City = bulawayo.
Note: When there is more than one answer, there is need for a semicolon after the first answer. The semicolon has the
meaning OR. The first city is instantiated to harare and the second city is instantiated to bulawayo.
Anonymous Variable
• An under-score is used to define an anonymous variable.
RULES IN PROLOG
Considering that a clause is of the form:
Head :- Body,
A rule’s body consists of calls to predicates, which are called the rule’s goals. A predicate is either
true or false, based on the terms. If the body of the rule is true, then the head of the rule is true
too.
parent(john, mary).
parent(john, alice).
parent(john, luke).
parent(peter, john).
From this knowledge base, we know that:
G is a grandparent of S, if G is parent of P and P is a parent of S.
From this knowledge base, we know that:
G is a grandparent of S, if G is parent of P and P is a parent of S.
We can write this as a rule as follows:
grandfather(G, S) IF parent(G, P) AND parent(P, S).
In Prolog the if is replaced by :- and the AND is replaced by a comma:
grandfather(G, S) :- parent(G, P), parent(P, S).
A person has a sibling (brother or sister) if they have the same parent. We can write this as the
Prolog rule:
sibling(A, B) :- parent(P, A), parent(P, B).
If we run the query:
sibling(jack, X)
We get the answers we expect, but we also get the answer that jack is his own sibling. To
avoid this, we modify the query to ensure that A is not equal to B:
sibling(A, B) :- parent(P, A), parent(P, B), not(A=B).
Example – Using a knowledge base
vegetable(carrot).
vegetable(potato)
vegetable(tomato).
meat(chicken).
meat(beef).
meat(pork).
ingredient(tagine, carrot, 250).
ingredient(tagine, tomato, 100).
ingredient(stew, beef, 400).
ingredient(stew, potato, 600).
We can check the ingredients of a tagine by asking:
ingredient((tagine, Ingredient, Amount).
Response from prolog is:
Ingredient = carrot
Amount = 250
Ingredient = tomato
Amount = 100
Adding a rule to a knowledge base
containsMeat(X) :- ingredient(X, Meat, _),
meat(Meat).
X = stew.
Instantiation and backtracking
When Prolog responds to a query with an answer like above:
X = stew.
The = is not an assignment as in imperative programs. The = sign shows
instantiation.
Instantiation refers to the process of assigning a specific value to a
variable during the execution of a Prolog program. In Prolog, variables
are placeholders that can be bound to values (atoms, numbers,
structures, etc.) as the program attempts to satisfy goals or queries.
Backtracking
• Backtracking is a key mechanism in Prolog that allows the interpreter
to explore multiple solutions by revisiting previous choices when the
current path fails to find a solution.
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
……………………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………………………………
……
…………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………...
………………………………………………………………………………………………………………………………………………………………..
……………………………………………………………………………………………………………………………………….