Ask HN: is Prolog special or enlightening?

In the book Coders at Work, Joe Armstrong got me curious about Prolog: "Prolog is a beautiful language but not widely used... Prolog is so different to all the other programming languages. It's just this amazing way of thinking."

Another blog post, "Six programming paradigms that will change how you think about coding", mentioned Prolog as an example of the mind-expanding "declarative programming" paradigm, which got me curious enough to start experimenting with it.

As an experienced programmer in C++, Python, SQL and other languages, I found myself thinking, "I could do all this quite easily with a relational database, or with structured data types in a mainstream language." Defining relationships between data (e.g. "likes(fred, apple)."), boolean predicates ("is_fruit_eater(X)."), etc., could be represented easily with boolean fields, boolean functions, and so on. And searching through your data for certain conditions is pretty trivial with a for loop or recursive search, with the added bonus that you know what the hell is going on algorithmically without having to read the Prolog interpreter's source code.

But: I'm a Prolog ignoramus at this point and have only scratched the surface, so I wanted to see if any Prolog veterans on here could shed some light. Is there a moment of enlightenment that comes with a little more persistence and study? Am I missing something? Can it be summed up in mainstream-language terms?

People giving advice online seem to take the attitude of "learn everything you possibly can, it can only help your programming knowledge" but with finite time on earth and a bottomless supply of information out there (look at how deep you can go into the game of chess!) that always strikes me a bit glib and uninsightful. So for someone who's walked down the Prolog road - how was it worth it?

  • Prolog is well worth learning for several reasons. One is practical and easy to explain: Syntactically, Prolog is a very simple language with a powerful implicit search mechanism. As one consequence, it lets you prototype ideas quickly, especially if search is involved in some way, which is almost always the case in practice.

    The second reason is harder to explain. I can only hint at it for now, and you will understand the full extent once you apply Prolog in actual practice: With Prolog, you feel closer to the actual task. First, because you will not manage to even get started unless you manage to express your ideas clearly. Without this, there is nothing "to do" in Prolog. In other languages, it feels that you can always "do" something, such as declaring interfaces, types etc., even if it does not truly help you for the task at hand.

    I have sometimes heard students proclaim: "Prolog is so hard! I could easily solve the task in Java, but I cannot solve it with Prolog." When you ask them to describe how they would solve the task in Java, they are unable to do it.

    In my view, this shows that the tasks we demand from Prolog beginners are typically already inherently much harder than what we demand from beginners in most other languages. There is also a reason for this: These simpler tasks would often be non-issues or at most extremely trivial in Prolog, so we eschew them entirely.

    Second, you are reasoning directly about terms, without any intermediaries such as pointers, references or even arrays. It's just terms, terms all the way down. They come into existence by simply writing them, or by stating the properties that ought to hold, and asking the system to find them. This second property is specific to logic programming and not present in functional languages.

    In a very real sense, this is close to the ideal of programming: We describe what we want, and the machine finds it.

    Prolog has many other advantages too, such as admitting very simple yet powerful meta-interpreters, much shorter ones than are possible for Lisp, for example. The implicit search mechanism and logical variables enable such features which are missing from most other programming languages.

    Finally, constraint logic programming blends in naturally in Prolog. This is a very important paradigm that is heavily used in specialized industries, making excellent Prolog skills also very marketable.

  • Logic programming is very useful and absolutely worth learning, like object-oriented programming and functional programming.

    Unfortunately, the Prolog implementation of logic programming, arguably, not so much.

    An alternative is to find some general purpose language which supports Prolog semantics as a library.

    E.g. one example is miniKanren, which has been implemented in a number of languages, many of them Lisps but not all:

    http://minikanren.org/#implementations

    https://stackoverflow.com/questions/28467011/what-are-the-ma...

    If you're using something like miniKanren, you've got the logic programming, plus the rest of the language in which to do other things in other ways.

    The idea that you specifically have to use Prolog for logic programming is as outdated as the idea that to do OOP, you must use Smalltalk.