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.
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.
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...
0s(0)s(s(0))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.
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!
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.
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.
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!
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!