Thursday, May 10, 2012

Artificial Intelligence (AI):Programming with VISUAL Prolog


PROLOG (for PROgramming in LOGic) is a very important tool in programming artificial intelligence applications and in the development of expert systems. It allows the programmer to model the logical relationships among objects and processes, complex problems are inherently easier to solve, and the resulting program is easier to maintain through its lifecycle. With Visual Prolog, applications such as customized knowledge bases, expert systems, natural language interfaces, and smart information management systems are easy to develop. Prolog is 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.

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.


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.                                                                                               For example: < ----- >                                                                                                      Care should be taken not to use the character combination that may some built in meaning.
  • Strings of characters enclosed in quotes.                                                                               For example: ‘Hari’    ‘Bird’ etc.                                                                                         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.

Visual Prolog Program to find the bigger of the Two Numbers.

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. 

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).


Visual Prolog 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).



Visual Prolog program combining facts and rules

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.


PREDICATES
husband(STRING,STRING)
father(STRING,STRING)
mother(STRING,STRING)
son(STRING,STRING)

CLAUSES
mother("Kushalya","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").


Visual Prolog Program to add the contents of an integer list.

Here in this program, the add function is made that adds the contents of list. The first segment ( i.e the Domains section ) defines or declares the space dynamically for integer list. Then the predicate add is defined that takes integer list and a Integer as arguments. The next clause section provides the necessary reasoning and facts to find the solution to the problem. Here the predicate add is called recursively until it gets the last element of the list which is the empty list itself and returns 0 if empty list is encountered. The add predicate else adds the first element to the solution repeatedly to find the solution.

DOMAINS
int_list=Integer*
PREDICATES
            add(int_list, Integer)
CLAUSES
            add([],0).
            add([H|T],Y):- add(T,Y1),Y=Y1+H.                                                
GOAL
add([3,2,1,0],X).


                                                                      

















Visual Prolog Program to find length of a list.

In this program the predicate length takes integer list and output a integer which is the length of list. Here the predicate length is called repeatedly or recursively until a last empty list is encountered and the 0 value is returned which is then continuously incremented to find the length.

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,3,4,5,6],X).

Visual Prolog Program to find the factorial of a number.

To find the factorial of a number in Visual Prolog, the number is decreased and the predicate “factorial” is continuously until a Zero is encountered when it returns a value 1. Then the predicate multiplies the returned value and the decremented number continuously. Thus, the factorial of a number is found. The visual prologue code to find the factorial of a number is given below:

PREDICATES
            factorial(Integer,Integer)
Clauses
            factorial(0,1).
            factorial(X,Y):-                      
                        X<>0,S=X-1,factorial(S,Y1),Y=X*Y1.
                                                           
Goal
factorial(5,X).

Visual Prolog Program to append two list.

Here the two list are taken by the predicate “append” and returns the appended list. To append two list a list is broken continuously until the last empty list is encountered and finally the other list is appended or joined at the end of first recursively. The code to append is given as follows:

DOMAINS
int_list=Integer *

PREDICATES
append(int_list,int_list,int_list)
CLAUSES

append([],X,Z):-
            Z=X.
append([H|T],X,Y):-
            append(T,X,Z),Y=[H|Z].
GOAL
append([1,2,3,4,5],[6,7,8,9],X).

Visual Prolog Program to delete a given element from the list.

To delete a element from a list the position and the element list is passed to the predicate delete. Then  the delete predicate continuously breaks the list into head (H) and Tail (T) to find that the correct position is reached. On reaching the appropriate position to delete the element the predicate slices the head off the list and finally merges the remaining art with the rest. The prolog code is given as below:

DOMAINS
int_list=Integer *

PREDICATES
delete(integer,int_list,int_list)
CLAUSES

delete(0,[H|T],T).
delete(P,[H|T],[H|Z]):-
            P1=P-1,delete(P1,T,Z).
GOAL
delete(0,[1,2,3,4,5,6,7,8,9],X).