12 ms·
A gentle introduction to Prolog (2013)
- kensai 10y agoI am always puzzled why Curry which is Haskell + Prolog in one unified packet has never taken off. Combining the advantages of functional and logic programming paradigms seems to have so many interesting applications. Admittedly, Curry is an academic language, not 100% production ready. http://curry-language.org/ http://curry-language.org/
- kuschku 10y agoEven more interesting is that Prof. Dr. Hanus, one of the two original designers of the language, teaches both Haskell and Prolog in his lecture, but not Curry. (And Dr. Huch, another person from Hanus’ team, who held that lecture this year, also didn’t mention Curry in it.)
- kensai 10y agoHe did reply to a mail of mine a couple of months ago though. Curry is still in development.
- kuschku 10y agoIt’s just weird – I’m taking that course right now myself, exam on the 14th – that it was never mentioned.
- kensai 10y agoI really don't know. Can you ask him? This was my mail and his reply: > I am curious, is there a potential date for release of version 1.0.0 > of the Curry programming language in the near future, ie to be > considered production-ready and feature-complete? Although we are working for years on Curry, I am careful to release a 1.x version. In the last years, the design stabilized a lot, in particular with the recent switch to 0.9. However, since Curry intends to be an extension of Haskell, one big thing is missing: type classes. If type classes are added, I think it could be called 1.0. However, for this purpose, we need serious implementations of type classes. We made one approach some time ago but it needs a lot of effort to get it ready with all the tools that exist so far. Now, we are working on another approach to add type classes that might be finished in the next year. Since the addition of type classes breaks (the type signatures of) existing code, it is not a simple extension. Sorry that I can't be more specific. At least, we are working on it, but our capacity is limited :-( Best regards, Michael
- doublec 10y agoMercury is another programming language that combines Prolog plus functional programming that probably doesn't get as much attention as it deserves https://mercurylang.org/ https://mercurylang.org/
- dmpk2k 10y agoWith a build system like that, it gets exactly as much attention as it deserves... D: Do a ./configure --help, and try to figure out what the best "grade"(s) to build is/are. I've been meaning to try Mercury for years, but after this, there's no chance I'm going to try writing anything in it. What other crufty unusable insanity have they built in there?
- doublec 10y agoIt is a bit obscure. I let it build the ones it wants to build by default. This gives me the flexibility of deciding later when building Mercury programs what I want as a backend. This blog post describes some of them: https://adventuresinmercury.blogspot.co.nz/2011/11/making-grade.html https://adventuresinmercury.blogspot.co.nz/2011/11/making-gr...
- collyw 10y agoProlog looks really useful for solving real world problems. Why doesn't it get used more? And can you interface to it using Python or some more mainstream languages?
- ch4s3 10y agoIt makes some trivial things really hard. Fun fact though, Joe Armstrong et al wrote the original version of Erlang in prolog. There are some bindings, ymmv.
- splicer 10y agoFor Python, check out Pyke or pyDatalog.
- pjmlp 10y agoBecause like FP it requires a different way of thinking. It also didn't help that the Japan project to use Prolog a systems language for a so called 5th generation computer didn't work out. https://en.m.wikipedia.org/wiki/Fifth_generation_computer https://en.m.wikipedia.org/wiki/Fifth_generation_computer
- nickpsecurity 10y agoOr the whole AI is about to be everywhere because it's working well on a few problems thing. That led to AI winter with LISP and Prolog taking brunt of it at language level. At least we haven't seen articles like that recently, right? ;)
- adamnemecek 10y agoI feel like people are somewhat aware of prolog but it's cousin datalog that's a db query language is much less widely known. I don't have too much experience with it but it seems like one of the technologies of the past that's superior to current alternatives but for some reason didn't take off.
- timClicks 10y agoThe Datomic database is a great datalog implementation
- dunkelheit 10y agoThe ideas of datalog are making a slow comeback. For example, datalog is a query language for Datomic DB.
- adamnemecek 10y agoI'm aware of Datomic but I feel like it will be a while before it becomes "the standard".
- mossmoth 10y agoDatalog is also quite actively used in ontology based data access where Datalog rules are used as mappings between databases and ontologies (see e.g. [1]). Variants and extensions of Datalog is also research actively in the field of answer set programming[2], with many usable implementations. [1] - http://ontop.inf.unibz.it/ http://ontop.inf.unibz.it/ [2] - https://en.m.wikipedia.org/wiki/Answer_set_programming https://en.m.wikipedia.org/wiki/Answer_set_programming
- edem 10y agoDoes your nick has anything to do with Burzum?
- ComNik 10y agoWe use Datalog every day in a React-Native application, via the datascript db (https://github.com/tonsky/datascript https://github.com/tonsky/datascript). Has been a very positive experience so far. Thinking in "facts" / binary relations is itself a refreshing approach.
- exDM69 10y agoI learned Prolog in some university courses but I'm gutted that I haven't had a single chance to use it in my professional life. I've given some thought to what kind of projects would it be suitable for. The other day I was playing Sid Meier's Civilization and realized that the rule engine for games like that would be a very good fit for a Prolog-like logic programming language (using an embedded interpreter). Prolog would allow adding new rules without touching the old ones, e.g. add a new rule for, say, defensive bonus would be just a single new line in the code base.
- jaggederest 10y agoProlog is very useful for decision and optimization problems. There are a ton of those available in the world. For example a lot of scheduling problems are relatively tractable with logic programming and a total bear in imperative/oo
- mianos 10y agoOn the other hand, I have first had experience of two companies who used C to solve these problems due to performance and maintenance of the logic. One in classroom scheduling, the other in hospital staff scheduling. Both of these are some of the examples most cited as useful. I still have The Art of Promoting on the shelf next to me. Fun but useless.
- agumonkey 10y agoUnification + backtracking is really, really, really really cute. Makes you think very abstractly, so much that it made me feel that haskell was verbose (not trying to be hyperbolic here). A nice book I've read recently is Ivan Bratko's https://ailab.si/ivan/novice.php https://ailab.si/ivan/novice.php Try to grab a 2nd hand one or a library with it on shelves (probably stacking dust). The basics are covered, then graph search, state space, tricks like diff-list (pretty unforeseen use of prolog semantics tbh), a bit of nlp. Very* cool
- catnaroek 10y agoAnd the scary thing is that one can do even better: https://billwadge.wordpress.com/2016/01/18/extensional-higher-order-prolog/ https://billwadge.wordpress.com/2016/01/18/extensional-highe... https://github.com/acharal/hopes https://github.com/acharal/hopes
- agumonkey 10y agoHa nice, I was looking for recent advances in this branch; most of things went the CPL way.
- nickpsecurity 10y agoNeat tools. Have you seen any attempts to model HOL in FOL for verification in FOL? There's a FOL down to assembly verified prover and stack. It's ACL2-like. I also found a paper doing HOL-to-FOL conversion with that tool modeled and proven in HOL. Doing HOL in FOL or HOL-to-FOL converter in FOL seems like a short-cut to verified, proof stack. If the shortcut is doable at all. The HOL-to-FOL gave me some excitement about this. It just wasn't done entirely in FOL itself.
- tomcam 10y agoI understood none of that FOLdeROL
- nickpsecurity 10y ago
- resynthesize 10y agoAt last, I have something to add to a HN thread. I used prolog professionally quite a bit back in the early 2000s. IBM had a product, Tivoli Enterprise Console, that was used to filter, aggregate, and correlate events from their monitoring toolset at the time. All of the rules for event routing and filtering were written in an extended version of prolog. The system was quite powerful at the time, and I found rule writing to be really intuitive once I wrapped my head around the concepts. IBM eventually sunsetted TEC in favor of a similar product they acquired when they bought Netcool, but I'll bet there are still a few organizations out there still using it.
- david-given 10y agoI had a Prolog course at university; it was interesting, but... very weird. (There's a joke that writing a compiler in Prolog is trivially easy; but writing a compiler in Prolog that does anything other than say 'No.' when given an invalid program is infeasibly hard.) I generally found that while getting Prolog to do cool things was fairly straightforward, getting Prolog to do cool things quickly involved knowing precisely where to put cut operators, and that was basically black magic. But it's been a while. BTW, our Prolog lecturer wrote a theorem prover in it. That's pretty straightforward; we all wrote theorem provers in it as an exercise; but his theorem prover had a polished MacOS GUI, and that was also written in Prolog. He was writing low-level MacOS stuff in raw Prolog. To this day I still have no idea how.
- tom_mellior 10y ago> There's a joke that writing a compiler in Prolog is trivially easy; but writing a compiler in Prolog that does anything other than say 'No.' when given an invalid program is infeasibly hard. I realize you said this was a joke, but still... A compiler in Prolog might look like this: compile(Input, Output) :- parse(Input, AST), process(AST, Output). and this does, indeed, just answer "no" or "false" when it fails. But you can improve it by just doing something like this: compile(Input, Output) :- ( parse(Input, AST) % if parsing succeeds... -> process(AST, Output) % ... then process the AST... ; writeln('syntax error') % ... otherwise report an error ). Or you can just use exceptions, which also exist in Prolog. Of course you'd need to collect data (line, column, expected next construct) during parsing in order to make the error message nicer. > I generally found that while getting Prolog to do cool things was fairly straightforward, getting Prolog to do cool things quickly involved knowing precisely where to put cut operators, and that was basically black magic. The cut is indeed complex, and for historical reasons it's taught too early and emphasized too much by almost all Prolog classes and books. For example, most resources would tell you to write the example above as: compile(Input, Output) :- parse(Input, AST), !, % commit to this choice if parsing succeeded process(AST, Output). compile(_Input, _Output) :- % we get here by funky implicit control flow if parsing failed writeln('syntax error'). It's not that hard to understand the cut once you have a good understanding of normal Prolog execution, but yes, that has to come first. And once you understand how it works, you can often find better solutions that do not use the cut. As for writing "low-level" code like GUIs, you just use your Prolog system's foreign function interface....
- moyix 10y agoA little known fact is that Microsoft actually used an embedded Prolog interpreter for network configuration in Windows NT: http://web.archive.org/web/20040603192757/research.microsoft.com/research/dtg/davidhov/pap.htm http://web.archive.org/web/20040603192757/research.microsoft...
- nickpsecurity 10y agoAnd its first version was developed on a Macintosh, too! Thanks for the link. Been some good history lessons on Microsoft recently that keep tripping me out.
- doublec 10y agoA system written using SWI Prolog is apparently used in a large number of NZ stock exchange trades https://dtai.cs.kuleuven.be/CHR/files/Elston_SecuritEase.pdf https://dtai.cs.kuleuven.be/CHR/files/Elston_SecuritEase.pdf
- qwertyuiop924 10y agoThis helped explain a lot about how and why Prolog works. The coolest thing? That's only a tiny percent of the language, but it's already enough to start solving problems. For those of you looking for prolog out in the real world, a surprising number of languages contain embedded interpreters for it. Mostly Lisps. Perhaps most famously, picolisp uses a tiny, lisp-syntax prolog called pilog as a query languages for its built in database. Yes, it has a built in database. Picolisp is weird.
- throwaway7645 10y agoPicolisp is pretty awesomely weird. It has a lot of doc, but I don't find it the easiest to grok. How does the database work? I understand how you can query it with a prolog-esque syntax, but where is all the data stored? Is it just in memory, so this database lives as long as the application is running?
- doublec 10y agoYou can create symbols that persist to an external database file when you call 'commit' or rollback changes with 'rollback'.
- throwaway7645 10y agoKind of like SQLite?
- qohen 10y agoa surprising number of languages contain embedded interpreters for it. Mostly Lisps. Also, Smalltalks (seriously).
- qwertyuiop924 10y agoReally? I can't say I'm entirely shocked, but still...
- jhbadger 10y ago
- acjohnson55 10y agoI'm always happy to see Prolog get some love. Learning it in a university course really expanded my mind, similar to when I first tried FP with ML in another course. I went to a talk about MicroKanran, another logic programming language, which is embeddable in host languages. I think it shows a lot of promise for implementing decision engines. The biggest downside is that it can be tough to reason about the runtime characteristics of nontrivial programs, right about at the point where the paradigm starts to be really useful for modeling complex problems. It takes some experience to know how to write it efficiently.
- gavanwoolery 10y agoPersonally, Prolog was the biggest "woah" moment for me in learning programming languages. If anybody wants a challenging and enlightening programming exercise, I recommend writing your own prolog[esque] interpreter. Surprisingly it still has not been used much in games (well, actually not that surprising given how time consuming and risky it is). I made a stab at [1] but ended up taking more time than I had budget for. One thing I suspect may be using it is the game 1935 [2], although they are vague on the details. [1] https://www.youtube.com/watch?v=2EHKDP2_ky0 https://www.youtube.com/watch?v=2EHKDP2_ky0 [2] https://mollyrocket.com/news_0023.html https://mollyrocket.com/news_0023.html
- orionblastar 10y agoI remember Borland Turbo Prolog. But it was so long ago that I lost the disk it was on.
- RandomOpinion 10y agoIts descendant, "Visual Prolog"[0], appears to have a free edition for personal use[1]. [0] https://en.wikipedia.org/wiki/Visual_Prolog https://en.wikipedia.org/wiki/Visual_Prolog "Visual Prolog, also formerly known as PDC Prolog and Turbo Prolog, is a strongly typed object-oriented extension of Prolog. As Turbo Prolog it was marketed by Borland, but it is now developed and marketed by the Danish firm Prolog Development Center (PDC) that originally developed it. " [1] http://www.visual-prolog.com/vip/download/default.htm http://www.visual-prolog.com/vip/download/default.htm
- feral 10y agoI studied Prolog in college (3rd CS year course, 2003). I concluded it didn't deliver on its promise. The promise was that, as in the article, you 'Say what you want, not how you want it done.' In practice, once you start writing non-trivial programs, you run into situations where they take ages to run, as Prolog does its search in the background. So, you have to start using all these languages features to control and optimize the search, such as: https://en.wikipedia.org/wiki/Cut_(logic_programming) https://en.wikipedia.org/wiki/Cut_(logic_programming) And you end up spending a lot of time reasoning about the how that it said you didn't have to care about. I concluded that, as a result of performance issues, the core Prolog abstraction 'leaks' too frequently for it to be worthwhile. (https://en.wikipedia.org/wiki/Leaky_abstraction https://en.wikipedia.org/wiki/Leaky_abstraction) Its a great promise, if a smarter language or implementation could achieve it, but overall I concluded that if I was going to have to spend all this time reasoning about the how anyway, I might as well just deal with that up-front, rather than after-the-fact. I'd love to hear a contrasting opinion (I got pretty good at it, but only over months, not years, of use). But I suspect this is the reason why Prolog didn't make it. It looks good at the start, but it doesn't deliver.
- lmkg 10y agoIMHO, Prolog is the leading example of a language you learn for the brain-twisting, and not for actual usage. Even moreso than Lisp. Full-blown end-to-end Prolog programs in production are vanishingly rare, and for good reason. But much more useful are the Prolog-inspired chunks of code dealing with a separable part of a larger problem. Logic-based programming is an appropriate tool for some problems, and it can be applied even in the context of procedural (of course, it's easier in languages that support DSLs). As an example, clojure's core.logic seems to be pretty popular and that's basically just a subset of Prolog. Somewhat counter-intuitively, Prolog-inspired DSLs tend to more straightforward because they're missing some of the advanced features that Prolog needs to make it usable. The DSL can rely on the host language to do some of the dirty work for it.
- mondoshawan 10y agoActually, less than you might think. The Android OTA recovery environment is written in prolog, last I remember.
- onetwo12 10y agoProlog is only appropriate for certain kinds of programs, long time ago I programmed in prolog and it was a very difficult task when the problem was not about matching and unification. For me Lisp was much more appropiate and easy to program. Forward chaining and unification is not enough for certain class of algorithms, sometime you need backward chaining, probabilistic inference and many other ideas that are not easily translated in prolog. I remember some discussion about the semantic of clauses. For example misil(ready), is this about the misil is already ready, or that you are going to prepare the misil, etc. I find that concurrency and paralelism in prolog are not easy. In the book "on Lisp" by P. Grahan there is a prolog interpreter in Lisp, this can be interesting for somebody wishing to learn prolog. Edit,Added, erlang was inspired by prolog, so learning prolog will help you learn erlang, but if you are a ruby type elixir is your best option.
- thinkmoore 10y agoHow many prolog programmers does it take to change a lightbulb? No.
- vram22 10y agoHow many Lisp programmers does it take to change a lightbulb? Ans. 1. (1) Ans 2. I'll tell you after I've debugged and can run a macro I'm writing for this. Ans 3. nil
- problems 10y agoA good post but (2013)
- Falkon1313 10y agoI've seen multiple examples of prolog over the years. From looking at them, one gets the idea that you must hardcode all your data into your program and the only UI is a somewhat odd shell, with no way to actually send input in or get output out. It gives the impression of a neat idea that is a cool academic toy but not a practical language for real-world use. I think that it would be helpful if more prolog intros had a simple example that read a CSV file or JSON object or something, processed it using the defined rules, and output the results to text or HTML file. That's a minor thing, but I think that it would give a better impression and get more people interested and trying it. Of course intros to other languages may gloss over I/O as well, but with mainstream imperative languages (that aren't oriented to defining and querying data), it's more well known or assumed that the standard I/O patterns apply.
- white-flame 10y agoProlog is a really good query language, in terms of expressiveness. However, once you start bolting on side effects, like I/O and asserting/retracting facts in the middle of queries, all declarativeness flies out the window and you're left trying to explicitly juggle the implicit branching and control of the program counter to try to keep the system sane, bringing in more and more of the complexities of imperative programming. Optimization also basically requires wresting control of the program counter back from the abstract system into explicit programmer control. It certainly works as part of practical real-world systems, but I wouldn't necessarily want to write a large, behavior-heavy application purely in Prolog. For large in-RAM data, but light on behavior stuff, fine. There are certain data analyses that are much faster to develop in Prolog than imperatively, and going multi-language can save a lot of programmer time compared to writing everything in $TRADITIONAL_LANGUAGE purely out of inertia.
- buzzybee 10y agoI've definitely gotten the sense that Prolog would make a great library or extension to another, more traditionally-paradigmed language. In the same way that LINQ has received a lot of praise as an extension to C#.
- partycoder 10y agoInstead of learning one language every year, learn new programming paradigms. The logic programming paradigm is not implemented by many languages, so, might be interesting to learn.
- pbowyer 10y agoIs Prolog good for assignment problems (assigning guests to rooms or people to beds) when there are soft constraints? I asked twice on StackOverflow [0][1] and had the question declined each time. [0]: http://stackoverflow.com/questions/36148764/weak-rules-in-prolog http://stackoverflow.com/questions/36148764/weak-rules-in-pr... [1]: http://stackoverflow.com/questions/37610341/assigning-people-to-beds-approaches-to-automate http://stackoverflow.com/questions/37610341/assigning-people...
- sesqu 10y agoProlog normally finds a solution, and if you decline it, a new solution, and so on until it can't find any more solutions. If you pay careful attention to your program, you can make it so that it finds ones that satisfy soft constraints first, and then solutions that don't satisfy them. This is what the replies in [0] suggested. Prolog isn't exactly intended for preferring some solutions over others, but it has predictable solution generation that can be used to prescribe priority. That's a lot of mental work, but the code is succinct. If you want to get by without flow diagrams, your best bet is to generate and optimize a cost function, and that can be done in any language.
- e12e 10y agoI was in touch with some researchers that had implemented a shift-scheduling system in Prolog - even got a sample of the code, bit in the end we had a small roster, and managed to agree on a good rotating schedule - so I never got around to really test it... But prior to that my plan was to try and write such a system in Prolog (or some kind of embedded logic, like mini kanten). Note that while Prolog can find all solutions to a combinatorial problem, it can't (obviously) protect you from the complexity of searching all solutions...
- YeGoblynQueenne 10y agoI've been using Prolog since 2008 ish, on and off, with more time spent using it in the last couple of years. I've written my degree and Masters dissertations in Prolog, using different Prologs for each. I code all my personnal stuff in Prolog. If I could condense the experience of those last few years with Prolog in one sentence it would be this one: "Prolog is not your friend". It's not an "easy" language to learn- it's not javascript but with first-order abstractions, it's not Java with a different VM. You need to grok a fair few things about both the theory behind it -in other words, first-order logic- and the implementation decisions taken to make it efficient. For instance- clause selection order or, yep, the cut. You need to understand how this works and, ahem, cut yourself too many times until you get the message. Well, I had to, anyway. There's a bunch of stuff you (or at least, I) never have to think about in day-to-day programming work, such as depth-first search and how it can go infinite. Then you need to get some experience with your chosen Prolog interpreter and learn its idiosyncracies [1]. There are rewards. [plug] My Masters dissertation [2] is a novel algorithm that learns a grammar from unannotated text. Because it produces a grammar in Prolog's Definite Clause Grammars notation, and because Prolog can interpret DCGs as first-order logic theories, these grammars are also logic programs that model the algorithm's input. I process some text, spit out a grammar and then without needing a separate compiler I run this grammar backwards to generate new strings, or the normal way to parse them. [/plug] There's a point in the Prolog learning curve where you risk baldness from pulling out your own hair. Once past that point... it keeps hurting you, but the things you can do with it, it's bit like magic. Or maybe it's Stockholm syndrome :) I've seen many criticisms of Prolog (some that are repeated in this thread). Eight years in I haven't seen one single line of criticism that still makes sense after you've used the language for a few years. So to me the real problem with Prolog is that it hurts you so much that most people give up before they really figure it out. Oh and- should you pick Prolog up never forget this: The dynamic database is evil. [1] http://www.swi-prolog.org/pldoc/man?section=jitindex http://www.swi-prolog.org/pldoc/man?section=jitindex Just a few days ago I was working on something I thought was best tackled using dynamic predicates (written to the database) rather than an in-memory list. My program took hours to process some data, until I noticed I was querying a compound term for its second argument. Just switching arguments brought the processing time down to five minutes on the same data. Did I mention the dynamic db is evil? [2] https://github.com/stassa/THELEMA https://github.com/stassa/THELEMA
- kriro 10y agoI'll add my default recommendation that I provide in every intro to Prolog thread: The best book to learn is "Prolog Programming for Artificial Intelligence". The intro part is great and the AI parts are also excellent. I think "Art of Prolog" and "Craft of Prolog" should be next in that order. I fully support the choice of SWI-Prolog by the author of the post. SWI also has good interfaces to other programming languages, I've only used the Java-bridge though but I think Prolog mostly shines if you mix it with another language. I tihnk a good next step would be researching definite clause grammars and constraint programming as those are the (imo) best use cases for Prolog. If you're mostly interested in constraint solvers I recommend eclipse (not the IDE, very unfortunate naming): http://www.eclipseclp.org/ http://www.eclipseclp.org/ I think Prolog is a great language to train your brain if that makes any sense. It's quite nice to write a Soduku solver and the like in it even though you can get better performance in other languages. Whenever I write Prolog it takes a bit for my brain to readjust which I feel is a pretty good sign as it means you're working in a different paradigm.
- YeGoblynQueenne 10y agoFor those interested in AI I would add "AI algorithms, Data structres, and idioms in Prolog, Lisp and Java": http://wps.aw.com/wps/media/objects/5771/5909832/PDF/Luger_0136070477_1.pdf http://wps.aw.com/wps/media/objects/5771/5909832/PDF/Luger_0... In fact I'd recommend that to anyone, regardless of interest in AI- it's a great programming textbook all around and accessible to anyone who knows at least one of the three languages (coughjavacough).
- sriku 10y agoIf you're interested in logic programming, Mozart/oz offers a better deal than raw prolog for practical problems. The reason is that you can express various search strategies using the concept of "computation spaces" in Mozart.
- tmaly 10y agoProlog was part of a course I took at RIT back in the late 90s. I ended up using it a few times since then in a professional setting to handle filling out extremely complex forms. I even build a module on CPAN to handle creating Prolog facts from Perl data structures.