Prolog: An Idea of a Language
Prolog is an old programming language from some AI winters ago—the idea was a combination of proving math theorems and writing “expert systems.” This is done by first creating a whole database of facts which assert some things are true about other things. Then you have “rules” which create new facts as long as some predicates argue about existing facts.
So you have facts like “article(this).” which state this is an article. Then you have another fact such as “author(this, quinn).” which states this article was written by me. We can then create a rule which states someone is a writer if there exists any situation they have become the author of something: “writer(X) :- author(_, X).” This combination of rules and facts comes to form what is called a knowledge base.
Finally we have queries: a query gives some goals which must be met using the facts and rules we have available.
Attempting to prove a goal can be acomplised involves going through all of the rules we have defined and asking them to produce an example of someting matching the goal. This is done “top-down, left-to-right,” in that we pause at each step and attempt to solve each sub-goal. There might be no solutions or multiple solutions–and Prolog doesn’t really elaborate much about that. There is a process called backtracking: walking back an attempted solution and trying the next most reasonable sounding answer.
This seemed like a great idea in the 1970s. Exhaustively “generating and testing” large volumes of outcomes will eventually solve problems. It is, however, incredibly bad at scaling up. Thus you have some copes: constraint programming dialects (like CLP), static analysis dialects (Mercury, Picat), or reducing the expressivity of the language entirely (Datalog.)
In a sense, Prolog is a language which is built around specifying facts and permitted inferences between those facts. Gerrit (Google’s code review platform) once used Prolog as part of a permission system–at least one valid inference had to be “ok” to allow an action. That feature was retired, but Biscuit keeps the idea alive.
Datalog on the other hand removes the ability to generate new proposals. Datalog flips handling to a “bottom-up” motif: you go as far down to base facts as possible first. You don’t reason about “I want a list of writers, thus look up the writers rule, which says to find an author relationship perhaps.” You just look at the facts about articles that exist. Some facts say an author exists. The rule says anything on the right hand side of an authorship fact is a writer. Thus the new fact is inferred and put on the table. This process repeats until no new inferences can result in any new facts–only at that point does Datalog now attempt to answer questions.
In practice Datalog requires a number of extensions to be production usable: extra predicates to do numeric comparisons, possibly aggregations, and extensions to support negations which rule out an otherwise valid inference. These are either used for one-off queries (Cozo) or part of an ongoing “if-then-else” logic system. Complicated roleplaying games, for instance, are riddled with cases of “if the player character has enough mana and also the target does not have immunity to poison unless the target is sleeping.” Business systems run in to these online cases all the time–which become systems like DROOLs.
Logic expressions can make the tabletop example trivial to express: ?- mana(actor) > 25, (sleeping(target); not poison_immune(target)).
Custom predicates bring game state in to the query language.
Inference decouples how poison immunity reaches the workspace (is it a racial? a worn item? a buff?)
And a final added bonus: often predicates can be visualized.
You can show exactly that “this is not a valid target because” and the player sees “Target is immune.” instead of “no.”
Datalog, like Prolog, end up being a neat idea that has to be extended to be capable of real work. Which is where we get the conclusion: Prolog is a mere idea of a language. It’s not really good at doing real work. It’s not specified enough to be compatible with anything. Prolog functions as a conveniently compact way to express some idea. Prolog is the gestalt concept of a logical reasoning engine. It’s not actually a programming language.
You will probably never use a Prolog. You will probably, transitively, use something Prolog was responsible for making. And that is a wild place for someone’s work to land.
Aside: it turns out the ZeroWriter Ink likes to reboot itself when plugged in to a computer. Without saving the document. So that lead to a fun rewrite.