## Prolog, Logic Programming and Programming Paradigm

Recently I did research about Prolog language in my Artificial Intelligence class. I finally got a chance to see what logic programming really is. At the very start I just want to write something about Prolog, but it seems like I have so many things to talk about, so this post ends up with the title of Prolog, Logic Programming and Programming Paradigm.

Class-based and Prototype-based, declarative programming and imperative programming, Object-oriented and procedure-oriented... all these programming paradigms are so disordered and complicated, but the Wikipedia has a brief definition of Programming Paradigm:

A programming paradigm is a fundamental style of computer programming.

Bazinga, Im really not into these methodology things, so I found this Principal Programming Paradigms chart made by Peter Van Roy：

From this chart we know that its usual that a single language supports kinds of programming paradigms, which is not so cool for simplists. Obviously this chart desnt specify every paradigm, I quoted it because this kind of colorful charting looks really smart and the sentence under the title says:

More is not better (or worse) than less, just different.

I cant agree it any more, at the same time I cant help to add something behind this short sentence:

but less is beautiful.

Anyway, I googled it and picked up four main programming paradigms from the Wikipedia List:

• Imperative Programming
• Functional Programming
• Object-oriented Programming
• Logic Programming

note: about computational model，functional programming is besed on lambda calculus, logic programming is based on first order logic, and object-oriented and imperative programming are based on Turing machine.

## Imperative Programming

First DO THIS and next DO THAT

Follow the ideas of Von Neumann, strict like training discipline, representative languages are Fortran, Pascal and C.

## Functional Programming

Evaluate an expression and use the resulting value for something

Based on mathematics and theory of functions, simpler and more clean than the imperative one. lan The reason is that the paradigm originates from a purely mathematical discipline: the theory of functions, representative languages are Lisp, Erlang and Haskell. (Seems like functional programming is becoming popular...WHY）

## Object-oriented Programming

Send messages between objects to simulate the temporal evolution of a set of real world phenomena

Most polular paradigm in recent decade, bases on Object concepts, supports encapsulation, inheritance and polymorhism, representative languages are Smalltalk, C++ and Java.

## Logic Programming

Answer a question via search for a solution

Dramatically different from the other three main programming paradigms, based on first order logic, most applied in Artificial Intelligence, representative languages are Prolog, Mercury and Logtalk.

Logic Programming sloves problem by setting rules rather than setting steps, the results are generating by matching lots of facts and rules. Actually, the implementation of logic programming is so simple that many other languages can do, but they should spend more execution efficiency and computing cost than Logic Programming Language. As an example, next I will show you how Prolog works.

## PROLOG

Prolog(Programming in Logic) was first conceived by a group around Alain Colmerauer in Marseille, France, in the early 1970s and the first Prolog system was developed in 1972 by Colmerauer with Philippe Roussel. Still, it widely used in many Aritificial Intelligence like Nature Language Processing, Expert System, etc. I really dont like rebuild wheels, so you guys can click here to check the Prolog tutorial "Learn Prolog Now!" wrote by Patrick Blackburn and his partners, the development tool I strongly recommend SWI-Prolog, an little fast opensource Prolog tool.

Prolog is simple but effcient, it takes just two lines to solve the hanoi problem:

// prolog hanoi move(1,X,Y,\_):-write('Move top disk from '),write(X),write(' to '), write(Y), nl. move(N,X,Y,Z):-N>1,M is N-1,move(M,X,Z,Y),move(1,X,Y,\_),move(M,Z,Y,X). view rawprolog1.pro hosted with ❤ by GitHub

I will make an simple example to show the three statements that Prolog has: fact, rule and result.

Assume we have a family tree like this:

Define the facts and rules in family.pl like this:

/* 事实 */male(baba).male(yeye).female(nainai).female(mama).male(wo).father(yeye,baba).father(baba,wo).mother(nainai,baba).mother(mama,wo)./* 规则 */grandfather(X,Y):-father(X,Z),father(Z,Y).grandmother(X,Y):-mother(X,Z),father(Z,Y).daughter(X,Y):-parent(X,Y),female(Y).son(X,Y):-parent(Y,X),male(X).parent(X,Y):-father(X,Y);mother(X,Y).ancestor(X,Y):-parent(X,Y).ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).

The results begin with " ?- ", query like this :

Whats more, we can also use prolog to solve NLP or Expert System stuff, the basic mechanism is nearly the same.