PROLOG is what is known as a declarative language. This means that given the necessary facts and rules, Prolog will use deductive reasoning to solve problems. This is in the contrast to traditional computer languages, such as C, BASIC and Pascal, which are procedural languages. We can also use prolog as any other programming languages in a procedural manner.
So prolog can be viewed as a tool to solve problems in the field of artificial intelligence or it can be very well used a general programming language.
Prolog enforces the different problem solving paradigm complementary to traditional programming languages so it is believed that a student of computer should learn programming in prolog.
With Visual Prolog, applications such as customized knowledge bases, expert systems, natural language interfaces, and smart information management systems are easy to develop.
With Visual Prolog, applications such as customized knowledge bases, expert systems, natural language interfaces, and smart information management systems are easy to develop.
Data types in prolog
There are mainly three data types in prolog which are:
a) Atoms and numbers
b) Variables
c) Structures
a) Atoms and numbers
Atoms can be constructed in three different ways
- Strings of letters, digits, and the underscore character starting with a lower case letter.
- For example: man, ram, comp_students, pc_ct_063
- Strings of special characters.
- Care should be taken not to use the character combination that may some built in meaning.
- Strings of characters enclosed in quotes.
Numbers used in prolog are integers and real numbers.
b) Variables
Variables are strings of letters digits and underscore that start with an underscore or an uppercase letter. The scope of a variable is one clause only. So the same variable used in different clauses mean different thing.
For example: X, Y, Hari, _load etc.
Note here that Hari is a variable unlike the earlier use ‘Hari’ where it was a constant, an atom.
An underscore ‘_’ also known as anonymous variable is used in clauses when a variable need not be inferred to more than once.
c) Structures
Structures are objects that have different components. The components can be atoms or yet some other structures. A function is used to construct a structure as follows:
family (father, mother, children)
Here family is a structure that has father, mother and children as its elements. The father and mother may de atoms where the children may be yet another structure or a list of atoms. List is a special built in structure in prolog.
A list is a built in structure in prolog. It can be thought of sequence of elements ordered linearly however it is internally represented as a binary tree.
For example: [ albert, john, krish, willium ]
This representation is as follows:
The list as such can be broken down into two parts, the HEAD and the TAIL. The head is the first element of the list and tail is the remaining list. The above list can be broken down as:
[H|T]
Where H = albert
And T = [ john, krish, willium ]
List is one of the most useful structure in prolog.
Writing programs in prolog
All prolog programs start from the goal. It then uses the facts and clauses to break down the goal to sub-goals and tries to prove the sub-goals. A clause is said to have succeeded if there is a combination of facts and clause(s) that holds true.
Prolog has built in backtracking (it will be more clear in next lab) mechanism i. e. whenever three are multiple paths, it chooses one tries to prove it, and comes back to the choices whether the first one succeeds or fails.
Example 1: Run the following program and observe the result
PREDICATES
bigger (integer, integer, integer)
CLAUSES
bigger(X,Y,Z):-
X>Y,Z=X.
bigger(X,Y,Z):-
X<Y,Z=Y.
GOAL
bigger (4,9,X).
Discussion:
In this program, we have defined a predicate that gives us the larger of atwo given integers. The predicate bigger is defined in the PREDICATES section and the relation is defined in the CLAUSES section. It is customary to put together the predicate definition and clause definition for each predicate but it is not necessary. However, all the clauses for a predicate have to be written together.
The predicate section defines what types of relations or facts are going to use. It is somewhat like declaring functions in C programming. The clauses section holds the relation and facts can be compared to function definition.
Example 2: A Program to find hcf of numbers.
PREDICATES
hcf(integer, integer, integer)
CLAUSES
hcf(X, Y, X):-
Y mod X = 0.
hcf(X,Y,Z):-
S = Y mod X,S<>0, hcf(S, X, Z).
GOAL
hcf(5,10,X).
Example 3: A program combining facts and rules
PREDICATES
husband(STRING,STRING)
father(STRING,STRING)
mother(STRING,STRING)
son(STRING,STRING)
CLAUSES
mother("Kaushalya","Ram").
mother("Kaikai","Bharat").
mother("Sumitra","Laxman").
mother("Sumitra","Satrughan").
husband("Dasrath","Kaushalya").
husband("Dasrath","Kaikai").
husband("Dasrath","Sumitra").
son(A,C):- mother(C,A).
son(A,C):-husband(C,B), mother(B,A).
father(A,B):- husband(A,C), mother(C,B).
GOAL
son(X, "Kaikai").
Discussion:
Here the predicate mother is used to assert facts. There are four facts in that section. Similarly the predicate husband is used to assert more facts. The predicates son and father are used to define rules. We could have defined rules even with mother and husband but we bet that conciseness for program clarity at this stage.
Assignment: Test and comment on the result with other different goals.
Example 4: A program to find the length of a list
DOMAINS
int_list = integer*
PREDICATES
length(int_list, integer)
CLAUSES
length([ ], 0).
length([H|T], L):-
length(T,L1),
L=L1+1.
GOAL
length([1,2,5,2,1,6,7], X).
Assignment: Test the goal with different lists having lengths like [ ], [0] etc. How [ ] is executed?
Problems
- Write a program to find factorial of a number.
Hint: You can use predicate like factorial (integer, integer).
- Write a program to add the content of an integer list.
- Write a program to find length of a list.
- Write a program to append two lists.
- Write a program to delete a given element from the list.
source:IOE pulchowk campus,nepal
No comments:
Post a Comment