◈ Prolog Made Easy

CHAPTER 01 · THE MINDSET SHIFT

Think
Differently!

Why learning Prolog isn't like learning another programming language.

You already know how to drive. Great!
But can you surf?

15 MIN READ

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.

01 / THE STORY

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.

Close-up of a hand on a steering wheel
A familiar world. We already know how this works. Photo: Paul Esch-Laurent / Unsplash

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.

Van on a winding mountain road
New vehicle. Familiar skills. Photo: Daniel J. Schwarz / Unsplash
02 / THE STORY

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.

PYTHON
for n in range(3):
    print(n)
JAVA
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:

Tell the computer how to do something, step by step.

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!

03 / THE STORY

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:

WHERE’S THE STEERING WHEEL?!

There isn’t one.

WHERE’S THE BRAKE?! Nope.

HOW DO I CHANGE GEARS?! My friend, we have a problem.

Surfer riding a breaking ocean wave
New world. New skills. No steering wheel. Photo: Mike Perry / Unsplash

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.

04 / THE STORY

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.

Japanese signs glowing at night in Tokyo
A different language invites a different way of organising ideas. Photo: Ramon Buçard / Unsplash

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.

05 / THE STORY

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.

STOP LOOKING FOR THE STEERING WHEEL!

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.

06 / THE STORY

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.

07 / THE STORY

HOW vs. WHAT

So what is the actual difference?

IMPERATIVE THINKING

HOW do I solve it?

Which instructions? In what order? Which variables change? How many times should I repeat?

LOGIC PROGRAMMING

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.

08 / THE STORY

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:

HEY! THIS IS DIFFERENT!

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:

OH! NOW I GET IT!

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!

Next chapter: Facts & Rules →

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.

Further reading