◈ Prolog Made Easy

YOUR FIRST PROLOG PROGRAM · INTERACTIVE CHAPTER

Natural Numbers.
Infinite Possibilities!

Two tiny lines of Prolog. An infinity of numbers. And yes — you get to write code today!

No installation. No account. No steering wheel.

◷ 25–35 MINBEGINNER3 HANDS-ON MISSIONS
25–35 MIN · READING + PRACTICE

What if you could describe an infinite collection of numbers with just two lines of code?

Sounds impossible? Fantastic. That means we have something interesting to discover.

Today you will write, run and change your very first Prolog program. Right here, in your browser.

01 / THE IDEA

Meet the most ambitious counting machine ever

Let's start with a question that sounds almost too easy:

What is a natural number?

Zero? Definitely. One? Sure. Two? Yep. How about a billion? Still a natural number!

Can we write a Prolog program that understands all of them? What, every single one? Even though there are infinitely many?

Yep. With just two rules. But first we need a little trick.

02 / THE NOTATION

Build numbers instead of counting them

We'll start at 0 and use s(...) to mean the successor of a number. Think of it as: one more than...

Zero
0
One
s(0)
Two
s(s(0))
Three
s(s(s(0)))

Yes, the notation looks a bit like Russian dolls. That's the point: each new number is built from the previous one.

These are called Peano-style natural numbers. They aren't Prolog's usual integer notation; we're representing naturals as symbolic terms so that the logic becomes visible.

03 / THE PROGRAM

The smallest infinite program

Ready? Here's our complete definition:

natural(0).
natural(s(N)) :- natural(N).

First line: zero is natural. Second line: the successor of N is natural if N is natural.

Look closely. No for. No while. No assignment. Just a fact and a rule describing a relationship.

We haven't written a list of a million numbers. We've described a pattern that can represent them all.

That's the Prolog mindset!

04 / YOUR EXPERIMENT
Let's code · Real Tau Prolog interpreter

Your Prolog playground

Choose a mission, edit the program, ask a question and press Run query. Prolog runs in your browser. No account, no installation.

Edit the clauses. Every clause ends with a period.

Loading the interpreter when you run your first query.

Prolog says...

Your answers will appear here.

Safety note: queries are limited to a small number of computation steps and a short time limit. Try natural(X). and use Next answer to explore the infinite sequence one result at a time.

05 / THE SURPRISE

What just happened?!

Ask natural(s(s(0))). and Prolog can show that two is natural: it peels away one s(...) at a time until it reaches 0.

Now ask natural(X).. Prolog can generate values for X: 0, s(0), s(s(0)), and so on. That doesn't mean it gives you infinitely many answers at once — you'd be here forever! Click Next answer to request another.

The same program can check an answer... or find one.

That's something worth remembering. A relation doesn't have to be a one-way procedure that takes an input and spits out an output.

One little technical secret: Prolog does follow an execution strategy. The order of your rules and goals can affect whether a query terminates. We'll discover why, with experiments, later in the book.
06 / THINK IT THROUGH

A challenge for your brain

Why does natural(s(s(s(0)))). work even though we never explicitly wrote down the number three?

Think about it... then reveal the explanation

Prolog uses the recursive rule to reduce the question to whether s(s(0)) is natural, then s(0), then 0. And natural(0). is a fact! This is the base case that stops the chain.

And here's your next surprise: in Mission 3 you can define even numbers using exactly the same idea. You may not need a loop at all!

07 / THE TAKEAWAY

You've written a real Prolog program!

You've met facts, rules, symbolic terms, variables, recursion, and queries — all in one little experiment.

Not bad for your first day, right?

Remember: don't search for the steering wheel. We're surfing now!

← Back to Think Differently! · All chapters →