0% found this document useful (0 votes)
19 views

Declarative programming

The document discusses declarative programming paradigms, focusing on the use of facts and rules to solve problems, particularly through the Prolog programming language. It explains the structure of Prolog programs, including facts, rules, and queries, and provides examples of how to create a knowledge base and run queries. Additionally, it includes activities and solutions related to querying and manipulating data within a Prolog knowledge base.

Uploaded by

Sooraj Rajmohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Declarative programming

The document discusses declarative programming paradigms, focusing on the use of facts and rules to solve problems, particularly through the Prolog programming language. It explains the structure of Prolog programs, including facts, rules, and queries, and provides examples of how to create a knowledge base and run queries. Additionally, it includes activities and solutions related to querying and manipulating data within a Prolog knowledge base.

Uploaded by

Sooraj Rajmohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Further Programming

20.1 Programming Paradigms


Declarative Programming Concepts

Computer Science 9618


20.1 Programming Paradigms – DECLARATIVE Programming
Candidates should be able to:
• solve a problem by writing appropriate facts and rules based on supplied
information
• write code that can satisfy a goal using facts and rules.

Computer Science 9618 2


Declarative Programming Languages:
• Declarative programming is used to extract knowledge by the use of queries from a situation
with known facts and rules.
• Declarative programming uses statements of facts and rules together with a mechanism for
setting goals in the form of a query.
• A fact is a ‘thing’ that is known, and rules are relationships between facts.
• Writing declarative programs is very different to writing imperative programs.
• In declarative programming, the programmer can state the facts and rules in any order
before writing the query.

Prolog is a logic programming language widely used for artificial intelligence


and expert systems. uses predicate logic to write facts and rules
For example, the fact that France is a country would be written in predicate logic as:
country(france).
Another fact about France – the language spoken in France is French – could be written as:
language(france,french).
Here country and language are known as predicates.
France and French are known as arguments.
Computer Science 9618
Facts & Rules
country(france). • These facts are used to set up a knowledge base.
country(germany). • This knowledge base can be consulted using queries.
country(japan). For example, a query about countries that speak a certain
country(newZealand).
language, English, could look like this:
country(england).
country(switzerland). language(Country,english)
language(france,french).
A variable in Prolog – Country, in this example – begins with an
language(germany,german).
language(japan,japanese). uppercase-letter.
language(newZealand,english). This would give the following results:
language(england,english).
newZealand ,
language(switzerland,french).
language(switzerland,german). england.
language(switzerland,italian). The results are usually shown in the order the facts are stored in
the knowledge base.
When a query is written as a statement,
A query about the languages spoken in a country, Switzerland,
this statement is called a goal and the
could look like this:
result is found when the goal is satisfied
using the facts and rules available. language(switzerland,Language).
This would give the following results:
french, german, italian.
Computer Science 9618
Prolog Basics

There are three basic constructs in Prolog: facts, rules and queries.
1. The program logic is expressed using clauses (facts and rules). A
collection of clauses is called a 'knowledge base'. Writing a Prolog
program means writing a knowledge base as a collection of clauses.
2. Problems are solved by running a query (goal).
3. Prolog is case sensitive.
A clause is of the form:

Head: -Body.

Note that a clause always terminates with a full stop(.)

Computer Science 9618


Prolog Basics
Datatype in Prolog:
Prolog has a single data type, called a 'term'.
A term can be:
1. an atom, a general-purpose name with no inherent meaning that
always starts with a lower-case letter.
2. A number, integer or float (real).
3. A variable, denoted by an identifier that starts with a capital letter
or an underscore (_).
4. A compound term, a predicate, consisting of an atom and a
number of arguments in parentheses.
5. If we are not interested on any one of the argument in a fact,
Anonymous variable: we can use anonymous variable. It is
represented by underscore character.

Computer Science 9618


RULE Symbols
Prolog Basics AND ,
Using rules in a knowledge base OR ;
Consider the following knowledge base: IF :-
01 parent(fred, jack). /* Fred is the father of Jack */ A person has a sibling (brother or sister) if they
02 parent(fred, alia). have the same parent. We can write this as the
03 parent(fred, paul). Prolog rule:
04 parent(dave, fred). sibling(A, B) :-
We know that G is a grandparent of S, if G is a parent parent(P, A),
of P and P is a parent of S. parent(P, B).
We could write this as a rule: If we run the query
grandparent(G, S) IF parent(G, P) AND parent(P, S).
sibling(jack, X).
However, in Prolog the IF is replaced by :- and the we get the answers we expect, but we also get the
AND is replaced with a comma answer that Jack is his own sibling. To avoid this, we
modify the query to ensure that A is not equal to B:
grandparent(G,S):-
parent(G,P), sibling(A, B) :-
parent(P,S) parent(P, A),
parent(P, B),
not(A=B).
Computer Science 9618
Example
Here is a knowledge base for the interest paid on bank accounts. The facts about each account include the
name of the account holder, the type of account (current or savings), and the amount of money in the account.
The facts about the interest rates are the percentage rate, the type of account and the amount of money
needed in the account for that interest rate to be paid.
Here is an example of a query using the
above rule:

