Why Learning Prolog Can Make You a Better Programmer
In Computer Science programs, there is a tradition of having a course of two that represent the watershed between people that can grok CS concepts and the ones that can’t. Although anyone can do some programming in a language like Java, where anecdotal evidence seems to show that the most complicated concept is class, it takes real intelligence to do something useful in C or Lisp.
At my Alma mater, a course in Prolog was the division line between the hopeful and the real CS guys. The reasoning is that if you could get your head around a Prolog interpreter written in Prolog, then you can probably handle any programming concept.
These days however, schools are more interested in teaching marketable languages such as Java or Python. It seems like that they are looking for ways of getting people through the program as quick as possible (maybe before they realize where they are really going into).
A little of Prolog
Prolog is nowadays not as trendy as it used to be, although at one time it was the main competitor of Lisp (at least for AI-people living in Europe). However, it is often said that it is an advantage to learn Lisp even if it is just for the learning experience.
I think it is just as fair to say the same of Prolog, and here are some of the reasons why Prolog still has something to teach most programmers.
Prolog is a pure functional language. In fact, it is so pure that the only values it returns are the literals true or false. In Prolog, everything happens as a binding of parameters, so there are no variables to update?—?only method calls.
The language provides a simple mechanism for accessing databases. Databases are viewed in Prolog as a set of facts that can be consulted by the rules. In this way, access to external data is treated in a logical fashion, instead of requiring imperative actions to fetch and read data. The Prolog model provides reduced impedance between code and data, which is good.
Provides a simple way to represent declarative programs. Prolog is based on matching rules in a way that is transparent to developers. It tries to satisfy the requirements of each boolean function using its internal rule-matching engine. It is easy to think of this as a non-deterministic machine that just does the right thing as needed, without programming intervention. Of course, this machinery won’t solve any problem without some additional help, but by necessity it frees the developer from thinking on how things are done, and concentrate instead in what is being done.
In summary, I think that good programmers should explore alternative ways to create software. Learning languages with a different paradigm, such as logic programming, is an excellent way of challenging traditional thinking processes. Understanding how things could be written in Prolog may provide better solutions to practical problems, even if you need to write the resulting code in C.
Further Reading
There are not many new books devoted to Prolog, but there is a classical one called The Craft of Prolog. I haven’t touched that book for several years, but it was the source of a lot of fun when I first started playing with the language. That is still the best source of wisdom when working with Prolog.









Wrong about “prolog is a pure functional language”
By aoeu on May 9, 2009
@aoeu, from a textbook definition point of view you are right. Prolog is better classified as a logic programming language since, among other things, Prolog goals can return only true or false.
However, learning Prolog may teach you more about the mindset of pure functional programming than Lisp, since Prolog works only by applying goals (boolean functions) that need to be satisfied.
By coliveira on May 9, 2009
My course included languages such as C (for the core programming classes), Java, Haskell, Python, Perl, TCL, and a few more, including Prolog. Prolog is the worse, crappiest language I’ve learned (did I mentioned I learned both Perl and Java?) and served no purpose what-so-ever in my experience as a programmer. Haskell is a pure functional language, it can teach you a couple of things about typing and such and it can be fun to learn. Prolog is a twisted, asinine, slow language, whose only purpose can be that of getting people to learn how to trace the goddamn code to see where the recursion went caput. Yeah, great language, if you need to learn how to read execution stacks and how to debug crappy code.
The whole “Databases are viewed in Prolog as a set of facts that can be consulted by the rules” sounds kinda cool, until you realize that using datalog, besides being painfully slow, is incredibly difficult to implement for any a bit more complicated query. Also, what about actually interfacing prolog and a database engine in order to turn it into facts? There are all kinds of fun to be had there. I hear someone actually got prolog to kinda almost interface with mysql once.
I’ve used prolog for 3 or 4 classes, using many of the language’s applications (such as optimizations, datalogging and of course, basic language learning) and it sucks. Learn Haskell instead. Hell, learn python in depth, it has a couple of interesting features there, from an academic standpoint. You can even learn Lisp (hey, emacs is great, so it’s not a wasted effort). But please, for your sanity, steer clear of prolog.Just…
No.
By YouAreWrong on May 9, 2009
Hi YouAreWrong, I understand your frustration, but maybe you were using the language for something it was not designed. Prolog shines at solving AI-related problems, where reasoning based on facts is required.
Also, we cannot bash a language for its implementation. Most implementations of Prolog out there are crappy, but it doesn’t mean that you can’t do something great using these ideas. For example, Lisp implementations sucked for a long time, but nowadays it is considered a fast language.
By coliveira on May 9, 2009
@YouAreWrong
“Prolog is the worse, crappiest language I’ve learned”
“I’ve used prolog for 3 or 4 classes”
Wow, we’ve got an expert here !
“Prolog is a twisted, asinine, slow language”
What about the logic programming paradigm ? Anything profound to say about its underpinnings ?
By zorg on May 10, 2009
I read about Prolog here:
weblogs.java.net
I think it has its place and there are many things to be explored ;)
By NewsReader on May 23, 2009
Actually I think it is a great language – why?
1. Because performance is not important for all types of applications. In fact, performance is your most important concern you should not be using Java/Python/TCL either.
2. you may model a specific domain effectively, entities of that domain can be made first class citizens from a language point of view.
3. the language of thought and prolog are very close to each other.
4. with respect to ” hear someone actually got prolog to kinda almost interface with mysql once.” by YouAreWrong – there are indeed a lot of examples which use Prolog to provide a natural language interface to some resource such as unix shells/ database/ etc.
5. if one frees its mind from any previous (imperative) programming experience, one actually may learn Prolog quite fast because there are only few major concepts to learn.
I hope that this has shed some light a bit.
By bitti on May 27, 2009
Yup
bitti is right!
Prolog is designed for symbolic reasoning, and not for numerical reasoning. And that is the reason why it does not have any great constructs for numerical processing.
If performance is the measure, than surely Prolog is not the thing!
But the fact is for the application of the future, the performance will not be the only measure. Things are changing and focus is slowly shiftingfrom the conventional applications to the ones like NLP, Robot Navigation etc.. And its possible that some efforts done may result in some implementations of Prolog that will give high performance at these tasks.
By ankit on Dec 26, 2009