18 ms·
Ask HN: Why isn't Prolog more popular?
Also, what would it take for Prolog to become a great general-purpose language?
- zmonx 9y agoAs to the first question: There are several reasons for this. One is rather inherent and can be understood by considering the following analogy: Java, C, and many other programming languages are like chess: There are many syntactic rules, and by learning them, you already obtain a rough overview of what you can do in principle. You try out these constructs, and get a sense that you have accomplished something, even if it is rather worthless, and more complex tasks are extremely hard to carry out successfully in these languages. Prolog is more like Go: The syntax is very simple, and there is essentially only a single language element, the logical rule. This means that even if you know, syntactically and semantically, almost everything about the language, you have no idea what to do at first. This can be rather frustrating. From this, beginners easily arrive at the misguided conclusion that the language is useless, or restricted to very specific applications. But it only means they have not grasped its true power and flexibility! Getting to the core of Prolog is hard, and requires systematic guidance. This inherent difficulty is frequently compounded by a rather ineffective and outdated didactic approach which, at its worst, stresses difficult and mostly superseded procedural aspects over more important declarative principles and more modern solutions like constraints. This easily gives the misguided impression that the language is rather imperative and limited in nature, and again causes many students to dismiss it due to their wrong impressions. A third reason is found in the implementational complexity: From a user's perspective, a major attraction of Prolog is its ease of use due to the syntactic simplicity, powerful implicit search mechanism, generality of predicates etc. which are features that are rather specific to logic programming languages. The complexity of all this is shifted to the implementation level: In order to make all this both powerful and efficient, the implementation must do many things for you. This means you need, among other things and in no particular order: an efficient garbage collector, JIT indexing, a fitting virtual machine architecture, a fast implementation of unbounded integers, rational numbers, good exception handling, ISO compliance, many goodies like tabling, an efficient implementation of constraints over integers, Boolean variables, Herbrand terms etc. Most of these topics are even now still subject of active research in the logic programming community, with different advantages and trade-offs. Implementing an efficient Prolog system is a project that easily takes 30 to 40 years. In fact, we are only now getting to the point where systems become sufficiently robust and feature-rich to run complex client/server applications for months and years. In such complexities, you find the answer why Prolog isn't more popular yet. It has simply taken a few decades to implement all this in satisfactory ways, and this work is still ongoing. In my view, Prolog is now becoming interesting. To the second point, Prolog already is a great general-purpose language. You can use it for almost all applications that are currently written in Java and Python, for example. Of course, there are always some features that are worth adding on top or via extensions, and certain tasks would benefit from this. For example, you can add extensions for type checking, and for fast arrays. Various Prolog implementations are already experimenting with such extensions. Many extensions can in fact be implemented via term and goal expansion, a facility that is analogous to macros in Lisp, or via simple reasoning over given programs.
- bordercases 9y agoGood post, in particular splitting out the differences between explicit syntax vs expressive syntax. I myself am quite fond of expressive formal systems, but equivalently useful explicit systems are much easier to pick up even if their ruleset is larger. One has hack-y corner cases but generally keeps you on the rails, the other has the hacks as the rails and expects you to not fuck up where you're going. Or something like that.
- zmonx 9y agoThank you! I found the following chart useful: *) machine and assembly languages: simple syntax, simple semantics *) procedural languages: complex syntax, AST not available, many language constructs *) Smalltalk, LISP, Prolog: AST available, few language constructs Source: http://www.complang.tuwien.ac.at/ulrich/prolog2009.pdf http://www.complang.tuwien.ac.at/ulrich/prolog2009.pdf
- bordercases 9y agoWell now I must definitely use Smalltalk!
- zmonx 9y agoIt's definitely worth a look! Richard O'Keefe, one of the most highly regarded and accomplished Prolog programmers, even wrote his own Smalltalk implementation too: http://www.cs.otago.ac.nz/staffpriv/ok/software.htm http://www.cs.otago.ac.nz/staffpriv/ok/software.htm
- miclill 9y agoThis is opinion but I think the problems you can express well with a language like prolog are quite special. The problems programmers need to solve every day are better expressed by a language that's closer to human languages. How would you write in prolog that you want it to serve a website? Of course this is possible but probably not in an idiomatic way. Furthermore like with functional languages the performance of the program is harder to predict.
- avmich 9y ago> but probably not in an idiomatic way This is the central problem, I think. There are problems which are traditionally solved using different approach, not a Prolog-like, and we're mostly comfortable with this approach unlike with others. A successful general-purpose language has to solve all - or at least all important - problems sufficiently well. In communications to each other we use natural languages, which conveniently allow to omit hard parts if we wish so, so they are rather easily bendable for everything. With precise languages we so far have to either hop paradigms or use clunky detailing. We either need to look to everything through a Prolog (or other language) lens or keep using a variety of tools.
- cronjobber 9y ago> How would you write in prolog that you want it to serve a website? Here's one approach: http://www.swi-prolog.org/pldoc/man?section=pwp http://www.swi-prolog.org/pldoc/man?section=pwp Scroll down for examples. You might disagree, but I think the fit between Prolog semantics and expanding XML templates is surprisingly natural.
- gpcz 9y agoYou can answer this question yourself: simply figure out how you'd implement certain types of programs in Prolog. For example: an operating system, a Unix command-line tool, a video game, etc. You'll quickly shake out deficiencies.
- zmonx 9y agoThese are good questions! As a simple control, I have asked myself these questions with Java in mind, which enjoys notable popularity. Would I write an operating system in Java? No. A Unix command-line too? No. A video game? Probably not. In my view, this casts some doubt on the test's adequacy to answer the initial question. In the concrete case of Java, I think marketing and other influences also played important roles. It could be possible to apply these advantages to Prolog too.
- jorams 9y agoYou are not asking the same questions. The question is how would, not would. While I'll admit writing an operating system in Java is non-trivial (thinking of kernel-level programming here), writing a command-line tool or a video game is pretty simple. You main method receives command line arguments, so that part is covered, and for games you do pretty much what you would do in any other language.
- zmonx 9y agoThank you! Indeed, the question was "how" would I do it, so let us consider it: Operating system in Prolog: Non-trivial, i.e., like in Java. Command-line tool: Pretty simple: My main predicate receives command line arguments. There are countless such examples already, included in the SWI-Prolog distribution (for example). And, as you say, for games I would do pretty much the same as in any language also in Prolog. In my view, this still leaves the same doubt about these questions: Can we distinguish Java from Prolog in any way by answering them?
- tom_mellior 9y agoWhat's wrong with writing Unix command-line tools in Prolog? I'm asking because the "deficiencies" aren't clear to me, although I have written command-line tools in Prolog.
- moomin 9y agoI'd argue it can't be done. It basically has features that are best implemented as a library. In fact, the many kanrens (including Clojure's core.logic) demonstrate the effectiveness of this approach. Meanwhile, stuff like GPU-accelerated ML have supplanted it in the natural language processing arena, and functional programming has become the paradigm of choice for theorem provers like Agda.
- killin_dan 9y agoDo you know very much about GPUs? Would but be viable to implement a kanren on top of cuda or something like that? Wouldn't that lend kanren insane performance gains?
- convolvatron 9y agonot too surprisingly, a lot of logic programs don't parallelize very well at all because of very linear dependencies (control flow). and some do a lot of largely independent but very regular work that would execute quite well on a simd/vector/smt array. people have come up with some tricks to map control flow into simd (like some really cool parser tricks), but i think in general those have regimes where they have sub-serial performance. so maybe? if you had the magic compiler? or you provided some manual annotation support? or a robust ffi? for sql, which has a much more limited footprint, there's been some cool vectorization work.
- tom_mellior 9y ago> It basically has features that are best implemented as a library. In fact, the many kanrens I've never seen one that came close to a real Prolog. Having a library that implements a slow, informally specified tiny subset of Prolog doesn't say anything about the usefulness of real Prolog.
- danharaj 9y agoProlog has a beautiful, powerful idea at its core: Resolution. Datalog shines because it doesn't try to overreach and focuses on making this core nimble and expressive in a domain excellently suited to resolution. Prolog tries to be general purpose and is all the worse for it. Eventually the ideas in Prolog will make their way into a general purpose language where the relationship between the logical components and the algorithmic components of a program is harmonious instead of a constant conflict.
- nickpsecurity 9y agoLook into Mercury language for a take on something that might be more acceptable by mixing logical and functional: https://www.mercurylang.org/about.html https://www.mercurylang.org/about.html
- vram22 9y agoPrince XML, a software tool to generate PDF from HTML and CSS, is written in Mercury. https://en.wikipedia.org/wiki/Prince_(software) https://en.wikipedia.org/wiki/Prince_(software) From above page: >Prince was developed using the Mercury functional logic programming language.
- kensai 9y agoI like Curry better for mixing logical and functional. http://www.curry-language.org http://www.curry-language.org
- athom 9y agoOddly enough, I was just recently trying to play around with the PortableApps package of SWI Prolog, available here: https://portableapps.com/apps/development/swi-prolog_portable https://portableapps.com/apps/development/swi-prolog_portabl... It's about the ONLY PortableApp that offers any kind of program development capability beyond text editing, that I could tell. No compilers, no interpreters outside of this and a couple of SQLite packages. Anyway, I pulled this one down, fired it up, and... no worky. I got a console, theoretically I could execute commands, but try and access the help or docs, and it bails out with an error, telling me xpce can't be loaded, because load_foreign_library/1 is not defined? At least half the menu commands failed with the same error, closing out the app in the process. Basically, the app is impossible to use. So, there's my answer, one that can be applied to many otherwise promising languages. Any system looking to gain traction really needs to go out of its way to Just Work; to make itself readily available, easily installable, immediately functional, and with clear documentation right at hand. You can carry on 'til you're blue in the face about lazy programmers unwilling to learn a simple build-and-install process, but with the ready availability of other environments that generally Just Work, there's really no excuse. At least, that's how I feel about it.
- zmonx 9y agoPlease don't take the following as "lazy programmers unwilling to learn a simple build-and-install process": Please consider filing this as an issue with the SWI-Prolog team, to benefit everyone who runs SWI-Prolog as such an app. Please note that SWI-Prolog is free software and depends on such contributions or at least reports to work reliably on all platforms. Alternatively, there are also several commercial Prolog implementations with professional support to help you in case of difficulties.
- stonemetal 9y agoYou only need a portable app version if it isn't a portable app by default. If I go grab the python zip for my platform it is already a "portable app" what is there for portable apps to do? The only dev environments I can think of that aren't portable by default are not free, so portable apps wouldn't be able to release them anyway.
- 9y ago
- dkersten 9y agoI'm no expert, but I've tinkered with prolog in the past and with Clojure's core.logic more recently and in my opinion it's because the part of my problems that would be a good fit for prolog make up only a small part of the solutions that I write to solve them. For this reason, something like core.logic is much more interesting and useful because it means I can express the part that is well suited to logic/constraint programming in "prolog" and the parts that are not well suited, can be written in another language more suited to those tasks. For example, I can write some code in clojure, that, for example, implements a UI which then calls core.logic to do some processing, which then calls some clojure to pull the logic data from a database. If I wanted to use prolog, I'd have to do something like: (other language -> ffi -> prolog -> ffi -> other language) which is usually too much effort for me to bother.
- mabbo 9y agoMy opinion: Humans are good at thinking sequentially, imperatively. Other language paradigms are harder. That isn't to say that Functional languages (Lisp, Haskell, Scala, etc) aren't as good; frankly, I like them better. There's just a mental gap that has to be crossed and for most developers I've met, that can be challenging. Why do things in a challenging way when I've got Java right here and it works just fine? (straw man, not my own view) Prolog (logic programming) is a bigger gap, imho. It takes more effort for me to really understand Prolog code. Can do some beautiful things with it, but it's easier to have a few good developers be good at it and put their hard work behind a library/API than it is to have every other developer try to get over that gap.
- jdc 9y agoIt seems to me that people whose first language is imperative often find thinking imperatively easier to do.
- mpfundstein 9y agodon't know. My first language was C++. For years actually. Then I learned Clojure, went to University, learned Prolog, Haskell etc.. and essentially turned out to become a FP geek. But it was a difficult road to climb. :-)
- ambrop7 9y agoI don't think there's really a "functional gap". All generally useful programming languages including Prolog are imperative "in the end" no matter what the books say, and anyone good at using them will think about the programs imperatively. The language just has "special features" such as ones that go like "try to match this with that and examine each possibility separately". If you try to write Prolog by "describing" the solution instead of thinking about the steps to achieve it, the code will likely not work at all (infinite recursion) or be very inefficient, in spite of being logically correct. What may look like a "functional gap" may just be the lack of ability for abstract thought, and more correlated with (in)ability to write well structured and generally "nice" code, especially to identify common patterns and create reusable code. Because functional/logical is just an implementation of some abstract pattern that is believed to be useful, and the person may have trouble understanding this pattern just as they would have problems understanding existing abstractions or creating their own abstractions.
- killercup 9y agoI don't know about general-purpose, but I've seen Prolog implementations for specific logic problems. Most recently, chalk [1], a Prolog interpreter designed to be used in the Rust compiler's trait system. So I wouldn't say it's not popular, but the areas it's used in are probably not what most programmers deal with every day. [1]: https://github.com/nikomatsakis/chalk https://github.com/nikomatsakis/chalk
- BjoernKW 9y agoIt's difficult to get started with Prolog beyond toy examples. Prolog was originally designed for creating linguistic models for use in NLP. Even for this original purpose it isn't exactly easy to use. When it comes to mundane tasks such as opening a file and reading its contents as a string or accessing databases, things get even more difficult. Technically, this is all possible with Prolog, too. It's just not exactly fun to do so.
- zmonx 9y agoFirst of all, I fully agree that it is hard to start with Prolog initially. However, let us take a look at these particular examples: As to opening a file and reading its contents as a string: I find it best to use Ulrich Neumerkel's library(pio) to accomplish this task. Importantly, this lets you apply a DCG to a file in a pure way. I start with a DCG that simply describes a list of characters: content([]) --> []. content([C|Cs]) --> [C], content(Cs). I save this in content.pl, just to have a file to try. I can now apply this DCG to the file contents with phrase_from_file/2: ?- phrase_from_file(content(Cs), 'content.pl'). Cs = [c, o, n, t, e, n, t, '(', '['|...] . Thus, I have read the file contents as a list of characters, which I can easily convert to anything I want with other predicates. As to accessing databases: That's quite straight-forward too, in particular if we take into account the following: If you are really using Prolog professionally, then typically Prolog is the database. You simply assert facts, and retrieve them by querying the built-in Prolog database. Personally, I find Prolog queries much more convenient and also more expressive than SQL, and great fun too.
- BjoernKW 9y ago> If you are really using Prolog professionally, then typically Prolog is the database. You simply assert facts, and retrieve them by querying the built-in Prolog database. That was my personal aha moment with Prolog when I realised that Prolog statements are quite similar to SQL queries in that you declaratively define the results you expect instead of the exact directions describing how to arrive at those results. It's a very powerful and elegant concept.
- Arcsech 9y ago
- pedrorijo91 9y agoI would say the problem is pretty similar to why isn't Haskell more popular? both have a different/specific (as in non-mainstream) thinking way, and it is not easy to switch from common programming languages to these. and since it isn't easy, most people don't go deeper on them from a company point of view: if it's hard to find a good Prolog/haskell developer, then they will be more expensive, so they stick with the common Java/C/C#/Python/Ruby/JS stack
- lolc 9y agoI couldn't find much use in Prolog when I tried it. At first it was neat how you could express simple problems, then I learned how one used ! for everything else. It felt like instead of working on a solution, I had to work on what the solution isn't. It's a very specialized system in my view, so there is no hope of it ever becoming general-purpose. But maybe that's because I don't know enough of Prolog.
- zmonx 9y agoHave you been exposed to more recent research on Prolog language constructs? In particular, I recommend the following publication: https://arxiv.org/abs/1607.01590 https://arxiv.org/abs/1607.01590 if_/3 and other declarative predicates like dif/2 are more general alternatives and likely good solutions in the cases you mention. They are still quite recent, at least if we ignore the fact that dif/2 was even available in the very first Prolog system, sometimes called Prolog 0.
- marcosdumay 9y agoFor me, what killed it was the fun of making a simple change to your program and due to a single character mistake insert a bug that turns its run time from O(n) to O(e^e^n). Besides, deep first recursive searches are easy to write and almost never work well in practice, so even the problems that are greatly represented in Prolog either do not get efficient binaries from the existent Prolog compilers or are easy enough to write in another language that little is lost on the transition (often both). That said, I do think search based programming is underrated. There ought to be some representation for theorem resolvers that is good for general purpose programming. It's just that nobody found it yet.
- tom_mellior 9y ago> There ought to be some representation for theorem resolvers that is good for general purpose programming. It's just that nobody found it yet. There are some nice examples of search-based programming in Z3 (using the Python API) in https://yurichev.com/writings/SAT_SMT_draft-EN.pdf https://yurichev.com/writings/SAT_SMT_draft-EN.pdf , if that is along the lines of what you meant.
- marcosdumay 9y agoThat's cool. Although the syntax is very specialized, I never though about approaching general purpose search from a SAT solver.
- danmaz74 9y agoShort answer: because intelligence isn't about solving propositional logic.
- gtycomb 9y agoIt’s about 15 or so years ago I tried Prolog in an application for analyzing features of images. I remember two things from that – 1. Even though concurrency is elegantly described through guards, implementation turned out to to be not that easy, 2. You have to bypass the beauty of pure logical statements in practice (for example see the 'Craft of Prolog' by Richard O'Keefe on optimizing Prolog code). Once we get to this level of writing Prolog code, you find other standard languages and libraries more competitive and practical.
- srpablo 9y agoI have a few ideas, but the main one is that: independent of the language/semantics, languages only really take off because of a "killer app" use case where they are required. Once that's in place, tooling is built to make it much easier to complete projects in. As a few examples: Objective-C was only popular for years because it was required to write iOS apps. Ditto JavaScript being the only way to write for web. Ruby only got popular after Rails. C++ was the blessed way to write for Windows in the 90's. Java had a giant marketing budget, "write once, run everywhere," and was looking to be the best way to write for the web (lol applets). This isn't a perfect explanation, but in many of these cases, it wasn't about eager-vs-lazy, control flow constructs, variations in type systems, or anything related to what the language offered you, it was primarily necessity to be on the platform of your choice. After that, tooling evolved, and they became easier to write major projects in. Why write a web app in Erlang when the JVM has every major templating system, an implementation of CommonMark, several high-performance JSON libraries, model validation, several mature build systems, and thousands of Stack Overflow answers? This makes it hard for languages like Crystal or Nim to take off, but ON TOP OF THAT Prolog is asking its devs to completely change how they approach programming. What would it take to make Prolog take off? A killer app. Which, in the 80's, looked like it was AI :-p
- hdhzy 9y agoI think sometimes you don't even need the "killer app" when the language itself is "pure tooling" and there is a sufficient demand for tooling in a given market, see TypeScript that basically is "JavaScript with tooling".
- protomyth 9y agoPHP's killer app was that it was easy for a System Admin to install on Apache and easy for a programmer to upload something that worked.
- zmonx 9y ago... for a quite generous definition of "worked". In comparison, writing web applications with SWI-Prolog is quite safe also thanks to the powerful quasi-quotation mechanism.
- protomyth 9y agoProlog never had the SQL moment. SQL had IBM, Oracle, and relational databases to back it up and propel it into popularity. Prolog just never had that application. I also believe that the Japanese Fifth Generation Project's failure did a bit to harm the idea of a mainstream Prolog. I was very interested when Borland released Turbo Prolog, but it didn't quite last that long. It also, in its early days, suffered from a bit of Smalltalk vendor syndrome. ..and, sadly, it didn't look like C // strangely Prolog is listed as a spelling error by Firefox...
- randomUser1122 9y agoI tried programming in prolog and read some introductionairy books. However, no book talks about how you would define more complex 'types' objects. How would you model the following domain in prolog: Comment: Attributes: text, points User: Attributes: name, emailAdress(as a struct of first part, domain, top-level-domain) Admin(a special user): additional Attributes: set of rights(can delete, can hide, can modify) Finally, there is an n-1 assocation between Comment and User and I want to make some queries about this domain.
- zmonx 9y agoThere are several ways to do it. One straight-forward way is as follows: We can represent comments by facts like: comment_id_user_text_points(3, 1, 'hello!', []). I do not know what exactly you mean by "points", so I have simply supplied an empty list (fourth argument) in this case. Note that there is an ID for the comment, and an ID for the user that posted the comment. Therefore, we say that this is a relation between users and comments and their text and points, usable in all directions. Each user can likewise be represented as follows, relating a unique ID to a name and e-mail address: user_id_name_email(1, 'randomUser1122', [random,'ycombinator.com']). For admins, I can simply add additional facts, relating user IDs to rights like this: admin_id_rights(1, [delete,hide]). Now, we can simply post queries over these facts, such as: ?- user_id_name_email(ID, _, _), comment_id_user_text_points(_, ID, Text, _). and obtain, on backtracking, all comments for any user. This is all completely analogous to how you would represent such data in any database system. It's more convenient in Prolog though for several reasons.
- randomUser1122 9y agoWow, modelling it like in a relational DB is a great idea! Thanks. Your trick of adding names of the properties like "_id_user_text" is neat. In the past, I tried a similar solution like but I would always forget which property is at which position. How would you make sure that the rights of admins are only delete, hide, and modify (and not e.g. walk, talk, chalk) ? If I use admin_id_rights(Id, Rights):- numeric(ID), sort(Right, SortedRights), subset(SortedRights, [delete, hide, modify]). , then I obviously have added all possible admins. Moreover, how would you then 'create' objects without adding them with asserta. Assume that all the user, admins, comments are written to a text file which should be queried. I can easily read them into some nested compounds with DCGs. However, I would like to create objects(e.g.like Java's new) to check that they adhere to the contraints of my model.
- PeterisP 9y agoFor 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.
- inputcoffee 9y agoI thought the same thing till I tried to program in it and I immediately understood. It is not intuitive, and most programs aren't logical problems in the sense that the prolog can solve. It is highly specialized. It belongs to an era -- and this era isn't "over" -- when the primary manner of solving AI was symbolic.
- nv-vn 9y agoHaving tried Prolog for a problem that it's relatively well-suited for, I found that all the advantages Prolog gives fall away very quickly. There's a lot of finicking around with weird organization of your code just to get things to work, despite being "declarative." For example, trying to rely on libraries to solve parts of the problem for you can be really strange because you can be 99% of the way to solving your problem, but not being able to edit/inline that code will create problems. As an example, trying to use a sorting function on an uninitialized list can't really do anything to help you, but if you inline that same code it can fix it because you're declaring properties of the list in-place.
- YeGoblynQueenne 9y ago>> trying to use a sorting function on an uninitialized list That would give you an error, and rightly so. You can't really expect to sort what's not there. Or am I misunderstanding your comment somehow? I don't quite understand what you mean by "inlining" and how that can help sort a list of no-values?
- nv-vn 9y agoThe way Prolog is "declarative" is that it bridges the gaps between assertions about the properties of a result and actions used to achieve those properties. For example, p(X) :- sorted(X). is saying both (the imperative) "p(X) = sort(X)" and (the assertive) "a sorted X has property p(X)". To solve for P(X), one only needs to find an X such that X is sorted. This makes it difficult to say: "I expect input that, when sorted, has this property" because there is no clear meaning when X is uninitialized. Yet if you expand the meaning you can become "more explicit" about it and convince Prolog that what you're saying is true.
- zmonx 9y agoTo express "I expect input that, when sorted, has this porperty", you have to use slightly more modern language features that are called constraints. For example, let us describe sorted lists of integers: sorted([]). sorted([L|Ls]) :- maplist(#=<(L), Ls), sorted(Ls). Here, I am using the CLP(FD) constraint (#=<)/2 that works correctly in all directions, whether or not its arguments are already instantiated to concrete integers. Such constraints are available in all widely used Prolog systems. You can try the above in GNU Prolog, for example. Now the point: Exactly as you say, the predicate works for concrete lists of integers that are already given: ?- sorted([1,2,3]). true. ?- sorted([2,1,3]). false. And moreover, it also works if the integers are not given, for example: ?- sorted([X,Y,Z]). Z#>=X, Y#>=X, Z#>=Y. We can even ask for example: ?- sorted([X,2,3]). X in inf..2. This means that if this relation holds, than X is at most 2. We can obtain concrete solutions with enumeration predicates. For example: ?- Vs = [X,Y,Z], sorted(Vs), Vs ins 1..3, label(Vs). Vs = [1, 1, 1], X = Y, Y = Z, Z = 1 ; Vs = [1, 1, 2], X = Y, Y = 1, Z = 2 ; Vs = [1, 1, 3], X = Y, Y = 1, Z = 3 ; Vs = [1, 2, 2], X = 1, Y = Z, Z = 2 . And maybe most strikingly, we can also use this in the most general sense, where we ask: Is there any solution whatsoever? The system generates answers in this case: ?- sorted(Ls). Ls = [] ; Ls = [_28] ; Ls = [_170, _176], _176#>=_170 ; Ls = [_1194, _1200, _1206], _1206#>=_1194, _1200#>=_1194, _1206#>=_1200 . Note that all these considerations lead us to the conclusion that "sorted" is a rather bad name for the relation, since it implies that "something has been sorted" and thus encourages a rather imperative view. A better name would be ascending/1, denoting a relation that is true iff its argument is a list of ascending integers, whether or not they are already known.
- FreeFull 9y agoPerhaps what is needed is a Prolog-derivative language which is nicer to use. There is a number of declarative languages available, but my personal favourite would be Picat http://picat-lang.org/ http://picat-lang.org/ A disadvantage of Picat would be though that unlike SWI Prolog, it doesn't have any sort of package management system, and doesn't have a general means of using FFI to call into C libraries.
- zmonx 9y agoPicat is a very interesting development. Note though that it, at least currently, omits some key features of Prolog such as the homoiconic syntax, and definite clause grammars (DCGs).
- gonmf 9y agoAs someone who has quite the extensive experience with Prolog, I can best summarize that there are some types of programming problems at which it excels: deduction, ordering, discrete constraint problems, etc; and at everything else an imperative language is often simpler to use to achieve the same end result.
- carvalho 9y agoProlog is used in IBM Watson, Datalog, and AFAIK in Windows OS. Prolog is not so popular for general purpose computing since: compilers are inconsistent, compatibility problems, difficult debugging, high maintenance costs, few experts, steep learning curves (my professor joked that the more computer science the student is exposed to, the harder is the mental switch to Prolog). Prolog remains great for education on logic, NLP parsers, recursion.
- macintux 9y agoI attended, but can't claim much comprehension of, Chris Martens' talk on logic programming at Strange Loop 2013. If you're interested in the world beyond Prolog, worth a watch. https://www.infoq.com/presentations/linear-logic-programming https://www.infoq.com/presentations/linear-logic-programming
- nmca 9y agoOne of the worst things I found during my prolog courses was that authors seemed to conflate "shorten" and "simplify". We had whole exercises dedicated to taking a readable chunk of code and turning it into some godawful one liner.
- harperlee 9y agoIs there a particular book that covers Japan's 5th generation fiasco, from a technical point of view?
- zmonx 9y agoI once asked a retired Japanese computer scientist why the 5th generation project had failed. He said: "What do you mean 'failed'? All my former colleagues who had worked on this project went on to become full professors!" Also, I once attended a talk by a Japanese researcher who was intimately involved in the project. One significant phenomenon at that time was that commodity hardware was progressing much faster than had been anticipated, in the end eclipsing the specialized designs that were being worked on. There are other reasons too, and here I can only say that Prolog as it is today had very little to do with the outcome. In fact I think this would be a great follow-up question to the present discussion!
- nickpsecurity 9y ago"One significant phenomenon at that time was that commodity hardware was progressing much faster than had been anticipated, in the end eclipsing the specialized designs that were being worked on." This is exactly what happened. A slow language like Prolog on OK hardware that accelerates it can't compete with a fast language like (not-Prolog) on highly-custom, top-of-the-line hardware that accelerates it. It can't in the general case and plenty times not in the special case. Now, that was when Moore's Law was in full swing. There's potential now for that pendulum to swing in reverse for something like this.
- hendzen 9y agoWhen I tried to use SWI prolog for a toy-task, it failed miserably when dealing with facts that contained numeric expressions. I think if a variant of Prolog was paired with an SMT solver (e.g. Z3) it would be much more relevant today. For example, when dealing with bitemporal data (common in finance) you might have a set of facts with two date range attributes. Lets simplify by saying we have a set of facts each having a start date and end date. Here is some non-working Prolog that could work if there was such a capability. entity('TimeWarner'). ticker(entity('TimeWarner'), 'TWC', date(1999-01-01), date(2014-04-31)). ticker(entity('TimeWarner'), 'AOL', date(2014-05-01), date(9999-01-01)). current_at(ticker(entity(_), _, Start, End), T) :- T @> Start, End @> T. -- find current ticker for Time Warner current_at(ticker(entity('TimeWarner'), _, _), date(2017-05-29)) -- SWI prolog can not unify the above clause! (The code above is semi-pseudocode - but I did try and fail to make this work some time ago) Now, it turns out that this sort of exists already, it's called Answer Set programming. There is one implementation out there [0] - but I didn't feel like dredging up an old research project. [0] - http://potassco.sourceforge.net/ http://potassco.sourceforge.net/
- 75dvtwin 9y agothe above seems to have only one temporal dimention, not 2. Why did you think the above is a bi-temporal data?
- hendzen 9y agoI was simplifying the example.
- YeGoblynQueenne 9y agoWould something like this work for you? (Edit: it parse_time/2 is a Swi-Prolog built-in so this won't run in other Prologs. chielk's solution above is best) entity('TimeWarner'). ticker(entity('TimeWarner'), 'TWC', date('1999-01-01'), date('2014-04-31')). ticker(entity('TimeWarner'), 'AOL', date('2014-05-01'), date('9999-01-01')). current_at(Entity,TLC,Date_start,Date_end,date(T)):- entity(Entity) ,ticker(entity(Entity),TLC,date(Date_start),date(Date_end)) ,parse_time(Date_start,DS) ,parse_time(Date_end,DE) ,parse_time(T,Tp) ,Tp > DS ,DE > Tp. ?- current_at('TimeWarner', TLC, DateStart, DateEnd, date('2017-05-29')). TLC = 'AOL', DateStart = '2014-05-01', DateEnd = '9999-01-01'. Basically, you need to treat the date as an atom and it's better to avoid wrapping up everything in compounds like you're doing in ticker(entity('TimeWarner') etc. That just makes it harder to read and write your queries. You can just pass the argument values in the head of the rule and match them with the facts in the database like I do above. If you really require the dates to be in yyyy-mm-dd format, the code above becomes slightly more verbose but it's not the end of the world.
- JabavuAdams 9y agoBecause it's more appropriate as an embedded DSL, or low-level on which to create a DSL, than a general programming language?
- YeGoblynQueenne 9y agoProlog is already a general-purpose language. So is any Turing complete language, by definition. Whether it's a "great" such language or not is kind of a personal tastes thing. As to why it's not more popular, I've thought about this very ofen and I don't have an answer. What I know for sure is it's never going to become more popular until people move on from that silly soundbite about its "general purpose"-ness, which never made any sense to begin with. People program "general purpose" stuff in languages that are much worse for "programming in the large" than Prolog. Most of the big operating systems are written in C, large swathes of game code is in some assembly language or other, about 60% of enterprise code is in Java and most supercomputing code is in FORTRAN fer chrissake. Not to mention, all of the internet is in javascript, a language that was originally meant just for writing small snippets of code to manage buttons and text fields and stuff. You're not going to tell me that javascript is "general purpose"? Prolog is already a general-purpose language. All you need to do is have a look at the library section in the Swi-Prolog documentation. Besides the usual suspects (constraint logic, tabling, lambdas and such and of course parsing all possible text-based formats ever in time dt) we find a bunch of diverse libraries: An http package for all your client/server needs [1] A library for opening web-pages in a browser in a system-agnostic manner [2] A library for command-line parsing [3] An RDF parser and a semantic web library A package manager [5] A random numbers generation library A library for manipulating the Windows registry [7] A library for solving linear programming problems [8] A thread pool management library [9] And a whole lot of support for a bunch of other stuff like corouting, multithreaded applications, a profiler, terminal control, an ODBC interface, an interface to Protocol Buffers, bindings to zlib, GNU readline, and so on and so forth. In what sense is all that not "general purpose"? ______________ [1] http://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/http.html%27) http://www.swi-prolog.org/pldoc/doc_for?object=section(%27pa... [2] http://www.swi-prolog.org/pldoc/man?section=www_browser http://www.swi-prolog.org/pldoc/man?section=www_browser [3] http://www.swi-prolog.org/pldoc/man?section=optparse http://www.swi-prolog.org/pldoc/man?section=optparse [5] http://www.swi-prolog.org/pldoc/man?section=prologpack http://www.swi-prolog.org/pldoc/man?section=prologpack [7] http://www.swi-prolog.org/pldoc/man?section=registry http://www.swi-prolog.org/pldoc/man?section=registry [8] http://www.swi-prolog.org/pldoc/man?section=simplex http://www.swi-prolog.org/pldoc/man?section=simplex [9] http://www.swi-prolog.org/pldoc/man?section=threadpool http://www.swi-prolog.org/pldoc/man?section=threadpool
- deleted 9y ago[deleted]
- garrybelka 9y ago1. 2. There were too many fully incompatible dialects of Prolog. I recall Lisp-like Micro-Prolog. Turbo
- garrybelka 9y ago1. 2. There were too many fully incompatible dialects of Prolog. I recall Lisp-like Micro-Prolog. Turbo
- cocktailpeanuts 9y agoYou would probably realize pretty soon if you try writing anything meaningful with Prolog. In my experience Prolog is conceptually the coolest, but practically the worst when trying to get anything done. Basically, writing a program in Prolog is like solving a puzzle. Nobody wants to solve an additional "puzzle" on top of their already existing problem they set out to solve by programming. (Unless they're doing it for fun)
- scottlocklin 9y agoI've never messed around with it beyond toy problems, but I recall Norvig saying in one of his books that it was trivial to code up NP hard problems in Prolog and other logic constraint languages without realizing that you did so.
- lowbloodsugar 9y agoBecause its only first order so its really tedious. So you can say that if a > b and b >c then a > c, but you can't say if a op b and b op c implies a op c then op is transitive.
- zmonx 9y agoThis is not the case: Prolog has several higher order constructs like the call/N family of predicates, maplist/[3,4] and foldl/N. Your particular example of transitive relations is one of the most elementary examples that are typically solved in basic Prolog courses as exercises, when defining reachability: B is reachable from A if there is an arc between A and B. Transitivity can easily be defined by a rule, as you correctly mention. For instance, let us take your example: fact(a > b). fact(b > c). transitive(A, B) :- fact(F), F =.. [_,A,B]. transitive(A, C) :- fact(F), F =.. [_,A,B], transitive(B, C). Here are all solutions: ?- transitive(X, Y). X = a, Y = b ; X = b, Y = c ; X = a, Y = c ; false. Note that the operator ">" is not mentioned in the definition of transitive/2. Instead, I am using the meta-predicate (=..)/2 to reason about all functors that can arise, making this a quite general definition of transitive relations. We can also reason explicitly about the relation, by making the functor (which may, or may not be defined as an operator) available as a predicate argument: transitive(Op, A, B) :- fact(F), F =.. [Op,A,B]. transitive(Op, A, C) :- fact(F), F =.. [Op,A,B], transitive(Op, B, C). Now we can ask queries like: ?- transitive(Op, X, c). Op = (>), X = b ; Op = (>), X = a ; false. and also in the other direction: ?- transitive(Op, a, Y). Op = (>), Y = b ; Op = (>), Y = c ; false. And we can also ask in the most general way possible, where all arguments are fresh variables.
- gillh 9y agoNetsil's stream-processor is programmed using Datalog, which is a subset of Prolog. Our architecture/use-case: At Netsil, stateful packet processing pipelines are written in declarative rules and materialized tables are backed by SQL compatible embedded in-mem DB. Tuples are executed in parallel and parallelism is controlled by specifying context constraints in rules (e.g. packets within same TCP flow should be processed in order). Further, Datalog workflows are distributable by providing "location specifier" in rules -- i.e. Tuples and attributes serialize to protocol buffers and can be sent/received over ZMQ. Also, the materialized tables in Datalog can be made to sync up with Zookeeper, allowing distributed stream processors to do service discovery and so on. It's a pretty sophisticated runtime/compiler, written primarily in C/C++ for optimal performance. The underlying runtime uses a combination of Intel TBB and Boost ASIO. We are in general big fans of declarative approaches as they have saved us a lot of time, allowing our small team to leapfrog the competition. You can learn more about our architecture here: https://netsil.com/blog/listen-to-your-apis-see-your-apps/ https://netsil.com/blog/listen-to-your-apis-see-your-apps/ Disclaimer: I am co-founder of Netsil (www.netsil.com).
- foolong 9y agoTake a look at ASP (Answer Set Programming) For me it worked a lot better than prolog
- jeffdavis 9y agoYou don't use a language, you use a compiler and a runtime. Maybe prolog has just not found the right context to run in?
- usgroup 9y agoIf you're into Clojure et al or at least datomic you've probably used some subset of prolog (qua datalog). Clojure also brings with it logic and relational programming which is likely the go-to choice of the Clojurist for expressing work-flow or permissions management type problems. Not exactly Prolog but it's the same difference.
- tmaly 9y agoI have used Prolog a few times in a profession setting. When you are using it for the right problem, it is perfect. To use it in a more general purpose sense, I think you need a couple of things. First, you need some really great training and or books showing practical examples as well as how to overcome common issues with performance. Second, you need more people contributing the some of the open source options like SWI Prolog. Third, I think you need more ready to use bindings for the popular languages out there. SWI Prolog provides a C interface, but if you had an interface to say Node, Go, Rust etc that was simple to install with some good examples, you could reach more people.
- mjn 9y agoSome previous blog posts on this topic, from a few years ago: "Who Killed Prolog?" by Maarten van Emden. https://vanemden.wordpress.com/2010/08/21/who-killed-prolog/ https://vanemden.wordpress.com/2010/08/21/who-killed-prolog/ Posits that the huge hype put into it by the Japanese Fifth Generation Computing Project, which it failed to live up to, essentially killed off interest in the language, which it never recovered from. "Why did Prolog lose steam?", a sort-of reply I wrote. http://www.kmjn.org/notes/prolog_lost_steam.html http://www.kmjn.org/notes/prolog_lost_steam.html Posits instead that much of the low-hanging declarative fruit has been picked off by other, more specialized languages, ranging from SQL to production-rule systems to even LINQ, so Prolog no longer is the default go-to declarative programming language.
- dennis714 9y agoIMHO, SAT/SMT solvers does mostly the same, so why would you designate separate PL for this task, when you use these tools or libraries?
- zmonx 9y agoSAT is about a very special case of logic, namely propositional logic, where every variable stands for exactly one of only two possible truth values. Finding satisfying assignments in propositional logic is indeed already a computationally hard task, and it is good that we have efficient SAT solvers for such use cases, allowing us to solve a large variety of combinatorial problems nowadays. However, Prolog lets you tackle not only propositional logic, but tasks that go far beyond this, belonging to a logic called classical first order logic, of which propositional logic is only a subset. In first order logic, we reason about predicates between terms, and this lets you tackle much more complex tasks, far beyond what SAT/SMT solvers can solve for you. In fact, first order logic is so powerful that it lets you describe everything you can in principle perform with a computer. It lets you describe how a compiler works, for example. Or how numerical integration works. And all other computations you have ever seen computers perform. In short, Prolog is a programming language, and can in fact even be used to implement a SAT solver, which is impossible to do with just a SAT solver. Many Prolog implementations even ship with a SAT solver as one of their libraries. There are also even higher-order constructs in Prolog, such as predicates that let you invoke other predicates, making programming in Prolog very expressive and convenient.
- dennis714 9y agoWhat about SMT vs. Prolog, do Prolog have more expressiveness?
- zmonx 9y agoSMT solvers typically only support decidable theories, and since first-order logic is not decidable (only semi-decidable), such solvers are not as expressive as Prolog. In Prolog, the more natural approach that closely corresponds to SMT is simply implementing the theory as a constraint solver. For example, check out CLP(FD) and CLP(Q) for constraint solvers over integers and rational numbers, respectively. They let you formulate statements over these theories, and search for solutions. Note though that solving equations over the second-order theory Z (i.e., integers) is not decidable either (only semi-decidable), and so you may search indefinitely if there is no solution. Importantly, constraints over these theories blend in completely seamlessly into Prolog, since they are simply available as predicates. For example, we can write: ?- A^N + B^N #= C^N, N #> 2, [A,B,C] ins 1..sup. This expresses Fermat's Last Theorem in terms of CLP(FD). A constraint solver with perfect propagation (which, as we know, cannot exist for the integers) would deduce that this conjunction of constraints cannot hold.
- seanwilson 9y agoI've wanted to use Prolog in JavaScript and Java projects before but I could never find robust Prolog implementations for either. I wouldn't want to use Prolog for a GUI or be tied to binary implementations of it but it has its uses.
- domenico_c 9y agoIn my opinion there are mainly three reasons why Prolog is not popular. The reason is that declarative languages in general are not as immediate as imperative languages. Nothing intrinsic about declarative languages themselves, this has to do mostly with the fact that we are taught and we are exposed to imperative languages first and then we hear about other more "exotic" paradigms. Second issue. In a sense, the elegance of the language has been the biggest weakness. The academics loved to play with conceptual matters. A lot of effort went into papers on semantics and mapping different types of reasoning but not much into tools, IDEs, or compilers. The community didn't make enough libraries, built the right abstractions and software engineering practice. Every time you start a project in Prolog you are starting from scratch. Also, the elegance of the language is the reason a lot of people approaching Prolog get quite demanding. "It's logic programming so why do we have to use a cut?". Yet most programmers have no problems with the quirks of C++ or Java. The third one is Prolog hasn't found it's excellence area. C, Go, Scala, Java all have their on strengths and scenarios where they are the best candidates. Prolog would make in theory an excellent candidate for representing complex domains based on rules, covering a module of a larger piece of software. It would be perfect to represent the rules of a board game or the knowledge of a chatbot, but for a number of reason that's no happening. How does the reasoner scale with larger datasets? Will it be hard to manage? Is there an example of something similar being attempted? In my very personal opinion, as a community we should learn from these lessons, take the best bits of Prolog and make something new. At Grakn.ai (https://grakn.ai/ https://grakn.ai/) we are working on a graph database that uses an inference layer that is based on Prolog's resolution, maybe worth having a look. The idea there is that Prolog maybe shouldn't become a great general-purpose language but its bests parts must be used as a base for the next advances in knowledge representation and reasoning.
- zmonx 9y agoGrakn was also recently posted on HN: https://news.ycombinator.com/item?id=14471238 https://news.ycombinator.com/item?id=14471238 In that thread, I show how you can express the sample relation in Prolog. Maybe you can go into more detail, either here or in that thread, on the advantages of Grakn over Prolog for such cases?