Thursday, July 19, 2012

Calling Prolog from Foreign Languages

When you define Prolog clauses for global predicates  as being of foreign language, those predicates may be called from foreign languages. They will have parameter access and entry and exit code, including register preservation, as for the language specified.
Example:predicate called from .pro in C language
/* Program hello_p.pro */
global predicates
char prohello_msg(string) - (i) language c
hello_c - language c
clauses
prohello_msg(S,C) :-
write(S," (press any key)"), readchar(C).
goal
prohello_msg("Hello from  Prolog"),
hello_c.

The global predicate prohello_msg is now accessible from C and can be called just like any other C function:

Tuesday, July 17, 2012

Monkey- Banana Problem in Prolog

Monkey-Banana Problem is the famous problem in AI. Where there is a room containing a monkey, a chair, and bananas that have been hung from the center of the ceiling of the room; out of reach from monkey. If the monkey is cleaver enough, he can reach the bananas by placing the chair directly below the bananas and climbing on the top of the chair.
Now the problem is to use FOPL to represent this monkey-banana problem and prove that monkey can reach the bananas.
The program is given below. Before running the program, think carefully what are the essential objects of the problem and how should them be arranged in predicate logic. 

PREDICATES
in_room(symbol)
dexterous(symbol)
tall(symbol)
can_move(symbol,symbol,symbol)
can_reach(symbol,symbol)
get_on(symbol,symbol)
can_climb(symbol,symbol)
close(symbol,symbol)
under(symbol,symbol)
can_climb(symbol,symbol)
 

Saturday, June 30, 2012

FOPL and Prolog In AI part 04

Introduction
First Order Predicate Logic (FOPL) is a generalisation of Propositional Logic. Logic Programs are written in a sub-language of FOPL and therefore derive their meaning and formal properties from it.
FOPL has two major extensions over Propositional Logic:

1. Propositions are renamed "predicates" and may possess an internal structure. In fact the predicates of FOPL have precisely the same syntactic structure as they have in Prolog - i.e. a predicate name with terms as arguments, each term being aa
-constant,
-variable, or
-funcion with term(s) as argument(s).

2. Variables must be quantified by either
A meaning "for all" or
E " "there exists"
e.g Ex P(x) means "some unspecified constant has property P". Each quantifier has a SCOPE which is defined as the textual area in which it binds occurrences of its variable. This scope is usually delimited by brackets, unless it is obvious as in the example above.

Friday, June 29, 2012

Constraint satisfaction problems In AI part 03


Introduction
            Constraint programming is a useful tool in formulating and solving problems that can be defined in terms of constraint among a set of variables. In fact real world problems are constraint satisfaction problems defined in terms of some variables that bear some constraints. Finding a set of variables, that are within the constraints given(or observed) is a solution to that problem.
            Let us consider a problem, that can be represented by some relations of the variables x, y and z. We have a domain Dx, Dy, Dz from where the variables can take a value. The constraint is given by a set C and may have a number of constraints C1,C2,C3,etc each relating some or all of the variables x,y and z. Now a solution (or solutions) to the problem is a set dx,dy,dz such that dx   Dx,  dy   Dy and dz   Dz and all the constraints of the set C are satisfied.

Eight queens problem
            Eight queens problem is a constraint satisfaction problem. The task is to place eight queens in the 64 available squares in such a way that no queen attacks eachother. So the problem can be formulated with variables x1,x2,x3,x4,x5,x6,x7,x8 and y1,y2,y3,y4,y5,y6, y7,y8; the xs represent the rows and ys the column. Now a solution for this problem is to assign values for x and for y such that the constraint is satisfied.
The problem can be formulated as
P={(x1,y1),(x2,y2),……………………..(x8,y8)}
where (x1,y1) gives the position of the first queen and so on.

            So it can be clearly seen that the domains for xi and yi are
Dx = {1,2,3,4,5,6,7,8}and Dy ={1,2,3,4,5,6,7,8} respectively.

BackTracking in Artificial Intelligence part 02




Backtracking
As has already been seen prolog has built in backtracking mechanism. It tries to prove a goal with all possible instantiations. Automatic backtracking is a useful programming concept because it reveals the programmer of the burden of backtracking explicitly. However in some cases this feature degrades the efficiency of the program.

For example in cases where one solution is sufficient, backtracking to find all the solutions is not a good idea. Similarly, in case of mutually exclusive rules(clauses) when one rule has been proved then it is known in advance that no other rules can succeed. So this backtracking can be controlled by the use of ‘cut’, (“!”).

The disadvantage of using cut is that we tend to move away from the declarative nature of the prolog because when we have used the cut the order of the clauses may make difference in the result we get.
              



Consider the function shown in the above figure. The relation between X and Y can be specified by the following three rules.
Rule 1: if X<3 then Y=0
Rule 2: if 3=<X <6 then Y=2
Rule 3: if 6<X then Y=4

This can be programmed as
PREDICATES
f(integer,integer)

CLAUSES
f(X,0):-
                X<3.
f(X,2):-
                3<=X,X<6.
               
f(X,4):-
                6<X.
               
GOAL
f(2,X).

 Assignment1.)

Now modify the program using cut and observe the difference between the two modules. Comment on the difference.

Solution:

Introduction to Prolog for AI programming part 01

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. 


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.