Getting Started
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
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
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
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
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.
| Aspect | Haskell (haskell1-1) | Prolog |
|---|---|---|
| Program shape | function definitions, one entry point | a database of facts/rules, no fixed entry point |
| "Running" the program | executing main | posing a query |
| What a call/query does | evaluates to a single value | searches for zero, one, or many satisfying answers |
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.
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
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 solutionAdd 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 solutionWrite 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 solutionChapter 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