Born to be geek!

Mind the gap (between Prolog and Python)

Escrito el Martes 14 Julio 2009

During the last four or five years I have programmed almost exclusively in Python, a well known object-oriented programming language, very popular in the free / open source software community. In the last months, I have had to program in Prolog, a logic programming language, because of the research topics I am currently working on.

At a first glance, it seems that both languages are very distant from each other. Python code is read almost as natural language, and Prolog code is more like mathematical statements. However, when learning Prolog, you realize that many of the features of the language are also present in Python. For instance, I always have liked that in Python variables can contain anything, including methods or functions. Or every time I needed to parse text, I felt very comfortable doing it in Python. Managing lists (or any other iterative structure) is quite easy in Python. Interestingly, among the self-raised strong points of Prolog, you will find those.

Actually, a quick look in Google will point out that there are shared interests and concerns between Pythonists and Prologists. It seems that many of the people involved in the Python community come from the old logic programming school, with very interesting results. For instance, when learning Prolog, it is useful to try to solve a set of 99 problems, that are (somehow) ordered in increasing complexity. The Python community has solved those problems in Python, and it is quite interesting to have a look at the code of the same problem both in Prolog and Python. In spite of being problems specially suited to be solved using logic and declarative programming, they look rather clean in Python. For instance, problem 8 is about eliminating consecutive duplicates from a list. Here it is the code in Prolog:

% P08 (**): Eliminate consecutive duplicates of list elements.

% compress(L1,L2) :- the list L2 is obtained from the list L1 by
%    compressing repeated occurrences of elements into a single copy
%    of the element.
%    (list,list) (+,?)

compress([],[]).
compress([X],[X]).
compress([X,X|Xs],Zs) :- compress([X|Xs],Zs).
compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).

Four lines of code, implemented using recursion (as typically done in Prolog), that are very easy to read.

The same problem solved in Python:

from itertools import groupby
def compress(alist):
     return [key for key, group in groupby(alist)]

Only three lines, implemented taking the advantage of the great library framework that comes with Python. Ok, this is kind of cheating, because Python has a very thorough standard library, that is not part of the language itself. Let’s look at a more complicated problem: problem 17, split a list in two parts, given the “splitting” index.

The code in Prolog is the following:

% P17 (*): Split a list into two parts

% split(L,N,L1,L2) :- the list L1 contains the first N elements
%    of the list L, the list L2 contains the remaining elements.
%    (list,integer,list,list) (?,+,?,?)

split(L,0,[],L).
split([X|Xs],N,[X|Ys],Zs) :- N > 0, N1 is N - 1, split(Xs,N1,Ys,Zs).

The same problem solved in Python:

 def split(L, N):
        return L[:N], L[N:]

Interesting, huh?

Of course, there is probably a lot of problems that are much easier to solve and look much cleaner in Prolog than in Python. But my point is that logic programming can be easily done in Python, and that many of the strong points of Prolog (like lists or iterative data structures managing and traversing) are by default also part of Python.

Anyway, I admit that changing from Prolog to Python (or the other way around) may be difficult, and that for certain kinds of problems, there is always a superior language. Considering this, wouldn’t it be great to have both Python and Prolog available in the same program? That will be the topic of the next post.


  1.  
    17.7.2009 | 12:15 am
     

    [...] Born to be geek! « Mind the gap (between Prolog and Python) The interplay between Prolog and Python Escrito el Viernes 17 Julio [...]

Disculpa, los comentarios están cerrados.