163 points · by triska · 3y ago · 104 comments
https://www.brz.gv.at/en/BRZ-Tech-Blog/Tech-Blog-7-Symbolic-...
In this way, several interesting questions can be answered, such as: Are there grants that have the same prerequisites? How many companies are not eligible for any grant? Are there grants where eligibility also implies other grants? etc.
Prolog is becoming more interesting now that several conforming and free implementations with comprehensive features arise, notably Scryer, Tau and Trealla Prolog as the most recent systems, and interesting bindings for other languages such as ichiban/prolog for Go. On the Datalog side, Google have recently published a Datalog system called Mangle, which supports the Datalog subset of Prolog as a proper subset.
What's wrong with SWI-Prolog?
https://www.complang.tuwien.ac.at/ulrich/iso-prolog/SWI7_and...
In my experience, there are two Prolog worlds, and that has been true right from the beginning of Prolog with Kowalski coming from the world of pure mathematical formalisms, and Colmerauer coming from the world of pragmatic, practical programmers. The power of Prolog (no pun intended :) comes from the tension between these two worlds.
If Kowalski had his way, Prolog would probably be a perfect jewel encased in golden amber, completely unusable but a wonder to behold. If Colmerauer had his way, Prolog would be a quick and dirty programming language fit to replace javascript or C. But they worked together, hand-in-hand, and made something bigger and better and more wonderful than either could have imagined [1].
This duality is also a curse, of course, because its critics can attack it from two sides: it's not pure enough for the purists, not dirty enough for the programmers. Half of its critics complain that it's not 100% declarative, the other half complain that it is not "general purpose" (see elsewhere in this thread). Well, it's not 100% declarative because it's got side-effects, but then what use is a programming language without side-effects? One simply ends up with twisted abominations like monads [2]. And I still don't understand what "general puprose" means, if it doesn't describe Prolog.
I think Jan Wielemaker [maintainer of SWI-Prolog] is firmly in the Colmerauer camp, striving to create a language that everybody can use, not just musty academics in their (our!) ivory towers. I have to say I don't always agree with his choices, but I, personally, am infinitely grateful for his oeuvre and I have often considered how little I could have done, for example in my PhD research, without his work.
_________________
[1] To hear the French say it of course, it was Colmerauer and Philippe Roussel who created Prolog and Kowalski was probably only visiting Marseilles for unrelated reasons. See the Prolog article on French wikipedia: https://fr.wikipedia.org/wiki/Prolog
[2] On the other hand, I think we can both agree that the dynamic database is evil.
:- use_module(library(clpfd)).
Goes at the top of the file (leave off the :- if you're in the repl).The arithmetic built in to prolog is traditionally of the form
A is 1+2,
B is A+3,
... etc.We can also think about other semantic queries such as RDF. Also Freebase offered this kind of queries[1] and now DBPedia.
I recall two key things from his presentation of his final product.
First, he claimed he'd quoted a half the cost/time of a COBOL-based, and a quarter of a C-based, competing tenders -- and that he'd met his deadlines.
Second, the UI was just beautiful - for both aesthetics and performance.
I didn't have much experience in enterprisey software at the time, but what little I had seen was archetypal terminal-style, monochrome, sluggish, far from user-friendly etc. I have some (no doubt unreliable) memories of this thing looking like a 'modern' (say Amiga) GUI app.
Another item in that large category of 'I wish they'd drop these things into the public domain once they retired it from production' as it would probably still make a great demo (though most free Prolog implementations these days wouldn't support (m)any of the pretty Turbo extensions, I'm sure).
They are still around if you want to get in touch.
SICStus Prolog is a workhorse in the transport and logistics industries, running systems that handle a third of all airline tickets, and helping railways to operate their trains better.
“Most people probably have used SICStus Prolog without knowing it,” says Mats Carlsson, its lead developer. “One of our customers runs a flight booking system on SICStus which handles nearly a third of all airline tickets in the world.”
https://web.archive.org/web/20190203010151/https://www.sics....
The new homepage doesn't say anything about flight bookings and I can't find anything about it in the "Customers" page either:
https://sicstus.sics.se/customers.html
I have a sneaking suspicion that might mean the customer replaced Prolog with something else, possibly some kind of machine learning :P
https://www.visual-prolog.com/
(edit: they were also the developers of turbo pascal but rebranded it)
I highly recommend it if you want to get the gist of Prolog and its modern features.
If you want a tour of Prolog, you can watch the video with the same name [1].
Want to explore your code, maybe test out some quick changes? Prolog comes with a nice REPL. Want to modify the knowledgebase? Add some clauses with assertz/1 and dump out the full program code with listing/0; no need to wrangle the text yourself. Want to make a previously static relationship more dynamic? Just throw in a new rule: as far as Prolog is concerned rules and facts are equal citizens. It's like a database that lets you intermix static and dynamic row definitions anywhere you want. Testing it is easy: Prolog will happily give you every possible result for a general query. Want to generate error messages from chunks of code, or run your queries backwards? Just throw in a little metainterpreter[1].
I think Prolog is the perfect companion language to static and difficult-to-extend languages like Go. I think that if we want Prolog to become more popular, we should focus on a great embedding experience for developers. That's my current goal :)
Around the same time, Joe Armstrong of Ericsson was experimenting with Prolog to build a switching system. Erlang came out of that effort, essentially stripping out the backtracking - wish I had thought of that.
Elixir is built on Erlang and is definitely a production language.
You might find both Erlang and Elixir viable.
I think people are far too constrained by adhering to what Prolog is standardized as, making sure there's literally no advancement whatsoever. We just need a more practical formal logic library or type or something for your average language without any stupid string definitions or passing literal prolog in for execution. I mean is that too much to ask?
https://9to5tutorial.com/the-story-of-how-to-make-a-prolog-c...
Embedding prolog (or limited versions, like Datalog, or versions of Prolog or Datalog extended with probabilistic, temporal, or other features), or other logic sublanguages like MiniKanren, in other languages isn’t particularly uncommon.
It was amazing when my colleague and I figured out how to integrate that system with our Oracle database in real-time. Having a prolog engine embedded that way made it incredibly powerful to manage and alert on pretty complex relationships.
I just wrote Prolog material for a book I am writing: Prolog embedded in Python. I also have an embedded MiniZinc example.
I have infrequently used Prolog over a 40 year period. Most interesting work project was rewriting a Common Lisp IR&D planning system in ExperProlog that supported nice graphics. The personal Prolog projects that most influenced me were using the semantic web library for SWI-Prolog in the early days of the semantic web: the best environment for learning about the SW that I found at the time.
I tried to learn it and it really screams “I’m not a general purpose programming language”.
If you take away the use cases more suitable for datalog or MiniZinc the only remaining strong use case I see is recursive parsers.
It’d be good to hear about how others see it.
I'm asking curiously and I guess I have a weird perspective, having used Prolog for a long time, so I would be grateful for an explanation.
(To clarify, most of my career as a programmer I worked mainly with C# and SQL, so I haven't somehow been hiding away in a secret Prolog dimension. I wish :)
Or you can collapse your stack down one technology that integrates it all in such a natural way that you can list recursive parsing and linear constraint optimization in the same breath and still say it's not a general purpose language.
Most programming language are Turing complete and if so they're also general purpose by definition. You can use them to build anything.
That said of course our definition might be different and if you don't have plenty of layers of abstraction that speed your development you won't consider that programming language compared to another.
After all, we are running JavaScript in the servers now and you can find an npm module for nearly everything. Probably someone 20 years ago would've said that "JavaScript is not a general purpose language"
Don't get me wrong, prolog may never be the next JavaScript, but is just fun how people would define something is "general purpose" or not (no hate, just a pure comment seeing things from a different angle)
How long do you think it would take to write a web server in Rule 110?
https://en.m.wikipedia.org/wiki/Rule_110
Turing completeness is an interesting theoretical concept, but it is wholly unsuitable and largely irrelevant when discussing the usefulness of programming languages.
That said, if it's about use cases, Prolog is an excellent language for web development and capable of replacing most of the usual mixed OOP and relational stack. For example, the SWI-Prolog website runs on SWI-Prolog's own http libraries.
Quoting from https://www.swi-prolog.org/dogfood.html:
This page presents a brief history of hosting and developing http://www.swi-prolog.org. The website runs SWI-Prolog and is basically `PlDoc, the SWI-Prolog source documentation system on steroids'. This site replaced a classic Apache server built from static documentation pages, a Twiki-based wiki and Bugzilla. The site was re-implemented as a SWI-Prolog-based HTTP server that extends PlDoc for the following reasons:
1. PlDoc unites the core reference manual with the package documentation and documentation extracted from all loaded sources in a coherent interface. In addition, it provides search, dynamically generated hyperlinks between the various pieces of documentation, and display of the source code. This is far better than the fragmented and not properly interlinked static documentation of the old site.
2. SWI-Prolog comes with extensive libraries for providing web services. Running the website on the system is just the ideal opportunity to test these services and prove to the Prolog community that they are a vital piece of the infrastructure and that we trust them. The server:
welcomes about 4,000 visitors each day, viewing about 30,000 pages (120,000 hits) (2013)
serves about 350 Gb of data per month
serves downloads from static files
serves manual pages from on-the-fly post-processed HTML documents
serves dynamically generated HTML pages from documented source files, coloured source files, etc.
analyses, indexes and presents packs. It also presents the meta-data for pack_install/1
runs a REST API to handle user annotations.
Also see:Can I replace a LAMP stack with SWI-Prolog?
For me, what Prolog does best is get all the ceremony of other programming languages out of my way and let me concentrate on the problem at hand. By "ceremony" I mean special syntactic constructs. For example, you don't even declare variables, as such, in Prolog, there's no assignment expressions, classes and enums and switch statements, there are no for and while loops etc etc. Prolog instead gives you a very small set of abstract building blocks that you can use to write any program. It's like programming with Lego bricks, except you have one kind of brick, a universal Lego brick that you can build anything you want with.
Which is why it's so good at solving hard problems, because once you're used to its abstractions and how to combine them, any problem becomes an exercise in finding the right way to fit the "bricks" together in the right way [1] [2].
That's also why Prolog is hard to learn: imperative languages have ceremonial constructs (if-then-else, while-do, yield, etc) for a reason. The reason being, because that makes it easier to write programs. But not easier to _solve problems_. Not when the problems get hard anyway [3].
So perhaps this is one reason why people say it's not "general purpose"? Because it's hard to learn, so it's hard to use when you're just trying to write, dunno, simple list processing programs, but makes things easier when you're trying to do something inherently difficult, like write a parser? If I think about that, I could see how Prolog may come across as a language only good for certain kinds of problem (which is my attempt to interpret "general purpose", though I'd still be grateful if someone gave me a clear definition that I can grok).
____________
[1] As an example, here's two ways to write the Ackermann–Péter function in Prolog:
https://rosettacode.org/wiki/Ackermann_function#Prolog
One uses the is/2 predicate, which is an instance of what I mean by "ceremony". The second uses Peano notation for natural numbers and is basically a direct translation of the Function's definition on wikipedia (full disclosure: the second version is mine).
[2] That's also the reason why it's infinitely easier to machine-learn Prolog programs, as in Inductive Logic Programming, than to learn, say, Python programs. If you want to loop in Prolog, all you need to do is place a literal in the body of a clause that has the same symbol as the literal at the head of the clause: p:- q, p is recursive. So if you want loops, just move your "bricks" around until they're in the right place, and job done. To machine-learn Python you have to find a way to compose every single special-purpose expression, and all the other kinds of loops, and that gets a lot more complicated, more so as a program grows in size.
It should be noted that you get the same simplicity with languages of the lambda calculus, hence Inductive Functional Programming is also a thing.
[3] All the high-level abstraction also makes it much harder to read. See the Peano notation Ackermann-Péter Function above.
All that abstraction can also make code easier to read by hiding details irrelevant to a function. Not that you can't do similar abstractions in prolog.
When I wrote my bf-in-prolog implementation [1] to play with the language, I found myself writing functions to create a purposed wad of data and then manipulate or extract from it, so the rest of the program could neatly ignore how that wad of data actually worked internally, in much the same way as I would in any language.
>why Prolog is hard to learn
Prolog is a pretty standard functional programming language, just hidden in a backtracking query runner.
All of the introductions and tutorials generally start from the query runner's point of view, of facts and rules and backtracking with examples of `parent(This,That)` and showing it can identify grandparents or cousins or whatever, so it's little wonder that most programmers aren't going to bother sifting through to see it's just a little pattern-matching haskell/c++-template-language kind of thing.
>you don't even declare variables, as such, in Prolog, there's no assignment expressions
I definitely viewed unification as a form of variable assignment ( understanding it would backtrack if unable to assign ). There is a `=` in there to unify a variable with another variable or a given chunk of data. I used it to initialize data structures and to terminate recursive calls by assigning to the 'out' variable. I understand it's "unifying" rather than "assigning", but the effect is the same if you know one of the variables to be unbound. Otherwise it's just an assertion of equality (unifiability?) between the two values.
-----
Disagree. The fundamental difference (to me) is that in a functional language you write functions that have inputs and outputs. You can write code like this in Prolog, but Prolog encourages the idea that there's no such thing as an input or an output; there are only relations. If you write a function such as
c = a + b
in a functional language you cannot just say "here are c and a; tell me what b is." That idea makes no sense in a functional language, but Prolog will simply tell you what b is. You can even say "here is c; give me an assortment of a's and b's that make the relation true" and Prolog will start listing them off (albeit probably not very efficiently, and one does not typically use this capability for simple arithmetic).This is a very different way of thinking about functions because they are inherently "bidirectional" and it's one of the things that makes Prolog feel more like a magical query language than a functional language.
I'm perplexed by your comment. Prolog is not a functional language. It's a logic programming language.
The terminology about "queries" and "facts" and "rules" is extremely unfortunate. I think it's inherited from databases, because of the common implementation trick of storing Prolog programs in a relational database. But explaining Prolog in these terms, as is indeed very common, only begets endless confusion. For instance, there is no "query runner" in Prolog. Because there are no "queries". What people call a Prolog "query" is really a Horn goal proved by SLD-refutation with respect to a set of defintie clauses, i.e. a Prolog program.
The point of all that is automated proofs. It just so happens that proving first-order logic theories and executing programs is the same, hence we can write a logic program as a theory and run a proof to execute it.
>> I definitely viewed unification as a form of variable assignment ( understanding it would backtrack if unable to assign ).
Yes, that's a common mistake to make, and not your fault. These things are never explained in Prolog courses. You can think of unification as a form of equality, as you say, but in truth, unification is a clever optimisation that makes execution efficient. It makes it possible to run an SLD-refutation proof without having to ground the entire program into a set of propositions, first.
But to understand what the hell that means you really need to understand resolution theorem-proving, and a lot of theory about first-order logic, that is simply not taught to programmers. I was very confused about all that myself, right up until the first year of my PhD when I finally had the leisure (and the need) to sit down and really try to get to grips with the material.
But I don't think one needs a PhD to understand all that. What is really needed is better teaching that doesn't muddle the water with useless terminology about "queries" and "facts" and "rules" and so on.
Added to that it has an explicit and srtict type system, and much saner numeric arithmetics (mostly because of the strict types). It also has explicit side-effects declarations, so you know if your code is pure or not.
I have never used on anything non-trivial. So I don't know how it copes on the real world. But the language looks very nice.
You could program in a totally declarative way, with logic formulas, and the system would take care evaluating the formulas! As a person who likes the math/theory side of CS, that sounded absolutely amazing! Except that it wasn't really like that, because IIRC (20 years have passed so there may be some inaccuracies here, but this is the gist of what I remember) logical operators like "and" or "or" weren't even commutative, "a and b" vs "b and a" could be the difference between running normally and an infinite loop, you had to insert "!" (which were totally non-declarative and non-logic) to make things work as you wanted, and at that point you were thinking about what order things would be run in, so why not use an imperative language instead, where at least the order of execution is clear in the first place.
To each their own, though.
For example, inspired by pioneering systems such as XSB Prolog, modern Prolog systems such as Scryer Prolog support alternative execution strategies, internally implemented via delimited continuations as described by Desouter et al., and thus prevent many infinite loops that arise with the default execution strategy:
https://www.ijcai.org/Proceedings/16/Papers/619.pdf
As another example, in 2016, Neumerkel and Kral introduced if_/3 and related predicates, which are now available as library(reif) in Scryer Prolog, and also available for SICStus with advanced compile-time expansions:
https://arxiv.org/abs/1607.01590
As yet further examples, better indexing techniques such as JIT indexing and Scryer Prolog's first instantiated argument indexing help to improve determinism and efficiency automatically, and much improved internal string representations allow efficient parsing of large amounts of data with Prolog's built-in grammar mechanism that is now being drafted for standardisation. Constraints such as dif/2 are now also becoming more widely available after decades of being largely ignored by implementors.
As long as we keep to the pure monotonic core of Prolog, conjunction and disjunction are indeed semantically commutative, and different execution strategies are applicable. Of course, using !/0 and other constructs that rely on specific execution strategies forfeits this advantage.
Try this on https://swish.swi-prolog.org/.
on(a,b). on(b,c). above(X,Y) :- on(X,Y). above(X,Y) :- on(X,Z), above(Z,Y).
Query above(a,c)
Ok, true - it works! no probs.
now try
on(a,b). on(b,c). above(X,Y) :- above(X,Z), on(Z,Y). above(X,Y) :- on(X,Y).
and query... and it explodes.
So - this kind of bug is very hard to find and extremely easy to introduce.
The good news is that answer set programming is not prone to it. So instead of prolog use Clingo !
A key advantage of using a declarative programming language such as Prolog is that we can change the execution strategy. For example, I tried your code in Scryer Prolog and added 2 directives as the first two lines to enable an execution strategy called SLG resolution instead of the default strategy:
:- use_module(library(tabling)).
:- table above/2.
on(a,b). on(b,c). above(X,Y) :- above(X,Z), on(Z,Y). above(X,Y) :- on(X,Y).
Now I get: ?- above(a,c).
true
; ... .
So, with a different search strategy, the Prolog system is also able to derive that above(a,c) holds, like Clingo.Note that answer set programming cannot help in any way when I need a program to perform side effects such as initiating a network connection, hosting a web page, parsing a file etc., whereas Prolog is extremely useful and appropriate for such applications. So, the fact that the default execution strategy of Prolog is not adequate to solve this specific example is not a sensible argument for or against Prolog.
https://link.springer.com/chapter/10.1007/3-540-16492-8_66
But then implementations took a bit to catch up.
>> Prolog is deeply unsuited for work, use Clingo instead.
Well, that doesn't make sense. ASP doesn't even allow lists, let alone side-effects (you know, like writing to a stream), so what "work" is it that it's better suited for, than Prolog?
Also, if I remember correctly, Clingo is not a language but a grounder? It takes a first-order program and grounds the entire Herbrand base into a set of propositions so that a SAT solver can then derive the stable models of the program? Do I have that wrong?
Let me say up front that I've never heard a worse idea in my life, and I've heard a few. First of all, that's why ASP can't use lists, or other functions, because then the Herbrand base goes infinite and good luck grounding it. Second, SAT-solving is NP-complete. As I realised recently (https://news.ycombinator.com/item?id=34072923) ASP is an elegant framework for dealing with uncertainty in a purely symbolic manner, with negation as failure and axiomatic, classical negation. But then, why go and mess this elegance up with a dumb propositionalisation approach that loses all the good stuff about first order logic, and that's terminally inefficient to boot? That, I still don't get.
Eg Authorization is one such use case. Polar is a logic programming language (like Prolog) specifically designed for authorization: https://www.osohq.com/docs/reference/polar-syntax
I hope you are joking.
I was surprised, because Prolog already seemed like one of those exotic things that you learned in school was used in AI research, or had been, but hadn't heard of it being used commercially at the time.
An old dream od marrying NN with rule based systems.