Beginner

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:

1
:- write('Hello, World!'), nl, halt.

Understanding the Code

This one-liner packs several Prolog concepts:

  • :- - This is a directive, telling Prolog to execute what follows immediately when the file loads
  • write('Hello, World!') - Outputs the atom (string) to standard output
  • nl - 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:

1
2
3
4
5
# Pull the SWI-Prolog Docker image
docker pull swipl:stable

# Run the program
docker run --rm -v $(pwd):/app -w /app swipl:stable swipl -q hello.pl

The -q flag suppresses the SWI-Prolog banner for cleaner output.

Running Locally

If you have SWI-Prolog installed:

1
2
3
4
5
6
# Run the script
swipl -q hello.pl

# Or interactively load it
swipl
?- [hello].

For GNU Prolog:

1
gprolog --consult-file hello.pl

Expected Output

Hello, World!

Alternative Approaches

Using format/2

SWI-Prolog provides format/2 for formatted output:

1
:- format('Hello, World!~n'), halt.

The ~n is a format directive for newline.

Defining a Predicate

A more structured approach:

1
2
3
hello :- write('Hello, World!'), nl.

:- hello, halt.

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:

1
2
3
?- write('Hello, World!'), nl.
Hello, World!
true.

The true. at the end indicates the query succeeded.

Using writeln/1

SWI-Prolog provides writeln/1 which combines write and newline:

1
:- writeln('Hello, World!'), halt.

Key Concepts

  1. Directives - :- executes goals when the file loads
  2. Goals - Statements that Prolog tries to prove true
  3. Conjunction - , chains goals together (AND)
  4. Atoms - Single-quoted strings like 'Hello, World!'
  5. Built-in Predicates - write, nl, halt are 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:

1
print("Hello, World!")

Java:

1
2
3
4
5
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Prolog:

1
:- write('Hello, World!'), nl, halt.

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!

Running Today

All examples can be run using Docker:

docker pull swipl:stable
Last updated: