As someone who has developed a somewhat weird obsession with Prolog, I can highly recommend Markus Triska's other articles on Prolog. His article on meta-interpreters [0] was particularly inspiring for me.
What do people use Prolog for in the real world? I learned about it on a university course and it seems so esoteric compared to other things on the course. Like something invented just for computer scientists to enjoy.
"Planning, optimization, diagnostics, and complex configuration" [1]
Prolog also works extremely well as a target language for code generation by LLMs for these domains due to it being "higher up in the food chain" compared to procedural languages so to speak, and because Prolog was originally envisioned for classic NLP and hence has a corpus of one-to-one mappings from natural language to logic (as in the example in [3]). So well in fact that even with last-gen models textual descriptions for suitable problems become the bottleneck and you can in many cases just go straight to Prolog code instead ([2]).
I've just rolled out an internal SWI Prolog app that is similar to one linked to elsewhere in the thread [1]. We have a large Cloud estate with 10s of thousands of resources in it. Detecting unused or misconfigured resources manually isn't practical, and there are significant cost savings to be had by cleaning things up. The Prolog app reads in JSON resource snapshots, creates an in-memory database of facts from it and then applies rules to detect issues. Most of the rules are simple and the ones for detecting unused resources (3 LOC) or resources that reference other non-existent resources (7 LOC) are entirely generic. There's also link metadata that models the possibility of links existing between resource types, even if they aren't always there in practice.
There's TUI that allows querying of issues and the resource hierarchy. Issues can also be output as JSON which is fed into a LLM to produce cleanup actions and management reporting.
The Prolog app is very fast, considering what it's doing, largely because it makes heavy use of tabling so once an issue has been detected it's not recomputed when queries are made.
I have a application that has actions and actions can happen pretty much in any order. By default all scenarios should be an error except for ones that are in the boundary of logical steps, e.g.
https://www.openpolicyagent.org/docs/policy-language
Not quite Prolog as they teach in universities, but its close descendant. Used for policy evaluation, e.g. to validate tree-structured datasets against arbitrary permission rule sets.
A few years ago I wrote a workforce scheduling program designed to be used by non-programmers. I worked in a restricted environment so couldn't install anything. The whole thing ran on SWIPL's web offering.
Users simply had to change the basic "facts" (who was available on what days, how many people were needed), and the program solved for the various constraints and offered solutions.
It was maybe about 300 lines of Prolog, no complex dependencies. It replaced a pile of Python scripts that required a lot of state, didn't really work, and could only run on a few specific computers.
For regular users, it was relatively easy to understand and change the facts. SWIPL for web also offers a nice "notebook" interface that lets you mix data, code, and markdown / output blocks so the documentation was inline.
20+ years ago, it was the backend for the business rules engine that processed various logging and monitoring events. The concept was interesting, the performance was terrible, and businesses mostly didn't want to touch it. After I setup clients with a generic set of rules that worked on Prolog facts, most all of my clients were happy to limit their changes to only those fact files.
I used it working on automated tests for telecoms protocols, the practical problem reduces to solving instances of the Pressburger arithmetic, triple exponential time in general (ouch) but we got some results anyway using heuristics. Prolog's a nice space to do high-level heuristics.
Many years ago Maemo (the mobile OS from Nokia) had the profile manager (day mode/night mode etc) written in Prolog. To me it seems like it's a very appropriate application for it.
The JS package manager Yarn had an experimental feature to define dependency constraints using prolog, it made for very concise way to represent the logic.
It never got released for good though. I actually had need of such feature for a project but I thought that using an exoteric programming language and an experimental feature was a bit much. I ended up setting up those constraints as a CI check hand-made script and the code was surprisingly large (~300 lines), but not that hard to understand.
Prolog is an elegant abstraction. One of the points of abstractions is that they let us concentrate our optimization efforts in one place. Prolog benefits from many decades of research into how to make it work fast. When your problem does require a Prolog-shaped solution the most sensible thing to do is to use a highly optimized Prolog system instead of reinventing a naive algorithm yourself. Your "inelegant" solution will not be faster.
(This is also the problem with "I'll just quickly implement a Prolog-like DSL when I need it". Sometimes not a bad idea, but you have to be realistic. Your "lightweight" Prolog will be worse in every way compared to serious Prolog implementations).
That sounds intuitively right but breaks down when you ask “inefficient at what?”. Are you efficient with CPU cycles or efficient with human working memory?
General programming [0], static analysis [1], RDF triple stores [2], authorization systems [3], incremental computation [4] [5], graph DBs [6]. But it is kind of hard to define Datalog exactly, since it is an entire family of technologies based on logic, each extending a clean mathematical model differently.
Other than databases, program analysis. The polonius borrow checker in rustc uses datalog internally.
But you can use it for lots of things. Whenever I'm frustrated with graph based tools being slow (like build systems), I run the graph through a datalog engine for comparison. It's usually much, much faster.
Datalog is not a subset of Prolog. It looks that way because both are based on Horn clause logic, while Prolog is more expressive.
This loops Prolog, but terminates in Datalog:
p :- p.
p.
?- p.
This is because the underlying mechanism is completely different. Datalog is like SQL with recursion, you start with known facts and repeatedly applies rules to derive all consequences until nothing new appears. In Prolog, you start from the query and works backward through rules until it either finds a proof or fails.
So, Datalog treats Horn clauses as database constraints/inference rules while Prolog treats Horn clauses as a search program. They use the same mathematical substrate, but completely different computational models.
Prolog is for logic what inverse kinematics is for robotics.
Where in imperative programming languages you supply arguments to a function and get a result in Prolog you can give the result of a function call and get all argument sets that lead to that result.
But you are right that in production systems you would seldom see Prolog, for reason that you can easily LLM.
Zenlisp it's similar but almost gives an intro CS course (pre SICP)
for the cheap implementing discrete Math and tons of stuff
in pure Lisp, even rational and complex numbers. The end
chapter it's about logic programmer of course.
I used Prolog twice in production, but none of them were what I'd expect to be the "typical" uses.
1. I used GoLog (a Prolog interpreted in Go) for defining some functional tests in the project where the testing infrastructure was otherwise written in Go.
2. I used Prolog (SWI) to write a parser for Thrift (both the definition and binary format) simply because I needed one, and Prolog was convenient.
What I expect people who use Prolog for the stuff it's really good at is databases that encode some complicated business or legal processes. I.e. databases with many complex constraints that have to all somehow come together to produce a solution set. Prolog would also be a good language to encode / query graph databases. So, whatever you can think of being a good match for a graph database would also work well with Prolog.
There are also (even more niche) Prolog derivatives, eg. Ciao or Mercury, that are... well, decent all-purpose languages. You can just use them in the same context where you'd use Python or Haskell respectively. The implementations are pretty solid in terms of performance and correctness... so, if you like the approach, then why not?
I haven’t used Prolog, but I have a little experience with Erlang and a lot with Elixir. As I understand it, the early versions of Erlang were inspired by Prolog.
For those with familiarity with both Prolog and Erlang, can you comment on the similarities and differences between? Is/was Erlang basically Prolog with OTP bolted on?
Not familiar with Erlang that much, but it's pretty clear Erlang was prototyped on Prolog because of its convenient facilities for DSLs using op/3 to define new tokens for its built-in bottom-up expression parser (using operator precedence parsing) and its Definite Clause Grammar recursive descent parser as trivial specialization of core SLD resolution (Prolog was created for NLP and planning apps in the first place after all).
I guess what may also have contributed is that there are a number of concurrent logics implemented in Prolog for prototyping Erlang's scheduler such as Concurrent Transaction Logic ([1]).
- In Prolog next_light is database, in Erlang it's just a function
Question: What's the light before green?
% Erlang
% Returns actual value
prev_light_search() ->
[State || State <- [green, yellow, red], next_light(State) == green].
% Prolog (?- means that it's in query mode)
?- next_light(Light, green).
So syntax IS similar though the thing is that Prolog is more like binding and quering database and Erlang is executing function.
In a nutshell Erlang is more like: "when I have X, then I can calculate Y" and Prolog like "If I want Y, what's the X".
Erlang has very little in common with Prolog, which is a language in an entirely different paradigm (logic programming).
Early versions of Erlang were implemented in Prolog, which is why Erlang's syntax looks a whole lot like Prolog's, but beyond that they're not very similar.
> ... forbidden knowledge must not be leaked to the public.
Understanding is a personal achievement and has nothing to do with "forbidden knowledge" when the source of said knowledge is both quoted above and freely available.
There's something quite illuminating with this first "horror", where they basically say "it's OK to report wrong answers, because you can check the answers".
I don't think I've ever felt like it's OK for my program to provide a list of answers where some are right and some are wrong, but reading this... and generally believing in P != NP.... maybe that's a decent way of looking at some stuff!
I've actually run into this in the wild, with regards to sales forecasting. A program we were using returned zero if the error bars on a forecast were over 100%. For example, selling somewhere between 1 and 7 units, but averaging 3.
Returning 3 was "wrong", but infinitely more correct than retuning 0.
That sounds like zero was meant to be treated as a special error value? If so then “bad forecast” is more correct than “everything’s fine here, situation normal”, no?
Of course this is far from the best way to do this.
The article server is offline, but I assume they found out that prolog rule evaluation depends on the order the rules are presented in the program.
If so, the language they thought they were using (and that they should actually use) is datalog, not prolog.
Datalog has declarative semantics: All facts that are derivable from the base database and the rules will be derived by the interpreter, and it will not add extra hallucinated facts. If that's not true, it's a bug in the runtime, not in the language.
I get what he's saying but I think it's overstated. I'd categorise his list as "Things to be careful with" not "Coding horrors". For example, "The primary means to make your programs defective in this way is to use predicates like assertz/1 and retract/1" is an unqualified statement that makes it sound like you should never ever use them, and that's not the case. I have a real-life Prolog app that applies rules to facts read from JSON data files. I could do that two ways:
1) Read the JSON with Prolog (there's a library) and assertz() the facts from that, building an immutable database in the first phase before applying the rules in the second phase.
2) Externally transform the JSON into Prolog facts, load that into the app on startup and apply the same rules to it.
I agree that mutating the database in the second phase is probably a bad idea, but that's not the same as saying "assertz() always bad". I'd read his site before it appeared on HN and whilst there a lot of very good stuff on it, some of it reminds me of FP purist edicts - fine if you want to go that way and it's appropriate to your problem, but that isn't always going to be the case. That was the basis of my earlier (downvoted) "Mostly overblown" comment.
!/0 is the cut. It prunes the search space. Useful to say "do not look at the other alternatives since I know they will fail" (when mutually exclusivity is hard) but also necessary to do negation in Prolog (when negated information cannot be easily or efficiently propagated).
is/2 is arithmetic evaluator. It runs only in one direction and it does not solve equations.
#>, #=, etc. are constraints, like (in)equalities over linear arithmetic. When constraints have the form of some known theory (like in SMT solvers), they can be solved (incrementally). That is called "constraint logic programming" (CLP). Modern Prolog systems are indeed CLP systems.
Prolog is older than CLP. CLP is older than SMT. Prolog+CLP systems are turing complete, can be used as programming languages. SMT is powerful but not a programming language.
Can "impure" features be avoided? Not in all cases. Think of them as 'unsafe' in Rust, but less dangerous.
Markus pushes for more purity in Prolog (using CLPFD), but sometimes some impurity (or imperative-like code with side-effects) is the best solution. Sometimes the pure solution is also the better. In other cases, it is not. Better compilers and static analyzers can reduce the friction between these worlds.
Take away: do pure code if you can afford it and it looks like a natural solution to your problem, use impure features later if you really need them.
Radio Yerevan: A listener asks: "Is it true that in Moscow, on Red Square, they are giving away cars?"
Our answer: "Yes, it is true. Except it isn't in Moscow, but in Leningrad. And it isn't on Red Square, but on Palace Square. And they aren't cars, but bicycles. And they aren't giving them away, they are stealing them."
[0] https://www.complang.tuwien.ac.at/ulrich/prolog_misc/acomip....
Prolog also works extremely well as a target language for code generation by LLMs for these domains due to it being "higher up in the food chain" compared to procedural languages so to speak, and because Prolog was originally envisioned for classic NLP and hence has a corpus of one-to-one mappings from natural language to logic (as in the example in [3]). So well in fact that even with last-gen models textual descriptions for suitable problems become the bottleneck and you can in many cases just go straight to Prolog code instead ([2]).
[1]: https://quantumprolog.sgml.net
[2]: https://quantumprolog.sgml.net/llm-demo/part1.html
[3]: https://news.ycombinator.com/item?id=48080201
There's TUI that allows querying of issues and the resource hierarchy. Issues can also be output as JSON which is fed into a LLM to produce cleanup actions and management reporting.
The Prolog app is very fast, considering what it's doing, largely because it makes heavy use of tabling so once an issue has been detected it's not recomputed when queries are made.
[1] https://web.archive.org/web/20190525163234/https://dev.to/da...
I have a application that has actions and actions can happen pretty much in any order. By default all scenarios should be an error except for ones that are in the boundary of logical steps, e.g.
Of course you can immediately see problem with this scope - what happens when non-logged-in user tries to upload or user logouts after upload etc.So I have ~50 actions, ~30 constraints which generate >200 scenarios which then are transformed into test suite.
Yet in short: Prolog is useful everywhere it's simple to express a rule but not that easy to implement it.
Users simply had to change the basic "facts" (who was available on what days, how many people were needed), and the program solved for the various constraints and offered solutions.
It was maybe about 300 lines of Prolog, no complex dependencies. It replaced a pile of Python scripts that required a lot of state, didn't really work, and could only run on a few specific computers.
For regular users, it was relatively easy to understand and change the facts. SWIPL for web also offers a nice "notebook" interface that lets you mix data, code, and markdown / output blocks so the documentation was inline.
https://v3.yarnpkg.com/features/constraints
It never got released for good though. I actually had need of such feature for a project but I thought that using an exoteric programming language and an experimental feature was a bit much. I ended up setting up those constraints as a CI check hand-made script and the code was surprisingly large (~300 lines), but not that hard to understand.
(This is also the problem with "I'll just quickly implement a Prolog-like DSL when I need it". Sometimes not a bad idea, but you have to be realistic. Your "lightweight" Prolog will be worse in every way compared to serious Prolog implementations).
[0] https://github.com/flix/flix
[1] https://github.com/rust-lang/polonius
[2] RDFox
[3] https://github.com/eclipse-biscuit/biscuit
[4] https://github.com/vmware-archive/differential-datalog [5] https://github.com/brurucy/pydbsp
[6] https://github.com/cozodb/cozo
But you can use it for lots of things. Whenever I'm frustrated with graph based tools being slow (like build systems), I run the graph through a datalog engine for comparison. It's usually much, much faster.
This loops Prolog, but terminates in Datalog:
p :- p.
p.
?- p.
This is because the underlying mechanism is completely different. Datalog is like SQL with recursion, you start with known facts and repeatedly applies rules to derive all consequences until nothing new appears. In Prolog, you start from the query and works backward through rules until it either finds a proof or fails.
So, Datalog treats Horn clauses as database constraints/inference rules while Prolog treats Horn clauses as a search program. They use the same mathematical substrate, but completely different computational models.
Where in imperative programming languages you supply arguments to a function and get a result in Prolog you can give the result of a function call and get all argument sets that lead to that result.
But you are right that in production systems you would seldom see Prolog, for reason that you can easily LLM.
Here[0] is an example of using Ruby and Prolog to solve a real-world AWS management problem.
0 - https://web.archive.org/web/20190525163234/https://dev.to/da...
https://t3x.org/lisp64k/prolog.html
The linked code runs in this: Unix, DOS and CP/M
https://t3x.org/klisp/index.html
It will compile for CP/M and DOS (Turbo C), Unix, Windows and whatnot.
64k Lisp:
https://t3x.org/lisp64k/index.html
Zenlisp it's similar but almost gives an intro CS course (pre SICP) for the cheap implementing discrete Math and tons of stuff in pure Lisp, even rational and complex numbers. The end chapter it's about logic programmer of course.
https://t3x.org/zsp/index.html
1: https://ds26gte.github.io/schelog/index.html
1. I used GoLog (a Prolog interpreted in Go) for defining some functional tests in the project where the testing infrastructure was otherwise written in Go.
2. I used Prolog (SWI) to write a parser for Thrift (both the definition and binary format) simply because I needed one, and Prolog was convenient.
What I expect people who use Prolog for the stuff it's really good at is databases that encode some complicated business or legal processes. I.e. databases with many complex constraints that have to all somehow come together to produce a solution set. Prolog would also be a good language to encode / query graph databases. So, whatever you can think of being a good match for a graph database would also work well with Prolog.
There are also (even more niche) Prolog derivatives, eg. Ciao or Mercury, that are... well, decent all-purpose languages. You can just use them in the same context where you'd use Python or Haskell respectively. The implementations are pretty solid in terms of performance and correctness... so, if you like the approach, then why not?
[1] https://github.com/terminusdb/terminusdb
For those with familiarity with both Prolog and Erlang, can you comment on the similarities and differences between? Is/was Erlang basically Prolog with OTP bolted on?
I guess what may also have contributed is that there are a number of concurrent logics implemented in Prolog for prototyping Erlang's scheduler such as Concurrent Transaction Logic ([1]).
[1]: https://www.cs.toronto.edu/~bonner/ctr/Home.html
- `;` in Erlang indicates multi clause function
- In Prolog next_light is database, in Erlang it's just a function
Question: What's the light before green?
So syntax IS similar though the thing is that Prolog is more like binding and quering database and Erlang is executing function.In a nutshell Erlang is more like: "when I have X, then I can calculate Y" and Prolog like "If I want Y, what's the X".
Early versions of Erlang were implemented in Prolog, which is why Erlang's syntax looks a whole lot like Prolog's, but beyond that they're not very similar.
https://grack.com/writing/school/enel553/report/prolog.html
Understanding is a personal achievement and has nothing to do with "forbidden knowledge" when the source of said knowledge is both quoted above and freely available.
I don't think I've ever felt like it's OK for my program to provide a list of answers where some are right and some are wrong, but reading this... and generally believing in P != NP.... maybe that's a decent way of looking at some stuff!
Returning 3 was "wrong", but infinitely more correct than retuning 0.
Of course this is far from the best way to do this.
https://www.youtube.com/watch?v=jYoY1cwAd90
If so, the language they thought they were using (and that they should actually use) is datalog, not prolog.
Datalog has declarative semantics: All facts that are derivable from the base database and the rules will be derived by the interpreter, and it will not add extra hallucinated facts. If that's not true, it's a bug in the runtime, not in the language.
So it's not that they "discovered" anything about Prolog; they already knew the language inside out.
This article explains how to appropriately use Prolog declaratively and with full generality.
1) Read the JSON with Prolog (there's a library) and assertz() the facts from that, building an immutable database in the first phase before applying the rules in the second phase.
2) Externally transform the JSON into Prolog facts, load that into the app on startup and apply the same rules to it.
I agree that mutating the database in the second phase is probably a bad idea, but that's not the same as saying "assertz() always bad". I'd read his site before it appeared on HN and whilst there a lot of very good stuff on it, some of it reminds me of FP purist edicts - fine if you want to go that way and it's appropriate to your problem, but that isn't always going to be the case. That was the basis of my earlier (downvoted) "Mostly overblown" comment.
But nice to see Prolog mentioned at all on HN :-)
The version without ! looks identical to the version with ! except only the ! is removed - is this a joke?
is/2 is arithmetic evaluator. It runs only in one direction and it does not solve equations.
#>, #=, etc. are constraints, like (in)equalities over linear arithmetic. When constraints have the form of some known theory (like in SMT solvers), they can be solved (incrementally). That is called "constraint logic programming" (CLP). Modern Prolog systems are indeed CLP systems.
Prolog is older than CLP. CLP is older than SMT. Prolog+CLP systems are turing complete, can be used as programming languages. SMT is powerful but not a programming language.
Can "impure" features be avoided? Not in all cases. Think of them as 'unsafe' in Rust, but less dangerous.
Markus pushes for more purity in Prolog (using CLPFD), but sometimes some impurity (or imperative-like code with side-effects) is the best solution. Sometimes the pure solution is also the better. In other cases, it is not. Better compilers and static analyzers can reduce the friction between these worlds.
Take away: do pure code if you can afford it and it looks like a natural solution to your problem, use impure features later if you really need them.
https://www.swi-prolog.org/pldoc/man?section=clpfd-integer-a...
https://www.swi-prolog.org/pldoc/doc_for?object=!/0
Radio Yerevan: A listener asks: "Is it true that in Moscow, on Red Square, they are giving away cars?"
Our answer: "Yes, it is true. Except it isn't in Moscow, but in Leningrad. And it isn't on Red Square, but on Palace Square. And they aren't cars, but bicycles. And they aren't giving them away, they are stealing them."