Hello World in Prolog
Your first Prolog program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Prolog program and get our first taste of logic programming.
The Code
Create a file named hello.pl:
| |
Understanding the Code
This one-liner packs several Prolog concepts:
:-- This is a directive, telling Prolog to execute what follows immediately when the file loadswrite('Hello, World!')- Outputs the atom (string) to standard outputnl- Outputs a newline (short for “new line”)halt- Terminates the Prolog interpreter,- The comma is Prolog’s conjunction operator (AND), meaning “and then do this”
Why halt?
Without halt, Prolog would enter its interactive REPL after printing. For a standalone script, we want it to exit after completing.
Single Quotes vs Double Quotes
In Prolog, single quotes create atoms (like symbols), while double quotes create lists of character codes. 'Hello, World!' is an atom, while "Hello, World!" is a list of ASCII values. Most modern Prologs can print both, but atoms are the traditional approach.
Running with Docker
The easiest way to run this without installing Prolog locally:
| |
The -q flag suppresses the SWI-Prolog banner for cleaner output.
Running Locally
If you have SWI-Prolog installed:
| |
For GNU Prolog:
| |
Expected Output
Hello, World!
Alternative Approaches
Using format/2
SWI-Prolog provides format/2 for formatted output:
| |
The ~n is a format directive for newline.
Defining a Predicate
A more structured approach:
| |
This defines a predicate hello that prints the message, then calls it.
Interactive Query
If you’re in the Prolog REPL, you can simply query:
| |
The true. at the end indicates the query succeeded.
Using writeln/1
SWI-Prolog provides writeln/1 which combines write and newline:
| |
Key Concepts
- Directives -
:-executes goals when the file loads - Goals - Statements that Prolog tries to prove true
- Conjunction -
,chains goals together (AND) - Atoms - Single-quoted strings like
'Hello, World!' - Built-in Predicates -
write,nl,haltare provided by Prolog
Prolog’s Unique Execution Model
Unlike procedural languages where you write step-by-step instructions, Prolog works differently:
- You define facts (things that are true)
- You define rules (relationships between facts)
- You ask queries and Prolog searches for solutions
Our Hello World is simple, but it’s actually asking Prolog: “Can you prove that writing ‘Hello, World!’, printing a newline, and halting are all true?”
The REPL Experience
Prolog shines in its interactive environment:
$ swipl
Welcome to SWI-Prolog
?- write('Hello, World!'), nl.
Hello, World!
true.
?- X = 'Hello, World!', write(X), nl.
Hello, World!
X = 'Hello, World!'.
?- halt.
The ?- prompt indicates you’re in query mode.
Prolog vs Other Languages
Python:
| |
Java:
| |
Prolog:
| |
Prolog is more concise than Java but the execution model is fundamentally different - we’re giving Prolog goals to prove, not instructions to execute.
SWI-Prolog
Our Docker image uses SWI-Prolog, which is:
- Full-featured - Comprehensive standard library
- Well-documented - Excellent online documentation
- Actively maintained - Regular updates since 1987
- Web-capable - Built-in HTTP server and Pengines
- Cross-platform - Runs on Windows, macOS, Linux
Other popular implementations include GNU Prolog, SICStus Prolog, and the newer Scryer Prolog (written in Rust).
Historical Note
Prolog was created in 1972 by Alain Colmerauer and Philippe Roussel at the University of Aix-Marseille in France. The name comes from “PROgrammation en LOGique” (Programming in Logic). It represents a fundamentally different approach to computing - instead of telling the computer how to solve a problem, you describe what the problem is.
Next Steps
Continue exploring Prolog’s unique features:
- Facts and rules (the knowledge base)
- Queries and unification
- Lists and recursion
- Backtracking and search
- Arithmetic and comparison
- Cut and control flow
Prolog will challenge how you think about programming. Instead of writing algorithms, you’ll describe relationships and let Prolog find solutions. It’s a paradigm shift that will make you a better programmer in any language.
Welcome to the world of logic programming!