Example-1
savingsRate(stefan,X).
And here is the result:
fivePercent

Here are examples of queries to find bank


account details:

Example-2
bankAccount(laila,X,Y).
bankAccount(victor,X,Y).
And here is the result:
current, 500.00
false

Computer Science 9618


Activity-1
Carry out the following activities
using the information above.
1 Write a query to find out the interest
rate for Laila’s bank account.
2 Write a query to find who has savings
accounts.
3 a) Set up a savings account for Robert
with 300.00.
b) Set up a new fact about savings
accounts allowing for an interest rate of
7% if there is 2000.00 or more in a savings
account.

Computer Science 9618


Activity Solution

Carry out the following activities using the


information above.
1 Write a query to find out the interest rate for Laila’s
bank account.
savingsRate(Laila,X).
2 Write a query to find who has savings accounts.
bankAccount(Name,savings,_).
3 a) Set up a savings account for Robert with 300.00.
bankAccount(robert,savings,300.00)
b) Set up a new fact about savings accounts allowing for
an interest rate of 7% if there is 2000.00 or more in a
savings account.
interest(sevenPercent,savings,2000.00).

Computer Science 9618


language(fortran,highLevel).
Activity-2 language(cobol,highLevel).
Use the knowledge base below to answer the language(visualBasic,highLevel).
following questions: language(visualBasic,oop).
a) Write two new facts about Java, showing that it language(python,highLevel).
is a high-level language and uses OOP. language(python,oop).
b) Show the results from these queries language(assembly,lowLevel).
i) teaching(X). language(masm,lowLevel).
ii) teaching(masm). translator(assembler,lowLevel).
c) Write a query to show all programming translator(compiler,highLevel).
languages translated by an assembler.
teaching(X):-
language(X,oop),
language(X,highLevel).

Computer Science 9618


Activity Solution

Activity-2 language(fortran,highLevel).
language(cobol,highLevel).
Use the knowledge base below to answer the language(visualBasic,highLevel).
following questions: language(visualBasic,oop).
a) Write two new facts about Java, showing that it language(python,highLevel).
is a high-level language and uses OOP. language(python,oop).
language(java, highLevel) language(assembly,lowLevel).
language(java, oop) language(masm,lowLevel).
a) Show the results from these queries translator(assembler,lowLevel).
i) teaching(X). translator(compiler,highLevel).
visualBasic;
Python teaching(X):-
ii) teaching(masm). language(X,oop),
false language(X,highLevel).
a) Write a query to show all programming
languages translated by an assembler.
language(Language,lowlevel).

Computer Science 9618


9608-s15-qp-
42
QUESTION
A declarative programming language is used to
represent the knowledge base shown below:
01 capital_city(amman).
02 capital_city(beijing). These clauses have the following meaning:
03 capital_city(brussels).
04 capital_city(cairo).
05 capital_city(london). Clause Explanation
06 city_in_country(amman, jordan).
07 city_in_country(shanghai, china). 01 Amman is a capital city
08 city_in_country(brussels, belgium). 06 Amman is a city in the country of
09 city_in_country(london, uk). Jordan
10 city_in_country(manchester, uk).
11 country_in_continent(belgium, europe). 11 Belgium is a country in the
12 country_in_continent(china, asia). continent of Europe
13 country_in_continent(uk, europe). 14 The travel writer visited Amman
14 city_visited(amman).
15 city_visited(beijing).
16 city_visited(cairo).

Computer Science 9618


9608-s15-qp-42

Computer Science 9618


9608-s15-qp-42

Computer Science 9618


9608-s15-qp-42

Computer Science 9618


9608-s15-qp-42 Solution

Computer Science 9618


9608/41/M/J/
18
QUESTION

Computer Science 9618


9608/41/M/J/
18
QUESTION

Computer Science 9618


9608/41/M/J/
18
QUESTION

Computer Science 9618


9608/41/M/J/
18
QUESTION

Computer Science 9618


9608/41/M/J/ SOLUTION
18

Computer Science 9618


9608/42/M/J/
18
QUESTION

Computer Science 9618


9608/42/M/J/
18
QUESTION

Computer Science 9618


9608/42/M/J/
18
QUESTION

Computer Science 9618


9608/42/M/J/
18
QUESTION

Computer Science 9618


9608/42/M/J/ SOLUTION
18

Computer Science 9618


9608/41/M/J/20 QUESTION

Computer Science 9618


9608/41/M/J/20 QUESTION

Computer Science 9618


9608/41/M/J/20 QUESTION

Computer Science 9618


9608/41/M/J/20 SOLUTION

Computer Science 9618


9608/42/M/J/20
QUESTION

Computer Science 9618


9608/42/M/J/20
QUESTION

Computer Science 9618


9608/42/M/J/20
QUESTION

Computer Science 9618


9608/42/M/J/20 SOLUTION

Computer Science 9618

You might also like