Wednesday, August 22, 2012

PROLOG: Intelligent Logic Programming in AI (Part I)

What is PROLOG?
Prolog was invented in the early seventies at the University of Marseille. Prolog stands for PROgramming in LOGic. It is a logic language that is particularly used by programs that use non-numeric objects. For this reason it is a frequently used language in Artificial Intelligence where manipulation of symbols is a common task. Prolog differs from the most common programmings languages because it is declarativre language. Traditional programming languages are said to be procedural. This means that the programmer specify how to solve a problem. In declarative languages the programmers only give the problem and the language find himself how to solve the problem.
What is the difference between PROLOG and the languages you have studied ever?
You will see that Prolog is quite different from other programming languages you have studied. First, Prolog has no types. In fact, the basic logic programming environment has no literal values as such. Identifiers starting with lower-case letters denote data values (almost like values in an enumerated type) while all other identifiers denote variables. Though the basic elements of Prolog are typeless, most implementations have been enhanced to include character and integer values and operations. Also, Prolog has mechanisms built in for describing tuples and lists.
             Remember that all programming languages have both declarative (definitional) and imperative (computational) components. Prolog is referred to as a declarative language because all program statements are definitional. In particular, a Prolog program consists of facts and rules which serve to define relations (in the mathematical sense) on sets of values. The imperative component of Prolog is its execution engine based on unification and resolution, a mechanism for recursively extracting sets of data values implicit in the facts and rules of a program. In this section you will be briefly introduced to each of these terms.

What are Facts, Rules and Predicates?

Facts and Rules
Everything in Prolog is defined in terms of two constructs: the fact and the rule. A fact is a Prolog statement consisting simply of an identifier followed by an n-tuple of constants. The identifier is interpreted as the name of a (mathematical) relation and the fact states that the specified n-tuple is in the relation. In Prolog a relation identifier is referred to as a predicate; when a tuple of values is in a relation we say the tuple satisfies the predicate.

More complicated facts consist of a relation and the items that this refers to. These items are called arguments. Facts can have arbitrary number of arguments from zero upwards. A general model is shown below:
   
 relation(<argument1>,<argument2>,....,<argumentN> ).

The arguments can be any legal Prolog term. The basic Prolog terms are an integer, an atom, a variable or a structure. Various Prolog implementations enhance this basic list with other data types, such as floating point numbers, or strings. Exemple:
    
likes(john,mary).

In the above fact john and mary are two atomes. Atoms are usally made from letters and digits with lowercase characters. The underscore (_) can also be used to separe 2 words but is not allowed as the first charactere. Atoms can also be legally made from symbols.

Consider the following sentence: 'All men are mortal'.  We can express this thing in Prolog by:
     mortal(X) :- human(X)

The clause can be read as 'X is mortal if X is human'.
To continue with this example, let us define the fact that Socrate is a human. Our program will be:
    
mortal(X) :- human(X).

    
human(socrate).

Now if we ask to prolog :
    
?- mortal(socrate).

Prolog will respond :
   
 Yes

SWI PROLOG Environment:

The version of Prolog that we will use is called SWI-Prolog, developed at the Swedish Institute of Computer Science. The SWI-Prolog environment is an interactive system, much like the Hugs functional programming environment.

After SWI-Prolog has been installed on a Windows system, the following important new things are available to the user:
  • A folder (called directory in the remainder of this document) called pl containing the executables, libraries, etc. of the system. No files are installed outside this directory.
  • A program swipl-win.exe, providing a window for interaction with Prolog. The program swipl.exe is a version of SWI-Prolog that runs in a DOS-box.
  • The file-extension .pl is associated with the program swipl-win.exe. Opening a .pl file will cause swipl-win.exe to start, change directory to the directory in which the file-to-open resides and load this file.
How to write and run programs in SWI-Prolog:

1. Go to \program files\pl\bin
2. Open a new Notepad file
3. Write the prolog code there
4. Save the file using .pl extension
5. The file will be saved as a prolog file
6. By double clicking execute the file

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.