Your First Prolog Program
Writing your first real Prolog program means saving facts and rules into a .pl file, loading that file into the SWI-Prolog top-level, and then querying it interactively. Unlike scripting languages where you simply run a file to see printed output, a Prolog program is a passive knowledge base — nothing happens until you issue a query at the ?- prompt that asks the engine to search it for an answer.
Cricket analogy: A .pl file is like a team's compiled statistics dossier before a match — it doesn't 'do' anything on its own, but once the analyst poses a question at the press conference (a query), the dossier is searched for the answer, just as loading family.pl only becomes useful once you type ?- sibling(ann, pat).
Writing and Loading a File
Create a text file named family.pl containing your facts and rules, then load it from the SWI-Prolog top-level with ?- [family]. (square brackets are shorthand for consult/1) or ?- consult('family.pl').; SWI-Prolog will report each predicate it loaded, and any syntax errors — like a missing period — will be reported with the offending line number so you can fix them before querying. After editing the file, simply reload it the same way to pick up the changes.
Cricket analogy: Loading family.pl with ?- [family]. is like a team submitting its final playing XI to the match referee before the toss — once submitted (loaded), the umpires (the engine) can reference it for every subsequent decision (query) during the match.
Querying and Reading Results
At the ?- prompt, typing a query like ?- parent(tom, Who). returns the first matching solution, Who = liz, and pressing ; (semicolon) asks Prolog to backtrack and find the next solution, Who = bob, continuing until Prolog reports false. to mean no more solutions exist; typing a period (or simply pressing Enter after the first result) instead accepts that single answer and returns you to the prompt without searching further.
Cricket analogy: Querying ?- parent(tom, Who). and pressing ; to see more results is like asking a stats database 'who did Rohit Sharma get out?' and tapping 'next' repeatedly to cycle through every dismissal, until the database reports no more results remain.
% file: family.pl
parent(tom, liz).
parent(tom, bob).
parent(bob, ann).
parent(bob, pat).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
% --- interactive session ---
?- [family].
true.
?- parent(tom, Who).
Who = liz ;
Who = bob.
?- sibling(ann, Who).
Who = pat.
?- halt.SWI-Prolog's built-in editor integration lets you run ?- edit(family). to open family.pl directly in a text editor from within the REPL, and after saving, ?- make. reloads any files that changed on disk — a fast edit-reload-query loop that avoids retyping the consult command every time.
Forgetting the trailing period at the end of a clause or query is the single most common beginner error — Prolog treats a missing period as 'the statement isn't finished yet' and will simply wait for more input (shown as a blank continuation line) rather than giving an obvious error, which can be confusing the first time it happens.
- A Prolog program is a passive knowledge base; nothing executes until a query is issued.
- Load a file with ?- [filename]. or ?- consult('filename.pl').
- SWI-Prolog reports syntax errors with the offending line number during loading.
- Typing ; after a result asks Prolog to backtrack and find the next solution.
- A query ends when Prolog reports false. meaning no further solutions exist.
- ?- edit(file). opens a file for editing, and ?- make. reloads changed files.
- Every clause and query must end with a period, or Prolog will wait for more input.
Practice what you learned
1. How do you load a file named family.pl into the SWI-Prolog top-level?
2. What does pressing ; after a query result do?
3. What does Prolog report when a query has no more remaining solutions?
4. What is the most common syntax mistake beginners make when writing Prolog clauses?
5. Which command reloads a file that has changed on disk without retyping the full consult command?
Was this page helpful?
You May Also Like
What Is Prolog?
An introduction to Prolog as a declarative, logic-based programming language built on facts, rules, and queries resolved through unification and backtracking.
Facts and Rules in Prolog
How Prolog knowledge bases are built from facts and rules, including how conjunctions (AND) and multiple clauses (OR) combine to express logic.
Prolog Syntax and Terms
A tour of Prolog's core data representation: atoms, numbers, variables, compound terms, and the [Head|Tail] list notation.
Installing SWI-Prolog
A practical walkthrough of installing SWI-Prolog on Windows, macOS, and Linux, and verifying the installation from the interactive top-level.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics