Prolog & AI
I am working on using Prolog for artificial intelligence. There’s such a wealth of research from the so-called “AI Winter” that is worth revisiting, because we can mix classical AI and large language models.
In classical AI, we use logic to state a goal, a set of facts about the world, and rules that allow an agent to take steps towards that goal. And we can be very precise about those rules. In short, classical AI does not hallucinate.
There are large classical AI systems, and we call them expert systems. We can query an expert system for financial advice, medical advice and so on.
As an appetizer:
action(book_test_drive(CarId, Date, Time)) :-
customer_budget(Budget),
customer_interested(CarId),
car_price(CarId, Price),
Price <= Budget,
customer_available(Date, Time),
store_open(Date, Time),
tolerance(Tolerance),
\+ booked(CarId, Date, Time - Tolerance, Time + Tolerance).
action(ask_budget) :-
\+ customer_budget(_).
action(ask_interest(CarId)) :-
customer_budget(Budget),
car_price(CarId, Price),
Price <= Budget.Look at the precision of the business rules, and also how cleanly they map to english: book a test drive if the car’s price is within the customer’s budget, if the store is open, the customer is available and the car is also not booked within a tolerance window.
Isn’t that so much reliable than your typical elegantly written prompt?
But feeding facts about the world stumbles upon another problem: interpreting unstructured, noisy user input into the structured facts that feed an expert system, and, more importantly, translating user’s questions to logical statements.
Now: isn’t this what LLMs are really great for?
Indeed, we can let the LLM play the role of natural language understanding, translating user input to logical statements, adding facts to a knowledge base, and defining what actions an agent should take and under what conditions, precisely.
The part for the LLM to play shall be:
1. Convert the user's request into Prolog facts or queries.
2. Use the `assert_fact` tool to add new facts to the knowledge base.
3. Use the `query` tool to call the predicate action/1 to get the list of actions.
4. Look at the actions, and call tools as required.
5. After each tool call, you will receive the result of the call. Add facts as needed.
6. Some actions will prompt you to ask the user for more information. Do so.
7. Do not suggest to the customer actions that are not in the action/1 predicate or engage in conversations outside of the defined actions.Where, naturally, the agent has access to a few tools to talk to a Prolog engine.
For the Prolog engine, we only need first order logic - the one with “exists” and “for all” and predicates - and arithmetic. In technical terms, we need SLD+CLP(Z), which is easy enough to implement following any computer science or artificial intelligence textbook.
Is this a paradox? We use less AI and more AI at the same time. Here, Prolog is doing tool selection, and LLMs have been demoted to middleware. A bit like, well, you really need senior engineers in your team instead of vibe coders.
In the next post, I will present a proof of concept, showing how we can mix Prolog, OpenAI and build a rock-solid car sales agent.
For reference, all technical material pertinent to classical AI in this post can be found in “Artificial Intelligence: A Modern Approach” (https://people.eecs.berkeley.edu/~russell/aima1e.html) from Peter Norvig and Stuart Russell.
This post is AI-free!


