Getting Started

Course 1 · Ch 1
Getting Started
No main, no function to call — a Prolog program is a database of true statements, and running it means asking it questions

Haskell's own course, just completed, spent sixteen chapters showing what "declarative" means when a language evaluates pure expressions to values. This course is the direct counterpoint: Prolog is also called declarative, but it means something genuinely different — describing relations that hold true, then asking the system to search for what satisfies them.

SWI-Prolog & the ?- REPL

$ swipl ?- 2 + 2 =:= 4. true.

SWI-Prolog is the standard modern implementation; swipl launches its interactive top-level. Similar in spirit to GHCi's own REPL — but every query here ends in a period, and the answers look nothing like a function's return value.

Facts — The Most Basic Unit

parent(tom, bob). parent(tom, liz).

A genuinely different starting point from haskell1-1's own function-first orientation. There's no function being defined here at all — parent(tom, bob). is a fact, a statement simply asserted as true and added permanently to Prolog's own database. A Prolog program begins as a collection of facts about relations, not a sequence of function definitions.

Querying — Asking Questions of the Database

?- parent(tom, bob). true. ?- parent(tom, X). X = bob.

Here's this chapter's central reveal: a query isn't "calling a function" — it's asking, literally, "is this true?" or "for what X is this true?" parent(tom, X) genuinely asks Prolog to search its own database and find a value for X that makes the statement hold. This is a fundamentally different orientation from every other language on this site, where calling something always means "run this code and give me back a value."

Multiple Facts, Multiple Answers

?- parent(tom, X). X = bob ; X = liz.

With two matching facts in the database, parent(tom, X) genuinely has two possible answers — typing ; at the prompt asks Prolog for the next one. A first, gentle preview of Chapter 4's own full backtracking treatment, without naming the underlying machinery yet.

No main, No Entry Point — A Genuinely Different Program Shape

Stated plainly: there's no equivalent to haskell1-1's own main :: IO () at this level at all. A Prolog "program" is its database of facts and rules — "running" it means posing queries against that database, not executing a fixed starting point. There's no single place execution begins the way there always was in every language covered before this one.

AspectHaskell (haskell1-1)Prolog
Program shapefunction definitions, one entry pointa database of facts/rules, no fixed entry point
"Running" the programexecuting mainposing a query
What a call/query doesevaluates to a single valuesearches for zero, one, or many satisfying answers
Think of a fact as a true statement, not stored data
parent(tom, bob). isn't "storing a value in a variable" the way an assignment would — it's adding a permanently-true statement to Prolog's own knowledge base, which every later query can then search against.
A forgotten period is a very common, very confusing first mistake
Every fact, rule, and query must end in a period. Forgetting it doesn't produce an immediate, obvious error — swipl simply sits waiting for more input, since it assumes the statement isn't finished yet. If the prompt seems stuck, check for a missing period first.

Coding Challenges

Challenge 1

Add three facts to Prolog's database describing which fruits are which colors (e.g. color(banana, yellow).), then query for the color of one specific fruit.

📄 View solution
Challenge 2

Add two facts asserting that two different fruits are both red, then run a single query that finds ALL red fruits, pressing ; to see each answer in turn.

📄 View solution
Challenge 3

Write a short comment explaining why "querying a Prolog database" and "calling a Haskell function" are fundamentally different operations, specifically addressing what happens when a query could have more than one true answer.

📄 View solution

Chapter 1 Quick Reference

  • swipl launches SWI-Prolog's interactive ?- top-level
  • A fact (parent(tom, bob).) is a statement asserted as permanently true, not a function definition or a variable assignment
  • A query asks "is this true?" or "for what X is this true?" — genuinely different from calling a function for its return value
  • A query can have zero, one, or many answers — ; requests the next one when more than one exists
  • There is no main and no fixed entry point — the program IS the database, and running it means querying it
  • Every fact, rule, and query ends in a period — a missing one leaves swipl waiting silently
  • Next chapter: facts and rules — building relations beyond plain facts