Thursday, November 27, 2008

Prolog

Prolog is a programming language, but it differ from procedural or object oriented languages. It is used mostly by logic-oriented programs. So let define some basics.
Atom is any string which begins with lower case and consist letters,numbers and underscore character, or any string enclosed in single quotes, or special character. For example
  • a,b,c, ab, aB, aC_ are atoms.
  • 'asb',''&^%&#@$ &*' are atoms.
  • special characters are @= and ====> and ; and :- and etc...
Number - any number.
Variable is a string of letters,numbers and underscore, which begins with uppercase letter or with underscore. Examples
  • X, Y, AX, Axx, _aAb, _ABA are variables
  • _ is special variable and it is called the anonymous variable.
Complex term (or compound term) is an atom(called functor) plus arguments, which are terms or variables or numbers or atoms.

The prolog program is consist of several rules or facts, which are separated by '.'.

The rule is of the form
head :- body.
and is read as "If body is true then head is also true". When the body is empty then it called fact and is written without :- operator. The head is complex term, but the body can have several terms which used together with conjunction(',') or disjunction(';'). For example
a(x) :- b(x), c(x).
If b(X) and c(X) are true then a(X) is true.
f(x) :- d(x); e(x).
If d(X) or e(X) are true then f(X) is true.

So we write some rules, but what the prolog program do. It just reads the rules and wait, until we ask some questions. Before discussing some real examples,let I give this link to the site of SWI-prolog(the popular implementation). There you can download and install the installation packages for variety of OS. After finishing, you can lunch the environment itself or write the prolog code in file with extension pl and lunch it. In second case SWI will read the rules from that file and get ready to answer the questions, in first case you must manually add rules.

Now let's discuss a simple example. Let a,b,c,d be the some properties of X and

a(X) :- b(X) , c(X).
d(X) :- b(X) ; c(X).

and 1 has properties b and c, 2 has property c.

b(1).
c(1).
c(2).

So now we can write down this into a file and run it, after it let give it some questions.
?- b(1)
true
it answers true, because 1 has the property b.

?- a(1)
true
it answers true, because 1 has the property b and c and from rule 1 has property a.

?-a(2)
false
because 2 has property c but hasn't property b so it hasn't property a.

In same way
?-d(1)
true

?-d(2)
true

Now an unusual question.
?-d(3)
false
because we don't know anything about 3.

This language can be showed simple in first view, but using it we can write some hard programs, which takes several time less time and less code size than in other programming languages. Next time I will tell more about Prolog and about ncDP prover program. Also I want to mention this site where you can find a good free book about Prolog.

1 comment:

Alan Curtis said...

I want to share with you all here on how I get my loan from Mr Benjamin who help me with a loan of 400,000.00 Euro to improve my business, It was easy and swift when i apply for the loan when things was getting rough with my business Mr Benjamin grant me loan without delay. here is Mr Benjamin email/whatsapp contact: +1 989-394-3740, 247officedept@gmail.com /