5 ms·
For me, the big problem with Prolog (and the whole concept of declarative languages) is that the intended benefit requires an unreasonably (or impossibly?) smar
by PeterisP 9y ago
For me, the big problem with Prolog (and the whole concept of declarative languages) is that the intended benefit requires an unreasonably (or impossibly?) smart compiler.
The concept works when I can declare what I want to be done, and the system does it - and when that happens, Prolog is great, the language is great for declaring what I want to be done.
However, often it happens that the system does it in a way that's somehow horribly inefficient and makes it totally unusable. And then I have to redeclare my requirements in a slightly different way to nudge the system into doing it differently - and this is much harder, then I have to worry about much more moving parts than just my code.
Also, the language is not really well suited for that; if I have to specify how exactly in which order the calculations need to be made, then imperative languages are a much better tool. I'm throwing away all the advantages of Prolog if I have to do this all the time - and in practice I do.
Haskell has a bit of similar problems (though generally not with unexpected speed complexity but unexpected memory complexity through laziness and thunks), but Prolog is much worse in that regard.
- zmonx 9y agoI definitely agree that a sufficiently smart compiler and/or fast interpreter is an extremely important asset when working with Prolog, and also that it takes almost superhuman effort to write such compilers. However, at least in my experience, "nudging" the system in the right direction is much easier with Prolog than with imperative languages: With Prolog, all it takes is often simply reordering a few goals, which some systems even perform automatically for you (see YAP, and some parts of SWI-Prolog for examples, in particular the RDF framework). There is also typically much less code to rewrite in the first place. Critically, the ability to reorder your goals depends on your working in the pure subset of the language. If you leave this subset, then also automatic or manual optimizations are orders of magnitude harder to apply. Regarding Haskell and Prolog, my experience also differs significantly: From my experience and observation, the consequences of lazy evaluation are much harder to understand than Prolog's implicit search mechanism and backtracking.
- agentultra 9y ago> For me, the big problem with Prolog (and the whole concept of declarative languages) is that the intended benefit requires an unreasonably (or impossibly?) smart compiler. I'd say... sufficiently smart. We've been writing SQL for decades without such complaints and it works rather well in practice. Sometimes you have to introspect your queries and interact with the underlying interpreter to understand the performance constraints but it is much better than the alternative! Imagine having to write every query as a procedure... what a headache and the duplication! Although learning to write well-performing queries does, I think, take some understanding of the underlying theory driving relational databases and SQL... which can be true for Prolog as well. I'm not really sure why Prolog isn't more popular. Any time I've written declarative DSLs for a solver engine it always feels like I'm re-inventing an under-specified sub-set of Prolog. After all the calculation of computer programs is the syntactic manipulation of predicates, is it not? Maybe if we all started with first-order logic and predicate calculus a language like Prolog would appear to be more practical. I think for a majority of programmers however it can seem a bit alien as we're trained to think in terms of procedures and steps rather than invariants and predicates.
- jackmott 9y ago>We've been writing SQL for decades without such complaints People have to do exactly the "State what I want done in another way to nudge the DB into a performant path" that he was talking about all the time in SQL. But SQL is a specific domain where it is (probably) worth it.
- barrkel 9y agoWe've been writing SQL for decades without such complaints and it works rather well in practice. Speak for yourself. My experience of SQL is that it requires careful tuning whenever the numbers involved grow beyond the trivial. Query plans need inspecting, and often indexes aren't used appropriately. Queries are written and rewritten several different ways to encourage the database to choose one plan or another. If your use of a database is limited to key-value lookups and object graph navigation with an ORM, with <100 values on any given edge, you'll have a fine time, I'm sure. Not everybody uses databases that could trivially be replaced with an object graph. For more complex operations, I'd like to be able to write in the form of a plan with a graph of data flow directly. Scan this table, filter against this index, sort these two data sources and do an merge anti-join, etc. Today, I need to rewrite the query while knowing the planner well enough to foresee how it's going to implement my query, gradually homing in on my target through a series of indirect inspections.
- tom_mellior 9y ago> specify how exactly in which order the calculations need to be made There is a language called Mercury that is an extension of a subset of Prolog, i.e., it takes some useful Prolog stuff away but adds some other useful stuff of its own. One of its additions is a mode system; essentially, a simple way to declare how you intend to use your predicates. You can then write your program once, putting the goals in clauses in any order, and the Mercury compiler will use the mode system to figure out all possible data flows and generate specialized, optimized code for each of them. I like a lot of Mercury's ideas, but as a Prolog fan I find it does throw some things out that I rather like. If you're less attached to Prolog, you might like Mercury more :-)
- thesz 9y agoI boldly compare your hardship to the problems with any sufficiently different CPU architecture. The same argument can be applied to GPGPU, data flow languages, IBM's Cell BE and, god forbid, GreenArray's GA144. Or computation architecture - you already mentioned Haskell and I added data flow computation paradigm to that list. They all about the same from sufficiently distant point of view. Prolog isn't all that worse.
- nickpsecurity 9y agoThose aren't declarative, search-for-answers types of languages in the style Prolog is. It's currently hard for most people to get high-performance code out of a Prolog for average problem in programming. Whereas, once you understand dataflow or SIMD tricks, you can get quite a bit of mileage out of the other stuff you mentioned that runs circles around imperative, sequential code.
- thesz 9y agoOnce you understand Prolog tricks, you can get code that runs circles around imperative code, even SIMD-optimized. And it's currently hard for people to get high-performance SIMD and/or data flow code out of imperative code.
- YeGoblynQueenne 9y ago>> It's currently hard for most people to get high-performance code out of a Prolog for average problem in programming. Hi nick. To my experience, this is not at all the case. Prolog was "slow" in the '70s, when nothing was as fast as C. Nowadays, it's not just the case anymore. Try a modern Prolog compiler like YAC, with tabling and everything. What kind of high-performance code were you trying to write, that didn't go as fast as you like? If it's something interesting I'm all for helping out.
- nickpsecurity 9y agoI mean in the sense of high-performance applications. I'm sure it's fast enough for average. Since other commenter mentioned SIMD, I'm going straight to examples to see what the speed is like. Good test cases would include a key-value store, web server of at least lighttpd complexity, game like Quake (esp interested in fps), top Prolog vs imperative engines for parsing/NLP, MP3 player, and so on. Stuff that taxes even imperative programs for speed. I'd love to see Prolog do those with similar speed (esp no pauses on real-time). Either of you have examples of such things? EDIT: And what's the story on concurrency in terms of safety and scaling?
- YeGoblynQueenne 9y ago>> Also, the language is not really well suited for that; if I have to specify how exactly in which order the calculations need to be made, then imperative languages are a much better tool. I'm throwing away all the advantages of Prolog if I have to do this all the time - and in practice I do. I'm going to kindly ask you to give at least a couple of different examples of this, because it's another of those criticisms of Prolog that have no real basis in actual practice of programming in the language. My source for that is that I've been programming all my side projects and two dissertations in Prolog since 2007 and I've never been in a situation where I had to think long and hard about how to order my predicates. Most of the time the correct ordering is pretty straight-forward. This criticism, about ordering of clauses, has its historical root in the competition between Prolog and PLANNER - because in PLANNER predicate order did not matter, which made the language more "pure" but also less efficient (which in turn is why many fewer people have heard of PLANNER than have heard of Prolog). The real point about it is not that you may have to pull your hair out a couple of times while programming. The point is that in first-order logic, as in maths, predicate order doesn't matter and therefore, in a pure first-order logic language, it shouldn't matter either. The criticism is that Prolog lacks purity, in other words. Which, if I may be so bold, is nonsense on stilts. The real world is not pure and you can't has purely anything languages. People who complain about Prolog's lack of purity are quite prepared to throw away the baby of a very useable, 99% declarative language, allegedly because the bathwater of the missing 1% is really messing their codes up. I'm just not convinced by that to be honest.
- tom_mellior 9y agoBe fair! There are many unfair criticisms directed at Prolog in this thread, but this one does have a basis in fact. You'll admit that there are huge practical differences between ancestor_of(P, P). ancestor_of(A, P) :- ancestor_of(A, Parent), parent_of(Parent, P). and ancestor_of(P, P). ancestor_of(A, P) :- parent_of(Parent, P), ancestor_of(A, Parent). You're right that "in actual practice" of someone who has been programming Prolog for ten years, "the correct ordering is pretty straight-forward" in many cases. But that doesn't mean that the order doesn't matter! It just means that you are experienced enough, and you have no problem with the fact that you have to specify the order.
- dllthomas 9y agoYeah, I'd like to see a system that let me specify the operational semantics, when I want to pin them down (probably because the planner isn't doing a good enough job, but maybe I have unusual operational requirements). And ideally then check what I've specified against my declared intent. That lets me work quickly while my needs aren't great and while "the compiler is sufficiently smart", but when I hit a wall it gives me an obvious path forward that doesn't involve obscuring what I'm trying to do.
- pseud0r 9y agoPerhaps the compiler could be optimized using deep learning. Isn't basically the problem that it has to go through a search space that's too large to find an ideal solution?
- seanwilson 9y ago> However, often it happens that the system does it in a way that's somehow horribly inefficient and makes it totally unusable. And then I have to redeclare my requirements in a slightly different way to nudge the system into doing it differently - and this is much harder, then I have to worry about much more moving parts than just my code. Why would you expect this not to happen for complex programs? Declarative languages are never going to have a compiler so smart you can completely forget about optimising. They let you get certain tasks done easily when performance isn't essential and when optimal performance is essential you need to know what's going on underneath. Prolog execution is essentially navigating a massive search tree. There's always going to be plenty of ways to prune the tree and optimise traversal.