Saturday, March 6, 2010

Logic Programming

Prolog (PROgramming in LOGic) 2 is the most widely available language in the logic
programming paradigm. It is based on the mathematical ideas of relations and logical
inference. Prolog is a declarative language meaning that rather than describing
how to compute a solution, a program consists of a data base of facts and logical
relationships (rules) which describe the relationships which hold for the given application.
Rather then running a program to obtain a solution, the user asks a question.
When asked a question, the run time system searches through the data base of facts
and rules to determine (by logical deduction) the answer.
Logic programming was an attempt to make a programming language that enabled
the expression of logic instead of carefully specified instructions on the computer.
In the logic programming language Prolog you supply a database of facts and
rules; you can then perform queries on the database.
This is also an example of a declarative style of programming where we state or
define what we know.
In the following example, we declare facts about some domain. We can then query
these facts—we can ask, for example, are sally and tom siblings?
sibling(X,Y) :− parent(Z,X), parent(Z,Y).
parent(X,Y) :− father(X,Y).
parent(X,Y) :− mother(X,Y).
mother(trude, sally).
father(tom, sally).
father(tom, erica).
father(mike, tom).
The factorial function is written in prolog as two rules. Again, notice the declarative
nature of the program.
fac(0,1).
fac(N,F) :− N > 0,
M is N − 1,
fac(M,Fm),
F is N Fm.
To summarize:

• In procedural languages, everything is a procedure.
• In functional languages, everything is a function.
• In logic programming languages, everything is a logical expression (predicate).
• In object-oriented languages, everything is an object.

No comments:

Post a Comment