Hacker News new | ask | show | jobs
by arethuza 4774 days ago
I seem to recall that one of the issue with Prolog is that, above a certain level of complexity, you can't really write purely declarative Prolog "progams" - you have to start considering the procedural aspect as well which (in certain cases) can be non-trivial.
3 comments

(See my other comment below too on this topic)

You indeed have to understand how the Prolog engine works in order to structure the declarative statements of your code in a way that will lead to an efficient execution.

A way to see a Prolog programs is as a sequence of facts and statements, and at least one query. The Prolog engine will then search for a solution that fits the given facts and requirements and answer the query. In a way the Prolog engine will search a space of possible answers to the query to find the one(s) that match the given facts and requirements. The key to speed is to structure the facts and statements that the search will fail as early as possible when a wrong path is taken. This amount to pruning the useless parts of the search space as aggressively as possible, so that the Prolog engine does not waste time evaluating options that are doomed in the end.

Before getting this I was often frustrated with apparently nice and correct Prolog programs that took forever and in effect just looked stuck (at some point you just stop waiting and abort the execution). I guess it's a pretty common frustration when beginning in Prolog. But once you get it, it's possible to come up with efficient code. It's still scary to see that some small changes in statements ordering can lead to dramatic difference in runtime. You can have big differences in performance for imperative programming too, but it's rare that it's so bad that a first implementation is completely useless. In Prolog it's quite common. And the way to optimize Prolog performance is very specific, you need to learn to anticipate how the engine walks the search space. I guess it's one of the big roadblock in the practical use of Prolog.

A book -- in fact, the book -- that covers this sort of thing is Richard O'Keefe's The Craft of Prolog, which, like Sterling & Shapiro's The Art of Prolog, is put out by MIT Press.
Mercury[1], a derivative of Prolog does away with these cuts. So I guess that these declarative short-circuits in Prolog are just a fault that arose because some things were not thought through in the creation of Prolog.

[1] http://mercurylang.org/information/doc-release/mercury_trans...