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 being 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, CS programs are more interested in teaching marketable languages such as Java, Python, or Ruby. 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

Nowadays Prolog is not as trendy as it used to be, although at one time it was the main competitor of Lisp as a high level language for complex applications (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 can be though as 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, which will match parameter values to formal arguments.

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 a good thing.

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 declared 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 here are some of the classic along with a few more recent ones:

The Craft of Prolog (by R. O’Keefe), which has been the source of a lot of fun since I first started playing with the language. That is still the best source of wisdom when working with Prolog.

The Art of Prolog (by Sterling and Shapiro) is another classic that describes the language in detail, along with some more advanced applications.

Programming in Prolog: Using the ISO standard is a more recent one, worthy the price because it describes the Prolog standard in detail. It should be part of any serious Prolog developer’s bookshelf.

Prolog Programming For Artificial Intelligence (by I. Bratko): a best-selling guide to Prolog and Artificial Intelligence, which has been updated to include key developments in the field.

Logic Programming with Prolog (by M.Bramer): a great tutorial of logic programming using Prolog as the main implementation language.

Clause and Effect: Prolog Programming for the Working Programmer (W. Clocksin): A subset of Prolog is used to demonstrate important concepts of the language.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

20 Responses to “Why Learning Prolog Can Make You a Better Programmer”

  1. Wrong about “prolog is a pure functional language”

    By aoeu on May 9, 2009

  2. @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

  3. 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…


    By YouAreWrong on May 9, 2009

  4. 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

  5. @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

  6. I read about Prolog here:

    I think it has its place and there are many things to be explored ;)

    By NewsReader on May 23, 2009

  7. 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

  8. 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

  9. I used Prolog to implement machine learning schemes and some other cool algos from games theory, it actually worked quite well to put together an application with gui implementation in java, but the prolog engine was really slow and the whole thing worked a lot better with a pure java implementation even though the the code was about 50 times bigger and a whole lot uglier.

    I thing the paradigm is interesting, but honestly I see no real practical use.
    I think we all need to accept prolog as kind of a programmers “rubrics cube” -fun as a brain teaser, but without real purpose..

    By u_turn on Dec 29, 2010

  10. I think Prolog is a language that can be used for real projects. It depends, however, on the type of project you want. Currently Prolog is better suited for AI-related projects. It could be extended to other domains, but the problem is that there is no critical mass of programmers trying to do this. Performance is currently an issue but it could vastly be improved if there were investments such is what is happening to Javascript (and happened with Java)

    By coliveira on Dec 31, 2010

  11. A long time ago I played with Prolog and thought it would make an interesting “brain” for a home automation system, so I wrote one. The Prolog rules are great for specifying the systems behavior in response to events. The system has now been running things in my house for almost 10 years.

    By Steve on Apr 8, 2011

  12. “currently Prolog is better suited for AI-related projects”. Which projects? Computer vision? Don’t think so. Machine learning? New research is using massive amounts of stats and mathematica programming. Natural language processing? Same thing.

    By Pedro on Apr 8, 2011

  13. @ Steve this really looks like a cool application. Creating a rule-based system for this is the most effective way to do automation.

    @Pedro I guess it depends on the lab where the research is performed. Most research on Prolog is done in Europe and Asia. For example, it was used to implement Erlang in Ericsson. A lot of natural language research is still done in Europe using Prolog. In the USA, however, it is hard to compete with Lisp and relatives, since most systems have been implemented in these languages.

    By coliveira on Apr 8, 2011

  14. A long time ago, I used Arity Prolog to parse ISCN notation describing human chromosomal abnormalities, so that one could search patients for a given abnormality (eg. Down Syndrome). Conventional databases wouldn’t work because string searching was not possible, since a given abnormality could have multiple descriptions. Fortunately, ISCN strings could be viewed as Prolog terms, so the program was all about writing hundreds of rules using Prolog’s notion of a variable. The whole package compiled to a 325K .EXE file and was blindingly fast. Prolog was tailor-made for this task.

    By Veteran on Apr 9, 2011

  15. Good post. I agree with you. Prolog is a great language, but if applied properly.

    By Adelar on Apr 9, 2011

  16. A good little post on the benefits of Prolog. I agree with some of the posts that it is tailor-made for some problems, but that is the same with many things. Horses for courses.

    I recently joined a system-control project which essentially had a core task of programming a 150-page document of rules into an application. That application was in C#. I have been an procedural programmer (C/C++) for years, but in the back of my mind was a niggling voice saying that Prolog was the perfect language for this application.

    I showed a few people how I was able to implement some of the rules in about as many lines as they took to specify in the document, rather than the 100s that it took in C#. They were impressed.

    I am not discounting the application of the mainstream procedural languages, but one of the points of this article is to be aware of the other paradigms, and I for one think that the best programmers know of the other programming paradigms and when it is best to make use of them.

    By AlwaysProgrammedProcedurally on Nov 9, 2011

  17. I used Turbo Prolog to provide small applications for users at Redcliffe City Council, 1990-1994. (Jim in Health, Andrew Osborne in the Library). I also completed work for for Spencer Yule in plumbing – graphs of water data – and Trevor Green in Health – image processing but did not use them. I also wrote a sort for Bill Darch because his MS-DOS sort would not handle the amount of data. In hindsight of course, I should have asked him to buy a new computer. I also wrote code to solve Lewis Carrol’s zebra problem, and also wrote to study the text of an RFT for the accounting system.

    With unlimited storage today for source code, it would make sense to keep all the code we wrote as part of a running system.

    Also wrote word counting application in 1990.

    What I would like to do now is to find gapped n-grams. Perhaps it is possible to find some gapped n-grams which fit together like pieces of a jigsaw puzzle.
    Will use an evaluation copy of SIcstus Prolog for now because I used Sicstus before at Macquarie in 2002.

    I also hope to go over some of the code in one of Covington’s books on NLP.

    By jesus smith on Mar 21, 2012

  18. Whenever I am working on an AI related project in a language other than prolog (and this isnt just me doing this, it tends to be others that have seemingly no prolog influence!) end up following Greenspuns Tenth Point One Rule Of Programming:

    Any sufficiently complicated Haskell, Lisp, Java, C, C++ or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of barely half of the Prolog programming language.

    By Douglas Miles on Jun 26, 2016

2 Trackback(s)

  1. Apr 26, 2011: ??????Prolog????????????? | Article2 Web
  2. Jul 3, 2011: Linkdump #48: Poczytajmy… sobie. « Tomasz Kowalczyk

Post a Comment