Before we write our first line of Prolog, there's something I really need to tell you.
Something that could save you hours of frustration. Perhaps even make you fall in love with logic programming.
Hey! You’ve done this before!
Let me ask you something.
Do you know how to drive a car?
Great! Now imagine that tomorrow someone hands you the keys to a van. Bigger steering wheel. Higher seat. Longer vehicle. A few extra mirrors.
Would you need to learn how to drive all over again?
Of course not! You already know how to accelerate, brake, turn, follow traffic rules and avoid crashing into things. (Hopefully!)
Sure, you need time to adjust. And driving a truck may require a lot more practice and a different licence. But you would not be starting from zero.
Why? Because your brain does something rather wonderful: it reuses what it already knows.
We do this all the time. Instead of starting from scratch, we recognise patterns, transfer skills and make a few adjustments. Learning gets easier when the new thing resembles something familiar.
Python to Java? No big deal!
Now, what does that have to do with programming?
Quite a lot!
Imagine you have been programming in Python for two years. You know variables, conditions, loops and functions. One day your boss says, “Hey! We need Java!”
“Oh no! A completely different language!”
Should you panic? Not really.
Java brings new syntax, static types, curly braces, classes and semicolons. You will have plenty to learn. But much of what you know transfers.
for n in range(3):
print(n)for (int n = 0; n < 3; n++) {
System.out.println(n);
}Different syntax. Familiar idea. Both examples explicitly describe how to repeat an action.
Of course, both languages support more than one programming style. But in their everyday imperative style they share a central idea:
Do this. Then that. Check a condition. Update a variable. Repeat.
From Python to Java is rather like going from a car to a van. Different vehicle; familiar driving skills.
So far, so good!
Now… let’s go surfing!
But wait a minute.
What if, instead of a van, someone hands you a surfboard?
“Come on! Let’s go surfing!”
Surfing?! You have years of driving experience. You know when to brake and how to turn. You are an excellent driver!
Fantastic. Except now you are in the ocean, a wave is coming, and you have one urgent question:
There isn’t one.
WHERE’S THE BRAKE?! Nope.
HOW DO I CHANGE GEARS?! My friend, we have a problem.
And it is not that you are a bad driver. You are trying to surf as if you were driving a car.
You need to learn balance, timing, how to read waves, and how to position your body. Some previous experience might help, but your usual driving techniques will not take you very far.
Here is the lesson: the sooner you stop looking for the steering wheel, the sooner you start learning to surf.
Learning Prolog after Python, Java or C++ can feel much more like learning to surf than learning to drive another vehicle.
Surprised? Good. That is exactly where I wanted us to be.
A new language. A new mental model.
Let’s try another analogy.
Imagine your native language is Spanish and you decide to learn Italian. Buongiorno! Come stai? Hey, that sounds familiar!
There are similarities in vocabulary, grammar and sentence structure. It still takes real work to become fluent, but you have a head start.
Now imagine you want to learn Japanese.
Different writing systems. Different conventions. Different sentence structures. You cannot simply replace Spanish words with Japanese ones and expect everything to work.
You need to understand how the new language works on its own terms. You need to stop translating word by word and start building a new mental model.
And that, my friend, is what we are going to do with Prolog.
The Prolog trap
I have spent 22 years teaching Prolog at university, and I have seen the same thing happen again and again.
A student arrives with plenty of programming experience. Python? Check. Java? Check. Maybe thousands of lines of C++.
They sit down in front of Prolog. “How hard can another programming language be?”
And then… BOOM!
“Where is my for loop?”
“How do I update this variable?”
“Why can’t I just use while?”
“Wait… what do you mean, a variable doesn’t work like that?”
“WHAT IS GOING ON HERE?!”
They start translating their Python algorithms into Prolog, line by line. They try to force Prolog to behave like an imperative language.
And the harder they try, the stranger everything becomes.
That is what I wish every new Prolog student could hear before their very first lesson.
Not because everything you learned before is useless. Far from it! But because some familiar habits may actually get in your way.
You already know how to program. Excellent! Now let’s learn how to think about programming in a different way.
Wait… no loops?!
Here is something that surprises beginners: in the usual declarative style of Prolog, you do not write imperative for, while or do…while statements.
One important way to express repeated structure is recursion.
Ah, yes. Recursion. That word.
The one that makes some students suddenly remember they have somewhere else to be.
Learning recursion can be challenging even for students of conventional languages. A review of computing education research discusses the mental models and misconceptions involved. Read the research ↗
So imagine arriving at Prolog, discovering that your familiar loops are missing, and hearing:
“Don’t worry! Just use recursion!”
JUST USE RECURSION?!
Relax. We are not going to throw you into the deep end. We will learn recursion gradually, through small relationships that actually make sense.
And one subtlety: Prolog does have procedural mechanisms for repetition, but recursion is central to the style of programming we will learn first.
HOW vs. WHAT
So what is the actual difference?
HOW do I solve it?
Which instructions? In what order? Which variables change? How many times should I repeat?
WHAT relationships hold?
Which facts are true? Which rules connect them? What follows from those rules?
Imagine we know a few family relationships:
parent(anna, ben).
parent(ben, clara).
grandparent(X, Z) :-
parent(X, Y),
parent(Y, Z).Now we can ask a question:
?- grandparent(anna, clara).
true.We describe relationships and Prolog searches for answers using its inference and execution mechanisms. No separate grandparent-checking loop required.
That does not make Prolog magic. The order of goals, recursion, termination and efficiency still matter. We will learn those things too—after building the right mental model.
One little promise
Classic Prolog books, including Ivan Bratko’s Prolog Programming for Artificial Intelligence, contain a wealth of valuable material. But I want to make one warning impossible to miss:
Do not approach Prolog as if it were Python with strange punctuation.
Do not translate every imperative instruction into something that looks vaguely equivalent.
Instead, give yourself permission to think differently.
In this course, we will build intuition before piling on details. We will ask questions. Play with tiny examples. Make mistakes. And have those wonderful moments when you suddenly say:
Because Prolog is not just another language to collect. It is an invitation to see computation from another perspective.
So… are you ready?
Put away your car keys. Grab your surfboard.
LET’S LEARN PROLOG!
Photo credits & research
All photographs are real photos published as free-to-use images under the Unsplash License. Free to use does not mean public domain. Image files are loaded from Unsplash; the photo authors retain copyright.
- Paul Esch-Laurent — Close-up of a hand on a steering wheel. Unsplash License.
- Daniel J. Schwarz — Van on a winding mountain road. Unsplash License.
- Mike Perry — Surfer riding a breaking ocean wave. Unsplash License.
- Ramon Buçard — Japanese signs glowing at night in Tokyo. Unsplash License.
Further reading
- McCauley, R., Grissom, S., Fitzgerald, S., & Murphy, L. (2015). Teaching and learning recursive programming: a review of the research literature. Computer Science Education, 25(1), 37–66.
- Scherz, Z., Goldberg, D., & Fund, Z. (1990). Cognitive implications of learning Prolog—mistakes and misconceptions. Journal of Educational Computing Research, 6(1), 89–110.